worktrust 0.10.31 → 0.10.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -2
- package/SECURITY.md +13 -0
- package/engine-release.json +1 -1
- package/history-outbox.mjs +123 -0
- package/log-session.mjs +491 -57
- package/package-release.json +1 -1
- package/package.json +1 -1
- package/stretch-evidence.mjs +1 -1
- package/transcript-readers.mjs +125 -5
- package/worktrust.mjs +32 -10
package/README.md
CHANGED
|
@@ -1,5 +1,57 @@
|
|
|
1
1
|
# worktrust
|
|
2
2
|
|
|
3
|
+
## 0.10.33: one line per autonomous process per day; a fair-use limit keeps the line
|
|
4
|
+
|
|
5
|
+
A finished headless or scheduled run (a run no person started) is no longer a line of its own. It waits on this
|
|
6
|
+
computer, in `~/.worktrust/process-days`, folded into its process's UTC day: a process is the AI app, how the session
|
|
7
|
+
was started, the timer's opaque reference when a timer started it, and the project folder, as a hash (`process_ref`),
|
|
8
|
+
never a name or a path. Once the day is over and quiet it goes as ONE line with how many runs it holds
|
|
9
|
+
(`process_runs`); the runs' timelines are joined at minute resolution, which keeps every counted second, with each
|
|
10
|
+
run's call outcomes, its turns' length buckets and its subagent runs kept in step (every turn and each minute's first
|
|
11
|
+
completed call stay). A run that
|
|
12
|
+
finishes after its day was sent becomes the day's next part, and once WorkTrust holds its parts it is merged into the
|
|
13
|
+
last one, which goes again whole. When WorkTrust answers a fair-use limit (429 with Retry-After) nothing is lost: the
|
|
14
|
+
line stays where it was and goes after the wait. What is measured is unchanged, so the measure rules stay 2 and no
|
|
15
|
+
re-measure is offered.
|
|
16
|
+
|
|
17
|
+
## 0.10.32: a re-measure sends only the days that changed; which subagents were active at once; each call's outcome
|
|
18
|
+
|
|
19
|
+
Before a re-measure sends anything, the CLI asks WorkTrust, once per AI app, which of this computer's days it holds as
|
|
20
|
+
current. The question carries nothing but the question (`import_history` with `rebuild` and `fingerprint`, no line).
|
|
21
|
+
The answer is the digest this computer sent with each such day, its own submission and never the record: no hours, no
|
|
22
|
+
lines, no figure. A day whose digest on this computer is the same is not sent, so a re-run with nothing new sends
|
|
23
|
+
nothing and says so; one changed day sends that day alone. If the answer is not one (an older WorkTrust, an error, no
|
|
24
|
+
network), every day is sent as before. The digest covers each line as it is sent, without the CLI's own version, so
|
|
25
|
+
an update that measures the same sends nothing. When WorkTrust names where the run waits, the command prints
|
|
26
|
+
`queued, position N, about M min`.
|
|
27
|
+
|
|
28
|
+
A Claude Code session's subagents (the transcripts Claude Code keeps in `<session>/subagents/`) now travel with the day's
|
|
29
|
+
stretch as `agents`: per run, by position only, its kind (`builtin`, `own` for an agent type of your own, `background`,
|
|
30
|
+
`fork`, read from the run's `.meta.json`), when it started in whole seconds from the stretch's start, the stretches of time
|
|
31
|
+
it was active (a gap over five minutes ends one) and how many of its tool steps failed and how many of those a later step
|
|
32
|
+
of the same kind recovered. Never a name, a prompt, a path, a tool or an id. At most 64 runs a stretch, 12 active stretches
|
|
33
|
+
a run and 256 in all; the longest are kept. WorkTrust counts a run only while the session it belongs to is steered by you
|
|
34
|
+
(within 8 hours of your last input), and shows how many agents were active at once beside the sessions at once; it is never
|
|
35
|
+
added to your hours.
|
|
36
|
+
|
|
37
|
+
Beside the timeline, each model answer's outcome travels as one character (`call_outcomes`: completed, failed or not
|
|
38
|
+
known), and in a run no person started (headless or scheduled) the length of each of your own turns as a bucket
|
|
39
|
+
(`input_lengths`: short, medium or long) and, when a timer started the run, its cadence word with an opaque reference
|
|
40
|
+
(`schedule_cadence`, `schedule_ref`). Never a word, a command, a path or the timer's name. WorkTrust credits Agentic Hours
|
|
41
|
+
only for calls recorded as completed.
|
|
42
|
+
|
|
43
|
+
The measure rules go to 2, so `update` offers a re-measure once, naming these changes.
|
|
44
|
+
|
|
45
|
+
The session hook no longer reads a Claude Code transcript from its first byte on every run. Beside each session's
|
|
46
|
+
watermark in `~/.worktrust/sessions.json` it keeps where its last read ended: the file's identity (device and inode), the
|
|
47
|
+
offset, a hash of eight 4 KB samples before it, the offset of the first message not yet sent, and what a full read would
|
|
48
|
+
know at that point (the session's working folder, how it was started, a subagent's first moment). That stays on this
|
|
49
|
+
computer and is never sent. The next run reads from the first unsent message, so a day that spans the offset is read
|
|
50
|
+
again from its start, and gives exactly what a full read gives. A file that was truncated, rewritten, replaced or
|
|
51
|
+
compacted, a new CLI version, other measure rules, or a state without positions is read whole, as before. A re-measure
|
|
52
|
+
(`history`, `history --rebuild`, the offer after an update) always reads everything. Measured: 1 MB appended to a 600 MB
|
|
53
|
+
transcript reads about 1 MB.
|
|
54
|
+
|
|
3
55
|
## 0.10.31: a re-measure is offered only when measuring changed
|
|
4
56
|
|
|
5
57
|
`update` offers to re-measure this computer's sessions only when the new version MEASURES differently from the one it
|
|
@@ -500,9 +552,9 @@ retried safely. A row kept pending on WorkTrust remains excluded from counted ac
|
|
|
500
552
|
| | |
|
|
501
553
|
|---|---|
|
|
502
554
|
| **Reads** | Which AI apps are installed (the settings folders of Claude Code, Codex, Cursor, Gemini CLI, Antigravity, VS Code, Windsurf, Hermes Agent, Goose, OpenCode, OpenClaw), and the chats Cursor and Copilot keep (clocks, models and token counts only). Afterwards, the session hook reads Claude Code's, Codex's and Antigravity's session files, the session databases of Hermes, Goose, OpenCode and OpenClaw, and the chats Cursor and Copilot keep, on this computer to measure durations and token counts. |
|
|
503
|
-
| **Writes** | The key file (above). In each AI app's MCP settings (`~/.claude.json` through `claude mcp`, `~/.cursor/mcp.json`, `~/.codex/config.toml`, `~/.gemini/settings.json`, `~/.gemini/config/mcp_config.json` (Antigravity), VS Code's `mcp.json`, Windsurf's `mcp_config.json`, Hermes's and Goose's `config.yaml`, OpenCode's `opencode.json`, OpenClaw through `openclaw mcp set`) a WorkTrust entry that runs the local bridge, with no key in it;
|
|
555
|
+
| **Writes** | The key file (above). In each AI app's MCP settings (`~/.claude.json` through `claude mcp`, `~/.cursor/mcp.json`, `~/.codex/config.toml`, `~/.gemini/settings.json`, `~/.gemini/config/mcp_config.json` (Antigravity), VS Code's `mcp.json`, Windsurf's `mcp_config.json`, Hermes's and Goose's `config.yaml`, OpenCode's `opencode.json`, OpenClaw through `openclaw mcp set`) a WorkTrust entry that runs the local bridge, with no key in it; four hooks in `~/.claude/settings.json` (SessionStart, SessionEnd, PreCompact and, since 0.10.32, Stop, which sweeps at most once an hour); the bridge, the hook and the counter in `~/.worktrust/`; and `~/.worktrust/consent.json`, what you agreed to (names and days only, 0.10.30). |
|
|
504
556
|
| **Sends, to pair** | This computer's system (macOS, Windows, Linux) and host name. WorkTrust also records the network address the request arrives from, as a security record kept 90 days and seen only by WorkTrust staff (to answer a theft or fraud report). |
|
|
505
|
-
| **Sends, afterwards** | Per measured stretch of work, and nothing else: its timeline and basis (`timeline`, `duration_basis`: since 0.10.20 the CLI sends no duration and no hours in layers of its own; WorkTrust computes them from the timeline), token counts (`tokens_in`, `tokens_out`, `tokens_cache_read`, `tokens_cache_write`, `token_basis`), the model's name (`model`), the kind of work and one layer keyword such as frontend or backend (`title`, `kind`, `layer`, `layers`), closed technology keys (`stack`) and separate display-only tool keys (`tools_seen`), how the turns are counted (`turn_basis`; the counts themselves WorkTrust reads from the timeline
|
|
557
|
+
| **Sends, afterwards** | Per measured stretch of work, and nothing else: its timeline and basis (`timeline`, `duration_basis`: since 0.10.20 the CLI sends no duration and no hours in layers of its own; WorkTrust computes them from the timeline), token counts (`tokens_in`, `tokens_out`, `tokens_cache_read`, `tokens_cache_write`, `token_basis`), the model's name (`model`), the kind of work and one layer keyword such as frontend or backend (`title`, `kind`, `layer`, `layers`), closed technology keys (`stack`) and separate display-only tool keys (`tools_seen`), how the turns are counted (`turn_basis`; the counts themselves WorkTrust reads from the timeline), the subagent runs that started in the stretch, by position only (`agents`, since 0.10.32: per run its kind, builtin, own, background or fork, its start and active intervals in whole seconds from the stretch's start, and its tool failures and recoveries as counts; never a name, a prompt, a path or an id; at most 64 runs, 12 intervals a run, 256 in all), when it began and ended and your computer's offset from UTC (`started_at`, `at`, `utc_offset`), what the CLI derived from the stretch as kinds and counts (`evidence`: checks run and failed, delivery steps and whether a check passed first, recoveries, delegation, context, routing, steering, refusals, planning, files and test files changed, the complexity class; with the WorkTrust mod on, its `live` counts from inside Claude Code: permission answers, effort levels, context and plan-limit peaks, subagents and setup; never a word, a command or a path; since 0.8.3, `live` since 0.10.15), a hash of the session and the day (`stretch_ref`), `steered_from` when it ran over SSH, the stretch's clock as step kinds and whole seconds so WorkTrust computes the figures itself (`timeline`, since 0.10.20: never a word, a tool name or a path), how the session was started as the AI app recorded it, beside the timeline (`session_kind`: interactive, headless or scheduled, since 0.10.29; left out when the app did not say), each model answer's outcome as one character, completed, failed or not known (`call_outcomes`, since 0.10.32), and in a run no person started the length of each of your turns as a bucket (`input_lengths`) and a timer's cadence word with an opaque reference (`schedule_cadence`, `schedule_ref`), the CLI's version and a digest of its own files as signals (`cli_version`, `cli_digest`), and, so the work can be matched to GitHub, the repository as its remote's owner/name (`repo`, only for a repository on GitHub, GitLab or Bitbucket, the hosts WorkTrust can couple; never one on your own or a client's server), for any other git repository a code in its place (`repo_code`: 16 hex of an HMAC of its remote, or its root, keyed with this computer's own secret in `~/.worktrust/archive-salt`, which is never sent, so WorkTrust can group its lines but cannot tell which repository it is) and the hashes of the commits made in the stretch (`commits`, never their messages). With a re-measure (0.10.32), each batch also carries a digest of each day it holds as this computer computed it (`days`: a date and 64 hex, never a line), the question before it asks only for the digests this computer sent before, and a count of transcripts this computer could not read rides beside that question (`unreadable`: a number, never which file). |
|
|
506
558
|
| **Never sends** | Prompts, answers, code, file names or paths, commit messages, branch names, or the name of your local folder (a repository is named only as its remote's owner/name, above). The door has no field for them and refuses a submission that carries them. |
|
|
507
559
|
|
|
508
560
|
**The clock is WorkTrust's, not this computer's (0.10.20).** A measured stretch travels as its timeline: one step per
|
package/SECURITY.md
CHANGED
|
@@ -73,6 +73,19 @@ private transcripts or account data in a public issue.
|
|
|
73
73
|
here, a wake hook); it cannot widen what is written, sent or read. An unreadable or unsafe file counts as no agreement.
|
|
74
74
|
- A No drops the entry it answered; `disconnect` deletes the file; a coupling to another account starts it empty.
|
|
75
75
|
|
|
76
|
+
## The question before a re-measure (0.10.32)
|
|
77
|
+
|
|
78
|
+
Before a re-measure, the hook asks the door, per AI app, which days of this computer WorkTrust holds as current. The
|
|
79
|
+
request is `import_history` with `rebuild: true, fingerprint: true` and nothing else: no line, no hours, no path. It
|
|
80
|
+
goes under the computer's own key and, on a bound computer, with its device proof, like every history batch. The
|
|
81
|
+
answer is a list of UTC days and, for each, the 64-hex digest this computer sent with that day: digests of its own
|
|
82
|
+
submissions, never a line, a figure or anything of the record. A digest is computed here from what this computer would
|
|
83
|
+
send; a day that matches is not sent again. An answer that cannot be read means not known, and every day is sent.
|
|
84
|
+
|
|
85
|
+
When a transcript on this computer exists but cannot be read (permissions, a disk error), the hook counts it, keeps the
|
|
86
|
+
count in `~/.worktrust/sessions.json`, shows it in `npx worktrust status`, and at most once a day asks the same question
|
|
87
|
+
with the count beside it (`unreadable: N`): a number, never which file, its path or anything in it.
|
|
88
|
+
|
|
76
89
|
## What measurement signatures establish
|
|
77
90
|
|
|
78
91
|
The metadata journal introduced in the 0.10.21 candidate preserves observations, not their
|
package/engine-release.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"keyId":"608168ccdef72562fc91f9c0f9f2c2d6876bece7f7362cb545b818f7947da6b5","payload":{"files":{"chat-exports.mjs":"04eafe333940ec03d5058d8d51062124aa12ab882bf04db03bcda5613ec69298","claude-mod.mjs":"02d86b0cdddf39879efc5194c0a5be6b41456faedec28963dc123a4e88dc3942","config-edits.mjs":"0eba2f176ef6f7afba071d6e00de0ff19ba2b283180a801dfd957aa38c1edd94","count-behaviour.mjs":"1beaec2a4d0aead76d166f89cc52441a9cf48f2e69da9888ca2091dd3a10a37b","credential-store.mjs":"a4b3540a61f4ce19e535c7f1d260c0d817686cd066d559a76db2ab108144abba","credential-vault-scripts.mjs":"7e42fe1db3885c2dec6f1de87bebfbefe2292d29179a68f3da87d19eb6c7704b","credential-vault.mjs":"279baabb217e2a3f170e8d7f2b271a88484531f9031c441360ea91561a70da99","history-outbox.mjs":"
|
|
1
|
+
{"keyId":"608168ccdef72562fc91f9c0f9f2c2d6876bece7f7362cb545b818f7947da6b5","payload":{"files":{"chat-exports.mjs":"04eafe333940ec03d5058d8d51062124aa12ab882bf04db03bcda5613ec69298","claude-mod.mjs":"02d86b0cdddf39879efc5194c0a5be6b41456faedec28963dc123a4e88dc3942","config-edits.mjs":"0eba2f176ef6f7afba071d6e00de0ff19ba2b283180a801dfd957aa38c1edd94","count-behaviour.mjs":"1beaec2a4d0aead76d166f89cc52441a9cf48f2e69da9888ca2091dd3a10a37b","credential-store.mjs":"a4b3540a61f4ce19e535c7f1d260c0d817686cd066d559a76db2ab108144abba","credential-vault-scripts.mjs":"7e42fe1db3885c2dec6f1de87bebfbefe2292d29179a68f3da87d19eb6c7704b","credential-vault.mjs":"279baabb217e2a3f170e8d7f2b271a88484531f9031c441360ea91561a70da99","history-outbox.mjs":"b88072a28aa1dd94d9fab880f96b711b5fbed76ca62e5de24d612c08171e5b71","log-session.mjs":"d20e20c4fa5e4dd6a48a2c3b7caf4cefb090063503f38dc4716d8daee6d3bf22","orchestration-contract.mjs":"ec08f303a166365b99e70b675de4cfe76c6156c2567e14b5569f6d992f0231b7","orchestration-evidence.mjs":"d0d06cecf0eb08e5c6e653c62a4f45b1eddf2119c2acb8341056b34f488d214a","orchestration-report.mjs":"d4e0a9ad8e4a01b462d22c6b1866aac291565c533ff476085d3367cb6d59bc1b","package-integrity.mjs":"a8210c9494a56bfb9a5ea3d10064c7a269f0b6330c0af7c0700e706c3c0224e3","preserve-lines.mjs":"2df64a69687aa51219bf7b86b88d175cc50990fa29574b31897e41a9ebeecf43","preserve-summary.mjs":"137f781ed7021596a3f0ea67afcf2ab8616ac293f833dbb8f176e838e0ed8321","preserve.mjs":"3e369083ba43bcda2fa0622fd526aa64fcf8ad66e7b6e8f23821752c4d47ad27","runtime-install.mjs":"aab47b52c79d726ef428a24f982c648fa3f1c2dc2b574f633611d02924864e5f","session-databases.mjs":"543fd3b82d28d96d789be0a1dc9186c5fafa783e98b87e5b3b17b211542e2b4d","setup-mcp.mjs":"0c29113179561af1c1e9ff8b7a786e7240edf48a2244993e12335e5ee362c812","stack-rules.mjs":"b23c56e72ec56ba02b17ed895f869172d988c468a697e8d6026b0e268f61e489","stretch-evidence.mjs":"5f5667b92adf11b1a5a6330a0f26dd6db73d56fe60823d9a5246dab71d27d380","transcript-readers.mjs":"ed6fef364aa28f39361d8f627abcda2162498cbaa13e90cc5860a768cb355fa2","worktrust-launch.mjs":"84801bd29d472d48188c126b9ebe3989a3132b54e13cc4fe534ff7c383a294ee","worktrust.mjs":"804e7527618ff4ddea6b6457ff9878c1179581827580387b4468fd02af364266"},"format":"worktrust-package/1","kind":"engine","version":"0.10.33"},"signature":"hpJAJKGQjzzyecwWYTpU2zL-StLIzDzTBSG9-C9f4skwv_Vy7zZOFP9MM3PlQcKJvyAk-sPQT8n0JYCuZWpmBw"}
|
package/history-outbox.mjs
CHANGED
|
@@ -163,3 +163,126 @@ export async function resumeHistory(target, {send, confirmed, note, retryReview=
|
|
|
163
163
|
}
|
|
164
164
|
/** A queued batch's id, as queuedHistory names it, so a send can hand its own batches to resumeHistory (`only`). */
|
|
165
165
|
export const historyIdOf=(target,body,source)=>digest(scopeOf(target)+'\0'+source+'\0'+body);
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* ONE LINE PER PROCESS PER DAY (0.10.33; owner, 2026-10-28; docs/FAIR-USE.md §1). An autonomous process (a headless or scheduled
|
|
169
|
+
* session: `claude -p` from cron, an agent every ten minutes) writes a new transcript per run, and every run used to be a line
|
|
170
|
+
* of its own: 144 a day for one process, a hundred processes 14,400. Its runs of one UTC day are one line now. A process is its
|
|
171
|
+
* client, its session kind, its schedule (when the client names one) and its project folder, as a hash: never content, never the
|
|
172
|
+
* path. The runs wait here, on disk, folded as they finish; the day is sent once it is over and quiet. A run is folded once (by
|
|
173
|
+
* its id), and a run that finishes after its day was sent becomes the day's next part; once the database's PROCESS_PARTS are spent
|
|
174
|
+
* it is merged into the last part, which goes again whole. Nothing is ever let go before WorkTrust took it.
|
|
175
|
+
* Nothing here reads a transcript or the network: the hook hands it measured runs and sends what it answers.
|
|
176
|
+
*/
|
|
177
|
+
export const PROCESS_PARTS=3; // the database's analysis.process_day_max_parts()
|
|
178
|
+
const STEP=/(G?)([HIURATWhiuratw])([0-9]{1,6})/g;
|
|
179
|
+
const PART_CHARS=150_000; // a part stays well inside the door's 200,000 characters of timeline
|
|
180
|
+
const b64=s=>createHash('sha256').update(s).digest('base64url');
|
|
181
|
+
export const processRefOf=({source,kind,schedule,cwd,day})=>b64(`worktrust-process/1|${source??'claude'}|${kind}|${schedule??''}|${cwd?digest(String(cwd)):''}|${day}`);
|
|
182
|
+
export const lineRefOf=(ref,part)=>b64(`worktrust-process-line/1|${ref}|${part}`);
|
|
183
|
+
/** A run's timeline at MINUTE RESOLUTION: per wall-clock minute its first and its last step, each with its own kind.
|
|
184
|
+
* A dropped step lies inside one minute between two kept ones, so every merged gap is under a minute, below the idle cap, and the
|
|
185
|
+
* seconds WorkTrust counts from the timeline are exactly the same; a day holds at most two steps a minute (2,880). The calls of a
|
|
186
|
+
* minute whose first and last steps are not the model's answers are not kept: a call counts at most once per five minutes anyway. */
|
|
187
|
+
export function minuteCompact(timeline,fromMs){return minuteCompactRun(timeline,fromMs).timeline;}
|
|
188
|
+
const ANSWER='ATW';
|
|
189
|
+
/** THE SAME, WITH WHAT RIDES ON THE STEPS (release 9 integration, 2026-10-29): 0.10.32's call outcomes (one character per
|
|
190
|
+
* answer step, A/T/W) and input lengths (one bucket per H) are aligned with the timeline's steps, so a compacted timeline
|
|
191
|
+
* carries exactly the outcomes of its kept answer steps and the lengths of its kept turns. Every H is kept (a person's turn
|
|
192
|
+
* starts steered time: dropping one would move it), and in each minute the first answer recorded as completed ('1') is
|
|
193
|
+
* kept beside the first and last step, so the guardrail never loses a positive call to the compaction. Seconds stay exact:
|
|
194
|
+
* every kept set holds each minute's first and last step. Without outcomes or lengths, null comes back for them. */
|
|
195
|
+
export function minuteCompactRun(timeline,fromMs,outcomes=null,lengths=null){
|
|
196
|
+
const steps=[...String(timeline).matchAll(STEP)].map(m=>({g:m[1],k:m[2],K:m[2].toUpperCase(),gap:Number(m[3])}));
|
|
197
|
+
if(!steps.length)return {timeline:'',outcomes:outcomes==null?null:'',lengths:lengths==null?null:''};
|
|
198
|
+
let c=0;const at=steps.map(s=>(c+=s.gap));const first=steps.findIndex(s=>s.k===s.K);const base=fromMs-1000*(at[Math.max(0,first)]??0);
|
|
199
|
+
let ai=0,hi=0;const oc=steps.map(s=>ANSWER.includes(s.K)?(outcomes==null?null:String(outcomes)[ai++]??null):null);
|
|
200
|
+
const lg=steps.map(s=>s.K==='H'?(lengths==null?null:String(lengths)[hi++]??null):null);
|
|
201
|
+
const groups=new Map(),keep=new Set();
|
|
202
|
+
steps.forEach((s,i)=>{const m=Math.floor((base+1000*at[i])/60_000),g=groups.get(m)??{first:i,last:i,done:-1};g.last=i;if(g.done<0&&oc[i]==='1')g.done=i;groups.set(m,g);});
|
|
203
|
+
for(const g of groups.values()){keep.add(g.first);keep.add(g.last);if(g.done>=0)keep.add(g.done);}
|
|
204
|
+
steps.forEach((s,i)=>{if(s.K==='H')keep.add(i);});
|
|
205
|
+
let out='',o='',l='',gap=0;
|
|
206
|
+
for(let i=0;i<steps.length;i++){gap+=i===0?0:steps[i].gap;if(!keep.has(i))continue;out+=`${steps[i].g}${steps[i].k}${Math.min(999999,gap)}`;gap=0;if(oc[i]!=null)o+=oc[i];if(lg[i]!=null)l+=lg[i];}
|
|
207
|
+
return {timeline:out,outcomes:outcomes==null?null:o,lengths:lengths==null?null:l};
|
|
208
|
+
}
|
|
209
|
+
const stepsOf=(t,kinds)=>[...String(t??'').matchAll(STEP)].filter(m=>kinds.includes(m[2].toUpperCase())).length;
|
|
210
|
+
/** The runs of one part as ONE stretch: their timelines joined in time order, each run's first step at a zero gap (the time
|
|
211
|
+
* between two runs belongs to neither, as it did when each was a line), a later run's bridge left out; tokens summed, the model
|
|
212
|
+
* the one most runs used, the window from the first run's start to the last run's end. Release 9: the runs' call outcomes and
|
|
213
|
+
* input lengths joined in the same order (a bridge left out takes its own character with it; a run whose steps carry none
|
|
214
|
+
* makes the part carry none, never a misaligned string), and their subagent runs (wall-clock stamps) listed together. */
|
|
215
|
+
export function combineRuns(runs){
|
|
216
|
+
const sorted=[...runs].sort((a,b)=>a.from-b.from),models=new Map();let timeline='',shapeKnown=true,outcomes='',lengths='',agents=[];
|
|
217
|
+
const sum={tokensIn:0,tokensOut:0,cacheRead:0,cacheWrite:0};
|
|
218
|
+
for(const [i,run] of sorted.entries()){
|
|
219
|
+
let t=run.timeline,o=run.outcomes??null,l=run.lengths??null;
|
|
220
|
+
if(o==null&&stepsOf(t,ANSWER)>0)outcomes=null;if(l==null&&stepsOf(t,'H')>0)lengths=null;
|
|
221
|
+
if(i>0){const bridge=/^G?([hiuratw])[0-9]{1,6}/.exec(t);if(bridge){const K=bridge[1].toUpperCase();if(o!=null&&ANSWER.includes(K))o=o.slice(1);if(l!=null&&K==='H')l=l.slice(1);}
|
|
222
|
+
t=t.replace(/^G?[hiuratw][0-9]{1,6}/,'').replace(/^(G?[HIURATW])[0-9]{1,6}/,(_,head)=>`${head}0`);}
|
|
223
|
+
timeline+=t;if(outcomes!=null&&o!=null)outcomes+=o;if(lengths!=null&&l!=null)lengths+=l;
|
|
224
|
+
for(const key of Object.keys(sum))sum[key]+=Number(run[key])||0;shapeKnown&&=run.shapeKnown!==false;
|
|
225
|
+
if(run.model)models.set(run.model,(models.get(run.model)??0)+1);
|
|
226
|
+
if(Array.isArray(run.agents))agents=agents.concat(run.agents);
|
|
227
|
+
}
|
|
228
|
+
return {...(shapeKnown?sum:{tokensIn:0,tokensOut:0,cacheRead:0,cacheWrite:0}),shapeKnown,timeline:timeline||null,model:[...models].sort((a,b)=>b[1]-a[1])[0]?.[0]??null,
|
|
229
|
+
from:sorted[0]?.from,to:Math.max(...sorted.map(r=>r.to)),offset:sorted[0]?.offset,
|
|
230
|
+
outcomes:outcomes&&outcomes.length===stepsOf(timeline,ANSWER)?outcomes:null,lengths:lengths&&lengths.length===stepsOf(timeline,'H')?lengths:null,
|
|
231
|
+
agents:agents.length?{list:agents}:null};
|
|
232
|
+
}
|
|
233
|
+
/** A process-day's runs in time order, cut into parts whose timelines stay under PART_CHARS (one part on any real day). */
|
|
234
|
+
export function processParts(runs){
|
|
235
|
+
const out=[];let part=[],chars=0;
|
|
236
|
+
for(const run of [...runs].sort((a,b)=>a.from-b.from)){if(part.length&&chars+run.timeline.length>PART_CHARS){out.push(part);part=[];chars=0;}part.push(run);chars+=run.timeline.length;}
|
|
237
|
+
if(part.length)out.push(part);
|
|
238
|
+
return out;
|
|
239
|
+
}
|
|
240
|
+
/** One file per process-day under ~/.worktrust/process-days (a hundred processes stay a hundred small files), one lock. */
|
|
241
|
+
function processStore(){
|
|
242
|
+
const home=join(homedir(),'.worktrust');directory(home);const root=join(home,'process-days');directory(root);
|
|
243
|
+
const path=ref=>{if(!/^[A-Za-z0-9_-]{43}$/.test(ref))throw Error('PROCESS_DAY_UNSAFE');return join(root,ref+'.json');};
|
|
244
|
+
return {root,load:ref=>read(path(ref)),save:(ref,value)=>write(path(ref),value),drop:ref=>{const p=path(ref);if(existsSync(p)){regular(p);unlinkSync(p);}},
|
|
245
|
+
refs:()=>readdirSync(root).filter(f=>/^[A-Za-z0-9_-]{43}\.json$/.test(f)).map(f=>f.slice(0,43))};
|
|
246
|
+
}
|
|
247
|
+
/** Folds one finished run into its process-day, on disk, before the caller passes the transcript's mark. Idempotent by run id. */
|
|
248
|
+
export function foldProcessRun(key,run){
|
|
249
|
+
const store=processStore();
|
|
250
|
+
return reservation(store.root,()=>{
|
|
251
|
+
const day=store.load(key.ref)??{format:'worktrust-process-day/1',day:key.day,source:key.source,kind:key.kind,cwd:key.cwd,steered:key.steered??null,schedule:key.schedule??null,codex:key.codex===true,sent:0,ids:[],runs:[]};
|
|
252
|
+
const id=digest(run.id).slice(0,20);if(day.ids.includes(id))return 'held';
|
|
253
|
+
day.ids.push(id);day.runs.push({...run,id});store.save(key.ref,day);return 'folded';
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
/** The process-days to send now: the UTC day over and every run quiet for `quietMs`, not waiting on a retry; each as its parts
|
|
257
|
+
* (runs cut so a part's timeline stays under PART_CHARS). A sent day is let go thirty days after it ends (its ids and last part kept till then, so a late run merges). */
|
|
258
|
+
export function dueProcessDays(now,quietMs){
|
|
259
|
+
const store=processStore();
|
|
260
|
+
return reservation(store.root,()=>{
|
|
261
|
+
const out=[];
|
|
262
|
+
for(const ref of store.refs()){
|
|
263
|
+
const day=store.load(ref);if(!day)continue;
|
|
264
|
+
const end=Date.parse(`${day.day}T00:00:00Z`)+86_400_000;
|
|
265
|
+
if(!day.runs.length){if(now>end+30*86_400_000)store.drop(ref);continue;}
|
|
266
|
+
if(now<end+quietMs||now-Math.max(...day.runs.map(r=>r.to))<quietMs||Number(day.retryAt)>now)continue;
|
|
267
|
+
// THE PARTS ARE SPENT (the database holds PROCESS_PARTS lines of a day): a later run is MERGED into the last part, sent
|
|
268
|
+
// again with every run it held, and the database puts the fuller line in its place. Nothing is ever let go unsent.
|
|
269
|
+
const parts=processParts(day.runs),room=Math.max(0,PROCESS_PARTS-day.sent);
|
|
270
|
+
if(room===0)out.push({ref,...day,part:PROCESS_PARTS,runs:[...(day.lastRuns??[]),...day.runs],pending:day.runs.map(r=>r.id)});
|
|
271
|
+
else{const keep=parts.slice(0,room-1),rest=parts.slice(room-1).flat();keep.forEach((runs,i)=>out.push({ref,...day,part:day.sent+i+1,runs,pending:runs.map(r=>r.id)}));
|
|
272
|
+
if(rest.length)out.push({ref,...day,part:day.sent+keep.length+1,runs:rest,pending:rest.map(r=>r.id)});}
|
|
273
|
+
}
|
|
274
|
+
return out;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
/** WorkTrust took the part: its new runs leave the pending list; the ids stay, so a run is never folded twice. */
|
|
278
|
+
export function markProcessSent(ref,due){
|
|
279
|
+
const store=processStore();
|
|
280
|
+
reservation(store.root,()=>{const day=store.load(ref);if(!day)return;const gone=new Set(due.pending);day.runs=day.runs.filter(r=>!gone.has(r.id));
|
|
281
|
+
// The last part's runs stay (the day's own copy) until the day is let go: a run that comes later is merged into them.
|
|
282
|
+
day.sent=Math.max(day.sent,due.part);if(due.part>=PROCESS_PARTS)day.lastRuns=due.runs;delete day.retryAt;store.save(ref,day);});
|
|
283
|
+
}
|
|
284
|
+
/** Not taken now (a limit, the network): kept, and asked again after `seconds`. */
|
|
285
|
+
export function deferProcessDay(ref,seconds){
|
|
286
|
+
const store=processStore();
|
|
287
|
+
reservation(store.root,()=>{const day=store.load(ref);if(!day)return;day.retryAt=Date.now()+Math.max(1,Math.min(MAX_RETRY_SECONDS,seconds))*1000;store.save(ref,day);});
|
|
288
|
+
}
|