@peer2peer/cli 0.0.0-stage → 0.1.1

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 (111) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +370 -2
  3. package/dist/ancestry.d.ts +13 -0
  4. package/dist/ancestry.js +71 -0
  5. package/dist/app.d.ts +368 -0
  6. package/dist/app.js +1181 -0
  7. package/dist/bin.d.ts +2 -0
  8. package/dist/bin.js +56 -0
  9. package/dist/circle-ops.d.ts +402 -0
  10. package/dist/circle-ops.js +1725 -0
  11. package/dist/cli.d.ts +660 -0
  12. package/dist/cli.js +1787 -0
  13. package/dist/codes.d.ts +231 -0
  14. package/dist/codes.js +998 -0
  15. package/dist/config.d.ts +159 -0
  16. package/dist/config.js +294 -0
  17. package/dist/doctor.d.ts +113 -0
  18. package/dist/doctor.js +930 -0
  19. package/dist/errors.d.ts +8 -0
  20. package/dist/errors.js +29 -0
  21. package/dist/ext-host.d.ts +38 -0
  22. package/dist/ext-host.js +293 -0
  23. package/dist/extensions.d.ts +487 -0
  24. package/dist/extensions.js +406 -0
  25. package/dist/features.d.ts +91 -0
  26. package/dist/features.js +164 -0
  27. package/dist/format.d.ts +57 -0
  28. package/dist/format.js +248 -0
  29. package/dist/graph-memory.d.ts +107 -0
  30. package/dist/graph-memory.js +393 -0
  31. package/dist/history.d.ts +155 -0
  32. package/dist/history.js +697 -0
  33. package/dist/identity.d.ts +239 -0
  34. package/dist/identity.js +759 -0
  35. package/dist/inbound.d.ts +32 -0
  36. package/dist/inbound.js +261 -0
  37. package/dist/index.d.ts +8 -0
  38. package/dist/index.js +14 -0
  39. package/dist/ix-local.d.ts +76 -0
  40. package/dist/ix-local.js +203 -0
  41. package/dist/join-answers.d.ts +49 -0
  42. package/dist/join-answers.js +149 -0
  43. package/dist/outbox.d.ts +109 -0
  44. package/dist/outbox.js +337 -0
  45. package/dist/pretty.d.ts +100 -0
  46. package/dist/pretty.js +256 -0
  47. package/dist/procstart.d.ts +1 -0
  48. package/dist/procstart.js +66 -0
  49. package/dist/progress.d.ts +30 -0
  50. package/dist/progress.js +78 -0
  51. package/dist/projects.d.ts +491 -0
  52. package/dist/projects.js +1383 -0
  53. package/dist/prompt/picker.d.ts +64 -0
  54. package/dist/prompt/picker.js +152 -0
  55. package/dist/prompt/tty.d.ts +29 -0
  56. package/dist/prompt/tty.js +122 -0
  57. package/dist/readonly.d.ts +20 -0
  58. package/dist/readonly.js +50 -0
  59. package/dist/receipts.d.ts +99 -0
  60. package/dist/receipts.js +182 -0
  61. package/dist/relay-client.d.ts +277 -0
  62. package/dist/relay-client.js +873 -0
  63. package/dist/relay-info.d.ts +24 -0
  64. package/dist/relay-info.js +53 -0
  65. package/dist/rotation.d.ts +402 -0
  66. package/dist/rotation.js +2054 -0
  67. package/dist/running.d.ts +5 -0
  68. package/dist/running.js +74 -0
  69. package/dist/sessions.d.ts +355 -0
  70. package/dist/sessions.js +1247 -0
  71. package/dist/shortcode.d.ts +149 -0
  72. package/dist/shortcode.js +465 -0
  73. package/dist/social.d.ts +139 -0
  74. package/dist/social.js +485 -0
  75. package/dist/state.d.ts +509 -0
  76. package/dist/state.js +220 -0
  77. package/dist/stdin.d.ts +17 -0
  78. package/dist/stdin.js +46 -0
  79. package/dist/sync.d.ts +77 -0
  80. package/dist/sync.js +375 -0
  81. package/dist/transient.d.ts +1 -0
  82. package/dist/transient.js +28 -0
  83. package/dist/trust.d.ts +37 -0
  84. package/dist/trust.js +134 -0
  85. package/dist/tty-confirm.d.ts +2 -0
  86. package/dist/tty-confirm.js +53 -0
  87. package/dist/tui/command.d.ts +20 -0
  88. package/dist/tui/command.js +70 -0
  89. package/dist/tui/frame.d.ts +43 -0
  90. package/dist/tui/frame.js +111 -0
  91. package/dist/tui/model.d.ts +221 -0
  92. package/dist/tui/model.js +259 -0
  93. package/dist/tui/sanitize.d.ts +13 -0
  94. package/dist/tui/sanitize.js +151 -0
  95. package/dist/tui/terminal.d.ts +88 -0
  96. package/dist/tui/terminal.js +354 -0
  97. package/dist/tui/tui.d.ts +78 -0
  98. package/dist/tui/tui.js +612 -0
  99. package/dist/tui/view.d.ts +4 -0
  100. package/dist/tui/view.js +358 -0
  101. package/dist/unknown-kinds.d.ts +12 -0
  102. package/dist/unknown-kinds.js +66 -0
  103. package/dist/version.d.ts +1 -0
  104. package/dist/version.js +5 -0
  105. package/dist/want-pow.d.ts +12 -0
  106. package/dist/want-pow.js +46 -0
  107. package/dist/watch.d.ts +35 -0
  108. package/dist/watch.js +331 -0
  109. package/dist/xstate.d.ts +125 -0
  110. package/dist/xstate.js +422 -0
  111. package/package.json +60 -4
@@ -0,0 +1,64 @@
1
+ import type { Key } from "../tui/model.js";
2
+ export interface Choice {
3
+ value: string;
4
+ label: string;
5
+ /** Dimmed after the label (member count, policy, …). */
6
+ detail?: string;
7
+ /** Why it can't be chosen (shown); a disabled choice is never selected. */
8
+ disabled?: string;
9
+ /** Pre-checked (multi). */
10
+ checked?: boolean;
11
+ /** Multi: choosing it clears the others, and choosing another clears it ("friend only"). */
12
+ exclusive?: boolean;
13
+ }
14
+ export interface SelectSpec {
15
+ message: string;
16
+ /** Lines shown above the message (a preview), sanitised. */
17
+ intro?: string[];
18
+ choices: Choice[];
19
+ multi?: boolean;
20
+ /** Multi: at most this many. */
21
+ max?: number;
22
+ /** Single: the value the cursor starts on. */
23
+ initial?: string;
24
+ }
25
+ export interface PickerState {
26
+ spec: SelectSpec;
27
+ cursor: number;
28
+ filter: string;
29
+ checked: string[];
30
+ note?: string;
31
+ /** Set once finished: the values chosen, or null (cancelled). */
32
+ done?: string[] | null;
33
+ }
34
+ export declare function initialState(spec: SelectSpec): PickerState;
35
+ /** The choices the filter lets through, with their index in spec.choices. */
36
+ export declare function visible(s: PickerState): {
37
+ c: Choice;
38
+ i: number;
39
+ }[];
40
+ /** One key. A finished state is returned as it is. */
41
+ export declare function reduce(s: PickerState, k: Key): PickerState;
42
+ export interface Style {
43
+ bold(s: string): string;
44
+ dim(s: string): string;
45
+ cyan(s: string): string;
46
+ green(s: string): string;
47
+ yellow(s: string): string;
48
+ red(s: string): string;
49
+ }
50
+ export declare const COLOR: Style;
51
+ export declare const PLAIN: Style;
52
+ export declare const HINT_MULTI = "up/down j/k move \u00B7 space select \u00B7 enter confirm \u00B7 type to filter \u00B7 esc cancel";
53
+ export declare const HINT_SINGLE = "up/down j/k move \u00B7 enter choose \u00B7 type to filter \u00B7 esc cancel";
54
+ /** The picker as lines, each at most `cols - 1` columns wide (no line ever wraps, so redrawing stays exact). */
55
+ export declare function render(s: PickerState, o: {
56
+ cols: number;
57
+ rows: number;
58
+ style: Style;
59
+ }): string[];
60
+ /** The one line left behind once the picker is done: the question and what was chosen. */
61
+ export declare function summary(s: PickerState, o: {
62
+ cols: number;
63
+ style: Style;
64
+ }): string;
@@ -0,0 +1,152 @@
1
+ // Copyright 2026 Ix Infrastructure Inc.
2
+ //
3
+ // The CLI's interactive picker: a pure model (state + key -> state) and a
4
+ // renderer (state -> lines). The terminal driver is ./tty.ts; tests drive
5
+ // the model with keys and check what it chose.
6
+ //
7
+ // - Arrow keys or j/k (while no filter is typed) move; space toggles (multi);
8
+ // enter confirms; typing filters; backspace edits the filter; esc clears the
9
+ // filter, or cancels; Ctrl-C always cancels.
10
+ // - A disabled choice is shown, with its reason, and can't be chosen.
11
+ // - Every label and detail goes through the TUI's sanitiser (cell/clean) on its
12
+ // way to the terminal: a circle or person's name can't carry an escape sequence.
13
+ import { cell, clean, displayWidth, wrap } from "../tui/sanitize.js";
14
+ export function initialState(spec) {
15
+ const checked = spec.multi ? spec.choices.filter((c) => c.checked && !c.disabled).map((c) => c.value) : [];
16
+ const at = spec.initial !== undefined ? spec.choices.findIndex((c) => c.value === spec.initial) : spec.choices.findIndex((c) => !c.disabled);
17
+ return { spec, cursor: Math.max(0, at), filter: "", checked };
18
+ }
19
+ /** The choices the filter lets through, with their index in spec.choices. */
20
+ export function visible(s) {
21
+ const f = s.filter.toLowerCase();
22
+ return s.spec.choices.map((c, i) => ({ c, i })).filter(({ c }) => f === "" || clean(`${c.label} ${c.detail ?? ""}`).toLowerCase().includes(f));
23
+ }
24
+ function move(s, by) {
25
+ const v = visible(s);
26
+ if (v.length === 0)
27
+ return s;
28
+ const at = Math.max(0, v.findIndex((x) => x.i === s.cursor));
29
+ const next = v[(at + by + v.length * 8) % v.length];
30
+ return { ...s, cursor: next.i, note: undefined };
31
+ }
32
+ function toggle(s, c) {
33
+ if (c.disabled)
34
+ return { ...s, note: c.disabled };
35
+ if (s.checked.includes(c.value))
36
+ return { ...s, checked: s.checked.filter((v) => v !== c.value), note: undefined };
37
+ const exclusive = new Set(s.spec.choices.filter((x) => x.exclusive).map((x) => x.value));
38
+ const base = c.exclusive ? [] : s.checked.filter((v) => !exclusive.has(v));
39
+ const max = s.spec.max;
40
+ if (max !== undefined && base.length >= max)
41
+ return { ...s, note: `at most ${max}` };
42
+ return { ...s, checked: [...base, c.value], note: undefined };
43
+ }
44
+ /** One key. A finished state is returned as it is. */
45
+ export function reduce(s, k) {
46
+ if (s.done !== undefined)
47
+ return s;
48
+ if (k.ctrl && k.name === "c")
49
+ return { ...s, done: null };
50
+ const v = visible(s);
51
+ const cur = v.find((x) => x.i === s.cursor) ?? v[0];
52
+ // Pasted text only ever goes into the filter: it never moves, toggles or confirms.
53
+ if (k.paste)
54
+ return k.ch ? retarget({ ...s, filter: (s.filter + k.ch).slice(0, 64), note: undefined }) : s;
55
+ switch (k.name) {
56
+ case "up":
57
+ return move(s, -1);
58
+ case "down":
59
+ case "tab":
60
+ return move(s, k.shift ? -1 : 1);
61
+ case "home":
62
+ return v.length ? { ...s, cursor: v[0].i } : s;
63
+ case "end":
64
+ return v.length ? { ...s, cursor: v[v.length - 1].i } : s;
65
+ case "escape":
66
+ return s.filter ? retarget({ ...s, filter: "", note: undefined }) : { ...s, done: null };
67
+ case "backspace":
68
+ return retarget({ ...s, filter: s.filter.slice(0, -1), note: undefined });
69
+ case "space":
70
+ if (!cur)
71
+ return s;
72
+ return s.spec.multi ? toggle(s, cur.c) : s;
73
+ case "return": {
74
+ if (!cur && !(s.spec.multi && s.checked.length))
75
+ return { ...s, note: "nothing matches the filter" };
76
+ if (s.spec.multi) {
77
+ if (s.checked.length > 0)
78
+ return { ...s, done: s.spec.choices.filter((c) => s.checked.includes(c.value)).map((c) => c.value) };
79
+ if (cur.c.disabled)
80
+ return { ...s, note: cur.c.disabled };
81
+ return { ...s, done: [cur.c.value] };
82
+ }
83
+ if (cur.c.disabled)
84
+ return { ...s, note: cur.c.disabled };
85
+ return { ...s, done: [cur.c.value] };
86
+ }
87
+ }
88
+ if (k.ctrl && (k.name === "n" || k.name === "p"))
89
+ return move(s, k.name === "n" ? 1 : -1);
90
+ if (k.ch && !k.ctrl) {
91
+ // j/k move while no filter is typed (vi keys); any other character starts or extends the filter.
92
+ if (s.filter === "" && (k.ch === "j" || k.ch === "k"))
93
+ return move(s, k.ch === "j" ? 1 : -1);
94
+ return retarget({ ...s, filter: (s.filter + k.ch).slice(0, 64), note: undefined });
95
+ }
96
+ return s;
97
+ }
98
+ /** After the filter changed: keep the cursor on a visible choice. */
99
+ function retarget(s) {
100
+ const v = visible(s);
101
+ if (v.length === 0 || v.some((x) => x.i === s.cursor))
102
+ return s;
103
+ return { ...s, cursor: (v.find((x) => !x.c.disabled) ?? v[0]).i };
104
+ }
105
+ const sgr = (on, off) => (s) => `\u001b[${on}m${s}\u001b[${off}m`;
106
+ export const COLOR = { bold: sgr("1", "22"), dim: sgr("2", "22"), cyan: sgr("36", "39"), green: sgr("32", "39"), yellow: sgr("33", "39"), red: sgr("31", "39") };
107
+ const id = (s) => s;
108
+ export const PLAIN = { bold: id, dim: id, cyan: id, green: id, yellow: id, red: id };
109
+ export const HINT_MULTI = "up/down j/k move · space select · enter confirm · type to filter · esc cancel";
110
+ export const HINT_SINGLE = "up/down j/k move · enter choose · type to filter · esc cancel";
111
+ /** The picker as lines, each at most `cols - 1` columns wide (no line ever wraps, so redrawing stays exact). */
112
+ export function render(s, o) {
113
+ const w = Math.max(20, o.cols - 1);
114
+ const st = o.style;
115
+ const lines = [];
116
+ for (const l of s.spec.intro ?? [])
117
+ lines.push(...(clean(l) === "" ? [""] : wrap(l, w, 12)));
118
+ lines.push(`${st.cyan("?")} ${st.bold(cell(s.spec.message, w - 2))}`);
119
+ const v = visible(s);
120
+ const room = Math.max(3, Math.min(12, o.rows - lines.length - 4));
121
+ const at = Math.max(0, v.findIndex((x) => x.i === s.cursor));
122
+ const top = Math.min(Math.max(0, at - room + 1), Math.max(0, v.length - room));
123
+ if (v.length === 0)
124
+ lines.push(st.dim(` nothing matches "${cell(s.filter, 32)}"`));
125
+ for (const { c, i } of v.slice(top, top + room)) {
126
+ const here = i === s.cursor;
127
+ const mark = s.spec.multi ? (s.checked.includes(c.value) ? "[x]" : "[ ]") : here ? "(*)" : "( )";
128
+ const head = `${here ? ">" : " "} ${mark} `;
129
+ const label = cell(c.label, Math.max(4, w - displayWidth(head) - 2));
130
+ const rest = w - displayWidth(head) - displayWidth(label) - 2;
131
+ const extra = [c.detail, c.disabled ? `unavailable: ${c.disabled}` : undefined].filter(Boolean).join(" · ");
132
+ const tail = extra && rest > 4 ? ` ${cell(extra, rest)}` : "";
133
+ const body = c.disabled ? st.dim(`${label}${tail}`) : `${here ? st.cyan(label) : label}${st.dim(tail)}`;
134
+ lines.push(`${here ? st.cyan(head) : head}${body}`);
135
+ }
136
+ if (v.length > room)
137
+ lines.push(st.dim(` … ${v.length - room} more (type to filter)`));
138
+ if (s.filter)
139
+ lines.push(` filter: ${cell(s.filter, w - 12)}`);
140
+ if (s.note)
141
+ lines.push(st.yellow(` ${cell(s.note, w - 2)}`));
142
+ lines.push(st.dim(cell(s.spec.multi ? HINT_MULTI : HINT_SINGLE, w)));
143
+ return lines;
144
+ }
145
+ /** The one line left behind once the picker is done: the question and what was chosen. */
146
+ export function summary(s, o) {
147
+ const w = Math.max(20, o.cols - 1);
148
+ const chosen = s.done ? s.spec.choices.filter((c) => s.done.includes(c.value)).map((c) => clean(c.label)) : [];
149
+ const what = s.done === null ? "cancelled" : chosen.join(", ") || "none";
150
+ const msg = cell(s.spec.message, Math.max(8, Math.floor(w / 2)));
151
+ return `${o.style.cyan("?")} ${o.style.bold(msg)} ${s.done === null ? o.style.yellow(cell(what, w - displayWidth(msg) - 3)) : o.style.cyan(cell(what, w - displayWidth(msg) - 3))}`;
152
+ }
@@ -0,0 +1,29 @@
1
+ import { type SelectSpec } from "./picker.js";
2
+ /** What the driver needs of stdin (a tty.ReadStream, or a fake in tests). */
3
+ export interface PickerInput {
4
+ isTTY?: boolean;
5
+ setRawMode?(on: boolean): unknown;
6
+ on(ev: "data", fn: (b: Buffer | string) => void): unknown;
7
+ on(ev: "end", fn: () => void): unknown;
8
+ off(ev: "data", fn: (b: Buffer | string) => void): unknown;
9
+ off(ev: "end", fn: () => void): unknown;
10
+ resume(): unknown;
11
+ pause(): unknown;
12
+ }
13
+ export interface PickerOutput {
14
+ write(s: string): unknown;
15
+ columns?: number;
16
+ rows?: number;
17
+ }
18
+ /** How the picker hooks termination signals (tests pass a fake). */
19
+ export interface PickerSignals {
20
+ on(sig: NodeJS.Signals, fn: () => void): unknown;
21
+ off(sig: NodeJS.Signals, fn: () => void): unknown;
22
+ /** Re-raises the signal once the terminal is restored, so the process ends as it would have. */
23
+ raise(sig: NodeJS.Signals): void;
24
+ }
25
+ /** Runs one picker: the values chosen, or undefined when cancelled. */
26
+ export declare function runPicker(spec: SelectSpec, input: PickerInput, output: PickerOutput, o: {
27
+ color: boolean;
28
+ signals?: PickerSignals;
29
+ }): Promise<string[] | undefined>;
@@ -0,0 +1,122 @@
1
+ // Copyright 2026 Ix Infrastructure Inc.
2
+ //
3
+ // The picker on a real terminal: raw keys through the TUI's KeyParser, drawn
4
+ // inline (no alternate screen) under the command's own output, and the
5
+ // terminal always restored: on a choice, on Esc / Ctrl-C, on the input
6
+ // ending and, as a last resort, on process exit.
7
+ //
8
+ // Only bin.ts wires this in, and only when stdin and stdout are both a
9
+ // terminal, CI and --no-input are unset, and no agent's tool call is running
10
+ // the command (CLAUDECODE / CODEX_THREAD_ID): piped input never reaches a picker.
11
+ import { StringDecoder } from "node:string_decoder";
12
+ import { KeyParser } from "../tui/terminal.js";
13
+ import { COLOR, initialState, PLAIN, reduce, render, summary } from "./picker.js";
14
+ const PROCESS_SIGNALS = {
15
+ on: (sig, fn) => process.on(sig, fn),
16
+ off: (sig, fn) => process.off(sig, fn),
17
+ raise: (sig) => process.kill(process.pid, sig),
18
+ };
19
+ const HOOKED = ["SIGINT", "SIGTERM", "SIGHUP"];
20
+ /** Runs one picker: the values chosen, or undefined when cancelled. */
21
+ export function runPicker(spec, input, output, o) {
22
+ const signals = o.signals ?? PROCESS_SIGNALS;
23
+ const style = o.color ? COLOR : PLAIN;
24
+ const parser = new KeyParser();
25
+ const decoder = new StringDecoder("utf8");
26
+ let state = initialState(spec);
27
+ let drawn = 0;
28
+ let escTimer;
29
+ const size = () => ({ cols: output.columns || 80, rows: output.rows || 24 });
30
+ const erase = () => {
31
+ if (drawn > 0)
32
+ output.write(`\r${drawn > 1 ? `\u001b[${drawn - 1}A` : ""}\u001b[J`);
33
+ drawn = 0;
34
+ };
35
+ const draw = () => {
36
+ const lines = render(state, { ...size(), style });
37
+ erase();
38
+ output.write(lines.join("\r\n"));
39
+ drawn = lines.length;
40
+ };
41
+ return new Promise((resolve) => {
42
+ let finished = false;
43
+ const restore = () => {
44
+ try {
45
+ input.setRawMode?.(false);
46
+ }
47
+ catch {
48
+ // already gone
49
+ }
50
+ output.write("\u001b[0m\u001b[?25h");
51
+ };
52
+ const detach = () => {
53
+ finished = true;
54
+ if (escTimer)
55
+ clearTimeout(escTimer);
56
+ input.off("data", onData);
57
+ input.off("end", onEnd);
58
+ process.off("exit", restore);
59
+ for (const [sig, fn] of onSignals)
60
+ signals.off(sig, fn);
61
+ };
62
+ const finish = () => {
63
+ if (finished)
64
+ return;
65
+ detach();
66
+ erase();
67
+ output.write(`${summary(state, { cols: size().cols, style })}\r\n`);
68
+ restore();
69
+ input.pause();
70
+ resolve(state.done ? state.done : undefined);
71
+ };
72
+ // SIGTERM, SIGHUP, or SIGINT from outside (raw mode turns Ctrl-C into a key): the terminal goes
73
+ // back first, then the signal is raised again, so the process ends as it would have (as the TUI does).
74
+ const onSignals = HOOKED.map((sig) => {
75
+ const fn = () => {
76
+ if (finished)
77
+ return;
78
+ detach();
79
+ erase();
80
+ output.write("\r\n");
81
+ restore();
82
+ input.pause();
83
+ resolve(undefined);
84
+ signals.raise(sig);
85
+ };
86
+ return [sig, fn];
87
+ });
88
+ const keys = (ks) => {
89
+ for (const k of ks) {
90
+ state = reduce(state, k);
91
+ if (state.done !== undefined)
92
+ return finish();
93
+ }
94
+ draw();
95
+ };
96
+ const onData = (b) => {
97
+ if (escTimer)
98
+ clearTimeout(escTimer);
99
+ keys(parser.feed(typeof b === "string" ? b : decoder.write(b)));
100
+ if (finished)
101
+ return;
102
+ const ms = parser.waitMs;
103
+ if (ms > 0) {
104
+ escTimer = setTimeout(() => keys(parser.idle()), ms);
105
+ escTimer.unref?.();
106
+ }
107
+ };
108
+ const onEnd = () => {
109
+ state = { ...state, done: null };
110
+ finish();
111
+ };
112
+ process.on("exit", restore);
113
+ for (const [sig, fn] of onSignals)
114
+ signals.on(sig, fn);
115
+ input.setRawMode?.(true);
116
+ input.on("data", onData);
117
+ input.on("end", onEnd);
118
+ input.resume();
119
+ output.write("\u001b[?25l");
120
+ draw();
121
+ });
122
+ }
@@ -0,0 +1,20 @@
1
+ import { TrustStoreV3, type DocBackend } from "@peer2peer/core/v3";
2
+ /** Reads fall through to `base` until a document is written; writes stay in memory. */
3
+ export declare class OverlayDocBackend implements DocBackend {
4
+ private readonly base;
5
+ private readonly docs;
6
+ private chain;
7
+ constructor(base: DocBackend);
8
+ private readonly removed;
9
+ read(name: string): Promise<unknown>;
10
+ /** In memory only, like write: the base is never touched. */
11
+ remove(name: string): Promise<void>;
12
+ write(name: string, value: unknown): Promise<void>;
13
+ /** In-process only: never the on-disk lock (taking it would create the lock file). */
14
+ withLock<T>(fn: () => Promise<T>): Promise<T>;
15
+ }
16
+ /** A trust store over `<dir>` that never writes to it. */
17
+ export declare class ReadOnlyTrustStoreV3 extends TrustStoreV3 {
18
+ readonly dir: string;
19
+ constructor(dir: string);
20
+ }
@@ -0,0 +1,50 @@
1
+ // Copyright 2026 Ix Infrastructure Inc.
2
+ //
3
+ // Read-only views of a P2P home, for `p2p doctor`: every document is read
4
+ // from disk on first use, and every write (an identity refresh, a "fetched"
5
+ // stamp, a discovered identity) lands in memory only and is gone when the
6
+ // process ends. Nothing takes the cross-process lock or creates a file, so a
7
+ // diagnosis never changes the home it diagnoses, even when one of its bounded
8
+ // steps times out and finishes later.
9
+ import { TrustStoreV3 } from "@peer2peer/core/v3";
10
+ import { FileDocBackend } from "@peer2peer/core/v3/node";
11
+ /** Reads fall through to `base` until a document is written; writes stay in memory. */
12
+ export class OverlayDocBackend {
13
+ base;
14
+ docs = new Map();
15
+ chain = Promise.resolve();
16
+ constructor(base) {
17
+ this.base = base;
18
+ }
19
+ removed = new Set();
20
+ async read(name) {
21
+ const mine = this.docs.get(name);
22
+ if (mine !== undefined)
23
+ return JSON.parse(mine);
24
+ if (this.removed.has(name))
25
+ return undefined;
26
+ return this.base.read(name);
27
+ }
28
+ /** In memory only, like write: the base is never touched. */
29
+ async remove(name) {
30
+ this.docs.delete(name);
31
+ this.removed.add(name);
32
+ }
33
+ async write(name, value) {
34
+ this.docs.set(name, JSON.stringify(value));
35
+ }
36
+ /** In-process only: never the on-disk lock (taking it would create the lock file). */
37
+ withLock(fn) {
38
+ const run = this.chain.then(fn, fn);
39
+ this.chain = run.catch(() => { });
40
+ return run;
41
+ }
42
+ }
43
+ /** A trust store over `<dir>` that never writes to it. */
44
+ export class ReadOnlyTrustStoreV3 extends TrustStoreV3 {
45
+ dir;
46
+ constructor(dir) {
47
+ super(new OverlayDocBackend(new FileDocBackend(dir)));
48
+ this.dir = dir;
49
+ }
50
+ }
@@ -0,0 +1,99 @@
1
+ import { type AcceptedMessageV3, type ReceiptOutcome } from "@peer2peer/core/v3";
2
+ import type { App } from "./app.js";
3
+ import { type NewEvent } from "./inbound.js";
4
+ /** What a sender remembers about a message that asked for receipts. */
5
+ export interface ReceiptWanted {
6
+ /** The user it went to: the only one whose receipts for it count. */
7
+ to: string;
8
+ /** The session it went to (own, ref), for the sender's display. */
9
+ own: string;
10
+ ref: string;
11
+ /** The local session that sent it: its watcher gets the receipts. */
12
+ fromSession: string;
13
+ at: number;
14
+ outcomes: {
15
+ outcome: ReceiptOutcome;
16
+ at: number;
17
+ }[];
18
+ }
19
+ /** What a receiver remembers about a message that asked, by its event id. */
20
+ export interface ReceiptAsked {
21
+ id: string;
22
+ /** The authenticated sender, and the device that sent it (the receipt goes there only). */
23
+ from: string;
24
+ did: string;
25
+ at: number;
26
+ /** The local session it was delivered into. */
27
+ toSession?: string;
28
+ /** An ended session's copy (inbound `unclaimedFor`): it only ever says `ended`. */
29
+ unclaimedFor?: string;
30
+ }
31
+ export interface ReceiptsDoc {
32
+ wanted: Record<string, ReceiptWanted>;
33
+ asked: Record<string, ReceiptAsked>;
34
+ /** Outcomes sent, by `<sender>|<id>`. */
35
+ sent: Record<string, {
36
+ at: number;
37
+ outcomes: ReceiptOutcome[];
38
+ }>;
39
+ /** When receipts went out, for the rate limits: by recipient, and all of them. */
40
+ rate: {
41
+ by: Record<string, number[]>;
42
+ all: number[];
43
+ };
44
+ }
45
+ /** Kept for as long as a message can still arrive (relays keep it 7 days), then dropped. */
46
+ export declare const RECEIPT_TTL_MS: number;
47
+ export declare const RECEIPTS_WANTED_MAX = 512;
48
+ export declare const RECEIPTS_ASKED_MAX = 1024;
49
+ /** A message has at most this many outcomes (held, then shown or declined or expired): more are dropped. */
50
+ export declare const RECEIPTS_PER_ID = 3;
51
+ /**
52
+ * Receipts this device sends in a minute: at most RECEIPT_RATE_PER_USER to one user. Past
53
+ * RECEIPT_RATE_ALL in all, a user gets only their fair share of it (RECEIPT_RATE_ALL divided among
54
+ * the users receipted this minute, this one included), so a few busy peers can't use up everyone
55
+ * else's receipts; RECEIPT_RATE_HARD is the ceiling either way (pre-release review R5).
56
+ */
57
+ export declare const RECEIPT_RATE_PER_USER = 30;
58
+ export declare const RECEIPT_RATE_ALL = 120;
59
+ export declare const RECEIPT_RATE_HARD = 240;
60
+ /** Whether one more receipt to `user` fits the limits (rate windows of the last minute). */
61
+ export declare function receiptRateAllows(rate: {
62
+ by: Record<string, number[]>;
63
+ all: number[];
64
+ }, user: string): boolean;
65
+ export declare function newReceiptId(): string;
66
+ /**
67
+ * Whether a message to `(own, sid)` may carry `rcpt`: only when the session is live now (a status
68
+ * within STATUS_LIVE_MS) and that status advertises receipts.v1 (never by substring). A client from
69
+ * before receipts rejects the whole message for an unknown field, so a stale status (the session
70
+ * since restarted on an older client, or turned receipts off) must never decide it: the 24 h
71
+ * offline path sends without one (pre-release review R2).
72
+ */
73
+ export declare function asksReceipts(app: App, own: string, sid: string): Promise<boolean>;
74
+ /** Sender: remember a message that asked for receipts. */
75
+ export declare function rememberWanted(app: App, id: string, w: Omit<ReceiptWanted, "at" | "outcomes">): Promise<void>;
76
+ /** Receiver: remember an inbound message that asked (only while receipts are on). */
77
+ export declare function rememberAsked(app: App, eventId: string, a: Omit<ReceiptAsked, "at">): Promise<void>;
78
+ export interface SendReceiptResult {
79
+ sent: boolean;
80
+ outcome?: ReceiptOutcome;
81
+ /** Why none went out: off, not-asked, private, not-yours, done, rate-limited, undelivered. */
82
+ reason?: string;
83
+ }
84
+ /**
85
+ * `p2p receipt <event> --from-session <sid> --outcome <o>`: what became of a message, reported by
86
+ * the session that has it. Never an error for "nothing to send": `{sent: false, reason}`.
87
+ */
88
+ export declare function sendReceipt(app: App, o: {
89
+ eventId: string;
90
+ fromSession: string;
91
+ outcome: string;
92
+ }): Promise<SendReceiptResult>;
93
+ /** Sender: a `receipt` envelope. Only from the user the message went to, for an id this device sent them. */
94
+ export declare function onReceipt(app: App, env: string, r: AcceptedMessageV3): Promise<NewEvent[]>;
95
+ /** `p2p receipts [on|off]`: the setting, as config.json and the environment make it. */
96
+ export declare function receiptsSetting(app: App): {
97
+ receipts: boolean;
98
+ overridden: boolean;
99
+ };