feat(core): bound v2 tool output (#30999)
This commit is contained in:
@@ -11,7 +11,6 @@ import { testEffect } from "./lib/effect"
|
||||
import { tmpdir } from "./fixture/tmpdir"
|
||||
|
||||
const sessionID = SessionV2.ID.make("ses_tool_output_store")
|
||||
const otherSessionID = SessionV2.ID.make("ses_tool_output_store_other")
|
||||
|
||||
const withStore = <A, E, R>(
|
||||
body: (input: { root: string; store: ToolOutputStore.Interface; fs: FSUtil.Interface }) => Effect.Effect<A, E, R>,
|
||||
@@ -44,140 +43,88 @@ const withStore = <A, E, R>(
|
||||
const it = testEffect(Layer.empty)
|
||||
|
||||
describe("ToolOutputStore", () => {
|
||||
it.live("returns under-limit text unchanged without writing a resource", () =>
|
||||
it.live("returns under-limit text unchanged without writing a file", () =>
|
||||
withStore(({ store }) =>
|
||||
Effect.gen(function* () {
|
||||
expect(yield* store.truncate({ sessionID, toolCallID: "call-short", content: "line one\nline two" })).toEqual({
|
||||
content: "line one\nline two",
|
||||
expect(yield* store.truncate({ sessionID, toolCallID: "call-short", content: "one\ntwo" })).toEqual({
|
||||
content: "one\ntwo",
|
||||
truncated: false,
|
||||
})
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("stores byte-truncated output and returns an opaque head-tail preview", () =>
|
||||
withStore(({ store }) =>
|
||||
it.live("stores full output at an absolute managed path", () =>
|
||||
withStore(({ root, store, fs }) =>
|
||||
Effect.gen(function* () {
|
||||
const content = "HEAD-" + "x".repeat(100) + "-TAIL"
|
||||
const result = yield* store.truncate({ sessionID, toolCallID: "call-bytes", content, maxBytes: 20 })
|
||||
|
||||
const content = "HEAD-" + "x".repeat(500) + "-TAIL"
|
||||
const result = yield* store.truncate({ sessionID, toolCallID: "call-large", content, maxBytes: 300 })
|
||||
expect(result.truncated).toBe(true)
|
||||
if (!result.truncated) throw new Error("expected truncation")
|
||||
expect(path.isAbsolute(result.outputPath)).toBe(true)
|
||||
expect(result.outputPath).toStartWith(path.join(root, "tool-output", "tool_"))
|
||||
expect(result.content).toContain(result.outputPath)
|
||||
expect(result.content).toContain("HEAD-")
|
||||
expect(result.content).toContain("-TAIL")
|
||||
expect(result.content).toContain("output truncated")
|
||||
expect(result.resource.uri).toMatch(/^tool-output:\/\/[0-9A-Za-z]+$/)
|
||||
expect(result.resource.uri.slice("tool-output://".length)).not.toContain("/")
|
||||
expect(result.resource.uri).not.toContain("\\")
|
||||
expect(result.resource).toMatchObject({ mime: "text/plain", size: Buffer.byteLength(content) })
|
||||
expect((yield* store.read({ sessionID, uri: result.resource.uri })).content).toBe(content)
|
||||
expect(yield* fs.readFileString(result.outputPath)).toBe(content)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("stores line-truncated output and keeps both ends in the preview", () =>
|
||||
withStore(({ store }) =>
|
||||
it.live("bounds aggregate text blocks with one managed file", () =>
|
||||
withStore(({ store, fs }) =>
|
||||
Effect.gen(function* () {
|
||||
const content = Array.from({ length: 10 }, (_, index) => `line-${index}`).join("\n")
|
||||
const result = yield* store.truncate({ sessionID, toolCallID: "call-lines", content, maxLines: 4 })
|
||||
|
||||
expect(result.truncated).toBe(true)
|
||||
if (!result.truncated) throw new Error("expected truncation")
|
||||
expect(result.content).toContain("line-0\nline-1")
|
||||
expect(result.content).toContain("line-8\nline-9")
|
||||
expect(result.content).not.toContain("line-4")
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("keeps one-line previews bounded", () =>
|
||||
withStore(({ store }) =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* store.truncate({
|
||||
const first = "HEAD-" + "x".repeat(30_000)
|
||||
const second = "y".repeat(30_000) + "-TAIL"
|
||||
const result = yield* store.bound({
|
||||
sessionID,
|
||||
toolCallID: "call-one-line",
|
||||
content: "one\ntwo\nthree",
|
||||
maxLines: 1,
|
||||
toolCallID: "call-aggregate",
|
||||
output: {
|
||||
structured: { kind: "report" },
|
||||
content: [
|
||||
{ type: "text", text: first },
|
||||
{ type: "text", text: second },
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.truncated).toBe(true)
|
||||
if (!result.truncated) throw new Error("expected truncation")
|
||||
const preview = result.content.split("\n\n... output truncated")[0]
|
||||
expect(preview).toBe("one")
|
||||
expect(result.output.structured).toEqual({ kind: "report" })
|
||||
expect(result.outputPaths).toHaveLength(1)
|
||||
expect(yield* fs.readFileString(result.outputPaths[0]!)).toBe(`${first}\n\n${second}`)
|
||||
if (result.output.content[0]?.type !== "text") throw new Error("expected text preview")
|
||||
expect(Buffer.byteLength(result.output.content[0].text)).toBeLessThanOrEqual(ToolOutputStore.MAX_BYTES)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("pages reads within the bounded managed-resource limit", () =>
|
||||
it.live("uses bounded text for oversized structured-only output", () =>
|
||||
withStore(({ store, fs }) =>
|
||||
Effect.gen(function* () {
|
||||
const structured = { text: "x".repeat(ToolOutputStore.MAX_BYTES) }
|
||||
const result = yield* store.bound({ sessionID, toolCallID: "call-json", output: { structured, content: [] } })
|
||||
expect(result.output.structured).toBe(structured)
|
||||
expect(result.outputPaths).toHaveLength(1)
|
||||
expect(yield* fs.readFileString(result.outputPaths[0]!)).toBe(JSON.stringify(structured))
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("degrades to lossy bounded output when writing fails", () =>
|
||||
withStore(({ root, store, fs }) =>
|
||||
Effect.gen(function* () {
|
||||
const resource = yield* store.write({
|
||||
yield* fs.writeFileString(path.join(root, "tool-output"), "not a directory")
|
||||
const result = yield* store.bound({
|
||||
sessionID,
|
||||
toolCallID: "call-page",
|
||||
content: "0123456789",
|
||||
name: "out.txt",
|
||||
toolCallID: "call-lossy",
|
||||
output: { structured: {}, content: [{ type: "text", text: "x".repeat(ToolOutputStore.MAX_BYTES + 1) }] },
|
||||
})
|
||||
const first = yield* store.read({ sessionID, uri: resource.uri, limit: 4 })
|
||||
const second = yield* store.read({ sessionID, uri: resource.uri, offset: first.next, limit: 4 })
|
||||
const last = yield* store.read({ sessionID, uri: resource.uri, offset: second.next, limit: 4 })
|
||||
|
||||
expect(first).toMatchObject({ content: "0123", offset: 0, truncated: true, next: 4 })
|
||||
expect(second).toMatchObject({ content: "4567", offset: 4, truncated: true, next: 8 })
|
||||
expect(last).toMatchObject({ content: "89", offset: 8, truncated: false })
|
||||
expect(last.resource).toEqual({ uri: resource.uri, mime: "text/plain", name: "out.txt", size: 10 })
|
||||
expect(
|
||||
JSON.parse(
|
||||
yield* fs.readFileString(
|
||||
path.join(root, "tool-output", "managed", `${resource.uri.slice("tool-output://".length)}.json`),
|
||||
),
|
||||
),
|
||||
).toMatchObject({
|
||||
sessionID,
|
||||
toolCallID: "call-page",
|
||||
})
|
||||
|
||||
const bounded = yield* store.read({
|
||||
sessionID,
|
||||
uri: (yield* store.write({
|
||||
sessionID,
|
||||
toolCallID: "call-bounded",
|
||||
content: "x".repeat(ToolOutputStore.MAX_READ_BYTES + 10),
|
||||
})).uri,
|
||||
limit: ToolOutputStore.MAX_READ_BYTES + 10,
|
||||
})
|
||||
expect(Buffer.byteLength(bounded.content)).toBe(ToolOutputStore.MAX_READ_BYTES)
|
||||
expect(bounded).toMatchObject({ truncated: true, next: ToolOutputStore.MAX_READ_BYTES })
|
||||
expect(result.outputPaths).toEqual([])
|
||||
if (result.output.content[0]?.type !== "text") throw new Error("expected text preview")
|
||||
expect(result.output.content[0].text).toContain("could not be retained")
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("allows the owning session and denies cross-session reads", () =>
|
||||
withStore(({ store }) =>
|
||||
Effect.gen(function* () {
|
||||
const resource = yield* store.write({ sessionID, toolCallID: "call-owned", content: "owned" })
|
||||
expect((yield* store.read({ sessionID, uri: resource.uri })).content).toBe("owned")
|
||||
expect(yield* Effect.flip(store.read({ sessionID: otherSessionID, uri: resource.uri }))).toBeInstanceOf(
|
||||
ToolOutputStore.AccessDeniedError,
|
||||
)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("rejects resources whose payload size no longer matches metadata", () =>
|
||||
withStore(({ root, store, fs }) =>
|
||||
Effect.gen(function* () {
|
||||
const resource = yield* store.write({ sessionID, toolCallID: "call-modified", content: "original" })
|
||||
const id = resource.uri.slice("tool-output://".length)
|
||||
yield* fs.writeFileString(path.join(root, "tool-output", "managed", `${id}.txt`), "changed payload")
|
||||
|
||||
expect(yield* Effect.flip(store.read({ sessionID, uri: resource.uri }))).toBeInstanceOf(
|
||||
ToolOutputStore.ResourceNotFoundError,
|
||||
)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("honors configured truncation limits", () =>
|
||||
it.live("honors configured limits", () =>
|
||||
withStore(
|
||||
({ store }) =>
|
||||
Effect.gen(function* () {
|
||||
@@ -190,75 +137,19 @@ describe("ToolOutputStore", () => {
|
||||
),
|
||||
)
|
||||
|
||||
it.live("cleans old managed resources while preserving recent and unrelated files", () =>
|
||||
it.live("cleans expired managed files and preserves unrelated files", () =>
|
||||
withStore(({ root, store, fs }) =>
|
||||
Effect.gen(function* () {
|
||||
const old = yield* store.write({ sessionID, toolCallID: "call-old", content: "old" })
|
||||
const recent = yield* store.write({ sessionID, toolCallID: "call-recent", content: "recent" })
|
||||
const directory = path.join(root, "tool-output", "managed")
|
||||
const oldID = old.uri.slice("tool-output://".length)
|
||||
const recentID = recent.uri.slice("tool-output://".length)
|
||||
const oldMetadata = path.join(directory, `${oldID}.json`)
|
||||
const unrelated = path.join(root, "tool-output", "unrelated.txt")
|
||||
const unrelatedManaged = path.join(directory, "unrelated.txt")
|
||||
const record = JSON.parse(yield* fs.readFileString(oldMetadata))
|
||||
|
||||
yield* fs.writeFileString(
|
||||
oldMetadata,
|
||||
JSON.stringify({ ...record, created: Date.now() - 8 * 24 * 60 * 60 * 1_000 }),
|
||||
)
|
||||
const old = yield* store.write({ sessionID, toolCallID: "old", content: "old" })
|
||||
const recent = yield* store.write({ sessionID, toolCallID: "recent", content: "recent" })
|
||||
const unrelated = path.join(root, "tool-output", "keep.txt")
|
||||
yield* fs.writeFileString(unrelated, "keep")
|
||||
yield* fs.writeFileString(unrelatedManaged, "keep")
|
||||
const expired = new Date(Date.now() - 8 * 24 * 60 * 60 * 1_000)
|
||||
yield* fs.utimes(old, expired, expired)
|
||||
yield* store.cleanup()
|
||||
|
||||
expect(yield* fs.exists(path.join(directory, `${oldID}.txt`))).toBe(false)
|
||||
expect(yield* fs.exists(oldMetadata)).toBe(false)
|
||||
expect(yield* fs.exists(path.join(directory, `${recentID}.txt`))).toBe(true)
|
||||
expect(yield* fs.exists(old)).toBe(false)
|
||||
expect(yield* fs.exists(recent)).toBe(true)
|
||||
expect(yield* fs.exists(unrelated)).toBe(true)
|
||||
expect(yield* fs.exists(unrelatedManaged)).toBe(true)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("cleans stale generated orphan payloads and malformed pairs", () =>
|
||||
withStore(({ root, store, fs }) =>
|
||||
Effect.gen(function* () {
|
||||
const directory = path.join(root, "tool-output", "managed")
|
||||
yield* fs.ensureDir(directory)
|
||||
const orphanID = "00000000000000000000000000"
|
||||
const malformedID = "00000000000000000000000001"
|
||||
const orphan = path.join(directory, `${orphanID}.txt`)
|
||||
const malformedPayload = path.join(directory, `${malformedID}.txt`)
|
||||
const malformedMetadata = path.join(directory, `${malformedID}.json`)
|
||||
yield* fs.writeFileString(orphan, "orphan")
|
||||
yield* fs.writeFileString(malformedPayload, "malformed")
|
||||
yield* fs.writeFileString(malformedMetadata, "not json")
|
||||
const old = new Date(Date.now() - 8 * 24 * 60 * 60 * 1_000)
|
||||
yield* Effect.all([fs.utimes(orphan, old, old), fs.utimes(malformedPayload, old, old)])
|
||||
|
||||
yield* store.cleanup()
|
||||
|
||||
expect(yield* fs.exists(orphan)).toBe(false)
|
||||
expect(yield* fs.exists(malformedPayload)).toBe(false)
|
||||
expect(yield* fs.exists(malformedMetadata)).toBe(false)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("cleans managed resources whose payload size no longer matches metadata", () =>
|
||||
withStore(({ root, store, fs }) =>
|
||||
Effect.gen(function* () {
|
||||
const resource = yield* store.write({ sessionID, toolCallID: "call-modified", content: "original" })
|
||||
const directory = path.join(root, "tool-output", "managed")
|
||||
const id = resource.uri.slice("tool-output://".length)
|
||||
const payload = path.join(directory, `${id}.txt`)
|
||||
const metadata = path.join(directory, `${id}.json`)
|
||||
yield* fs.writeFileString(payload, "changed payload")
|
||||
|
||||
yield* store.cleanup()
|
||||
|
||||
expect(yield* fs.exists(payload)).toBe(false)
|
||||
expect(yield* fs.exists(metadata)).toBe(false)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user