GeezerVison is showing github.com — fix: parseFrontmatter型バリデーション + repetition共有util抽出 by terisuke · Pull Request #70 · Cor-Incorporated/opencode
fix: parseFrontmatter型バリデーション + repetition共有util抽出
#70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.ts
(5)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
import path from "path"
import { Instance } from "@/project/instance"
import { Log } from "@/util/log"
import type { Memory } from "./types"
import { Memory } from "./types"
const log = Log.create({ service: "memory.file" })
Expand Down
Expand Up
@@ -35,6 +35,8 @@ function parseFrontmatter(raw: string): { frontmatter: Memory.Frontmatter; conte
fm[line.slice(0, idx).trim()] = line.slice(idx + 1).trim()
}
if (!fm.topic || !fm.type) return undefined
const validTypes: readonly string[] = Memory.TYPES
if (!validTypes.includes(fm.type)) return undefined
return {
frontmatter: {
topic: fm.topic,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -19,12 +19,10 @@ import { SessionStatus } from "./status"
import { SessionSummary } from "./summary"
import type { Provider } from "@/provider/provider"
import { Question } from "@/question"
import { detectRepetition, REPETITION_THRESHOLD } from "./repetition"
export namespace SessionProcessor {
const DOOM_LOOP_THRESHOLD = 3
// 50+ consecutive repeats of a 4-200 char pattern in the last 8KB indicates a model generation loop
const REPETITION_THRESHOLD = 50
const REPETITION_WINDOW = 8000
const log = Log.create({ service: "session.processor" })
class RepetitionError extends Error {
Expand All
@@ -34,24 +32,6 @@ export namespace SessionProcessor {
}
}
function detectRepetition(text: string): boolean {
if (text.length < REPETITION_WINDOW) return false
const tail = text.slice(-REPETITION_WINDOW)
for (let len = 4; len <= 200; len++) {
const pattern = tail.slice(-len)
let count = 0
let pos = tail.length - len
while (pos >= 0) {
if (tail.slice(pos, pos + len) === pattern) {
count++
pos -= len
} else break
}
if (count >= REPETITION_THRESHOLD) return true
}
return false
}
export type Result = "compact" | "stop" | "continue"
export type Event = LLM.Event
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,21 @@
// 50+ consecutive repeats of a 4-200 char pattern in the last 8KB indicates a model generation loop
export const REPETITION_THRESHOLD = 50
export const REPETITION_WINDOW = 8000
export function detectRepetition(text: string): boolean {
if (text.length < REPETITION_WINDOW) return false
const tail = text.slice(-REPETITION_WINDOW)
for (let len = 4; len <= 200; len++) {
const pattern = tail.slice(-len)
let count = 0
let pos = tail.length - len
while (pos >= 0) {
if (tail.slice(pos, pos + len) === pattern) {
count++
pos -= len
} else break
}
if (count >= REPETITION_THRESHOLD) return true
}
return false
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -3,6 +3,7 @@ import path from "path"
import fs from "fs/promises"
import { Instance } from "../../src/project/instance"
import { MemoryFile } from "../../src/memory/file"
import { Memory } from "../../src/memory/types"
import { tmpdir } from "../fixture/fixture"
describe("memory.file", () => {
Expand Down
Expand Up
@@ -193,4 +194,52 @@ describe("memory.file", () => {
},
})
})
test.each(Memory.TYPES.map((t) => [t]))("readEntry parses valid type: %s", async (validType) => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const dir = MemoryFile.getMemoryDir()
await fs.mkdir(dir, { recursive: true })
const raw = `---\ntopic: test\ntype: ${validType}\n---\nsome content`
await Bun.write(path.join(dir, "valid-type.md"), raw)
const read = await MemoryFile.readEntry("valid-type.md")
expect(read).toBeDefined()
expect(read!.frontmatter.type).toBe(validType)
expect(read!.frontmatter.topic).toBe("test")
expect(read!.content).toBe("some content")
},
})
})
test("readEntry returns undefined for invalid type in frontmatter", async () => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const dir = MemoryFile.getMemoryDir()
await fs.mkdir(dir, { recursive: true })
const raw = "---\ntopic: test\ntype: invalid-type\n---\nsome content"
await Bun.write(path.join(dir, "invalid-type.md"), raw)
const read = await MemoryFile.readEntry("invalid-type.md")
expect(read).toBeUndefined()
},
})
})
test("readEntry returns undefined for missing type in frontmatter", async () => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const dir = MemoryFile.getMemoryDir()
await fs.mkdir(dir, { recursive: true })
const raw = "---\ntopic: test\n---\nsome content"
await Bun.write(path.join(dir, "no-type.md"), raw)
const read = await MemoryFile.readEntry("no-type.md")
expect(read).toBeUndefined()
},
})
})
})
Original file line number
Diff line number
Diff line change
@@ -1,29 +1,6 @@
import { describe, test, expect } from "bun:test"
// detectRepetition is a namespace-internal function in SessionProcessor.
// We replicate the algorithm here to test it in isolation.
// Constants match processor.ts: REPETITION_THRESHOLD=50, REPETITION_WINDOW=8000
const REPETITION_THRESHOLD = 50
const REPETITION_WINDOW = 8000
function detectRepetition(text: string): boolean {
if (text.length < REPETITION_WINDOW) return false
const tail = text.slice(-REPETITION_WINDOW)
for (let len = 4; len <= 200; len++) {
const pattern = tail.slice(-len)
let count = 0
let pos = tail.length - len
while (pos >= 0) {
if (tail.slice(pos, pos + len) === pattern) {
count++
pos -= len
} else break
}
if (count >= REPETITION_THRESHOLD) return true
}
return false
}
import { detectRepetition } from "../../src/session/repetition"
import { REPETITION_THRESHOLD, REPETITION_WINDOW } from "../../src/session/repetition"
Comment thread
Comment on lines
+2
to
+3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
These two imports come from the same module; consider combining them into a single import to reduce redundancy and keep imports easier to maintain.
import { detectRepetition } from "../../src/session/repetition"
import { REPETITION_THRESHOLD , REPETITION_WINDOW } from "../../src/session/repetition"
import { detectRepetition , REPETITION_THRESHOLD , REPETITION_WINDOW } from "../../src/session/repetition"
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Copilot uses AI. Check for mistakes.
describe("session.detectRepetition", () => {
test("no repetition returns false", () => {
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.