@skydiveai/pi-extensions 0.1.0-beta.262 → 0.1.0-beta.2621

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/dist/index.mjs CHANGED
@@ -2,26 +2,31 @@ import { createRequire } from "node:module";
2
2
  import { DefaultExecutionEventBusManager, DefaultRequestHandler, InMemoryTaskStore } from "@a2a-js/sdk/server";
3
3
  import { UserBuilder, restHandler } from "@a2a-js/sdk/server/express";
4
4
  import { buildAgentCard, chainMiddleware, composeHandlers, createAgentExecutor, createProtocolHandlers, getCurrentTraceparent, logger, mountAt, requestHeaders, requestUrl, webHandlerToMiddleware } from "@skydiveai/pi-server";
5
- import { mkdir, open, readFile, readdir, stat, unlink } from "node:fs/promises";
5
+ import { mkdir, open, readFile, readdir, stat, unlink, writeFile } from "node:fs/promises";
6
6
  import { basename, dirname, join, relative, resolve } from "node:path";
7
- import { z } from "zod";
8
7
  import { pathToFileURL } from "node:url";
8
+ import { z } from "zod";
9
9
  import { CallToolResultSchema } from "@modelcontextprotocol/sdk/types.js";
10
10
  import { Type } from "typebox";
11
11
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
12
- import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
12
+ import { StdioClientTransport, getDefaultEnvironment } from "@modelcontextprotocol/sdk/client/stdio.js";
13
13
  import { StreamableHTTPClientTransport, StreamableHTTPError } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
14
14
  import { UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js";
15
15
  import { Check, Errors } from "typebox/value";
16
+ import { hc } from "hono/client";
16
17
  import { ROOT_CONTEXT, SpanStatusCode, propagation, trace } from "@opentelemetry/api";
17
18
  import { W3CTraceContextPropagator } from "@opentelemetry/core";
18
19
  import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
19
20
  import { Resource } from "@opentelemetry/resources";
20
21
  import { BatchSpanProcessor, NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
21
22
  import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
22
- import { hc } from "hono/client";
23
+ import { execFile } from "node:child_process";
24
+ import { availableParallelism } from "node:os";
25
+ import { promisify } from "node:util";
23
26
  import { parse } from "yaml";
27
+ import { getModels } from "@earendil-works/pi-ai";
24
28
  import { quote } from "shell-quote";
29
+ import { createHash } from "node:crypto";
25
30
  import { createWriteStream } from "node:fs";
26
31
  import { finished } from "node:stream/promises";
27
32
  import { createLocalBashOperations } from "@earendil-works/pi-coding-agent";
@@ -238,167 +243,6 @@ function createHealthHandler({ metadata }) {
238
243
  };
239
244
  }
240
245
  //#endregion
241
- //#region src/extensions/context-management-config.ts
242
- /**
243
- * Configuration for the context-management capability (tool-output trimming
244
- * and client-side microcompact). See the design notes in the extension for
245
- * what each layer does; this module is purely the env → config surface.
246
- *
247
- * The harness is provider-agnostic and runs inside the sandbox, where its
248
- * only config channel is the environment (loaded by the platform env
249
- * middleware before any session starts — see platform-env-middleware.ts). So
250
- * the LaunchDarkly flag `harness-context-management-enabled` is resolved by
251
- * the platform when it provisions the sandbox and passed through as
252
- * `SKYDIVE_CONTEXT_MANAGEMENT`; the individual knobs override the defaults
253
- * below when present.
254
- *
255
- * Resolution is defensive: a malformed value never throws (this config is
256
- * read on the hot path before every LLM call), it falls back to the default
257
- * for that knob and logs once.
258
- */
259
- const log$13 = logger.child({ module: "context-management-config" });
260
- const DEFAULT_CONTEXT_MANAGEMENT_CONFIG = {
261
- enabled: false,
262
- perResultMaxBytes: 16 * 1024,
263
- keepRecentToolResults: 3,
264
- coldCacheGapSeconds: 240,
265
- warmClearTriggerTokens: 5e4,
266
- clearAtLeastTokens: 1e4,
267
- excludeTools: [],
268
- maxModelCallsPerTurn: 80,
269
- nativeAnthropicEdits: false
270
- };
271
- const boolFromEnv = (value, fallback) => {
272
- if (value === void 0) return fallback;
273
- const normalized = value.trim().toLowerCase();
274
- if (normalized === "1" || normalized === "true") return true;
275
- if (normalized === "0" || normalized === "false") return false;
276
- return fallback;
277
- };
278
- const positiveInt = (fallback) => z.coerce.number().int().positive().catch(fallback);
279
- const nonNegativeInt = (fallback) => z.coerce.number().int().nonnegative().catch(fallback);
280
- const configSchema = z.object({
281
- perResultMaxBytes: positiveInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.perResultMaxBytes),
282
- keepRecentToolResults: nonNegativeInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.keepRecentToolResults),
283
- coldCacheGapSeconds: positiveInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.coldCacheGapSeconds),
284
- warmClearTriggerTokens: positiveInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.warmClearTriggerTokens),
285
- clearAtLeastTokens: positiveInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.clearAtLeastTokens),
286
- maxModelCallsPerTurn: positiveInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.maxModelCallsPerTurn)
287
- });
288
- const parseExcludeTools = (value) => {
289
- if (!value) return DEFAULT_CONTEXT_MANAGEMENT_CONFIG.excludeTools;
290
- return value.split(",").map((name) => name.trim()).filter((name) => name.length > 0);
291
- };
292
- /**
293
- * Reads the context-management config from `env` (defaults to `process.env`).
294
- * Never throws — invalid values fall back to defaults.
295
- */
296
- function resolveContextManagementConfig(env = process.env) {
297
- if (!boolFromEnv(env.SKYDIVE_CONTEXT_MANAGEMENT, false)) return { ...DEFAULT_CONTEXT_MANAGEMENT_CONFIG };
298
- const parsed = configSchema.safeParse({
299
- perResultMaxBytes: env.SKYDIVE_CTX_PER_RESULT_MAX_BYTES,
300
- keepRecentToolResults: env.SKYDIVE_CTX_KEEP_RECENT,
301
- coldCacheGapSeconds: env.SKYDIVE_CTX_COLD_GAP_SECONDS,
302
- warmClearTriggerTokens: env.SKYDIVE_CTX_WARM_TRIGGER_TOKENS,
303
- clearAtLeastTokens: env.SKYDIVE_CTX_CLEAR_AT_LEAST_TOKENS,
304
- maxModelCallsPerTurn: env.SKYDIVE_CTX_MAX_MODEL_CALLS
305
- });
306
- if (!parsed.success) {
307
- log$13.warn({
308
- event: "context_management_config_invalid",
309
- err: parsed.error
310
- }, "falling back to default context-management config");
311
- return {
312
- ...DEFAULT_CONTEXT_MANAGEMENT_CONFIG,
313
- enabled: true
314
- };
315
- }
316
- return {
317
- enabled: true,
318
- ...parsed.data,
319
- excludeTools: parseExcludeTools(env.SKYDIVE_CTX_EXCLUDE_TOOLS),
320
- nativeAnthropicEdits: boolFromEnv(env.SKYDIVE_CTX_NATIVE_ANTHROPIC_EDITS, DEFAULT_CONTEXT_MANAGEMENT_CONFIG.nativeAnthropicEdits)
321
- };
322
- }
323
- //#endregion
324
- //#region src/extensions/context-management-runtime.ts
325
- /**
326
- * Live, mutable view of the context-management config.
327
- *
328
- * The env-derived config (context-management-config.ts) is the boot-time
329
- * default. On top of it, the platform can deliver a global on/off at runtime
330
- * via the LaunchDarkly flag `harness-context-management-enabled` — resolved
331
- * server-side and polled by the harness (see the poller in
332
- * context-management.ts). This holder is where that override lands so a flip
333
- * (especially a kill-switch) reaches long-lived sandboxes without a restart.
334
- *
335
- * Only the master `enabled` toggle is overridable at runtime; the per-knob
336
- * tunables stay env-derived. `enabled` resolves to the flag override when the
337
- * platform has reported one, else the env value.
338
- */
339
- let baseConfig = null;
340
- let flagOverride = null;
341
- function base() {
342
- baseConfig ??= resolveContextManagementConfig();
343
- return baseConfig;
344
- }
345
- /** The effective config, with the runtime flag override applied to `enabled`. */
346
- function getContextManagementConfig() {
347
- const resolved = base();
348
- return {
349
- ...resolved,
350
- enabled: flagOverride ?? resolved.enabled
351
- };
352
- }
353
- /**
354
- * Apply the platform-reported flag value. `null` clears the override (fall back
355
- * to the env default) — used when the phone-home result is indeterminate so a
356
- * transient failure never silently changes behavior.
357
- */
358
- function setContextManagementFlagOverride(enabled) {
359
- flagOverride = enabled;
360
- }
361
- /**
362
- * Whether a phone-home channel exists to learn the flag at runtime. When false
363
- * (e.g. a bare local CLI with no platform API), the env value is the only
364
- * source and there's nothing to poll.
365
- */
366
- function hasFlagSource(env = process.env) {
367
- return Boolean(env.SKYDIVE_API_URL ?? env.ANYONE_API_URL);
368
- }
369
- //#endregion
370
- //#region src/iteration-cap.ts
371
- function installIterationCap({ session, log }, configOverride = null) {
372
- const readConfig = () => configOverride ?? getContextManagementConfig();
373
- if (!readConfig().enabled && (configOverride !== null || !hasFlagSource())) return;
374
- const agent = session.agent;
375
- if (typeof agent.createLoopConfig !== "function") throw new Error("installIterationCap: session.agent.createLoopConfig is missing — pi-agent-core internals changed; update iteration-cap.ts.");
376
- const original = agent.createLoopConfig.bind(agent);
377
- agent.createLoopConfig = (options) => {
378
- const loopConfig = original(options);
379
- const previousStop = loopConfig.shouldStopAfterTurn;
380
- let modelCalls = 0;
381
- return {
382
- ...loopConfig,
383
- shouldStopAfterTurn: async (ctx) => {
384
- if (previousStop && await previousStop(ctx)) return true;
385
- const config = readConfig();
386
- if (!config.enabled) return false;
387
- modelCalls += 1;
388
- if (modelCalls >= config.maxModelCallsPerTurn) {
389
- log.warn({
390
- event: "iteration_cap_reached",
391
- modelCalls,
392
- max: config.maxModelCallsPerTurn
393
- }, "reached per-turn model-call cap; stopping turn gracefully");
394
- return true;
395
- }
396
- return false;
397
- }
398
- };
399
- };
400
- }
401
- //#endregion
402
246
  //#region src/extensions/capability-soul-nudge.ts
403
247
  /**
404
248
  * One-line reminder appended to a tool-update continuation when the agent
@@ -415,87 +259,356 @@ function installIterationCap({ session, log }, configOverride = null) {
415
259
  */
416
260
  const CAPABILITY_SOUL_NUDGE = "New capability gained — once the current task is done, if this changes what you can do for the user, record it in `soul.md` so it carries into future conversations rather than being rediscovered from scratch (then commit and push).";
417
261
  //#endregion
418
- //#region src/extensions/local-tools.ts
262
+ //#region src/extensions/tool-call-summary.ts
263
+ const log$15 = logger.child({ module: "tool-call-summary-extension" });
419
264
  /**
420
- * Local-tools adapter as a pi extension. Mirrors the mcp.ts hot-reload pattern
421
- * one level over: walks `tools/` (relative to the harness cwd), imports each
422
- * module, and registers the exported `ToolDefinition`s with pi.
423
- *
424
- * The agent edits files under `tools/` between turns; on every `tool_result`
425
- * the extension stat()s the directory's children, compares mtimes against
426
- * what we last reconciled against, and queues a `pendingLocalToolsUpdate`
427
- * if anything was added/removed/changed. The chat handler drains that queue
428
- * after the current `session.prompt(...)` returns, calls `session.reload()`,
429
- * and injects a synthetic continuation message — same dance as MCP.
430
- *
431
- * **ESM cache-busting.** `import(url)` in Node's ESM loader keys cached
432
- * modules by URL. Re-importing the same path after editing the file gets
433
- * the *original* module back. To force a fresh load on mtime change we
434
- * append `?v=<mtimeMs>` to the URL — different URL, fresh module
435
- * evaluation. The old version stays in memory but is unreachable.
436
- *
437
- * Each `.ts`/`.mjs`/`.js` file's default export should be a `ToolDefinition`
438
- * or `ToolDefinition[]`. Files starting with `_` or `.` are skipped, so
439
- * `tools/_example.ts` documents the shape without registering.
265
+ * The injected parameter name: a namespaced sentinel, so it can never collide
266
+ * with a real tool argument and is unmistakable in transcripts and logs. The
267
+ * frontend renderer (ANY-2723) duplicates this literal — keep the two in sync.
440
268
  */
441
- const log$12 = logger.child({ module: "local-tools-extension" });
442
- const TOOLS_DIRNAME = "tools";
443
- const fileState = /* @__PURE__ */ new Map();
444
- let pendingLocalToolsUpdate = null;
445
- function consumePendingLocalToolsUpdate() {
446
- const update = pendingLocalToolsUpdate;
447
- pendingLocalToolsUpdate = null;
448
- return update;
449
- }
269
+ const TOOL_CALL_SUMMARY_FIELD = "__skydive_summary__";
270
+ /** JSON Schema fragment for the injected parameter. */
271
+ const SUMMARY_PROPERTY = {
272
+ type: "string",
273
+ description: "Required for every tool call. A concise, specific summary (max ~8 words) of what THIS call does and why, written for a person watching the conversation, e.g. \"Searching feedback for billing complaints\" or \"Reading the auth middleware\". Address the user directly in second person: the summary is read by the user, so refer to their things as \"your\", never in third person — \"Reading your emails\", not \"Reading his emails\". Always use the present progressive tense, since it is shown while the call runs: \"Updating your Slack\", never \"Updated your Slack\". Make each summary distinct from your other tool calls; never reuse a generic label like \"Search query\" or \"Running command\"."
274
+ };
275
+ const jsonSchemaObjectSchema = z.object({
276
+ type: z.unknown().optional(),
277
+ properties: z.record(z.string(), z.unknown()).optional(),
278
+ required: z.array(z.string()).optional(),
279
+ additionalProperties: z.unknown().optional()
280
+ }).passthrough();
281
+ const toolEntrySchema = z.object({
282
+ name: z.string().optional(),
283
+ input_schema: jsonSchemaObjectSchema.optional(),
284
+ parameters: jsonSchemaObjectSchema.optional(),
285
+ function: z.object({
286
+ name: z.string().optional(),
287
+ parameters: jsonSchemaObjectSchema.optional()
288
+ }).passthrough().optional()
289
+ }).passthrough();
290
+ const payloadWithToolsSchema = z.object({ tools: z.array(z.unknown()) }).passthrough();
450
291
  /**
451
- * Non-consuming peek used by the agent loop's `shouldStopAfterTurn` hook to
452
- * decide whether to end the current `prompt()` after the in-flight turn so a
453
- * fresh tool snapshot can be taken. The drain still happens in postPrompt via
454
- * `consumePendingLocalToolsUpdate`.
292
+ * Add the summary property to one JSON Schema object. Returns the augmented
293
+ * copy, or `null` when the tool should be left untouched: a strict schema
294
+ * (`additionalProperties: false`) whose validation would reject the extra
295
+ * field, or one that already declares a `__skydive_summary__` property of its own.
455
296
  */
456
- function hasPendingLocalToolsUpdate() {
457
- return pendingLocalToolsUpdate !== null;
458
- }
459
- function formatLocalToolsUpdateMessage(summary) {
460
- const lines = ["[system] Your local tools/ inventory changed during the previous turn. Your tool list is now updated; act on the new set rather than what was visible before."];
461
- if (summary.added.length > 0) lines.push(`Newly loaded tool files: ${summary.added.join(", ")}`);
462
- if (summary.refreshed.length > 0) lines.push(`Refreshed tool files: ${summary.refreshed.join(", ")}`);
463
- if (summary.removed.length > 0) lines.push(`Removed tool files (and their tools): ${summary.removed.join(", ")}`);
464
- if (summary.errors.length > 0) {
465
- lines.push("Errors:");
466
- for (const e of summary.errors) lines.push(` - ${e.file}: ${e.message}`);
467
- }
468
- if (summary.added.length > 0) lines.push(CAPABILITY_SOUL_NUDGE);
469
- lines.push("Continue from where you left off, using the current tool list. Do not re-do work that already succeeded last turn.");
470
- return lines.join("\n");
471
- }
472
- function isToolDefinition(x) {
473
- return !!x && typeof x === "object" && typeof x.name === "string" && typeof x.execute === "function";
297
+ function augmentSchema(schema) {
298
+ if (schema.additionalProperties === false) return null;
299
+ const properties = schema.properties ?? {};
300
+ if ("__skydive_summary__" in properties) return null;
301
+ const required = schema.required ?? [];
302
+ return {
303
+ ...schema,
304
+ type: schema.type ?? "object",
305
+ properties: {
306
+ [TOOL_CALL_SUMMARY_FIELD]: SUMMARY_PROPERTY,
307
+ ...properties
308
+ },
309
+ required: required.includes("__skydive_summary__") ? required : [...required, TOOL_CALL_SUMMARY_FIELD]
310
+ };
474
311
  }
475
312
  /**
476
- * Pi's system-prompt builder filters its visible-tool list to entries with a
477
- * non-empty `promptSnippet` (system-prompt.js:49) — a tool with only a
478
- * `description` won't appear in the system prompt's "available tools"
479
- * section, which biases the LLM against using it. If the local tool author
480
- * didn't set a snippet, default to the tool's description so the tool stays
481
- * visible.
313
+ * Augment a single tool entry, dispatching on which provider shape it is.
314
+ * Returns the (possibly rebuilt) entry and whether anything changed. Skipped
315
+ * tools — wrong shape, strict, or name in `strictToolNames` — return unchanged.
482
316
  */
483
- function withDefaultPromptSnippet(tool) {
484
- if (typeof tool.promptSnippet === "string" && tool.promptSnippet.trim()) return tool;
485
- const fallback = tool.description?.trim();
486
- if (!fallback) return tool;
487
- return {
488
- ...tool,
489
- promptSnippet: fallback
317
+ function augmentToolEntry(entry, strictToolNames) {
318
+ const parsed = toolEntrySchema.safeParse(entry);
319
+ if (!parsed.success) return {
320
+ entry,
321
+ changed: false
490
322
  };
491
- }
492
- async function listToolFiles(dir) {
493
- let entries;
494
- try {
495
- entries = await readdir(dir);
496
- } catch (err) {
497
- if (err?.code === "ENOENT") return [];
498
- throw err;
323
+ const tool = parsed.data;
324
+ const name = tool.name ?? tool.function?.name ?? null;
325
+ if (name !== null && strictToolNames.has(name)) return {
326
+ entry,
327
+ changed: false
328
+ };
329
+ if (tool.input_schema) {
330
+ const augmented = augmentSchema(tool.input_schema);
331
+ if (!augmented) return {
332
+ entry,
333
+ changed: false
334
+ };
335
+ return {
336
+ entry: {
337
+ ...tool,
338
+ input_schema: augmented
339
+ },
340
+ changed: true
341
+ };
342
+ }
343
+ if (tool.parameters) {
344
+ const augmented = augmentSchema(tool.parameters);
345
+ if (!augmented) return {
346
+ entry,
347
+ changed: false
348
+ };
349
+ return {
350
+ entry: {
351
+ ...tool,
352
+ parameters: augmented
353
+ },
354
+ changed: true
355
+ };
356
+ }
357
+ if (tool.function?.parameters) {
358
+ const augmented = augmentSchema(tool.function.parameters);
359
+ if (!augmented) return {
360
+ entry,
361
+ changed: false
362
+ };
363
+ return {
364
+ entry: {
365
+ ...tool,
366
+ function: {
367
+ ...tool.function,
368
+ parameters: augmented
369
+ }
370
+ },
371
+ changed: true
372
+ };
373
+ }
374
+ return {
375
+ entry,
376
+ changed: false
377
+ };
378
+ }
379
+ /**
380
+ * Inject the summary field into every eligible tool in a provider payload.
381
+ * Returns a new payload when at least one tool was augmented, or `undefined`
382
+ * to signal "no change" (which keeps the original payload, per the
383
+ * `before_provider_request` contract).
384
+ *
385
+ * @param payload The outgoing provider payload (shape varies by provider).
386
+ * @param strictToolNames Names of tools whose registered schema is strict and
387
+ * must be skipped to avoid validation errors.
388
+ */
389
+ function injectToolCallSummary(payload, strictToolNames) {
390
+ const parsed = payloadWithToolsSchema.safeParse(payload);
391
+ if (!parsed.success || parsed.data.tools.length === 0) return void 0;
392
+ let changed = false;
393
+ const tools = parsed.data.tools.map((entry) => {
394
+ const result = augmentToolEntry(entry, strictToolNames);
395
+ if (result.changed) changed = true;
396
+ return result.entry;
397
+ });
398
+ if (!changed) return void 0;
399
+ return {
400
+ ...parsed.data,
401
+ tools
402
+ };
403
+ }
404
+ /**
405
+ * Names of registered tools whose schema sets `additionalProperties: false`.
406
+ * Pi validates the model's tool args against this registered schema, so the
407
+ * injected field would make a strict tool's call fail validation — skip them.
408
+ */
409
+ function getStrictToolNames(pi) {
410
+ const names = /* @__PURE__ */ new Set();
411
+ for (const tool of pi.getAllTools()) {
412
+ const parsed = jsonSchemaObjectSchema.safeParse(tool.parameters);
413
+ if (parsed.success && parsed.data.additionalProperties === false) names.add(tool.name);
414
+ }
415
+ return names;
416
+ }
417
+ /**
418
+ * Whether a tool's own schema declares a `__skydive_summary__` property. We
419
+ * never inject into such a tool, so any value it carries is a real argument
420
+ * and must be left alone.
421
+ */
422
+ function schemaDeclaresSummary(parameters) {
423
+ const parsed = jsonSchemaObjectSchema.safeParse(parameters);
424
+ return parsed.success && parsed.data.properties != null && "__skydive_summary__" in parsed.data.properties;
425
+ }
426
+ function toolDeclaresSummaryParam(pi, toolName) {
427
+ const tool = pi.getAllTools().find((candidate) => candidate.name === toolName);
428
+ if (!tool) return false;
429
+ return schemaDeclaresSummary(tool.parameters);
430
+ }
431
+ /** A copy of `args` without the injected summary. Never mutates its input. */
432
+ function withoutInjectedSummary(args) {
433
+ if (args == null || typeof args !== "object" || Array.isArray(args)) return args;
434
+ if (!("__skydive_summary__" in args)) return args;
435
+ const { [TOOL_CALL_SUMMARY_FIELD]: _summary, ...rest } = args;
436
+ return rest;
437
+ }
438
+ /**
439
+ * Register a tool so the injected summary is removed *before* pi validates
440
+ * the model's arguments against the tool's schema.
441
+ *
442
+ * Needed because the `tool_call` strip below runs too late. pi-agent-core's
443
+ * `prepareToolCall` goes: `tool.prepareArguments` → `validateToolArguments` →
444
+ * `beforeToolCall` (which is what dispatches `tool_call`). A tool whose schema
445
+ * sets `additionalProperties: false` therefore rejects the summary and errors
446
+ * the call — client-side, before any request reaches the server — while the
447
+ * strip meant to prevent exactly that sits one step further down.
448
+ *
449
+ * `augmentSchema` already skips strict tools, but that only controls what the
450
+ * model is *told*. It still emits the field on those tools, because every
451
+ * other tool in the list declares it as required for every call. So a server
452
+ * can connect, bind its tools, present a healthy inventory, and have every
453
+ * call fail — and it reads as the server's fault when it is ours.
454
+ *
455
+ * Apply to tools registered from schemas we do not author: MCP servers and
456
+ * local `tools/*.ts`. Tools built here with `Type.Object(...)` do not need it
457
+ * (TypeBox emits no `additionalProperties`, so the field validates fine and
458
+ * the `tool_call` strip removes it in time).
459
+ *
460
+ * Fails open, like the rest of this module: if the strip throws, the original
461
+ * arguments are used rather than failing the call.
462
+ */
463
+ function withSummaryStrippedBeforeValidation(tool) {
464
+ if (schemaDeclaresSummary(tool.parameters)) return tool;
465
+ const toolPrepare = tool.prepareArguments;
466
+ const prepareArguments = ((args) => {
467
+ let stripped = args;
468
+ try {
469
+ stripped = withoutInjectedSummary(args);
470
+ } catch (err) {
471
+ log$15.error({
472
+ err,
473
+ event: "tool_call_summary_prepare_strip_failed",
474
+ toolName: tool.name
475
+ }, "tool_call_summary pre-validation strip failed; leaving arguments untouched");
476
+ }
477
+ return toolPrepare ? toolPrepare(stripped) : stripped;
478
+ });
479
+ return {
480
+ ...tool,
481
+ prepareArguments
482
+ };
483
+ }
484
+ /**
485
+ * Remove the injected summary from a tool's execution input. No-op when the
486
+ * field is absent, or when the tool genuinely declares a `__skydive_summary__`
487
+ * parameter of its own (which we never inject into, so its value is real).
488
+ * Mutates `input` in place, matching the `tool_call` contract.
489
+ *
490
+ * Fails open: this runs on the critical path of tool execution, and the
491
+ * `getAllTools()` lookup can throw. On any error we leave `input` untouched
492
+ * (the sentinel may pass through to the tool, but a bug here can never break
493
+ * tool execution).
494
+ */
495
+ function stripInjectedSummary(pi, toolName, input) {
496
+ try {
497
+ if (!("__skydive_summary__" in input)) return;
498
+ if (toolDeclaresSummaryParam(pi, toolName)) return;
499
+ delete input[TOOL_CALL_SUMMARY_FIELD];
500
+ } catch (err) {
501
+ log$15.error({
502
+ err,
503
+ event: "tool_call_summary_strip_failed",
504
+ toolName
505
+ }, "tool_call_summary strip failed; leaving tool input untouched");
506
+ }
507
+ }
508
+ /**
509
+ * Compute the rewritten payload for a `before_provider_request` event, failing
510
+ * open: on any error the original payload is left untouched so a bug here can
511
+ * never break an LLM call.
512
+ */
513
+ function buildInjectedPayload(pi, payload) {
514
+ try {
515
+ return injectToolCallSummary(payload, getStrictToolNames(pi));
516
+ } catch (err) {
517
+ log$15.error({
518
+ err,
519
+ event: "tool_call_summary_injection_failed"
520
+ }, "tool_call_summary injection failed; passing payload through unchanged");
521
+ return;
522
+ }
523
+ }
524
+ const toolCallSummaryExtension = (pi) => {
525
+ pi.on("before_provider_request", (event) => buildInjectedPayload(pi, event.payload));
526
+ pi.on("tool_call", (event) => {
527
+ stripInjectedSummary(pi, event.toolName, event.input);
528
+ });
529
+ };
530
+ //#endregion
531
+ //#region src/extensions/local-tools.ts
532
+ /**
533
+ * Local-tools adapter as a pi extension. Mirrors the mcp.ts hot-reload pattern
534
+ * one level over: walks `tools/` (relative to the harness cwd), imports each
535
+ * module, and registers the exported `ToolDefinition`s with pi.
536
+ *
537
+ * The agent edits files under `tools/` between turns; on every `tool_result`
538
+ * the extension stat()s the directory's children, compares mtimes against
539
+ * what we last reconciled against, and queues a `pendingLocalToolsUpdate`
540
+ * if anything was added/removed/changed. The chat handler drains that queue
541
+ * after the current `session.prompt(...)` returns, calls `session.reload()`,
542
+ * and injects a synthetic continuation message — same dance as MCP.
543
+ *
544
+ * **ESM cache-busting.** `import(url)` in Node's ESM loader keys cached
545
+ * modules by URL. Re-importing the same path after editing the file gets
546
+ * the *original* module back. To force a fresh load on mtime change we
547
+ * append `?v=<mtimeMs>` to the URL — different URL, fresh module
548
+ * evaluation. The old version stays in memory but is unreachable.
549
+ *
550
+ * Each `.ts`/`.mjs`/`.js` file's default export should be a `ToolDefinition`
551
+ * or `ToolDefinition[]`. Files starting with `_` or `.` are skipped, so
552
+ * `tools/_example.ts` documents the shape without registering.
553
+ */
554
+ const log$14 = logger.child({ module: "local-tools-extension" });
555
+ const TOOLS_DIRNAME = "tools";
556
+ const fileState = /* @__PURE__ */ new Map();
557
+ let pendingLocalToolsUpdate = null;
558
+ function consumePendingLocalToolsUpdate() {
559
+ const update = pendingLocalToolsUpdate;
560
+ pendingLocalToolsUpdate = null;
561
+ return update;
562
+ }
563
+ /**
564
+ * Non-consuming peek used by the agent loop's `shouldStopAfterTurn` hook to
565
+ * decide whether to end the current `prompt()` after the in-flight turn so a
566
+ * fresh tool snapshot can be taken. The drain still happens in postPrompt via
567
+ * `consumePendingLocalToolsUpdate`.
568
+ */
569
+ function hasPendingLocalToolsUpdate() {
570
+ return pendingLocalToolsUpdate !== null;
571
+ }
572
+ function formatLocalToolsUpdateMessage(summary) {
573
+ const lines = ["[system] Your local tools/ inventory changed during the previous turn. Your tool list is now updated; act on the new set rather than what was visible before."];
574
+ if (summary.added.length > 0) lines.push(`Newly loaded tool files: ${summary.added.join(", ")}`);
575
+ if (summary.refreshed.length > 0) lines.push(`Refreshed tool files: ${summary.refreshed.join(", ")}`);
576
+ if (summary.removed.length > 0) lines.push(`Removed tool files (and their tools): ${summary.removed.join(", ")}`);
577
+ if (summary.errors.length > 0) {
578
+ lines.push("Errors:");
579
+ for (const e of summary.errors) lines.push(` - ${e.file}: ${e.message}`);
580
+ }
581
+ if (summary.added.length > 0) lines.push(CAPABILITY_SOUL_NUDGE);
582
+ lines.push("Continue from where you left off, using the current tool list. Do not re-do work that already succeeded last turn.");
583
+ return lines.join("\n");
584
+ }
585
+ function isToolDefinition(x) {
586
+ return !!x && typeof x === "object" && typeof x.name === "string" && typeof x.execute === "function";
587
+ }
588
+ /**
589
+ * Pi's system-prompt builder filters its visible-tool list to entries with a
590
+ * non-empty `promptSnippet` (system-prompt.js:49) — a tool with only a
591
+ * `description` won't appear in the system prompt's "available tools"
592
+ * section, which biases the LLM against using it. If the local tool author
593
+ * didn't set a snippet, default to the tool's description so the tool stays
594
+ * visible.
595
+ */
596
+ function withDefaultPromptSnippet(tool) {
597
+ if (typeof tool.promptSnippet === "string" && tool.promptSnippet.trim()) return tool;
598
+ const fallback = tool.description?.trim();
599
+ if (!fallback) return tool;
600
+ return {
601
+ ...tool,
602
+ promptSnippet: fallback
603
+ };
604
+ }
605
+ async function listToolFiles(dir) {
606
+ let entries;
607
+ try {
608
+ entries = await readdir(dir);
609
+ } catch (err) {
610
+ if (err?.code === "ENOENT") return [];
611
+ throw err;
499
612
  }
500
613
  const out = [];
501
614
  for (const file of entries) {
@@ -565,7 +678,7 @@ async function reconcileLocalTools({ pi, dir }) {
565
678
  action = existing ? "refreshed" : "added";
566
679
  }
567
680
  for (const tool of tools) {
568
- pi.registerTool(withDefaultPromptSnippet(tool));
681
+ pi.registerTool(withSummaryStrippedBeforeValidation(withDefaultPromptSnippet(tool)));
569
682
  summary.totalTools++;
570
683
  }
571
684
  if (action === "added") summary.added.push(file);
@@ -582,7 +695,7 @@ async function reconcileAndQueue({ pi, dir, reason }) {
582
695
  dir
583
696
  });
584
697
  if (reason !== "session_start" && summaryHasChanges$1(summary)) pendingLocalToolsUpdate = summary;
585
- log$12.info({
698
+ log$14.info({
586
699
  event: "local_tools_reconcile",
587
700
  reason,
588
701
  total_tools: summary.totalTools,
@@ -604,7 +717,7 @@ const localToolsExtension = (pi) => {
604
717
  reason: "session_start"
605
718
  });
606
719
  } catch (err) {
607
- log$12.error({
720
+ log$14.error({
608
721
  err,
609
722
  event: "local_tools_reconcile_failed"
610
723
  }, "local tools reconcile failed");
@@ -616,7 +729,7 @@ const localToolsExtension = (pi) => {
616
729
  try {
617
730
  current = await listToolFiles(dir);
618
731
  } catch (err) {
619
- log$12.warn({
732
+ log$14.warn({
620
733
  err,
621
734
  event: "local_tools_listing_failed"
622
735
  }, "tools/ listing failed");
@@ -638,7 +751,7 @@ const localToolsExtension = (pi) => {
638
751
  reason: "auto_reload"
639
752
  });
640
753
  } catch (err) {
641
- log$12.error({
754
+ log$14.error({
642
755
  err,
643
756
  event: "local_tools_auto_reload_failed"
644
757
  }, "auto-reload after tools/ change failed");
@@ -682,16 +795,101 @@ function createStderrBuffer({ maxBytes }) {
682
795
  * The transport stays alive — caller holds the client so the bridge's
683
796
  * callback server keeps listening and a later reconcile re-probes.
684
797
  */
798
+ var SkipOutputSchemaValidation = class {
799
+ getValidator(_schema) {
800
+ return (input) => ({
801
+ valid: true,
802
+ data: input,
803
+ errorMessage: void 0
804
+ });
805
+ }
806
+ };
807
+ const skipOutputSchemaValidation = new SkipOutputSchemaValidation();
685
808
  const DEFAULT_CONNECT_TIMEOUT_MS = 5e3;
686
809
  const STDERR_BUFFER_BYTES = 4096;
810
+ const SESSION_NOT_FOUND_RETRY_DELAYS_MS = [
811
+ 250,
812
+ 500,
813
+ 1e3,
814
+ 2e3,
815
+ 4e3,
816
+ 8e3
817
+ ];
818
+ function delay(ms) {
819
+ return new Promise((resolve) => setTimeout(resolve, ms));
820
+ }
821
+ async function fetchWithSessionNotFoundRetry(input, init) {
822
+ for (let attempt = 0;; attempt += 1) {
823
+ const response = await fetch(input, init);
824
+ const sessionId = new Headers(init?.headers).get("mcp-session-id");
825
+ const retryDelay = SESSION_NOT_FOUND_RETRY_DELAYS_MS[attempt];
826
+ if (!sessionId || response.status !== 404 || retryDelay === void 0 || !(await response.clone().text()).includes("session_not_found")) return response;
827
+ await response.body?.cancel();
828
+ await delay(retryDelay);
829
+ }
830
+ }
687
831
  /**
688
- * undici's fetch throws `TypeError: fetch failed` with the actual
689
- * network error hung off `.cause` (e.g. `getaddrinfo ENOTFOUND ...`,
690
- * `ECONNREFUSED`, TLS errors). Surfacing only `err.message` makes
691
- * every network failure read as a bare "fetch failed", which is
692
- * indistinguishable from any other transport problem. Walk the cause
693
- * chain so the agent sees the real underlying error.
832
+ * Sandbox egress variables a stdio MCP server needs to make outbound HTTPS
833
+ * calls. The SDK's default child environment is a tiny whitelist (HOME,
834
+ * LOGNAME, PATH, SHELL, TERM, USER on Linux), which drops NODE_EXTRA_CA_CERTS
835
+ * — but the sandbox's egress proxy terminates TLS with a private CA, so a
836
+ * child spawned without it fails every HTTPS request with a bare
837
+ * `fetch failed` (cause: UNABLE_TO_VERIFY_LEAF_SIGNATURE) that looks like the
838
+ * remote service is down. Same story for SSL_CERT_FILE (OpenSSL-based
839
+ * runtimes) and the *_PROXY set. Inherit them from this process so a stdio
840
+ * server gets working egress like every other process in the sandbox.
694
841
  */
842
+ const INHERITED_EGRESS_ENV_VARS = [
843
+ "NODE_EXTRA_CA_CERTS",
844
+ "SSL_CERT_FILE",
845
+ "SSL_CERT_DIR",
846
+ "REQUESTS_CA_BUNDLE",
847
+ "CURL_CA_BUNDLE",
848
+ "HTTP_PROXY",
849
+ "HTTPS_PROXY",
850
+ "NO_PROXY",
851
+ "http_proxy",
852
+ "https_proxy",
853
+ "no_proxy"
854
+ ];
855
+ /**
856
+ * Environment for a stdio MCP server child: the SDK's safe defaults, plus the
857
+ * sandbox's egress/TLS variables, plus (last, so it wins) the server's own
858
+ * configured `env`. Building the merge here — instead of only when `env` is
859
+ * unset — also fixes the workaround trap where supplying any `env` in
860
+ * mcp.config.json silently replaced the ENTIRE default set, so a config that
861
+ * added one API key lost PATH/HOME and the server failed to spawn at all.
862
+ */
863
+ function buildStdioEnv(configured, processEnv = process.env) {
864
+ const env = { ...getDefaultEnvironment() };
865
+ for (const key of INHERITED_EGRESS_ENV_VARS) {
866
+ const value = processEnv[key];
867
+ if (value !== void 0) env[key] = value;
868
+ }
869
+ if (configured) Object.assign(env, configured);
870
+ return env;
871
+ }
872
+ /**
873
+ * undici's fetch throws `TypeError: fetch failed` with the actual
874
+ * network error hung off `.cause` (e.g. `getaddrinfo ENOTFOUND ...`,
875
+ * `ECONNREFUSED`, TLS errors). Surfacing only `err.message` makes
876
+ * every network failure read as a bare "fetch failed", which is
877
+ * indistinguishable from any other transport problem. Walk the cause
878
+ * chain so the agent sees the real underlying error.
879
+ */
880
+ /**
881
+ * True when an error from an http MCP transport (connect, listTools, or a tool
882
+ * call) is an authentication failure. With no authProvider configured the SDK
883
+ * surfaces a 401 as `StreamableHTTPError(401)`; older paths translate it to
884
+ * `UnauthorizedError`. A dead/expired OAuth token (the proxy can no longer
885
+ * mint one) shows up here on the NEXT request against a previously-connected
886
+ * client — not just at connect — so reconcile must re-classify such a failure
887
+ * as `pending_auth` instead of a generic `failed`, keeping the "waiting on
888
+ * auth" report consistent with `platform auth`.
889
+ */
890
+ function isUnauthorizedError(err) {
891
+ return err instanceof UnauthorizedError || err instanceof StreamableHTTPError && err.code === 401;
892
+ }
695
893
  function formatError(err) {
696
894
  if (!(err instanceof Error)) return String(err);
697
895
  const parts = [err.message];
@@ -703,8 +901,20 @@ function formatError(err) {
703
901
  }
704
902
  return parts.join(": ");
705
903
  }
904
+ function probeAlive(pid) {
905
+ if (pid === null) return null;
906
+ try {
907
+ process.kill(pid, 0);
908
+ return true;
909
+ } catch {
910
+ return false;
911
+ }
912
+ }
706
913
  async function connectHttp(_id, config, client) {
707
- const transport = new StreamableHTTPClientTransport(new URL(config.url), { ...config.headers !== null ? { requestInit: { headers: config.headers } } : {} });
914
+ const transport = new StreamableHTTPClientTransport(new URL(config.url), {
915
+ ...config.headers !== null ? { requestInit: { headers: config.headers } } : {},
916
+ fetch: fetchWithSessionNotFoundRetry
917
+ });
708
918
  try {
709
919
  await client.connect(transport);
710
920
  return {
@@ -713,12 +923,12 @@ async function connectHttp(_id, config, client) {
713
923
  stderr: null
714
924
  };
715
925
  } catch (err) {
716
- if (err instanceof UnauthorizedError || err instanceof StreamableHTTPError && err.code === 401) return {
926
+ if (isUnauthorizedError(err)) return {
717
927
  status: "pending_auth",
718
928
  client,
719
929
  stderr: "",
720
930
  stderrBuffer: null,
721
- cliHint: `platform auth mcp ${config.url}`
931
+ cliHint: `platform auth mcp ${config.url} --service "<Product>"`
722
932
  };
723
933
  return {
724
934
  status: "failed",
@@ -732,14 +942,17 @@ async function connectClient(id, config, opts = {}) {
732
942
  const client = new Client({
733
943
  name: "skydive-harness",
734
944
  version: "0.1.0"
735
- }, { capabilities: {} });
945
+ }, {
946
+ capabilities: {},
947
+ jsonSchemaValidator: skipOutputSchemaValidation
948
+ });
736
949
  if (config.transport === "http") return connectHttp(id, config, client);
737
950
  const params = {
738
951
  command: config.command,
739
952
  args: config.args,
740
- stderr: "pipe"
953
+ stderr: "pipe",
954
+ env: buildStdioEnv(config.env)
741
955
  };
742
- if (config.env !== null) params.env = config.env;
743
956
  if (config.cwd !== null) params.cwd = config.cwd;
744
957
  const transport = new StdioClientTransport(params);
745
958
  const stderrBuffer = createStderrBuffer({ maxBytes: STDERR_BUFFER_BYTES });
@@ -751,6 +964,10 @@ async function connectClient(id, config, opts = {}) {
751
964
  resolve();
752
965
  };
753
966
  });
967
+ let stdoutMessages = 0;
968
+ transport.onmessage = () => {
969
+ if (stdoutMessages < 1e4) stdoutMessages += 1;
970
+ };
754
971
  const connectPromise = client.connect(transport);
755
972
  const TIMEOUT_SENTINEL = Symbol("timeout");
756
973
  const result = await Promise.race([
@@ -768,12 +985,20 @@ async function connectClient(id, config, opts = {}) {
768
985
  error: `server "${id}" exited before completing initialize`,
769
986
  stderr: stderrBuffer.read()
770
987
  };
771
- if (result === TIMEOUT_SENTINEL) return {
772
- status: "timeout",
773
- client,
774
- stderr: stderrBuffer.read(),
775
- stderrBuffer
776
- };
988
+ if (result === TIMEOUT_SENTINEL) {
989
+ const pid = transport.pid;
990
+ return {
991
+ status: "timeout",
992
+ client,
993
+ stderr: stderrBuffer.read(),
994
+ stderrBuffer,
995
+ diagnostics: {
996
+ pid,
997
+ alive: probeAlive(pid),
998
+ stdoutMessages
999
+ }
1000
+ };
1001
+ }
777
1002
  try {
778
1003
  await client.close();
779
1004
  } catch {}
@@ -784,6 +1009,126 @@ async function connectClient(id, config, opts = {}) {
784
1009
  };
785
1010
  }
786
1011
  //#endregion
1012
+ //#region src/extensions/mcp/limits.ts
1013
+ /**
1014
+ * Tool-budget guardrails for MCP registration.
1015
+ *
1016
+ * Why this exists: the agent loop sends the *full* tool list — every tool's
1017
+ * name, description, and JSON-schema `parameters` — to the model on every
1018
+ * prompt. MCP servers add tools without bound: a handful of chatty servers (or
1019
+ * one server that exposes 100+ tools, or a few tools with enormous schemas) can
1020
+ * push the registered set past what the model can accept, and the request is
1021
+ * rejected before the turn even runs. Because `reconcile` re-registers from
1022
+ * `mcp.config.json` on *every* turn, an over-limit config bricks the harness on
1023
+ * a loop — the agent can't get a turn to run in order to edit the config back
1024
+ * down. Worse, the person can't tell *why*: tools just stop working.
1025
+ *
1026
+ * What actually overflows the request is *tokens*, not tool count — a few tools
1027
+ * with deeply-nested schemas and long descriptions cost more than a hundred
1028
+ * trivial ones. And how many tokens are safe depends on the *model*: a 200K
1029
+ * context window can afford far more tool surface than a 32K one. So the primary
1030
+ * limiter is a **token budget derived from the active model's context window**,
1031
+ * with a fixed tool-count cap as a coarse secondary guard (and the fallback
1032
+ * when the model — hence its window — isn't known at reconcile time).
1033
+ *
1034
+ * The fix is to make registration bounded and fail-soft. We register in
1035
+ * deterministic config order and stop before we blow the budget, recording how
1036
+ * much we dropped so the agent is *told* it hit the limit and which servers
1037
+ * were truncated. A config that would have bricked the harness now degrades to
1038
+ * "a bounded set of tools plus a loud warning", which the agent can act on by
1039
+ * pruning servers.
1040
+ *
1041
+ * Everything is env-overridable so the ceilings can be tuned per deployment
1042
+ * without a release, but ships with conservative defaults. A cap value of 0 (or
1043
+ * a non-finite / negative override) disables that cap — an explicit escape
1044
+ * hatch, not the default.
1045
+ */
1046
+ const env = process.env;
1047
+ /**
1048
+ * Fraction of the model's context window we're willing to spend on MCP tool
1049
+ * schemas. Tool definitions are sent on every prompt, so they permanently eat
1050
+ * into the window available for the conversation, but a generous tool budget is
1051
+ * worth more than a marginally larger conversation window given how the harness
1052
+ * is used. 0.35 of a 200K window is ~70K tokens of tool schema, comfortably
1053
+ * more than any sane MCP setup; of a 32K window it's ~11.2K, which still forces
1054
+ * truncation before a small model chokes.
1055
+ */
1056
+ const DEFAULT_MCP_TOOL_TOKEN_BUDGET_FRACTION = .35;
1057
+ /**
1058
+ * Floor for the token budget when the model's context window is unknown at
1059
+ * reconcile time (e.g. the model hasn't been resolved yet). Generous enough not
1060
+ * to truncate an ordinary tool set, low enough to still catch a runaway.
1061
+ */
1062
+ const DEFAULT_MCP_TOOL_TOKEN_BUDGET_FLOOR = 16e3;
1063
+ /**
1064
+ * Read a positive-integer cap from an env var, falling back to `fallback`.
1065
+ * A `0` override (or any non-finite / negative value) means "no cap" and is
1066
+ * returned as `Infinity`, so callers can compare against it directly.
1067
+ */
1068
+ function readCap(raw, fallback) {
1069
+ if (raw === void 0 || raw.trim() === "") return fallback;
1070
+ const parsed = Number(raw);
1071
+ if (!Number.isFinite(parsed) || parsed < 0) return Number.POSITIVE_INFINITY;
1072
+ if (parsed === 0) return Number.POSITIVE_INFINITY;
1073
+ return Math.floor(parsed);
1074
+ }
1075
+ /** Read a fraction in (0, 1] from an env var, falling back to `fallback`. */
1076
+ function readFraction(raw, fallback) {
1077
+ if (raw === void 0 || raw.trim() === "") return fallback;
1078
+ const parsed = Number(raw);
1079
+ if (!Number.isFinite(parsed) || parsed <= 0 || parsed > 1) return fallback;
1080
+ return parsed;
1081
+ }
1082
+ /** Read a non-negative integer from an env var, falling back to `fallback`. */
1083
+ function readNonNegativeInt(raw, fallback) {
1084
+ if (raw === void 0 || raw.trim() === "") return fallback;
1085
+ const parsed = Number(raw);
1086
+ if (!Number.isFinite(parsed) || parsed < 0) return fallback;
1087
+ return Math.floor(parsed);
1088
+ }
1089
+ /**
1090
+ * Resolve the active caps from the environment. Read once per reconcile so a
1091
+ * deployment can retune without a restart, cheap enough not to cache.
1092
+ */
1093
+ function resolveMcpToolLimits() {
1094
+ return {
1095
+ maxTotalTools: readCap(env["SKYDIVE_MCP_MAX_TOTAL_TOOLS"], 128),
1096
+ maxToolsPerServer: readCap(env["SKYDIVE_MCP_MAX_TOOLS_PER_SERVER"], 50),
1097
+ tokenBudgetFraction: readFraction(env["SKYDIVE_MCP_TOOL_TOKEN_BUDGET_FRACTION"], DEFAULT_MCP_TOOL_TOKEN_BUDGET_FRACTION),
1098
+ tokenBudgetFloor: readNonNegativeInt(env["SKYDIVE_MCP_TOOL_TOKEN_BUDGET_FLOOR"], DEFAULT_MCP_TOOL_TOKEN_BUDGET_FLOOR)
1099
+ };
1100
+ }
1101
+ /**
1102
+ * The token budget for MCP tool schemas, given the active model's context
1103
+ * window (or undefined when the model isn't known yet).
1104
+ *
1105
+ * With a known window we spend `tokenBudgetFraction` of it, but never less than
1106
+ * the floor — a tiny window shouldn't collapse the budget to near-zero and
1107
+ * strand every tool. With no window we fall back to the floor outright.
1108
+ */
1109
+ function resolveTokenBudget(limits, contextWindow) {
1110
+ if (contextWindow === void 0 || !Number.isFinite(contextWindow)) return limits.tokenBudgetFloor;
1111
+ return Math.max(limits.tokenBudgetFloor, Math.floor(contextWindow * limits.tokenBudgetFraction));
1112
+ }
1113
+ /**
1114
+ * Estimate the tokens an MCP tool's *definition* costs in the request. We
1115
+ * serialize what's actually sent to the model — the tool name, its description,
1116
+ * and its JSON-schema parameters — and apply pi's own ~chars/4 heuristic
1117
+ * (`estimateTokens` in the coding agent uses the same convention; there is no
1118
+ * per-provider tokenizer to lean on, and the provider's real usage is only
1119
+ * known after the response). Rough by design, but it tracks the true cost far
1120
+ * better than a flat per-tool count: a fat schema is charged for its fatness.
1121
+ */
1122
+ function estimateToolTokens(tool) {
1123
+ let chars = tool.name.length + (tool.description?.length ?? 0);
1124
+ if (tool.inputSchema !== void 0 && tool.inputSchema !== null) try {
1125
+ chars += JSON.stringify(tool.inputSchema).length;
1126
+ } catch {
1127
+ chars += 256;
1128
+ }
1129
+ return Math.ceil(chars / 4);
1130
+ }
1131
+ //#endregion
787
1132
  //#region src/extensions/mcp/mcp-config.ts
788
1133
  /**
789
1134
  * mcp.config.json schema + loader. Split out from the extension so it has
@@ -863,7 +1208,7 @@ async function loadMcpConfig(path) {
863
1208
  * Clients are keyed by JSON-stringified config and reused across
864
1209
  * reloads — only changed configs reconnect.
865
1210
  */
866
- const log$11 = logger.child({ module: "mcp-extension" });
1211
+ const log$13 = logger.child({ module: "mcp-extension" });
867
1212
  async function closeConnected(connected) {
868
1213
  try {
869
1214
  await connected.client.close();
@@ -933,6 +1278,57 @@ function mcpResultToPiContent(result) {
933
1278
  text: "MCP tool returned no content."
934
1279
  }];
935
1280
  }
1281
+ const MAX_ENUM_VALUES_LISTED = 12;
1282
+ function formatEnumValues(values) {
1283
+ const shown = values.slice(0, MAX_ENUM_VALUES_LISTED).map((v) => JSON.stringify(v));
1284
+ if (values.length > MAX_ENUM_VALUES_LISTED) shown.push(`… (${values.length} total)`);
1285
+ return shown.join(", ");
1286
+ }
1287
+ function extractEnum(propSchema) {
1288
+ if (Array.isArray(propSchema.enum) && propSchema.enum.length > 0) return propSchema.enum;
1289
+ const items = propSchema.items;
1290
+ if (items !== null && typeof items === "object" && Array.isArray(items.enum) && items.enum.length > 0) return items.enum;
1291
+ return null;
1292
+ }
1293
+ /**
1294
+ * Build a compact, human-readable constraint hint from an MCP tool's raw JSON
1295
+ * schema so the model sees required fields and each param's allowed enum values
1296
+ * inline in the tool description — the schema pass-through hands pi the server's
1297
+ * schema verbatim, but the model attends to the prose far more than the raw
1298
+ * `enum`/`required` keys. Returns '' when there is nothing worth surfacing.
1299
+ */
1300
+ function describeToolConstraints(inputSchema) {
1301
+ if (inputSchema === null || typeof inputSchema !== "object") return "";
1302
+ const schema = inputSchema;
1303
+ const required = Array.isArray(schema.required) ? schema.required.filter((r) => typeof r === "string") : [];
1304
+ const properties = schema.properties !== null && typeof schema.properties === "object" ? schema.properties : {};
1305
+ const enumLines = [];
1306
+ for (const [propName, rawProp] of Object.entries(properties)) {
1307
+ if (rawProp === null || typeof rawProp !== "object") continue;
1308
+ const values = extractEnum(rawProp);
1309
+ if (values) enumLines.push(`- \`${propName}\` must be one of: ${formatEnumValues(values)}`);
1310
+ }
1311
+ if (required.length === 0 && enumLines.length === 0) return "";
1312
+ const lines = ["Argument constraints:"];
1313
+ if (required.length > 0) lines.push(`- required: ${required.map((r) => `\`${r}\``).join(", ")}`);
1314
+ lines.push(...enumLines);
1315
+ return lines.join("\n");
1316
+ }
1317
+ /**
1318
+ * Build the `promptSnippet` a registered MCP tool exposes to the model: the
1319
+ * server's description with the argument-constraint hint appended, falling back
1320
+ * to a labelled placeholder when the server omitted a description AND the schema
1321
+ * carries no constraints (pi's system-prompt builder hides tools whose snippet
1322
+ * is empty — system-prompt.js:49). This is the layer where `describeToolConstraints`
1323
+ * actually reaches the model, so it is exported and tested directly: the pure
1324
+ * hint being correct is necessary but not sufficient; what the model sees is
1325
+ * this composed snippet.
1326
+ */
1327
+ function buildToolPromptSnippet({ description, inputSchema, serverId }) {
1328
+ const constraintHint = describeToolConstraints(inputSchema);
1329
+ const enriched = constraintHint.length > 0 ? `${description}${description.length > 0 ? "\n\n" : ""}${constraintHint}` : description;
1330
+ return enriched.length > 0 ? enriched : `MCP tool from server "${serverId}".`;
1331
+ }
936
1332
  var McpExtension = class {
937
1333
  mcpClients = /* @__PURE__ */ new Map();
938
1334
  registeredMcpToolNames = /* @__PURE__ */ new Set();
@@ -952,8 +1348,12 @@ var McpExtension = class {
952
1348
  const name = makeToolName(serverId, tool.name);
953
1349
  const parameters = Type.Unsafe(tool.inputSchema);
954
1350
  const description = tool.description?.trim() ?? "";
955
- const promptSnippet = description.length > 0 ? description : `MCP tool from server "${serverId}".`;
956
- pi.registerTool({
1351
+ const promptSnippet = buildToolPromptSnippet({
1352
+ description,
1353
+ inputSchema: tool.inputSchema,
1354
+ serverId
1355
+ });
1356
+ pi.registerTool(withSummaryStrippedBeforeValidation({
957
1357
  name,
958
1358
  label: `MCP: ${serverId}/${tool.name}`,
959
1359
  description,
@@ -987,22 +1387,29 @@ var McpExtension = class {
987
1387
  };
988
1388
  }
989
1389
  }
990
- });
1390
+ }));
991
1391
  this.registeredMcpToolNames.add(name);
992
1392
  }
993
- async reconcile({ pi, configPath, connectTimeoutMs }) {
1393
+ async reconcile({ pi, configPath, connectTimeoutMs, contextWindow }) {
994
1394
  let config;
995
1395
  try {
996
1396
  config = await loadMcpConfig(configPath);
997
1397
  } catch (err) {
998
1398
  throw new Error(`Failed to load MCP config: ${err instanceof Error ? err.message : String(err)}`);
999
1399
  }
1400
+ const limits = resolveMcpToolLimits();
1401
+ const tokenBudget = resolveTokenBudget(limits, contextWindow);
1000
1402
  const summary = {
1001
1403
  added: [],
1002
1404
  removed: [],
1003
1405
  refreshed: [],
1004
1406
  errors: [],
1005
1407
  totalTools: 0,
1408
+ droppedTools: 0,
1409
+ limits,
1410
+ tokenBudget,
1411
+ tokensUsed: 0,
1412
+ toolCounts: {},
1006
1413
  servers: {}
1007
1414
  };
1008
1415
  const desiredIds = new Set(Object.keys(config.servers));
@@ -1026,15 +1433,58 @@ var McpExtension = class {
1026
1433
  if (outcome.error) summary.errors.push(outcome.error);
1027
1434
  if (outcome.change === "added") summary.added.push(id);
1028
1435
  else if (outcome.change === "refreshed") summary.refreshed.push(id);
1029
- if (outcome.tools) for (const tool of outcome.tools.list) {
1030
- this.registerMcpTool({
1031
- pi,
1032
- serverId: id,
1033
- client: outcome.tools.client,
1034
- tool
1035
- });
1036
- summary.totalTools++;
1436
+ }
1437
+ const alloc = outcomes.filter((o) => Boolean(o.tools)).map((o) => {
1438
+ const advertised = o.tools.list.length;
1439
+ return {
1440
+ id: o.id,
1441
+ client: o.tools.client,
1442
+ list: o.tools.list,
1443
+ advertised,
1444
+ room: Math.min(advertised, limits.maxToolsPerServer),
1445
+ registered: 0,
1446
+ serverTokens: 0,
1447
+ droppedReason: null
1448
+ };
1449
+ });
1450
+ const maxAdvertised = alloc.reduce((m, s) => Math.max(m, s.advertised), 0);
1451
+ outer: for (let i = 0; i < maxAdvertised; i++) for (const s of alloc) {
1452
+ if (summary.totalTools >= limits.maxTotalTools) {
1453
+ for (const t of alloc) if (t.registered < t.advertised && t.droppedReason === null) t.droppedReason = "total";
1454
+ break outer;
1455
+ }
1456
+ if (i >= s.room) {
1457
+ if (s.registered < s.advertised && s.droppedReason === null) s.droppedReason = "per_server";
1458
+ continue;
1037
1459
  }
1460
+ const tool = s.list[i];
1461
+ if (tool === void 0) continue;
1462
+ const cost = estimateToolTokens(tool);
1463
+ if (summary.tokensUsed + cost > tokenBudget && summary.totalTools > 0) {
1464
+ for (const t of alloc) if (t.registered < t.advertised && t.droppedReason === null) t.droppedReason = "tokens";
1465
+ break outer;
1466
+ }
1467
+ this.registerMcpTool({
1468
+ pi,
1469
+ serverId: s.id,
1470
+ client: s.client,
1471
+ tool
1472
+ });
1473
+ s.registered++;
1474
+ s.serverTokens += cost;
1475
+ summary.totalTools++;
1476
+ summary.tokensUsed += cost;
1477
+ }
1478
+ for (const s of alloc) {
1479
+ const dropped = s.advertised - s.registered;
1480
+ const droppedReason = dropped > 0 ? s.droppedReason : null;
1481
+ if (dropped > 0) summary.droppedTools += dropped;
1482
+ summary.toolCounts[s.id] = {
1483
+ advertised: s.advertised,
1484
+ registered: s.registered,
1485
+ tokens: s.serverTokens,
1486
+ droppedReason
1487
+ };
1038
1488
  }
1039
1489
  return summary;
1040
1490
  }
@@ -1099,7 +1549,8 @@ var McpExtension = class {
1099
1549
  },
1100
1550
  serverStatus: {
1101
1551
  status: "timeout",
1102
- stderr: result.stderr
1552
+ stderr: result.stderr,
1553
+ diagnostics: result.diagnostics
1103
1554
  },
1104
1555
  change,
1105
1556
  error: null,
@@ -1146,7 +1597,8 @@ var McpExtension = class {
1146
1597
  },
1147
1598
  serverStatus: {
1148
1599
  status: "timeout",
1149
- stderr: retry.stderr
1600
+ stderr: retry.stderr,
1601
+ diagnostics: retry.diagnostics
1150
1602
  },
1151
1603
  change: null,
1152
1604
  error: null,
@@ -1179,7 +1631,8 @@ var McpExtension = class {
1179
1631
  store: connected,
1180
1632
  serverStatus: {
1181
1633
  status: "timeout",
1182
- stderr
1634
+ stderr,
1635
+ diagnostics: null
1183
1636
  },
1184
1637
  change: null,
1185
1638
  error: null,
@@ -1190,6 +1643,64 @@ var McpExtension = class {
1190
1643
  try {
1191
1644
  mcpTools = (await connected.client.listTools()).tools;
1192
1645
  } catch (err) {
1646
+ if (isUnauthorizedError(err) && serverConfig.transport === "http") {
1647
+ await closeConnected(connected);
1648
+ const retry = await connectClient(id, serverConfig, { connectTimeoutMs });
1649
+ if (retry.status === "pending_auth") return {
1650
+ id,
1651
+ store: {
1652
+ client: retry.client,
1653
+ configKey,
1654
+ status: "pending_auth",
1655
+ stderrBuffer: null,
1656
+ cliHint: retry.cliHint
1657
+ },
1658
+ serverStatus: {
1659
+ status: "pending_auth",
1660
+ stderr: "",
1661
+ cliHint: retry.cliHint
1662
+ },
1663
+ change: null,
1664
+ error: null,
1665
+ tools: null
1666
+ };
1667
+ if (retry.status === "failed") return {
1668
+ id,
1669
+ store: null,
1670
+ serverStatus: {
1671
+ status: "failed",
1672
+ error: retry.error,
1673
+ stderr: retry.stderr
1674
+ },
1675
+ change: null,
1676
+ error: {
1677
+ serverId: id,
1678
+ message: retry.error
1679
+ },
1680
+ tools: null
1681
+ };
1682
+ if (retry.status === "connected") {
1683
+ connected = {
1684
+ client: retry.client,
1685
+ configKey,
1686
+ status: "connected",
1687
+ stderrBuffer: retry.stderr,
1688
+ cliHint: null
1689
+ };
1690
+ mcpTools = (await connected.client.listTools()).tools;
1691
+ return {
1692
+ id,
1693
+ store: connected,
1694
+ serverStatus: { status: "connected" },
1695
+ change: action === "reused" ? "refreshed" : action,
1696
+ error: null,
1697
+ tools: {
1698
+ client: connected.client,
1699
+ list: mcpTools
1700
+ }
1701
+ };
1702
+ }
1703
+ }
1193
1704
  const message = err instanceof Error ? err.message : String(err);
1194
1705
  const stderr = connected.stderrBuffer?.read() ?? "";
1195
1706
  return {
@@ -1220,17 +1731,21 @@ var McpExtension = class {
1220
1731
  }
1221
1732
  };
1222
1733
  }
1223
- async reconcileAndRecordMtime({ pi, configPath, reason }) {
1734
+ async reconcileAndRecordMtime({ pi, configPath, reason, contextWindow }) {
1224
1735
  const summary = await this.reconcile({
1225
1736
  pi,
1226
- configPath
1737
+ configPath,
1738
+ contextWindow
1227
1739
  });
1228
1740
  this.lastConfigMtimeMs = await readConfigMtimeMs(configPath);
1229
1741
  if (reason !== "session_start" && summaryHasChanges(summary)) this.pendingMcpUpdate = summary;
1230
- log$11.info({
1742
+ log$13.info({
1231
1743
  event: "mcp_reconcile",
1232
1744
  reason,
1233
1745
  total_tools: summary.totalTools,
1746
+ tokens_used: summary.tokensUsed,
1747
+ token_budget: summary.tokenBudget,
1748
+ dropped_tools: summary.droppedTools,
1234
1749
  added: summary.added,
1235
1750
  removed: summary.removed,
1236
1751
  refreshed: summary.refreshed,
@@ -1247,10 +1762,11 @@ var McpExtension = class {
1247
1762
  await this.reconcileAndRecordMtime({
1248
1763
  pi,
1249
1764
  configPath,
1250
- reason: "session_start"
1765
+ reason: "session_start",
1766
+ contextWindow: ctx.model?.contextWindow
1251
1767
  });
1252
1768
  } catch (err) {
1253
- log$11.error({
1769
+ log$13.error({
1254
1770
  err,
1255
1771
  event: "mcp_reconcile_failed"
1256
1772
  }, "MCP reconcile failed");
@@ -1262,7 +1778,7 @@ var McpExtension = class {
1262
1778
  try {
1263
1779
  mtime = await readConfigMtimeMs(configPath);
1264
1780
  } catch (err) {
1265
- log$11.warn({
1781
+ log$13.warn({
1266
1782
  err,
1267
1783
  event: "mcp_mtime_check_failed"
1268
1784
  }, "mtime check on mcp.config.json failed");
@@ -1273,10 +1789,11 @@ var McpExtension = class {
1273
1789
  await this.reconcileAndRecordMtime({
1274
1790
  pi,
1275
1791
  configPath,
1276
- reason: "auto_reload"
1792
+ reason: "auto_reload",
1793
+ contextWindow: ctx.model?.contextWindow
1277
1794
  });
1278
1795
  } catch (err) {
1279
- log$11.error({
1796
+ log$13.error({
1280
1797
  err,
1281
1798
  event: "mcp_auto_reload_failed"
1282
1799
  }, "auto-reload after mcp.config.json change failed");
@@ -1293,7 +1810,8 @@ var McpExtension = class {
1293
1810
  const summary = await this.reconcileAndRecordMtime({
1294
1811
  pi,
1295
1812
  configPath,
1296
- reason: "tool"
1813
+ reason: "tool",
1814
+ contextWindow: ctx.model?.contextWindow
1297
1815
  });
1298
1816
  return {
1299
1817
  content: [{
@@ -1335,9 +1853,21 @@ function pendingAuthEntries(summary) {
1335
1853
  function timeoutEntries(summary) {
1336
1854
  return Object.entries(summary.servers).flatMap(([id, status]) => status.status === "timeout" ? [{
1337
1855
  id,
1338
- stderr: status.stderr
1856
+ stderr: status.stderr,
1857
+ diagnostics: status.diagnostics
1339
1858
  }] : []);
1340
1859
  }
1860
+ /**
1861
+ * One line describing what the connect probe learned about a timed-out
1862
+ * stdio child, so an empty stderr is no longer a dead end. Empty array
1863
+ * when the entry was re-surfaced without a fresh probe.
1864
+ */
1865
+ function timeoutDiagnosticLines(diagnostics) {
1866
+ if (diagnostics === null) return [];
1867
+ const alive = diagnostics.alive === null ? "unknown (no pid)" : diagnostics.alive ? "alive" : "DEAD";
1868
+ const spoke = diagnostics.stdoutMessages === 0 ? "no JSON-RPC received on stdout (initialize never answered — slow startup, or blocked e.g. on OAuth)" : `${diagnostics.stdoutMessages} JSON-RPC message(s) received on stdout (server spoke but the handshake stalled)`;
1869
+ return [` child process: ${alive}${diagnostics.pid !== null ? ` (pid ${diagnostics.pid})` : ""}; ${spoke}`];
1870
+ }
1341
1871
  function failedEntries(summary) {
1342
1872
  return Object.entries(summary.servers).flatMap(([id, status]) => status.status === "failed" ? [{
1343
1873
  id,
@@ -1345,6 +1875,13 @@ function failedEntries(summary) {
1345
1875
  stderr: status.stderr
1346
1876
  }] : []);
1347
1877
  }
1878
+ function appendTimeoutStderrBlock(lines, stderr) {
1879
+ if (!stderr) {
1880
+ lines.push(" stderr: (empty — the child wrote nothing to stderr)");
1881
+ return;
1882
+ }
1883
+ appendStderrBlock(lines, stderr);
1884
+ }
1348
1885
  function appendStderrBlock(lines, stderr) {
1349
1886
  if (!stderr) return;
1350
1887
  lines.push(" stderr:");
@@ -1352,9 +1889,29 @@ function appendStderrBlock(lines, stderr) {
1352
1889
  for (const line of stderr.trimEnd().split("\n")) lines.push(` ${line}`);
1353
1890
  lines.push(" ---");
1354
1891
  }
1892
+ /**
1893
+ * Human-readable lines describing any budget-driven truncation, shared by
1894
+ * `summaryText` (the reload_mcp tool output) and `formatMcpUpdateMessage` (the
1895
+ * synthetic continuation). Empty when nothing was dropped.
1896
+ */
1897
+ function truncationLines(summary) {
1898
+ if (summary.droppedTools <= 0) return [];
1899
+ const lines = [];
1900
+ const { maxTotalTools, maxToolsPerServer } = summary.limits;
1901
+ const totalCapHint = Number.isFinite(maxTotalTools) ? `${maxTotalTools}` : "unlimited";
1902
+ lines.push(` WARNING: ${summary.droppedTools} MCP tool(s) were NOT registered because a tool budget was hit (token budget: ~${summary.tokenBudget} tokens, used ~${summary.tokensUsed}; total-tool cap: ${totalCapHint}, per-server cap: ${Number.isFinite(maxToolsPerServer) ? maxToolsPerServer : "unlimited"}).`);
1903
+ lines.push(" Tool definitions are sent to the model on every prompt; too many (or too-large) tool schemas push the request over the limit, so they are budgeted against the model context window. Prune servers from mcp.config.json to bring the tool surface down.");
1904
+ for (const [id, count] of Object.entries(summary.toolCounts)) {
1905
+ if (count.droppedReason === null) continue;
1906
+ const why = count.droppedReason === "tokens" ? "token budget exhausted" : count.droppedReason === "total" ? "total-tool cap reached" : "per-server cap";
1907
+ lines.push(` - ${id}: registered ${count.registered}/${count.advertised} tools (~${count.tokens} tokens, ${why}).`);
1908
+ }
1909
+ return lines;
1910
+ }
1355
1911
  function summaryText(summary) {
1356
1912
  const lines = [];
1357
1913
  lines.push(`MCP reconcile complete: ${summary.totalTools} tool(s) live.`);
1914
+ lines.push(...truncationLines(summary));
1358
1915
  if (summary.added.length > 0) lines.push(` Added: ${summary.added.join(", ")}`);
1359
1916
  if (summary.refreshed.length > 0) lines.push(` Refreshed: ${summary.refreshed.join(", ")}`);
1360
1917
  if (summary.removed.length > 0) lines.push(` Removed: ${summary.removed.join(", ")}`);
@@ -1363,11 +1920,12 @@ function summaryText(summary) {
1363
1920
  lines.push(` run \`${cliHint}\` in the shell to authenticate.`);
1364
1921
  appendStderrBlock(lines, stderr);
1365
1922
  }
1366
- for (const { id, stderr } of timeoutEntries(summary)) {
1923
+ for (const { id, stderr, diagnostics } of timeoutEntries(summary)) {
1367
1924
  lines.push(` TIMED OUT ${id}:`);
1368
1925
  lines.push(` bridge spawned but did not complete initialize in time`);
1369
1926
  lines.push(` (usually mcp-remote mid-OAuth — see captured stderr).`);
1370
- appendStderrBlock(lines, stderr);
1927
+ lines.push(...timeoutDiagnosticLines(diagnostics));
1928
+ appendTimeoutStderrBlock(lines, stderr);
1371
1929
  }
1372
1930
  for (const { id, error, stderr } of failedEntries(summary)) {
1373
1931
  lines.push(` FAILED ${id}: ${error}`);
@@ -1380,7 +1938,7 @@ function summaryText(summary) {
1380
1938
  return lines.join("\n");
1381
1939
  }
1382
1940
  function summaryHasChanges(summary) {
1383
- return summary.added.length > 0 || summary.removed.length > 0 || summary.refreshed.length > 0 || summary.errors.length > 0;
1941
+ return summary.added.length > 0 || summary.removed.length > 0 || summary.refreshed.length > 0 || summary.errors.length > 0 || summary.droppedTools > 0;
1384
1942
  }
1385
1943
  /**
1386
1944
  * Format a queued tool-update as a synthetic system-style message for
@@ -1393,6 +1951,10 @@ function formatMcpUpdateMessage(summary) {
1393
1951
  if (summary.added.length > 0) lines.push(`Newly available servers: ${summary.added.join(", ")}`);
1394
1952
  if (summary.refreshed.length > 0) lines.push(`Refreshed servers: ${summary.refreshed.join(", ")}`);
1395
1953
  if (summary.removed.length > 0) lines.push(`Removed servers (and their tools): ${summary.removed.join(", ")}`);
1954
+ if (summary.droppedTools > 0) {
1955
+ lines.push("");
1956
+ lines.push(...truncationLines(summary));
1957
+ }
1396
1958
  const pending = pendingAuthEntries(summary);
1397
1959
  if (pending.length > 0) {
1398
1960
  lines.push("");
@@ -1408,9 +1970,10 @@ function formatMcpUpdateMessage(summary) {
1408
1970
  if (timedOut.length > 0) {
1409
1971
  lines.push("");
1410
1972
  lines.push("Servers that timed out during initialize (bridge is still alive in the background; usually mcp-remote-style stdio bridges mid-OAuth):");
1411
- for (const { id, stderr } of timedOut) {
1973
+ for (const { id, stderr, diagnostics } of timedOut) {
1412
1974
  lines.push(` - ${id}:`);
1413
- appendStderrBlock(lines, stderr);
1975
+ lines.push(...timeoutDiagnosticLines(diagnostics));
1976
+ appendTimeoutStderrBlock(lines, stderr);
1414
1977
  }
1415
1978
  lines.push("If the bridge prints an auth URL in its stderr, share it with the user. Then call `reload_mcp` once they've finished.");
1416
1979
  }
@@ -1499,156 +2062,16 @@ async function runToolUpdateLoop({ session, log }) {
1499
2062
  }, "reached tool-update continuation cap; further updates will land on next request");
1500
2063
  }
1501
2064
  //#endregion
1502
- //#region src/tracing.ts
1503
- const SERVICE_NAME = "skydive-agent-harness";
1504
- const DAEMON_TRACES_URL = "http://localhost:38994/v1/traces";
1505
- let provider = null;
1506
- function initTracing() {
1507
- if (provider) return;
1508
- propagation.setGlobalPropagator(new W3CTraceContextPropagator());
1509
- provider = new NodeTracerProvider({ resource: new Resource({ [ATTR_SERVICE_NAME]: SERVICE_NAME }) });
1510
- const exporter = new OTLPTraceExporter({ url: DAEMON_TRACES_URL });
1511
- provider.addSpanProcessor(new BatchSpanProcessor(exporter, { scheduledDelayMillis: 1e3 }));
1512
- provider.register();
1513
- logger.info({
1514
- event: "tracing_enabled",
1515
- endpoint: DAEMON_TRACES_URL
1516
- }, "OTel tracing initialized — exporting via daemon");
1517
- const shutdown = async () => {
1518
- await shutdownTracing();
1519
- process.exit(0);
1520
- };
1521
- process.on("SIGTERM", shutdown);
1522
- process.on("SIGINT", shutdown);
1523
- }
1524
- function getTracer() {
1525
- return trace.getTracer(SERVICE_NAME);
1526
- }
1527
- function extractRemoteContext() {
1528
- const traceparent = getCurrentTraceparent();
1529
- if (!traceparent) return ROOT_CONTEXT;
1530
- const carrier = { traceparent };
1531
- return propagation.extract(ROOT_CONTEXT, carrier, {
1532
- get: (c, key) => c[key],
1533
- keys: (c) => Object.keys(c)
1534
- });
1535
- }
1536
- async function shutdownTracing() {
1537
- if (provider) await provider.shutdown();
1538
- }
1539
- //#endregion
1540
- //#region src/harness.ts
1541
- /**
1542
- * Skydive composition over @skydiveai/pi-server: wires the platform
1543
- * defaults (tracing to the daemon, tool-update hot-reload hooks, prewarm
1544
- * paths, header passthrough for proxy routing, Skydive agent-card
1545
- * branding) into the generic protocol server, and returns mountable
1546
- * handlers. The agent supplies the pi session factory (their session.ts)
1547
- * and owns the express app:
1548
- *
1549
- * const { platform, protocols } = createHarness({
1550
- * cwd: process.cwd(),
1551
- * createSession,
1552
- * });
1553
- * app.get('/health', platform.handlers.health);
1554
- * app.use(platform.handlers.injectEnv);
1555
- * app.use(platform.handlers.prewarm); // matches /_skydive/prewarm + legacy alias
1556
- * app.use(protocols.handlers.all);
1557
- */
1558
- const A2A_PATH = "/a2a";
1559
- const AGENT_CARD_PATH = "/.well-known/agent-card.json";
1560
- const PREWARM_PATHS = ["/_skydive/prewarm", "/_anyone/prewarm"];
1561
- const PASSTHROUGH_HEADER_PREFIXES = ["x-anyone-", "x-skydive-"];
1562
- function createHarness(options) {
1563
- initTracing();
1564
- readPlatformVersions().then((runtimeVersions) => logger.info({
1565
- event: "runtime_versions",
1566
- runtimeVersions
1567
- }, "platform runtime versions"));
1568
- const serverOptions = {
1569
- ...options,
1570
- onSessionSetup: options.onSessionSetup ?? ((args) => {
1571
- installToolUpdateAutoStop(args);
1572
- installIterationCap(args);
1573
- }),
1574
- postPrompt: options.postPrompt ?? runToolUpdateLoop,
1575
- passthroughHeaderPrefixes: options.passthroughHeaderPrefixes ?? PASSTHROUGH_HEADER_PREFIXES,
1576
- prewarmPaths: options.prewarmPaths ?? PREWARM_PATHS
1577
- };
1578
- const webHandlers = createProtocolHandlers(serverOptions);
1579
- const cardOverrides = {
1580
- name: "Skydive Agent",
1581
- description: "An AI coding agent powered by the Skydive platform.",
1582
- ...options.agentCard
1583
- };
1584
- const agentCardWeb = async (request) => {
1585
- const url = new URL(request.url);
1586
- if (request.method !== "GET" || url.pathname !== AGENT_CARD_PATH) return null;
1587
- return Response.json(buildAgentCard(cardOverrides.url ?? `${url.origin}${A2A_PATH}`, cardOverrides));
1588
- };
1589
- const a2a = restHandler({
1590
- requestHandler: new DefaultRequestHandler(buildAgentCard(cardOverrides.url ?? A2A_PATH, cardOverrides), new InMemoryTaskStore(), createAgentExecutor(serverOptions), new DefaultExecutionEventBusManager()),
1591
- userBuilder: UserBuilder.noAuthentication
1592
- });
1593
- return {
1594
- platform: { handlers: {
1595
- /**
1596
- * Platform health plus the agent's `healthMetadata`. Mount above
1597
- * injectEnv — health must respond immediately for prewarm
1598
- * stashing and readiness probes, and injectEnv can wait up to
1599
- * 10s for env vars during boot.
1600
- */
1601
- health: createHealthHandler({ metadata: options.healthMetadata ?? null }),
1602
- /** Loads platform env (e2b envd / daemon long-poll). */
1603
- injectEnv: createPlatformEnvMiddleware(),
1604
- prewarm: webHandlerToMiddleware(webHandlers.prewarm)
1605
- } },
1606
- protocols: { handlers: {
1607
- /** Express-style; mount at /a2a. */
1608
- a2a,
1609
- /** GET /.well-known/agent-card.json. */
1610
- agentCard: webHandlerToMiddleware(agentCardWeb),
1611
- /** Mirrors each vendor's API shape. */
1612
- openai: { v1: {
1613
- chat: { completions: webHandlerToMiddleware(webHandlers.chatCompletions) },
1614
- responses: webHandlerToMiddleware(webHandlers.responses)
1615
- } },
1616
- anthropic: { v1: { messages: webHandlerToMiddleware(webHandlers.messages) } },
1617
- /**
1618
- * Everything in one mount: a2a (+ agent card) at their well-known
1619
- * paths, then chat-completions / anthropic-messages / responses.
1620
- * Calls next() when nothing matches.
1621
- */
1622
- all: chainMiddleware([mountAt(A2A_PATH, a2a), webHandlerToMiddleware(composeHandlers([
1623
- agentCardWeb,
1624
- webHandlers.chatCompletions,
1625
- webHandlers.messages,
1626
- webHandlers.responses
1627
- ]))])
1628
- } }
1629
- };
1630
- }
1631
- /** Effective bash timeout: the model's value when it gave a positive number, else the default. */
1632
- function resolveBashTimeout(provided) {
1633
- return typeof provided === "number" && provided > 0 ? provided : 600;
1634
- }
1635
- const bashDefaultTimeoutExtension = (pi) => {
1636
- pi.on("tool_call", async (event) => {
1637
- if (event.toolName !== "bash") return;
1638
- event.input.timeout = resolveBashTimeout(event.input.timeout);
1639
- });
1640
- };
1641
- //#endregion
1642
2065
  //#region src/channel-context-ref.ts
1643
2066
  /**
1644
- * The worker injects only a reference — `{ channel, messageId }` — into the
1645
- * sandbox env (`SKYDIVE_CHANNEL_CONTEXT`) rather than the full context.
2067
+ * The worker injects a small reference — `{ channel, messageId, runId }` —
2068
+ * into the sandbox env (`SKYDIVE_CHANNEL_CONTEXT`) rather than the full context.
1646
2069
  *
1647
2070
  * The canonical `ChannelContextRef` type + `parseChannelContextRef` live in
1648
2071
  * `@createinc/anyone-channels`, but the harness (`@skydiveai/*`) keeps zero
1649
2072
  * `@createinc/*` dependencies — importing that package would pull the whole
1650
- * platform channel stack (Slack/email/Linq SDKs, messaging) in just to read
1651
- * two fields. So we validate the (stable) shape locally instead.
2073
+ * platform channel stack (Slack/email/Linq SDKs, messaging) just to read one
2074
+ * field. So we validate the field this consumer needs locally instead.
1652
2075
  */
1653
2076
  const channelContextRefSchema = z.object({ messageId: z.string().nullable() });
1654
2077
  /**
@@ -1680,20 +2103,84 @@ function apiBaseUrl() {
1680
2103
  }
1681
2104
  //#endregion
1682
2105
  //#region src/extensions/platform.ts
2106
+ /**
2107
+ * Platform extension — bridges the agent harness to the Skydive platform daemon.
2108
+ *
2109
+ * Responsibilities:
2110
+ * - Heartbeat: periodic POST to the API so the sandbox manager knows the
2111
+ * agent is alive. Throttled to once per minute, triggered by tool events.
2112
+ * - Session tracking: registers the session with the daemon on start,
2113
+ * streams tool_call / tool_result events so the daemon can track which
2114
+ * session is actively executing, and signals session end on agent_end.
2115
+ * - Channel context: passes the SKYDIVE_CHANNEL_CONTEXT (containing the
2116
+ * messageId) to the daemon so file writes can be attributed to the
2117
+ * correct conversation.
2118
+ *
2119
+ * All daemon POSTs are fire-and-forget — failures are logged but never
2120
+ * block the agent. The daemon may not be running (e.g. local dev without
2121
+ * a sandbox), and that's fine.
2122
+ */
1683
2123
  const HEARTBEAT_THROTTLE_MS = 6e4;
1684
2124
  const TOOL_HEARTBEAT_INTERVAL_MS = 5e3;
1685
2125
  const MAX_TOOL_HEARTBEATS = 1440 * 60 * 1e3 / TOOL_HEARTBEAT_INTERVAL_MS;
1686
2126
  const DAEMON_URL = "http://localhost:38994";
1687
- const log$10 = logger.child({ module: "platform-ext" });
2127
+ const log$12 = logger.child({ module: "platform-ext" });
1688
2128
  function sandboxClient() {
1689
2129
  const apiUrl = apiBaseUrl();
1690
2130
  if (!apiUrl) return null;
1691
2131
  return hc(`${apiUrl}/api/v1/sandbox`);
1692
2132
  }
1693
2133
  /**
1694
- * Fetch every harness feature flag in one GET (`{ contextManagement, ... }`
2134
+ * Is this box still an unclaimed warm-pool sandbox? (ANY-6000, the
2135
+ * feature-flags half of the ANY-5184 pool 403 wave.)
2136
+ *
2137
+ * `GET /sandbox/feature-flags` is agent-only, so the shared poller's request
2138
+ * from a pool box can only 403 — a guaranteed-failing GET every 60s for the
2139
+ * life of the pool phase. The discriminator is the sandbox token's `type`
2140
+ * claim, read UNVERIFIED (this box never holds the signing secret): not an
2141
+ * authorization decision, only "should I bother calling?", and the api still
2142
+ * authorizes every request.
2143
+ *
2144
+ * Read per call from the daemon's persisted env file, NOT process.env:
2145
+ * claiming a pool box rebinds the token in place (the daemon rewrites this
2146
+ * file) while the harness's process.env keeps the boot snapshot, so a
2147
+ * process-env gate would leave a claimed box permanently skipping — trading a
2148
+ * wasted request for silently frozen flags, which is strictly worse. "Cannot
2149
+ * tell" (no file, no token, unparseable payload) reports false so the poll
2150
+ * proceeds.
2151
+ */
2152
+ const daemonEnvIdentitySchema = z.object({
2153
+ ANYONE_SANDBOX_TOKEN: z.string().optional(),
2154
+ SKYDIVE_SANDBOX_TOKEN: z.string().optional()
2155
+ }).passthrough();
2156
+ const tokenTypeSchema = z.object({ type: z.string() }).passthrough();
2157
+ async function isPoolIdentity() {
2158
+ try {
2159
+ const override = process.env.ANYONE_DAEMON_ENV_CACHE;
2160
+ const candidates = override ? [override] : ["/run/anyone-system/daemon-env.json", "/tmp/.anyone/daemon-env.json"];
2161
+ let raw = null;
2162
+ for (const file of candidates) {
2163
+ raw = await readFile(file, "utf8").catch(() => null);
2164
+ if (raw !== null) break;
2165
+ }
2166
+ if (raw === null) return false;
2167
+ const env = daemonEnvIdentitySchema.safeParse(JSON.parse(raw));
2168
+ if (!env.success) return false;
2169
+ const token = env.data.ANYONE_SANDBOX_TOKEN ?? env.data.SKYDIVE_SANDBOX_TOKEN;
2170
+ if (typeof token !== "string" || token === "") return false;
2171
+ const payload = token.split(".")[1];
2172
+ if (!payload) return false;
2173
+ const claims = tokenTypeSchema.safeParse(JSON.parse(Buffer.from(payload, "base64url").toString("utf8")));
2174
+ return claims.success && claims.data.type === "onboarding-pool";
2175
+ } catch (_err) {
2176
+ return false;
2177
+ }
2178
+ }
2179
+ /**
2180
+ * Fetch every harness feature flag in one GET (`{ contextManagement, ... }`
1695
2181
  * — see apps/anyone/api/src/routes/sandbox-feature-flags.ts). Returns
1696
- * null when indeterminate (no api url, or the request failed) so the shared
2182
+ * null when indeterminate (no api url, the request failed, or the box is an
2183
+ * unclaimed pool sandbox whose token the route would 403) so the shared
1697
2184
  * poller keeps the last-known values rather than flipping on a transient error.
1698
2185
  * This is the single fetch behind `feature-flags-poll.ts`; extensions read the
1699
2186
  * polled values there instead of issuing their own GET.
@@ -1701,18 +2188,23 @@ function sandboxClient() {
1701
2188
  async function fetchHarnessFlags() {
1702
2189
  const client = sandboxClient();
1703
2190
  if (!client) return null;
2191
+ if (await isPoolIdentity()) return null;
1704
2192
  try {
1705
2193
  const res = await client["feature-flags"].$get();
1706
2194
  if (!res.ok) {
1707
- log$10.debug({
2195
+ log$12.debug({
1708
2196
  status: res.status,
1709
2197
  event: "feature_flags_fetch_failed"
1710
2198
  }, "feature-flags fetch failed");
1711
2199
  return null;
1712
2200
  }
1713
- return { contextManagement: (await res.json()).contextManagement ?? null };
2201
+ const body = await res.json();
2202
+ return {
2203
+ contextManagement: body.contextManagement ?? null,
2204
+ closingText: body.closingText ?? null
2205
+ };
1714
2206
  } catch (err) {
1715
- log$10.debug({
2207
+ log$12.debug({
1716
2208
  err,
1717
2209
  event: "feature_flags_fetch_error"
1718
2210
  }, "feature-flags request errored");
@@ -1723,7 +2215,7 @@ function postHeartbeat({ messageId }) {
1723
2215
  const client = sandboxClient();
1724
2216
  if (!client) return;
1725
2217
  client.heartbeat.$post({ json: { messageId } }).catch((err) => {
1726
- log$10.debug({
2218
+ log$12.debug({
1727
2219
  err,
1728
2220
  event: "heartbeat_failed"
1729
2221
  }, "heartbeat failed");
@@ -1735,7 +2227,7 @@ async function resolveConversationFromApi(messageId) {
1735
2227
  try {
1736
2228
  const res = await client["message-conversation"].$get({ query: { messageId } });
1737
2229
  if (!res.ok) {
1738
- log$10.warn({
2230
+ log$12.warn({
1739
2231
  status: res.status,
1740
2232
  messageId,
1741
2233
  event: "resolve_conversation_failed"
@@ -1744,7 +2236,7 @@ async function resolveConversationFromApi(messageId) {
1744
2236
  }
1745
2237
  return (await res.json()).conversationId ?? null;
1746
2238
  } catch (err) {
1747
- log$10.warn({
2239
+ log$12.warn({
1748
2240
  err,
1749
2241
  messageId,
1750
2242
  event: "resolve_conversation_error"
@@ -1761,6 +2253,71 @@ async function postBackgroundTaskDone({ messageId, content }) {
1761
2253
  } });
1762
2254
  if (!res.ok) throw new Error(`bg-task-done POST failed: ${res.status}`);
1763
2255
  }
2256
+ async function putBackgroundTaskJournalSpec({ messageId, spec }) {
2257
+ const client = sandboxClient();
2258
+ if (!client) return;
2259
+ try {
2260
+ const res = await client["bg-task-journal"].$put({ json: {
2261
+ messageId,
2262
+ spec
2263
+ } });
2264
+ if (!res.ok) log$12.warn({
2265
+ status: res.status,
2266
+ taskId: spec.id,
2267
+ event: "bg_journal_put_failed"
2268
+ }, "bg-task journal PUT failed");
2269
+ } catch (err) {
2270
+ log$12.warn({
2271
+ err,
2272
+ taskId: spec.id,
2273
+ event: "bg_journal_put_failed"
2274
+ }, "bg-task journal PUT threw");
2275
+ }
2276
+ }
2277
+ async function deleteBackgroundTaskJournalSpec({ messageId, taskId }) {
2278
+ const client = sandboxClient();
2279
+ if (!client) return;
2280
+ try {
2281
+ await client["bg-task-journal"].$delete({ json: {
2282
+ messageId,
2283
+ taskId
2284
+ } });
2285
+ } catch (err) {
2286
+ log$12.debug({
2287
+ err,
2288
+ taskId,
2289
+ event: "bg_journal_delete_failed"
2290
+ }, "bg-task journal DELETE failed");
2291
+ }
2292
+ }
2293
+ async function listBackgroundTaskJournalSpecs({ messageId }) {
2294
+ const client = sandboxClient();
2295
+ if (!client) return [];
2296
+ try {
2297
+ const res = await client["bg-task-journal"].$get({ query: { messageId } });
2298
+ if (!res.ok) return [];
2299
+ return (await res.json()).specs ?? [];
2300
+ } catch (err) {
2301
+ log$12.debug({
2302
+ err,
2303
+ event: "bg_journal_list_failed"
2304
+ }, "bg-task journal GET failed");
2305
+ return [];
2306
+ }
2307
+ }
2308
+ function postBackgroundTasksSnapshot({ messageId, tasks }) {
2309
+ const client = sandboxClient();
2310
+ if (!client || !messageId) return;
2311
+ client["bg-tasks"].$post({ json: {
2312
+ messageId,
2313
+ tasks
2314
+ } }).catch((err) => {
2315
+ log$12.debug({
2316
+ err,
2317
+ event: "bg_tasks_snapshot_failed"
2318
+ }, "bg-tasks snapshot publish failed");
2319
+ });
2320
+ }
1764
2321
  async function postSubagentSpawn({ messageId, tasks }) {
1765
2322
  const client = sandboxClient();
1766
2323
  if (!client) throw new Error("no api url for subagent-spawn");
@@ -1768,8 +2325,19 @@ async function postSubagentSpawn({ messageId, tasks }) {
1768
2325
  messageId,
1769
2326
  tasks
1770
2327
  } });
1771
- if (!res.ok) throw new Error(`subagent-spawn POST failed: ${res.status}`);
1772
- return { taskIds: (await res.json()).taskIds };
2328
+ if (!res.ok) {
2329
+ let detail = "";
2330
+ try {
2331
+ const errBody = await res.json();
2332
+ if (errBody && typeof errBody.error === "string") detail = `: ${errBody.error}`;
2333
+ } catch {}
2334
+ throw new Error(`subagent-spawn POST failed (${res.status})${detail}`);
2335
+ }
2336
+ const body = await res.json();
2337
+ return {
2338
+ taskIds: body.taskIds,
2339
+ tasks: body.tasks ?? []
2340
+ };
1773
2341
  }
1774
2342
  function createHeartbeatThrottle({ messageId }) {
1775
2343
  let lastAt = 0;
@@ -1818,7 +2386,7 @@ function createToolHeartbeat({ messageId }) {
1818
2386
  }
1819
2387
  heartbeatCount++;
1820
2388
  if (heartbeatCount > MAX_TOOL_HEARTBEATS) {
1821
- log$10.warn({
2389
+ log$12.warn({
1822
2390
  heartbeatCount,
1823
2391
  activeToolCalls: [...activeToolCalls]
1824
2392
  }, "tool heartbeat max reached, stopping");
@@ -1849,7 +2417,7 @@ function postToDaemon(path, body) {
1849
2417
  headers: { "content-type": "application/json" },
1850
2418
  body: JSON.stringify(body)
1851
2419
  }).catch((err) => {
1852
- log$10.debug({
2420
+ log$12.debug({
1853
2421
  err,
1854
2422
  path,
1855
2423
  event: "daemon_post_failed"
@@ -1858,7 +2426,7 @@ function postToDaemon(path, body) {
1858
2426
  }
1859
2427
  function createPlatformExtensions({ sessionId, channelContext }) {
1860
2428
  return (pi) => {
1861
- log$10.info({
2429
+ log$12.info({
1862
2430
  sessionId,
1863
2431
  hasChannelContext: Boolean(channelContext)
1864
2432
  }, "platform extension initialized");
@@ -1907,94 +2475,793 @@ function createPlatformExtensions({ sessionId, channelContext }) {
1907
2475
  });
1908
2476
  });
1909
2477
  pi.on("agent_end", () => {
1910
- log$10.info({ sessionId }, "session ending");
2478
+ log$12.info({ sessionId }, "session ending");
1911
2479
  postToDaemon("/session/end", { sessionId });
1912
2480
  });
1913
2481
  };
1914
2482
  }
1915
2483
  //#endregion
1916
- //#region src/extensions/feature-flags-poll.ts
2484
+ //#region src/extensions/context-management-config.ts
2485
+ /**
2486
+ * Configuration for the context-management capability (tool-output trimming
2487
+ * and client-side microcompact). See the design notes in the extension for
2488
+ * what each layer does; this module is purely the env → config surface.
2489
+ *
2490
+ * The harness is provider-agnostic and runs inside the sandbox, where its
2491
+ * only config channel is the environment (loaded by the platform env
2492
+ * middleware before any session starts — see platform-env-middleware.ts).
2493
+ * `SKYDIVE_CONTEXT_MANAGEMENT` is the env-side master toggle; the individual
2494
+ * `SKYDIVE_CTX_*` knobs override the defaults below only when that toggle is
2495
+ * set. Prod never sets it: the LaunchDarkly flag
2496
+ * `harness-context-management-enabled` reaches running sandboxes through the
2497
+ * feature-flag poller (context-management-runtime.ts), which overlays only
2498
+ * `enabled`. So in prod every knob resolves to its default regardless of what
2499
+ * the platform forwards — see the knob-gating follow-up before relying on one.
2500
+ *
2501
+ * Resolution is defensive: a malformed value never throws (this config is
2502
+ * read on the hot path before every LLM call), it falls back to the default
2503
+ * for that knob and logs once.
2504
+ */
2505
+ const log$11 = logger.child({ module: "context-management-config" });
2506
+ const DEFAULT_CONTEXT_MANAGEMENT_CONFIG = {
2507
+ enabled: false,
2508
+ perResultMaxBytes: 16 * 1024,
2509
+ keepRecentToolResults: 3,
2510
+ coldCacheGapSeconds: 240,
2511
+ warmClearTriggerTokens: 5e4,
2512
+ clearAtLeastTokens: 1e4,
2513
+ excludeTools: [],
2514
+ nativeAnthropicEdits: false
2515
+ };
2516
+ const boolFromEnv = (value, fallback) => {
2517
+ if (value === void 0) return fallback;
2518
+ const normalized = value.trim().toLowerCase();
2519
+ if (normalized === "1" || normalized === "true") return true;
2520
+ if (normalized === "0" || normalized === "false") return false;
2521
+ return fallback;
2522
+ };
2523
+ const positiveInt = (fallback) => z.coerce.number().int().positive().catch(fallback);
2524
+ const nonNegativeInt = (fallback) => z.coerce.number().int().nonnegative().catch(fallback);
2525
+ const configSchema = z.object({
2526
+ perResultMaxBytes: positiveInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.perResultMaxBytes),
2527
+ keepRecentToolResults: nonNegativeInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.keepRecentToolResults),
2528
+ coldCacheGapSeconds: positiveInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.coldCacheGapSeconds),
2529
+ warmClearTriggerTokens: positiveInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.warmClearTriggerTokens),
2530
+ clearAtLeastTokens: positiveInt(DEFAULT_CONTEXT_MANAGEMENT_CONFIG.clearAtLeastTokens)
2531
+ });
2532
+ const parseExcludeTools = (value) => {
2533
+ if (!value) return DEFAULT_CONTEXT_MANAGEMENT_CONFIG.excludeTools;
2534
+ return value.split(",").map((name) => name.trim()).filter((name) => name.length > 0);
2535
+ };
2536
+ /**
2537
+ * Reads the context-management config from `env` (defaults to `process.env`).
2538
+ * Never throws — invalid values fall back to defaults.
2539
+ */
2540
+ function resolveContextManagementConfig(env = process.env) {
2541
+ if (!boolFromEnv(env.SKYDIVE_CONTEXT_MANAGEMENT, false)) return { ...DEFAULT_CONTEXT_MANAGEMENT_CONFIG };
2542
+ const parsed = configSchema.safeParse({
2543
+ perResultMaxBytes: env.SKYDIVE_CTX_PER_RESULT_MAX_BYTES,
2544
+ keepRecentToolResults: env.SKYDIVE_CTX_KEEP_RECENT,
2545
+ coldCacheGapSeconds: env.SKYDIVE_CTX_COLD_GAP_SECONDS,
2546
+ warmClearTriggerTokens: env.SKYDIVE_CTX_WARM_TRIGGER_TOKENS,
2547
+ clearAtLeastTokens: env.SKYDIVE_CTX_CLEAR_AT_LEAST_TOKENS
2548
+ });
2549
+ if (!parsed.success) {
2550
+ log$11.warn({
2551
+ event: "context_management_config_invalid",
2552
+ err: parsed.error
2553
+ }, "falling back to default context-management config");
2554
+ return {
2555
+ ...DEFAULT_CONTEXT_MANAGEMENT_CONFIG,
2556
+ enabled: true
2557
+ };
2558
+ }
2559
+ return {
2560
+ enabled: true,
2561
+ ...parsed.data,
2562
+ excludeTools: parseExcludeTools(env.SKYDIVE_CTX_EXCLUDE_TOOLS),
2563
+ nativeAnthropicEdits: boolFromEnv(env.SKYDIVE_CTX_NATIVE_ANTHROPIC_EDITS, DEFAULT_CONTEXT_MANAGEMENT_CONFIG.nativeAnthropicEdits)
2564
+ };
2565
+ }
2566
+ //#endregion
2567
+ //#region src/extensions/context-management-runtime.ts
2568
+ /**
2569
+ * Live, mutable view of the context-management config.
2570
+ *
2571
+ * The env-derived config (context-management-config.ts) is the boot-time
2572
+ * default. On top of it, the platform can deliver a global on/off at runtime
2573
+ * via the LaunchDarkly flag `harness-context-management-enabled` — resolved
2574
+ * server-side and polled by the harness (see the poller in
2575
+ * context-management.ts). This holder is where that override lands so a flip
2576
+ * (especially a kill-switch) reaches long-lived sandboxes without a restart.
2577
+ *
2578
+ * Only the master `enabled` toggle is overridable at runtime; the per-knob
2579
+ * tunables stay env-derived. `enabled` resolves to the flag override when the
2580
+ * platform has reported one, else the env value.
2581
+ */
2582
+ let baseConfig = null;
2583
+ let flagOverride = null;
2584
+ function base() {
2585
+ baseConfig ??= resolveContextManagementConfig();
2586
+ return baseConfig;
2587
+ }
2588
+ /** The effective config, with the runtime flag override applied to `enabled`. */
2589
+ function getContextManagementConfig() {
2590
+ const resolved = base();
2591
+ return {
2592
+ ...resolved,
2593
+ enabled: flagOverride ?? resolved.enabled
2594
+ };
2595
+ }
2596
+ /**
2597
+ * Apply the platform-reported flag value. `null` clears the override (fall back
2598
+ * to the env default) — used when the phone-home result is indeterminate so a
2599
+ * transient failure never silently changes behavior.
2600
+ */
2601
+ function setContextManagementFlagOverride(enabled) {
2602
+ flagOverride = enabled;
2603
+ }
2604
+ /**
2605
+ * Whether a phone-home channel exists to learn the flag at runtime. When false
2606
+ * (e.g. a bare local CLI with no platform API), the env value is the only
2607
+ * source and there's nothing to poll.
2608
+ */
2609
+ function hasFlagSource(env = process.env) {
2610
+ return Boolean(env.SKYDIVE_API_URL ?? env.ANYONE_API_URL);
2611
+ }
2612
+ //#endregion
2613
+ //#region src/extensions/feature-flags-poll.ts
2614
+ /**
2615
+ * Shared harness feature-flag poll.
2616
+ *
2617
+ * The api exposes one `/feature-flags` GET that returns every harness flag in a
2618
+ * single response (`{ contextManagement, closingText, commandFlags }` — see
2619
+ * apps/anyone/api/src/routes/sandbox-feature-flags.ts). Rather than each
2620
+ * extension issuing its own GET — and, worse, a *blocking* GET on the
2621
+ * pre-first-token `session_start` path — a single background poller fetches
2622
+ * that response once per interval and fans the values out to every subscriber.
2623
+ *
2624
+ * Why one poller: context-management consumes the `contextManagement` flag and
2625
+ * the closing-text loop consumes `closingText`, both without a blocking GET on
2626
+ * the pre-first-token `session_start` path. Reading the last-polled value keeps
2627
+ * the hot path allocation-only; a cold cache reads as `null` and a
2628
+ * newly-flipped flag takes effect on the next poll.
2629
+ *
2630
+ * The poll is fire-and-forget and self-unref'd — it never keeps the process
2631
+ * alive and an indeterminate result (no api url / transient failure) leaves the
2632
+ * last-known values untouched so a blip can't silently flip behavior.
2633
+ */
2634
+ const log$10 = logger.child({ module: "feature-flags-poll" });
2635
+ const FLAG_POLL_INTERVAL_MS = 6e4;
2636
+ let contextManagement = null;
2637
+ let closingText = null;
2638
+ const subscribers = {
2639
+ contextManagement: /* @__PURE__ */ new Set(),
2640
+ closingText: /* @__PURE__ */ new Set()
2641
+ };
2642
+ let pollerStarted = false;
2643
+ let firstPollSettled = false;
2644
+ let resolveFirstPoll = null;
2645
+ new Promise((resolve) => {
2646
+ resolveFirstPoll = resolve;
2647
+ });
2648
+ function markFirstPollSettled() {
2649
+ if (firstPollSettled) return;
2650
+ firstPollSettled = true;
2651
+ resolveFirstPoll?.();
2652
+ }
2653
+ /** Last-polled value of a flag, or `null` if not yet resolved. */
2654
+ function getPolledFlag(name) {
2655
+ return name === "closingText" ? closingText : contextManagement;
2656
+ }
2657
+ /**
2658
+ * Subscribe to changes of a flag. The callback fires only on a *transition*
2659
+ * (skipped while the value is unchanged), so a subscriber registered before the
2660
+ * first poll still learns the initial value. Returns an unsubscribe fn.
2661
+ */
2662
+ function onFlagChange(name, cb) {
2663
+ subscribers[name].add(cb);
2664
+ return () => subscribers[name].delete(cb);
2665
+ }
2666
+ function apply(name, next) {
2667
+ if (next === null) return;
2668
+ const prev = name === "closingText" ? closingText : contextManagement;
2669
+ if (name === "closingText") closingText = next;
2670
+ else contextManagement = next;
2671
+ if (next !== prev) for (const cb of subscribers[name]) try {
2672
+ cb(next);
2673
+ } catch (err) {
2674
+ log$10.warn({
2675
+ err,
2676
+ flag: name
2677
+ }, "flag subscriber threw");
2678
+ }
2679
+ }
2680
+ async function pollOnce() {
2681
+ try {
2682
+ const flags = await fetchHarnessFlags();
2683
+ if (!flags) return;
2684
+ apply("contextManagement", flags.contextManagement ?? null);
2685
+ apply("closingText", flags.closingText ?? null);
2686
+ } catch (err) {
2687
+ log$10.debug({ err }, "feature-flag poll threw");
2688
+ }
2689
+ }
2690
+ /**
2691
+ * Start the shared background poll (idempotent). No-op when there's no
2692
+ * phone-home channel (bare CLI): there's nothing to poll and callers keep their
2693
+ * env/boot default. Kicks an immediate poll, then repeats on an interval that
2694
+ * does not keep the process alive.
2695
+ */
2696
+ function startFeatureFlagPoller() {
2697
+ if (pollerStarted || !hasFlagSource()) return;
2698
+ pollerStarted = true;
2699
+ pollOnce().finally(markFirstPollSettled);
2700
+ setInterval(() => void pollOnce(), FLAG_POLL_INTERVAL_MS).unref?.();
2701
+ }
2702
+ //#endregion
2703
+ //#region src/closing-text-loop.ts
2704
+ const CLOSING_TEXT_NUDGE = "<system_notification>You ended that turn on a tool call without writing any reply to the user. Write a brief closing message now: what you did and the outcome. Do not call any more tools unless you genuinely still need to.</system_notification>";
2705
+ function isNonEmptyText(block) {
2706
+ return block.type === "text" && typeof block.text === "string" && block.text.trim().length > 0;
2707
+ }
2708
+ /**
2709
+ * True when the assistant produced no visible text since the human's last
2710
+ * message this turn — i.e. the turn ended on tool calls / thinking only.
2711
+ *
2712
+ * We scan from the end back to the most recent `user` message (the human turn
2713
+ * that triggered this run) and inspect every `assistant` message after it.
2714
+ * A string-content assistant message (rare, but pi allows it) counts as text
2715
+ * when non-blank. `toolResult` and custom messages are ignored.
2716
+ */
2717
+ function turnEndedWithoutVisibleText(messages) {
2718
+ let sawAssistant = false;
2719
+ for (let i = messages.length - 1; i >= 0; i--) {
2720
+ const m = messages[i];
2721
+ if (!m || m.role === "user") break;
2722
+ if (m.role !== "assistant") continue;
2723
+ sawAssistant = true;
2724
+ if (typeof m.content === "string") {
2725
+ if (m.content.trim().length > 0) return false;
2726
+ continue;
2727
+ }
2728
+ if (Array.isArray(m.content) && m.content.some(isNonEmptyText)) return false;
2729
+ }
2730
+ return sawAssistant;
2731
+ }
2732
+ /**
2733
+ * True when the last assistant message this turn ended in a way we must not
2734
+ * nudge: a user abort or a provider error. Both already surface their own
2735
+ * terminal signal to the client; a closing-text prompt on top would be noise.
2736
+ */
2737
+ function turnEndedAbnormally(messages) {
2738
+ for (let i = messages.length - 1; i >= 0; i--) {
2739
+ const m = messages[i];
2740
+ if (!m || m.role !== "assistant") continue;
2741
+ return m.stopReason === "aborted" || m.stopReason === "error";
2742
+ }
2743
+ return false;
2744
+ }
2745
+ async function runClosingTextLoop({ session, log, enabled = getPolledFlag("closingText") === true }) {
2746
+ if (!enabled) return;
2747
+ if (turnEndedAbnormally(session.messages)) return;
2748
+ if (!turnEndedWithoutVisibleText(session.messages)) return;
2749
+ log.info({ event: "closing_text_continuation_injected" }, "turn ended on a tool call with no visible reply; injecting a closing-text continuation");
2750
+ await session.sendCustomMessage({
2751
+ customType: "closing-text-nudge",
2752
+ content: CLOSING_TEXT_NUDGE,
2753
+ display: false,
2754
+ details: {}
2755
+ }, { triggerTurn: true });
2756
+ }
2757
+ //#endregion
2758
+ //#region src/tracing.ts
2759
+ const SERVICE_NAME = "skydive-agent-harness";
2760
+ const DAEMON_TRACES_URL = "http://localhost:38994/v1/traces";
2761
+ let provider = null;
2762
+ function initTracing() {
2763
+ if (provider) return;
2764
+ propagation.setGlobalPropagator(new W3CTraceContextPropagator());
2765
+ provider = new NodeTracerProvider({ resource: new Resource({ [ATTR_SERVICE_NAME]: SERVICE_NAME }) });
2766
+ const exporter = new OTLPTraceExporter({ url: DAEMON_TRACES_URL });
2767
+ provider.addSpanProcessor(new BatchSpanProcessor(exporter, { scheduledDelayMillis: 1e3 }));
2768
+ provider.register();
2769
+ logger.info({
2770
+ event: "tracing_enabled",
2771
+ endpoint: DAEMON_TRACES_URL
2772
+ }, "OTel tracing initialized — exporting via daemon");
2773
+ const shutdown = async () => {
2774
+ await shutdownTracing();
2775
+ process.exit(0);
2776
+ };
2777
+ process.on("SIGTERM", shutdown);
2778
+ process.on("SIGINT", shutdown);
2779
+ }
2780
+ function getTracer() {
2781
+ return trace.getTracer(SERVICE_NAME);
2782
+ }
2783
+ function extractRemoteContext() {
2784
+ const traceparent = getCurrentTraceparent();
2785
+ if (!traceparent) return ROOT_CONTEXT;
2786
+ const carrier = { traceparent };
2787
+ return propagation.extract(ROOT_CONTEXT, carrier, {
2788
+ get: (c, key) => c[key],
2789
+ keys: (c) => Object.keys(c)
2790
+ });
2791
+ }
2792
+ async function shutdownTracing() {
2793
+ if (provider) await provider.shutdown();
2794
+ }
2795
+ //#endregion
2796
+ //#region src/harness.ts
2797
+ /**
2798
+ * Skydive composition over @skydiveai/pi-server: wires the platform
2799
+ * defaults (tracing to the daemon, tool-update hot-reload hooks, prewarm
2800
+ * paths, header passthrough for proxy routing, Skydive agent-card
2801
+ * branding) into the generic protocol server, and returns mountable
2802
+ * handlers. The agent supplies the pi session factory (their session.ts)
2803
+ * and owns the express app:
2804
+ *
2805
+ * const { platform, protocols } = createHarness({
2806
+ * cwd: process.cwd(),
2807
+ * createSession,
2808
+ * });
2809
+ * app.get('/health', platform.handlers.health);
2810
+ * app.use(platform.handlers.injectEnv);
2811
+ * app.use(platform.handlers.prewarm); // matches /_skydive/prewarm + legacy alias
2812
+ * app.use(protocols.handlers.all);
2813
+ */
2814
+ const A2A_PATH = "/a2a";
2815
+ const AGENT_CARD_PATH = "/.well-known/agent-card.json";
2816
+ const PREWARM_PATHS = ["/_skydive/prewarm", "/_anyone/prewarm"];
2817
+ const PASSTHROUGH_HEADER_PREFIXES = ["x-anyone-", "x-skydive-"];
2818
+ function createHarness(options) {
2819
+ initTracing();
2820
+ startFeatureFlagPoller();
2821
+ readPlatformVersions().then((runtimeVersions) => logger.info({
2822
+ event: "runtime_versions",
2823
+ runtimeVersions
2824
+ }, "platform runtime versions"));
2825
+ const serverOptions = {
2826
+ ...options,
2827
+ onSessionSetup: options.onSessionSetup ?? installToolUpdateAutoStop,
2828
+ postPrompt: options.postPrompt ?? (async (args) => {
2829
+ await runToolUpdateLoop(args);
2830
+ await runClosingTextLoop(args);
2831
+ }),
2832
+ passthroughHeaderPrefixes: options.passthroughHeaderPrefixes ?? PASSTHROUGH_HEADER_PREFIXES,
2833
+ prewarmPaths: options.prewarmPaths ?? PREWARM_PATHS
2834
+ };
2835
+ const webHandlers = createProtocolHandlers(serverOptions);
2836
+ const cardOverrides = {
2837
+ name: "Skydive Agent",
2838
+ description: "An AI coding agent powered by the Skydive platform.",
2839
+ ...options.agentCard
2840
+ };
2841
+ const agentCardWeb = async (request) => {
2842
+ const url = new URL(request.url);
2843
+ if (request.method !== "GET" || url.pathname !== AGENT_CARD_PATH) return null;
2844
+ return Response.json(buildAgentCard(cardOverrides.url ?? `${url.origin}${A2A_PATH}`, cardOverrides));
2845
+ };
2846
+ const a2a = restHandler({
2847
+ requestHandler: new DefaultRequestHandler(buildAgentCard(cardOverrides.url ?? A2A_PATH, cardOverrides), new InMemoryTaskStore(), createAgentExecutor(serverOptions), new DefaultExecutionEventBusManager()),
2848
+ userBuilder: UserBuilder.noAuthentication
2849
+ });
2850
+ return {
2851
+ platform: { handlers: {
2852
+ /**
2853
+ * Platform health plus the agent's `healthMetadata`. Mount above
2854
+ * injectEnv — health must respond immediately for prewarm
2855
+ * stashing and readiness probes, and injectEnv can wait up to
2856
+ * 10s for env vars during boot.
2857
+ */
2858
+ health: createHealthHandler({ metadata: options.healthMetadata ?? null }),
2859
+ /** Loads platform env (e2b envd / daemon long-poll). */
2860
+ injectEnv: createPlatformEnvMiddleware(),
2861
+ prewarm: webHandlerToMiddleware(webHandlers.prewarm)
2862
+ } },
2863
+ protocols: { handlers: {
2864
+ /** Express-style; mount at /a2a. */
2865
+ a2a,
2866
+ /** GET /.well-known/agent-card.json. */
2867
+ agentCard: webHandlerToMiddleware(agentCardWeb),
2868
+ /** Mirrors each vendor's API shape. */
2869
+ openai: { v1: {
2870
+ chat: { completions: webHandlerToMiddleware(webHandlers.chatCompletions) },
2871
+ responses: webHandlerToMiddleware(webHandlers.responses)
2872
+ } },
2873
+ anthropic: { v1: { messages: webHandlerToMiddleware(webHandlers.messages) } },
2874
+ /**
2875
+ * Everything in one mount: a2a (+ agent card) at their well-known
2876
+ * paths, then chat-completions / anthropic-messages / responses.
2877
+ * Calls next() when nothing matches.
2878
+ */
2879
+ all: chainMiddleware([mountAt(A2A_PATH, a2a), webHandlerToMiddleware(composeHandlers([
2880
+ agentCardWeb,
2881
+ webHandlers.chatCompletions,
2882
+ webHandlers.messages,
2883
+ webHandlers.responses
2884
+ ]))])
2885
+ } }
2886
+ };
2887
+ }
2888
+ /** Effective bash timeout: the model's value when it gave a positive number, else the default. */
2889
+ function resolveBashTimeout(provided) {
2890
+ return typeof provided === "number" && provided > 0 ? provided : 600;
2891
+ }
2892
+ const bashDefaultTimeoutExtension = (pi) => {
2893
+ pi.on("tool_call", async (event) => {
2894
+ if (event.toolName !== "bash") return;
2895
+ event.input.timeout = resolveBashTimeout(event.input.timeout);
2896
+ });
2897
+ };
2898
+ //#endregion
2899
+ //#region src/extensions/resource-pressure-warning.ts
1917
2900
  /**
1918
- * Shared harness feature-flag poll.
2901
+ * Mid-run resource-pressure warning to the agent.
1919
2902
  *
1920
- * The api exposes one `/feature-flags` GET that returns every harness flag in a
1921
- * single response (`{ contextManagement, commandFlags }` — see
1922
- * apps/anyone/api/src/routes/sandbox-feature-flags.ts). Rather than each
1923
- * extension issuing its own GET — and, worse, a *blocking* GET on the
1924
- * pre-first-token `session_start` path — a single background poller fetches
1925
- * that response once per interval and fans the values out to every subscriber.
2903
+ * The sandbox already detects pressure — the boot scripts cap the
2904
+ * user-workload cgroup (memory.high/memory.max) and watchers log warn/crit
2905
+ * edges for memory and disk — but nothing told the *agent*, so a turn burned
2906
+ * straight to the OOM kill (or a full disk) and only learned about it from
2907
+ * the post-mortem notice. This extension closes that gap in-process: while a
2908
+ * turn is active it polls the agent cgroup and the root filesystem and, the
2909
+ * first time usage crosses a warn threshold, folds a system notification into
2910
+ * the open turn so the agent can checkpoint, shed work (constrain
2911
+ * parallelism, kill a background hog, clean scratch space), or request a
2912
+ * bigger tier BEFORE the kill. The warning is model-facing only; the
2913
+ * agent must not narrate it to the user unless asked.
1926
2914
  *
1927
- * Why one poller: context-management consumes the `contextManagement` flag
1928
- * without a blocking GET on the pre-first-token `session_start` path. Reading
1929
- * the last-polled value keeps the hot path allocation-only; a cold cache reads
1930
- * as `null` and a newly-flipped flag takes effect on the next poll.
2915
+ * The notification is triggered by the two conditions that actually kill
2916
+ * work — memory near the cgroup hard cap, disk near full — and reports a
2917
+ * snapshot of all the relevant stats (memory, CPU utilization, disk) so the
2918
+ * agent can tell which resource is the problem and how much headroom the
2919
+ * others have.
1931
2920
  *
1932
- * The poll is fire-and-forget and self-unref'd — it never keeps the process
1933
- * alive and an indeterminate result (no api url / transient failure) leaves the
1934
- * last-known values untouched so a blip can't silently flip behavior.
2921
+ * Edge-triggered, once per trigger per turn: the fired flags reset on
2922
+ * agent_start, so a turn that rides a threshold gets one warning per
2923
+ * resource, not a stream. Polling only runs while the agent is active — an
2924
+ * idle sandbox's resource usage is not the agent's problem and there is no
2925
+ * open turn to deliver into anyway.
2926
+ *
2927
+ * Best-effort throughout: any read failure (cgroup absent, controller not
2928
+ * delegated, non-cgroup-v2 host, df missing) reads as "no signal" for that
2929
+ * stat and the extension warns on what it can see — it must never break a
2930
+ * turn over an observability feature.
1935
2931
  */
1936
- const log$9 = logger.child({ module: "feature-flags-poll" });
1937
- const FLAG_POLL_INTERVAL_MS = 6e4;
1938
- let contextManagement = null;
1939
- const subscribers = { contextManagement: /* @__PURE__ */ new Set() };
1940
- let pollerStarted = false;
1941
- let firstPollSettled = false;
1942
- let resolveFirstPoll = null;
1943
- new Promise((resolve) => {
1944
- resolveFirstPoll = resolve;
1945
- });
1946
- function markFirstPollSettled() {
1947
- if (firstPollSettled) return;
1948
- firstPollSettled = true;
1949
- resolveFirstPoll?.();
2932
+ const execFileAsync = promisify(execFile);
2933
+ const log$9 = logger.child({ module: "resource-pressure-warning" });
2934
+ const POLL_INTERVAL_MS = 1e4;
2935
+ function envOverride(name) {
2936
+ for (const prefix of ["SKYDIVE_", "ANYONE_"]) {
2937
+ const value = process.env[`${prefix}${name}`];
2938
+ if (value != null && value !== "") return value;
2939
+ }
2940
+ return null;
1950
2941
  }
1951
- /** Last-polled value of a flag, or `null` if not yet resolved. */
1952
- function getPolledFlag(_name) {
1953
- return contextManagement;
2942
+ function cgroupDir() {
2943
+ return envOverride("AGENT_CGROUP") ?? "/sys/fs/cgroup/agent";
2944
+ }
2945
+ function diskRoot() {
2946
+ return envOverride("DISK_ROOT") ?? "/";
1954
2947
  }
1955
2948
  /**
1956
- * Subscribe to changes of a flag. The callback fires only on a *transition*
1957
- * (skipped while the value is unchanged), so a subscriber registered before the
1958
- * first poll still learns the initial value. Returns an unsubscribe fn.
2949
+ * Read a cgroup v2 scalar file. Returns a number, or null for "max"
2950
+ * (uncapped), an empty/absent file, or any read/parse error — an uncapped or
2951
+ * unreadable limit means there is nothing meaningful to warn against.
1959
2952
  */
1960
- function onFlagChange(name, cb) {
1961
- subscribers[name].add(cb);
1962
- return () => subscribers[name].delete(cb);
2953
+ async function readScalar(file) {
2954
+ try {
2955
+ const raw = (await readFile(`${cgroupDir()}/${file}`, "utf8")).trim();
2956
+ if (raw === "" || raw === "max") return null;
2957
+ const n = Number(raw);
2958
+ return Number.isFinite(n) ? n : null;
2959
+ } catch (_error) {
2960
+ return null;
2961
+ }
1963
2962
  }
1964
- function apply(name, next) {
1965
- if (next === null) return;
1966
- const prev = contextManagement;
1967
- contextManagement = next;
1968
- if (next !== prev) for (const cb of subscribers[name]) try {
1969
- cb(next);
1970
- } catch (err) {
1971
- log$9.warn({
1972
- err,
1973
- flag: name
1974
- }, "flag subscriber threw");
2963
+ /**
2964
+ * Read a cgroup v2 "flat keyed" file (one `key value` pair per line, e.g.
2965
+ * cpu.stat) and return the counter for `key`, or null when absent.
2966
+ */
2967
+ async function readKeyedCounter(file, key) {
2968
+ try {
2969
+ const raw = await readFile(`${cgroupDir()}/${file}`, "utf8");
2970
+ for (const line of raw.split("\n")) {
2971
+ const [k, v] = line.trim().split(/\s+/);
2972
+ if (k === key) {
2973
+ const n = Number(v);
2974
+ return Number.isFinite(n) ? n : null;
2975
+ }
2976
+ }
2977
+ return null;
2978
+ } catch (_error) {
2979
+ return null;
1975
2980
  }
1976
2981
  }
1977
- async function pollOnce() {
2982
+ /**
2983
+ * Live memory usage as an integer percent of the hard cap, or null when
2984
+ * either side is unreadable/uncapped. Exported for tests.
2985
+ */
2986
+ async function readMemUsePct() {
2987
+ const [current, max] = await Promise.all([readScalar("memory.current"), readScalar("memory.max")]);
2988
+ if (current === null || max === null || max <= 0) return null;
2989
+ return {
2990
+ pct: Math.floor(current / max * 100),
2991
+ currentBytes: current,
2992
+ maxBytes: max
2993
+ };
2994
+ }
2995
+ /**
2996
+ * Root filesystem used% (df -P Capacity column), or null on any failure.
2997
+ * Exported for tests.
2998
+ */
2999
+ async function readDiskUsePct() {
1978
3000
  try {
1979
- const flags = await fetchHarnessFlags();
1980
- if (!flags) return;
1981
- apply("contextManagement", flags.contextManagement ?? null);
1982
- } catch (err) {
1983
- log$9.debug({ err }, "feature-flag poll threw");
3001
+ const { stdout } = await execFileAsync("df", ["-P", diskRoot()]);
3002
+ const dataRow = stdout.trim().split("\n")[1];
3003
+ if (dataRow == null) return null;
3004
+ const capacity = dataRow.trim().split(/\s+/)[4];
3005
+ if (capacity == null) return null;
3006
+ const pct = Number(capacity.replace("%", ""));
3007
+ return Number.isFinite(pct) ? pct : null;
3008
+ } catch (_error) {
3009
+ return null;
1984
3010
  }
1985
3011
  }
1986
3012
  /**
1987
- * Start the shared background poll (idempotent). No-op when there's no
1988
- * phone-home channel (bare CLI): there's nothing to poll and callers keep their
1989
- * env/boot default. Kicks an immediate poll, then repeats on an interval that
1990
- * does not keep the process alive.
3013
+ * CPU utilization sampler. cgroup v2 exposes cumulative CPU time
3014
+ * (cpu.stat usage_usec); utilization is the delta between two samples over
3015
+ * the wall time between them, normalized by core count. The first call after
3016
+ * construction has no previous sample and returns null.
1991
3017
  */
1992
- function startFeatureFlagPoller() {
1993
- if (pollerStarted || !hasFlagSource()) return;
1994
- pollerStarted = true;
1995
- pollOnce().finally(markFirstPollSettled);
1996
- setInterval(() => void pollOnce(), FLAG_POLL_INTERVAL_MS).unref?.();
3018
+ function createCpuSampler() {
3019
+ let prevUsageUsec = null;
3020
+ let prevAtMs = null;
3021
+ return async () => {
3022
+ const usage = await readKeyedCounter("cpu.stat", "usage_usec");
3023
+ const now = Date.now();
3024
+ const prev = prevUsageUsec;
3025
+ const prevAt = prevAtMs;
3026
+ prevUsageUsec = usage;
3027
+ prevAtMs = now;
3028
+ if (usage === null || prev === null || prevAt === null) return null;
3029
+ const wallUsec = (now - prevAt) * 1e3;
3030
+ if (wallUsec <= 0) return null;
3031
+ const cores = availableParallelism();
3032
+ const pct = Math.round((usage - prev) / (wallUsec * cores) * 100);
3033
+ return Math.max(0, Math.min(100, pct));
3034
+ };
3035
+ }
3036
+ function fmtMb(bytes) {
3037
+ return Math.round(bytes / 1024 / 1024);
3038
+ }
3039
+ /** The model-facing warning text. Exported for tests. */
3040
+ function resourcePressureWarningText(trigger, { mem, cpuPct, diskPct }) {
3041
+ const stats = [];
3042
+ if (mem) stats.push(`memory ${mem.pct}% of cap (${fmtMb(mem.currentBytes)}/${fmtMb(mem.maxBytes)} MB)`);
3043
+ if (cpuPct !== null) stats.push(`CPU ${cpuPct}%`);
3044
+ if (diskPct !== null) stats.push(`disk ${diskPct}% full`);
3045
+ const lead = trigger === "memory" ? `Your sandbox is at ${mem?.pct}% of its memory cap. If usage keeps climbing, the kernel will kill the offending process and this turn may die with it.` : `Your sandbox's disk is ${diskPct}% full. If it fills completely, writes will start failing and this turn may die with them.`;
3046
+ const remedy = trigger === "memory" ? "checkpoint in-flight work (commit and push), then reduce the footprint — constrain parallelism, run heavy steps sequentially, or kill background processes you no longer need." : "checkpoint in-flight work (commit and push), then free space — clean build artifacts, caches, and scratch files you no longer need.";
3047
+ const closer = trigger === "memory" ? "If the workload genuinely needs more memory or CPU, request a bigger sandbox with `platform compute request`." : "A bigger compute tier will not add disk. I cannot add disk from this box, and a replacement sandbox on the same template has the same disk.";
3048
+ const jargon = trigger === "memory" ? "\"I freed up memory on my computer\" or \"I'll probably need a bigger computer with more memory\", not \"I killed the leftover Chromium render so we don't get OOM'd\". Instead, just request the resources you need or change how you work on the task to succeed within this budget." : "\"I freed up disk on my computer\", not \"the root filesystem is at capacity\". Change how you work on the task to succeed within this budget. Do not request a compute upgrade for disk pressure.";
3049
+ return `<system_notification>${lead} Current usage: ${stats.join(", ")}. Act now: ${remedy} ${closer} Do not mention this constraint as you work unless the user asks. No jargon: ${jargon} This is an automated resource warning, not a message from the user; continue the task, adjusted.</system_notification>`;
3050
+ }
3051
+ const resourcePressureWarningExtension = (pi) => {
3052
+ let agentActive = false;
3053
+ let warnedMemThisTurn = false;
3054
+ let warnedDiskThisTurn = false;
3055
+ let timer = null;
3056
+ const sampleCpu = createCpuSampler();
3057
+ async function checkOnce() {
3058
+ if (!agentActive || warnedMemThisTurn && warnedDiskThisTurn) return;
3059
+ const [mem, cpuPct, diskPct] = await Promise.all([
3060
+ readMemUsePct(),
3061
+ sampleCpu(),
3062
+ readDiskUsePct()
3063
+ ]);
3064
+ let trigger = null;
3065
+ if (!warnedMemThisTurn && mem !== null && mem.pct >= 80) {
3066
+ trigger = "memory";
3067
+ warnedMemThisTurn = true;
3068
+ } else if (!warnedDiskThisTurn && diskPct !== null && diskPct >= 80) {
3069
+ trigger = "disk";
3070
+ warnedDiskThisTurn = true;
3071
+ }
3072
+ if (trigger === null) return;
3073
+ log$9.warn({
3074
+ trigger,
3075
+ mem,
3076
+ cpuPct,
3077
+ diskPct
3078
+ }, "resource pressure warning delivered to agent");
3079
+ await pi.sendMessage({
3080
+ customType: "anyone-resource-pressure-warning",
3081
+ content: resourcePressureWarningText(trigger, {
3082
+ mem,
3083
+ cpuPct,
3084
+ diskPct
3085
+ }),
3086
+ display: false
3087
+ }, {
3088
+ triggerTurn: true,
3089
+ deliverAs: "followUp"
3090
+ });
3091
+ }
3092
+ pi.on("agent_start", async () => {
3093
+ agentActive = true;
3094
+ warnedMemThisTurn = false;
3095
+ warnedDiskThisTurn = false;
3096
+ if (!timer) {
3097
+ timer = setInterval(() => {
3098
+ checkOnce().catch((err) => {
3099
+ log$9.error({ err }, "resource pressure check failed");
3100
+ });
3101
+ }, POLL_INTERVAL_MS);
3102
+ timer.unref?.();
3103
+ }
3104
+ });
3105
+ pi.on("agent_end", async () => {
3106
+ agentActive = false;
3107
+ if (timer) {
3108
+ clearInterval(timer);
3109
+ timer = null;
3110
+ }
3111
+ });
3112
+ };
3113
+ //#endregion
3114
+ //#region src/extensions/disk-guard.ts
3115
+ const log$8 = logger.child({ module: "disk-guard" });
3116
+ /**
3117
+ * In-band bypass. The guard is a safety net, not a jail: when the agent knows
3118
+ * a flagged command is genuinely safe (writing to a different mount, a tiny
3119
+ * bounded download, a delete-then-clone one-liner, an emergency it accepts the
3120
+ * risk on) it can force the command through by appending this marker as a
3121
+ * trailing shell comment. Kept as a comment so it never changes what the
3122
+ * command does, and matched case-insensitively with flexible spacing so the
3123
+ * agent doesn't have to reproduce it byte-for-byte.
3124
+ */
3125
+ const BYPASS_MARKER = /#\s*disk-guard:\s*allow\b/i;
3126
+ /** The exact marker text the block message tells the agent to append. */
3127
+ const BYPASS_HINT = "# disk-guard: allow";
3128
+ /**
3129
+ * Harness-level kill switch: set DISK_GUARD_DISABLE=1 to turn the guard off
3130
+ * entirely. This is the "I own my harness, let me opt out" knob — an agent
3131
+ * that boots its own harness can disable the guard for its whole process
3132
+ * without a code roll, and it's also the fleet-wide escape hatch if the
3133
+ * classifier ever misfires and blocks real work. The bare name is honored
3134
+ * first; the SKYDIVE_/ANYONE_ prefixes are accepted too for consistency with
3135
+ * the other env overrides. Empty/unset/"0"/"false" leave the guard on.
3136
+ */
3137
+ function guardDisabledByEnv() {
3138
+ for (const name of [
3139
+ "DISK_GUARD_DISABLE",
3140
+ "SKYDIVE_DISK_GUARD_DISABLE",
3141
+ "ANYONE_DISK_GUARD_DISABLE"
3142
+ ]) {
3143
+ const value = process.env[name];
3144
+ if (value != null && value !== "" && value !== "0" && value !== "false") return true;
3145
+ }
3146
+ return false;
3147
+ }
3148
+ /** True when the command carries the in-band bypass marker. */
3149
+ function hasBypassMarker(command) {
3150
+ return BYPASS_MARKER.test(command);
3151
+ }
3152
+ /**
3153
+ * Commands that reclaim space or merely inspect it. If any of these verbs
3154
+ * appears in the command line, we never block — otherwise the guard would trap
3155
+ * the agent by blocking the exact command it needs to dig out. Matched as
3156
+ * whole words so `remove-item` etc. don't accidentally match `rm`.
3157
+ */
3158
+ const RECLAIM_PATTERNS = [
3159
+ /\brm\b/,
3160
+ /\brmdir\b/,
3161
+ /\bdf\b/,
3162
+ /\bdu\b/,
3163
+ /\bncdu\b/,
3164
+ /\bfind\b[^|]*\s-delete\b/,
3165
+ /\btruncate\b/,
3166
+ /\bgit\s+(gc|prune|clean|worktree\s+remove|worktree\s+prune)\b/,
3167
+ /\b(yarn|npm|pnpm|bun)\s+.*\b(cache\s+clean|cache\s+clear|store\s+prune)\b/,
3168
+ /\bcache\s+(clean|clear|prune)\b/,
3169
+ /\b(docker|podman)\s+.*\bprune\b/,
3170
+ /\bapt(-get)?\s+clean\b/,
3171
+ /\bjournalctl\b[^|]*--vacuum/
3172
+ ];
3173
+ /**
3174
+ * File extensions that mean a download is actually LARGE — archives, disk
3175
+ * images, compiled/binary artifacts, model weights, media. A curl/wget is only
3176
+ * gated when it writes one of these; an API/page fetch to a `.json`/`.html`/
3177
+ * `.txt` file is tiny and must not be blocked. Derived from 4,144 real
3178
+ * commands: ~64% of `curl -o` uses were tiny fetches, only ~4% large.
3179
+ */
3180
+ const BIG_DOWNLOAD_EXT = "(?:tar\\.gz|tgz|tar|zip|iso|gz|bz2|xz|zst|deb|rpm|pkg|dmg|whl|jar|7z|img|mp4|mov|avi|mkv|onnx|gguf|safetensors|bin|node)";
3181
+ /**
3182
+ * Commands that consume a meaningful amount of disk. Kept deliberately tight
3183
+ * and high-precision: validated against 4,144 real commands from the last 7
3184
+ * days, the earlier "writes a file" heuristic flagged 82% of everything (a
3185
+ * `curl -o /tmp/x.json` API call is not a disk event). This set flags ~33%,
3186
+ * almost all genuinely large — real installs, clones, big-archive downloads,
3187
+ * extractions. What was DROPPED and why:
3188
+ * - `git fetch` / `git pull` — incremental on an existing clone, usually tiny.
3189
+ * - `git checkout` — overwhelmingly `git checkout <ref> -- <file>` or a
3190
+ * branch switch, ~zero net growth; the rare full materialization isn't
3191
+ * worth the false-positive rate.
3192
+ * - bare `curl -o` / `wget -o` — see BIG_DOWNLOAD_EXT above.
3193
+ * - loose `… build` — matched `--mode=skip-build`, `oxfmt … build`, prose.
3194
+ * The remaining big-disk op in escher is `git clone` and `git worktree add`
3195
+ * (which is really a checkout), both kept.
3196
+ */
3197
+ const SPACE_HUNGRY_PATTERNS = [
3198
+ /\bgit\s+clone\b/,
3199
+ /\bgit\s+worktree\s+add\b/,
3200
+ /\b(yarn|npm|pnpm|bun)\s+(install|add|ci)\b/,
3201
+ /\byarn\s*$/,
3202
+ /\byarn\s+--(?!version|help)\S/,
3203
+ /\bpip3?\s+install\b/,
3204
+ /\bapt(-get)?\s+install\b/,
3205
+ /\bnpm\s+pack\b/,
3206
+ /\bdocker\s+(build|pull)\b/,
3207
+ new RegExp(`\\b(?:curl|wget)\\b[^\\n]*\\s-[a-zA-Z]*[oO]\\b[^\\n]*\\.${BIG_DOWNLOAD_EXT}\\b`, "i"),
3208
+ new RegExp(`\\b(?:curl|wget)\\b[^\\n]*\\.${BIG_DOWNLOAD_EXT}\\b[^\\n]*\\s-[a-zA-Z]*[oO]\\b`, "i"),
3209
+ /\btar\s+[^\n|]*x[^\n|]*f/,
3210
+ /\bunzip\b/,
3211
+ /\bdd\b[^\n|]*\bof=/
3212
+ ];
3213
+ /**
3214
+ * True when the command reclaims or inspects space — these are always allowed,
3215
+ * even on a 100%-full box, so the agent can dig itself out.
3216
+ */
3217
+ function isReclaimCommand(command) {
3218
+ return RECLAIM_PATTERNS.some((re) => re.test(command));
3219
+ }
3220
+ /**
3221
+ * True when the command is likely to consume a meaningful amount of disk.
3222
+ * A reclaim/inspect command is never space-hungry — the reclaim check wins so a
3223
+ * `git worktree remove` or a `yarn cache clean` is never mistaken for growth.
3224
+ */
3225
+ function isSpaceHungryCommand(command) {
3226
+ if (isReclaimCommand(command)) return false;
3227
+ return SPACE_HUNGRY_PATTERNS.some((re) => re.test(command));
3228
+ }
3229
+ /**
3230
+ * The decision, factored out and pure so it's exhaustively testable without a
3231
+ * real filesystem. Block only when we have a disk reading, it's at/above the
3232
+ * critical threshold, the command is space-hungry (and not a reclaim), and the
3233
+ * agent hasn't explicitly opted out with the bypass marker.
3234
+ */
3235
+ function shouldBlockForDisk(command, diskPct) {
3236
+ if (diskPct === null) return false;
3237
+ if (diskPct < 95) return false;
3238
+ if (hasBypassMarker(command)) return false;
3239
+ return isSpaceHungryCommand(command);
1997
3240
  }
3241
+ /** The agent-facing explanation returned as the blocked tool result. */
3242
+ function diskBlockReason(command, diskPct) {
3243
+ return `Blocked: the sandbox disk is ${diskPct}% full and this command (\`${command.trim().slice(0, 120)}\`) writes a large amount, so it would fail partway with ENOSPC and leave a corrupt result. Reclaim space FIRST, then retry. Free ONLY what THIS conversation created — scratch/build output you wrote this run, downloads you're done with, and worktrees/branches whose work you've already committed and pushed (\`git worktree remove\`, \`yarn cache clean\`, delete your own scratch). Do NOT blindly wipe /tmp or delete a clone/worktree you don't recognize — other conversations share this box. Check headroom with \`df -h /\` and \`du -sh ~/workspace/* 2>/dev/null\`. If you genuinely can't free enough, stop and tell the user you're blocked on disk rather than retrying the write. If you're certain this command is safe anyway (writes elsewhere, tiny bounded size, delete-then-write), force it through by appending \` ${BYPASS_HINT}\` to the command.`;
3244
+ }
3245
+ const diskGuardExtension = (pi) => {
3246
+ pi.on("tool_call", async (event) => {
3247
+ if (event.toolName !== "bash") return;
3248
+ if (guardDisabledByEnv()) return;
3249
+ const command = event.input.command;
3250
+ if (typeof command !== "string" || command.length === 0) return;
3251
+ if (hasBypassMarker(command)) return;
3252
+ if (!isSpaceHungryCommand(command)) return;
3253
+ const diskPct = await readDiskUsePct();
3254
+ if (!shouldBlockForDisk(command, diskPct)) return;
3255
+ log$8.warn({
3256
+ diskPct,
3257
+ command: command.slice(0, 200)
3258
+ }, "blocked space-hungry bash command on near-full disk");
3259
+ return {
3260
+ block: true,
3261
+ reason: diskBlockReason(command, diskPct)
3262
+ };
3263
+ });
3264
+ };
1998
3265
  //#endregion
1999
3266
  //#region src/extensions/context-management-trim.ts
2000
3267
  const CLEARED_PLACEHOLDER = "[old tool result cleared to save context — re-run the tool or re-read the source to recover it]";
@@ -2087,7 +3354,7 @@ function transformContextMessages(messages, config, now) {
2087
3354
  }
2088
3355
  //#endregion
2089
3356
  //#region src/extensions/context-management.ts
2090
- const log$8 = logger.child({ module: "context-management-extension" });
3357
+ const log$7 = logger.child({ module: "context-management-extension" });
2091
3358
  function isAnthropicMessagesPayload(payload) {
2092
3359
  if (typeof payload !== "object" || payload === null) return false;
2093
3360
  const candidate = payload;
@@ -2152,13 +3419,13 @@ function createContextManagementExtension() {
2152
3419
  setContextManagementFlagOverride(getPolledFlag("contextManagement"));
2153
3420
  onFlagChange("contextManagement", (enabled) => {
2154
3421
  setContextManagementFlagOverride(enabled);
2155
- log$8.info({
3422
+ log$7.info({
2156
3423
  event: "context_management_flag_update",
2157
3424
  enabled
2158
3425
  }, "context-management flag updated from platform");
2159
3426
  });
2160
3427
  startFeatureFlagPoller();
2161
- log$8.info({
3428
+ log$7.info({
2162
3429
  event: "context_management_registered",
2163
3430
  enabled: initial.enabled,
2164
3431
  flagSource: hasFlagSource(),
@@ -2170,13 +3437,13 @@ function createContextManagementExtension() {
2170
3437
  const { messages } = event;
2171
3438
  try {
2172
3439
  const result = transformContextIfEnabled(messages, getContextManagementConfig(), Date.now());
2173
- if (result.stats && (result.stats.clearedResults > 0 || result.stats.trimmedResults > 0)) log$8.info({
3440
+ if (result.stats && (result.stats.clearedResults > 0 || result.stats.trimmedResults > 0)) log$7.info({
2174
3441
  event: "context_management_applied",
2175
3442
  ...result.stats
2176
3443
  }, "trimmed/cleared tool output before LLM call");
2177
3444
  return { messages: result.messages };
2178
3445
  } catch (err) {
2179
- log$8.error({
3446
+ log$7.error({
2180
3447
  err,
2181
3448
  event: "context_management_transform_failed"
2182
3449
  }, "context transform failed; passing messages through unchanged");
@@ -2188,7 +3455,7 @@ function createContextManagementExtension() {
2188
3455
  }
2189
3456
  //#endregion
2190
3457
  //#region src/extensions/current-time.ts
2191
- const log$7 = logger.child({ module: "current-time-extension" });
3458
+ const log$6 = logger.child({ module: "current-time-extension" });
2192
3459
  const PI_DATE_LINE = /^Current date:.*$/m;
2193
3460
  function formatCurrentTimeLine(now) {
2194
3461
  return `Current date: ${now.getUTCFullYear()}-${String(now.getUTCMonth() + 1).padStart(2, "0")}-${String(now.getUTCDate()).padStart(2, "0")} (${new Intl.DateTimeFormat("en-US", {
@@ -2201,7 +3468,7 @@ const currentTimeExtension = (pi) => {
2201
3468
  const line = formatCurrentTimeLine(/* @__PURE__ */ new Date());
2202
3469
  const base = event.systemPrompt;
2203
3470
  if (PI_DATE_LINE.test(base)) {
2204
- log$7.info({ event: "pi_date_line_present" }, "pi base prompt carries its own 'Current date:' line again; replacing it in place (pi prompt format may have changed)");
3471
+ log$6.info({ event: "pi_date_line_present" }, "pi base prompt carries its own 'Current date:' line again; replacing it in place (pi prompt format may have changed)");
2205
3472
  return { systemPrompt: base.replace(PI_DATE_LINE, line) };
2206
3473
  }
2207
3474
  return { systemPrompt: `${base}\n${line}` };
@@ -2210,37 +3477,35 @@ const currentTimeExtension = (pi) => {
2210
3477
  //#endregion
2211
3478
  //#region src/memory.ts
2212
3479
  /**
2213
- * In-harness memory index builder.
2214
- *
2215
- * The agent has an agent-level file-based memory at `<cwd>/.memory/`,
2216
- * organized by directory:
3480
+ * In-harness memory readers.
2217
3481
  *
2218
- * .memory/users/<id>-<name>/<topic>.md
2219
- * .memory/projects/<project_slug>/<topic>.md
2220
- * .memory/feedback/<topic>.md
2221
- * .memory/reference/<topic>.md
3482
+ * The agent has an agent-level file-based memory at `<cwd>/.memory/`, split
3483
+ * into two halves that are surfaced differently:
2222
3484
  *
2223
- * The path encodes type and subject (for `users/`, the subject is the
2224
- * person's stable id with a readable name suffix); each `.md` file's
2225
- * frontmatter only carries `name` and `description`.
3485
+ * 1. Shared knowledge (projects, lessons, external systems) — indexed by a
3486
+ * single hand-maintained `<cwd>/.memory/MEMORY.md` that the *agent* writes
3487
+ * and curates, Claude-Code style: one line per fact pointing at the file
3488
+ * that holds it. `readMemoryIndexFile` just reads that file; the agent owns
3489
+ * its contents. This is the whole index for the shared half — there is no
3490
+ * derived walk and no per-directory `MEMORY.md`.
2226
3491
  *
2227
- * `buildMemoryIndex` walks `.memory/` by type directory, reads only the
2228
- * frontmatter of each `.md` (open fd → read first ~4KB → close, in
2229
- * parallel), and renders a markdown index grouped by type and (where
2230
- * applicable) by subject. Files outside the four type directories are
2231
- * ignored. Bodies are never read — the agent loads a specific memory's
2232
- * body on demand via the `read` tool when the index entry says it's
2233
- * relevant.
3492
+ * 2. Per-person memory — `.memory/users/<id>-<name>/<topic>.md`. This half is
3493
+ * *derived*, not hand-maintained, because it has to be filtered to the one
3494
+ * person on the current turn (a single hand-written index couldn't be
3495
+ * scoped per-user without leaking one person's notes into another's
3496
+ * conversation). `buildMemoryIndex` walks a single user's directory, reads
3497
+ * only the frontmatter of each `.md` (open fd → read first ~4KB → close, in
3498
+ * parallel), and renders an index. Each `.md`'s frontmatter carries `name`
3499
+ * and `description`; bodies are never read — the agent loads a specific
3500
+ * memory's body on demand via the `read` tool.
2234
3501
  *
2235
- * Mtime cache keyed by cwd — within the lifetime of a sandbox the cwd
2236
- * is fixed, so this is effectively a single-entry cache. Cache invalidates
2237
- * when any `.md` in the tree is added/modified/deleted; turns where
2238
- * memory didn't change reuse the cached string.
3502
+ * Mtime cache (for the derived per-user half) keyed by cwd — within the
3503
+ * lifetime of a sandbox the cwd is fixed, so this is effectively a single-entry
3504
+ * cache. It invalidates when any `.md` under `users/` is added/modified/
3505
+ * deleted; turns where memory didn't change reuse the cached entries.
2239
3506
  *
2240
- * Frontmatter is parsed as YAML (`yaml` package) and validated with a
2241
- * zod schema — files that don't match the shape are dropped from the
2242
- * index. The same schema can be reused at write time if we want to
2243
- * validate before commit.
3507
+ * Frontmatter is parsed as YAML (`yaml` package) and validated with a zod
3508
+ * schema — files that don't match the shape are dropped from the index.
2244
3509
  */
2245
3510
  const FRONTMATTER_READ_BYTES = 4096;
2246
3511
  const FrontmatterSchema = z.object({
@@ -2248,33 +3513,118 @@ const FrontmatterSchema = z.object({
2248
3513
  description: z.string().min(1)
2249
3514
  }).passthrough();
2250
3515
  const MEMORY_DIRNAME = ".memory";
2251
- const TYPE_DIRS = [
2252
- "users",
2253
- "projects",
2254
- "feedback",
2255
- "reference"
3516
+ const MEMORY_INDEX_FILENAME = "MEMORY.md";
3517
+ const USERS_DIRNAME = "users";
3518
+ /**
3519
+ * The shared-knowledge type dirs from the old frontmatter-indexed layout, used
3520
+ * only to seed a `MEMORY.md` for agents created before it existed (see
3521
+ * `seedMemoryIndexFile`). `users/` is deliberately excluded — per-person memory
3522
+ * stays derived and never lands in the shared, un-scoped `MEMORY.md`.
3523
+ */
3524
+ const LEGACY_SHARED_TYPES = [
3525
+ {
3526
+ dir: "projects",
3527
+ label: "Projects"
3528
+ },
3529
+ {
3530
+ dir: "feedback",
3531
+ label: "Feedback"
3532
+ },
3533
+ {
3534
+ dir: "reference",
3535
+ label: "Reference"
3536
+ }
2256
3537
  ];
2257
- const TYPES_WITH_SUBJECT = new Set(["users", "projects"]);
2258
- const TYPE_LABELS = {
2259
- users: "Users",
2260
- projects: "Projects",
2261
- feedback: "Feedback",
2262
- reference: "Reference"
2263
- };
3538
+ /**
3539
+ * Soft budget for an injected index block. The index is read and injected into
3540
+ * the system prompt on every turn, so every entry costs context for the rest of
3541
+ * the conversation. Past this size we nudge the agent to consolidate and prune
3542
+ * rather than keep appending. Not a hard cap — nothing is truncated.
3543
+ */
3544
+ const MEMORY_INDEX_SOFT_BUDGET_CHARS = 2e4;
3545
+ /**
3546
+ * Hard cap on the injected index — double the soft budget. The soft budget only
3547
+ * warns; this actually bounds what we inject so a runaway index can't consume
3548
+ * unbounded context on every turn. Past this, the index is truncated (on a line
3549
+ * boundary) before injection. It's a backstop, not a normal operating point.
3550
+ */
3551
+ const MEMORY_INDEX_HARD_BUDGET_CHARS = MEMORY_INDEX_SOFT_BUDGET_CHARS * 2;
3552
+ /**
3553
+ * Cheap size summary of a rendered index block, used to surface how much of the
3554
+ * every-turn context budget the index is spending so the agent keeps it lean.
3555
+ * Counts pointer/file lines — both the derived `` - `path` — desc`` form and
3556
+ * the hand-maintained `- [Title](path) — hook` form — not the group headers.
3557
+ */
3558
+ function summarizeIndex(index) {
3559
+ const entryCount = index.split("\n").filter((line) => /^\s*- (?:`|\[)/.test(line)).length;
3560
+ const charCount = index.length;
3561
+ return {
3562
+ entryCount,
3563
+ charCount,
3564
+ overBudget: charCount > MEMORY_INDEX_SOFT_BUDGET_CHARS
3565
+ };
3566
+ }
3567
+ /**
3568
+ * One-line size note for an index block header, e.g. `12 entries, 3187 chars`.
3569
+ */
3570
+ function indexSizeNote(index) {
3571
+ const { entryCount, charCount } = summarizeIndex(index);
3572
+ return `${entryCount} ${entryCount === 1 ? "entry" : "entries"}, ${charCount} chars`;
3573
+ }
3574
+ /**
3575
+ * An explicit warning to surface to the agent when an index has grown past its
3576
+ * budget, or `null` when it's within budget. Extensions render this prominently
3577
+ * above the index so the agent prunes before it keeps appending.
3578
+ */
3579
+ function indexBudgetWarning(index) {
3580
+ const { charCount, overBudget } = summarizeIndex(index);
3581
+ if (!overBudget) return null;
3582
+ return `⚠️ This memory index is ${charCount} chars, over its ${MEMORY_INDEX_SOFT_BUDGET_CHARS}-char budget. It's costing you context on every turn — consolidate duplicate entries and delete stale ones to bring it back under budget before adding anything new.`;
3583
+ }
3584
+ /**
3585
+ * Enforce the hard cap on an index before injection. Under the cap the index is
3586
+ * returned unchanged; over it, the index is truncated on a line boundary and a
3587
+ * notice is appended naming the true size so the agent knows entries are hidden
3588
+ * and must be pruned. This is the actual bound on injected context — callers
3589
+ * still report the true size via {@link indexSizeNote} so nothing is masked.
3590
+ */
3591
+ function enforceMemoryIndexHardBudget(index) {
3592
+ if (index.length <= 4e4) return index;
3593
+ const clipped = index.slice(0, MEMORY_INDEX_HARD_BUDGET_CHARS);
3594
+ const lastNewline = clipped.lastIndexOf("\n");
3595
+ return `${lastNewline > 0 ? clipped.slice(0, lastNewline) : clipped}\n\n⚠️ Memory index truncated at ${MEMORY_INDEX_HARD_BUDGET_CHARS} chars (it is ${index.length}). Entries past this point are NOT shown. Prune the index now — delete stale entries and consolidate duplicates.`;
3596
+ }
3597
+ /**
3598
+ * Read the agent's hand-maintained shared index at `.memory/MEMORY.md`.
3599
+ * Returns the trimmed contents, or `null` when the file is absent or empty —
3600
+ * the agent owns this file, so we surface exactly what it wrote.
3601
+ */
3602
+ async function readMemoryIndexFile({ cwd }) {
3603
+ const path = join(cwd, MEMORY_DIRNAME, MEMORY_INDEX_FILENAME);
3604
+ try {
3605
+ const trimmed = (await readFile(path, "utf-8")).trim();
3606
+ return trimmed.length > 0 ? trimmed : null;
3607
+ } catch {
3608
+ return null;
3609
+ }
3610
+ }
2264
3611
  const cache = /* @__PURE__ */ new Map();
2265
3612
  /**
3613
+ * Build the derived per-person index for a single user.
3614
+ *
2266
3615
  * Returns:
2267
- * - `null` if `.memory/` doesn't exist
2268
- * - `""` if the dir exists but contains nothing in the requested scope
3616
+ * - `null` if `.memory/users/` doesn't exist
3617
+ * - `""` if it exists but this user has no memory
2269
3618
  * - rendered markdown body (no surrounding header — caller wraps)
2270
3619
  *
2271
- * The mtime-keyed cache stores the raw walked entries (the cost is the FS
2272
- * walk); filtering by scope is cheap and runs per call, so two turns with
2273
- * different scopes on the same cwd render correctly from one cached walk.
3620
+ * Scoping by id keeps one person's memory from bleeding into another's
3621
+ * conversation. The mtime-keyed cache stores the raw walked entries (the cost
3622
+ * is the FS walk); filtering by user is cheap and runs per call, so two turns
3623
+ * with different users on the same cwd render correctly from one cached walk.
2274
3624
  */
2275
- async function buildMemoryIndex({ cwd, scope }) {
2276
- const memoryDirAbs = join(cwd, MEMORY_DIRNAME);
2277
- const maxMtimeMs = await maxMtimeAcrossDir(memoryDirAbs);
3625
+ async function buildMemoryIndex({ cwd, userId }) {
3626
+ const usersDirAbs = join(cwd, MEMORY_DIRNAME, USERS_DIRNAME);
3627
+ const maxMtimeMs = await maxMtimeAcrossDir(usersDirAbs);
2278
3628
  if (maxMtimeMs === null) {
2279
3629
  cache.delete(cwd);
2280
3630
  return null;
@@ -2282,13 +3632,13 @@ async function buildMemoryIndex({ cwd, scope }) {
2282
3632
  let cached = cache.get(cwd);
2283
3633
  if (!cached || cached.builtAtMs < maxMtimeMs) {
2284
3634
  cached = {
2285
- entries: await collectEntries(memoryDirAbs, cwd),
3635
+ entries: await collectUserEntries(usersDirAbs, cwd),
2286
3636
  builtAtMs: Date.now()
2287
3637
  };
2288
3638
  cache.set(cwd, cached);
2289
3639
  }
2290
- const visible = cached.entries.filter((entry) => scope.kind === "user" ? entry.type === "users" && entry.subject?.startsWith(scope.userId) === true : entry.type !== "users");
2291
- return visible.length === 0 ? "" : renderIndex(visible);
3640
+ const visible = cached.entries.filter((entry) => entry.subject.startsWith(userId));
3641
+ return visible.length === 0 ? "" : renderUserIndex(visible);
2292
3642
  }
2293
3643
  async function maxMtimeAcrossDir(dir) {
2294
3644
  let dirStat;
@@ -2336,45 +3686,22 @@ async function listSubdirs(dir) {
2336
3686
  }
2337
3687
  return entries.filter((e) => e.isDirectory()).map((e) => join(dir, e.name));
2338
3688
  }
2339
- async function collectEntries(rootDirAbs, cwd) {
2340
- const collected = [];
2341
- await Promise.all(TYPE_DIRS.map(async (type) => {
2342
- const typeDirAbs = join(rootDirAbs, type);
2343
- if (TYPES_WITH_SUBJECT.has(type)) {
2344
- const subjectDirs = await listSubdirs(typeDirAbs);
2345
- await Promise.all(subjectDirs.map(async (subjectDirAbs) => {
2346
- const subject = basename(subjectDirAbs);
2347
- const files = await listMdFilesShallow(subjectDirAbs);
2348
- const parsed = await Promise.all(files.map(async (file) => {
2349
- const fm = await readFrontmatterOnly(file);
2350
- if (!fm?.name || !fm?.description) return null;
2351
- return {
2352
- name: fm.name,
2353
- description: fm.description,
2354
- type,
2355
- subject,
2356
- relPath: relative(cwd, file)
2357
- };
2358
- }));
2359
- for (const e of parsed) if (e) collected.push(e);
2360
- }));
2361
- } else {
2362
- const files = await listMdFilesShallow(typeDirAbs);
2363
- const parsed = await Promise.all(files.map(async (file) => {
2364
- const fm = await readFrontmatterOnly(file);
2365
- if (!fm?.name || !fm?.description) return null;
2366
- return {
2367
- name: fm.name,
2368
- description: fm.description,
2369
- type,
2370
- subject: null,
2371
- relPath: relative(cwd, file)
2372
- };
2373
- }));
2374
- for (const e of parsed) if (e) collected.push(e);
2375
- }
2376
- }));
2377
- return collected;
3689
+ async function collectUserEntries(usersDirAbs, cwd) {
3690
+ const subjectDirs = await listSubdirs(usersDirAbs);
3691
+ return (await Promise.all(subjectDirs.map(async (subjectDirAbs) => {
3692
+ const subject = basename(subjectDirAbs);
3693
+ const files = await listMdFilesShallow(subjectDirAbs);
3694
+ return (await Promise.all(files.map(async (file) => {
3695
+ const fm = await readFrontmatterOnly(file);
3696
+ if (!fm?.name || !fm?.description) return null;
3697
+ return {
3698
+ name: fm.name,
3699
+ description: fm.description,
3700
+ subject,
3701
+ relPath: relative(cwd, file)
3702
+ };
3703
+ }))).filter((e) => e !== null);
3704
+ }))).flat();
2378
3705
  }
2379
3706
  async function readFrontmatterOnly(filePath) {
2380
3707
  let fh;
@@ -2405,77 +3732,189 @@ function parseFrontmatter(text) {
2405
3732
  const result = FrontmatterSchema.safeParse(parsed);
2406
3733
  return result.success ? result.data : null;
2407
3734
  }
2408
- function renderIndex(entries) {
2409
- const byType = {
2410
- users: [],
2411
- projects: [],
2412
- feedback: [],
2413
- reference: []
2414
- };
2415
- for (const e of entries) byType[e.type].push(e);
3735
+ function renderUserIndex(entries) {
3736
+ const bySubject = /* @__PURE__ */ new Map();
3737
+ for (const e of entries) {
3738
+ const list = bySubject.get(e.subject) ?? [];
3739
+ list.push(e);
3740
+ bySubject.set(e.subject, list);
3741
+ }
3742
+ const lines = ["### Users"];
3743
+ for (const subject of [...bySubject.keys()].sort()) {
3744
+ lines.push(`- **${subject}**`);
3745
+ for (const e of bySubject.get(subject) ?? []) lines.push(` - \`${e.relPath}\` — ${e.description}`);
3746
+ }
3747
+ return lines.join("\n");
3748
+ }
3749
+ /**
3750
+ * One-time migration for agents created before `MEMORY.md` existed. If there is
3751
+ * no hand-maintained `.memory/MEMORY.md` yet but the agent has shared memory
3752
+ * files from the old frontmatter-indexed layout (`projects/`, `feedback/`,
3753
+ * `reference/`), derive a `MEMORY.md` from their frontmatter and write it once.
3754
+ * After that the agent owns the file — this never runs again for that agent and
3755
+ * never clobbers an existing index.
3756
+ *
3757
+ * Returns the seeded contents (also written to disk), or `null` when nothing
3758
+ * was seeded (index already present, or no legacy shared files). A write
3759
+ * failure propagates so the caller can log it; the read path then falls back to
3760
+ * whatever is on disk.
3761
+ */
3762
+ async function seedMemoryIndexFile({ cwd }) {
3763
+ const memoryDirAbs = join(cwd, MEMORY_DIRNAME);
3764
+ const indexPath = join(memoryDirAbs, MEMORY_INDEX_FILENAME);
3765
+ if (await stat(indexPath).catch(() => null)) return null;
3766
+ const perType = await Promise.all(LEGACY_SHARED_TYPES.map(async ({ dir, label }) => {
3767
+ const typeDirAbs = join(memoryDirAbs, dir);
3768
+ const files = [];
3769
+ await walkMdFiles(typeDirAbs, files);
3770
+ return {
3771
+ label,
3772
+ entries: (await Promise.all(files.map(async (file) => {
3773
+ const fm = await readFrontmatterOnly(file);
3774
+ if (!fm?.name || !fm?.description) return null;
3775
+ return {
3776
+ name: fm.name,
3777
+ description: fm.description,
3778
+ relPath: relative(cwd, file)
3779
+ };
3780
+ }))).filter((e) => !!e)
3781
+ };
3782
+ }));
3783
+ if (perType.every((group) => group.entries.length === 0)) return null;
3784
+ const content = renderSeededIndex(perType);
3785
+ await writeFile(indexPath, `${content}\n`, "utf-8");
3786
+ return content;
3787
+ }
3788
+ function renderSeededIndex(groups) {
2416
3789
  const sections = [];
2417
- for (const type of TYPE_DIRS) {
2418
- const items = byType[type];
2419
- if (items.length === 0) continue;
2420
- sections.push(`### ${TYPE_LABELS[type]}`);
2421
- if (TYPES_WITH_SUBJECT.has(type)) {
2422
- const bySubject = /* @__PURE__ */ new Map();
2423
- for (const e of items) {
2424
- const subject = e.subject ?? "(unknown)";
2425
- const list = bySubject.get(subject) ?? [];
2426
- list.push(e);
2427
- bySubject.set(subject, list);
2428
- }
2429
- const subjects = [...bySubject.keys()].sort();
2430
- for (const subject of subjects) {
2431
- sections.push(`- **${subject}**`);
2432
- for (const e of bySubject.get(subject) ?? []) sections.push(` - \`${e.relPath}\` — ${e.description}`);
2433
- }
2434
- } else for (const e of items) sections.push(`- \`${e.relPath}\` — ${e.description}`);
3790
+ for (const { label, entries } of groups) {
3791
+ if (entries.length === 0) continue;
3792
+ sections.push(`### ${label}`);
3793
+ const sorted = [...entries].sort((a, b) => a.relPath.localeCompare(b.relPath));
3794
+ for (const e of sorted) sections.push(`- [${e.name}](${e.relPath}) — ${e.description}`);
2435
3795
  sections.push("");
2436
3796
  }
2437
3797
  return sections.join("\n").trimEnd();
2438
3798
  }
3799
+ /**
3800
+ * The version at which the hand-maintained `MEMORY.md` layout was introduced.
3801
+ * Used to classify an unversioned `.memory/`: if it already has a `MEMORY.md`
3802
+ * it's on this layout (not the pre-MEMORY.md v1 frontmatter layout), so it
3803
+ * shouldn't be treated as v1 and re-seeded.
3804
+ */
3805
+ const HAND_MAINTAINED_INDEX_VERSION = 2;
3806
+ const VERSION_FILENAME = ".version";
3807
+ const MEMORY_MIGRATIONS = [{
3808
+ from: 1,
3809
+ to: 2,
3810
+ apply: async ({ cwd }) => {
3811
+ await seedMemoryIndexFile({ cwd });
3812
+ }
3813
+ }];
3814
+ /**
3815
+ * The layout version of an agent's `.memory/`:
3816
+ * - `null` when there's no `.memory/` at all (a fresh agent is current by
3817
+ * construction; nothing to migrate).
3818
+ * - `1` when `.memory/` exists but carries no `.version` marker — i.e. it
3819
+ * predates versioning.
3820
+ * - otherwise the integer in `.memory/.version`.
3821
+ */
3822
+ async function readMemoryVersion(cwd) {
3823
+ const memoryDirAbs = join(cwd, MEMORY_DIRNAME);
3824
+ if (!(await stat(memoryDirAbs).catch(() => null))?.isDirectory()) return null;
3825
+ const raw = await readFile(join(memoryDirAbs, VERSION_FILENAME), "utf-8").catch(() => null);
3826
+ if (raw === null) return await stat(join(memoryDirAbs, MEMORY_INDEX_FILENAME)).then((s) => s.isFile()).catch(() => false) ? HAND_MAINTAINED_INDEX_VERSION : 1;
3827
+ const parsed = Number.parseInt(raw.trim(), 10);
3828
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : 1;
3829
+ }
3830
+ async function writeMemoryVersion(cwd, version) {
3831
+ await writeFile(join(cwd, MEMORY_DIRNAME, VERSION_FILENAME), `${version}\n`, "utf-8");
3832
+ }
3833
+ /**
3834
+ * Bring an agent's `.memory/` up to `CURRENT_MEMORY_VERSION` by applying the
3835
+ * ordered migrations. Runs on session start. No-op when there's no `.memory/`
3836
+ * yet or it's already current. Migrations must be idempotent, so a lost/unwritten
3837
+ * version marker (the file isn't committed by the harness) only costs a repeated
3838
+ * no-op, never corruption. Returns the `{ from, to }` actually applied, or
3839
+ * `null` when nothing ran.
3840
+ */
3841
+ async function migrateMemory({ cwd }) {
3842
+ const from = await readMemoryVersion(cwd);
3843
+ if (from === null || from >= 2) return null;
3844
+ let version = from;
3845
+ while (version < 2) {
3846
+ const migration = MEMORY_MIGRATIONS.find((m) => m.from === version);
3847
+ if (!migration) break;
3848
+ await migration.apply({ cwd });
3849
+ version = migration.to;
3850
+ }
3851
+ await writeMemoryVersion(cwd, version);
3852
+ return {
3853
+ from,
3854
+ to: version
3855
+ };
3856
+ }
2439
3857
  //#endregion
2440
3858
  //#region src/extensions/memory.ts
2441
- const log$6 = logger.child({ module: "memory-extension" });
3859
+ const log$5 = logger.child({ module: "memory-extension" });
2442
3860
  /**
2443
3861
  * The standing instructions for the memory system. Always injected (even with
2444
- * an empty `.memory/`) so the agent knows it can persist notes. `users/` is
3862
+ * no `MEMORY.md`) so the agent knows it can persist notes and how. `users/` is
2445
3863
  * described by the platform memory extension, which is the only thing that can
2446
3864
  * scope it to a person — here we just point at it.
2447
3865
  */
2448
3866
  function memoryInstructions(cwd) {
2449
3867
  return `## Memory across conversations
2450
3868
 
2451
- Persistent notes across conversations live at \`${cwd}/.memory/\` — plain markdown files in your repo. The harness builds and injects an **index** of these files (paths + one-line descriptions) into your system prompt every turn; **bodies are NOT auto-loaded** — when an index entry looks relevant, use your \`read\` tool to load that specific file.
3869
+ Persistent notes across conversations live at \`${cwd}/.memory/\` — plain markdown files in your repo, one fact per file. You maintain a hand-written index of them at \`${cwd}/.memory/MEMORY.md\`, and the harness injects that index into your system prompt every turn. **Bodies are NOT auto-loaded** — when an index line looks relevant, use your \`read\` tool to load that specific file.
2452
3870
 
2453
3871
  Memory records **what happened**: facts you learned, events, investigation findings, project and system details worth carrying forward. It is NOT where behavior goes. A standing rule about how you should act — a "from now on, always/never …", a tone or format preference, a workflow convention a user wants you to follow — belongs in \`soul.md\` (see the Persona / Standing instructions section), not here. When a note is really an instruction about your behavior, write it to \`soul.md\`; when it is a fact or a record of something that occurred, write it here.
2454
3872
 
2455
- Shared knowledge is laid out as \`projects/<slug>/<topic>.md\` for project and system context, \`feedback/<topic>.md\` for concrete lessons learned from something that happened (the event and what it taught you — not a free-floating rule; the rule itself, if durable, goes in \`soul.md\`), and \`reference/<topic>.md\` for how external systems work. (Notes about a specific person live under \`users/\` and are shown separately, scoped to whoever you're talking to.) Each file's frontmatter declares \`name\` and \`description\` (the description is what shows up in the index, so make it a one-line behavior-triggering hook). Commit and push after writing to persist it.`;
3873
+ You own \`MEMORY.md\`. When you learn something durable, write the fact to its own \`.md\` file and add a one-line pointer to \`MEMORY.md\` in the form \`- [Title](relative/path.md) — one-line hook\`, where the hook is what tells future-you when to open the file. \`MEMORY.md\` is an *index*, never a store — put the actual content in the topic file and only a pointer line in \`MEMORY.md\`; do not inline a fact's body into the index even when it seems cheaper. Start each topic file with \`name:\`/\`description:\` frontmatter (the \`description\` is the one-line hook) so the index can be re-seeded, re-derived, or linted from the files themselves. When a fact changes, edit both the file and its line; when it stops being true, delete the file and its line. Shared knowledge is laid out as \`projects/<slug>/<topic>.md\` for project and system context, \`feedback/<topic>.md\` for concrete lessons learned from something that happened (the event and what it taught you — not a free-floating rule; the rule itself, if durable, goes in \`soul.md\`), and \`reference/<topic>.md\` for how external systems work. (Notes about a specific person live under \`users/\` and are indexed for you separately, scoped to whoever you're talking to — don't put people's private notes in the shared \`MEMORY.md\`.)
3874
+
3875
+ Keep \`MEMORY.md\` lean. It's re-injected on *every* turn, so a small, high-signal index is worth far more than an exhaustive one — curate it like a tightly-edited table of contents, not a log:
3876
+ - Be selective. Only record something durable that will matter in a *future* conversation. Don't record what only matters right now, what you can re-derive on demand, or what's already obvious from the repo.
3877
+ - Consolidate before you create. Before adding a line, scan \`MEMORY.md\` for one that already covers the topic; if it exists, \`read\` that file and rewrite it with the new facts merged in rather than adding a near-duplicate. One fact per file, but don't fragment a topic across many thin files.
3878
+ - Prune as you go. Delete lines (and their files) that are wrong, stale, or superseded. The index header reports its size — when it's flagged over budget, consolidate and delete before adding anything new.
3879
+ - Write dates absolute, not relative. Resolve "last week" / "yesterday" to a concrete date when you record it (e.g. "on 7/3 Dhruv told me to …"), so the note still reads correctly in a future conversation.
3880
+
3881
+ Commit and push after editing \`.memory/\` to persist it.`;
2456
3882
  }
2457
3883
  function composeBlock$1({ cwd, index }) {
2458
3884
  const instructions = memoryInstructions(cwd);
2459
3885
  if (!index || index.length === 0) return instructions;
2460
- return `${instructions}\n\n## Memory index\n\n${index}`;
3886
+ const header = `## Memory index (${indexSizeNote(index)})`;
3887
+ const warning = indexBudgetWarning(index);
3888
+ const rendered = enforceMemoryIndexHardBudget(index);
3889
+ return `${instructions}\n\n${warning ? `${header}\n\n${warning}\n\n${rendered}` : `${header}\n\n${rendered}`}`;
2461
3890
  }
2462
3891
  const memoryExtension = (pi) => {
2463
3892
  let cachedBlock = null;
2464
3893
  pi.on("session_start", async (_event, ctx) => {
2465
3894
  try {
2466
- const index = await buildMemoryIndex({
2467
- cwd: ctx.cwd,
2468
- scope: { kind: "shared" }
2469
- });
3895
+ const migrated = await migrateMemory({ cwd: ctx.cwd });
3896
+ if (migrated !== null) log$5.info({
3897
+ event: "memory_migrated",
3898
+ from: migrated.from,
3899
+ to: migrated.to
3900
+ }, "migrated memory layout to current version");
3901
+ } catch (err) {
3902
+ log$5.warn({
3903
+ err,
3904
+ event: "memory_migration_failed"
3905
+ }, "memory migration failed; continuing with existing index");
3906
+ }
3907
+ try {
3908
+ const index = await readMemoryIndexFile({ cwd: ctx.cwd });
2470
3909
  cachedBlock = composeBlock$1({
2471
3910
  cwd: ctx.cwd,
2472
3911
  index
2473
3912
  });
2474
3913
  } catch (err) {
2475
- log$6.warn({
3914
+ log$5.warn({
2476
3915
  err,
2477
3916
  event: "memory_index_failed"
2478
- }, "memory index build failed; injecting instructions only");
3917
+ }, "memory index read failed; injecting instructions only");
2479
3918
  cachedBlock = memoryInstructions(ctx.cwd);
2480
3919
  }
2481
3920
  });
@@ -2486,7 +3925,7 @@ const memoryExtension = (pi) => {
2486
3925
  };
2487
3926
  //#endregion
2488
3927
  //#region src/extensions/platform-memory.ts
2489
- const log$5 = logger.child({ module: "platform-memory-extension" });
3928
+ const log$4 = logger.child({ module: "platform-memory-extension" });
2490
3929
  /**
2491
3930
  * Resolve the human on this turn via the API, keyed by the message id.
2492
3931
  * `/sandbox/channel-context` only returns a sender for a platform-known
@@ -2499,13 +3938,13 @@ const log$5 = logger.child({ module: "platform-memory-extension" });
2499
3938
  async function resolveTurnUser(messageId) {
2500
3939
  const client = sandboxClient();
2501
3940
  if (!client) {
2502
- log$5.debug({ event: "resolve_turn_user_no_api_url" }, "no API url in env; withholding user memory");
3941
+ log$4.debug({ event: "resolve_turn_user_no_api_url" }, "no API url in env; withholding user memory");
2503
3942
  return null;
2504
3943
  }
2505
3944
  try {
2506
3945
  const res = await client["channel-context"].$get({ query: { messageId } });
2507
3946
  if (!res.ok) {
2508
- log$5.warn({
3947
+ log$4.warn({
2509
3948
  event: "resolve_turn_user_failed",
2510
3949
  status: res.status
2511
3950
  }, "channel-context returned non-ok; withholding user memory");
@@ -2518,7 +3957,7 @@ async function resolveTurnUser(messageId) {
2518
3957
  displayName: sender.displayName
2519
3958
  };
2520
3959
  } catch (err) {
2521
- log$5.warn({
3960
+ log$4.warn({
2522
3961
  err,
2523
3962
  event: "resolve_turn_user_failed"
2524
3963
  }, "failed to resolve current user; withholding user memory");
@@ -2534,9 +3973,12 @@ function slugifyName(name) {
2534
3973
  function composeBlock({ index, user }) {
2535
3974
  const instructions = `## Current user memory
2536
3975
 
2537
- Notes about the person on this turn — the only \`users/\` memory you can see. Store anything you learn about them under \`${`.memory/users/${user.id}-${slugifyName(user.displayName)}/`}<topic>.md\`, using exactly this directory. Other people's \`users/\` notes are never shown, so never address someone by a name you only find in memory.`;
3976
+ Notes about the person on this turn — the only \`users/\` memory you can see. Store anything you learn about them under \`${`.memory/users/${user.id}-${slugifyName(user.displayName)}/`}<topic>.md\`, using exactly this directory. Other people's \`users/\` notes are never shown, so never address someone by a name you only find in memory. Keep it lean and be selective — this is re-injected every turn; consolidate related facts into one file and delete what's stale rather than piling on near-duplicates.`;
2538
3977
  if (!index || index.length === 0) return instructions;
2539
- return `${instructions}\n\n${index}`;
3978
+ const warning = indexBudgetWarning(index);
3979
+ const header = `Memory index (${indexSizeNote(index)}):`;
3980
+ const rendered = enforceMemoryIndexHardBudget(index);
3981
+ return `${instructions}\n\n${warning ? `${warning}\n\n${header}\n\n${rendered}` : `${header}\n\n${rendered}`}`;
2540
3982
  }
2541
3983
  /**
2542
3984
  * Build the platform memory extension. `channelContext` is the per-turn ref
@@ -2557,15 +3999,12 @@ function createPlatformMemoryExtension({ channelContext }) {
2557
3999
  cachedBlock = composeBlock({
2558
4000
  index: await buildMemoryIndex({
2559
4001
  cwd: ctx.cwd,
2560
- scope: {
2561
- kind: "user",
2562
- userId: user.id
2563
- }
4002
+ userId: user.id
2564
4003
  }),
2565
4004
  user
2566
4005
  });
2567
4006
  } catch (err) {
2568
- log$5.warn({
4007
+ log$4.warn({
2569
4008
  err,
2570
4009
  event: "user_memory_index_failed"
2571
4010
  }, "user memory index build failed; skipping injection");
@@ -2579,8 +4018,22 @@ function createPlatformMemoryExtension({ channelContext }) {
2579
4018
  };
2580
4019
  }
2581
4020
  //#endregion
4021
+ //#region src/extensions/provider-api-compat.ts
4022
+ const OPENAI_COMPATIBLE_PROVIDERS = ["google", "openai"];
4023
+ const OPENAI_COMPLETIONS_API = "openai-completions";
4024
+ /**
4025
+ * Keep older harnesses compatible with provider-native model specs that use
4026
+ * the OpenAI-compatible wire through the transparent proxy.
4027
+ */
4028
+ const providerApiCompatExtension = (pi) => {
4029
+ for (const provider of OPENAI_COMPATIBLE_PROVIDERS) pi.registerProvider(provider, { models: getModels(provider).map((model) => ({
4030
+ ...model,
4031
+ api: OPENAI_COMPLETIONS_API
4032
+ })) });
4033
+ };
4034
+ //#endregion
2582
4035
  //#region src/extensions/self-trace.ts
2583
- const log$4 = logger.child({ module: "self-trace-extension" });
4036
+ const log$3 = logger.child({ module: "self-trace-extension" });
2584
4037
  /**
2585
4038
  * Reports the agent's own execution as OpenTelemetry spans:
2586
4039
  * agent.session → agent.run → agent.turn.N → tool.NAME, with token/cost
@@ -2605,7 +4058,7 @@ const selfTraceExtension = (pi) => {
2605
4058
  sessionSpan = tracer.startSpan("agent.session", { attributes: { "agent.model": modelId } }, remoteCtx);
2606
4059
  sessionCtx = trace.setSpan(remoteCtx, sessionSpan);
2607
4060
  const sc = sessionSpan.spanContext();
2608
- log$4.info({
4061
+ log$3.info({
2609
4062
  event: "self_trace_session_start",
2610
4063
  trace_id: sc.traceId,
2611
4064
  span_id: sc.spanId,
@@ -2717,13 +4170,13 @@ const selfTraceExtension = (pi) => {
2717
4170
  * Lives in the harness package — soul.md is content from the agent's
2718
4171
  * own git repo, not from the platform — so its handling stays here.
2719
4172
  */
2720
- const log$3 = logger.child({ module: "soul-extension" });
4173
+ const log$2 = logger.child({ module: "soul-extension" });
2721
4174
  async function readSoul(cwd) {
2722
4175
  try {
2723
4176
  return (await readFile(join(cwd, "soul.md"), "utf8")).trim() || null;
2724
4177
  } catch (err) {
2725
4178
  if (err?.code === "ENOENT") return null;
2726
- log$3.warn({
4179
+ log$2.warn({
2727
4180
  err,
2728
4181
  event: "soul_read_failed"
2729
4182
  }, "soul.md read failed");
@@ -2755,7 +4208,7 @@ const soulExtension = (pi) => {
2755
4208
  };
2756
4209
  //#endregion
2757
4210
  //#region src/extensions/subagent/index.ts
2758
- const log$2 = logger.child({ module: "subagent-ext" });
4211
+ const log$1 = logger.child({ module: "subagent-ext" });
2759
4212
  const MAX_TASKS = 8;
2760
4213
  const TaskItem = Type.Object({
2761
4214
  task: Type.String({ description: "The task to delegate to a subagent run." }),
@@ -2764,8 +4217,14 @@ const TaskItem = Type.Object({
2764
4217
  maxLength: 120
2765
4218
  }),
2766
4219
  persona: Type.Optional(Type.String({ description: "Optional extra system prompt / role for this task, applied ON TOP of the child run's own default persona (your full identity and soul are still there underneath). Omit to run with just your default persona." })),
2767
- model: Type.Optional(Type.String({ description: "Optional model id to run this subagent on. PREFER A LOWER-COST, FASTER MODEL when the task is well-scoped and does not need your full reasoning depth — most delegated subtasks (searching, summarizing, mechanical edits, gathering or reformatting data, running a check) run just as well on a lighter model and cost far less. Reserve a top-tier model for subtasks that genuinely need deep reasoning or careful judgment. Must be a real catalogued model id. Omit to inherit your own model. If you are locked to a Google-compliant model, only compliant models are accepted." }))
4220
+ model: Type.Optional(Type.String({ description: "Optional model id to run this subagent on. PREFER A LOWER-COST, FASTER MODEL when the task is well-scoped and does not need your full reasoning depth — most delegated subtasks (searching, summarizing, mechanical edits, gathering or reformatting data, running a check) run just as well on a lighter model and cost far less. Reserve a top-tier model for subtasks that genuinely need deep reasoning or careful judgment. Must be a real catalogued model id. Omit to inherit your own model. If you are locked to a Google-compliant model, only compliant models are accepted." })),
4221
+ timeoutMinutes: Type.Optional(Type.Integer({
4222
+ description: "Optional wall-clock timeout for this subagent, in minutes. If the run is still going after this long it is ended and you are rewoken with a timeout result, so a hung subagent can never strand you. Omit for the default (30 minutes). Raise it for genuinely long work (a big migration, a large audit); lower it for a quick lookup. Range 1-360.",
4223
+ minimum: 1,
4224
+ maximum: 360
4225
+ }))
2768
4226
  });
4227
+ const DEFAULT_SUBAGENT_TIMEOUT_MS = 30 * 6e4;
2769
4228
  const SubagentParams = Type.Object({ tasks: Type.Array(TaskItem, {
2770
4229
  description: "One or more tasks to delegate. Each spawns an isolated subagent run linked to this conversation; they run in parallel and each rewakes you with its result when it finishes.",
2771
4230
  minItems: 1,
@@ -2780,7 +4239,9 @@ function buildTool(messageId) {
2780
4239
  "Delegate one or more tasks to subagent runs — fresh isolated copies of yourself, each with its own context window, linked to this conversation.",
2781
4240
  "Use it to parallelize independent work, to keep a large or noisy subtask out of your own context, or to run a task under a specialized persona.",
2782
4241
  "Fire-and-forget: this returns immediately after queueing. It does NOT wait for results. Each subagent runs on its own and, when it finishes, sends you its result on this thread — so queue the work, then keep going or end your turn. To chain, re-delegate after a result lands.",
2783
- "Pass tasks: [{ task, title, persona?, model? }]. title is a short 3-6 word name for the task — it is shown to the person in the chat as that subagent's row, so name the work rather than restating the prompt. persona is an optional extra system prompt layered ON TOP of your default persona for that task (it adds to, it does not replace, your identity); omit it to run with just your default persona. model is an optional model id for that task — prefer a lower-cost, faster model for well-scoped subtasks that don't need deep reasoning, and reserve a top-tier model for the ones that do; omit it to inherit your own model."
4242
+ "Pass tasks: [{ task, title, persona?, model? }]. title is a short 3-6 word name for the task — it is shown to the person in the chat as that subagent's row, so name the work rather than restating the prompt. persona is an optional extra system prompt layered ON TOP of your default persona for that task (it adds to, it does not replace, your identity); omit it to run with just your default persona. model is an optional model id for that task — prefer a lower-cost, faster model for well-scoped subtasks that don't need deep reasoning, and reserve a top-tier model for the ones that do; omit it to inherit your own model.",
4243
+ "Peering: each queued task comes back with its own conversation id. A subagent is a real linked conversation, so to see what one is doing RIGHT NOW while it runs — its reasoning, the tools it has called and their results, its progress — read that conversation with `platform conversations show <conversationId>` (you are already authorized; it is your own delegated run). Check in that way instead of waiting blind for the final result. The read reflects the child's persisted state, which lags a few seconds behind live (tool results land as they complete; in-progress reasoning can be up to ~5s stale), so peek between checkpoints rather than polling in a tight loop.",
4244
+ "Steering: to add context, correct course, or answer a question a subagent needs mid-run, post to its conversation with `platform conversations post <conversationId> --message \"...\"`. If the subagent is still running, your message lands as a live steer picked up in that same turn; if it has gone idle, it queues as its next turn. This is the same primitive as any conversation message — there is no separate steer channel."
2784
4245
  ].join(" "),
2785
4246
  promptSnippet: "subagent — delegate tasks to isolated subagent runs; each rewakes you with its result when done",
2786
4247
  parameters: SubagentParams,
@@ -2798,28 +4259,39 @@ function buildTool(messageId) {
2798
4259
  task: t.task,
2799
4260
  title: t.title ?? null,
2800
4261
  persona: t.persona ?? null,
2801
- model: t.model ?? null
4262
+ model: t.model ?? null,
4263
+ timeoutMs: t.timeoutMinutes != null ? t.timeoutMinutes * 6e4 : DEFAULT_SUBAGENT_TIMEOUT_MS
2802
4264
  }));
2803
4265
  try {
2804
- const { taskIds } = await postSubagentSpawn({
4266
+ const spawned = await postSubagentSpawn({
2805
4267
  messageId,
2806
4268
  tasks: spawnTasks
2807
4269
  });
2808
- log$2.info({
4270
+ const { taskIds } = spawned;
4271
+ log$1.info({
2809
4272
  event: "subagent_spawned",
2810
4273
  count: taskIds.length
2811
4274
  }, "subagent tasks queued");
2812
- const lines = taskIds.map((id, i) => `- ${id}: ${spawnTasks[i]?.title ?? spawnTasks[i]?.task ?? ""}`).join("\n");
4275
+ const convByTask = new Map(spawned.tasks.map((t) => [t.taskId, t.conversationId]));
4276
+ const lines = taskIds.map((id, i) => {
4277
+ const label = spawnTasks[i]?.title ?? spawnTasks[i]?.task ?? "";
4278
+ const conv = convByTask.get(id);
4279
+ return conv ? `- conversation ${conv}: ${label} (taskId ${id})` : `- ${label} (taskId ${id}, conversation id unavailable — cannot peer this run)`;
4280
+ }).join("\n");
4281
+ const peerHint = spawned.tasks.length ? "\nEach subagent runs on its own conversation (id shown per task above). To SEE what one is doing while it runs, read it with `platform conversations show <conversationId>`. To STEER one mid-run — add context, correct course, answer a question — post to its conversation with `platform conversations post <conversationId> --message \"...\"`; it lands as a live steer if the subagent is still running, or as its next turn if it has gone idle." : "";
2813
4282
  return {
2814
4283
  content: [{
2815
4284
  type: "text",
2816
- text: `Queued ${taskIds.length} subagent ${taskIds.length === 1 ? "run" : "runs"}. Each runs on its own and will send you its result on this thread when it finishes — keep working or end your turn meanwhile.\n${lines}`
4285
+ text: `Queued ${taskIds.length} subagent ${taskIds.length === 1 ? "run" : "runs"}. Each runs on its own and will send you its result on this thread when it finishes — keep working or end your turn meanwhile.\n${lines}${peerHint}`
2817
4286
  }],
2818
- details: { taskIds }
4287
+ details: {
4288
+ taskIds,
4289
+ tasks: spawned.tasks
4290
+ }
2819
4291
  };
2820
4292
  } catch (err) {
2821
4293
  const message = err instanceof Error ? err.message : String(err);
2822
- log$2.warn({
4294
+ log$1.warn({
2823
4295
  err,
2824
4296
  event: "subagent_spawn_failed"
2825
4297
  }, "subagent spawn failed");
@@ -2850,7 +4322,7 @@ function createSubagentExtension({ channelContext }) {
2850
4322
  if (registered) return;
2851
4323
  registered = true;
2852
4324
  pi.registerTool(buildTool(messageId));
2853
- log$2.info({ event: "subagent_enabled" }, "subagent tool registered");
4325
+ log$1.info({ event: "subagent_enabled" }, "subagent tool registered");
2854
4326
  };
2855
4327
  pi.on("session_start", () => {
2856
4328
  registerOnce();
@@ -2858,6 +4330,128 @@ function createSubagentExtension({ channelContext }) {
2858
4330
  };
2859
4331
  }
2860
4332
  //#endregion
4333
+ //#region src/extensions/proactive-result.ts
4334
+ const proactiveShadowTool = {
4335
+ name: "get_latest_proactive_shadow_result",
4336
+ label: "Review proactive shadow result",
4337
+ description: "Use when your creator or an organization owner/admin asks what you most recently considered doing proactively or why. Returns the latest completed scheduled shadow evaluation, which is hypothetical and was not delivered. Explain the stored result faithfully without claiming that its proposed action or message was performed or sent.",
4338
+ parameters: Type.Object({}, { additionalProperties: false })
4339
+ };
4340
+ const contextSchema = z.object({
4341
+ proactiveAction: z.literal(true).optional(),
4342
+ runId: z.string().uuid(),
4343
+ messageId: z.string().uuid()
4344
+ });
4345
+ function createProactiveResultExtension({ channelContext }) {
4346
+ return (pi) => {
4347
+ let context;
4348
+ try {
4349
+ context = contextSchema.safeParse(JSON.parse(channelContext ?? ""));
4350
+ } catch (_error) {
4351
+ return;
4352
+ }
4353
+ if (!context.success) return;
4354
+ const { proactiveAction, runId, messageId } = context.data;
4355
+ let registered = false;
4356
+ pi.on("session_start", async () => {
4357
+ if (registered) return;
4358
+ const client = sandboxClient();
4359
+ if (!client) return;
4360
+ if (!proactiveAction) {
4361
+ pi.registerTool({
4362
+ ...proactiveShadowTool,
4363
+ async execute(_toolCallId, _params, signal) {
4364
+ try {
4365
+ const response = await client["proactive-result"]["latest-shadow"].$get({ query: {
4366
+ runId,
4367
+ messageId
4368
+ } }, { init: { signal } });
4369
+ if (response.status === 404) return {
4370
+ content: [{
4371
+ type: "text",
4372
+ text: "Proactive shadow result review is not available yet."
4373
+ }],
4374
+ isError: false,
4375
+ details: {}
4376
+ };
4377
+ if (!response.ok) throw new Error(`proactive shadow result lookup failed: ${response.status}`);
4378
+ const { result } = await response.json();
4379
+ if (!result) return {
4380
+ content: [{
4381
+ type: "text",
4382
+ text: "No completed scheduled shadow result is available to this conversation."
4383
+ }],
4384
+ isError: false,
4385
+ details: {}
4386
+ };
4387
+ return {
4388
+ content: [{
4389
+ type: "text",
4390
+ text: JSON.stringify({
4391
+ result,
4392
+ reminder: "This was a shadow evaluation. Nothing in it was performed or delivered."
4393
+ })
4394
+ }],
4395
+ isError: false,
4396
+ details: {}
4397
+ };
4398
+ } catch (err) {
4399
+ logger.warn({ err }, "proactive shadow result lookup failed");
4400
+ return {
4401
+ content: [{
4402
+ type: "text",
4403
+ text: "The proactive shadow result could not be retrieved."
4404
+ }],
4405
+ isError: true,
4406
+ details: {}
4407
+ };
4408
+ }
4409
+ }
4410
+ });
4411
+ registered = true;
4412
+ return;
4413
+ }
4414
+ try {
4415
+ const response = await client["proactive-result"].$get({ query: {
4416
+ runId,
4417
+ messageId
4418
+ } });
4419
+ if (response.status === 404) return;
4420
+ if (!response.ok) throw new Error(`proactive result discovery failed: ${response.status}`);
4421
+ const { tool } = await response.json();
4422
+ if (!tool) return;
4423
+ pi.registerTool({
4424
+ name: tool.name,
4425
+ label: "Submit proactive result",
4426
+ description: tool.description,
4427
+ parameters: Type.Unsafe(tool.parameters),
4428
+ async execute(_toolCallId, result, signal) {
4429
+ const submitted = await client["proactive-result"].$post({ json: {
4430
+ runId,
4431
+ messageId,
4432
+ result
4433
+ } }, { init: { signal } });
4434
+ return {
4435
+ content: [{
4436
+ type: "text",
4437
+ text: await submitted.text()
4438
+ }],
4439
+ isError: !submitted.ok,
4440
+ details: {}
4441
+ };
4442
+ }
4443
+ });
4444
+ registered = true;
4445
+ } catch (err) {
4446
+ logger.warn({
4447
+ err,
4448
+ runId
4449
+ }, "proactive result tool unavailable; platform CLI submission remains available");
4450
+ }
4451
+ });
4452
+ };
4453
+ }
4454
+ //#endregion
2861
4455
  //#region src/extensions/tool-call-env.ts
2862
4456
  const TOOL_CALL_ID_VAR = "TOOL_CALL_ID";
2863
4457
  function shellQuoteValue(value) {
@@ -2884,212 +4478,31 @@ const toolCallEnvExtension = (pi) => {
2884
4478
  });
2885
4479
  };
2886
4480
  //#endregion
2887
- //#region src/extensions/tool-call-summary.ts
2888
- const log$1 = logger.child({ module: "tool-call-summary-extension" });
2889
- /**
2890
- * The injected parameter name: a namespaced sentinel, so it can never collide
2891
- * with a real tool argument and is unmistakable in transcripts and logs. The
2892
- * frontend renderer (ANY-2723) duplicates this literal — keep the two in sync.
2893
- */
2894
- const TOOL_CALL_SUMMARY_FIELD = "__skydive_summary__";
2895
- /** JSON Schema fragment for the injected parameter. */
2896
- const SUMMARY_PROPERTY = {
2897
- type: "string",
2898
- description: "Required for every tool call. A concise, specific summary (max ~8 words) of what THIS call does and why, written for a person watching the conversation, e.g. \"Searching feedback for billing complaints\" or \"Reading the auth middleware\". Address the user directly in second person: the summary is read by the user, so refer to their things as \"your\", never in third person — \"Reading your emails\", not \"Reading his emails\". Always use the present progressive tense, since it is shown while the call runs: \"Updating your Slack\", never \"Updated your Slack\". Make each summary distinct from your other tool calls; never reuse a generic label like \"Search query\" or \"Running command\"."
2899
- };
2900
- const jsonSchemaObjectSchema = z.object({
2901
- type: z.unknown().optional(),
2902
- properties: z.record(z.string(), z.unknown()).optional(),
2903
- required: z.array(z.string()).optional(),
2904
- additionalProperties: z.unknown().optional()
2905
- }).passthrough();
2906
- const toolEntrySchema = z.object({
2907
- name: z.string().optional(),
2908
- input_schema: jsonSchemaObjectSchema.optional(),
2909
- parameters: jsonSchemaObjectSchema.optional(),
2910
- function: z.object({
2911
- name: z.string().optional(),
2912
- parameters: jsonSchemaObjectSchema.optional()
2913
- }).passthrough().optional()
2914
- }).passthrough();
2915
- const payloadWithToolsSchema = z.object({ tools: z.array(z.unknown()) }).passthrough();
2916
- /**
2917
- * Add the summary property to one JSON Schema object. Returns the augmented
2918
- * copy, or `null` when the tool should be left untouched: a strict schema
2919
- * (`additionalProperties: false`) whose validation would reject the extra
2920
- * field, or one that already declares a `__skydive_summary__` property of its own.
2921
- */
2922
- function augmentSchema(schema) {
2923
- if (schema.additionalProperties === false) return null;
2924
- const properties = schema.properties ?? {};
2925
- if ("__skydive_summary__" in properties) return null;
2926
- const required = schema.required ?? [];
2927
- return {
2928
- ...schema,
2929
- type: schema.type ?? "object",
2930
- properties: {
2931
- [TOOL_CALL_SUMMARY_FIELD]: SUMMARY_PROPERTY,
2932
- ...properties
2933
- },
2934
- required: required.includes("__skydive_summary__") ? required : [...required, TOOL_CALL_SUMMARY_FIELD]
2935
- };
2936
- }
2937
- /**
2938
- * Augment a single tool entry, dispatching on which provider shape it is.
2939
- * Returns the (possibly rebuilt) entry and whether anything changed. Skipped
2940
- * tools — wrong shape, strict, or name in `strictToolNames` — return unchanged.
2941
- */
2942
- function augmentToolEntry(entry, strictToolNames) {
2943
- const parsed = toolEntrySchema.safeParse(entry);
2944
- if (!parsed.success) return {
2945
- entry,
2946
- changed: false
2947
- };
2948
- const tool = parsed.data;
2949
- const name = tool.name ?? tool.function?.name ?? null;
2950
- if (name !== null && strictToolNames.has(name)) return {
2951
- entry,
2952
- changed: false
2953
- };
2954
- if (tool.input_schema) {
2955
- const augmented = augmentSchema(tool.input_schema);
2956
- if (!augmented) return {
2957
- entry,
2958
- changed: false
2959
- };
2960
- return {
2961
- entry: {
2962
- ...tool,
2963
- input_schema: augmented
2964
- },
2965
- changed: true
2966
- };
2967
- }
2968
- if (tool.parameters) {
2969
- const augmented = augmentSchema(tool.parameters);
2970
- if (!augmented) return {
2971
- entry,
2972
- changed: false
2973
- };
2974
- return {
2975
- entry: {
2976
- ...tool,
2977
- parameters: augmented
2978
- },
2979
- changed: true
2980
- };
2981
- }
2982
- if (tool.function?.parameters) {
2983
- const augmented = augmentSchema(tool.function.parameters);
2984
- if (!augmented) return {
2985
- entry,
2986
- changed: false
4481
+ //#region src/extensions/tool-history.ts
4482
+ function historyToolName(name) {
4483
+ if (/^[a-zA-Z0-9_-]{1,64}$/.test(name)) return name;
4484
+ return `invalid_tool_${createHash("sha256").update(name).digest("hex").slice(0, 32)}`;
4485
+ }
4486
+ /** Repair provider-bound history, never tool execution or persisted messages. */
4487
+ function repairToolHistory(messages) {
4488
+ if (!messages.some((message) => message.role === "assistant" && message.content.some((block) => block.type === "toolCall" && historyToolName(block.name) !== block.name) || message.role === "toolResult" && historyToolName(message.toolName) !== message.toolName)) return messages;
4489
+ return messages.map((message) => {
4490
+ if (message.role === "assistant") return {
4491
+ ...message,
4492
+ content: message.content.map((block) => block.type === "toolCall" ? {
4493
+ ...block,
4494
+ name: historyToolName(block.name)
4495
+ } : block)
2987
4496
  };
2988
- return {
2989
- entry: {
2990
- ...tool,
2991
- function: {
2992
- ...tool.function,
2993
- parameters: augmented
2994
- }
2995
- },
2996
- changed: true
4497
+ if (message.role === "toolResult") return {
4498
+ ...message,
4499
+ toolName: historyToolName(message.toolName)
2997
4500
  };
2998
- }
2999
- return {
3000
- entry,
3001
- changed: false
3002
- };
3003
- }
3004
- /**
3005
- * Inject the summary field into every eligible tool in a provider payload.
3006
- * Returns a new payload when at least one tool was augmented, or `undefined`
3007
- * to signal "no change" (which keeps the original payload, per the
3008
- * `before_provider_request` contract).
3009
- *
3010
- * @param payload The outgoing provider payload (shape varies by provider).
3011
- * @param strictToolNames Names of tools whose registered schema is strict and
3012
- * must be skipped to avoid validation errors.
3013
- */
3014
- function injectToolCallSummary(payload, strictToolNames) {
3015
- const parsed = payloadWithToolsSchema.safeParse(payload);
3016
- if (!parsed.success || parsed.data.tools.length === 0) return void 0;
3017
- let changed = false;
3018
- const tools = parsed.data.tools.map((entry) => {
3019
- const result = augmentToolEntry(entry, strictToolNames);
3020
- if (result.changed) changed = true;
3021
- return result.entry;
4501
+ return message;
3022
4502
  });
3023
- if (!changed) return void 0;
3024
- return {
3025
- ...parsed.data,
3026
- tools
3027
- };
3028
- }
3029
- /**
3030
- * Names of registered tools whose schema sets `additionalProperties: false`.
3031
- * Pi validates the model's tool args against this registered schema, so the
3032
- * injected field would make a strict tool's call fail validation — skip them.
3033
- */
3034
- function getStrictToolNames(pi) {
3035
- const names = /* @__PURE__ */ new Set();
3036
- for (const tool of pi.getAllTools()) {
3037
- const parsed = jsonSchemaObjectSchema.safeParse(tool.parameters);
3038
- if (parsed.success && parsed.data.additionalProperties === false) names.add(tool.name);
3039
- }
3040
- return names;
3041
- }
3042
- function toolDeclaresSummaryParam(pi, toolName) {
3043
- const tool = pi.getAllTools().find((candidate) => candidate.name === toolName);
3044
- if (!tool) return false;
3045
- const parsed = jsonSchemaObjectSchema.safeParse(tool.parameters);
3046
- return parsed.success && parsed.data.properties != null && "__skydive_summary__" in parsed.data.properties;
3047
- }
3048
- /**
3049
- * Remove the injected summary from a tool's execution input. No-op when the
3050
- * field is absent, or when the tool genuinely declares a `__skydive_summary__`
3051
- * parameter of its own (which we never inject into, so its value is real).
3052
- * Mutates `input` in place, matching the `tool_call` contract.
3053
- *
3054
- * Fails open: this runs on the critical path of tool execution, and the
3055
- * `getAllTools()` lookup can throw. On any error we leave `input` untouched
3056
- * (the sentinel may pass through to the tool, but a bug here can never break
3057
- * tool execution).
3058
- */
3059
- function stripInjectedSummary(pi, toolName, input) {
3060
- try {
3061
- if (!("__skydive_summary__" in input)) return;
3062
- if (toolDeclaresSummaryParam(pi, toolName)) return;
3063
- delete input[TOOL_CALL_SUMMARY_FIELD];
3064
- } catch (err) {
3065
- log$1.error({
3066
- err,
3067
- event: "tool_call_summary_strip_failed",
3068
- toolName
3069
- }, "tool_call_summary strip failed; leaving tool input untouched");
3070
- }
3071
- }
3072
- /**
3073
- * Compute the rewritten payload for a `before_provider_request` event, failing
3074
- * open: on any error the original payload is left untouched so a bug here can
3075
- * never break an LLM call.
3076
- */
3077
- function buildInjectedPayload(pi, payload) {
3078
- try {
3079
- return injectToolCallSummary(payload, getStrictToolNames(pi));
3080
- } catch (err) {
3081
- log$1.error({
3082
- err,
3083
- event: "tool_call_summary_injection_failed"
3084
- }, "tool_call_summary injection failed; passing payload through unchanged");
3085
- return;
3086
- }
3087
4503
  }
3088
- const toolCallSummaryExtension = (pi) => {
3089
- pi.on("before_provider_request", (event) => buildInjectedPayload(pi, event.payload));
3090
- pi.on("tool_call", (event) => {
3091
- stripInjectedSummary(pi, event.toolName, event.input);
3092
- });
4504
+ const toolHistoryExtension = (pi) => {
4505
+ pi.on("context", ({ messages }) => ({ messages: repairToolHistory(messages) }));
3093
4506
  };
3094
4507
  //#endregion
3095
4508
  //#region src/extensions/background-tasks.ts
@@ -3131,21 +4544,43 @@ const toolCallSummaryExtension = (pi) => {
3131
4544
  * from the origin messageId in its channel context (`resolveConversationFromApi`)
3132
4545
  * — and every run of the same conversation resolves to the same id, keeping the
3133
4546
  * shared map correctly scoped across turns. `bg_*`, the completion wake, and the
3134
- * next-session injection all filter to the resolved conversation — an agent
3135
- * never sees or is woken by a task from a different chat. Only the output log
4547
+ * next-session injection all filter by a **scope key** — the resolved
4548
+ * conversation id, or, when a session's conversation is unresolvable (a bare
4549
+ * CLI session, or a run whose channel-context ref carries no messageId), a
4550
+ * sentinel unique to that one session instance. Comparing on the raw
4551
+ * `conversationId` would bucket every unresolvable session together under
4552
+ * `null` and leak one's completion wake / status / next-session injection into
4553
+ * another; the sentinel keeps each isolated so an agent never sees or is woken
4554
+ * by a task from a different chat. Only the output log
3136
4555
  * spills to disk (/home/user/.anyone/bg-tasks/<id>.log) to avoid buffering a chatty job
3137
4556
  * in memory; exit code and run state live on the in-memory task.
3138
4557
  *
3139
- * **No cross-restart survival (v1, deliberate).** Task state lives only in
3140
- * the running harness process. A harness restart (crash → supervisord
3141
- * respawn, or `platform harness reload` after the agent edits its own
3142
- * harness) drops the map and pi's exec children are reaped with it. We don't
3143
- * resurrect from disk because the common next-run case cold-provisions a
3144
- * *different* sandbox anyway (warm reuse is the minority in prod), so on-disk
3145
- * state would rarely be the box the next run lands on. The idle-completion wake
3146
- * does cross the sandbox → platform boundary (a fresh run via `bg-task-done`),
3147
- * but a task whose harness dies before it finishes is gone — it is not
3148
- * resurrected, and this stays distinct from the scheduled-run (cron) system.
4558
+ * **Cross-restart survival is OPT-IN, via `bg_run({ resumable: true })`.**
4559
+ * A non-resumable task's state lives only in the running harness process: a
4560
+ * harness restart (crash → supervisord respawn, `platform harness reload`, or
4561
+ * a sandbox recycle on idle timeout / redeploy / template rebuild) drops the
4562
+ * map and pi's exec children are reaped with it, and the task is gone — the
4563
+ * right behavior for a one-shot side-effecting command, which must never
4564
+ * silently re-run.
4565
+ *
4566
+ * A RESUMABLE task additionally checkpoints its *spec* (command, cwd, labels,
4567
+ * origin messageId — not its live output/exit state) to a durable, api-side
4568
+ * journal keyed by conversation (`/sandbox/bg-task-journal`, redis). On the
4569
+ * NEXT run's `session_start` — which usually lands on a *different*,
4570
+ * cold-provisioned sandbox, which is exactly why the journal is api-side and
4571
+ * not on the sandbox disk — the harness lists the journal and relaunches any
4572
+ * spec it isn't already running, keeping the original id and prepending a
4573
+ * `<resumed-after-restart>` banner so the agent knows it re-ran from scratch,
4574
+ * not continued. The spec is dropped from the journal when the task
4575
+ * finishes/kills. Because relaunch RE-EXECUTES the command, resumable is only
4576
+ * for idempotent, long-lived work (pollers, watchers, retry loops); the tool
4577
+ * description enforces this and the default is false.
4578
+ *
4579
+ * The idle-completion wake (a task finishing while the agent is idle) crosses
4580
+ * the sandbox → platform boundary via a fresh run (`bg-task-done`) for both
4581
+ * resumable and non-resumable tasks; the journal is a separate, additive layer
4582
+ * that only handles a task whose harness dies BEFORE it finishes. This stays
4583
+ * distinct from the scheduled-run (cron) system.
3149
4584
  */
3150
4585
  const log = logger.child({ module: "background-tasks-ext" });
3151
4586
  const ops = createLocalBashOperations();
@@ -3156,6 +4591,7 @@ const WATCHDOG_INTERVAL_MS = 3e4;
3156
4591
  const KEEPALIVE_EVERY_MS = 6e4;
3157
4592
  const KEEPALIVE_MAX_MS = 3600 * 1e3;
3158
4593
  const STALL_HINT_AFTER_MS = 120 * 1e3;
4594
+ const PUBLISH_DEBOUNCE_MS = 300;
3159
4595
  const MAX_LOG_BYTES = 100 * 1024 * 1024;
3160
4596
  const DEFAULT_TAIL_LINES = 30;
3161
4597
  const TAIL_READ_BYTES = 64 * 1024;
@@ -3163,11 +4599,12 @@ function taskLabel(meta) {
3163
4599
  return `${meta.id} "${meta.description ?? meta.command.slice(0, 60)}"`;
3164
4600
  }
3165
4601
  let taskCounter = 0;
4602
+ let sessionScopeCounter = 0;
3166
4603
  const tasks = /* @__PURE__ */ new Map();
3167
4604
  let watchdogInterval = null;
3168
4605
  let lastKeepaliveAt = 0;
3169
- function sameConversation(meta, conversationId) {
3170
- return meta.conversationId === conversationId;
4606
+ function sameScope(meta, scopeKey) {
4607
+ return meta.scopeKey === scopeKey;
3171
4608
  }
3172
4609
  function logPath(id) {
3173
4610
  return join(tasksDir(), `${id}.log`);
@@ -3280,6 +4717,10 @@ function createBackgroundTasksExtension({ channelContext }) {
3280
4717
  return id;
3281
4718
  });
3282
4719
  }
4720
+ const unresolvedScopeSentinel = `unresolved:${process.pid.toString(36)}:${(sessionScopeCounter += 1).toString(36)}`;
4721
+ function scopeKey() {
4722
+ return conversationId ?? unresolvedScopeSentinel;
4723
+ }
3283
4724
  let agentActive = false;
3284
4725
  pi.on("agent_start", async () => {
3285
4726
  agentActive = true;
@@ -3307,13 +4748,16 @@ ${recentOutput}
3307
4748
  </background-task-finished>
3308
4749
  Run bg_logs for the full output.
3309
4750
 
3310
- This is a background-task completion, not a message from the user. If it needs no user-facing response — a routine or expected finish, a leftover or self-killed process, nothing the user must act on or would want to know right now — call \`platform channel suppress-reply\` and output nothing. Only send a message if the outcome changes what the user should do or know, or if you were explicitly waiting to report this result.`,
4751
+ This is a background-task completion, not a message from the user.
4752
+ For a routine or expected completion, output nothing.
4753
+ Only reply if the outcome changes what the user should know or do,
4754
+ or if you were explicitly waiting to report it.`,
3311
4755
  display: false
3312
4756
  };
3313
4757
  }
3314
4758
  async function notifyCompletion(meta) {
3315
4759
  if (meta.notified) return;
3316
- if (agentActive && sameConversation(meta, conversationId)) {
4760
+ if (agentActive && sameScope(meta, scopeKey())) {
3317
4761
  meta.notified = true;
3318
4762
  pi.sendMessage(await taskDoneMessage(meta), {
3319
4763
  triggerTurn: true,
@@ -3344,9 +4788,83 @@ This is a background-task completion, not a message from the user. If it needs n
3344
4788
  });
3345
4789
  } else log.info({ taskId: meta.id }, "bg task completed idle with no origin message; deferring to next session_start");
3346
4790
  }
3347
- async function launchTask({ command, description, cwd }) {
4791
+ const SNAPSHOT_TAIL_LINES = 40;
4792
+ let lastPublishedSignature = null;
4793
+ let publishInFlight = null;
4794
+ let publishQueued = false;
4795
+ function snapshotSignature(snapshot) {
4796
+ return JSON.stringify(snapshot.map((t) => ({
4797
+ id: t.id,
4798
+ state: t.state,
4799
+ exitCode: t.exitCode,
4800
+ killedReason: t.killedReason,
4801
+ outputTail: t.outputTail
4802
+ })));
4803
+ }
4804
+ async function doPublishSnapshot() {
4805
+ if (!messageId) return;
4806
+ const mine = [...tasks.values()].filter((t) => sameScope(t, scopeKey()));
4807
+ try {
4808
+ const snapshot = await Promise.all(mine.map(async (t) => ({
4809
+ id: t.id,
4810
+ command: t.command,
4811
+ description: t.description,
4812
+ startedAt: t.startedAt,
4813
+ state: t.running ? "running" : "finished",
4814
+ exitCode: t.exitCode,
4815
+ killedReason: t.killedReason,
4816
+ outputTail: await tailLog(t.id, SNAPSHOT_TAIL_LINES)
4817
+ })));
4818
+ const signature = snapshotSignature(snapshot);
4819
+ if (signature === lastPublishedSignature) return;
4820
+ lastPublishedSignature = signature;
4821
+ postBackgroundTasksSnapshot({
4822
+ messageId,
4823
+ tasks: snapshot
4824
+ });
4825
+ } catch (err) {
4826
+ log.debug({
4827
+ err,
4828
+ event: "bg_tasks_snapshot_build_failed"
4829
+ }, "building bg-tasks snapshot failed");
4830
+ }
4831
+ }
4832
+ async function publishSnapshotNow() {
4833
+ if (publishInFlight) {
4834
+ publishQueued = true;
4835
+ return;
4836
+ }
4837
+ publishInFlight = (async () => {
4838
+ try {
4839
+ do {
4840
+ publishQueued = false;
4841
+ await doPublishSnapshot();
4842
+ } while (publishQueued);
4843
+ } finally {
4844
+ publishInFlight = null;
4845
+ }
4846
+ })();
4847
+ await publishInFlight;
4848
+ }
4849
+ let publishTimer = null;
4850
+ function schedulePublishSnapshot() {
4851
+ if (publishTimer) return;
4852
+ publishTimer = setTimeout(() => {
4853
+ publishTimer = null;
4854
+ publishSnapshotNow();
4855
+ }, PUBLISH_DEBOUNCE_MS);
4856
+ publishTimer.unref?.();
4857
+ }
4858
+ async function flushPublishSnapshot() {
4859
+ if (publishTimer) {
4860
+ clearTimeout(publishTimer);
4861
+ publishTimer = null;
4862
+ }
4863
+ await publishSnapshotNow();
4864
+ }
4865
+ async function launchTask({ command, description, cwd, resumable = false, resumedFromJournal = false, id: providedId, startedAt: providedStartedAt }) {
3348
4866
  taskCounter += 1;
3349
- const id = `bg-${process.pid.toString(36)}-${taskCounter}`;
4867
+ const id = providedId ?? `bg-${process.pid.toString(36)}-${taskCounter}`;
3350
4868
  try {
3351
4869
  await mkdir(tasksDir(), { recursive: true });
3352
4870
  } catch (err) {
@@ -3362,7 +4880,7 @@ This is a background-task completion, not a message from the user. If it needs n
3362
4880
  taskId: id
3363
4881
  }, "bg task log write failed");
3364
4882
  });
3365
- const startedAt = Date.now();
4883
+ const startedAt = providedStartedAt ?? Date.now();
3366
4884
  const meta = {
3367
4885
  id,
3368
4886
  command: stripPlatformExportsForDisplay(command),
@@ -3370,6 +4888,7 @@ This is a background-task completion, not a message from the user. If it needs n
3370
4888
  logBytes: 0,
3371
4889
  lastOutputAt: startedAt,
3372
4890
  conversationId,
4891
+ scopeKey: scopeKey(),
3373
4892
  messageId,
3374
4893
  description,
3375
4894
  notified: false,
@@ -3377,9 +4896,23 @@ This is a background-task completion, not a message from the user. If it needs n
3377
4896
  controller: new AbortController(),
3378
4897
  running: true,
3379
4898
  exitCode: null,
3380
- error: null
4899
+ error: null,
4900
+ resumable,
4901
+ cwd,
4902
+ resumedFromJournal
3381
4903
  };
3382
4904
  tasks.set(id, meta);
4905
+ if (resumable && messageId) putBackgroundTaskJournalSpec({
4906
+ messageId,
4907
+ spec: {
4908
+ id,
4909
+ command,
4910
+ cwd,
4911
+ description,
4912
+ startedAt,
4913
+ messageId
4914
+ }
4915
+ });
3383
4916
  ops.exec(command, cwd, {
3384
4917
  onData: (chunk) => {
3385
4918
  meta.logBytes += chunk.length;
@@ -3405,6 +4938,11 @@ This is a background-task completion, not a message from the user. If it needs n
3405
4938
  taskId: id,
3406
4939
  exitCode: meta.exitCode
3407
4940
  }, "bg task finished");
4941
+ if (meta.resumable && meta.messageId) deleteBackgroundTaskJournalSpec({
4942
+ messageId: meta.messageId,
4943
+ taskId: id
4944
+ });
4945
+ schedulePublishSnapshot();
3408
4946
  await notifyCompletion(meta);
3409
4947
  });
3410
4948
  ensureWatchdog();
@@ -3412,19 +4950,55 @@ This is a background-task completion, not a message from the user. If it needs n
3412
4950
  taskId: id,
3413
4951
  conversationId
3414
4952
  }, "bg task started");
4953
+ schedulePublishSnapshot();
3415
4954
  return meta;
3416
4955
  }
3417
4956
  function knownTaskIds() {
3418
- return [...tasks.values()].filter((t) => sameConversation(t, conversationId)).map((t) => t.id).join(", ") || "(none)";
4957
+ return [...tasks.values()].filter((t) => sameScope(t, scopeKey())).map((t) => t.id).join(", ") || "(none)";
4958
+ }
4959
+ let journalChecked = false;
4960
+ async function rehydrateJournaledTasks() {
4961
+ if (!messageId || journalChecked) return;
4962
+ journalChecked = true;
4963
+ const specs = await listBackgroundTaskJournalSpecs({ messageId });
4964
+ if (specs.length === 0) return;
4965
+ for (const spec of specs) {
4966
+ const live = tasks.get(spec.id);
4967
+ if (live && sameScope(live, scopeKey())) continue;
4968
+ log.info({
4969
+ taskId: spec.id,
4970
+ conversationId
4971
+ }, "relaunching journaled resumable bg task after restart");
4972
+ const meta = await launchTask({
4973
+ command: spec.command,
4974
+ description: spec.description,
4975
+ cwd: spec.cwd,
4976
+ resumable: true,
4977
+ resumedFromJournal: true,
4978
+ id: spec.id,
4979
+ startedAt: spec.startedAt
4980
+ });
4981
+ try {
4982
+ const stream = createWriteStream(logPath(meta.id), { flags: "a" });
4983
+ stream.write(`<resumed-after-restart>requeued and relaunched on a new sandbox after the previous one was drained; this is a fresh execution of the command from the start, not a continuation</resumed-after-restart>\n`);
4984
+ stream.end();
4985
+ } catch (err) {
4986
+ log.debug({
4987
+ err,
4988
+ taskId: meta.id
4989
+ }, "resume banner write failed");
4990
+ }
4991
+ }
3419
4992
  }
3420
4993
  pi.on("session_start", async () => {
3421
- if (tasks.size === 0) return;
4994
+ if (tasks.size === 0 && (!messageId || journalChecked)) return;
3422
4995
  await ensureConversationId();
3423
- for (const [id, meta] of tasks) if (sameConversation(meta, conversationId) && !meta.running && meta.notified) {
4996
+ await rehydrateJournaledTasks();
4997
+ for (const [id, meta] of tasks) if (sameScope(meta, scopeKey()) && !meta.running && meta.notified) {
3424
4998
  tasks.delete(id);
3425
4999
  await unlink(logPath(id)).catch(() => {});
3426
5000
  }
3427
- const unnotified = [...tasks.values()].filter((t) => sameConversation(t, conversationId) && !t.notified && !t.running);
5001
+ const unnotified = [...tasks.values()].filter((t) => sameScope(t, scopeKey()) && !t.notified && !t.running);
3428
5002
  for (const meta of unnotified) {
3429
5003
  meta.notified = true;
3430
5004
  pi.sendMessage(await taskDoneMessage(meta));
@@ -3433,7 +5007,9 @@ This is a background-task completion, not a message from the user. If it needs n
3433
5007
  conversationId,
3434
5008
  count: unnotified.length
3435
5009
  }, "injected completed bg tasks at session_start");
3436
- if ([...tasks.values()].some((t) => sameConversation(t, conversationId) && t.running)) ensureWatchdog();
5010
+ if ([...tasks.values()].some((t) => sameScope(t, scopeKey()) && t.running)) ensureWatchdog();
5011
+ lastPublishedSignature = null;
5012
+ await flushPublishSnapshot();
3437
5013
  });
3438
5014
  function err(text) {
3439
5015
  return {
@@ -3450,11 +5026,11 @@ This is a background-task completion, not a message from the user. If it needs n
3450
5026
  }
3451
5027
  function resolveTask(taskId) {
3452
5028
  const exact = tasks.get(taskId);
3453
- if (exact && sameConversation(exact, conversationId)) return {
5029
+ if (exact && sameScope(exact, scopeKey())) return {
3454
5030
  error: null,
3455
5031
  meta: exact
3456
5032
  };
3457
- const matches = [...tasks.values()].filter((t) => sameConversation(t, conversationId) && t.id.startsWith(taskId));
5033
+ const matches = [...tasks.values()].filter((t) => sameScope(t, scopeKey()) && t.id.startsWith(taskId));
3458
5034
  if (matches.length === 1) return {
3459
5035
  error: null,
3460
5036
  meta: matches[0]
@@ -3463,7 +5039,7 @@ This is a background-task completion, not a message from the user. If it needs n
3463
5039
  return err(`Unknown task ${taskId}. Known tasks: ${knownTaskIds()}`);
3464
5040
  }
3465
5041
  function listTasks() {
3466
- const mine = [...tasks.values()].filter((t) => sameConversation(t, conversationId));
5042
+ const mine = [...tasks.values()].filter((t) => sameScope(t, scopeKey()));
3467
5043
  if (mine.length === 0) return "No background tasks.";
3468
5044
  return mine.map((t) => {
3469
5045
  const state = t.running ? "running" : t.exitCode !== null ? `exited ${t.exitCode}${t.killedReason ? ` (killed: ${t.killedReason})` : ""}` : t.killedReason ? `killed: ${t.killedReason}` : "ended";
@@ -3476,24 +5052,27 @@ This is a background-task completion, not a message from the user. If it needs n
3476
5052
  const bgRun = {
3477
5053
  name: "bg_run",
3478
5054
  label: "Run in background",
3479
- description: "Run a bash command in the background. Returns immediately with a task id; output streams to a log file. You are sent a message when it finishes — keep working or end your turn meanwhile. Use for anything over a couple of minutes (builds, batch jobs, retry loops, downloads). Inspect with bg_status / bg_logs, stop with bg_kill.",
3480
- promptSnippet: "bg_run — run a long command without blocking; you are notified on completion",
5055
+ description: "Run a bash command in the background. Returns immediately with a task id; output streams to a log file. You are sent a message when it finishes — keep working or end your turn meanwhile. Use for anything over a couple of minutes (builds, batch jobs, retry loops, downloads). Inspect with bg_status / bg_logs, stop with bg_kill. Pass resumable:true ONLY for an idempotent, long-lived command (a poller/watcher/retry loop) that should be requeued and relaunched on your next run if the sandbox goes away before it finishes (a sandbox is drained and replaced with a fresh one, not restarted in place) — the requeue re-runs the command from scratch, so never mark a one-shot side-effecting job (a migration, an apply, a send) resumable.",
5056
+ promptSnippet: "bg_run — run a long command without blocking; you are notified on completion (resumable:true requeues the task onto a fresh sandbox if the current one is drained, idempotent commands only)",
3481
5057
  parameters: Type.Object({
3482
5058
  command: Type.String({ description: "Bash command to execute" }),
3483
- description: Type.Optional(Type.String({ description: "Clear, concise description of what this command does in active voice (2-6 words)." }))
5059
+ description: Type.Optional(Type.String({ description: "Clear, concise description of what this command does in active voice (2-6 words)." })),
5060
+ resumable: Type.Optional(Type.Boolean({ description: "When true, this task is requeued and relaunched on your next run if the sandbox it is running on goes away before it finishes (a sandbox is drained and replaced with a fresh one, rather than restarted in place, so an in-flight task would otherwise be lost). Use ONLY for idempotent, long-lived commands (pollers, watchers, retry loops) — the requeue re-runs the command from scratch on the new sandbox, so never set this on a one-shot side-effecting command." }))
3484
5061
  }),
3485
5062
  async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
3486
5063
  await ensureConversationId();
3487
- const { command, description = null } = params;
5064
+ const { command, description = null, resumable = false } = params;
3488
5065
  const meta = await launchTask({
3489
5066
  command,
3490
5067
  description,
3491
- cwd: ctx.cwd
5068
+ cwd: ctx.cwd,
5069
+ resumable
3492
5070
  });
5071
+ const resumeNote = resumable && meta.messageId ? "\nResumable: if this sandbox is drained before the task finishes, it will be requeued and relaunched from the start on your next run." : resumable ? "\nNote: resumable was requested but this session has no durable conversation, so it will NOT be requeued if the sandbox is drained." : "";
3493
5072
  return {
3494
5073
  content: [{
3495
5074
  type: "text",
3496
- text: `Started background task ${taskLabel(meta)}.\nlog: ${logPath(meta.id)}\nYou will get a message when it finishes. Check on it with bg_status {"taskId":"${meta.id}"}.`
5075
+ text: `Started background task ${taskLabel(meta)}.\nlog: ${logPath(meta.id)}\nYou will get a message when it finishes. Check on it with bg_status {"taskId":"${meta.id}"}.${resumeNote}\n\nProgress is already visible to the user. Output nothing unless the user asked for an update.`
3497
5076
  }],
3498
5077
  details: {}
3499
5078
  };
@@ -3522,7 +5101,7 @@ This is a background-task completion, not a message from the user. If it needs n
3522
5101
  return {
3523
5102
  content: [{
3524
5103
  type: "text",
3525
- text: await describeStatus(resolved.meta, lines)
5104
+ text: `${await describeStatus(resolved.meta, lines)}${resolved.meta.running ? "\n\nProgress is already visible to the user. Output nothing unless the user asked for an update." : ""}`
3526
5105
  }],
3527
5106
  details: {}
3528
5107
  };
@@ -3613,6 +5192,7 @@ const all = [
3613
5192
  localToolsExtension,
3614
5193
  toolCallEnvExtension,
3615
5194
  bashDefaultTimeoutExtension,
5195
+ diskGuardExtension,
3616
5196
  toolCallSummaryExtension
3617
5197
  ];
3618
5198
  /**
@@ -3623,6 +5203,7 @@ const all = [
3623
5203
  */
3624
5204
  function platformExtensions({ sessionId, channelContext }) {
3625
5205
  return [
5206
+ providerApiCompatExtension,
3626
5207
  createPlatformExtensions({
3627
5208
  sessionId,
3628
5209
  channelContext
@@ -3631,8 +5212,11 @@ function platformExtensions({ sessionId, channelContext }) {
3631
5212
  selfTraceExtension,
3632
5213
  createBackgroundTasksExtension({ channelContext }),
3633
5214
  createSubagentExtension({ channelContext }),
3634
- createContextManagementExtension()
5215
+ createProactiveResultExtension({ channelContext }),
5216
+ createContextManagementExtension(),
5217
+ resourcePressureWarningExtension,
5218
+ toolHistoryExtension
3635
5219
  ];
3636
5220
  }
3637
5221
  //#endregion
3638
- export { all, createHarness, createHealthHandler, createPlatformEnvMiddleware, installToolUpdateAutoStop, isPlatformConfigLoaded, loadPlatformConfig, localToolsExtension, mcp_default as mcpExtension, memoryExtension, platformExtensions, runToolUpdateLoop, soulExtension, toolCallEnvExtension };
5222
+ export { all, createHarness, createHealthHandler, createPlatformEnvMiddleware, installToolUpdateAutoStop, isPlatformConfigLoaded, loadPlatformConfig, localToolsExtension, mcp_default as mcpExtension, memoryExtension, platformExtensions, providerApiCompatExtension, runClosingTextLoop, runToolUpdateLoop, soulExtension, toolCallEnvExtension, turnEndedWithoutVisibleText };