@spunto/build 0.28.4 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spunto/build",
3
- "version": "0.28.4",
3
+ "version": "0.30.0",
4
4
  "description": "Spunto's shared Build engine — the devcontainer image protocol and VS Code extension registry clients, with no database, no HTTP framework and no UI.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -159,6 +159,16 @@
159
159
  "type": "null"
160
160
  }
161
161
  ]
162
+ },
163
+ "parentCallId": {
164
+ "anyOf": [
165
+ {
166
+ "type": "string"
167
+ },
168
+ {
169
+ "type": "null"
170
+ }
171
+ ]
162
172
  }
163
173
  },
164
174
  "required": [
@@ -182,6 +192,16 @@
182
192
  },
183
193
  "truncated": {
184
194
  "type": "boolean"
195
+ },
196
+ "parentCallId": {
197
+ "anyOf": [
198
+ {
199
+ "type": "string"
200
+ },
201
+ {
202
+ "type": "null"
203
+ }
204
+ ]
185
205
  }
186
206
  },
187
207
  "required": [
@@ -217,6 +237,16 @@
217
237
  },
218
238
  "truncated": {
219
239
  "type": "boolean"
240
+ },
241
+ "parentCallId": {
242
+ "anyOf": [
243
+ {
244
+ "type": "string"
245
+ },
246
+ {
247
+ "type": "null"
248
+ }
249
+ ]
220
250
  }
221
251
  },
222
252
  "required": [
@@ -253,6 +283,16 @@
253
283
  },
254
284
  "truncated": {
255
285
  "type": "boolean"
286
+ },
287
+ "parentCallId": {
288
+ "anyOf": [
289
+ {
290
+ "type": "string"
291
+ },
292
+ {
293
+ "type": "null"
294
+ }
295
+ ]
256
296
  }
257
297
  },
258
298
  "required": [
@@ -289,6 +329,16 @@
289
329
  ],
290
330
  "additionalProperties": false
291
331
  }
332
+ },
333
+ "parentCallId": {
334
+ "anyOf": [
335
+ {
336
+ "type": "string"
337
+ },
338
+ {
339
+ "type": "null"
340
+ }
341
+ ]
292
342
  }
293
343
  },
294
344
  "required": [
@@ -412,6 +462,16 @@
412
462
  "type": "null"
413
463
  }
414
464
  ]
465
+ },
466
+ "parentCallId": {
467
+ "anyOf": [
468
+ {
469
+ "type": "string"
470
+ },
471
+ {
472
+ "type": "null"
473
+ }
474
+ ]
415
475
  }
416
476
  },
417
477
  "required": [
@@ -559,6 +619,237 @@
559
619
  ],
560
620
  "additionalProperties": false
561
621
  },
622
+ {
623
+ "type": "object",
624
+ "properties": {
625
+ "type": {
626
+ "const": "subagent"
627
+ },
628
+ "source": {
629
+ "description": "The harness's own message this event was read from, verbatim — kept so the projection can be replayed."
630
+ },
631
+ "callId": {
632
+ "type": "string"
633
+ },
634
+ "status": {
635
+ "type": "string",
636
+ "enum": [
637
+ "running",
638
+ "completed",
639
+ "failed",
640
+ "stopped"
641
+ ]
642
+ },
643
+ "description": {
644
+ "anyOf": [
645
+ {
646
+ "type": "string"
647
+ },
648
+ {
649
+ "type": "null"
650
+ }
651
+ ]
652
+ },
653
+ "kind": {
654
+ "anyOf": [
655
+ {
656
+ "type": "string"
657
+ },
658
+ {
659
+ "type": "null"
660
+ }
661
+ ]
662
+ },
663
+ "background": {
664
+ "type": "boolean"
665
+ },
666
+ "activity": {
667
+ "anyOf": [
668
+ {
669
+ "type": "string"
670
+ },
671
+ {
672
+ "type": "null"
673
+ }
674
+ ]
675
+ },
676
+ "lastTool": {
677
+ "anyOf": [
678
+ {
679
+ "type": "string"
680
+ },
681
+ {
682
+ "type": "null"
683
+ }
684
+ ]
685
+ },
686
+ "toolUses": {
687
+ "anyOf": [
688
+ {
689
+ "type": "number"
690
+ },
691
+ {
692
+ "type": "null"
693
+ }
694
+ ]
695
+ },
696
+ "tokens": {
697
+ "anyOf": [
698
+ {
699
+ "type": "number"
700
+ },
701
+ {
702
+ "type": "null"
703
+ }
704
+ ]
705
+ },
706
+ "durationMs": {
707
+ "anyOf": [
708
+ {
709
+ "type": "number"
710
+ },
711
+ {
712
+ "type": "null"
713
+ }
714
+ ]
715
+ },
716
+ "summary": {
717
+ "type": "string"
718
+ }
719
+ },
720
+ "required": [
721
+ "type",
722
+ "callId",
723
+ "status"
724
+ ],
725
+ "additionalProperties": false
726
+ },
727
+ {
728
+ "type": "object",
729
+ "properties": {
730
+ "type": {
731
+ "const": "context.compacted"
732
+ },
733
+ "source": {
734
+ "description": "The harness's own message this event was read from, verbatim — kept so the projection can be replayed."
735
+ },
736
+ "trigger": {
737
+ "anyOf": [
738
+ {
739
+ "type": "string"
740
+ },
741
+ {
742
+ "type": "null"
743
+ }
744
+ ]
745
+ },
746
+ "preTokens": {
747
+ "anyOf": [
748
+ {
749
+ "type": "number"
750
+ },
751
+ {
752
+ "type": "null"
753
+ }
754
+ ]
755
+ },
756
+ "postTokens": {
757
+ "anyOf": [
758
+ {
759
+ "type": "number"
760
+ },
761
+ {
762
+ "type": "null"
763
+ }
764
+ ]
765
+ },
766
+ "durationMs": {
767
+ "anyOf": [
768
+ {
769
+ "type": "number"
770
+ },
771
+ {
772
+ "type": "null"
773
+ }
774
+ ]
775
+ },
776
+ "summary": {
777
+ "type": "string"
778
+ },
779
+ "truncated": {
780
+ "type": "boolean"
781
+ },
782
+ "parentCallId": {
783
+ "anyOf": [
784
+ {
785
+ "type": "string"
786
+ },
787
+ {
788
+ "type": "null"
789
+ }
790
+ ]
791
+ }
792
+ },
793
+ "required": [
794
+ "type"
795
+ ],
796
+ "additionalProperties": false
797
+ },
798
+ {
799
+ "type": "object",
800
+ "properties": {
801
+ "type": {
802
+ "const": "skill.loaded"
803
+ },
804
+ "source": {
805
+ "description": "The harness's own message this event was read from, verbatim — kept so the projection can be replayed."
806
+ },
807
+ "callId": {
808
+ "anyOf": [
809
+ {
810
+ "type": "string"
811
+ },
812
+ {
813
+ "type": "null"
814
+ }
815
+ ]
816
+ },
817
+ "name": {
818
+ "type": "string"
819
+ },
820
+ "path": {
821
+ "anyOf": [
822
+ {
823
+ "type": "string"
824
+ },
825
+ {
826
+ "type": "null"
827
+ }
828
+ ]
829
+ },
830
+ "text": {
831
+ "type": "string"
832
+ },
833
+ "truncated": {
834
+ "type": "boolean"
835
+ },
836
+ "parentCallId": {
837
+ "anyOf": [
838
+ {
839
+ "type": "string"
840
+ },
841
+ {
842
+ "type": "null"
843
+ }
844
+ ]
845
+ }
846
+ },
847
+ "required": [
848
+ "type",
849
+ "name"
850
+ ],
851
+ "additionalProperties": false
852
+ },
562
853
  {
563
854
  "type": "object",
564
855
  "properties": {
@@ -11,6 +11,7 @@ export {
11
11
  MAX_TEXT,
12
12
  readContractLine,
13
13
  readContractObject,
14
+ SUBAGENT_STATUSES,
14
15
  TASK_EVENT_TYPES,
15
16
  TRANSIENT_TYPES,
16
17
  } from "./vocabulary"
@@ -43,6 +43,9 @@ export const TASK_EVENT_TYPES = [
43
43
  "usage",
44
44
  "session.ended",
45
45
  "session.background",
46
+ "subagent",
47
+ "context.compacted",
48
+ "skill.loaded",
46
49
  "raw",
47
50
  ] as const
48
51
  export type TaskEventType = (typeof TASK_EVENT_TYPES)[number]
@@ -140,6 +143,17 @@ const LIMIT = object({
140
143
 
141
144
  const COUNT = nullable(num())
142
145
 
146
+ /**
147
+ * `parentCallId` — the event happened **inside a subagent**: an agent the session started itself,
148
+ * through one of its own tool calls (Claude Code's `Agent`), whose `callId` this is. Absent or null:
149
+ * the session's own thread. A reader that knows nothing of subagents still reads the line; one that
150
+ * does nests it under that call instead of putting it on the session's thread.
151
+ */
152
+ const PARENT = nullable(str())
153
+
154
+ /** How a subagent stands. `stopped`: killed before it ended (an interrupt, a dead process). */
155
+ export const SUBAGENT_STATUSES = ["running", "completed", "failed", "stopped"] as const
156
+
143
157
  /**
144
158
  * Every line type, and its fields. `type` and `source` are on every line and not repeated here.
145
159
  * A field absent from a line is "unknown", which is not the same as `null` only for the reader of
@@ -158,12 +172,24 @@ const FIELDS: Record<TaskEventType | TransientType, Record<string, Field>> = {
158
172
  },
159
173
  // The name the harness gave the conversation — in answer to the `title` verb, or on its own.
160
174
  "session.title": { title: req(str()) },
161
- message: { role: req(oneOf(["assistant", "user"])), text: req(str()), truncated: bool(), limit: nullable(LIMIT) },
162
- thinking: { text: req(str()), truncated: bool() },
175
+ message: {
176
+ role: req(oneOf(["assistant", "user"])),
177
+ text: req(str()),
178
+ truncated: bool(),
179
+ limit: nullable(LIMIT),
180
+ parentCallId: PARENT,
181
+ },
182
+ thinking: { text: req(str()), truncated: bool(), parentCallId: PARENT },
163
183
  // `input` is text: the call's arguments as the harness wrote them, JSON or not.
164
- "tool.call": { callId: nullable(str()), name: req(str()), input: req(str()), truncated: bool() },
165
- "tool.result": { callId: nullable(str()), isError: req(bool()), output: req(str()), truncated: bool() },
166
- plan: { steps: req(array(object({ text: req(str()), status: req(str()) }))) },
184
+ "tool.call": { callId: nullable(str()), name: req(str()), input: req(str()), truncated: bool(), parentCallId: PARENT },
185
+ "tool.result": {
186
+ callId: nullable(str()),
187
+ isError: req(bool()),
188
+ output: req(str()),
189
+ truncated: bool(),
190
+ parentCallId: PARENT,
191
+ },
192
+ plan: { steps: req(array(object({ text: req(str()), status: req(str()) }))), parentCallId: PARENT },
167
193
  // `call`: one model call, summed over the task; its `contextTokens` is how full the window is
168
194
  // now. `session`: the harness's own running total, restated — the latest one wins.
169
195
  usage: {
@@ -178,6 +204,8 @@ const FIELDS: Record<TaskEventType | TransientType, Record<string, Field>> = {
178
204
  costUsd: COUNT,
179
205
  turns: COUNT,
180
206
  durationMs: COUNT,
207
+ // A call a subagent made: its tokens count, its prompt is not the session's context.
208
+ parentCallId: PARENT,
181
209
  },
182
210
  // The turn is over and the harness is listening again (or exiting). `limit` when the account,
183
211
  // not the work, ended it — the platform then waits for `resetsAt` instead of calling it review.
@@ -192,6 +220,48 @@ const FIELDS: Record<TaskEventType | TransientType, Record<string, Field>> = {
192
220
  "session.background": {
193
221
  tasks: req(array(object({ id: nullable(str()), kind: nullable(str()), description: nullable(str()) }))),
194
222
  },
223
+ // A subagent the session started (`callId`: the tool call that did), restated whole on every
224
+ // change — the latest line of a `callId` is where it stands. What it does inside is on lines of
225
+ // their own, each with `parentCallId` set to that same `callId`.
226
+ subagent: {
227
+ callId: req(str()),
228
+ status: req(oneOf(SUBAGENT_STATUSES)),
229
+ // What it was asked to do, in a few words; `kind`, which agent (`general-purpose`, `Explore`…).
230
+ description: nullable(str()),
231
+ kind: nullable(str()),
232
+ // Started in the background: the call that started it answered at once, the turn went on.
233
+ background: bool(),
234
+ // What it is doing right now, one line (« Reading /etc/hostname »), and the tool it last used.
235
+ activity: nullable(str()),
236
+ lastTool: nullable(str()),
237
+ toolUses: COUNT,
238
+ tokens: COUNT,
239
+ durationMs: COUNT,
240
+ // Its report, once it has ended.
241
+ summary: str(),
242
+ },
243
+ // The harness compacted its context: what it dropped, how long it took, and the summary it
244
+ // carries on with in their place — one event, never a message of the human's (the harness hands
245
+ // the summary back as a user turn). `parentCallId` when it is a subagent's context, not the session's.
246
+ "context.compacted": {
247
+ trigger: nullable(str()),
248
+ preTokens: COUNT,
249
+ postTokens: COUNT,
250
+ durationMs: COUNT,
251
+ summary: str(),
252
+ truncated: bool(),
253
+ parentCallId: PARENT,
254
+ },
255
+ // The harness loaded a skill's instructions into the context — the body of its SKILL.md, which
256
+ // the harness hands the model as a user turn. `callId` is the call that asked for it, when one did.
257
+ "skill.loaded": {
258
+ callId: nullable(str()),
259
+ name: req(str()),
260
+ path: nullable(str()),
261
+ text: str(),
262
+ truncated: bool(),
263
+ parentCallId: PARENT,
264
+ },
195
265
  raw: { json: any(), text: str() },
196
266
  // Not stored. `start` opens a fresh draft (one draft is one text block), else `text` is appended.
197
267
  "message.delta": { text: req(str()), start: bool() },
@@ -251,6 +321,8 @@ const CLIPPED: Partial<Record<TaskEventType, string>> = {
251
321
  thinking: "text",
252
322
  "tool.call": "input",
253
323
  "tool.result": "output",
324
+ "context.compacted": "summary",
325
+ "skill.loaded": "text",
254
326
  }
255
327
 
256
328
  /** Why this line is not a valid output line of the contract, or null. */
@@ -287,7 +359,7 @@ export function readContractObject(obj: Record<string, unknown>, line: string):
287
359
  payload[field] = text
288
360
  if (truncated) payload.truncated = true
289
361
  }
290
- if (type === "session.ended" && typeof payload.summary === "string") payload.summary = clip(payload.summary).text
362
+ if ((type === "session.ended" || type === "subagent") && typeof payload.summary === "string") payload.summary = clip(payload.summary).text
291
363
  return [{ type: type as TaskEventType, payload, source }]
292
364
  }
293
365
 
@@ -12,7 +12,7 @@
12
12
  * failing test rather than as an empty timeline.
13
13
  */
14
14
 
15
- import { clip, type NormalizedEvent } from "../../agent-contract/vocabulary"
15
+ import { SUBAGENT_STATUSES, clip, type NormalizedEvent } from "../../agent-contract/vocabulary"
16
16
  import { contextWindowOf } from "../../agent-stream/model-window"
17
17
  import { claudeTurnLimit, resetFromText, type TurnLimit } from "../../agent-stream/turn-limit"
18
18
 
@@ -75,12 +75,19 @@ export function adaptClaudeJson(obj: Record<string, unknown>): NormalizedEvent[]
75
75
  const message = obj.message as Record<string, unknown> | undefined
76
76
  const content = Array.isArray(message?.content) ? message!.content : []
77
77
  const role = type === "assistant" ? "assistant" : "user"
78
+ // Set on every message a subagent produced: the `Agent` call that started it. What it does is
79
+ // nested under that call rather than told as the session's own thread.
80
+ const parentCallId = typeof obj.parent_tool_use_id === "string" && obj.parent_tool_use_id ? obj.parent_tool_use_id : null
78
81
  const events: NormalizedEvent[] = []
79
82
  for (const rawBlock of content) {
80
83
  if (!rawBlock || typeof rawBlock !== "object") continue
81
84
  const block = rawBlock as Record<string, unknown>
82
85
  switch (block.type) {
83
86
  case "text": {
87
+ // A subagent's `user` text is the prompt it was given — already the input of the call
88
+ // that started it. Told again as a `user` message, it would read as a human's turn
89
+ // to everything that looks for one (the hand-back, the shared thread, the cockpit).
90
+ if (parentCallId && role === "user") break
84
91
  const { text, truncated } = clip(block.text)
85
92
  if (text) events.push({ type: "message", payload: { role, text, truncated } })
86
93
  break
@@ -134,6 +141,7 @@ export function adaptClaudeJson(obj: Record<string, unknown>): NormalizedEvent[]
134
141
  const measured = callUsage(message)
135
142
  if (measured) events.push(measured)
136
143
  }
144
+ if (parentCallId) for (const e of events) e.payload.parentCallId = parentCallId
137
145
  return events
138
146
  }
139
147
 
@@ -310,10 +318,19 @@ export function withoutFileBytes(value: unknown, depth = 0): unknown {
310
318
  */
311
319
  export function claudeTranslator(now: () => Date = () => new Date()): (message: Record<string, unknown>) => Record<string, unknown>[] {
312
320
  let carried: string | null = null
321
+ const compaction = compactionReader()
322
+ const skills = skillReader()
323
+ const subagents = subagentTracker()
313
324
  return (message) => {
314
325
  if (message.type === "stream_event") return claudeDelta(message)
315
326
  // The runner answers the `title` verb itself; a control response is never a title here.
316
327
  if (message.type === "control_response") return []
328
+ const compacted = compaction(message)
329
+ if (compacted.handled) return compacted.lines
330
+ const subagent = subagents(message)
331
+ if (subagent) return [...compacted.lines, ...subagent.map((line) => ({ ...line, source: message }))]
332
+ const skill = skills(message)
333
+ if (skill) return [...compacted.lines, skill]
317
334
  const events = adaptClaudeJson(withoutFileBytes(message) as Record<string, unknown>)
318
335
  for (const e of events) {
319
336
  const limit = e.payload.limit as TurnLimit | undefined
@@ -325,6 +342,292 @@ export function claudeTranslator(now: () => Date = () => new Date()): (message:
325
342
  }
326
343
  carried = null
327
344
  }
328
- return events.map((e) => ({ type: e.type, ...e.payload, source: message }))
345
+ return [...compacted.lines, ...events.map((e) => ({ type: e.type, ...e.payload, source: message }))]
329
346
  }
330
347
  }
348
+
349
+ // ── Skills ────────────────────────────────────────────────────────────────────
350
+
351
+ /** How Claude Code opens the turn that carries a skill's instructions. Measured on 2.1.310, 2026-11-24. */
352
+ const SKILL_PREFIX = /^Base directory for this skill: ([^\n]*)\n*/
353
+
354
+ /** How many asked-for skills are remembered while their body has not come: a loop guard, not a limit anyone reaches. */
355
+ const MAX_PENDING_SKILLS = 16
356
+
357
+ /**
358
+ * A skill being loaded, read as one `skill.loaded` rather than as something the member wrote.
359
+ *
360
+ * When the model calls the `Skill` tool, Claude Code answers the call with one line (« Launching
361
+ * skill: e2e-proof ») and then hands the model the skill's whole SKILL.md as a **user** turn,
362
+ * `isSynthetic`, opening with « Base directory for this skill: <path> ». Read naively that is
363
+ * kilobytes of instructions in the member's name. So the calls are remembered as they go by, and
364
+ * the synthetic turn that follows is tied back to the call that asked for it — by the skill's
365
+ * name, else the oldest call still waiting. A body no call explains (a slash command typed in a
366
+ * terminal session) still comes out as `skill.loaded`, named after its directory.
367
+ */
368
+ function skillReader(): (message: Record<string, unknown>) => Record<string, unknown> | null {
369
+ const pending: { callId: string; name: string }[] = []
370
+ return (message) => {
371
+ if (message.type === "assistant") {
372
+ const content = (message.message as Record<string, unknown> | undefined)?.content
373
+ for (const block of Array.isArray(content) ? content : []) {
374
+ if (!block || typeof block !== "object" || block.type !== "tool_use" || block.name !== "Skill") continue
375
+ const skill = (block.input as Record<string, unknown> | undefined)?.skill
376
+ if (typeof block.id === "string" && typeof skill === "string") pending.push({ callId: block.id, name: skill })
377
+ if (pending.length > MAX_PENDING_SKILLS) pending.shift()
378
+ }
379
+ return null
380
+ }
381
+ if (message.type !== "user" || message.isSynthetic !== true) return null
382
+ const full = userText(message)
383
+ const header = SKILL_PREFIX.exec(full)
384
+ if (!header) return null
385
+ const path = header[1]!.trim() || null
386
+ const dir = path?.split("/").filter(Boolean).pop() ?? ""
387
+ // `plugin:skill` is called by its full name and lives in a directory named after the skill.
388
+ const at = pending.findIndex((p) => p.name === dir || p.name.split(":").pop() === dir)
389
+ const call = at >= 0 ? pending.splice(at, 1)[0]! : pending.shift()
390
+ const { text, truncated } = clip(full.slice(header[0].length))
391
+ const parentCallId = typeof message.parent_tool_use_id === "string" && message.parent_tool_use_id ? message.parent_tool_use_id : null
392
+ return {
393
+ type: "skill.loaded",
394
+ callId: call?.callId ?? null,
395
+ name: call?.name ?? (dir || "skill"),
396
+ path,
397
+ text,
398
+ truncated,
399
+ ...(parentCallId ? { parentCallId } : {}),
400
+ source: message,
401
+ }
402
+ }
403
+ }
404
+
405
+ // ── Compaction ────────────────────────────────────────────────────────────────
406
+
407
+ /**
408
+ * How Claude Code's summary turn begins — only for a summary nothing else marks (a subagent's has
409
+ * no boundary before it). Measured on Claude Code 2.1.310, 2026-11-24.
410
+ */
411
+ const SUMMARY_PREFIX = "This session is being continued from a previous conversation"
412
+
413
+ type Compaction = { lines: Record<string, unknown>[]; handled: boolean }
414
+
415
+ /**
416
+ * A compaction, read as **one** `context.compacted`. What Claude Code prints, measured on
417
+ * 2026-11-24 (a session that compacted twelve times):
418
+ *
419
+ * 1. `system/status` `compacting`, restated as the summary is written (`compact_output_chars`);
420
+ * 2. `system/status` `null`, `compact_result: "success"`;
421
+ * 3. `system/compact_boundary` — the numbers (`compact_metadata`: trigger, tokens before and
422
+ * after, duration), and `preserved_segment.anchor_uuid`, the uuid of…
423
+ * 4. …a **user** turn, `isSynthetic`, whose text is the summary the session carries on with.
424
+ *
425
+ * Read naively, (1)–(3) are `raw` and (4) is 8 KB the member seems to have written. So (1) and (2)
426
+ * are absorbed, (3) is held, and (4) closes it — recognised by the anchor first, then by what
427
+ * Claude Code marks (`isCompactSummary`, `isVisibleInTranscriptOnly`), then as the synthetic turn
428
+ * right after a boundary, and by its first words only when nothing else says so. A boundary that
429
+ * no summary follows still comes out, without one, before the next line.
430
+ *
431
+ * A failed compaction (`compact_result: "failed"`) is left to the generic reader: it is information
432
+ * about the harness, and rare enough not to need a type.
433
+ */
434
+ function compactionReader(): (message: Record<string, unknown>) => Compaction {
435
+ let boundary: Record<string, unknown> | null = null
436
+ const flush = (): Record<string, unknown>[] => {
437
+ if (!boundary) return []
438
+ const line = compactedLine(boundary, null)
439
+ boundary = null
440
+ return [line]
441
+ }
442
+ return (message) => {
443
+ if (message.type === "system" && message.subtype === "status") {
444
+ const absorbed = message.status === "compacting" || (message.status === null && message.compact_result === "success")
445
+ return { lines: absorbed ? [] : flush(), handled: absorbed }
446
+ }
447
+ if (message.type === "system" && message.subtype === "compact_boundary") {
448
+ const lines = flush()
449
+ boundary = message
450
+ return { lines, handled: true }
451
+ }
452
+ if (message.type === "user" && isSummaryTurn(message, boundary)) {
453
+ const line = compactedLine(boundary, message)
454
+ boundary = null
455
+ return { lines: [line], handled: true }
456
+ }
457
+ return { lines: flush(), handled: false }
458
+ }
459
+ }
460
+
461
+ function isSummaryTurn(message: Record<string, unknown>, boundary: Record<string, unknown> | null): boolean {
462
+ if (message.isCompactSummary === true) return true
463
+ if (boundary) {
464
+ const anchor = anchorOf(boundary)
465
+ if (anchor && message.uuid === anchor) return true
466
+ if (message.isVisibleInTranscriptOnly === true) return true
467
+ if (!anchor && message.isSynthetic === true && !message.parent_tool_use_id) return true
468
+ }
469
+ return message.isSynthetic === true && userText(message).startsWith(SUMMARY_PREFIX)
470
+ }
471
+
472
+ function anchorOf(boundary: Record<string, unknown>): string | null {
473
+ const meta = (boundary.compact_metadata ?? {}) as Record<string, unknown>
474
+ for (const key of ["preserved_messages", "preserved_segment"]) {
475
+ const anchor = (meta[key] as Record<string, unknown> | undefined)?.anchor_uuid
476
+ if (typeof anchor === "string") return anchor
477
+ }
478
+ return null
479
+ }
480
+
481
+ function userText(message: Record<string, unknown>): string {
482
+ const content = (message.message as Record<string, unknown> | undefined)?.content
483
+ if (typeof content === "string") return content
484
+ if (!Array.isArray(content)) return ""
485
+ return content
486
+ .filter((b): b is Record<string, unknown> => !!b && typeof b === "object" && b.type === "text" && typeof b.text === "string")
487
+ .map((b) => b.text as string)
488
+ .join("\n\n")
489
+ }
490
+
491
+ /** The event, with both harness lines as its source when there are two — the projection stays replayable. */
492
+ function compactedLine(boundary: Record<string, unknown> | null, summary: Record<string, unknown> | null): Record<string, unknown> {
493
+ const meta = (boundary?.compact_metadata ?? {}) as Record<string, unknown>
494
+ const { text, truncated } = clip(summary ? userText(summary) : "")
495
+ const parentCallId = typeof summary?.parent_tool_use_id === "string" && summary.parent_tool_use_id ? summary.parent_tool_use_id : null
496
+ return {
497
+ type: "context.compacted",
498
+ trigger: typeof meta.trigger === "string" ? meta.trigger : null,
499
+ preTokens: num(meta.pre_tokens),
500
+ postTokens: num(meta.post_tokens),
501
+ durationMs: num(meta.duration_ms),
502
+ ...(summary ? { summary: text, truncated } : {}),
503
+ ...(parentCallId ? { parentCallId } : {}),
504
+ source: boundary && summary ? [boundary, summary] : (boundary ?? summary),
505
+ }
506
+ }
507
+
508
+ /** A subagent as the contract states it: one `subagent` line, restated whole on every change. */
509
+ type Subagent = {
510
+ callId: string
511
+ status: (typeof SUBAGENT_STATUSES)[number]
512
+ description: string | null
513
+ kind: string | null
514
+ background: boolean
515
+ activity: string | null
516
+ lastTool: string | null
517
+ toolUses: number | null
518
+ tokens: number | null
519
+ durationMs: number | null
520
+ summary?: string
521
+ }
522
+
523
+ /**
524
+ * The subagents of a session, read off Claude Code's task lines — `null` for a line that is not
525
+ * about one, `[]` for one that is but changes nothing worth a row.
526
+ *
527
+ * Measured with the SDK 0.3.310 (2026-11-24), for a subagent in the foreground and two in the
528
+ * background: `system/task_started` (with the `tool_use_id` of the `Agent` call, its description,
529
+ * `subagent_type`, `is_backgrounded`), a `system/task_progress` per tool it uses (`description`
530
+ * is then what it is doing — « Reading /etc/hostname » —, `last_tool_name`, running totals),
531
+ * `system/task_updated` (a patch: `status`, `is_backgrounded`), and `system/task_notification`
532
+ * when it ends, with its report as `summary`. The same lines exist for a backgrounded shell
533
+ * (`task_type: "local_bash"`), which is not a subagent: only the tasks that started as an agent
534
+ * are followed, and a line about a task never seen starting (a session resumed mid-way) is one
535
+ * only if it names a `subagent_type`.
536
+ *
537
+ * Stateful because only `task_started` says what the agent was asked: every later line is the
538
+ * whole state again, so a reader keeps the latest line of a `callId` and needs nothing else.
539
+ */
540
+ export function subagentTracker(): (message: Record<string, unknown>) => Record<string, unknown>[] | null {
541
+ const byTask = new Map<string, Subagent>()
542
+ const line = (sub: Subagent): Record<string, unknown>[] => [{ type: "subagent", ...sub }]
543
+ const text = (v: unknown): string | null => (typeof v === "string" && v.trim() ? v.replace(/\s+/g, " ").trim() : null)
544
+ const totals = (sub: Subagent, usage: unknown) => {
545
+ if (!usage || typeof usage !== "object") return
546
+ const u = usage as Record<string, unknown>
547
+ sub.toolUses = num(u.tool_uses) ?? sub.toolUses
548
+ sub.tokens = num(u.total_tokens) ?? sub.tokens
549
+ sub.durationMs = num(u.duration_ms) ?? sub.durationMs
550
+ }
551
+
552
+ return (message) => {
553
+ if (message.type !== "system" || typeof message.subtype !== "string") return null
554
+ const taskId = typeof message.task_id === "string" ? message.task_id : null
555
+ if (!taskId) return null
556
+ const known = byTask.get(taskId)
557
+ const callId = typeof message.tool_use_id === "string" && message.tool_use_id ? message.tool_use_id : null
558
+ const isAgent = message.task_type === "local_agent" || typeof message.subagent_type === "string"
559
+
560
+ // Met for the first time: a start, or a later line of a subagent whose start this process
561
+ // never saw. Anything that is not an agent — or that names no call to hang it under — is not ours.
562
+ const meet = (): Subagent | null => {
563
+ if (known) return known
564
+ if (!isAgent || !callId) return null
565
+ const sub: Subagent = {
566
+ callId,
567
+ status: "running",
568
+ description: text(message.description),
569
+ kind: text(message.subagent_type),
570
+ background: message.is_backgrounded === true,
571
+ activity: null,
572
+ lastTool: null,
573
+ toolUses: null,
574
+ tokens: null,
575
+ durationMs: null,
576
+ }
577
+ byTask.set(taskId, sub)
578
+ return sub
579
+ }
580
+
581
+ switch (message.subtype) {
582
+ case "task_started": {
583
+ const sub = meet()
584
+ return sub ? line(sub) : null
585
+ }
586
+ case "task_progress": {
587
+ const sub = meet()
588
+ if (!sub) return null
589
+ // With `agentProgressSummaries`, a model-written line every ~30 s; else the harness's own
590
+ // (« Running List first three entries in /etc »). Either is what it is doing now.
591
+ sub.activity = text(message.summary) ?? text(message.description) ?? sub.activity
592
+ sub.lastTool = text(message.last_tool_name) ?? sub.lastTool
593
+ totals(sub, message.usage)
594
+ return line(sub)
595
+ }
596
+ case "task_updated": {
597
+ if (!known) return null
598
+ const patch = (message.patch ?? {}) as Record<string, unknown>
599
+ let changed = false
600
+ if (patch.is_backgrounded === true && !known.background) {
601
+ known.background = true
602
+ changed = true
603
+ }
604
+ // `completed` is said again, with the report, by the notification right behind it.
605
+ const ended = endStatus(patch.status)
606
+ if (ended && ended !== "completed" && known.status === "running") {
607
+ known.status = ended
608
+ changed = true
609
+ }
610
+ return changed ? line(known) : []
611
+ }
612
+ case "task_notification": {
613
+ const sub = meet()
614
+ if (!sub) return null
615
+ sub.status = endStatus(message.status) ?? "completed"
616
+ sub.activity = null
617
+ const summary = text(message.summary) === null ? null : String(message.summary).trim()
618
+ if (summary) sub.summary = summary
619
+ totals(sub, message.usage)
620
+ return line(sub)
621
+ }
622
+ default:
623
+ return null
624
+ }
625
+ }
626
+ }
627
+
628
+ function endStatus(value: unknown): Subagent["status"] | null {
629
+ if (value === "completed") return "completed"
630
+ if (value === "failed") return "failed"
631
+ if (value === "stopped" || value === "killed") return "stopped"
632
+ return null
633
+ }
@@ -173,6 +173,10 @@ const session = sdk.query({
173
173
  // them. Never stored — the platform folds them into the draft the cockpit shows until the
174
174
  // whole message lands.
175
175
  includePartialMessages: process.env.SPUNTO_AGENT_STREAM === "1",
176
+ // A one-line « what it is doing now » for each subagent every ~30 s (`task_progress.summary`),
177
+ // written by a fork of the subagent on its own prompt cache — what the cockpit shows on the
178
+ // subagent's row while it runs. Without it the row says only the tool it last used.
179
+ agentProgressSummaries: true,
176
180
  stderr: (line) => process.stderr.write(line),
177
181
  },
178
182
  })