memhouse 0.18.12 → 0.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +22 -151
- package/CHANGELOG.md +263 -29
- package/LICENSE +202 -22
- package/LICENSES/agentlytics-MIT.txt +21 -0
- package/LICENSES/memhouse-MIT-until-0.18.11.txt +22 -0
- package/LICENSES/models.dev-MIT.txt +22 -0
- package/NOTICE +28 -0
- package/README.md +42 -20
- package/SECURITY.md +21 -8
- package/bin/memhouse.js +2223 -311
- package/docs/README.md +11 -9
- package/docs/guides/admin-password.md +37 -10
- package/docs/guides/configuration.md +13 -6
- package/docs/guides/daemons.md +64 -3
- package/docs/guides/import-claude-ai.md +204 -0
- package/docs/guides/install.md +11 -6
- package/docs/guides/invite.md +21 -7
- package/docs/guides/operate.md +114 -7
- package/docs/guides/search-and-resume.md +72 -3
- package/docs/guides/share.md +91 -52
- package/docs/guides/storage.md +44 -4
- package/docs/guides/troubleshooting.md +121 -14
- package/docs/guides/uninstall.md +4 -3
- package/docs/guides/update.md +16 -0
- package/docs/guides/upgrade-to-0.20.md +188 -0
- package/docs/releases/README.md +2 -1
- package/docs/releases/v0.18.10.md +5 -5
- package/docs/releases/v0.18.11.md +3 -3
- package/docs/releases/v0.20.0.md +627 -0
- package/docs/releases/v0.20.1.md +114 -0
- package/docs/tutorials/01-first-house.md +10 -7
- package/docs/tutorials/02-join-a-house.md +24 -15
- package/docs/tutorials/03-share-your-memory.md +40 -31
- package/editors/antigravity.js +1 -80
- package/editors/base.js +463 -9
- package/editors/bridge.js +119 -0
- package/editors/claude.js +273 -105
- package/editors/codex.js +278 -52
- package/editors/copilot.js +1 -70
- package/editors/cursor.js +1 -75
- package/editors/derive-fingerprints.json +18 -0
- package/editors/harness-paths.js +69 -0
- package/editors/index.js +1 -20
- package/editors/normalise/agy.js +910 -0
- package/editors/normalise/copilot-cli.js +321 -0
- package/editors/normalise/cursor-agent.js +852 -0
- package/editors/normalise/cursor.js +474 -0
- package/editors/normalise/devin.js +575 -0
- package/editors/normalise/gemini-cli.js +602 -0
- package/editors/normalise/goose.js +673 -0
- package/editors/normalise/opencode.js +633 -0
- package/editors/normalise/vscode.js +549 -0
- package/editors/normalise/zed.js +473 -0
- package/editors/opencode-v4.js +119 -0
- package/editors/scope.js +14 -3
- package/editors/sessions-log.js +349 -0
- package/editors/sqlite.js +28 -1
- package/editors/vscode.js +1 -72
- package/editors/windsurf.js +1 -143
- package/examples/01-local-one-machine/README.md +24 -19
- package/examples/01-local-one-machine/run.sh +1 -1
- package/examples/02-existing-clickhouse/README.md +30 -22
- package/examples/02-existing-clickhouse/run.sh +5 -4
- package/examples/03-team-house/README.md +44 -37
- package/examples/03-team-house/run.sh +35 -18
- package/examples/04-self-hosted-team/README.md +29 -14
- package/examples/04-self-hosted-team/compose.yml +5 -2
- package/examples/04-self-hosted-team/smoke.sh +26 -12
- package/examples/README.md +8 -4
- package/examples/fixtures/README.md +4 -1
- package/examples/fixtures/make-sessions.js +8 -5
- package/memhouse/admin-secret.js +89 -24
- package/memhouse/argv-secrets.js +64 -0
- package/memhouse/channel.js +49 -9
- package/memhouse/codex-plugin.js +194 -0
- package/memhouse/delivery/AGENT-INSTALL-PROMPT.md +2 -1
- package/memhouse/delivery/AGENT-INSTALL.md +22 -11
- package/memhouse/delivery/INVITATION.md.tpl +44 -45
- package/memhouse/delivery/PROMPT.md +53 -5
- package/memhouse/delivery/UPGRADE.md.tpl +1 -1
- package/memhouse/delivery/codex/AGENTS-BLOCK.md +11 -0
- package/memhouse/delivery/codex/SKILL.md +50 -0
- package/memhouse/delivery/kernel-install.md +17 -9
- package/memhouse/delivery/plugin/.claude-plugin/plugin.json +2 -2
- package/memhouse/delivery/plugin/reference/HOUSE.md +168 -41
- package/memhouse/delivery/plugin/skills/access/SKILL.md +13 -5
- package/memhouse/delivery/plugin/skills/admin/SKILL.md +14 -6
- package/memhouse/delivery/plugin/skills/house/SKILL.md +25 -8
- package/memhouse/delivery/plugin/skills/recall/SKILL.md +66 -8
- package/memhouse/delivery/plugin/skills/sql/SKILL.md +66 -7
- package/memhouse/deploy.js +1 -1
- package/memhouse/flags.js +35 -18
- package/memhouse/help.js +37 -0
- package/memhouse/host.js +82 -2
- package/memhouse/house/HOUSE.md +2 -2
- package/memhouse/house/convert-profile.js +638 -0
- package/memhouse/house/convert-v3.js +742 -0
- package/memhouse/house/doctor-v3.js +324 -0
- package/memhouse/house/host-alias.js +85 -0
- package/memhouse/house/house.js +3 -3
- package/memhouse/house/migrate.js +2 -2
- package/memhouse/house/profile-dedupe.js +183 -0
- package/memhouse/house/profile-edges.js +554 -0
- package/memhouse/house/profile-house.js +430 -0
- package/memhouse/house/profile-rollup.js +190 -0
- package/memhouse/house/q-cli.js +863 -0
- package/memhouse/house/queries-v3.js +1013 -0
- package/memhouse/house/reconcile-v3.js +192 -0
- package/memhouse/house/records-to-profile.js +174 -0
- package/memhouse/house/records-to-rows.js +169 -0
- package/memhouse/house/relocate.js +2 -1
- package/memhouse/house/schema-choice.js +121 -0
- package/memhouse/house/schema-profile.js +290 -0
- package/memhouse/house/schema-profile.sql.tpl +398 -0
- package/memhouse/house/schema-v3.js +267 -0
- package/memhouse/house/schema-v3.sql.tpl +213 -0
- package/memhouse/import/claude-ai.js +420 -0
- package/memhouse/import/import.js +372 -0
- package/memhouse/import/pyjson.js +206 -0
- package/memhouse/import/records.js +64 -0
- package/memhouse/import/writer-profile.js +192 -0
- package/memhouse/import/writer-v3.js +240 -0
- package/memhouse/import/zip.js +112 -0
- package/memhouse/invitation.js +102 -0
- package/memhouse/invitefile.js +5 -2
- package/memhouse/plugin-copies.js +176 -0
- package/memhouse/provision.js +7 -2
- package/memhouse/release-hint.js +24 -0
- package/memhouse/resume.js +4 -4
- package/memhouse/server/api-v3.js +632 -0
- package/memhouse/server/dashboard-auth.js +181 -0
- package/memhouse/server/dashboard-port.js +91 -0
- package/memhouse/server/queries.js +30 -0
- package/memhouse/server/server.js +118 -20
- package/memhouse/service.js +1 -1
- package/memhouse/share.js +687 -1
- package/memhouse/shipper/adapter-capture.js +756 -0
- package/memhouse/shipper/capture.js +2571 -0
- package/memhouse/shipper/dedupe-profile.js +237 -0
- package/memhouse/shipper/dedupe-v3.js +1027 -0
- package/memhouse/shipper/forks.js +233 -0
- package/memhouse/shipper/log-adapter.js +219 -0
- package/memhouse/shipper/maintenance.js +49 -0
- package/memhouse/shipper/normalise-run.js +148 -0
- package/memhouse/shipper/passlog.js +68 -5
- package/memhouse/shipper/profile-capture.js +903 -0
- package/memhouse/shipper/ship.js +91 -8
- package/memhouse/shipper/snapshots.js +193 -0
- package/memhouse/shipper/source-roots.js +111 -0
- package/memhouse/version-notice.js +66 -0
- package/package.json +14 -7
- package/public/THIRD_PARTY_NOTICES.txt +2 -2
- package/public/assets/index-CcNnhfM3.js +78 -0
- package/public/index.html +1 -1
- package/ui/package-lock.json +56 -37
- package/ui/package.json +1 -0
- package/ui/src/App.jsx +29 -14
- package/ui/src/components/ChatSidebar.jsx +69 -12
- package/ui/src/lib/api.js +76 -0
- package/ui/src/lib/dashboard-token.js +41 -0
- package/ui/src/main.jsx +22 -5
- package/ui/src/pages/Dashboard.jsx +22 -36
- package/ui/third-party-notices.js +119 -0
- package/ui/vite.config.js +5 -1
- package/docs/releases/v0.18.12.md +0 -59
- package/memhouse/COMPETITION.md +0 -221
- package/memhouse/DESIGN.md +0 -222
- package/memhouse/RESUME-AND-UPDATE.md +0 -182
- package/public/assets/index-CnWzrjF3.js +0 -78
|
@@ -0,0 +1,852 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// v4 wave 1: cursor-agent (the Cursor CLI) → normalised session log records
|
|
3
|
+
// (docs/design/sessions-log.md). The Cursor IDE (state.vscdb) is a separate harness, `cursor`.
|
|
4
|
+
//
|
|
5
|
+
// Store: <CFG>/chats/<md5(cwd)>/<chatId>/store.db, CFG = harness-paths' dataFolder('cursor-agent')
|
|
6
|
+
// ($CURSOR_CONFIG_DIR, else $XDG_CONFIG_HOME/cursor, else ~/.cursor). Also
|
|
7
|
+
// <CFG>/acp-sessions/<id>/store.db (same store class; UNVERIFIED once a turn exists). SQLite, WAL:
|
|
8
|
+
// - blobs(id, data): immutable rows, id = hex sha256(data);
|
|
9
|
+
// - meta key "0": hex(utf8(JSON)) {agentId, latestRootBlobId, name, createdAt, subagentInfo, …}.
|
|
10
|
+
// The current conversation is the blob tree under latestRootBlobId, a protobuf
|
|
11
|
+
// ConversationStateStructure: #8 turns (ConversationTurnStructure: #1 agent turn | #2 shell turn),
|
|
12
|
+
// #1 the live model context (JSON model-message blobs), #13/#11 summary archives.
|
|
13
|
+
// Spec: docs/design/adapter-specs/cursor-agent.md (repository); sessions-model v0 §1, §2, §4.
|
|
14
|
+
//
|
|
15
|
+
// Rules:
|
|
16
|
+
// - membership: a session is a chat dir with its store.db. agent-transcripts/*.jsonl is never read,
|
|
17
|
+
// so an orphan transcript (its chat deleted) is never listed (conformance/cursor-orphan);
|
|
18
|
+
// - ids: the user message is UserMessage.message_id (else the turn's user_message_id, else
|
|
19
|
+
// `<request_id>:user`, as in sub-agent stores); the assistant message is `<request_id>:assistant`,
|
|
20
|
+
// its steps the parts in steps[] order, part_id = the step index. A shell-mode turn (`!cmd`) has
|
|
21
|
+
// no id at all: `<anchor>+n`, the anchor being the previous agent turn's native user id (or the
|
|
22
|
+
// chat id), sessions-model §1 rule 4. A compaction summary is a system message keyed by its
|
|
23
|
+
// ConversationSummaryArchive blob id, placed after the turn its last archived request belongs to;
|
|
24
|
+
// - tool parts: call_id = the step's tool_call_id, byte-exact (it contains '\n'). Name, args and
|
|
25
|
+
// output come from the model context (the JSON tool-call and tool-result with that id, reached from
|
|
26
|
+
// the root's #1 and the summary archives), else from the step itself: typed args and typed results
|
|
27
|
+
// for the observed kinds only (see redaction). An untyped kind's STEP result is never decoded (a
|
|
28
|
+
// schema-less decode takes every string): from the step, only whether it succeeded, output null.
|
|
29
|
+
// This is not a redaction of the tool's output: where the model context holds the result the model
|
|
30
|
+
// saw, that text is the output and is written as free text (sessions-model §4.6), whatever the
|
|
31
|
+
// kind, except for the credential-bearing kinds below.
|
|
32
|
+
// CallDynamicTool is unwrapped to the tool it calls;
|
|
33
|
+
// A shell step's (and a `!cmd` turn's) exit code is `exit_code`; an MCP step's server
|
|
34
|
+
// (McpArgs server_identifier, else provider_identifier) is `server`, whatever the context says. An
|
|
35
|
+
// output the step holds only by reference (a binary read's data blob, a truncated step's original
|
|
36
|
+
// blob) gets `output_ref` {sha: the blob id, len};
|
|
37
|
+
// - source: a simulated user message (UserMessage is_simulated_msg) is `automated` (PLAN_EXECUTION
|
|
38
|
+
// observed), except COMMIT_REMINDER (`injected`) and the reasons that carry the user's own words
|
|
39
|
+
// (USER_*_SUBAGENT, INTERACTION_RESPONSE). In a sub-agent store the prompt is the parent's:
|
|
40
|
+
// `automated`, as is a message sent by another agent (sent_by_agent_id; source-only). A JSON
|
|
41
|
+
// `<system_reminder>` user message in the model context (the sub-agent reminder, the output-limit
|
|
42
|
+
// "cut off" notice) is a hidden system message, `injected`, keyed by its blob id and placed after
|
|
43
|
+
// the turn of the request before it (before the first turn when none is);
|
|
44
|
+
// - usage: none is stored, so `usage` is null. Model: the JSON assistant part's
|
|
45
|
+
// providerOptions.cursor.modelName for the turn's requestId, else routed_model_display_name.
|
|
46
|
+
// model_requested: meta lastUsedModel (`default` = Auto). It is chat-level, so a model switch
|
|
47
|
+
// relabels the earlier replies once. Effort stays folded into the model id, unparsed;
|
|
48
|
+
// - held back: the LAST turn's reply while a tool step has no result, or no step exists yet.
|
|
49
|
+
// Everything after it waits too. A turn followed by another is released; a tool that never got a
|
|
50
|
+
// result is `interrupted`. Step completed_at_ms is not used: the final text step of every model
|
|
51
|
+
// response has none (seen on a test machine). So a reply can be written between two steps and grow by a rewrite;
|
|
52
|
+
// whether a step is persisted while it streams was never observed (UNVERIFIED);
|
|
53
|
+
// - lifecycle: rewind moves latestRootBlobId back, so the later turns vanish from the log (a
|
|
54
|
+
// rewrite). The cut turns stay in the store as orphan blobs: an orphaned user message sent from a
|
|
55
|
+
// snapshot that is still in the tree gives a `rewind` lifecycle record at that point (findRewinds).
|
|
56
|
+
// A fork copies the blobs to a new chatId with no parent ref: no link. A sub-agent store's meta
|
|
57
|
+
// subagentInfo {parentAgentId, toolCallId} is an explicit child-side link; the parent names its
|
|
58
|
+
// child (Task result agent_id, subagent_states, subagent runs) in `children`. A compaction summary
|
|
59
|
+
// is `hidden` (model context, not the transcript). A refused first prompt leaves the empty root
|
|
60
|
+
// (sha256 of no bytes): a titled session with no messages;
|
|
61
|
+
// - volatile fields stay out: meta.json updatedAtMs, mode, the context gauge (token_details).
|
|
62
|
+
// `updated` is null;
|
|
63
|
+
// - redaction by field path: only whitelisted meta keys are read (the per-chat encryption key never
|
|
64
|
+
// leaves the parser), the turn's encrypted_model (#4) is never decoded, and step args come only
|
|
65
|
+
// through a curated projection per typed kind, whose field names are an allowlist (ShellArgs
|
|
66
|
+
// secret_scope_id is dropped). An untyped kind's step args are never decoded (a schema-less decode
|
|
67
|
+
// cannot tell a path from a secret): its args come from the model context or are {}. The
|
|
68
|
+
// credential-bearing kinds (replace_env, mcp_login, connect_scm, or a call the model context names
|
|
69
|
+
// so, in any case) never have their step args or result text decoded, and the model context's
|
|
70
|
+
// copies are filtered the same way: args keep only naming keys (name, server, provider, …) whose
|
|
71
|
+
// value is a string or a list of strings, with any user:pass@ cut from a URL in it; output is null.
|
|
72
|
+
// Other tools' args and outputs from the model context are written as the model saw them.
|
|
73
|
+
// cli-config.json and auth.json are never opened;
|
|
74
|
+
// - reading: a consistent snapshot in one read transaction, opened through editors/sqlite
|
|
75
|
+
// openReadOnly (immutable when there is no -wal, so nothing is created beside the store). A read
|
|
76
|
+
// error (a busy or locked store) fails the pass, never reads as empty: normalise throws, and the
|
|
77
|
+
// runner retries.
|
|
78
|
+
// A referenced blob that is missing means a torn read: normalise returns null and the pass skips.
|
|
79
|
+
|
|
80
|
+
const fs = require('fs');
|
|
81
|
+
const path = require('path');
|
|
82
|
+
const crypto = require('crypto');
|
|
83
|
+
const { fileURLToPath } = require('url');
|
|
84
|
+
const { openReadOnly, textOf, bytesOf } = require('../sqlite');
|
|
85
|
+
const harnessPaths = require('../harness-paths');
|
|
86
|
+
const { placeChildren } = require('../sessions-log');
|
|
87
|
+
|
|
88
|
+
const HARNESS = 'cursor-agent';
|
|
89
|
+
const EMPTY_ROOT = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
|
|
90
|
+
const EMPTY = Buffer.alloc(0);
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The config folder that holds chats/: harness-paths' data folder, which honours CURSOR_CONFIG_DIR,
|
|
94
|
+
* then XDG_CONFIG_HOME/cursor, then ~/.cursor, so harness_path matches the 0.18 bridge's and the
|
|
95
|
+
* switch dedupes. The XDG branch is from source (adapter spec §1); CURSOR_CONFIG_DIR was verified on
|
|
96
|
+
* a test machine.
|
|
97
|
+
*/
|
|
98
|
+
function storeRootDefault() { return harnessPaths.dataFolder(HARNESS); }
|
|
99
|
+
|
|
100
|
+
const md5 = (s) => crypto.createHash('md5').update(s).digest('hex');
|
|
101
|
+
const sha256 = (b) => crypto.createHash('sha256').update(b).digest('hex');
|
|
102
|
+
function sortedDir(dir) { try { return fs.readdirSync(dir).sort(); } catch { return []; } }
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Sessions in one store: every chats/<bucket>/<chatId>/ and acp-sessions/<id>/ that has a store.db.
|
|
106
|
+
* `harness_path` is the config folder's real path (absolute, symlinks resolved, the filesystem's case),
|
|
107
|
+
* through the same resolver as harness-paths' dataFolder. Each entry names its own store.db and -wal
|
|
108
|
+
* (one store per session, so the runner re-reads only this session when they change). meta.json is
|
|
109
|
+
* not named: it is rewritten (updatedAtMs) on every open, and only its cwd is read, as a fallback.
|
|
110
|
+
*/
|
|
111
|
+
function list(storeRoot = storeRootDefault()) {
|
|
112
|
+
try { if (!fs.statSync(storeRoot).isDirectory()) return []; } catch { return []; }
|
|
113
|
+
const harnessPath = harnessPaths.resolveNative(storeRoot);
|
|
114
|
+
const out = [];
|
|
115
|
+
const add = (dir, sid, bucket) => {
|
|
116
|
+
const db = path.join(dir, 'store.db');
|
|
117
|
+
try { if (!fs.statSync(db).isFile()) return; } catch { return; } // empty or refused-headless chat
|
|
118
|
+
out.push({ harness: HARNESS, harness_path: harnessPath, session_id: sid, dir, db, wal: `${db}-wal`, bucket });
|
|
119
|
+
};
|
|
120
|
+
const chats = path.join(storeRoot, 'chats');
|
|
121
|
+
for (const b of sortedDir(chats)) for (const id of sortedDir(path.join(chats, b))) add(path.join(chats, b, id), id, b);
|
|
122
|
+
const acp = path.join(storeRoot, 'acp-sessions');
|
|
123
|
+
for (const id of sortedDir(acp)) add(path.join(acp, id), id, null);
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ── protobuf wire format: just enough to walk the store ───────────────────────────────────────
|
|
128
|
+
class Malformed extends Error {}
|
|
129
|
+
class MissingBlob extends Error {}
|
|
130
|
+
|
|
131
|
+
function varint(buf, pos) {
|
|
132
|
+
let n = 0;
|
|
133
|
+
let mul = 1;
|
|
134
|
+
for (let i = 0; i < 7; i++) {
|
|
135
|
+
if (pos >= buf.length) throw new Malformed('varint');
|
|
136
|
+
const b = buf[pos++];
|
|
137
|
+
n += (b & 0x7f) * mul;
|
|
138
|
+
if (b < 0x80) return [n, pos];
|
|
139
|
+
mul *= 128;
|
|
140
|
+
}
|
|
141
|
+
let big = BigInt(n);
|
|
142
|
+
for (let i = 7; i < 10; i++) {
|
|
143
|
+
if (pos >= buf.length) throw new Malformed('varint');
|
|
144
|
+
const b = buf[pos++];
|
|
145
|
+
big += BigInt(b & 0x7f) << BigInt(7 * i);
|
|
146
|
+
if (b < 0x80) return [big, pos];
|
|
147
|
+
}
|
|
148
|
+
throw new Malformed('varint');
|
|
149
|
+
}
|
|
150
|
+
const toNum = (v) => (typeof v === 'bigint' ? Number(BigInt.asIntN(64, v)) : v);
|
|
151
|
+
|
|
152
|
+
function parseFields(buf) {
|
|
153
|
+
const f = new Map();
|
|
154
|
+
let pos = 0;
|
|
155
|
+
while (pos < buf.length) {
|
|
156
|
+
let key;
|
|
157
|
+
[key, pos] = varint(buf, pos);
|
|
158
|
+
key = toNum(key);
|
|
159
|
+
const fn = Math.floor(key / 8);
|
|
160
|
+
const w = key % 8;
|
|
161
|
+
if (fn < 1) throw new Malformed('field 0');
|
|
162
|
+
let v;
|
|
163
|
+
if (w === 0) [v, pos] = varint(buf, pos);
|
|
164
|
+
else if (w === 2) {
|
|
165
|
+
let len;
|
|
166
|
+
[len, pos] = varint(buf, pos);
|
|
167
|
+
len = toNum(len);
|
|
168
|
+
if (len < 0 || pos + len > buf.length) throw new Malformed('length');
|
|
169
|
+
v = buf.subarray(pos, pos + len);
|
|
170
|
+
pos += len;
|
|
171
|
+
} else if (w === 1 || w === 5) {
|
|
172
|
+
const n = w === 1 ? 8 : 4;
|
|
173
|
+
if (pos + n > buf.length) throw new Malformed('fixed');
|
|
174
|
+
v = buf.subarray(pos, pos + n);
|
|
175
|
+
pos += n;
|
|
176
|
+
} else throw new Malformed(`wire type ${w}`);
|
|
177
|
+
if (!f.has(fn)) f.set(fn, []);
|
|
178
|
+
f.get(fn).push({ w, v });
|
|
179
|
+
}
|
|
180
|
+
return f;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A decoded message: field number → values. Singular reads take the last value (proto3 merge). */
|
|
184
|
+
class Msg {
|
|
185
|
+
constructor(buf) { this.buf = buf || EMPTY; this.f = parseFields(this.buf); }
|
|
186
|
+
has(n) { return this.f.has(n); }
|
|
187
|
+
get size() { return this.f.size; }
|
|
188
|
+
last(n) { const a = this.f.get(n); return a ? a[a.length - 1] : null; }
|
|
189
|
+
lens(n) { return (this.f.get(n) || []).filter((x) => x.w === 2).map((x) => x.v); }
|
|
190
|
+
str(n) { const x = this.last(n); return x && x.w === 2 ? x.v.toString('utf8') : ''; }
|
|
191
|
+
strs(n) { return this.lens(n).map((v) => v.toString('utf8')); }
|
|
192
|
+
num(n) { const x = this.last(n); return x && x.w === 0 ? toNum(x.v) : null; }
|
|
193
|
+
bool(n) { return !!this.num(n); }
|
|
194
|
+
double(n) { const x = this.last(n); return x && x.w === 1 ? x.v.readDoubleLE(0) : null; }
|
|
195
|
+
msg(n) { const x = this.last(n); return x && x.w === 2 ? new Msg(x.v) : null; }
|
|
196
|
+
msgs(n) { return this.lens(n).map((v) => new Msg(v)); }
|
|
197
|
+
ref(n) { const x = this.last(n); return x && x.w === 2 ? x.v.toString('hex') : ''; }
|
|
198
|
+
refs(n) { return this.lens(n).map((v) => v.toString('hex')); }
|
|
199
|
+
/** The first field present among `cases` (a oneof), with its message. */
|
|
200
|
+
oneof(cases) { for (const c of cases) if (this.has(c)) return [c, this.msg(c) || new Msg(EMPTY)]; return [0, null]; }
|
|
201
|
+
}
|
|
202
|
+
function tryMsg(buf) { try { return new Msg(buf); } catch { return null; } }
|
|
203
|
+
|
|
204
|
+
// No schema-less decode: a step field is read only through a typed projection (see redaction).
|
|
205
|
+
|
|
206
|
+
// ── tool calls ────────────────────────────────────────────────────────────────────────────────
|
|
207
|
+
// ToolCall oneof `tool`: field number → kind (agent.v1, cursor-agent 2026.10.19).
|
|
208
|
+
const KINDS = {
|
|
209
|
+
1: 'shell', 3: 'delete', 4: 'glob', 5: 'grep', 8: 'read', 9: 'update_todos', 10: 'read_todos', 12: 'edit', 13: 'ls',
|
|
210
|
+
14: 'read_lints', 15: 'mcp', 16: 'sem_search', 17: 'create_plan', 18: 'web_search', 19: 'task', 20: 'list_mcp_resources',
|
|
211
|
+
21: 'read_mcp_resource', 22: 'apply_agent_diff', 23: 'ask_question', 24: 'fetch', 25: 'switch_mode', 28: 'generate_image',
|
|
212
|
+
29: 'record_screen', 30: 'computer_use', 31: 'write_shell_stdin', 32: 'reflect', 33: 'setup_vm_environment', 34: 'truncated',
|
|
213
|
+
35: 'start_grind_execution', 36: 'start_grind_planning', 37: 'web_fetch', 38: 'report_bugfix_results', 39: 'ai_attribution',
|
|
214
|
+
40: 'pr_management', 41: 'mcp_login', 42: 'await', 43: 'blame_by_file_path', 44: 'get_mcp_tools', 45: 'report_bug',
|
|
215
|
+
46: 'set_active_branch', 48: 'communicate_update', 49: 'send_final_summary', 51: 'replace_env', 52: 'edit_pr_labels',
|
|
216
|
+
53: 'record_ci_investigation_findings', 55: 'send_message', 56: 'fetch_cloud_agent_data', 58: 'send_to_user', 61: 'pi_read',
|
|
217
|
+
62: 'pi_bash', 63: 'pi_edit', 64: 'pi_write', 65: 'pi_grep', 66: 'pi_find', 67: 'pi_ls', 68: 'connect_scm',
|
|
218
|
+
69: 'search_conversations', 70: 'create_goal', 71: 'update_goal', 73: 'get_agent_status', 74: 'send_to_agent',
|
|
219
|
+
75: 'read_agent_transcript', 76: 'create_agent', 77: 'stop_agent', 79: 'write_canvas', 80: 'read_canvas', 81: 'generate_video',
|
|
220
|
+
82: 'x_search', 83: 'get_origin_pr_code_tour', 84: 'update_origin_pr_code_tour', 85: 'list_tracked_tasks',
|
|
221
|
+
86: 'create_tracked_tasks', 87: 'update_tracked_tasks', 88: 'delete_tracked_tasks', 89: 'update_tracked_task_statuses',
|
|
222
|
+
90: 'create_project',
|
|
223
|
+
};
|
|
224
|
+
const COMMON = new Set([54, 57, 59, 60]); // hook_additional_contexts, tool_call_id, started/completed ms
|
|
225
|
+
// Kinds whose arguments may carry credentials: no fallback decode of their args (the JSON args, when
|
|
226
|
+
// present, are what the model sent).
|
|
227
|
+
const SENSITIVE = new Set(['replace_env', 'mcp_login', 'connect_scm']);
|
|
228
|
+
// The same tools by their model-facing names (the JSON context may name a call the step kind does not).
|
|
229
|
+
const SENSITIVE_NAMES = /^(ReplaceEnv|McpLogin|McpAuth|ConnectScm|replace_env|mcp_login|mcp_auth|connect_scm)$/i;
|
|
230
|
+
// The only argument keys kept for those tools, from any source: what names the thing, never a value.
|
|
231
|
+
const SENSITIVE_ARG_NAMES = new Set(['name', 'names', 'server', 'server_name', 'serverName', 'provider', 'provider_identifier', 'providerIdentifier', 'scm', 'host']);
|
|
232
|
+
const noUserinfo = (v) => v.replace(/([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@]*@/gi, '$1');
|
|
233
|
+
/** A kept key's value for those tools: a string or a list of strings (userinfo cut), else dropped. */
|
|
234
|
+
function sensitiveValue(v) {
|
|
235
|
+
if (typeof v === 'string') return noUserinfo(v);
|
|
236
|
+
if (Array.isArray(v) && v.every((x) => typeof x === 'string')) return v.map(noUserinfo);
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
// The model-facing name where it differs from the kind in PascalCase (observed in the JSON context).
|
|
240
|
+
const NAMES = { update_todos: 'TodoWrite', read_todos: 'TodoRead', get_mcp_tools: 'GetDynamicTools', ls: 'LS', mcp: 'CallMcpTool' };
|
|
241
|
+
const pascal = (k) => k.split('_').map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join('');
|
|
242
|
+
const TODO_STATUS = ['unspecified', 'pending', 'in_progress', 'completed', 'cancelled'];
|
|
243
|
+
|
|
244
|
+
function pick(o) {
|
|
245
|
+
const out = {};
|
|
246
|
+
for (const [k, v] of Object.entries(o)) if (v !== '' && v !== null && v !== undefined && v !== false && v !== 0) out[k] = v;
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
const todo = (t) => pick({ id: t.str(1), content: t.str(2), status: TODO_STATUS[t.num(3) || 0] || String(t.num(3)) });
|
|
250
|
+
/** google.protobuf.Value (the MCP args map's value type; UNVERIFIED, no MCP call was observed). */
|
|
251
|
+
function pbValue(m) {
|
|
252
|
+
if (!m) return null;
|
|
253
|
+
if (m.has(3)) return m.str(3);
|
|
254
|
+
if (m.has(2)) return m.double(2);
|
|
255
|
+
if (m.has(4)) return m.bool(4);
|
|
256
|
+
if (m.has(5)) return pbStruct(m.msg(5));
|
|
257
|
+
if (m.has(6)) return m.msg(6).msgs(1).map(pbValue);
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
function pbStruct(s) { const o = {}; for (const e of s.msgs(1)) o[e.str(1)] = pbValue(e.msg(2)); return o; }
|
|
261
|
+
|
|
262
|
+
// Curated args per kind: the fields the model chose, never the harness's bookkeeping
|
|
263
|
+
// (parsing_result, sandbox policy, conversation_id, request_id, secret_scope_id).
|
|
264
|
+
const ARGS = {
|
|
265
|
+
shell: (a) => pick({ command: a.str(1), working_directory: a.str(2), description: a.str(15), timeout: a.num(3), is_background: a.bool(11) }),
|
|
266
|
+
read: (a) => pick({ path: a.str(1), offset: a.num(2), limit: a.num(3) }),
|
|
267
|
+
edit: (a) => pick({ path: a.str(1), stream_content: a.str(6) }),
|
|
268
|
+
glob: (a) => pick({ target_directory: a.str(1), glob_pattern: a.str(2) }),
|
|
269
|
+
grep: (a) => pick({ pattern: a.str(1), path: a.str(2), glob: a.str(3), output_mode: a.str(4) }),
|
|
270
|
+
task: (a) => pick({ description: a.str(1), prompt: a.str(2), model: a.str(4) }),
|
|
271
|
+
update_todos: (a) => pick({ todos: a.msgs(1).map(todo), merge: a.bool(2) }),
|
|
272
|
+
mcp: (a) => {
|
|
273
|
+
let args = {};
|
|
274
|
+
try { for (const e of a.msgs(2)) args[e.str(1)] = pbValue(e.msg(2)); } catch { args = {}; }
|
|
275
|
+
return pick({ server: a.str(9) || a.str(4), tool: a.str(5) || a.str(1), args });
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
// The names the projections above may emit: anything else is dropped, whatever a projection returns.
|
|
279
|
+
const ARG_NAMES = new Set([
|
|
280
|
+
'command', 'working_directory', 'description', 'timeout', 'is_background', 'path', 'offset', 'limit', 'stream_content',
|
|
281
|
+
'target_directory', 'glob_pattern', 'pattern', 'glob', 'output_mode', 'prompt', 'model', 'todos', 'merge', 'server', 'tool', 'args',
|
|
282
|
+
]);
|
|
283
|
+
const allowArgs = (o) => Object.fromEntries(Object.entries(o || {}).filter(([k]) => ARG_NAMES.has(k)));
|
|
284
|
+
|
|
285
|
+
const ok = (output) => ({ output, is_error: false, status: 'completed' });
|
|
286
|
+
const err = (output) => ({ output, is_error: true, status: 'completed' });
|
|
287
|
+
const denied = (output) => ({ output, is_error: true, status: 'denied' });
|
|
288
|
+
const joinOut = (...xs) => xs.filter(Boolean).join('\n');
|
|
289
|
+
|
|
290
|
+
// Typed result text per kind; `blobText` resolves a content blob reference, `blobLen` gives a blob's
|
|
291
|
+
// size (both lenient: '' and null for a missing blob).
|
|
292
|
+
const RESULTS = {
|
|
293
|
+
shell(r) {
|
|
294
|
+
const [c, m] = r.oneof([1, 2, 3, 4, 5, 7]);
|
|
295
|
+
if (c === 1) return { ...ok(m.str(10) || joinOut(m.str(5), m.str(6))), exit_code: m.num(3) || 0 };
|
|
296
|
+
if (c === 2) return { output: m.str(9) || joinOut(m.str(5), m.str(6)), is_error: true, status: m.bool(11) ? 'cancelled' : 'completed', exit_code: m.num(3) || 0 };
|
|
297
|
+
if (c === 3) return { output: `timed out after ${m.num(3) || 0} ms`, is_error: true, status: 'cancelled' };
|
|
298
|
+
if (c === 4) return denied(m.str(3));
|
|
299
|
+
if (c === 5) return err(m.str(3));
|
|
300
|
+
if (c === 7) return denied(m.str(3));
|
|
301
|
+
return null;
|
|
302
|
+
},
|
|
303
|
+
read(r, blobText, blobLen) {
|
|
304
|
+
const [c, m] = r.oneof([1, 2]);
|
|
305
|
+
if (c === 1) {
|
|
306
|
+
if (m.has(1)) return ok(m.str(1));
|
|
307
|
+
if (m.has(10)) return ok(blobText(m.ref(10)));
|
|
308
|
+
if (m.has(6)) return ok(`<binary ${m.last(6).v.length} bytes>`);
|
|
309
|
+
if (m.has(9)) return { ...ok(`<binary blob ${m.ref(9)}>`), output_ref: { sha: m.ref(9), len: blobLen(m.ref(9)) } };
|
|
310
|
+
return ok('');
|
|
311
|
+
}
|
|
312
|
+
if (c === 2) return err(m.str(1));
|
|
313
|
+
return null;
|
|
314
|
+
},
|
|
315
|
+
edit(r) {
|
|
316
|
+
const [c, m] = r.oneof([1, 2, 3, 4, 6, 7]);
|
|
317
|
+
if (c === 1) return ok(joinOut(m.str(8), m.str(5)));
|
|
318
|
+
if (c === 2) return err(`file not found: ${m.str(1)}`);
|
|
319
|
+
if (c === 3) return denied(`read permission denied: ${m.str(1)}`);
|
|
320
|
+
if (c === 4) return denied(m.str(2) || `write permission denied: ${m.str(1)}`);
|
|
321
|
+
if (c === 6) return denied(m.str(2));
|
|
322
|
+
if (c === 7) return err(m.str(2));
|
|
323
|
+
return null;
|
|
324
|
+
},
|
|
325
|
+
glob(r) { const [c, m] = r.oneof([1, 2]); return c === 1 ? ok(m.strs(3).join('\n')) : c === 2 ? err(m.str(1)) : null; },
|
|
326
|
+
grep(r) {
|
|
327
|
+
const [c, m] = r.oneof([1, 2]);
|
|
328
|
+
if (c === 2) return err(m.str(1));
|
|
329
|
+
if (c !== 1) return null;
|
|
330
|
+
// GrepSuccess #4 workspace_results map<workspace, GrepUnionResult>, #5 active_editor_result. The
|
|
331
|
+
// content mode (union #3, observed): files {#1 path, #2 matches {#1 line, #2 text}}. The other
|
|
332
|
+
// modes' shapes were never observed, so their text is not decoded.
|
|
333
|
+
const lines = [];
|
|
334
|
+
let seen = false;
|
|
335
|
+
const union = (u) => {
|
|
336
|
+
const ct = u && u.msg(3);
|
|
337
|
+
if (!ct) return;
|
|
338
|
+
seen = true;
|
|
339
|
+
for (const f of ct.msgs(1)) for (const ln of f.msgs(2)) lines.push(`${f.str(1)}:${ln.num(1) ?? ''}:${ln.str(2)}`);
|
|
340
|
+
};
|
|
341
|
+
for (const e of m.msgs(4)) union(e.msg(2));
|
|
342
|
+
union(m.msg(5));
|
|
343
|
+
return seen ? ok(lines.join('\n')) : statusOnly(r);
|
|
344
|
+
},
|
|
345
|
+
task(r) {
|
|
346
|
+
const [c, m] = r.oneof([1, 2]);
|
|
347
|
+
if (c === 1) return ok(m.msgs(1).map((s) => (s.msg(1) ? s.msg(1).str(1) : '')).filter(Boolean).join('\n\n'));
|
|
348
|
+
return c === 2 ? err(m.str(1)) : null;
|
|
349
|
+
},
|
|
350
|
+
update_todos(r) {
|
|
351
|
+
const [c, m] = r.oneof([1, 2]);
|
|
352
|
+
if (c === 1) return ok(m.msgs(1).map((t) => `- [${TODO_STATUS[t.num(3) || 0] || t.num(3)}] ${t.str(2)}`).join('\n'));
|
|
353
|
+
return c === 2 ? err(m.str(1)) : null;
|
|
354
|
+
},
|
|
355
|
+
create_plan(r) { const [c, m] = r.oneof([2]); return c === 2 ? err(m.str(1)) : ok(r.str(3)); },
|
|
356
|
+
mcp(r) {
|
|
357
|
+
const [c, m] = r.oneof([1, 2, 3, 4, 5, 6, 7]);
|
|
358
|
+
if (c === 1) {
|
|
359
|
+
// McpSuccess #1 content items; a text item is {#1 {#1 text}} (UNVERIFIED: no MCP call was
|
|
360
|
+
// observed). Any other item is counted, never decoded.
|
|
361
|
+
const texts = [];
|
|
362
|
+
let other = 0;
|
|
363
|
+
for (const item of m.msgs(1)) { const t = item.msg(1); if (t && t.has(1)) texts.push(t.str(1)); else other++; }
|
|
364
|
+
if (other) texts.push(`[${other} non-text content item${other > 1 ? 's' : ''}]`);
|
|
365
|
+
return { output: texts.join('\n'), is_error: m.bool(2), status: 'completed' };
|
|
366
|
+
}
|
|
367
|
+
if (c === 2) return err(m.str(1));
|
|
368
|
+
if (c === 3 || c === 4) return denied(m.str(1));
|
|
369
|
+
if (c === 5) return err(`tool not found: ${m.str(1)}`);
|
|
370
|
+
if (c === 6) return err(`server not found: ${m.str(1)}`);
|
|
371
|
+
if (c === 7) return ok('approved');
|
|
372
|
+
return null;
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
/** A result whose text is not decoded: only which case is set (success is field 1 in every observed oneof). */
|
|
376
|
+
function statusOnly(r) { return { output: null, is_error: !r.has(1), status: 'completed' }; }
|
|
377
|
+
|
|
378
|
+
// ── the JSON model context ────────────────────────────────────────────────────────────────────
|
|
379
|
+
function jsonResultText(p) {
|
|
380
|
+
if (typeof p.result === 'string') return p.result;
|
|
381
|
+
if (Array.isArray(p.experimental_content)) return p.experimental_content.map((c) => (c && typeof c.text === 'string' ? c.text : '')).join('');
|
|
382
|
+
if (p.result != null) return JSON.stringify(p.result);
|
|
383
|
+
if (p.output != null) return typeof p.output === 'string' ? p.output : JSON.stringify(p.output);
|
|
384
|
+
return '';
|
|
385
|
+
}
|
|
386
|
+
function jsonUserText(m) {
|
|
387
|
+
if (typeof m.content === 'string') return m.content;
|
|
388
|
+
return (Array.isArray(m.content) ? m.content : []).map((p) => (p && p.type === 'text' && typeof p.text === 'string' ? p.text : '')).join('');
|
|
389
|
+
}
|
|
390
|
+
function imageBytes(img) {
|
|
391
|
+
if (img && typeof img === 'object' && typeof img.hex === 'string') return Buffer.from(img.hex, 'hex');
|
|
392
|
+
if (typeof img === 'string') { const m = /^data:[^;,]*;base64,(.*)$/s.exec(img); return Buffer.from(m ? m[1] : img, 'base64'); }
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Index the model-message JSON reachable from the root, in chronological order (archived messages
|
|
398
|
+
* first): the served model per requestId, tool calls and results per toolCallId, images per requestId.
|
|
399
|
+
*/
|
|
400
|
+
function jsonIndex(lists, json) {
|
|
401
|
+
const ix = { models: new Map(), calls: new Map(), results: new Map(), images: new Map(), lastReq: new Map(), reminders: new Map() };
|
|
402
|
+
let lastSeen = ''; // the latest requestId across the lists, which are in chronological order
|
|
403
|
+
for (const { key, ids } of lists) {
|
|
404
|
+
let req = '';
|
|
405
|
+
for (const id of ids) {
|
|
406
|
+
const m = json(id);
|
|
407
|
+
if (!m || typeof m !== 'object') continue;
|
|
408
|
+
const po = (m.providerOptions && m.providerOptions.cursor) || {};
|
|
409
|
+
const content = Array.isArray(m.content) ? m.content : [];
|
|
410
|
+
if (m.role === 'user') {
|
|
411
|
+
if (typeof po.requestId !== 'string' || !po.requestId) {
|
|
412
|
+
// One message per distinct blob: identical text is one blob, and the head of the context may
|
|
413
|
+
// be carried into an archive and the root alike, so a repeat is not told from a carry-over.
|
|
414
|
+
const text = jsonUserText(m);
|
|
415
|
+
if (/^\s*<system_reminder>/.test(text) && !ix.reminders.has(id)) ix.reminders.set(id, { text, after: lastSeen });
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
lastSeen = po.requestId;
|
|
419
|
+
req = po.requestId;
|
|
420
|
+
if (key) ix.lastReq.set(key, req);
|
|
421
|
+
if (ix.images.has(req)) continue;
|
|
422
|
+
const imgs = [];
|
|
423
|
+
for (const p of content) {
|
|
424
|
+
if (!p || p.type !== 'image') continue;
|
|
425
|
+
const bytes = imageBytes(p.image);
|
|
426
|
+
imgs.push({ media_type: p.mimeType || p.mediaType || '', att_sha: bytes ? sha256(bytes) : null, att_len: bytes ? bytes.length : null });
|
|
427
|
+
}
|
|
428
|
+
if (imgs.length) ix.images.set(req, imgs);
|
|
429
|
+
} else if (m.role === 'assistant') {
|
|
430
|
+
for (const p of content) {
|
|
431
|
+
if (!p || typeof p !== 'object') continue;
|
|
432
|
+
const mn = p.providerOptions && p.providerOptions.cursor && p.providerOptions.cursor.modelName;
|
|
433
|
+
if (req && typeof mn === 'string' && mn && !ix.models.has(req)) ix.models.set(req, mn);
|
|
434
|
+
if (p.type === 'tool-call' && typeof p.toolCallId === 'string') ix.calls.set(p.toolCallId, { name: p.toolName, args: p.args });
|
|
435
|
+
}
|
|
436
|
+
} else if (m.role === 'tool') {
|
|
437
|
+
const hl = po.highLevelToolCallResult && typeof po.highLevelToolCallResult === 'object' ? po.highLevelToolCallResult : null;
|
|
438
|
+
const out = hl && hl.output && typeof hl.output === 'object' && !Array.isArray(hl.output) ? Object.keys(hl.output)[0] : '';
|
|
439
|
+
for (const p of content) {
|
|
440
|
+
if (!p || p.type !== 'tool-result' || typeof p.toolCallId !== 'string') continue;
|
|
441
|
+
ix.results.set(p.toolCallId, { output: jsonResultText(p), is_error: hl && typeof hl.isError === 'boolean' ? hl.isError : null, outcome: out });
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return ix;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// ── normalise ─────────────────────────────────────────────────────────────────────────────────
|
|
450
|
+
/** The meta row, reduced to the keys this module uses: nothing else leaves the parser. */
|
|
451
|
+
function pickMeta(o) {
|
|
452
|
+
if (!o || typeof o !== 'object') return null;
|
|
453
|
+
const s = (v) => (typeof v === 'string' ? v : '');
|
|
454
|
+
const si = o.subagentInfo && typeof o.subagentInfo === 'object' ? o.subagentInfo : null;
|
|
455
|
+
const created = Number(o.createdAt);
|
|
456
|
+
return {
|
|
457
|
+
agentId: s(o.agentId), latestRootBlobId: s(o.latestRootBlobId).toLowerCase(), name: s(o.name), lastUsedModel: s(o.lastUsedModel),
|
|
458
|
+
createdAt: Number.isFinite(created) && created > 0 ? created : null,
|
|
459
|
+
subagentInfo: si ? { parentAgentId: s(si.parentAgentId), toolCallId: s(si.toolCallId), typeName: s(si.typeName) } : null,
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
function parseMetaValue(v) {
|
|
463
|
+
const t = textOf(v);
|
|
464
|
+
if (typeof t !== 'string' || !t) return null;
|
|
465
|
+
try {
|
|
466
|
+
const json = /^(?:[0-9a-fA-F]{2})+$/.test(t) ? Buffer.from(t, 'hex').toString('utf8') : t;
|
|
467
|
+
return JSON.parse(json);
|
|
468
|
+
} catch { return null; }
|
|
469
|
+
}
|
|
470
|
+
function readMetaJsonCwd(dir) {
|
|
471
|
+
try { const j = JSON.parse(fs.readFileSync(path.join(dir, 'meta.json'), 'utf-8')); return typeof j.cwd === 'string' ? j.cwd : ''; } catch { return ''; }
|
|
472
|
+
}
|
|
473
|
+
function uriPath(u) {
|
|
474
|
+
if (!u) return '';
|
|
475
|
+
if (!u.startsWith('file:')) return u;
|
|
476
|
+
try { return fileURLToPath(u); } catch { return u; }
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Normalise one session. `session` is a list() entry, or `{ session_id, meta, blobs }` already loaded
|
|
481
|
+
* (tests): `meta` the meta-row object, `blobs` a Map (or object) of hex id → bytes; optionally
|
|
482
|
+
* `bucket` and `meta_cwd` (meta.json's cwd). Returns the log records, or null when the store cannot
|
|
483
|
+
* be read consistently this pass.
|
|
484
|
+
*/
|
|
485
|
+
function normalise(session) {
|
|
486
|
+
if (session.blobs) {
|
|
487
|
+
const b = session.blobs;
|
|
488
|
+
const m = b instanceof Map ? b : new Map(Object.entries(b));
|
|
489
|
+
const scan = (fn) => { for (const [id, data] of m) { const buf = Buffer.from(data); if (buf[0] !== 0x7b) fn(id, buf); } };
|
|
490
|
+
return build(session, pickMeta(session.meta), (id) => m.get(id), scan);
|
|
491
|
+
}
|
|
492
|
+
let db;
|
|
493
|
+
try { db = openReadOnly(session.db); } catch { return null; }
|
|
494
|
+
try {
|
|
495
|
+
db.exec('BEGIN');
|
|
496
|
+
const row = db.prepare("SELECT value FROM meta WHERE key = '0'").get();
|
|
497
|
+
const meta = pickMeta(parseMetaValue(row && row.value));
|
|
498
|
+
const stmt = db.prepare('SELECT data FROM blobs WHERE id = ?');
|
|
499
|
+
// A read error (a busy or locked store above all) must not read as a missing blob: some callers
|
|
500
|
+
// are lenient (a JSON blob, a result's content blob), so it is latched and the pass fails after.
|
|
501
|
+
let failed = null;
|
|
502
|
+
const get = (id) => {
|
|
503
|
+
try { const r = stmt.get(id); return r && r.data != null ? bytesOf(r.data) : undefined; } catch (e) { failed = failed || e; throw e; }
|
|
504
|
+
};
|
|
505
|
+
// Every protobuf blob, live or orphaned (JSON model messages, which start with '{', are skipped).
|
|
506
|
+
const all = db.prepare("SELECT id, data FROM blobs WHERE substr(data, 1, 1) <> X'7B'");
|
|
507
|
+
const scan = (fn) => { for (const r of all.iterate()) if (r.data != null) fn(String(r.id), bytesOf(r.data)); };
|
|
508
|
+
const recs = build({ ...session, meta_cwd: session.bucket ? readMetaJsonCwd(session.dir) : '' }, meta, get, scan);
|
|
509
|
+
if (failed) throw failed;
|
|
510
|
+
return recs;
|
|
511
|
+
} finally {
|
|
512
|
+
try { db.exec('COMMIT'); } catch { /* nothing to end */ }
|
|
513
|
+
db.close();
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// UserMessage #9 simulated_msg_reason (agent.v1 SimulatedMsgReason). A simulated message is the
|
|
518
|
+
// harness's, except where the text is the user's own; a commit reminder is injected.
|
|
519
|
+
const SIM_USER = new Set([9, 10, 11, 35]); // USER_SENT/INTERRUPTED/QUEUED_TO_SUBAGENT, INTERACTION_RESPONSE
|
|
520
|
+
const SIM_INJECTED = new Set([2]); // COMMIT_REMINDER
|
|
521
|
+
/** The user message's `source`, or '' for the user's own words. */
|
|
522
|
+
function userSource(um, inSubagent, sid) {
|
|
523
|
+
if (um && um.bool(5)) {
|
|
524
|
+
const r = um.num(9) || 0;
|
|
525
|
+
if (SIM_USER.has(r)) return '';
|
|
526
|
+
return SIM_INJECTED.has(r) ? 'injected' : 'automated';
|
|
527
|
+
}
|
|
528
|
+
const by = um ? um.str(27) : ''; // sent_by_agent_id (source-only)
|
|
529
|
+
if (by && by !== sid) return 'automated';
|
|
530
|
+
return inSubagent ? 'automated' : ''; // a sub-agent's prompt is its parent's
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function build(session, meta, get, scan) {
|
|
534
|
+
if (!meta) return null;
|
|
535
|
+
try { return derive(session, meta, get, scan); } catch (e) {
|
|
536
|
+
if (e instanceof MissingBlob || e instanceof Malformed) return null;
|
|
537
|
+
throw e;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function derive(session, meta, get, scan) {
|
|
542
|
+
const sid = String(session.session_id);
|
|
543
|
+
const blob = (id) => { const b = get(id); if (b == null) throw new MissingBlob(id); return Buffer.isBuffer(b) ? b : Buffer.from(b); };
|
|
544
|
+
const pbCache = new Map();
|
|
545
|
+
const pb = (id) => { if (!pbCache.has(id)) pbCache.set(id, new Msg(blob(id))); return pbCache.get(id); };
|
|
546
|
+
const jsonCache = new Map();
|
|
547
|
+
const json = (id) => {
|
|
548
|
+
if (!jsonCache.has(id)) { let v = null; try { v = JSON.parse(blob(id).toString('utf8')); } catch (e) { if (e instanceof MissingBlob) throw e; } jsonCache.set(id, v); }
|
|
549
|
+
return jsonCache.get(id);
|
|
550
|
+
};
|
|
551
|
+
const blobText = (id) => { const b = get(id); return b == null ? '' : Buffer.from(b).toString('utf8'); };
|
|
552
|
+
const blobLen = (id) => { const b = get(id); return b == null ? null : b.length; };
|
|
553
|
+
|
|
554
|
+
const rootId = meta.latestRootBlobId;
|
|
555
|
+
const root = !rootId || rootId === EMPTY_ROOT ? new Msg(EMPTY) : pb(rootId);
|
|
556
|
+
|
|
557
|
+
// Summary archives, and the JSON lists they and the root reach.
|
|
558
|
+
const archives = [...new Set([...root.refs(13), ...(root.has(11) ? [root.ref(11)] : [])])];
|
|
559
|
+
const lists = [];
|
|
560
|
+
for (const a of archives) {
|
|
561
|
+
const am = pb(a);
|
|
562
|
+
lists.push({ key: a, ids: [...am.refs(1), ...(am.has(4) ? [am.ref(4)] : [])] });
|
|
563
|
+
}
|
|
564
|
+
lists.push({ key: '', ids: root.refs(1) });
|
|
565
|
+
const J = jsonIndex(lists, json);
|
|
566
|
+
|
|
567
|
+
// Turns, decoded up front: summaries and rewinds are placed relative to them.
|
|
568
|
+
const turns = [];
|
|
569
|
+
for (const id of root.refs(8)) {
|
|
570
|
+
const t = tryMsg(blob(id));
|
|
571
|
+
if (!t) continue; // a malformed turn is skipped, always the same way
|
|
572
|
+
const agent = t.has(1) ? t.msg(1) : null;
|
|
573
|
+
const shell = !agent && t.has(2) ? t.msg(2) : null;
|
|
574
|
+
if (!agent && !shell) continue;
|
|
575
|
+
const umBlob = agent && agent.has(1) ? agent.ref(1) : '';
|
|
576
|
+
const um = umBlob ? tryMsg(blob(umBlob)) : null;
|
|
577
|
+
turns.push({ agent, shell, rid: agent ? agent.str(3) : '', umBlob, um, snap: um ? um.ref(10) : '' });
|
|
578
|
+
}
|
|
579
|
+
const rids = new Set(turns.map((t) => t.rid).filter(Boolean));
|
|
580
|
+
const summaries = new Map(); // request_id ('' = no anchor) → [archive id]
|
|
581
|
+
for (const a of archives) {
|
|
582
|
+
const r = J.lastReq.get(a) || '';
|
|
583
|
+
const k = rids.has(r) ? r : '';
|
|
584
|
+
if (!summaries.has(k)) summaries.set(k, []);
|
|
585
|
+
summaries.get(k).push(a);
|
|
586
|
+
}
|
|
587
|
+
const rewinds = findRewinds(scan, turns, rootId);
|
|
588
|
+
// Reminders by the request before them: '' = before the first turn, a dead request = at the end.
|
|
589
|
+
const reminders = new Map();
|
|
590
|
+
for (const [id, r] of J.reminders) {
|
|
591
|
+
const k = !r.after ? '^' : rids.has(r.after) ? r.after : '';
|
|
592
|
+
if (!reminders.has(k)) reminders.set(k, []);
|
|
593
|
+
reminders.get(k).push({ id, text: r.text });
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const messages = [];
|
|
597
|
+
const children = [];
|
|
598
|
+
const addChild = (id, after = null) => { if (id && id !== sid && !children.some((c) => c.session_id === id)) children.push({ kind: 'subagent', session_id: id, evidence: 'explicit', after }); };
|
|
599
|
+
let order = 0;
|
|
600
|
+
let held = false;
|
|
601
|
+
let anchor = sid;
|
|
602
|
+
let anchorN = 0;
|
|
603
|
+
let lastId = null;
|
|
604
|
+
const push = (m) => { messages.push(m); lastId = m.message_id; };
|
|
605
|
+
const pushSummaries = (key) => {
|
|
606
|
+
for (const a of summaries.get(key) || []) {
|
|
607
|
+
push({
|
|
608
|
+
k: 'message', message_id: a, role: 'system', ts: null, order: order++, agent_id: '', state: 'current', status: 'complete', hidden: true,
|
|
609
|
+
parts: [{ part_id: '0', kind: 'summary', text: pb(a).str(2), replaces: { archive_ref: a } }],
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
const pushReminders = (key) => {
|
|
614
|
+
for (const r of reminders.get(key) || []) {
|
|
615
|
+
push({
|
|
616
|
+
k: 'message', message_id: r.id, role: 'system', ts: null, order: order++, agent_id: '', state: 'current', status: 'complete', hidden: true, source: 'injected',
|
|
617
|
+
parts: [{ part_id: '0', kind: 'text', text: r.text }],
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
const pushRewinds = (snap) => {
|
|
622
|
+
for (const r of rewinds.get(snap) || []) messages.push({ k: 'lifecycle', event: 'rewind', at: null, message_ids: r.message_ids, through: lastId });
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
for (let i = 0; i < turns.length && !held; i++) {
|
|
626
|
+
const last = i === turns.length - 1;
|
|
627
|
+
const { agent, shell, rid, um, snap } = turns[i];
|
|
628
|
+
if (snap) pushRewinds(snap);
|
|
629
|
+
if (i === 0) pushReminders('^');
|
|
630
|
+
|
|
631
|
+
if (shell) {
|
|
632
|
+
const mid = `${anchor}+${++anchorN}`;
|
|
633
|
+
const done = shell.has(2);
|
|
634
|
+
if (!done && last) { held = true; break; } // the command is still running
|
|
635
|
+
const cmdMsg = shell.has(1) ? tryMsg(blob(shell.ref(1))) : null;
|
|
636
|
+
const cmd = cmdMsg ? cmdMsg.str(1) : '';
|
|
637
|
+
const out = done ? (tryMsg(blob(shell.ref(2))) || new Msg(EMPTY)) : null;
|
|
638
|
+
const exit = out ? (out.num(3) || 0) : null;
|
|
639
|
+
const part = {
|
|
640
|
+
part_id: '0', kind: 'tool', call_id: `@${mid}:0`, tool: 'Shell', args: { command: cmd },
|
|
641
|
+
output: out ? joinOut(out.str(1), out.str(2)) : null, is_error: out ? exit !== 0 : false, status: out ? 'completed' : 'interrupted',
|
|
642
|
+
};
|
|
643
|
+
if (out) part.exit_code = exit;
|
|
644
|
+
push({ k: 'message', message_id: mid, role: 'user', ts: null, order: order++, agent_id: '', state: 'current', status: done ? 'complete' : 'interrupted', parts: [part] });
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// Agent turn: the user message, then the reply.
|
|
649
|
+
const nativeUser = (um && um.str(2)) || agent.str(10);
|
|
650
|
+
const userId = nativeUser || (rid ? `${rid}:user` : `${anchor}+${++anchorN}`);
|
|
651
|
+
const uparts = [];
|
|
652
|
+
if (um && um.str(1)) uparts.push({ part_id: '0', kind: 'text', text: um.str(1) });
|
|
653
|
+
(J.images.get(rid) || []).forEach((im, k) => uparts.push({ part_id: `att:${k}`, kind: 'file', media_type: im.media_type, att_sha: im.att_sha, att_len: im.att_len, ref: '' }));
|
|
654
|
+
const source = userSource(um, !!(meta.subagentInfo && meta.subagentInfo.parentAgentId), sid);
|
|
655
|
+
push({ k: 'message', message_id: userId, role: 'user', ts: (um && um.num(25)) || null, order: order++, agent_id: '', state: 'current', status: 'complete', ...(source ? { source } : {}), parts: uparts });
|
|
656
|
+
if (nativeUser || rid) { anchor = nativeUser || rid; anchorN = 0; }
|
|
657
|
+
|
|
658
|
+
const assistantId = `${rid || userId}:assistant`;
|
|
659
|
+
const steps = agent.refs(2);
|
|
660
|
+
const parts = [];
|
|
661
|
+
let running = false;
|
|
662
|
+
let interrupted = false;
|
|
663
|
+
steps.forEach((id, j) => {
|
|
664
|
+
const s = tryMsg(blob(id));
|
|
665
|
+
const partId = String(j);
|
|
666
|
+
if (!s) { parts.push({ part_id: partId, kind: 'notice', text: 'undecodable step' }); return; }
|
|
667
|
+
const am = s.has(1) ? s.msg(1) : null;
|
|
668
|
+
const th = s.has(3) ? s.msg(3) : null;
|
|
669
|
+
const tc = s.has(2) ? s.msg(2) : null;
|
|
670
|
+
if (am || th) {
|
|
671
|
+
const m = am || th;
|
|
672
|
+
// completed_at_ms is no streaming signal: on the VM's finished stores the last text step of
|
|
673
|
+
// every model response has started_at_ms and never completed_at_ms.
|
|
674
|
+
const st = am ? m.num(2) : m.num(3);
|
|
675
|
+
const text = m.str(1);
|
|
676
|
+
const part = am ? { part_id: partId, kind: 'text', text } : text ? { part_id: partId, kind: 'reasoning', text } : { part_id: partId, kind: 'reasoning', text: '', opaque: true };
|
|
677
|
+
if (st) part.ts = st;
|
|
678
|
+
parts.push(part);
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
if (tc) {
|
|
682
|
+
const { part, child } = toolPart(tc, partId, `@${assistantId}:${j}`, J, blobText, blobLen);
|
|
683
|
+
if (part.status === 'interrupted') { if (last) running = true; else interrupted = true; }
|
|
684
|
+
addChild(child, assistantId);
|
|
685
|
+
parts.push(part);
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
const fn = [...s.f.keys()][0];
|
|
689
|
+
parts.push({ part_id: partId, kind: 'notice', text: `unknown step kind #${fn ?? '?'}` });
|
|
690
|
+
});
|
|
691
|
+
if (last && (running || !steps.length)) { held = true; break; } // the reply is not finished
|
|
692
|
+
const ts = parts.find((p) => p.ts)?.ts ?? null;
|
|
693
|
+
push({
|
|
694
|
+
k: 'message', message_id: assistantId, role: 'assistant', ts, order: order++, parent_id: userId, agent_id: '', state: 'current',
|
|
695
|
+
status: interrupted || !steps.length ? 'interrupted' : 'complete',
|
|
696
|
+
model: J.models.get(rid) || agent.str(7) || '', model_requested: meta.lastUsedModel || '', effort: '', usage: null, parts,
|
|
697
|
+
});
|
|
698
|
+
if (rid) { pushSummaries(rid); pushReminders(rid); }
|
|
699
|
+
}
|
|
700
|
+
if (!held) { if (!turns.length) pushReminders('^'); pushSummaries(''); pushReminders(''); if (rootId) pushRewinds(rootId); }
|
|
701
|
+
|
|
702
|
+
// The parent names its sub-agents: the Task result's agent_id, subagent_states, subagent runs.
|
|
703
|
+
for (const e of root.msgs(16)) addChild(e.str(1));
|
|
704
|
+
for (const e of root.msgs(30)) { const run = e.msg(2); if (run) addChild(run.str(2)); }
|
|
705
|
+
|
|
706
|
+
let cwd = uriPath(root.strs(9)[0] || '');
|
|
707
|
+
if (!cwd && session.meta_cwd && session.bucket && md5(session.meta_cwd) === session.bucket) cwd = session.meta_cwd;
|
|
708
|
+
const links = [];
|
|
709
|
+
const si = meta.subagentInfo;
|
|
710
|
+
if (si && si.parentAgentId) links.push({ kind: 'subagent', origin: si.parentAgentId, evidence: 'explicit', at: si.toolCallId || '' });
|
|
711
|
+
const head = {
|
|
712
|
+
k: 'session',
|
|
713
|
+
title: meta.name || '',
|
|
714
|
+
cwd,
|
|
715
|
+
created: meta.createdAt || root.num(26) || null,
|
|
716
|
+
updated: null, archived: null,
|
|
717
|
+
version: '',
|
|
718
|
+
incarnation: '',
|
|
719
|
+
client: root.str(22),
|
|
720
|
+
links,
|
|
721
|
+
children,
|
|
722
|
+
};
|
|
723
|
+
return placeChildren([head, ...messages]);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Rewinds, from orphans. A rewind moves latestRootBlobId back to the snapshot a turn was sent from
|
|
728
|
+
* (UserMessage #10 conversation_state_blob_id) and writes nothing; the cut turns stay as blobs, which
|
|
729
|
+
* are never deleted. So an orphaned user message whose snapshot is still in the tree (a live turn's
|
|
730
|
+
* snapshot, or the root itself) marks a cut at that point, permanently. Returns snapshot → records.
|
|
731
|
+
* A Ctrl-C retraction or a stale concurrent writer re-pointing the root looks the same (UNVERIFIED).
|
|
732
|
+
*/
|
|
733
|
+
function findRewinds(scan, turns, rootId) {
|
|
734
|
+
const out = new Map();
|
|
735
|
+
const snaps = new Set(turns.map((t) => t.snap).filter(Boolean));
|
|
736
|
+
if (rootId) snaps.add(rootId);
|
|
737
|
+
if (!scan || !snaps.size) return out;
|
|
738
|
+
const liveBlobs = new Set(turns.map((t) => t.umBlob).filter(Boolean));
|
|
739
|
+
const liveUsers = new Set(turns.map((t) => (t.um && t.um.str(2)) || (t.agent && t.agent.str(10))).filter(Boolean));
|
|
740
|
+
const liveRids = new Set(turns.map((t) => t.rid).filter(Boolean));
|
|
741
|
+
const orphans = new Map(); // user-message blob id → { snap, uid, ts, rid }
|
|
742
|
+
const SNAP = Buffer.from([0x52, 0x20]); // UserMessage #10, 32 bytes
|
|
743
|
+
scan((id, buf) => {
|
|
744
|
+
if (liveBlobs.has(id)) return;
|
|
745
|
+
for (let at = buf.indexOf(SNAP); at >= 0 && at + 34 <= buf.length; at = buf.indexOf(SNAP, at + 1)) {
|
|
746
|
+
if (!snaps.has(buf.toString('hex', at + 2, at + 34))) continue;
|
|
747
|
+
const m = tryMsg(buf);
|
|
748
|
+
const snap = m ? m.ref(10) : '';
|
|
749
|
+
if (snap && snaps.has(snap) && !liveUsers.has(m.str(2))) orphans.set(id, { snap, uid: m.str(2), ts: m.num(25) || 0, rid: '' });
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
if (!orphans.size) return out;
|
|
754
|
+
// Their turns, for the request id: an agent turn starts with #1 user_message (0a 20 <32 bytes>).
|
|
755
|
+
const UM = Buffer.from([0x0a, 0x20]);
|
|
756
|
+
scan((id, buf) => {
|
|
757
|
+
for (let at = buf.indexOf(UM); at >= 0 && at + 34 <= buf.length; at = buf.indexOf(UM, at + 1)) {
|
|
758
|
+
const o = orphans.get(buf.toString('hex', at + 2, at + 34));
|
|
759
|
+
if (!o) continue;
|
|
760
|
+
const t = tryMsg(buf);
|
|
761
|
+
const a = t && t.has(1) ? tryMsg(t.last(1).v) : null;
|
|
762
|
+
if (a && orphans.get(a.ref(1)) === o && a.str(3)) o.rid = a.str(3);
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
});
|
|
766
|
+
const rows = [...orphans.values()].filter((o) => (o.uid || o.rid) && !liveRids.has(o.rid));
|
|
767
|
+
rows.sort((x, y) => x.ts - y.ts || (x.uid + x.rid).localeCompare(y.uid + y.rid));
|
|
768
|
+
for (const o of rows) {
|
|
769
|
+
const ids = [o.uid || `${o.rid}:user`];
|
|
770
|
+
if (o.rid) ids.push(`${o.rid}:assistant`);
|
|
771
|
+
if (!out.has(o.snap)) out.set(o.snap, []);
|
|
772
|
+
out.get(o.snap).push({ message_ids: ids });
|
|
773
|
+
}
|
|
774
|
+
return out;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/** One tool step → one log part (call and result together), and the sub-agent it started, if any. */
|
|
778
|
+
function toolPart(tc, partId, fallbackCall, J, blobText, blobLen) {
|
|
779
|
+
const callId = tc.has(57) ? tc.str(57) : fallbackCall;
|
|
780
|
+
let c = 0;
|
|
781
|
+
for (const fn of tc.f.keys()) if (!COMMON.has(fn)) { c = fn; break; }
|
|
782
|
+
const kind = KINDS[c] || `tool_${c}`;
|
|
783
|
+
const K = c ? tryMsg(tc.last(c).w === 2 ? tc.last(c).v : EMPTY) : null;
|
|
784
|
+
const [af, rf] = c === 34 ? [2, 3] : [1, 2];
|
|
785
|
+
const argsMsg = K && K.has(af) && K.last(af).w === 2 ? tryMsg(K.last(af).v) : null;
|
|
786
|
+
const resMsg = K && K.has(rf) ? tryMsg(K.last(rf).v) : null;
|
|
787
|
+
const hasResult = !!(resMsg && resMsg.size);
|
|
788
|
+
|
|
789
|
+
// From the step: a typed projection where the kind has one (allowlisted names), else nothing.
|
|
790
|
+
const secret = SENSITIVE.has(kind);
|
|
791
|
+
let name = NAMES[kind] || pascal(kind);
|
|
792
|
+
let args = {};
|
|
793
|
+
if (argsMsg && !secret && ARGS[kind]) {
|
|
794
|
+
try { args = allowArgs(ARGS[kind](argsMsg)); } catch { args = {}; }
|
|
795
|
+
}
|
|
796
|
+
let res = null;
|
|
797
|
+
let child = '';
|
|
798
|
+
if (hasResult && (secret || !RESULTS[kind])) {
|
|
799
|
+
// A credential-bearing or untyped kind: only which result case is set, never its text. The model
|
|
800
|
+
// context below supplies the output the model saw, if any.
|
|
801
|
+
res = statusOnly(resMsg);
|
|
802
|
+
} else if (hasResult) {
|
|
803
|
+
try { res = RESULTS[kind](resMsg, blobText, blobLen) || statusOnly(resMsg); } catch { res = statusOnly(resMsg); }
|
|
804
|
+
if (kind === 'task' && resMsg.has(1)) { try { child = resMsg.msg(1).str(2); } catch { child = ''; } }
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// From the model context, where the call is in it: what the model sent and was sent back.
|
|
808
|
+
const jc = J.calls.get(callId);
|
|
809
|
+
if (jc) {
|
|
810
|
+
name = typeof jc.name === 'string' && jc.name ? jc.name : name;
|
|
811
|
+
args = jc.args && typeof jc.args === 'object' ? jc.args : {};
|
|
812
|
+
if (name === 'CallDynamicTool' && typeof args.toolName === 'string' && args.toolName) {
|
|
813
|
+
name = args.toolName;
|
|
814
|
+
args = args.arguments && typeof args.arguments === 'object' ? args.arguments : (args.args && typeof args.args === 'object' ? args.args : {});
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
const jr = J.results.get(callId);
|
|
818
|
+
if (jr) {
|
|
819
|
+
res = {
|
|
820
|
+
...(res || {}),
|
|
821
|
+
output: jr.output,
|
|
822
|
+
is_error: typeof jr.is_error === 'boolean' ? jr.is_error : (res ? res.is_error : false),
|
|
823
|
+
status: res ? res.status : (jr.outcome === 'rejected' ? 'denied' : 'completed'),
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// A credential-bearing tool: the model context's copy is no safer than the step's. Its args keep
|
|
828
|
+
// only naming keys and its output is never written, whatever the source.
|
|
829
|
+
if (secret || SENSITIVE_NAMES.test(name)) {
|
|
830
|
+
args = Object.fromEntries(Object.entries(args || {})
|
|
831
|
+
.filter(([k]) => SENSITIVE_ARG_NAMES.has(k))
|
|
832
|
+
.map(([k, v]) => [k, sensitiveValue(v)])
|
|
833
|
+
.filter(([, v]) => v !== undefined));
|
|
834
|
+
if (res) res = { ...res, output: null };
|
|
835
|
+
}
|
|
836
|
+
const part = {
|
|
837
|
+
part_id: partId, kind: 'tool', call_id: callId, tool: name, args,
|
|
838
|
+
output: res ? res.output : null, is_error: res ? !!res.is_error : false, status: res ? res.status : 'interrupted',
|
|
839
|
+
};
|
|
840
|
+
if (res && typeof res.exit_code === 'number') part.exit_code = res.exit_code;
|
|
841
|
+
// The MCP server from the step itself: the JSON context names only the tool.
|
|
842
|
+
if (kind === 'mcp' && argsMsg) { try { const sv = argsMsg.str(9) || argsMsg.str(4); if (sv) part.server = sv; } catch { /* no server */ } }
|
|
843
|
+
if (res && res.output_ref) part.output_ref = res.output_ref;
|
|
844
|
+
// A truncated step keeps a preview; the full step is another blob, referenced by id and size
|
|
845
|
+
// (TruncatedToolCall #1, #4; source-only). Blob ids are sha256 of the bytes.
|
|
846
|
+
if (c === 34 && K && K.has(1)) part.output_ref = { sha: K.ref(1), len: K.num(4) };
|
|
847
|
+
const ts = tc.num(59);
|
|
848
|
+
if (ts) part.ts = ts;
|
|
849
|
+
return { part, child };
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
module.exports = { HARNESS, list, normalise, storeRootDefault, parseFields, Msg, EMPTY_ROOT };
|