|
1 | 1 | import { afterEach, describe, expect, mock, test } from "bun:test" |
2 | 2 | import { ConfigV1 } from "@opencode-ai/core/v1/config/config" |
3 | 3 | import { SessionV1 } from "@opencode-ai/core/v1/session" |
| 4 | +import { HarnessNote } from "../../src/session/harness-note" |
4 | 5 | import { Database } from "@opencode-ai/core/database/database" |
5 | 6 | import { EventV2Bridge } from "@/event-v2-bridge" |
6 | 7 | import { APICallError } from "ai" |
@@ -1825,6 +1826,43 @@ describe("session.compaction.process", () => { |
1825 | 1826 | { git: true }, |
1826 | 1827 | ) |
1827 | 1828 |
|
| 1829 | + itCompaction.instance("a harness note does not count as a turn when the recent tail is kept", () => { |
| 1830 | + const stub = llm() |
| 1831 | + stub.push(reply("summary")) |
| 1832 | + |
| 1833 | + return Effect.gen(function* () { |
| 1834 | + const ssn = yield* SessionNs.Service |
| 1835 | + const session = yield* ssn.create({}) |
| 1836 | + yield* createUserMessage(session.id, "one") |
| 1837 | + yield* createUserMessage(session.id, "two") |
| 1838 | + const u3 = yield* createUserMessage(session.id, "three") |
| 1839 | + // a reminder the harness injected into turn three (accuracy A), not a turn of its own |
| 1840 | + const note = HarnessNote.build({ |
| 1841 | + user: u3 as SessionV1.User, |
| 1842 | + kind: "runaway_guard", |
| 1843 | + text: "<system-reminder>change approach</system-reminder>", |
| 1844 | + }) |
| 1845 | + yield* ssn.updateMessage(note.info) |
| 1846 | + yield* ssn.updatePart(note.part) |
| 1847 | + const u4 = yield* createUserMessage(session.id, "four") |
| 1848 | + yield* createCompactionMarker(session.id) |
| 1849 | + |
| 1850 | + const msgs = yield* ssn.messages({ sessionID: session.id }) |
| 1851 | + yield* SessionCompaction.use.process({ |
| 1852 | + parentID: msgs.at(-1)!.info.id, |
| 1853 | + messages: msgs, |
| 1854 | + sessionID: session.id, |
| 1855 | + auto: false, |
| 1856 | + }) |
| 1857 | + |
| 1858 | + // the two recent turns are three (with its note) and four |
| 1859 | + const ids = MessageV2.filterCompacted(yield* MessageV2.stream(session.id)).map((msg) => msg.info.id) |
| 1860 | + expect(ids).toContain(u3.id) |
| 1861 | + expect(ids).toContain(note.info.id) |
| 1862 | + expect(ids).toContain(u4.id) |
| 1863 | + }).pipe(withCompaction({ llm: stub.llmLayer, config: cfg({ tail_turns: 2, preserve_recent_tokens: 10_000 }) })) |
| 1864 | + }) |
| 1865 | + |
1828 | 1866 | itCompaction.instance("keeps recent pre-compaction turns across repeated compactions", () => { |
1829 | 1867 | const stub = llm() |
1830 | 1868 | stub.push(reply("summary one")) |
|