Skip to content

Commit 810aaff

Browse files
committed
perf(session): cache messages across prompt loop to preserve prompt cache byte-identity
1 parent f80651f commit 810aaff

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

packages/app/vite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default [
1616
resolve: {
1717
alias: {
1818
"@": fileURLToPath(new URL("./src", import.meta.url)),
19+
"@opencode-ai/core": fileURLToPath(new URL("../core/src", import.meta.url)),
1920
},
2021
},
2122
worker: {

packages/opencode/src/session/prompt.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1647,12 +1647,23 @@ NOTE: At any point in time through this workflow you should feel free to ask the
16471647
let structured: unknown
16481648
let step = 0
16491649
const session = yield* sessions.get(sessionID).pipe(Effect.orDie)
1650+
let msgs: MessageV2.WithParts[] | undefined
1651+
let needsFullReload = true
16501652

16511653
while (true) {
16521654
yield* status.set(sessionID, { type: "busy" })
16531655
yield* slog.info("loop", { step })
16541656

1655-
let msgs = yield* MessageV2.filterCompactedEffect(sessionID)
1657+
if (needsFullReload || !msgs) {
1658+
msgs = yield* MessageV2.filterCompactedEffect(sessionID)
1659+
needsFullReload = false
1660+
} else {
1661+
const fresh = yield* MessageV2.filterCompactedEffect(sessionID)
1662+
const knownIDs = new Set(msgs.map((m) => m.info.id))
1663+
for (const m of fresh) {
1664+
if (!knownIDs.has(m.info.id)) msgs.push(m)
1665+
}
1666+
}
16561667

16571668
const { user: lastUser, assistant: lastAssistant, finished: lastFinished, tasks } = MessageV2.latest(msgs)
16581669

@@ -1692,6 +1703,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
16921703

16931704
if (task?.type === "subtask") {
16941705
yield* handleSubtask({ task, model, lastUser, sessionID, session, msgs })
1706+
needsFullReload = true
16951707
continue
16961708
}
16971709

@@ -1704,6 +1716,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
17041716
overflow: task.overflow,
17051717
})
17061718
if (result === "stop") break
1719+
needsFullReload = true
17071720
continue
17081721
}
17091722

@@ -1713,6 +1726,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
17131726
(yield* compaction.isOverflow({ tokens: lastFinished.tokens, model }))
17141727
) {
17151728
yield* compaction.create({ sessionID, agent: lastUser.agent, model: lastUser.model, auto: true })
1729+
needsFullReload = true
17161730
continue
17171731
}
17181732

@@ -1859,6 +1873,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
18591873
auto: true,
18601874
overflow: !handle.message.finish,
18611875
})
1876+
needsFullReload = true
18621877
}
18631878
return "continue" as const
18641879
}).pipe(

0 commit comments

Comments
 (0)