@spexcode/spec-cli 0.6.5
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/README.md +95 -0
- package/bin/spex.mjs +76 -0
- package/dist/attach.d.ts +2 -0
- package/dist/attach.js +48 -0
- package/dist/claude-headless.d.ts +33 -0
- package/dist/claude-headless.js +307 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1859 -0
- package/dist/client.d.ts +111 -0
- package/dist/client.js +476 -0
- package/dist/cockpit.d.ts +7 -0
- package/dist/cockpit.js +26 -0
- package/dist/codex-headless.d.ts +1 -0
- package/dist/codex-headless.js +7 -0
- package/dist/codex-runtime-generations.d.ts +53 -0
- package/dist/codex-runtime-generations.js +686 -0
- package/dist/commit-surgery.d.ts +1 -0
- package/dist/commit-surgery.js +90 -0
- package/dist/contract-filter.d.ts +14 -0
- package/dist/contract-filter.js +219 -0
- package/dist/dashboard-assets.d.ts +6 -0
- package/dist/dashboard-assets.js +50 -0
- package/dist/delivery-queue.d.ts +23 -0
- package/dist/delivery-queue.js +179 -0
- package/dist/doctor.d.ts +9 -0
- package/dist/doctor.js +547 -0
- package/dist/eval-host.d.ts +1 -0
- package/dist/eval-host.js +19 -0
- package/dist/execution-trace.d.ts +26 -0
- package/dist/execution-trace.js +475 -0
- package/dist/file-write.d.ts +2 -0
- package/dist/file-write.js +26 -0
- package/dist/flat.d.ts +89 -0
- package/dist/flat.js +1076 -0
- package/dist/gateway-auth.d.ts +56 -0
- package/dist/gateway-auth.js +168 -0
- package/dist/gateway-hub.d.ts +28 -0
- package/dist/gateway-hub.js +361 -0
- package/dist/gateway.d.ts +37 -0
- package/dist/gateway.js +503 -0
- package/dist/graphCache.d.ts +27 -0
- package/dist/graphCache.js +716 -0
- package/dist/graphSnapshot.d.ts +14 -0
- package/dist/graphSnapshot.js +35 -0
- package/dist/graphStream.d.ts +85 -0
- package/dist/graphStream.js +1195 -0
- package/dist/guidance-catalog.d.ts +64 -0
- package/dist/guidance-catalog.js +166 -0
- package/dist/guide.d.ts +8 -0
- package/dist/guide.js +684 -0
- package/dist/harness-select.d.ts +18 -0
- package/dist/harness-select.js +62 -0
- package/dist/harness.d.ts +325 -0
- package/dist/harness.js +3026 -0
- package/dist/headless-controller.d.ts +9 -0
- package/dist/headless-controller.js +47 -0
- package/dist/help.d.ts +10 -0
- package/dist/help.js +574 -0
- package/dist/hook-prompts.d.ts +18 -0
- package/dist/hook-prompts.js +122 -0
- package/dist/hooks.d.ts +1 -0
- package/dist/hooks.js +27 -0
- package/dist/host-resources.d.ts +97 -0
- package/dist/host-resources.js +795 -0
- package/dist/host.d.ts +88 -0
- package/dist/host.js +663 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +899 -0
- package/dist/init.d.ts +1 -0
- package/dist/init.js +284 -0
- package/dist/issues-cli.d.ts +6 -0
- package/dist/issues-cli.js +334 -0
- package/dist/issues.d.ts +69 -0
- package/dist/issues.js +189 -0
- package/dist/lint.d.ts +34 -0
- package/dist/lint.js +476 -0
- package/dist/listen.d.ts +7 -0
- package/dist/listen.js +24 -0
- package/dist/localIssues.d.ts +65 -0
- package/dist/localIssues.js +582 -0
- package/dist/login-page.d.ts +5 -0
- package/dist/login-page.js +86 -0
- package/dist/loop-in.d.ts +23 -0
- package/dist/loop-in.js +68 -0
- package/dist/machine-peer.d.ts +69 -0
- package/dist/machine-peer.js +603 -0
- package/dist/materialize.d.ts +18 -0
- package/dist/materialize.js +468 -0
- package/dist/mentions.d.ts +50 -0
- package/dist/mentions.js +117 -0
- package/dist/opencode-headless.d.ts +4 -0
- package/dist/opencode-headless.js +208 -0
- package/dist/opencode.d.ts +3 -0
- package/dist/opencode.js +142 -0
- package/dist/pi-harness.d.ts +3 -0
- package/dist/pi-harness.js +129 -0
- package/dist/pi-headless.d.ts +25 -0
- package/dist/pi-headless.js +196 -0
- package/dist/plugin-harness.d.ts +21 -0
- package/dist/plugin-harness.js +146 -0
- package/dist/pty-bridge.d.ts +10 -0
- package/dist/pty-bridge.js +404 -0
- package/dist/pty-helper.d.mts +1 -0
- package/dist/pty-helper.mjs +101 -0
- package/dist/pty-native-helper.d.mts +2 -0
- package/dist/pty-native-helper.mjs +23 -0
- package/dist/public-graph.d.ts +46 -0
- package/dist/public-graph.js +56 -0
- package/dist/ranker.d.ts +15 -0
- package/dist/ranker.js +188 -0
- package/dist/reaper.d.ts +6 -0
- package/dist/reaper.js +81 -0
- package/dist/reviews.d.ts +189 -0
- package/dist/reviews.js +350 -0
- package/dist/runtime-guard.d.ts +6 -0
- package/dist/runtime-guard.js +37 -0
- package/dist/runtime-ownership.d.ts +21 -0
- package/dist/runtime-ownership.js +84 -0
- package/dist/search.bench.d.mts +1 -0
- package/dist/search.bench.mjs +75 -0
- package/dist/search.d.ts +20 -0
- package/dist/search.js +61 -0
- package/dist/session-cursors.d.ts +14 -0
- package/dist/session-cursors.js +82 -0
- package/dist/session-declarations.d.ts +13 -0
- package/dist/session-declarations.js +126 -0
- package/dist/session-execution.d.ts +10 -0
- package/dist/session-execution.js +70 -0
- package/dist/session-files.d.ts +28 -0
- package/dist/session-files.js +130 -0
- package/dist/session-follow.d.ts +31 -0
- package/dist/session-follow.js +179 -0
- package/dist/session-reparent.d.ts +10 -0
- package/dist/session-reparent.js +33 -0
- package/dist/session-timeline.d.ts +51 -0
- package/dist/session-timeline.js +233 -0
- package/dist/session-web.d.ts +24 -0
- package/dist/session-web.js +141 -0
- package/dist/sessions.d.ts +412 -0
- package/dist/sessions.js +4239 -0
- package/dist/sh.d.ts +1 -0
- package/dist/sh.js +3 -0
- package/dist/shim-runtime.d.ts +1 -0
- package/dist/shim-runtime.js +161 -0
- package/dist/slash-commands.d.ts +9 -0
- package/dist/slash-commands.js +307 -0
- package/dist/source-files.d.ts +9 -0
- package/dist/source-files.js +113 -0
- package/dist/supervise.d.ts +1 -0
- package/dist/supervise.js +308 -0
- package/dist/tree.d.ts +25 -0
- package/dist/tree.js +98 -0
- package/dist/tsx-bin.d.ts +3 -0
- package/dist/tsx-bin.js +41 -0
- package/dist/uninstall.d.ts +3 -0
- package/dist/uninstall.js +177 -0
- package/dist/uploads.d.ts +36 -0
- package/dist/uploads.js +243 -0
- package/dist/worktree-sources.d.ts +1 -0
- package/dist/worktree-sources.js +47 -0
- package/hooks/compat/mark-active-0.5.2-eef1.fixture +53 -0
- package/hooks/compat/mark-active-sed-v0.fixture +46 -0
- package/hooks/dispatch.sh +105 -0
- package/hooks/harness.sh +272 -0
- package/package.json +47 -0
- package/templates/hooks/commit-msg +14 -0
- package/templates/hooks/post-checkout +25 -0
- package/templates/hooks/post-merge +34 -0
- package/templates/hooks/pre-commit +120 -0
- package/templates/hooks/prepare-commit-msg +35 -0
- package/templates/hooks/reference-transaction +105 -0
- package/templates/spec/project/.plugins/commands/extract/spec.md +66 -0
- package/templates/spec/project/.plugins/commands/regroup/spec.md +25 -0
- package/templates/spec/project/.plugins/commands/rename/spec.md +11 -0
- package/templates/spec/project/.plugins/commands/spec.md +30 -0
- package/templates/spec/project/.plugins/commands/supervisor/spec.md +8 -0
- package/templates/spec/project/.plugins/commands/tidy/spec.md +43 -0
- package/templates/spec/project/.plugins/core/comment-altitude/spec.md +9 -0
- package/templates/spec/project/.plugins/core/idle/idle.sh +15 -0
- package/templates/spec/project/.plugins/core/idle/spec.md +13 -0
- package/templates/spec/project/.plugins/core/mark-active/mark-active.sh +59 -0
- package/templates/spec/project/.plugins/core/mark-active/spec.md +22 -0
- package/templates/spec/project/.plugins/core/session-fail/fail.sh +12 -0
- package/templates/spec/project/.plugins/core/session-fail/spec.md +15 -0
- package/templates/spec/project/.plugins/core/spec-first/spec-first.sh +43 -0
- package/templates/spec/project/.plugins/core/spec-first/spec.md +17 -0
- package/templates/spec/project/.plugins/core/spec-of-file/spec-of-file.sh +66 -0
- package/templates/spec/project/.plugins/core/spec-of-file/spec.md +15 -0
- package/templates/spec/project/.plugins/core/spec.md +22 -0
- package/templates/spec/project/.plugins/core/stop-gate/spec.md +23 -0
- package/templates/spec/project/.plugins/core/stop-gate/stop-gate.sh +149 -0
- package/templates/spec/project/.plugins/prompts/atomic-landing/spec.md +15 -0
- package/templates/spec/project/.plugins/prompts/forge-link/spec.md +10 -0
- package/templates/spec/project/.plugins/prompts/memory-hygiene/spec.md +13 -0
- package/templates/spec/project/.plugins/prompts/reproduce-before-fix/spec.md +12 -0
- package/templates/spec/project/.plugins/prompts/spec.md +24 -0
- package/templates/spec/project/.plugins/skills/distill/digest.mjs +136 -0
- package/templates/spec/project/.plugins/skills/distill/spec.md +67 -0
- package/templates/spec/project/.plugins/skills/spec.md +19 -0
- package/templates/spec/project/.plugins/spec.md +23 -0
- package/templates/spec/project/spec.md +20 -0
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { closeSync, openSync, readFileSync, readSync, readdirSync, statSync } from 'node:fs';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
const cache = new Map();
|
|
6
|
+
const sessionJsonlPaths = new Map();
|
|
7
|
+
const localStoreCache = new Map();
|
|
8
|
+
const emptyTrace = (turn, revision = '0') => ({
|
|
9
|
+
revision: `${turn?.token ?? '0'}:${revision}`,
|
|
10
|
+
turnId: turn?.token ?? null,
|
|
11
|
+
workingNote: null,
|
|
12
|
+
steps: [],
|
|
13
|
+
});
|
|
14
|
+
const object = (value) => value && typeof value === 'object' && !Array.isArray(value) ? value : null;
|
|
15
|
+
const items = (value) => Array.isArray(value) ? value : [];
|
|
16
|
+
const text = (value) => typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
17
|
+
const field = (value, ...names) => {
|
|
18
|
+
for (const name of names) {
|
|
19
|
+
const found = text(value[name]);
|
|
20
|
+
if (found)
|
|
21
|
+
return found;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
};
|
|
25
|
+
const children = (dir) => { try {
|
|
26
|
+
return readdirSync(dir).sort().reverse();
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return [];
|
|
30
|
+
} };
|
|
31
|
+
const sensitive = /(token|secret|password|authorization|bearer|cookie|credential|api[_-]?key|private[ _-]?key)/i;
|
|
32
|
+
const detailFields = [
|
|
33
|
+
['cmd', 'cmd'], ['command', 'command'], ['path', 'path'], ['file_path', 'path'],
|
|
34
|
+
['query', 'query'], ['pattern', 'pattern'], ['url', 'url'], ['workdir', 'in'],
|
|
35
|
+
];
|
|
36
|
+
function toolKind(name) {
|
|
37
|
+
const normalized = name.toLowerCase();
|
|
38
|
+
if (/(search|browse|web)/.test(normalized))
|
|
39
|
+
return 'search';
|
|
40
|
+
if (/(read|list|find|grep|glob|stat)/.test(normalized))
|
|
41
|
+
return 'read';
|
|
42
|
+
if (/(write|edit|patch|create|delete|move|rename)/.test(normalized))
|
|
43
|
+
return 'write';
|
|
44
|
+
if (/(exec|shell|command|terminal|bash|npm|git)/.test(normalized))
|
|
45
|
+
return 'command';
|
|
46
|
+
return 'tool';
|
|
47
|
+
}
|
|
48
|
+
function toolLabel(name, kind) {
|
|
49
|
+
const labels = {
|
|
50
|
+
command: 'Run command', read: 'Read files', write: 'Update files', search: 'Search', tool: 'Use tool',
|
|
51
|
+
};
|
|
52
|
+
return name.trim() || labels[kind];
|
|
53
|
+
}
|
|
54
|
+
function recordInput(value) {
|
|
55
|
+
if (value && typeof value === 'object' && !Array.isArray(value))
|
|
56
|
+
return value;
|
|
57
|
+
if (typeof value !== 'string')
|
|
58
|
+
return null;
|
|
59
|
+
try {
|
|
60
|
+
const parsed = JSON.parse(value);
|
|
61
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function sensitiveInput(value) {
|
|
68
|
+
if (typeof value === 'string')
|
|
69
|
+
return sensitive.test(value);
|
|
70
|
+
if (Array.isArray(value))
|
|
71
|
+
return value.some(sensitiveInput);
|
|
72
|
+
if (value && typeof value === 'object')
|
|
73
|
+
return Object.entries(value)
|
|
74
|
+
.some(([key, entry]) => sensitive.test(key) || sensitiveInput(entry));
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
function compactValue(key, value) {
|
|
78
|
+
const compact = String(value).trim().replace(/\s+/g, ' ');
|
|
79
|
+
if (key === 'path' || key === 'file_path' || key === 'workdir') {
|
|
80
|
+
const parts = compact.replace(/\\/g, '/').split('/').filter(Boolean);
|
|
81
|
+
return parts.slice(-2).join('/') || compact;
|
|
82
|
+
}
|
|
83
|
+
return compact.length > 96 ? `${compact.slice(0, 95)}...` : compact;
|
|
84
|
+
}
|
|
85
|
+
function detailFromInput(value) {
|
|
86
|
+
const input = recordInput(value);
|
|
87
|
+
if (!input || sensitiveInput(input))
|
|
88
|
+
return undefined;
|
|
89
|
+
const details = [];
|
|
90
|
+
for (const [key, title] of detailFields) {
|
|
91
|
+
const value = input[key];
|
|
92
|
+
if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean')
|
|
93
|
+
continue;
|
|
94
|
+
details.push(`${title}: ${compactValue(key, value)}`);
|
|
95
|
+
if (details.length === 2)
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
const lineStart = input.line_start;
|
|
99
|
+
const lineEnd = input.line_end;
|
|
100
|
+
if (details.length < 2 && (typeof lineStart === 'number' || typeof lineEnd === 'number')) {
|
|
101
|
+
details.push(`lines: ${typeof lineStart === 'number' ? lineStart : 1}-${typeof lineEnd === 'number' ? lineEnd : lineStart}`);
|
|
102
|
+
}
|
|
103
|
+
return details.length ? details.join(' · ') : undefined;
|
|
104
|
+
}
|
|
105
|
+
function displayableNote(value) {
|
|
106
|
+
const note = text(value)?.replace(/\s+/g, ' ');
|
|
107
|
+
return note ? (note.length > 240 ? `${note.slice(0, 239)}...` : note) : null;
|
|
108
|
+
}
|
|
109
|
+
function stepFromInput(id, name, input, state) {
|
|
110
|
+
const kind = toolKind(name);
|
|
111
|
+
const detail = detailFromInput(input);
|
|
112
|
+
return { id, kind, label: toolLabel(name, kind), ...(detail ? { detail } : {}), state };
|
|
113
|
+
}
|
|
114
|
+
function timestamp(value) {
|
|
115
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
116
|
+
return value;
|
|
117
|
+
if (typeof value !== 'string' || !value.trim())
|
|
118
|
+
return null;
|
|
119
|
+
const number = Number(value);
|
|
120
|
+
if (Number.isFinite(number))
|
|
121
|
+
return number;
|
|
122
|
+
const parsed = Date.parse(value);
|
|
123
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
124
|
+
}
|
|
125
|
+
function identities(...values) {
|
|
126
|
+
const result = new Set();
|
|
127
|
+
for (const value of values)
|
|
128
|
+
for (const key of ['id', 'uuid', 'message_id', 'messageId', 'client_id', 'clientId', 'client_user_message_id', 'clientUserMessageId', 'promptId']) {
|
|
129
|
+
const id = field(value || {}, key);
|
|
130
|
+
if (id)
|
|
131
|
+
result.add(id);
|
|
132
|
+
}
|
|
133
|
+
return [...result];
|
|
134
|
+
}
|
|
135
|
+
function at(value) {
|
|
136
|
+
for (const key of ['timestamp', 'created_at', 'createdAt', 'created', 'time']) {
|
|
137
|
+
const found = timestamp(value[key]);
|
|
138
|
+
if (found !== null)
|
|
139
|
+
return found;
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
function isPrompt(content) {
|
|
144
|
+
if (typeof content === 'string')
|
|
145
|
+
return !!content.trim();
|
|
146
|
+
const blocks = items(content);
|
|
147
|
+
return blocks.some((block) => object(block)?.type !== 'tool_result');
|
|
148
|
+
}
|
|
149
|
+
function project(events, sourceRevision, turn) {
|
|
150
|
+
let start = -1;
|
|
151
|
+
if (!turn) {
|
|
152
|
+
for (const [index, event] of events.entries())
|
|
153
|
+
if (event.type === 'boundary')
|
|
154
|
+
start = index;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
const acceptedAt = timestamp(turn.acceptedAt);
|
|
158
|
+
const exact = events.flatMap((event, index) => event.type === 'boundary' && event.ids.includes(turn.token) ? [index] : []);
|
|
159
|
+
start = exact.length === 1 ? exact[0] : -1;
|
|
160
|
+
if (start < 0 && exact.length === 0 && acceptedAt !== null) {
|
|
161
|
+
const candidates = events.flatMap((event, index) => event.type === 'boundary' && event.at !== null && event.at >= acceptedAt
|
|
162
|
+
? [{ index, at: event.at }] : []);
|
|
163
|
+
const earliest = candidates.reduce((value, candidate) => value === null || candidate.at < value ? candidate.at : value, null);
|
|
164
|
+
const matches = earliest === null ? [] : candidates.filter((candidate) => candidate.at === earliest);
|
|
165
|
+
if (matches.length === 1)
|
|
166
|
+
start = matches[0].index;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (turn && start < 0)
|
|
170
|
+
return emptyTrace(turn, sourceRevision);
|
|
171
|
+
let note = null;
|
|
172
|
+
let steps = [];
|
|
173
|
+
for (let index = start + 1; index < events.length; index++) {
|
|
174
|
+
const event = events[index];
|
|
175
|
+
if (event.type === 'boundary')
|
|
176
|
+
return emptyTrace(turn, sourceRevision);
|
|
177
|
+
if (event.type === 'note') {
|
|
178
|
+
note = event.text;
|
|
179
|
+
steps = [];
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (event.type === 'call') {
|
|
183
|
+
if (note)
|
|
184
|
+
steps.push(event.step);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const step = steps.find((value) => value.id === event.id);
|
|
188
|
+
if (step)
|
|
189
|
+
steps[steps.indexOf(step)] = { ...step, state: 'done' };
|
|
190
|
+
}
|
|
191
|
+
return note
|
|
192
|
+
? { revision: `${turn?.token ?? '0'}:${sourceRevision}`, turnId: turn?.token ?? null, workingNote: note, steps }
|
|
193
|
+
: emptyTrace(turn, sourceRevision);
|
|
194
|
+
}
|
|
195
|
+
function applyRolloutEvent(value, ordinal) {
|
|
196
|
+
const event = object(value);
|
|
197
|
+
const payload = object(event?.payload);
|
|
198
|
+
if (!event || !payload)
|
|
199
|
+
return [];
|
|
200
|
+
const result = [];
|
|
201
|
+
const type = field(payload, 'type');
|
|
202
|
+
if ((event.type === 'event_msg' && type === 'user_message')
|
|
203
|
+
|| (event.type === 'response_item' && (type === 'message' || type === 'input_message') && payload.role === 'user')) {
|
|
204
|
+
result.push({ type: 'boundary', ids: identities(payload, event), at: at(payload) ?? at(event) });
|
|
205
|
+
}
|
|
206
|
+
if (event.type === 'event_msg' && type === 'agent_message' && payload.phase === 'commentary') {
|
|
207
|
+
const note = displayableNote(payload.message ?? payload.text);
|
|
208
|
+
if (note)
|
|
209
|
+
result.push({ type: 'note', text: note });
|
|
210
|
+
}
|
|
211
|
+
if (event.type === 'response_item' && (type === 'custom_tool_call' || type === 'function_call')) {
|
|
212
|
+
const name = field(payload, 'name', 'tool_name') || 'tool';
|
|
213
|
+
const id = field(payload, 'call_id', 'id') || `tool-${ordinal}`;
|
|
214
|
+
result.push({ type: 'call', step: stepFromInput(id, name, payload.input ?? payload.arguments ?? payload.args, 'running') });
|
|
215
|
+
}
|
|
216
|
+
if (event.type === 'response_item' && (type === 'custom_tool_call_output' || type === 'function_call_output')) {
|
|
217
|
+
const id = field(payload, 'call_id', 'id');
|
|
218
|
+
if (id)
|
|
219
|
+
result.push({ type: 'done', id });
|
|
220
|
+
}
|
|
221
|
+
if (event.type === 'event_msg' && type === 'web_search_end') {
|
|
222
|
+
result.push({ type: 'call', step: stepFromInput(field(payload, 'id', 'call_id') || `search-${ordinal}`, 'Search', payload, 'done') });
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
function applyProjectJsonlEvent(value, ordinal) {
|
|
227
|
+
const entry = object(value);
|
|
228
|
+
const message = object(entry?.message);
|
|
229
|
+
if (!entry || !message)
|
|
230
|
+
return [];
|
|
231
|
+
const result = [];
|
|
232
|
+
if (entry.type === 'user' && message.role === 'user' && isPrompt(message.content))
|
|
233
|
+
result.push({ type: 'boundary', ids: identities(entry, message), at: at(entry) ?? at(message) });
|
|
234
|
+
if (entry.type === 'assistant' && message.role === 'assistant')
|
|
235
|
+
for (const blockValue of items(message.content)) {
|
|
236
|
+
const block = object(blockValue);
|
|
237
|
+
if (block?.type === 'text') {
|
|
238
|
+
const note = displayableNote(block.text);
|
|
239
|
+
if (note)
|
|
240
|
+
result.push({ type: 'note', text: note });
|
|
241
|
+
}
|
|
242
|
+
if (block?.type === 'tool_use') {
|
|
243
|
+
const id = field(block, 'id') || `tool-${ordinal}`;
|
|
244
|
+
result.push({ type: 'call', step: stepFromInput(id, field(block, 'name') || 'tool', block.input, 'running') });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (entry.type === 'user' && message.role === 'user')
|
|
248
|
+
for (const blockValue of items(message.content)) {
|
|
249
|
+
const block = object(blockValue);
|
|
250
|
+
if (block?.type === 'tool_result') {
|
|
251
|
+
const id = field(block, 'tool_use_id');
|
|
252
|
+
if (id)
|
|
253
|
+
result.push({ type: 'done', id });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
function applySessionJsonlEvent(value, ordinal) {
|
|
259
|
+
const entry = object(value);
|
|
260
|
+
const message = object(entry?.message);
|
|
261
|
+
if (!entry || entry.type !== 'message' || !message)
|
|
262
|
+
return [];
|
|
263
|
+
const result = [];
|
|
264
|
+
if (message.role === 'user' && isPrompt(message.content))
|
|
265
|
+
result.push({ type: 'boundary', ids: identities(entry, message), at: at(entry) ?? at(message) });
|
|
266
|
+
if (message.role === 'assistant')
|
|
267
|
+
for (const blockValue of items(message.content)) {
|
|
268
|
+
const block = object(blockValue);
|
|
269
|
+
if (block?.type === 'text') {
|
|
270
|
+
const note = displayableNote(block.text);
|
|
271
|
+
if (note)
|
|
272
|
+
result.push({ type: 'note', text: note });
|
|
273
|
+
}
|
|
274
|
+
if (block?.type === 'toolCall') {
|
|
275
|
+
const id = field(block, 'id') || `tool-${ordinal}`;
|
|
276
|
+
result.push({ type: 'call', step: stepFromInput(id, field(block, 'name') || 'tool', block.arguments, 'running') });
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (message.role === 'toolResult') {
|
|
280
|
+
const id = field(message, 'toolCallId');
|
|
281
|
+
if (id)
|
|
282
|
+
result.push({ type: 'done', id });
|
|
283
|
+
}
|
|
284
|
+
return result;
|
|
285
|
+
}
|
|
286
|
+
function completeLines(buffer) {
|
|
287
|
+
const lines = [];
|
|
288
|
+
let start = 0;
|
|
289
|
+
for (let index = 0; index < buffer.length; index++) {
|
|
290
|
+
if (buffer[index] !== 10)
|
|
291
|
+
continue;
|
|
292
|
+
if (index > start)
|
|
293
|
+
lines.push(buffer.subarray(start, index));
|
|
294
|
+
start = index + 1;
|
|
295
|
+
}
|
|
296
|
+
return { lines, remainder: buffer.subarray(start) };
|
|
297
|
+
}
|
|
298
|
+
function readFrom(path, start) {
|
|
299
|
+
let fd = null;
|
|
300
|
+
try {
|
|
301
|
+
const size = statSync(path).size;
|
|
302
|
+
if (size <= start)
|
|
303
|
+
return Buffer.alloc(0);
|
|
304
|
+
const chunk = Buffer.allocUnsafe(size - start);
|
|
305
|
+
fd = openSync(path, 'r');
|
|
306
|
+
readSync(fd, chunk, 0, chunk.length, start);
|
|
307
|
+
return chunk;
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
finally {
|
|
313
|
+
if (fd !== null)
|
|
314
|
+
closeSync(fd);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function readIncremental(path, apply, turn) {
|
|
318
|
+
if (!path)
|
|
319
|
+
return emptyTrace(turn);
|
|
320
|
+
let size;
|
|
321
|
+
try {
|
|
322
|
+
size = statSync(path).size;
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
return emptyTrace(turn);
|
|
326
|
+
}
|
|
327
|
+
const prior = cache.get(path);
|
|
328
|
+
const trace = prior && prior.size <= size
|
|
329
|
+
? { ...prior, events: [...prior.events] }
|
|
330
|
+
: { size: 0, remainder: Buffer.alloc(0), events: [] };
|
|
331
|
+
const appended = readFrom(path, trace.size);
|
|
332
|
+
if (appended === null)
|
|
333
|
+
return emptyTrace(turn);
|
|
334
|
+
const { lines, remainder } = completeLines(Buffer.concat([trace.remainder, appended]));
|
|
335
|
+
for (const [index, line] of lines.entries()) {
|
|
336
|
+
try {
|
|
337
|
+
trace.events.push(...apply(JSON.parse(line.toString('utf8')), trace.size + index));
|
|
338
|
+
}
|
|
339
|
+
catch { /* malformed native data is ignored */ }
|
|
340
|
+
}
|
|
341
|
+
trace.size = size;
|
|
342
|
+
trace.remainder = remainder;
|
|
343
|
+
cache.set(path, trace);
|
|
344
|
+
return project(trace.events, String(size), turn);
|
|
345
|
+
}
|
|
346
|
+
const codexSessionsDir = () => join(process.env.CODEX_HOME || join(homedir(), '.codex'), 'sessions');
|
|
347
|
+
export function codexRolloutPath(threadId, root = codexSessionsDir()) {
|
|
348
|
+
for (const year of children(root))
|
|
349
|
+
for (const month of children(join(root, year)))
|
|
350
|
+
for (const day of children(join(root, year, month))) {
|
|
351
|
+
const dir = join(root, year, month, day);
|
|
352
|
+
const file = children(dir).find((name) => name.includes(threadId));
|
|
353
|
+
if (file)
|
|
354
|
+
return join(dir, file);
|
|
355
|
+
}
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
export function readCodexExecutionTrace(threadId, turn, root = codexSessionsDir()) {
|
|
359
|
+
return readIncremental(codexRolloutPath(threadId, root), applyRolloutEvent, turn);
|
|
360
|
+
}
|
|
361
|
+
const projectTranscriptRoot = () => join(process.env.CLAUDE_CONFIG_DIR || join(homedir(), '.claude'), 'projects');
|
|
362
|
+
function projectJsonlPath(threadId, root = projectTranscriptRoot()) {
|
|
363
|
+
for (const project of children(root)) {
|
|
364
|
+
const path = join(root, project, `${threadId}.jsonl`);
|
|
365
|
+
try {
|
|
366
|
+
if (statSync(path).isFile())
|
|
367
|
+
return path;
|
|
368
|
+
}
|
|
369
|
+
catch { /* try next */ }
|
|
370
|
+
}
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
export function readProjectJsonlExecutionTrace(threadId, turn, root = projectTranscriptRoot()) {
|
|
374
|
+
return readIncremental(projectJsonlPath(threadId, root), applyProjectJsonlEvent, turn);
|
|
375
|
+
}
|
|
376
|
+
const sessionJsonlRoot = () => join(process.env.SPEXCODE_PI_AGENT_DIR || join(homedir(), '.pi', 'agent'), 'sessions');
|
|
377
|
+
function sessionJsonlPath(threadId, root = sessionJsonlRoot()) {
|
|
378
|
+
const key = `${root}:${threadId}`;
|
|
379
|
+
const cached = sessionJsonlPaths.get(key);
|
|
380
|
+
if (cached) {
|
|
381
|
+
try {
|
|
382
|
+
if (statSync(cached).isFile())
|
|
383
|
+
return cached;
|
|
384
|
+
}
|
|
385
|
+
catch {
|
|
386
|
+
sessionJsonlPaths.delete(key);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
for (const directory of children(root))
|
|
390
|
+
for (const file of children(join(root, directory))) {
|
|
391
|
+
if (!file.endsWith('.jsonl'))
|
|
392
|
+
continue;
|
|
393
|
+
const path = join(root, directory, file);
|
|
394
|
+
try {
|
|
395
|
+
const header = object(JSON.parse(readFileSync(path, 'utf8').split('\n', 1)[0]));
|
|
396
|
+
if (header?.type === 'session' && header.id === threadId) {
|
|
397
|
+
sessionJsonlPaths.set(key, path);
|
|
398
|
+
return path;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
catch { /* unreadable entries are not a match */ }
|
|
402
|
+
}
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
export function readSessionJsonlExecutionTrace(threadId, turn, root = sessionJsonlRoot()) {
|
|
406
|
+
return readIncremental(sessionJsonlPath(threadId, root), applySessionJsonlEvent, turn);
|
|
407
|
+
}
|
|
408
|
+
const localStoreRoot = () => process.env.SPEXCODE_OPENCODE_DATA_DIR
|
|
409
|
+
|| join(process.env.XDG_DATA_HOME || join(homedir(), '.local', 'share'), 'opencode');
|
|
410
|
+
function readLocalExport(threadId) {
|
|
411
|
+
return execFileSync(process.env.SPEXCODE_OPENCODE_CMD || 'opencode', ['export', threadId, '--sanitize'], {
|
|
412
|
+
encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'],
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
function localStoreRevision(root) {
|
|
416
|
+
try {
|
|
417
|
+
const database = statSync(join(root, 'opencode.db'));
|
|
418
|
+
let writeAheadLog = '0:0';
|
|
419
|
+
try {
|
|
420
|
+
const stat = statSync(join(root, 'opencode.db-wal'));
|
|
421
|
+
writeAheadLog = `${stat.size}:${Math.floor(stat.mtimeMs)}`;
|
|
422
|
+
}
|
|
423
|
+
catch { /* a checkpointed database has no separate write-ahead log */ }
|
|
424
|
+
return `${database.size}:${Math.floor(database.mtimeMs)}:${writeAheadLog}`;
|
|
425
|
+
}
|
|
426
|
+
catch {
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
function localStoreEvents(value) {
|
|
431
|
+
const events = [];
|
|
432
|
+
for (const messageValue of items(object(value)?.messages)) {
|
|
433
|
+
const message = object(messageValue);
|
|
434
|
+
const info = object(message?.info);
|
|
435
|
+
if (!message || !info)
|
|
436
|
+
continue;
|
|
437
|
+
if (info.role === 'user')
|
|
438
|
+
events.push({ type: 'boundary', ids: identities(info, message), at: at(object(info.time) || info) });
|
|
439
|
+
if (info.role !== 'assistant')
|
|
440
|
+
continue;
|
|
441
|
+
for (const partValue of items(message.parts)) {
|
|
442
|
+
const part = object(partValue);
|
|
443
|
+
if (part?.type === 'text') {
|
|
444
|
+
const note = displayableNote(part.text);
|
|
445
|
+
if (note)
|
|
446
|
+
events.push({ type: 'note', text: note });
|
|
447
|
+
}
|
|
448
|
+
if (part?.type === 'tool') {
|
|
449
|
+
const state = object(part.state);
|
|
450
|
+
const status = field(state || {}, 'status')?.toLowerCase() || '';
|
|
451
|
+
const id = field(part, 'callID', 'id') || `tool-${events.length}`;
|
|
452
|
+
events.push({ type: 'call', step: stepFromInput(id, field(part, 'tool') || 'tool', state?.input, /completed|error|cancelled/.test(status) ? 'done' : 'running') });
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return events;
|
|
457
|
+
}
|
|
458
|
+
export function readLocalStoreExecutionTrace(threadId, turn, root = localStoreRoot(), load = readLocalExport) {
|
|
459
|
+
const revision = localStoreRevision(root);
|
|
460
|
+
if (!revision)
|
|
461
|
+
return emptyTrace(turn);
|
|
462
|
+
const key = `${root}:${threadId}`;
|
|
463
|
+
let cached = localStoreCache.get(key);
|
|
464
|
+
if (!cached || cached.revision !== revision) {
|
|
465
|
+
try {
|
|
466
|
+
cached = { revision, events: localStoreEvents(JSON.parse(load(threadId))) };
|
|
467
|
+
localStoreCache.set(key, cached);
|
|
468
|
+
}
|
|
469
|
+
catch {
|
|
470
|
+
return emptyTrace(turn, revision);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return project(cached.events, revision, turn);
|
|
474
|
+
}
|
|
475
|
+
export const noExecutionTrace = (_threadId, _turn) => null;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { copyFileSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
export function writeFileIfChanged(path, content) {
|
|
3
|
+
const next = Buffer.from(content);
|
|
4
|
+
try {
|
|
5
|
+
if (readFileSync(path).equals(next))
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
if (error?.code !== 'ENOENT')
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
writeFileSync(path, content);
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
export function copyFileIfChanged(source, target) {
|
|
16
|
+
try {
|
|
17
|
+
if (readFileSync(target).equals(readFileSync(source)))
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (error?.code !== 'ENOENT')
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
copyFileSync(source, target);
|
|
25
|
+
return true;
|
|
26
|
+
}
|
package/dist/flat.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { type Harness } from './harness.js';
|
|
2
|
+
export declare const FLAT_BRANCH = "flatcode";
|
|
3
|
+
export declare const FLAT_AGENT_CHOICES: readonly [{
|
|
4
|
+
readonly name: "claude";
|
|
5
|
+
readonly label: "Claude Code";
|
|
6
|
+
readonly harness: "claude";
|
|
7
|
+
readonly cmd: "claude";
|
|
8
|
+
}, {
|
|
9
|
+
readonly name: "codex";
|
|
10
|
+
readonly label: "Codex";
|
|
11
|
+
readonly harness: "codex";
|
|
12
|
+
readonly cmd: "codex";
|
|
13
|
+
}, {
|
|
14
|
+
readonly name: "opencode";
|
|
15
|
+
readonly label: "OpenCode";
|
|
16
|
+
readonly harness: "opencode";
|
|
17
|
+
readonly cmd: "opencode";
|
|
18
|
+
}, {
|
|
19
|
+
readonly name: "pi";
|
|
20
|
+
readonly label: "Pi";
|
|
21
|
+
readonly harness: "pi";
|
|
22
|
+
readonly cmd: "pi";
|
|
23
|
+
}];
|
|
24
|
+
export declare const languageName: (code: string) => string;
|
|
25
|
+
export type FlatGate = {
|
|
26
|
+
errors: number;
|
|
27
|
+
governed: number;
|
|
28
|
+
uncovered: number;
|
|
29
|
+
coverage: number;
|
|
30
|
+
errorFindings: readonly string[];
|
|
31
|
+
uncoveredFiles: readonly string[];
|
|
32
|
+
sourceFiles: readonly string[];
|
|
33
|
+
};
|
|
34
|
+
export type FlatProfile = {
|
|
35
|
+
sourceExtensions: readonly string[];
|
|
36
|
+
governedRoots: readonly string[];
|
|
37
|
+
languages: readonly string[];
|
|
38
|
+
fileCount: number;
|
|
39
|
+
};
|
|
40
|
+
export declare function profileFiles(files: readonly string[]): FlatProfile;
|
|
41
|
+
export declare function readGate(payload: string): FlatGate;
|
|
42
|
+
export declare const gatePassed: (gate: FlatGate, coverageFloor: number) => boolean;
|
|
43
|
+
export declare function confirmProfile(proposed: FlatProfile, sourceFiles: readonly string[]): FlatProfile;
|
|
44
|
+
type FlatTurnHarness = {
|
|
45
|
+
harness: Harness;
|
|
46
|
+
cmd: string;
|
|
47
|
+
name: string;
|
|
48
|
+
harnessId: string;
|
|
49
|
+
};
|
|
50
|
+
export type FlatOptions = {
|
|
51
|
+
target: string;
|
|
52
|
+
out?: string;
|
|
53
|
+
launcher?: string;
|
|
54
|
+
rounds?: number;
|
|
55
|
+
coverage?: number;
|
|
56
|
+
lang?: string;
|
|
57
|
+
};
|
|
58
|
+
export type FlatResult = {
|
|
59
|
+
out: string;
|
|
60
|
+
repo: string;
|
|
61
|
+
source: string;
|
|
62
|
+
revision: string;
|
|
63
|
+
rounds: number;
|
|
64
|
+
gate: FlatGate;
|
|
65
|
+
passed: boolean;
|
|
66
|
+
profile: FlatProfile;
|
|
67
|
+
};
|
|
68
|
+
export declare function flatNew(options: FlatOptions, log?: (line: string) => void, choose?: (root: string) => Promise<FlatTurnHarness>): Promise<FlatResult>;
|
|
69
|
+
export declare function publicShellDir(): string;
|
|
70
|
+
export declare function flatSite(flatDir: string, log?: (line: string) => void): Promise<{
|
|
71
|
+
site: string;
|
|
72
|
+
nodes: number;
|
|
73
|
+
}>;
|
|
74
|
+
export declare function gallerySlug(source: string): string;
|
|
75
|
+
export type GalleryEntry = {
|
|
76
|
+
slug: string;
|
|
77
|
+
source: string;
|
|
78
|
+
revision: string;
|
|
79
|
+
coverage: number;
|
|
80
|
+
governed: number;
|
|
81
|
+
nodes: number;
|
|
82
|
+
passed: boolean;
|
|
83
|
+
languages: readonly string[];
|
|
84
|
+
lang: string;
|
|
85
|
+
};
|
|
86
|
+
export declare function galleryIndexHtml(entries: readonly GalleryEntry[]): string;
|
|
87
|
+
export declare function flatGallery(out: string, flatDirs: readonly string[], log?: (line: string) => void): Promise<GalleryEntry[]>;
|
|
88
|
+
export declare function runFlat(argv: readonly string[]): Promise<number>;
|
|
89
|
+
export {};
|