@fatelabs/pi-estelle 0.0.0-stage → 0.0.18
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/LICENSE +201 -0
- package/NOTICE +6 -0
- package/README.md +44 -2
- package/adapters/claude.mjs +17 -0
- package/adapters/codex.mjs +85 -0
- package/adapters/cursor.mjs +66 -0
- package/adapters/dsh-host.mjs +181 -0
- package/adapters/dsh-plugin.mjs +11 -0
- package/adapters/dsh.mjs +54 -0
- package/adapters/grok.mjs +63 -0
- package/adapters/host-bridge.mjs +85 -0
- package/adapters/kimi.mjs +135 -0
- package/adapters/opencode-mirror.mjs +103 -0
- package/adapters/opencode-plugin.mjs +143 -0
- package/adapters/opencode.mjs +15 -0
- package/adapters/pi-extension.mjs +107 -0
- package/adapters/pi.mjs +13 -0
- package/adapters/plugin-envelope.mjs +56 -0
- package/commands/cloud.md +28 -0
- package/commands/code.md +6 -0
- package/commands/handoff.md +25 -0
- package/commands/monitor.md +6 -0
- package/commands/resume.md +6 -0
- package/commands/review.md +6 -0
- package/commands/rooms.md +7 -0
- package/instructions.md +19 -0
- package/package.json +38 -4
- package/runtime/auth.mjs +27 -0
- package/runtime/checkpoint.mjs +208 -0
- package/runtime/claude-setup.mjs +130 -0
- package/runtime/cli.mjs +112 -0
- package/runtime/codex-trust.mjs +68 -0
- package/runtime/credential.mjs +341 -0
- package/runtime/dsh-session.mjs +91 -0
- package/runtime/dsh-telemetry.mjs +31 -0
- package/runtime/hooks.mjs +234 -0
- package/runtime/identity.mjs +20 -0
- package/runtime/io.mjs +155 -0
- package/runtime/key-check.mjs +51 -0
- package/runtime/kimi-history.mjs +163 -0
- package/runtime/mailbox.mjs +46 -0
- package/runtime/migration.mjs +118 -0
- package/runtime/move.mjs +443 -0
- package/runtime/native-export.mjs +204 -0
- package/runtime/native-index.mjs +77 -0
- package/runtime/native-tree.mjs +117 -0
- package/runtime/queued-context.mjs +152 -0
- package/runtime/redact.mjs +74 -0
- package/runtime/reindex.mjs +259 -0
- package/runtime/setup.mjs +174 -0
- package/runtime/stream.mjs +755 -0
- package/runtime/version.mjs +2 -0
- package/runtime/write-warning.mjs +67 -0
- package/skills/abstain-right/SKILL.md +19 -0
- package/skills/api-call-ground/SKILL.md +19 -0
- package/skills/bug-hunt/SKILL.md +20 -0
- package/skills/chain-of-verification/SKILL.md +19 -0
- package/skills/change-impact/SKILL.md +19 -0
- package/skills/coding-workflow/SKILL.md +20 -0
- package/skills/commit-guard/SKILL.md +20 -0
- package/skills/context-checkpoint/SKILL.md +19 -0
- package/skills/continuity-brief/SKILL.md +19 -0
- package/skills/dead-code-sweep/SKILL.md +19 -0
- package/skills/deep-search/SKILL.md +19 -0
- package/skills/diff-review/SKILL.md +19 -0
- package/skills/estelle/SKILL.md +119 -0
- package/skills/estelle-loop/SKILL.md +19 -0
- package/skills/estelle-monitor-install/SKILL.md +20 -0
- package/skills/evidence-traceability-audit/SKILL.md +19 -0
- package/skills/grounded-generate/SKILL.md +19 -0
- package/skills/incident-rca/SKILL.md +20 -0
- package/skills/mutation-test/SKILL.md +19 -0
- package/skills/program-repair/SKILL.md +20 -0
- package/skills/prompt-injection-defense/SKILL.md +19 -0
- package/skills/property-based-test/SKILL.md +19 -0
- package/skills/refactor-rfc/SKILL.md +19 -0
- package/skills/root-cause-loop/SKILL.md +19 -0
- package/skills/test-first/SKILL.md +19 -0
- package/skills/threat-model-stride/SKILL.md +19 -0
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
// Port of the public Estelle CLI transcript transport. No hosted backend implementation.
|
|
2
|
+
import { open, mkdir, stat, readFile, writeFile, rename, unlink, opendir } from 'node:fs/promises';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
5
|
+
import { cap, sanitize, FIELD_CAP } from './redact.mjs';
|
|
6
|
+
import { timelineIdentity } from './identity.mjs';
|
|
7
|
+
import { normalizeKimiRecord } from './kimi-history.mjs';
|
|
8
|
+
|
|
9
|
+
export const LIMITS = Object.freeze({
|
|
10
|
+
readBytes: 8_000_000, skipBytes: 256_000_000, batchEvents: 50, batchBytes: 2_000_000,
|
|
11
|
+
claudeEvents: 200, codexEvents: 1_000, codexWindows: 4, lines: 10_000,
|
|
12
|
+
spoolBytes: 64 * 1024 * 1024, spoolFiles: 4096, drainFiles: 64, stateBytes: 32_000,
|
|
13
|
+
messageBlocks: 100, directoryEntries: 2_000, prResultBytes: 4096,
|
|
14
|
+
subagents: 40, subagentEvents: 400,
|
|
15
|
+
});
|
|
16
|
+
const INJECTED = ['<system-reminder', '<command-', '<local-command', '<task-notification', '[SYSTEM NOTIFICATION', 'Caveat:'];
|
|
17
|
+
const CODEX_INJECTED = [...INJECTED, '<environment_context', '<user_instructions', '# AGENTS.md instructions', '<skill', '<turn_aborted', '<user_shell_command'];
|
|
18
|
+
const AGENTS = { claude: 'claude-code', 'claude-code': 'claude-code', codex: 'codex', kimi: 'kimi-code', 'kimi-code': 'kimi-code',
|
|
19
|
+
grok: 'grok', cursor: 'cursor', dsh: 'deepseek-harness', 'deepseek-harness': 'deepseek-harness', opencode: 'opencode', pi: 'pi' };
|
|
20
|
+
const WAITING = 'waiting_for_review/input_required';
|
|
21
|
+
const string = value => typeof value === 'string' ? value : '';
|
|
22
|
+
const validTime = value => typeof value === 'string' && Number.isFinite(Date.parse(value));
|
|
23
|
+
export const safeSession = value => typeof value === 'string' && /^[A-Za-z0-9_-]{1,200}$/.test(value);
|
|
24
|
+
const event = (event_id, kind, author, at, payload) => ({ event_id, kind, author, at, payload });
|
|
25
|
+
const agent = id => ({ kind: 'agent', id });
|
|
26
|
+
const lifecycle = (id, at, state, reason) => event(id, 'lifecycle', { kind: 'system', id: 'estelle-hook' }, at, { state, reason });
|
|
27
|
+
const textOf = value => typeof value === 'string' ? value : Array.isArray(value) ? value.map(b => string(b.text)).filter(Boolean).join('\n') : '';
|
|
28
|
+
const injected = (text, prefixes) => prefixes.some(prefix => text.trimStart().startsWith(prefix));
|
|
29
|
+
|
|
30
|
+
function messageEvents(blocks, role, id, at, harness, notes, sidechain = false, child = false) {
|
|
31
|
+
const out = [];
|
|
32
|
+
if (blocks.length > LIMITS.messageBlocks) throw new Error('transcript message exceeds block bound; cursor retained');
|
|
33
|
+
for (const [index, block] of blocks.entries()) {
|
|
34
|
+
if (!block || typeof block !== 'object' || Array.isArray(block)) continue;
|
|
35
|
+
const key = `${id}:${index}`;
|
|
36
|
+
const text = string(block.text);
|
|
37
|
+
if (['text', 'input_text', 'output_text'].includes(block.type) && text.trim()) {
|
|
38
|
+
if (role === 'user' && injected(text, harness === 'codex' ? CODEX_INJECTED : INJECTED)) continue;
|
|
39
|
+
if (role === 'user' && !sidechain) notes.title ??= text.trim().split('\n')[0].slice(0, 90);
|
|
40
|
+
out.push(event(key, role === 'user' && !sidechain ? 'customer_message' : 'agent_message',
|
|
41
|
+
role === 'user' && !sidechain ? { kind: 'customer' } : agent(sidechain || child ? 'subagent' : harness), at,
|
|
42
|
+
{ text: cap(text), ...(sidechain ? { subagent: true } : {}) }));
|
|
43
|
+
} else if (role === 'user' && ['image', 'input_image'].includes(block.type)) {
|
|
44
|
+
out.push(event(key, 'artifact', { kind: 'customer' }, at, { title: 'Image you attached', media: 'image', stored: false }));
|
|
45
|
+
} else if (block.type === 'tool_result') {
|
|
46
|
+
out.push(event(key, 'tool_result', agent(harness), at, { call_id: block.tool_use_id ?? null, ok: block.is_error !== true, output: cap(textOf(block.content)) }));
|
|
47
|
+
} else if (role === 'assistant' && block.type === 'tool_use') {
|
|
48
|
+
out.push(harness === 'cursor' ? cursorTool(block, key, at, sidechain, child) : claudeTool(block, key, at, sidechain, child, harness));
|
|
49
|
+
} else if (role === 'assistant' && block.type === 'thinking' && string(block.thinking).trim()) {
|
|
50
|
+
out.push(event(key, 'thinking', agent(harness), at, { text: cap(block.thinking) }));
|
|
51
|
+
}
|
|
52
|
+
// Only public transcript text is transported; private/encrypted reasoning is never recovered.
|
|
53
|
+
}
|
|
54
|
+
if (role === 'user' && !sidechain && !child && out.some(item => item.kind === 'customer_message')) {
|
|
55
|
+
out.push(lifecycle(`${id}:input`, at, 'running', 'input_received'));
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// OpenCode names MCP tools `<server>_<tool>` (Estelle_verify); Claude Code and Pi name them `mcp__<server>__<tool>`.
|
|
61
|
+
const ESTELLE_TOOL = /mcp__estelle__|^estelle_/i;
|
|
62
|
+
function claudeTool(block, id, at, sidechain, child, harness = 'claude-code') {
|
|
63
|
+
const name = string(block.name) || 'tool';
|
|
64
|
+
const input = block.input ?? {};
|
|
65
|
+
const who = sidechain || child ? 'subagent' : harness;
|
|
66
|
+
if (child && name === 'SubagentHandback') return event(id, 'agent_message', agent(who), at, { text: cap(string(input.message)), report: true });
|
|
67
|
+
if (['Write', 'Edit', 'MultiEdit'].includes(name)) {
|
|
68
|
+
const edits = name === 'MultiEdit' ? input.edits ?? [] : [input];
|
|
69
|
+
const diff = name === 'Write' ? string(input.content).split('\n').map(line => `+${line}`).join('\n')
|
|
70
|
+
: edits.flatMap(edit => [...string(edit.old_string).split('\n').map(line => `-${line}`), ...string(edit.new_string).split('\n').map(line => `+${line}`)]).join('\n');
|
|
71
|
+
return event(id, 'edit', agent(who), at, { path: string(input.file_path), diff: cap(diff), call_id: block.id ?? null });
|
|
72
|
+
}
|
|
73
|
+
const summary = name === 'Bash' ? `Ran ${string(input.description) || string(input.command).slice(0, 80)}` : name === 'Read' ? `Read ${string(input.file_path)}` : name;
|
|
74
|
+
return event(id, 'tool_call', { kind: ESTELLE_TOOL.test(name) ? 'estelle' : 'agent', id: who }, at,
|
|
75
|
+
{ call_id: block.id ?? null, tool: name, summary: cap(summary), input: sanitize({ ...input, ...(input.file_path ? { path: input.file_path } : {}) }) });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Cursor tool_use blocks follow the Anthropic shape but with Cursor's names: `path` not `file_path`, `contents` on Write. */
|
|
79
|
+
function cursorTool(block, id, at, sidechain, child) {
|
|
80
|
+
const name = string(block.name) || 'tool';
|
|
81
|
+
const input = block.input ?? {};
|
|
82
|
+
const who = sidechain || child ? 'subagent' : 'cursor';
|
|
83
|
+
const path = string(input.path) || string(input.file_path);
|
|
84
|
+
if (['Write', 'StrReplace', 'Edit'].includes(name)) {
|
|
85
|
+
const diff = name === 'Write' ? string(input.contents ?? input.content).split('\n').map(line => `+${line}`).join('\n')
|
|
86
|
+
: [...string(input.old_string).split('\n').map(line => `-${line}`), ...string(input.new_string).split('\n').map(line => `+${line}`)].join('\n');
|
|
87
|
+
return event(id, 'edit', agent(who), at, { path, diff: cap(diff), call_id: block.id ?? null });
|
|
88
|
+
}
|
|
89
|
+
const summary = name === 'Shell' ? `Ran ${string(input.description) || string(input.command).slice(0, 80)}` : name === 'Read' ? `Read ${path}` : name;
|
|
90
|
+
return event(id, 'tool_call', { kind: /estelle/i.test(name) ? 'estelle' : 'agent', id: who }, at,
|
|
91
|
+
{ call_id: block.id ?? null, tool: name, summary: cap(summary), input: sanitize({ ...input, ...(path ? { path } : {}) }) });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Claude JSONL UUIDs and Codex rollout byte offsets remain the server's replay identity. */
|
|
95
|
+
export function mapClaude(entry, offset, notes = {}, child = false) {
|
|
96
|
+
if (!entry || !validTime(entry.timestamp) || !string(entry.uuid)) return [];
|
|
97
|
+
const { timestamp: at, uuid: id } = entry;
|
|
98
|
+
if (entry.type === 'system' && entry.subtype === 'turn_duration' && !child && !entry.isSidechain) {
|
|
99
|
+
return [lifecycle(`${id}:turn-end`, at, 'waiting_for_review', 'input_required')];
|
|
100
|
+
}
|
|
101
|
+
if (entry.type === 'attachment') return claudeAttachment(entry, notes, child);
|
|
102
|
+
if (!['user', 'assistant'].includes(entry.type) || entry.isMeta || entry.isCompactSummary) return [];
|
|
103
|
+
const content = entry.message?.content;
|
|
104
|
+
const blocks = typeof content === 'string' ? [{ type: 'text', text: content }] : Array.isArray(content) ? content : [];
|
|
105
|
+
// OpenCode's plugin writes its session in this same shape (adapters/opencode-mirror.mjs); notes.harness labels it.
|
|
106
|
+
const out = messageEvents(blocks, entry.type, id, at, notes.harness ?? 'claude-code', notes, entry.isSidechain === true, child);
|
|
107
|
+
if (entry.type === 'assistant' && entry.message?.usage && out.length) {
|
|
108
|
+
const usage = entry.message.usage;
|
|
109
|
+
out[0].payload.usage = { input: usage.input_tokens ?? 0, output: usage.output_tokens ?? 0,
|
|
110
|
+
cache_read: usage.cache_read_input_tokens ?? 0, cache_write: usage.cache_creation_input_tokens ?? 0, model: entry.message.model ?? null };
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function claudeAttachment(entry, notes, child) {
|
|
116
|
+
const attachment = entry.attachment ?? {};
|
|
117
|
+
if (attachment.type === 'queued_command') {
|
|
118
|
+
const text = textOf(attachment.prompt).trim();
|
|
119
|
+
if (!text || injected(text, INJECTED)) return [];
|
|
120
|
+
notes.title ??= text.split('\n')[0].slice(0, 90);
|
|
121
|
+
return [event(`${entry.uuid}:queued`, 'customer_message', { kind: 'customer' }, entry.timestamp, { text: cap(text) }),
|
|
122
|
+
...(child ? [] : [lifecycle(`${entry.uuid}:input`, entry.timestamp, 'running', 'input_received')])];
|
|
123
|
+
}
|
|
124
|
+
const hook = attachment.hookEvent || attachment.hookName;
|
|
125
|
+
if (child || attachment.type !== 'hook_additional_context' || hook !== 'UserPromptSubmit') return [];
|
|
126
|
+
const parts = typeof attachment.content === 'string' ? [attachment.content] : Array.isArray(attachment.content) ? attachment.content : [];
|
|
127
|
+
const mine = parts.filter(part => typeof part === 'string' && /estelle/i.test(part));
|
|
128
|
+
if (!mine.length) return [];
|
|
129
|
+
return [event(`${entry.uuid}:estelle-context`, 'tool_result', { kind: 'estelle', id: 'estelle-hook' }, entry.timestamp,
|
|
130
|
+
{ source: 'estelle-hook', hook, output: cap(mine.join('\n\n')), parts: mine.length })];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Core rollout schema, not app-server v2: openai/codex rust-v0.166.0
|
|
134
|
+
// protocol/src/items.rs (TurnItem) and protocol/src/protocol.rs (FileChange).
|
|
135
|
+
// Stateless metadata artifacts do not claim canonical commands, edits, PR links or semantic deduplication.
|
|
136
|
+
const PR_CANDIDATE_SOURCE = 'codex.gh_pr_create_candidate';
|
|
137
|
+
const NATIVE_COMPLETION_STATUSES = new Set(['completed', 'failed', 'declined']);
|
|
138
|
+
const nativeText = (value, limit = FIELD_CAP) => typeof value === 'string' && value.length > 0 && value.length <= limit ? value : null;
|
|
139
|
+
|
|
140
|
+
function nativeCommandMetadata(item, unavailable) {
|
|
141
|
+
const argv = Array.isArray(item.command) && item.command.length > 0 && item.command.length <= LIMITS.messageBlocks
|
|
142
|
+
&& item.command.every(value => typeof value === 'string')
|
|
143
|
+
&& item.command.reduce((n, value) => n + value.length, 0) <= FIELD_CAP ? item.command : null;
|
|
144
|
+
const cwd = nativeText(item.cwd);
|
|
145
|
+
const recorded = Number.isInteger(item.exit_code) && item.exit_code >= -2147483648 && item.exit_code <= 2147483647;
|
|
146
|
+
const exitState = recorded ? 'recorded' : item.exit_code == null ? 'unrecorded' : 'invalid';
|
|
147
|
+
if (argv === null) unavailable.push('argv');
|
|
148
|
+
if (cwd === null) unavailable.push('cwd');
|
|
149
|
+
if (!recorded) unavailable.push('exit_code');
|
|
150
|
+
return { argv, cwd, exit_code: recorded ? item.exit_code : null, exit_code_state: exitState };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function nativeChangesMetadata(item, unavailable) {
|
|
154
|
+
const changes = item.changes;
|
|
155
|
+
const paths = changes && typeof changes === 'object' && !Array.isArray(changes) ? Object.keys(changes) : null;
|
|
156
|
+
if (paths === null || paths.length > LIMITS.messageBlocks) {
|
|
157
|
+
unavailable.push('changes');
|
|
158
|
+
return { changes: null, change_count: null };
|
|
159
|
+
}
|
|
160
|
+
const rows = [];
|
|
161
|
+
let chars = 0;
|
|
162
|
+
for (const path of paths) {
|
|
163
|
+
const change = changes[path];
|
|
164
|
+
const kind = change?.type;
|
|
165
|
+
const move = kind === 'update' ? change.move_path : null;
|
|
166
|
+
const body = kind === 'update' ? change?.unified_diff : change?.content;
|
|
167
|
+
chars += path.length + (typeof move === 'string' ? move.length : 0);
|
|
168
|
+
if (!path || chars > FIELD_CAP || !['add', 'delete', 'update'].includes(kind)
|
|
169
|
+
|| typeof body !== 'string' || (kind === 'update' && move !== null && typeof move !== 'string')) {
|
|
170
|
+
unavailable.push('changes');
|
|
171
|
+
return { changes: null, change_count: null };
|
|
172
|
+
}
|
|
173
|
+
rows.push({ path, change_type: kind, move_path: move });
|
|
174
|
+
}
|
|
175
|
+
return { changes: rows, change_count: rows.length };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function nativeCompletion(item, id, at) {
|
|
179
|
+
if (!item || !['CommandExecution', 'FileChange'].includes(item.type)) return [];
|
|
180
|
+
const unavailable = [];
|
|
181
|
+
const nativeId = nativeText(item.id, 200);
|
|
182
|
+
const status = NATIVE_COMPLETION_STATUSES.has(item.status) ? item.status : null;
|
|
183
|
+
if (nativeId === null) unavailable.push('native_id');
|
|
184
|
+
if (status === null) unavailable.push('status');
|
|
185
|
+
const detail = item.type === 'CommandExecution' ? nativeCommandMetadata(item, unavailable) : nativeChangesMetadata(item, unavailable);
|
|
186
|
+
let completion = 'unknown';
|
|
187
|
+
if (unavailable.length === 0) {
|
|
188
|
+
completion = status === 'completed' ? 'succeeded' : status;
|
|
189
|
+
if (item.type === 'CommandExecution' && ((status === 'completed' && detail.exit_code !== 0)
|
|
190
|
+
|| (status === 'failed' && detail.exit_code === 0))) completion = 'inconsistent';
|
|
191
|
+
}
|
|
192
|
+
return [event(`${id}:0`, 'artifact', agent('codex'), at, sanitize({
|
|
193
|
+
title: item.type === 'CommandExecution' ? 'Codex command completion recorded' : 'Codex file-change completion recorded',
|
|
194
|
+
source: 'codex.event_msg.item_completed', schema: 'codex-rust-v0.166.0', native_type: item.type,
|
|
195
|
+
native_id: nativeId, status, completion, ...detail, metadata_complete: unavailable.length === 0,
|
|
196
|
+
unavailable_fields: unavailable, capture_scope: 'native completion metadata only; canonical work and PR association unverified',
|
|
197
|
+
output_stored: false, diff_stored: false,
|
|
198
|
+
}))];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// A small single-command recognizer, not a general shell interpreter. Unsupported syntax stays metadata.
|
|
202
|
+
function singleShellWords(text) {
|
|
203
|
+
if (typeof text !== 'string' || text.length > FIELD_CAP || /[\x00-\x1f]/.test(text)) return null;
|
|
204
|
+
const words = []; let part = '', quote = '', escaped = false, started = false;
|
|
205
|
+
for (const ch of text) {
|
|
206
|
+
if (quote === "'") { if (ch === "'") quote = ''; else part += ch; continue; }
|
|
207
|
+
if (escaped) { part += ch; escaped = false; continue; }
|
|
208
|
+
if (ch === '\\') { escaped = true; started = true; continue; }
|
|
209
|
+
if (ch === '$' || ch === '`') return null;
|
|
210
|
+
if (quote) { if (ch === quote) quote = ''; else part += ch; continue; }
|
|
211
|
+
if (ch === '"' || ch === "'") { quote = ch; started = true; continue; }
|
|
212
|
+
if (/[;&|<>(){}#]/.test(ch)) return null;
|
|
213
|
+
if (ch === ' ') { if (started) words.push(part); part = ''; started = false; }
|
|
214
|
+
else { part += ch; started = true; }
|
|
215
|
+
}
|
|
216
|
+
if (quote || escaped) return null;
|
|
217
|
+
if (started) words.push(part);
|
|
218
|
+
return words.length <= LIMITS.messageBlocks ? words : null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function observedPrCandidate(item, id, at, repository, original) {
|
|
222
|
+
if (original.payload.completion !== 'succeeded' || !repository || typeof item.aggregated_output !== 'string'
|
|
223
|
+
|| Buffer.byteLength(item.aggregated_output) > LIMITS.prResultBytes) return [];
|
|
224
|
+
let argv = original.payload.argv;
|
|
225
|
+
if (argv?.length === 3 && /(?:^|\/)(?:sh|bash|zsh)$/.test(argv[0]) && ['-c', '-lc'].includes(argv[1])) {
|
|
226
|
+
argv = singleShellWords(argv[2]);
|
|
227
|
+
}
|
|
228
|
+
if (!argv || !/(?:^|\/)gh$/.test(argv[0]) || argv[1] !== 'pr' || argv[2] !== 'create') return [];
|
|
229
|
+
const url = item.aggregated_output.trim();
|
|
230
|
+
const match = /^https:\/\/github\.com\/([A-Za-z0-9_.-]{1,100}\/[A-Za-z0-9_.-]{1,100})\/pull\/([1-9][0-9]{0,7})$/.exec(url);
|
|
231
|
+
if (!match || match[1] !== repository || match[1].includes('..') || Number(match[2]) > 10_000_000) return [];
|
|
232
|
+
return [event(`${id}:pr-candidate`, 'artifact', agent('codex'), at, {
|
|
233
|
+
title: 'Observed pull request target candidate', source: PR_CANDIDATE_SOURCE,
|
|
234
|
+
completion_event_id: original.event_id, native_id: original.payload.native_id,
|
|
235
|
+
repo: match[1], number: Number(match[2]), url,
|
|
236
|
+
})];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function mapCodex(record, offset, notes = {}) {
|
|
240
|
+
if (!record || !validTime(record.timestamp)) return [];
|
|
241
|
+
const at = record.timestamp;
|
|
242
|
+
const id = `r${offset}`;
|
|
243
|
+
const item = record.payload ?? record;
|
|
244
|
+
if (record.type === 'event_msg') {
|
|
245
|
+
if (item.type === 'item_completed') {
|
|
246
|
+
const original = nativeCompletion(item.item, id, at);
|
|
247
|
+
return item.item?.type === 'CommandExecution' && original.length
|
|
248
|
+
? [...original, ...observedPrCandidate(item.item, id, at, notes.repository, original[0])] : original;
|
|
249
|
+
}
|
|
250
|
+
return ['task_complete', 'turn_aborted'].includes(item.type)
|
|
251
|
+
? [lifecycle(`${id}:turn-end`, at, 'waiting_for_review', 'input_required')] : [];
|
|
252
|
+
}
|
|
253
|
+
if (record.type !== 'response_item' && record.payload) return [];
|
|
254
|
+
if (item.type === 'message') {
|
|
255
|
+
if (!['user', 'assistant'].includes(item.role)) return [];
|
|
256
|
+
const blocks = typeof item.content === 'string' ? [{ type: 'text', text: item.content }] : Array.isArray(item.content) ? item.content : [];
|
|
257
|
+
return messageEvents(blocks, item.role, id, at, 'codex', notes);
|
|
258
|
+
}
|
|
259
|
+
if (item.type === 'agent_message') return [event(`${id}:0`, 'agent_message', agent('subagent'), at, { text: cap(textOf(item.content)), subagent: true })];
|
|
260
|
+
if (item.type === 'reasoning' && textOf(item.summary).trim()) {
|
|
261
|
+
return [event(`${id}:0`, 'thinking', agent('codex'), at, { text: cap(textOf(item.summary)) })];
|
|
262
|
+
}
|
|
263
|
+
if (['function_call_output', 'custom_tool_call_output'].includes(item.type)) {
|
|
264
|
+
return [event(`${id}:0`, 'tool_result', agent('codex'), at, { call_id: item.call_id ?? null, ok: null,
|
|
265
|
+
output: cap(textOf(item.output) || (item.output && typeof item.output === 'object' ? JSON.stringify(item.output) : '')) })];
|
|
266
|
+
}
|
|
267
|
+
if (!['function_call', 'custom_tool_call', 'local_shell_call'].includes(item.type)) return [];
|
|
268
|
+
let input = { input: string(item.input) };
|
|
269
|
+
if (item.type === 'function_call') {
|
|
270
|
+
try { input = JSON.parse(string(item.arguments)); } catch { input = { arguments: string(item.arguments) }; }
|
|
271
|
+
} else if (item.type === 'local_shell_call') input = item.action ?? {};
|
|
272
|
+
const name = [item.namespace, item.name].filter(Boolean).join('.') || 'shell';
|
|
273
|
+
if (name === 'apply_patch' || name.endsWith('.apply_patch')) {
|
|
274
|
+
const patch = string(item.input);
|
|
275
|
+
const path = patch.match(/^\*\*\* (?:Update|Add|Delete) File: (.+)$/m)?.[1] ?? '';
|
|
276
|
+
return [event(`${id}:0`, 'edit', agent('codex'), at, { path, diff: cap(patch), call_id: item.call_id ?? null })];
|
|
277
|
+
}
|
|
278
|
+
return [event(`${id}:0`, 'tool_call', { kind: /estelle/i.test(name) ? 'estelle' : 'agent', id: 'codex' }, at,
|
|
279
|
+
{ call_id: item.call_id ?? null, tool: name, summary: cap(`Ran ${name}`), input: sanitize(input) })];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Current and legacy Kimi journal vocabulary, retaining original byte-offset replay IDs. */
|
|
283
|
+
export function mapKimi(record, offset, notes = {}) {
|
|
284
|
+
record = normalizeKimiRecord(record) ?? record;
|
|
285
|
+
if (!record || !Number.isFinite(record.time)) return [];
|
|
286
|
+
if (!Number.isFinite(new Date(record.time).getTime())) return [];
|
|
287
|
+
const at = new Date(record.time).toISOString();
|
|
288
|
+
const id = `r${offset}`;
|
|
289
|
+
if (record.type === 'turn.ended') return [lifecycle(`${id}:turn-end`, at, 'waiting_for_review', 'input_required')];
|
|
290
|
+
if (record.type !== 'context.append_message') return [];
|
|
291
|
+
const message = record.message;
|
|
292
|
+
if (!message || !['user', 'assistant', 'tool'].includes(message.role)) return [];
|
|
293
|
+
if (message.role === 'tool') return [event(`${id}:0`, 'tool_result', agent('kimi-code'), at,
|
|
294
|
+
{ call_id: message.toolCallId ?? null, ok: typeof message.isError === 'boolean' ? !message.isError : null, output: cap(textOf(message.content)) })];
|
|
295
|
+
const blocks = Array.isArray(message.content) ? message.content.map(block => block?.type === 'image_url' ? { type: 'input_image' } : block) : [];
|
|
296
|
+
const out = messageEvents(blocks, message.role, id, at, 'kimi-code', notes);
|
|
297
|
+
const calls = Array.isArray(message.toolCalls) ? message.toolCalls : [];
|
|
298
|
+
if (calls.length > LIMITS.messageBlocks) throw new Error('Kimi tool-call record exceeds block bound; cursor retained');
|
|
299
|
+
for (const [index, call] of calls.entries()) {
|
|
300
|
+
if (!call || typeof call !== 'object') continue;
|
|
301
|
+
const name = string(call.name) || string(call.function?.name);
|
|
302
|
+
let input;
|
|
303
|
+
try { input = JSON.parse(string(call.arguments ?? call.function?.arguments)); } catch { input = { arguments: string(call.arguments) }; }
|
|
304
|
+
out.push(event(`${id}:tool-${index}`, 'tool_call', { kind: /estelle/i.test(name) ? 'estelle' : 'agent', id: 'kimi-code' }, at,
|
|
305
|
+
{ call_id: call.id ?? null, tool: name || 'tool', summary: cap(`Ran ${name || 'tool'}`), input: sanitize(input) }));
|
|
306
|
+
}
|
|
307
|
+
return out;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Capture time for dialects whose transcript lines carry no verified per-record timestamp. */
|
|
311
|
+
function observedAt(record, notes) {
|
|
312
|
+
if (validTime(record?.timestamp)) return record.timestamp;
|
|
313
|
+
const at = string(notes.observedAt);
|
|
314
|
+
return validTime(at) ? at : null;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Cursor agent-transcripts JSONL: `{"role":"user"|"assistant","message":{"content":[...]}}` lines with
|
|
319
|
+
* Anthropic-shaped text/tool_use blocks (Cursor tool names: Shell, Write carries `contents`, `path` not
|
|
320
|
+
* `file_path`), plus role-less control lines such as `{"type":"turn_ended","status":"success"}`.
|
|
321
|
+
* Tool outputs are not recorded by the host. Confidence: inferred from third-party observations of
|
|
322
|
+
* ~/.cursor/projects/<slug>/agent-transcripts/ (checked 2026-09/10), not verified against a live session.
|
|
323
|
+
* Lines carry no timestamp, so events are stamped with capture time (notes.observedAt).
|
|
324
|
+
*/
|
|
325
|
+
export function mapCursor(record, offset, notes = {}) {
|
|
326
|
+
if (!record || typeof record !== 'object' || Array.isArray(record)) return [];
|
|
327
|
+
const at = observedAt(record, notes);
|
|
328
|
+
if (!at) return [];
|
|
329
|
+
const id = `c${offset}`;
|
|
330
|
+
if (!['user', 'assistant'].includes(record.role)) {
|
|
331
|
+
return record.role === undefined && record.type === 'turn_ended'
|
|
332
|
+
? [lifecycle(`${id}:turn-end`, at, 'waiting_for_review', 'input_required')] : [];
|
|
333
|
+
}
|
|
334
|
+
const content = record.message?.content ?? record.content;
|
|
335
|
+
const blocks = typeof content === 'string' ? [{ type: 'text', text: content }] : Array.isArray(content) ? content : [];
|
|
336
|
+
return messageEvents(blocks, record.role, id, at, 'cursor', notes);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** ACP content is a single block; chat-history content may be a string or a block array. Depth is bounded. */
|
|
340
|
+
export function grokContentText(content, depth = 0) {
|
|
341
|
+
if (typeof content === 'string') return content;
|
|
342
|
+
if (depth >= 8 || !content || typeof content !== 'object') return '';
|
|
343
|
+
if (Array.isArray(content)) return content.map(block => grokContentText(block, depth + 1)).filter(Boolean).join('\n');
|
|
344
|
+
return string(content.text) || grokContentText(content.content, depth + 1);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Grok Build hook `transcriptPath` JSONL. Confidence: INFERRED, not verified against a live Grok session.
|
|
349
|
+
* Grok persists sessions as an ACP session-update stream (updates.jsonl) and as raw model messages
|
|
350
|
+
* (chat_history.jsonl); which one transcriptPath names is unverified, so both recognized families are read
|
|
351
|
+
* and everything else is skipped. ACP updates may arrive bare, under `update`, or as a session/update
|
|
352
|
+
* notification (`params.update`). Message chunks are incremental: each chunk becomes one event.
|
|
353
|
+
* Lines carry no verified timestamp, so events are stamped with capture time (notes.observedAt).
|
|
354
|
+
*/
|
|
355
|
+
export function mapGrok(record, offset, notes = {}) {
|
|
356
|
+
if (!record || typeof record !== 'object' || Array.isArray(record)) return [];
|
|
357
|
+
const at = observedAt(record, notes);
|
|
358
|
+
if (!at) return [];
|
|
359
|
+
const id = `g${offset}`;
|
|
360
|
+
const update = record.params?.update ?? record.update ?? (typeof record.sessionUpdate === 'string' ? record : null);
|
|
361
|
+
if (update && typeof update === 'object') {
|
|
362
|
+
const kind = string(update.sessionUpdate);
|
|
363
|
+
if (kind === 'user_message_chunk' || kind === 'agent_message_chunk') {
|
|
364
|
+
const text = grokContentText(update.content);
|
|
365
|
+
if (!text.trim() || (kind === 'user_message_chunk' && injected(text, INJECTED))) return [];
|
|
366
|
+
if (kind === 'user_message_chunk') notes.title ??= text.trim().split('\n')[0].slice(0, 90);
|
|
367
|
+
return [event(id, kind === 'user_message_chunk' ? 'customer_message' : 'agent_message',
|
|
368
|
+
kind === 'user_message_chunk' ? { kind: 'customer' } : agent('grok'), at, { text: cap(text) })];
|
|
369
|
+
}
|
|
370
|
+
if (kind === 'agent_thought_chunk') {
|
|
371
|
+
const text = grokContentText(update.content);
|
|
372
|
+
return text.trim() ? [event(id, 'thinking', agent('grok'), at, { text: cap(text) })] : [];
|
|
373
|
+
}
|
|
374
|
+
if (kind === 'tool_call') {
|
|
375
|
+
const name = string(update.title) || 'tool';
|
|
376
|
+
const input = update.rawInput && typeof update.rawInput === 'object' ? update.rawInput : {};
|
|
377
|
+
return [event(id, 'tool_call', { kind: /estelle/i.test(name) ? 'estelle' : 'agent', id: 'grok' }, at,
|
|
378
|
+
{ call_id: string(update.toolCallId) || null, tool: name, summary: cap(name), input: sanitize(input) })];
|
|
379
|
+
}
|
|
380
|
+
if (kind === 'tool_call_update') {
|
|
381
|
+
const status = string(update.status);
|
|
382
|
+
return [event(id, 'tool_result', agent('grok'), at, { call_id: string(update.toolCallId) || null,
|
|
383
|
+
ok: status === 'completed' ? true : status === 'failed' ? false : null, output: cap(grokContentText(update.content)) })];
|
|
384
|
+
}
|
|
385
|
+
return [];
|
|
386
|
+
}
|
|
387
|
+
if (record.role === 'tool') {
|
|
388
|
+
return [event(id, 'tool_result', agent('grok'), at,
|
|
389
|
+
{ call_id: string(record.tool_call_id) || null, ok: null, output: cap(grokContentText(record.content)) })];
|
|
390
|
+
}
|
|
391
|
+
if (!['user', 'assistant'].includes(record.role)) return [];
|
|
392
|
+
const content = record.content;
|
|
393
|
+
const blocks = typeof content === 'string' ? [{ type: 'text', text: content }] : Array.isArray(content) ? content : [];
|
|
394
|
+
const out = messageEvents(blocks, record.role, id, at, 'grok', notes);
|
|
395
|
+
const calls = Array.isArray(record.tool_calls) ? record.tool_calls : [];
|
|
396
|
+
if (calls.length > LIMITS.messageBlocks) throw new Error('Grok tool-call record exceeds block bound; cursor retained');
|
|
397
|
+
for (const [index, call] of calls.entries()) {
|
|
398
|
+
if (!call || typeof call !== 'object') continue;
|
|
399
|
+
const name = string(call.name) || string(call.function?.name);
|
|
400
|
+
let input;
|
|
401
|
+
try { input = JSON.parse(string(call.arguments ?? call.function?.arguments)); } catch { input = { arguments: string(call.arguments ?? call.function?.arguments) }; }
|
|
402
|
+
out.push(event(`${id}:tool-${index}`, 'tool_call', { kind: /estelle/i.test(name) ? 'estelle' : 'agent', id: 'grok' }, at,
|
|
403
|
+
{ call_id: string(call.id) || null, tool: name || 'tool', summary: cap(`Ran ${name || 'tool'}`), input: sanitize(input) }));
|
|
404
|
+
}
|
|
405
|
+
return out;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const DSH_AGENT = 'deepseek-harness';
|
|
409
|
+
const dshAt = time => Number.isSafeInteger(time) && time > 0 ? new Date(time).toISOString() : null;
|
|
410
|
+
const dshInput = raw => { try { return JSON.parse(string(raw)); } catch { return { arguments: string(raw) }; } };
|
|
411
|
+
|
|
412
|
+
function dshToolCall(data, id, at) {
|
|
413
|
+
const name = string(data.name) || 'tool';
|
|
414
|
+
const input = dshInput(data.arguments);
|
|
415
|
+
const path = string(input.file_path);
|
|
416
|
+
const call = string(data.callId) || null;
|
|
417
|
+
if (name === 'write') return event(id, 'edit', agent(DSH_AGENT), at, { path, diff: cap(string(input.content).split('\n').map(line => `+${line}`).join('\n')), call_id: call });
|
|
418
|
+
if (name === 'edit') {
|
|
419
|
+
const lines = (value, mark) => string(value).split('\n').map(line => `${mark}${line}`);
|
|
420
|
+
return event(id, 'edit', agent(DSH_AGENT), at, { path, diff: cap([...lines(input.old_string, '-'), ...lines(input.new_string, '+')].join('\n')), call_id: call });
|
|
421
|
+
}
|
|
422
|
+
const summary = name === 'bash' ? `Ran ${string(input.command).slice(0, 80)}` : name === 'read' ? `Read ${path}` : name;
|
|
423
|
+
return event(id, 'tool_call', { kind: /mcp__estelle__/i.test(name) ? 'estelle' : 'agent', id: DSH_AGENT }, at,
|
|
424
|
+
{ call_id: call, tool: name, summary: cap(summary), input: sanitize({ ...input, ...(path ? { path } : {}) }) });
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* The JSONL adapters/dsh-plugin.mjs writes: dsh's own canonical events, read from a real session log (format v4).
|
|
429
|
+
* Every line has a type, a sequence number and a millisecond time. dsh sends its runtime context and skill catalog
|
|
430
|
+
* as user-role messages too, so source.kind is what tells the person from the host.
|
|
431
|
+
*/
|
|
432
|
+
export function mapDsh(record, offset, notes = {}) {
|
|
433
|
+
const at = dshAt(record?.time);
|
|
434
|
+
const data = record?.data;
|
|
435
|
+
if (!at || !data || typeof data !== 'object') return [];
|
|
436
|
+
const id = `d${offset}`;
|
|
437
|
+
if (record.type === 'user/message') {
|
|
438
|
+
if (data.source?.kind !== 'user') return [];
|
|
439
|
+
const blocks = Array.isArray(data.content) ? data.content : [];
|
|
440
|
+
return messageEvents(blocks, 'user', id, at, DSH_AGENT, notes);
|
|
441
|
+
}
|
|
442
|
+
if (record.type === 'assistant/message') {
|
|
443
|
+
const blocks = Array.isArray(data.message?.content) ? data.message.content : [];
|
|
444
|
+
return messageEvents(blocks, 'assistant', id, at, DSH_AGENT, notes);
|
|
445
|
+
}
|
|
446
|
+
if (record.type === 'tool/call') return [dshToolCall(data, id, at)];
|
|
447
|
+
if (record.type === 'tool/result') {
|
|
448
|
+
const message = data.message ?? {};
|
|
449
|
+
return [event(id, 'tool_result', agent(DSH_AGENT), at, { call_id: string(message.toolCallId) || null,
|
|
450
|
+
ok: message.isError === true ? false : null, output: cap(textOf(message.content)) })];
|
|
451
|
+
}
|
|
452
|
+
if (record.type === 'turn/end') {
|
|
453
|
+
return data.reason?.kind === 'completed' ? [lifecycle(`${id}:turn-end`, at, 'waiting_for_review', 'input_required')] : [];
|
|
454
|
+
}
|
|
455
|
+
return [];
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const PI_AGENT = 'pi';
|
|
459
|
+
/** Pi's own session file (docs/session-format.md, version 3): `message` entries carrying an AgentMessage. Extension
|
|
460
|
+
* messages (role `custom`, Estelle's own injected context among them) and system prompt records are not conversation. */
|
|
461
|
+
export function mapPi(entry, offset, notes = {}) {
|
|
462
|
+
if (!entry || entry.type !== 'message' || !validTime(entry.timestamp) || !string(entry.id)) return [];
|
|
463
|
+
const { timestamp: at, id } = entry;
|
|
464
|
+
const message = entry.message;
|
|
465
|
+
if (!message || typeof message !== 'object') return [];
|
|
466
|
+
if (message.role === 'user') {
|
|
467
|
+
const blocks = typeof message.content === 'string' ? [{ type: 'text', text: message.content }]
|
|
468
|
+
: Array.isArray(message.content) ? message.content : [];
|
|
469
|
+
return messageEvents(blocks, 'user', id, at, PI_AGENT, notes);
|
|
470
|
+
}
|
|
471
|
+
if (message.role === 'toolResult') {
|
|
472
|
+
return messageEvents([{ type: 'tool_result', tool_use_id: string(message.toolCallId) || null,
|
|
473
|
+
is_error: message.isError === true, content: Array.isArray(message.content) ? message.content : [] }], 'tool', id, at, PI_AGENT, notes);
|
|
474
|
+
}
|
|
475
|
+
if (message.role !== 'assistant' || !Array.isArray(message.content)) return [];
|
|
476
|
+
const blocks = message.content.map(block => block?.type === 'toolCall'
|
|
477
|
+
? { type: 'tool_use', id: block.id, name: block.name, input: block.arguments ?? {} } : block);
|
|
478
|
+
const out = messageEvents(blocks, 'assistant', id, at, PI_AGENT, notes);
|
|
479
|
+
if (message.usage && out.length) out[0].payload.usage = { input: message.usage.input ?? 0, output: message.usage.output ?? 0,
|
|
480
|
+
cache_read: message.usage.cacheRead ?? 0, cache_write: message.usage.cacheWrite ?? 0, model: string(message.model) || null };
|
|
481
|
+
// A reply that stopped without asking for a tool is the end of the turn: Pi waits for its person.
|
|
482
|
+
if (message.stopReason === 'stop') out.push(lifecycle(`${id}:turn-end`, at, 'waiting_for_review', 'input_required'));
|
|
483
|
+
return out;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
async function readJson(path, fallback, max = LIMITS.stateBytes) {
|
|
487
|
+
let handle;
|
|
488
|
+
try {
|
|
489
|
+
handle = await open(path, 'r');
|
|
490
|
+
const info = await handle.stat();
|
|
491
|
+
if (!info.isFile() || info.size > max) throw new Error('state file exceeds bound');
|
|
492
|
+
const buffer = Buffer.alloc(info.size + 1);
|
|
493
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
494
|
+
if (bytesRead > info.size) throw new Error('state file exceeds bound');
|
|
495
|
+
return JSON.parse(buffer.subarray(0, bytesRead).toString('utf8'));
|
|
496
|
+
} catch (error) {
|
|
497
|
+
if (error.code === 'ENOENT') return fallback;
|
|
498
|
+
throw new Error('stream state is unreadable or invalid', { cause: error });
|
|
499
|
+
} finally { await handle?.close(); }
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
async function saveJson(path, value) {
|
|
503
|
+
const temp = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
504
|
+
try {
|
|
505
|
+
await writeFile(temp, JSON.stringify(value), { mode: 0o600, flag: 'wx' });
|
|
506
|
+
await rename(temp, path);
|
|
507
|
+
} finally { await unlink(temp).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
async function listBounded(dir, bound) {
|
|
511
|
+
const files = [];
|
|
512
|
+
let entries = 0;
|
|
513
|
+
try {
|
|
514
|
+
for await (const entry of await opendir(dir)) {
|
|
515
|
+
if (++entries > bound + 1) throw new Error('stream directory entry bound reached');
|
|
516
|
+
if (entry.isFile() && entry.name.endsWith('.json')) files.push(join(dir, entry.name));
|
|
517
|
+
if (files.length > bound) throw new Error('spool file bound reached');
|
|
518
|
+
}
|
|
519
|
+
} catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
520
|
+
return files.sort();
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function retryable(error) {
|
|
524
|
+
return !Number.isInteger(error.status) || error.status >= 500 || [401, 403, 408, 429].includes(error.status);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function associationPartial(response, events) {
|
|
528
|
+
if (!events.some(e => e.payload?.source === PR_CANDIDATE_SOURCE)) return false;
|
|
529
|
+
const row = response?.captured_pr;
|
|
530
|
+
return !(row?.state === 'linked' && row.linked === 1 && row.deferred === 0 && row.unconfirmed === 0);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
async function sendBatch(request, session, events) {
|
|
534
|
+
let pending = events;
|
|
535
|
+
let dropped = 0;
|
|
536
|
+
const bound = events.length + 1;
|
|
537
|
+
for (let attempt = 0; attempt < bound; attempt++) {
|
|
538
|
+
if (!pending.length) return { dropped };
|
|
539
|
+
try {
|
|
540
|
+
const response = await request('/session/events', { session_id: session, events: pending });
|
|
541
|
+
return { dropped, association_partial: associationPartial(response, pending) };
|
|
542
|
+
}
|
|
543
|
+
catch (error) {
|
|
544
|
+
const index = error.status === 400 ? /^event (\d+):/.exec(string(error.message))?.[1] : undefined;
|
|
545
|
+
if (index === undefined || Number(index) >= pending.length) throw error;
|
|
546
|
+
pending = pending.filter((_, i) => i !== Number(index));
|
|
547
|
+
dropped++;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
throw new Error('event isolation exhausted its bound');
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
async function spoolBatch(dir, session, events) {
|
|
554
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
555
|
+
const body = JSON.stringify({ session_id: session, events });
|
|
556
|
+
const hash = createHash('sha256').update(body).digest('hex');
|
|
557
|
+
const path = join(dir, `${hash}.json`);
|
|
558
|
+
try { await stat(path); return; } catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
559
|
+
const files = await listBounded(dir, LIMITS.spoolFiles);
|
|
560
|
+
let bytes = Buffer.byteLength(body);
|
|
561
|
+
for (const file of files) bytes += (await stat(file)).size;
|
|
562
|
+
if (files.length >= LIMITS.spoolFiles || bytes > LIMITS.spoolBytes) throw new Error('stream spool is full; transcript cursor retained');
|
|
563
|
+
await saveJson(path, { session_id: session, events });
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
async function drainSpool(dir, request) {
|
|
567
|
+
let replayed = 0;
|
|
568
|
+
let associationIncomplete = false;
|
|
569
|
+
const files = (await listBounded(dir, LIMITS.spoolFiles)).slice(0, LIMITS.drainFiles);
|
|
570
|
+
for (const file of files) {
|
|
571
|
+
const record = await readJson(file, null, LIMITS.batchBytes + 2048);
|
|
572
|
+
if (!record || !Array.isArray(record.events) || !string(record.session_id)) throw new Error('invalid stream spool record');
|
|
573
|
+
try {
|
|
574
|
+
const result = await sendBatch(request, record.session_id, record.events);
|
|
575
|
+
associationIncomplete ||= result.association_partial === true;
|
|
576
|
+
await unlink(file);
|
|
577
|
+
replayed += record.events.length;
|
|
578
|
+
} catch (error) { if (retryable(error)) break; }
|
|
579
|
+
}
|
|
580
|
+
return { replayed, ...(associationIncomplete ? { association_partial: true } : {}) };
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
async function readWindow(file, offset) {
|
|
584
|
+
const handle = await open(file, 'r');
|
|
585
|
+
try {
|
|
586
|
+
const length = (await handle.stat()).size;
|
|
587
|
+
const start = offset > length ? 0 : offset;
|
|
588
|
+
const buffer = Buffer.alloc(Math.min(LIMITS.readBytes, length - start));
|
|
589
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, start);
|
|
590
|
+
const end = buffer.subarray(0, bytesRead).lastIndexOf(10) + 1;
|
|
591
|
+
const oversized = end === 0 && bytesRead === LIMITS.readBytes;
|
|
592
|
+
const skipTo = oversized ? await endOfLongLine(handle, start + bytesRead, length) : null;
|
|
593
|
+
return { buffer: buffer.subarray(0, end), start, length, incomplete: start + end < length, oversized, skipTo };
|
|
594
|
+
} finally { await handle.close(); }
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
async function endOfLongLine(handle, from, length) {
|
|
598
|
+
const chunk = Buffer.alloc(1_048_576);
|
|
599
|
+
const max = Math.min(length, from + LIMITS.skipBytes);
|
|
600
|
+
const windows = Math.ceil((max - from) / chunk.length);
|
|
601
|
+
let at = from;
|
|
602
|
+
for (let pass = 0; pass < windows; pass++) {
|
|
603
|
+
const { bytesRead } = await handle.read(chunk, 0, Math.min(chunk.length, max - at), at);
|
|
604
|
+
if (!bytesRead) break;
|
|
605
|
+
const newline = chunk.subarray(0, bytesRead).indexOf(10);
|
|
606
|
+
if (newline >= 0) return at + newline + 1;
|
|
607
|
+
at += bytesRead;
|
|
608
|
+
}
|
|
609
|
+
return null; // Never advance into a line or past a bound that did not find its newline.
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
function collectWindow(window, host, notes, remaining, child = false) {
|
|
613
|
+
const events = [];
|
|
614
|
+
let used = 0;
|
|
615
|
+
let invalid = 0;
|
|
616
|
+
let lines = 0;
|
|
617
|
+
const mapper = host === 'codex' ? mapCodex : host === 'kimi-code' ? mapKimi
|
|
618
|
+
: host === 'grok' ? mapGrok : host === 'cursor' ? mapCursor : host === DSH_AGENT ? mapDsh : host === PI_AGENT ? mapPi : mapClaude;
|
|
619
|
+
const buffer = window.buffer;
|
|
620
|
+
for (let from = 0; from < buffer.length && lines < LIMITS.lines; lines++) {
|
|
621
|
+
const end = buffer.indexOf(10, from) + 1;
|
|
622
|
+
if (!end) break;
|
|
623
|
+
let parsed;
|
|
624
|
+
try { parsed = JSON.parse(buffer.subarray(from, end).toString('utf8')); } catch { invalid++; }
|
|
625
|
+
const mapped = parsed ? mapper(parsed, window.start + from, notes, child) : [];
|
|
626
|
+
if (events.length + mapped.length > remaining) break;
|
|
627
|
+
events.push(...mapped);
|
|
628
|
+
used = end;
|
|
629
|
+
from = end;
|
|
630
|
+
if (events.length >= remaining) break;
|
|
631
|
+
}
|
|
632
|
+
return { events, offset: window.start + used, invalid };
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
function collapse(events, last) {
|
|
636
|
+
const kept = [];
|
|
637
|
+
for (const item of events) {
|
|
638
|
+
if (item.kind === 'lifecycle') {
|
|
639
|
+
const key = `${item.payload.state}/${item.payload.reason}`;
|
|
640
|
+
if (key === WAITING && last === WAITING) continue;
|
|
641
|
+
last = key;
|
|
642
|
+
}
|
|
643
|
+
kept.push(item);
|
|
644
|
+
}
|
|
645
|
+
return { events: kept, last };
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function eventBatches(events) {
|
|
649
|
+
const batches = [];
|
|
650
|
+
let batch = [];
|
|
651
|
+
let size = 0;
|
|
652
|
+
for (const item of events) {
|
|
653
|
+
const bytes = Buffer.byteLength(JSON.stringify(item));
|
|
654
|
+
if (bytes > LIMITS.batchBytes) throw new Error('one stream event exceeds wire bound; cursor retained');
|
|
655
|
+
if (batch.length && (batch.length >= LIMITS.batchEvents || size + bytes > LIMITS.batchBytes - 512)) {
|
|
656
|
+
batches.push(batch); batch = []; size = 0;
|
|
657
|
+
}
|
|
658
|
+
batch.push(item); size += bytes;
|
|
659
|
+
}
|
|
660
|
+
if (batch.length) batches.push(batch);
|
|
661
|
+
return batches;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/** request(path, body) must reject HTTP failures with {status,message}. No credentials are persisted here. */
|
|
665
|
+
export async function streamSession({ payload, host, repo, stateDir, request }) {
|
|
666
|
+
const harness = AGENTS[host];
|
|
667
|
+
if (!harness) return { sent: 0, unsupported: true, reason: 'transcript dialect is not verified for this host' };
|
|
668
|
+
const session = payload.session_id;
|
|
669
|
+
if (!safeSession(session)) throw new Error('invalid host session identity');
|
|
670
|
+
if (typeof request !== 'function' || typeof stateDir !== 'string') throw new TypeError('stream request and state directory required');
|
|
671
|
+
await mkdir(stateDir, { recursive: true, mode: 0o700 });
|
|
672
|
+
const spoolDir = join(stateDir, 'spool');
|
|
673
|
+
const replay = await drainSpool(spoolDir, request);
|
|
674
|
+
const { replayed } = replay;
|
|
675
|
+
if (!string(payload.transcript_path)) return { sent: 0, ...replay, incomplete: true, reason: 'host did not supply a transcript path' };
|
|
676
|
+
const transcript = resolve(payload.transcript_path);
|
|
677
|
+
const statePath = join(stateDir, `${harness}-${session}.stream.json`);
|
|
678
|
+
const state = await readJson(statePath, { offset: 0, started: false, last_lifecycle: null });
|
|
679
|
+
if (!Number.isSafeInteger(state.offset) || state.offset < 0) throw new Error('invalid transcript cursor');
|
|
680
|
+
const timeline = timelineIdentity(host, session);
|
|
681
|
+
const notes = { repository: repo };
|
|
682
|
+
// Grok and Cursor transcript lines carry no verified timestamps; capture time stamps their events.
|
|
683
|
+
if (harness === 'grok' || harness === 'cursor') notes.observedAt = new Date().toISOString();
|
|
684
|
+
if (harness === 'opencode') notes.harness = harness;
|
|
685
|
+
let events = [];
|
|
686
|
+
let offset = state.offset;
|
|
687
|
+
let invalid = 0;
|
|
688
|
+
let skipped = 0;
|
|
689
|
+
let incomplete = false;
|
|
690
|
+
const windows = harness === 'codex' ? LIMITS.codexWindows : 1;
|
|
691
|
+
const budget = harness === 'codex' ? LIMITS.codexEvents : LIMITS.claudeEvents;
|
|
692
|
+
for (let pass = 0; pass < windows && events.length < budget; pass++) {
|
|
693
|
+
const window = await readWindow(transcript, offset);
|
|
694
|
+
if (window.oversized) {
|
|
695
|
+
if (window.skipTo === null) return { sent: 0, ...replay, incomplete: true, reason: 'unterminated transcript line exceeds read bound; cursor retained' };
|
|
696
|
+
offset = window.skipTo; skipped++; incomplete = offset < window.length;
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
const collected = collectWindow(window, harness, notes, budget - events.length);
|
|
700
|
+
events.push(...collected.events); invalid += collected.invalid;
|
|
701
|
+
incomplete = collected.offset < window.length;
|
|
702
|
+
if (collected.offset === offset) break;
|
|
703
|
+
offset = collected.offset;
|
|
704
|
+
if (!incomplete) break;
|
|
705
|
+
}
|
|
706
|
+
const first = events[0]?.at;
|
|
707
|
+
const started = state.started || Boolean(first);
|
|
708
|
+
if (!state.started && first) events.unshift(event(`${timeline}:started`, 'lifecycle', { kind: 'system', id: 'estelle-hook' }, first,
|
|
709
|
+
{ state: 'running', reason: 'started', title: cap(notes.title || `${harness} session`), repo, origin: harness, harness }));
|
|
710
|
+
// Do not announce a turn end ahead of still-unread transcript events.
|
|
711
|
+
const last = events.at(-1)?.at ?? state.last_at;
|
|
712
|
+
if (started && payload.hook_event_name === 'UserPromptSubmit') {
|
|
713
|
+
const at = new Date(Math.max(Date.now(), validTime(last) ? Date.parse(last) : 0)).toISOString();
|
|
714
|
+
events.push(lifecycle(`${timeline}:prompt:${offset}`, at, 'running', 'input_received'));
|
|
715
|
+
}
|
|
716
|
+
if (!incomplete && last && ['Stop', 'SessionEnd'].includes(payload.hook_event_name)) {
|
|
717
|
+
const end = payload.hook_event_name === 'SessionEnd';
|
|
718
|
+
events.push(lifecycle(`${timeline}:${end ? 'end' : 'stop'}:${offset}`, last, end ? 'ended' : 'waiting_for_review', end ? 'completed' : 'input_required'));
|
|
719
|
+
}
|
|
720
|
+
const collapsed = collapse(events, state.last_lifecycle);
|
|
721
|
+
const batches = eventBatches(collapsed.events.map(item => sanitize(item)));
|
|
722
|
+
let sent = 0;
|
|
723
|
+
let dropped = 0;
|
|
724
|
+
let associationIncomplete = replay.association_partial === true;
|
|
725
|
+
for (const [index, batch] of batches.entries()) {
|
|
726
|
+
try {
|
|
727
|
+
const result = await sendBatch(request, timeline, batch);
|
|
728
|
+
dropped += result.dropped; sent += batch.length - result.dropped;
|
|
729
|
+
associationIncomplete ||= result.association_partial === true;
|
|
730
|
+
}
|
|
731
|
+
catch (error) {
|
|
732
|
+
if (retryable(error)) {
|
|
733
|
+
for (const remaining of batches.slice(index)) await spoolBatch(spoolDir, timeline, remaining);
|
|
734
|
+
}
|
|
735
|
+
// Spooling preserves the failed batch across transcript deletion; only server ACK advances cursor.
|
|
736
|
+
await saveJson(statePath, { ...state, last_error: 'stream upload pending', invalid_lines: invalid });
|
|
737
|
+
return { sent, dropped, replayed, ...(associationIncomplete ? { association_partial: true } : {}),
|
|
738
|
+
pending: true, incomplete: true, error: 'stream upload pending; cursor retained' };
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
await saveJson(statePath, { offset, started, last_at: events.at(-1)?.at ?? last, last_lifecycle: collapsed.last, invalid_lines: invalid, skipped_lines: skipped, last_error: null });
|
|
742
|
+
return { sent, dropped, replayed, ...(associationIncomplete ? { association_partial: true } : {}), backlog: incomplete, incomplete: incomplete || harness === 'kimi-code', invalid_lines: invalid, skipped_lines: skipped, offset, session_id: timeline,
|
|
743
|
+
...(harness === 'kimi-code' ? { transcript_format: 'kimi-wire', current_format_verified: true,
|
|
744
|
+
capture_scope: 'main-agent journal; stream includes recorded branch history' } : {}),
|
|
745
|
+
...(harness === DSH_AGENT ? { transcript_format: 'dsh-session-events', current_format_verified: true,
|
|
746
|
+
capture_scope: 'root agent; subagent sessions are not captured' } : {}),
|
|
747
|
+
...(harness === 'grok' ? { transcript_format: 'grok-jsonl-inferred', current_format_verified: false,
|
|
748
|
+
event_times: 'capture-time', capture_scope: 'hook transcriptPath JSONL; ACP update and chat-history lines recognized, other lines skipped' } : {}),
|
|
749
|
+
...(harness === 'cursor' ? { transcript_format: 'cursor-jsonl-inferred', current_format_verified: false,
|
|
750
|
+
event_times: 'capture-time', capture_scope: 'hook transcript_path JSONL; tool outputs are not recorded by the host' } : {}),
|
|
751
|
+
...(harness === 'opencode' ? { transcript_format: 'opencode-plugin-mirror', current_format_verified: true,
|
|
752
|
+
capture_scope: 'messages the plugin read from OpenCode after each turn; subagent sessions are captured as their own sessions' } : {}),
|
|
753
|
+
...(harness === PI_AGENT ? { transcript_format: 'pi-session-v3', current_format_verified: true,
|
|
754
|
+
capture_scope: "Pi's own session file, every branch in file order; extension messages are skipped" } : {}) };
|
|
755
|
+
}
|