@spunto/build 0.34.0 → 0.36.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.34.0",
3
+ "version": "0.36.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",
@@ -978,6 +978,81 @@
978
978
  ],
979
979
  "additionalProperties": false
980
980
  },
981
+ {
982
+ "type": "object",
983
+ "properties": {
984
+ "type": {
985
+ "const": "mcp.reloaded"
986
+ },
987
+ "source": {
988
+ "description": "The harness's own message this event was read from, verbatim — kept so the projection can be replayed."
989
+ },
990
+ "id": {
991
+ "anyOf": [
992
+ {
993
+ "type": "string"
994
+ },
995
+ {
996
+ "type": "null"
997
+ }
998
+ ]
999
+ },
1000
+ "servers": {
1001
+ "type": "array",
1002
+ "items": {
1003
+ "type": "object",
1004
+ "properties": {
1005
+ "name": {
1006
+ "type": "string"
1007
+ },
1008
+ "status": {
1009
+ "type": "string"
1010
+ },
1011
+ "tools": {
1012
+ "anyOf": [
1013
+ {
1014
+ "type": "number"
1015
+ },
1016
+ {
1017
+ "type": "null"
1018
+ }
1019
+ ]
1020
+ },
1021
+ "error": {
1022
+ "anyOf": [
1023
+ {
1024
+ "type": "string"
1025
+ },
1026
+ {
1027
+ "type": "null"
1028
+ }
1029
+ ]
1030
+ }
1031
+ },
1032
+ "required": [
1033
+ "name",
1034
+ "status"
1035
+ ],
1036
+ "additionalProperties": false
1037
+ }
1038
+ },
1039
+ "error": {
1040
+ "anyOf": [
1041
+ {
1042
+ "type": "string"
1043
+ },
1044
+ {
1045
+ "type": "null"
1046
+ }
1047
+ ]
1048
+ }
1049
+ },
1050
+ "required": [
1051
+ "type",
1052
+ "servers"
1053
+ ],
1054
+ "additionalProperties": false
1055
+ },
981
1056
  {
982
1057
  "type": "object",
983
1058
  "properties": {
@@ -1135,6 +1210,44 @@
1135
1210
  "answers"
1136
1211
  ],
1137
1212
  "additionalProperties": false
1213
+ },
1214
+ {
1215
+ "type": "object",
1216
+ "properties": {
1217
+ "type": {
1218
+ "type": "string",
1219
+ "enum": [
1220
+ "mcp.reload"
1221
+ ]
1222
+ },
1223
+ "id": {
1224
+ "type": "string"
1225
+ },
1226
+ "servers": {
1227
+ "type": "array",
1228
+ "items": {
1229
+ "type": "object",
1230
+ "properties": {
1231
+ "name": {
1232
+ "type": "string"
1233
+ },
1234
+ "path": {
1235
+ "type": "string"
1236
+ }
1237
+ },
1238
+ "required": [
1239
+ "name",
1240
+ "path"
1241
+ ],
1242
+ "additionalProperties": false
1243
+ }
1244
+ }
1245
+ },
1246
+ "required": [
1247
+ "type",
1248
+ "id"
1249
+ ],
1250
+ "additionalProperties": false
1138
1251
  }
1139
1252
  ]
1140
1253
  }
@@ -23,6 +23,8 @@
23
23
  * Pure: no I/O, no clock.
24
24
  */
25
25
 
26
+ import type { AgentMcpServer } from "./runner"
27
+
26
28
 
27
29
  /** One line, no runs of whitespace, bounded — a title goes in a list row and in a page header. */
28
30
  export function cleanTitle(value: unknown): string | null {
@@ -75,6 +77,7 @@ export const TASK_EVENT_TYPES = [
75
77
  "context.compacted",
76
78
  "skill.loaded",
77
79
  "question",
80
+ "mcp.reloaded",
78
81
  "raw",
79
82
  ] as const
80
83
  export type TaskEventType = (typeof TASK_EVENT_TYPES)[number]
@@ -99,7 +102,7 @@ export type SourcedEvent = NormalizedEvent & { source: string }
99
102
  export type QuestionAnswer = { question: string; selected: string[]; text?: string | null }
100
103
 
101
104
  /**
102
- * One line of a runner's stdin. The whole input side of the contract — four verbs, the same for
105
+ * One line of a runner's stdin. The whole input side of the contract — five verbs, the same for
103
106
  * every harness, so the platform never writes a harness's own shape into a runner. Closing stdin
104
107
  * ends the session once its turn is over.
105
108
  */
@@ -108,6 +111,7 @@ export type RunnerInput =
108
111
  | { type: "interrupt"; id: string }
109
112
  | { type: "title"; id: string; description: string }
110
113
  | { type: "answer"; callId: string; answers: QuestionAnswer[] }
114
+ | { type: "mcp.reload"; id: string; servers?: AgentMcpServer[] }
111
115
 
112
116
  // ── The table ─────────────────────────────────────────────────────────────────
113
117
 
@@ -195,6 +199,13 @@ export const SUBAGENT_STATUSES = ["running", "completed", "failed", "stopped"] a
195
199
  */
196
200
  export const QUESTION_STATUSES = ["pending", "answered", "dismissed"] as const
197
201
 
202
+ /**
203
+ * How an MCP server of the session stands after a reload, in the harness's own word (Claude Code:
204
+ * `connected`, `failed`, `needs-auth`, `pending`, `disabled`) — plus `deferred`: a call to it was
205
+ * in flight, so it is reconnected once that call is over rather than cut. Shown, never decided on.
206
+ */
207
+ const MCP_SERVER = object({ name: req(str()), status: req(str()), tools: COUNT, error: nullable(str()) })
208
+
198
209
  const ANSWERS = array(object({ question: req(str()), selected: req(array(str())), text: nullable(str()) }))
199
210
 
200
211
  /**
@@ -327,6 +338,10 @@ const FIELDS: Record<TaskEventType | TransientType, Record<string, Field>> = {
327
338
  ),
328
339
  answers: ANSWERS,
329
340
  },
341
+ // The session's MCP servers were reloaded, in answer to the `mcp.reload` verb (`id`): each one
342
+ // and the tools it now lists. `error` when the harness could not reload at all (one that only
343
+ // connects its servers when the session starts) — the servers it has are then left alone.
344
+ "mcp.reloaded": { id: nullable(str()), servers: req(array(MCP_SERVER)), error: nullable(str()) },
330
345
  raw: { json: any(), text: str() },
331
346
  // Not stored. `start` opens a fresh draft (one draft is one text block), else `text` is appended.
332
347
  "message.delta": { text: req(str()), start: bool() },
@@ -344,6 +359,7 @@ export function agentContractSchema(): Record<string, unknown> {
344
359
  { type: req(oneOf(["interrupt"])), id: req(str()) },
345
360
  { type: req(oneOf(["title"])), id: req(str()), description: req(str()) },
346
361
  { type: req(oneOf(["answer"])), callId: req(str()), answers: req(ANSWERS) },
362
+ { type: req(oneOf(["mcp.reload"])), id: req(str()), servers: array(object({ name: req(str()), path: req(str()) })) },
347
363
  ]
348
364
  return {
349
365
  $schema: "https://json-schema.org/draft/2020-12/schema",
@@ -145,7 +145,12 @@ export type PlatformMessage =
145
145
 
146
146
  /** Agent → control plane. */
147
147
  export type AgentMessage =
148
- | { type: "hello"; token: string; info: AgentInfo }
148
+ /**
149
+ * `gitToken`: the machine token (`sbxg_`) the plan of an earlier hello of this same boot handed
150
+ * out (agent 0.12.0+). The platform keeps it when it is still this sandbox's, instead of minting
151
+ * one that the processes already running would not have.
152
+ */
153
+ | { type: "hello"; token: string; info: AgentInfo; gitToken?: string }
149
154
  | { type: "heartbeat" }
150
155
  | { type: "pong"; at: number }
151
156
  /** After a resume (SIGUSR1): does this socket still reach the platform? */
@@ -22,6 +22,7 @@
22
22
  */
23
23
 
24
24
  import { readContractLine, type NormalizedEvent, type QuestionAnswer, type RunnerInput, type SourcedEvent } from "../agent-contract/vocabulary"
25
+ import type { AgentMcpServer } from "../agent-contract/runner"
25
26
 
26
27
  export { TASK_EVENT_TYPES } from "../agent-contract/vocabulary"
27
28
  export type { NormalizedEvent, QuestionAnswer, RunnerInput, SourcedEvent, TaskEventType } from "../agent-contract/vocabulary"
@@ -69,6 +70,16 @@ export function answerLine(protocol: AgentProtocol, callId: string, answers: Que
69
70
  return runnerLine({ type: "answer", callId, answers })
70
71
  }
71
72
 
73
+ /**
74
+ * Ask the session to **reload its MCP servers** now, or null for a session that cannot be told.
75
+ * `servers`: the platform's servers as they stand today — one turned on since the session started
76
+ * is added, one turned off removed. The runner answers with an `mcp.reloaded` line under `requestId`.
77
+ */
78
+ export function mcpReloadLine(protocol: AgentProtocol, requestId: string, servers: AgentMcpServer[]): string | null {
79
+ if (protocol !== "agent-v1") return null
80
+ return runnerLine({ type: "mcp.reload", id: requestId, servers })
81
+ }
82
+
72
83
  /**
73
84
  * Ask the session to **name the conversation**, or null when it cannot be asked. The runner
74
85
  * answers — if its harness can name a conversation at all — with a `session.title` line; the
@@ -9,6 +9,7 @@ export {
9
9
  answerLine,
10
10
  interruptLine,
11
11
  isInteractive,
12
+ mcpReloadLine,
12
13
  parseLines,
13
14
  parseStreamLine,
14
15
  sessionTitleRequestLine,
@@ -19,7 +19,7 @@ import { fileURLToPath } from "node:url"
19
19
  import os from "node:os"
20
20
  import net from "node:net"
21
21
 
22
- const VERSION = "0.11.0"
22
+ const VERSION = "0.12.0"
23
23
  const API = (process.env.SPUNTO_API_URL ?? "").replace(/\/$/, "")
24
24
  // `let`: a fork (RFC 0039 § 4.10) hands this process the identity of the copy it now runs in.
25
25
  let TOKEN = process.env.SPUNTO_SANDBOX_TOKEN ?? ""
@@ -279,6 +279,33 @@ function refreshGitIdentity(plan) {
279
279
  }
280
280
  }
281
281
 
282
+ // The machine token (`sbxg_`) of this boot, shown back at every hello of the same boot so the
283
+ // platform keeps it rather than minting another (`buildProjectPlan`, sandbox-project.ts). Every
284
+ // process started since holds it in its env — the agent sessions' MCP servers, the doorbell, the
285
+ // setup's git helper — and a mere reconnect (the API redeployed, the network dropped) must not
286
+ // take it from them. On the disk, root's, with the boot it was handed out in: an agent restarted
287
+ // in the same boot (or a box resumed with its RAM) shows it; a new boot does not, and gets a new
288
+ // one. A copy (a fork) shows its source's, which the platform refuses: another sandbox's.
289
+ const GIT_TOKEN_FILE = () => join(DIR, "git-token.json")
290
+ function keepGitToken(plan) {
291
+ const token = plan.env?.SPUNTO_WORKER_TOKEN
292
+ if (!token) return
293
+ try {
294
+ writeFileSync(GIT_TOKEN_FILE(), JSON.stringify({ boot: bootId(), token }), { mode: 0o600 })
295
+ } catch (err) {
296
+ console.warn(`[sandbox-agent] git token not kept: ${err.message}`)
297
+ }
298
+ }
299
+ function gitTokenOfThisBoot() {
300
+ try {
301
+ const kept = JSON.parse(readFileSync(GIT_TOKEN_FILE(), "utf8"))
302
+ const boot = bootId()
303
+ return boot && kept?.boot === boot && typeof kept.token === "string" ? kept.token : null
304
+ } catch {
305
+ return null
306
+ }
307
+ }
308
+
282
309
  /**
283
310
  * The image's own ENV (the plan's `imageEnv`), for a box whose provider booted the image's files
284
311
  * without it (a microVM): its PATH in front of ours — `claude` in spunto-worker-base is in
@@ -305,6 +332,7 @@ function applyProject(plan) {
305
332
  childEnv = { ...baseEnv, ...underImageEnv(baseEnv, plan.imageEnv), ...plan.env }
306
333
  runAs = plan.user || null
307
334
  refreshGitIdentity(plan)
335
+ keepGitToken(plan)
308
336
  project = { plan, exitCode: project?.exitCode ?? null }
309
337
 
310
338
  if (runnerPid()) {
@@ -1027,7 +1055,10 @@ function connect() {
1027
1055
  const sock = (ws = new WebSocket(CONNECT_URL))
1028
1056
  wsDialedAt = uptime()
1029
1057
  ws.binaryType = "arraybuffer"
1030
- ws.addEventListener("open", () => send({ type: "hello", token: TOKEN, info: info() }))
1058
+ ws.addEventListener("open", () => {
1059
+ const gitToken = gitTokenOfThisBoot()
1060
+ send({ type: "hello", token: TOKEN, info: info(), ...(gitToken ? { gitToken } : {}) })
1061
+ })
1031
1062
  ws.addEventListener("message", (ev) => {
1032
1063
  // A socket given up on after a resume (see SIGUSR1) may still deliver late: not ours anymore.
1033
1064
  if (ws !== sock) return
@@ -278,8 +278,12 @@ while IFS= read -r line; do
278
278
  esac
279
279
  done
280
280
  REPO="\${REPO%.git}"
281
+ # The machine token the sandbox agent keeps current, before the one frozen in this process's env:
282
+ # a shell of an earlier boot, or of a fork's source, still holds that one (RFC 0039 § 4.10).
283
+ T=$(cat "$HOME/.spunto/git-token" 2>/dev/null); [ -n "$T" ] || T="$SPUNTO_WORKER_TOKEN"
284
+ A=$(cat "$HOME/.spunto/api-url" 2>/dev/null); [ -n "$A" ] || A="$SPUNTO_API_URL"
281
285
  # No -f: a refusal's body says why (which account the member authorized, what to do about it).
282
- RESPONSE=$(curl -sS -m 20 -X POST "$SPUNTO_API_URL/api/workers/git-credential" -H "Authorization: Bearer $SPUNTO_WORKER_TOKEN" -H "Content-Type: application/json" -d "{\\"repo\\":\\"$REPO\\"}" 2>/dev/null)
286
+ RESPONSE=$(curl -sS -m 20 -X POST "$A/api/workers/git-credential" -H "Authorization: Bearer $T" -H "Content-Type: application/json" -d "{\\"repo\\":\\"$REPO\\"}" 2>/dev/null)
283
287
  PASSWORD=$(printf "%s" "$RESPONSE" | sed -n 's/.*"password":"\\([^"]*\\)".*/\\1/p')
284
288
  if [ -z "$PASSWORD" ]; then
285
289
  REASON=$(printf "%s" "$RESPONSE" | sed -n 's/.*"error":"\\([^"]*\\)".*/\\1/p')