@spunto/build 0.28.3 → 0.29.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.
|
|
3
|
+
"version": "0.29.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,111 @@
|
|
|
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
|
+
},
|
|
562
727
|
{
|
|
563
728
|
"type": "object",
|
|
564
729
|
"properties": {
|
|
@@ -43,6 +43,7 @@ export const TASK_EVENT_TYPES = [
|
|
|
43
43
|
"usage",
|
|
44
44
|
"session.ended",
|
|
45
45
|
"session.background",
|
|
46
|
+
"subagent",
|
|
46
47
|
"raw",
|
|
47
48
|
] as const
|
|
48
49
|
export type TaskEventType = (typeof TASK_EVENT_TYPES)[number]
|
|
@@ -140,6 +141,17 @@ const LIMIT = object({
|
|
|
140
141
|
|
|
141
142
|
const COUNT = nullable(num())
|
|
142
143
|
|
|
144
|
+
/**
|
|
145
|
+
* `parentCallId` — the event happened **inside a subagent**: an agent the session started itself,
|
|
146
|
+
* through one of its own tool calls (Claude Code's `Agent`), whose `callId` this is. Absent or null:
|
|
147
|
+
* the session's own thread. A reader that knows nothing of subagents still reads the line; one that
|
|
148
|
+
* does nests it under that call instead of putting it on the session's thread.
|
|
149
|
+
*/
|
|
150
|
+
const PARENT = nullable(str())
|
|
151
|
+
|
|
152
|
+
/** How a subagent stands. `stopped`: killed before it ended (an interrupt, a dead process). */
|
|
153
|
+
export const SUBAGENT_STATUSES = ["running", "completed", "failed", "stopped"] as const
|
|
154
|
+
|
|
143
155
|
/**
|
|
144
156
|
* Every line type, and its fields. `type` and `source` are on every line and not repeated here.
|
|
145
157
|
* A field absent from a line is "unknown", which is not the same as `null` only for the reader of
|
|
@@ -158,12 +170,24 @@ const FIELDS: Record<TaskEventType | TransientType, Record<string, Field>> = {
|
|
|
158
170
|
},
|
|
159
171
|
// The name the harness gave the conversation — in answer to the `title` verb, or on its own.
|
|
160
172
|
"session.title": { title: req(str()) },
|
|
161
|
-
message: {
|
|
162
|
-
|
|
173
|
+
message: {
|
|
174
|
+
role: req(oneOf(["assistant", "user"])),
|
|
175
|
+
text: req(str()),
|
|
176
|
+
truncated: bool(),
|
|
177
|
+
limit: nullable(LIMIT),
|
|
178
|
+
parentCallId: PARENT,
|
|
179
|
+
},
|
|
180
|
+
thinking: { text: req(str()), truncated: bool(), parentCallId: PARENT },
|
|
163
181
|
// `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": {
|
|
166
|
-
|
|
182
|
+
"tool.call": { callId: nullable(str()), name: req(str()), input: req(str()), truncated: bool(), parentCallId: PARENT },
|
|
183
|
+
"tool.result": {
|
|
184
|
+
callId: nullable(str()),
|
|
185
|
+
isError: req(bool()),
|
|
186
|
+
output: req(str()),
|
|
187
|
+
truncated: bool(),
|
|
188
|
+
parentCallId: PARENT,
|
|
189
|
+
},
|
|
190
|
+
plan: { steps: req(array(object({ text: req(str()), status: req(str()) }))), parentCallId: PARENT },
|
|
167
191
|
// `call`: one model call, summed over the task; its `contextTokens` is how full the window is
|
|
168
192
|
// now. `session`: the harness's own running total, restated — the latest one wins.
|
|
169
193
|
usage: {
|
|
@@ -178,6 +202,8 @@ const FIELDS: Record<TaskEventType | TransientType, Record<string, Field>> = {
|
|
|
178
202
|
costUsd: COUNT,
|
|
179
203
|
turns: COUNT,
|
|
180
204
|
durationMs: COUNT,
|
|
205
|
+
// A call a subagent made: its tokens count, its prompt is not the session's context.
|
|
206
|
+
parentCallId: PARENT,
|
|
181
207
|
},
|
|
182
208
|
// The turn is over and the harness is listening again (or exiting). `limit` when the account,
|
|
183
209
|
// not the work, ended it — the platform then waits for `resetsAt` instead of calling it review.
|
|
@@ -192,6 +218,26 @@ const FIELDS: Record<TaskEventType | TransientType, Record<string, Field>> = {
|
|
|
192
218
|
"session.background": {
|
|
193
219
|
tasks: req(array(object({ id: nullable(str()), kind: nullable(str()), description: nullable(str()) }))),
|
|
194
220
|
},
|
|
221
|
+
// A subagent the session started (`callId`: the tool call that did), restated whole on every
|
|
222
|
+
// change — the latest line of a `callId` is where it stands. What it does inside is on lines of
|
|
223
|
+
// their own, each with `parentCallId` set to that same `callId`.
|
|
224
|
+
subagent: {
|
|
225
|
+
callId: req(str()),
|
|
226
|
+
status: req(oneOf(SUBAGENT_STATUSES)),
|
|
227
|
+
// What it was asked to do, in a few words; `kind`, which agent (`general-purpose`, `Explore`…).
|
|
228
|
+
description: nullable(str()),
|
|
229
|
+
kind: nullable(str()),
|
|
230
|
+
// Started in the background: the call that started it answered at once, the turn went on.
|
|
231
|
+
background: bool(),
|
|
232
|
+
// What it is doing right now, one line (« Reading /etc/hostname »), and the tool it last used.
|
|
233
|
+
activity: nullable(str()),
|
|
234
|
+
lastTool: nullable(str()),
|
|
235
|
+
toolUses: COUNT,
|
|
236
|
+
tokens: COUNT,
|
|
237
|
+
durationMs: COUNT,
|
|
238
|
+
// Its report, once it has ended.
|
|
239
|
+
summary: str(),
|
|
240
|
+
},
|
|
195
241
|
raw: { json: any(), text: str() },
|
|
196
242
|
// Not stored. `start` opens a fresh draft (one draft is one text block), else `text` is appended.
|
|
197
243
|
"message.delta": { text: req(str()), start: bool() },
|
|
@@ -287,7 +333,7 @@ export function readContractObject(obj: Record<string, unknown>, line: string):
|
|
|
287
333
|
payload[field] = text
|
|
288
334
|
if (truncated) payload.truncated = true
|
|
289
335
|
}
|
|
290
|
-
if (type === "session.ended" && typeof payload.summary === "string") payload.summary = clip(payload.summary).text
|
|
336
|
+
if ((type === "session.ended" || type === "subagent") && typeof payload.summary === "string") payload.summary = clip(payload.summary).text
|
|
291
337
|
return [{ type: type as TaskEventType, payload, source }]
|
|
292
338
|
}
|
|
293
339
|
|
|
@@ -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,13 @@ 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 subagents = subagentTracker()
|
|
313
322
|
return (message) => {
|
|
314
323
|
if (message.type === "stream_event") return claudeDelta(message)
|
|
315
324
|
// The runner answers the `title` verb itself; a control response is never a title here.
|
|
316
325
|
if (message.type === "control_response") return []
|
|
326
|
+
const subagent = subagents(message)
|
|
327
|
+
if (subagent) return subagent.map((line) => ({ ...line, source: message }))
|
|
317
328
|
const events = adaptClaudeJson(withoutFileBytes(message) as Record<string, unknown>)
|
|
318
329
|
for (const e of events) {
|
|
319
330
|
const limit = e.payload.limit as TurnLimit | undefined
|
|
@@ -328,3 +339,130 @@ export function claudeTranslator(now: () => Date = () => new Date()): (message:
|
|
|
328
339
|
return events.map((e) => ({ type: e.type, ...e.payload, source: message }))
|
|
329
340
|
}
|
|
330
341
|
}
|
|
342
|
+
|
|
343
|
+
/** A subagent as the contract states it: one `subagent` line, restated whole on every change. */
|
|
344
|
+
type Subagent = {
|
|
345
|
+
callId: string
|
|
346
|
+
status: (typeof SUBAGENT_STATUSES)[number]
|
|
347
|
+
description: string | null
|
|
348
|
+
kind: string | null
|
|
349
|
+
background: boolean
|
|
350
|
+
activity: string | null
|
|
351
|
+
lastTool: string | null
|
|
352
|
+
toolUses: number | null
|
|
353
|
+
tokens: number | null
|
|
354
|
+
durationMs: number | null
|
|
355
|
+
summary?: string
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* The subagents of a session, read off Claude Code's task lines — `null` for a line that is not
|
|
360
|
+
* about one, `[]` for one that is but changes nothing worth a row.
|
|
361
|
+
*
|
|
362
|
+
* Measured with the SDK 0.3.310 (2026-11-24), for a subagent in the foreground and two in the
|
|
363
|
+
* background: `system/task_started` (with the `tool_use_id` of the `Agent` call, its description,
|
|
364
|
+
* `subagent_type`, `is_backgrounded`), a `system/task_progress` per tool it uses (`description`
|
|
365
|
+
* is then what it is doing — « Reading /etc/hostname » —, `last_tool_name`, running totals),
|
|
366
|
+
* `system/task_updated` (a patch: `status`, `is_backgrounded`), and `system/task_notification`
|
|
367
|
+
* when it ends, with its report as `summary`. The same lines exist for a backgrounded shell
|
|
368
|
+
* (`task_type: "local_bash"`), which is not a subagent: only the tasks that started as an agent
|
|
369
|
+
* are followed, and a line about a task never seen starting (a session resumed mid-way) is one
|
|
370
|
+
* only if it names a `subagent_type`.
|
|
371
|
+
*
|
|
372
|
+
* Stateful because only `task_started` says what the agent was asked: every later line is the
|
|
373
|
+
* whole state again, so a reader keeps the latest line of a `callId` and needs nothing else.
|
|
374
|
+
*/
|
|
375
|
+
export function subagentTracker(): (message: Record<string, unknown>) => Record<string, unknown>[] | null {
|
|
376
|
+
const byTask = new Map<string, Subagent>()
|
|
377
|
+
const line = (sub: Subagent): Record<string, unknown>[] => [{ type: "subagent", ...sub }]
|
|
378
|
+
const text = (v: unknown): string | null => (typeof v === "string" && v.trim() ? v.replace(/\s+/g, " ").trim() : null)
|
|
379
|
+
const totals = (sub: Subagent, usage: unknown) => {
|
|
380
|
+
if (!usage || typeof usage !== "object") return
|
|
381
|
+
const u = usage as Record<string, unknown>
|
|
382
|
+
sub.toolUses = num(u.tool_uses) ?? sub.toolUses
|
|
383
|
+
sub.tokens = num(u.total_tokens) ?? sub.tokens
|
|
384
|
+
sub.durationMs = num(u.duration_ms) ?? sub.durationMs
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return (message) => {
|
|
388
|
+
if (message.type !== "system" || typeof message.subtype !== "string") return null
|
|
389
|
+
const taskId = typeof message.task_id === "string" ? message.task_id : null
|
|
390
|
+
if (!taskId) return null
|
|
391
|
+
const known = byTask.get(taskId)
|
|
392
|
+
const callId = typeof message.tool_use_id === "string" && message.tool_use_id ? message.tool_use_id : null
|
|
393
|
+
const isAgent = message.task_type === "local_agent" || typeof message.subagent_type === "string"
|
|
394
|
+
|
|
395
|
+
// Met for the first time: a start, or a later line of a subagent whose start this process
|
|
396
|
+
// never saw. Anything that is not an agent — or that names no call to hang it under — is not ours.
|
|
397
|
+
const meet = (): Subagent | null => {
|
|
398
|
+
if (known) return known
|
|
399
|
+
if (!isAgent || !callId) return null
|
|
400
|
+
const sub: Subagent = {
|
|
401
|
+
callId,
|
|
402
|
+
status: "running",
|
|
403
|
+
description: text(message.description),
|
|
404
|
+
kind: text(message.subagent_type),
|
|
405
|
+
background: message.is_backgrounded === true,
|
|
406
|
+
activity: null,
|
|
407
|
+
lastTool: null,
|
|
408
|
+
toolUses: null,
|
|
409
|
+
tokens: null,
|
|
410
|
+
durationMs: null,
|
|
411
|
+
}
|
|
412
|
+
byTask.set(taskId, sub)
|
|
413
|
+
return sub
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
switch (message.subtype) {
|
|
417
|
+
case "task_started": {
|
|
418
|
+
const sub = meet()
|
|
419
|
+
return sub ? line(sub) : null
|
|
420
|
+
}
|
|
421
|
+
case "task_progress": {
|
|
422
|
+
const sub = meet()
|
|
423
|
+
if (!sub) return null
|
|
424
|
+
// With `agentProgressSummaries`, a model-written line every ~30 s; else the harness's own
|
|
425
|
+
// (« Running List first three entries in /etc »). Either is what it is doing now.
|
|
426
|
+
sub.activity = text(message.summary) ?? text(message.description) ?? sub.activity
|
|
427
|
+
sub.lastTool = text(message.last_tool_name) ?? sub.lastTool
|
|
428
|
+
totals(sub, message.usage)
|
|
429
|
+
return line(sub)
|
|
430
|
+
}
|
|
431
|
+
case "task_updated": {
|
|
432
|
+
if (!known) return null
|
|
433
|
+
const patch = (message.patch ?? {}) as Record<string, unknown>
|
|
434
|
+
let changed = false
|
|
435
|
+
if (patch.is_backgrounded === true && !known.background) {
|
|
436
|
+
known.background = true
|
|
437
|
+
changed = true
|
|
438
|
+
}
|
|
439
|
+
// `completed` is said again, with the report, by the notification right behind it.
|
|
440
|
+
const ended = endStatus(patch.status)
|
|
441
|
+
if (ended && ended !== "completed" && known.status === "running") {
|
|
442
|
+
known.status = ended
|
|
443
|
+
changed = true
|
|
444
|
+
}
|
|
445
|
+
return changed ? line(known) : []
|
|
446
|
+
}
|
|
447
|
+
case "task_notification": {
|
|
448
|
+
const sub = meet()
|
|
449
|
+
if (!sub) return null
|
|
450
|
+
sub.status = endStatus(message.status) ?? "completed"
|
|
451
|
+
sub.activity = null
|
|
452
|
+
const summary = text(message.summary) === null ? null : String(message.summary).trim()
|
|
453
|
+
if (summary) sub.summary = summary
|
|
454
|
+
totals(sub, message.usage)
|
|
455
|
+
return line(sub)
|
|
456
|
+
}
|
|
457
|
+
default:
|
|
458
|
+
return null
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function endStatus(value: unknown): Subagent["status"] | null {
|
|
464
|
+
if (value === "completed") return "completed"
|
|
465
|
+
if (value === "failed") return "failed"
|
|
466
|
+
if (value === "stopped" || value === "killed") return "stopped"
|
|
467
|
+
return null
|
|
468
|
+
}
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
// (`./catalogs.ts`): `{"effort":"high"}`
|
|
17
17
|
// SPUNTO_AGENT_MCP — the org's MCP servers (RFC 0049), in the platform's words:
|
|
18
18
|
// `[{"name":"linear","path":"/api/mcp-gateway/…"}]`, reached through the platform's
|
|
19
|
-
// gateway with the machine's own identity (`SPUNTO_WORKER_TOKEN`)
|
|
19
|
+
// gateway with the machine's own identity (`SPUNTO_WORKER_TOKEN`), and reconnected
|
|
20
|
+
// before each turn after the first, so a server's new tools reach a running session
|
|
20
21
|
// SPUNTO_AGENT_STREAM — "1": also print the tokens of a message as they are written
|
|
21
22
|
// (`stream_event` lines), set behind the org flag `token-streaming`
|
|
22
23
|
// exit 0 once the input is closed and the last turn is over, 1 when the session could not run
|
|
@@ -172,10 +173,33 @@ const session = sdk.query({
|
|
|
172
173
|
// them. Never stored — the platform folds them into the draft the cockpit shows until the
|
|
173
174
|
// whole message lands.
|
|
174
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,
|
|
175
180
|
stderr: (line) => process.stderr.write(line),
|
|
176
181
|
},
|
|
177
182
|
})
|
|
178
183
|
|
|
184
|
+
/**
|
|
185
|
+
* The gateway's servers are connected once, when the process starts, and the session lives as long
|
|
186
|
+
* as the task: a server redeployed with new tools since would stay invisible until the task ends.
|
|
187
|
+
* So before a turn that comes after the session went idle, each is reconnected — which lists its
|
|
188
|
+
* tools again (~30 ms each, measured with the SDK 0.3.310). Never during a turn: that could cut a
|
|
189
|
+
* call in flight. A server that fails to reconnect keeps the tools it had.
|
|
190
|
+
*/
|
|
191
|
+
let idle = false
|
|
192
|
+
let ahead = Promise.resolve()
|
|
193
|
+
async function refreshMcpServers() {
|
|
194
|
+
if (!idle) return
|
|
195
|
+
idle = false
|
|
196
|
+
await Promise.all(
|
|
197
|
+
Object.keys(mcpServers).map((name) =>
|
|
198
|
+
session.reconnectMcpServer(name).catch((err) => process.stderr.write(`claude runner: reconnecting ${name} failed: ${err?.message ?? err}\n`)),
|
|
199
|
+
),
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
|
|
179
203
|
createInterface({ input: process.stdin, crlfDelay: Infinity })
|
|
180
204
|
.on("line", (line) => {
|
|
181
205
|
let msg
|
|
@@ -186,7 +210,9 @@ createInterface({ input: process.stdin, crlfDelay: Infinity })
|
|
|
186
210
|
}
|
|
187
211
|
if (!msg || typeof msg !== "object") return
|
|
188
212
|
if (msg.type === "user" && typeof msg.text === "string") {
|
|
189
|
-
|
|
213
|
+
const turn = { type: "user", message: { role: "user", content: [{ type: "text", text: msg.text }] }, parent_tool_use_id: null }
|
|
214
|
+
// Chained, so that a turn refreshed between two others still goes in the order it was sent.
|
|
215
|
+
ahead = ahead.then(refreshMcpServers).then(() => input.push(turn))
|
|
190
216
|
} else if (msg.type === "interrupt") {
|
|
191
217
|
session.interrupt().catch((err) => process.stderr.write(`claude runner: interrupt failed: ${err?.message ?? err}\n`))
|
|
192
218
|
} else if (msg.type === "title" && typeof msg.description === "string") {
|
|
@@ -210,6 +236,7 @@ try {
|
|
|
210
236
|
for await (const message of session) {
|
|
211
237
|
await emit(message)
|
|
212
238
|
lastType = message?.type ?? null
|
|
239
|
+
if (lastType === "result") idle = true
|
|
213
240
|
}
|
|
214
241
|
} catch (err) {
|
|
215
242
|
if (lastType === "result") process.exit(1)
|