Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(compact): a /compact after a run's compaction is still a layer

purple2ee363d2117e
diff --git a/src/lib/__tests__/compact-layers.test.ts b/src/lib/__tests__/compact-layers.test.ts
index 34638980..54e7a11b 100644
--- a/src/lib/__tests__/compact-layers.test.ts
+++ b/src/lib/__tests__/compact-layers.test.ts
@@ -116,6 +116,23 @@ describe('a compaction in the middle of an Agent or Code run', () => {
})
})
+describe('a /compact after a compaction in the middle of a run', () => {
+ // Found on the web run of 28.10.: the record keeps the newest messages
+ // verbatim, its anchor lies before the run's cut, and it was dropped, so
+ // the /compact left no line and no layer.
+ const run = msg('a2', { agentBlocks: [cutBlock('k1', 50)] })
+ const messages = [msg('u1'), msg('a1'), msg('u2'), run, msg('n1', { role: 'system', notice: 'info', content: '/compact' })]
+ const rec = record({ id: 'r9', upToMessageId: 'a1', endMessageId: 'n1', at: 200 })
+
+ it('is still a layer, and the view after it does not run backwards', () => {
+ const cuts = compactCuts(messages, [rec])
+ expect(cuts.map((c) => c.key)).toEqual(['k1', 'r9'])
+ expect(ids(layerItems(messages, cuts, null))).toEqual(['|r9|', 'a2', 'n1'])
+ expect(ids(layerItems(messages, cuts, 1))).toEqual(['|k1|', 'a2', 'n1'])
+ expect(ids(layerItems(messages, cuts, 0))).toEqual(['u1', 'a1', 'u2'])
+ })
+})
+
describe('nothing is lost', () => {
// The rule the 2.6.8 guard protected ("a compaction takes nothing away from
// the transcript") in its 3.0.9 form: every stored message, and every step
diff --git a/src/lib/compact-layers.ts b/src/lib/compact-layers.ts
index 74f7b574..f1e13a68 100644
--- a/src/lib/compact-layers.ts
+++ b/src/lib/compact-layers.ts
@@ -111,15 +111,21 @@ export function compactCuts(
})
})
- // In the order they happened. A cut that would start before the one it
- // follows (data from a hand-edited or imported chat) cannot make a layer
- // and is dropped instead of producing a view that runs backwards.
+ // In the order they happened. A view never runs backwards: a per-turn
+ // record keeps the newest messages verbatim, so its anchor can lie before
+ // the start of the view it follows (a /compact after a compaction in the
+ // middle of a run, Mac run 28.10.). The model then reads the summary and
+ // those kept messages, and the view after it starts where the one before
+ // started, with the new line on top. Its layer ends no earlier than that.
cuts.sort((a, b) => a.at - b.at)
const out: CompactCut[] = []
for (const cut of cuts) {
const prev = out[out.length - 1]
- if (prev && before(cut.start, prev.start)) continue
- out.push(cut)
+ if (prev && before(cut.start, prev.start)) {
+ out.push({ ...cut, start: prev.start, end: before(cut.end, prev.start) ? prev.start : cut.end })
+ } else {
+ out.push(cut)
+ }
}
return out
}