Skip to content

Commit cc485d3

Browse files
terisukeclaude
andcommitted
fix(memory): validate frontmatter type against Memory.TYPES
parseFrontmatter() now checks fm.type is a valid Memory.Type value before casting. Invalid types return undefined instead of silently propagating through the system. Closes #69 (partial) Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 7439e20 commit cc485d3

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎packages/opencode/src/memory/file.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from "path"
22
import { Instance } from "@/project/instance"
33
import { Log } from "@/util/log"
4-
import type { Memory } from "./types"
4+
import { Memory } from "./types"
55

66
const log = Log.create({ service: "memory.file" })
77

@@ -35,6 +35,8 @@ function parseFrontmatter(raw: string): { frontmatter: Memory.Frontmatter; conte
3535
fm[line.slice(0, idx).trim()] = line.slice(idx + 1).trim()
3636
}
3737
if (!fm.topic || !fm.type) return undefined
38+
const validTypes: readonly string[] = Memory.TYPES
39+
if (!validTypes.includes(fm.type)) return undefined
3840
return {
3941
frontmatter: {
4042
topic: fm.topic,

‎packages/opencode/test/memory/file.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "path"
33
import fs from "fs/promises"
44
import { Instance } from "../../src/project/instance"
55
import { MemoryFile } from "../../src/memory/file"
6+
import { Memory } from "../../src/memory/types"
67
import { tmpdir } from "../fixture/fixture"
78

89
describe("memory.file", () => {
@@ -193,4 +194,52 @@ describe("memory.file", () => {
193194
},
194195
})
195196
})
197+
198+
test.each(Memory.TYPES.map((t) => [t]))("readEntry parses valid type: %s", async (validType) => {
199+
await using tmp = await tmpdir()
200+
await Instance.provide({
201+
directory: tmp.path,
202+
fn: async () => {
203+
const dir = MemoryFile.getMemoryDir()
204+
await fs.mkdir(dir, { recursive: true })
205+
const raw = `---\ntopic: test\ntype: ${validType}\n---\nsome content`
206+
await Bun.write(path.join(dir, "valid-type.md"), raw)
207+
const read = await MemoryFile.readEntry("valid-type.md")
208+
expect(read).toBeDefined()
209+
expect(read!.frontmatter.type).toBe(validType)
210+
expect(read!.frontmatter.topic).toBe("test")
211+
expect(read!.content).toBe("some content")
212+
},
213+
})
214+
})
215+
216+
test("readEntry returns undefined for invalid type in frontmatter", async () => {
217+
await using tmp = await tmpdir()
218+
await Instance.provide({
219+
directory: tmp.path,
220+
fn: async () => {
221+
const dir = MemoryFile.getMemoryDir()
222+
await fs.mkdir(dir, { recursive: true })
223+
const raw = "---\ntopic: test\ntype: invalid-type\n---\nsome content"
224+
await Bun.write(path.join(dir, "invalid-type.md"), raw)
225+
const read = await MemoryFile.readEntry("invalid-type.md")
226+
expect(read).toBeUndefined()
227+
},
228+
})
229+
})
230+
231+
test("readEntry returns undefined for missing type in frontmatter", async () => {
232+
await using tmp = await tmpdir()
233+
await Instance.provide({
234+
directory: tmp.path,
235+
fn: async () => {
236+
const dir = MemoryFile.getMemoryDir()
237+
await fs.mkdir(dir, { recursive: true })
238+
const raw = "---\ntopic: test\n---\nsome content"
239+
await Bun.write(path.join(dir, "no-type.md"), raw)
240+
const read = await MemoryFile.readEntry("no-type.md")
241+
expect(read).toBeUndefined()
242+
},
243+
})
244+
})
196245
})

0 commit comments

Comments
 (0)