cc-peer 1.3.5 → 1.5.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.
@@ -0,0 +1,312 @@
1
+ import { EventEmitter } from "node:events";
2
+ import { z } from "zod";
3
+ //#region src/adapters/node/paths.d.ts
4
+ interface PathConfig {
5
+ homeDir?: string;
6
+ socketDir?: string;
7
+ }
8
+ //#endregion
9
+ //#region src/ports/transport.d.ts
10
+ /** Transport port: per-exchange connections and the listening inbox. */
11
+ interface Transport {
12
+ /**
13
+ * One connection, one write of the given lines (each newline-terminated), then close after a short linger. Mirrors the reference client's `Pe`.
14
+ */
15
+ connectWrite: (socketPath: string, lines: readonly string[], lingerMs?: number) => Promise<void>;
16
+ /** Probe whether a socket accepts connections (roster admission uses this). */
17
+ probe: (socketPath: string) => Promise<boolean>;
18
+ listen: (socketPath: string, onConnection: (conn: Readonly<InboundConnection>) => void) => Promise<ListeningSocket>;
19
+ }
20
+ interface InboundConnection {
21
+ /** Resolves once the kernel reports the connecting process's pid (macOS). */
22
+ peerPid: () => number | undefined;
23
+ readLines: () => AsyncIterable<string>;
24
+ close: () => void;
25
+ }
26
+ interface ListeningSocket {
27
+ socketPath: string;
28
+ close: () => Promise<void>;
29
+ }
30
+ //#endregion
31
+ //#region src/schemas/registry.d.ts
32
+ /** One entry of the ~/.claude/sessions/<pid>.json registry. */
33
+ declare const RegistryEntrySchema: z.ZodObject<{
34
+ pid: z.ZodNumber;
35
+ sessionId: z.ZodString;
36
+ cwd: z.ZodString;
37
+ startedAt: z.ZodNumber;
38
+ procStart: z.ZodString;
39
+ version: z.ZodString;
40
+ ccPeerVersion: z.ZodOptional<z.ZodString>;
41
+ peerProtocol: z.ZodNumber;
42
+ peerFeatures: z.ZodArray<z.ZodEnum<{
43
+ notify_idle: "notify_idle";
44
+ reply_across_default_dirs: "reply_across_default_dirs";
45
+ artifact_yield: "artifact_yield";
46
+ }> & {
47
+ is(value: unknown): value is "notify_idle" | "reply_across_default_dirs" | "artifact_yield";
48
+ }>;
49
+ kind: z.ZodEnum<{
50
+ interactive: "interactive";
51
+ bg: "bg";
52
+ daemon: "daemon";
53
+ "daemon-worker": "daemon-worker";
54
+ }> & {
55
+ is(value: unknown): value is "interactive" | "bg" | "daemon" | "daemon-worker";
56
+ };
57
+ entrypoint: z.ZodString;
58
+ pidDomain: z.ZodString;
59
+ messagingSocketPath: z.ZodString;
60
+ name: z.ZodOptional<z.ZodString>;
61
+ nameSource: z.ZodOptional<z.ZodEnum<{
62
+ user: "user";
63
+ peer: "peer";
64
+ derived: "derived";
65
+ collision: "collision";
66
+ auto: "auto";
67
+ hook: "hook";
68
+ }> & {
69
+ is(value: unknown): value is "user" | "peer" | "derived" | "collision" | "auto" | "hook";
70
+ }>;
71
+ nameSince: z.ZodOptional<z.ZodNumber>;
72
+ updatedAt: z.ZodNumber;
73
+ status: z.ZodOptional<z.ZodEnum<{
74
+ idle: "idle";
75
+ busy: "busy";
76
+ shell: "shell";
77
+ waiting: "waiting";
78
+ }> & {
79
+ is(value: unknown): value is "idle" | "busy" | "shell" | "waiting";
80
+ }>;
81
+ statusUpdatedAt: z.ZodOptional<z.ZodNumber>;
82
+ bridgeSessionId: z.ZodOptional<z.ZodString>;
83
+ }, z.core.$strip> & {
84
+ is(value: unknown): value is {
85
+ pid: number;
86
+ sessionId: string;
87
+ cwd: string;
88
+ startedAt: number;
89
+ procStart: string;
90
+ version: string;
91
+ peerProtocol: number;
92
+ peerFeatures: ("notify_idle" | "reply_across_default_dirs" | "artifact_yield")[];
93
+ kind: "interactive" | "bg" | "daemon" | "daemon-worker";
94
+ entrypoint: string;
95
+ pidDomain: string;
96
+ messagingSocketPath: string;
97
+ updatedAt: number;
98
+ ccPeerVersion?: string | undefined;
99
+ name?: string | undefined;
100
+ nameSource?: "user" | "peer" | "derived" | "collision" | "auto" | "hook" | undefined;
101
+ nameSince?: number | undefined;
102
+ status?: "idle" | "busy" | "shell" | "waiting" | undefined;
103
+ statusUpdatedAt?: number | undefined;
104
+ bridgeSessionId?: string | undefined;
105
+ };
106
+ };
107
+ type RegistryEntry = z.infer<typeof RegistryEntrySchema>;
108
+ //#endregion
109
+ //#region src/ports/registry-store.d.ts
110
+ /** Registry port: the ~/.claude/sessions/<pid>.json store. */
111
+ interface RegistryStore {
112
+ list: () => Promise<RegistryEntry[]>;
113
+ read: (pid: number) => Promise<RegistryEntry | undefined>;
114
+ write: (entry: RegistryEntry) => Promise<void>;
115
+ /** Heartbeat: bump updatedAt/statusUpdatedAt without rewriting the rest. */
116
+ touch: (pid: number) => Promise<void>;
117
+ remove: (pid: number) => Promise<void>;
118
+ }
119
+ //#endregion
120
+ //#region src/schemas/keyfile.d.ts
121
+ /** The auth key a session publishes next to its socket. */
122
+ declare const PeerKeyFileSchema: z.ZodObject<{
123
+ peerToken: z.ZodString;
124
+ procStart: z.ZodString;
125
+ pidDomain: z.ZodString;
126
+ }, z.core.$strip> & {
127
+ is(value: unknown): value is {
128
+ peerToken: string;
129
+ procStart: string;
130
+ pidDomain: string;
131
+ };
132
+ };
133
+ type PeerKeyFile = z.infer<typeof PeerKeyFileSchema>;
134
+ //#endregion
135
+ //#region src/ports/key-store.d.ts
136
+ /** Key-store port: auth keys published next to each session socket. */
137
+ interface KeyStore {
138
+ readForSocket: (socketPath: string) => Promise<PeerKeyFile | undefined>;
139
+ /** Write our own key file for the socket we own. */
140
+ writeForSocket: (socketPath: string, keyFile: Readonly<PeerKeyFile>) => Promise<void>;
141
+ removeForSocket: (socketPath: string) => Promise<void>;
142
+ }
143
+ //#endregion
144
+ //#region src/ports/proc-info.d.ts
145
+ /** Process-info port: the liveness and proc-start facts the roster needs. */
146
+ interface ProcInfo {
147
+ alive: (pid: number) => Promise<boolean>;
148
+ /**
149
+ * The process start string, byte-equal to `LC_ALL=C TZ=UTC ps -o lstart=` output — the exact value the receiver's registry byte-compares against.
150
+ */
151
+ lstart: (pid: number) => Promise<string | undefined>;
152
+ }
153
+ //#endregion
154
+ //#region src/ports/clock.d.ts
155
+ /** Clock port: injectable time for pacer/refill and freshness windows. */
156
+ interface Clock {
157
+ nowMs: () => number;
158
+ }
159
+ //#endregion
160
+ //#region src/schemas/wire.d.ts
161
+ declare const PeerMessageStatusSchema: z.ZodObject<{
162
+ type: z.ZodLiteral<"control">;
163
+ action: z.ZodLiteral<"peer_message_status">;
164
+ status: z.ZodEnum<{
165
+ held: "held";
166
+ delivered: "delivered";
167
+ denied: "denied";
168
+ expired: "expired";
169
+ dropped: "dropped";
170
+ }>;
171
+ reason: z.ZodString;
172
+ from: z.ZodString;
173
+ orig_msg_id: z.ZodString;
174
+ status_detail: z.ZodOptional<z.ZodString>;
175
+ drop_reason: z.ZodOptional<z.ZodEnum<{
176
+ "rate-limited": "rate-limited";
177
+ duplicate: "duplicate";
178
+ "hop-loop": "hop-loop";
179
+ "hop-runaway": "hop-runaway";
180
+ "queue-full": "queue-full";
181
+ }> & {
182
+ is(value: unknown): value is "rate-limited" | "duplicate" | "hop-loop" | "hop-runaway" | "queue-full";
183
+ }>;
184
+ dropped_msg_ids: z.ZodOptional<z.ZodArray<z.ZodString>>;
185
+ msgV: z.ZodNumber;
186
+ msg_id: z.ZodString;
187
+ }, z.core.$strip> & {
188
+ is(value: unknown): value is {
189
+ type: "control";
190
+ action: "peer_message_status";
191
+ status: "held" | "delivered" | "denied" | "expired" | "dropped";
192
+ reason: string;
193
+ from: string;
194
+ orig_msg_id: string;
195
+ msgV: number;
196
+ msg_id: string;
197
+ status_detail?: string | undefined;
198
+ drop_reason?: "rate-limited" | "duplicate" | "hop-loop" | "hop-runaway" | "queue-full" | undefined;
199
+ dropped_msg_ids?: string[] | undefined;
200
+ };
201
+ };
202
+ type PeerMessageStatus = z.infer<typeof PeerMessageStatusSchema>;
203
+ declare const PeerIdleNoticeSchema: z.ZodObject<{
204
+ type: z.ZodLiteral<"control">;
205
+ action: z.ZodLiteral<"peer_idle_notice">;
206
+ orig_msg_id: z.ZodString;
207
+ state: z.ZodEnum<{
208
+ idle: "idle";
209
+ exited: "exited";
210
+ }>;
211
+ finished_at: z.ZodNumber;
212
+ detail: z.ZodOptional<z.ZodString>;
213
+ from: z.ZodString;
214
+ from_mode: z.ZodOptional<z.ZodEnum<{
215
+ bypass: "bypass";
216
+ prompting: "prompting";
217
+ }>>;
218
+ msgV: z.ZodNumber;
219
+ msg_id: z.ZodString;
220
+ }, z.core.$strip> & {
221
+ is(value: unknown): value is {
222
+ type: "control";
223
+ action: "peer_idle_notice";
224
+ orig_msg_id: string;
225
+ state: "idle" | "exited";
226
+ finished_at: number;
227
+ from: string;
228
+ msgV: number;
229
+ msg_id: string;
230
+ detail?: string | undefined;
231
+ from_mode?: "bypass" | "prompting" | undefined;
232
+ };
233
+ };
234
+ type PeerIdleNotice = z.infer<typeof PeerIdleNoticeSchema>;
235
+ //#endregion
236
+ //#region src/cc-peer.d.ts
237
+ /** cc-peer's own package version, read from package.json and inlined at bundle time so it needs no filesystem access at runtime (the SEA build ships as a single file with no package.json alongside it). */
238
+ declare const CC_PEER_VERSION: string;
239
+ /** How a target can be addressed. */
240
+ type PeerRef = {
241
+ pid: number;
242
+ } | {
243
+ name: string;
244
+ } | {
245
+ address: string;
246
+ };
247
+ interface CcPeerOptions extends PathConfig {
248
+ name?: string;
249
+ sessionId?: string;
250
+ heartbeatMs?: number;
251
+ logger?: (message: string) => void;
252
+ }
253
+ interface SendOptions {
254
+ priority?: "next" | "later";
255
+ /** Attaches from-mode attestation; false deliberately sends unattested (recipient holds it). */
256
+ fromMode?: "bypass" | "prompting" | false;
257
+ /** When set, must match the receiver's sessionId or the frame is silently dropped. */
258
+ sessionId?: string;
259
+ }
260
+ interface InboundMessage {
261
+ from?: string;
262
+ fromSession?: string;
263
+ fromName?: string;
264
+ fromMode?: "bypass" | "prompting";
265
+ hopChain?: string[];
266
+ body: string;
267
+ msgId: string;
268
+ }
269
+ type Receipt = PeerMessageStatus;
270
+ type IdleNotice = PeerIdleNotice;
271
+ interface Deps {
272
+ transport: Transport;
273
+ registry: RegistryStore;
274
+ keys: KeyStore;
275
+ procInfo: ProcInfo;
276
+ clock: Clock;
277
+ }
278
+ /**
279
+ * A registered peer on Claude Code's cross-session mesh. Receipts and idle notices only reach the process that owns the listening socket (the protocol verifies return addresses via kernel peer pids), so do not split sending and listening across differently-owned processes.
280
+ */
281
+ declare class CcPeer extends EventEmitter {
282
+ private readonly options;
283
+ private readonly deps;
284
+ private listening;
285
+ private heartbeatTimer;
286
+ private ownKey;
287
+ private readonly pacer;
288
+ private readonly log;
289
+ private stopped;
290
+ constructor(options: Readonly<CcPeerOptions>, deps: Deps);
291
+ static create(options?: Readonly<CcPeerOptions>): Promise<CcPeer>;
292
+ /**
293
+ * Bind, publish key and registry, and begin listening. Public rather than private because dependency-injected construction (this constructor takes the full Deps) needs to trigger it explicitly; everyday callers use the static create(), which wires the real node adapters.
294
+ */
295
+ start(): Promise<void>;
296
+ /** procStart is a required parameter, not read from this.ownKey: start() already guarantees a non-empty value before this is called, so the type checker enforces it rather than a runtime fallback that can never actually fire. */
297
+ private buildRegistryEntry;
298
+ roster(): Promise<RegistryEntry[]>;
299
+ send(target: PeerRef, body: string, options?: Readonly<SendOptions>): Promise<{
300
+ msgId: string;
301
+ }>;
302
+ private pacedSend;
303
+ subscribeIdle(target: PeerRef): Promise<{
304
+ msgId: string;
305
+ }>;
306
+ private resolveTarget;
307
+ /** ownToken is passed explicitly rather than read from this.ownKey: the listener callback that invokes this is only ever registered after start() has set ownKey, so the parameter records that guarantee at the type level instead of a runtime guard that can never actually be false. */
308
+ private handleConnection;
309
+ stop(): Promise<void>;
310
+ }
311
+ //#endregion
312
+ export { InboundMessage as a, SendOptions as c, IdleNotice as i, PathConfig as l, CcPeer as n, PeerRef as o, CcPeerOptions as r, Receipt as s, CC_PEER_VERSION as t };
@@ -1,3 +1,4 @@
1
+ import { a as UnknownPeerError, i as NotStartedError, n as MessageTooLargeError, o as defineSchema, r as NoLiveInboxError } from "./errors-Dcqq2MIW.mjs";
1
2
  import { EventEmitter } from "node:events";
2
3
  import { createHash, randomBytes, randomUUID } from "node:crypto";
3
4
  import { connect, createServer } from "node:net";
@@ -6,6 +7,9 @@ import { dirname, join } from "node:path";
6
7
  import { z } from "zod";
7
8
  import { homedir } from "node:os";
8
9
  import { spawn } from "node:child_process";
10
+ //#region package.json
11
+ var version = "1.5.0";
12
+ //#endregion
9
13
  //#region src/adapters/node/uds-transport.ts
10
14
  /** macOS linger before close, matching the reference client's ~150ms. */
11
15
  const DEFAULT_LINGER_MS = 150;
@@ -129,16 +133,6 @@ var SystemClock = class {
129
133
  return Date.now();
130
134
  }
131
135
  };
132
- //#endregion
133
- //#region src/schemas/define-schema.ts
134
- /**
135
- * Attach an `.is()` type guard to a Zod schema so schema, inferred type, and runtime guard derive from one definition (single source of truth). `Schema.parse()` at JSON boundaries; `Schema.is()` for narrowing.
136
- */
137
- function defineSchema(schema) {
138
- return Object.assign(schema, { is(value) {
139
- return schema.safeParse(value).success;
140
- } });
141
- }
142
136
  const hex = (bytes) => bytes * 2;
143
137
  const PEER_TOKEN_HEX_LENGTH = hex(16);
144
138
  const HOP_ID_HEX_LENGTH = hex(12);
@@ -293,6 +287,8 @@ const RegistryEntrySchema = defineSchema(z.object({
293
287
  startedAt: z.number().int().nonnegative(),
294
288
  procStart: z.string().min(1),
295
289
  version: z.string().min(1),
290
+ /** cc-peer's own package version, present on every entry cc-peer itself writes regardless of kind; absent on an entry a real interactive Claude Code session wrote directly, since only cc-peer knows to populate it. Distinct from `version`, which is overloaded between a real Claude Code app version and cc-peer's own sentinel string depending on who wrote the entry. */
291
+ ccPeerVersion: z.string().min(1).optional(),
296
292
  peerProtocol: z.number().int(),
297
293
  peerFeatures: z.array(PeerFeatureSchema),
298
294
  kind: SessionKindSchema,
@@ -738,44 +734,9 @@ const ControlFrameSchema = z.union([
738
734
  ]);
739
735
  const WireFrameSchema = z.union([UserFrameSchema, ControlFrameSchema]);
740
736
  //#endregion
741
- //#region src/errors.ts
742
- /** Base error for every cc-peer failure; `code` is machine-readable. */
743
- var CcPeerError = class extends Error {
744
- code;
745
- constructor(code, message) {
746
- super(message);
747
- this.code = code;
748
- this.name = "CcPeerError";
749
- }
750
- };
751
- var NoLiveInboxError = class extends CcPeerError {
752
- constructor(message) {
753
- super("NO_LIVE_INBOX", message);
754
- this.name = "NoLiveInboxError";
755
- }
756
- };
757
- var UnknownPeerError = class extends CcPeerError {
758
- constructor(message) {
759
- super("UNKNOWN_PEER", message);
760
- this.name = "UnknownPeerError";
761
- }
762
- };
763
- var MessageTooLargeError = class extends CcPeerError {
764
- constructor(message) {
765
- super("MESSAGE_TOO_LARGE", message);
766
- this.name = "MessageTooLargeError";
767
- }
768
- };
769
- var NotStartedError = class extends CcPeerError {
770
- constructor(message) {
771
- super("NOT_STARTED", message);
772
- this.name = "NotStartedError";
773
- }
774
- };
775
- //#endregion
776
737
  //#region src/cc-peer.ts
777
- /** Package version placeholder until semantic-release owns it. */
778
- const CC_PEER_VERSION = "0.0.0";
738
+ /** cc-peer's own package version, read from package.json and inlined at bundle time so it needs no filesystem access at runtime (the SEA build ships as a single file with no package.json alongside it). */
739
+ const CC_PEER_VERSION = version;
779
740
  /** Default log sink: logs go nowhere unless a logger is provided. */
780
741
  const sinkLog = () => void 0;
781
742
  /** Peer tokens are 16 random bytes in hex, matching the reference key files. */
@@ -851,6 +812,7 @@ var CcPeer = class CcPeer extends EventEmitter {
851
812
  startedAt: now,
852
813
  procStart,
853
814
  version: "cc-peer",
815
+ ccPeerVersion: CC_PEER_VERSION,
854
816
  peerProtocol: 1,
855
817
  peerFeatures: ["notify_idle", "reply_across_default_dirs"],
856
818
  kind: "interactive",
@@ -995,4 +957,4 @@ var CcPeer = class CcPeer extends EventEmitter {
995
957
  }
996
958
  };
997
959
  //#endregion
998
- export { defineSchema as a, RegistryEntrySchema as i, CcPeer as n, PrioritySchema as r, CC_PEER_VERSION as t };
960
+ export { RegistryEntrySchema as i, CcPeer as n, PrioritySchema as r, CC_PEER_VERSION as t };