GeezerVison is showing github.com — feat(desktop): the goal loop's 600K compaction is a logged backstop · garysomerhalder/opencode@a44f376
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/desktop/src/main/goal-loop.test.ts +94-1Lines changed: 94 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -658,7 +658,14 @@ type TurnPlan = { reply: Reply; busyFor?: number; startAfter?: number } | 658 | 658 | // posted mid-turn sorts after it. | 659 | 659 | // `violations` counts prompts the loop sent while a turn was still queued or | 660 | 660 | // running, i.e. a double-send into a busy session. | 661 | | -function fakeServer(sessionID: string, plan: (index: number) => TurnPlan) { | | 661 | +// `serverCompactAt` models the server's own threshold (autonomous turns compact at | | 662 | +// 150K by default): a turn that finishes at or above it is followed, in the same | | 663 | +// turn, by a compaction message and its summary, as the real loop writes them. | | 664 | +function fakeServer( | | 665 | + sessionID: string, | | 666 | + plan: (index: number) => TurnPlan, | | 667 | + options: { serverCompactAt?: number; onServerCompact?: () => void } = {}, | | 668 | +) { | 662 | 669 | const messages: unknown[] = [] | 663 | 670 | const queue: Turn[] = [] | 664 | 671 | const calls: string[] = [] | @@ -673,6 +680,7 @@ function fakeServer(sessionID: string, plan: (index: number) => TurnPlan) { | 673 | 680 | // status polls the session stays busy after a summarize | 674 | 681 | summaryBusy: 0, | 675 | 682 | summaries: [] as { body: unknown; beforePrompt: number }[], | | 683 | + serverCompactions: 0, | 676 | 684 | } | 677 | 685 |
| 678 | 686 | function admit(text: string, turn: TurnPlan) { | @@ -700,6 +708,25 @@ function fakeServer(sessionID: string, plan: (index: number) => TurnPlan) { | 700 | 708 | turn.message.info["providerID"] = "opencode-go" | 701 | 709 | turn.message.info["modelID"] = "muse-spark-1.3-contributor" | 702 | 710 | turn.message.parts.push({ type: "text", text: turn.reply.text }) | | 711 | + if (options.serverCompactAt !== undefined && (turn.reply.tokens ?? 0) >= options.serverCompactAt) { | | 712 | + state.serverCompactions += 1 | | 713 | + messages.push({ | | 714 | + info: { id: `msg_${String(++state.ids).padStart(4, "0")}`, role: "user" }, | | 715 | + parts: [{ type: "compaction", auto: true }], | | 716 | + }) | | 717 | + messages.push({ | | 718 | + info: { | | 719 | + id: `msg_${String(++state.ids).padStart(4, "0")}`, | | 720 | + role: "assistant", | | 721 | + summary: true, | | 722 | + tokens: tokens(turn.reply.tokens ?? 0), | | 723 | + providerID: "opencode-go", | | 724 | + modelID: "muse-spark-1.3-contributor", | | 725 | + }, | | 726 | + parts: [{ type: "text", text: "server summary" }], | | 727 | + }) | | 728 | + options.onServerCompact?.() | | 729 | + } | 703 | 730 | } | 704 | 731 |
| 705 | 732 | function poll(): boolean { | @@ -787,6 +814,7 @@ function fakeServer(sessionID: string, plan: (index: number) => TurnPlan) { | 787 | 814 | }, | 788 | 815 | external: admit, | 789 | 816 | summaries: () => state.summaries, | | 817 | + serverCompactions: () => state.serverCompactions, | 790 | 818 | // history of a reused session: one finished assistant turn of `total` tokens | 791 | 819 | seed: (total: number) => { | 792 | 820 | messages.push({ info: { id: `msg_${String(++state.ids).padStart(4, "0")}`, role: "user" }, parts: [] }) | @@ -1193,10 +1221,12 @@ describe("goal loop failed turns and context size", () => { | 1193 | 1221 | : { reply: { text: "done\nGOAL_COMPLETE", tokens: 90_000 } }, | 1194 | 1222 | ) | 1195 | 1223 | const model = { providerID: "opencode-go", modelID: "muse-spark-1.3-contributor" } | | 1224 | + const warnings: { message: string; detail: Record<string, unknown> }[] = [] | 1196 | 1225 | const loop = createGoalLoop({ | 1197 | 1226 | getServer: async () => server, | 1198 | 1227 | fetchImpl: fake.fetchImpl, | 1199 | 1228 | onEvent: (e) => events.push(e), | | 1229 | + warn: (message, detail) => warnings.push({ message, detail }), | 1200 | 1230 | pollIntervalMs: 2, | 1201 | 1231 | maxConsecutiveErrors: 1, | 1202 | 1232 | }) | @@ -1208,10 +1238,73 @@ describe("goal loop failed turns and context size", () => { | 1208 | 1238 | // the summary turn is not a loop turn: one continue, and not while busy | 1209 | 1239 | expect(fake.prompts()).toBe(2) | 1210 | 1240 | expect(fake.violations()).toBe(0) | | 1241 | + // the backstop fired, so it says why it had to: the server's own threshold did not | | 1242 | + expect(warnings).toHaveLength(1) | | 1243 | + expect(warnings[0]!.message).toContain("server's compaction threshold") | | 1244 | + expect(warnings[0]!.detail).toMatchObject({ tokens: 700_000, backstopAt: 600_000 }) | 1211 | 1245 | }, | 1212 | 1246 | SLOW_HOST_MS + 5_000, | 1213 | 1247 | ) | 1214 | 1248 |
| | 1249 | + // The server compacts autonomous turns at 150K by default (overflow.ts | | 1250 | + // AUTONOMOUS_COMPACT_AT, tested server-side in accuracy-loop.test.ts). With the | | 1251 | + // server doing that, the goal loop's 600K backstop never fires; the control | | 1252 | + // arm, with the server's compaction off, shows the same run does reach it. | | 1253 | + const growingSession = (serverCompactAt: number | undefined) => { | | 1254 | + let context = 0 | | 1255 | + const fake = fakeServer( | | 1256 | + "ses_growing", | | 1257 | + (index) => { | | 1258 | + context += 60_000 | | 1259 | + // the last turn stays small so no compaction summary hides its completion marker | | 1260 | + return index === 11 | | 1261 | + ? { reply: { text: "done\nGOAL_COMPLETE", tokens: 20_000 } } | | 1262 | + : { reply: { text: "working", tokens: context } } | | 1263 | + }, | | 1264 | + // after a server compaction the session carries on from ~110K | | 1265 | + { serverCompactAt, onServerCompact: () => (context = 110_000) }, | | 1266 | + ) | | 1267 | + return fake | | 1268 | + } | | 1269 | + | | 1270 | + for (const arm of [ | | 1271 | + { name: "with the server compacting at its 150K default, the 600K backstop never fires", at: 150_000 }, | | 1272 | + { name: "control: with the server's compaction off, the same run reaches the backstop", at: undefined }, | | 1273 | + ]) | | 1274 | + test( | | 1275 | + arm.name, | | 1276 | + async () => { | | 1277 | + const events: GoalLoopEvent[] = [] | | 1278 | + const warnings: string[] = [] | | 1279 | + const fake = growingSession(arm.at) | | 1280 | + const loop = createGoalLoop({ | | 1281 | + getServer: async () => server, | | 1282 | + fetchImpl: fake.fetchImpl, | | 1283 | + onEvent: (e) => events.push(e), | | 1284 | + warn: (message) => warnings.push(message), | | 1285 | + pollIntervalMs: 2, | | 1286 | + maxConsecutiveErrors: 1, | | 1287 | + }) | | 1288 | + await loop.start({ | | 1289 | + directory: "/repo", | | 1290 | + goal: "long autonomous run", | | 1291 | + model: { providerID: "opencode-go", modelID: "muse-spark-1.3-contributor" }, | | 1292 | + }) | | 1293 | + await waitFor(() => loop.status() === null, SLOW_HOST_MS) | | 1294 | + expect(events.at(-1)?.type).toBe("completed") | | 1295 | + if (arm.at !== undefined) { | | 1296 | + expect(fake.serverCompactions()).toBeGreaterThan(0) | | 1297 | + expect(fake.summaries()).toHaveLength(0) | | 1298 | + expect(warnings).toHaveLength(0) | | 1299 | + } else { | | 1300 | + expect(fake.serverCompactions()).toBe(0) | | 1301 | + expect(fake.summaries().length).toBeGreaterThan(0) | | 1302 | + expect(warnings.length).toBe(fake.summaries().length) | | 1303 | + } | | 1304 | + }, | | 1305 | + SLOW_HOST_MS + 5_000, | | 1306 | + ) | | 1307 | + | 1215 | 1308 | test( | 1216 | 1309 | "a non-retryable 400 on a large context compacts once and recovers", | 1217 | 1310 | async () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/desktop/src/main/goal-loop.ts +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,8 @@ export type GoalLoopServer = { | 27 | 27 | export type GoalLoopDeps = { | 28 | 28 | getServer: () => Promise<GoalLoopServer> | 29 | 29 | fetchImpl?: typeof fetch | | 30 | + /** One-line warnings for the log (e.g. when the compaction backstop has to fire). */ | | 31 | + warn?: (message: string, detail: Record<string, unknown>) => void | 30 | 32 | now?: () => number | 31 | 33 | randomID?: () => string | 32 | 34 | onEvent?: (event: GoalLoopEvent) => void | @@ -489,6 +491,13 @@ export function createGoalLoop(deps: GoalLoopDeps) { | 489 | 491 | const context = contextOf(messages) | 490 | 492 | if (context && context.tokens > compactAtTokens && context.id !== track.compactedFor) { | 491 | 493 | track.compactedFor = context.id | | 494 | + // A backstop only: goal-loop turns are autonomous, and the server compacts | | 495 | + // those at its own threshold (150K by default) long before this. Reaching | | 496 | + // it means that threshold is off or set too high, so say so. | | 497 | + deps.warn?.( | | 498 | + "goal loop compacted the session itself: the server's compaction threshold did not (is experimental.accuracy.autonomous_compact_at off, or compaction.threshold set above the backstop?)", | | 499 | + { sessionID, tokens: context.tokens, backstopAt: compactAtTokens }, | | 500 | + ) | 492 | 501 | await compact(messages, `compacted the session at ${context.tokens} tokens`) | 493 | 502 | if (!alive()) return | 494 | 503 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/desktop/src/main/index.ts +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -410,7 +410,12 @@ const main = Effect.gen(function* () { | 410 | 410 | } | 411 | 411 | const goalLoopStore = getStore(GOAL_LOOP_STORE) as unknown as KeyValueStore | 412 | 412 | const goalLoops = createGoalLoops({ | 413 | | - create: (hooks) => createGoalLoop({ getServer: () => Effect.runPromise(Deferred.await(serverReady)), ...hooks }), | | 413 | + create: (hooks) => | | 414 | + createGoalLoop({ | | 415 | + getServer: () => Effect.runPromise(Deferred.await(serverReady)), | | 416 | + warn: (message, detail) => writeLog("main", message, detail, "warn"), | | 417 | + ...hooks, | | 418 | + }), | 414 | 419 | persist: (sessionID, state) => saveState(goalLoopStore, sessionID, state), | 415 | 420 | persistLast: (sessionID, input) => saveLast(goalLoopStore, sessionID, input), | 416 | 421 | onEvent: (event) => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/session/accuracy-loop.test.ts +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -919,6 +919,41 @@ it.instance( | 919 | 919 | 60_000, | 920 | 920 | ) | 921 | 921 |
| | 922 | +// The server half of the goal loop's backstop test (goal-loop.test.ts): with no | | 923 | +// compaction settings at all, an autonomous turn on a 1M-window model compacts at | | 924 | +// the 150K default, long before the desktop's 600K backstop. | | 925 | +it.instance( | | 926 | + "with no settings, an autonomous turn on a 1M-window model compacts at the 150K default", | | 927 | + () => | | 928 | + Effect.gen(function* () { | | 929 | + const { llm } = yield* useConfig(undefined, { | | 930 | + provider: { | | 931 | + test: { | | 932 | + ...provider.test, | | 933 | + models: { | | 934 | + "test-model": { ...provider.test.models["test-model"], limit: { context: 1_048_576, output: 131_072 } }, | | 935 | + }, | | 936 | + options: { ...provider.test.options, baseURL: (yield* TestLLMServer).url }, | | 937 | + }, | | 938 | + }, | | 939 | + }) | | 940 | + const prompt = yield* SessionPrompt.Service | | 941 | + const chat = yield* session() | | 942 | + yield* llm.push(reply().tool("glob", { pattern: "*.none" }).usage({ input: 160_000, output: 10 })) | | 943 | + yield* llm.text("summary") | | 944 | + yield* llm.text("done") | | 945 | + yield* prompt.prompt({ | | 946 | + sessionID: chat.id, | | 947 | + agent: "build", | | 948 | + autonomous: true, | | 949 | + parts: [{ type: "text", text: "hi" }], | | 950 | + }) | | 951 | + expect(yield* llm.hits).toHaveLength(3) | | 952 | + expect(yield* compactions(chat.id)).toHaveLength(1) | | 953 | + }), | | 954 | + 60_000, | | 955 | +) | | 956 | + | 922 | 957 | // D. compaction checkpoints (#6, docs/accuracy-d.md) | 923 | 958 |
| 924 | 959 | const withTodos = Effect.fn("test.withTodos")(function* (sessionID: string) { | |
|