sumo-agents 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +6 -5
- package/CHANGELOG.md +54 -0
- package/README.md +4 -2
- package/package.json +1 -1
- package/src/chat.mjs +63 -21
- package/src/cli.mjs +12 -2
- package/src/herdr.mjs +71 -36
- package/src/jobs.mjs +40 -20
- package/src/loop.mjs +33 -4
- package/src/sessions.mjs +20 -14
- package/src/tools.mjs +78 -49
- package/src/tty.mjs +47 -22
- package/src/ui.mjs +18 -9
- package/src/verify.mjs +56 -11
- package/src/worktree.mjs +81 -4
package/src/jobs.mjs
CHANGED
|
@@ -21,7 +21,7 @@ import { addCheckpoint, pendingTurns } from './sessions.mjs';
|
|
|
21
21
|
import { CONFIG_DEFAULTS } from './setup.mjs';
|
|
22
22
|
import { clip, estimateTokens, head, STOPWORDS } from './text.mjs';
|
|
23
23
|
import { changesSince, diffStat, hasCommit, snapshot, stillThere, takeBaseline, verdictLines, verify, writeChanges } from './verify.mjs';
|
|
24
|
-
import { bringBack, dropTree, makeTree, treeDiff } from './worktree.mjs';
|
|
24
|
+
import { bringBack, dropTree, makeTree, sharedSourceDependencies, treeDiff } from './worktree.mjs';
|
|
25
25
|
|
|
26
26
|
const AGENTS = ['scout', 'worker', 'reviewer'];
|
|
27
27
|
/** Work with a written way of doing it: guides/<name>.md, carried into the brief on request. */
|
|
@@ -440,7 +440,7 @@ export async function newJob(db, { project: nameOrAlias, title, agent = 'worker'
|
|
|
440
440
|
// The project's installed dependencies are linked in (makeTree): outside the job's roots, the sandbox lets it read them, never change them.
|
|
441
441
|
const deps =
|
|
442
442
|
tree.linked?.length > 0
|
|
443
|
-
? ` The project's installed dependencies — ${tree.linked.join(', ')} — are its own, linked in: the checks run with them, and they are read-only here; if one must be added or changed, say so in the report.
|
|
443
|
+
? ` The project's installed dependencies — ${tree.linked.join(', ')} — are its own, linked in: the checks run with them, and they are read-only here; if one must be added or changed, say so in the report. Verification reports shared workspace packages and editable installs that need an independent installation in this worktree.`
|
|
444
444
|
: ' If the checks need installed dependencies, install them there.';
|
|
445
445
|
appendFileSync(fileOf(job.id, 'brief.md'), `\n## Where you work\n${why} Files git ignores — build output and the like — were not copied into it.${deps}\n`, { mode: 0o600 });
|
|
446
446
|
}
|
|
@@ -697,9 +697,16 @@ function checkable(db, id, verb) {
|
|
|
697
697
|
return { job, state, root };
|
|
698
698
|
}
|
|
699
699
|
|
|
700
|
+
function sharedSourceReason(state, project) {
|
|
701
|
+
const shared = sharedSourceDependencies(state.tree, project.path);
|
|
702
|
+
return shared.length ? `shared dependencies (${shared.join(', ')}) may load editable or workspace source from the original checkout. Install those dependencies independently in the worktree and verify again` : null;
|
|
703
|
+
}
|
|
704
|
+
|
|
700
705
|
/** The project's checks as they stand before the work. Taken once; a second call reports what the first found. */
|
|
701
706
|
export function baseline(db, id) {
|
|
702
|
-
const { state, root } = checkable(db, id, 'given a baseline');
|
|
707
|
+
const { job, state, root } = checkable(db, id, 'given a baseline');
|
|
708
|
+
const shared = sharedSourceReason(state, getProject(db, job.project));
|
|
709
|
+
if (shared) return [`no baseline taken: ${shared}`];
|
|
703
710
|
if (!state.baseline) {
|
|
704
711
|
const taken = takeBaseline(root, state.snap, dirOf(id));
|
|
705
712
|
if (taken.refused) return [`no baseline taken: ${taken.refused}.`, 'Every check that fails at the end will count as this job\'s.'];
|
|
@@ -717,8 +724,13 @@ export function baseline(db, id) {
|
|
|
717
724
|
|
|
718
725
|
/** Runs the checks now and records the verdict against the exact tree it judged. */
|
|
719
726
|
export function verifyJob(db, id, now = new Date().toISOString()) {
|
|
720
|
-
const { state, root } = checkable(db, id, 'verified');
|
|
727
|
+
const { job, state, root } = checkable(db, id, 'verified');
|
|
721
728
|
const verdict = verify(root, state, dirOf(id), now);
|
|
729
|
+
const shared = sharedSourceReason(state, getProject(db, job.project));
|
|
730
|
+
if (shared) {
|
|
731
|
+
verdict.ok = false;
|
|
732
|
+
verdict.inconclusive = [...(verdict.inconclusive ?? []), shared];
|
|
733
|
+
}
|
|
722
734
|
writeState(db, id, { ...state, verdict });
|
|
723
735
|
return verdict;
|
|
724
736
|
}
|
|
@@ -742,10 +754,11 @@ export function changes(db, id) {
|
|
|
742
754
|
* whether the tree moved, so it is always run again.
|
|
743
755
|
*/
|
|
744
756
|
function verdictForDone(db, id, state, now) {
|
|
745
|
-
const
|
|
757
|
+
const project = getProject(db, getJob(db, id).project);
|
|
758
|
+
const root = workdir(state, project);
|
|
746
759
|
if (!existsSync(root)) return null;
|
|
747
760
|
const last = state.verdict;
|
|
748
|
-
if (last?.fingerprint && state.snap && unmoved(root, state.snap, last.fingerprint)) return last;
|
|
761
|
+
if (last?.version === 2 && !last.inconclusive?.length && !sharedSourceReason(state, project) && last.fingerprint && state.snap && unmoved(root, state.snap, last.fingerprint)) return last;
|
|
749
762
|
return verifyJob(db, id, now);
|
|
750
763
|
}
|
|
751
764
|
|
|
@@ -1022,6 +1035,7 @@ export function finish(db, id, { status, report, accept }, now = new Date().toIS
|
|
|
1022
1035
|
// The checks can take minutes: report a job closed in the meantime before interpreting their verdict.
|
|
1023
1036
|
requireOpen(getJob(db, id), 'finished');
|
|
1024
1037
|
if (graded && accept === undefined && !verdict) throw unchecked(`${getProject(db, job.project).path} no longer exists, so its checks cannot be run`);
|
|
1038
|
+
if (verdict?.inconclusive?.length && !verdict.blocking.length) throw unchecked(verdict.inconclusive.join('\n'));
|
|
1025
1039
|
if (verdict && !verdict.ok) {
|
|
1026
1040
|
throw new Refusal(
|
|
1027
1041
|
[
|
|
@@ -1153,16 +1167,20 @@ function said(id, heading, lines) {
|
|
|
1153
1167
|
return lines;
|
|
1154
1168
|
}
|
|
1155
1169
|
|
|
1156
|
-
|
|
1170
|
+
/* Keep the review text and a complete, applicable patch before removing or merging a tree. */
|
|
1171
|
+
function archiveTree(id, state) {
|
|
1172
|
+
writeChanges(state.tree.path, state.snap, fileOf(id, 'changes.diff'));
|
|
1173
|
+
const patch = treeDiff(state.tree, { binary: true });
|
|
1174
|
+
const file = fileOf(id, 'change.patch');
|
|
1175
|
+
writeFileSync(file + '.tmp', patch, { mode: 0o600, flush: true });
|
|
1176
|
+
renameSync(file + '.tmp', file);
|
|
1177
|
+
return patch;
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1157
1180
|
function release(id, project, state) {
|
|
1158
|
-
|
|
1159
|
-
try {
|
|
1160
|
-
writeChanges(state.tree.path, state.snap, kept);
|
|
1161
|
-
} catch {
|
|
1162
|
-
// A tree too broken to diff has nothing to keep.
|
|
1163
|
-
}
|
|
1181
|
+
archiveTree(id, state);
|
|
1164
1182
|
dropTree(project.path, state.tree.top);
|
|
1165
|
-
return
|
|
1183
|
+
return fileOf(id, 'changes.diff');
|
|
1166
1184
|
}
|
|
1167
1185
|
|
|
1168
1186
|
/** The newest other worker at work in the project itself — not in a worktree of its own — right now, or undefined. */
|
|
@@ -1175,25 +1193,27 @@ const inPlace = (db, project, id) => openWorkers(db, project.slug, id).find((o)
|
|
|
1175
1193
|
*/
|
|
1176
1194
|
function applyTree(db, id, project, state) {
|
|
1177
1195
|
const busy = inPlace(db, project, id);
|
|
1196
|
+
const patch = archiveTree(id, state);
|
|
1178
1197
|
if (busy) {
|
|
1179
|
-
const
|
|
1180
|
-
|
|
1181
|
-
const kept = release(id, project, state);
|
|
1198
|
+
const kept = fileOf(id, 'changes.diff');
|
|
1199
|
+
dropTree(project.path, state.tree.top);
|
|
1182
1200
|
if (patch.length === 0) return 'It changed nothing; its worktree is removed.';
|
|
1183
1201
|
return `The change was NOT applied: j${busy.id} "${busy.title}" is still working in ${project.path}, and the change would land under it — its start, its checks and its review would count it as its own. Its worktree is removed; what it changed is kept in ${kept}, and whole as a patch: once j${busy.id} is done, git -C ${project.path} apply ${fileOf(id, 'change.patch')}`;
|
|
1184
1202
|
}
|
|
1185
|
-
const back = bringBack(project.path, state.tree, dirOf(id));
|
|
1203
|
+
const back = bringBack(project.path, state.tree, dirOf(id), patch);
|
|
1186
1204
|
if (!back.applied && !back.empty) {
|
|
1187
1205
|
return `The change was NOT applied: it does not apply to ${project.path} as that stands now. It is kept in ${state.tree.top} — bring it over by hand, then: git -C ${project.path} worktree remove --force ${state.tree.top}${back.why ? ` (git said: ${back.why})` : ''}`;
|
|
1188
1206
|
}
|
|
1189
|
-
|
|
1207
|
+
dropTree(project.path, state.tree.top);
|
|
1190
1208
|
return back.empty ? 'It changed nothing; its worktree is removed.' : `The change was applied to ${project.path}${back.merged ? ' by a three-way merge, and staged' : ''}; its worktree is removed.`;
|
|
1191
1209
|
}
|
|
1192
1210
|
|
|
1193
1211
|
/** Done, judged by the checks, and its tree still the one they judged: an attempt worth keeping. */
|
|
1194
1212
|
function verifiedDone(db, id, state) {
|
|
1195
1213
|
const tree = state?.tree?.path;
|
|
1196
|
-
|
|
1214
|
+
const job = getJob(db, id);
|
|
1215
|
+
if (/^## Accepted without verification$/m.test(reportOf(id)) || sharedSourceReason(state, getProject(db, job.project))) return false;
|
|
1216
|
+
return job.status === 'done' && state.verdict?.version === 2 && state.verdict.ok === true && existsSync(tree ?? '') && unmoved(tree, state.snap, state.verdict.fingerprint);
|
|
1197
1217
|
}
|
|
1198
1218
|
|
|
1199
1219
|
/**
|
package/src/loop.mjs
CHANGED
|
@@ -506,6 +506,7 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
|
|
|
506
506
|
}
|
|
507
507
|
|
|
508
508
|
while (turns < MAX_TURNS) {
|
|
509
|
+
if (signal?.aborted) return interrupted();
|
|
509
510
|
// A job that has spent its budget sends nothing more: the last round it made is answered, and the run ends there.
|
|
510
511
|
if (overBudget()) {
|
|
511
512
|
stop = 'budget';
|
|
@@ -521,6 +522,7 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
|
|
|
521
522
|
let response;
|
|
522
523
|
// The same request, sent again while the API or the connection is the trouble; nothing of a failed try is kept.
|
|
523
524
|
for (let attempt = 0; ; ) {
|
|
525
|
+
if (overTime()) return { turns, stop: 'time', error: null, text, totals, toldLast };
|
|
524
526
|
try {
|
|
525
527
|
response = await send(params, { onText, onThinking, onBlock, signal });
|
|
526
528
|
break;
|
|
@@ -578,6 +580,10 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
|
|
|
578
580
|
if (!refused) text = textOf(response.content) || text;
|
|
579
581
|
for (const block of response.content) onBlock(block);
|
|
580
582
|
onTurn?.(response, totals);
|
|
583
|
+
if (overTime()) {
|
|
584
|
+
stop = 'time';
|
|
585
|
+
break;
|
|
586
|
+
}
|
|
581
587
|
// The API paused a long turn of its own accord: the reply so far is kept, and the turn simply goes on.
|
|
582
588
|
if (response.stop_reason === 'pause_turn') continue;
|
|
583
589
|
if (response.stop_reason === 'end_turn') {
|
|
@@ -597,6 +603,7 @@ export async function converse(db, params, { send = sendToApi, ctx, ledger, befo
|
|
|
597
603
|
|
|
598
604
|
totals.toolCalls += calls.length;
|
|
599
605
|
const answer = async (call) => {
|
|
606
|
+
if (overTime()) return { type: 'tool_result', tool_use_id: call.id, content: 'not run: the job reached its time limit', is_error: true };
|
|
600
607
|
if (signal?.aborted) return { type: 'tool_result', tool_use_id: call.id, content: 'not run: interrupted by the user', is_error: true };
|
|
601
608
|
onTool?.(call);
|
|
602
609
|
let out;
|
|
@@ -746,8 +753,6 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
|
|
|
746
753
|
const project = getProject(db, job.project);
|
|
747
754
|
const tree = treeOf(db, id);
|
|
748
755
|
if (tree && !existsSync(tree.path)) throw new UsageError(`j${id}'s worktree, ${tree.path}, no longer exists — abandon it and create it again`);
|
|
749
|
-
// The MCP servers of this process are the job's too: their tools behind the search tool, their names in its prompt.
|
|
750
|
-
const registry = await mcpReady();
|
|
751
756
|
// Whom this job may ask, by the setting and the switches as they are now: the next model up that is on, or nobody.
|
|
752
757
|
const advisor = (getMeta(db, 'config.jobs.advisor') ?? CONFIG_DEFAULTS['jobs.advisor']) === 'on' ? advisorOf(job.model, usableModels(db)) : null;
|
|
753
758
|
// What this job may spend, by the setting as it is now; shared by both runs, so the second goes on from what the first spent.
|
|
@@ -757,7 +762,6 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
|
|
|
757
762
|
// with an answer, say — counts from its own start, as its budget does: nothing of an earlier `sumo job run` is carried over.
|
|
758
763
|
const minutes = jobMinutes(db);
|
|
759
764
|
const clock = minutes === null ? null : { ms: minutes * 60_000, from: Date.parse(now?.() ?? new Date().toISOString()), warned: false };
|
|
760
|
-
const params = jobParams({ job, text: brief(db, id), system: withServers(systemPrompt(), registry), mcp: registry.tools(), advisor });
|
|
761
765
|
const lock = tx(db, () => takeRunLock(id));
|
|
762
766
|
const heard = () => [...(inbox?.() ?? []), ...takeInbox(id)];
|
|
763
767
|
// A command the sandbox blocked is put to the user with the job named, as its MCP calls are; its background commands are its own to end.
|
|
@@ -773,7 +777,29 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
|
|
|
773
777
|
const held = handleEvent(db, 'pre-tool', { session_id: job.session_id ?? `job:j${id}`, agent_id: `j${id}`, project: job.project, cwd: ctx.cwd, tool_name: 'bash', tool_input: { command: String(call.input?.command ?? '') } }, now?.());
|
|
774
778
|
return held ? { content: JSON.parse(held).deny, isError: true } : null;
|
|
775
779
|
};
|
|
780
|
+
const deadline = new AbortController();
|
|
781
|
+
let timer;
|
|
782
|
+
// Node timers accept at most a signed 32-bit delay.
|
|
783
|
+
function arm(ms) {
|
|
784
|
+
const delay = Math.min(ms, 2_147_483_647);
|
|
785
|
+
timer = setTimeout(() => ms > delay ? arm(ms - delay) : deadline.abort(), delay);
|
|
786
|
+
timer.unref?.();
|
|
787
|
+
}
|
|
788
|
+
if (clock) {
|
|
789
|
+
arm(clock.ms);
|
|
790
|
+
signal = signal ? AbortSignal.any([signal, deadline.signal]) : deadline.signal;
|
|
791
|
+
}
|
|
792
|
+
let cancelSetup;
|
|
776
793
|
try {
|
|
794
|
+
// The registry is shared by other conversations. Stop waiting for it when this job ends.
|
|
795
|
+
const stopped = new Promise((resolve) => {
|
|
796
|
+
cancelSetup = () => resolve(null);
|
|
797
|
+
if (signal?.aborted) cancelSetup();
|
|
798
|
+
else signal?.addEventListener('abort', cancelSetup, { once: true });
|
|
799
|
+
});
|
|
800
|
+
const registry = await Promise.race([mcpReady(), stopped]);
|
|
801
|
+
signal?.removeEventListener('abort', cancelSetup);
|
|
802
|
+
const params = jobParams({ job, text: brief(db, id), system: withServers(systemPrompt(), registry), mcp: registry?.tools(), advisor });
|
|
777
803
|
onStart?.(job);
|
|
778
804
|
// A job that outgrows the context is summarized and goes on; its brief is on disk for whatever the summary left out, and what code
|
|
779
805
|
// recorded of its work is said after the summary.
|
|
@@ -804,14 +830,17 @@ export async function runJob(db, id, { send = sendToApi, now, signal = null, onS
|
|
|
804
830
|
const totals = Object.fromEntries(Object.keys(more.totals).map((k) => [k, k === 'contextTokens' ? more.totals[k] : outcome.totals[k] + more.totals[k]]));
|
|
805
831
|
outcome = { ...more, turns: outcome.turns + more.turns, text: more.text || outcome.text, totals, toldLast: outcome.toldLast || more.toldLast };
|
|
806
832
|
}
|
|
833
|
+
if (deadline.signal.aborted && signal.reason === deadline.signal.reason) outcome.stop = 'time';
|
|
807
834
|
// Kept for what follows the job: one told here to close near a limit is not retried past it (`followUp`, src/jobs.mjs).
|
|
808
|
-
toldToClose(db, id, budget?.warned ? 'budget' : clock?.warned ? 'time' : outcome.toldLast ? 'turns' : null);
|
|
835
|
+
toldToClose(db, id, budget?.warned ? 'budget' : (clock?.warned || outcome.stop === 'time') ? 'time' : outcome.toldLast ? 'turns' : null);
|
|
809
836
|
// What it left running in the background goes before its worktree is settled: still running, it would write into a tree being
|
|
810
837
|
// brought back and removed, or be there when the change lands in the project.
|
|
811
838
|
for (const group of ctx.background) killGroup(group);
|
|
812
839
|
const settled = await settle(db, id, now?.());
|
|
813
840
|
return { ...outcome, job: getJob(db, id), settled };
|
|
814
841
|
} finally {
|
|
842
|
+
clearTimeout(timer);
|
|
843
|
+
signal?.removeEventListener('abort', cancelSetup);
|
|
815
844
|
for (const group of ctx.background) killGroup(group);
|
|
816
845
|
rmSync(lock, { force: true });
|
|
817
846
|
}
|
package/src/sessions.mjs
CHANGED
|
@@ -489,23 +489,29 @@ const redactValues = (value) => {
|
|
|
489
489
|
return value;
|
|
490
490
|
};
|
|
491
491
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
if (
|
|
499
|
-
|
|
500
|
-
if (
|
|
501
|
-
if (
|
|
502
|
-
|
|
492
|
+
// Identifiers pair tool calls and results. The API needs encrypted web results and citation indices byte-for-byte.
|
|
493
|
+
const PROTOCOL_FIELDS = new Set(['type', 'name', 'id', 'tool_use_id', 'encrypted_content', 'encrypted_index', 'encrypted_stdout']);
|
|
494
|
+
|
|
495
|
+
/** Keep the API block structure for resume while scrubbing its human-readable contents, including nested web results. */
|
|
496
|
+
function keepBlock(value) {
|
|
497
|
+
if (typeof value === 'string') return redact(value).text;
|
|
498
|
+
if (Array.isArray(value)) return value.map(keepBlock);
|
|
499
|
+
if (!value || typeof value !== 'object') return value;
|
|
500
|
+
if (value.type === 'image') return { type: 'text', text: NO_PICTURE };
|
|
501
|
+
if (value.type === 'base64') return { type: 'text', media_type: 'text/plain', data: '[binary attachment omitted]' };
|
|
502
|
+
const kept = {};
|
|
503
|
+
for (const [key, child] of Object.entries(value)) {
|
|
504
|
+
if (key === 'cache_control') continue;
|
|
505
|
+
// Tool inputs contain arbitrary user data, even under keys that look like protocol fields.
|
|
506
|
+
if (key === 'input') kept[key] = redactValues(child);
|
|
507
|
+
else kept[key] = PROTOCOL_FIELDS.has(key) ? child : keepBlock(child);
|
|
508
|
+
}
|
|
503
509
|
return kept;
|
|
504
510
|
}
|
|
505
511
|
|
|
506
|
-
/**
|
|
512
|
+
/** What the session can replay after resuming. Thinking signatures cannot survive redaction or a changed prefix. */
|
|
507
513
|
function keepable(messages) {
|
|
508
|
-
return messages.map((m) => ({ role: m.role, content:
|
|
514
|
+
return withoutThinking(messages).map((m) => ({ role: m.role, content: keepBlock(m.content) }));
|
|
509
515
|
}
|
|
510
516
|
|
|
511
517
|
/**
|
|
@@ -526,7 +532,7 @@ export function saveState({ id, messages, model, effort, routed, cwd, contextTok
|
|
|
526
532
|
const file = stateFile(id);
|
|
527
533
|
// The summary first, so a listing can read it without the messages behind it; the route's reason last, since it is prose of any length.
|
|
528
534
|
// `pending` is what jobs reported that the model has not read yet: it goes with the session, to be read when it is picked up again.
|
|
529
|
-
const state = { version: 1, id, savedAt: now, contextTokens, model, effort, cwd, messages: keepable(messages), routed, pending };
|
|
535
|
+
const state = { version: 1, id, savedAt: now, contextTokens, model, effort, cwd, messages: keepable(messages), routed: redactValues(routed), pending: redactValues(pending) };
|
|
530
536
|
writeFileSync(`${file}.tmp`, JSON.stringify(state), { mode: 0o600 });
|
|
531
537
|
renameSync(`${file}.tmp`, file);
|
|
532
538
|
return file;
|
package/src/tools.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
1
2
|
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
-
import { accessSync, closeSync, constants, existsSync, fstatSync, ftruncateSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { accessSync, closeSync, constants, existsSync, fstatSync, ftruncateSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readlinkSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
3
4
|
import { homedir, tmpdir } from 'node:os';
|
|
4
5
|
import { delimiter, dirname, extname, join, relative, resolve, sep } from 'node:path';
|
|
5
6
|
import { CREDENTIAL_NAMES } from './auth.mjs';
|
|
@@ -868,23 +869,12 @@ function undoTurns(sessionDir) {
|
|
|
868
869
|
/** The folder the coming turn's undo goes in — made only when the editor first changes a file in it. */
|
|
869
870
|
export const nextUndoTurn = (sessionDir) => join(sessionDir, String((undoTurns(sessionDir).at(-1) ?? 0) + 1));
|
|
870
871
|
|
|
871
|
-
const
|
|
872
|
-
try {
|
|
873
|
-
return JSON.parse(readFileSync(join(turnDir, MANIFEST), 'utf8'));
|
|
874
|
-
} catch {
|
|
875
|
-
return null;
|
|
876
|
-
}
|
|
877
|
-
};
|
|
872
|
+
const digest = (data) => createHash('sha256').update(data).digest('hex');
|
|
878
873
|
|
|
879
|
-
/**
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
* UNDO_TURNS, and of the undo of sessions long untouched.
|
|
884
|
-
*/
|
|
885
|
-
function keepForUndo(turnDir, path, before) {
|
|
886
|
-
const manifest = readManifest(turnDir) ?? [];
|
|
887
|
-
if (manifest.some((entry) => entry.path === path)) return;
|
|
874
|
+
/** Keep the original bytes once, and the expected file identity and content after the last editor write. */
|
|
875
|
+
function keepForUndo(turnDir, path, before, written) {
|
|
876
|
+
const file = join(turnDir, MANIFEST);
|
|
877
|
+
const manifest = existsSync(turnDir) ? JSON.parse(readFileSync(file, 'utf8')) : [];
|
|
888
878
|
if (manifest.length === 0) {
|
|
889
879
|
const sessionDir = dirname(turnDir);
|
|
890
880
|
mkdirSync(turnDir, { recursive: true, mode: 0o700 });
|
|
@@ -895,39 +885,78 @@ function keepForUndo(turnDir, path, before) {
|
|
|
895
885
|
if (other !== sessionDir && Date.now() - statSync(other).mtimeMs > UNDO_KEEP_MS) rmSync(other, { recursive: true, force: true });
|
|
896
886
|
}
|
|
897
887
|
}
|
|
898
|
-
const
|
|
899
|
-
|
|
900
|
-
manifest.
|
|
901
|
-
|
|
888
|
+
const target = realpathSync.native(path);
|
|
889
|
+
const stat = statSync(target);
|
|
890
|
+
let entry = manifest.find((item) => item.path === path || item.target === target);
|
|
891
|
+
if (!entry) {
|
|
892
|
+
const kept = before === null ? null : String(manifest.length);
|
|
893
|
+
if (kept !== null) writeFileSync(join(turnDir, kept), before, { mode: 0o600, flag: 'wx' });
|
|
894
|
+
entry = { path, target, kept, original: before === null ? null : digest(before), dev: stat.dev, ino: stat.ino, nlink: stat.nlink };
|
|
895
|
+
manifest.push(entry);
|
|
896
|
+
} else if (before === null || entry.after !== digest(before) || entry.target !== target || entry.dev !== stat.dev || entry.ino !== stat.ino || entry.nlink !== stat.nlink) {
|
|
897
|
+
entry.conflict = true; // A later editor write cannot claim an intervening change as its own.
|
|
898
|
+
}
|
|
899
|
+
entry.after = digest(written);
|
|
900
|
+
writeFileSync(file + '.tmp', JSON.stringify(manifest), { mode: 0o600 });
|
|
901
|
+
renameSync(file + '.tmp', file);
|
|
902
902
|
}
|
|
903
903
|
|
|
904
|
-
/**
|
|
905
|
-
* /undo: every file the session's last kept turn changed, put back as it was before that turn — a file it created is
|
|
906
|
-
* deleted — and the turn let go, so the next /undo reaches the one before. Null when there is none to undo.
|
|
907
|
-
*/
|
|
904
|
+
/** Check every destination and backup before touching the first file. A conflict keeps the whole turn for inspection or retry. */
|
|
908
905
|
export function undoLast(sessionDir) {
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
906
|
+
const n = undoTurns(sessionDir).at(-1);
|
|
907
|
+
if (n === undefined) return null;
|
|
908
|
+
const turnDir = join(sessionDir, String(n));
|
|
909
|
+
const cursor = join(turnDir, 'restoring');
|
|
910
|
+
let manifest, start, plan;
|
|
911
|
+
try {
|
|
912
|
+
manifest = JSON.parse(readFileSync(join(turnDir, MANIFEST), 'utf8'));
|
|
913
|
+
if (!Array.isArray(manifest) || !manifest.length) throw new Error('the undo record is incomplete');
|
|
914
|
+
// The cursor is written before each mutation. After an interruption, recognise a completed
|
|
915
|
+
// write or unlink at the cursor, and leave already restored files alone on the next /undo.
|
|
916
|
+
const resuming = existsSync(cursor);
|
|
917
|
+
start = resuming ? JSON.parse(readFileSync(cursor, 'utf8')) : 0;
|
|
918
|
+
if (!Number.isInteger(start) || start < 0 || start >= manifest.length) throw new Error('invalid undo progress');
|
|
919
|
+
plan = manifest.slice(start).map(({ path, target, kept, original, after, dev, ino, nlink, conflict }, offset) => {
|
|
920
|
+
if (!after || !target) throw new Error(`the saved record for ${path} has no verifiable after-state`);
|
|
921
|
+
if (conflict) throw new Error(`${path} was changed outside the editor during this turn`);
|
|
922
|
+
const atCursor = resuming && offset === 0;
|
|
923
|
+
const stat = statSync(path, { throwIfNoEntry: false });
|
|
924
|
+
if (atCursor && kept === null && original === null && !stat && !lstatSync(target, { throwIfNoEntry: false })) return null;
|
|
925
|
+
if (!stat?.isFile() || realpathSync.native(path) !== target || stat.dev !== dev || stat.ino !== ino || stat.nlink !== nlink) throw new Error(`${path} was removed or replaced`);
|
|
926
|
+
const content = digest(readFileSync(target));
|
|
927
|
+
if (atCursor && kept !== null && content === original) return null;
|
|
928
|
+
if (content !== after) throw new Error(`${path} changed after the editor wrote it`);
|
|
929
|
+
const remove = kept === null && original === null;
|
|
930
|
+
if (remove) accessSync(dirname(target), constants.W_OK | constants.X_OK);
|
|
931
|
+
else if (kept !== String(start + offset)) throw new Error(`invalid backup for ${path}`);
|
|
932
|
+
else accessSync(target, constants.W_OK);
|
|
933
|
+
const bytes = remove ? null : readFileSync(join(turnDir, kept));
|
|
934
|
+
if (bytes !== null && digest(bytes) !== original) throw new Error(`the backup for ${path} has changed`);
|
|
935
|
+
return { path, target, stat, after, bytes };
|
|
936
|
+
});
|
|
937
|
+
} catch (cause) {
|
|
938
|
+
return { blocked: `undo stopped before changing any files: ${cause.message}. The saved turn is at ${turnDir}.` };
|
|
939
|
+
}
|
|
940
|
+
try {
|
|
941
|
+
for (const [offset, entry] of plan.entries()) {
|
|
942
|
+
if (!entry) continue;
|
|
943
|
+
const { path, target, stat, after, bytes } = entry;
|
|
944
|
+
writeFileSync(cursor + '.tmp', String(start + offset), { mode: 0o600 });
|
|
945
|
+
renameSync(cursor + '.tmp', cursor);
|
|
946
|
+
const current = statSync(path);
|
|
947
|
+
if (realpathSync.native(path) !== target || current.dev !== stat.dev || current.ino !== stat.ino || current.nlink !== stat.nlink || digest(readFileSync(target)) !== after) {
|
|
948
|
+
throw new Error(`${path} changed while undo was running`);
|
|
925
949
|
}
|
|
950
|
+
if (bytes === null) rmSync(target);
|
|
951
|
+
else writeTo(target, bytes);
|
|
926
952
|
}
|
|
927
953
|
rmSync(turnDir, { recursive: true, force: true });
|
|
928
|
-
|
|
954
|
+
} catch (cause) {
|
|
955
|
+
return { blocked: `undo stopped while restoring files: ${cause.message}. Progress is saved in ${turnDir}. Retry /undo to resume.` };
|
|
929
956
|
}
|
|
930
|
-
|
|
957
|
+
const done = { restored: [], deleted: [] };
|
|
958
|
+
for (const { path, kept } of manifest) (kept === null ? done.deleted : done.restored).push(path);
|
|
959
|
+
return done;
|
|
931
960
|
}
|
|
932
961
|
|
|
933
962
|
/**
|
|
@@ -935,16 +964,16 @@ export function undoLast(sessionDir) {
|
|
|
935
964
|
* chat's turn folder), a change on disk since the model saw it is said first, and a parse the change broke — one that held
|
|
936
965
|
* before it — is said after. `created`: the file was not there, so it parsed before as far as anyone is concerned.
|
|
937
966
|
*/
|
|
938
|
-
function change(path, ctx,
|
|
967
|
+
function change(path, ctx, written, { created = false } = {}) {
|
|
939
968
|
const stale = created ? null : changedSince(path, ctx);
|
|
940
969
|
const before = ctx.undo && !created ? readFileSync(path) : null;
|
|
941
970
|
const broken = created ? null : parseError(path, ctx);
|
|
942
|
-
|
|
971
|
+
writeTo(path, written, { create: created });
|
|
943
972
|
// Kept once the write has gone through: a change refused — a create of a file already there — has nothing to undo. A keep
|
|
944
973
|
// that fails is said, not thrown: the change is made, and a model told it failed would make it twice.
|
|
945
974
|
let unkept = null;
|
|
946
975
|
try {
|
|
947
|
-
if (ctx.undo) keepForUndo(ctx.undo, path, before);
|
|
976
|
+
if (ctx.undo) keepForUndo(ctx.undo, path, before, written);
|
|
948
977
|
} catch (cause) {
|
|
949
978
|
unkept = `(this change could not be kept for /undo: ${cause.message})`;
|
|
950
979
|
}
|
|
@@ -1160,7 +1189,7 @@ export function runEditor(input, ctx) {
|
|
|
1160
1189
|
return result(`${path} already exists (${lines} line${lines === 1 ? '' : 's'}): change the lines that change with str_replace or insert — a file is never rewritten whole`, true);
|
|
1161
1190
|
}
|
|
1162
1191
|
mkdirSync(dirname(path), { recursive: true });
|
|
1163
|
-
const said = change(path, ctx,
|
|
1192
|
+
const said = change(path, ctx, String(input.file_text ?? ''), { created: true });
|
|
1164
1193
|
return result(said(`created ${path}`));
|
|
1165
1194
|
}
|
|
1166
1195
|
case 'str_replace': {
|
|
@@ -1178,7 +1207,7 @@ export function runEditor(input, ctx) {
|
|
|
1178
1207
|
if (text.includes('\r\n')) replacement = replacement.replace(/\r?\n/g, '\r\n');
|
|
1179
1208
|
if (where.reindent) replacement = where.reindent(replacement);
|
|
1180
1209
|
}
|
|
1181
|
-
const said = change(path, ctx,
|
|
1210
|
+
const said = change(path, ctx, `${text.slice(0, where.start)}${replacement}${text.slice(where.end)}`);
|
|
1182
1211
|
return { ...result(said(`edited ${path}${where.how ? ` (old_str matched ${where.how})` : ''}`)), edit: changeOf(text, where, replacement) };
|
|
1183
1212
|
}
|
|
1184
1213
|
case 'insert': {
|
|
@@ -1194,7 +1223,7 @@ export function runEditor(input, ctx) {
|
|
|
1194
1223
|
// A file ending in a newline splits to a last empty piece, which is not a line to insert after.
|
|
1195
1224
|
const at = Math.max(0, Math.min(lines.at(-1) === '' ? lines.length - 1 : lines.length, line));
|
|
1196
1225
|
lines.splice(at, 0, text.replace(/\n$/, ''));
|
|
1197
|
-
const said = change(path, ctx,
|
|
1226
|
+
const said = change(path, ctx, lines.join('\n'));
|
|
1198
1227
|
return { ...result(said(`inserted into ${path} after line ${at}`)), edit: { line: at + 1, old: '', new: redact(text.replace(/\n$/, '')).text } };
|
|
1199
1228
|
}
|
|
1200
1229
|
default:
|
package/src/tty.mjs
CHANGED
|
@@ -23,11 +23,11 @@ const CODES = { bold: ['1', '22'], dim: ['2', '22'], italic: ['3', '23'], underl
|
|
|
23
23
|
* reader says as itself: no box drawing, no symbol spelled out by its Unicode name.
|
|
24
24
|
*/
|
|
25
25
|
const GLYPHS = {
|
|
26
|
-
tool: '⏺', fail: '✗', pick: '❯', under: '⎿', bar: '┃', quote: '│', rule: '─',
|
|
26
|
+
tool: '⏺', fail: '✗', pick: '❯', under: '⎿', bar: '┃', quote: '│', rule: '─', todo: '☐', done: '☑', bullet: '•', warn: '⚠', thought: '✻',
|
|
27
27
|
box: { h: '─', v: '│', tl: '┌', tm: '┬', tr: '┐', ml: '├', mm: '┼', mr: '┤', bl: '└', bm: '┴', br: '┘' },
|
|
28
28
|
};
|
|
29
29
|
const PLAIN_GLYPHS = {
|
|
30
|
-
tool: '*', fail: 'x', pick: '>', under: '|', bar: '>', quote: '|', rule: '-',
|
|
30
|
+
tool: '*', fail: 'x', pick: '>', under: '|', bar: '>', quote: '|', rule: '-', todo: '[ ]', done: '[x]', bullet: '-', warn: '!', thought: '*',
|
|
31
31
|
box: { h: '-', v: '|', tl: '+', tm: '+', tr: '+', ml: '+', mm: '+', mr: '+', bl: '+', bm: '+', br: '+' },
|
|
32
32
|
};
|
|
33
33
|
|
|
@@ -72,7 +72,7 @@ const WIDE = /[\u1100-\u115F\u2E80-\u303E\u3041-\u33FF\u3400-\u4DBF\u4E00-\u9FFF
|
|
|
72
72
|
const columnsOf = (ch) => (WIDE.test(ch) ? 2 : 1);
|
|
73
73
|
|
|
74
74
|
/** The columns text takes on a terminal, colour codes not counted: a wide character is two, so a table around it lines up. */
|
|
75
|
-
const visible = (t) => {
|
|
75
|
+
export const visible = (t) => {
|
|
76
76
|
const plain = t.replace(/\x1b\[[0-9;]*m|\x1b\]8;[^\x07]*\x07/g, '');
|
|
77
77
|
if (!/[^\x00-\x7e]/.test(plain)) return plain.length;
|
|
78
78
|
let width = 0;
|
|
@@ -223,10 +223,11 @@ export function highlight(line, lang, s, code = null) {
|
|
|
223
223
|
* One line of a reply: block-level marks replaced by weight and indent, then wrapped with a hanging indent.
|
|
224
224
|
* `hang` is the indent of the list item a line may go on with: an indented line that is not an item of its own is drawn there.
|
|
225
225
|
*/
|
|
226
|
-
export function renderLine(line, s, { fence = false, width = MAX_WIDTH, hang = null, lang = '', code = null } = {}) {
|
|
227
|
-
// Code is
|
|
228
|
-
//
|
|
229
|
-
|
|
226
|
+
export function renderLine(line, s, { fence = false, width = MAX_WIDTH, hang = null, lang = '', code = null, whole = false } = {}) {
|
|
227
|
+
// Code is drawn as it was written, from the first column, with nothing around it: what is selected on the screen is the
|
|
228
|
+
// code. `whole`, a line too long for the window is left for the terminal to wrap, and a copy joins it again; else it
|
|
229
|
+
// goes on under itself, at its own indent, so the code keeps its shape.
|
|
230
|
+
if (fence) return whole ? highlight(line, lang, s, code) : wrap(highlight(line, lang, s, code), width, /^\s*/.exec(line)[0]);
|
|
230
231
|
const heading = /^(#{1,6})\s+(.*)$/.exec(line);
|
|
231
232
|
if (heading) return wrap(heading[1].length === 1 ? s.bold(s.underline(inline(heading[2], s))) : s.bold(inline(heading[2], s)), width);
|
|
232
233
|
const item = LIST_ITEM.exec(line);
|
|
@@ -382,7 +383,14 @@ function closedForNow(line) {
|
|
|
382
383
|
return shown.slice(at + 2).trim() ? `${words}**${shown.slice(words.length)}` : shown.slice(0, at);
|
|
383
384
|
}
|
|
384
385
|
|
|
385
|
-
|
|
386
|
+
const FENCE = /^(\s*)```\s*(\S*)/;
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* A reply rendered as it streams, each finished line handed to `write` with `{ code }`: whether it is a line of a code
|
|
390
|
+
* block. `whole` leaves a code line unbroken for the terminal to wrap (see renderLine); the line still arriving is
|
|
391
|
+
* always wrapped, for it is drawn and drawn again inside the window.
|
|
392
|
+
*/
|
|
393
|
+
export function createRenderer(write, s, { width = MAX_WIDTH, whole = false } = {}) {
|
|
386
394
|
let tail = '';
|
|
387
395
|
let fence = false;
|
|
388
396
|
let rows = [];
|
|
@@ -391,19 +399,30 @@ export function createRenderer(write, s, { width = MAX_WIDTH } = {}) {
|
|
|
391
399
|
/** The language the code block in hand is in, and where its colouring has got to: a comment can run over lines. */
|
|
392
400
|
let lang = '';
|
|
393
401
|
let code = { comment: false };
|
|
402
|
+
/** A code line loses the indent its fence has, as Markdown reads it: a block in a list item is not drawn, or copied, at the item's indent. */
|
|
403
|
+
let indent = '';
|
|
404
|
+
const unindented = (raw) => (raw.startsWith(indent) ? raw.slice(indent.length) : raw.trimStart());
|
|
405
|
+
/** A line rendered, or null for a fence: outside plain mode it draws no row of its own. */
|
|
394
406
|
const line = (raw) => {
|
|
395
|
-
const marks =
|
|
407
|
+
const marks = FENCE.exec(raw);
|
|
396
408
|
if (marks) {
|
|
397
409
|
fence = !fence;
|
|
398
410
|
hang = null;
|
|
399
|
-
lang = fence ? marks[
|
|
411
|
+
lang = fence ? marks[2].toLowerCase() : '';
|
|
412
|
+
indent = fence ? marks[1] : '';
|
|
400
413
|
code = { comment: false };
|
|
401
|
-
// Plain mode names the block in words, for a reader
|
|
402
|
-
if (s.plain) return s.dim(fence ? `
|
|
403
|
-
return
|
|
414
|
+
// Plain mode names the block in words, for a reader.
|
|
415
|
+
if (s.plain) return s.dim(fence ? `code${marks[2] ? ` (${marks[2]})` : ''}:` : 'end of code');
|
|
416
|
+
return null;
|
|
404
417
|
}
|
|
405
|
-
if (
|
|
406
|
-
|
|
418
|
+
if (fence) return renderLine(unindented(raw), s, { fence, width, lang, code, whole });
|
|
419
|
+
hang = hangOf(raw) ?? (/^\s+\S/.test(raw) || raw.trim() === '' ? hang : null);
|
|
420
|
+
return renderLine(raw, s, { width, hang: hangOf(raw) === null ? hang : null });
|
|
421
|
+
};
|
|
422
|
+
const put = (raw, end) => {
|
|
423
|
+
const isCode = fence && !FENCE.test(raw);
|
|
424
|
+
const shown = line(raw);
|
|
425
|
+
if (shown !== null) write(`${shown}${end}`, { code: isCode });
|
|
407
426
|
};
|
|
408
427
|
/** The rows held so far: a table once its rule has come, the lines they were otherwise. */
|
|
409
428
|
const held = () => (rows.length >= 2 && TABLE_RULE.test(rows[1]) ? renderTable(rows, s, width) : rows.map((r) => renderLine(r, s, { width })).join('\n'));
|
|
@@ -418,9 +437,9 @@ export function createRenderer(write, s, { width = MAX_WIDTH } = {}) {
|
|
|
418
437
|
rows.push(raw);
|
|
419
438
|
continue;
|
|
420
439
|
}
|
|
421
|
-
if (rows.length > 0) write(`${held()}\n
|
|
440
|
+
if (rows.length > 0) write(`${held()}\n`, { code: false });
|
|
422
441
|
rows = [];
|
|
423
|
-
|
|
442
|
+
put(raw, '\n');
|
|
424
443
|
}
|
|
425
444
|
},
|
|
426
445
|
flush() {
|
|
@@ -428,19 +447,24 @@ export function createRenderer(write, s, { width = MAX_WIDTH } = {}) {
|
|
|
428
447
|
rows.push(tail);
|
|
429
448
|
tail = '';
|
|
430
449
|
}
|
|
431
|
-
if (rows.length > 0) write(`${held()}${tail ? '\n' : ''}
|
|
432
|
-
if (tail)
|
|
450
|
+
if (rows.length > 0) write(`${held()}${tail ? '\n' : ''}`, { code: false });
|
|
451
|
+
if (tail) put(tail, '');
|
|
433
452
|
tail = '';
|
|
434
453
|
rows = [];
|
|
435
454
|
fence = false;
|
|
436
455
|
hang = null;
|
|
437
456
|
lang = '';
|
|
457
|
+
indent = '';
|
|
438
458
|
code = { comment: false };
|
|
439
459
|
},
|
|
460
|
+
/** Whether the line still arriving is a line of code. */
|
|
461
|
+
get fenced() {
|
|
462
|
+
return fence;
|
|
463
|
+
},
|
|
440
464
|
/** What has not been written yet, rendered as far as it has got: the rows of a table, and the line still arriving. */
|
|
441
465
|
get tail() {
|
|
442
466
|
const going = /^\s+\S/.test(tail) && hangOf(tail) === null ? hang : null;
|
|
443
|
-
return [rows.length > 0 ? held() : '', tail &&
|
|
467
|
+
return [rows.length > 0 ? held() : '', tail && !FENCE.test(tail) ? renderLine(fence ? unindented(tail) : closedForNow(tail), s, { fence, width, hang: going, lang, code: { ...code } }) : ''].filter(Boolean).join('\n');
|
|
444
468
|
},
|
|
445
469
|
};
|
|
446
470
|
}
|
|
@@ -847,10 +871,11 @@ export function transcript(log, s, { width = MAX_WIDTH, cwd = null } = {}) {
|
|
|
847
871
|
if (entry.kind === 'user') out.push('', ...untab(entry.text).split('\n').map((line) => `${s.yellow(g.bar)} ${line}`));
|
|
848
872
|
else if (entry.kind === 'reply') {
|
|
849
873
|
const drawn = [];
|
|
850
|
-
const reply = createRenderer((text) => drawn.push(text), s, { width: Math.max(MIN_WIDTH, width - 3) });
|
|
874
|
+
const reply = createRenderer((text, { code }) => drawn.push(...text.replace(/\n$/, '').split('\n').map((row) => ({ row, code }))), s, { width: Math.max(MIN_WIDTH, width - 3) });
|
|
851
875
|
reply.write(entry.raw);
|
|
852
876
|
reply.flush();
|
|
853
|
-
|
|
877
|
+
// A line of code stands at the first column, as on the chat screen: what is selected is the code.
|
|
878
|
+
out.push('', ...drawn.map(({ row, code }, i) => (code ? row : `${i === 0 ? `${g.tool} ` : ' '}${row}`)));
|
|
854
879
|
} else if (entry.kind === 'tool') {
|
|
855
880
|
const view = toolView(entry.call, entry.result, { full: true, cwd });
|
|
856
881
|
const stat = statOf(view.stat, s);
|