@fro.bot/space-bus 0.4.0 → 0.6.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.
package/dist/contract.js CHANGED
@@ -1,68 +1,84 @@
1
1
  // src/contract.ts
2
2
  import { z } from "zod";
3
- var sessionSchema = z.object({
3
+ var sessionSchema = z.looseObject({
4
4
  id: z.string(),
5
5
  directory: z.string().optional(),
6
6
  title: z.string().optional()
7
- }).passthrough();
7
+ });
8
8
  var sessionListSchema = z.array(sessionSchema);
9
- var sessionStatusMapSchema = z.record(z.string(), z.object({ type: z.string() }).passthrough());
10
- var todoSchema = z.array(z.object({ content: z.string(), status: z.string(), priority: z.string() }).passthrough()).default([]);
11
- var diffEntrySchema = z.object({
9
+ var sessionStatusMapSchema = z.record(z.string(), z.looseObject({ type: z.string() }));
10
+ var todoSchema = z.array(z.looseObject({
11
+ content: z.string(),
12
+ status: z.string(),
13
+ priority: z.string()
14
+ })).default([]);
15
+ var diffEntrySchema = z.looseObject({
12
16
  file: z.string().optional(),
13
17
  additions: z.number(),
14
18
  deletions: z.number(),
15
19
  status: z.string().optional()
16
- }).passthrough();
20
+ });
17
21
  var diffSchema = z.array(diffEntrySchema).default([]);
18
- var vcsStatusEntrySchema = z.object({
22
+ var vcsStatusEntrySchema = z.looseObject({
19
23
  file: z.string(),
20
24
  additions: z.number(),
21
25
  deletions: z.number(),
22
26
  status: z.string().optional()
23
- }).passthrough();
27
+ });
24
28
  var vcsStatusSchema = z.array(vcsStatusEntrySchema).default([]);
25
29
  var turnDiffEntrySchema = diffEntrySchema;
26
- var turnMessageSchema = z.object({
27
- info: z.object({
30
+ var turnMessageSchema = z.looseObject({
31
+ info: z.looseObject({
28
32
  role: z.string(),
29
- summary: z.object({
33
+ summary: z.looseObject({
30
34
  diffs: z.array(turnDiffEntrySchema).optional()
31
- }).passthrough().optional()
32
- }).passthrough()
33
- }).passthrough();
35
+ }).optional()
36
+ })
37
+ });
34
38
  var turnMessageListSchema = z.array(turnMessageSchema);
35
- var sessionSummarySchema = z.object({
36
- summary: z.object({
39
+ var sessionSummarySchema = z.looseObject({
40
+ summary: z.looseObject({
37
41
  diffs: z.array(turnDiffEntrySchema).optional()
38
- }).passthrough().optional()
39
- }).passthrough();
40
- var messagePartSchema = z.object({ type: z.string(), text: z.string().optional() }).passthrough();
41
- var messageEnvelopeSchema = z.object({
42
- info: z.object({ role: z.string() }).passthrough(),
42
+ }).optional()
43
+ });
44
+ var messagePartSchema = z.looseObject({
45
+ type: z.string(),
46
+ text: z.string().optional()
47
+ });
48
+ var messageEnvelopeSchema = z.looseObject({
49
+ info: z.looseObject({ role: z.string() }),
43
50
  parts: z.array(messagePartSchema)
44
- }).passthrough();
51
+ });
45
52
  var messageListSchema = z.array(messageEnvelopeSchema);
46
- var pendingQuestionEntrySchema = z.object({
53
+ var pendingQuestionEntrySchema = z.looseObject({
47
54
  id: z.string(),
48
55
  sessionID: z.string(),
49
- questions: z.array(z.object({
56
+ questions: z.array(z.looseObject({
50
57
  question: z.string().optional(),
51
- options: z.array(z.object({ label: z.string().optional() }).passthrough()).optional()
52
- }).passthrough()).optional()
53
- }).passthrough();
58
+ options: z.array(z.looseObject({ label: z.string().optional() })).optional()
59
+ })).optional()
60
+ });
54
61
  var pendingQuestionListSchema = z.array(pendingQuestionEntrySchema);
55
- var questionEntrySchema = z.object({ id: z.string(), sessionID: z.string() }).passthrough();
62
+ var questionEntrySchema = z.looseObject({
63
+ id: z.string(),
64
+ sessionID: z.string()
65
+ });
56
66
  var questionListSchema = z.array(questionEntrySchema);
57
- var projectSchema = z.object({
67
+ var LOOPBACK_HOSTS = new Set([
68
+ "127.0.0.1",
69
+ "::1",
70
+ "[::1]",
71
+ "localhost"
72
+ ]);
73
+ var projectSchema = z.looseObject({
58
74
  name: z.string(),
59
75
  path: z.string(),
60
76
  description: z.string(),
61
77
  expandedPath: z.string(),
62
78
  exists: z.boolean()
63
- }).passthrough();
79
+ });
64
80
  var rosterSchema = z.object({
65
- server: z.object({ baseUrl: z.string().url() }),
81
+ server: z.object({ baseUrl: z.url() }),
66
82
  projects: z.array(projectSchema)
67
83
  });
68
84
  var credentialsSchema = z.object({
@@ -96,5 +112,6 @@ export {
96
112
  diffSchema,
97
113
  diffEntrySchema,
98
114
  credentialsSchema,
99
- busContextSchema
115
+ busContextSchema,
116
+ LOOPBACK_HOSTS
100
117
  };
package/dist/core.js CHANGED
@@ -1,68 +1,84 @@
1
1
  // src/contract.ts
2
2
  import { z } from "zod";
3
- var sessionSchema = z.object({
3
+ var sessionSchema = z.looseObject({
4
4
  id: z.string(),
5
5
  directory: z.string().optional(),
6
6
  title: z.string().optional()
7
- }).passthrough();
7
+ });
8
8
  var sessionListSchema = z.array(sessionSchema);
9
- var sessionStatusMapSchema = z.record(z.string(), z.object({ type: z.string() }).passthrough());
10
- var todoSchema = z.array(z.object({ content: z.string(), status: z.string(), priority: z.string() }).passthrough()).default([]);
11
- var diffEntrySchema = z.object({
9
+ var sessionStatusMapSchema = z.record(z.string(), z.looseObject({ type: z.string() }));
10
+ var todoSchema = z.array(z.looseObject({
11
+ content: z.string(),
12
+ status: z.string(),
13
+ priority: z.string()
14
+ })).default([]);
15
+ var diffEntrySchema = z.looseObject({
12
16
  file: z.string().optional(),
13
17
  additions: z.number(),
14
18
  deletions: z.number(),
15
19
  status: z.string().optional()
16
- }).passthrough();
20
+ });
17
21
  var diffSchema = z.array(diffEntrySchema).default([]);
18
- var vcsStatusEntrySchema = z.object({
22
+ var vcsStatusEntrySchema = z.looseObject({
19
23
  file: z.string(),
20
24
  additions: z.number(),
21
25
  deletions: z.number(),
22
26
  status: z.string().optional()
23
- }).passthrough();
27
+ });
24
28
  var vcsStatusSchema = z.array(vcsStatusEntrySchema).default([]);
25
29
  var turnDiffEntrySchema = diffEntrySchema;
26
- var turnMessageSchema = z.object({
27
- info: z.object({
30
+ var turnMessageSchema = z.looseObject({
31
+ info: z.looseObject({
28
32
  role: z.string(),
29
- summary: z.object({
33
+ summary: z.looseObject({
30
34
  diffs: z.array(turnDiffEntrySchema).optional()
31
- }).passthrough().optional()
32
- }).passthrough()
33
- }).passthrough();
35
+ }).optional()
36
+ })
37
+ });
34
38
  var turnMessageListSchema = z.array(turnMessageSchema);
35
- var sessionSummarySchema = z.object({
36
- summary: z.object({
39
+ var sessionSummarySchema = z.looseObject({
40
+ summary: z.looseObject({
37
41
  diffs: z.array(turnDiffEntrySchema).optional()
38
- }).passthrough().optional()
39
- }).passthrough();
40
- var messagePartSchema = z.object({ type: z.string(), text: z.string().optional() }).passthrough();
41
- var messageEnvelopeSchema = z.object({
42
- info: z.object({ role: z.string() }).passthrough(),
42
+ }).optional()
43
+ });
44
+ var messagePartSchema = z.looseObject({
45
+ type: z.string(),
46
+ text: z.string().optional()
47
+ });
48
+ var messageEnvelopeSchema = z.looseObject({
49
+ info: z.looseObject({ role: z.string() }),
43
50
  parts: z.array(messagePartSchema)
44
- }).passthrough();
51
+ });
45
52
  var messageListSchema = z.array(messageEnvelopeSchema);
46
- var pendingQuestionEntrySchema = z.object({
53
+ var pendingQuestionEntrySchema = z.looseObject({
47
54
  id: z.string(),
48
55
  sessionID: z.string(),
49
- questions: z.array(z.object({
56
+ questions: z.array(z.looseObject({
50
57
  question: z.string().optional(),
51
- options: z.array(z.object({ label: z.string().optional() }).passthrough()).optional()
52
- }).passthrough()).optional()
53
- }).passthrough();
58
+ options: z.array(z.looseObject({ label: z.string().optional() })).optional()
59
+ })).optional()
60
+ });
54
61
  var pendingQuestionListSchema = z.array(pendingQuestionEntrySchema);
55
- var questionEntrySchema = z.object({ id: z.string(), sessionID: z.string() }).passthrough();
62
+ var questionEntrySchema = z.looseObject({
63
+ id: z.string(),
64
+ sessionID: z.string()
65
+ });
56
66
  var questionListSchema = z.array(questionEntrySchema);
57
- var projectSchema = z.object({
67
+ var LOOPBACK_HOSTS = new Set([
68
+ "127.0.0.1",
69
+ "::1",
70
+ "[::1]",
71
+ "localhost"
72
+ ]);
73
+ var projectSchema = z.looseObject({
58
74
  name: z.string(),
59
75
  path: z.string(),
60
76
  description: z.string(),
61
77
  expandedPath: z.string(),
62
78
  exists: z.boolean()
63
- }).passthrough();
79
+ });
64
80
  var rosterSchema = z.object({
65
- server: z.object({ baseUrl: z.string().url() }),
81
+ server: z.object({ baseUrl: z.url() }),
66
82
  projects: z.array(projectSchema)
67
83
  });
68
84
  var credentialsSchema = z.object({
@@ -191,7 +207,6 @@ async function fetchDiffWithFallback(baseUrl, credentials, directory, sessionId)
191
207
  } catch {}
192
208
  return { diff, diffSource: "session" };
193
209
  }
194
- var ALLOWED_HOSTS = new Set(["127.0.0.1", "::1", "[::1]", "localhost"]);
195
210
  function validateContext(context) {
196
211
  const parsed = busContextSchema.safeParse(context);
197
212
  if (!parsed.success) {
@@ -204,7 +219,7 @@ function validateContext(context) {
204
219
  } catch {
205
220
  return err("space-bus: context roster server.baseUrl is not a valid URL");
206
221
  }
207
- if (!ALLOWED_HOSTS.has(hostname)) {
222
+ if (!LOOPBACK_HOSTS.has(hostname)) {
208
223
  return err(`space-bus: context roster server.baseUrl must point to localhost (got ${hostname}) — refusing to send credentials off-machine`);
209
224
  }
210
225
  return {
@@ -0,0 +1,126 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * Per-roster state directory: `$XDG_STATE_HOME|~/.local/state`/
4
+ * `space-bus/<first 16 hex of sha256(absolute roster path)>/`. Keying by a
5
+ * hash of the roster path keeps concurrent workspaces from colliding
6
+ * without leaking the roster path itself into the filesystem layout.
7
+ */
8
+ export declare function stateDirFor(rosterPath: string): string;
9
+ export declare function discoveryFilePath(rosterPath: string): string;
10
+ export declare function lockFilePath(rosterPath: string): string;
11
+ export declare function logFilePath(rosterPath: string): string;
12
+ export declare const managedSpawnConfigSchema: z.ZodObject<{
13
+ command: z.ZodOptional<z.ZodArray<z.ZodString>>;
14
+ cwd: z.ZodOptional<z.ZodString>;
15
+ port: z.ZodOptional<z.ZodNumber>;
16
+ }, z.core.$strip>;
17
+ export declare const discoveryFileSchema: z.ZodObject<{
18
+ port: z.ZodNumber;
19
+ pid: z.ZodNumber;
20
+ identity: z.ZodString;
21
+ password: z.ZodString;
22
+ spawnConfig: z.ZodObject<{
23
+ command: z.ZodOptional<z.ZodArray<z.ZodString>>;
24
+ cwd: z.ZodOptional<z.ZodString>;
25
+ port: z.ZodOptional<z.ZodNumber>;
26
+ }, z.core.$strip>;
27
+ baseUrl: z.ZodURL;
28
+ }, z.core.$strip>;
29
+ export type DiscoveryFile = z.infer<typeof discoveryFileSchema>;
30
+ /**
31
+ * Writes the discovery file atomically: a temp file (0600) in the same
32
+ * directory, then rename over the target (rename is atomic within a
33
+ * filesystem). The state dir itself is created 0700. Mirrors the
34
+ * temp+rename+chmod mechanics used for copilot-delegate's port/pid files.
35
+ */
36
+ export declare function writeDiscovery(rosterPath: string, data: DiscoveryFile): void;
37
+ /** Reads and validates the discovery file; returns null if absent or corrupt. */
38
+ export declare function readDiscovery(rosterPath: string): DiscoveryFile | null;
39
+ export declare function removeDiscovery(rosterPath: string): void;
40
+ export declare function loopbackOk(baseUrl: string): boolean;
41
+ export interface LiveEndpoint {
42
+ baseUrl: string;
43
+ credentials: {
44
+ username?: string;
45
+ password?: string;
46
+ };
47
+ pid: number;
48
+ port: number;
49
+ }
50
+ /**
51
+ * Attach-only: reads the discovery file, verifies the recorded pid is alive
52
+ * with a matching identity, and that the discovered baseUrl passes the
53
+ * loopback guard. Never spawns. Lives here (not server.ts) so `config.ts`
54
+ * can attach to a managed roster without importing `server.ts` — server.ts
55
+ * already imports config.ts (for manifestSchema), so config->server would
56
+ * be a cycle. `config.ts` and `server.ts` both import this instead.
57
+ */
58
+ export declare function attachLive(rosterPath: string): LiveEndpoint | null;
59
+ /**
60
+ * Captures a locale-pinned `lstart,comm` identity string for a pid, used to
61
+ * distinguish a live process from a recycled pid reusing the same number.
62
+ * Best-effort within the same-user trust boundary: `lstart` is
63
+ * second-granularity, so a pid recycled by a different process within the
64
+ * same second and matching `comm` could in principle collide — not a
65
+ * "never" guarantee, just a strong practical deterrent. Returns null if
66
+ * the process cannot be inspected (e.g. already exited, or on a platform
67
+ * without `ps`).
68
+ */
69
+ export declare function captureIdentity(pid: number): string | null;
70
+ /** True if a process with the given pid currently exists (signal 0 probe). */
71
+ export declare function isAlive(pid: number): boolean;
72
+ /**
73
+ * A pid is verified only if it is BOTH alive and its captured identity
74
+ * matches the stored one — a recycled pid (alive, different start
75
+ * time/command) fails this check in practice. Best-effort, not a formal
76
+ * guarantee: `lstart` is second-granularity, so a pid recycled within the
77
+ * same second by a process sharing `comm` could theoretically pass. This
78
+ * is acceptable within the documented same-user trust boundary.
79
+ */
80
+ export declare function verifyIdentity(pid: number, storedIdentity: string): boolean;
81
+ export declare const lockFileSchema: z.ZodObject<{
82
+ pid: z.ZodNumber;
83
+ startTime: z.ZodString;
84
+ since: z.ZodNumber;
85
+ }, z.core.$strip>;
86
+ export type LockFile = z.infer<typeof lockFileSchema>;
87
+ export interface LockHandle {
88
+ rosterPath: string;
89
+ }
90
+ /**
91
+ * Acquires the per-roster spawn lock via O_EXCL create. Arbitration: a
92
+ * lock is stale ONLY when its owner is dead (identity-checked) — age never
93
+ * preempts a live owner. Returns a handle on success, or null if the lock
94
+ * is currently held by a verified-live owner.
95
+ */
96
+ export declare function acquireLock(rosterPath: string): LockHandle | null;
97
+ /**
98
+ * Releases the spawn lock. Swallows ALL unlink errors (not just ENOENT) —
99
+ * this typically runs in a `finally` after spawning/readiness work, and a
100
+ * failure here (e.g. permissions, race with an external cleanup) must
101
+ * never replace or mask a primary error already in flight.
102
+ */
103
+ export declare function releaseLock(handle: LockHandle): void;
104
+ /** Reads the current lock holder, or null if no lock file / it's corrupt. */
105
+ export declare function readLockFile(target: string): LockFile | null;
106
+ export declare function closeQuietly(fd: number): void;
107
+ export declare function provisionalFilePath(rosterPath: string): string;
108
+ export declare const provisionalFileSchema: z.ZodObject<{
109
+ pid: z.ZodNumber;
110
+ identity: z.ZodString;
111
+ password: z.ZodString;
112
+ since: z.ZodNumber;
113
+ }, z.core.$strip>;
114
+ export type ProvisionalFile = z.infer<typeof provisionalFileSchema>;
115
+ export declare function writeProvisional(rosterPath: string, data: ProvisionalFile): void;
116
+ /** Reads the provisional spawn record, or null if absent/corrupt. */
117
+ export declare function readProvisional(rosterPath: string): ProvisionalFile | null;
118
+ /**
119
+ * Removes the provisional spawn record. Swallows ALL unlink errors (not
120
+ * just ENOENT) — this runs in `spawnAndWaitReady`'s `finally`, after a
121
+ * readiness/spawn error may already be in flight, and a cleanup failure
122
+ * here (e.g. permissions, a race with external cleanup) must never replace
123
+ * or mask that primary error. Mirrors `releaseLock`'s swallow-all
124
+ * best-effort semantics.
125
+ */
126
+ export declare function removeProvisional(rosterPath: string): void;