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,910 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// v4 wave 1: agy (the Antigravity CLI) → normalised session log records (docs/design/sessions-log.md,
|
|
3
|
+
// plus the wave-1 format additions: model_requested, exit_code, hidden, children, lifecycle, state).
|
|
4
|
+
//
|
|
5
|
+
// Store: ~/.gemini/antigravity-cli/ (only HOME moves it; agy has no *_HOME variable).
|
|
6
|
+
// - conversations/<cascade>.db: SQLite in WAL mode, canonical. `steps` holds one protobuf
|
|
7
|
+
// gemini_coder.Step per `idx`; its metadata carries `model_api_content_id`, the 32-hex message key.
|
|
8
|
+
// `gen_metadata` (one row per generation) gives the served model; `executor_metadata` (one row per
|
|
9
|
+
// finished execution) says a turn ended; `parent_references` holds the fork link and
|
|
10
|
+
// `trajectory_metadata_blob` the sub-agent's parent.
|
|
11
|
+
// - brain/<cascade>/.system_generated/logs/transcript_full.jsonl: an append-only side source with no
|
|
12
|
+
// ids. With a DB it only supplies rewind events. Without one it is the source (sessions-model §1
|
|
13
|
+
// rules 3–4): ids are offsets, `@<byte offset of the line>`, and a step_index not above a kept
|
|
14
|
+
// line's retracts the kept lines from that index on (a rewind, the lines stay: state retracted).
|
|
15
|
+
// - conversation_summaries.db: the index (title, parent_conversation_id), shared with the hub and IDE.
|
|
16
|
+
// - annotations/<cascade>.pbtxt: title and archived flag.
|
|
17
|
+
// Spec: docs/design/adapter-specs/agy.md (repository), sessions-model v0 and its annex.
|
|
18
|
+
//
|
|
19
|
+
// Rules from the spec:
|
|
20
|
+
// - message = step; id = model_api_content_id, never idx (a rewind hard-deletes rows and reuses idx,
|
|
21
|
+
// which reaches the log as a rewrite). A step without one gets an anchored id (<prev id>+n);
|
|
22
|
+
// - order = idx; ts = metadata.created_at;
|
|
23
|
+
// - PLANNER_RESPONSE: response, thinking (opaque when only a signature is stored) and tool_calls[]
|
|
24
|
+
// (`call_N`); each call's GENERIC step (metadata.tool_call.id) is its result, folded into the
|
|
25
|
+
// planner's tool part (the log keeps call and result in one part). A GENERIC no planner claims is a
|
|
26
|
+
// message of its own with role tool. A RunCommand payload gives `exit_code`;
|
|
27
|
+
// - statuses: DONE 3, CANCELED 6, ERROR 7 (denied when the error says so), INTERRUPTED 12. PENDING 1,
|
|
28
|
+
// RUNNING 2, GENERATING 8, WAITING 9 and QUEUED 11 are open: an open step is held back, and a
|
|
29
|
+
// planner with an open or missing tool result is held back until the result is final or the turn
|
|
30
|
+
// (execution) has ended. A held message stops the log there: nothing after it is written until it
|
|
31
|
+
// is, so its release is an append (order = idx). After the turn ends the call is written as
|
|
32
|
+
// running/waiting (a background task) or interrupted (no result step), and a step still open (a
|
|
33
|
+
// crash left it so) is written as interrupted, so a dead step never stalls the log;
|
|
34
|
+
// - CHECKPOINT: a summary part replacing through `included_step_index_end`, and a compaction
|
|
35
|
+
// lifecycle record whose `count` is the steps it covers (included_step_indices, else the index
|
|
36
|
+
// range); a DB rewind's `count` is the transcript lines it cut;
|
|
37
|
+
// - SYSTEM_MESSAGE and EPHEMERAL_MESSAGE: system notices (EPHEMERAL is persisted despite its name);
|
|
38
|
+
// `hidden` where the step says so (render_info.hidden, agent_message.hide_from_user);
|
|
39
|
+
// - source: EPHEMERAL (a hook's ephemeralMessage) and a SYSTEM_MESSAGE from the system or a hook
|
|
40
|
+
// (restart notice, SessionStart) are `injected`; a SYSTEM_MESSAGE that starts a turn for the
|
|
41
|
+
// agent (task_notification: a background task or /schedule timer done; agent_message from a
|
|
42
|
+
// parent or sub-agent) is `automated`, as is a USER_INPUT whose step source is SYSTEM/SYSTEM_SDK;
|
|
43
|
+
// - format v3 on tool parts: `content_id` = the folded GENERIC step's model_api_content_id; `server`
|
|
44
|
+
// from the result's genai McpServerToolCall/Result step or a nested legacy MCP_TOOL step
|
|
45
|
+
// (source-only: agy 1.4.4 never ran an MCP tool); `output_ref` {sha, len} of
|
|
46
|
+
// brain/<id>/.system_generated/steps/<idx>/output.txt when the step's text is shorter than that
|
|
47
|
+
// file (agy cut it: "The tool's output was truncated…"); the file is read only then, and only
|
|
48
|
+
// from this conversation's own steps folder;
|
|
49
|
+
// - model: gen_metadata.chat_model.response_model, else the model enum name kept verbatim;
|
|
50
|
+
// model_requested: the requested/generator enum name (MODEL_PLACEHOLDER_M418) kept verbatim;
|
|
51
|
+
// effort is folded into the model and left unparsed;
|
|
52
|
+
// - usage: metadata.model_usage per generation (else the gen row's), basis unknown; a stored zero
|
|
53
|
+
// is null (proto3 cannot tell zero from unset); credits from chat_model.credit_cost;
|
|
54
|
+
// - links, child-side and explicit only: fork from parent_references (FORK), at = the copied step at
|
|
55
|
+
// the fork point, through = the last copied step (copies keep the origin's cascade_id and ids);
|
|
56
|
+
// subagent from trajectory_metadata_blob / summaries parent_conversation_id, at = the parent's
|
|
57
|
+
// invoke_subagent call id carried by the prompt the child received. Origin side: `children` from
|
|
58
|
+
// the parent's invoke_subagent results;
|
|
59
|
+
// - redaction: the contract declares no secret path for agy conversation data. Signatures are never
|
|
60
|
+
// decoded (only their byte length is read). Credential files are never opened: this module reads
|
|
61
|
+
// conversations/, brain/<id>/…/transcript_full.jsonl, conversation_summaries.db and annotations/.
|
|
62
|
+
// A file part's URI other than file: or a path is cut to scheme://host (else one marker): its path
|
|
63
|
+
// or query may carry a token;
|
|
64
|
+
// - read failures: a step whose payload or metadata does not decode throws Undecodable (a skipped step
|
|
65
|
+
// loses a message and shifts the anchored ids after it; a fallback changes its id); a side file that
|
|
66
|
+
// is listed but cannot be read (anything but ENOENT) throws; a SQLite error other than a missing
|
|
67
|
+
// table or column throws. The runner retries a failed session.
|
|
68
|
+
|
|
69
|
+
const fs = require('fs');
|
|
70
|
+
const path = require('path');
|
|
71
|
+
const crypto = require('crypto');
|
|
72
|
+
const { placeChildren } = require('../sessions-log');
|
|
73
|
+
|
|
74
|
+
const HARNESS = 'agy';
|
|
75
|
+
const APP_DIR = 'antigravity-cli';
|
|
76
|
+
|
|
77
|
+
/** The data folder: harness-paths' (~/.gemini/antigravity-cli; only HOME moves it). */
|
|
78
|
+
function storeRootDefault() { return require('../harness-paths').dataFolder(HARNESS); }
|
|
79
|
+
|
|
80
|
+
// ── protobuf wire format ──────────────────────────────────────────────────────────────────────
|
|
81
|
+
// A schema maps a field number to [name, type, repeated]. Types: 's' string, 'b' bytes, 'len' the
|
|
82
|
+
// byte length only, 'u' unsigned, 'i' signed, 'e' enum, 'bool', 'f' float, 'ts' Timestamp → ms, or a
|
|
83
|
+
// nested schema. Unknown fields are skipped without decoding.
|
|
84
|
+
|
|
85
|
+
function toBuf(v) {
|
|
86
|
+
if (v == null) return Buffer.alloc(0);
|
|
87
|
+
if (Buffer.isBuffer(v)) return v;
|
|
88
|
+
if (v instanceof Uint8Array) return Buffer.from(v.buffer, v.byteOffset, v.byteLength);
|
|
89
|
+
if (typeof v === 'string') return Buffer.from(v, 'utf-8');
|
|
90
|
+
throw new Error('pb: not bytes');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** [value, next position]: a Number up to 49 bits, a BigInt beyond. */
|
|
94
|
+
function varint(buf, p) {
|
|
95
|
+
let n = 0; let mul = 1;
|
|
96
|
+
for (let i = 0; i < 7; i++) {
|
|
97
|
+
if (p >= buf.length) throw new Error('pb: truncated varint');
|
|
98
|
+
const b = buf[p++];
|
|
99
|
+
n += (b & 0x7f) * mul;
|
|
100
|
+
if (b < 0x80) return [n, p];
|
|
101
|
+
mul *= 128;
|
|
102
|
+
}
|
|
103
|
+
let big = BigInt(n); let shift = 49n;
|
|
104
|
+
for (let i = 7; i < 10; i++) {
|
|
105
|
+
if (p >= buf.length) throw new Error('pb: truncated varint');
|
|
106
|
+
const b = buf[p++];
|
|
107
|
+
big |= BigInt(b & 0x7f) << shift;
|
|
108
|
+
if (b < 0x80) return [BigInt.asUintN(64, big), p];
|
|
109
|
+
shift += 7n;
|
|
110
|
+
}
|
|
111
|
+
throw new Error('pb: varint too long');
|
|
112
|
+
}
|
|
113
|
+
const unsigned = (v) => (typeof v === 'bigint' ? Number(v) : v);
|
|
114
|
+
const signed = (v) => (typeof v === 'bigint' ? Number(BigInt.asIntN(64, v)) : v);
|
|
115
|
+
const PACKABLE = new Set(['u', 'i', 'e', 'bool']);
|
|
116
|
+
const TS = { 1: ['seconds', 'i'], 2: ['nanos', 'i'] };
|
|
117
|
+
|
|
118
|
+
function scalar(type, wt, val) {
|
|
119
|
+
switch (type) {
|
|
120
|
+
case 's': return wt === 2 ? val.toString('utf-8') : '';
|
|
121
|
+
case 'b': return wt === 2 ? val : null;
|
|
122
|
+
case 'len': return wt === 2 ? val.length : 0;
|
|
123
|
+
case 'u': return wt === 0 ? unsigned(val) : 0;
|
|
124
|
+
case 'i': case 'e': return wt === 0 ? signed(val) : 0;
|
|
125
|
+
case 'bool': return wt === 0 ? unsigned(val) !== 0 : false;
|
|
126
|
+
case 'f': return wt === 5 ? val.readFloatLE(0) : 0;
|
|
127
|
+
case 'ts': {
|
|
128
|
+
if (wt !== 2) return null;
|
|
129
|
+
const t = decode(val, TS);
|
|
130
|
+
return t.seconds > 0 ? t.seconds * 1000 + Math.floor((t.nanos || 0) / 1e6) : null;
|
|
131
|
+
}
|
|
132
|
+
default: return wt === 2 ? decode(val, type) : null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Decode a protobuf message with a schema; unknown fields are skipped. */
|
|
137
|
+
function decode(input, schema) {
|
|
138
|
+
const buf = toBuf(input);
|
|
139
|
+
const out = {};
|
|
140
|
+
let p = 0;
|
|
141
|
+
while (p < buf.length) {
|
|
142
|
+
let key;
|
|
143
|
+
[key, p] = varint(buf, p);
|
|
144
|
+
key = unsigned(key);
|
|
145
|
+
const no = Math.floor(key / 8);
|
|
146
|
+
const wt = key % 8;
|
|
147
|
+
let val;
|
|
148
|
+
if (wt === 0) [val, p] = varint(buf, p);
|
|
149
|
+
else if (wt === 2) {
|
|
150
|
+
let len;
|
|
151
|
+
[len, p] = varint(buf, p);
|
|
152
|
+
len = unsigned(len);
|
|
153
|
+
if (p + len > buf.length) throw new Error('pb: truncated field');
|
|
154
|
+
val = buf.subarray(p, p + len); p += len;
|
|
155
|
+
} else if (wt === 1) { if (p + 8 > buf.length) throw new Error('pb: truncated'); val = buf.subarray(p, p + 8); p += 8; }
|
|
156
|
+
else if (wt === 5) { if (p + 4 > buf.length) throw new Error('pb: truncated'); val = buf.subarray(p, p + 4); p += 4; }
|
|
157
|
+
else throw new Error(`pb: wire type ${wt}`);
|
|
158
|
+
const f = schema[no];
|
|
159
|
+
if (!f) continue;
|
|
160
|
+
const [name, type, rep] = f;
|
|
161
|
+
if (rep && wt === 2 && PACKABLE.has(type)) {
|
|
162
|
+
const arr = out[name] || (out[name] = []);
|
|
163
|
+
let q = 0;
|
|
164
|
+
while (q < val.length) { let v; [v, q] = varint(val, q); arr.push(scalar(type, 0, v)); }
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const v = scalar(type, wt, val);
|
|
168
|
+
if (rep) (out[name] || (out[name] = [])).push(v); else out[name] = v;
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Field numbers from the descriptors mined out of the agy 1.4.4 binary (fixtures
|
|
174
|
+
// agy/pass3/schema.proto-excerpt.txt, ag3-msg.py). Only the fields this module reads.
|
|
175
|
+
const S = {};
|
|
176
|
+
S.ChatToolCall = { 1: ['id', 's'], 2: ['name', 's'], 3: ['arguments_json', 's'] }; // 7 thinking_signature: never read
|
|
177
|
+
S.Usage = {
|
|
178
|
+
1: ['model', 'e'], 2: ['input_tokens', 'u'], 3: ['output_tokens', 'u'], 4: ['cache_write_tokens', 'u'],
|
|
179
|
+
5: ['cache_read_tokens', 'u'], 9: ['thinking_output_tokens', 'u'], 10: ['response_output_tokens', 'u'], 11: ['response_id', 's'],
|
|
180
|
+
};
|
|
181
|
+
S.StepInfo = { 1: ['trajectory_id', 's'], 2: ['step_index', 'u'], 3: ['metadata_index', 'u'], 4: ['cascade_id', 's'] };
|
|
182
|
+
S.RetryInfo = { 2: ['usage', S.Usage] };
|
|
183
|
+
S.ModelOrAlias = { 1: ['model', 'e'] };
|
|
184
|
+
S.Meta = {
|
|
185
|
+
1: ['created_at', 'ts'], 3: ['source', 'e'], 4: ['tool_call', S.ChatToolCall], 8: ['completed_at', 'ts'],
|
|
186
|
+
9: ['model_usage', S.Usage], 10: ['model_cost', 'f'], 11: ['generator_model', 'e'], 12: ['execution_id', 's'], 13: ['requested_model', S.ModelOrAlias],
|
|
187
|
+
20: ['source_trajectory_step_info', S.StepInfo], 28: ['retry_infos', S.RetryInfo, true], 35: ['model_api_content_id', 's'],
|
|
188
|
+
};
|
|
189
|
+
S.ImageData = { 1: ['base64_data', 's'], 2: ['mime_type', 's'], 4: ['uri', 's'] };
|
|
190
|
+
S.Media = { 1: ['mime_type', 's'], 2: ['inline_data', 'b'], 5: ['uri', 's'], 9: ['display_name', 's'] };
|
|
191
|
+
S.TextOrScopeItem = { 1: ['text', 's'] };
|
|
192
|
+
S.UserInput = { 1: ['query', 's'], 2: ['user_response', 's'], 3: ['items', S.TextOrScopeItem, true], 5: ['images', S.ImageData, true], 9: ['media', S.Media, true] };
|
|
193
|
+
S.Planner = {
|
|
194
|
+
1: ['response', 's'], 3: ['thinking', 's'], 4: ['signature_len', 'len'], 5: ['thinking_redacted', 'bool'],
|
|
195
|
+
6: ['message_id', 's'], 7: ['tool_calls', S.ChatToolCall, true], 8: ['modified_response', 's'], 14: ['thinking_signature_len', 'len'],
|
|
196
|
+
};
|
|
197
|
+
S.Checkpoint = {
|
|
198
|
+
1: ['checkpoint_index', 'u'], 3: ['included_step_indices', 'u', true], 5: ['session_summary', 's'],
|
|
199
|
+
11: ['included_step_index_start', 'u'], 12: ['included_step_index_end', 'u'],
|
|
200
|
+
};
|
|
201
|
+
S.ErrorDetails = { 1: ['user_error_message', 's'], 2: ['short_error', 's'], 3: ['full_error', 's'], 9: ['model_error_message', 's'] };
|
|
202
|
+
S.Ephemeral = { 1: ['content', 's'], 2: ['media', S.Media, true] };
|
|
203
|
+
S.ToolSource = { 1: ['conversation_id', 's'], 2: ['step_index', 'i'], 3: ['tool_call', S.ChatToolCall] };
|
|
204
|
+
S.SourceMetadata = { 1: ['tool', S.ToolSource] };
|
|
205
|
+
S.AgentMessage = { 1: ['id', 's'], 2: ['recipient', 's'], 3: ['sender', 's'], 4: ['content', 's'], 11: ['hide_from_user', 'bool'], 12: ['source_metadata', S.SourceMetadata] };
|
|
206
|
+
S.Any = { 1: ['type_url', 's'], 2: ['value', 'b'] };
|
|
207
|
+
S.RenderInfo = { 4: ['hidden', 'bool'] };
|
|
208
|
+
S.McpServerTool = { 1: ['name', 's'], 2: ['server_name', 's'] }; // genai.McpServerToolCallStep / ResultStep
|
|
209
|
+
S.ToolCallStep = { 6: ['mcp_server_tool_call', S.McpServerTool] };
|
|
210
|
+
S.ToolResultStep = { 7: ['mcp_server_tool_result', S.McpServerTool] };
|
|
211
|
+
S.GenericResult = {
|
|
212
|
+
1: ['result', 's'], 3: ['step_render_info', S.RenderInfo], 4: ['media', S.Media, true], 6: ['payload', S.Any], 7: ['full_output_uri', 's'],
|
|
213
|
+
8: ['tool_call_step', S.ToolCallStep], 9: ['tool_result_step', S.ToolResultStep],
|
|
214
|
+
};
|
|
215
|
+
// Tool payloads (google.protobuf.Any in GenericStepResult.payload).
|
|
216
|
+
S.RunCommand = { 1: ['exit_code', 'i'], 2: ['timed_out', 'bool'], 8: ['exit_code_unknown', 'bool'] };
|
|
217
|
+
S.SubagentResult = { 1: ['conversation_id', 's'] };
|
|
218
|
+
S.InvokeSubagent = { 2: ['results', S.SubagentResult, true] };
|
|
219
|
+
S.SystemMessage = { 1: ['message', 's'], 2: ['render_info', S.RenderInfo], 3: ['event_type', 's'], 4: ['agent_message', S.AgentMessage], 6: ['media', S.Media, true] };
|
|
220
|
+
S.Generic = { 2: ['result', S.GenericResult] };
|
|
221
|
+
S.TaskDetails = { 1: ['id', 's'], 4: ['description', 's'], 7: ['title', 's'] };
|
|
222
|
+
S.Step = {
|
|
223
|
+
1: ['type', 'e'], 4: ['status', 'e'], 5: ['metadata', S.Meta], 19: ['user_input', S.UserInput],
|
|
224
|
+
20: ['planner_response', S.Planner], 30: ['checkpoint', S.Checkpoint], 31: ['error', S.ErrorDetails],
|
|
225
|
+
103: ['ephemeral_message', S.Ephemeral], 114: ['system_message', S.SystemMessage], 140: ['generic', S.Generic],
|
|
226
|
+
148: ['task_details', S.TaskDetails],
|
|
227
|
+
};
|
|
228
|
+
S.McpTool = { 1: ['server_name', 's'] }; // CortexStepMcpTool, as a nested legacy step (source-only)
|
|
229
|
+
S.Step[47] = ['mcp_tool', S.McpTool];
|
|
230
|
+
S.ChatModel = { 3: ['model', 'e'], 4: ['usage', S.Usage], 5: ['model_cost', 'f'], 13: ['credit_cost', 'i'], 19: ['response_model', 's'], 21: ['model_display_name', 's'] };
|
|
231
|
+
S.GenMeta = { 1: ['chat_model', S.ChatModel], 2: ['step_indices', 'u', true], 4: ['execution_id', 's'] };
|
|
232
|
+
S.ExecMeta = { 1: ['termination_reason', 'e'], 3: ['last_step_idx', 'i'], 9: ['execution_id', 's'] };
|
|
233
|
+
S.Workspace = { 1: ['workspace_folder_absolute_uri', 's'], 2: ['git_root_absolute_uri', 's'], 4: ['branch_name', 's'] };
|
|
234
|
+
S.TrajMeta = {
|
|
235
|
+
1: ['workspaces', S.Workspace, true], 2: ['created_at', 'ts'], 5: ['parent_conversation_id', 's'],
|
|
236
|
+
6: ['root_conversation_id', 's'], 7: ['workspace_uris', 's', true], 17: ['nesting_depth', 'i'], 18: ['project_id', 's'],
|
|
237
|
+
};
|
|
238
|
+
S.TrajRef = { 1: ['trajectory_id', 's'], 2: ['step_index', 'i'], 3: ['trajectory_type', 'e'], 4: ['step_type', 'e'], 5: ['reference_type', 'e'], 6: ['conversation_id', 's'] };
|
|
239
|
+
|
|
240
|
+
function safeDecode(v, schema) { try { return v == null ? {} : decode(v, schema); } catch { return {}; } }
|
|
241
|
+
|
|
242
|
+
// ── store enums ───────────────────────────────────────────────────────────────────────────────
|
|
243
|
+
const T = { USER_INPUT: 14, PLANNER_RESPONSE: 15, GENERIC: 132, SYSTEM_MESSAGE: 101, EPHEMERAL_MESSAGE: 90, CHECKPOINT: 23 };
|
|
244
|
+
const ST = { PENDING: 1, RUNNING: 2, DONE: 3, CANCELED: 6, ERROR: 7, GENERATING: 8, WAITING: 9, QUEUED: 11, INTERRUPTED: 12 };
|
|
245
|
+
const OPEN = new Set([ST.PENDING, ST.RUNNING, ST.GENERATING, ST.WAITING, ST.QUEUED]);
|
|
246
|
+
const REF_FORK = 2;
|
|
247
|
+
const SRC = { SYSTEM: 5, SYSTEM_SDK: 6 }; // CortexStepSource
|
|
248
|
+
|
|
249
|
+
/** exa.codeium_common_pb.Model: 1000–2650 are MODEL_PLACEHOLDER_M<n-1000> (checked against the 1.4.4 enum). */
|
|
250
|
+
function modelName(v) {
|
|
251
|
+
const n = Number(v);
|
|
252
|
+
if (!Number.isFinite(n) || n <= 0) return '';
|
|
253
|
+
if (n >= 1000 && n <= 2650) return `MODEL_PLACEHOLDER_M${n - 1000}`;
|
|
254
|
+
return `model_enum:${n}`;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ── shared helpers ────────────────────────────────────────────────────────────────────────────
|
|
258
|
+
const sha256 = (buf) => crypto.createHash('sha256').update(buf).digest('hex');
|
|
259
|
+
const nz = (v) => (v == null || !Number.isFinite(Number(v)) || Number(v) === 0 ? null : Number(v));
|
|
260
|
+
const msOf = (iso) => { const t = Date.parse(iso); return Number.isFinite(t) ? t : null; };
|
|
261
|
+
function argsOf(a) {
|
|
262
|
+
if (a == null || a === '') return {};
|
|
263
|
+
if (typeof a !== 'string') return a;
|
|
264
|
+
try { return JSON.parse(a); } catch { return { raw: a }; }
|
|
265
|
+
}
|
|
266
|
+
function uriPath(u) {
|
|
267
|
+
const s = String(u || '');
|
|
268
|
+
if (!s.startsWith('file://')) return s;
|
|
269
|
+
try { return decodeURIComponent(new URL(s).pathname); } catch { return s; }
|
|
270
|
+
}
|
|
271
|
+
function firstJson(text) {
|
|
272
|
+
try { const v = JSON.parse(text || '[]'); return Array.isArray(v) ? v[0] || '' : ''; } catch { return ''; }
|
|
273
|
+
}
|
|
274
|
+
function usageOf(u, cost, credits) {
|
|
275
|
+
if (!u) return null;
|
|
276
|
+
const out = {
|
|
277
|
+
basis: 'unknown', input: nz(u.input_tokens), output: nz(u.output_tokens), cache_read: nz(u.cache_read_tokens),
|
|
278
|
+
cache_write: nz(u.cache_write_tokens), reasoning: nz(u.thinking_output_tokens), cost: nz(cost),
|
|
279
|
+
};
|
|
280
|
+
if (nz(credits) != null) out.credits = nz(credits);
|
|
281
|
+
return Object.values(out).some((v) => typeof v === 'number') ? out : null;
|
|
282
|
+
}
|
|
283
|
+
function errorText(e) {
|
|
284
|
+
if (!e) return '';
|
|
285
|
+
return e.short_error || e.user_error_message || e.full_error || e.model_error_message || '';
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* A file part's ref: a file: URI, a plain path or a Windows drive path as is; any other URI (http(s),
|
|
289
|
+
* data:, a resource URI) cut to scheme://host, else one marker, since its path or query may carry a
|
|
290
|
+
* token (as opencode does).
|
|
291
|
+
*/
|
|
292
|
+
function fileRef(v) {
|
|
293
|
+
const s = String(v || '');
|
|
294
|
+
if (!s || s.startsWith('file:') || /^[a-z]:[\\/]/i.test(s) || !/^[a-z][a-z0-9+.-]*:/i.test(s)) return s;
|
|
295
|
+
try { const u = new URL(s); if (u.host) return `${u.protocol}//${u.host}`; } catch { /* not a URL */ }
|
|
296
|
+
return `<redacted len=${Buffer.byteLength(s, 'utf8')}>`;
|
|
297
|
+
}
|
|
298
|
+
function mediaPart(m, part_id) {
|
|
299
|
+
const bytes = m.inline_data && m.inline_data.length ? m.inline_data : null;
|
|
300
|
+
return { part_id, kind: 'file', media_type: m.mime_type || '', att_sha: bytes ? sha256(bytes) : null, att_len: bytes ? bytes.length : null, ref: m.uri ? fileRef(m.uri) : (m.display_name || '') };
|
|
301
|
+
}
|
|
302
|
+
function imagePart(im, part_id) {
|
|
303
|
+
const bytes = im.base64_data ? Buffer.from(im.base64_data, 'base64') : null;
|
|
304
|
+
return { part_id, kind: 'file', media_type: im.mime_type || '', att_sha: bytes ? sha256(bytes) : null, att_len: bytes ? bytes.length : null, ref: fileRef(im.uri) };
|
|
305
|
+
}
|
|
306
|
+
/** A tool payload (google.protobuf.Any), decoded when it is one this module reads. */
|
|
307
|
+
function payloadOf(any, name) {
|
|
308
|
+
if (!any || !any.type_url || !any.type_url.endsWith(`.${name}`)) return null;
|
|
309
|
+
return safeDecode(any.value, S[name]);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** annotations/<id>.pbtxt (a text proto): { title, archived }. Octal and C escapes in the title. */
|
|
313
|
+
function parseAnnotation(text) {
|
|
314
|
+
const src = String(text || '');
|
|
315
|
+
const out = { title: '', archived: /^\s*archived:\s*true\b/m.test(src) };
|
|
316
|
+
const m = /^\s*title:\s*"((?:[^"\\]|\\.)*)"/m.exec(src);
|
|
317
|
+
if (!m) return out;
|
|
318
|
+
const s = m[1];
|
|
319
|
+
const bytes = [];
|
|
320
|
+
const MAP = { n: '\n', t: '\t', r: '\r', a: '\x07', b: '\b', f: '\f', v: '\v' };
|
|
321
|
+
for (let i = 0; i < s.length; i++) {
|
|
322
|
+
const ch = String.fromCodePoint(s.codePointAt(i));
|
|
323
|
+
i += ch.length - 1;
|
|
324
|
+
if (ch !== '\\') { for (const b of Buffer.from(ch, 'utf-8')) bytes.push(b); continue; }
|
|
325
|
+
const n = s[++i];
|
|
326
|
+
if (/[0-7]/.test(n)) {
|
|
327
|
+
let o = n;
|
|
328
|
+
while (o.length < 3 && /[0-7]/.test(s[i + 1] || '')) o += s[++i];
|
|
329
|
+
bytes.push(parseInt(o, 8) & 0xff);
|
|
330
|
+
} else if (n === 'x' && /[0-9a-f]/i.test(s[i + 1] || '')) {
|
|
331
|
+
let h = '';
|
|
332
|
+
while (h.length < 2 && /[0-9a-f]/i.test(s[i + 1] || '')) h += s[++i];
|
|
333
|
+
bytes.push(parseInt(h, 16));
|
|
334
|
+
} else for (const b of Buffer.from(MAP[n] ?? n, 'utf-8')) bytes.push(b);
|
|
335
|
+
}
|
|
336
|
+
out.title = Buffer.from(bytes).toString('utf-8');
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** A side file's text; null only when it is not there. Any other read error throws: a listed file
|
|
341
|
+
* that cannot be read must not read as absent (its rewind records, its title would vanish). */
|
|
342
|
+
function readText(file) {
|
|
343
|
+
if (!file) return null;
|
|
344
|
+
try { return fs.readFileSync(file, 'utf-8'); } catch (e) { if (e && e.code === 'ENOENT') return null; throw e; }
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* A system message's `source`: `automated` when it starts a turn for the agent (a background task or
|
|
349
|
+
* timer done, a message from another agent), else `injected`. `sender` is agent_message.sender, or
|
|
350
|
+
* the `sender=` of the "[Message] …" text the jsonl keeps.
|
|
351
|
+
*/
|
|
352
|
+
function systemSource(eventType, sender) {
|
|
353
|
+
if (eventType === 'task_notification' || eventType === 'agent_message') return 'automated';
|
|
354
|
+
if (sender && sender !== 'system') return 'automated'; // <cascade>/task-<n>, or an agent's id
|
|
355
|
+
return 'injected';
|
|
356
|
+
}
|
|
357
|
+
const senderOf = (text) => { const m = /\[Message\][^\n]*?\bsender=(\S+)/.exec(String(text || '')); return m ? m[1] : ''; };
|
|
358
|
+
|
|
359
|
+
/** The MCP server a GENERIC result names, if any (source-only shapes, see the header). */
|
|
360
|
+
function serverOf(r) {
|
|
361
|
+
const c = r.tool_call_step && r.tool_call_step.mcp_server_tool_call;
|
|
362
|
+
if (c && c.server_name) return c.server_name;
|
|
363
|
+
const t = r.tool_result_step && r.tool_result_step.mcp_server_tool_result;
|
|
364
|
+
if (t && t.server_name) return t.server_name;
|
|
365
|
+
const nested = payloadOf(r.payload, 'Step');
|
|
366
|
+
return (nested && nested.mcp_tool && nested.mcp_tool.server_name) || '';
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* output_ref for a result agy cut: the full text sits in this conversation's
|
|
371
|
+
* steps/<idx>/output.txt (full_output_uri). The uri's folder is not trusted, only its step index;
|
|
372
|
+
* the file is read (to hash it) only when it is longer than the text the step holds.
|
|
373
|
+
*/
|
|
374
|
+
function spillRef(uri, shown, stepsDir, sid) {
|
|
375
|
+
if (!stepsDir || !uri) return null;
|
|
376
|
+
const m = /\/brain\/([^/]+)\/\.system_generated\/steps\/(\d+)\/output\.txt$/.exec(uriPath(uri));
|
|
377
|
+
if (!m || m[1] !== sid) return null;
|
|
378
|
+
const file = path.join(stepsDir, m[2], 'output.txt');
|
|
379
|
+
let st;
|
|
380
|
+
try { st = fs.statSync(file); } catch { return null; }
|
|
381
|
+
if (!st.isFile() || st.size <= Buffer.byteLength(String(shown || ''), 'utf-8')) return null;
|
|
382
|
+
let buf;
|
|
383
|
+
try { buf = fs.readFileSync(file); } catch { return null; }
|
|
384
|
+
return buf.length > Buffer.byteLength(String(shown || ''), 'utf-8') ? { sha: sha256(buf), len: buf.length } : null;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// ── brain transcript_full.jsonl ───────────────────────────────────────────────────────────────
|
|
388
|
+
/** Complete lines only (a partial last line is held back), each with its byte offset; malformed lines skipped. */
|
|
389
|
+
function parseLines(text) {
|
|
390
|
+
const buf = Buffer.from(String(text ?? ''), 'utf-8');
|
|
391
|
+
const end = buf.lastIndexOf(0x0a);
|
|
392
|
+
const out = [];
|
|
393
|
+
let off = 0;
|
|
394
|
+
while (off <= end) {
|
|
395
|
+
const nl = buf.indexOf(0x0a, off);
|
|
396
|
+
const line = buf.subarray(off, nl).toString('utf-8');
|
|
397
|
+
if (line.trim()) { try { out.push({ off, o: JSON.parse(line) }); } catch { /* skipped */ } }
|
|
398
|
+
off = nl + 1;
|
|
399
|
+
}
|
|
400
|
+
return out;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Walk the lines in file order. A line whose step_index is not above the last kept line's retracts
|
|
404
|
+
* every kept line from that index on (a rewind; they stay in the file). Returns the lines with
|
|
405
|
+
* `retracted` set, and the rewind events: { at: line position, retracted: [line positions],
|
|
406
|
+
* through: the line kept before the cut or -1 }.
|
|
407
|
+
*/
|
|
408
|
+
function foldTranscript(lines) {
|
|
409
|
+
const out = lines.filter((l) => l && l.o && Number.isInteger(Number(l.o.step_index)))
|
|
410
|
+
.map((l) => ({ ...l, si: Number(l.o.step_index), retracted: false }));
|
|
411
|
+
const kept = [];
|
|
412
|
+
const rewinds = [];
|
|
413
|
+
out.forEach((l, k) => {
|
|
414
|
+
const gone = [];
|
|
415
|
+
while (kept.length && out[kept[kept.length - 1]].si >= l.si) gone.unshift(kept.pop());
|
|
416
|
+
if (gone.length) {
|
|
417
|
+
for (const g of gone) out[g].retracted = true;
|
|
418
|
+
rewinds.push({ at: k, retracted: gone, through: kept.length ? kept[kept.length - 1] : -1 });
|
|
419
|
+
}
|
|
420
|
+
kept.push(k);
|
|
421
|
+
});
|
|
422
|
+
return { lines: out, rewinds };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// ── the DB path ───────────────────────────────────────────────────────────────────────────────
|
|
426
|
+
/** A step row that is there but does not decode: never skipped (the log would lose it, and the ids of
|
|
427
|
+
* the id-less steps after it would shift), never read from a fallback (its message id would change). */
|
|
428
|
+
class Undecodable extends Error { constructor(message) { super(message); this.name = 'Undecodable'; } }
|
|
429
|
+
function stepOf(row) {
|
|
430
|
+
let st;
|
|
431
|
+
try { st = decode(row.step_payload, S.Step); } catch (e) { throw new Undecodable(`agy: step ${row.idx}: payload does not decode (${e.message})`); }
|
|
432
|
+
let meta = null;
|
|
433
|
+
if (row.metadata != null) {
|
|
434
|
+
try { meta = decode(row.metadata, S.Meta); } catch (e) { throw new Undecodable(`agy: step ${row.idx}: metadata does not decode (${e.message})`); }
|
|
435
|
+
}
|
|
436
|
+
meta = meta || st.metadata || {};
|
|
437
|
+
return {
|
|
438
|
+
idx: Number(row.idx),
|
|
439
|
+
type: Number(row.step_type) || st.type || 0,
|
|
440
|
+
status: Number(row.status) || st.status || 0,
|
|
441
|
+
meta, st,
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
const infoOf = (s) => s.meta.source_trajectory_step_info || {};
|
|
445
|
+
const requestedOf = (s, cm) => modelName((s.meta.requested_model || {}).model || s.meta.generator_model || (s.meta.model_usage || {}).model || (cm || {}).model);
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* src: { steps: rows[], refs: rows[], traj: blob|null, gen(i) → blob|null, execs() → rows[],
|
|
449
|
+
* transcript: jsonl text|null }, rows as the DB stores them (blobs are protobuf bytes).
|
|
450
|
+
*/
|
|
451
|
+
function fromDb(sid, src, summary, ann, stepsDir = null) {
|
|
452
|
+
const steps = (src.steps || []).map(stepOf).sort((a, b) => a.idx - b.idx);
|
|
453
|
+
|
|
454
|
+
// Ids: the content id; an anchored fallback for a step without one, assigned over every step so
|
|
455
|
+
// held-back and folded steps never shift it.
|
|
456
|
+
const ids = new Map();
|
|
457
|
+
{ let anchor = ''; let n = 0;
|
|
458
|
+
for (const s of steps) {
|
|
459
|
+
const id = s.meta.model_api_content_id;
|
|
460
|
+
if (id) { anchor = id; n = 0; ids.set(s, id); } else ids.set(s, `${anchor}+${++n}`);
|
|
461
|
+
} }
|
|
462
|
+
|
|
463
|
+
// Turn end: a later step of another execution, or an executor_metadata row (read only if needed).
|
|
464
|
+
const execOf = (s) => s.meta.execution_id || '';
|
|
465
|
+
const laterOther = new Array(steps.length).fill(false);
|
|
466
|
+
{ const seen = new Set();
|
|
467
|
+
for (let i = steps.length - 1; i >= 0; i--) {
|
|
468
|
+
const x = execOf(steps[i]);
|
|
469
|
+
laterOther[i] = seen.size > (seen.has(x) ? 1 : 0);
|
|
470
|
+
if (x) seen.add(x);
|
|
471
|
+
} }
|
|
472
|
+
let ended = null;
|
|
473
|
+
const turnEnded = (i) => {
|
|
474
|
+
if (laterOther[i]) return true;
|
|
475
|
+
const x = execOf(steps[i]);
|
|
476
|
+
if (!x) return false;
|
|
477
|
+
if (!ended) {
|
|
478
|
+
ended = new Set();
|
|
479
|
+
for (const r of src.execs ? src.execs() : []) { const e = safeDecode(r.data, S.ExecMeta); if (e.execution_id) ended.add(e.execution_id); }
|
|
480
|
+
}
|
|
481
|
+
return ended.has(x);
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
const gens = new Map();
|
|
485
|
+
const genOf = (i) => { if (!gens.has(i)) gens.set(i, safeDecode(src.gen ? src.gen(i) : null, S.GenMeta)); return gens.get(i); };
|
|
486
|
+
/** The generation that produced this step, when its step_indices say so. */
|
|
487
|
+
const genFor = (s) => {
|
|
488
|
+
const info = infoOf(s);
|
|
489
|
+
const g = genOf(info.metadata_index || 0);
|
|
490
|
+
const own = [s.idx, info.step_index || 0];
|
|
491
|
+
return (g.step_indices || []).some((x) => own.includes(x)) ? g : null;
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// Each planner call claims the first later GENERIC step with its id.
|
|
495
|
+
const byCall = new Map();
|
|
496
|
+
for (const s of steps) if (s.type === T.GENERIC) {
|
|
497
|
+
const id = (s.meta.tool_call || {}).id;
|
|
498
|
+
if (!id) continue;
|
|
499
|
+
if (!byCall.has(id)) byCall.set(id, []);
|
|
500
|
+
byCall.get(id).push(s);
|
|
501
|
+
}
|
|
502
|
+
const claimed = new Set();
|
|
503
|
+
const results = new Map(); // planner step → Map(call id → GENERIC step)
|
|
504
|
+
const ownerOf = new Map(); // GENERIC step → the planner step whose call it answers
|
|
505
|
+
for (const s of steps) if (s.type === T.PLANNER_RESPONSE) {
|
|
506
|
+
const m = new Map();
|
|
507
|
+
for (const c of (s.st.planner_response || {}).tool_calls || []) {
|
|
508
|
+
if (!c.id) continue;
|
|
509
|
+
const g = (byCall.get(c.id) || []).find((x) => x.idx > s.idx && !claimed.has(x));
|
|
510
|
+
if (g) { claimed.add(g); m.set(c.id, g); ownerOf.set(g, s); }
|
|
511
|
+
}
|
|
512
|
+
results.set(s, m);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
const emittedAs = new Map(); // step → the message id it was written under
|
|
516
|
+
const records = [];
|
|
517
|
+
const head = (s, role) => ({ k: 'message', message_id: ids.get(s), role, ts: s.meta.created_at ?? null, order: s.idx, agent_id: '', state: 'current' });
|
|
518
|
+
const doneStatus = (s) => (s.status === ST.ERROR ? 'error' : s.status === ST.CANCELED ? 'cancelled' : s.status === ST.INTERRUPTED ? 'interrupted' : 'complete');
|
|
519
|
+
function toolPart(callId, name, argsJson, g, folded = false) {
|
|
520
|
+
const part = { part_id: `tool:${callId}`, kind: 'tool', call_id: callId, tool: name || '', args: argsOf(argsJson), output: null, is_error: false, status: 'interrupted' };
|
|
521
|
+
if (!g) return part;
|
|
522
|
+
if (folded && g.meta.model_api_content_id) part.content_id = g.meta.model_api_content_id;
|
|
523
|
+
const r = (g.st.generic || {}).result || {};
|
|
524
|
+
const server = serverOf(r);
|
|
525
|
+
if (server) part.server = server;
|
|
526
|
+
const err = errorText(g.st.error);
|
|
527
|
+
if (OPEN.has(g.status)) part.status = g.status === ST.WAITING ? 'waiting' : 'running';
|
|
528
|
+
else if (g.status === ST.ERROR) { part.output = r.result || err || ''; part.is_error = true; part.status = /denied/i.test(err) ? 'denied' : 'error'; }
|
|
529
|
+
else if (g.status === ST.CANCELED) { part.output = r.result || ''; part.status = 'cancelled'; }
|
|
530
|
+
else if (g.status === ST.INTERRUPTED) { part.output = r.result || ''; part.status = 'interrupted'; }
|
|
531
|
+
else { part.output = r.result || ''; part.status = 'completed'; }
|
|
532
|
+
const rc = OPEN.has(g.status) ? null : payloadOf(r.payload, 'RunCommand');
|
|
533
|
+
if (rc && !rc.exit_code_unknown && !rc.timed_out) part.exit_code = rc.exit_code || 0; // proto3 omits a zero
|
|
534
|
+
if (r.step_render_info && r.step_render_info.hidden) part.hidden = true;
|
|
535
|
+
const ref = OPEN.has(g.status) ? null : spillRef(r.full_output_uri, part.output, stepsDir, sid);
|
|
536
|
+
if (ref) part.output_ref = ref;
|
|
537
|
+
part.ts = g.meta.created_at ?? null;
|
|
538
|
+
return part;
|
|
539
|
+
}
|
|
540
|
+
const resultMedia = (g, callId) => ((((g && g.st.generic) || {}).result || {}).media || []).map((m, k) => mediaPart(m, `tool:${callId}:media:${k}`));
|
|
541
|
+
|
|
542
|
+
for (let i = 0; i < steps.length; i++) {
|
|
543
|
+
const s = steps[i];
|
|
544
|
+
if (claimed.has(s)) continue; // folded into its planner's tool part
|
|
545
|
+
const open = OPEN.has(s.status);
|
|
546
|
+
if (s.type === T.PLANNER_RESPONSE) {
|
|
547
|
+
if (open && !turnEnded(i)) break; // still generating: held back, and all after it
|
|
548
|
+
const pr = s.st.planner_response || {};
|
|
549
|
+
const calls = pr.tool_calls || [];
|
|
550
|
+
const res = results.get(s);
|
|
551
|
+
const pending = calls.some((c) => { const g = c.id ? res.get(c.id) : null; return !g || OPEN.has(g.status); });
|
|
552
|
+
if (pending && !turnEnded(i)) break; // a tool still running: held back, and all after it
|
|
553
|
+
const parts = [];
|
|
554
|
+
if (pr.thinking) parts.push({ part_id: 'thinking', kind: 'reasoning', text: pr.thinking });
|
|
555
|
+
else if (pr.thinking_redacted || pr.signature_len || pr.thinking_signature_len) parts.push({ part_id: 'thinking', kind: 'reasoning', opaque: true });
|
|
556
|
+
const text = pr.response || pr.modified_response || '';
|
|
557
|
+
if (text) parts.push({ part_id: 'response', kind: 'text', text });
|
|
558
|
+
let missing = false;
|
|
559
|
+
calls.forEach((c, k) => {
|
|
560
|
+
const callId = c.id || `@${ids.get(s)}:${k}`;
|
|
561
|
+
const g = c.id ? res.get(c.id) : null;
|
|
562
|
+
if (!g) missing = true;
|
|
563
|
+
parts.push(toolPart(callId, c.name, c.arguments_json, g, true));
|
|
564
|
+
parts.push(...resultMedia(g, callId));
|
|
565
|
+
if (g) emittedAs.set(g, ids.get(s));
|
|
566
|
+
});
|
|
567
|
+
const err = errorText(s.st.error);
|
|
568
|
+
if (err) parts.push({ part_id: 'error', kind: 'error', text: err });
|
|
569
|
+
const g = genFor(s);
|
|
570
|
+
const cm = (g && g.chat_model) || {};
|
|
571
|
+
const own = s.meta.model_usage;
|
|
572
|
+
const first = g && (g.step_indices || [])[0];
|
|
573
|
+
const fromGen = !own && g && (first === s.idx || first === infoOf(s).step_index);
|
|
574
|
+
records.push({
|
|
575
|
+
...head(s, 'assistant'),
|
|
576
|
+
status: open || (s.status === ST.DONE && missing) ? 'interrupted' : doneStatus(s),
|
|
577
|
+
model: cm.response_model || modelName((own || {}).model || s.meta.generator_model || cm.model),
|
|
578
|
+
model_requested: requestedOf(s, cm),
|
|
579
|
+
effort: '',
|
|
580
|
+
usage: usageOf(own || (fromGen ? cm.usage : null), own ? s.meta.model_cost : cm.model_cost, g ? cm.credit_cost : null),
|
|
581
|
+
parts,
|
|
582
|
+
});
|
|
583
|
+
emittedAs.set(s, ids.get(s));
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
if (open && !turnEnded(i)) break; // unfinished: held back, and all after it
|
|
587
|
+
// Open after its turn ended: a GENERIC is a background task (its tool part says running); any
|
|
588
|
+
// other step was left open (interrupted).
|
|
589
|
+
const stale = open && s.type !== T.GENERIC;
|
|
590
|
+
const closedStatus = stale ? 'interrupted' : doneStatus(s);
|
|
591
|
+
if (s.type === T.USER_INPUT) {
|
|
592
|
+
const ui = s.st.user_input || {};
|
|
593
|
+
const text = ui.user_response || ui.query || (ui.items || []).map((x) => x.text || '').join('');
|
|
594
|
+
const parts = [];
|
|
595
|
+
if (text) parts.push({ part_id: 'text', kind: 'text', text });
|
|
596
|
+
(ui.images || []).forEach((im, k) => parts.push(imagePart(im, `image:${k}`)));
|
|
597
|
+
(ui.media || []).forEach((m, k) => parts.push(mediaPart(m, `media:${k}`)));
|
|
598
|
+
const auto = s.meta.source === SRC.SYSTEM || s.meta.source === SRC.SYSTEM_SDK;
|
|
599
|
+
records.push({ ...head(s, 'user'), status: closedStatus, ...(auto ? { source: 'automated' } : {}), parts });
|
|
600
|
+
} else if (s.type === T.GENERIC) {
|
|
601
|
+
// A result no planner claims: its own tool message, the call taken from its metadata.
|
|
602
|
+
const tc = s.meta.tool_call || {};
|
|
603
|
+
const callId = tc.id || `@${ids.get(s)}:0`;
|
|
604
|
+
records.push({ ...head(s, 'tool'), status: open ? 'complete' : doneStatus(s), parts: [toolPart(callId, tc.name, tc.arguments_json, s), ...resultMedia(s, callId)] });
|
|
605
|
+
} else if (s.type === T.SYSTEM_MESSAGE) {
|
|
606
|
+
const sm = s.st.system_message || {};
|
|
607
|
+
const am = sm.agent_message || {};
|
|
608
|
+
const parts = [{ part_id: 'notice', kind: 'notice', text: sm.message || am.content || '' }];
|
|
609
|
+
(sm.media || []).forEach((m, k) => parts.push(mediaPart(m, `media:${k}`)));
|
|
610
|
+
const hidden = !!((sm.render_info && sm.render_info.hidden) || am.hide_from_user);
|
|
611
|
+
const source = systemSource(sm.event_type || '', am.sender || senderOf(sm.message));
|
|
612
|
+
records.push({ ...head(s, 'system'), status: closedStatus, ...(hidden ? { hidden: true } : {}), source, parts });
|
|
613
|
+
} else if (s.type === T.EPHEMERAL_MESSAGE) {
|
|
614
|
+
const em = s.st.ephemeral_message || {};
|
|
615
|
+
const parts = [{ part_id: 'notice', kind: 'notice', text: em.content || '' }];
|
|
616
|
+
(em.media || []).forEach((m, k) => parts.push(mediaPart(m, `media:${k}`)));
|
|
617
|
+
records.push({ ...head(s, 'system'), status: closedStatus, source: 'injected', parts });
|
|
618
|
+
} else if (s.type === T.CHECKPOINT) {
|
|
619
|
+
const cp = s.st.checkpoint || {};
|
|
620
|
+
const g = genFor(s);
|
|
621
|
+
const cm = (g && g.chat_model) || {};
|
|
622
|
+
const retried = (s.meta.retry_infos || []).filter((r) => r.usage).pop();
|
|
623
|
+
const u = s.meta.model_usage || cm.usage || (retried && retried.usage) || null;
|
|
624
|
+
const end = cp.included_step_index_end ?? null;
|
|
625
|
+
records.push({
|
|
626
|
+
...head(s, 'system'), status: closedStatus,
|
|
627
|
+
model: cm.response_model || modelName((u || {}).model || s.meta.generator_model || cm.model),
|
|
628
|
+
model_requested: requestedOf(s, cm), effort: '',
|
|
629
|
+
usage: usageOf(u, s.meta.model_cost || cm.model_cost, g ? cm.credit_cost : null),
|
|
630
|
+
parts: [{ part_id: 'summary', kind: 'summary', text: cp.session_summary || '', replaces: { through_index: end } }],
|
|
631
|
+
});
|
|
632
|
+
// Through: the last message written at or before included_step_index_end, the checkpoint excluded.
|
|
633
|
+
let through = '';
|
|
634
|
+
for (const p of steps) if (p.idx < s.idx && (end == null || p.idx <= end) && emittedAs.has(p)) through = emittedAs.get(p);
|
|
635
|
+
const covered = (cp.included_step_indices || []).length || (end != null ? end - (cp.included_step_index_start || 0) + 1 : 0);
|
|
636
|
+
records.push({ k: 'lifecycle', event: 'compaction', at: s.meta.created_at ?? null, ...(through ? { through } : {}), ...(covered > 0 ? { count: covered } : {}) });
|
|
637
|
+
} else {
|
|
638
|
+
records.push({ ...head(s, 'system'), status: closedStatus, parts: [{ part_id: 'notice', kind: 'notice', text: `[agy step type ${s.type}]` }] });
|
|
639
|
+
}
|
|
640
|
+
emittedAs.set(s, ids.get(s));
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// Rewinds: the DB keeps no trace, the brain transcript does (a step_index not above a kept line's).
|
|
644
|
+
// Each goes before the first message at or after the index it cut back to; through = the message
|
|
645
|
+
// the cut kept last, while it is still in the DB.
|
|
646
|
+
if (src.transcript) {
|
|
647
|
+
const { lines, rewinds } = foldTranscript(parseLines(src.transcript));
|
|
648
|
+
const byIdx = new Map(steps.map((s) => [s.idx, s]));
|
|
649
|
+
for (const r of rewinds) {
|
|
650
|
+
const cut = lines[r.at].si;
|
|
651
|
+
const keptStep = r.through >= 0 ? byIdx.get(lines[r.through].si) : null;
|
|
652
|
+
const through = keptStep && emittedAs.get(keptStep);
|
|
653
|
+
const rec = { k: 'lifecycle', event: 'rewind', at: null, ...(through ? { through } : {}), count: r.retracted.length };
|
|
654
|
+
const pos = records.findIndex((m) => m.k === 'message' && m.order >= cut);
|
|
655
|
+
if (pos < 0) records.push(rec); else records.splice(pos, 0, rec);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
// Session record: links (child side) and children (origin side).
|
|
660
|
+
const traj = safeDecode(src.traj, S.TrajMeta);
|
|
661
|
+
const sum = summary || {};
|
|
662
|
+
const ws = (traj.workspaces || [])[0] || {};
|
|
663
|
+
const links = [];
|
|
664
|
+
for (const r of src.refs || []) {
|
|
665
|
+
const ref = safeDecode(r.data, S.TrajRef);
|
|
666
|
+
if (ref.reference_type !== REF_FORK || !ref.conversation_id || ref.conversation_id === sid) continue;
|
|
667
|
+
const copied = steps.filter((s) => infoOf(s).cascade_id === ref.conversation_id && emittedAs.has(s));
|
|
668
|
+
const atStep = copied.find((s) => (infoOf(s).step_index || 0) === (ref.step_index || 0));
|
|
669
|
+
const link = { kind: 'fork', origin: ref.conversation_id, evidence: 'explicit', at: atStep ? emittedAs.get(atStep) : '' };
|
|
670
|
+
if (copied.length) link.through = emittedAs.get(copied[copied.length - 1]);
|
|
671
|
+
links.push(link);
|
|
672
|
+
}
|
|
673
|
+
const parent = traj.parent_conversation_id || sum.parent_conversation_id || '';
|
|
674
|
+
if (parent && parent !== sid) {
|
|
675
|
+
let at = '';
|
|
676
|
+
for (const s of steps) {
|
|
677
|
+
if (s.type !== T.SYSTEM_MESSAGE) continue;
|
|
678
|
+
const tool = ((((s.st.system_message || {}).agent_message || {}).source_metadata) || {}).tool;
|
|
679
|
+
if (!tool || tool.conversation_id !== parent || !tool.tool_call || !tool.tool_call.id) continue;
|
|
680
|
+
if (tool.tool_call.name === 'invoke_subagent') { at = tool.tool_call.id; break; }
|
|
681
|
+
if (!at) at = tool.tool_call.id;
|
|
682
|
+
}
|
|
683
|
+
links.push({ kind: 'subagent', origin: parent, evidence: 'explicit', at });
|
|
684
|
+
}
|
|
685
|
+
const children = [];
|
|
686
|
+
for (const s of steps) {
|
|
687
|
+
if (s.type !== T.GENERIC) continue;
|
|
688
|
+
const inv = payloadOf((((s.st.generic || {}).result) || {}).payload, 'InvokeSubagent');
|
|
689
|
+
for (const r of (inv && inv.results) || []) {
|
|
690
|
+
if (r.conversation_id && r.conversation_id !== sid && !children.some((c) => c.session_id === r.conversation_id)) {
|
|
691
|
+
// After the planner whose call it answers (or the step itself, written on its own), by id:
|
|
692
|
+
// known whether or not that message is out yet, so a held planner holds its child too.
|
|
693
|
+
children.push({ kind: 'subagent', session_id: r.conversation_id, evidence: 'explicit', after: ids.get(ownerOf.get(s) || s) || null });
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
const session = {
|
|
698
|
+
k: 'session',
|
|
699
|
+
title: sum.title || ann.title || '',
|
|
700
|
+
cwd: uriPath(ws.workspace_folder_absolute_uri || (traj.workspace_uris || [])[0] || firstJson(sum.workspace_uris)),
|
|
701
|
+
created: traj.created_at ?? null,
|
|
702
|
+
updated: null,
|
|
703
|
+
archived: ann.archived ? true : null,
|
|
704
|
+
version: '',
|
|
705
|
+
incarnation: '',
|
|
706
|
+
links,
|
|
707
|
+
children,
|
|
708
|
+
};
|
|
709
|
+
return placeChildren([session, ...records]);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// ── the jsonl fallback (no DB) ────────────────────────────────────────────────────────────────
|
|
713
|
+
function fromTranscript(sid, text, summary, ann) {
|
|
714
|
+
const { lines: L, rewinds } = foldTranscript(parseLines(text));
|
|
715
|
+
const rewindAt = new Map(rewinds.map((r) => [r.at, r]));
|
|
716
|
+
const idOf = (k) => `@${L[k].off}`;
|
|
717
|
+
const consumed = new Set();
|
|
718
|
+
const records = [];
|
|
719
|
+
const resultPart = (o, callId, name, args) => {
|
|
720
|
+
const part = { part_id: `tool:${callId}`, kind: 'tool', call_id: callId, tool: name || '', args: args ?? {}, output: null, is_error: false, status: 'interrupted' };
|
|
721
|
+
if (!o) return part;
|
|
722
|
+
if (o.status === 'ERROR' || o.error) {
|
|
723
|
+
part.output = o.content || o.error || ''; part.is_error = true; part.status = /denied/i.test(String(o.error || '')) ? 'denied' : 'error';
|
|
724
|
+
} else if (o.status === 'RUNNING') { part.output = o.content ?? null; part.status = 'running'; }
|
|
725
|
+
else { part.output = o.content || ''; part.status = 'completed'; }
|
|
726
|
+
const ex = name === 'run_command' && /exited with code (-?\d+)/.exec(String(o.content || ''));
|
|
727
|
+
if (ex) part.exit_code = Number(ex[1]);
|
|
728
|
+
part.ts = msOf(o.created_at);
|
|
729
|
+
return part;
|
|
730
|
+
};
|
|
731
|
+
const media = (o, prefix) => (o && Array.isArray(o.media) ? o.media : []).map((m, k) => ({ part_id: `${prefix}media:${k}`, kind: 'file', media_type: m.mime_type || '', att_sha: null, att_len: null, ref: fileRef(m.uri) }));
|
|
732
|
+
for (let i = 0; i < L.length; i++) {
|
|
733
|
+
const rw = rewindAt.get(i);
|
|
734
|
+
if (rw) {
|
|
735
|
+
const through = rw.through >= 0 ? idOf(rw.through) : '';
|
|
736
|
+
records.push({ k: 'lifecycle', event: 'rewind', at: null, message_ids: rw.retracted.filter((k) => !consumed.has(k)).map(idOf), ...(through ? { through } : {}) });
|
|
737
|
+
}
|
|
738
|
+
if (consumed.has(i)) continue;
|
|
739
|
+
const { o } = L[i];
|
|
740
|
+
const id = idOf(i);
|
|
741
|
+
const head = (role) => ({ k: 'message', message_id: id, role, ts: msOf(o.created_at), order: L[i].off, agent_id: '', state: L[i].retracted ? 'retracted' : 'current' });
|
|
742
|
+
const status = o.status === 'ERROR' ? 'error' : 'complete';
|
|
743
|
+
if (o.type === 'USER_INPUT') {
|
|
744
|
+
const content = String(o.content || '');
|
|
745
|
+
const m = /<USER_REQUEST>\n?([\s\S]*?)\n?<\/USER_REQUEST>/.exec(content);
|
|
746
|
+
const parts = [{ part_id: 'text', kind: 'text', text: m ? m[1] : content }];
|
|
747
|
+
const rest = m ? content.replace(m[0], '').trim() : '';
|
|
748
|
+
if (rest) parts.push({ part_id: 'context', kind: 'notice', text: rest });
|
|
749
|
+
parts.push(...media(o, ''));
|
|
750
|
+
const auto = o.source === 'SYSTEM' || o.source === 'SYSTEM_SDK';
|
|
751
|
+
records.push({ ...head('user'), status, ...(auto ? { source: 'automated' } : {}), parts });
|
|
752
|
+
} else if (o.type === 'PLANNER_RESPONSE') {
|
|
753
|
+
// Its results: the GENERIC lines after it, by position, up to the next planner or user line
|
|
754
|
+
// or a rewind.
|
|
755
|
+
const res = [];
|
|
756
|
+
let j = i + 1;
|
|
757
|
+
while (j < L.length && !rewindAt.has(j) && L[j].o.type !== 'PLANNER_RESPONSE' && L[j].o.type !== 'USER_INPUT') { if (L[j].o.type === 'GENERIC') res.push(j); j++; }
|
|
758
|
+
const closed = j < L.length || L[i].retracted;
|
|
759
|
+
const calls = Array.isArray(o.tool_calls) ? o.tool_calls : [];
|
|
760
|
+
const pending = calls.some((c, k) => res[k] == null || L[res[k]].o.status === 'RUNNING');
|
|
761
|
+
if (pending && !closed) break; // the turn is still open: held back, and all after it
|
|
762
|
+
const parts = [];
|
|
763
|
+
if (o.thinking) parts.push({ part_id: 'thinking', kind: 'reasoning', text: String(o.thinking) });
|
|
764
|
+
if (o.content) parts.push({ part_id: 'response', kind: 'text', text: String(o.content) });
|
|
765
|
+
let missing = false;
|
|
766
|
+
calls.forEach((c, k) => {
|
|
767
|
+
const callId = `@${id}:${k}`; // sessions-model §1 rule 6: '@' + the call's message id + ':' + index
|
|
768
|
+
const r = res[k] != null ? L[res[k]].o : null;
|
|
769
|
+
if (r) consumed.add(res[k]); else missing = true;
|
|
770
|
+
parts.push(resultPart(r, callId, c.name, c.args), ...media(r, `tool:${callId}:`));
|
|
771
|
+
});
|
|
772
|
+
if (o.error) parts.push({ part_id: 'error', kind: 'error', text: String(o.error) });
|
|
773
|
+
records.push({
|
|
774
|
+
...head('assistant'), status: status === 'complete' && missing ? 'interrupted' : status, model: '', model_requested: '', effort: '',
|
|
775
|
+
usage: usageOf({ input_tokens: o.input_tokens, output_tokens: o.output_tokens, cache_read_tokens: o.cache_read_tokens }, null, null),
|
|
776
|
+
parts,
|
|
777
|
+
});
|
|
778
|
+
} else if (o.type === 'GENERIC') {
|
|
779
|
+
if (o.status === 'RUNNING' && i === L.length - 1) break; // the tail is still running
|
|
780
|
+
const callId = `@${id}:0`;
|
|
781
|
+
records.push({ ...head('tool'), status, parts: [resultPart(o, callId, '', {}), ...media(o, `tool:${callId}:`)] });
|
|
782
|
+
} else if (o.type === 'CHECKPOINT') {
|
|
783
|
+
records.push({ ...head('system'), status, parts: [{ part_id: 'summary', kind: 'summary', text: String(o.content || ''), replaces: 'unknown' }] });
|
|
784
|
+
const prev = records.filter((r) => r.k === 'message' && r.state === 'current' && r.message_id !== id).pop();
|
|
785
|
+
records.push({ k: 'lifecycle', event: 'compaction', at: msOf(o.created_at), ...(prev ? { through: prev.message_id } : {}) });
|
|
786
|
+
} else {
|
|
787
|
+
const parts = [{ part_id: 'notice', kind: 'notice', text: String(o.content || (o.type ? `[agy ${o.type}]` : '')) }];
|
|
788
|
+
const source = o.type === 'EPHEMERAL_MESSAGE' ? 'injected' : o.type === 'SYSTEM_MESSAGE' ? systemSource('', senderOf(o.content)) : '';
|
|
789
|
+
records.push({ ...head('system'), status, ...(source ? { source } : {}), parts: [...parts, ...media(o, '')] });
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
const sum = summary || {};
|
|
793
|
+
const links = [];
|
|
794
|
+
if (sum.parent_conversation_id && sum.parent_conversation_id !== sid) links.push({ kind: 'subagent', origin: sum.parent_conversation_id, evidence: 'explicit', at: '' });
|
|
795
|
+
const session = {
|
|
796
|
+
k: 'session', title: sum.title || ann.title || '', cwd: uriPath(firstJson(sum.workspace_uris)), created: null, updated: null,
|
|
797
|
+
archived: ann.archived ? true : null, version: '', incarnation: '', links,
|
|
798
|
+
};
|
|
799
|
+
return [session, ...records];
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
// ── list / normalise ──────────────────────────────────────────────────────────────────────────
|
|
803
|
+
// The one error that may read as empty: a table or column an older schema lacks. Everything else
|
|
804
|
+
// (busy, locked, SQLITE_PROTOCOL, malformed, IOERR, NOTADB) fails list() or normalise().
|
|
805
|
+
const isMissing = (e) => /no such (table|column)/i.test(String((e && e.message) || e));
|
|
806
|
+
function readdir(dir) { try { return fs.readdirSync(dir); } catch { return []; } }
|
|
807
|
+
function isFile(p) { try { return fs.statSync(p).isFile(); } catch { return false; } }
|
|
808
|
+
|
|
809
|
+
function readSummaries(file) {
|
|
810
|
+
const out = new Map();
|
|
811
|
+
if (!isFile(file)) return out;
|
|
812
|
+
const { openReadOnly } = require('../sqlite');
|
|
813
|
+
let db = null;
|
|
814
|
+
try {
|
|
815
|
+
db = openReadOnly(file);
|
|
816
|
+
// Not step_count or last_modified_time: they move with every step, and the DB file says so already.
|
|
817
|
+
const rows = db.prepare('SELECT conversation_id, title, parent_conversation_id, agent_name, app_data_dir, workspace_uris FROM conversation_summaries').all();
|
|
818
|
+
for (const r of rows) out.set(String(r.conversation_id), { ...r });
|
|
819
|
+
} catch (e) {
|
|
820
|
+
// A schema without the table only costs titles and the summaries' parent link; any other error
|
|
821
|
+
// fails list() (the runner keeps its index) rather than list sessions without them.
|
|
822
|
+
if (!isMissing(e)) throw e;
|
|
823
|
+
} finally { if (db) try { db.close(); } catch { /* closed */ } }
|
|
824
|
+
return out;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* Sessions in a store: one per conversations/<id>.db, plus a brain transcript with no DB. Each entry
|
|
829
|
+
* names the files it is read from (the DB and its -wal, the brain transcript, the annotation) and the
|
|
830
|
+
* summary row's stable fields, so the runner re-reads it when one changes. The shared
|
|
831
|
+
* conversation_summaries.db is not named: what is read from it is in the entry.
|
|
832
|
+
*/
|
|
833
|
+
function list(storeRoot = storeRootDefault()) {
|
|
834
|
+
if (!fs.existsSync(storeRoot)) return [];
|
|
835
|
+
const harnessPath = require('../harness-paths').resolveNative(storeRoot); // = dataFolder('agy') for the default store
|
|
836
|
+
const byId = new Map();
|
|
837
|
+
const add = (id) => {
|
|
838
|
+
if (!byId.has(id)) byId.set(id, { harness: HARNESS, session_id: id, harness_path: harnessPath, root: storeRoot, db_path: null, wal_path: null, transcript_path: null, annotation_path: null, summary: null });
|
|
839
|
+
return byId.get(id);
|
|
840
|
+
};
|
|
841
|
+
const conv = path.join(storeRoot, 'conversations');
|
|
842
|
+
for (const f of readdir(conv)) {
|
|
843
|
+
const m = /^(.+)\.db$/.exec(f);
|
|
844
|
+
if (m && isFile(path.join(conv, f))) { const e = add(m[1]); e.db_path = path.join(conv, f); e.wal_path = `${e.db_path}-wal`; }
|
|
845
|
+
}
|
|
846
|
+
const brain = path.join(storeRoot, 'brain');
|
|
847
|
+
for (const id of readdir(brain)) {
|
|
848
|
+
const p = path.join(brain, id, '.system_generated', 'logs', 'transcript_full.jsonl');
|
|
849
|
+
if (isFile(p)) add(id).transcript_path = p;
|
|
850
|
+
}
|
|
851
|
+
const sums = readSummaries(path.join(storeRoot, 'conversation_summaries.db'));
|
|
852
|
+
for (const e of byId.values()) {
|
|
853
|
+
e.summary = sums.get(e.session_id) || null;
|
|
854
|
+
const ann = path.join(storeRoot, 'annotations', `${e.session_id}.pbtxt`);
|
|
855
|
+
if (isFile(ann)) e.annotation_path = ann;
|
|
856
|
+
}
|
|
857
|
+
return [...byId.values()].sort((a, b) => (a.session_id < b.session_id ? -1 : a.session_id > b.session_id ? 1 : 0));
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
function dbSource(db, transcript) {
|
|
861
|
+
// Only a missing table (an older schema) reads as empty; any other error throws, or the log would
|
|
862
|
+
// be rewritten as if steps were gone.
|
|
863
|
+
const all = (sql) => { try { return db.prepare(sql).all(); } catch (e) { if (!isMissing(e)) throw e; return []; } };
|
|
864
|
+
let genStmt = null;
|
|
865
|
+
try { genStmt = db.prepare('SELECT data FROM gen_metadata WHERE idx = ?'); } catch (e) { if (!isMissing(e)) throw e; genStmt = null; }
|
|
866
|
+
const traj = all("SELECT id, data FROM trajectory_metadata_blob ORDER BY (id = 'main') DESC, id LIMIT 1")[0];
|
|
867
|
+
return {
|
|
868
|
+
steps: all('SELECT idx, step_type, status, metadata, step_payload FROM steps ORDER BY idx'),
|
|
869
|
+
refs: all('SELECT idx, data FROM parent_references ORDER BY idx'),
|
|
870
|
+
traj: traj ? traj.data : null,
|
|
871
|
+
gen: (i) => { if (!genStmt) return null; const r = genStmt.get(i); return r ? r.data : null; },
|
|
872
|
+
execs: () => all('SELECT idx, data FROM executor_metadata ORDER BY idx'),
|
|
873
|
+
transcript,
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
function memSource(s) {
|
|
877
|
+
const gens = new Map((s.gens || []).map((r) => [Number(r.idx), r.data]));
|
|
878
|
+
return { steps: s.steps, refs: s.refs || [], traj: s.traj || null, gen: (i) => gens.get(i) ?? null, execs: () => s.execs || [], transcript: s.transcript ?? null };
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* Normalise one session: a list() entry, or native data already loaded (tests) —
|
|
883
|
+
* `{ session_id, steps: [{idx, step_type, status, metadata, step_payload}], gens, execs, refs, traj,
|
|
884
|
+
* transcript, summary, annotation, root? }` for the DB (root: the store, for spilled outputs) (the transcript then only adds rewind events), or
|
|
885
|
+
* `{ session_id, transcript: '<jsonl text>', summary, annotation }` without one. Returns the log
|
|
886
|
+
* records, the session record first; null when the session has no source.
|
|
887
|
+
*/
|
|
888
|
+
function normalise(session) {
|
|
889
|
+
const sid = session.session_id;
|
|
890
|
+
const summary = session.summary || null;
|
|
891
|
+
const ann = parseAnnotation(session.annotation != null ? session.annotation : readText(session.annotation_path));
|
|
892
|
+
const stepsDir = session.root ? path.join(session.root, 'brain', String(sid), '.system_generated', 'steps') : null;
|
|
893
|
+
if (Array.isArray(session.steps)) return fromDb(sid, memSource(session), summary, ann, stepsDir);
|
|
894
|
+
const transcript = session.transcript != null ? session.transcript : readText(session.transcript_path);
|
|
895
|
+
if (session.db_path && isFile(session.db_path)) {
|
|
896
|
+
const { openReadOnly } = require('../sqlite');
|
|
897
|
+
const db = openReadOnly(session.db_path);
|
|
898
|
+
try {
|
|
899
|
+
db.exec('BEGIN'); // one snapshot of a live WAL DB
|
|
900
|
+
return fromDb(sid, dbSource(db, transcript), summary, ann, stepsDir);
|
|
901
|
+
} finally {
|
|
902
|
+
try { db.exec('COMMIT'); } catch { /* BEGIN failed: no transaction to end */ }
|
|
903
|
+
db.close();
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
if (transcript == null) return null;
|
|
907
|
+
return fromTranscript(sid, transcript, summary, ann);
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
module.exports = { HARNESS, list, normalise, decode, S, T, ST, modelName, foldTranscript, parseLines, parseAnnotation, storeRootDefault };
|