Repository navigation
Expand file tree
/
Copy pathplugin.test.ts
More file actions
633 lines (597 loc) · 25.8 KB
/
Copy pathplugin.test.ts
File metadata and controls
633 lines (597 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import { expect, test } from "bun:test"
import { Media, Message } from "@opencode/ai"
import type { Context } from "@opencode/plugin/promise/plugin"
import type { SessionContext as SessionContextHook } from "@opencode/plugin/promise/session"
import type { Info, ToolContext } from "@opencode/plugin/promise/tool"
import plugin from "./index"
const prose = `First episode begins. ${"Detailed findings from the search. ".repeat(40)} First episode ends.`
const second = prose.replaceAll("First", "Second")
const original = () => [
Message.make({ id: "u1", role: "user", content: "Find the commit" }),
Message.make({ id: "a1", role: "assistant", content: `${prose}\n${second}` }),
]
const firstInput = { start: "First episode begins.", end: "First episode ends.", summary: "First finding." }
const secondInput = {
start: "Second episode begins.",
end: "Second episode ends.",
summary: "Second finding.",
}
const nextMessages = () => [...original(), Message.make({ id: "u2", role: "user", content: "Next question" })]
const continuation = (calls: string[]) => [
...original(),
Message.make({
id: "current",
role: "assistant",
content: calls.map((id) => ({ type: "tool-call" as const, id, name: "fold", input: {} })),
}),
Message.make({
role: "tool",
content: calls.map((id) => ({
type: "tool-result" as const,
id,
name: "fold",
result: { type: "text" as const, value: "queued" },
})),
}),
]
async function harness(
storage = new Map<string, unknown>(),
options: Record<string, unknown> = { debug: false },
) {
const tools = new Map<string, Info>()
const hooks = new Map<string, (event: SessionContextHook) => void | Promise<void>>()
let failWrites = false
let failNotices = false
let durable: unknown[] = [{ type: "user", id: "u1" }]
const notices: string[] = []
const events: { name: string; data: unknown }[] = []
let foldAvailable = true
const cleanup = await plugin.setup({
options,
location: { directory: "/unused" },
storage: {
get: async (key: string) => storage.get(key),
set: async (key: string, value: unknown) => {
if (failWrites) throw new Error("Simulated storage failure")
storage.set(key, structuredClone(value))
},
},
tool: {
transform: async (callback: (editor: { add: (tool: Info) => void }) => void) =>
callback({ add: (tool) => tools.set(tool.name, tool) }),
},
rpc: {
register: async () => ({
events: {
emit: async (name: string, data: unknown) => {
events.push({ name, data })
},
},
dispose: async () => {},
}),
},
session: {
context: async () => durable,
synthetic: async ({ text }: { text: string }) => {
if (failNotices) throw new Error("Simulated notice failure")
notices.push(text)
},
hook: async (name: string, callback: (event: SessionContextHook) => void | Promise<void>) => {
hooks.set(name, callback)
},
},
} as unknown as Context)
return {
storage,
cleanup,
notices,
events,
foldAvailable: (available: boolean) => {
foldAvailable = available
},
history: (items: unknown[]) => {
durable = items
},
finish: () => {
durable = [
{ type: "user", id: "u1" },
{ type: "assistant", id: "current", finish: "stop", time: { completed: 1 }, content: [] },
{ type: "user", id: "u2" },
]
},
failWrites: (value: boolean) => {
failWrites = value
},
failNotices: (value: boolean) => {
failNotices = value
},
async request(messages = original(), sessionID = "s1", kind = "context", providerID = "test") {
const event = {
sessionID,
agent: "build",
model: { providerID, id: "test" },
system: [],
tools: foldAvailable ? { fold: {}, peek: {}, unfold: {} } : {},
messages,
options: {},
} as unknown as SessionContextHook
await hooks.get(kind)!(event)
return event
},
async call(name: string, input: unknown, callID: string, sessionID = "s1") {
return tools.get(name)!.execute(input, {
sessionID,
messageID: "current",
id: callID,
agent: "build",
progress: async () => {},
} as unknown as ToolContext)
},
}
}
test("parallel tool commits survive reload and peek is session-scoped", async () => {
const host = await harness()
await host.request()
const results = await Promise.all([
host.call("fold", firstInput, "f1"),
host.call("fold", secondInput, "f2"),
])
const ids = results.map((result) => JSON.parse(result.content as string).id as string)
expect(ids.every(Boolean)).toBe(true)
await host.cleanup?.()
const reloaded = await harness(host.storage)
expect(JSON.stringify((await reloaded.request()).messages)).toContain("Detailed findings")
reloaded.finish()
const next = await reloaded.request(nextMessages())
expect(JSON.stringify(next.messages)).toContain(ids[0])
expect(JSON.stringify(next.messages)).toContain(ids[1])
expect(JSON.stringify(next.messages)).not.toContain("Detailed findings")
const expanded = await reloaded.call("peek", { id: ids[0] }, "e1")
expect(expanded.content).toContain(prose)
const other = await reloaded.call("peek", { id: ids[0] }, "e1", "s2")
expect(other.content).toContain("No fold")
// A retried call is acknowledged without creating a second fold.
expect(JSON.parse((await reloaded.call("fold", firstInput, "f1")).content as string)).toMatchObject({
id: ids[0],
status: "active",
})
})
test("parallel folds activate together before a same-turn continuation", async () => {
const host = await harness()
await host.request()
const results = await Promise.all([
host.call("fold", firstInput, "f1"),
host.call("fold", secondInput, "f2"),
])
expect(results.map((result) => JSON.parse(result.content as string).applies)).toEqual([
"next_model_request",
"next_model_request",
])
const next = await host.request(continuation(["f1", "f2"]))
const output = JSON.stringify(next.messages)
expect(output).toContain(JSON.parse(results[0].content as string).id)
expect(output).toContain(JSON.parse(results[1].content as string).id)
expect(output).not.toContain("Detailed findings")
expect(
(host.storage.get("sessions/s1") as { folds: { status: string }[] }).folds.map((fold) => fold.status),
).toEqual(["active", "active"])
expect(host.events).toEqual([
{
name: "foldsActivated",
data: expect.objectContaining({
sessionID: "s1",
count: 2,
removedChars: expect.any(Number),
beforeChars: expect.any(Number),
duration: 4_000,
}),
},
])
})
test("fold activation notifications can be disabled", async () => {
const host = await harness(new Map(), { notifications: false })
await host.request()
await host.call("fold", firstInput, "f1")
await host.request(continuation(["f1"]))
expect(host.events).toEqual([])
})
test("fold activation notifications accept a custom duration", async () => {
const host = await harness(new Map(), { notifications: { duration: 7_500 } })
await host.request()
await host.call("fold", firstInput, "f1")
await host.request(continuation(["f1"]))
expect(host.events[0]).toEqual({
name: "foldsActivated",
data: expect.objectContaining({ count: 1, duration: 7_500 }),
})
})
test("failed persistence does not publish a fold and the queue recovers", async () => {
const host = await harness()
await host.request()
host.failWrites(true)
await expect(host.call("fold", firstInput, "f1")).rejects.toThrow("storage failure")
expect(host.storage.size).toBe(0)
host.failWrites(false)
const retry = await host.call("fold", firstInput, "f1")
expect(JSON.parse(retry.content as string).id).toBeTruthy()
})
test("overlapping parallel requests return a conflict without losing the first fold", async () => {
const host = await harness()
await host.request()
const [first, second] = await Promise.all([
host.call("fold", firstInput, "f1"),
host.call("fold", { ...firstInput, summary: "Conflicting finding." }, "f2"),
])
expect(JSON.parse(first.content as string).id).toBeTruthy()
expect(JSON.parse(second.content as string).error).toContain("no longer visible")
expect((host.storage.get("sessions/s1") as { folds: unknown[] }).folds).toHaveLength(1)
})
test("expansion lifetime persists across plugin reloads", async () => {
const host = await harness()
await host.request()
const folded = await host.call("fold", firstInput, "f1")
const id = JSON.parse(folded.content as string).id
const expanded = await host.call("peek", { id }, "e1")
const messages = [
...original(),
Message.make({
id: "peek-result",
role: "tool",
content: [
{ type: "tool-result", id: "e1", name: "peek", result: { type: "text", value: expanded.content } },
],
}),
]
const reloaded = await harness(host.storage)
expect(JSON.stringify((await reloaded.request(messages)).messages)).toContain("historical content")
messages.push(Message.make({ id: "u2", role: "user", content: "Next question" }))
reloaded.finish()
const next = await reloaded.request(messages)
expect(JSON.stringify(next.messages)).toContain("call peek again")
expect(JSON.stringify(next.messages)).not.toContain("historical content")
})
test("compaction without a fold receipt leaves it queued; active folds remain summarized", async () => {
const host = await harness()
await host.request()
const folded = await host.call("fold", firstInput, "f1")
const id = JSON.parse(folded.content as string).id
const pending = await host.request(original(), "s1", "compaction")
expect(JSON.stringify(pending.messages)).toContain("First episode begins")
host.finish()
await host.request(nextMessages())
const event = await host.request(nextMessages(), "s1", "compaction")
expect(JSON.stringify(event.messages)).toContain(id)
expect(JSON.stringify(event.messages)).not.toContain("First episode begins")
expect(event.system[0].text).toContain("copying IDs exactly")
})
test("compaction activates a queued fold when its tool result is present", async () => {
const host = await harness()
await host.request()
const folded = JSON.parse((await host.call("fold", firstInput, "f1")).content as string)
const event = await host.request(continuation(["f1"]), "s1", "compaction")
const output = JSON.stringify(event.messages)
expect(output).toContain(folded.id)
expect(output).not.toContain("First episode begins")
expect(event.system[0].text).toContain("copying IDs exactly")
expect((host.storage.get("sessions/s1") as { folds: { status: string }[] }).folds[0].status).toBe("active")
})
test("bad anchors fail immediately and can be corrected without waiting a turn", async () => {
const host = await harness()
await host.request()
expect(
JSON.parse((await host.call("fold", { ...firstInput, start: "missing anchor" }, "bad")).content as string)
.error,
).toContain("not found")
expect(host.storage.size).toBe(0)
const valid = JSON.parse((await host.call("fold", firstInput, "good")).content as string)
expect(valid).toMatchObject({ status: "pending", applies: "next_model_request" })
expect(JSON.stringify((await host.request()).messages)).toContain(prose)
})
test("pending overlaps are still reserved across tool-driven continuations", async () => {
const host = await harness()
await host.request()
await host.call("fold", firstInput, "f1")
await host.request()
const conflict = await host.call("fold", { ...firstInput, summary: "Another summary" }, "f2")
expect(JSON.parse(conflict.content as string).error).toContain("no longer visible")
})
test("steering and unfinished responses cannot activate a fold", async () => {
const host = await harness()
await host.request()
await host.call("fold", firstInput, "f1")
host.history([
{ type: "user", id: "u1" },
{ type: "assistant", id: "current", finish: "tool-calls", time: { completed: 1 } },
{ type: "user", id: "u2" },
])
expect(JSON.stringify((await host.request(nextMessages())).messages)).toContain(prose)
host.history([
{ type: "user", id: "u1" },
{ type: "assistant", id: "current", finish: "stop" },
{ type: "user", id: "u2" },
])
expect(JSON.stringify((await host.request(nextMessages())).messages)).toContain(prose)
host.finish()
expect(JSON.stringify((await host.request(nextMessages())).messages)).not.toContain("First episode begins")
})
test("activation failure leaves original text and provides a persistent notice", async () => {
const host = await harness()
await host.request()
await host.call("fold", firstInput, "f1")
host.finish()
const changed = [
original()[0],
Message.make({ id: "a1", role: "assistant", content: "Edited historical text" }),
nextMessages()[2],
]
const event = await host.request(changed)
expect(JSON.stringify(event.messages)).toContain("Edited historical text")
expect(host.notices).toHaveLength(1)
expect(host.events).toHaveLength(0)
expect(host.notices[0]).toContain("not applied")
expect(event.messages.at(-1)?.content).toEqual([{ type: "text", text: host.notices[0] }])
await host.request(changed)
expect(host.notices).toHaveLength(1)
})
test("a notice that cannot be persisted still reaches the current request", async () => {
const host = await harness()
await host.request()
await host.call("fold", firstInput, "f1")
host.finish()
host.failNotices(true)
const changed = [
original()[0],
Message.make({ id: "a1", role: "assistant", content: "Edited historical text" }),
nextMessages()[2],
]
const event = await host.request(changed)
expect(JSON.stringify(event.messages.at(-1))).toContain("not applied")
expect(host.notices).toEqual([])
})
test("activation persists before changing the outgoing history; storage failure serves the previous view", async () => {
const host = await harness()
await host.request()
await host.call("fold", firstInput, "f1")
host.finish()
host.failWrites(true)
const degraded = await host.request(nextMessages())
expect(JSON.stringify(degraded.messages)).toContain("First episode begins")
expect(host.notices).toHaveLength(0)
expect(host.events).toHaveLength(0)
host.failWrites(false)
expect(JSON.stringify((await host.request(nextMessages())).messages)).not.toContain("First episode begins")
expect(host.events).toHaveLength(1)
})
test("pending peek returns captured media via ordinary tool file content", async () => {
const host = await harness()
const messages = [
original()[0],
Message.assistant("Media episode starts."),
Message.user([{ type: "media", media: Media.base64("iVBORw==", "image/png"), filename: "screen.png" }]),
Message.assistant("Media episode ends."),
]
await host.request(messages)
const folded = JSON.parse(
(
await host.call(
"fold",
{ start: "Media episode starts.", end: "Media episode ends.", summary: "Screenshot reviewed." },
"media-fold",
)
).content as string,
)
expect(folded.status).toBe("pending")
const peeked = await host.call("peek", { id: folded.id }, "media-peek")
expect(peeked.content).toContainEqual({
type: "file",
uri: "data:image/png;base64,iVBORw==",
mime: "image/png",
name: "screen.png",
})
expect(JSON.stringify((await host.request(messages)).messages)).toContain('"type":"media"')
})
const reading = (id: string, input: number) => ({
type: "assistant",
id,
model: { providerID: "test", id: "test" },
tokens: { input, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
})
const reminded = (event: SessionContextHook) =>
JSON.stringify(event.messages.at(-1)).includes("without a fold")
test("growth nudges are persisted, trailing, optional, and require fold to be available", async () => {
const grow = async (host: Awaited<ReturnType<typeof harness>>) => {
host.history([{ type: "user", id: "u1" }, reading("a1", 10_000)])
await host.request()
host.history([{ type: "user", id: "u1" }, reading("a2", 60_000)])
return host.request()
}
const host = await harness()
const event = await grow(host)
expect(reminded(event)).toBe(true)
expect(event.messages.at(-1)?.role).toBe("user")
expect(event.system).toEqual([])
expect(host.notices).toHaveLength(1)
expect(event.messages.at(-1)?.content).toEqual([{ type: "text", text: host.notices[0] }])
expect(reminded(await host.request())).toBe(false)
expect(reminded(await host.request(original(), "s1", "compaction"))).toBe(false)
expect(host.notices).toHaveLength(1)
expect(reminded(await grow(await harness(new Map(), { nudges: false })))).toBe(false)
expect(reminded(await grow(await harness(new Map(), { nudges: { every: 60_000 } })))).toBe(false)
expect(reminded(await grow(await harness(new Map(), { nudges: { every: 50_000 } })))).toBe(true)
const unavailable = await harness()
unavailable.foldAvailable(false)
expect(reminded(await grow(unavailable))).toBe(false)
})
test("successful fold activation suppresses nudges based on the old context", async () => {
const host = await harness()
host.history([{ type: "user", id: "u1" }, reading("a1", 10_000)])
await host.request()
await host.call("fold", firstInput, "f1")
host.history([{ type: "user", id: "u1" }, reading("current", 90_000)])
const event = await host.request(continuation(["f1"]))
expect(JSON.stringify(event.messages)).toContain("[folded")
expect(reminded(event)).toBe(false)
expect(reminded(await host.request(continuation(["f1"])))).toBe(false)
})
test("unfold waits for its result, survives reload, keeps peek, and permits an identical refold", async () => {
const host = await harness()
await host.request()
const fold = JSON.parse((await host.call("fold", firstInput, "f1")).content as string)
const foldedMessages = continuation(["f1"])
await host.request(foldedMessages)
const queued = JSON.parse((await host.call("unfold", { id: fold.id }, "uf1")).content as string)
expect(queued).toMatchObject({ id: fold.id, status: "pending", applies: "next_model_request" })
expect(JSON.stringify((await host.request(foldedMessages)).messages)).toContain(`[folded ${fold.id}]`)
const reloaded = await harness(host.storage)
const messages = continuation(["f1", "uf1"])
const restored = await reloaded.request(messages)
expect(JSON.stringify(restored.messages)).toContain(prose)
expect(JSON.stringify(restored.messages)).not.toContain(`[folded ${fold.id}]`)
expect(JSON.parse((await reloaded.call("unfold", { id: fold.id }, "uf1")).content as string).status).toBe(
"unfolded",
)
expect((await reloaded.call("peek", { id: fold.id }, "p1")).content).toContain(prose)
const repeated = JSON.parse((await reloaded.call("fold", firstInput, "f2")).content as string)
expect(repeated.status).toBe("pending")
expect(repeated.id).not.toBe(fold.id)
const refolded = await reloaded.request(continuation(["f1", "uf1", "f2"]))
expect(JSON.stringify(refolded.messages)).toContain(`[folded ${repeated.id}]`)
expect(JSON.stringify(refolded.messages)).not.toContain(prose)
})
test("unfold cancels a pending fold, frees its reservation, and is session-scoped", async () => {
const host = await harness()
await host.request()
const fold = JSON.parse((await host.call("fold", firstInput, "f1")).content as string)
const cancelled = JSON.parse((await host.call("unfold", { id: fold.id }, "uf1")).content as string)
expect(cancelled).toEqual({ id: fold.id, status: "unfolded" })
expect(JSON.stringify((await host.request(continuation(["f1", "uf1"]))).messages)).toContain(prose)
expect((await host.call("peek", { id: fold.id }, "p1")).content).toContain(prose)
expect((await host.call("unfold", { id: fold.id }, "uf1", "s2")).content).toContain("No fold")
const refolded = JSON.parse((await host.call("fold", firstInput, "f2")).content as string)
expect(refolded.status).toBe("pending")
expect(refolded.id).not.toBe(fold.id)
})
test("unfold commits and activation persist before changing memory or outgoing context", async () => {
const host = await harness()
await host.request()
const fold = JSON.parse((await host.call("fold", firstInput, "f1")).content as string)
await host.request(continuation(["f1"]))
host.failWrites(true)
await expect(host.call("unfold", { id: fold.id }, "uf1")).rejects.toThrow("storage failure")
host.failWrites(false)
await host.call("unfold", { id: fold.id }, "uf1")
host.failWrites(true)
expect(JSON.stringify((await host.request(continuation(["f1", "uf1"]))).messages)).toContain(
`[folded ${fold.id}]`,
)
host.failWrites(false)
expect(JSON.stringify((await host.request(continuation(["f1", "uf1"]))).messages)).toContain(prose)
})
test("parallel disjoint unfolds activate together, while overlapping fold/unfold calls conflict", async () => {
const host = await harness()
await host.request()
const first = JSON.parse((await host.call("fold", firstInput, "f1")).content as string)
const second = JSON.parse((await host.call("fold", secondInput, "f2")).content as string)
const messages = continuation(["f1", "f2"])
await host.request(messages)
const combined = {
start: `[folded ${first.id}]`,
end: `[folded ${second.id}] Second finding. [/folded]`,
summary: "Combined.",
}
const outer = JSON.parse((await host.call("fold", combined, "outer")).content as string)
expect(outer.status).toBe("pending")
const hidden = await host.call("unfold", { id: first.id }, "hidden")
expect(JSON.parse(hidden.content as string).error).toContain("marker is not available")
await host.call("unfold", { id: outer.id }, "cancel-outer")
await Promise.all([
host.call("unfold", { id: first.id }, "uf1"),
host.call("unfold", { id: second.id }, "uf2"),
])
const conflict = await host.call("fold", combined, "f3")
expect(JSON.parse(conflict.content as string).error).toBeTruthy()
const restored = await host.request(continuation(["f1", "f2", "uf1", "uf2"]))
expect(JSON.stringify(restored.messages)).toContain(prose)
expect(JSON.stringify(restored.messages)).toContain(secondInput.start)
expect(JSON.stringify(restored.messages)).not.toContain("[folded")
})
test("unfold activation works at compaction and completed-turn boundaries", async () => {
for (const kind of ["context", "compaction"]) {
const host = await harness()
await host.request()
const fold = JSON.parse((await host.call("fold", firstInput, "f1")).content as string)
await host.request(continuation(["f1"]))
await host.call("unfold", { id: fold.id }, "uf1")
if (kind === "context") host.finish()
const messages = kind === "context" ? nextMessages() : continuation(["f1", "uf1"])
const restored = await host.request(messages, "s1", kind)
expect(JSON.stringify(restored.messages)).toContain(prose)
expect(JSON.stringify(restored.messages)).not.toContain(`[folded ${fold.id}]`)
expect(restored.system).toEqual([])
}
})
test("source changes after queuing an unfold produce one persistent notice and retain the archive", async () => {
const host = await harness()
await host.request()
const fold = JSON.parse((await host.call("fold", firstInput, "f1")).content as string)
await host.request(continuation(["f1"]))
await host.call("unfold", { id: fold.id }, "uf1")
const messages = continuation(["f1", "uf1"])
messages[1] = Message.make({ id: "a1", role: "assistant", content: "Edited history" })
expect(JSON.stringify((await host.request(messages)).messages)).toContain("Edited history")
expect(host.notices).toHaveLength(1)
expect(host.notices[0]).toContain("was not unfolded")
// OpenCode replays persisted synthetic notices as user messages.
messages.push(Message.make({ id: "notice", role: "user", content: host.notices[0] }))
await host.request(messages)
expect(host.notices).toHaveLength(1)
expect((await host.call("peek", { id: fold.id }, "p1")).content).toContain(prose)
const receipt = JSON.parse((await host.call("unfold", { id: fold.id }, "uf1")).content as string)
expect(receipt.status).toBe("failed")
})
test("folds removed by compaction are surfaced once, and again only if the notice is lost", async () => {
const host = await harness()
await host.request()
const { id } = JSON.parse((await host.call("fold", firstInput, "f1")).content as string)
await host.request(continuation(["f1"]))
expect(host.notices).toEqual([])
const checkpoint = (n: number) =>
Message.make({
id: `c${n}`,
role: "user",
content: `<conversation-checkpoint>Summary ${n}</conversation-checkpoint>`,
})
const compacted = [checkpoint(1), Message.make({ id: "u3", role: "user", content: "Carry on" })]
const first = await host.request(compacted)
expect(host.notices).toHaveLength(1)
expect(host.notices[0]).toContain(`[folded ${id}] First finding. [/folded]`)
expect(first.messages.at(-1)?.content).toEqual([{ type: "text", text: host.notices[0] }])
const persisted = Message.make({ id: "syn1", role: "user", content: host.notices[0] })
await host.request([...compacted, persisted])
expect(host.notices).toHaveLength(1)
const recompacted = [checkpoint(2), Message.make({ id: "u4", role: "user", content: "Keep going" })]
await host.request(recompacted)
expect(host.notices).toHaveLength(2)
const withNotice = [...recompacted, Message.make({ id: "syn2", role: "user", content: host.notices[1] })]
await host.request(withNotice)
const folded = await host.call(
"fold",
{ start: "[context-fold] Earlier", end: "First finding. [/folded]", summary: "Old research." },
"f2",
)
expect(JSON.parse(folded.content as string).status).toBe("pending")
const next = await host.request([
...withNotice,
Message.make({
id: "current",
role: "assistant",
content: [{ type: "tool-call", id: "f2", name: "fold", input: {} }],
}),
Message.make({
role: "tool",
content: [{ type: "tool-result", id: "f2", name: "fold", result: { type: "text", value: "queued" } }],
}),
])
expect(JSON.stringify(next.messages)).not.toContain("First finding")
expect(host.notices).toHaveLength(2)
})