Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix(capture): context ties short connections to their call, sees the turn's last messages, and names only the session's processes

ogsamrat3d77fa583942
diff --git a/apps/server/src/capture-context.test.ts b/apps/server/src/capture-context.test.ts
index b62fa4a0..65e55c11 100644
--- a/apps/server/src/capture-context.test.ts
+++ b/apps/server/src/capture-context.test.ts
@@ -215,8 +215,12 @@ test("#7 services: docker containers with image digests, local databases by port
const calls = [];
const run = fakeRun({
docker: (args) => {
- if (args[0] === "ps") return ok(`${JSON.stringify({ ID: "abc123", Names: "demo-db-1", Image: "postgres:16", Ports: "127.0.0.1:5432->5432/tcp", Status: "Up 2 hours", Labels: "com.docker.compose.project.config_files=/srv/demo/compose.yaml" })}\n`);
- if (args[0] === "inspect") return ok("abc123 sha256:img1\n");
+ if (args[0] === "ps") return ok([
+ JSON.stringify({ ID: "abc123", Names: "demo-db-1", Image: "postgres:16", Ports: "127.0.0.1:5432->5432/tcp", Status: "Up 2 hours", Labels: "com.docker.compose.project.config_files=/srv/demo/compose.yaml" }),
+ JSON.stringify({ ID: "def456", Names: "telegram-bot", Image: "secret/bot:1", Ports: "", Status: "Up 9 days", Labels: "com.docker.compose.project.config_files=/home/someone/private/bot/compose.yml" }),
+ ].join("\n") + "\n");
+ // demo-db-1 bind-mounts the project; telegram-bot is unrelated and older than the session.
+ if (args[0] === "inspect") return ok(`abc123 sha256:img1 2020-01-01T00:00:00.123456789Z ${JSON.stringify([{ Type: "bind", Source: `${root}/data` }])}\ndef456 sha256:img2 2020-01-01T00:00:00Z ${JSON.stringify([{ Type: "bind", Source: "/home/someone/private/bot" }])}\n`);
if (args[0] === "image") return ok('sha256:img1 ["postgres@sha256:deadbeef"]\n');
return null;
},
@@ -238,6 +242,8 @@ test("#7 services: docker containers with image digests, local databases by port
resolve: (command) => (["docker", "psql", "redis-server"].includes(command) ? `/usr/local/bin/${command}` : null),
});
assert.equal(result.docker.reachable, true);
+ assert.equal(result.docker.other_containers, 1, "the unrelated container is counted, not named");
+ assert.ok(!JSON.stringify(result).includes("telegram") && !JSON.stringify(result).includes("private/bot"));
assert.deepEqual(result.docker.containers, [{ name: "demo-db-1", image: "postgres:16", image_digest: "postgres@sha256:deadbeef", ports: "127.0.0.1:5432->5432/tcp".replace("127.0.0.1", uploader.redactUploadText("127.0.0.1").text), status: "Up 2 hours" }]);
assert.deepEqual(result.databases, [
{ kind: "postgres", port: 5432, address: "loopback", client_version: "psql (PostgreSQL) 16.3", process: "docker-proxy" },
@@ -747,3 +753,199 @@ test("a folder the agent created stays allowed, even under ~/.config, minus cred
test("Windows: a timed-out command's whole process tree is ended (taskkill /T /F)", () => {
assert.deepEqual(exec.windowsTreeKillArgs(4242), ["/PID", "4242", "/T", "/F"]);
});
+
+// --- release e2e fixes: process privacy, docker, network per call, final messages ---------
+
+test("#19 privacy: only the session's process tree and processes in its folders are named; other ports are port + base name", async () => {
+ const project = tmp("proc-proj");
+ const table = [
+ { pid: 100, ppid: 1, name: "node", command: "node /usr/lib/omnirush/bin.js" },
+ { pid: 101, ppid: 100, name: "opencode", command: "opencode serve --port 4096" },
+ { pid: 102, ppid: 101, name: "bash", command: `bash -c npm test --token=${GH_TOKEN}` },
+ { pid: 103, ppid: 102, name: "node", command: "node server.js" },
+ { pid: 200, ppid: 1, name: "python3", command: "python3 /home/other/telegram-bot/bot.py --api-key=xyz" },
+ { pid: 201, ppid: 1, name: "node", command: "node /opt/bootstrapper/agent.js" },
+ { pid: 202, ppid: 1, name: "vite", command: "/usr/bin/node vite --port 5173" },
+ ];
+ const listening = [
+ { proto: "tcp", address: "127.0.0.1", port: 3000, pid: 103, process: "node" },
+ { proto: "tcp", address: "0.0.0.0", port: 8443, pid: 200, process: "/home/other/telegram-bot/venv/bin/python3" },
+ { proto: "tcp", address: "127.0.0.1", port: 5173, pid: 202, process: "vite" },
+ ];
+ const snapshot = await processes.processSnapshot("turn_start", 1, {
+ privacy: { home: "/home/me", user: "me" },
+ scrub: SCRUB,
+ selfPid: 100,
+ precomputed: { processes: table, listening },
+ scope: { rootPid: 100, folders: () => [project] },
+ cwdOf: async (pids) => new Map(pids.map((pid) => [pid, pid === 202 ? path.join(project, "web") : "/home/other/telegram-bot"])),
+ });
+ assert.deepEqual(snapshot.processes.map((p) => [p.pid, p.scope]), [[102, "session"], [103, "session"], [202, "folder"]]);
+ assert.ok(snapshot.processes.find((p) => p.pid === 102).command.includes("--token=[REDACTED]"));
+ assert.deepEqual(snapshot.listening, [
+ { proto: "tcp", address: "loopback", port: 3000, pid: 103, process: "node" },
+ { proto: "tcp", port: 8443, process: "python3" },
+ { proto: "tcp", address: "loopback", port: 5173, pid: 202, process: "vite" },
+ ]);
+ assert.equal(snapshot.total_processes, table.length);
+ const text = JSON.stringify(snapshot);
+ for (const leaked of ["telegram", "bootstrapper", "/home/other", "xyz", "opencode serve"]) assert.ok(!text.includes(leaked), leaked);
+});
+
+test.skipIf(process.platform !== "linux")("#19 privacy, live (Linux): the snapshot lists this process's children, never another account's or an unrelated process", async () => {
+ const child = spawn(process.execPath, ["-e", "setTimeout(()=>{}, 20000)", "--", "child-marker"], { stdio: "ignore" });
+ const outsider = spawn("/bin/sh", ["-c", "exec sleep 20"], { stdio: "ignore", detached: true });
+ try {
+ await new Promise((resolve) => setTimeout(resolve, 200));
+ const snapshot = await processes.processSnapshot("turn_start", 1, { privacy: privacy.currentPrivacy(), scrub: SCRUB, scope: { rootPid: process.pid, folders: () => [] } });
+ assert.ok(snapshot.processes.some((p) => p.pid === child.pid && p.command.includes("child-marker")));
+ const pids = new Set(snapshot.processes.map((p) => p.pid));
+ const all = await processes.linuxProcesses();
+ const tree = processes.descendantsOf(process.pid, all);
+ for (const pid of pids) assert.ok(tree.has(pid), `pid ${pid} is in the session tree`);
+ } finally {
+ child.kill();
+ outsider.kill();
+ }
+});
+
+test("#20 a shell call's start makes the observer sample every 50 ms", async () => {
+ const times = [];
+ const sampler = { method: "fake", pollMs: 1_000, tree: async () => { times.push(Date.now()); return []; }, connections: async () => [] };
+ const observer = new network.NetworkObserver({ sampler });
+ observer.start();
+ await new Promise((resolve) => setTimeout(resolve, 100));
+ const before = times.length;
+ observer.boost(600);
+ await new Promise((resolve) => setTimeout(resolve, 500));
+ await observer.stop();
+ assert.ok(times.length - before >= 6, `samples while boosted: ${times.length - before}`);
+});
+
+test("#20 attribution: a shell fed its command on stdin goes to the one call running; a socket-table root to its call", async () => {
+ const now = Date.now();
+ const resolver = { lookup: async (host) => (host === "example.com" ? ["93.184.215.14"] : []), reverse: async () => [] };
+ const calls = [
+ { callId: "c-curl", tool: "bash", input: {}, status: "completed", start: now, end: now + 300, command: "curl -s https://example.com -o /dev/null", cwd: "/p" },
+ { callId: "c-npx", tool: "bash", input: {}, status: "completed", start: now + 1_000, end: now + 4_000, command: "npx -y [email protected] hi", cwd: "/p" },
+ ];
+ const roots = [
+ { pid: 5, command: "/bin/bash", bareShell: true, firstSeen: now + 50, connections: new Map([["93.184.215.14|443", { ip: "93.184.215.14", port: 443, first: now + 50 }]]) },
+ { pid: -1, command: "x", firstSeen: now + 1_200, callId: "c-npx", connections: new Map([["104.16.0.1|443", { ip: "104.16.0.1", port: 443, first: now + 1_200 }]]) },
+ ];
+ const events = await network.attributeConnections(roots, calls, { turn: 1, method: "proc", pollMs: 250, resolver });
+ assert.deepEqual(events.map((e) => [e.tool_call_id, e.scope]).sort(), [["c-curl", "call"], ["c-npx", "call"]]);
+ assert.equal(events.find((e) => e.tool_call_id === "c-curl").connections[0].host, "example.com");
+ // The socket-table diff keeps only peers of hosts the command names.
+ const before = new Map([["a", { ip: "1.1.1.1", port: 443 }]]);
+ const after = new Map([["a", { ip: "1.1.1.1", port: 443 }], ["b", { ip: "93.184.215.14", port: 443 }], ["c", { ip: "8.8.8.8", port: 443 }]]);
+ const diff = await network.socketDiffConnections(before, after, "curl https://example.com", now, resolver);
+ assert.deepEqual([...diff.keys()], ["93.184.215.14|443"]);
+});
+
+test("#20 opencode tool events: message.part.updated running and completed (and v2) are parsed", async () => {
+ const toolEvents = await import("./context/tool-events.js");
+ const part = (status) => ({ type: "message.part.updated", properties: { part: { type: "tool", tool: "bash", callID: "call_1", sessionID: "ses_1", state: { status, input: { command: "curl https://example.com", workdir: "/w" }, time: { start: 10, end: 20 } } } } });
+ assert.deepEqual(toolEvents.toolEventOf(part("running")), { sessionId: "ses_1", event: { callId: "call_1", tool: "bash", status: "running", command: "curl https://example.com", workdir: "/w", at: 10 } });
+ assert.equal(toolEvents.toolEventOf(part("completed")).event.status, "completed");
+ assert.equal(toolEvents.toolEventOf({ payload: part("pending") }).event.status, "running");
+ assert.equal(toolEvents.toolEventOf({ type: "message.part.updated", properties: { part: { type: "text" } } }), null);
+ assert.deepEqual(toolEvents.toolEventOf({ type: "session.tool.called", data: { sessionID: "s", callID: "c", tool: "bash", input: { command: "ls" } } }), { sessionId: "s", event: { callId: "c", tool: "bash", status: "running", command: "ls", workdir: null } });
+});
+
+test("#20 the turn's network waits for its last messages: a call known only from the final flush still gets its tool_call_id", async () => {
+ const self = process.pid;
+ let live = false;
+ const sampler = {
+ method: "fake",
+ pollMs: 1_000,
+ tree: async () => (live ? [{ pid: 999_001, ppid: self, argv: ["/bin/bash"] }] : []),
+ connections: async (pids) => (pids.includes(999_001) ? [{ pid: 999_001, ip: "93.184.215.14", port: 443 }] : []),
+ };
+ const resolver = { lookup: async (host) => (host === "example.com" ? ["93.184.215.14"] : []), reverse: async () => [] };
+ const recorded = [];
+ const capture = new context.ContextCapture({
+ client: "cli", scrub: SCRUB, record: (_id, type, data) => recorded.push({ type, data }), env: {}, sampler, resolver, platform: "darwin",
+ sections: { system_packages: false, services: false, setup: false, pm_config: false, shell_aliases: false, processes: false },
+ ephemeral: { registryFallback: false },
+ });
+ const sid = "ses-final-msgs";
+ capture.sessionStarted(sid, tmp("final"));
+ capture.turnStarted(sid);
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const command = "curl -s https://example.com -o /dev/null";
+ const start = Date.now();
+ capture.toolEvent(sid, { callId: "call-curl", tool: "bash", status: "running", command });
+ live = true;
+ await new Promise((resolve) => setTimeout(resolve, 150));
+ live = false;
+ capture.toolEvent(sid, { callId: "call-curl", tool: "bash", status: "completed" });
+ // The milestone comes before the uploader flushes the turn's last messages.
+ capture.turnEnded(sid);
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ capture.turnMessages(sid, [{ info: { role: "assistant" }, parts: [
+ { type: "tool", tool: "bash", callID: "call-curl", state: { status: "completed", input: { command }, time: { start, end: Date.now() } } },
+ { type: "tool", tool: "bash", callID: "call-npx", state: { status: "completed", input: { command: "npx -y [email protected] hi" }, time: { start: Date.now() + 1_000, end: Date.now() + 2_000 } } },
+ ] }], true);
+ await capture.settled(sid, 10_000);
+ const net = recorded.filter((e) => e.type === "context.network").map((e) => e.data);
+ assert.deepEqual(net.map((e) => [e.tool_call_id, e.scope, e.connections[0]?.host]), [["call-curl", "call", "example.com"]]);
+ const tools = recorded.filter((e) => e.type === "context.ephemeral_tool").map((e) => e.data);
+ assert.deepEqual(tools.map((t) => [t.tool_call_id, t.package, t.version]), [["call-npx", "cowsay", "1.6.0"]]);
+});
+
+test.skipIf(process.platform !== "linux")("#20 live (Linux): a curl-short connection (~20 ms) is tied to its call by the tool-start boost and the socket-table diff", async () => {
+ // The client closes first (as curl does after a response): its socket lingers in TIME_WAIT.
+ const server = net.createServer((socket) => socket.on("end", () => socket.end())).listen(0, "127.0.0.1");
+ await new Promise((resolve) => server.once("listening", resolve));
+ const port = server.address().port;
+ const recorded = [];
+ const capture = new context.ContextCapture({
+ client: "cli", scrub: SCRUB, record: (_id, type, data) => recorded.push({ type, data }), env: {}, platform: "linux",
+ sections: { system_packages: false, services: false, setup: false, pm_config: false, shell_aliases: false, processes: false },
+ ephemeral: { registryFallback: false },
+ });
+ const sid = "ses-live-short";
+ capture.sessionStarted(sid, tmp("live-short"));
+ capture.turnStarted(sid);
+ const command = `node -e "const s=require('net').connect(${port},'127.0.0.1',()=>s.end());s.on('close',()=>process.exit(0))"`;
+ const start = Date.now();
+ capture.toolEvent(sid, { callId: "call-short", tool: "bash", status: "running", command });
+ await new Promise((resolve) => spawn("/bin/bash", ["-c", command], { stdio: "ignore" }).on("exit", resolve));
+ capture.toolEvent(sid, { callId: "call-short", tool: "bash", status: "completed" });
+ capture.turnEnded(sid);
+ capture.turnMessages(sid, [{ info: { role: "assistant" }, parts: [{ type: "tool", tool: "bash", callID: "call-short", state: { status: "completed", input: { command }, time: { start, end: Date.now() } } }] }], true);
+ await capture.settled(sid, 10_000);
+ server.close();
+ const event = recorded.find((e) => e.type === "context.network" && e.data.tool_call_id === "call-short");
+ assert.ok(event, JSON.stringify(recorded.filter((e) => e.type === "context.network")));
+ assert.ok(event.data.connections.some((c) => c.port === port && c.address_class === "loopback"));
+});
+
+test.skipIf(!hasZstd)("uploader: the turn's last messages (the final flush, not turn.messages) feed the ephemeral tools", async () => {
+ const root = write(tmp("final-flush"), { "package.json": "{}" });
+ const envelopes = [];
+ const syncer = new uploader.SessionUploader({
+ stateDir: tmp("final-flush-state"),
+ fallbackScanMs: 60_000,
+ capabilities: async () => ({ schema_versions: [2], canonical_trace: false }),
+ upload: async (_sessionId, compressed) => {
+ envelopes.push(JSON.parse(zlib.zstdDecompressSync(Buffer.from(compressed)).toString("utf8")));
+ return new Response("{}", { status: 201 });
+ },
+ context: { sections: { system_packages: false, services: false, pm_config: false, setup: false, shell_aliases: false }, sampler: null, ephemeral: { registryFallback: false }, env: {} },
+ });
+ const sessionId = "01a0dcd2-d8ee-7222-80eb-240063770432";
+ syncer.startSession(sessionId, "ws", root);
+ syncer.captureSnapshot(sessionId, "prompt");
+ syncer.captureSnapshot(sessionId, "turn_completed");
+ syncer.flushTrace(sessionId, { messages: [{ info: { role: "assistant" }, parts: [
+ { type: "tool", tool: "bash", callID: "call-cow", state: { status: "completed", input: { command: "npx -y [email protected] hi" }, time: { start: Date.now(), end: Date.now() } } },
+ ] }] });
+ await syncer.context?.settled(sessionId, 30_000);
+ syncer.finishSession(sessionId);
+ await syncer.idle(sessionId);
+ await syncer.stop();
+ const tools = traceEventsOf(envelopes).filter((e) => e.type === "context.ephemeral_tool").map((e) => e.data);
+ assert.deepEqual(tools.map((t) => [t.tool_call_id, t.package, t.version, t.resolved]), [["call-cow", "cowsay", "1.6.0", "exact"]]);
+});
diff --git a/apps/server/src/capture-host.ts b/apps/server/src/capture-host.ts
index 2fc8782a..218215fb 100644
--- a/apps/server/src/capture-host.ts
+++ b/apps/server/src/capture-host.ts
@@ -15,7 +15,7 @@ import {
currentEngineTarget,
engineReplaced,
observeUploadedSession,
- followToolStart,
+ followToolStart, followContextToolEvents,
projectArchiveEngineReads,
promptDispatched,
type EngineReplacement,
@@ -180,6 +180,8 @@ export class CaptureHost {
this.archive.sessionStarted({ sessionId, root, engine: projectArchiveEngineReads(() => currentEngineTarget(this.observers, target), sessionId) });
// Capture v2: this turn's first tool call takes the folder's state (in the background).
followToolStart({ observers: this.observers, archive: this.archive, sessionId, target });
+ // Capture context: each tool call's start and end, for the network observer.
+ followContextToolEvents({ observers: this.observers, context: this.sessionUploader.context, sessionId, target });
}
/** Capture v2: the session-start manifest, before the first prompt reaches the engine (lifecycle.ts startGate). */
diff --git a/apps/server/src/context/PARITY.sha256 b/apps/server/src/context/PARITY.sha256
index 7c4dee42..5d175192 100644
--- a/apps/server/src/context/PARITY.sha256
+++ b/apps/server/src/context/PARITY.sha256
@@ -3,13 +3,14 @@
67b853751c5a32e0fc62d626837fa4be571362b26e11a6d23544db8bf8cce76c commands.ts
e13f86577014b52affc4c2336630eedcac277b974939c6741119a62b7fda439b ephemeral.ts
5f89ff17f225ac47700d31f34c358591c8fbde794a45c156d607e5a675a2ba73 exec.ts
-aa87962064344b7739fff6a5d951741b7aa96710b39fc00df6d153d19d507209 index.ts
-b4460fb7279e273fbb28d42b8636d598793e4b07ff8beb7d57eb127c897136c8 network.ts
-809a324fdcedf0f483911cb87afca2abf6cf52261f090c500ba20a03b3c85f9a outside.ts
+bc7d049e7749bef129e23dcb0b6bde3bd733af0a0db3ede2e3b486cd49727e38 index.ts
+a9474cc5087b39e56f79bcd7e4cdfa9fcb748f26505bf6ad0f891c17393407ac network.ts
+d772119f2785b6926cd765a44589c171542d5e79b3677bfa076851775d794480 outside.ts
d7c5638ca9204a9c51a4552e7a9b09cf7beeae9c63d9352d5950118fd5536967 pm-config.ts
d13231905671ad40fdd73e3fe7af81e44dcc909ace606f45a79c215984b6b35d privacy.ts
-d21e8ba20f64148c69d07363d23f8ab27524ed61abbfc9dfdb8634d3734f5560 processes.ts
-04e135b3fdab1e8427c7279672f9896a65ff1fd8c5790d2ad3b417850a13e662 services.ts
+0e04845a2d0ad2136c01a61d3ed1193b62ef99f5b82e1f555b7c97bb6dc18773 processes.ts
+d8c0636406c8cd9d55f9fb06af8827b3d1b73247393f48aaca2ef053ccf5a1ff services.ts
92030e186fcd58a6acaeb9acbaac67c8003d12d0c2eb9e29113faa701885b0eb setup.ts
d51d434283f757f0ed45bb6996a74fb5bf9e1d56f34146e99df3c392cde5ea19 shell-aliases.ts
5af6e3e4d75e62e0dfc0fee53c583f34463871f9a4d5693e4bc13f688ed2142e system-packages.ts
+4208574258fc043bc0cbb1770c718bfec22d996a44da493489d74d8a05bd7684 tool-events.ts
diff --git a/apps/server/src/context/index.ts b/apps/server/src/context/index.ts
index b33d7886..a68c36bf 100644
--- a/apps/server/src/context/index.ts
+++ b/apps/server/src/context/index.ts
@@ -19,11 +19,11 @@
import { toolCallsOf, commandEffects, toolKind, type ToolCall } from "./commands.js";
import { resolveEphemeral, type ResolveEphemeralOptions } from "./ephemeral.js";
-import { attributeConnections, defaultSampler, NetworkObserver, type HostResolver, type Sampler } from "./network.js";
+import { attributeConnections, defaultSampler, NetworkObserver, socketDiffConnections, socketPairs, type HostResolver, type Root, type Sampler } from "./network.js";
import { OutsideTracker } from "./outside.js";
import { collectPmConfig } from "./pm-config.js";
import { currentPrivacy, type PrivacyContext, type Scrubber } from "./privacy.js";
-import { processSnapshot, processTable, listeningPorts, type ProcessInfo, type ListeningPort } from "./processes.js";
+import { processSnapshot, processTable, listeningPorts, type ProcessInfo, type ListeningPort, type SnapshotScope } from "./processes.js";
import { collectServices } from "./services.js";
import { collectSetup, type SetupFile, type SetupModel } from "./setup.js";
import { collectShellAliases } from "./shell-aliases.js";
@@ -31,7 +31,7 @@ import { systemPackages } from "./system-packages.js";
export const CONTEXT_SCHEMA = 1;
const SECTION_TIMEOUT_MS = 30_000;
-const SETTLE_TIMEOUT_MS = 1_000;
+const SETTLE_TIMEOUT_MS = 3_000;
const MAX_SETUP_FILES_EVENT_BYTES = 512 * 1024;
const MAX_EPHEMERAL_PER_SESSION = 500;
@@ -78,6 +78,8 @@ export type ContextOptions = {
mcpFiles?: string[];
env?: NodeJS.ProcessEnv;
platform?: NodeJS.Platform;
+ /** The app's process, whose descendants are the session's processes (process.pid by default). */
+ rootPid?: number;
/** Tests: overrides. */
enabled?: boolean;
network?: boolean;
@@ -100,6 +102,14 @@ type SessionContext = {
/** Tool calls of the current turn, by call id (or position). */
calls: Map<string, ToolCall>;
processedCalls: Set<string>;
+ /** Shell calls the engine reported starting this turn (tool events), by call id. */
+ liveCalls: Map<string, ToolCall>;
+ /** Linux: the socket table as each live shell call started (its diff at the end names short connections). */
+ socketsAtStart: Map<string, Promise<Map<string, { ip: string; port: number }>>>;
+ /** Connections found by a call's socket-table diff, as roots for the attribution. */
+ diffRoots: Promise<Root | null>[];
+ /** Resolves when the turn's last messages arrived (the uploader's final trace flush). */
+ finalMessages: { promise: Promise<void>; resolve: () => void };
ephemeral: number;
pending: Set<Promise<unknown>>;
ended: boolean;
@@ -111,6 +121,27 @@ export type ContextTimings = { session_start: number[]; turn_start: number[]; tu
/** Active turns across every capture in this process: an unmatched tool shell is only a turn's when one turn runs. */
const activeTurns = new Set<string>();
+function deferred(): { promise: Promise<void>; resolve: () => void } {
+ let resolve: () => void = () => undefined;
+ const promise = new Promise<void>((done) => (resolve = done));
+ return { promise, resolve };
+}
+
+/** A tool call the engine reported starting or ending (its event stream, or pi's tool hooks). */
+export type ToolEvent = {
+ callId: string;
+ tool: string;
+ status: "running" | "completed" | "error";
+ /** The shell command (shell tools). */
+ command?: string | null;
+ /** Its working directory input, as given. */
+ workdir?: string | null;
+ at?: number;
+};
+
+/** How long a turn's network attribution waits for the turn's last messages. */
+const FINAL_MESSAGES_WAIT_MS = 5_000;
+
function withTimeout<T>(promise: Promise<T>, ms: number): Promise<{ value: T } | { timeout: true } | { error: true }> {
return Promise.race([
promise.then((value) => ({ value }), () => ({ error: true as const })),
@@ -135,6 +166,11 @@ export class ContextCapture {
this.platform = options.platform ?? process.platform;
}
+ /** The session's processes: the app's process tree, and processes working in the project or its outside folders. */
+ private scopeOf(state: SessionContext): SnapshotScope {
+ return { rootPid: this.options.rootPid ?? process.pid, folders: () => [state.root, ...state.outside.folderPaths()] };
+ }
+
private section(name: keyof NonNullable<ContextOptions["sections"]>): boolean {
return this.options.sections?.[name] !== false;
}
@@ -176,6 +212,10 @@ export class ContextCapture {
observer: null,
calls: new Map(),
processedCalls: new Set(),
+ liveCalls: new Map(),
+ socketsAtStart: new Map(),
+ diffRoots: [],
+ finalMessages: deferred(),
ephemeral: 0,
pending: new Set(),
ended: false,
@@ -216,14 +256,14 @@ export class ContextCapture {
table = processes;
const ports = await listeningPorts(processes, runOpts);
listening = ports.ports;
- return processSnapshot("session_start", 0, { ...runOpts, privacy, scrub, precomputed: { processes, listening, method: `${method}+${ports.method}` } });
+ return processSnapshot("session_start", 0, { ...runOpts, privacy, scrub, scope: this.scopeOf(state), precomputed: { processes, listening, method: `${method}+${ports.method}` } });
});
if (snapshot) this.emit(state.id, "context.processes", snapshot);
}
const [packages, services, setup, pmConfig, aliases] = await Promise.all([
this.section("system_packages") ? timed("system_packages", () => systemPackages({ platform: this.platform, cacheDir: this.options.cacheDir ?? null })) : undefined,
- this.section("services") ? timed("services", () => collectServices({ root: state.root, listening, processes: table, privacy, scrub, platform: this.platform })) : undefined,
+ this.section("services") ? timed("services", () => collectServices({ root: state.root, listening, processes: table, privacy, scrub, platform: this.platform, sessionStartMs: state.startedAt, folders: state.outside.folderPaths() })) : undefined,
this.section("setup")
? timed("setup", () => collectSetup({
root: state.root,
@@ -288,6 +328,10 @@ export class ContextCapture {
if (!state || state.ended) return;
state.turn += 1;
state.calls = new Map();
+ state.liveCalls = new Map();
+ state.socketsAtStart = new Map();
+ state.diffRoots = [];
+ state.finalMessages = deferred();
if (state.observer) {
activeTurns.add(sessionId);
state.observer.start();
@@ -297,16 +341,54 @@ export class ContextCapture {
this.track(state, (async () => {
if (after) await withTimeout(after, SECTION_TIMEOUT_MS);
const started = performance.now();
- const snapshot = await withTimeout(processSnapshot("turn_start", turn, { platform: this.platform, privacy: this.privacy, scrub: this.options.scrub }), SECTION_TIMEOUT_MS);
+ const snapshot = await withTimeout(processSnapshot("turn_start", turn, { platform: this.platform, privacy: this.privacy, scrub: this.options.scrub, scope: this.scopeOf(state) }), SECTION_TIMEOUT_MS);
this.timings.turn_start.push(Math.round(performance.now() - started));
if ("value" in snapshot) this.emit(sessionId, "context.processes", snapshot.value);
})());
}
- /** The turn's engine messages (possibly in several parts): its tool calls feed #10, #21 and #22. */
- turnMessages(sessionId: string, messages: unknown): void {
+ /**
+ * A tool call started or ended (the engine's event stream, pi's tool
+ * hooks). A shell call's start makes the network observer sample every
+ * 50 ms for its first 2 s and, on Linux, notes the socket table so that a
+ * connection too short to be sampled is still found from the table's
+ * diff at its end (only for hosts its command names).
+ */
+ toolEvent(sessionId: string, event: ToolEvent): void {
+ const state = this.sessions.get(sessionId);
+ if (!state || state.ended || !event.callId || toolKind(event.tool) !== "shell") return;
+ const at = event.at ?? Date.now();
+ const known = state.liveCalls.get(event.callId);
+ if (event.status === "running") {
+ if (known) return;
+ const command = typeof event.command === "string" ? event.command : null;
+ const workdir = typeof event.workdir === "string" && event.workdir.trim() ? event.workdir.trim() : null;
+ state.liveCalls.set(event.callId, { callId: event.callId, tool: event.tool, input: {}, status: "running", start: at, end: null, command, cwd: workdir ? workdir : state.root });
+ state.observer?.boost();
+ if (this.platform === "linux" && state.observer && command) state.socketsAtStart.set(event.callId, socketPairs().catch(() => new Map()));
+ return;
+ }
+ if (!known) return;
+ known.end = at;
+ known.status = event.status;
+ if (typeof event.command === "string" && !known.command) known.command = event.command;
+ const before = state.socketsAtStart.get(event.callId);
+ state.socketsAtStart.delete(event.callId);
+ if (before && known.command) {
+ const command = known.command;
+ const callId = event.callId;
+ state.diffRoots.push((async () => {
+ const connections = await socketDiffConnections(await before, await socketPairs().catch(() => new Map()), command, known.start ?? at, this.options.resolver);
+ return connections.size > 0 ? { pid: -1, command, firstSeen: known.start ?? at, connections, callId, method: "socket_table" } : null;
+ })().catch(() => null));
+ }
+ }
+
+ /** The turn's engine messages (possibly in several parts): its tool calls feed #10, #21 and #22. `final`: the turn's last part. */
+ turnMessages(sessionId: string, messages: unknown, final = false): void {
const state = this.sessions.get(sessionId);
if (!state) return;
+ if (final) queueMicrotask(() => state.finalMessages.resolve());
const calls = toolCallsOf(messages, state.root, this.privacy.home);
const fresh: ToolCall[] = [];
calls.forEach((call, index) => {
@@ -344,13 +426,29 @@ export class ContextCapture {
const state = this.sessions.get(sessionId);
if (!state || !state.observer) return;
const observer = state.observer;
- const calls = [...state.calls.values()];
const turn = state.turn;
const alone = activeTurns.size <= 1;
activeTurns.delete(sessionId);
+ const finalMessages = state.finalMessages.promise;
+ const live = state.liveCalls;
+ const diffs = state.diffRoots;
this.track(state, (async () => {
+ const roots: Root[] = await observer.stop();
+ // The turn's calls are known once its last messages arrived (the uploader flushes them right after this milestone).
+ await withTimeout(finalMessages, FINAL_MESSAGES_WAIT_MS);
const started = performance.now();
- const roots = await observer.stop();
+ for (const root of await Promise.all(diffs)) if (root) roots.push(root);
+ const calls = [...state.calls.values()];
+ // Calls the tool events saw but no message carried yet (the engine had not stored them).
+ for (const call of live.values()) if (!calls.some((known) => known.callId === call.callId)) calls.push(call);
+ // A message's call takes the tool event's start and end when it has none.
+ for (const call of calls) {
+ const seen = call.callId ? live.get(call.callId) : undefined;
+ if (seen) {
+ call.start ??= seen.start;
+ call.end ??= seen.end;
+ }
+ }
this.timings.network_sample_ms = observer.sampleMs;
this.timings.network_samples = observer.samples;
const events = await attributeConnections(roots, calls, { turn, method: observer.method, pollMs: observer.pollMs, ...(this.options.resolver ? { resolver: this.options.resolver } : {}) });
@@ -377,6 +475,7 @@ export class ContextCapture {
const state = this.sessions.get(sessionId);
if (!state || state.ended) return;
if (state.observer && activeTurns.has(sessionId)) this.turnEnded(sessionId);
+ state.finalMessages.resolve();
state.ended = true;
void this.settled(sessionId, SETTLE_TIMEOUT_MS).finally(() => this.sessions.delete(sessionId));
}
diff --git a/apps/server/src/context/network.ts b/apps/server/src/context/network.ts
index 62a08506..57c69e67 100644
--- a/apps/server/src/context/network.ts
+++ b/apps/server/src/context/network.ts
@@ -28,6 +28,9 @@ import { addressClass, type AddressClass } from "./privacy.js";
import { parseNetstat, parsePs, parseProcNetTcp, parseProcStat, parseWindowsProcesses, splitHostPort, type ProcessInfo } from "./processes.js";
export const MAX_CONNECTIONS_PER_CALL = 100;
+/** Sampling interval in the first BOOST_MS of each tool call. */
+export const BOOST_POLL_MS = 50;
+export const BOOST_MS = 2_000;
const MAX_ROOTS_PER_TURN = 500;
const SHELLS = new Set(["bash", "sh", "zsh", "dash", "ksh", "fish", "pwsh", "powershell", "cmd", "busybox"]);
@@ -44,7 +47,18 @@ export type NetworkEvent = {
};
type Seen = { ip: string; port: number; first: number };
-type Root = { pid: number; command: string; firstSeen: number; connections: Map<string, Seen> };
+export type Root = {
+ pid: number;
+ command: string;
+ firstSeen: number;
+ connections: Map<string, Seen>;
+ /** The tool call this root is known to belong to (a socket-table diff of that call). */
+ callId?: string;
+ /** How the connections were seen when not by sampling the process tree. */
+ method?: string;
+ /** A shell that names no command (fed on stdin): attributed to the one shell call running when it appeared. */
+ bareShell?: boolean;
+};
/** The engine that runs the tools (`opencode serve`, the app's engine binary). */
export function isEngineProcess(argv: string[]): boolean {
@@ -326,6 +340,8 @@ export class NetworkObserver {
private preexisting: Set<number> | null = null;
/** Whether the last sample saw a tool shell running (sample faster then). */
private busy = false;
+ /** Until when to sample at BOOST_POLL_MS: the first seconds of a tool call that just started. */
+ private boostUntil = 0;
/** Time spent sampling (overhead measurement). */
sampleMs = 0;
samples = 0;
@@ -351,11 +367,34 @@ export class NetworkObserver {
private schedule(delay: number): void {
if (!this.active) return;
this.timer = setTimeout(() => {
- this.sampling = this.sample().catch(() => undefined).finally(() => this.schedule(this.busy ? this.options.sampler.activePollMs ?? this.options.sampler.pollMs : this.options.sampler.pollMs));
+ this.sampling = this.sample().catch(() => undefined).finally(() => this.schedule(this.nextDelay()));
}, delay);
this.timer.unref?.();
}
+ private nextDelay(): number {
+ if (Date.now() < this.boostUntil) return BOOST_POLL_MS;
+ return this.busy ? this.options.sampler.activePollMs ?? this.options.sampler.pollMs : this.options.sampler.pollMs;
+ }
+
+ /**
+ * A tool call is about to run (the engine's tool-start event comes before
+ * its process is spawned): sample every BOOST_POLL_MS for the next
+ * BOOST_MS, so a command that lasts a fraction of a second is still seen.
+ */
+ boost(ms = BOOST_MS): void {
+ if (!this.active) return;
+ this.boostUntil = Math.max(this.boostUntil, Date.now() + ms);
+ // Take the next sample now rather than after the slow interval.
+ if (this.timer) {
+ clearTimeout(this.timer);
+ this.timer = null;
+ void this.sampling.then(() => {
+ if (this.active && !this.timer) this.schedule(0);
+ });
+ }
+ }
+
private stopTimer(): void {
if (this.timer) clearTimeout(this.timer);
this.timer = null;
@@ -408,7 +447,9 @@ export class NetworkObserver {
members.push(entry.pid);
if (!this.roots.has(root) && this.roots.size < MAX_ROOTS_PER_TURN) {
const argv = byPid.get(root)!.argv;
- this.roots.set(root, { pid: root, command: shellCommandOf(argv) ?? argv.join(" "), firstSeen: now, connections: new Map() });
+ const command = shellCommandOf(argv);
+ const bareShell = command === null && SHELLS.has((argv[0]?.split(/[\\/]/).at(-1) ?? "").replace(/\.exe$/i, "").toLowerCase());
+ this.roots.set(root, { pid: root, command: command ?? argv.join(" "), firstSeen: now, connections: new Map(), ...(bareShell ? { bareShell } : {}) });
}
}
this.busy = members.length > 0;
@@ -437,6 +478,54 @@ export class NetworkObserver {
}
}
+// --- socket-table diff (Linux): connections of a call too short to sample ------------
+
+/** Every TCP socket's (local, remote) pair with a remote peer, from /proc/net/tcp{,6}: TIME_WAIT included. */
+export async function socketPairs(procNet = "/proc/net"): Promise<Map<string, { ip: string; port: number }>> {
+ const out = new Map<string, { ip: string; port: number }>();
+ const sockets = (await Promise.all(["tcp", "tcp6"].map((name) => readFile(`${procNet}/${name}`, "utf8").catch(() => "")))).flatMap(parseProcNetTcp);
+ // The accepting side of a local connection (its local port listens) is not a connection the tool made.
+ const listening = new Set(sockets.filter((socket) => socket.state === "0A").map((socket) => socket.local.port));
+ for (const socket of sockets) {
+ if (socket.state === "0A" || socket.remote.port === 0 || listening.has(socket.local.port)) continue;
+ out.set(`${socket.local.address}:${socket.local.port}>${socket.remote.address}:${socket.remote.port}`, { ip: socket.remote.address, port: socket.remote.port });
+ }
+ return out;
+}
+
+/**
+ * The connections a call made that sampling may have missed: sockets that
+ * appeared between its start and end (a closed one lingers in TIME_WAIT),
+ * kept only when their peer is an address of a host the command names, so
+ * nothing another process on the machine did is ever attributed to it.
+ */
+export async function socketDiffConnections(
+ before: Map<string, { ip: string; port: number }>,
+ after: Map<string, { ip: string; port: number }>,
+ command: string,
+ at: number,
+ resolver: HostResolver = SYSTEM_RESOLVER,
+): Promise<Map<string, Seen>> {
+ const fresh = [...after].filter(([key]) => !before.has(key)).map(([, peer]) => peer);
+ const out = new Map<string, Seen>();
+ if (fresh.length === 0) return out;
+ const named = new Set<string>();
+ await Promise.all(hostnamesInCommand(command).map(async (host) => {
+ for (const ip of (await withTimeout(resolver.lookup(host), 1_000)) ?? []) named.add(normalizeIp(ip));
+ }));
+ // IP literals the command names count as named hosts too.
+ for (const match of command.matchAll(/(?<![\d.])(\d{1,3}(?:\.\d{1,3}){3})(?![\d.])/g)) named.add(match[1]!);
+ if (/\blocalhost\b/.test(command)) {
+ named.add("127.0.0.1");
+ named.add("::1");
+ }
+ for (const peer of fresh) {
+ if (!named.has(normalizeIp(peer.ip))) continue;
+ out.set(`${peer.ip}|${peer.port}`, { ip: peer.ip, port: peer.port, first: at });
+ }
+ return out;
+}
+
// --- naming hosts and attributing roots to calls -------------------------------------
const HOST_IN_COMMAND = /(?:[a-z][a-z0-9+.-]*:\/\/(?:[^@/\s]*@)?([^/:\s"'?#]+))|(?:[\w.-]+@([a-z0-9.-]+\.[a-z]{2,}):)|(?<![\w/.@:-])((?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,24})\b/gi;
@@ -551,11 +640,15 @@ export async function attributeConnections(
const perCall = new Map<ToolCall, Seen[]>();
const turn: Seen[] = [];
const turnCommands: string[] = [];
+ const within = (call: ToolCall, at: number, slack: number) => call.start !== null && at >= call.start - slack && (call.end === null || at <= call.end + slack);
for (const root of roots) {
if (root.connections.size === 0) continue;
+ const byId = root.callId ? shellCalls.find((call) => call.callId === root.callId) : undefined;
const candidates = shellCalls.filter((call) => sameCommand(call.command!, root.command));
- const timed = candidates.filter((call) => call.start === null || (root.firstSeen >= call.start - 2_000 && (call.end === null || root.firstSeen <= call.end + 2_000)));
- const call = (timed.length > 0 ? timed : candidates)[0];
+ const timed = candidates.filter((call) => call.start === null || within(call, root.firstSeen, 2_000));
+ // A shell the command cannot name (a command fed on stdin): the one shell call running when it appeared.
+ const running = shellCalls.filter((call) => within(call, root.firstSeen, 500));
+ const call = byId ?? (timed.length > 0 ? timed : candidates)[0] ?? (root.bareShell && running.length === 1 ? running[0] : undefined);
if (call) perCall.set(call, [...(perCall.get(call) ?? []), ...root.connections.values()]);
else {
turn.push(...root.connections.values());
diff --git a/apps/server/src/context/outside.ts b/apps/server/src/context/outside.ts
index b72959a7..38a2d594 100644
--- a/apps/server/src/context/outside.ts
+++ b/apps/server/src/context/outside.ts
@@ -182,6 +182,11 @@ export class OutsideTracker {
return isSecretFile(path) || this.options.exclusion(path) !== null;
}
+ /** The outside folders of the session (created or worked in, not refused), absolute. */
+ folderPaths(): string[] {
+ return [...this.folders.keys()];
+ }
+
/** Processes one turn's tool calls; returns the events to record. */
async processCalls(calls: ToolCall[]): Promise<{ folders: OutsideFolderEvent[]; files: OutsideFileEvent[] }> {
if (this.realRoot === null) {
@@ -285,7 +290,10 @@ export class OutsideTracker {
// A folder older than the session that a command claims to create (mkdir -p on an existing one) is only "modified".
const createdNow = folder.created && (rootStats.birthtimeMs || rootStats.ctimeMs) >= this.options.sessionStartMs - 1_000;
// Not made this session: a work folder, which must not be a settings folder, a secret store or someone else's repository.
- if (!createdNow && (await this.refusedWorkFolder(folder.path)) !== null) return null;
+ if (!createdNow && (await this.refusedWorkFolder(folder.path)) !== null) {
+ this.folders.delete(folder.path);
+ return null;
+ }
let entries = 0;
let regenerable = 0;
let added = 0;
diff --git a/apps/server/src/context/processes.ts b/apps/server/src/context/processes.ts
index 1c3cfd24..a9d5c308 100644
--- a/apps/server/src/context/processes.ts
+++ b/apps/server/src/context/processes.ts
@@ -322,17 +322,31 @@ export async function listeningPorts(processes: ProcessInfo[], options: ProcessT
return { ports: out, method };
}
+/** A listening port of a process outside the session: its port and the process's base name, nothing else. */
+export type ForeignListeningPort = { proto: "tcp"; port: number; process: string | null };
+
export type ProcessSnapshot = {
phase: "session_start" | "turn_start";
turn: number;
collected_at: string;
- listening: ListeningPort[];
- processes: Array<{ pid: number; name: string; command: string }>;
+ /** Ports of the session's processes (address class, pid, name) and of any other process (port and base name only). */
+ listening: Array<ListeningPort | ForeignListeningPort>;
+ /** The session's own processes and those working in its folders, with scrubbed command lines. */
+ processes: Array<{ pid: number; name: string; command: string; scope: "session" | "folder" }>;
+ /** How many processes the machine runs (none of the others is named). */
total_processes: number;
truncated: boolean;
method: string;
};
+/** Which processes belong to the session: the app's process tree, and processes working in the session's folders. */
+export type SnapshotScope = {
+ /** The app's process (the CLI, or the desktop server): the session's tree is its descendants. */
+ rootPid: number;
+ /** The project and the outside folders the agent created or worked in (absolute). */
+ folders: () => readonly string[];
+};
+
export type SnapshotOptions = ProcessTableOptions & {
privacy: PrivacyContext;
scrub: Scrubber;
@@ -340,27 +354,109 @@ export type SnapshotOptions = ProcessTableOptions & {
now?: () => Date;
/** A process table and port list already taken (session start shares them with services). */
precomputed?: { processes: ProcessInfo[]; listening: ListeningPort[]; method?: string };
+ scope?: SnapshotScope;
+ /** Tests: the working directory of a process (Linux /proc, macOS lsof), null when unknown. */
+ cwdOf?: (pids: number[]) => Promise<Map<number, string>>;
};
-/** One #19 snapshot: listening ports and the dev-relevant processes, scrubbed and capped. */
+/** The descendants of `rootPid` in a process table. */
+export function descendantsOf(rootPid: number, processes: readonly ProcessInfo[]): Set<number> {
+ const children = new Map<number, number[]>();
+ for (const info of processes) {
+ const list = children.get(info.ppid) ?? [];
+ list.push(info.pid);
+ children.set(info.ppid, list);
+ }
+ const out = new Set<number>();
+ const queue = [...(children.get(rootPid) ?? [])];
+ while (queue.length > 0 && out.size < 100_000) {
+ const pid = queue.shift()!;
+ if (out.has(pid)) continue;
+ out.add(pid);
+ queue.push(...(children.get(pid) ?? []));
+ }
+ return out;
+}
+
+/** The working directories of `pids`: /proc/<pid>/cwd on Linux, `lsof -d cwd` on macOS, none on Windows. */
+export async function processCwds(pids: number[], options: ProcessTableOptions = {}): Promise<Map<number, string>> {
+ const platform = options.platform ?? process.platform;
+ const out = new Map<number, string>();
+ if (pids.length === 0) return out;
+ if (platform === "linux") {
+ await Promise.all(pids.map(async (pid) => {
+ try {
+ out.set(pid, await readlink(`/proc/${pid}/cwd`));
+ } catch {
+ // Another account's process, or gone.
+ }
+ }));
+ return out;
+ }
+ if (platform === "darwin") {
+ const result = await runTool("lsof", ["-a", "-d", "cwd", "-Fpn", "-p", pids.slice(0, 500).join(",")], { timeoutMs: 4_000, maxBytes: 1024 * 1024, ...(options.run ? { run: options.run } : {}) });
+ let pid = 0;
+ for (const line of result?.stdout.split(/\r?\n/) ?? []) {
+ if (line.startsWith("p")) pid = Number(line.slice(1));
+ else if (line.startsWith("n") && pid) out.set(pid, line.slice(1));
+ }
+ }
+ return out;
+}
+
+function insideAny(path: string, folders: readonly string[]): boolean {
+ const portable = path.replaceAll("\\", "/").replace(/\/+$/, "");
+ return folders.some((folder) => {
+ const base = folder.replaceAll("\\", "/").replace(/\/+$/, "");
+ return base.length > 1 && (portable === base || portable.startsWith(`${base}/`));
+ });
+}
+
+function baseNameOnly(name: string | null): string | null {
+ if (!name) return null;
+ const base = name.trim().split(/\s+/)[0]!.split(/[\\/]/).at(-1) ?? "";
+ return base.replace(/[^\w.+-]/g, "").slice(0, 64) || null;
+}
+
+/**
+ * One #19 snapshot. Only the session's own processes (descendants of the
+ * app's process) and processes working inside the project or an outside
+ * folder of the session are listed, with their scrubbed command lines. Any
+ * other process on the machine is never named, except as the owner of a
+ * listening port, shown as the port and the process's base name only.
+ */
export async function processSnapshot(phase: ProcessSnapshot["phase"], turn: number, options: SnapshotOptions): Promise<ProcessSnapshot> {
const { processes, method } = options.precomputed ? { processes: options.precomputed.processes, method: options.precomputed.method ?? "shared" } : await processTable(options);
const { ports, method: portMethod } = options.precomputed ? { ports: options.precomputed.listening, method: "shared" } : await listeningPorts(processes, options);
const self = options.selfPid ?? process.pid;
- const dev = processes.filter((info) => !isOwnProcess(info, self) && isDevProcess(info.name, info.command));
- const kept = dev.slice(0, MAX_PROCESSES).map((info) => ({
+ const scope = options.scope ?? { rootPid: self, folders: () => [] };
+ const tree = descendantsOf(scope.rootPid, processes);
+ const folders = scope.folders();
+ const others = processes.filter((info) => !tree.has(info.pid) && info.pid !== scope.rootPid);
+ const cwds = folders.length > 0 ? await (options.cwdOf ?? ((pids) => processCwds(pids, options)))(others.map((info) => info.pid)).catch(() => new Map<number, string>()) : new Map<number, string>();
+ const inFolders = new Set(others.filter((info) => {
+ const cwd = cwds.get(info.pid);
+ return cwd !== undefined && insideAny(cwd, folders);
+ }).map((info) => info.pid));
+ const ours = processes.filter((info) => (tree.has(info.pid) || inFolders.has(info.pid)) && !isOwnProcess(info, self));
+ const kept = ours.slice(0, MAX_PROCESSES).map((info) => ({
pid: info.pid,
- name: cleanText(info.name, options.privacy, options.scrub),
+ name: cleanText(baseNameOnly(info.name) ?? info.name, options.privacy, options.scrub),
command: cleanCommand(info.command, options.privacy, options.scrub),
+ scope: tree.has(info.pid) ? ("session" as const) : ("folder" as const),
}));
+ const owned = (pid: number | null) => pid !== null && (tree.has(pid) || inFolders.has(pid) || pid === scope.rootPid);
+ const listening = ports.slice(0, MAX_LISTENING).map((port): ListeningPort | ForeignListeningPort => (owned(port.pid)
+ ? { ...port, address: addressClass(port.address), process: port.process ? cleanText(baseNameOnly(port.process) ?? port.process, options.privacy, options.scrub) : null }
+ : { proto: "tcp", port: port.port, process: baseNameOnly(port.process) }));
return {
phase,
turn,
collected_at: (options.now ?? (() => new Date()))().toISOString(),
- listening: ports.slice(0, MAX_LISTENING).map((port) => ({ ...port, address: addressClass(port.address), process: port.process ? cleanText(port.process, options.privacy, options.scrub) : null })),
+ listening,
processes: kept,
total_processes: processes.length,
- truncated: dev.length > MAX_PROCESSES || ports.length > MAX_LISTENING,
+ truncated: ours.length > MAX_PROCESSES || ports.length > MAX_LISTENING,
method: options.precomputed ? method : `${method}+${portMethod}`,
};
}
diff --git a/apps/server/src/context/services.ts b/apps/server/src/context/services.ts
index 14dac1a6..9c65eedf 100644
--- a/apps/server/src/context/services.ts
+++ b/apps/server/src/context/services.ts
@@ -19,7 +19,11 @@ const SKIP_DIRS = new Set(["node_modules", ".git", ".venv", "venv", "dist", "bui
export type DockerContainer = { name: string; image: string; image_digest: string | null; ports: string; status: string };
export type LocalDatabase = { kind: string; port: number; address: string; client_version: string | null; process: string | null };
export type Services = {
- docker: { reachable: boolean; containers: DockerContainer[]; truncated?: boolean; skipped?: string };
+ /**
+ * Only the containers the session started or that bind-mount the project
+ * (or one of its outside folders) are named; `other_containers` counts the rest.
+ */
+ docker: { reachable: boolean; containers: DockerContainer[]; other_containers?: number; truncated?: boolean; skipped?: string };
databases: LocalDatabase[];
compose_files: string[];
};
@@ -118,7 +122,34 @@ async function findComposeFiles(root: string, maxDepth = 3): Promise<string[]> {
return out;
}
+/** One `docker inspect --format '{{.Id}} {{.Image}} {{.Created}} {{json .Mounts}}'` line. */
+export function parseInspectLine(line: string): { id: string; image: string; createdMs: number | null; bindSources: string[] } | null {
+ const match = /^(\S+) (\S+) (\S+) (.*)$/.exec(line.trim());
+ if (!match) return null;
+ const created = Date.parse(match[3]!.replace(/(\.\d{3})\d+/, "$1"));
+ let bindSources: string[] = [];
+ try {
+ const mounts = JSON.parse(match[4]!) as Array<{ Type?: string; Source?: string }>;
+ if (Array.isArray(mounts)) bindSources = mounts.filter((mount) => mount && mount.Type === "bind" && typeof mount.Source === "string").map((mount) => mount.Source!);
+ } catch {
+ bindSources = [];
+ }
+ return { id: match[1]!, image: match[2]!, createdMs: Number.isFinite(created) ? created : null, bindSources };
+}
+
+function insideFolder(path: string, folders: readonly string[]): boolean {
+ const portable = path.replaceAll("\\", "/").replace(/\/+$/, "");
+ return folders.some((folder) => {
+ const base = folder.replaceAll("\\", "/").replace(/\/+$/, "");
+ return base.length > 1 && (portable === base || portable.startsWith(`${base}/`));
+ });
+}
+
export type ServicesOptions = {
+ /** When the session started: a container created since is the session's. */
+ sessionStartMs?: number;
+ /** Outside folders of the session, beside the project root. */
+ folders?: readonly string[];
root: string;
listening: ListeningPort[];
processes: ProcessInfo[];
@@ -158,18 +189,28 @@ export async function collectServices(options: ServicesOptions): Promise<Service
const ps = await tool("docker", ["ps", "--no-trunc", "--format", "{{json .}}"], 8_000);
if (ps && ps.code === 0) {
docker.reachable = true;
- const rows = parseDockerPs(ps.stdout);
- const kept = rows.slice(0, MAX_CONTAINERS);
- if (rows.length > MAX_CONTAINERS) docker.truncated = true;
- // Image digests: container -> image id -> repo digest, in two calls.
+ const rows = parseDockerPs(ps.stdout).slice(0, 500);
+ // Each container's image, creation time and bind mounts, in one call.
+ const imageOf = new Map<string, string>();
+ const related = new Set<string>();
+ if (rows.length > 0) {
+ const inspected = await tool("docker", ["inspect", "--format", "{{.Id}} {{.Image}} {{.Created}} {{json .Mounts}}", ...rows.map((row) => row.id)], 8_000);
+ const folders = [options.root, ...(options.folders ?? [])];
+ for (const line of inspected?.stdout.split(/\r?\n/) ?? []) {
+ const parsed = parseInspectLine(line);
+ if (!parsed) continue;
+ imageOf.set(parsed.id, parsed.image);
+ const startedInSession = options.sessionStartMs !== undefined && parsed.createdMs !== null && parsed.createdMs >= options.sessionStartMs - 5_000;
+ if (startedInSession || parsed.bindSources.some((source) => insideFolder(source, folders))) related.add(parsed.id);
+ }
+ }
+ const ours = rows.filter((row) => [...related].some((id) => id.startsWith(row.id) || row.id.startsWith(id)));
+ docker.other_containers = rows.length - ours.length;
+ const kept = ours.slice(0, MAX_CONTAINERS);
+ if (ours.length > MAX_CONTAINERS) docker.truncated = true;
+ // Image digests: image id -> repo digest.
const digests = new Map<string, string>();
if (kept.length > 0) {
- const images = await tool("docker", ["inspect", "--format", "{{.Id}} {{.Image}}", ...kept.map((row) => row.id)], 8_000);
- const imageOf = new Map<string, string>();
- for (const line of images?.stdout.split(/\r?\n/) ?? []) {
- const [id, image] = line.trim().split(/\s+/);
- if (id && image) imageOf.set(id, image);
- }
const imageIds = [...new Set(imageOf.values())];
if (imageIds.length > 0) {
const repo = await tool("docker", ["image", "inspect", "--format", "{{.Id}} {{json .RepoDigests}}", ...imageIds], 8_000);
@@ -194,7 +235,7 @@ export async function collectServices(options: ServicesOptions): Promise<Service
ports: clean(row.ports),
status: clean(row.status),
}));
- for (const row of rows) for (const file of composeFilesOfLabels(row.labels)) composeFiles.add(file);
+ for (const row of kept) for (const file of composeFilesOfLabels(row.labels)) composeFiles.add(file);
} else {
docker.skipped = ps?.timedOut ? "timeout" : "unreachable";
}
diff --git a/apps/server/src/context/tool-events.ts b/apps/server/src/context/tool-events.ts
new file mode 100644
index 00000000..c19b3e1d
--- /dev/null
+++ b/apps/server/src/context/tool-events.ts
@@ -0,0 +1,105 @@
+// The engine's tool-call events for capture context (#20): every tool part
+// of a session as it starts running and as it ends, from the opencode event
+// stream (v1 `/event` `message.part.updated`; v2 `/api/event`
+// `session.tool.*`). A shell call's start reaches ContextCapture.toolEvent
+// before its process is spawned, so the network observer can sample it from
+// its first milliseconds. Identical in the CLI and the desktop app.
+
+import { sseFrames } from "../session-archive/tool-start.js";
+import type { ToolEvent } from "./index.js";
+
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return typeof value === "object" && value !== null && !Array.isArray(value);
+}
+
+function text(value: unknown): string | null {
+ return typeof value === "string" && value ? value : null;
+}
+
+/** A tool start or end of one session, or null when `payload` is not one. */
+export function toolEventOf(payload: unknown): { sessionId: string; event: ToolEvent } | null {
+ if (!isRecord(payload)) return null;
+ if (isRecord(payload.payload) && typeof payload.payload.type === "string") return toolEventOf(payload.payload);
+ const type = typeof payload.type === "string" ? payload.type : "";
+ if (type === "message.part.updated") {
+ const properties = isRecord(payload.properties) ? payload.properties : null;
+ const part = properties && isRecord(properties.part) ? properties.part : null;
+ if (!part || part.type !== "tool") return null;
+ const state = isRecord(part.state) ? part.state : {};
+ const status = state.status === "running" || state.status === "pending" ? "running" : state.status === "completed" ? "completed" : state.status === "error" ? "error" : null;
+ const sessionId = text(part.sessionID);
+ const callId = text(part.callID) ?? text(part.id);
+ const tool = text(part.tool);
+ if (!status || !sessionId || !callId || !tool) return null;
+ const input = isRecord(state.input) ? state.input : {};
+ const time = isRecord(state.time) ? state.time : {};
+ const at = status === "running" ? (typeof time.start === "number" ? time.start : undefined) : typeof time.end === "number" ? time.end : undefined;
+ return { sessionId, event: { callId, tool, status, command: text(input.command), workdir: text(input.workdir) ?? text(input.cwd), ...(at !== undefined ? { at } : {}) } };
+ }
+ const v2 = /^session\.(?:next\.)?tool\.(input\.started|called|completed|failed|error|result)$/.exec(type);
+ if (v2) {
+ const data = isRecord(payload.data) ? payload.data : isRecord(payload.properties) ? payload.properties : null;
+ if (!data) return null;
+ const sessionId = text(data.sessionID);
+ const callId = text(data.callID) ?? text(data.toolCallID) ?? text(data.id);
+ const tool = text(data.tool) ?? text(data.name);
+ if (!sessionId || !callId || !tool) return null;
+ const input = isRecord(data.input) ? data.input : {};
+ const status = v2[1] === "input.started" || v2[1] === "called" ? "running" : v2[1] === "completed" || v2[1] === "result" ? "completed" : "error";
+ return { sessionId, event: { callId, tool, status, command: text(input.command), workdir: text(input.workdir) ?? text(input.cwd) } };
+ }
+ return null;
+}
+
+export type ToolEventFetch = (url: string, init: { headers: Headers; signal: AbortSignal }) => Promise<Response>;
+
+const MAX_FRAME_CHARS = 4 * 1024 * 1024;
+
+/** Follows the event stream until `signal` aborts, handing each tool event of `sessionId` to `onEvent`. Never throws. */
+export async function watchToolEvents(input: {
+ url: string;
+ headers: Headers;
+ sessionId: string;
+ signal: AbortSignal;
+ fetch: ToolEventFetch;
+ onEvent: (event: ToolEvent) => void;
+}): Promise<void> {
+ if (input.signal.aborted) return;
+ try {
+ const headers = new Headers(input.headers);
+ headers.set("accept", "text/event-stream");
+ const response = await input.fetch(input.url, { headers, signal: input.signal });
+ if (!response.ok || !response.body) {
+ await response.body?.cancel().catch(() => undefined);
+ return;
+ }
+ const reader = response.body.getReader();
+ const decoder = new TextDecoder();
+ let buffer = "";
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) return;
+ buffer += decoder.decode(value, { stream: true });
+ const { payloads, rest } = sseFrames(buffer);
+ buffer = rest.length > MAX_FRAME_CHARS ? "" : rest;
+ for (const raw of payloads) {
+ let payload: unknown;
+ try {
+ payload = JSON.parse(raw);
+ } catch {
+ continue;
+ }
+ const found = toolEventOf(payload);
+ if (found && found.sessionId === input.sessionId) {
+ try {
+ input.onEvent(found.event);
+ } catch {
+ // Capture never stops the stream.
+ }
+ }
+ }
+ }
+ } catch {
+ // Aborted, or the engine went away.
+ }
+}
diff --git a/apps/server/src/session-upload-observer.ts b/apps/server/src/session-upload-observer.ts
index 5a33df45..ab8ef38c 100644
--- a/apps/server/src/session-upload-observer.ts
+++ b/apps/server/src/session-upload-observer.ts
@@ -12,6 +12,8 @@ import { isFinishedAssistantMessage, isPromptMessage, type ArchiveEngineReads, t
import { MAX_UPLOAD_CHILD_SESSION_DEPTH, type UploadSessionModel, type SessionUploader } from "./session-uploader.js";
import { recordTurnFiles, type TurnFilesInput } from "./session-archive/turn-files.js";
import { watchToolStarts } from "./session-archive/tool-start.js";
+import { watchToolEvents } from "./context/tool-events.js";
+import type { ContextCapture } from "./context/index.js";
/** The engine a captured request went to: base URL, request headers (the engine's auth), query and API generation. */
export type EngineTarget = {
@@ -1102,3 +1104,43 @@ export function followToolStart(input: {
if (map.get(input.sessionId) === controller) map.delete(input.sessionId);
});
}
+
+const CONTEXT_WATCHES = new WeakMap<SessionObservers, Map<string, AbortController>>();
+
+/**
+ * Capture context (#20): every tool call of this turn as it starts and
+ * ends, from the engine's event stream, for the network observer (a shell
+ * call's start comes before its process is spawned). Runs until the turn's
+ * observation stops; the next turn's call replaces it.
+ */
+export function followContextToolEvents(input: {
+ observers: SessionObservers;
+ context: Pick<ContextCapture, "toolEvent"> | null | undefined;
+ sessionId: string;
+ target: EngineTarget;
+}): void {
+ const context = input.context;
+ if (!context) return;
+ let watches = CONTEXT_WATCHES.get(input.observers);
+ if (!watches) {
+ watches = new Map();
+ CONTEXT_WATCHES.set(input.observers, watches);
+ }
+ watches.get(input.sessionId)?.abort();
+ const controller = new AbortController();
+ watches.set(input.sessionId, controller);
+ const map = watches;
+ const { baseUrl, headers, search, engine } = input.target;
+ const url = buildOpencodeProxyUrl(baseUrl, engine === "v2" ? "/api/event" : "/event", search);
+ const signal = AbortSignal.any([controller.signal, input.observers.controller.signal, AbortSignal.timeout(MAX_TOOL_WATCH_MS)]);
+ void watchToolEvents({
+ url,
+ headers: new Headers(headers),
+ sessionId: input.sessionId,
+ signal,
+ fetch: (target, init) => loopbackFetch(target, init),
+ onEvent: (event) => context.toolEvent(input.sessionId, event),
+ }).finally(() => {
+ if (map.get(input.sessionId) === controller) map.delete(input.sessionId);
+ });
+}
diff --git a/apps/server/src/session-uploader.ts b/apps/server/src/session-uploader.ts
index 26d90ad5..93dc17df 100644
--- a/apps/server/src/session-uploader.ts
+++ b/apps/server/src/session-uploader.ts
@@ -5139,6 +5139,8 @@ export class SessionUploader {
for (const evidence of knownToolPathEvidence("session.completed", finalTrace)) {
this.recordTouchedPath(state, evidence.path, evidence.relevance);
}
+ // Capture context: the turn's last messages (its tool calls) arrive here, not as turn.messages.
+ if (isRecord(finalTrace)) this.context?.turnMessages(sessionId, finalTrace.messages, true);
}
state.finished = true;
this.context?.sessionEnded(sessionId);
@@ -5176,6 +5178,8 @@ export class SessionUploader {
for (const evidence of knownToolPathEvidence("turn.completed", finalTrace)) {
this.recordTouchedPath(state, evidence.path, evidence.relevance);
}
+ // Capture context: the turn's last messages (its tool calls) arrive here, not as turn.messages.
+ if (isRecord(finalTrace)) this.context?.turnMessages(sessionId, finalTrace.messages, true);
pendingTrace.push({ at: new Date().toISOString(), type: "turn.completed", data: finalTrace });
}
if (pendingTrace.length === 0) return;