worktrust 0.10.35 → 0.10.37
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 +1 -1
- package/README.md +47 -1
- package/SECURITY.md +63 -1
- package/chat-exports.mjs +281 -54
- package/config-edits.mjs +25 -0
- package/count-behaviour.mjs +8 -3
- package/engine-release.json +1 -1
- package/history-outbox.mjs +75 -16
- package/history-progress.mjs +141 -0
- package/log-session.mjs +39 -36
- package/package-release.json +1 -1
- package/package.json +4 -2
- package/preserve.mjs +4 -3
- package/self-update.mjs +274 -0
- package/setup-mcp.mjs +6 -4
- package/worktrust.mjs +309 -44
package/history-outbox.mjs
CHANGED
|
@@ -39,7 +39,9 @@ export function queuedHistory(target,body,source) {
|
|
|
39
39
|
const request=join(root,id+'.request.json'),ack=join(root,id+'.ack.json'),state=join(root,id+'.state.json');
|
|
40
40
|
const acknowledged=read(ack);
|
|
41
41
|
// REPLACED (0.10.29): a batch cut into smaller ones is acknowledged by its pieces; the caller follows them.
|
|
42
|
-
|
|
42
|
+
// CLOSED (2026-10-31): a batch WorkTrust refused for good is let go here, never "acknowledged": WorkTrust holds nothing of it,
|
|
43
|
+
// so a send that finds it says it was refused, never "already held" (CLI 0.10.35 counted it held and reported a run whole).
|
|
44
|
+
if(acknowledged?.id===id)return {id,status:Array.isArray(acknowledged.replacedBy)?'replaced':acknowledged.closedAt?'closed':'acknowledged',replacedBy:Array.isArray(acknowledged.replacedBy)?acknowledged.replacedBy.filter(child=>/^[a-f0-9]{64}$/.test(child)):[],lines:Number.isInteger(acknowledged.lines)?acknowledged.lines:null,confirm(){},close(){},defer(){},oversize(){},replace(){return [];}};
|
|
43
45
|
const current=read(state);
|
|
44
46
|
reservation(root,()=>{
|
|
45
47
|
if(!existsSync(request)) {
|
|
@@ -73,10 +75,41 @@ export function historyRefusal(text,response) {
|
|
|
73
75
|
// A staged run WorkTrust closed or refused (0.10.27: another computer's key, seven days without its last batch) is final.
|
|
74
76
|
const closed=rpc?.id===1 && String(rpc?.error?.message??'').startsWith('history run refused:');
|
|
75
77
|
// A 413 (0.10.29) is the batch, not the queue: it is cut smaller, never sent again as it was, and nothing else waits for it.
|
|
76
|
-
// THE SERVER'S OWN SENTENCE (0.10.35
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
// THE SERVER'S OWN SENTENCE (0.10.35; every final refusal since 2026-10-31): why WorkTrust refused the batch, printed instead of
|
|
79
|
+
// a guess (0.10.35 said "it missed a batch for seven days" of a batch refused for its lines). One line, control characters
|
|
80
|
+
// taken out, at most 400 characters. AND OF A BATCH THAT WAITS (0.10.37, incident 2026-11-05): an 'unconfirmed' reply that
|
|
81
|
+
// carries WorkTrust's own error ("could not import (57014)") hands its sentence on too, so a person reads why, not a bare
|
|
82
|
+
// "Not all of it arrived" that needed a database to explain. A review is said by the caller in its own words.
|
|
83
|
+
const message=rpc?.id===1&&typeof rpc?.error?.message==='string'&&rpc.error.message.trim()?rpc.error.message:null;
|
|
84
|
+
const said=message&&!review?message.replace(/[\u0000-\u001f\u007f-\u009f]/g,' ').slice(0,400):null;
|
|
85
|
+
// WHICH BUDGET AND HOW MUCH LANDED (2026-11-05): a 429's data.reason (minute_budget is waited out, day_budget stops), and the
|
|
86
|
+
// door's "ran out of time" counts, so a resend that lands more is told from one that lands nothing (deliverHistory).
|
|
87
|
+
const budget=rpc?.id===1&&typeof rpc?.error?.data?.reason==='string'&&/^[a-z_]{1,40}$/.test(rpc.error.data.reason)?rpc.error.data.reason:null,partial=said?partialOf(said):null;
|
|
88
|
+
return {reason:review?'review':closed?'closed':response.status===413?'oversize':response.status===429?'capacity':'unconfirmed',seconds:Number.isFinite(raw)&&raw>0?Math.min(MAX_RETRY_SECONDS,raw):0,...(said?{said}:{}),...(budget?{budget}:{}),...(partial?{partial}:{})};
|
|
89
|
+
}
|
|
90
|
+
/** import-history.ts's "the batch ran out of time: N new lines were stored in whole slices and M were not sent" as { stored, unsent }. */
|
|
91
|
+
export function partialOf(said){
|
|
92
|
+
if(typeof said!=='string'||!/\bran out of time\b/i.test(said))return null;
|
|
93
|
+
const stored=/(\d{1,7})\s+new\s+lines?\s+(?:were\s+|was\s+)?stored/i.exec(said),unsent=/(\d{1,7})\s+(?:were\s+|was\s+)?not\s+sent/i.exec(said);
|
|
94
|
+
return stored?{stored:Number(stored[1]),unsent:unsent?Number(unsent[1]):null}:null;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A BATCH THAT LANDED IN PART GOES AGAIN AT ONCE (incident 2026-11-05: 0.10.36 paused at the door's "ran out of time" and left 26
|
|
98
|
+
* of 45 export batches waiting). A line already received is kept once, so a PATIENT send (the person's own; never a hook) resends
|
|
99
|
+
* the batch while each resend lands more (a stored count above 0; a sentence without counts earns one resend), at most ten (each
|
|
100
|
+
* lands a whole slice of 20 of at most 200), and waits out the upload budget per minute (≤ 90 s a wait, 5 a batch). Anything else
|
|
101
|
+
* is answered as before. `confirm(text)` throws unless the text is a receipt for every line; a network error is the caller's.
|
|
102
|
+
*/
|
|
103
|
+
export const HISTORY_RESENDS=10,BUDGET_WAIT_SECONDS=90,BUDGET_WAITS=5;
|
|
104
|
+
export async function deliverHistory(send,confirm,{patient=false,lines=0,progress=null,halt=null,sleep=(ms)=>new Promise((done)=>setTimeout(done,ms))}={}){ // `halt`: a stop asked, so nothing is sent again
|
|
105
|
+
for(let resends=0,waits=0;;){
|
|
106
|
+
const response=await send(),text=await response.text();
|
|
107
|
+
try{if(!response.ok)throw Error();confirm(text);return {ok:true,response,text};}catch{/* a refusal, read below */}
|
|
108
|
+
const refusal=historyRefusal(text,response),part=refusal.partial;
|
|
109
|
+
if(patient&&!halt?.()&&refusal.reason==='unconfirmed'&&response.status===200&&refusal.said&&!(refusal.seconds>0)&&resends<HISTORY_RESENDS&&(part?part.stored>0:resends===0)){resends++;progress?.({phase:'resend',lines,...(part?{stored:part.stored,...(Number.isInteger(part.unsent)?{unsent:part.unsent}:{})}:{})});continue;}
|
|
110
|
+
if(patient&&!halt?.()&&refusal.budget==='minute_budget'&&refusal.reason==='capacity'&&refusal.seconds>0&&refusal.seconds<=BUDGET_WAIT_SECONDS&&waits<BUDGET_WAITS){waits++;progress?.({phase:'wait',seconds:refusal.seconds});await sleep(refusal.seconds*1000);continue;}
|
|
111
|
+
return {ok:false,response,text,refusal};
|
|
112
|
+
}
|
|
80
113
|
}
|
|
81
114
|
|
|
82
115
|
/** Explicit recovery, even after the AI client removed its source files. A changed account/key
|
|
@@ -89,8 +122,10 @@ export function historyRefusal(text,response) {
|
|
|
89
122
|
* of its batches is still on this computer; once WorkTrust holds part of a run, the rest can never complete it, and is let go
|
|
90
123
|
* (`refused`: WorkTrust keeps the earlier lines counting, and `npx worktrust@latest history` measures again). A single line too
|
|
91
124
|
* large for a call is let go the same way. `only` limits a pass to those batches (and their pieces): the send that just queued them.
|
|
125
|
+
* `patient` (2026-11-05): the person's own pass resends a batch landed in part and waits out a minute's budget (deliverHistory);
|
|
126
|
+
* a hook's pass never does. `progress` hears each batch's counts, for the command's display.
|
|
92
127
|
*/
|
|
93
|
-
export async function resumeHistory(target, {send, confirmed, note, retryReview=false, dry=false, limit=Infinity, cutter=null, only=null}) {
|
|
128
|
+
export async function resumeHistory(target, {send, confirmed, note, closed=null, retryReview=false, dry=false, limit=Infinity, cutter=null, only=null, patient=false, progress=null, halt=null, sleep=undefined}) {
|
|
94
129
|
const root=join(homedir(),'.worktrust','history-outbox'),scope=scopeOf(target);
|
|
95
130
|
const totals={received:0,review:0,waiting:0,batches:0,refused:0,held:0,split:0};let paused=null,sent=0;
|
|
96
131
|
if(!existsSync(root))return totals;
|
|
@@ -113,7 +148,8 @@ export async function resumeHistory(target, {send, confirmed, note, retryReview=
|
|
|
113
148
|
const marked=(id)=>read(join(root,id+'.state.json'))?.status==='oversize';
|
|
114
149
|
const whole=(batches)=>batches.length>0&&batches.length===batches[0].run.count&&batches.every((item,index)=>item.run.index===index&&item.run.count===batches[0].run.count);
|
|
115
150
|
const queue=only?[...only]:[...ids],seen=new Set();
|
|
116
|
-
|
|
151
|
+
// `closed` (2026-10-31): told which run lost a batch for good, so the send never reports that run whole.
|
|
152
|
+
const letGo=(batches)=>{for(const batch of batches){queuedHistory(target,batch.body,batch.source).close('oversize');items.set(batch.id,null);seen.add(batch.id);totals.refused+=batch.entries.length;closed?.(batch.run,batch.entries.length);}return null;};
|
|
117
153
|
// Cut smaller: answers the pieces' ids, or null when the batch (or its run) can never fit and was let go.
|
|
118
154
|
const resplit=(item,queued,tighter)=>{
|
|
119
155
|
if(item.run){
|
|
@@ -134,10 +170,10 @@ export async function resumeHistory(target, {send, confirmed, note, retryReview=
|
|
|
134
170
|
if(ack?.id===id){if(Array.isArray(ack.replacedBy))queue.unshift(...ack.replacedBy.filter(child=>/^[a-f0-9]{64}$/.test(child)));else if(only&&Number.isInteger(ack.lines))totals.held+=ack.lines;continue;}
|
|
135
171
|
const item=load(id);if(!item)continue;
|
|
136
172
|
const queued=queuedHistory(target,item.body,item.source);totals.batches++;
|
|
137
|
-
if(queued.status==='acknowledged')continue;
|
|
173
|
+
if(queued.status==='acknowledged'||queued.status==='closed')continue;
|
|
138
174
|
if(queued.status==='replaced'){queue.unshift(...queued.replacedBy);continue;}
|
|
139
175
|
// `limit` (0.10.27): the hook sends a few a run by itself; the rest wait, untouched, for the next run.
|
|
140
|
-
if(sent>=limit){totals.waiting+=item.entries.length;continue;}
|
|
176
|
+
if(sent>=limit||halt?.()){totals.waiting+=item.entries.length;continue;} // or a stop was asked (2026-11-05)
|
|
141
177
|
if(queued.status==='review'&&!retryReview){totals.review+=item.entries.length;continue;}
|
|
142
178
|
if(cutter){
|
|
143
179
|
const tighter=queued.status==='oversize',runTooLarge=Boolean(item.run)&&runOf(item.run.id).some(batch=>!cutter.fits(batch.entries)||marked(batch.id));
|
|
@@ -150,20 +186,43 @@ export async function resumeHistory(target, {send, confirmed, note, retryReview=
|
|
|
150
186
|
if(queued.status==='waiting'&&!paused)paused={reason:'unconfirmed',seconds:Math.max(1,Math.ceil((queued.retryAt-Date.now())/1000))};
|
|
151
187
|
if(paused){if(queued.status==='review'){totals.review+=item.entries.length;continue;}queued.defer(paused.reason,paused.seconds);totals.waiting+=item.entries.length;continue;}
|
|
152
188
|
try {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const refusal=
|
|
157
|
-
if(refusal.reason==='closed'){queued.close();totals.refused+=item.entries.length;if(refusal.said)totals.said=refusal.said;continue;}
|
|
189
|
+
// 2026-11-05: through deliverHistory, so a patient pass (`--resume`, the send's own cut) resends a batch landed in part at once.
|
|
190
|
+
sent++;const delivered=await deliverHistory(()=>send(item.body,item.source),(text)=>confirmed(text,item.entries.length),{patient,lines:item.entries.length,progress,halt,sleep}),text=delivered.text;
|
|
191
|
+
if(!delivered.ok){
|
|
192
|
+
const refusal=delivered.refusal;if(refusal.reason!=='oversize')progress?.({phase:'batch',lines:item.entries.length,landed:false});
|
|
193
|
+
if(refusal.reason==='closed'){queued.close();totals.refused+=item.entries.length;if(refusal.said)totals.said=refusal.said;closed?.(item.run,item.entries.length);continue;}
|
|
158
194
|
// Too large (0.10.29): marked and taken again at once, by the cut, never resent as it was. Without a cutter it waits.
|
|
159
195
|
if(refusal.reason==='oversize'){queued.oversize();if(cutter){seen.delete(id);queue.unshift(id);}else totals.waiting+=item.entries.length;continue;}
|
|
196
|
+
if(refusal.said)totals.reason=refusal.said; // 0.10.37: why the batch waits, in WorkTrust's words (the last one heard)
|
|
160
197
|
if(refusal.reason!=='review')paused={reason:refusal.reason,seconds:refusal.seconds>0?refusal.seconds:5};queued.defer(refusal.reason,paused?.seconds??refusal.seconds);totals[refusal.reason==='review'?'review':'waiting']+=item.entries.length;continue;
|
|
161
198
|
}
|
|
162
|
-
queued.confirm(item.entries.length);totals.received+=item.entries.length;note(text);
|
|
163
|
-
}catch{paused={reason:'unconfirmed',seconds:5};queued.defer(paused.reason,paused.seconds);totals.waiting+=item.entries.length;}
|
|
199
|
+
queued.confirm(item.entries.length);totals.received+=item.entries.length;note(text,item.run);progress?.({phase:'batch',lines:item.entries.length,landed:true});
|
|
200
|
+
}catch{paused={reason:'unconfirmed',seconds:5};queued.defer(paused.reason,paused.seconds);totals.waiting+=item.entries.length;progress?.({phase:'batch',lines:item.entries.length,landed:false});}
|
|
164
201
|
}
|
|
165
202
|
return totals;
|
|
166
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* A RUN IS DONE WHEN WORKTRUST HOLDS ALL OF IT (2026-10-31). On 2026-10-30 a re-measure of four batches lost one to a final
|
|
206
|
+
* refusal and the command said "✓ Re-measured. WorkTrust holds all 3 batches": the receipts it had were read as the run. Per run
|
|
207
|
+
* a send touches: its declared count, the batches WorkTrust confirmed (a receipt now, or an acknowledgement an earlier send kept
|
|
208
|
+
* here), the most the door said had arrived, whether a receipt said the run is complete, and the batches refused for good.
|
|
209
|
+
* `state()` is what the command reads: whole only when every run is; null when the send touched none.
|
|
210
|
+
*/
|
|
211
|
+
export function runLedger() {
|
|
212
|
+
const runs=new Map();
|
|
213
|
+
const of=(run)=>{if(!run||typeof run.id!=='string'||!Number.isInteger(run.count)||!Number.isInteger(run.index))return null;const known=runs.get(run.id)??{count:run.count,confirmed:new Set(),arrived:0,complete:false,refused:new Set()};runs.set(run.id,known);return known;};
|
|
214
|
+
const kept=(run)=>Math.min(run.count,Math.max(run.arrived,run.confirmed.size));
|
|
215
|
+
return {
|
|
216
|
+
heard(run,receipt){const known=of(run);if(!known)return;known.confirmed.add(run.index);if(Number.isInteger(receipt?.arrived))known.arrived=Math.max(known.arrived,receipt.arrived);if(receipt?.complete===true||(Number.isInteger(receipt?.of)&&receipt.arrived===receipt.of))known.complete=true;},
|
|
217
|
+
held(run){of(run)?.confirmed.add(run.index);},
|
|
218
|
+
refused(run){of(run)?.refused.add(run.index);},
|
|
219
|
+
state(){
|
|
220
|
+
const all=[...runs.values()];if(!all.length)return null;
|
|
221
|
+
return {batches:all.reduce((n,run)=>n+run.count,0),arrived:all.reduce((n,run)=>n+kept(run),0),refused:all.reduce((n,run)=>n+run.refused.size,0),
|
|
222
|
+
whole:all.every(run=>run.refused.size===0&&(run.complete||kept(run)===run.count))};
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
167
226
|
/** A queued batch's id, as queuedHistory names it, so a send can hand its own batches to resumeHistory (`only`). */
|
|
168
227
|
export const historyIdOf=(target,body,source)=>digest(scopeOf(target)+'\0'+source+'\0'+body);
|
|
169
228
|
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE LONG STEPS, AS THEY GO, AND A STOP THAT LEAVES NOTHING HALF DONE (owner, incident 2026-11-05). `npx worktrust history
|
|
3
|
+
* --export` of 5,003 conversations read a 4 GB archive and sent about 45 batches for many minutes with nothing on the screen,
|
|
4
|
+
* and Ctrl+C had no handler. Reads no file and no network; counts only, never a title, a text or a file's name.
|
|
5
|
+
*
|
|
6
|
+
* THE EVENTS. The hook, asked by WORKTRUST_HISTORY_PROGRESS, hands each step's counts on (`progressSink`): "1" prints one JSON
|
|
7
|
+
* line each on stdout ({"history_progress":{phase,…}}) for the command to draw; "stderr" draws them here, on stderr (preserve
|
|
8
|
+
* reads the hook's stdout as the archive's lines). Written with writeSync, so a long synchronous read still shows as it goes.
|
|
9
|
+
*
|
|
10
|
+
* phase read { unit, part, parts, bytes?, size?, conversations? } reading an export's parts, or the sessions here
|
|
11
|
+
* phase plan { batches, lines } what the send will be (the read is closed, the stop hint said once)
|
|
12
|
+
* phase batch { lines, landed } a batch confirmed (or already held), or one that did not arrive
|
|
13
|
+
* phase resend { lines, stored?, unsent? } a batch the door landed in part, sent again at once
|
|
14
|
+
* phase wait { seconds } the door's upload limit per minute, waited out
|
|
15
|
+
*
|
|
16
|
+
* THE DISPLAY. On a terminal ONE line rewritten in place (\r, no spinner), redrawn each second: a bar, the counts, percent,
|
|
17
|
+
* elapsed and, sending, the time left from the rate so far, waits included (said once two batches landed). A resend and a wait
|
|
18
|
+
* are said on lines of their own. Not a terminal (piped, CI): a reading line per tenth, a line per batch, never a carriage
|
|
19
|
+
* return or an escape. `end()` closes with the totals; `stop()` with where it stopped and how to go on.
|
|
20
|
+
*/
|
|
21
|
+
import { writeSync } from "node:fs";
|
|
22
|
+
|
|
23
|
+
const thousands = (n) => Number(n).toLocaleString("en-US");
|
|
24
|
+
const count = (n) => (Number.isSafeInteger(n) && n >= 0 ? n : 0);
|
|
25
|
+
/** 12s, 4m10s, 1h04m. */
|
|
26
|
+
export const took = (ms) => {
|
|
27
|
+
const s = Math.max(0, Math.round(ms / 1000));
|
|
28
|
+
if (s < 60) return `${s}s`;
|
|
29
|
+
const m = Math.floor(s / 60);
|
|
30
|
+
return m < 60 ? `${m}m${String(s % 60).padStart(2, "0")}s` : `${Math.floor(m / 60)}h${String(m % 60).padStart(2, "0")}m`;
|
|
31
|
+
};
|
|
32
|
+
const UNITS = { parts: ["part", "parts"], sessions: ["session", "sessions"] };
|
|
33
|
+
const bar = (percent) => { const full = Math.min(16, Math.floor((16 * percent) / 100)); return "▓".repeat(full) + "░".repeat(16 - full); };
|
|
34
|
+
export const CONTINUE = "Run the same command again to continue: a line already received is kept once.";
|
|
35
|
+
|
|
36
|
+
export function historyProgress({ stream = process.stdout, now = Date.now } = {}) {
|
|
37
|
+
const tty = stream?.isTTY === true;
|
|
38
|
+
let plan = null, started = null, landed = 0, lines = 0, tried = 0, open = false, ticking = null, read = null, ended = false;
|
|
39
|
+
const of = () => Math.max(plan?.lines ?? 0, lines);
|
|
40
|
+
const percent = () => (of() ? Math.floor((100 * lines) / of()) : 0);
|
|
41
|
+
const elapsed = (from = started) => took(now() - (from ?? now()));
|
|
42
|
+
// The rate so far, over the whole time since the send began (a wait or a resend included), never a guess before two batches.
|
|
43
|
+
const left = () => {
|
|
44
|
+
if (landed < 2 || lines >= of()) return "";
|
|
45
|
+
const spent = now() - started;
|
|
46
|
+
if (!(spent > 0)) return "";
|
|
47
|
+
const seconds = ((of() - lines) * spent) / lines / 1000;
|
|
48
|
+
return seconds < 60 ? " · <1 min left" : ` · ~${Math.round(seconds / 60)} min left`;
|
|
49
|
+
};
|
|
50
|
+
const totals = () => `${landed}/${Math.max(plan?.batches ?? 0, landed)} batches · ${thousands(lines)}/${thousands(of())} lines · ${percent()}% · ${elapsed()}`;
|
|
51
|
+
// Reading: by bytes when the size is known (an export's parts), else by the count (the sessions here).
|
|
52
|
+
const readPercent = () => (read.size > 0 ? Math.min(100, Math.floor((100 * read.bytes) / read.size)) : read.parts > 0 ? Math.floor((100 * read.part) / read.parts) : 0);
|
|
53
|
+
const readCounts = () => `${thousands(read.part)}/${thousands(read.parts)} ${UNITS[read.unit]?.[read.parts === 1 ? 0 : 1] ?? "parts"}${read.conversations !== null ? ` · ${thousands(read.conversations)} conversations` : ""}`;
|
|
54
|
+
const line = () => (read && !plan ? ` Reading ${bar(readPercent())} ${readCounts()} · ${readPercent()}% · ${elapsed(read.started)}` : ` Sending ${bar(percent())} ${totals()}${left()}`);
|
|
55
|
+
const draw = () => { if (!tty || (!plan && !read)) return; stream.write(`\r\x1b[K${line()}`); open = true; };
|
|
56
|
+
const clear = () => { if (open) stream.write("\r\x1b[K"); open = false; };
|
|
57
|
+
const note = (text) => { if (tty) { clear(); stream.write(` ${text}\n`); draw(); } else stream.write(` ${text}\n`); };
|
|
58
|
+
const tick = () => { if (tty && !ticking) { ticking = setInterval(() => { if (open) draw(); }, 1000); ticking.unref?.(); } };
|
|
59
|
+
const closeRead = () => { if (!read || read.closed) return; read.closed = true; clear(); stream.write(` Read ${readCounts()} · ${elapsed(read.started)}\n`); };
|
|
60
|
+
const handle = (event) => {
|
|
61
|
+
if (event.phase === "read") {
|
|
62
|
+
if (plan) return;
|
|
63
|
+
const unit = event.unit === "sessions" ? "sessions" : "parts";
|
|
64
|
+
if (!read || read.unit !== unit) read = { unit, started: now(), step: -1, closed: false };
|
|
65
|
+
Object.assign(read, { part: count(event.part), parts: Math.max(count(event.parts), count(event.part)), bytes: count(event.bytes), size: count(event.size), conversations: Number.isSafeInteger(event.conversations) ? event.conversations : null });
|
|
66
|
+
if (tty) { draw(); tick(); return; }
|
|
67
|
+
const step = Math.floor(readPercent() / 10);
|
|
68
|
+
if (step > read.step) { read.step = step; stream.write(` reading ${readCounts()} · ${readPercent()}% · ${elapsed(read.started)}\n`); }
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (event.phase === "plan") {
|
|
72
|
+
closeRead();
|
|
73
|
+
plan = { batches: count(event.batches), lines: count(event.lines) };
|
|
74
|
+
started ??= now();
|
|
75
|
+
stream.write(tty ? "\x1b[2m Ctrl+C stops; run it again to continue.\x1b[22m\n" : " Ctrl+C stops; run it again to continue.\n");
|
|
76
|
+
draw(); tick();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
started ??= now();
|
|
80
|
+
if (event.phase === "batch") {
|
|
81
|
+
tried += 1;
|
|
82
|
+
if (event.landed === true) { landed += 1; lines += count(event.lines); }
|
|
83
|
+
if (tty) draw();
|
|
84
|
+
else stream.write(` batch ${tried}/${Math.max(plan?.batches ?? 0, tried)} · ${thousands(lines)}/${thousands(of())} lines · ${percent()}% · ${elapsed()}${left()}${event.landed === true ? "" : " · not confirmed"}\n`);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (event.phase === "resend") {
|
|
88
|
+
const n = count(event.lines), unsent = Number.isSafeInteger(event.unsent) ? event.unsent : null, stored = Number.isSafeInteger(event.stored) ? event.stored : null;
|
|
89
|
+
const what = unsent !== null && n >= unsent ? `${n - unsent} of ${n} stored` : stored !== null ? `${stored} new lines stored` : "not confirmed";
|
|
90
|
+
note(`batch ${tried + 1}: ${what}, ${unsent !== null || stored !== null ? "resending the rest" : "sending it once more"}`);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (event.phase === "wait") note(`waiting ${count(event.seconds)} s: upload limit per minute`);
|
|
94
|
+
};
|
|
95
|
+
const quiet = () => { if (ticking) { clearInterval(ticking); ticking = null; } };
|
|
96
|
+
return {
|
|
97
|
+
event(event) { if (event && typeof event === "object") handle(event); },
|
|
98
|
+
line(text) {
|
|
99
|
+
if (typeof text !== "string" || !text.startsWith('{"history_progress":')) return false;
|
|
100
|
+
try { const event = JSON.parse(text).history_progress; if (event && typeof event === "object") handle(event); } catch { /* a torn event is dropped, never printed */ }
|
|
101
|
+
return true;
|
|
102
|
+
},
|
|
103
|
+
end() {
|
|
104
|
+
quiet();
|
|
105
|
+
if (ended) return; ended = true;
|
|
106
|
+
if (!plan) { closeRead(); return; }
|
|
107
|
+
clear();
|
|
108
|
+
stream.write(` ${lines >= of() && of() > 0 ? "Sent" : "Stopped"} ${totals()}\n`);
|
|
109
|
+
},
|
|
110
|
+
/** Ctrl+C: the line restored, where it stopped, and how to go on. */
|
|
111
|
+
stop({ sending = CONTINUE, reading = "Nothing was sent; run the same command again to start again." } = {}) {
|
|
112
|
+
quiet(); clear(); ended = true;
|
|
113
|
+
if (plan) stream.write(` Stopped at ${landed}/${Math.max(plan.batches, landed)} batches · ${thousands(lines)}/${thousands(of())} lines. ${sending}\n`);
|
|
114
|
+
else stream.write(` Stopped${read ? ` while reading, at ${readCounts()}` : ""}. ${reading}\n`);
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The hook's side of the events (see above); null when nobody asked. */
|
|
120
|
+
export function progressSink(mode = process.env.WORKTRUST_HISTORY_PROGRESS) {
|
|
121
|
+
const out = (fd) => (text) => { try { writeSync(fd, text); } catch { /* a full or closed pipe drops an event, never the run */ } return true; };
|
|
122
|
+
if (mode === "1") { const write = out(1); return { event: (event) => write(`${JSON.stringify({ history_progress: event })}\n`), end() {} }; }
|
|
123
|
+
if (mode !== "stderr") return null;
|
|
124
|
+
const shown = historyProgress({ stream: { isTTY: process.stderr.isTTY === true, write: out(2) } });
|
|
125
|
+
process.once("exit", () => shown.end()); // the totals, however the hook ends (writeSync: an exit handler may write)
|
|
126
|
+
return { event: (event) => shown.event(event), end: () => shown.end() };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* STOPPING SAFELY (owner, 2026-11-05). The first SIGINT or SIGTERM asks a send to stop: the call in flight is abandoned through
|
|
131
|
+
* `signal` (a line it carried that WorkTrust already received is kept once, so the next run sends it again safely), no batch
|
|
132
|
+
* starts after it, the outbox keeps every batch not confirmed (its files are written to a temporary name and renamed, and a
|
|
133
|
+
* signal is handled between, never inside, those synchronous writes), and the hook exits 130. A second one exits at once.
|
|
134
|
+
*/
|
|
135
|
+
export function stopSignals() {
|
|
136
|
+
const controller = new AbortController();
|
|
137
|
+
let asked = 0;
|
|
138
|
+
const stop = () => { asked += 1; if (asked > 1) process.exit(130); controller.abort(); };
|
|
139
|
+
process.on("SIGINT", stop); process.on("SIGTERM", stop);
|
|
140
|
+
return { get asked() { return asked > 0; }, signal: controller.signal };
|
|
141
|
+
}
|
package/log-session.mjs
CHANGED
|
@@ -802,7 +802,7 @@ function callOutcomesOf(messages) {
|
|
|
802
802
|
/** A digest of this hook and the readers beside it, as installed: the door records it beside each line as a signal of which code ran, never as proof. */
|
|
803
803
|
const selfDigest = (() => { try { const here = dirname(fileURLToPath(import.meta.url)); const hash = createHash("sha256"); for (const name of ["log-session.mjs", ...READER_FILES].sort()) { try { hash.update(`${name}\0`).update(readFileSync(join(here, name))).update("\0"); } catch { hash.update(`${name}\0missing\0`); } } return hash.digest("hex"); } catch { return null; } })();
|
|
804
804
|
const CLI_VERSION_SENT = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,4}$/.test(process.env.WORKTRUST_CLI_VERSION ?? "") ? process.env.WORKTRUST_CLI_VERSION : null; // `worktrust.mjs` hands its own version to the hook it starts
|
|
805
|
-
|
|
805
|
+
const DOOR_MODE_SENT = CLI_VERSION_SENT && /^(hours_only|work_lines)(\.[0-9]{9,11})?$/.test(process.env.WORKTRUST_DOOR_MODE ?? "") ? process.env.WORKTRUST_DOOR_MODE : null; // 0.10.37: `mcp off|on`, a closed word and its moment beside the version
|
|
806
806
|
/**
|
|
807
807
|
* THE TOKENS, AND THE CACHE HALVES APART (roadmap 16). Claude Code's usage block carries input_tokens, output_tokens,
|
|
808
808
|
* cache_creation_input_tokens and cache_read_input_tokens. `tokensIn` stays what it was — new input plus what was written
|
|
@@ -919,13 +919,11 @@ function payloadFor(stretch) {
|
|
|
919
919
|
* adding a twin. Returns how many entries were accepted, so a caller can advance a watermark over
|
|
920
920
|
* exactly what landed. `announce` because one caller is a person watching a terminal and the other
|
|
921
921
|
* is a hook holding a session's exit open.
|
|
922
|
-
|
|
923
|
-
/**
|
|
922
|
+
*
|
|
924
923
|
* THE BATCHES: at most two hundred lines each. `byDay` (an export, 2026-10-10) never splits a UTC day across two batches unless
|
|
925
924
|
* one day alone exceeds the cap: the door's receipt counts per day what it replaced and how many AI-app lines lie there, and a
|
|
926
925
|
* day in two batches would be counted twice.
|
|
927
|
-
|
|
928
|
-
/**
|
|
926
|
+
*
|
|
929
927
|
* A batch stays under the door's ceilings for one history call (2026-10-23, docs/INTAKE-HARDENING.md): 2 MB a call and 1.5 MB of
|
|
930
928
|
* timelines a call, each with room for the envelope. A timeline (0.10.20) is up to 200 KB a line, so a batch is cut by bytes
|
|
931
929
|
* and by timeline bytes as well as by count. Until 0.10.28 the cut was 3.5 MB, under the door's 4.5 MB outer bound only.
|
|
@@ -956,12 +954,12 @@ function batchesOf(group, byDay, limit = BATCH_LIMIT) {
|
|
|
956
954
|
return out;
|
|
957
955
|
}
|
|
958
956
|
/** THE RECEIPTS' COUNTS (2026-10-10): what an export send replaced, how many AI-app lines lie on its days, how many were withdrawn; summed over the batches, read for the counts alone. */
|
|
959
|
-
const RECEIPTS = { new: 0, alreadyHeld: 0, filled: 0, replaced: 0, superseded: 0, recut: 0, unknownOnDays: 0, unknownWithdrawn: 0, corrected: 0, measuredSeconds: 0, staged: 0, kept: 0, restored: 0, untimed: 0 }; // 0.10.27: staged, the batches WorkTrust keeps on its server, and kept, their lines // 0.10.20: measuredSeconds is WorkTrust's own figure for what the batch stored, the one the terminal shows // 0.10.17: recut, the one-envelope export lines its runs replaced // 2026-10-10: and what a re-measure did (new, held, filled, replaced), so the command can say it
|
|
960
|
-
const noteReceipt = (answer) => {
|
|
957
|
+
const RECEIPTS = { new: 0, alreadyHeld: 0, filled: 0, replaced: 0, superseded: 0, recut: 0, unknownOnDays: 0, unknownWithdrawn: 0, corrected: 0, measuredSeconds: 0, reconstructedSeconds: 0, staged: 0, kept: 0, restored: 0, untimed: 0 }; // 0.10.27: staged, the batches WorkTrust keeps on its server, and kept, their lines // 0.10.20: measuredSeconds is WorkTrust's own figure for what the batch stored, the one the terminal shows // 0.10.17: recut, the one-envelope export lines its runs replaced // 2026-10-10: and what a re-measure did (new, held, filled, replaced), so the command can say it
|
|
958
|
+
const noteReceipt = (answer, run = null) => {
|
|
961
959
|
try {
|
|
962
960
|
const json = JSON.parse(answer.trim().startsWith("{") ? answer : answer.split("\n").find((row) => row.startsWith("data:"))?.slice(5) ?? "null");
|
|
963
961
|
const receipt = JSON.parse(json?.result?.content?.[0]?.text ?? "{}");
|
|
964
|
-
for (const key of Object.keys(RECEIPTS)) RECEIPTS[key] += Number(receipt[key]) || 0;
|
|
962
|
+
for (const key of Object.keys(RECEIPTS)) RECEIPTS[key] += Number(receipt[key]) || 0; if (receipt.staged === true) RUNS.heard(run, receipt);
|
|
965
963
|
// THE QUEUE (0.10.32): the receipt of the batch that completes a run says where the run waits; the last one seen is kept.
|
|
966
964
|
const queued = receipt.queued;
|
|
967
965
|
if (queued && Number.isInteger(queued.position) && queued.position >= 1 && Number.isFinite(Number(queued.eta_minutes)) && Number(queued.eta_minutes) >= 0) QUEUED.at = { position: queued.position, eta_minutes: Math.max(1, Math.ceil(Number(queued.eta_minutes))) };
|
|
@@ -969,6 +967,7 @@ const noteReceipt = (answer) => {
|
|
|
969
967
|
};
|
|
970
968
|
/** Where the last completed run waits in WorkTrust's queue (0.10.32): null when no receipt said so (an older server, or not complete yet). */
|
|
971
969
|
const QUEUED = { at: null };
|
|
970
|
+
const RUNS = (await import("./history-outbox.mjs").then((m) => m.runLedger?.(), () => null)) ?? { heard() {}, held() {}, refused() {}, state: () => null }; // A RUN IS DONE WHEN WORKTRUST HOLDS ALL OF IT (2026-10-31): each run's declared batches against the receipts; a copy without the outbox (preserve) sends no run
|
|
972
971
|
/** Only explicit durable receipts authorize a checkpoint. HTTP 200 alone says nothing. */
|
|
973
972
|
function confirmedReceipt(answer, expectedCount = null) {
|
|
974
973
|
try {
|
|
@@ -1143,8 +1142,9 @@ const outboxCut = {
|
|
|
1143
1142
|
};
|
|
1144
1143
|
async function sendHistory(door, entries, announce = false, rebuild = false, named = false, { byDay = false, withdrawUnknown = false } = {}) {
|
|
1145
1144
|
let accepted = 0;
|
|
1146
|
-
const { queuedHistory,
|
|
1145
|
+
const { queuedHistory, historyIdOf, resumeHistory, deliverHistory } = await import("./history-outbox.mjs");
|
|
1147
1146
|
const plan = historyPlan(door, entries, rebuild, { byDay, withdrawUnknown });
|
|
1147
|
+
const shown = announce ? historyProgressOut : null, STOP = announce && PROGRESS.stopSignals ? (STOPPING ??= PROGRESS.stopSignals()) : null; shown?.({ phase: "plan", batches: plan.length, lines: plan.reduce((n, item) => n + item.batch.length, 0) }); // THE SEND, AS IT GOES (2026-11-05)
|
|
1148
1148
|
// Persist the approved plan before sending, so later batches survive removed source files too.
|
|
1149
1149
|
try { for (const item of plan) queuedHistory(item.target, item.body, item.source); }
|
|
1150
1150
|
catch { console.error("History queue is full, busy or unavailable. Resume the saved batches before retrying this import; nothing was marked complete."); HISTORY_DELIVERY.waiting += entries.length; return 0; }
|
|
@@ -1158,7 +1158,7 @@ async function sendHistory(door, entries, announce = false, rebuild = false, nam
|
|
|
1158
1158
|
const handLater = (item) => { const row = later.get(item.target.token) ?? { target: item.target, ids: [] }; row.ids.push(historyIdOf(item.target, item.body, item.source)); later.set(item.target.token, row); };
|
|
1159
1159
|
for (const item of plan) {
|
|
1160
1160
|
const { source, target, batch, body } = item;
|
|
1161
|
-
if (stopped.has(source)) continue;
|
|
1161
|
+
if (stopped.has(source) || STOP?.asked) { HISTORY_DELIVERY.waiting += batch.length; continue; } // 2026-10-31: held back behind its AI app's failed batch, waiting in the outbox, and said
|
|
1162
1162
|
if (item.run && stoppedRuns.has(item.run.id)) { handLater(item); continue; }
|
|
1163
1163
|
const number = (numbers.get(source) ?? 0) + 1; numbers.set(source, number);
|
|
1164
1164
|
for (const entry of batch) retainMeasurement(entry, source);
|
|
@@ -1166,32 +1166,31 @@ async function sendHistory(door, entries, announce = false, rebuild = false, nam
|
|
|
1166
1166
|
let queued;
|
|
1167
1167
|
try { queued = queuedHistory(target, body, source); }
|
|
1168
1168
|
catch { console.error("Pending history could not be stored safely. Nothing was marked complete; keep the local WorkTrust folder for recovery."); HISTORY_DELIVERY.waiting += batch.length; stopped.add(source); continue; }
|
|
1169
|
-
if (queued.status === "
|
|
1169
|
+
if (queued.status === "closed") { HISTORY_DELIVERY.refused += batch.length; RUNS.refused(item.run); continue; } // 2026-10-31: let go on an earlier send is refused, never "already held"
|
|
1170
|
+
if (queued.status === "acknowledged" && !byDay) { accepted += batch.length; RECEIPTS.alreadyHeld += batch.length; RUNS.held(item.run); shown?.({ phase: "batch", lines: batch.length, landed: true }); continue; }
|
|
1170
1171
|
if (queued.status === "replaced" || queued.status === "oversize") { handLater(item); if (item.run) stoppedRuns.add(item.run.id); continue; }
|
|
1171
1172
|
if (queued.status === "review" && !flag("retry-review")) { HISTORY_DELIVERY.review += batch.length; continue; }
|
|
1172
1173
|
if (queued.status === "waiting") { HISTORY_DELIVERY.waiting += batch.length; continue; }
|
|
1173
1174
|
try {
|
|
1174
|
-
const
|
|
1175
|
-
const text =
|
|
1176
|
-
|
|
1177
|
-
catch {
|
|
1178
|
-
const refusal = historyRefusal(text, response);
|
|
1175
|
+
const delivered = await deliverHistory(() => fetch(target.url, { method: "POST", headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${target.token}`, ...clientOf(source, rebuild || named), ...proofFor(target.url, body) }, body, redirect: "error", signal: callSignal(65_000) }), (text) => confirmedReceipt(text, batch.length), { patient: announce, lines: batch.length, progress: shown, halt: () => STOP?.asked, sleep: pause });
|
|
1176
|
+
const { text, refusal } = delivered; // A BATCH LANDED IN PART GOES AGAIN AT ONCE (2026-11-05): the person's send (`announce`) resends it while each resend lands more and waits out a minute's budget; a hook's send answers as before
|
|
1177
|
+
if (!delivered.ok) { if (refusal.reason !== "oversize") shown?.({ phase: "batch", lines: batch.length, landed: false }); if (STOP?.asked) { HISTORY_DELIVERY.waiting += batch.length; stopped.add(source); continue; } // stopped: it stays pending, never deferred
|
|
1179
1178
|
// A run WorkTrust closed (seven days without its last batch, another computer's key) is final: kept here it would wait forever.
|
|
1180
|
-
if (refusal.reason === "closed") { queued.close(); HISTORY_DELIVERY.refused += batch.length; if (refusal.said) HISTORY_DELIVERY.said = refusal.said; continue; }
|
|
1179
|
+
if (refusal.reason === "closed") { queued.close(); HISTORY_DELIVERY.refused += batch.length; if (refusal.said) HISTORY_DELIVERY.said = refusal.said; RUNS.refused(item.run); continue; }
|
|
1181
1180
|
if (refusal.reason === "oversize") { queued.oversize(); handLater(item); if (item.run) stoppedRuns.add(item.run.id); continue; }
|
|
1182
|
-
queued.defer(refusal.reason, refusal.seconds);
|
|
1181
|
+
queued.defer(refusal.reason, refusal.seconds); if (refusal.said) HISTORY_DELIVERY.reason = refusal.said; // why it waits, in WorkTrust's words (0.10.37, incident 2026-11-05)
|
|
1183
1182
|
if (refusal.reason === "review") { HISTORY_DELIVERY.review += batch.length; continue; }
|
|
1184
1183
|
HISTORY_DELIVERY.waiting += batch.length; stopped.add(source); continue;
|
|
1185
1184
|
}
|
|
1186
|
-
queued.confirm(batch.length); accepted += batch.length; noteReceipt(text);
|
|
1185
|
+
queued.confirm(batch.length); accepted += batch.length; noteReceipt(text, item.run); shown?.({ phase: "batch", lines: batch.length, landed: true });
|
|
1187
1186
|
if (announce) console.log(`batch ${number}: received ${batch.length}`);
|
|
1188
|
-
} catch { queued.defer("unconfirmed"); HISTORY_DELIVERY.waiting += batch.length; stopped.add(source); }
|
|
1187
|
+
} catch { queued.defer("unconfirmed"); HISTORY_DELIVERY.waiting += batch.length; stopped.add(source); shown?.({ phase: "batch", lines: batch.length, landed: false }); }
|
|
1189
1188
|
}
|
|
1190
|
-
for (const { target, ids } of later.values()) {
|
|
1189
|
+
for (const { target, ids } of later.values()) { // `pause`: a minute's wait that a stop cuts short (below)
|
|
1191
1190
|
try {
|
|
1192
|
-
const totals = await resumeHistory(target, { only: ids, cutter: outboxCut, retryReview: flag("retry-review"), confirmed: confirmedReceipt, note: noteReceipt, send: sendQueued(target) });
|
|
1191
|
+
const totals = await resumeHistory(target, { only: ids, cutter: outboxCut, retryReview: flag("retry-review"), confirmed: confirmedReceipt, note: noteReceipt, closed: RUNS.refused, send: sendQueued(target), patient: announce, progress: shown, halt: () => STOP?.asked, sleep: pause });
|
|
1193
1192
|
accepted += totals.received + totals.held; RECEIPTS.alreadyHeld += totals.held;
|
|
1194
|
-
HISTORY_DELIVERY.waiting += totals.waiting; HISTORY_DELIVERY.review += totals.review; HISTORY_DELIVERY.refused += totals.refused; if (totals.said) HISTORY_DELIVERY.said = totals.said;
|
|
1193
|
+
HISTORY_DELIVERY.waiting += totals.waiting; HISTORY_DELIVERY.review += totals.review; HISTORY_DELIVERY.refused += totals.refused; if (totals.said) HISTORY_DELIVERY.said = totals.said; if (totals.reason) HISTORY_DELIVERY.reason = totals.reason;
|
|
1195
1194
|
if (announce && totals.split) console.log(`${totals.split} batch${totals.split === 1 ? "" : "es"} too large for one call: cut smaller and sent again in pieces`);
|
|
1196
1195
|
if (announce && totals.refused) console.log(`${totals.refused} line${totals.refused === 1 ? "" : "s"} could not be sent in a call WorkTrust takes; the earlier lines keep counting. Run npx worktrust@latest history again to measure them anew.`);
|
|
1197
1196
|
} catch { console.error("Pending history could not be read safely. Nothing was marked complete; keep the local WorkTrust folder for recovery."); }
|
|
@@ -1199,8 +1198,9 @@ async function sendHistory(door, entries, announce = false, rebuild = false, nam
|
|
|
1199
1198
|
return accepted;
|
|
1200
1199
|
}
|
|
1201
1200
|
|
|
1201
|
+
const PROGRESS = await import("./history-progress.mjs").then((m) => ({ sink: m.progressSink(), stopSignals: m.stopSignals }), () => ({ sink: null, stopSignals: null })); let STOPPING = null; const historyProgressOut = PROGRESS.sink?.event ?? null, pause = (ms) => new Promise((done) => { const at = setTimeout(done, ms); STOPPING?.signal.addEventListener("abort", () => { clearTimeout(at); done(); }, { once: true }); }), callSignal = (ms) => (STOPPING && AbortSignal.any ? AbortSignal.any([AbortSignal.timeout(ms), STOPPING.signal]) : AbortSignal.timeout(ms)), readTick = (index, total) => { if (historyProgressOut && (!(Date.now() < readTick.at) || index + 1 === total)) { readTick.at = Date.now() + 250; historyProgressOut({ phase: "read", unit: "sessions", part: index + 1, parts: total }); } }; // THE LONG STEPS, AS THEY GO, AND A SAFE STOP (2026-11-05): history-progress.mjs; `pause` is a minute's wait a stop cuts short
|
|
1202
1202
|
/** The outbox's batches for one coupling, sent as `--resume` and the hook send them: signed, named, bounded in time. */
|
|
1203
|
-
const sendQueued = (target, ms = 65_000) => (body, source) => fetch(target.url, { method: "POST", headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${target.token}`, ...clientOf(source, true), ...proofFor(target.url, body) }, body, redirect: "error", signal:
|
|
1203
|
+
const sendQueued = (target, ms = 65_000) => (body, source) => fetch(target.url, { method: "POST", headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${target.token}`, ...clientOf(source, true), ...proofFor(target.url, body) }, body, redirect: "error", signal: callSignal(ms) });
|
|
1204
1204
|
/**
|
|
1205
1205
|
* PENDING HISTORY GOES BY ITSELF (0.10.27; owner, 2026-10-21). A batch the server did not receive (no network, a closed lid,
|
|
1206
1206
|
* the upload limit) stays in ~/.worktrust/history-outbox and is sent again by the next hook run, a few at a time, without a
|
|
@@ -1215,7 +1215,7 @@ async function resendPending(limit) {
|
|
|
1215
1215
|
} catch (error) { if (process.env.WORKTRUST_HOOK_DEBUG) console.error(`worktrust: ${error.message}`); }
|
|
1216
1216
|
}
|
|
1217
1217
|
|
|
1218
|
-
/** Whether the door's import_history takes `from_archive` (0.8.0)
|
|
1218
|
+
/** Whether the door's import_history takes `from_archive` (0.8.0), asked with tools/list; a door that no longer LISTS import_history to a computer's key (2026-10-31) is one that takes it. */
|
|
1219
1219
|
async function doorKnowsArchive(target) {
|
|
1220
1220
|
if (!target) return false;
|
|
1221
1221
|
try {
|
|
@@ -1224,7 +1224,7 @@ async function doorKnowsArchive(target) {
|
|
|
1224
1224
|
const text = await response.text();
|
|
1225
1225
|
const json = JSON.parse(text.trim().startsWith("{") ? text : text.split("\n").find((row) => row.startsWith("data:"))?.slice(5) ?? "null");
|
|
1226
1226
|
const tool = json?.result?.tools?.find((one) => one.name === "import_history");
|
|
1227
|
-
return Boolean(tool
|
|
1227
|
+
return tool ? Boolean(tool.inputSchema?.properties?.entries?.items?.properties?.from_archive) : Array.isArray(json?.result?.tools) && json.result.tools.some((one) => one.name === "log_work");
|
|
1228
1228
|
} catch { return false; }
|
|
1229
1229
|
}
|
|
1230
1230
|
|
|
@@ -1242,7 +1242,7 @@ const proofFor = (url, body) => {
|
|
|
1242
1242
|
* on this computer, and the door names a coupling's clients by the user-agent it hears (mcp_identify's
|
|
1243
1243
|
* agents_seen). An Antigravity line names Antigravity: the product name, nothing of the work.
|
|
1244
1244
|
*/
|
|
1245
|
-
const clientOf = (source, always = false) => (CLI_VERSION_SENT || always || source === "antigravity" || DATABASE_CLIENTS.includes(source) || EXPORT_CLIENTS.includes(source) ? { "user-agent": `${source ?? "claude"}/hook (worktrust-hook)${CLI_VERSION_SENT ? ` worktrust-cli/${CLI_VERSION_SENT}` : ""}` } : {});
|
|
1245
|
+
const clientOf = (source, always = false) => (CLI_VERSION_SENT || always || source === "antigravity" || DATABASE_CLIENTS.includes(source) || EXPORT_CLIENTS.includes(source) ? { "user-agent": `${source ?? "claude"}/hook (worktrust-hook)${CLI_VERSION_SENT ? ` worktrust-cli/${CLI_VERSION_SENT}` : ""}${DOOR_MODE_SENT ? ` worktrust-mode/${DOOR_MODE_SENT}` : ""}` } : {});
|
|
1246
1246
|
|
|
1247
1247
|
async function send(door, payload) {
|
|
1248
1248
|
retainMeasurement(payload, entrySource.get(payload));
|
|
@@ -1310,7 +1310,7 @@ async function sendProcessDays() {
|
|
|
1310
1310
|
*/
|
|
1311
1311
|
function historyEntries(floor = null, all = false) {
|
|
1312
1312
|
const entries = [], processGroups = new Map();
|
|
1313
|
-
for (const { file } of
|
|
1313
|
+
const transcripts = [...allTranscripts()]; for (const [index, { file }] of transcripts.entries()) { readTick(index, transcripts.length);
|
|
1314
1314
|
const lines = parsedLines(file);
|
|
1315
1315
|
if (!lines) continue;
|
|
1316
1316
|
let cwd = null, steered = null;
|
|
@@ -1390,7 +1390,7 @@ async function exportEntries() {
|
|
|
1390
1390
|
if (!exportSessions || !exportRunRef) throw Error("chat-exports.mjs is not beside this file, or is older than this hook: a data export cannot be read");
|
|
1391
1391
|
const entries = [], reports = [];
|
|
1392
1392
|
for (const given of EXPORT_PATHS) {
|
|
1393
|
-
const read = {}, report = { days: new Set() }; // 0.10.18: the reader streams the export and fills `read` as it goes (path and vendor first; digest, count and the conversations left out as too large at the end)
|
|
1393
|
+
const read = { onProgress: (counts) => historyProgressOut?.({ phase: "read", unit: "parts", ...counts }) }, report = { days: new Set() }; // 0.10.18: the reader streams the export and fills `read` as it goes (path and vendor first; digest, count and the conversations left out as too large at the end)
|
|
1394
1394
|
for await (const { id, lines } of exportSessions(given, { excluded, report: read })) {
|
|
1395
1395
|
const messages = [];
|
|
1396
1396
|
for (const line of lines) { const at = line.timestamp ? Date.parse(line.timestamp) : NaN; if (Number.isFinite(at)) messages.push(messageOf(line, at)); }
|
|
@@ -1421,7 +1421,10 @@ async function exportEntries() {
|
|
|
1421
1421
|
}
|
|
1422
1422
|
}
|
|
1423
1423
|
const days = [...report.days].sort();
|
|
1424
|
-
|
|
1424
|
+
// A DOWNLOAD CUT OFF (2026-10-31): the reader read the conversations parts the archive held whole before the cut, and says
|
|
1425
|
+
// where it stopped; the person is told every time, so a partial export is never shown as the whole (counts and part names only).
|
|
1426
|
+
for (const cut of read.cut ?? []) process.stderr.write(`export: the archive is cut off (${cut.where}) after ${cut.last}: ${cut.parts} conversations part${cut.parts === 1 ? "" : "s"} read whole, ${read.conversations} conversations; nothing past the cut is read, so this is not the whole export\n`);
|
|
1427
|
+
reports.push({ path: read.path, vendor: read.vendor, digest: read.digest, conversations: read.conversations, days: days.length, first: days[0] ?? null, last: days.at(-1) ?? null, oversized: read.oversized, ...(read.empty ? { empty: read.empty } : {}), ...(Number.isInteger(read.parts) ? { parts: read.parts } : {}), ...(read.cut ? { cut: read.cut } : {}) }); // no hours (0.10.20): WorkTrust computes them on arrival and the receipt says
|
|
1425
1428
|
}
|
|
1426
1429
|
const seen = new Set();
|
|
1427
1430
|
return { entries: entries.filter((entry) => !seen.has(entry.stretch_ref) && seen.add(entry.stretch_ref)).sort((a, b) => a.at.localeCompare(b.at)), reports };
|
|
@@ -1456,7 +1459,7 @@ function folderEntries(given) {
|
|
|
1456
1459
|
if (files.length === 0) throw Error(`no session files under ${given} (Claude Code transcripts, Codex rollouts or Antigravity transcripts, as .jsonl)`);
|
|
1457
1460
|
const present = new Set(historyEntries(null, true).map((entry) => entry.stretch_ref)), seen = new Set();
|
|
1458
1461
|
const entries = [], report = { stretches: 0, skipped: 0, days: new Set() };
|
|
1459
|
-
for (const file of files) {
|
|
1462
|
+
for (const [index, file] of files.entries()) { readTick(index, files.length);
|
|
1460
1463
|
const client = CODEX_ROLLOUT.test(file) ? "codex" : ANTIGRAVITY_TRANSCRIPT.test(file) ? "antigravity" : "claude";
|
|
1461
1464
|
const lines = parsedLines(file);
|
|
1462
1465
|
if (!lines) continue;
|
|
@@ -1656,7 +1659,7 @@ if (flag("evidence-summary")) {
|
|
|
1656
1659
|
process.stdout.write(`${JSON.stringify(summary)}\n`);
|
|
1657
1660
|
return await exitFlushed(0);
|
|
1658
1661
|
}
|
|
1659
|
-
if (flag("archive-lines")) { const lines = historyEntries(null, true).map((entry) => JSON.stringify({ ...entry, ...(DERIVED.get(entry) ?? {}), client: entrySource.get(entry) ?? "claude", started_at: new Date(entryFile.get(entry).from).toISOString(), folder: entryFile.get(entry).folder })); process.stdout.write(`${[...lines, JSON.stringify({ archive_end: lines.length })].join("\n")}\n`); return await exitFlushed(0); } // one write and a count, so the reader tells a whole answer from a cut one
|
|
1662
|
+
if (flag("archive-lines")) { const lines = historyEntries(null, true).map((entry) => JSON.stringify({ ...entry, ...(DERIVED.get(entry) ?? {}), client: entrySource.get(entry) ?? "claude", started_at: new Date(entryFile.get(entry).from).toISOString(), folder: entryFile.get(entry).folder })); PROGRESS.sink?.end(); process.stdout.write(`${[...lines, JSON.stringify({ archive_end: lines.length })].join("\n")}\n`); return await exitFlushed(0); } // one write and a count, so the reader tells a whole answer from a cut one
|
|
1660
1663
|
// THE EXPORTS IN A FOLDER (0.10.10, for the bare `npx worktrust`): which lie there, by vendor and digest, and which this computer
|
|
1661
1664
|
// already sent or was asked about; reads, prints one JSON line, sends nothing. `--export-offered <digest>` records a No.
|
|
1662
1665
|
if (flag("find-exports")) { const named = value("find-exports"), state = readExportsState(); const found = (findExports ? await findExports(named && !named.startsWith("--") ? named : undefined) : []).map((one) => ({ ...one, sent: state[one.digest]?.sent === true, offered: Boolean(state[one.digest]) })); process.stdout.write(`${JSON.stringify(found)}\n`); return await exitFlushed(0); }
|
|
@@ -1674,7 +1677,7 @@ if (flag("history") && flag("resume")) {
|
|
|
1674
1677
|
const totals = { received: 0, review: 0, waiting: 0, batches: 0, refused: 0, split: 0 };
|
|
1675
1678
|
try {
|
|
1676
1679
|
for (const target of [door, ...(codexDoor && codexDoor.token !== door?.token ? [codexDoor] : [])].filter(Boolean)) {
|
|
1677
|
-
const result = await resumeHistory(target, { dry: DRY, retryReview: flag("retry-review"), cutter: outboxCut, confirmed: confirmedReceipt, note: noteReceipt, send: sendQueued(target) });
|
|
1680
|
+
const result = await resumeHistory(target, { dry: DRY, retryReview: flag("retry-review"), cutter: outboxCut, confirmed: confirmedReceipt, note: noteReceipt, send: sendQueued(target), patient: true }); // the person's own resume resends a batch landed in part (2026-11-05)
|
|
1678
1681
|
for (const key of Object.keys(totals)) totals[key] += result[key];
|
|
1679
1682
|
}
|
|
1680
1683
|
console.log(JSON.stringify({ resumed: totals.received, ...totals, ...RECEIPTS }));
|
|
@@ -1707,7 +1710,7 @@ if (flag("history") && flag("from-archive")) {
|
|
|
1707
1710
|
say(`${all.length} stretches in the archive · ${signedCount} signed by this computer's key (signature checked) · ${all.length - signedCount} unsigned or signed by another key (reconstructed) · ${entries.length} of them no longer in any AI app here`);
|
|
1708
1711
|
if (DRY || entries.length === 0) { if (entries[0]) say(`\nthe oldest of them, in full:\n${JSON.stringify(entries[0], null, 1)}`); say(entries.length === 0 ? "Nothing to send." : "\ndry run — nothing sent."); return await exitFlushed(0); }
|
|
1709
1712
|
if (!(await doorKnowsArchive(door))) { say("WorkTrust does not accept history from an archive yet (the door does not know the mark). Nothing was sent."); return await exitFlushed(1); }
|
|
1710
|
-
return await exitFlushed((await sendHistory(door, entries, true)) === entries.length ? 0 : 1);
|
|
1713
|
+
return await exitFlushed((await sendHistory(door, entries, true)) === entries.length ? 0 : STOPPING?.asked ? 130 : 1);
|
|
1711
1714
|
}
|
|
1712
1715
|
|
|
1713
1716
|
if (flag("history")) {
|
|
@@ -1759,8 +1762,8 @@ if (flag("history")) {
|
|
|
1759
1762
|
if (fromExports && accepted === entries.length) for (const report of fromExports.reports) rememberExport(report.digest, { vendor: report.vendor, sent: true, at: new Date().toISOString(), conversations: report.conversations });
|
|
1760
1763
|
// ONE LINE FOR THE COMMAND TO ASK WITH (2026-10-10): what the door replaced, how many AI-app lines lie on the export's days, how many it withdrew.
|
|
1761
1764
|
if (!fromExports && !fromFolder) await settleReadFailures(door ?? codexDoor); // 0.10.32: a transcript that could not be read is counted and told
|
|
1762
|
-
if (fromExports) console.log(JSON.stringify({ export_sent: accepted, of: entries.length, ...RECEIPTS })); else console.log(JSON.stringify({ history_sent: accepted, of: sending.length, unreadable: READ_FAILURES.n, ...RECEIPTS, delivery: HISTORY_DELIVERY, ...(staged ? { days: { compared: COMPARED.asked ? COMPARED.days : null, changed: COMPARED.asked ? COMPARED.changed : null }, queued: QUEUED.at } : {}) })); // the same line for a history send: what the door did with it, and WorkTrust's measuredSeconds for what it stored
|
|
1763
|
-
return await exitFlushed(accepted === sending.length ? 0 : 1);
|
|
1765
|
+
if (fromExports) console.log(JSON.stringify({ export_sent: accepted, of: entries.length, ...RECEIPTS, delivery: HISTORY_DELIVERY })); else console.log(JSON.stringify({ history_sent: accepted, of: sending.length, unreadable: READ_FAILURES.n, ...RECEIPTS, delivery: HISTORY_DELIVERY, ...(staged ? { days: { compared: COMPARED.asked ? COMPARED.days : null, changed: COMPARED.asked ? COMPARED.changed : null }, queued: QUEUED.at, run: RUNS.state() } : {}) })); // the same line for a history send: what the door did with it, and WorkTrust's measuredSeconds for what it stored
|
|
1766
|
+
return await exitFlushed(STOPPING?.asked ? 130 : accepted === sending.length && RUNS.state()?.whole !== false ? 0 : 1); // 2026-10-31: a run WorkTrust does not hold whole fails too
|
|
1764
1767
|
}
|
|
1765
1768
|
|
|
1766
1769
|
const input = hookInput();
|
package/package-release.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"keyId":"608168ccdef72562fc91f9c0f9f2c2d6876bece7f7362cb545b818f7947da6b5","payload":{"files":{"AGENTS.md":"
|
|
1
|
+
{"keyId":"608168ccdef72562fc91f9c0f9f2c2d6876bece7f7362cb545b818f7947da6b5","payload":{"files":{"AGENTS.md":"e8abb38c293b57b71d3a6163d6242d1f375e773372b91b6d8ce39c584c51262a","CLAUDE.md":"5933504f93e5a40369a4790e96dd5dad791d849d3178b1833eb64088e456aaeb","LICENSE":"3096ed1693974a90e1f4f59bf01c9559d94a7941d933e36fb81d3995a97f25c1","README.md":"f603caa93e074200d8ebde79cfed1c6b884ac6a3079926a94536c71d21a3b538","SECURITY.md":"4f2b644d8a7ff87bd87d593f2bdff3f1149454151559f633fc16801cedff7dbe","chat-exports.mjs":"1dc109a971f88605ee3fa3a7564bcd57bce2047b86bfcd92eb3e5acf865d8f11","claude-mod.mjs":"02d86b0cdddf39879efc5194c0a5be6b41456faedec28963dc123a4e88dc3942","commit-rules.mjs":"e94cc32a5415f6d9e978145ab9505ac4ae0bac9431e6e9104b9b37155260b51f","config-edits.mjs":"527a7674f9d52b284bfda3b8089eb4b930a709bf14aedbec7f6eef1e59ef52ee","count-behaviour.mjs":"179baf829eda488bab3f17ae6917473ec01d0d6346f47de7bc144351274f19c8","credential-store.mjs":"a4b3540a61f4ce19e535c7f1d260c0d817686cd066d559a76db2ab108144abba","credential-vault-scripts.mjs":"7e42fe1db3885c2dec6f1de87bebfbefe2292d29179a68f3da87d19eb6c7704b","credential-vault.mjs":"279baabb217e2a3f170e8d7f2b271a88484531f9031c441360ea91561a70da99","engine-release.json":"0e766c012881c228e0472ec3426620e0d1e4e01d5668f6f33d3c60e4a0e0ed1b","git-commits.mjs":"f9c084c54937a86df8fe30a0db2a58e04eeef0dbae69c54b8e368efc9964d6d8","history-outbox.mjs":"85c70389effc5b4ec9d9b33af9192314e67f47af2752515b42a7959344ce2354","history-progress.mjs":"dcc25ee94b727f2605a763349a4d8764640335d6cf0043fb6108cebeed16dcb9","log-session.mjs":"f7e8f247edcc5a3ad17c2c05b83452a938d0e9e20cde5f806a54c1fb04f28958","orchestration-contract.mjs":"ec08f303a166365b99e70b675de4cfe76c6156c2567e14b5569f6d992f0231b7","orchestration-evidence.mjs":"d0d06cecf0eb08e5c6e653c62a4f45b1eddf2119c2acb8341056b34f488d214a","orchestration-report.mjs":"d4e0a9ad8e4a01b462d22c6b1866aac291565c533ff476085d3367cb6d59bc1b","package-integrity.mjs":"a8210c9494a56bfb9a5ea3d10064c7a269f0b6330c0af7c0700e706c3c0224e3","package.json":"fb98ed631188c3ffe3d516646056408556899c74368e1d7d517c68837b79b84a","preserve-lines.mjs":"2df64a69687aa51219bf7b86b88d175cc50990fa29574b31897e41a9ebeecf43","preserve-summary.mjs":"137f781ed7021596a3f0ea67afcf2ab8616ac293f833dbb8f176e838e0ed8321","preserve.mjs":"cd111cae0f53a25a2d878a158479abfb1ace353361454843f91a16d241101d46","runtime-install.mjs":"aab47b52c79d726ef428a24f982c648fa3f1c2dc2b574f633611d02924864e5f","self-update.mjs":"378e3620d48c72cf0bf812be408883b995cdb2223de34a20f762dd73eeffb258","session-databases.mjs":"b9fe20d7445205bdbf4cf2eaaf9cc770dad01e23d41e644b04930ca34e10426d","setup-mcp.mjs":"b732e3dd9b3e522b7cc8acf167ce28ba8f1a8dee797ee15bd7c3b02919b13e6f","stack-rules.mjs":"b23c56e72ec56ba02b17ed895f869172d988c468a697e8d6026b0e268f61e489","stretch-evidence.mjs":"5f5667b92adf11b1a5a6330a0f26dd6db73d56fe60823d9a5246dab71d27d380","transcript-readers.mjs":"e9f20415c2b5d62a09f35a63075002ebd1da56edb32f242d52822d58ecbd8ac3","worktrust-launch.mjs":"84801bd29d472d48188c126b9ebe3989a3132b54e13cc4fe534ff7c383a294ee","worktrust.mjs":"c417a0cabf02318cb3200bda514a3d4ca39dc95f5528d36ff59307f2f045b839"},"format":"worktrust-package/1","kind":"npm","version":"0.10.37"},"signature":"XHc9pXa0YEZrsLz9bB4Zy9AMprpIipFsGNpGUbefQwzJpqPHhqOjN4ch5rXJcGb1JZBxIiqq_FMKGK25EZQZBA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "worktrust",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.37",
|
|
4
4
|
"description": "Couple this computer to WorkTrust: approve a code in your browser, and every AI app here reports what you built. Metadata only, no dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -39,7 +39,9 @@
|
|
|
39
39
|
"history-outbox.mjs",
|
|
40
40
|
"credential-store.mjs",
|
|
41
41
|
"credential-vault.mjs",
|
|
42
|
-
"credential-vault-scripts.mjs"
|
|
42
|
+
"credential-vault-scripts.mjs",
|
|
43
|
+
"self-update.mjs",
|
|
44
|
+
"history-progress.mjs"
|
|
43
45
|
],
|
|
44
46
|
"engines": {
|
|
45
47
|
"node": ">=18"
|
package/preserve.mjs
CHANGED
|
@@ -287,8 +287,8 @@ function archive(dir) {
|
|
|
287
287
|
IDS = computerIds();
|
|
288
288
|
const existing = existsSync(join(dir, "manifest.json")) || readArchive(dir).length > 0 ? check(dir) : { lines: [], manifest: null };
|
|
289
289
|
if (existing.broken) { say(` The archive in ${dir} does not verify, so nothing was added: ${existing.broken}`); process.exit(1); }
|
|
290
|
-
const hook = spawnSync(moduleExecutable(HERE), moduleArgs(HERE, "log-session.mjs", ["--archive-lines"]), { encoding: "utf8", maxBuffer: 1 << 29, env: { ...process.env, WORKTRUST_MCP_URL: "", WORKTRUST_MCP_TOKEN: "", WORKTRUST_HOOK_INPUT: "" } });
|
|
291
|
-
if (hook.status !== 0) { say(
|
|
290
|
+
const hook = spawnSync(moduleExecutable(HERE), moduleArgs(HERE, "log-session.mjs", ["--archive-lines"]), { encoding: "utf8", maxBuffer: 1 << 29, stdio: ["ignore", "pipe", "inherit"], env: { ...process.env, WORKTRUST_MCP_URL: "", WORKTRUST_MCP_TOKEN: "", WORKTRUST_HOOK_INPUT: "", WORKTRUST_HISTORY_PROGRESS: "stderr" } });
|
|
291
|
+
if (hook.status !== 0) { if (hook.signal) { say(" Stopped while reading this computer's sessions: nothing was added. Run the same command again to keep them."); process.exit(130); } say(" The session hook could not read this computer's sessions; nothing was added."); process.exit(1); }
|
|
292
292
|
// A WHOLE ANSWER OR NOTHING: the hook ends with its own count; one line missing or cut is a read to refuse, never part of an archive.
|
|
293
293
|
const rows = hook.stdout.split("\n").filter(Boolean), end = (() => { try { return JSON.parse(rows.at(-1) ?? "").archive_end; } catch { return undefined; } })();
|
|
294
294
|
if (!Number.isInteger(end) || end !== rows.length - 1) { say(` The session hook's answer came back incomplete (${rows.length - 1} of ${end ?? "an unknown number of"} lines), so nothing was added. Run it again.`); process.exit(1); }
|
|
@@ -381,7 +381,8 @@ async function guided() {
|
|
|
381
381
|
else if (!kept) say(write === null ? " Not a terminal: no archive written. Add --yes to agree in advance." : " No archive written.");
|
|
382
382
|
if (existsSync(join(dir, "manifest.json"))) { say(); verifyArchive(dir); summary(dir); }
|
|
383
383
|
}
|
|
384
|
-
|
|
384
|
+
process.on("SIGINT", stopped); process.on("SIGTERM", stopped); function stopped() { // STOPPING SAFELY (2026-11-05): the archive is written in one synchronous pass, so a Ctrl+C is handled after it, never inside it
|
|
385
|
+
say(" Stopped. What was kept so far stays in the archive, whole; run the same command again to keep the rest."); process.exit(130); }
|
|
385
386
|
if (has("summary")) summary(folderAfter("summary"));
|
|
386
387
|
else if (has("verify")) verifyArchive(folderAfter("verify"));
|
|
387
388
|
else if (has("archive")) archive(folderAfter("archive"));
|