GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(desktop): the goal loop's 600K compaction is a logged backstop · garysomerhalder/opencode@a44f376

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • desktop/src/main
      • goal-loop.test.ts
      • goal-loop.ts
      • index.ts
    • opencode/test/session
      • accuracy-loop.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/desktop/src/main/goal-loop.test.ts‎

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 numberDiff line numberDiff line change
@@ -658,7 +658,14 @@ type TurnPlan = { reply: Reply; busyFor?: number; startAfter?: number }
658658
// posted mid-turn sorts after it.
659659
// `violations` counts prompts the loop sent while a turn was still queued or
660660
// 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+
) {
662669
const messages: unknown[] = []
663670
const queue: Turn[] = []
664671
const calls: string[] = []
@@ -673,6 +680,7 @@ function fakeServer(sessionID: string, plan: (index: number) => TurnPlan) {
673680
// status polls the session stays busy after a summarize
674681
summaryBusy: 0,
675682
summaries: [] as { body: unknown; beforePrompt: number }[],
683+
serverCompactions: 0,
676684
}
677685

678686
function admit(text: string, turn: TurnPlan) {
@@ -700,6 +708,25 @@ function fakeServer(sessionID: string, plan: (index: number) => TurnPlan) {
700708
turn.message.info["providerID"] = "opencode-go"
701709
turn.message.info["modelID"] = "muse-spark-1.3-contributor"
702710
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+
}
703730
}
704731

705732
function poll(): boolean {
@@ -787,6 +814,7 @@ function fakeServer(sessionID: string, plan: (index: number) => TurnPlan) {
787814
},
788815
external: admit,
789816
summaries: () => state.summaries,
817+
serverCompactions: () => state.serverCompactions,
790818
// history of a reused session: one finished assistant turn of `total` tokens
791819
seed: (total: number) => {
792820
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", () => {
11931221
: { reply: { text: "done\nGOAL_COMPLETE", tokens: 90_000 } },
11941222
)
11951223
const model = { providerID: "opencode-go", modelID: "muse-spark-1.3-contributor" }
1224+
const warnings: { message: string; detail: Record<string, unknown> }[] = []
11961225
const loop = createGoalLoop({
11971226
getServer: async () => server,
11981227
fetchImpl: fake.fetchImpl,
11991228
onEvent: (e) => events.push(e),
1229+
warn: (message, detail) => warnings.push({ message, detail }),
12001230
pollIntervalMs: 2,
12011231
maxConsecutiveErrors: 1,
12021232
})
@@ -1208,10 +1238,73 @@ describe("goal loop failed turns and context size", () => {
12081238
// the summary turn is not a loop turn: one continue, and not while busy
12091239
expect(fake.prompts()).toBe(2)
12101240
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 })
12111245
},
12121246
SLOW_HOST_MS + 5_000,
12131247
)
12141248

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+
12151308
test(
12161309
"a non-retryable 400 on a large context compacts once and recovers",
12171310
async () => {
Collapse file

‎packages/desktop/src/main/goal-loop.ts‎

Copy file name to clipboardExpand all lines: packages/desktop/src/main/goal-loop.ts
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export type GoalLoopServer = {
2727
export type GoalLoopDeps = {
2828
getServer: () => Promise<GoalLoopServer>
2929
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
3032
now?: () => number
3133
randomID?: () => string
3234
onEvent?: (event: GoalLoopEvent) => void
@@ -489,6 +491,13 @@ export function createGoalLoop(deps: GoalLoopDeps) {
489491
const context = contextOf(messages)
490492
if (context && context.tokens > compactAtTokens && context.id !== track.compactedFor) {
491493
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+
)
492501
await compact(messages, `compacted the session at ${context.tokens} tokens`)
493502
if (!alive()) return
494503
}
Collapse file

‎packages/desktop/src/main/index.ts‎

Copy file name to clipboardExpand all lines: packages/desktop/src/main/index.ts
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,12 @@ const main = Effect.gen(function* () {
410410
}
411411
const goalLoopStore = getStore(GOAL_LOOP_STORE) as unknown as KeyValueStore
412412
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+
}),
414419
persist: (sessionID, state) => saveState(goalLoopStore, sessionID, state),
415420
persistLast: (sessionID, input) => saveLast(goalLoopStore, sessionID, input),
416421
onEvent: (event) => {
Collapse file

‎packages/opencode/test/session/accuracy-loop.test.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/test/session/accuracy-loop.test.ts
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,41 @@ it.instance(
919919
60_000,
920920
)
921921

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+
922957
// D. compaction checkpoints (#6, docs/accuracy-d.md)
923958

924959
const withTodos = Effect.fn("test.withTodos")(function* (sessionID: string) {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.