@tangle-network/harness-sessions 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/builder.js +2 -1
- package/dist/capture.js +16 -15
- package/dist/catalog.d.ts +14 -15
- package/dist/catalog.js +4 -14
- package/dist/formats/claude-code.js +1 -10
- package/dist/formats/codex.js +1 -11
- package/dist/formats/factory.js +19 -19
- package/dist/formats/grok.d.ts +3 -0
- package/dist/formats/grok.js +114 -0
- package/dist/formats/kimi.js +1 -16
- package/dist/formats/opencode.js +1 -12
- package/dist/formats/pi.js +1 -10
- package/dist/index.d.ts +2 -10
- package/dist/index.js +1 -0
- package/dist/registry.js +2 -1
- package/dist/schema.d.ts +0 -30
- package/dist/schema.js +0 -9
- package/package.json +1 -1
package/dist/builder.js
CHANGED
|
@@ -238,7 +238,8 @@ export class SessionBuilder {
|
|
|
238
238
|
*/
|
|
239
239
|
inferEnding(aborted) {
|
|
240
240
|
if (aborted) {
|
|
241
|
-
|
|
241
|
+
const error = aborted.error ?? this.modelCalls.at(-1)?.error ?? null;
|
|
242
|
+
this.ending = { status: error ? 'error' : 'aborted', error, at: aborted.at ?? error?.at ?? null };
|
|
242
243
|
return;
|
|
243
244
|
}
|
|
244
245
|
const last = this.modelCalls[this.modelCalls.length - 1];
|
package/dist/capture.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* The sidecar's raw-evidence archive v2 lists each execution's native session copies in
|
|
6
|
-
* `nativeSessions[]`, with the harness, the session id recorded at spawn, the copy kind and the
|
|
7
|
-
* files. A capture taken before v2 (provider-tangle's retention layout, `tangle-native-session-
|
|
8
|
-
* evidence.v1`) has no such list: it holds each execution's private runtime HOME under
|
|
9
|
-
* `__retention__/sessions/<sandbox session>/native/<source>/<root scope>/`, and its manifest names
|
|
10
|
-
* the harness and, once an attempt settled, the native session ids. Both are read through the
|
|
11
|
-
* store layouts the readers declare; no consumer matches session paths itself.
|
|
2
|
+
/** Read sidecar v2 native copies and historical provider retention through their
|
|
3
|
+
* declared stores, preserving recorded execution identities and native families.
|
|
12
4
|
*/
|
|
13
5
|
import { readdir, readFile } from 'node:fs/promises';
|
|
14
6
|
import { join } from 'node:path';
|
|
@@ -63,11 +55,18 @@ async function fromV2(archiveDir, manifest, listing) {
|
|
|
63
55
|
}
|
|
64
56
|
const path = join(archiveDir, db.path);
|
|
65
57
|
const refs = await opencodeSessionsInStore(path, null, {});
|
|
66
|
-
const
|
|
67
|
-
// Child sessions of the execution's session live in the same store.
|
|
58
|
+
const children = new Map();
|
|
68
59
|
for (const ref of refs)
|
|
69
|
-
if (ref.parentNativeSessionId
|
|
70
|
-
|
|
60
|
+
if (ref.parentNativeSessionId) {
|
|
61
|
+
const group = children.get(ref.parentNativeSessionId) ?? [];
|
|
62
|
+
group.push(ref.nativeSessionId);
|
|
63
|
+
children.set(ref.parentNativeSessionId, group);
|
|
64
|
+
}
|
|
65
|
+
const ids = new Set([copy.nativeSessionId]);
|
|
66
|
+
// Set iteration visits newly added children once, independent of store order.
|
|
67
|
+
for (const id of ids)
|
|
68
|
+
for (const child of children.get(id) ?? [])
|
|
69
|
+
ids.add(child);
|
|
71
70
|
for (const ref of refs.filter((r) => ids.has(r.nativeSessionId))) {
|
|
72
71
|
listing.sessions.push(captured({ ...ref, truncated }, copy, ref.nativeSessionId === copy.nativeSessionId, archiveDir, path));
|
|
73
72
|
}
|
|
@@ -86,7 +85,9 @@ async function fromV2(archiveDir, manifest, listing) {
|
|
|
86
85
|
}
|
|
87
86
|
const mainPath = join(archiveDir, mainFile.path);
|
|
88
87
|
const nestedFiles = copy.files.filter((f) => { const rel = inStore(storePathOf(f)); return rel !== null && nested.some((n) => n.test(rel)); }).map((f) => join(archiveDir, f.path));
|
|
89
|
-
|
|
88
|
+
const declared = store.files.map((pattern) => storeGlob(pattern, copy.nativeSessionId));
|
|
89
|
+
const files = [...new Set([mainPath, ...nestedFiles, ...copy.files.filter((f) => { const rel = inStore(storePathOf(f)); return rel !== null && declared.some((pattern) => pattern.test(rel)); }).map((f) => join(archiveDir, f.path))])];
|
|
90
|
+
listing.sessions.push(captured({ harness: reader.harness, format: store.format, nativeSessionId: copy.nativeSessionId, path: mainPath, files, home: null, cwd: null, mtimeMs: Date.parse(copy.capturedAt) || 0, parentNativeSessionId: null, truncated: mainFile.truncated }, copy, true, archiveDir, mainPath));
|
|
90
91
|
for (const file of nestedFiles) {
|
|
91
92
|
const child = file.split('/').pop().replace(/\.jsonl$/u, '').replace(/^agent-/u, '');
|
|
92
93
|
listing.sessions.push(captured({ harness: reader.harness, format: store.format, nativeSessionId: child, path: file, files: [file], home: null, cwd: null, mtimeMs: Date.parse(copy.capturedAt) || 0, parentNativeSessionId: copy.nativeSessionId, truncated }, copy, false, archiveDir, file));
|
package/dist/catalog.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const SESSION_FORMAT_IDS: readonly ["claude-code.projects-jsonl", "codex.rollout-jsonl", "opencode.sqlite", "pi.session-jsonl", "kimi.session-dir", "factory.session-jsonl", "gemini.chat-json", "amp.thread-json", "forge.sqlite", "hermes.state-sqlite", "prime.session-jsonl", "openclaw.session-jsonl"];
|
|
1
|
+
export declare const SESSION_FORMAT_IDS: readonly ["claude-code.projects-jsonl", "codex.rollout-jsonl", "opencode.sqlite", "pi.session-jsonl", "kimi.session-dir", "factory.session-jsonl", "gemini.chat-json", "amp.thread-json", "forge.sqlite", "hermes.state-sqlite", "prime.session-jsonl", "openclaw.session-jsonl", "grok.acp-session-dir"];
|
|
2
2
|
/** Store layouts, relative to the HOME a harness ran with. */
|
|
3
3
|
export declare const STORES: {
|
|
4
4
|
readonly 'claude-code.projects-jsonl': {
|
|
@@ -85,6 +85,13 @@ export declare const STORES: {
|
|
|
85
85
|
write: "jsonl-append" | "json-rewrite" | "sqlite-wal";
|
|
86
86
|
shared: boolean;
|
|
87
87
|
};
|
|
88
|
+
readonly 'grok.acp-session-dir': {
|
|
89
|
+
format: "grok.acp-session-dir";
|
|
90
|
+
root: string;
|
|
91
|
+
files: readonly ["*/{id}/updates.jsonl"];
|
|
92
|
+
write: "jsonl-append" | "json-rewrite" | "sqlite-wal";
|
|
93
|
+
shared: boolean;
|
|
94
|
+
};
|
|
88
95
|
};
|
|
89
96
|
/** Kimi Code's current and retained legacy layouts; the legacy catalog entry stays stable. */
|
|
90
97
|
export declare const KIMI_STORES: readonly [{
|
|
@@ -103,11 +110,6 @@ export declare const KIMI_STORES: readonly [{
|
|
|
103
110
|
write: "jsonl-append" | "json-rewrite" | "sqlite-wal";
|
|
104
111
|
shared: boolean;
|
|
105
112
|
}];
|
|
106
|
-
/**
|
|
107
|
-
* Where each format's served model, usage and ending error come from. `turn-context` means the
|
|
108
|
-
* model the harness was configured or asked for (Codex `turn_context`, OpenCode's message
|
|
109
|
-
* `modelID`, Factory's settings); `none` means no served identity is proven by the records.
|
|
110
|
-
*/
|
|
111
113
|
export declare const EVIDENCE: {
|
|
112
114
|
readonly 'claude-code.projects-jsonl': {
|
|
113
115
|
readonly servedModel: "provider-response";
|
|
@@ -169,16 +171,13 @@ export declare const EVIDENCE: {
|
|
|
169
171
|
readonly usage: "per-response";
|
|
170
172
|
readonly endingError: "none";
|
|
171
173
|
};
|
|
174
|
+
readonly 'grok.acp-session-dir': {
|
|
175
|
+
readonly servedModel: "session-record";
|
|
176
|
+
readonly usage: "session-total";
|
|
177
|
+
readonly endingError: "none";
|
|
178
|
+
};
|
|
172
179
|
};
|
|
173
|
-
/**
|
|
174
|
-
* Codex writes to whichever Codex home it runs with, so one HOME can hold several: `~/.codex`, a
|
|
175
|
-
* sibling such as `~/.codex-lane`, the `$CODEX_HOME` a process set, one acct home per account under
|
|
176
|
-
* `${ACCT_CONFIG:-~/.config/agent-accounts}/codex/<account>`, and on a fleet host a seat's `codex`
|
|
177
|
-
* directory. Every home keeps live rollouts in `sessions/` and archived ones in
|
|
178
|
-
* `archived_sessions/`. `STORES` declares the default home's live store, which is what a sandbox
|
|
179
|
-
* captures; the readers search every home. The same rule as tangle-tools
|
|
180
|
-
* `trace-archive/catalog.py` `classify`.
|
|
181
|
-
*/
|
|
180
|
+
/** Codex homes: `.codex`, `.codex-*`, acct homes, and a fleet seat's `codex` directory. */
|
|
182
181
|
export declare const CODEX_HOME_RULE: {
|
|
183
182
|
/** Directory names directly under HOME that are Codex homes: `.codex` and `.codex-<name>`. */
|
|
184
183
|
readonly siblings: RegExp;
|
package/dist/catalog.js
CHANGED
|
@@ -11,6 +11,7 @@ export const SESSION_FORMAT_IDS = [
|
|
|
11
11
|
'hermes.state-sqlite',
|
|
12
12
|
'prime.session-jsonl',
|
|
13
13
|
'openclaw.session-jsonl',
|
|
14
|
+
'grok.acp-session-dir',
|
|
14
15
|
];
|
|
15
16
|
/** One catalog declaration owns append-only defaults and shared SQLite copy semantics. */
|
|
16
17
|
function store(format, root, files, write = 'jsonl-append') {
|
|
@@ -38,17 +39,13 @@ export const STORES = {
|
|
|
38
39
|
'hermes.state-sqlite': store('hermes.state-sqlite', '.hermes', ['state.db', 'state.db-wal', 'state.db-shm'], 'sqlite-wal'),
|
|
39
40
|
'prime.session-jsonl': store('prime.session-jsonl', '.prime-provider/prime-agent-dirs', ['**/*_{id}.jsonl']),
|
|
40
41
|
'openclaw.session-jsonl': store('openclaw.session-jsonl', '.openclaw/agents', ['**/{id}.jsonl']),
|
|
42
|
+
'grok.acp-session-dir': store('grok.acp-session-dir', '.grok/sessions', ['*/{id}/updates.jsonl']),
|
|
41
43
|
};
|
|
42
44
|
/** Kimi Code's current and retained legacy layouts; the legacy catalog entry stays stable. */
|
|
43
45
|
export const KIMI_STORES = [
|
|
44
46
|
{ ...STORES['kimi.session-dir'], root: '.kimi-code/sessions', files: ['wd_*/{id}/**'], globs: { session: 'wd_*/{id}/agents/main/wire.jsonl' } },
|
|
45
47
|
STORES['kimi.session-dir'],
|
|
46
48
|
];
|
|
47
|
-
/**
|
|
48
|
-
* Where each format's served model, usage and ending error come from. `turn-context` means the
|
|
49
|
-
* model the harness was configured or asked for (Codex `turn_context`, OpenCode's message
|
|
50
|
-
* `modelID`, Factory's settings); `none` means no served identity is proven by the records.
|
|
51
|
-
*/
|
|
52
49
|
export const EVIDENCE = {
|
|
53
50
|
'claude-code.projects-jsonl': { servedModel: 'provider-response', usage: 'per-response', endingError: 'session-record' },
|
|
54
51
|
'codex.rollout-jsonl': { servedModel: 'turn-context', usage: 'cumulative', endingError: 'session-record' },
|
|
@@ -62,16 +59,9 @@ export const EVIDENCE = {
|
|
|
62
59
|
'hermes.state-sqlite': { servedModel: 'session-record', usage: 'session-total', endingError: 'none' },
|
|
63
60
|
'prime.session-jsonl': { servedModel: 'session-record', usage: 'per-response', endingError: 'session-record' },
|
|
64
61
|
'openclaw.session-jsonl': { servedModel: 'session-record', usage: 'per-response', endingError: 'none' },
|
|
62
|
+
'grok.acp-session-dir': { servedModel: 'session-record', usage: 'session-total', endingError: 'none' },
|
|
65
63
|
};
|
|
66
|
-
/**
|
|
67
|
-
* Codex writes to whichever Codex home it runs with, so one HOME can hold several: `~/.codex`, a
|
|
68
|
-
* sibling such as `~/.codex-lane`, the `$CODEX_HOME` a process set, one acct home per account under
|
|
69
|
-
* `${ACCT_CONFIG:-~/.config/agent-accounts}/codex/<account>`, and on a fleet host a seat's `codex`
|
|
70
|
-
* directory. Every home keeps live rollouts in `sessions/` and archived ones in
|
|
71
|
-
* `archived_sessions/`. `STORES` declares the default home's live store, which is what a sandbox
|
|
72
|
-
* captures; the readers search every home. The same rule as tangle-tools
|
|
73
|
-
* `trace-archive/catalog.py` `classify`.
|
|
74
|
-
*/
|
|
64
|
+
/** Codex homes: `.codex`, `.codex-*`, acct homes, and a fleet seat's `codex` directory. */
|
|
75
65
|
export const CODEX_HOME_RULE = {
|
|
76
66
|
/** Directory names directly under HOME that are Codex homes: `.codex` and `.codex-<name>`. */
|
|
77
67
|
siblings: /^\.codex(?:-.+)?$/u,
|
|
@@ -1,14 +1,5 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/**
|
|
3
|
-
* Claude Code: `~/.claude/projects/<cwd slug>/<session id>.jsonl`, one JSON record per line, and
|
|
4
|
-
* each subagent's own transcript at `<session id>/subagents/agent-<agent id>.jsonl`.
|
|
5
|
-
*
|
|
6
|
-
* Claude Code writes one `assistant` record per content block of an API response, repeating the
|
|
7
|
-
* response's `message.id`, `model` and `usage` on each; the blocks are one model call. It writes a
|
|
8
|
-
* failed call as an `assistant` record with `isApiErrorMessage` and model `<synthetic>`, and each
|
|
9
|
-
* retried API failure as a `system` record with subtype `api_error`. `message.model` is the model
|
|
10
|
-
* the API answered with.
|
|
11
|
-
*/
|
|
2
|
+
/** Claude Code jsonl under ~/.claude/projects. One assistant record per content block; message.model is served. */
|
|
12
3
|
import { basename, join } from 'node:path';
|
|
13
4
|
import { SessionBuilder, count, sessionError, str, textOf, usageOrNull } from '../builder.js';
|
|
14
5
|
import { fileRecords } from '../source.js';
|
package/dist/formats/codex.js
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/**
|
|
3
|
-
* Codex: `~/.codex/sessions/YYYY/MM/DD/rollout-<timestamp>-<thread id>.jsonl`, one
|
|
4
|
-
* `{timestamp, type, payload}` record per line.
|
|
5
|
-
*
|
|
6
|
-
* `response_item` records are the conversation (messages, reasoning, tool calls and their
|
|
7
|
-
* outputs); `event_msg` records are the harness's events (`token_count` after each model response,
|
|
8
|
-
* `error`, `stream_error`, `turn_aborted`, `task_complete`). Codex records the model it was
|
|
9
|
-
* configured with in `turn_context`, not the model the provider answered with, so the served
|
|
10
|
-
* model here has source `turn-context`. Usage is per response (`last_token_usage`) and cumulative
|
|
11
|
-
* (`total_token_usage`); the cumulative counter is the session total.
|
|
12
|
-
*/
|
|
2
|
+
/** Codex rollout jsonl. turn_context is the configured model. total_token_usage is the session total. */
|
|
13
3
|
import { basename } from 'node:path';
|
|
14
4
|
import { SessionBuilder, argumentsOf, count, sessionError, str, textOf, usageOrNull, } from '../builder.js';
|
|
15
5
|
import { fileRecords, isMissing } from '../source.js';
|
package/dist/formats/factory.js
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/**
|
|
3
|
-
* Factory Droid: `~/.factory/sessions/<encoded cwd>/<session id>.jsonl` plus a
|
|
4
|
-
* `<session id>.settings.json` beside it.
|
|
5
|
-
*
|
|
6
|
-
* The JSONL holds a `session_start` line (id, cwd) and `message` lines whose `message.content[]`
|
|
7
|
-
* carries Anthropic-style blocks (`text`, `thinking`, `tool_use`, `tool_result`). Each assistant
|
|
8
|
-
* line is one model response. The transcript records no model and no per-response usage: the
|
|
9
|
-
* settings file holds the configured model and the session's token totals, so the model here has
|
|
10
|
-
* source `turn-context` (configured, not served) and usage is the session total.
|
|
11
|
-
*/
|
|
2
|
+
/** Factory Droid sessions: jsonl transcript plus a sibling settings file (model and token totals). */
|
|
12
3
|
import { readFile } from 'node:fs/promises';
|
|
13
4
|
import { basename, join } from 'node:path';
|
|
14
5
|
import { SessionBuilder, count, str, textOf, usageOrNull } from '../builder.js';
|
|
@@ -95,18 +86,27 @@ export function createFold(ref, mode, stats, settings = { model: null, usage: nu
|
|
|
95
86
|
};
|
|
96
87
|
return { builder, observe, finish };
|
|
97
88
|
}
|
|
98
|
-
async function readSettings(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
89
|
+
async function readSettings(ref) {
|
|
90
|
+
const seen = new Set();
|
|
91
|
+
for (const file of [ref.path.replace(/\.jsonl$/u, '.settings.json'), ...ref.files]) {
|
|
92
|
+
if (!seen.has(file))
|
|
93
|
+
seen.add(file);
|
|
94
|
+
else
|
|
95
|
+
continue;
|
|
96
|
+
try {
|
|
97
|
+
const settings = settingsOf(JSON.parse(await readFile(file, 'utf8')));
|
|
98
|
+
if (settings.model !== null || settings.usage !== null)
|
|
99
|
+
return settings;
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (!(isMissing(error) || error instanceof SyntaxError))
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
106
105
|
}
|
|
106
|
+
return { model: null, usage: null };
|
|
107
107
|
}
|
|
108
108
|
export async function fold(ref, mode, options, source = fileRecords(ref.path, { strict: options.corruption === 'strict', signal: options.signal })) {
|
|
109
|
-
return runFold(createFold(ref, mode, source.stats, await readSettings(ref
|
|
109
|
+
return runFold(createFold(ref, mode, source.stats, await readSettings(ref)), source);
|
|
110
110
|
}
|
|
111
111
|
async function locate(home, opts) {
|
|
112
112
|
const root = join(home, STORE.root);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { basename, dirname, join } from 'node:path';
|
|
4
|
+
import { SessionBuilder, count, str, textOf, usageOrNull } from '../builder.js';
|
|
5
|
+
import { fileRecords, isMissing } from '../source.js';
|
|
6
|
+
import { EVIDENCE, STORES, mtimeMs, storeGlob, walkFiles } from '../stores.js';
|
|
7
|
+
import { runFold } from './fold.js';
|
|
8
|
+
const FORMAT = 'grok.acp-session-dir';
|
|
9
|
+
const STORE = STORES[FORMAT];
|
|
10
|
+
const HARNESS = 'grok';
|
|
11
|
+
const callId = (id) => `${id}:model`;
|
|
12
|
+
function textBlocks(value) {
|
|
13
|
+
if (typeof value === 'string')
|
|
14
|
+
return value;
|
|
15
|
+
if (Array.isArray(value))
|
|
16
|
+
return value.map(textBlocks).filter(Boolean).join('');
|
|
17
|
+
return isRecord(value) ? str(value.text) ?? textBlocks(value.content) : '';
|
|
18
|
+
}
|
|
19
|
+
function usageOf(value) {
|
|
20
|
+
if (!isRecord(value))
|
|
21
|
+
return null;
|
|
22
|
+
const bag = isRecord(value.usage) ? value.usage : isRecord(value.session) ? value.session : value;
|
|
23
|
+
return usageOrNull({
|
|
24
|
+
input: count(bag.input_tokens ?? bag.inputTokens ?? bag.prompt_tokens),
|
|
25
|
+
output: count(bag.output_tokens ?? bag.outputTokens ?? bag.completion_tokens),
|
|
26
|
+
reasoning: count(bag.reasoning_tokens ?? bag.reasoningTokens),
|
|
27
|
+
cacheRead: count(bag.cache_read_tokens ?? bag.cacheReadTokens),
|
|
28
|
+
cacheWrite: count(bag.cache_write_tokens ?? bag.cacheCreationTokens),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async function readJson(path) {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(await readFile(path, 'utf8'));
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (isMissing(error) || error instanceof SyntaxError)
|
|
37
|
+
return null;
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function createFold(ref, mode, stats, model, usage) {
|
|
42
|
+
const builder = new SessionBuilder(HARNESS, FORMAT, EVIDENCE[FORMAT].servedModel, mode, ref.nativeSessionId, ref.parentNativeSessionId);
|
|
43
|
+
let opened = false;
|
|
44
|
+
const observe = (record) => {
|
|
45
|
+
const update = isRecord(record.update) ? record.update : record;
|
|
46
|
+
const kind = str(update.sessionUpdate);
|
|
47
|
+
if (!kind)
|
|
48
|
+
return;
|
|
49
|
+
const at = builder.seen(update.timestamp ?? record.timestamp);
|
|
50
|
+
const id = str(update.toolCallId) ?? `${builder.nativeSessionId}:${stats.line}`;
|
|
51
|
+
if (kind === 'user_message_chunk') {
|
|
52
|
+
const text = textBlocks(update.content);
|
|
53
|
+
if (text)
|
|
54
|
+
builder.message({ id, role: 'user', actor: 'human', at, modelCallId: null, parts: [{ type: 'text', text }] });
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (kind === 'agent_message_chunk' || kind === 'agent_thought_chunk') {
|
|
58
|
+
if (!opened)
|
|
59
|
+
builder.modelCall({ id: callId(builder.nativeSessionId), at, provider: null, servedModel: model, requestedModel: model, usage, costUsd: null, stopReason: null, error: null });
|
|
60
|
+
opened = true;
|
|
61
|
+
const text = textBlocks(update.content);
|
|
62
|
+
const parts = kind === 'agent_thought_chunk' ? [{ type: 'reasoning', text, redacted: text.length === 0 }] : [{ type: 'text', text }];
|
|
63
|
+
if (text || kind === 'agent_thought_chunk')
|
|
64
|
+
builder.message({ id, role: 'assistant', actor: 'agent', at, modelCallId: callId(builder.nativeSessionId), parts });
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (kind === 'tool_call' && typeof update.toolCallId === 'string') {
|
|
68
|
+
builder.toolCall({ id: update.toolCallId, name: str(update.title) ?? str(update.kind) ?? 'unknown', input: update.rawInput ?? null, inputText: null, at, messageId: id });
|
|
69
|
+
builder.message({ id, role: 'assistant', actor: 'agent', at, modelCallId: callId(builder.nativeSessionId), parts: [{ type: 'tool-call', toolCallId: update.toolCallId }] });
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (kind === 'tool_call_update' && typeof update.toolCallId === 'string') {
|
|
73
|
+
builder.toolResult(update.toolCallId, { output: update.rawOutput ?? update.content ?? null, text: textOf(update.rawOutput ?? update.content), isError: update.status === 'failed', at, messageId: `${id}:tool` });
|
|
74
|
+
builder.message({ id: `${id}:tool`, role: 'tool', actor: 'tool-result', at, modelCallId: null, parts: [{ type: 'tool-result', toolCallId: update.toolCallId }] });
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
return { builder, observe, finish: () => { builder.source(stats); builder.inferEnding(); return builder; } };
|
|
78
|
+
}
|
|
79
|
+
async function fold(ref, mode, options) {
|
|
80
|
+
const records = fileRecords(ref.path, { strict: options.corruption === 'strict', signal: options.signal });
|
|
81
|
+
const dir = dirname(ref.path);
|
|
82
|
+
const summary = await readJson(join(dir, 'summary.json'));
|
|
83
|
+
const model = isRecord(summary) ? str(summary.current_model_id) : null;
|
|
84
|
+
return runFold(createFold(ref, mode, records.stats, model, usageOf(await readJson(join(dir, 'signals.json')))), records);
|
|
85
|
+
}
|
|
86
|
+
export const grokReader = {
|
|
87
|
+
harness: HARNESS,
|
|
88
|
+
aliases: ['grok-build'],
|
|
89
|
+
formats: [FORMAT],
|
|
90
|
+
stores: [STORE],
|
|
91
|
+
globs: { session: STORE.files[0] },
|
|
92
|
+
evidence: EVIDENCE[FORMAT],
|
|
93
|
+
async locate(home, opts = {}) {
|
|
94
|
+
const root = join(home, STORE.root);
|
|
95
|
+
const pattern = storeGlob(STORE.files[0], opts.nativeSessionId);
|
|
96
|
+
const refs = [];
|
|
97
|
+
for (const rel of await walkFiles(root)) {
|
|
98
|
+
if (!pattern.test(rel))
|
|
99
|
+
continue;
|
|
100
|
+
const path = join(root, rel);
|
|
101
|
+
const mtime = await mtimeMs(path);
|
|
102
|
+
const id = basename(dirname(rel));
|
|
103
|
+
if ((opts.sinceMs && mtime < opts.sinceMs) || (opts.nativeSessionId && id !== opts.nativeSessionId))
|
|
104
|
+
continue;
|
|
105
|
+
refs.push({ harness: HARNESS, format: FORMAT, nativeSessionId: id, path, files: [path], home, cwd: null, mtimeMs: mtime, parentNativeSessionId: null });
|
|
106
|
+
}
|
|
107
|
+
return refs.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
108
|
+
},
|
|
109
|
+
async read(ref, opts = {}) { return (await fold(ref, 'full', opts)).build(); },
|
|
110
|
+
async summarize(ref, opts = {}) { return (await fold(ref, 'summary', opts)).summary(); },
|
|
111
|
+
};
|
|
112
|
+
export function grokRefForFile(path) {
|
|
113
|
+
return { harness: HARNESS, format: FORMAT, nativeSessionId: basename(dirname(path)), path, files: [path], home: null, cwd: null, mtimeMs: 0, parentNativeSessionId: null };
|
|
114
|
+
}
|
package/dist/formats/kimi.js
CHANGED
|
@@ -1,20 +1,5 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/**
|
|
3
|
-
* Kimi Code: one directory per session, `~/.kimi/sessions/<workdir hash>/<session id>/`, holding
|
|
4
|
-
* `wire.jsonl` (the event stream this reader folds) and `context.jsonl` (the model context).
|
|
5
|
-
*
|
|
6
|
-
* Wire records are `{timestamp, message: {type, payload}}` with epoch-second timestamps.
|
|
7
|
-
* `TurnBegin` carries the user's input; each `StepBegin` is one model call, whose `ContentPart`s
|
|
8
|
-
* (`think`, `text`), `ToolCall`s (arguments streamed on in `ToolCallPart`s) and `StatusUpdate`
|
|
9
|
-
* (`token_usage`: `input_other`, `output`, `input_cache_read`, `input_cache_creation`, disjoint,
|
|
10
|
-
* and the provider's `message_id`) follow. `ToolResult` answers a call. `StepRetry` is a failed
|
|
11
|
-
* attempt with the provider's status code; `StepInterrupted` ends a step without an answer. A
|
|
12
|
-
* subagent's events arrive wrapped in `SubagentEvent`. The legacy wire records no model.
|
|
13
|
-
* Current `.kimi-code/sessions/wd_<hash>/session_<uuid>/agents/main/wire.jsonl` records are flat:
|
|
14
|
-
* turn prompts and loop events own messages, tools and step usage. Appended message snapshots,
|
|
15
|
-
* usage records and token-counting totals mirror that work, so are not counted again. Request
|
|
16
|
-
* metadata names the requested model; a completed native turn is required to prove completion.
|
|
17
|
-
*/
|
|
2
|
+
/** Kimi wire.jsonl: TurnBegin, StepBegin, ToolCall, StatusUpdate, ToolResult, StepRetry. Legacy wire has no model. */
|
|
18
3
|
import { basename, dirname, join } from 'node:path';
|
|
19
4
|
import { SessionBuilder, count, sessionError, str, textOf, usageOrNull } from '../builder.js';
|
|
20
5
|
import { fileRecords } from '../source.js';
|
package/dist/formats/opencode.js
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/**
|
|
3
|
-
* OpenCode 1.x: one SQLite store for every session in the HOME,
|
|
4
|
-
* `~/.local/share/opencode/opencode.db` (with `-wal` and `-shm` siblings while it is open).
|
|
5
|
-
*
|
|
6
|
-
* `session` rows carry the cwd, the parent session and session totals. `message` rows carry a JSON
|
|
7
|
-
* `data` blob: role, the provider and model the message was sent to, tokens, cost, finish and the
|
|
8
|
-
* error that ended it. `part` rows carry the content: `text`, `reasoning`, `tool` (the call and
|
|
9
|
-
* its result in one row), `step-start` and `step-finish` (one model response, with its tokens and
|
|
10
|
-
* cost), `retry` (a failed attempt) and bookkeeping parts. The store is opened read-only where it
|
|
11
|
-
* lies, so a read sees the rows a running OpenCode has committed to its write-ahead log and never
|
|
12
|
-
* writes the store, its log or a copy of it.
|
|
13
|
-
*/
|
|
2
|
+
/** OpenCode sqlite at ~/.local/share/opencode/opencode.db. Opened read-only, including the wal. */
|
|
14
3
|
import { createHash } from 'node:crypto';
|
|
15
4
|
import { join } from 'node:path';
|
|
16
5
|
import { SessionBuilder, count, isoTime, sessionError, str, usageOrNull } from '../builder.js';
|
package/dist/formats/pi.js
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/**
|
|
3
|
-
* Pi: `~/.pi/agent/sessions/<encoded cwd>/<timestamp>_<session id>.jsonl`, one entry per line.
|
|
4
|
-
*
|
|
5
|
-
* The first entry is the `session` header (id, cwd). Conversation entries are `message` entries
|
|
6
|
-
* whose `message.role` is `user`, `assistant` or `toolResult`. Each assistant message records the
|
|
7
|
-
* provider and model it was sent to, its usage (input, output and cache counts are disjoint, with a
|
|
8
|
-
* cost breakdown), its stop reason and, on a failure, the provider's error message. Tool calls are
|
|
9
|
-
* `toolCall` blocks inside assistant content, answered by `toolResult` messages keyed by
|
|
10
|
-
* `toolCallId`. Entries form a tree through `parentId`; branches share one file.
|
|
11
|
-
*/
|
|
2
|
+
/** Pi jsonl under ~/.pi/agent/sessions. Session header, then message entries. Usage fields are disjoint. */
|
|
12
3
|
import { basename, join } from 'node:path';
|
|
13
4
|
import { SessionBuilder, count, sessionError, str, textOf, usageOrNull } from '../builder.js';
|
|
14
5
|
import { fileRecords } from '../source.js';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `@tangle-network/harness-sessions` — one reader for every coding-agent harness's native session.
|
|
3
|
-
*
|
|
4
|
-
* Every harness persists its session in its own store (Claude Code and Pi append JSONL, Codex
|
|
5
|
-
* appends a rollout, OpenCode writes SQLite). This package declares each store's layout once and
|
|
6
|
-
* parses each format into one normalized {@link HarnessSession}: messages, tool calls with their
|
|
7
|
-
* inputs and results, the model calls with the model that served them, usage and the error a
|
|
8
|
-
* session ended with. The sidecar's capture contract, the Discovery gate, agent-eval,
|
|
9
|
-
* agent-record and the blog read sessions only through it.
|
|
10
|
-
*/
|
|
1
|
+
/** One normalized reader for each coding-agent harness session store. */
|
|
11
2
|
export type { HarnessSession, HarnessSessionReader, LocateOptions, ModelCall, NativeSessionCopy, NativeSessionStore, Part, RawEvidenceArchiveManifestV2, ReadOptions, SessionEnding, SessionError, SessionEvidenceSources, SessionFormatId, SessionIntegrity, SessionMessage, SessionRef, SessionSummary, SessionToolCall, TokenUsage, } from './schema.js';
|
|
12
3
|
export { HARNESS_SESSION_SCHEMA } from './schema.js';
|
|
13
4
|
export { CODEX_HOME_RULE, EVIDENCE, SESSION_FORMAT_IDS, STORES, assertStoreRoot, isCodexHome, storeGlob, withinPath } from './catalog.js';
|
|
@@ -20,6 +11,7 @@ export { opencodeReader, opencodeSessionsInStore } from './formats/opencode.js';
|
|
|
20
11
|
export { piReader, piRefForFile } from './formats/pi.js';
|
|
21
12
|
export { kimiReader, kimiRefForFile } from './formats/kimi.js';
|
|
22
13
|
export { factoryReader, factoryRefForFile } from './formats/factory.js';
|
|
14
|
+
export { grokReader, grokRefForFile } from './formats/grok.js';
|
|
23
15
|
export { listCapture, readCapture, RETENTION_MANIFEST_KIND, RETENTION_SESSIONS, type CaptureListing, type CapturedSessionRef, } from './capture.js';
|
|
24
16
|
export { SessionParseError } from './source.js';
|
|
25
17
|
export { servedModelKey } from './builder.js';
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export { opencodeReader, opencodeSessionsInStore } from './formats/opencode.js';
|
|
|
9
9
|
export { piReader, piRefForFile } from './formats/pi.js';
|
|
10
10
|
export { kimiReader, kimiRefForFile } from './formats/kimi.js';
|
|
11
11
|
export { factoryReader, factoryRefForFile } from './formats/factory.js';
|
|
12
|
+
export { grokReader, grokRefForFile } from './formats/grok.js';
|
|
12
13
|
export { listCapture, readCapture, RETENTION_MANIFEST_KIND, RETENTION_SESSIONS, } from './capture.js';
|
|
13
14
|
export { SessionParseError } from './source.js';
|
|
14
15
|
export { servedModelKey } from './builder.js';
|
package/dist/registry.js
CHANGED
|
@@ -2,9 +2,10 @@ import { claudeCodeReader } from './formats/claude-code.js';
|
|
|
2
2
|
import { codexReader } from './formats/codex.js';
|
|
3
3
|
import { kimiReader } from './formats/kimi.js';
|
|
4
4
|
import { factoryReader } from './formats/factory.js';
|
|
5
|
+
import { grokReader } from './formats/grok.js';
|
|
5
6
|
import { opencodeReader } from './formats/opencode.js';
|
|
6
7
|
import { piReader } from './formats/pi.js';
|
|
7
|
-
const READERS = [claudeCodeReader, codexReader, opencodeReader, piReader, kimiReader, factoryReader];
|
|
8
|
+
const READERS = [claudeCodeReader, codexReader, opencodeReader, piReader, kimiReader, factoryReader, grokReader];
|
|
8
9
|
const BY_NAME = new Map();
|
|
9
10
|
for (const reader of READERS) {
|
|
10
11
|
BY_NAME.set(reader.harness, reader);
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,50 +1,24 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The normalized form of one harness session, and the store layouts every harness declares.
|
|
3
|
-
*
|
|
4
|
-
* Each coding-agent harness persists its own native session: Claude Code and Pi append JSONL,
|
|
5
|
-
* Codex appends a rollout JSONL, OpenCode writes SQLite. Every consumer that needs to know what
|
|
6
|
-
* happened in a turn reads it through this one shape, so a count, a served model or an ending
|
|
7
|
-
* error means the same thing in the sidecar, the Discovery gate, agent-eval, agent-record and the
|
|
8
|
-
* blog.
|
|
9
|
-
*/
|
|
10
1
|
/** One native session format. The id names the harness family and the on-disk layout. */
|
|
11
2
|
export type SessionFormatId = (typeof import('./catalog.js').SESSION_FORMAT_IDS)[number];
|
|
12
|
-
/**
|
|
13
|
-
* Where a harness keeps its sessions, relative to the HOME it ran with. Capture consumers can
|
|
14
|
-
* share these declarations; consumer adoption of a producer layout is a separate change.
|
|
15
|
-
*/
|
|
16
3
|
export interface NativeSessionStore {
|
|
17
|
-
/** Relative to the execution's private runtime HOME. Normalized, never absolute, never `..`. */
|
|
18
4
|
root: string;
|
|
19
|
-
/** Globs under `root` that hold one session. `{id}` is replaced by the native session id. */
|
|
20
5
|
files: readonly string[];
|
|
21
|
-
/** How the harness writes. This decides how a copy is taken while the harness runs. */
|
|
22
6
|
write: 'jsonl-append' | 'json-rewrite' | 'sqlite-wal';
|
|
23
|
-
/** True when one store holds every session in the HOME (opencode.db). Readers select by id. */
|
|
24
7
|
shared: boolean;
|
|
25
8
|
format: SessionFormatId;
|
|
26
|
-
/** Reader files when this layout differs from the harness's default globs. */
|
|
27
9
|
globs?: {
|
|
28
10
|
session: string;
|
|
29
11
|
children?: string;
|
|
30
12
|
};
|
|
31
13
|
}
|
|
32
|
-
/** Where a format's served-model, usage and ending-error evidence comes from. */
|
|
33
14
|
export interface SessionEvidenceSources {
|
|
34
|
-
/** `none`: no proven served identity (including Kimi request labels); served models stay empty. */
|
|
35
15
|
servedModel: 'provider-response' | 'session-record' | 'turn-context' | 'none';
|
|
36
16
|
usage: 'per-response' | 'cumulative' | 'session-total';
|
|
37
17
|
endingError: 'session-record' | 'none';
|
|
38
18
|
}
|
|
39
19
|
export interface TokenUsage {
|
|
40
|
-
/**
|
|
41
|
-
* Prompt tokens that were neither read from nor written to a provider cache. Cache reads and
|
|
42
|
-
* cache writes are counted in their own fields and never inside `input`, so every field can be
|
|
43
|
-
* priced at its own rate. A harness that reports an inclusive input (Codex) is converted.
|
|
44
|
-
*/
|
|
45
20
|
input: number | null;
|
|
46
21
|
output: number | null;
|
|
47
|
-
/** Reasoning tokens as the harness recorded them; providers count them inside `output`. */
|
|
48
22
|
reasoning: number | null;
|
|
49
23
|
cacheRead: number | null;
|
|
50
24
|
cacheWrite: number | null;
|
|
@@ -78,10 +52,6 @@ export type Part = {
|
|
|
78
52
|
export interface SessionMessage {
|
|
79
53
|
id: string;
|
|
80
54
|
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
81
|
-
/**
|
|
82
|
-
* Who produced the message. `injected` is text the harness or the runtime added to the
|
|
83
|
-
* conversation (environment context, instructions, error notices), not the human's prompt.
|
|
84
|
-
*/
|
|
85
55
|
actor: 'human' | 'agent' | 'subagent-spawn' | 'injected' | 'tool-result';
|
|
86
56
|
at: string | null;
|
|
87
57
|
/** The model call that produced an assistant message; null for every other message. */
|
package/dist/schema.js
CHANGED
|
@@ -1,10 +1 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The normalized form of one harness session, and the store layouts every harness declares.
|
|
3
|
-
*
|
|
4
|
-
* Each coding-agent harness persists its own native session: Claude Code and Pi append JSONL,
|
|
5
|
-
* Codex appends a rollout JSONL, OpenCode writes SQLite. Every consumer that needs to know what
|
|
6
|
-
* happened in a turn reads it through this one shape, so a count, a served model or an ending
|
|
7
|
-
* error means the same thing in the sidecar, the Discovery gate, agent-eval, agent-record and the
|
|
8
|
-
* blog.
|
|
9
|
-
*/
|
|
10
1
|
export const HARNESS_SESSION_SCHEMA = 'tangle.harness-session.v1';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tangle-network/harness-sessions",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "One reader for every coding-agent harness's native session (Claude Code, Codex, OpenCode, Pi): store layouts, a normalized session with messages, tool calls, served models, usage and ending errors, and projections to chat messages, OTLP spans and turns",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|