@raibitsi/tiresias 1.0.0 → 1.1.0

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +199 -0
  2. package/README.md +50 -27
  3. package/dist/background.js +6 -3
  4. package/dist/browsecli.js +4 -3
  5. package/dist/cdp.js +22 -1
  6. package/dist/cli.js +82 -29
  7. package/dist/config.js +32 -26
  8. package/dist/confirm.js +16 -7
  9. package/dist/cooldown.js +65 -34
  10. package/dist/dailylog.js +11 -5
  11. package/dist/engine.js +5 -4
  12. package/dist/envelope.js +20 -2
  13. package/dist/fetcher.js +5 -0
  14. package/dist/filereply.js +7 -1
  15. package/dist/files.js +4 -4
  16. package/dist/formflow.js +21 -3
  17. package/dist/forms.js +257 -34
  18. package/dist/getactions.js +99 -0
  19. package/dist/history.js +8 -5
  20. package/dist/hook.js +2 -0
  21. package/dist/hosts.js +15 -13
  22. package/dist/inside.js +2 -2
  23. package/dist/inspect.js +4 -1
  24. package/dist/keys.js +420 -18
  25. package/dist/mcp.js +10 -10
  26. package/dist/needs.js +9 -5
  27. package/dist/parentwatch.js +6 -16
  28. package/dist/pdf.js +4 -1
  29. package/dist/pdfchild.js +0 -2
  30. package/dist/pdfproc.js +26 -11
  31. package/dist/pdfwatch.js +14 -0
  32. package/dist/pipeline.js +25 -8
  33. package/dist/profiles.js +3 -3
  34. package/dist/queue.js +15 -7
  35. package/dist/reader.js +43 -18
  36. package/dist/receipts.js +2 -2
  37. package/dist/render.js +103 -24
  38. package/dist/resume.js +17 -8
  39. package/dist/rules.js +29 -7
  40. package/dist/safebrowsing.js +3 -3
  41. package/dist/safety.js +9 -3
  42. package/dist/sandbox.js +1 -1
  43. package/dist/saved.js +108 -60
  44. package/dist/savedlock.js +208 -0
  45. package/dist/settings.js +7 -3
  46. package/dist/shelf.js +1 -1
  47. package/dist/sites.js +9 -4
  48. package/dist/slips.js +43 -1
  49. package/dist/store.js +119 -25
  50. package/dist/tabs.js +1 -1
  51. package/dist/textview.js +10 -0
  52. package/dist/translate.js +22 -2
  53. package/dist/types.js +2 -2
  54. package/dist/watchflow.js +1 -2
  55. package/dist/work.js +4 -2
  56. package/package.json +2 -1
package/dist/saved.js CHANGED
@@ -15,7 +15,8 @@
15
15
  import fs from "node:fs";
16
16
  import path from "node:path";
17
17
  import { randomBytes, sign as edSign, verify as edVerify } from "node:crypto";
18
- import { ensureSlipKey, readSlipKey } from "./keys.js";
18
+ import { ensureSlipKey, readSlipKey, retiredSlipKeys } from "./keys.js";
19
+ import { releaseLock, stillOwned, takeLock } from "./savedlock.js";
19
20
  import { bound as boundReceipt } from "./receipts.js";
20
21
  import { DEFAULT_LIMITS, loadLimits, loadSavingMode } from "./config.js";
21
22
  import { NOFOLLOW, NONBLOCK, checkSub, rmRegular, isPrivate, ownFile, profileName, readBounded, readHome, sha256, subDir, PRIVATE_WRITE, PrivateWriteError } from "./store.js";
@@ -244,53 +245,67 @@ export function saveSnapshot(input, receipts, limits) {
244
245
  return { status: "failed", reason: "store" };
245
246
  // Everything this snapshot will take on disk, the manifest included (Codex v0.8 round 1).
246
247
  const newBytes = parts.reduce((n, p) => n + p.data.length, 0);
247
- let d;
248
- let dirId;
248
+ // Capacity and publication share one lock with cleanup; only a dead owner's lock can be recovered.
249
+ let held;
249
250
  try {
250
- const lim = limits ?? loadLimits();
251
- d = savedDir(true);
252
- dirId = identOf(d);
253
- const { snaps, tmps, complete } = entries(d);
254
- if (!complete)
255
- return { status: "failed", reason: "store" }; // the caps cannot be checked: fail closed
256
- // Temp folders take disk too (another save in progress, or one left behind): they count (Codex v0.8 round 3).
257
- const used = [...snaps, ...tmps].reduce((n, e) => n + e.bytes, 0);
258
- // A save never evicts: a prompt-injected agent saving again and again must not push out older evidence.
259
- if (snaps.length >= MAX_SNAPSHOTS || used + newBytes + manifestBytes.length > (lim.max_saved_mb ?? DEFAULT_LIMITS.max_saved_mb) * 1024 * 1024)
260
- return { status: "over_cap" };
261
- }
262
- catch (e) {
263
- if (e instanceof PrivateWriteError)
264
- return { status: "private" };
265
- return { status: "failed", reason: "store" };
266
- }
267
- const tmpName = `.tmp-${randomBytes(8).toString("hex")}`;
268
- const tmp = path.join(d, tmpName);
269
- let made = false;
270
- try {
271
- sameDir(d, dirId);
272
- fs.mkdirSync(tmp, { mode: 0o700 });
273
- made = true;
274
- const tmpId = identOf(tmp);
275
- for (const p of parts) {
251
+ let d;
252
+ let dirId;
253
+ try {
254
+ const lim = limits ?? loadLimits();
255
+ d = savedDir(true);
256
+ dirId = identOf(d);
257
+ const token = takeLock(d);
258
+ if (!token)
259
+ return { status: "failed", reason: "store" };
260
+ held = { file: d, token };
261
+ const { snaps, tmps, complete } = entries(d);
262
+ if (!complete)
263
+ return { status: "failed", reason: "store" }; // the caps cannot be checked: fail closed
264
+ // Temp folders take disk too (another save in progress, or one left behind): they count (Codex v0.8 round 3).
265
+ const used = [...snaps, ...tmps].reduce((n, e) => n + e.bytes, 0);
266
+ // A save never evicts: a prompt-injected agent saving again and again must not push out older evidence.
267
+ if (snaps.length >= MAX_SNAPSHOTS || used + newBytes + manifestBytes.length > (lim.max_saved_mb ?? DEFAULT_LIMITS.max_saved_mb) * 1024 * 1024)
268
+ return { status: "over_cap" };
269
+ }
270
+ catch (e) {
271
+ if (e instanceof PrivateWriteError)
272
+ return { status: "private" };
273
+ return { status: "failed", reason: "store" };
274
+ }
275
+ const tmpName = `.tmp-${randomBytes(8).toString("hex")}`;
276
+ const tmp = path.join(d, tmpName);
277
+ let made = false;
278
+ try {
279
+ sameDir(d, dirId);
280
+ fs.mkdirSync(tmp, { mode: 0o700 });
281
+ made = true;
282
+ const tmpId = identOf(tmp);
283
+ for (const p of parts) {
284
+ sameDir(d, dirId), sameDir(tmp, tmpId);
285
+ writeNew(path.join(tmp, p.name), p.data);
286
+ }
276
287
  sameDir(d, dirId), sameDir(tmp, tmpId);
277
- writeNew(path.join(tmp, p.name), p.data);
288
+ writeNew(path.join(tmp, "manifest.json"), manifestBytes);
289
+ sameDir(d, dirId), sameDir(tmp, tmpId);
290
+ if (!stillOwned(d, held.token))
291
+ throw new SavedError("The snapshot lock changed.");
292
+ fs.renameSync(tmp, path.join(d, id)); // one step: a reader never sees half a snapshot
293
+ made = false;
294
+ sameDir(path.join(d, id), tmpId); // the folder now named by the id is the one just written
295
+ }
296
+ catch {
297
+ if (made)
298
+ removeDir(tmp, true);
299
+ return { status: "failed", reason: "store" };
278
300
  }
279
- sameDir(d, dirId), sameDir(tmp, tmpId);
280
- writeNew(path.join(tmp, "manifest.json"), manifestBytes);
281
- sameDir(d, dirId), sameDir(tmp, tmpId);
282
- fs.renameSync(tmp, path.join(d, id)); // one step: a reader never sees half a snapshot
283
- made = false;
284
- sameDir(path.join(d, id), tmpId); // the folder now named by the id is the one just written
301
+ for (const r of receipts.slice(0, MAX_RECEIPTS))
302
+ r.snapshot = id;
303
+ return { status: "saved", id, files: parts.length, bytes: newBytes, ...(input.rendered ? { rendered: true } : {}), ...(input.file && !parts.length ? { fingerprint_only: true } : {}) };
285
304
  }
286
- catch {
287
- if (made)
288
- removeDir(tmp, true);
289
- return { status: "failed", reason: "store" };
305
+ finally {
306
+ if (held)
307
+ releaseLock(held.file, held.token);
290
308
  }
291
- for (const r of receipts.slice(0, MAX_RECEIPTS))
292
- r.snapshot = id;
293
- return { status: "saved", id, files: parts.length, bytes: newBytes, ...(input.rendered ? { rendered: true } : {}), ...(input.file && !parts.length ? { fingerprint_only: true } : {}) };
294
309
  }
295
310
  // ---- reading back ----
296
311
  export class SavedError extends Error {
@@ -470,18 +485,32 @@ export async function verify(idOrPrefix) {
470
485
  return { id, ok: false, manifest: "damaged", message: "The manifest is damaged or not one of Tiresias's: this snapshot cannot be trusted." };
471
486
  const idMatches = m.id === id && snapshotId(m) === id;
472
487
  let signature = "bad";
488
+ let retiredAt;
489
+ const sig = Buffer.from(m.sig, "base64url");
490
+ const sigOk = sig.length === 64 && sig.toString("base64url") === m.sig;
473
491
  try {
474
492
  const keys = readSlipKey();
475
493
  if (!keys)
476
494
  signature = "no_key";
477
- else {
478
- const sig = Buffer.from(m.sig, "base64url");
479
- signature = sig.length === 64 && sig.toString("base64url") === m.sig && edVerify(null, signedBytes(m), keys.pub, sig) ? "ok" : "bad";
480
- }
495
+ else
496
+ signature = sigOk && edVerify(null, signedBytes(m), keys.pub, sig) ? "ok" : "bad";
481
497
  }
482
498
  catch {
483
499
  signature = "no_key";
484
500
  }
501
+ // v1.1: a snapshot signed before `keys rotate --slips` is checked against the retired public halves.
502
+ if (signature !== "ok" && sigOk) {
503
+ try {
504
+ const hit = retiredSlipKeys().find((k) => edVerify(null, signedBytes(m), k.pub, sig));
505
+ if (hit) {
506
+ signature = "retired_key";
507
+ retiredAt = hit.retired_at;
508
+ }
509
+ }
510
+ catch {
511
+ // the retired list cannot be read: the result stays as it was
512
+ }
513
+ }
485
514
  const parts = new Map();
486
515
  const files = m.files.map((f) => {
487
516
  const b = readFileChecked(path.join(dir, f.name), MAX_PART);
@@ -503,7 +532,7 @@ export async function verify(idOrPrefix) {
503
532
  // (Codex v0.8 round 1: a planted manifest must not choose work for the reader).
504
533
  let texts = null;
505
534
  let why;
506
- if (m.receipts.length && (!idMatches || signature !== "ok" || !filesOk))
535
+ if (m.receipts.length && (!idMatches || !(signature === "ok" || signature === "retired_key") || !filesOk))
507
536
  why = "not_authentic";
508
537
  else if (m.receipts.length && m.files.length) {
509
538
  try {
@@ -525,7 +554,8 @@ export async function verify(idOrPrefix) {
525
554
  quote: (texts ? (texts.some((t) => r.quote.length <= t.length && t.includes(r.quote)) ? "found" : "not_found") : "not_checked"),
526
555
  }));
527
556
  const quotesOk = receipts.every((r) => r.snapshot_matches && r.quote === "found");
528
- const ok = idMatches && signature === "ok" && filesOk && !unreadable && extra === 0 && quotesOk;
557
+ const signed = signature === "ok" || signature === "retired_key";
558
+ const ok = idMatches && signed && filesOk && !unreadable && extra === 0 && quotesOk;
529
559
  const reader = m.parser === PARSER_VERSION ? "same" : "different";
530
560
  const problems = [
531
561
  !idMatches && "the id does not match the manifest",
@@ -539,9 +569,9 @@ export async function verify(idOrPrefix) {
539
569
  receipts.some((r) => r.quote === "not_checked") && "some quotes could not be checked",
540
570
  ].filter((x) => !!x);
541
571
  return {
542
- id, ok, manifest: "ok", id_matches: idMatches, signature, files, ...(extra ? { extra_files: extra } : {}), ...(unreadable ? { folder: "unreadable" } : {}), receipts, reader, ...(why ? { quotes_unchecked_reason: why } : {}),
572
+ id, ok, manifest: "ok", id_matches: idMatches, signature, ...(retiredAt ? { retired_key_at: retiredAt } : {}), files, ...(extra ? { extra_files: extra } : {}), ...(unreadable ? { folder: "unreadable" } : {}), receipts, reader, ...(why ? { quotes_unchecked_reason: why } : {}),
543
573
  message: ok
544
- ? `Verified: the signature, the id and every saved file match, and all ${receipts.length} quotes are in the saved bytes.`
574
+ ? `Verified: the signature, the id and every saved file match, and all ${receipts.length} quotes are in the saved bytes.${retiredAt ? ` The signature is by this profile's earlier slip key, retired on ${retiredAt} (tiresias keys rotate --slips); if that key had leaked, someone holding it could also have made this snapshot.` : ""}`
545
575
  : `Not verified: ${problems.join("; ")}.`,
546
576
  };
547
577
  }
@@ -615,15 +645,33 @@ export function planCleanup(now, limits) {
615
645
  }
616
646
  export function applyCleanup(plan) {
617
647
  const result = { removed: 0, kept: plan.kept, bytes_freed: 0 };
618
- for (const r of plan.remove) {
619
- try {
620
- removeDir(r.dir);
621
- result.removed++;
622
- result.bytes_freed += r.bytes;
623
- }
624
- catch {
625
- result.kept++; // left for the next run
648
+ if (!plan.remove.length)
649
+ return result;
650
+ let held;
651
+ try {
652
+ const d = savedDir(false);
653
+ if (!d || isPrivate())
654
+ return { ...result, kept: result.kept + plan.remove.length };
655
+ const token = takeLock(d);
656
+ if (!token)
657
+ return { ...result, kept: result.kept + plan.remove.length };
658
+ held = { file: d, token };
659
+ for (const r of plan.remove) {
660
+ try {
661
+ if (!stillOwned(d, token))
662
+ throw new SavedError("The snapshot lock changed.");
663
+ removeDir(r.dir);
664
+ result.removed++;
665
+ result.bytes_freed += r.bytes;
666
+ }
667
+ catch {
668
+ result.kept++; // left for the next run
669
+ }
626
670
  }
671
+ return result;
672
+ }
673
+ finally {
674
+ if (held)
675
+ releaseLock(held.file, held.token);
627
676
  }
628
- return result;
629
677
  }
@@ -0,0 +1,208 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Saves run in killable MCP servers. Publish a nonempty lock directory atomically, so even a kill during
3
+ // acquisition leaves a named owner. Recovery needs proof the owner is gone: ESRCH, or (v1.1, Codex K.9) the pid now
4
+ // belongs to a different process than the one whose start stamp the lock recorded. A live owner is never timed out.
5
+ // Recovery removes the dead owner's unique entry, then rmdir: a delayed recoverer cannot remove a new,
6
+ // nonempty lock. Config commands keep ownedlock.ts's deliberately manual recovery policy.
7
+ import { execFileSync } from "node:child_process";
8
+ import { randomBytes } from "node:crypto";
9
+ import fs from "node:fs";
10
+ import os from "node:os";
11
+ import path from "node:path";
12
+ import { NOFOLLOW, NONBLOCK } from "./store.js";
13
+ const TRY_MS = 1_000;
14
+ const OWNER_RE = /^[1-9][0-9]*-[0-9a-f]{24}$/;
15
+ const sleepSync = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
16
+ // A process's start stamp, as the kernel recorded it, or null. Format: "<scope>|<value>".
17
+ // - Linux: scope = boot id + pid namespace + time namespace (ours), value = start time in clock ticks since boot
18
+ // (/proc/<pid>/stat field 22).
19
+ // - Elsewhere: scope = "ps" + host name, value = ps's lstart, printed with TZ=UTC and LC_ALL=C so every reader
20
+ // prints it the same way (Codex r4: time zone and locale change the text).
21
+ // The value stays the same for the life of a process whatever the wall clock does, and differs for a later
22
+ // process given the same pid.
23
+ function scope() {
24
+ try {
25
+ if (process.platform === "linux") {
26
+ // Codex r5: stat's start time includes the READER's time-namespace offset, so the time namespace is part of
27
+ // the scope too. Kernels without time namespaces have no such link: one shared clock.
28
+ let timeNs = "no-time-ns";
29
+ try {
30
+ timeNs = fs.readlinkSync("/proc/self/ns/time");
31
+ }
32
+ catch {
33
+ // kept as one shared clock
34
+ }
35
+ return `linux:${fs.readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim()}:${fs.readlinkSync("/proc/self/ns/pid")}:${timeNs}`;
36
+ }
37
+ return `ps:${os.hostname()}`;
38
+ }
39
+ catch {
40
+ return null;
41
+ }
42
+ }
43
+ function startStamp(pid) {
44
+ const sc = scope();
45
+ if (!sc)
46
+ return null;
47
+ try {
48
+ if (process.platform === "linux") {
49
+ // Only meaningful when this /proc numbers pids the way we do (Codex r6: a procfs mounted for another pid
50
+ // namespace can map our number to someone else) and that pid lives in OUR pid namespace.
51
+ if (fs.readlinkSync("/proc/self") !== String(process.pid))
52
+ return null;
53
+ if (fs.readlinkSync(`/proc/${pid}/ns/pid`) !== fs.readlinkSync("/proc/self/ns/pid"))
54
+ return null;
55
+ const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
56
+ const ticks = stat.slice(stat.lastIndexOf(")") + 2).split(" ")[19];
57
+ return ticks && /^\d+$/.test(ticks) ? `${sc}|${ticks}` : null;
58
+ }
59
+ const env = { PATH: process.env.PATH ?? "/usr/bin:/bin", TZ: "UTC", LC_ALL: "C" };
60
+ const t = execFileSync("ps", ["-o", "lstart=", "-p", String(pid)], { encoding: "utf8", timeout: 2_000, env, stdio: ["ignore", "pipe", "ignore"] }).trim();
61
+ return t ? `${sc}|${t}` : null;
62
+ }
63
+ catch {
64
+ return null;
65
+ }
66
+ }
67
+ let ownStamp;
68
+ const myStamp = () => (ownStamp === undefined ? (ownStamp = startStamp(process.pid)) : ownStamp);
69
+ // The owner is gone if its pid no longer exists (ESRCH). Codex K.9: a pid can be reused, so when the pid is alive
70
+ // and the lock recorded its owner's start stamp, a different stamp on that pid now means a different process,
71
+ // but only when the lock was taken in the same scope (same machine and boot, same pid namespace) as ours; a lock
72
+ // from another container or host is never judged this way. The wall clock is never used (Codex r3). Anything
73
+ // unclear, including an old lock with no stamp or a stamp that cannot be read, counts as alive.
74
+ export function dead(pid, recordedStamp) {
75
+ if (!Number.isSafeInteger(pid) || pid <= 0)
76
+ return false;
77
+ try {
78
+ process.kill(pid, 0);
79
+ }
80
+ catch (e) {
81
+ return e.code === "ESRCH";
82
+ }
83
+ if (!recordedStamp)
84
+ return false;
85
+ const sep = recordedStamp.lastIndexOf("|");
86
+ if (sep < 0)
87
+ return false;
88
+ const now = startStamp(pid);
89
+ if (now === null)
90
+ return false;
91
+ const nowSep = now.lastIndexOf("|");
92
+ // Both stamps must come from the same scope, checked on the stamp just read (Codex r6: a host name change
93
+ // between two samples must not turn a scope change into a "different process").
94
+ if (now.slice(0, nowSep) !== recordedStamp.slice(0, sep))
95
+ return false;
96
+ return now.slice(nowSep + 1) !== recordedStamp.slice(sep + 1);
97
+ }
98
+ export const _startStamp = startStamp; // tests
99
+ function recover(file) {
100
+ const l = file + ".lock";
101
+ try {
102
+ const st = fs.lstatSync(l);
103
+ if (st.isDirectory()) {
104
+ const d = fs.opendirSync(l);
105
+ let owner;
106
+ try {
107
+ const entry = d.readSync();
108
+ if (entry?.isFile() && OWNER_RE.test(entry.name) && d.readSync() === null)
109
+ owner = entry.name;
110
+ }
111
+ finally {
112
+ d.closeSync();
113
+ }
114
+ if (!owner)
115
+ return;
116
+ let stamp;
117
+ try {
118
+ const st = fs.lstatSync(path.join(l, owner));
119
+ if (st.size > 0 && st.size <= 256)
120
+ stamp = fs.readFileSync(path.join(l, owner), "utf8").trim() || undefined;
121
+ }
122
+ catch {
123
+ return;
124
+ }
125
+ if (!dead(Number(owner.split("-")[0]), stamp))
126
+ return;
127
+ releaseLock(file, owner);
128
+ }
129
+ else if (st.isFile()) {
130
+ // Recover a lock left by the previous saved.ts implementation. unlink cannot remove a new directory.
131
+ const fd = fs.openSync(l, fs.constants.O_RDONLY | NOFOLLOW | NONBLOCK);
132
+ try {
133
+ if (fs.fstatSync(fd).size > 256)
134
+ return;
135
+ const owner = JSON.parse(fs.readFileSync(fd, "utf8"));
136
+ if (dead(owner?.pid))
137
+ fs.unlinkSync(l);
138
+ }
139
+ finally {
140
+ fs.closeSync(fd);
141
+ }
142
+ }
143
+ }
144
+ catch {
145
+ // Gone meanwhile, malformed, or not ours: retry acquisition without removing anything else.
146
+ }
147
+ }
148
+ export function takeLock(file, tryMs = TRY_MS) {
149
+ const l = file + ".lock";
150
+ const token = `${process.pid}-${randomBytes(12).toString("hex")}`;
151
+ const tmp = l + "-" + token;
152
+ const until = Date.now() + tryMs;
153
+ try {
154
+ fs.mkdirSync(tmp, { mode: 0o700 });
155
+ const ofd = fs.openSync(path.join(tmp, token), fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | NOFOLLOW, 0o600);
156
+ try {
157
+ const stamp = myStamp(); // K.9: who holds this pid, so a later process given the same pid is told apart
158
+ if (stamp)
159
+ fs.writeSync(ofd, stamp + "\n");
160
+ }
161
+ finally {
162
+ fs.closeSync(ofd);
163
+ }
164
+ for (;;) {
165
+ try {
166
+ fs.renameSync(tmp, l); // replaces an empty recovered directory, never a live nonempty one
167
+ return token;
168
+ }
169
+ catch (e) {
170
+ if (!["EEXIST", "ENOTEMPTY", "ENOTDIR", "EISDIR"].includes(e.code ?? ""))
171
+ return null;
172
+ }
173
+ recover(file);
174
+ if (Date.now() >= until)
175
+ return null;
176
+ sleepSync(5);
177
+ }
178
+ }
179
+ catch {
180
+ return null;
181
+ }
182
+ finally {
183
+ try {
184
+ fs.unlinkSync(path.join(tmp, token));
185
+ fs.rmdirSync(tmp);
186
+ }
187
+ catch {
188
+ // Renamed into place, or acquisition failed before the owner entry was written.
189
+ }
190
+ }
191
+ }
192
+ export function stillOwned(file, token) {
193
+ try {
194
+ return fs.lstatSync(file + ".lock").isDirectory() && fs.lstatSync(path.join(file + ".lock", token)).isFile();
195
+ }
196
+ catch {
197
+ return false;
198
+ }
199
+ }
200
+ export function releaseLock(file, token) {
201
+ try {
202
+ fs.unlinkSync(path.join(file + ".lock", token));
203
+ fs.rmdirSync(file + ".lock");
204
+ }
205
+ catch {
206
+ // Gone meanwhile, or a new nonempty lock has already replaced ours.
207
+ }
208
+ }
package/dist/settings.js CHANGED
@@ -10,7 +10,7 @@
10
10
  // Risk key is shown only as set or not set.
11
11
  import os from "node:os";
12
12
  import path from "node:path";
13
- import { ConfigError, DEFAULT_ANCHORING, DEFAULT_CALENDARS, DEFAULT_DOORS, DEFAULT_LIMITS, DEFAULT_SAVING, parseAnchoring, parseCalendars, KNOWN_KEYS, parseAgentStyle, parseDoors, parseKeyFile, parseSafeBrowsing, parseSaving, parseScanner, parseSignatureAgent, parseTranslator, readConfigObject, } from "./config.js";
13
+ import { parseChoice, ConfigError, DEFAULT_ANCHORING, DEFAULT_CALENDARS, DEFAULT_DOORS, DEFAULT_LIMITS, DEFAULT_SAVING, parseAnchoring, parseCalendars, KNOWN_KEYS, parseAgentStyle, parseDoors, parseKeyFile, parseSafeBrowsing, parseSaving, parseScanner, parseSignatureAgent, parseTranslator, readConfigObject, } from "./config.js";
14
14
  import { defaultKeyFile } from "./safebrowsing.js";
15
15
  import { DEFAULT_RENDER, parseRender } from "./render.js";
16
16
  import { MAX_SPACING_S, MIN_SPACING_S, candidates, loadSites, normalizeSite, parsePortList, parseSiteRule, parseSites } from "./sites.js";
@@ -35,6 +35,10 @@ const onOff = (name) => (s) => {
35
35
  throw new ConfigError(`"${name}" takes on or off.`);
36
36
  };
37
37
  export const DEFS = [
38
+ { name: "get_form_actions", default: "read", description: 'Reading a URL matching a GET form action seen on its source page: "read" reads it normally; "slip" requires a submit_form slip. Rendered scripts may never request the GET form actions on that page.', check: (v) => parseChoice("get_form_actions", v), fromText: (v) => parseChoice("get_form_actions", v) },
39
+ { name: "private_spacing", default: "shared", description: 'Private calls and form previews: "shared" writes origin and times only to share spacing across processes; "none" writes nothing and separate processes do not space these calls against each other.', check: (v) => parseChoice("private_spacing", v), fromText: (v) => parseChoice("private_spacing", v) },
40
+ { name: "hook_secrets", default: "hidden", description: 'Scanner and translator environment: "hidden" removes Tiresias secret variables; "inherit" passes the full owner environment, including those secrets.', check: (v) => parseChoice("hook_secrets", v), fromText: (v) => parseChoice("hook_secrets", v) },
41
+ { name: "text_views", default: "marked", description: 'Text views: "marked" keeps them readable with a marker before web content; "wrapped" wraps the text inside untrusted_content, like JSON. Agents should use JSON output.', check: (v) => parseChoice("text_views", v), fromText: (v) => parseChoice("text_views", v) },
38
42
  { name: "doors", default: DEFAULT_DOORS, description: 'How a first read of a site treats its door checks (llms.txt, MCP, API): "background" returns the page at once and checks them afterwards; "wait" checks them first (slower).', check: parseDoors, fromText: parseDoors },
39
43
  { name: "rendering", default: DEFAULT_RENDER, description: 'Running a page\'s JavaScript (W-10), in the installed Google Chrome, sandboxed and throwaway, with every request it makes going through Tiresias\'s rules: "off" never; "ask" when a read asks for it; "auto" also when a plain read is an empty JavaScript shell. TIRESIAS_RENDERING overrides it.', check: parseRender, fromText: parseRender },
40
44
  { name: "saving", default: DEFAULT_SAVING, description: 'Keeping a signed snapshot of a page a read asks to save (W-28): "off" never; "ask" when a read asks for it (read --save, or save on the MCP read). Snapshots are kept in this profile\'s saved/ folder; see tiresias saved.', check: parseSaving, fromText: parseSaving },
@@ -84,7 +88,7 @@ const SITE_DEF = new Map(SITE_DEFS.map((d) => [d.name, d]));
84
88
  const ENV = [
85
89
  { name: "TIRESIAS_HOME", default: "~/.tiresias", description: "The store folder (shared by every agent pointed at it)." },
86
90
  { name: "TIRESIAS_PROFILE", default: "default", description: "The profile to use when --profile is not given." },
87
- { name: "TIRESIAS_PRIVATE", default: "off", description: "1 makes the whole session private: nothing about any visit is saved." },
91
+ { name: "TIRESIAS_PRIVATE", default: "off", description: "1 makes the whole session private: no page records are saved; minimal politeness state still applies." },
88
92
  { name: "TIRESIAS_SAFE_BROWSING", default: "on", description: "0 turns the Safe Browsing check off for the session." },
89
93
  { name: "TIRESIAS_ALLOW_PORTS", default: "none", description: 'More ports to allow on every site, like "8080,9000-9100".' },
90
94
  { name: "TIRESIAS_MAX_BYTES", default: String(5 * 1024 * 1024), description: "The most bytes of a page that are read." },
@@ -353,7 +357,7 @@ export function confirmNow(name, rest, priv, confirm) {
353
357
  if (priv)
354
358
  throw new ConfigError(PRIVATE);
355
359
  if (!name || !isRisky(name))
356
- throw new ConfigError("Usage: tiresias settings confirm <risky setting> [<site>] --confirm \"...\". Risky settings: saving, rendering, translator, scanner, anchoring, anchor_calendars, safe_browsing, and the site settings ports, spacing_seconds, never_visit, hand_off.");
360
+ throw new ConfigError("Usage: tiresias settings confirm <risky setting> [<site>] --confirm \"...\". Risky settings: private_spacing, hook_secrets, saving, rendering, translator, scanner, anchoring, anchor_calendars, safe_browsing, and the site settings ports, spacing_seconds, never_visit, hand_off.");
357
361
  const site = SITE_DEF.has(name) ? siteArgs(name, rest, false).site : undefined;
358
362
  if (!site && rest.length)
359
363
  throw new ConfigError(`Usage: tiresias settings confirm ${name} --confirm "..."`);
package/dist/shelf.js CHANGED
@@ -130,7 +130,7 @@ export function clean(x) {
130
130
  url: c(o.url, MAX_URL_LOG), final_url: c(o.final_url, MAX_URL_LOG),
131
131
  ...str("declared_type", 200), ...str("declared_name", 255),
132
132
  agent: typeof o.agent === "string" ? c(o.agent, 200) : "unknown",
133
- ...(o.source === "provided_by_person" ? { source: "provided_by_person" } : {}),
133
+ ...(o.source === "provided_by_person" || o.source === "provided_by_agent" ? { source: o.source } : {}),
134
134
  };
135
135
  if (cut)
136
136
  r.cut = true;
package/dist/sites.js CHANGED
@@ -24,12 +24,14 @@
24
24
  // A bad "sites" value fails every call that sends a request, with an error naming the site and the setting
25
25
  // (fail closed: a broken entry might be a "never_visit"). clear and the daily trim never read it.
26
26
  import net from "node:net";
27
+ import { readConfigObject } from "./config.js";
27
28
  import { inForceConfig } from "./confirm.js";
28
29
  import { callContext } from "./context.js";
29
30
  import { ConfigError, Refused, SafetyError } from "./errors.js";
30
31
  import { hostKey } from "./sitekey.js";
31
32
  export const SITE_SETTINGS = ["spacing_seconds", "ports", "hand_off", "never_visit"];
32
33
  export const MAX_SITES = 500;
34
+ export const MAX_CONFIRMED_SITES = 5000;
33
35
  export const MIN_SPACING_S = 1;
34
36
  export const MAX_SPACING_S = 30;
35
37
  export const DEFAULT_PORTS = [[80, 80], [443, 443], [8000, 8999]];
@@ -121,12 +123,12 @@ export function parseSiteRule(site, v) {
121
123
  }
122
124
  return out;
123
125
  }
124
- export function parseSites(v) {
126
+ export function parseSites(v, max = MAX_SITES) {
125
127
  if (typeof v !== "object" || v === null || Array.isArray(v))
126
128
  throw new ConfigError('config.json: "sites" must be an object, like {"example.com": {"spacing_seconds": 5}}.');
127
129
  const keys = Object.keys(v);
128
- if (keys.length > MAX_SITES)
129
- throw new ConfigError(`config.json: "sites" has more than ${MAX_SITES} sites.`);
130
+ if (keys.length > max)
131
+ throw new ConfigError(`config.json: "sites" has more than ${max} sites.`);
130
132
  const out = Object.create(null);
131
133
  for (const k of keys) {
132
134
  normalizeSite(k); // strict: the written form
@@ -135,8 +137,11 @@ export function parseSites(v) {
135
137
  return out;
136
138
  }
137
139
  export function loadSites() {
140
+ const raw = readConfigObject();
141
+ if (raw && Object.hasOwn(raw, "sites"))
142
+ parseSites(raw.sites);
138
143
  const o = inForceConfig(); // W-32: an unconfirmed loosening is held back
139
- return o && Object.hasOwn(o, "sites") ? parseSites(o.sites) : Object.create(null);
144
+ return o && Object.hasOwn(o, "sites") ? parseSites(o.sites, MAX_SITES + MAX_CONFIRMED_SITES) : Object.create(null);
140
145
  }
141
146
  // The entries that can apply to a host, most specific first: the host itself, then each wildcard above it.
142
147
  // A host name longer than DNS allows (253 characters) names no site anyone can reach: it is refused before any
package/dist/slips.js CHANGED
@@ -36,7 +36,7 @@ import { SlipError } from "./errors.js";
36
36
  import { PATH_RE, pathAllowed } from "./forms.js";
37
37
  import { readItem, writeItem } from "./itemfiles.js";
38
38
  import * as history from "./history.js";
39
- import { ensureSlipKey, readSlipKey } from "./keys.js";
39
+ import { SLIP_KEY_FILE, ensureSlipKey, readSlipKey, replaceSlipKey } from "./keys.js";
40
40
  import { NOFOLLOW, NONBLOCK, cliSafeId, isPrivate, ownFile, profileName, rmRegular, subDir } from "./store.js";
41
41
  export { SlipError };
42
42
  // The fixed allow-list of actions. v0.4 has exactly one.
@@ -491,6 +491,35 @@ export function revokeAll(priv = false) {
491
491
  removeLooseUse(d, f);
492
492
  return n;
493
493
  }
494
+ // ---- v1.1 workstream 4: rotating the slip key ----
495
+ // A new slip key makes every existing slip fail its signature check, so the rotation revokes them all, cleanly and
496
+ // on the record (revoked.jsonl), before the key is replaced, and once more after it (a slip granted meanwhile was
497
+ // signed by the old key). Saved pages are not touched: the old key's public half is kept for `saved verify`.
498
+ // Receipts never reference the slip key (a receipt names a snapshot by its SHA-256 id). An irreversible action, so
499
+ // it always needs its phrase, even with confirmations for risky settings turned off; the CLI also asks a person at
500
+ // a terminal to type "rotate slip key", like `slip revoke --all`. Written to history (tool "keys").
501
+ export const ROTATE_SLIP_PHRASE = "YES, ROTATE THE SLIP KEY";
502
+ export function checkRotatePhrase(confirm) {
503
+ if (confirm !== ROTATE_SLIP_PHRASE)
504
+ throw new SlipError(`Rotating the slip key revokes every permission slip of this profile, and saved pages signed before it are then checked against the old key's public half. It needs the exact confirmation ${JSON.stringify(ROTATE_SLIP_PHRASE)}. Run it again with: tiresias keys rotate --slips --confirm '${ROTATE_SLIP_PHRASE}' (type it exactly, in capitals; it is recorded in history). Nothing was changed.`);
505
+ }
506
+ export function rotateSlipKey(confirm, priv = false, now = Date.now()) {
507
+ if (priv || isPrivate())
508
+ throw new SlipError("Private mode: nothing is written, so the slip key is not rotated.");
509
+ checkRotatePhrase(confirm);
510
+ const d = subDir("keys", false);
511
+ if (!d || !fs.lstatSync(path.join(d, SLIP_KEY_FILE), { throwIfNoEntry: false }))
512
+ throw new SlipError("There is no slip key yet, so there is nothing to rotate. One is made by the first slip grant or saved page.");
513
+ // The slips are revoked inside replaceSlipKey, after every refusal (synced folder, another rotation, a full retired
514
+ // list), so a refused rotation never destroys them (Codex K.4).
515
+ let revoked = 0;
516
+ const r = replaceSlipKey(now, () => {
517
+ revoked = revokeAll(priv);
518
+ });
519
+ revoked += revokeAll(priv);
520
+ history.record({ time: new Date(now).toISOString(), tool: "keys", url: "", agent: "cli", status: "ok", door: "none", event: "slip_key_rotated", revoked, by: process.stdin.isTTY === true ? "terminal" : "not_terminal" });
521
+ return { revoked, retired: r.retired };
522
+ }
494
523
  // A use file is deleted on its own only when its slip file is confirmed gone at that moment. A slip
495
524
  // granted (and claimed) after any listing was made still has its <id>.json, so its uses are never
496
525
  // touched (Codex round 49: a final sweep deleted a new slip's use and gave the use back). Uses only
@@ -687,3 +716,16 @@ export function claimSlip(action, url, label, opts = {}) {
687
716
  }
688
717
  return no("no_slip");
689
718
  }
719
+ // A claimed use stays spent, but may leave the queue only while its slip still permits this target.
720
+ export function claimedStillValid(claim, action, url) {
721
+ const t = target(url);
722
+ const d = subDir("slips", false);
723
+ if (!d || !t || !pathAllowed(url) || !PATH_RE.test(t.pathname))
724
+ return false;
725
+ const keys = readSlipKey();
726
+ const profile = profileName();
727
+ const s = readOne(d, claim.slip_id, keys, profile);
728
+ return !!s && s !== "damaged" && s.action === action && s.site === t.origin && pathCovers(s.path, t.pathname)
729
+ && Date.parse(s.expires_at) > Date.now() && claim.use >= 1 && claim.use <= s.uses
730
+ && useVerifies(d, s.id, claim.use, keys, profile);
731
+ }