@skydiveai/pi-extensions 0.1.0-beta.167 → 0.1.0-beta.1673

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.
Files changed (2) hide show
  1. package/dist/index.mjs +1148 -242
  2. package/package.json +2 -9
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ 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
7
  import { z } from "zod";
8
8
  import { pathToFileURL } from "node:url";
@@ -19,6 +19,9 @@ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
19
19
  import { Resource } from "@opentelemetry/resources";
20
20
  import { BatchSpanProcessor, NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
21
21
  import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
22
+ import { execFile } from "node:child_process";
23
+ import { availableParallelism } from "node:os";
24
+ import { promisify } from "node:util";
22
25
  import { hc } from "hono/client";
23
26
  import { parse } from "yaml";
24
27
  import { quote } from "shell-quote";
@@ -256,7 +259,7 @@ function createHealthHandler({ metadata }) {
256
259
  * read on the hot path before every LLM call), it falls back to the default
257
260
  * for that knob and logs once.
258
261
  */
259
- const log$13 = logger.child({ module: "context-management-config" });
262
+ const log$15 = logger.child({ module: "context-management-config" });
260
263
  const DEFAULT_CONTEXT_MANAGEMENT_CONFIG = {
261
264
  enabled: false,
262
265
  perResultMaxBytes: 16 * 1024,
@@ -304,7 +307,7 @@ function resolveContextManagementConfig(env = process.env) {
304
307
  maxModelCallsPerTurn: env.SKYDIVE_CTX_MAX_MODEL_CALLS
305
308
  });
306
309
  if (!parsed.success) {
307
- log$13.warn({
310
+ log$15.warn({
308
311
  event: "context_management_config_invalid",
309
312
  err: parsed.error
310
313
  }, "falling back to default context-management config");
@@ -438,7 +441,7 @@ const CAPABILITY_SOUL_NUDGE = "New capability gained — once the current task i
438
441
  * or `ToolDefinition[]`. Files starting with `_` or `.` are skipped, so
439
442
  * `tools/_example.ts` documents the shape without registering.
440
443
  */
441
- const log$12 = logger.child({ module: "local-tools-extension" });
444
+ const log$14 = logger.child({ module: "local-tools-extension" });
442
445
  const TOOLS_DIRNAME = "tools";
443
446
  const fileState = /* @__PURE__ */ new Map();
444
447
  let pendingLocalToolsUpdate = null;
@@ -582,7 +585,7 @@ async function reconcileAndQueue({ pi, dir, reason }) {
582
585
  dir
583
586
  });
584
587
  if (reason !== "session_start" && summaryHasChanges$1(summary)) pendingLocalToolsUpdate = summary;
585
- log$12.info({
588
+ log$14.info({
586
589
  event: "local_tools_reconcile",
587
590
  reason,
588
591
  total_tools: summary.totalTools,
@@ -604,7 +607,7 @@ const localToolsExtension = (pi) => {
604
607
  reason: "session_start"
605
608
  });
606
609
  } catch (err) {
607
- log$12.error({
610
+ log$14.error({
608
611
  err,
609
612
  event: "local_tools_reconcile_failed"
610
613
  }, "local tools reconcile failed");
@@ -616,7 +619,7 @@ const localToolsExtension = (pi) => {
616
619
  try {
617
620
  current = await listToolFiles(dir);
618
621
  } catch (err) {
619
- log$12.warn({
622
+ log$14.warn({
620
623
  err,
621
624
  event: "local_tools_listing_failed"
622
625
  }, "tools/ listing failed");
@@ -638,7 +641,7 @@ const localToolsExtension = (pi) => {
638
641
  reason: "auto_reload"
639
642
  });
640
643
  } catch (err) {
641
- log$12.error({
644
+ log$14.error({
642
645
  err,
643
646
  event: "local_tools_auto_reload_failed"
644
647
  }, "auto-reload after tools/ change failed");
@@ -692,6 +695,19 @@ const STDERR_BUFFER_BYTES = 4096;
692
695
  * indistinguishable from any other transport problem. Walk the cause
693
696
  * chain so the agent sees the real underlying error.
694
697
  */
698
+ /**
699
+ * True when an error from an http MCP transport (connect, listTools, or a tool
700
+ * call) is an authentication failure. With no authProvider configured the SDK
701
+ * surfaces a 401 as `StreamableHTTPError(401)`; older paths translate it to
702
+ * `UnauthorizedError`. A dead/expired OAuth token (the proxy can no longer
703
+ * mint one) shows up here on the NEXT request against a previously-connected
704
+ * client — not just at connect — so reconcile must re-classify such a failure
705
+ * as `pending_auth` instead of a generic `failed`, keeping the "waiting on
706
+ * auth" report consistent with `platform auth`.
707
+ */
708
+ function isUnauthorizedError(err) {
709
+ return err instanceof UnauthorizedError || err instanceof StreamableHTTPError && err.code === 401;
710
+ }
695
711
  function formatError(err) {
696
712
  if (!(err instanceof Error)) return String(err);
697
713
  const parts = [err.message];
@@ -713,7 +729,7 @@ async function connectHttp(_id, config, client) {
713
729
  stderr: null
714
730
  };
715
731
  } catch (err) {
716
- if (err instanceof UnauthorizedError || err instanceof StreamableHTTPError && err.code === 401) return {
732
+ if (isUnauthorizedError(err)) return {
717
733
  status: "pending_auth",
718
734
  client,
719
735
  stderr: "",
@@ -863,7 +879,7 @@ async function loadMcpConfig(path) {
863
879
  * Clients are keyed by JSON-stringified config and reused across
864
880
  * reloads — only changed configs reconnect.
865
881
  */
866
- const log$11 = logger.child({ module: "mcp-extension" });
882
+ const log$13 = logger.child({ module: "mcp-extension" });
867
883
  async function closeConnected(connected) {
868
884
  try {
869
885
  await connected.client.close();
@@ -1190,6 +1206,64 @@ var McpExtension = class {
1190
1206
  try {
1191
1207
  mcpTools = (await connected.client.listTools()).tools;
1192
1208
  } catch (err) {
1209
+ if (isUnauthorizedError(err) && serverConfig.transport === "http") {
1210
+ await closeConnected(connected);
1211
+ const retry = await connectClient(id, serverConfig, { connectTimeoutMs });
1212
+ if (retry.status === "pending_auth") return {
1213
+ id,
1214
+ store: {
1215
+ client: retry.client,
1216
+ configKey,
1217
+ status: "pending_auth",
1218
+ stderrBuffer: null,
1219
+ cliHint: retry.cliHint
1220
+ },
1221
+ serverStatus: {
1222
+ status: "pending_auth",
1223
+ stderr: "",
1224
+ cliHint: retry.cliHint
1225
+ },
1226
+ change: null,
1227
+ error: null,
1228
+ tools: null
1229
+ };
1230
+ if (retry.status === "failed") return {
1231
+ id,
1232
+ store: null,
1233
+ serverStatus: {
1234
+ status: "failed",
1235
+ error: retry.error,
1236
+ stderr: retry.stderr
1237
+ },
1238
+ change: null,
1239
+ error: {
1240
+ serverId: id,
1241
+ message: retry.error
1242
+ },
1243
+ tools: null
1244
+ };
1245
+ if (retry.status === "connected") {
1246
+ connected = {
1247
+ client: retry.client,
1248
+ configKey,
1249
+ status: "connected",
1250
+ stderrBuffer: retry.stderr,
1251
+ cliHint: null
1252
+ };
1253
+ mcpTools = (await connected.client.listTools()).tools;
1254
+ return {
1255
+ id,
1256
+ store: connected,
1257
+ serverStatus: { status: "connected" },
1258
+ change: action === "reused" ? "refreshed" : action,
1259
+ error: null,
1260
+ tools: {
1261
+ client: connected.client,
1262
+ list: mcpTools
1263
+ }
1264
+ };
1265
+ }
1266
+ }
1193
1267
  const message = err instanceof Error ? err.message : String(err);
1194
1268
  const stderr = connected.stderrBuffer?.read() ?? "";
1195
1269
  return {
@@ -1227,7 +1301,7 @@ var McpExtension = class {
1227
1301
  });
1228
1302
  this.lastConfigMtimeMs = await readConfigMtimeMs(configPath);
1229
1303
  if (reason !== "session_start" && summaryHasChanges(summary)) this.pendingMcpUpdate = summary;
1230
- log$11.info({
1304
+ log$13.info({
1231
1305
  event: "mcp_reconcile",
1232
1306
  reason,
1233
1307
  total_tools: summary.totalTools,
@@ -1250,7 +1324,7 @@ var McpExtension = class {
1250
1324
  reason: "session_start"
1251
1325
  });
1252
1326
  } catch (err) {
1253
- log$11.error({
1327
+ log$13.error({
1254
1328
  err,
1255
1329
  event: "mcp_reconcile_failed"
1256
1330
  }, "MCP reconcile failed");
@@ -1262,7 +1336,7 @@ var McpExtension = class {
1262
1336
  try {
1263
1337
  mtime = await readConfigMtimeMs(configPath);
1264
1338
  } catch (err) {
1265
- log$11.warn({
1339
+ log$13.warn({
1266
1340
  err,
1267
1341
  event: "mcp_mtime_check_failed"
1268
1342
  }, "mtime check on mcp.config.json failed");
@@ -1276,7 +1350,7 @@ var McpExtension = class {
1276
1350
  reason: "auto_reload"
1277
1351
  });
1278
1352
  } catch (err) {
1279
- log$11.error({
1353
+ log$13.error({
1280
1354
  err,
1281
1355
  event: "mcp_auto_reload_failed"
1282
1356
  }, "auto-reload after mcp.config.json change failed");
@@ -1639,16 +1713,380 @@ const bashDefaultTimeoutExtension = (pi) => {
1639
1713
  });
1640
1714
  };
1641
1715
  //#endregion
1716
+ //#region src/extensions/resource-pressure-warning.ts
1717
+ /**
1718
+ * Mid-run resource-pressure warning to the agent.
1719
+ *
1720
+ * The sandbox already detects pressure — the boot scripts cap the
1721
+ * user-workload cgroup (memory.high/memory.max) and watchers log warn/crit
1722
+ * edges for memory and disk — but nothing told the *agent*, so a turn burned
1723
+ * straight to the OOM kill (or a full disk) and only learned about it from
1724
+ * the post-mortem notice. This extension closes that gap in-process: while a
1725
+ * turn is active it polls the agent cgroup and the root filesystem and, the
1726
+ * first time usage crosses a warn threshold, folds a system notification into
1727
+ * the open turn so the agent can checkpoint, shed work (constrain
1728
+ * parallelism, kill a background hog, clean scratch space), or request a
1729
+ * bigger tier BEFORE the kill.
1730
+ *
1731
+ * The notification is triggered by the two conditions that actually kill
1732
+ * work — memory near the cgroup hard cap, disk near full — and reports a
1733
+ * snapshot of all the relevant stats (memory, CPU utilization, disk) so the
1734
+ * agent can tell which resource is the problem and how much headroom the
1735
+ * others have.
1736
+ *
1737
+ * Edge-triggered, once per trigger per turn: the fired flags reset on
1738
+ * agent_start, so a turn that rides a threshold gets one warning per
1739
+ * resource, not a stream. Polling only runs while the agent is active — an
1740
+ * idle sandbox's resource usage is not the agent's problem and there is no
1741
+ * open turn to deliver into anyway.
1742
+ *
1743
+ * Best-effort throughout: any read failure (cgroup absent, controller not
1744
+ * delegated, non-cgroup-v2 host, df missing) reads as "no signal" for that
1745
+ * stat and the extension warns on what it can see — it must never break a
1746
+ * turn over an observability feature.
1747
+ */
1748
+ const execFileAsync = promisify(execFile);
1749
+ const log$12 = logger.child({ module: "resource-pressure-warning" });
1750
+ const POLL_INTERVAL_MS = 1e4;
1751
+ function envOverride(name) {
1752
+ for (const prefix of ["SKYDIVE_", "ANYONE_"]) {
1753
+ const value = process.env[`${prefix}${name}`];
1754
+ if (value != null && value !== "") return value;
1755
+ }
1756
+ return null;
1757
+ }
1758
+ function cgroupDir() {
1759
+ return envOverride("AGENT_CGROUP") ?? "/sys/fs/cgroup/agent";
1760
+ }
1761
+ function diskRoot() {
1762
+ return envOverride("DISK_ROOT") ?? "/";
1763
+ }
1764
+ /**
1765
+ * Read a cgroup v2 scalar file. Returns a number, or null for "max"
1766
+ * (uncapped), an empty/absent file, or any read/parse error — an uncapped or
1767
+ * unreadable limit means there is nothing meaningful to warn against.
1768
+ */
1769
+ async function readScalar(file) {
1770
+ try {
1771
+ const raw = (await readFile(`${cgroupDir()}/${file}`, "utf8")).trim();
1772
+ if (raw === "" || raw === "max") return null;
1773
+ const n = Number(raw);
1774
+ return Number.isFinite(n) ? n : null;
1775
+ } catch (_error) {
1776
+ return null;
1777
+ }
1778
+ }
1779
+ /**
1780
+ * Read a cgroup v2 "flat keyed" file (one `key value` pair per line, e.g.
1781
+ * cpu.stat) and return the counter for `key`, or null when absent.
1782
+ */
1783
+ async function readKeyedCounter(file, key) {
1784
+ try {
1785
+ const raw = await readFile(`${cgroupDir()}/${file}`, "utf8");
1786
+ for (const line of raw.split("\n")) {
1787
+ const [k, v] = line.trim().split(/\s+/);
1788
+ if (k === key) {
1789
+ const n = Number(v);
1790
+ return Number.isFinite(n) ? n : null;
1791
+ }
1792
+ }
1793
+ return null;
1794
+ } catch (_error) {
1795
+ return null;
1796
+ }
1797
+ }
1798
+ /**
1799
+ * Live memory usage as an integer percent of the hard cap, or null when
1800
+ * either side is unreadable/uncapped. Exported for tests.
1801
+ */
1802
+ async function readMemUsePct() {
1803
+ const [current, max] = await Promise.all([readScalar("memory.current"), readScalar("memory.max")]);
1804
+ if (current === null || max === null || max <= 0) return null;
1805
+ return {
1806
+ pct: Math.floor(current / max * 100),
1807
+ currentBytes: current,
1808
+ maxBytes: max
1809
+ };
1810
+ }
1811
+ /**
1812
+ * Root filesystem used% (df -P Capacity column), or null on any failure.
1813
+ * Exported for tests.
1814
+ */
1815
+ async function readDiskUsePct() {
1816
+ try {
1817
+ const { stdout } = await execFileAsync("df", ["-P", diskRoot()]);
1818
+ const dataRow = stdout.trim().split("\n")[1];
1819
+ if (dataRow == null) return null;
1820
+ const capacity = dataRow.trim().split(/\s+/)[4];
1821
+ if (capacity == null) return null;
1822
+ const pct = Number(capacity.replace("%", ""));
1823
+ return Number.isFinite(pct) ? pct : null;
1824
+ } catch (_error) {
1825
+ return null;
1826
+ }
1827
+ }
1828
+ /**
1829
+ * CPU utilization sampler. cgroup v2 exposes cumulative CPU time
1830
+ * (cpu.stat usage_usec); utilization is the delta between two samples over
1831
+ * the wall time between them, normalized by core count. The first call after
1832
+ * construction has no previous sample and returns null.
1833
+ */
1834
+ function createCpuSampler() {
1835
+ let prevUsageUsec = null;
1836
+ let prevAtMs = null;
1837
+ return async () => {
1838
+ const usage = await readKeyedCounter("cpu.stat", "usage_usec");
1839
+ const now = Date.now();
1840
+ const prev = prevUsageUsec;
1841
+ const prevAt = prevAtMs;
1842
+ prevUsageUsec = usage;
1843
+ prevAtMs = now;
1844
+ if (usage === null || prev === null || prevAt === null) return null;
1845
+ const wallUsec = (now - prevAt) * 1e3;
1846
+ if (wallUsec <= 0) return null;
1847
+ const cores = availableParallelism();
1848
+ const pct = Math.round((usage - prev) / (wallUsec * cores) * 100);
1849
+ return Math.max(0, Math.min(100, pct));
1850
+ };
1851
+ }
1852
+ function fmtMb(bytes) {
1853
+ return Math.round(bytes / 1024 / 1024);
1854
+ }
1855
+ /** The model-facing warning text. Exported for tests. */
1856
+ function resourcePressureWarningText(trigger, { mem, cpuPct, diskPct }) {
1857
+ const stats = [];
1858
+ if (mem) stats.push(`memory ${mem.pct}% of cap (${fmtMb(mem.currentBytes)}/${fmtMb(mem.maxBytes)} MB)`);
1859
+ if (cpuPct !== null) stats.push(`CPU ${cpuPct}%`);
1860
+ if (diskPct !== null) stats.push(`disk ${diskPct}% full`);
1861
+ 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.`;
1862
+ 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.";
1863
+ return `<system_notification>${lead} Current usage: ${stats.join(", ")}. Act now: ${remedy} If the workload genuinely needs more resources, request a bigger sandbox with \`platform compute request\`. This is an automated resource warning, not a message from the user; continue the task, adjusted.</system_notification>`;
1864
+ }
1865
+ const resourcePressureWarningExtension = (pi) => {
1866
+ let agentActive = false;
1867
+ let warnedMemThisTurn = false;
1868
+ let warnedDiskThisTurn = false;
1869
+ let timer = null;
1870
+ const sampleCpu = createCpuSampler();
1871
+ async function checkOnce() {
1872
+ if (!agentActive || warnedMemThisTurn && warnedDiskThisTurn) return;
1873
+ const [mem, cpuPct, diskPct] = await Promise.all([
1874
+ readMemUsePct(),
1875
+ sampleCpu(),
1876
+ readDiskUsePct()
1877
+ ]);
1878
+ let trigger = null;
1879
+ if (!warnedMemThisTurn && mem !== null && mem.pct >= 80) {
1880
+ trigger = "memory";
1881
+ warnedMemThisTurn = true;
1882
+ } else if (!warnedDiskThisTurn && diskPct !== null && diskPct >= 80) {
1883
+ trigger = "disk";
1884
+ warnedDiskThisTurn = true;
1885
+ }
1886
+ if (trigger === null) return;
1887
+ log$12.warn({
1888
+ trigger,
1889
+ mem,
1890
+ cpuPct,
1891
+ diskPct
1892
+ }, "resource pressure warning delivered to agent");
1893
+ await pi.sendMessage({
1894
+ customType: "anyone-resource-pressure-warning",
1895
+ content: resourcePressureWarningText(trigger, {
1896
+ mem,
1897
+ cpuPct,
1898
+ diskPct
1899
+ }),
1900
+ display: false
1901
+ }, {
1902
+ triggerTurn: true,
1903
+ deliverAs: "followUp"
1904
+ });
1905
+ }
1906
+ pi.on("agent_start", async () => {
1907
+ agentActive = true;
1908
+ warnedMemThisTurn = false;
1909
+ warnedDiskThisTurn = false;
1910
+ if (!timer) {
1911
+ timer = setInterval(() => {
1912
+ checkOnce().catch((err) => {
1913
+ log$12.error({ err }, "resource pressure check failed");
1914
+ });
1915
+ }, POLL_INTERVAL_MS);
1916
+ timer.unref?.();
1917
+ }
1918
+ });
1919
+ pi.on("agent_end", async () => {
1920
+ agentActive = false;
1921
+ if (timer) {
1922
+ clearInterval(timer);
1923
+ timer = null;
1924
+ }
1925
+ });
1926
+ };
1927
+ //#endregion
1928
+ //#region src/extensions/disk-guard.ts
1929
+ const log$11 = logger.child({ module: "disk-guard" });
1930
+ /**
1931
+ * In-band bypass. The guard is a safety net, not a jail: when the agent knows
1932
+ * a flagged command is genuinely safe (writing to a different mount, a tiny
1933
+ * bounded download, a delete-then-clone one-liner, an emergency it accepts the
1934
+ * risk on) it can force the command through by appending this marker as a
1935
+ * trailing shell comment. Kept as a comment so it never changes what the
1936
+ * command does, and matched case-insensitively with flexible spacing so the
1937
+ * agent doesn't have to reproduce it byte-for-byte.
1938
+ */
1939
+ const BYPASS_MARKER = /#\s*disk-guard:\s*allow\b/i;
1940
+ /** The exact marker text the block message tells the agent to append. */
1941
+ const BYPASS_HINT = "# disk-guard: allow";
1942
+ /**
1943
+ * Harness-level kill switch: set DISK_GUARD_DISABLE=1 to turn the guard off
1944
+ * entirely. This is the "I own my harness, let me opt out" knob — an agent
1945
+ * that boots its own harness can disable the guard for its whole process
1946
+ * without a code roll, and it's also the fleet-wide escape hatch if the
1947
+ * classifier ever misfires and blocks real work. The bare name is honored
1948
+ * first; the SKYDIVE_/ANYONE_ prefixes are accepted too for consistency with
1949
+ * the other env overrides. Empty/unset/"0"/"false" leave the guard on.
1950
+ */
1951
+ function guardDisabledByEnv() {
1952
+ for (const name of [
1953
+ "DISK_GUARD_DISABLE",
1954
+ "SKYDIVE_DISK_GUARD_DISABLE",
1955
+ "ANYONE_DISK_GUARD_DISABLE"
1956
+ ]) {
1957
+ const value = process.env[name];
1958
+ if (value != null && value !== "" && value !== "0" && value !== "false") return true;
1959
+ }
1960
+ return false;
1961
+ }
1962
+ /** True when the command carries the in-band bypass marker. */
1963
+ function hasBypassMarker(command) {
1964
+ return BYPASS_MARKER.test(command);
1965
+ }
1966
+ /**
1967
+ * Commands that reclaim space or merely inspect it. If any of these verbs
1968
+ * appears in the command line, we never block — otherwise the guard would trap
1969
+ * the agent by blocking the exact command it needs to dig out. Matched as
1970
+ * whole words so `remove-item` etc. don't accidentally match `rm`.
1971
+ */
1972
+ const RECLAIM_PATTERNS = [
1973
+ /\brm\b/,
1974
+ /\brmdir\b/,
1975
+ /\bdf\b/,
1976
+ /\bdu\b/,
1977
+ /\bncdu\b/,
1978
+ /\bfind\b[^|]*\s-delete\b/,
1979
+ /\btruncate\b/,
1980
+ /\bgit\s+(gc|prune|clean|worktree\s+remove|worktree\s+prune)\b/,
1981
+ /\b(yarn|npm|pnpm|bun)\s+.*\b(cache\s+clean|cache\s+clear|store\s+prune)\b/,
1982
+ /\bcache\s+(clean|clear|prune)\b/,
1983
+ /\b(docker|podman)\s+.*\bprune\b/,
1984
+ /\bapt(-get)?\s+clean\b/,
1985
+ /\bjournalctl\b[^|]*--vacuum/
1986
+ ];
1987
+ /**
1988
+ * File extensions that mean a download is actually LARGE — archives, disk
1989
+ * images, compiled/binary artifacts, model weights, media. A curl/wget is only
1990
+ * gated when it writes one of these; an API/page fetch to a `.json`/`.html`/
1991
+ * `.txt` file is tiny and must not be blocked. Derived from 4,144 real
1992
+ * commands: ~64% of `curl -o` uses were tiny fetches, only ~4% large.
1993
+ */
1994
+ 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)";
1995
+ /**
1996
+ * Commands that consume a meaningful amount of disk. Kept deliberately tight
1997
+ * and high-precision: validated against 4,144 real commands from the last 7
1998
+ * days, the earlier "writes a file" heuristic flagged 82% of everything (a
1999
+ * `curl -o /tmp/x.json` API call is not a disk event). This set flags ~33%,
2000
+ * almost all genuinely large — real installs, clones, big-archive downloads,
2001
+ * extractions. What was DROPPED and why:
2002
+ * - `git fetch` / `git pull` — incremental on an existing clone, usually tiny.
2003
+ * - `git checkout` — overwhelmingly `git checkout <ref> -- <file>` or a
2004
+ * branch switch, ~zero net growth; the rare full materialization isn't
2005
+ * worth the false-positive rate.
2006
+ * - bare `curl -o` / `wget -o` — see BIG_DOWNLOAD_EXT above.
2007
+ * - loose `… build` — matched `--mode=skip-build`, `oxfmt … build`, prose.
2008
+ * The remaining big-disk op in escher is `git clone` and `git worktree add`
2009
+ * (which is really a checkout), both kept.
2010
+ */
2011
+ const SPACE_HUNGRY_PATTERNS = [
2012
+ /\bgit\s+clone\b/,
2013
+ /\bgit\s+worktree\s+add\b/,
2014
+ /\b(yarn|npm|pnpm|bun)\s+(install|add|ci)\b/,
2015
+ /\byarn\s*$/,
2016
+ /\byarn\s+--(?!version|help)\S/,
2017
+ /\bpip3?\s+install\b/,
2018
+ /\bapt(-get)?\s+install\b/,
2019
+ /\bnpm\s+pack\b/,
2020
+ /\bdocker\s+(build|pull)\b/,
2021
+ new RegExp(`\\b(?:curl|wget)\\b[^\\n]*\\s-[a-zA-Z]*[oO]\\b[^\\n]*\\.${BIG_DOWNLOAD_EXT}\\b`, "i"),
2022
+ new RegExp(`\\b(?:curl|wget)\\b[^\\n]*\\.${BIG_DOWNLOAD_EXT}\\b[^\\n]*\\s-[a-zA-Z]*[oO]\\b`, "i"),
2023
+ /\btar\s+[^\n|]*x[^\n|]*f/,
2024
+ /\bunzip\b/,
2025
+ /\bdd\b[^\n|]*\bof=/
2026
+ ];
2027
+ /**
2028
+ * True when the command reclaims or inspects space — these are always allowed,
2029
+ * even on a 100%-full box, so the agent can dig itself out.
2030
+ */
2031
+ function isReclaimCommand(command) {
2032
+ return RECLAIM_PATTERNS.some((re) => re.test(command));
2033
+ }
2034
+ /**
2035
+ * True when the command is likely to consume a meaningful amount of disk.
2036
+ * A reclaim/inspect command is never space-hungry — the reclaim check wins so a
2037
+ * `git worktree remove` or a `yarn cache clean` is never mistaken for growth.
2038
+ */
2039
+ function isSpaceHungryCommand(command) {
2040
+ if (isReclaimCommand(command)) return false;
2041
+ return SPACE_HUNGRY_PATTERNS.some((re) => re.test(command));
2042
+ }
2043
+ /**
2044
+ * The decision, factored out and pure so it's exhaustively testable without a
2045
+ * real filesystem. Block only when we have a disk reading, it's at/above the
2046
+ * critical threshold, the command is space-hungry (and not a reclaim), and the
2047
+ * agent hasn't explicitly opted out with the bypass marker.
2048
+ */
2049
+ function shouldBlockForDisk(command, diskPct) {
2050
+ if (diskPct === null) return false;
2051
+ if (diskPct < 95) return false;
2052
+ if (hasBypassMarker(command)) return false;
2053
+ return isSpaceHungryCommand(command);
2054
+ }
2055
+ /** The agent-facing explanation returned as the blocked tool result. */
2056
+ function diskBlockReason(command, diskPct) {
2057
+ 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.`;
2058
+ }
2059
+ const diskGuardExtension = (pi) => {
2060
+ pi.on("tool_call", async (event) => {
2061
+ if (event.toolName !== "bash") return;
2062
+ if (guardDisabledByEnv()) return;
2063
+ const command = event.input.command;
2064
+ if (typeof command !== "string" || command.length === 0) return;
2065
+ if (hasBypassMarker(command)) return;
2066
+ if (!isSpaceHungryCommand(command)) return;
2067
+ const diskPct = await readDiskUsePct();
2068
+ if (!shouldBlockForDisk(command, diskPct)) return;
2069
+ log$11.warn({
2070
+ diskPct,
2071
+ command: command.slice(0, 200)
2072
+ }, "blocked space-hungry bash command on near-full disk");
2073
+ return {
2074
+ block: true,
2075
+ reason: diskBlockReason(command, diskPct)
2076
+ };
2077
+ });
2078
+ };
2079
+ //#endregion
1642
2080
  //#region src/channel-context-ref.ts
1643
2081
  /**
1644
- * The worker injects only a reference — `{ channel, messageId }` — into the
1645
- * sandbox env (`SKYDIVE_CHANNEL_CONTEXT`) rather than the full context.
2082
+ * The worker injects a small reference — `{ channel, messageId, runId }` —
2083
+ * into the sandbox env (`SKYDIVE_CHANNEL_CONTEXT`) rather than the full context.
1646
2084
  *
1647
2085
  * The canonical `ChannelContextRef` type + `parseChannelContextRef` live in
1648
2086
  * `@createinc/anyone-channels`, but the harness (`@skydiveai/*`) keeps zero
1649
2087
  * `@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.
2088
+ * platform channel stack (Slack/email/Linq SDKs, messaging) just to read one
2089
+ * field. So we validate the field this consumer needs locally instead.
1652
2090
  */
1653
2091
  const channelContextRefSchema = z.object({ messageId: z.string().nullable() });
1654
2092
  /**
@@ -1680,6 +2118,23 @@ function apiBaseUrl() {
1680
2118
  }
1681
2119
  //#endregion
1682
2120
  //#region src/extensions/platform.ts
2121
+ /**
2122
+ * Platform extension — bridges the agent harness to the Skydive platform daemon.
2123
+ *
2124
+ * Responsibilities:
2125
+ * - Heartbeat: periodic POST to the API so the sandbox manager knows the
2126
+ * agent is alive. Throttled to once per minute, triggered by tool events.
2127
+ * - Session tracking: registers the session with the daemon on start,
2128
+ * streams tool_call / tool_result events so the daemon can track which
2129
+ * session is actively executing, and signals session end on agent_end.
2130
+ * - Channel context: passes the SKYDIVE_CHANNEL_CONTEXT (containing the
2131
+ * messageId) to the daemon so file writes can be attributed to the
2132
+ * correct conversation.
2133
+ *
2134
+ * All daemon POSTs are fire-and-forget — failures are logged but never
2135
+ * block the agent. The daemon may not be running (e.g. local dev without
2136
+ * a sandbox), and that's fine.
2137
+ */
1683
2138
  const HEARTBEAT_THROTTLE_MS = 6e4;
1684
2139
  const TOOL_HEARTBEAT_INTERVAL_MS = 5e3;
1685
2140
  const MAX_TOOL_HEARTBEATS = 1440 * 60 * 1e3 / TOOL_HEARTBEAT_INTERVAL_MS;
@@ -1691,9 +2146,56 @@ function sandboxClient() {
1691
2146
  return hc(`${apiUrl}/api/v1/sandbox`);
1692
2147
  }
1693
2148
  /**
1694
- * Fetch every harness feature flag in one GET (`{ contextManagement, subagent,
1695
- * ... }` 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
2149
+ * Is this box still an unclaimed warm-pool sandbox? (ANY-6000, the
2150
+ * feature-flags half of the ANY-5184 pool 403 wave.)
2151
+ *
2152
+ * `GET /sandbox/feature-flags` is agent-only, so the shared poller's request
2153
+ * from a pool box can only 403 — a guaranteed-failing GET every 60s for the
2154
+ * life of the pool phase. The discriminator is the sandbox token's `type`
2155
+ * claim, read UNVERIFIED (this box never holds the signing secret): not an
2156
+ * authorization decision, only "should I bother calling?", and the api still
2157
+ * authorizes every request.
2158
+ *
2159
+ * Read per call from the daemon's persisted env file, NOT process.env:
2160
+ * claiming a pool box rebinds the token in place (the daemon rewrites this
2161
+ * file) while the harness's process.env keeps the boot snapshot, so a
2162
+ * process-env gate would leave a claimed box permanently skipping — trading a
2163
+ * wasted request for silently frozen flags, which is strictly worse. "Cannot
2164
+ * tell" (no file, no token, unparseable payload) reports false so the poll
2165
+ * proceeds.
2166
+ */
2167
+ const daemonEnvIdentitySchema = z.object({
2168
+ ANYONE_SANDBOX_TOKEN: z.string().optional(),
2169
+ SKYDIVE_SANDBOX_TOKEN: z.string().optional()
2170
+ }).passthrough();
2171
+ const tokenTypeSchema = z.object({ type: z.string() }).passthrough();
2172
+ async function isPoolIdentity() {
2173
+ try {
2174
+ const override = process.env.ANYONE_DAEMON_ENV_CACHE;
2175
+ const candidates = override ? [override] : ["/run/anyone-system/daemon-env.json", "/tmp/.anyone/daemon-env.json"];
2176
+ let raw = null;
2177
+ for (const file of candidates) {
2178
+ raw = await readFile(file, "utf8").catch(() => null);
2179
+ if (raw !== null) break;
2180
+ }
2181
+ if (raw === null) return false;
2182
+ const env = daemonEnvIdentitySchema.safeParse(JSON.parse(raw));
2183
+ if (!env.success) return false;
2184
+ const token = env.data.ANYONE_SANDBOX_TOKEN ?? env.data.SKYDIVE_SANDBOX_TOKEN;
2185
+ if (typeof token !== "string" || token === "") return false;
2186
+ const payload = token.split(".")[1];
2187
+ if (!payload) return false;
2188
+ const claims = tokenTypeSchema.safeParse(JSON.parse(Buffer.from(payload, "base64url").toString("utf8")));
2189
+ return claims.success && claims.data.type === "onboarding-pool";
2190
+ } catch (_err) {
2191
+ return false;
2192
+ }
2193
+ }
2194
+ /**
2195
+ * Fetch every harness feature flag in one GET (`{ contextManagement, ... }`
2196
+ * — see apps/anyone/api/src/routes/sandbox-feature-flags.ts). Returns
2197
+ * null when indeterminate (no api url, the request failed, or the box is an
2198
+ * unclaimed pool sandbox whose token the route would 403) so the shared
1697
2199
  * poller keeps the last-known values rather than flipping on a transient error.
1698
2200
  * This is the single fetch behind `feature-flags-poll.ts`; extensions read the
1699
2201
  * polled values there instead of issuing their own GET.
@@ -1701,6 +2203,7 @@ function sandboxClient() {
1701
2203
  async function fetchHarnessFlags() {
1702
2204
  const client = sandboxClient();
1703
2205
  if (!client) return null;
2206
+ if (await isPoolIdentity()) return null;
1704
2207
  try {
1705
2208
  const res = await client["feature-flags"].$get();
1706
2209
  if (!res.ok) {
@@ -1710,11 +2213,7 @@ async function fetchHarnessFlags() {
1710
2213
  }, "feature-flags fetch failed");
1711
2214
  return null;
1712
2215
  }
1713
- const body = await res.json();
1714
- return {
1715
- contextManagement: body.contextManagement ?? null,
1716
- subagent: body.subagent ?? null
1717
- };
2216
+ return { contextManagement: (await res.json()).contextManagement ?? null };
1718
2217
  } catch (err) {
1719
2218
  log$10.debug({
1720
2219
  err,
@@ -1765,6 +2264,71 @@ async function postBackgroundTaskDone({ messageId, content }) {
1765
2264
  } });
1766
2265
  if (!res.ok) throw new Error(`bg-task-done POST failed: ${res.status}`);
1767
2266
  }
2267
+ async function putBackgroundTaskJournalSpec({ messageId, spec }) {
2268
+ const client = sandboxClient();
2269
+ if (!client) return;
2270
+ try {
2271
+ const res = await client["bg-task-journal"].$put({ json: {
2272
+ messageId,
2273
+ spec
2274
+ } });
2275
+ if (!res.ok) log$10.warn({
2276
+ status: res.status,
2277
+ taskId: spec.id,
2278
+ event: "bg_journal_put_failed"
2279
+ }, "bg-task journal PUT failed");
2280
+ } catch (err) {
2281
+ log$10.warn({
2282
+ err,
2283
+ taskId: spec.id,
2284
+ event: "bg_journal_put_failed"
2285
+ }, "bg-task journal PUT threw");
2286
+ }
2287
+ }
2288
+ async function deleteBackgroundTaskJournalSpec({ messageId, taskId }) {
2289
+ const client = sandboxClient();
2290
+ if (!client) return;
2291
+ try {
2292
+ await client["bg-task-journal"].$delete({ json: {
2293
+ messageId,
2294
+ taskId
2295
+ } });
2296
+ } catch (err) {
2297
+ log$10.debug({
2298
+ err,
2299
+ taskId,
2300
+ event: "bg_journal_delete_failed"
2301
+ }, "bg-task journal DELETE failed");
2302
+ }
2303
+ }
2304
+ async function listBackgroundTaskJournalSpecs({ messageId }) {
2305
+ const client = sandboxClient();
2306
+ if (!client) return [];
2307
+ try {
2308
+ const res = await client["bg-task-journal"].$get({ query: { messageId } });
2309
+ if (!res.ok) return [];
2310
+ return (await res.json()).specs ?? [];
2311
+ } catch (err) {
2312
+ log$10.debug({
2313
+ err,
2314
+ event: "bg_journal_list_failed"
2315
+ }, "bg-task journal GET failed");
2316
+ return [];
2317
+ }
2318
+ }
2319
+ function postBackgroundTasksSnapshot({ messageId, tasks }) {
2320
+ const client = sandboxClient();
2321
+ if (!client || !messageId) return;
2322
+ client["bg-tasks"].$post({ json: {
2323
+ messageId,
2324
+ tasks
2325
+ } }).catch((err) => {
2326
+ log$10.debug({
2327
+ err,
2328
+ event: "bg_tasks_snapshot_failed"
2329
+ }, "bg-tasks snapshot publish failed");
2330
+ });
2331
+ }
1768
2332
  async function postSubagentSpawn({ messageId, tasks }) {
1769
2333
  const client = sandboxClient();
1770
2334
  if (!client) throw new Error("no api url for subagent-spawn");
@@ -1772,8 +2336,19 @@ async function postSubagentSpawn({ messageId, tasks }) {
1772
2336
  messageId,
1773
2337
  tasks
1774
2338
  } });
1775
- if (!res.ok) throw new Error(`subagent-spawn POST failed: ${res.status}`);
1776
- return { taskIds: (await res.json()).taskIds };
2339
+ if (!res.ok) {
2340
+ let detail = "";
2341
+ try {
2342
+ const errBody = await res.json();
2343
+ if (errBody && typeof errBody.error === "string") detail = `: ${errBody.error}`;
2344
+ } catch {}
2345
+ throw new Error(`subagent-spawn POST failed (${res.status})${detail}`);
2346
+ }
2347
+ const body = await res.json();
2348
+ return {
2349
+ taskIds: body.taskIds,
2350
+ tasks: body.tasks ?? []
2351
+ };
1777
2352
  }
1778
2353
  function createHeartbeatThrottle({ messageId }) {
1779
2354
  let lastAt = 0;
@@ -1922,20 +2497,16 @@ function createPlatformExtensions({ sessionId, channelContext }) {
1922
2497
  * Shared harness feature-flag poll.
1923
2498
  *
1924
2499
  * The api exposes one `/feature-flags` GET that returns every harness flag in a
1925
- * single response (`{ contextManagement, subagent, commandFlags }` — see
2500
+ * single response (`{ contextManagement, commandFlags }` — see
1926
2501
  * apps/anyone/api/src/routes/sandbox-feature-flags.ts). Rather than each
1927
2502
  * extension issuing its own GET — and, worse, a *blocking* GET on the
1928
2503
  * pre-first-token `session_start` path — a single background poller fetches
1929
2504
  * that response once per interval and fans the values out to every subscriber.
1930
2505
  *
1931
- * Why one poller: the subagent extension gates its tool registration on the
1932
- * `subagent` flag. If it awaited a fresh GET inside `session_start` the tool
1933
- * schema (part of the prefill) couldn't be finalized until a serial
1934
- * sandbox→api round-trip settled, adding a net-new pre-token network hop on
1935
- * every session, flag on or off. Reading the last-polled value instead keeps
1936
- * the hot path allocation-only. A cold cache reads as `null` (fail-open to
1937
- * unregistered); a newly-flipped flag takes effect on the next poll, matching
1938
- * how context-management already treats its flag.
2506
+ * Why one poller: context-management consumes the `contextManagement` flag
2507
+ * without a blocking GET on the pre-first-token `session_start` path. Reading
2508
+ * the last-polled value keeps the hot path allocation-only; a cold cache reads
2509
+ * as `null` and a newly-flipped flag takes effect on the next poll.
1939
2510
  *
1940
2511
  * The poll is fire-and-forget and self-unref'd — it never keeps the process
1941
2512
  * alive and an indeterminate result (no api url / transient failure) leaves the
@@ -1944,15 +2515,11 @@ function createPlatformExtensions({ sessionId, channelContext }) {
1944
2515
  const log$9 = logger.child({ module: "feature-flags-poll" });
1945
2516
  const FLAG_POLL_INTERVAL_MS = 6e4;
1946
2517
  let contextManagement = null;
1947
- let subagent = null;
1948
- const subscribers = {
1949
- contextManagement: /* @__PURE__ */ new Set(),
1950
- subagent: /* @__PURE__ */ new Set()
1951
- };
2518
+ const subscribers = { contextManagement: /* @__PURE__ */ new Set() };
1952
2519
  let pollerStarted = false;
1953
2520
  let firstPollSettled = false;
1954
2521
  let resolveFirstPoll = null;
1955
- const firstPollPromise = new Promise((resolve) => {
2522
+ new Promise((resolve) => {
1956
2523
  resolveFirstPoll = resolve;
1957
2524
  });
1958
2525
  function markFirstPollSettled() {
@@ -1961,20 +2528,8 @@ function markFirstPollSettled() {
1961
2528
  resolveFirstPoll?.();
1962
2529
  }
1963
2530
  /** Last-polled value of a flag, or `null` if not yet resolved. */
1964
- function getPolledFlag(name) {
1965
- return name === "contextManagement" ? contextManagement : subagent;
1966
- }
1967
- /**
1968
- * Await the first poll already kicked by `startFeatureFlagPoller` (never a new
1969
- * GET). Resolves when that poll settles, immediately if it already has, or
1970
- * immediately when there's no flag source to poll. Callers on the hot path
1971
- * should race this against their own short timeout so a slow/failed flag
1972
- * service cannot delay first-token; a timeout just means the caller reads the
1973
- * still-cold cache and falls back to its default, exactly as before.
1974
- */
1975
- function awaitFirstFlagPoll() {
1976
- if (firstPollSettled || !hasFlagSource()) return Promise.resolve();
1977
- return firstPollPromise;
2531
+ function getPolledFlag(_name) {
2532
+ return contextManagement;
1978
2533
  }
1979
2534
  /**
1980
2535
  * Subscribe to changes of a flag. The callback fires only on a *transition*
@@ -1987,9 +2542,8 @@ function onFlagChange(name, cb) {
1987
2542
  }
1988
2543
  function apply(name, next) {
1989
2544
  if (next === null) return;
1990
- const prev = name === "contextManagement" ? contextManagement : subagent;
1991
- if (name === "contextManagement") contextManagement = next;
1992
- else subagent = next;
2545
+ const prev = contextManagement;
2546
+ contextManagement = next;
1993
2547
  if (next !== prev) for (const cb of subscribers[name]) try {
1994
2548
  cb(next);
1995
2549
  } catch (err) {
@@ -2004,7 +2558,6 @@ async function pollOnce() {
2004
2558
  const flags = await fetchHarnessFlags();
2005
2559
  if (!flags) return;
2006
2560
  apply("contextManagement", flags.contextManagement ?? null);
2007
- apply("subagent", flags.subagent ?? null);
2008
2561
  } catch (err) {
2009
2562
  log$9.debug({ err }, "feature-flag poll threw");
2010
2563
  }
@@ -2236,37 +2789,35 @@ const currentTimeExtension = (pi) => {
2236
2789
  //#endregion
2237
2790
  //#region src/memory.ts
2238
2791
  /**
2239
- * In-harness memory index builder.
2792
+ * In-harness memory readers.
2240
2793
  *
2241
- * The agent has an agent-level file-based memory at `<cwd>/.memory/`,
2242
- * organized by directory:
2794
+ * The agent has an agent-level file-based memory at `<cwd>/.memory/`, split
2795
+ * into two halves that are surfaced differently:
2243
2796
  *
2244
- * .memory/users/<id>-<name>/<topic>.md
2245
- * .memory/projects/<project_slug>/<topic>.md
2246
- * .memory/feedback/<topic>.md
2247
- * .memory/reference/<topic>.md
2797
+ * 1. Shared knowledge (projects, lessons, external systems) — indexed by a
2798
+ * single hand-maintained `<cwd>/.memory/MEMORY.md` that the *agent* writes
2799
+ * and curates, Claude-Code style: one line per fact pointing at the file
2800
+ * that holds it. `readMemoryIndexFile` just reads that file; the agent owns
2801
+ * its contents. This is the whole index for the shared half — there is no
2802
+ * derived walk and no per-directory `MEMORY.md`.
2248
2803
  *
2249
- * The path encodes type and subject (for `users/`, the subject is the
2250
- * person's stable id with a readable name suffix); each `.md` file's
2251
- * frontmatter only carries `name` and `description`.
2804
+ * 2. Per-person memory `.memory/users/<id>-<name>/<topic>.md`. This half is
2805
+ * *derived*, not hand-maintained, because it has to be filtered to the one
2806
+ * person on the current turn (a single hand-written index couldn't be
2807
+ * scoped per-user without leaking one person's notes into another's
2808
+ * conversation). `buildMemoryIndex` walks a single user's directory, reads
2809
+ * only the frontmatter of each `.md` (open fd → read first ~4KB → close, in
2810
+ * parallel), and renders an index. Each `.md`'s frontmatter carries `name`
2811
+ * and `description`; bodies are never read — the agent loads a specific
2812
+ * memory's body on demand via the `read` tool.
2252
2813
  *
2253
- * `buildMemoryIndex` walks `.memory/` by type directory, reads only the
2254
- * frontmatter of each `.md` (open fd read first ~4KB close, in
2255
- * parallel), and renders a markdown index grouped by type and (where
2256
- * applicable) by subject. Files outside the four type directories are
2257
- * ignored. Bodies are never read — the agent loads a specific memory's
2258
- * body on demand via the `read` tool when the index entry says it's
2259
- * relevant.
2814
+ * Mtime cache (for the derived per-user half) keyed by cwd within the
2815
+ * lifetime of a sandbox the cwd is fixed, so this is effectively a single-entry
2816
+ * cache. It invalidates when any `.md` under `users/` is added/modified/
2817
+ * deleted; turns where memory didn't change reuse the cached entries.
2260
2818
  *
2261
- * Mtime cache keyed by cwd within the lifetime of a sandbox the cwd
2262
- * is fixed, so this is effectively a single-entry cache. Cache invalidates
2263
- * when any `.md` in the tree is added/modified/deleted; turns where
2264
- * memory didn't change reuse the cached string.
2265
- *
2266
- * Frontmatter is parsed as YAML (`yaml` package) and validated with a
2267
- * zod schema — files that don't match the shape are dropped from the
2268
- * index. The same schema can be reused at write time if we want to
2269
- * validate before commit.
2819
+ * Frontmatter is parsed as YAML (`yaml` package) and validated with a zod
2820
+ * schema files that don't match the shape are dropped from the index.
2270
2821
  */
2271
2822
  const FRONTMATTER_READ_BYTES = 4096;
2272
2823
  const FrontmatterSchema = z.object({
@@ -2274,33 +2825,118 @@ const FrontmatterSchema = z.object({
2274
2825
  description: z.string().min(1)
2275
2826
  }).passthrough();
2276
2827
  const MEMORY_DIRNAME = ".memory";
2277
- const TYPE_DIRS = [
2278
- "users",
2279
- "projects",
2280
- "feedback",
2281
- "reference"
2828
+ const MEMORY_INDEX_FILENAME = "MEMORY.md";
2829
+ const USERS_DIRNAME = "users";
2830
+ /**
2831
+ * The shared-knowledge type dirs from the old frontmatter-indexed layout, used
2832
+ * only to seed a `MEMORY.md` for agents created before it existed (see
2833
+ * `seedMemoryIndexFile`). `users/` is deliberately excluded — per-person memory
2834
+ * stays derived and never lands in the shared, un-scoped `MEMORY.md`.
2835
+ */
2836
+ const LEGACY_SHARED_TYPES = [
2837
+ {
2838
+ dir: "projects",
2839
+ label: "Projects"
2840
+ },
2841
+ {
2842
+ dir: "feedback",
2843
+ label: "Feedback"
2844
+ },
2845
+ {
2846
+ dir: "reference",
2847
+ label: "Reference"
2848
+ }
2282
2849
  ];
2283
- const TYPES_WITH_SUBJECT = new Set(["users", "projects"]);
2284
- const TYPE_LABELS = {
2285
- users: "Users",
2286
- projects: "Projects",
2287
- feedback: "Feedback",
2288
- reference: "Reference"
2289
- };
2850
+ /**
2851
+ * Soft budget for an injected index block. The index is read and injected into
2852
+ * the system prompt on every turn, so every entry costs context for the rest of
2853
+ * the conversation. Past this size we nudge the agent to consolidate and prune
2854
+ * rather than keep appending. Not a hard cap — nothing is truncated.
2855
+ */
2856
+ const MEMORY_INDEX_SOFT_BUDGET_CHARS = 2e4;
2857
+ /**
2858
+ * Hard cap on the injected index — double the soft budget. The soft budget only
2859
+ * warns; this actually bounds what we inject so a runaway index can't consume
2860
+ * unbounded context on every turn. Past this, the index is truncated (on a line
2861
+ * boundary) before injection. It's a backstop, not a normal operating point.
2862
+ */
2863
+ const MEMORY_INDEX_HARD_BUDGET_CHARS = MEMORY_INDEX_SOFT_BUDGET_CHARS * 2;
2864
+ /**
2865
+ * Cheap size summary of a rendered index block, used to surface how much of the
2866
+ * every-turn context budget the index is spending so the agent keeps it lean.
2867
+ * Counts pointer/file lines — both the derived `` - `path` — desc`` form and
2868
+ * the hand-maintained `- [Title](path) — hook` form — not the group headers.
2869
+ */
2870
+ function summarizeIndex(index) {
2871
+ const entryCount = index.split("\n").filter((line) => /^\s*- (?:`|\[)/.test(line)).length;
2872
+ const charCount = index.length;
2873
+ return {
2874
+ entryCount,
2875
+ charCount,
2876
+ overBudget: charCount > MEMORY_INDEX_SOFT_BUDGET_CHARS
2877
+ };
2878
+ }
2879
+ /**
2880
+ * One-line size note for an index block header, e.g. `12 entries, 3187 chars`.
2881
+ */
2882
+ function indexSizeNote(index) {
2883
+ const { entryCount, charCount } = summarizeIndex(index);
2884
+ return `${entryCount} ${entryCount === 1 ? "entry" : "entries"}, ${charCount} chars`;
2885
+ }
2886
+ /**
2887
+ * An explicit warning to surface to the agent when an index has grown past its
2888
+ * budget, or `null` when it's within budget. Extensions render this prominently
2889
+ * above the index so the agent prunes before it keeps appending.
2890
+ */
2891
+ function indexBudgetWarning(index) {
2892
+ const { charCount, overBudget } = summarizeIndex(index);
2893
+ if (!overBudget) return null;
2894
+ 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.`;
2895
+ }
2896
+ /**
2897
+ * Enforce the hard cap on an index before injection. Under the cap the index is
2898
+ * returned unchanged; over it, the index is truncated on a line boundary and a
2899
+ * notice is appended naming the true size so the agent knows entries are hidden
2900
+ * and must be pruned. This is the actual bound on injected context — callers
2901
+ * still report the true size via {@link indexSizeNote} so nothing is masked.
2902
+ */
2903
+ function enforceMemoryIndexHardBudget(index) {
2904
+ if (index.length <= 4e4) return index;
2905
+ const clipped = index.slice(0, MEMORY_INDEX_HARD_BUDGET_CHARS);
2906
+ const lastNewline = clipped.lastIndexOf("\n");
2907
+ 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.`;
2908
+ }
2909
+ /**
2910
+ * Read the agent's hand-maintained shared index at `.memory/MEMORY.md`.
2911
+ * Returns the trimmed contents, or `null` when the file is absent or empty —
2912
+ * the agent owns this file, so we surface exactly what it wrote.
2913
+ */
2914
+ async function readMemoryIndexFile({ cwd }) {
2915
+ const path = join(cwd, MEMORY_DIRNAME, MEMORY_INDEX_FILENAME);
2916
+ try {
2917
+ const trimmed = (await readFile(path, "utf-8")).trim();
2918
+ return trimmed.length > 0 ? trimmed : null;
2919
+ } catch {
2920
+ return null;
2921
+ }
2922
+ }
2290
2923
  const cache = /* @__PURE__ */ new Map();
2291
2924
  /**
2925
+ * Build the derived per-person index for a single user.
2926
+ *
2292
2927
  * Returns:
2293
- * - `null` if `.memory/` doesn't exist
2294
- * - `""` if the dir exists but contains nothing in the requested scope
2928
+ * - `null` if `.memory/users/` doesn't exist
2929
+ * - `""` if it exists but this user has no memory
2295
2930
  * - rendered markdown body (no surrounding header — caller wraps)
2296
2931
  *
2297
- * The mtime-keyed cache stores the raw walked entries (the cost is the FS
2298
- * walk); filtering by scope is cheap and runs per call, so two turns with
2299
- * different scopes on the same cwd render correctly from one cached walk.
2932
+ * Scoping by id keeps one person's memory from bleeding into another's
2933
+ * conversation. The mtime-keyed cache stores the raw walked entries (the cost
2934
+ * is the FS walk); filtering by user is cheap and runs per call, so two turns
2935
+ * with different users on the same cwd render correctly from one cached walk.
2300
2936
  */
2301
- async function buildMemoryIndex({ cwd, scope }) {
2302
- const memoryDirAbs = join(cwd, MEMORY_DIRNAME);
2303
- const maxMtimeMs = await maxMtimeAcrossDir(memoryDirAbs);
2937
+ async function buildMemoryIndex({ cwd, userId }) {
2938
+ const usersDirAbs = join(cwd, MEMORY_DIRNAME, USERS_DIRNAME);
2939
+ const maxMtimeMs = await maxMtimeAcrossDir(usersDirAbs);
2304
2940
  if (maxMtimeMs === null) {
2305
2941
  cache.delete(cwd);
2306
2942
  return null;
@@ -2308,13 +2944,13 @@ async function buildMemoryIndex({ cwd, scope }) {
2308
2944
  let cached = cache.get(cwd);
2309
2945
  if (!cached || cached.builtAtMs < maxMtimeMs) {
2310
2946
  cached = {
2311
- entries: await collectEntries(memoryDirAbs, cwd),
2947
+ entries: await collectUserEntries(usersDirAbs, cwd),
2312
2948
  builtAtMs: Date.now()
2313
2949
  };
2314
2950
  cache.set(cwd, cached);
2315
2951
  }
2316
- const visible = cached.entries.filter((entry) => scope.kind === "user" ? entry.type === "users" && entry.subject?.startsWith(scope.userId) === true : entry.type !== "users");
2317
- return visible.length === 0 ? "" : renderIndex(visible);
2952
+ const visible = cached.entries.filter((entry) => entry.subject.startsWith(userId));
2953
+ return visible.length === 0 ? "" : renderUserIndex(visible);
2318
2954
  }
2319
2955
  async function maxMtimeAcrossDir(dir) {
2320
2956
  let dirStat;
@@ -2362,45 +2998,22 @@ async function listSubdirs(dir) {
2362
2998
  }
2363
2999
  return entries.filter((e) => e.isDirectory()).map((e) => join(dir, e.name));
2364
3000
  }
2365
- async function collectEntries(rootDirAbs, cwd) {
2366
- const collected = [];
2367
- await Promise.all(TYPE_DIRS.map(async (type) => {
2368
- const typeDirAbs = join(rootDirAbs, type);
2369
- if (TYPES_WITH_SUBJECT.has(type)) {
2370
- const subjectDirs = await listSubdirs(typeDirAbs);
2371
- await Promise.all(subjectDirs.map(async (subjectDirAbs) => {
2372
- const subject = basename(subjectDirAbs);
2373
- const files = await listMdFilesShallow(subjectDirAbs);
2374
- const parsed = await Promise.all(files.map(async (file) => {
2375
- const fm = await readFrontmatterOnly(file);
2376
- if (!fm?.name || !fm?.description) return null;
2377
- return {
2378
- name: fm.name,
2379
- description: fm.description,
2380
- type,
2381
- subject,
2382
- relPath: relative(cwd, file)
2383
- };
2384
- }));
2385
- for (const e of parsed) if (e) collected.push(e);
2386
- }));
2387
- } else {
2388
- const files = await listMdFilesShallow(typeDirAbs);
2389
- const parsed = await Promise.all(files.map(async (file) => {
2390
- const fm = await readFrontmatterOnly(file);
2391
- if (!fm?.name || !fm?.description) return null;
2392
- return {
2393
- name: fm.name,
2394
- description: fm.description,
2395
- type,
2396
- subject: null,
2397
- relPath: relative(cwd, file)
2398
- };
2399
- }));
2400
- for (const e of parsed) if (e) collected.push(e);
2401
- }
2402
- }));
2403
- return collected;
3001
+ async function collectUserEntries(usersDirAbs, cwd) {
3002
+ const subjectDirs = await listSubdirs(usersDirAbs);
3003
+ return (await Promise.all(subjectDirs.map(async (subjectDirAbs) => {
3004
+ const subject = basename(subjectDirAbs);
3005
+ const files = await listMdFilesShallow(subjectDirAbs);
3006
+ return (await Promise.all(files.map(async (file) => {
3007
+ const fm = await readFrontmatterOnly(file);
3008
+ if (!fm?.name || !fm?.description) return null;
3009
+ return {
3010
+ name: fm.name,
3011
+ description: fm.description,
3012
+ subject,
3013
+ relPath: relative(cwd, file)
3014
+ };
3015
+ }))).filter((e) => e !== null);
3016
+ }))).flat();
2404
3017
  }
2405
3018
  async function readFrontmatterOnly(filePath) {
2406
3019
  let fh;
@@ -2431,68 +3044,180 @@ function parseFrontmatter(text) {
2431
3044
  const result = FrontmatterSchema.safeParse(parsed);
2432
3045
  return result.success ? result.data : null;
2433
3046
  }
2434
- function renderIndex(entries) {
2435
- const byType = {
2436
- users: [],
2437
- projects: [],
2438
- feedback: [],
2439
- reference: []
2440
- };
2441
- for (const e of entries) byType[e.type].push(e);
3047
+ function renderUserIndex(entries) {
3048
+ const bySubject = /* @__PURE__ */ new Map();
3049
+ for (const e of entries) {
3050
+ const list = bySubject.get(e.subject) ?? [];
3051
+ list.push(e);
3052
+ bySubject.set(e.subject, list);
3053
+ }
3054
+ const lines = ["### Users"];
3055
+ for (const subject of [...bySubject.keys()].sort()) {
3056
+ lines.push(`- **${subject}**`);
3057
+ for (const e of bySubject.get(subject) ?? []) lines.push(` - \`${e.relPath}\` — ${e.description}`);
3058
+ }
3059
+ return lines.join("\n");
3060
+ }
3061
+ /**
3062
+ * One-time migration for agents created before `MEMORY.md` existed. If there is
3063
+ * no hand-maintained `.memory/MEMORY.md` yet but the agent has shared memory
3064
+ * files from the old frontmatter-indexed layout (`projects/`, `feedback/`,
3065
+ * `reference/`), derive a `MEMORY.md` from their frontmatter and write it once.
3066
+ * After that the agent owns the file — this never runs again for that agent and
3067
+ * never clobbers an existing index.
3068
+ *
3069
+ * Returns the seeded contents (also written to disk), or `null` when nothing
3070
+ * was seeded (index already present, or no legacy shared files). A write
3071
+ * failure propagates so the caller can log it; the read path then falls back to
3072
+ * whatever is on disk.
3073
+ */
3074
+ async function seedMemoryIndexFile({ cwd }) {
3075
+ const memoryDirAbs = join(cwd, MEMORY_DIRNAME);
3076
+ const indexPath = join(memoryDirAbs, MEMORY_INDEX_FILENAME);
3077
+ if (await stat(indexPath).catch(() => null)) return null;
3078
+ const perType = await Promise.all(LEGACY_SHARED_TYPES.map(async ({ dir, label }) => {
3079
+ const typeDirAbs = join(memoryDirAbs, dir);
3080
+ const files = [];
3081
+ await walkMdFiles(typeDirAbs, files);
3082
+ return {
3083
+ label,
3084
+ entries: (await Promise.all(files.map(async (file) => {
3085
+ const fm = await readFrontmatterOnly(file);
3086
+ if (!fm?.name || !fm?.description) return null;
3087
+ return {
3088
+ name: fm.name,
3089
+ description: fm.description,
3090
+ relPath: relative(cwd, file)
3091
+ };
3092
+ }))).filter((e) => !!e)
3093
+ };
3094
+ }));
3095
+ if (perType.every((group) => group.entries.length === 0)) return null;
3096
+ const content = renderSeededIndex(perType);
3097
+ await writeFile(indexPath, `${content}\n`, "utf-8");
3098
+ return content;
3099
+ }
3100
+ function renderSeededIndex(groups) {
2442
3101
  const sections = [];
2443
- for (const type of TYPE_DIRS) {
2444
- const items = byType[type];
2445
- if (items.length === 0) continue;
2446
- sections.push(`### ${TYPE_LABELS[type]}`);
2447
- if (TYPES_WITH_SUBJECT.has(type)) {
2448
- const bySubject = /* @__PURE__ */ new Map();
2449
- for (const e of items) {
2450
- const subject = e.subject ?? "(unknown)";
2451
- const list = bySubject.get(subject) ?? [];
2452
- list.push(e);
2453
- bySubject.set(subject, list);
2454
- }
2455
- const subjects = [...bySubject.keys()].sort();
2456
- for (const subject of subjects) {
2457
- sections.push(`- **${subject}**`);
2458
- for (const e of bySubject.get(subject) ?? []) sections.push(` - \`${e.relPath}\` — ${e.description}`);
2459
- }
2460
- } else for (const e of items) sections.push(`- \`${e.relPath}\` — ${e.description}`);
3102
+ for (const { label, entries } of groups) {
3103
+ if (entries.length === 0) continue;
3104
+ sections.push(`### ${label}`);
3105
+ const sorted = [...entries].sort((a, b) => a.relPath.localeCompare(b.relPath));
3106
+ for (const e of sorted) sections.push(`- [${e.name}](${e.relPath}) — ${e.description}`);
2461
3107
  sections.push("");
2462
3108
  }
2463
3109
  return sections.join("\n").trimEnd();
2464
3110
  }
3111
+ /**
3112
+ * The version at which the hand-maintained `MEMORY.md` layout was introduced.
3113
+ * Used to classify an unversioned `.memory/`: if it already has a `MEMORY.md`
3114
+ * it's on this layout (not the pre-MEMORY.md v1 frontmatter layout), so it
3115
+ * shouldn't be treated as v1 and re-seeded.
3116
+ */
3117
+ const HAND_MAINTAINED_INDEX_VERSION = 2;
3118
+ const VERSION_FILENAME = ".version";
3119
+ const MEMORY_MIGRATIONS = [{
3120
+ from: 1,
3121
+ to: 2,
3122
+ apply: async ({ cwd }) => {
3123
+ await seedMemoryIndexFile({ cwd });
3124
+ }
3125
+ }];
3126
+ /**
3127
+ * The layout version of an agent's `.memory/`:
3128
+ * - `null` when there's no `.memory/` at all (a fresh agent is current by
3129
+ * construction; nothing to migrate).
3130
+ * - `1` when `.memory/` exists but carries no `.version` marker — i.e. it
3131
+ * predates versioning.
3132
+ * - otherwise the integer in `.memory/.version`.
3133
+ */
3134
+ async function readMemoryVersion(cwd) {
3135
+ const memoryDirAbs = join(cwd, MEMORY_DIRNAME);
3136
+ if (!(await stat(memoryDirAbs).catch(() => null))?.isDirectory()) return null;
3137
+ const raw = await readFile(join(memoryDirAbs, VERSION_FILENAME), "utf-8").catch(() => null);
3138
+ if (raw === null) return await stat(join(memoryDirAbs, MEMORY_INDEX_FILENAME)).then((s) => s.isFile()).catch(() => false) ? HAND_MAINTAINED_INDEX_VERSION : 1;
3139
+ const parsed = Number.parseInt(raw.trim(), 10);
3140
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : 1;
3141
+ }
3142
+ async function writeMemoryVersion(cwd, version) {
3143
+ await writeFile(join(cwd, MEMORY_DIRNAME, VERSION_FILENAME), `${version}\n`, "utf-8");
3144
+ }
3145
+ /**
3146
+ * Bring an agent's `.memory/` up to `CURRENT_MEMORY_VERSION` by applying the
3147
+ * ordered migrations. Runs on session start. No-op when there's no `.memory/`
3148
+ * yet or it's already current. Migrations must be idempotent, so a lost/unwritten
3149
+ * version marker (the file isn't committed by the harness) only costs a repeated
3150
+ * no-op, never corruption. Returns the `{ from, to }` actually applied, or
3151
+ * `null` when nothing ran.
3152
+ */
3153
+ async function migrateMemory({ cwd }) {
3154
+ const from = await readMemoryVersion(cwd);
3155
+ if (from === null || from >= 2) return null;
3156
+ let version = from;
3157
+ while (version < 2) {
3158
+ const migration = MEMORY_MIGRATIONS.find((m) => m.from === version);
3159
+ if (!migration) break;
3160
+ await migration.apply({ cwd });
3161
+ version = migration.to;
3162
+ }
3163
+ await writeMemoryVersion(cwd, version);
3164
+ return {
3165
+ from,
3166
+ to: version
3167
+ };
3168
+ }
2465
3169
  //#endregion
2466
3170
  //#region src/extensions/memory.ts
2467
3171
  const log$6 = logger.child({ module: "memory-extension" });
2468
3172
  /**
2469
3173
  * The standing instructions for the memory system. Always injected (even with
2470
- * an empty `.memory/`) so the agent knows it can persist notes. `users/` is
3174
+ * no `MEMORY.md`) so the agent knows it can persist notes and how. `users/` is
2471
3175
  * described by the platform memory extension, which is the only thing that can
2472
3176
  * scope it to a person — here we just point at it.
2473
3177
  */
2474
3178
  function memoryInstructions(cwd) {
2475
3179
  return `## Memory across conversations
2476
3180
 
2477
- 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.
3181
+ 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.
2478
3182
 
2479
3183
  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.
2480
3184
 
2481
- 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.`;
3185
+ 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\`.)
3186
+
3187
+ 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:
3188
+ - 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.
3189
+ - 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.
3190
+ - 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.
3191
+ - 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.
3192
+
3193
+ Commit and push after editing \`.memory/\` to persist it.`;
2482
3194
  }
2483
3195
  function composeBlock$1({ cwd, index }) {
2484
3196
  const instructions = memoryInstructions(cwd);
2485
3197
  if (!index || index.length === 0) return instructions;
2486
- return `${instructions}\n\n## Memory index\n\n${index}`;
3198
+ const header = `## Memory index (${indexSizeNote(index)})`;
3199
+ const warning = indexBudgetWarning(index);
3200
+ const rendered = enforceMemoryIndexHardBudget(index);
3201
+ return `${instructions}\n\n${warning ? `${header}\n\n${warning}\n\n${rendered}` : `${header}\n\n${rendered}`}`;
2487
3202
  }
2488
3203
  const memoryExtension = (pi) => {
2489
3204
  let cachedBlock = null;
2490
3205
  pi.on("session_start", async (_event, ctx) => {
2491
3206
  try {
2492
- const index = await buildMemoryIndex({
2493
- cwd: ctx.cwd,
2494
- scope: { kind: "shared" }
2495
- });
3207
+ const migrated = await migrateMemory({ cwd: ctx.cwd });
3208
+ if (migrated !== null) log$6.info({
3209
+ event: "memory_migrated",
3210
+ from: migrated.from,
3211
+ to: migrated.to
3212
+ }, "migrated memory layout to current version");
3213
+ } catch (err) {
3214
+ log$6.warn({
3215
+ err,
3216
+ event: "memory_migration_failed"
3217
+ }, "memory migration failed; continuing with existing index");
3218
+ }
3219
+ try {
3220
+ const index = await readMemoryIndexFile({ cwd: ctx.cwd });
2496
3221
  cachedBlock = composeBlock$1({
2497
3222
  cwd: ctx.cwd,
2498
3223
  index
@@ -2501,7 +3226,7 @@ const memoryExtension = (pi) => {
2501
3226
  log$6.warn({
2502
3227
  err,
2503
3228
  event: "memory_index_failed"
2504
- }, "memory index build failed; injecting instructions only");
3229
+ }, "memory index read failed; injecting instructions only");
2505
3230
  cachedBlock = memoryInstructions(ctx.cwd);
2506
3231
  }
2507
3232
  });
@@ -2560,9 +3285,12 @@ function slugifyName(name) {
2560
3285
  function composeBlock({ index, user }) {
2561
3286
  const instructions = `## Current user memory
2562
3287
 
2563
- 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.`;
3288
+ 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.`;
2564
3289
  if (!index || index.length === 0) return instructions;
2565
- return `${instructions}\n\n${index}`;
3290
+ const warning = indexBudgetWarning(index);
3291
+ const header = `Memory index (${indexSizeNote(index)}):`;
3292
+ const rendered = enforceMemoryIndexHardBudget(index);
3293
+ return `${instructions}\n\n${warning ? `${warning}\n\n${header}\n\n${rendered}` : `${header}\n\n${rendered}`}`;
2566
3294
  }
2567
3295
  /**
2568
3296
  * Build the platform memory extension. `channelContext` is the per-turn ref
@@ -2583,10 +3311,7 @@ function createPlatformMemoryExtension({ channelContext }) {
2583
3311
  cachedBlock = composeBlock({
2584
3312
  index: await buildMemoryIndex({
2585
3313
  cwd: ctx.cwd,
2586
- scope: {
2587
- kind: "user",
2588
- userId: user.id
2589
- }
3314
+ userId: user.id
2590
3315
  }),
2591
3316
  user
2592
3317
  });
@@ -2782,7 +3507,6 @@ const soulExtension = (pi) => {
2782
3507
  //#endregion
2783
3508
  //#region src/extensions/subagent/index.ts
2784
3509
  const log$2 = logger.child({ module: "subagent-ext" });
2785
- const COLD_START_FLAG_WAIT_MS = 750;
2786
3510
  const MAX_TASKS = 8;
2787
3511
  const TaskItem = Type.Object({
2788
3512
  task: Type.String({ description: "The task to delegate to a subagent run." }),
@@ -2791,8 +3515,14 @@ const TaskItem = Type.Object({
2791
3515
  maxLength: 120
2792
3516
  }),
2793
3517
  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." })),
2794
- model: Type.Optional(Type.String({ description: "Optional model id to run this subagent on (e.g. \"anthropic/claude-opus-4-8\"). Must be a real catalogued model. Omit to run on your own model. If you are locked to a Google-compliant model, only compliant models are accepted." }))
3518
+ 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." })),
3519
+ timeoutMinutes: Type.Optional(Type.Integer({
3520
+ 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.",
3521
+ minimum: 1,
3522
+ maximum: 360
3523
+ }))
2795
3524
  });
3525
+ const DEFAULT_SUBAGENT_TIMEOUT_MS = 30 * 6e4;
2796
3526
  const SubagentParams = Type.Object({ tasks: Type.Array(TaskItem, {
2797
3527
  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.",
2798
3528
  minItems: 1,
@@ -2807,7 +3537,9 @@ function buildTool(messageId) {
2807
3537
  "Delegate one or more tasks to subagent runs — fresh isolated copies of yourself, each with its own context window, linked to this conversation.",
2808
3538
  "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.",
2809
3539
  "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.",
2810
- "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; omit it to run on your own model."
3540
+ "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.",
3541
+ "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.",
3542
+ "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."
2811
3543
  ].join(" "),
2812
3544
  promptSnippet: "subagent — delegate tasks to isolated subagent runs; each rewakes you with its result when done",
2813
3545
  parameters: SubagentParams,
@@ -2825,24 +3557,35 @@ function buildTool(messageId) {
2825
3557
  task: t.task,
2826
3558
  title: t.title ?? null,
2827
3559
  persona: t.persona ?? null,
2828
- model: t.model ?? null
3560
+ model: t.model ?? null,
3561
+ timeoutMs: t.timeoutMinutes != null ? t.timeoutMinutes * 6e4 : DEFAULT_SUBAGENT_TIMEOUT_MS
2829
3562
  }));
2830
3563
  try {
2831
- const { taskIds } = await postSubagentSpawn({
3564
+ const spawned = await postSubagentSpawn({
2832
3565
  messageId,
2833
3566
  tasks: spawnTasks
2834
3567
  });
3568
+ const { taskIds } = spawned;
2835
3569
  log$2.info({
2836
3570
  event: "subagent_spawned",
2837
3571
  count: taskIds.length
2838
3572
  }, "subagent tasks queued");
2839
- const lines = taskIds.map((id, i) => `- ${id}: ${spawnTasks[i]?.title ?? spawnTasks[i]?.task ?? ""}`).join("\n");
3573
+ const convByTask = new Map(spawned.tasks.map((t) => [t.taskId, t.conversationId]));
3574
+ const lines = taskIds.map((id, i) => {
3575
+ const label = spawnTasks[i]?.title ?? spawnTasks[i]?.task ?? "";
3576
+ const conv = convByTask.get(id);
3577
+ return `- ${id}: ${label}${conv ? ` — conversation ${conv}` : ""}`;
3578
+ }).join("\n");
3579
+ 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." : "";
2840
3580
  return {
2841
3581
  content: [{
2842
3582
  type: "text",
2843
- 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}`
3583
+ 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}`
2844
3584
  }],
2845
- details: { taskIds }
3585
+ details: {
3586
+ taskIds,
3587
+ tasks: spawned.tasks
3588
+ }
2846
3589
  };
2847
3590
  } catch (err) {
2848
3591
  const message = err instanceof Error ? err.message : String(err);
@@ -2863,16 +3606,15 @@ function buildTool(messageId) {
2863
3606
  };
2864
3607
  }
2865
3608
  /**
2866
- * Gated on `harness-subagent-enabled`, read from the shared feature-flag poll.
2867
3609
  * The factory takes the session's channel context to resolve the originating
2868
3610
  * messageId — the api links each spawned run to the conversation that message
2869
3611
  * belongs to and rewakes it on completion (nothing about the parent is piped
2870
- * from the sandbox beyond that id).
3612
+ * from the sandbox beyond that id). The tool is registered unconditionally at
3613
+ * session_start.
2871
3614
  */
2872
3615
  function createSubagentExtension({ channelContext }) {
2873
3616
  return (pi) => {
2874
3617
  const messageId = extractMessageId(channelContext);
2875
- startFeatureFlagPoller();
2876
3618
  let registered = false;
2877
3619
  const registerOnce = () => {
2878
3620
  if (registered) return;
@@ -2880,12 +3622,8 @@ function createSubagentExtension({ channelContext }) {
2880
3622
  pi.registerTool(buildTool(messageId));
2881
3623
  log$2.info({ event: "subagent_enabled" }, "subagent tool registered");
2882
3624
  };
2883
- onFlagChange("subagent", (enabled) => {
2884
- if (enabled) registerOnce();
2885
- });
2886
- pi.on("session_start", async () => {
2887
- if (getPolledFlag("subagent") === null) await Promise.race([awaitFirstFlagPoll(), new Promise((resolve) => setTimeout(resolve, COLD_START_FLAG_WAIT_MS).unref?.())]);
2888
- if (getPolledFlag("subagent") === true) registerOnce();
3625
+ pi.on("session_start", () => {
3626
+ registerOnce();
2889
3627
  });
2890
3628
  };
2891
3629
  }
@@ -3163,21 +3901,43 @@ const toolCallSummaryExtension = (pi) => {
3163
3901
  * from the origin messageId in its channel context (`resolveConversationFromApi`)
3164
3902
  * — and every run of the same conversation resolves to the same id, keeping the
3165
3903
  * shared map correctly scoped across turns. `bg_*`, the completion wake, and the
3166
- * next-session injection all filter to the resolved conversationan agent
3167
- * never sees or is woken by a task from a different chat. Only the output log
3904
+ * next-session injection all filter by a **scope key**the resolved
3905
+ * conversation id, or, when a session's conversation is unresolvable (a bare
3906
+ * CLI session, or a run whose channel-context ref carries no messageId), a
3907
+ * sentinel unique to that one session instance. Comparing on the raw
3908
+ * `conversationId` would bucket every unresolvable session together under
3909
+ * `null` and leak one's completion wake / status / next-session injection into
3910
+ * another; the sentinel keeps each isolated so an agent never sees or is woken
3911
+ * by a task from a different chat. Only the output log
3168
3912
  * spills to disk (/home/user/.anyone/bg-tasks/<id>.log) to avoid buffering a chatty job
3169
3913
  * in memory; exit code and run state live on the in-memory task.
3170
3914
  *
3171
- * **No cross-restart survival (v1, deliberate).** Task state lives only in
3172
- * the running harness process. A harness restart (crash supervisord
3173
- * respawn, or `platform harness reload` after the agent edits its own
3174
- * harness) drops the map and pi's exec children are reaped with it. We don't
3175
- * resurrect from disk because the common next-run case cold-provisions a
3176
- * *different* sandbox anyway (warm reuse is the minority in prod), so on-disk
3177
- * state would rarely be the box the next run lands on. The idle-completion wake
3178
- * does cross the sandbox → platform boundary (a fresh run via `bg-task-done`),
3179
- * but a task whose harness dies before it finishes is gone — it is not
3180
- * resurrected, and this stays distinct from the scheduled-run (cron) system.
3915
+ * **Cross-restart survival is OPT-IN, via `bg_run({ resumable: true })`.**
3916
+ * A non-resumable task's state lives only in the running harness process: a
3917
+ * harness restart (crash → supervisord respawn, `platform harness reload`, or
3918
+ * a sandbox recycle on idle timeout / redeploy / template rebuild) drops the
3919
+ * map and pi's exec children are reaped with it, and the task is gone the
3920
+ * right behavior for a one-shot side-effecting command, which must never
3921
+ * silently re-run.
3922
+ *
3923
+ * A RESUMABLE task additionally checkpoints its *spec* (command, cwd, labels,
3924
+ * origin messageId not its live output/exit state) to a durable, api-side
3925
+ * journal keyed by conversation (`/sandbox/bg-task-journal`, redis). On the
3926
+ * NEXT run's `session_start` — which usually lands on a *different*,
3927
+ * cold-provisioned sandbox, which is exactly why the journal is api-side and
3928
+ * not on the sandbox disk — the harness lists the journal and relaunches any
3929
+ * spec it isn't already running, keeping the original id and prepending a
3930
+ * `<resumed-after-restart>` banner so the agent knows it re-ran from scratch,
3931
+ * not continued. The spec is dropped from the journal when the task
3932
+ * finishes/kills. Because relaunch RE-EXECUTES the command, resumable is only
3933
+ * for idempotent, long-lived work (pollers, watchers, retry loops); the tool
3934
+ * description enforces this and the default is false.
3935
+ *
3936
+ * The idle-completion wake (a task finishing while the agent is idle) crosses
3937
+ * the sandbox → platform boundary via a fresh run (`bg-task-done`) for both
3938
+ * resumable and non-resumable tasks; the journal is a separate, additive layer
3939
+ * that only handles a task whose harness dies BEFORE it finishes. This stays
3940
+ * distinct from the scheduled-run (cron) system.
3181
3941
  */
3182
3942
  const log = logger.child({ module: "background-tasks-ext" });
3183
3943
  const ops = createLocalBashOperations();
@@ -3188,6 +3948,7 @@ const WATCHDOG_INTERVAL_MS = 3e4;
3188
3948
  const KEEPALIVE_EVERY_MS = 6e4;
3189
3949
  const KEEPALIVE_MAX_MS = 3600 * 1e3;
3190
3950
  const STALL_HINT_AFTER_MS = 120 * 1e3;
3951
+ const PUBLISH_DEBOUNCE_MS = 300;
3191
3952
  const MAX_LOG_BYTES = 100 * 1024 * 1024;
3192
3953
  const DEFAULT_TAIL_LINES = 30;
3193
3954
  const TAIL_READ_BYTES = 64 * 1024;
@@ -3195,11 +3956,12 @@ function taskLabel(meta) {
3195
3956
  return `${meta.id} "${meta.description ?? meta.command.slice(0, 60)}"`;
3196
3957
  }
3197
3958
  let taskCounter = 0;
3959
+ let sessionScopeCounter = 0;
3198
3960
  const tasks = /* @__PURE__ */ new Map();
3199
3961
  let watchdogInterval = null;
3200
3962
  let lastKeepaliveAt = 0;
3201
- function sameConversation(meta, conversationId) {
3202
- return meta.conversationId === conversationId;
3963
+ function sameScope(meta, scopeKey) {
3964
+ return meta.scopeKey === scopeKey;
3203
3965
  }
3204
3966
  function logPath(id) {
3205
3967
  return join(tasksDir(), `${id}.log`);
@@ -3312,6 +4074,10 @@ function createBackgroundTasksExtension({ channelContext }) {
3312
4074
  return id;
3313
4075
  });
3314
4076
  }
4077
+ const unresolvedScopeSentinel = `unresolved:${process.pid.toString(36)}:${(sessionScopeCounter += 1).toString(36)}`;
4078
+ function scopeKey() {
4079
+ return conversationId ?? unresolvedScopeSentinel;
4080
+ }
3315
4081
  let agentActive = false;
3316
4082
  pi.on("agent_start", async () => {
3317
4083
  agentActive = true;
@@ -3339,13 +4105,16 @@ ${recentOutput}
3339
4105
  </background-task-finished>
3340
4106
  Run bg_logs for the full output.
3341
4107
 
3342
- 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.`,
4108
+ This is a background-task completion, not a message from the user.
4109
+ For a routine or expected completion, output nothing.
4110
+ Only reply if the outcome changes what the user should know or do,
4111
+ or if you were explicitly waiting to report it.`,
3343
4112
  display: false
3344
4113
  };
3345
4114
  }
3346
4115
  async function notifyCompletion(meta) {
3347
4116
  if (meta.notified) return;
3348
- if (agentActive && sameConversation(meta, conversationId)) {
4117
+ if (agentActive && sameScope(meta, scopeKey())) {
3349
4118
  meta.notified = true;
3350
4119
  pi.sendMessage(await taskDoneMessage(meta), {
3351
4120
  triggerTurn: true,
@@ -3376,9 +4145,83 @@ This is a background-task completion, not a message from the user. If it needs n
3376
4145
  });
3377
4146
  } else log.info({ taskId: meta.id }, "bg task completed idle with no origin message; deferring to next session_start");
3378
4147
  }
3379
- async function launchTask({ command, description, cwd }) {
4148
+ const SNAPSHOT_TAIL_LINES = 40;
4149
+ let lastPublishedSignature = null;
4150
+ let publishInFlight = null;
4151
+ let publishQueued = false;
4152
+ function snapshotSignature(snapshot) {
4153
+ return JSON.stringify(snapshot.map((t) => ({
4154
+ id: t.id,
4155
+ state: t.state,
4156
+ exitCode: t.exitCode,
4157
+ killedReason: t.killedReason,
4158
+ outputTail: t.outputTail
4159
+ })));
4160
+ }
4161
+ async function doPublishSnapshot() {
4162
+ if (!messageId) return;
4163
+ const mine = [...tasks.values()].filter((t) => sameScope(t, scopeKey()));
4164
+ try {
4165
+ const snapshot = await Promise.all(mine.map(async (t) => ({
4166
+ id: t.id,
4167
+ command: t.command,
4168
+ description: t.description,
4169
+ startedAt: t.startedAt,
4170
+ state: t.running ? "running" : "finished",
4171
+ exitCode: t.exitCode,
4172
+ killedReason: t.killedReason,
4173
+ outputTail: await tailLog(t.id, SNAPSHOT_TAIL_LINES)
4174
+ })));
4175
+ const signature = snapshotSignature(snapshot);
4176
+ if (signature === lastPublishedSignature) return;
4177
+ lastPublishedSignature = signature;
4178
+ postBackgroundTasksSnapshot({
4179
+ messageId,
4180
+ tasks: snapshot
4181
+ });
4182
+ } catch (err) {
4183
+ log.debug({
4184
+ err,
4185
+ event: "bg_tasks_snapshot_build_failed"
4186
+ }, "building bg-tasks snapshot failed");
4187
+ }
4188
+ }
4189
+ async function publishSnapshotNow() {
4190
+ if (publishInFlight) {
4191
+ publishQueued = true;
4192
+ return;
4193
+ }
4194
+ publishInFlight = (async () => {
4195
+ try {
4196
+ do {
4197
+ publishQueued = false;
4198
+ await doPublishSnapshot();
4199
+ } while (publishQueued);
4200
+ } finally {
4201
+ publishInFlight = null;
4202
+ }
4203
+ })();
4204
+ await publishInFlight;
4205
+ }
4206
+ let publishTimer = null;
4207
+ function schedulePublishSnapshot() {
4208
+ if (publishTimer) return;
4209
+ publishTimer = setTimeout(() => {
4210
+ publishTimer = null;
4211
+ publishSnapshotNow();
4212
+ }, PUBLISH_DEBOUNCE_MS);
4213
+ publishTimer.unref?.();
4214
+ }
4215
+ async function flushPublishSnapshot() {
4216
+ if (publishTimer) {
4217
+ clearTimeout(publishTimer);
4218
+ publishTimer = null;
4219
+ }
4220
+ await publishSnapshotNow();
4221
+ }
4222
+ async function launchTask({ command, description, cwd, resumable = false, resumedFromJournal = false, id: providedId, startedAt: providedStartedAt }) {
3380
4223
  taskCounter += 1;
3381
- const id = `bg-${process.pid.toString(36)}-${taskCounter}`;
4224
+ const id = providedId ?? `bg-${process.pid.toString(36)}-${taskCounter}`;
3382
4225
  try {
3383
4226
  await mkdir(tasksDir(), { recursive: true });
3384
4227
  } catch (err) {
@@ -3394,7 +4237,7 @@ This is a background-task completion, not a message from the user. If it needs n
3394
4237
  taskId: id
3395
4238
  }, "bg task log write failed");
3396
4239
  });
3397
- const startedAt = Date.now();
4240
+ const startedAt = providedStartedAt ?? Date.now();
3398
4241
  const meta = {
3399
4242
  id,
3400
4243
  command: stripPlatformExportsForDisplay(command),
@@ -3402,6 +4245,7 @@ This is a background-task completion, not a message from the user. If it needs n
3402
4245
  logBytes: 0,
3403
4246
  lastOutputAt: startedAt,
3404
4247
  conversationId,
4248
+ scopeKey: scopeKey(),
3405
4249
  messageId,
3406
4250
  description,
3407
4251
  notified: false,
@@ -3409,9 +4253,23 @@ This is a background-task completion, not a message from the user. If it needs n
3409
4253
  controller: new AbortController(),
3410
4254
  running: true,
3411
4255
  exitCode: null,
3412
- error: null
4256
+ error: null,
4257
+ resumable,
4258
+ cwd,
4259
+ resumedFromJournal
3413
4260
  };
3414
4261
  tasks.set(id, meta);
4262
+ if (resumable && messageId) putBackgroundTaskJournalSpec({
4263
+ messageId,
4264
+ spec: {
4265
+ id,
4266
+ command,
4267
+ cwd,
4268
+ description,
4269
+ startedAt,
4270
+ messageId
4271
+ }
4272
+ });
3415
4273
  ops.exec(command, cwd, {
3416
4274
  onData: (chunk) => {
3417
4275
  meta.logBytes += chunk.length;
@@ -3437,6 +4295,11 @@ This is a background-task completion, not a message from the user. If it needs n
3437
4295
  taskId: id,
3438
4296
  exitCode: meta.exitCode
3439
4297
  }, "bg task finished");
4298
+ if (meta.resumable && meta.messageId) deleteBackgroundTaskJournalSpec({
4299
+ messageId: meta.messageId,
4300
+ taskId: id
4301
+ });
4302
+ schedulePublishSnapshot();
3440
4303
  await notifyCompletion(meta);
3441
4304
  });
3442
4305
  ensureWatchdog();
@@ -3444,19 +4307,55 @@ This is a background-task completion, not a message from the user. If it needs n
3444
4307
  taskId: id,
3445
4308
  conversationId
3446
4309
  }, "bg task started");
4310
+ schedulePublishSnapshot();
3447
4311
  return meta;
3448
4312
  }
3449
4313
  function knownTaskIds() {
3450
- return [...tasks.values()].filter((t) => sameConversation(t, conversationId)).map((t) => t.id).join(", ") || "(none)";
4314
+ return [...tasks.values()].filter((t) => sameScope(t, scopeKey())).map((t) => t.id).join(", ") || "(none)";
4315
+ }
4316
+ let journalChecked = false;
4317
+ async function rehydrateJournaledTasks() {
4318
+ if (!messageId || journalChecked) return;
4319
+ journalChecked = true;
4320
+ const specs = await listBackgroundTaskJournalSpecs({ messageId });
4321
+ if (specs.length === 0) return;
4322
+ for (const spec of specs) {
4323
+ const live = tasks.get(spec.id);
4324
+ if (live && sameScope(live, scopeKey())) continue;
4325
+ log.info({
4326
+ taskId: spec.id,
4327
+ conversationId
4328
+ }, "relaunching journaled resumable bg task after restart");
4329
+ const meta = await launchTask({
4330
+ command: spec.command,
4331
+ description: spec.description,
4332
+ cwd: spec.cwd,
4333
+ resumable: true,
4334
+ resumedFromJournal: true,
4335
+ id: spec.id,
4336
+ startedAt: spec.startedAt
4337
+ });
4338
+ try {
4339
+ const stream = createWriteStream(logPath(meta.id), { flags: "a" });
4340
+ 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`);
4341
+ stream.end();
4342
+ } catch (err) {
4343
+ log.debug({
4344
+ err,
4345
+ taskId: meta.id
4346
+ }, "resume banner write failed");
4347
+ }
4348
+ }
3451
4349
  }
3452
4350
  pi.on("session_start", async () => {
3453
- if (tasks.size === 0) return;
4351
+ if (tasks.size === 0 && (!messageId || journalChecked)) return;
3454
4352
  await ensureConversationId();
3455
- for (const [id, meta] of tasks) if (sameConversation(meta, conversationId) && !meta.running && meta.notified) {
4353
+ await rehydrateJournaledTasks();
4354
+ for (const [id, meta] of tasks) if (sameScope(meta, scopeKey()) && !meta.running && meta.notified) {
3456
4355
  tasks.delete(id);
3457
4356
  await unlink(logPath(id)).catch(() => {});
3458
4357
  }
3459
- const unnotified = [...tasks.values()].filter((t) => sameConversation(t, conversationId) && !t.notified && !t.running);
4358
+ const unnotified = [...tasks.values()].filter((t) => sameScope(t, scopeKey()) && !t.notified && !t.running);
3460
4359
  for (const meta of unnotified) {
3461
4360
  meta.notified = true;
3462
4361
  pi.sendMessage(await taskDoneMessage(meta));
@@ -3465,7 +4364,9 @@ This is a background-task completion, not a message from the user. If it needs n
3465
4364
  conversationId,
3466
4365
  count: unnotified.length
3467
4366
  }, "injected completed bg tasks at session_start");
3468
- if ([...tasks.values()].some((t) => sameConversation(t, conversationId) && t.running)) ensureWatchdog();
4367
+ if ([...tasks.values()].some((t) => sameScope(t, scopeKey()) && t.running)) ensureWatchdog();
4368
+ lastPublishedSignature = null;
4369
+ await flushPublishSnapshot();
3469
4370
  });
3470
4371
  function err(text) {
3471
4372
  return {
@@ -3482,11 +4383,11 @@ This is a background-task completion, not a message from the user. If it needs n
3482
4383
  }
3483
4384
  function resolveTask(taskId) {
3484
4385
  const exact = tasks.get(taskId);
3485
- if (exact && sameConversation(exact, conversationId)) return {
4386
+ if (exact && sameScope(exact, scopeKey())) return {
3486
4387
  error: null,
3487
4388
  meta: exact
3488
4389
  };
3489
- const matches = [...tasks.values()].filter((t) => sameConversation(t, conversationId) && t.id.startsWith(taskId));
4390
+ const matches = [...tasks.values()].filter((t) => sameScope(t, scopeKey()) && t.id.startsWith(taskId));
3490
4391
  if (matches.length === 1) return {
3491
4392
  error: null,
3492
4393
  meta: matches[0]
@@ -3495,7 +4396,7 @@ This is a background-task completion, not a message from the user. If it needs n
3495
4396
  return err(`Unknown task ${taskId}. Known tasks: ${knownTaskIds()}`);
3496
4397
  }
3497
4398
  function listTasks() {
3498
- const mine = [...tasks.values()].filter((t) => sameConversation(t, conversationId));
4399
+ const mine = [...tasks.values()].filter((t) => sameScope(t, scopeKey()));
3499
4400
  if (mine.length === 0) return "No background tasks.";
3500
4401
  return mine.map((t) => {
3501
4402
  const state = t.running ? "running" : t.exitCode !== null ? `exited ${t.exitCode}${t.killedReason ? ` (killed: ${t.killedReason})` : ""}` : t.killedReason ? `killed: ${t.killedReason}` : "ended";
@@ -3508,24 +4409,27 @@ This is a background-task completion, not a message from the user. If it needs n
3508
4409
  const bgRun = {
3509
4410
  name: "bg_run",
3510
4411
  label: "Run in background",
3511
- 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.",
3512
- promptSnippet: "bg_run — run a long command without blocking; you are notified on completion",
4412
+ 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.",
4413
+ 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)",
3513
4414
  parameters: Type.Object({
3514
4415
  command: Type.String({ description: "Bash command to execute" }),
3515
- description: Type.Optional(Type.String({ description: "Clear, concise description of what this command does in active voice (2-6 words)." }))
4416
+ description: Type.Optional(Type.String({ description: "Clear, concise description of what this command does in active voice (2-6 words)." })),
4417
+ 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." }))
3516
4418
  }),
3517
4419
  async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
3518
4420
  await ensureConversationId();
3519
- const { command, description = null } = params;
4421
+ const { command, description = null, resumable = false } = params;
3520
4422
  const meta = await launchTask({
3521
4423
  command,
3522
4424
  description,
3523
- cwd: ctx.cwd
4425
+ cwd: ctx.cwd,
4426
+ resumable
3524
4427
  });
4428
+ 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." : "";
3525
4429
  return {
3526
4430
  content: [{
3527
4431
  type: "text",
3528
- 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}"}.`
4432
+ 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}`
3529
4433
  }],
3530
4434
  details: {}
3531
4435
  };
@@ -3645,6 +4549,7 @@ const all = [
3645
4549
  localToolsExtension,
3646
4550
  toolCallEnvExtension,
3647
4551
  bashDefaultTimeoutExtension,
4552
+ diskGuardExtension,
3648
4553
  toolCallSummaryExtension
3649
4554
  ];
3650
4555
  /**
@@ -3663,7 +4568,8 @@ function platformExtensions({ sessionId, channelContext }) {
3663
4568
  selfTraceExtension,
3664
4569
  createBackgroundTasksExtension({ channelContext }),
3665
4570
  createSubagentExtension({ channelContext }),
3666
- createContextManagementExtension()
4571
+ createContextManagementExtension(),
4572
+ resourcePressureWarningExtension
3667
4573
  ];
3668
4574
  }
3669
4575
  //#endregion