worktrust 0.10.33 → 0.10.35
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 +30 -0
- package/commit-rules.mjs +447 -0
- package/engine-release.json +1 -1
- package/git-commits.mjs +346 -0
- package/history-outbox.mjs +5 -2
- package/log-session.mjs +5 -5
- package/package-release.json +1 -1
- package/package.json +3 -1
- package/session-databases.mjs +90 -1
- package/transcript-readers.mjs +14 -0
- package/worktrust.mjs +58 -8
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
|
@@ -73,7 +73,10 @@ export function historyRefusal(text,response) {
|
|
|
73
73
|
// A staged run WorkTrust closed or refused (0.10.27: another computer's key, seven days without its last batch) is final.
|
|
74
74
|
const closed=rpc?.id===1 && String(rpc?.error?.message??'').startsWith('history run refused:');
|
|
75
75
|
// 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
|
-
|
|
76
|
+
// THE SERVER'S OWN SENTENCE (0.10.35): a re-measure refused for older measure rules says why and the one command that helps;
|
|
77
|
+
// the CLI prints it instead of guessing (one line, control characters taken out, at most 400 characters).
|
|
78
|
+
const said=closed&&rpc?.error?.data?.reason==='measure_rules'?String(rpc.error.message).replace(/[\u0000-\u001f\u007f-\u009f]/g,' ').slice(0,400):null;
|
|
79
|
+
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}:{})};
|
|
77
80
|
}
|
|
78
81
|
|
|
79
82
|
/** Explicit recovery, even after the AI client removed its source files. A changed account/key
|
|
@@ -151,7 +154,7 @@ export async function resumeHistory(target, {send, confirmed, note, retryReview=
|
|
|
151
154
|
try{if(!response.ok)throw Error();confirmed(text,item.entries.length);}
|
|
152
155
|
catch{
|
|
153
156
|
const refusal=historyRefusal(text,response);
|
|
154
|
-
if(refusal.reason==='closed'){queued.close();totals.refused+=item.entries.length;continue;}
|
|
157
|
+
if(refusal.reason==='closed'){queued.close();totals.refused+=item.entries.length;if(refusal.said)totals.said=refusal.said;continue;}
|
|
155
158
|
// Too large (0.10.29): marked and taken again at once, by the cut, never resent as it was. Without a cutter it waits.
|
|
156
159
|
if(refusal.reason==='oversize'){queued.oversize();if(cutter){seen.delete(id);queue.unshift(id);}else totals.waiting+=item.entries.length;continue;}
|
|
157
160
|
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;
|
package/log-session.mjs
CHANGED
|
@@ -242,7 +242,7 @@ const { modCounts = () => null } = (await import("./claude-mod.mjs").catch(() =>
|
|
|
242
242
|
const liveOf = (file, day) => modCounts(String(file).split(/[\\/]/).pop().replace(/\.jsonl$/, ""), day) ?? codexLive(file, day); // 0.10.16: a Codex rollout's own
|
|
243
243
|
const { stackOfFiles = () => [], displayStackOfFiles = () => [] } = (await import("./stack-rules.mjs").catch(() => null)) ?? {}; // 0.10.11: core's stack rules and the display-only keys (tools_seen), generated (build-stack-rules --check); absent → nothing named, never a guess
|
|
244
244
|
const EXPORT_PATHS = []; for (let i = 0; i < args.length; i += 1) if (args[i] === "--export" && args[i + 1]) EXPORT_PATHS.push(args[i + 1]);
|
|
245
|
-
const DATABASE_CLIENTS = ["hermes", "goose", "opencode", "openclaw", "cursor", "copilot"]; // the registry's keys, the name each line carries
|
|
245
|
+
const DATABASE_CLIENTS = ["hermes", "goose", "opencode", "openclaw", "cursor", "copilot", "gemini-cli"]; // the registry's keys, the name each line carries (0.10.35: Gemini CLI)
|
|
246
246
|
const LIVE_SOURCE = new RegExp(`^(codex|antigravity|${DATABASE_CLIENTS.join("|")}):`); // a sweep entry's id → the client it names (every database client, 0.6.16)
|
|
247
247
|
const READER_FILES = ["history-outbox.mjs", "transcript-readers.mjs", "session-databases.mjs", "stretch-evidence.mjs", "chat-exports.mjs", "stack-rules.mjs", "claude-mod.mjs"]; // 0.7.4: the derived record travels with the hook; 0.10.10: the export reader; 0.10.11: the stack rules; 0.10.15: the mod
|
|
248
248
|
/** The roots this machine's transcripts live under; a test names its own. */
|
|
@@ -1177,7 +1177,7 @@ async function sendHistory(door, entries, announce = false, rebuild = false, nam
|
|
|
1177
1177
|
catch {
|
|
1178
1178
|
const refusal = historyRefusal(text, response);
|
|
1179
1179
|
// 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; continue; }
|
|
1180
|
+
if (refusal.reason === "closed") { queued.close(); HISTORY_DELIVERY.refused += batch.length; if (refusal.said) HISTORY_DELIVERY.said = refusal.said; continue; }
|
|
1181
1181
|
if (refusal.reason === "oversize") { queued.oversize(); handLater(item); if (item.run) stoppedRuns.add(item.run.id); continue; }
|
|
1182
1182
|
queued.defer(refusal.reason, refusal.seconds);
|
|
1183
1183
|
if (refusal.reason === "review") { HISTORY_DELIVERY.review += batch.length; continue; }
|
|
@@ -1191,7 +1191,7 @@ async function sendHistory(door, entries, announce = false, rebuild = false, nam
|
|
|
1191
1191
|
try {
|
|
1192
1192
|
const totals = await resumeHistory(target, { only: ids, cutter: outboxCut, retryReview: flag("retry-review"), confirmed: confirmedReceipt, note: noteReceipt, send: sendQueued(target) });
|
|
1193
1193
|
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;
|
|
1194
|
+
HISTORY_DELIVERY.waiting += totals.waiting; HISTORY_DELIVERY.review += totals.review; HISTORY_DELIVERY.refused += totals.refused; if (totals.said) HISTORY_DELIVERY.said = totals.said;
|
|
1195
1195
|
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
1196
|
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
1197
|
} catch { console.error("Pending history could not be read safely. Nothing was marked complete; keep the local WorkTrust folder for recovery."); }
|
|
@@ -1749,9 +1749,9 @@ if (flag("history")) {
|
|
|
1749
1749
|
return await exitFlushed(0);
|
|
1750
1750
|
}
|
|
1751
1751
|
// ONLY THE DAYS THAT CHANGED (0.10.32): a re-measure of this computer's own sessions asks the door first; an export, a copied
|
|
1752
|
-
// folder and a plain history never do. Not known is every day.
|
|
1752
|
+
// folder and a plain history never do. Not known is every day. `--force-days` (0.10.35) asks nothing and sends every day of every AI app again.
|
|
1753
1753
|
const staged = flag("rebuild") && !fromExports && !fromFolder;
|
|
1754
|
-
const sending = staged ? await changedOnly(entries) : entries;
|
|
1754
|
+
const sending = staged && !flag("force-days") ? await changedOnly(entries) : entries; // FORCED (0.10.35; owner, 2026-10-30): the lines WorkTrust holds without their app, named; every app at once, so a day loses no app's line
|
|
1755
1755
|
if (staged && COMPARED.asked && COMPARED.days > COMPARED.changed) console.log(sending.length === 0 ? `nothing changed: WorkTrust holds all ${COMPARED.days} days as this computer measures them` : `${COMPARED.days - COMPARED.changed} of ${COMPARED.days} days unchanged on WorkTrust; sending the ${COMPARED.changed} that changed`);
|
|
1756
1756
|
// A batch the door refused is a failure the caller must see, not a quiet zero.
|
|
1757
1757
|
const accepted = sending.length === 0 ? 0 : await sendHistory(door, sending, true, flag("rebuild"), Boolean(fromFolder), fromExports ? { byDay: true, withdrawUnknown: flag("withdraw-unknown") } : {});
|
package/package-release.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"keyId":"608168ccdef72562fc91f9c0f9f2c2d6876bece7f7362cb545b818f7947da6b5","payload":{"files":{"AGENTS.md":"c6f5280c5bf81a14db4bdc282218fb005706e92dd9a9170c0e727716ee91685a","CLAUDE.md":"5933504f93e5a40369a4790e96dd5dad791d849d3178b1833eb64088e456aaeb","LICENSE":"3096ed1693974a90e1f4f59bf01c9559d94a7941d933e36fb81d3995a97f25c1","README.md":"
|
|
1
|
+
{"keyId":"608168ccdef72562fc91f9c0f9f2c2d6876bece7f7362cb545b818f7947da6b5","payload":{"files":{"AGENTS.md":"c6f5280c5bf81a14db4bdc282218fb005706e92dd9a9170c0e727716ee91685a","CLAUDE.md":"5933504f93e5a40369a4790e96dd5dad791d849d3178b1833eb64088e456aaeb","LICENSE":"3096ed1693974a90e1f4f59bf01c9559d94a7941d933e36fb81d3995a97f25c1","README.md":"9774f45499ba5fd3db19135ddd3c9606f0744db11556acf7a41100bd2c0bd9d4","SECURITY.md":"b672649c7c7bf18538dd4560d7b533498721e0be765a000796d7d18390b5cbb6","chat-exports.mjs":"04eafe333940ec03d5058d8d51062124aa12ab882bf04db03bcda5613ec69298","claude-mod.mjs":"02d86b0cdddf39879efc5194c0a5be6b41456faedec28963dc123a4e88dc3942","commit-rules.mjs":"e94cc32a5415f6d9e978145ab9505ac4ae0bac9431e6e9104b9b37155260b51f","config-edits.mjs":"0eba2f176ef6f7afba071d6e00de0ff19ba2b283180a801dfd957aa38c1edd94","count-behaviour.mjs":"1beaec2a4d0aead76d166f89cc52441a9cf48f2e69da9888ca2091dd3a10a37b","credential-store.mjs":"a4b3540a61f4ce19e535c7f1d260c0d817686cd066d559a76db2ab108144abba","credential-vault-scripts.mjs":"7e42fe1db3885c2dec6f1de87bebfbefe2292d29179a68f3da87d19eb6c7704b","credential-vault.mjs":"279baabb217e2a3f170e8d7f2b271a88484531f9031c441360ea91561a70da99","engine-release.json":"982e696f36761c655ab7696cf920bafac1ebe0c59d040b7219d510523fc346eb","git-commits.mjs":"f9c084c54937a86df8fe30a0db2a58e04eeef0dbae69c54b8e368efc9964d6d8","history-outbox.mjs":"10cdcdcc595fbc3222e39b3d845a2ff16c0e7ae20eb43a2d2794e8c4d3a7353e","log-session.mjs":"378a2fd38f4eafd8820a85e4bc4490929ceed59c2711e5cfef6e01e2e195ad3d","orchestration-contract.mjs":"ec08f303a166365b99e70b675de4cfe76c6156c2567e14b5569f6d992f0231b7","orchestration-evidence.mjs":"d0d06cecf0eb08e5c6e653c62a4f45b1eddf2119c2acb8341056b34f488d214a","orchestration-report.mjs":"d4e0a9ad8e4a01b462d22c6b1866aac291565c533ff476085d3367cb6d59bc1b","package-integrity.mjs":"a8210c9494a56bfb9a5ea3d10064c7a269f0b6330c0af7c0700e706c3c0224e3","package.json":"f357f15ad0a9bf85b82371dabccc20a15a38c040cc7cd1aad60e58fd7edfdca6","preserve-lines.mjs":"2df64a69687aa51219bf7b86b88d175cc50990fa29574b31897e41a9ebeecf43","preserve-summary.mjs":"137f781ed7021596a3f0ea67afcf2ab8616ac293f833dbb8f176e838e0ed8321","preserve.mjs":"3e369083ba43bcda2fa0622fd526aa64fcf8ad66e7b6e8f23821752c4d47ad27","runtime-install.mjs":"aab47b52c79d726ef428a24f982c648fa3f1c2dc2b574f633611d02924864e5f","session-databases.mjs":"b9fe20d7445205bdbf4cf2eaaf9cc770dad01e23d41e644b04930ca34e10426d","setup-mcp.mjs":"0c29113179561af1c1e9ff8b7a786e7240edf48a2244993e12335e5ee362c812","stack-rules.mjs":"b23c56e72ec56ba02b17ed895f869172d988c468a697e8d6026b0e268f61e489","stretch-evidence.mjs":"5f5667b92adf11b1a5a6330a0f26dd6db73d56fe60823d9a5246dab71d27d380","transcript-readers.mjs":"e9f20415c2b5d62a09f35a63075002ebd1da56edb32f242d52822d58ecbd8ac3","worktrust-launch.mjs":"84801bd29d472d48188c126b9ebe3989a3132b54e13cc4fe534ff7c383a294ee","worktrust.mjs":"2a3ea97efabb582ebdc78bcb772e78a5aae06c243a5092563c88904df93003b9"},"format":"worktrust-package/1","kind":"npm","version":"0.10.35"},"signature":"zBGC2wIWsTuBDq7bcmDRreaEYyxq59m78Cd8yfiLHx_s4gMOqj2ZMeXON5ePQgB_OhsB1Esf2JWLpR_xrGJyCQ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "worktrust",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.35",
|
|
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": {
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
"claude-mod.mjs",
|
|
33
33
|
"package-integrity.mjs",
|
|
34
34
|
"worktrust-launch.mjs",
|
|
35
|
+
"git-commits.mjs",
|
|
36
|
+
"commit-rules.mjs",
|
|
35
37
|
"engine-release.json",
|
|
36
38
|
"package-release.json",
|
|
37
39
|
"history-outbox.mjs",
|
package/session-databases.mjs
CHANGED
|
@@ -12,7 +12,7 @@ import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
|
12
12
|
import * as zlib from "node:zlib"; // zstd since Node 22.15; older Node reads OpenClaw's uncompressed rows only
|
|
13
13
|
import { homedir, platform } from "node:os";
|
|
14
14
|
import { join } from "node:path";
|
|
15
|
-
import { cleanModel, cursorCall, copilotCall } from "./transcript-readers.mjs";
|
|
15
|
+
import { cleanModel, cursorCall, copilotCall, geminiCall } from "./transcript-readers.mjs";
|
|
16
16
|
|
|
17
17
|
// ── hermes session reader ──
|
|
18
18
|
/**
|
|
@@ -450,6 +450,94 @@ export function copilotLines(pseudo) {
|
|
|
450
450
|
}
|
|
451
451
|
// ── end copilot session reader ──
|
|
452
452
|
|
|
453
|
+
// ── gemini-cli session reader ──
|
|
454
|
+
/**
|
|
455
|
+
* GEMINI CLI (0.10.35; owner, 2026-10-30). Gemini CLI records every chat itself (ChatRecordingService,
|
|
456
|
+
* google-gemini/gemini-cli packages/core/src/services/chatRecordingService.ts and config/storage.ts, read 2026-10-30): one
|
|
457
|
+
* file per session under `<global temp dir>/<project id>/chats/`, the global temp dir `~/.gemini/tmp`; the file is
|
|
458
|
+
* `session-<YYYY-MM-DDTHH-MM>-<8 characters of the session id>.jsonl`, a record per line, or a whole `.json`
|
|
459
|
+
* ConversationRecord from before the JSONL form. A subagent's session is a folder under its parent's
|
|
460
|
+
* (`chats/<parent id>/<id>.jsonl`): its time lies inside its parent's and it is not read. The records are replayed as
|
|
461
|
+
* Gemini CLI's own loader does: the first line the session's metadata; a message (`id`, `timestamp`, `type` user | gemini |
|
|
462
|
+
* info | error | warning) re-appended whole when its tokens or tool calls arrive, the last one per id kept; `$rewindTo`
|
|
463
|
+
* drops that message and everything after it; `$patch` may remove or reorder messages; `$set` merges metadata (an old
|
|
464
|
+
* `messages` in it replaces them all). Read for clocks and counts only: a `user` message is the person's turn; a `gemini`
|
|
465
|
+
* message the model's, with its `model` and its tokens (`input` the prompt, of which `cached` came from the cache, `output`,
|
|
466
|
+
* `thoughts`, `tool`); each tool call its name, status and clock (a shell command's line for the CLI to name a kind of,
|
|
467
|
+
* never kept). The `content`, the thoughts, `displayContent`, the tool calls' other arguments and results, `summary` and
|
|
468
|
+
* `directories` are parsed with the line and never kept. The project folder is not read (its directory under tmp is Gemini
|
|
469
|
+
* CLI's own short id, and a path never leaves): these lines name no repository.
|
|
470
|
+
* NOT VERIFIED on a real install here: the paths and records are read from Gemini CLI's source; the tests run on fixtures
|
|
471
|
+
* of that shape.
|
|
472
|
+
*/
|
|
473
|
+
export function geminiHomes(given) { return given ? [given.replace(/^~(?=\/|$)/, homedir())] : [join(homedir(), ".gemini")]; }
|
|
474
|
+
export function* geminiSessions(homes) {
|
|
475
|
+
for (const home of homes) {
|
|
476
|
+
let projects = []; try { projects = readdirSync(join(home, "tmp")); } catch { continue; }
|
|
477
|
+
for (const project of projects.sort()) {
|
|
478
|
+
const dir = join(home, "tmp", project, "chats");
|
|
479
|
+
let names = []; try { names = readdirSync(dir); } catch { continue; }
|
|
480
|
+
for (const name of names.sort()) {
|
|
481
|
+
if (!/^session-.*\.jsonl?$/.test(name)) continue;
|
|
482
|
+
// A session migrated to .jsonl may leave its .json beside it: the .jsonl is the session.
|
|
483
|
+
if (name.endsWith(".json") && names.includes(`${name}l`)) continue;
|
|
484
|
+
const id = name.replace(/\.jsonl?$/, "");
|
|
485
|
+
if (!/^[A-Za-z0-9_.:-]{1,120}$/.test(id)) continue;
|
|
486
|
+
let writtenAt = 0; try { const st = statSync(join(dir, name)); if (!st.isFile()) continue; writtenAt = st.mtimeMs; } catch { continue; }
|
|
487
|
+
yield { sessionId: id, file: `gemini-cli:${join(dir, name)}#${id}`, cwd: null, writtenAt };
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
/** A session file back into its messages, in order: the JSONL records replayed (the loader's rules), or the old whole file. */
|
|
493
|
+
function geminiMessages(text, jsonl) {
|
|
494
|
+
if (!jsonl) { const whole = JSON.parse(text); return Array.isArray(whole?.messages) ? whole.messages : []; }
|
|
495
|
+
let messages = new Map();
|
|
496
|
+
for (const raw of text.split("\n")) {
|
|
497
|
+
if (!raw.trim()) continue;
|
|
498
|
+
let record; try { record = JSON.parse(raw); } catch { continue; }
|
|
499
|
+
if (!record || typeof record !== "object") continue;
|
|
500
|
+
if (typeof record.$rewindTo === "string") {
|
|
501
|
+
const ids = [...messages.keys()], at = ids.indexOf(record.$rewindTo);
|
|
502
|
+
messages = new Map(at < 0 ? [] : ids.slice(0, at).map((id) => [id, messages.get(id)]));
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
if (record.$patch && typeof record.$patch === "object") {
|
|
506
|
+
for (const id of Array.isArray(record.$patch.removeIds) ? record.$patch.removeIds : []) messages.delete(id);
|
|
507
|
+
if (Array.isArray(record.$patch.orderIds)) { const order = record.$patch.orderIds.filter((id) => messages.has(id)); messages = new Map([...order, ...[...messages.keys()].filter((id) => !order.includes(id))].map((id) => [id, messages.get(id)])); }
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (record.$set && typeof record.$set === "object") { if (Array.isArray(record.$set.messages)) messages = new Map(record.$set.messages.filter((m) => typeof m?.id === "string").map((m) => [m.id, m])); continue; }
|
|
511
|
+
if (typeof record.id === "string" && typeof record.type === "string") messages.set(record.id, record);
|
|
512
|
+
}
|
|
513
|
+
return [...messages.values()];
|
|
514
|
+
}
|
|
515
|
+
export function geminiLines(pseudo) {
|
|
516
|
+
const { file } = splitSession(pseudo);
|
|
517
|
+
let messages; try { messages = geminiMessages(readFileSync(file, "utf8"), file.endsWith(".jsonl")); } catch { return null; }
|
|
518
|
+
const lines = [];
|
|
519
|
+
const count = (value) => (Number.isFinite(Number(value)) && Number(value) > 0 ? Math.round(Number(value)) : 0);
|
|
520
|
+
for (const message of messages) {
|
|
521
|
+
const timestamp = isoOf(message?.timestamp); if (!timestamp) continue;
|
|
522
|
+
if (message.type === "user") { lines.push({ type: "user", timestamp, message: { content: [{ type: "text" }] } }); continue; }
|
|
523
|
+
if (message.type !== "gemini") continue;
|
|
524
|
+
// The prompt's count holds what the cache served: new input is the rest (and the tool-use prompt), the cache read apart;
|
|
525
|
+
// a thought is output, as every other reader counts reasoning.
|
|
526
|
+
const t = message.tokens && typeof message.tokens === "object" ? message.tokens : null;
|
|
527
|
+
const usage = t ? { usage: { input_tokens: Math.max(0, count(t.input) - count(t.cached)) + count(t.tool), output_tokens: count(t.output) + count(t.thoughts), cache_read_input_tokens: count(t.cached), cache_creation_input_tokens: 0 } } : {};
|
|
528
|
+
for (const raw of Array.isArray(message.toolCalls) ? message.toolCalls : []) {
|
|
529
|
+
const call = geminiCall(raw), at = isoOf(raw?.timestamp) ?? timestamp;
|
|
530
|
+
if (!call) continue;
|
|
531
|
+
lines.push({ type: "assistant", timestamp: at, message: { content: [call.use] } });
|
|
532
|
+
lines.push({ type: "user", timestamp: at, message: { content: [call.result] } });
|
|
533
|
+
}
|
|
534
|
+
lines.push({ type: "assistant", timestamp, message: { model: cleanModel(message.model), ...usage, content: [] } });
|
|
535
|
+
}
|
|
536
|
+
// Each line at its own clock: a tool call's moment may fall before or after the message that carries it.
|
|
537
|
+
return lines.map((line, at) => ({ line, at })).sort((a, b) => Date.parse(a.line.timestamp) - Date.parse(b.line.timestamp) || a.at - b.at).map(({ line }) => line);
|
|
538
|
+
}
|
|
539
|
+
// ── end gemini-cli session reader ──
|
|
540
|
+
|
|
453
541
|
// ── database clients ──
|
|
454
542
|
/** The clients whose sessions live in a database, one registry: the hook and the counter ask it, not each client. */
|
|
455
543
|
const DATABASE_CLIENTS = {
|
|
@@ -459,6 +547,7 @@ const DATABASE_CLIENTS = {
|
|
|
459
547
|
openclaw: { homes: openclawHomes, sessions: openclawSessions, lines: openclawLines },
|
|
460
548
|
cursor: { homes: cursorHomes, sessions: cursorSessions, lines: cursorLines },
|
|
461
549
|
copilot: { homes: copilotHomes, sessions: copilotSessions, lines: copilotLines },
|
|
550
|
+
"gemini-cli": { homes: geminiHomes, sessions: geminiSessions, lines: geminiLines },
|
|
462
551
|
};
|
|
463
552
|
export const DATABASE_SESSION = new RegExp(`^(${Object.keys(DATABASE_CLIENTS).join("|")}):.+#[A-Za-z0-9_.:-]{1,120}$`);
|
|
464
553
|
/** Every attended session of every database client; `given` names a client's own home (a test), `onlyGiven` reads no other. */
|
package/transcript-readers.mjs
CHANGED
|
@@ -453,6 +453,20 @@ export function cursorCall(bubble) {
|
|
|
453
453
|
* a kind of, never kept) with the exit code VS Code recorded; a missing exit code is unknown. Its to-do list is TodoWrite
|
|
454
454
|
* (statuses only), its subagents are Agent. What a confirmation type means is not documented, so no refusal is read.
|
|
455
455
|
*/
|
|
456
|
+
/**
|
|
457
|
+
* A GEMINI CLI TOOL CALL AS THE STRETCH READS IT (0.10.35): its name mapped to the common one (a shell command is Bash, its
|
|
458
|
+
* to-do list TodoWrite), the command for the CLI to name a KIND of (never kept), the to-do statuses (never their text), and
|
|
459
|
+
* the status Gemini CLI recorded (`success`; `error` or `cancelled` a failure); any other status is unknown, never a pass.
|
|
460
|
+
*/
|
|
461
|
+
const GEMINI_NAMES = { run_shell_command: "Bash", write_todos: "TodoWrite", replace: "Edit", write_file: "Write", read_file: "Read", read_many_files: "Read", search_file_content: "Grep", grep_search: "Grep", glob: "Glob", list_directory: "LS" };
|
|
462
|
+
export function geminiCall(call) {
|
|
463
|
+
const id = typeof call?.id === "string" && call.id ? call.id : null;
|
|
464
|
+
if (!id) return null;
|
|
465
|
+
const raw = String(call.name ?? "tool"), name = GEMINI_NAMES[raw] ?? raw, args = call.args && typeof call.args === "object" ? call.args : {};
|
|
466
|
+
const input = name === "Bash" ? { command: typeof args.command === "string" ? args.command : "" } : name === "TodoWrite" && Array.isArray(args.todos) ? { todos: args.todos.map((todo) => ({ status: todo?.status === "completed" ? "completed" : "pending" })) } : {};
|
|
467
|
+
const failed = call.status === "error" || call.status === "cancelled", known = failed || call.status === "success";
|
|
468
|
+
return { use: { type: "tool_use", id, name, input }, result: { type: "tool_result", tool_use_id: id, is_error: failed, ...(known ? {} : { outcome: "unknown" }), content: "" } };
|
|
469
|
+
}
|
|
456
470
|
const COPILOT_NAMES = { run_in_terminal: "Bash", manage_todo_list: "TodoWrite", execution_subagent: "Agent", runSubagent: "Agent", copilot_replaceString: "Edit", copilot_createFile: "Write", copilot_readFile: "Read", copilot_findTextInFiles: "Grep", copilot_findFiles: "Glob" };
|
|
457
471
|
export function copilotCall(part) {
|
|
458
472
|
const id = typeof part.toolCallId === "string" && part.toolCallId ? part.toolCallId : null;
|