@peer2peer/cli 0.1.0 → 0.1.2
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 +21 -15
- package/dist/app.d.ts +83 -0
- package/dist/app.js +155 -40
- package/dist/cli.d.ts +76 -6
- package/dist/cli.js +386 -34
- package/dist/config.d.ts +52 -0
- package/dist/config.js +51 -0
- package/dist/doctor.d.ts +84 -1
- package/dist/doctor.js +198 -6
- package/dist/ext-host.d.ts +6 -0
- package/dist/ext-host.js +41 -3
- package/dist/extensions.d.ts +101 -0
- package/dist/extensions.js +13 -2
- package/dist/graph-memory.d.ts +107 -0
- package/dist/graph-memory.js +393 -0
- package/dist/history.d.ts +155 -0
- package/dist/history.js +697 -0
- package/dist/identity.d.ts +31 -2
- package/dist/identity.js +80 -10
- package/dist/inbound.js +4 -1
- package/dist/ix-local.d.ts +128 -0
- package/dist/ix-local.js +260 -0
- package/dist/pretty.d.ts +3 -1
- package/dist/pretty.js +9 -2
- package/dist/projects.d.ts +330 -12
- package/dist/projects.js +1044 -74
- package/dist/receipts.js +3 -1
- package/dist/rotation.js +15 -7
- package/dist/sessions.d.ts +28 -5
- package/dist/sessions.js +182 -30
- package/dist/social.js +11 -0
- package/dist/state.d.ts +26 -0
- package/dist/state.js +3 -0
- package/dist/tui/model.d.ts +7 -1
- package/dist/tui/model.js +8 -4
- package/dist/tui/tui.d.ts +9 -2
- package/dist/tui/tui.js +47 -6
- package/dist/tui/view.js +6 -4
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/watch.js +6 -0
- package/package.json +6 -3
package/dist/extensions.d.ts
CHANGED
|
@@ -72,6 +72,12 @@ export interface ExtCommand {
|
|
|
72
72
|
}
|
|
73
73
|
export interface P2PExtension {
|
|
74
74
|
api: number;
|
|
75
|
+
/**
|
|
76
|
+
* The graph-target contract this extension implements (1: every Ix call takes its endpoint only from
|
|
77
|
+
* ExtContext.graphTarget). Required of the `ix` namespace (REQUIRED_GRAPH_TARGETS): an ext-ix from
|
|
78
|
+
* before it would run Ix with the caller's environment and config, which Ix Pro routes to the cloud.
|
|
79
|
+
*/
|
|
80
|
+
graphTargets?: number;
|
|
75
81
|
/** Its §8.1 namespace: the package must be `@peer2peer/ext-<ns>`. */
|
|
76
82
|
ns: string;
|
|
77
83
|
name: string;
|
|
@@ -87,7 +93,65 @@ export interface P2PExtension {
|
|
|
87
93
|
* advertises `<kind>.v1` for each.
|
|
88
94
|
*/
|
|
89
95
|
state?: Record<string, ExtStateKind>;
|
|
96
|
+
/**
|
|
97
|
+
* Graph memory (epic #41 piece 2): writes this device's own sessions' context (their record, status
|
|
98
|
+
* one-liners, the files they edit) into the person's local graph. `p2p watch` gathers it from the
|
|
99
|
+
* statuses this device posted, queues it (bounded) and hands it over in batches, off the hook path.
|
|
100
|
+
* Optional: an extension without it (an older ext-ix) is simply never asked. Each batch is for one
|
|
101
|
+
* session and has already been checked local by graphTarget; the extension checks again.
|
|
102
|
+
*/
|
|
103
|
+
memory?: ExtMemory;
|
|
104
|
+
}
|
|
105
|
+
/** One thing to remember about a session (graph-memory.ts). Every string is local, the person's own. */
|
|
106
|
+
export interface ExtMemoryItem {
|
|
107
|
+
kind: "start" | "status" | "files" | "end";
|
|
108
|
+
/** When it happened (ms). */
|
|
109
|
+
at: number;
|
|
110
|
+
/** start: the session's label, repository and branch. */
|
|
111
|
+
label?: string;
|
|
112
|
+
repo?: string;
|
|
113
|
+
branch?: string;
|
|
114
|
+
/** status: the one-line status. */
|
|
115
|
+
summary?: string;
|
|
116
|
+
/** files: repo-relative files edited since the last batch. */
|
|
117
|
+
files?: string[];
|
|
118
|
+
}
|
|
119
|
+
/** Why memory can't be written now; `message` says what the person can do. */
|
|
120
|
+
export interface ExtMemoryUnavailable {
|
|
121
|
+
ok: false;
|
|
122
|
+
/** e.g. `ix_missing`, `ix_upgrade` (no local `ix note`), `ix_pro` (Ix Pro answers: cloud). */
|
|
123
|
+
reason: string;
|
|
124
|
+
message: string;
|
|
125
|
+
}
|
|
126
|
+
export interface ExtMemoryResult {
|
|
127
|
+
written: number;
|
|
128
|
+
/** Items not written, by reason (counted as dropped). */
|
|
129
|
+
failed?: {
|
|
130
|
+
reason: string;
|
|
131
|
+
n: number;
|
|
132
|
+
message?: string;
|
|
133
|
+
}[];
|
|
134
|
+
/** Stop asking for now (the same as available() turning false). */
|
|
135
|
+
unavailable?: ExtMemoryUnavailable;
|
|
90
136
|
}
|
|
137
|
+
export interface ExtMemory {
|
|
138
|
+
v: 1;
|
|
139
|
+
/** Whether the writes can go to the local graph at all (asked before anything is gathered). */
|
|
140
|
+
available(ctx: ExtContext): Promise<{
|
|
141
|
+
ok: true;
|
|
142
|
+
} | ExtMemoryUnavailable>;
|
|
143
|
+
/**
|
|
144
|
+
* Writes one session's items. `signal` aborts when the host's time for the batch is up: the writer
|
|
145
|
+
* must check it before every write and write nothing after it fires (the host waits for it to settle).
|
|
146
|
+
*/
|
|
147
|
+
write(ctx: ExtContext, batch: {
|
|
148
|
+
sid: string;
|
|
149
|
+
items: ExtMemoryItem[];
|
|
150
|
+
signal?: AbortSignal;
|
|
151
|
+
}): Promise<ExtMemoryResult>;
|
|
152
|
+
}
|
|
153
|
+
/** `ext.memory` when it is a usable v1 memory writer; undefined otherwise (absent or malformed: never asked). */
|
|
154
|
+
export declare function memoryOf(ext: P2PExtension | undefined): ExtMemory | undefined;
|
|
91
155
|
export interface ExtStateKind {
|
|
92
156
|
/** Seconds a value stays valid (EXT_STATE_TTL_MIN-MAX); default EXT_STATE_TTL_DEFAULT. */
|
|
93
157
|
ttl?: number;
|
|
@@ -227,11 +291,39 @@ export interface ExtAudience {
|
|
|
227
291
|
/** Distinct verified organisations among them (a trust provider's labels); absent when unknown. */
|
|
228
292
|
orgs?: number;
|
|
229
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* Which Ix graph a call may reach (projects.ts graphTarget, the chokepoint): this machine's backend,
|
|
296
|
+
* or one circle's shared graph. An extension that runs Ix takes the endpoint (and any token) only
|
|
297
|
+
* from here, never from its environment.
|
|
298
|
+
*/
|
|
299
|
+
export type ExtGraphTarget =
|
|
300
|
+
/** `trustRootListener`: the person set `ixTrustRootListener: true` in config.json (a root-owned listener on the port, Docker's proxy on Linux, is theirs). */
|
|
301
|
+
{
|
|
302
|
+
kind: "local";
|
|
303
|
+
endpoint: string;
|
|
304
|
+
why: string;
|
|
305
|
+
trustRootListener?: true;
|
|
306
|
+
} | {
|
|
307
|
+
kind: "shared";
|
|
308
|
+
circle: string;
|
|
309
|
+
endpoint: string;
|
|
310
|
+
token?: string;
|
|
311
|
+
};
|
|
230
312
|
/** What a command gets from `p2p`: no keys, no store, only these. */
|
|
231
313
|
export interface ExtContext {
|
|
232
314
|
env: Env;
|
|
233
315
|
/** The user's working directory: P2P_WORKDIR (an existing absolute directory), else the process's. */
|
|
234
316
|
cwd: string;
|
|
317
|
+
/**
|
|
318
|
+
* The graph target for a call made for session `sid` (when given), working in this context's folders
|
|
319
|
+
* (P2P_WORKDIR, P2P_PROJECT_DIRS and the process's cwd) and naming `paths` (absolute: the call's
|
|
320
|
+
* targets, `--path`, `--changed`, the child's cwd; every one is intersected in): projects.ts
|
|
321
|
+
* graphTarget. Absent on clients from before graph targets: an extension then treats every call as local.
|
|
322
|
+
*/
|
|
323
|
+
graphTarget?(opts?: {
|
|
324
|
+
sid?: string;
|
|
325
|
+
paths?: readonly string[];
|
|
326
|
+
}): Promise<ExtGraphTarget>;
|
|
235
327
|
/** stdout lines are for `--json`-less output only; use the returned `human`. */
|
|
236
328
|
io: {
|
|
237
329
|
err(s: string): void;
|
|
@@ -287,6 +379,13 @@ export interface ExtContext {
|
|
|
287
379
|
* file tools): store the MAC with the setting and honour the setting only when it verifies.
|
|
288
380
|
*/
|
|
289
381
|
localMac?(data: string): string;
|
|
382
|
+
/**
|
|
383
|
+
* Whether session `fromSid` may pass on context recorded for session `ofSid` (graph memory's
|
|
384
|
+
* records, ExtMemory): the same session, or both bound to projects whose ceilings are shared, with
|
|
385
|
+
* `fromSid`'s audience within `ofSid`'s and the same graph target. Absent on older clients: a
|
|
386
|
+
* caller then refuses anything but the same session.
|
|
387
|
+
*/
|
|
388
|
+
mayShareSessionContext?(ofSid: string, fromSid: string): Promise<boolean>;
|
|
290
389
|
/** Own sessions' last posted statuses on this device. */
|
|
291
390
|
ownStatus(sid: string): Promise<{
|
|
292
391
|
repo?: string;
|
|
@@ -322,6 +421,8 @@ export interface LoadOptions {
|
|
|
322
421
|
/** Overrides where this CLI is installed (tests). */
|
|
323
422
|
selfUrl?: string;
|
|
324
423
|
}
|
|
424
|
+
/** Namespaces that run Ix, and the graph-target contract version each must declare to load. */
|
|
425
|
+
export declare const REQUIRED_GRAPH_TARGETS: Readonly<Record<string, number>>;
|
|
325
426
|
/** Validates what a module exports; undefined (with a reason) when it isn't a usable extension. */
|
|
326
427
|
export declare function checkExtension(mod: unknown, ns: string): {
|
|
327
428
|
ext?: P2PExtension;
|
package/dist/extensions.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
// can reach (the home directory, the identity and device keys). ExtContext
|
|
13
13
|
// is the API it is meant to use, not a sandbox. So loading is opt-in:
|
|
14
14
|
// - Which: only namespaces registered in protocol §8.3 (KNOWN_EXTENSIONS)
|
|
15
|
-
// that the user enabled: P2P_EXTENSIONS ("ix
|
|
15
|
+
// that the user enabled: P2P_EXTENSIONS ("ix"; "none" = none), else
|
|
16
16
|
// `extensions` in <home>/config.json (`p2p extensions enable ix`). Nothing
|
|
17
17
|
// enabled, nothing loaded.
|
|
18
18
|
// - Where: only explicit node_modules directories: each P2P_EXTENSION_PATH
|
|
@@ -44,7 +44,7 @@ import { extensionKindCap, isExtensionKind, isExtStateTtl, sanitizePeerText } fr
|
|
|
44
44
|
/** The seam's version; an extension declares the one it was built against. */
|
|
45
45
|
export const EXTENSION_API = 1;
|
|
46
46
|
/** Namespaces registered in docs/protocol.md §8.3: the only ones that can be enabled. */
|
|
47
|
-
export const KNOWN_EXTENSIONS = ["ix"
|
|
47
|
+
export const KNOWN_EXTENSIONS = ["ix"];
|
|
48
48
|
/** Cap on the JSON a `receive` digest may add to an event. */
|
|
49
49
|
export const MAX_RENDERED_BYTES = 16 * 1024;
|
|
50
50
|
/** Cap on the validated body kept with an event (for user-initiated actions later). */
|
|
@@ -58,6 +58,11 @@ export function extensionPackageName(ns) {
|
|
|
58
58
|
export function kindNamespace(kind) {
|
|
59
59
|
return isExtensionKind(kind) ? kind.slice(2, kind.indexOf(".")) : undefined;
|
|
60
60
|
}
|
|
61
|
+
/** `ext.memory` when it is a usable v1 memory writer; undefined otherwise (absent or malformed: never asked). */
|
|
62
|
+
export function memoryOf(ext) {
|
|
63
|
+
const m = ext?.memory;
|
|
64
|
+
return m && typeof m === "object" && m.v === 1 && typeof m.available === "function" && typeof m.write === "function" ? m : undefined;
|
|
65
|
+
}
|
|
61
66
|
export class ExtensionRegistry {
|
|
62
67
|
byNs = new Map();
|
|
63
68
|
constructor(list = []) {
|
|
@@ -76,6 +81,8 @@ export class ExtensionRegistry {
|
|
|
76
81
|
return [...this.byNs.values()];
|
|
77
82
|
}
|
|
78
83
|
}
|
|
84
|
+
/** Namespaces that run Ix, and the graph-target contract version each must declare to load. */
|
|
85
|
+
export const REQUIRED_GRAPH_TARGETS = { ix: 1 };
|
|
79
86
|
/** Validates what a module exports; undefined (with a reason) when it isn't a usable extension. */
|
|
80
87
|
export function checkExtension(mod, ns) {
|
|
81
88
|
const m = mod;
|
|
@@ -86,6 +93,10 @@ export function checkExtension(mod, ns) {
|
|
|
86
93
|
return { why: `built for extension API ${String(ext.api)}, this p2p has ${EXTENSION_API}` };
|
|
87
94
|
if (ext.ns !== ns)
|
|
88
95
|
return { why: `declares namespace ${String(ext.ns)}, expected ${ns}` };
|
|
96
|
+
const need = REQUIRED_GRAPH_TARGETS[ns];
|
|
97
|
+
if (need !== undefined && !(typeof ext.graphTargets === "number" && ext.graphTargets >= need)) {
|
|
98
|
+
return { why: `it predates graph targets (it would run Ix with your environment and config, which Ix Pro can route to the cloud): update ${extensionPackageName(ns)} (npm i -g ${extensionPackageName(ns)}@latest; the Claude Code mod ships a current copy)` };
|
|
99
|
+
}
|
|
89
100
|
if (ext.receive !== undefined && typeof ext.receive !== "function")
|
|
90
101
|
return { why: "receive is not a function" };
|
|
91
102
|
if (ext.state !== undefined) {
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { App } from "./app.js";
|
|
2
|
+
import { type ExtMemory, type ExtMemoryItem, type ExtMemoryUnavailable, type P2PExtension } from "./extensions.js";
|
|
3
|
+
import { type GraphTarget } from "./projects.js";
|
|
4
|
+
import type { PeerStatusRec } from "./state.js";
|
|
5
|
+
/** How often `p2p watch` gathers and drains (P2P_IX_MEMORY_TICK_MS, 1-60 s, for tests). */
|
|
6
|
+
export declare const GRAPH_MEMORY_TICK_MS = 5000;
|
|
7
|
+
/** At most this many items wait; past it, new ones are dropped (counted as `queue_full`). */
|
|
8
|
+
export declare const GRAPH_MEMORY_QUEUE_MAX = 64;
|
|
9
|
+
/** At most this many items are handed over in one drain. */
|
|
10
|
+
export declare const GRAPH_MEMORY_BATCH = 16;
|
|
11
|
+
/** At most this many sessions are tracked (the oldest ended ones go first). */
|
|
12
|
+
export declare const GRAPH_MEMORY_SESSIONS_MAX = 64;
|
|
13
|
+
/** At most this many files are remembered as recorded per session. */
|
|
14
|
+
export declare const GRAPH_MEMORY_FILES_MAX = 200;
|
|
15
|
+
/** A session first seen with no status newer than this isn't recorded (one from before an upgrade). */
|
|
16
|
+
export declare const GRAPH_MEMORY_FRESH_MS: number;
|
|
17
|
+
/** While memory is unavailable, it is asked again this often (Ix installed or upgraded meanwhile). */
|
|
18
|
+
export declare const GRAPH_MEMORY_RECHECK_MS: number;
|
|
19
|
+
/** One drain gives up waiting on the extension after this long (its `ix` calls are bounded too). */
|
|
20
|
+
export declare const GRAPH_MEMORY_DRAIN_MS = 120000;
|
|
21
|
+
export type GraphMemoryItem = ExtMemoryItem & {
|
|
22
|
+
sid: string;
|
|
23
|
+
};
|
|
24
|
+
export interface GraphMemorySession {
|
|
25
|
+
/** When the session's start was queued (ms). */
|
|
26
|
+
start: number;
|
|
27
|
+
/** The last status one-liner queued. */
|
|
28
|
+
summary?: string;
|
|
29
|
+
/** Files already queued (capped). */
|
|
30
|
+
files: string[];
|
|
31
|
+
/** When its end was queued. */
|
|
32
|
+
ended?: number;
|
|
33
|
+
}
|
|
34
|
+
export interface GraphMemoryDoc {
|
|
35
|
+
v: 1;
|
|
36
|
+
sessions: Record<string, GraphMemorySession>;
|
|
37
|
+
queue: GraphMemoryItem[];
|
|
38
|
+
/** Items written to the local graph. */
|
|
39
|
+
written: number;
|
|
40
|
+
/** Items dropped, by reason (queue_full, not_local, ix_failed, ix_timeout, graph_target, ix_unconfirmed, …). */
|
|
41
|
+
dropped: Record<string, number>;
|
|
42
|
+
lastWriteAt?: number;
|
|
43
|
+
lastDropAt?: number;
|
|
44
|
+
/** Why memory is off now (no Ix, an Ix without local notes, Ix Pro), as the extension said it. */
|
|
45
|
+
unavailable?: {
|
|
46
|
+
reason: string;
|
|
47
|
+
message: string;
|
|
48
|
+
at: number;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Sessions a drain is writing now, until when (ms): one writer per session at a time, across
|
|
52
|
+
* every process (take() skips them), so a session's record is never created twice.
|
|
53
|
+
*/
|
|
54
|
+
leases?: Record<string, number>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* What changed in this device's own sessions since the last look, as items, queued (pure, for
|
|
58
|
+
* tests: `ended` says whether a session's watch is gone). Returns the next doc and how many were
|
|
59
|
+
* queued and dropped. Only sessions `recordable` says yes to are gathered: a session whose project
|
|
60
|
+
* ceiling isn't shared (a private or unmapped project, `.p2p-private`, not bound yet) is never
|
|
61
|
+
* recorded, and one that becomes so stops being recorded (fail closed: its context never reaches a
|
|
62
|
+
* graph that a shared session's agent could read and pass on).
|
|
63
|
+
*/
|
|
64
|
+
export declare function gather(doc: GraphMemoryDoc, recs: readonly PeerStatusRec[], ended: (sid: string) => boolean, now?: number, recordable?: (sid: string) => boolean): {
|
|
65
|
+
doc: GraphMemoryDoc;
|
|
66
|
+
queued: number;
|
|
67
|
+
dropped: number;
|
|
68
|
+
};
|
|
69
|
+
/** Gathers and queues what changed (under the state lock). */
|
|
70
|
+
export declare function gatherGraphMemory(app: App, now?: number): Promise<{
|
|
71
|
+
queued: number;
|
|
72
|
+
dropped: number;
|
|
73
|
+
}>;
|
|
74
|
+
export interface DrainDeps {
|
|
75
|
+
/** The graph target of a session's batch (default: the chokepoint, projects.ts graphTarget). */
|
|
76
|
+
target?: (sid: string) => Promise<GraphTarget>;
|
|
77
|
+
log?: (msg: string) => void;
|
|
78
|
+
/** One session's time to write (tests); default GRAPH_MEMORY_DRAIN_MS. */
|
|
79
|
+
deadlineMs?: number;
|
|
80
|
+
/** After the abort, how long to wait for the writer to settle (tests); default GRAPH_MEMORY_SETTLE_MS. */
|
|
81
|
+
settleMs?: number;
|
|
82
|
+
}
|
|
83
|
+
/** After a timed-out batch is aborted, how long the drain waits for the writer to settle before giving up on it. */
|
|
84
|
+
export declare const GRAPH_MEMORY_SETTLE_MS = 30000;
|
|
85
|
+
/** How long a session's lease lasts: past every drain's own bound, then free again (a crashed writer). */
|
|
86
|
+
export declare const GRAPH_MEMORY_LEASE_MS: number;
|
|
87
|
+
/**
|
|
88
|
+
* Hands the next batch to the extension, a session at a time. A session whose graph target isn't
|
|
89
|
+
* local is never handed over: its items are dropped as `not_local`. Returns what happened.
|
|
90
|
+
*/
|
|
91
|
+
export declare function drainGraphMemory(app: App, ext: P2PExtension, mem: ExtMemory, deps?: DrainDeps): Promise<{
|
|
92
|
+
written: number;
|
|
93
|
+
dropped: number;
|
|
94
|
+
unavailable?: ExtMemoryUnavailable;
|
|
95
|
+
}>;
|
|
96
|
+
/** The tick interval (P2P_IX_MEMORY_TICK_MS, 1-60 s). */
|
|
97
|
+
export declare function graphMemoryTickMs(env: Record<string, string | undefined>): number;
|
|
98
|
+
/**
|
|
99
|
+
* The loop `p2p watch` runs: `tick()` gathers and drains when memory is on and available (asked of
|
|
100
|
+
* the extension at most every GRAPH_MEMORY_RECHECK_MS while it isn't). Never throws; one tick at a
|
|
101
|
+
* time.
|
|
102
|
+
*/
|
|
103
|
+
export declare function graphMemoryLoop(app: App, deps?: DrainDeps): {
|
|
104
|
+
tick(): Promise<void>;
|
|
105
|
+
};
|
|
106
|
+
/** What `p2p doctor` says about graph memory (undefined: nothing recorded yet). */
|
|
107
|
+
export declare function graphMemoryReport(app: App): Promise<GraphMemoryDoc | undefined>;
|