worktrust 0.10.32 → 0.10.34
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 +26 -0
- package/commit-rules.mjs +447 -0
- package/engine-release.json +1 -1
- package/git-commits.mjs +346 -0
- package/history-outbox.mjs +123 -0
- package/log-session.mjs +67 -6
- package/package-release.json +1 -1
- package/package.json +3 -1
- package/worktrust.mjs +53 -7
package/git-commits.mjs
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* YOUR OWN COMMITS, MEASURED ON THIS COMPUTER (`npx worktrust git`, CLI 0.10.34; option C, owner 2026-10-29;
|
|
3
|
+
* docs/GITHUB.md). GitHub shows a commit-reading App as "Read access to code", and there is no narrower permission. So
|
|
4
|
+
* the commits are read HERE, with `git log`, filtered to the person's own author e-mails; each commit's file names are
|
|
5
|
+
* turned into the GitHub walk's own closed keys on this computer (commit-rules.mjs, generated from core), and what is
|
|
6
|
+
* sent is counts, keys and markers, never a path, a message, a diff, a sha or the remote's address. A commit travels as
|
|
7
|
+
* its owner-keyed reference: HMAC-SHA256 under `worktrust-commit:<owner id>` over `uuidFrom("commit", sha)`, the bytes
|
|
8
|
+
* the server computes from GitHub's own head sha (docs/GITHUB.md §1), so a GitHub-observed check run on the same commit
|
|
9
|
+
* can corroborate it without this computer ever sending the sha.
|
|
10
|
+
*
|
|
11
|
+
* Asked once, with the list of repositories: what is not shown is not read, and Enter (or --yes) is No. The answers are
|
|
12
|
+
* kept in ~/.worktrust/git.json (0600): this computer's own paths, never sent.
|
|
13
|
+
*/
|
|
14
|
+
import { execFileSync } from "node:child_process";
|
|
15
|
+
import { createHash, createHmac } from "node:crypto";
|
|
16
|
+
import { closeSync, existsSync, openSync, readFileSync, readSync, readdirSync, realpathSync, statSync, writeFileSync, mkdirSync, chmodSync } from "node:fs";
|
|
17
|
+
import { homedir } from "node:os";
|
|
18
|
+
import { join, resolve } from "node:path";
|
|
19
|
+
import * as rules from "./commit-rules.mjs";
|
|
20
|
+
|
|
21
|
+
/** The walker's id primitive (apps/worktrust/app/src/lib/scoring/ids.ts), byte for byte. */
|
|
22
|
+
export function uuidFrom(...parts) {
|
|
23
|
+
const hex = createHash("sha256").update(parts.join(" ")).digest("hex");
|
|
24
|
+
const variant = ((parseInt(hex.slice(16, 18), 16) % 64) + 128).toString(16).padStart(2, "0");
|
|
25
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-5${hex.slice(13, 16)}-${variant}${hex.slice(18, 20)}-${hex.slice(20, 32)}`;
|
|
26
|
+
}
|
|
27
|
+
/** The commit's owner-keyed reference (docs/GITHUB.md §1). */
|
|
28
|
+
export const commitRefOf = (owner, sha) => createHmac("sha256", `worktrust-commit:${owner}`).update(uuidFrom("commit", sha)).digest("hex");
|
|
29
|
+
/** A Supabase migration's version as the walk marks it (packages/integrations/src/providers/supabase/api.ts). */
|
|
30
|
+
const migrationMarkerOf = (owner, version) => createHash("sha256").update(`supabase-migration:${owner}:${version}`).digest("hex");
|
|
31
|
+
export const migrationMarkersOf = (paths, owner) => [...new Set(paths.flatMap((path) => { const match = /(?:^|\/)supabase\/migrations\/(\d{1,30})_[^/]+\.sql$/.exec(path); return match ? [migrationMarkerOf(owner, match[1])] : []; }))].slice(0, 50);
|
|
32
|
+
|
|
33
|
+
/** GitHub's commit detail lists at most 300 files on the page the walk reads; the same paths are read here. */
|
|
34
|
+
const DETAIL_FILES = 300;
|
|
35
|
+
export const LIMITS = { perRepo: 1000, perRun: 2000, perBatch: 400, batchBytes: 1_500_000, transcripts: 2000 };
|
|
36
|
+
const HOSTS = /^(?:[a-z][a-z0-9+.-]*:\/\/)?(?:[^@/]+@)?(github\.com|gitlab\.com|bitbucket\.org)[:/]/i;
|
|
37
|
+
|
|
38
|
+
function git(cwd, ...argv) {
|
|
39
|
+
try { return execFileSync("git", ["-C", cwd, "-c", "core.quotePath=false", ...argv], { encoding: "utf8", timeout: 30_000, maxBuffer: 256 * 1024 * 1024, stdio: ["ignore", "pipe", "ignore"] }); }
|
|
40
|
+
catch { return ""; }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The work tree a folder sits in, or null. */
|
|
44
|
+
export function repoRootOf(dir) {
|
|
45
|
+
if (!dir || !existsSync(dir)) return null;
|
|
46
|
+
const top = git(dir, "rev-parse", "--show-toplevel").trim();
|
|
47
|
+
if (!top) return null;
|
|
48
|
+
try { return realpathSync(top); } catch { return top; }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* What a repository is called on the way out: owner/name, lowercased, for a remote on a host WorkTrust couples (what a
|
|
53
|
+
* session already names, CLI 0.8.6), so it is filed under the GitHub walk's project; otherwise a code only this
|
|
54
|
+
* computer can make (HMAC under its archive salt over the normalised remote or the root), never a name.
|
|
55
|
+
*/
|
|
56
|
+
export function repoIdentity(root, salt) {
|
|
57
|
+
const remote = git(root, "remote", "get-url", "origin").trim();
|
|
58
|
+
if (remote && HOSTS.test(remote)) {
|
|
59
|
+
const match = /[:/]([^/:]+)\/([^/]+?)(?:\.git)?\/?$/.exec(remote);
|
|
60
|
+
const name = match ? `${match[1]}/${match[2]}`.toLowerCase() : null;
|
|
61
|
+
if (name && /^[a-z0-9_.-]{1,100}\/[a-z0-9_.-]{1,100}$/.test(name)) return { name, github: /github\.com/i.test(remote) };
|
|
62
|
+
}
|
|
63
|
+
const basis = remote ? `remote:${remote.trim().toLowerCase().replace(/^[a-z][a-z0-9+.-]*:\/\//, "").replace(/^[^@/]+@/, "").replace(/:(?!\d)/, "/").replace(/\.git$/, "").replace(/\/+$/, "")}` : `root:${root}`;
|
|
64
|
+
return salt ? { code: createHmac("sha256", salt).update(`worktrust-repo-code|${basis}`).digest("hex").slice(0, 16), github: false } : null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The person's author e-mails: git's own setting here and globally, plus what they added. Lowercased, once each. */
|
|
68
|
+
export function authorEmails(root, extra = []) {
|
|
69
|
+
const found = [git(root, "config", "user.email"), git(root, "config", "--global", "user.email"), ...extra].map((email) => String(email).trim().toLowerCase()).filter((email) => /^[^\s@]+@[^\s@]+$/.test(email));
|
|
70
|
+
return [...new Set(found)];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A numstat path as GitHub's detail names it: a rename is its new path. */
|
|
74
|
+
export function numstatPath(text) {
|
|
75
|
+
let path = text.replace(/\{([^{}]*) => ([^{}]*)\}/g, "$2").replace(/\/{2,}/g, "/").replace(/^\//, "");
|
|
76
|
+
if (path.includes(" => ")) path = path.slice(path.lastIndexOf(" => ") + 4);
|
|
77
|
+
return path;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The person's own commits in one work tree, newest first, at most `limit`: every local branch, merges diffed against
|
|
82
|
+
* their first parent (as GitHub's detail is), only commits whose author e-mail is one of theirs. Read: the sha, the
|
|
83
|
+
* parents, two dates, the author e-mail (to filter, never kept) and per file the added and deleted lines and the path.
|
|
84
|
+
* Never the message, never the content.
|
|
85
|
+
*/
|
|
86
|
+
export function readCommits(root, { emails, since = null, limit = LIMITS.perRepo }) {
|
|
87
|
+
if (!emails.length) return [];
|
|
88
|
+
const argv = ["log", "--branches", "--no-color", "--no-ext-diff", "--numstat", "--diff-merges=first-parent", "--format=%x01%H%x09%P%x09%aI%x09%cI%x09%aE", `--max-count=${limit}`, "--fixed-strings", "--regexp-ignore-case", ...emails.map((email) => `--author=<${email}>`)];
|
|
89
|
+
if (since) argv.push(`--since=${since}`);
|
|
90
|
+
const out = git(root, ...argv);
|
|
91
|
+
const mine = new Set(emails);
|
|
92
|
+
const commits = [];
|
|
93
|
+
for (const block of out.split("\x01").slice(1)) {
|
|
94
|
+
const [head, ...rest] = block.split("\n");
|
|
95
|
+
const [sha, parents, authored, committed, email] = head.split("\t");
|
|
96
|
+
if (!/^[0-9a-f]{40}$/.test(sha ?? "") || !mine.has(String(email).toLowerCase())) continue;
|
|
97
|
+
let additions = 0, deletions = 0;
|
|
98
|
+
const paths = [];
|
|
99
|
+
for (const line of rest) {
|
|
100
|
+
const match = /^(\d+|-)\t(\d+|-)\t(.+)$/.exec(line);
|
|
101
|
+
if (!match) continue;
|
|
102
|
+
if (match[1] !== "-") additions += Number(match[1]);
|
|
103
|
+
if (match[2] !== "-") deletions += Number(match[2]);
|
|
104
|
+
paths.push(numstatPath(match[3]));
|
|
105
|
+
}
|
|
106
|
+
commits.push({ sha, parents: (parents ?? "").split(" ").filter((p) => /^[0-9a-f]{40}$/.test(p)), authored, committed, additions, deletions, paths });
|
|
107
|
+
}
|
|
108
|
+
return commits;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** When the repository began, as a day: its first commit. The walk reads GitHub's creation date for the work mode. */
|
|
112
|
+
export function repoSince(root) {
|
|
113
|
+
const days = git(root, "log", "--max-parents=0", "--format=%cI", "HEAD").split("\n").map((line) => line.trim()).filter(Boolean).map((at) => new Date(at)).filter((at) => Number.isFinite(at.getTime()));
|
|
114
|
+
return days.length ? new Date(Math.min(...days.map(Number))).toISOString().slice(0, 10) : null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* ONE COMMIT AS THE GITHUB WALK WOULD FILE IT (walk.ts processNode), computed here. The paths go in and never come
|
|
119
|
+
* out: what comes out is the reference, the counts and the closed keys. `projectRef` is the project the server filed
|
|
120
|
+
* the repository under (the walk's own for a GitHub repository); without it no path marker is made.
|
|
121
|
+
*/
|
|
122
|
+
export function commitPayload(commit, { owner, projectRef }) {
|
|
123
|
+
const paths = commit.paths.slice(0, DETAIL_FILES);
|
|
124
|
+
const features = rules.featuresOfFiles(paths);
|
|
125
|
+
const authoredMs = Date.parse(commit.authored ?? ""), committedMs = Date.parse(commit.committed);
|
|
126
|
+
return {
|
|
127
|
+
ref: commitRefOf(owner, commit.sha),
|
|
128
|
+
at: new Date(committedMs).toISOString().replace(/\.000Z$/, "Z"),
|
|
129
|
+
replayDays: Number.isFinite(authoredMs) ? Math.max(0, Math.round((committedMs - authoredMs) / 86_400_000)) : null,
|
|
130
|
+
additions: commit.additions,
|
|
131
|
+
deletions: commit.deletions,
|
|
132
|
+
files: paths.length,
|
|
133
|
+
parents: commit.parents.length,
|
|
134
|
+
parentRefs: commit.parents.slice(0, 8).map((sha) => commitRefOf(owner, sha)),
|
|
135
|
+
classes: rules.classesOfFiles(paths),
|
|
136
|
+
stack: rules.stackOfFiles(paths),
|
|
137
|
+
arch: rules.archOfFiles(paths),
|
|
138
|
+
security: rules.securityMarkersOfFiles(paths),
|
|
139
|
+
agents: rules.agentMarkersOfFiles(paths),
|
|
140
|
+
rootMarkers: rules.rootPrefixesOf(paths).map((prefix) => uuidFrom("root", owner, prefix)),
|
|
141
|
+
pathMarkers: projectRef ? [...new Set(paths)].slice(0, 40).map((path) => uuidFrom("path", owner, projectRef, path)) : [],
|
|
142
|
+
migrationMarkers: migrationMarkersOf(paths, owner),
|
|
143
|
+
extensions: features.extensions,
|
|
144
|
+
classRules: features.classRules,
|
|
145
|
+
manifests: features.manifests,
|
|
146
|
+
archPoints: features.archPoints,
|
|
147
|
+
classifier: rules.CLASSIFIER_VERSION,
|
|
148
|
+
markerVersion: rules.MARKER_VERSION,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* NOTHING THAT NAMES THE WORK LEAVES (the planted-leak test holds it). Before a batch is sent it is read as text for
|
|
154
|
+
* every path it was made from, every sha, every remote and root it came from: one found refuses the whole batch. A
|
|
155
|
+
* commit's fields are a closed set, so a field nobody wrote here is refused as well.
|
|
156
|
+
*/
|
|
157
|
+
const COMMIT_FIELDS = new Set(["ref", "at", "replayDays", "additions", "deletions", "files", "parents", "parentRefs", "classes", "stack", "arch", "security", "agents", "rootMarkers", "pathMarkers", "migrationMarkers", "extensions", "classRules", "manifests", "archPoints", "classifier", "markerVersion"]);
|
|
158
|
+
const REF = /^[0-9a-f]{64}$/, UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/, VERSION = /^\d{1,3}\.\d{1,3}\.\d{1,3}$/;
|
|
159
|
+
const CLOSED = {
|
|
160
|
+
classes: new Set([...rules.CLASS_RULES.map((rule) => rule.cls), "code"]), classRules: new Set(rules.CLASS_RULES.map((rule) => rule.id)),
|
|
161
|
+
stack: new Set(rules.TECHNOLOGIES), manifests: new Set(rules.TECHNOLOGIES), arch: new Set(rules.ARCHITECTURE_POINTS), archPoints: new Set(rules.ARCHITECTURE_POINTS),
|
|
162
|
+
security: new Set(rules.SECURITY_MARKERS), agents: new Set(rules.AGENT_MARKERS),
|
|
163
|
+
};
|
|
164
|
+
const SHAPED = { parentRefs: REF, migrationMarkers: REF, rootMarkers: UUID, pathMarkers: UUID };
|
|
165
|
+
const isCount = (value) => value === null || (Number.isInteger(value) && value >= 0);
|
|
166
|
+
/** Why a commit is not of the closed shape the door takes, or null: every word from a list, every string a pattern. */
|
|
167
|
+
function shapeRefusal(commit) {
|
|
168
|
+
for (const key of Object.keys(commit)) if (!COMMIT_FIELDS.has(key)) return `an unknown field (${key})`;
|
|
169
|
+
if (!REF.test(commit.ref ?? "") || typeof commit.at !== "string" || !/^\d{4}-\d{2}-\d{2}T[\d:.]+Z$/.test(commit.at)) return "a reference or an instant of the wrong shape";
|
|
170
|
+
for (const key of ["replayDays", "additions", "deletions", "files", "parents"]) if (!isCount(commit[key])) return `${key} is not a count`;
|
|
171
|
+
for (const [key, words] of Object.entries(CLOSED)) if (!Array.isArray(commit[key]) || commit[key].some((word) => !words.has(word))) return `a word outside the list in ${key}`;
|
|
172
|
+
for (const [key, shape] of Object.entries(SHAPED)) if (!Array.isArray(commit[key]) || commit[key].some((value) => typeof value !== "string" || !shape.test(value))) return `${key} is not markers`;
|
|
173
|
+
if (!commit.extensions || typeof commit.extensions !== "object" || Object.entries(commit.extensions).some(([key, n]) => !/^([A-Za-z0-9]{1,12}|\(none\))$/.test(key) || !isCount(n))) return "extensions are not counts by file type";
|
|
174
|
+
if (!VERSION.test(commit.classifier ?? "") || !VERSION.test(commit.markerVersion ?? "")) return "a version of the wrong shape";
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
export function leakIn(body, secrets) {
|
|
178
|
+
const text = JSON.stringify(body);
|
|
179
|
+
for (const repo of body.repos ?? []) for (const commit of repo.commits ?? []) { const refused = shapeRefusal(commit); if (refused) return refused; }
|
|
180
|
+
if (/(?<![0-9a-f])[0-9a-f]{40}(?![0-9a-f])/.test(text)) return "a sha";
|
|
181
|
+
for (const path of secrets.paths ?? []) if ((path.includes("/") || path.includes(".") || path.length >= 6) && text.includes(JSON.stringify(path).slice(1, -1))) return "a path";
|
|
182
|
+
for (const other of [...(secrets.remotes ?? []), ...(secrets.roots ?? [])]) if (other && text.includes(JSON.stringify(other).slice(1, -1))) return "a remote or a folder";
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The folders the AI apps' sessions ran in, newest first: Claude Code's transcripts name their `cwd`. */
|
|
187
|
+
export function transcriptCwds(home = homedir(), limit = LIMITS.transcripts) {
|
|
188
|
+
const root = join(home, ".claude", "projects");
|
|
189
|
+
const files = [];
|
|
190
|
+
try {
|
|
191
|
+
for (const project of readdirSync(root)) {
|
|
192
|
+
const dir = join(root, project);
|
|
193
|
+
try { for (const name of readdirSync(dir)) if (name.endsWith(".jsonl")) { const file = join(dir, name); files.push([file, statSync(file).mtimeMs]); } } catch { /* not a folder */ }
|
|
194
|
+
}
|
|
195
|
+
} catch { return []; }
|
|
196
|
+
const cwds = new Set();
|
|
197
|
+
const buffer = Buffer.alloc(65_536);
|
|
198
|
+
for (const [file] of files.sort((a, b) => b[1] - a[1]).slice(0, limit)) {
|
|
199
|
+
let fd = null;
|
|
200
|
+
try { fd = openSync(file, "r"); const read = readSync(fd, buffer, 0, buffer.length, 0); const match = /"cwd":"((?:[^"\\]|\\.)+)"/.exec(buffer.toString("utf8", 0, read)); if (match) cwds.add(JSON.parse(`"${match[1]}"`)); }
|
|
201
|
+
catch { /* unreadable: skipped */ }
|
|
202
|
+
finally { if (fd !== null) closeSync(fd); }
|
|
203
|
+
}
|
|
204
|
+
return [...cwds];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The person's own exclusions (~/.worktrust-counter.json `exclude`, the counter's file): a folder under one is never read. */
|
|
208
|
+
function excluded(dir, home) {
|
|
209
|
+
try { const list = JSON.parse(readFileSync(join(home, ".worktrust-counter.json"), "utf8")).exclude; return Array.isArray(list) && list.some((prefix) => typeof prefix === "string" && prefix && resolve(dir).startsWith(resolve(prefix))); } catch { return false; }
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ── this computer's answers ──
|
|
213
|
+
export function stateFile(home = homedir()) { return join(home, ".worktrust", "git.json"); }
|
|
214
|
+
export function loadState(home = homedir()) {
|
|
215
|
+
try { const state = JSON.parse(readFileSync(stateFile(home), "utf8")); if (state && typeof state === "object" && state.version === 1) return { version: 1, owner: typeof state.owner === "string" ? state.owner : null, emails: Array.isArray(state.emails) ? state.emails.filter((e) => typeof e === "string") : [], repos: state.repos && typeof state.repos === "object" ? state.repos : {} }; } catch { /* none yet */ }
|
|
216
|
+
return { version: 1, owner: null, emails: [], repos: {} };
|
|
217
|
+
}
|
|
218
|
+
export function saveState(state, home = homedir()) {
|
|
219
|
+
mkdirSync(join(home, ".worktrust"), { recursive: true });
|
|
220
|
+
writeFileSync(stateFile(home), `${JSON.stringify(state)}\n`, { mode: 0o600 });
|
|
221
|
+
try { chmodSync(stateFile(home), 0o600); } catch { /* best effort */ }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The work trees to offer: the folders the sessions named, plus the ones the person added; none they removed. */
|
|
225
|
+
export function discoverRepos(state, home = homedir()) {
|
|
226
|
+
const roots = new Set();
|
|
227
|
+
for (const cwd of transcriptCwds(home)) { if (excluded(cwd, home)) continue; const root = repoRootOf(cwd); if (root) roots.add(root); }
|
|
228
|
+
for (const [root, entry] of Object.entries(state.repos)) if (entry?.how === "typed" && existsSync(root)) roots.add(root);
|
|
229
|
+
return [...roots].sort();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** What each of the three ways in reads: the table the consent shows (and the Sources card, in the app's words). */
|
|
233
|
+
export const READS_TABLE = [
|
|
234
|
+
" GitHub Basic GitHub Full This computer (worktrust git)",
|
|
235
|
+
" GitHub permission checks, statuses + code (contents) none",
|
|
236
|
+
" deploys, actions",
|
|
237
|
+
" Commits no yes yes, your own (author e-mail)",
|
|
238
|
+
" File names no yes, as keys yes, as keys, here only",
|
|
239
|
+
" Code, diffs, messages never never never",
|
|
240
|
+
" Sent outcomes counts and keys counts and keys, no path, no sha",
|
|
241
|
+
];
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* `npx worktrust git [add <path> | remove <path> | list | emails [add|remove <e-mail>]] [--dry-run] [--yes]`.
|
|
245
|
+
* `io` lends the CLI's own: say, ask (Enter is No here), send(body) → the receipt or null, consent, salt, home.
|
|
246
|
+
*/
|
|
247
|
+
export async function gitMain(argv, io) {
|
|
248
|
+
const home = io.home ?? homedir();
|
|
249
|
+
const state = loadState(home);
|
|
250
|
+
const [sub, value, extra] = argv;
|
|
251
|
+
if (sub === "emails") {
|
|
252
|
+
if ((value === "add" || value === "remove") && extra) {
|
|
253
|
+
const email = extra.trim().toLowerCase();
|
|
254
|
+
if (!/^[^\s@]+@[^\s@]+$/.test(email)) { io.say(" Not an e-mail address. Nothing changed."); return 1; }
|
|
255
|
+
state.emails = value === "add" ? [...new Set([...state.emails, email])] : state.emails.filter((e) => e !== email);
|
|
256
|
+
if (!io.dryRun) saveState(state, home);
|
|
257
|
+
io.say(` ${value === "add" ? "Added" : "Removed"}. Commits by these e-mails are yours here: ${state.emails.join(", ") || "(only git's own user.email)"}`);
|
|
258
|
+
return 0;
|
|
259
|
+
}
|
|
260
|
+
io.say(` Your author e-mails beyond git's own user.email: ${state.emails.join(", ") || "none"}`);
|
|
261
|
+
io.say(" Add one: npx worktrust git emails add <e-mail>");
|
|
262
|
+
return 0;
|
|
263
|
+
}
|
|
264
|
+
if (sub === "add" || sub === "remove") {
|
|
265
|
+
const root = repoRootOf(resolve(value ?? "."));
|
|
266
|
+
if (!root) { io.say(" Not a git work tree. Nothing changed."); return 1; }
|
|
267
|
+
if (sub === "remove") { state.repos[root] = { ...(state.repos[root] ?? {}), how: "removed", agreed: false }; if (!io.dryRun) saveState(state, home); io.say(" Removed: nothing more is read or sent from it. What was sent stays on your record until you remove it under Sources."); return 0; }
|
|
268
|
+
state.repos[root] = { ...(state.repos[root] ?? {}), how: "typed", agreed: null };
|
|
269
|
+
if (!io.dryRun) saveState(state, home);
|
|
270
|
+
io.say(" Added.");
|
|
271
|
+
}
|
|
272
|
+
const roots = discoverRepos(state, home).filter((root) => state.repos[root]?.how !== "removed");
|
|
273
|
+
if (sub === "list") {
|
|
274
|
+
io.say(" Repositories WorkTrust may read on this computer:");
|
|
275
|
+
for (const root of roots) {
|
|
276
|
+
const entry = state.repos[root], id = repoIdentity(root, io.salt);
|
|
277
|
+
const status = entry?.agreed === true ? `sending${entry.sentThrough ? `, through ${entry.sentThrough.slice(0, 10)}` : ""}` : entry?.agreed === false ? "not agreed" : "not asked yet";
|
|
278
|
+
io.say(` ${root} (${id?.name ?? "by code"}) ${status}`);
|
|
279
|
+
}
|
|
280
|
+
io.say(` Author e-mails: git's user.email${state.emails.length ? `, ${state.emails.join(", ")}` : ""}`);
|
|
281
|
+
return 0;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// ── consent: once, with the list; what is not shown is not read ──
|
|
285
|
+
const unasked = roots.filter((root) => state.repos[root]?.agreed !== true && state.repos[root]?.agreed !== false);
|
|
286
|
+
if (unasked.length) {
|
|
287
|
+
io.say();
|
|
288
|
+
io.say(" WorkTrust can read your own commits in these repositories, on this computer:");
|
|
289
|
+
for (const root of unasked) io.say(` ${root}`);
|
|
290
|
+
io.say();
|
|
291
|
+
io.say(" What WorkTrust reads:");
|
|
292
|
+
for (const line of READS_TABLE) io.say(line);
|
|
293
|
+
io.say();
|
|
294
|
+
io.say(" Only commits whose author e-mail is yours. Sent: per commit its counts, kinds of files and markers,");
|
|
295
|
+
io.say(" never a file name, a message, code or the commit's id.");
|
|
296
|
+
const yes = await io.ask(`Send your own commits of these ${unasked.length} repositories?`, false);
|
|
297
|
+
for (const root of unasked) state.repos[root] = { ...(state.repos[root] ?? { how: "found" }), agreed: yes };
|
|
298
|
+
if (yes) io.consent?.agree("git-commits", "typed");
|
|
299
|
+
if (!io.dryRun) saveState(state, home);
|
|
300
|
+
}
|
|
301
|
+
const agreed = roots.filter((root) => state.repos[root]?.agreed === true);
|
|
302
|
+
if (!agreed.length) { io.say(" Nothing to send: no repository agreed. Add one: npx worktrust git add <folder>"); return 0; }
|
|
303
|
+
|
|
304
|
+
// ── where each repository is filed, and the owner the references are keyed with ──
|
|
305
|
+
const repos = agreed.map((root) => ({ root, identity: repoIdentity(root, io.salt) })).filter((repo) => repo.identity);
|
|
306
|
+
const label = (repo) => (repo.identity.name ? { name: repo.identity.name } : { code: repo.identity.code });
|
|
307
|
+
const where = io.dryRun ? null : await io.send({ repos: repos.map((repo) => ({ ...label(repo), commits: [] })) });
|
|
308
|
+
if (!io.dryRun && !where?.owner) { io.say(" WorkTrust could not be reached, or refused this computer. Nothing was sent."); return 1; }
|
|
309
|
+
const owner = where?.owner ?? state.owner ?? "00000000-0000-0000-0000-000000000000";
|
|
310
|
+
if (where?.owner) state.owner = where.owner;
|
|
311
|
+
let budget = LIMITS.perRun, landed = 0, already = 0, read = 0;
|
|
312
|
+
for (const repo of repos) {
|
|
313
|
+
if (budget <= 0) break;
|
|
314
|
+
const entry = state.repos[repo.root];
|
|
315
|
+
const key = repo.identity.name ?? `code:${repo.identity.code}`;
|
|
316
|
+
const projectRef = where?.projects?.[key] ?? null;
|
|
317
|
+
const emails = authorEmails(repo.root, state.emails);
|
|
318
|
+
// Incremental: from two days before the last commit sent (a late rebase), each commit once by its reference.
|
|
319
|
+
const since = entry.sentThrough ? new Date(Date.parse(entry.sentThrough) - 2 * 86_400_000).toISOString() : null;
|
|
320
|
+
const sent = new Set(entry.sent ?? []);
|
|
321
|
+
const commits = readCommits(repo.root, { emails, since, limit: Math.min(LIMITS.perRepo, budget) });
|
|
322
|
+
const fresh = commits.map((commit) => ({ commit, payload: commitPayload(commit, { owner, projectRef }) })).filter(({ payload }) => !sent.has(payload.ref.slice(0, 16)));
|
|
323
|
+
budget -= fresh.length; read += fresh.length;
|
|
324
|
+
if (io.dryRun) { io.say(` ${key}: ${fresh.length} commit(s) would be sent (nothing sent: --dry-run)`); continue; }
|
|
325
|
+
const since0 = repoSince(repo.root);
|
|
326
|
+
let through = entry.sentThrough ?? "";
|
|
327
|
+
for (let at = 0; at < fresh.length; at += LIMITS.perBatch) {
|
|
328
|
+
const part = fresh.slice(at, at + LIMITS.perBatch);
|
|
329
|
+
const body = { repos: [{ ...label(repo), ...(since0 ? { since: since0 } : {}), commits: part.map(({ payload }) => payload) }] };
|
|
330
|
+
const leak = leakIn(body, { paths: part.flatMap(({ commit }) => commit.paths), roots: [repo.root], remotes: [git(repo.root, "remote", "get-url", "origin").trim()] });
|
|
331
|
+
if (leak) { io.say(` Refused before sending: the batch would carry ${leak}. Nothing of ${key} was sent.`); return 1; }
|
|
332
|
+
const receipt = await io.send(body);
|
|
333
|
+
// Not landed, nothing lost: the mark does not move, so the next run reads and sends these commits again.
|
|
334
|
+
if (receipt?.refused) { io.say(` ${key}: not sent. ${receipt.refused.message}`); if (!io.dryRun) saveState(state, home); return 1; }
|
|
335
|
+
if (!receipt) { io.say(` ${key}: not sent (WorkTrust unreachable or refused); the next run tries again.`); if (!io.dryRun) saveState(state, home); return 1; }
|
|
336
|
+
landed += receipt.landed ?? 0; already += receipt.already ?? 0;
|
|
337
|
+
for (const { payload } of part) sent.add(payload.ref.slice(0, 16));
|
|
338
|
+
through = part.reduce((max, { payload }) => (payload.at > max ? payload.at : max), through);
|
|
339
|
+
state.repos[repo.root] = { ...state.repos[repo.root], sentThrough: through, sent: [...sent].slice(-5000) };
|
|
340
|
+
saveState(state, home);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (!io.dryRun) saveState(state, home);
|
|
344
|
+
io.say(io.dryRun ? ` ${read} commit(s) read; nothing sent.` : ` Sent: ${landed} commit(s) landed, ${already} already on your record through GitHub.`);
|
|
345
|
+
return 0;
|
|
346
|
+
}
|
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
|
+
}
|
package/log-session.mjs
CHANGED
|
@@ -981,7 +981,7 @@ function confirmedReceipt(answer, expectedCount = null) {
|
|
|
981
981
|
if (!Array.isArray(content) || content.length !== 1 || content[0].type !== "text") throw Error();
|
|
982
982
|
const receipt = JSON.parse(content[0].text);
|
|
983
983
|
if (expectedCount === null) {
|
|
984
|
-
if (!(receipt.receipt === "received" && ["accepted", "flagged", "pending"].includes(receipt.status)) && !(receipt.receipt === "unchanged" && receipt.reason === "duplicate")) throw Error();
|
|
984
|
+
if (!(receipt.receipt === "received" && ["accepted", "flagged", "pending"].includes(receipt.status)) && !(receipt.receipt === "unchanged" && (receipt.reason === "duplicate" || receipt.reason === "process_day_held"))) throw Error();
|
|
985
985
|
} else if (receipt.staged === true) {
|
|
986
986
|
// KEPT ON THE SERVER (0.10.27): a batch of a run WorkTrust holds and rebuilds itself; every line counted as kept or refused.
|
|
987
987
|
if (receipt.receipt !== "received" || receipt.received !== expectedCount || !Number.isInteger(receipt.kept) || receipt.kept < 0
|
|
@@ -1248,11 +1248,51 @@ async function send(door, payload) {
|
|
|
1248
1248
|
retainMeasurement(payload, entrySource.get(payload));
|
|
1249
1249
|
const body = JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "log_work", arguments: payload } });
|
|
1250
1250
|
const response = await fetch(door.url, { method: "POST", headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${door.token}`, ...clientOf(entrySource.get(payload)), ...proofFor(door.url, body) }, body });
|
|
1251
|
+
// A FAIR-USE LIMIT (0.10.33; owner, 2026-10-28): 429 with Retry-After. Nothing was stored; the line is kept and sent again after the delay.
|
|
1252
|
+
if (response.status === 429) throw Object.assign(new Error("door said 429"), { retryAfter: Math.max(1, Math.min(86_400, Number(response.headers.get("retry-after")) || 60)) });
|
|
1251
1253
|
if (!response.ok) throw new Error(`door said ${response.status}`);
|
|
1252
1254
|
const text = await response.text();
|
|
1253
1255
|
confirmedReceipt(text);
|
|
1254
1256
|
return text;
|
|
1255
1257
|
}
|
|
1258
|
+
/**
|
|
1259
|
+
* ONE LINE PER PROCESS PER DAY (0.10.33; owner, 2026-10-28; docs/FAIR-USE.md §1): a finished headless or scheduled run is folded
|
|
1260
|
+
* into its process's UTC day on disk (history-outbox.mjs) and the day goes as ONE line once it is over and quiet: live while the
|
|
1261
|
+
* door takes it, else through the history outbox; a refusal keeps it. A copy without that module sends each run as before.
|
|
1262
|
+
*/
|
|
1263
|
+
const PROCESS = await import("./history-outbox.mjs").then((m) => (m.foldProcessRun ? m : null), () => null);
|
|
1264
|
+
const isProcessRun = (stretch) => Boolean(PROCESS && stretch.timeline && (stretch.session === "headless" || stretch.session === "scheduled"));
|
|
1265
|
+
const processKey = (source, stretch, codex) => { const day = new Date(stretch.to).toISOString().slice(0, 10); return { ref: PROCESS.processRefOf({ source, kind: stretch.session, schedule: stretch.schedule?.ref ?? null, cwd: stretch.cwd, day }), day, source: source ?? "claude", kind: stretch.session, cwd: stretch.cwd ?? null, steered: stretch.steered ?? null, schedule: stretch.schedule ?? null, codex }; };
|
|
1266
|
+
// A RUN KEEPS WHAT RIDES ON ITS STEPS (release 9): its call outcomes and input lengths compacted with the timeline
|
|
1267
|
+
// (minuteCompactRun), and its subagent runs as read (wall-clock stamps), so the day's line carries them as each run's did.
|
|
1268
|
+
const processRun = (id, stretch) => { const compact = PROCESS.minuteCompactRun ? PROCESS.minuteCompactRun(stretch.timeline, stretch.from, stretch.outcomes ?? null, stretch.lengths ?? null) : { timeline: PROCESS.minuteCompact(stretch.timeline, stretch.from), outcomes: null, lengths: null };
|
|
1269
|
+
return { id: `${id}|${stretch.from}`, from: stretch.from, to: stretch.to, offset: stretch.offset, timeline: compact.timeline, outcomes: compact.outcomes, lengths: compact.lengths, agents: stretch.agents?.list ?? null, tokensIn: stretch.tokensIn, tokensOut: stretch.tokensOut, cacheRead: stretch.cacheRead, cacheWrite: stretch.cacheWrite, shapeKnown: stretch.shapeKnown, model: stretch.model ?? null }; };
|
|
1270
|
+
function processPayload(group, part, runs) {
|
|
1271
|
+
const payload = { ...payloadFor({ ...PROCESS.combineRuns(runs), cwd: group.cwd && existsSync(group.cwd) ? group.cwd : null, ref: PROCESS.lineRefOf(group.ref, part), steered: group.steered, session: group.kind, schedule: group.schedule ?? null, derived: null, live: null }), process_ref: group.ref, process_runs: runs.length };
|
|
1272
|
+
if (group.source && group.source !== "claude") entrySource.set(payload, group.source);
|
|
1273
|
+
return payload;
|
|
1274
|
+
}
|
|
1275
|
+
const liveWaiting = () => Date.parse(state.__liveRetryAt ?? "") > Date.now();
|
|
1276
|
+
const liveWait = (seconds) => { state.__liveRetryAt = new Date(Date.now() + seconds * 1000).toISOString(); LIVE_NOTED.n += 1; };
|
|
1277
|
+
const LIVE_NOTED = { n: 0 };
|
|
1278
|
+
async function sendProcessDays() {
|
|
1279
|
+
let n = 0;
|
|
1280
|
+
for (const due of PROCESS ? PROCESS.dueProcessDays(Date.now(), IDLE_BEFORE_SWEEP) : []) {
|
|
1281
|
+
const payload = processPayload(due, due.part, due.runs);
|
|
1282
|
+
if (DRY) { console.log(JSON.stringify({ ...payload, client: due.source, route: "process-day" }, null, 1)); continue; }
|
|
1283
|
+
if (Date.now() - Date.parse(payload.at) > WINDOW_HOURS * 3600_000 - IDLE_BEFORE_SWEEP) {
|
|
1284
|
+
const before = HISTORY_PERSISTED.n;
|
|
1285
|
+
await sendHistory(door, [payload]).catch(() => 0);
|
|
1286
|
+
if (HISTORY_PERSISTED.n > before) { PROCESS.markProcessSent(due.ref, due); n += 1; } else PROCESS.deferProcessDay(due.ref, 300);
|
|
1287
|
+
continue;
|
|
1288
|
+
}
|
|
1289
|
+
if (liveWaiting()) continue;
|
|
1290
|
+
// HELD IS NOT TAKEN (0.10.33): a day WorkTrust holds whole against this part keeps its runs here and asks again later.
|
|
1291
|
+
try { const held = /process_day_held/.test(await send(due.codex ? codexDoor ?? door : door, payload)); if (held) PROCESS.deferProcessDay(due.ref, 21_600); else { PROCESS.markProcessSent(due.ref, due); n += 1; } }
|
|
1292
|
+
catch (error) { PROCESS.deferProcessDay(due.ref, error.retryAfter ?? 300); if (error.retryAfter) liveWait(error.retryAfter); }
|
|
1293
|
+
}
|
|
1294
|
+
return n;
|
|
1295
|
+
}
|
|
1256
1296
|
|
|
1257
1297
|
/**
|
|
1258
1298
|
* THE MONTHS ALREADY ON THIS MACHINE, AS HISTORY.
|
|
@@ -1269,7 +1309,7 @@ async function send(door, payload) {
|
|
|
1269
1309
|
* average of one. Younger than the live window is left alone — that is `log_work`'s half.
|
|
1270
1310
|
*/
|
|
1271
1311
|
function historyEntries(floor = null, all = false) {
|
|
1272
|
-
const entries = [];
|
|
1312
|
+
const entries = [], processGroups = new Map();
|
|
1273
1313
|
for (const { file } of allTranscripts()) {
|
|
1274
1314
|
const lines = parsedLines(file);
|
|
1275
1315
|
if (!lines) continue;
|
|
@@ -1295,7 +1335,15 @@ function historyEntries(floor = null, all = false) {
|
|
|
1295
1335
|
// The live window belongs to the hook: a stretch that log_work may still report is not history.
|
|
1296
1336
|
if (!all && Date.now() - last < WINDOW_HOURS * 3600_000) continue;
|
|
1297
1337
|
// The same measure as a live day (seconds, layers, tokens, turns, model); history differs only in the door it goes through.
|
|
1298
|
-
const
|
|
1338
|
+
const stretch = { ...measure(cwd, messages, session), ref: stretchRef(file, day), steered, session, agents: agents.get(day) ?? null, live: liveOf(file, day) };
|
|
1339
|
+
// ONE LINE PER PROCESS PER DAY (0.10.33), as live: an autonomous run joins its process's day, sent below as one line.
|
|
1340
|
+
if (isProcessRun(stretch)) {
|
|
1341
|
+
const key = processKey(CODEX_ROLLOUT.test(file) ? "codex" : ANTIGRAVITY_TRANSCRIPT.test(file) ? "antigravity" : DATABASE_SESSION.test(file) ? file.slice(0, file.indexOf(":")) : null, stretch, false);
|
|
1342
|
+
const group = processGroups.get(key.ref) ?? { ...key, runs: [], file, from: stretch.from, folder: cwd ? String(cwd).split(/[\\/]/).filter(Boolean).pop() ?? null : null };
|
|
1343
|
+
group.runs.push(processRun(file, stretch)); processGroups.set(key.ref, group);
|
|
1344
|
+
continue;
|
|
1345
|
+
}
|
|
1346
|
+
const entry = payloadFor(stretch);
|
|
1299
1347
|
if (CODEX_ROLLOUT.test(file)) entrySource.set(entry, "codex");
|
|
1300
1348
|
if (ANTIGRAVITY_TRANSCRIPT.test(file)) entrySource.set(entry, "antigravity");
|
|
1301
1349
|
if (DATABASE_SESSION.test(file)) entrySource.set(entry, file.slice(0, file.indexOf(":")));
|
|
@@ -1303,6 +1351,7 @@ function historyEntries(floor = null, all = false) {
|
|
|
1303
1351
|
entries.push(entry);
|
|
1304
1352
|
}
|
|
1305
1353
|
}
|
|
1354
|
+
for (const group of processGroups.values()) PROCESS.processParts(group.runs).forEach((runs, i) => { const entry = processPayload(group, i + 1, runs); entryFile.set(entry, { file: group.file, from: group.from, folder: group.folder }); entries.push(entry); });
|
|
1306
1355
|
const seen = new Set(); // ONE SESSION FILE IN TWO PROJECT FOLDERS (a copied folder) IS ONE STRETCH (0.6.16, audit M6), as live and preserve count it
|
|
1307
1356
|
return entries.filter((entry) => !seen.has(entry.stretch_ref) && seen.add(entry.stretch_ref)).sort((a, b) => a.at.localeCompare(b.at));
|
|
1308
1357
|
}
|
|
@@ -1757,7 +1806,7 @@ if (work.length > 0 && input?.transcript_path && work[0].file === input.transcri
|
|
|
1757
1806
|
const found = timerSchedule(), secret = found ? repoCodeSecret() : null, timer = found && secret ? { cadence: found.cadence, ref: createHmac("sha256", secret).update(`worktrust-schedule-ref|${found.ref}`).digest("base64url") } : null; // the label's hash keyed with this computer's secret, as repo_code (L4)
|
|
1758
1807
|
if (timer &&state[work[0].id]?.schedule?.ref !== timer.ref) { state[work[0].id] = { ...(state[work[0].id] ?? {}), schedule: timer }; scheduleNoted = true; }
|
|
1759
1808
|
}
|
|
1760
|
-
let sent = 0, filed = 0, positioned = false;
|
|
1809
|
+
let sent = 0, filed = 0, folded = 0, positioned = false;
|
|
1761
1810
|
const late = []; // measured days the live door will no longer take: they are not dropped, they are filed
|
|
1762
1811
|
const heldBack = new Map(); // per transcript with a late day: its state before that day, put back unless history took every late day
|
|
1763
1812
|
for (const entry of work) {
|
|
@@ -1776,14 +1825,24 @@ for (const entry of work) {
|
|
|
1776
1825
|
// every day but today, on whichever run comes first — it no longer waits for its own end.
|
|
1777
1826
|
if (Date.now() - stretch.to < IDLE_BEFORE_SWEEP) break;
|
|
1778
1827
|
const total = grownBy(stretch, reported);
|
|
1779
|
-
|
|
1828
|
+
// THE TIMER'S SCHEDULE (0.10.32) travels with the run, so a scheduled process-day is its own process and its line names it.
|
|
1829
|
+
const timed = { ...stretch, schedule: state[entry.id]?.schedule ?? null };
|
|
1830
|
+
// ONE LINE PER PROCESS PER DAY (0.10.33): a finished autonomous run waits on disk for its day's line; the mark passes it once it is there.
|
|
1831
|
+
if (isProcessRun(timed)) {
|
|
1832
|
+
if (DRY) { console.log(JSON.stringify({ process_run: true, session_kind: timed.session, stretch_ref: timed.ref ?? null, at: new Date(timed.to).toISOString() })); through = timed.to; reported = total; continue; }
|
|
1833
|
+
try { PROCESS.foldProcessRun(processKey(LIVE_SOURCE.exec(String(entry.id))?.[1], timed, String(entry.id).startsWith("codex:")), processRun(entry.id, timed)); through = timed.to; reported = total; folded += 1; continue; } catch { break; }
|
|
1834
|
+
}
|
|
1835
|
+
const payload = payloadFor(timed);
|
|
1780
1836
|
const source = LIVE_SOURCE.exec(String(entry.id))?.[1];
|
|
1781
1837
|
if (source) entrySource.set(payload, source);
|
|
1782
1838
|
if (DRY) { console.log(JSON.stringify({ ...payload, client: source ?? "claude", route: Date.now() - stretch.to > WINDOW_HOURS * 3600_000 ? "history" : "log_work" }, null, 1)); through = stretch.to; reported = total; continue; }
|
|
1783
1839
|
// MEASURED TIME IS NEVER DROPPED. The live door refuses anything older than two days, which is right, and used to mean a machine closed
|
|
1784
1840
|
// for a fortnight lost the fortnight. The same numbers go through the history door instead, standing as history stands. Late is not untrue.
|
|
1785
1841
|
if (Date.now() - stretch.to > WINDOW_HOURS * 3600_000) { before ??= { mark: through === null ? state[entry.id] : { ...state[entry.id], through: new Date(through).toISOString() } }; late.push(payload); through = stretch.to; reported = total; continue; }
|
|
1842
|
+
// A LIMIT SAID WHEN (0.10.33): while WorkTrust asked to wait, nothing is sent; the mark stays and the next run sends it.
|
|
1843
|
+
if (liveWaiting()) break;
|
|
1786
1844
|
try { await send(doorFor(entry), payload); through = stretch.to; reported = total; sent += 1; } catch (error) {
|
|
1845
|
+
if (error.retryAfter) liveWait(error.retryAfter);
|
|
1787
1846
|
if (process.env.WORKTRUST_HOOK_DEBUG) console.error(`worktrust: ${error.message}`);
|
|
1788
1847
|
break;
|
|
1789
1848
|
}
|
|
@@ -1803,7 +1862,9 @@ if (!DRY && late.length > 0) {
|
|
|
1803
1862
|
// session whose first late day waited (the owner's, from 2026-10-18) never moved again.
|
|
1804
1863
|
if (filed < late.length && HISTORY_PERSISTED.n < late.length) for (const [id, mark] of heldBack) { if (mark) state[id] = mark; else delete state[id]; }
|
|
1805
1864
|
}
|
|
1806
|
-
|
|
1865
|
+
// THE PROCESS-DAYS THAT ARE OVER (0.10.33): one line each; what WorkTrust does not take now stays on disk for the next run.
|
|
1866
|
+
await sendProcessDays();
|
|
1867
|
+
if (!DRY && (sent > 0 || filed > 0 || folded > 0 || scheduleNoted || positioned || LIVE_NOTED.n > 0 || (late.length > 0 && HISTORY_PERSISTED.n >= late.length))) writeState(state);
|
|
1807
1868
|
// What an earlier run left in the outbox because the server never received it: a few batches a run, by itself; not in a run
|
|
1808
1869
|
// that just sent late days itself (a batch it just tried waits for the next run). Since 0.10.32 the mark passes a late day the
|
|
1809
1870
|
// outbox keeps, so the next run has no late day of its own and asks; before, a long session always had one and never asked.
|