@fitchmultz/pi-subagents 0.46.1 → 0.47.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 (92) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +1 -1
  3. package/dist/agents/agent-management.js +8 -2
  4. package/dist/agents/agents.js +8 -8
  5. package/dist/extension/build-info.js +1 -1
  6. package/dist/extension/index.js +11 -5
  7. package/dist/extension/result-snapshot.js +0 -18
  8. package/dist/history/ingest.js +40 -31
  9. package/dist/history/prune.js +1 -1
  10. package/dist/history/run-projection.js +1 -2
  11. package/dist/history/search-queries.js +8 -2
  12. package/dist/history/store.js +20 -4
  13. package/dist/history/text-chunks.js +1 -2
  14. package/dist/history/transcript-queries.js +12 -6
  15. package/dist/intercom/result-intercom.js +0 -74
  16. package/dist/pi-intercom/bridge-config.js +35 -26
  17. package/dist/pi-intercom/bridge-files.js +47 -0
  18. package/dist/pi-intercom/bridge-mailbox.js +324 -0
  19. package/dist/pi-intercom/bridge-presence.js +100 -0
  20. package/dist/pi-intercom/bridge-routes.js +27 -3
  21. package/dist/pi-intercom/bridge-session.js +60 -64
  22. package/dist/pi-intercom/bridge-wake.js +158 -0
  23. package/dist/pi-intercom/bridge.js +55 -7
  24. package/dist/pi-intercom/broker/broker.js +33 -16
  25. package/dist/pi-intercom/broker/framing.js +67 -17
  26. package/dist/pi-intercom/broker/snapshot-reader.js +29 -8
  27. package/dist/pi-intercom/broker/snapshots.js +144 -41
  28. package/dist/pi-intercom/session-targets.js +0 -1
  29. package/dist/runs/background/async-run-discovery.js +4 -6
  30. package/dist/runs/background/completion-dedupe.js +4 -55
  31. package/dist/runs/background/notify.js +1 -9
  32. package/dist/runs/background/result-watcher.js +1 -1
  33. package/dist/runs/background/runner-child-observer.js +12 -7
  34. package/dist/runs/background/runner-monitor.js +2 -0
  35. package/dist/runs/background/runner-parallel.js +2 -3
  36. package/dist/runs/shared/child-observation-spool.js +8 -3
  37. package/dist/runs/shared/run-restoration.js +4 -5
  38. package/dist/runs/shared/supervisor-questions.js +7 -4
  39. package/dist/shared/journal-frames.js +1 -1
  40. package/dist/slash/command-discovery.js +13 -12
  41. package/dist/tui/agent-history.js +0 -14
  42. package/dist/tui/agent-view.js +0 -1
  43. package/dist/tui/conversation-history.js +10 -1
  44. package/dist/tui/history-cards.js +8 -22
  45. package/docs/intercom-bridge.md +39 -17
  46. package/docs/intercom.md +5 -1
  47. package/docs/performance.md +72 -3
  48. package/package.json +1 -1
  49. package/scripts/intercom-bridge-credentials.mjs +54 -12
  50. package/scripts/intercom-remote.mjs +25 -1
  51. package/scripts/package-smoke.mjs +1 -1
  52. package/src/agents/agent-management.ts +8 -4
  53. package/src/agents/agents.ts +17 -12
  54. package/src/extension/index.ts +12 -5
  55. package/src/extension/result-snapshot.ts +1 -27
  56. package/src/history/ingest.ts +57 -49
  57. package/src/history/prune.ts +1 -1
  58. package/src/history/run-projection.ts +2 -4
  59. package/src/history/search-queries.ts +9 -2
  60. package/src/history/store.ts +21 -5
  61. package/src/history/text-chunks.ts +7 -2
  62. package/src/history/transcript-queries.ts +16 -10
  63. package/src/intercom/result-intercom.ts +0 -103
  64. package/src/pi-intercom/bridge-config.ts +46 -36
  65. package/src/pi-intercom/bridge-files.ts +59 -0
  66. package/src/pi-intercom/bridge-mailbox.ts +395 -0
  67. package/src/pi-intercom/bridge-presence.ts +128 -0
  68. package/src/pi-intercom/bridge-routes.ts +41 -4
  69. package/src/pi-intercom/bridge-session.ts +73 -71
  70. package/src/pi-intercom/bridge-wake.ts +188 -0
  71. package/src/pi-intercom/bridge.ts +58 -7
  72. package/src/pi-intercom/broker/broker.ts +34 -20
  73. package/src/pi-intercom/broker/framing.ts +71 -20
  74. package/src/pi-intercom/broker/snapshot-reader.ts +47 -9
  75. package/src/pi-intercom/broker/snapshots.ts +184 -51
  76. package/src/pi-intercom/session-targets.ts +0 -2
  77. package/src/runs/background/async-run-discovery.ts +4 -10
  78. package/src/runs/background/completion-dedupe.ts +6 -69
  79. package/src/runs/background/notify.ts +2 -12
  80. package/src/runs/background/result-watcher.ts +1 -1
  81. package/src/runs/background/runner-child-observer.ts +19 -11
  82. package/src/runs/background/runner-monitor.ts +2 -0
  83. package/src/runs/background/runner-parallel.ts +2 -3
  84. package/src/runs/shared/child-observation-spool.ts +8 -3
  85. package/src/runs/shared/run-restoration.ts +4 -4
  86. package/src/runs/shared/supervisor-questions.ts +10 -10
  87. package/src/shared/journal-frames.ts +1 -1
  88. package/src/slash/command-discovery.ts +13 -12
  89. package/src/tui/agent-history.ts +1 -18
  90. package/src/tui/agent-view.ts +0 -1
  91. package/src/tui/conversation-history.ts +15 -1
  92. package/src/tui/history-cards.ts +8 -26
@@ -0,0 +1,324 @@
1
+ import { closeSync, constants, fstatSync, lstatSync, openSync, realpathSync } from "node:fs";
2
+ import { isAbsolute, join } from "node:path";
3
+ import { DatabaseSync } from "node:sqlite";
4
+ import { isRecord } from "../shared/unknown.js";
5
+ import { normalizeMessage, normalizeSessionInfo } from "./types.js";
6
+ import { Fault, INBOX_BYTES, INBOX_COUNT, fingerprint as certificateFingerprint, label, } from "./bridge-protocol.js";
7
+ export function stateDirectory(value) {
8
+ if (value === undefined) {
9
+ return;
10
+ }
11
+ const path = label(value, "stateDir", 4096);
12
+ const stat = lstatSync(path);
13
+ if (!isAbsolute(path) ||
14
+ !stat.isDirectory() ||
15
+ stat.uid !== process.getuid?.() ||
16
+ (stat.mode & 0o777) !== 0o700) {
17
+ throw new Error("stateDir must be an absolute owned directory with mode 0700, not a symlink.");
18
+ }
19
+ return realpathSync(path);
20
+ }
21
+ function privateFile(path, create = false) {
22
+ const flags = create ? constants.O_RDWR | constants.O_CREAT : constants.O_RDONLY;
23
+ const fd = openSync(path, flags | constants.O_NOFOLLOW, 0o600);
24
+ try {
25
+ const stat = fstatSync(fd);
26
+ if (!stat.isFile() ||
27
+ stat.uid !== process.getuid?.() ||
28
+ (stat.mode & 0o777) !== 0o600 ||
29
+ stat.nlink !== 1) {
30
+ throw new Error("Mailbox files must be owned regular files with mode 0600 and one link.");
31
+ }
32
+ }
33
+ finally {
34
+ closeSync(fd);
35
+ }
36
+ }
37
+ function database(stateDir, fingerprint) {
38
+ stateDirectory(stateDir);
39
+ const path = join(stateDir, `${certificateFingerprint(fingerprint)}.sqlite`);
40
+ privateFile(path, true);
41
+ for (const suffix of ["-journal", "-wal", "-shm"]) {
42
+ try {
43
+ lstatSync(`${path}${suffix}`);
44
+ }
45
+ catch (error) {
46
+ if (isRecord(error) && error.code === "ENOENT") {
47
+ continue;
48
+ }
49
+ throw error;
50
+ }
51
+ privateFile(`${path}${suffix}`);
52
+ }
53
+ const db = new DatabaseSync(path, { enableDoubleQuotedStringLiterals: false });
54
+ try {
55
+ db.exec(`
56
+ PRAGMA journal_mode=DELETE;
57
+ PRAGMA synchronous=FULL;
58
+ PRAGMA secure_delete=ON;
59
+ PRAGMA cache_size=-1024;
60
+ PRAGMA busy_timeout=1000;
61
+ CREATE TABLE IF NOT EXISTS mailbox (
62
+ sequence INTEGER PRIMARY KEY AUTOINCREMENT,
63
+ id TEXT NOT NULL UNIQUE,
64
+ envelope TEXT,
65
+ bytes INTEGER NOT NULL,
66
+ ask INTEGER NOT NULL,
67
+ receivedAt INTEGER NOT NULL,
68
+ acked INTEGER NOT NULL DEFAULT 0,
69
+ notified INTEGER NOT NULL DEFAULT 1
70
+ );
71
+ CREATE INDEX IF NOT EXISTS unread ON mailbox(sequence) WHERE acked=0;
72
+ CREATE INDEX IF NOT EXISTS asks ON mailbox(ask) WHERE ask=1 AND acked=0;
73
+ CREATE INDEX IF NOT EXISTS notifications ON mailbox(sequence) WHERE notified=0;
74
+ CREATE TABLE IF NOT EXISTS losses (
75
+ singleton INTEGER PRIMARY KEY CHECK(singleton=1),
76
+ count INTEGER NOT NULL
77
+ );
78
+ INSERT OR IGNORE INTO losses VALUES(1, 0);
79
+ `);
80
+ return db;
81
+ }
82
+ catch (error) {
83
+ db.close();
84
+ throw error;
85
+ }
86
+ }
87
+ function decode(value) {
88
+ const raw = typeof value === "string" ? JSON.parse(value) : undefined;
89
+ const from = isRecord(raw) ? normalizeSessionInfo(raw.from) : null;
90
+ const message = isRecord(raw) ? normalizeMessage(raw.message) : null;
91
+ if (!from || !message) {
92
+ throw new Fault(507, "mailbox_storage_failed", "Stored mailbox entry is invalid.");
93
+ }
94
+ return { from, message };
95
+ }
96
+ /** Persistent payloads leave only ID tombstones after acknowledgement, preventing replay resurrection. */
97
+ export class Mailbox {
98
+ db;
99
+ entries = new Map();
100
+ bytes = 0;
101
+ sequence = 0;
102
+ failure;
103
+ admissionFailed = false;
104
+ notifications;
105
+ constructor(stateDir, fingerprint, notifications = false) {
106
+ try {
107
+ this.db = stateDir === undefined ? undefined : database(stateDir, fingerprint);
108
+ }
109
+ catch {
110
+ throw new Fault(507, "mailbox_storage_failed", "Cannot open private mailbox storage.");
111
+ }
112
+ this.notifications = notifications;
113
+ }
114
+ get persistent() {
115
+ return this.db !== undefined;
116
+ }
117
+ stored(action) {
118
+ if (this.failure) {
119
+ throw this.failure;
120
+ }
121
+ try {
122
+ return action();
123
+ }
124
+ catch (error) {
125
+ throw error instanceof Fault
126
+ ? error
127
+ : new Fault(507, "mailbox_storage_failed", "Mailbox storage operation failed.");
128
+ }
129
+ }
130
+ add(envelope) {
131
+ if (!this.db) {
132
+ return this.addVolatile(envelope);
133
+ }
134
+ const db = this.db;
135
+ return this.stored(() => {
136
+ try {
137
+ const payload = JSON.stringify(envelope);
138
+ const inserted = db
139
+ .prepare("INSERT OR IGNORE INTO mailbox(id,envelope,bytes,ask,receivedAt,notified) VALUES(?,?,?,?,?,?)")
140
+ .run(envelope.message.id, payload, Buffer.byteLength(payload), envelope.message.expectsReply === true ? 1 : 0, Date.now(), this.notifications ? 0 : 1);
141
+ this.admissionFailed = false;
142
+ return inserted.changes === 0 ? "duplicate" : "stored";
143
+ }
144
+ catch {
145
+ this.admissionFailed = true;
146
+ try {
147
+ db.prepare("UPDATE losses SET count=MIN(count+1,9007199254740991) WHERE singleton=1").run();
148
+ }
149
+ catch {
150
+ this.failure = new Fault(507, "mailbox_storage_failed", "Mailbox admission and loss recording failed; loss count is not durable.", { lossRecorded: false });
151
+ throw this.failure;
152
+ }
153
+ return "lost";
154
+ }
155
+ });
156
+ }
157
+ addVolatile(envelope) {
158
+ if (this.entries.has(envelope.message.id)) {
159
+ return "duplicate";
160
+ }
161
+ const bytes = Buffer.byteLength(JSON.stringify(envelope));
162
+ if (this.entries.size >= INBOX_COUNT || this.bytes + bytes > INBOX_BYTES) {
163
+ return "lost";
164
+ }
165
+ this.entries.set(envelope.message.id, {
166
+ ...envelope,
167
+ bytes,
168
+ sequence: ++this.sequence,
169
+ receivedAt: Date.now(),
170
+ acked: 0,
171
+ });
172
+ this.bytes += bytes;
173
+ return "stored";
174
+ }
175
+ get(id) {
176
+ if (!this.db) {
177
+ return this.entries.get(id);
178
+ }
179
+ const db = this.db;
180
+ return this.stored(() => {
181
+ const row = db.prepare("SELECT envelope FROM mailbox WHERE id=? AND acked=0").get(id);
182
+ return row ? decode(row.envelope) : undefined;
183
+ });
184
+ }
185
+ remove(id) {
186
+ if (this.db) {
187
+ const db = this.db;
188
+ this.stored(() => db
189
+ .prepare("UPDATE mailbox SET acked=1, envelope=CASE WHEN notified=1 THEN NULL ELSE envelope END, bytes=CASE WHEN notified=1 THEN 0 ELSE bytes END WHERE id=?")
190
+ .run(id));
191
+ return;
192
+ }
193
+ const entry = this.entries.get(id);
194
+ if (entry) {
195
+ this.bytes -= entry.bytes;
196
+ this.entries.delete(id);
197
+ }
198
+ }
199
+ ack(ids) {
200
+ const acked = [];
201
+ const retained = [];
202
+ for (const id of ids) {
203
+ const entry = this.get(id);
204
+ if (entry?.message.expectsReply === true) {
205
+ // Durable consumer intake owns the primary wake; keep only the reply obligation here.
206
+ this.notificationAccepted(id);
207
+ retained.push(id);
208
+ }
209
+ else if (entry) {
210
+ this.remove(id);
211
+ acked.push(id);
212
+ }
213
+ }
214
+ return { acked, retained };
215
+ }
216
+ page(after, limit) {
217
+ return this.readPage(after, limit, false);
218
+ }
219
+ pendingNotifications(after = 0, limit = INBOX_COUNT) {
220
+ return this.readPage(after, limit, true);
221
+ }
222
+ notificationAccepted(id) {
223
+ const db = this.db;
224
+ if (db) {
225
+ this.stored(() => db
226
+ .prepare("UPDATE mailbox SET notified=1, envelope=CASE WHEN acked=1 THEN NULL ELSE envelope END, bytes=CASE WHEN acked=1 THEN 0 ELSE bytes END WHERE id=?")
227
+ .run(id));
228
+ }
229
+ }
230
+ readPage(after, limit, notifications) {
231
+ if (!Number.isSafeInteger(after) ||
232
+ after < 0 ||
233
+ !Number.isInteger(limit) ||
234
+ limit < 1 ||
235
+ limit > INBOX_COUNT) {
236
+ throw new Fault(400, "invalid_input", "Invalid mailbox page bounds.");
237
+ }
238
+ return this.stored(() => {
239
+ const candidates = this.candidates(after, limit, notifications);
240
+ const messages = [];
241
+ let bytes = 0;
242
+ let cursor = after;
243
+ let hasMore = false;
244
+ for (const candidate of candidates) {
245
+ const row = indexRow(candidate);
246
+ if (messages.length === limit || (messages.length > 0 && bytes + row.bytes > INBOX_BYTES)) {
247
+ hasMore = true;
248
+ break;
249
+ }
250
+ const envelope = decode(isRecord(candidate) && typeof candidate.envelope === "string"
251
+ ? candidate.envelope
252
+ : JSON.stringify(candidate));
253
+ messages.push(notifications
254
+ ? { ...envelope, receivedAt: row.receivedAt, acknowledged: row.acked === 1 }
255
+ : envelope);
256
+ bytes += row.bytes;
257
+ cursor = row.sequence;
258
+ }
259
+ return { messages, nextCursor: String(cursor), hasMore };
260
+ });
261
+ }
262
+ candidates(after, limit, notifications) {
263
+ if (this.db) {
264
+ return this.db
265
+ .prepare(`SELECT sequence,bytes,receivedAt,acked,envelope FROM mailbox WHERE ${notifications ? "notified=0" : "acked=0"} AND sequence>? ORDER BY sequence LIMIT ?`)
266
+ .iterate(after, limit + 1);
267
+ }
268
+ return notifications
269
+ ? []
270
+ : [...this.entries.values()].filter((entry) => entry.sequence > after).slice(0, limit + 1);
271
+ }
272
+ status(volatileLosses) {
273
+ const db = this.db;
274
+ const lostMessages = db
275
+ ? this.stored(() => db.prepare("SELECT count FROM losses WHERE singleton=1").get()?.count)
276
+ : volatileLosses;
277
+ if (typeof lostMessages !== "number") {
278
+ throw new Fault(507, "mailbox_storage_failed", "Stored loss counter is invalid.");
279
+ }
280
+ return {
281
+ lostMessages,
282
+ overflow: lostMessages > 0,
283
+ storageError: this.persistent && lostMessages > 0,
284
+ };
285
+ }
286
+ presence() {
287
+ const db = this.db;
288
+ if (!db) {
289
+ return {
290
+ pendingAsks: [...this.entries.values()].filter((entry) => entry.message.expectsReply === true).length,
291
+ acceptsAsks: this.entries.size < INBOX_COUNT && this.bytes < INBOX_BYTES,
292
+ };
293
+ }
294
+ return this.stored(() => {
295
+ const pendingAsks = db
296
+ .prepare("SELECT COUNT(*) AS count FROM mailbox WHERE ask=1 AND acked=0")
297
+ .get()?.count;
298
+ if (typeof pendingAsks !== "number") {
299
+ throw new Fault(507, "mailbox_storage_failed", "Stored ask counter is invalid.");
300
+ }
301
+ return { pendingAsks, acceptsAsks: !this.admissionFailed };
302
+ });
303
+ }
304
+ close() {
305
+ this.db?.close();
306
+ const discarded = this.entries.size;
307
+ this.entries.clear();
308
+ this.bytes = 0;
309
+ return discarded;
310
+ }
311
+ }
312
+ function indexRow(row) {
313
+ if (!isRecord(row)) {
314
+ throw new Fault(507, "mailbox_storage_failed", "Stored mailbox index is invalid.");
315
+ }
316
+ const { sequence, bytes, receivedAt, acked } = row;
317
+ if (typeof sequence !== "number" ||
318
+ typeof bytes !== "number" ||
319
+ typeof receivedAt !== "number" ||
320
+ typeof acked !== "number") {
321
+ throw new Fault(507, "mailbox_storage_failed", "Stored mailbox index is invalid.");
322
+ }
323
+ return { sequence, bytes, receivedAt, acked };
324
+ }
@@ -0,0 +1,100 @@
1
+ import { join } from "node:path";
2
+ import { isRecord } from "../shared/unknown.js";
3
+ import { readBridgeFile, writeBridgeJson } from "./bridge-files.js";
4
+ import { log } from "./bridge-protocol.js";
5
+ export class PersistentPresence {
6
+ registrations = new Map();
7
+ stopped = false;
8
+ timer;
9
+ running;
10
+ owner;
11
+ constructor(owner) {
12
+ this.owner = owner;
13
+ for (const identity of owner.clients().values()) {
14
+ this.restore(identity);
15
+ }
16
+ }
17
+ path(fingerprint) {
18
+ return join(this.owner.stateDir, `presence-${fingerprint}.json`);
19
+ }
20
+ restore(identity) {
21
+ try {
22
+ const value = JSON.parse(readBridgeFile(this.path(identity.fingerprint256), true, 8192).toString("utf8"));
23
+ if (isRecord(value) &&
24
+ value.fingerprint === identity.fingerprint256 &&
25
+ value.name === identity.name &&
26
+ value.cwd === identity.cwd &&
27
+ typeof value.expires === "number" &&
28
+ Number.isFinite(value.expires) &&
29
+ value.expires > Date.now()) {
30
+ this.registrations.set(identity.fingerprint256, {
31
+ fingerprint: identity.fingerprint256,
32
+ name: identity.name,
33
+ cwd: identity.cwd,
34
+ expires: value.expires,
35
+ });
36
+ }
37
+ }
38
+ catch {
39
+ // An absent/invalid saved registration never grants a certificate identity.
40
+ }
41
+ }
42
+ authenticated(identity, expires) {
43
+ const previous = this.registrations.get(identity.fingerprint256);
44
+ if (previous?.expires === expires &&
45
+ previous.name === identity.name &&
46
+ previous.cwd === identity.cwd) {
47
+ return;
48
+ }
49
+ const registration = {
50
+ fingerprint: identity.fingerprint256,
51
+ name: identity.name,
52
+ cwd: identity.cwd,
53
+ expires,
54
+ };
55
+ writeBridgeJson(this.path(identity.fingerprint256), registration);
56
+ this.registrations.set(identity.fingerprint256, registration);
57
+ this.start();
58
+ }
59
+ start() {
60
+ if (!this.stopped && !this.running) {
61
+ clearTimeout(this.timer);
62
+ this.timer = setTimeout(() => {
63
+ this.running = this.maintain().finally(() => {
64
+ this.running = undefined;
65
+ if (!this.stopped) {
66
+ this.timer = setTimeout(() => this.start(), 15000);
67
+ }
68
+ });
69
+ }, 0);
70
+ }
71
+ }
72
+ async maintain() {
73
+ for (const registration of this.registrations.values()) {
74
+ if (this.stopped) {
75
+ return;
76
+ }
77
+ const identity = this.owner.clients().get(registration.fingerprint);
78
+ if (!identity ||
79
+ identity.name !== registration.name ||
80
+ identity.cwd !== registration.cwd ||
81
+ registration.expires <= Date.now()) {
82
+ this.registrations.delete(registration.fingerprint);
83
+ continue;
84
+ }
85
+ try {
86
+ // Each identity has one connection attempt; preserve the native broker's spawn safeguards.
87
+ // oxlint-disable-next-line no-await-in-loop
88
+ await this.owner.connect(identity, registration.expires);
89
+ }
90
+ catch {
91
+ log("presence_retry", { name: identity.name, result: "broker_unavailable" });
92
+ }
93
+ }
94
+ }
95
+ async stop() {
96
+ this.stopped = true;
97
+ clearTimeout(this.timer);
98
+ await this.running;
99
+ }
100
+ }
@@ -21,16 +21,39 @@ export function routeFor(req) {
21
21
  Object.keys(req.headers).some((key) => key.startsWith("sec-fetch-"))) {
22
22
  throw new Fault(403, "browser_forbidden", "Browser access is not supported.");
23
23
  }
24
- const route = `${req.method ?? ""} ${req.url ?? ""}`;
24
+ const route = `${req.method ?? ""} ${(req.url ?? "").split("?")[0]}`;
25
25
  if (!isRoute(route)) {
26
26
  throw new Fault(404, "not_found", "Unknown method or path.");
27
27
  }
28
+ if (req.url?.includes("?") === true && route !== "GET /v1/inbox") {
29
+ throw new Fault(400, "invalid_input", "Only inbox accepts query parameters.");
30
+ }
31
+ checkGetBody(req);
32
+ return route;
33
+ }
34
+ function checkGetBody(req) {
28
35
  if (req.method === "GET" &&
29
36
  (req.headers["transfer-encoding"] !== undefined ||
30
37
  (req.headers["content-length"] !== undefined && req.headers["content-length"] !== "0"))) {
31
38
  throw new Fault(400, "invalid_input", "GET requests must not have a body.");
32
39
  }
33
- return route;
40
+ }
41
+ function inboxQuery(req) {
42
+ const url = req.url ?? "";
43
+ const separator = url.indexOf("?");
44
+ const query = new URLSearchParams(separator === -1 ? "" : url.slice(separator + 1));
45
+ if ([...query.keys()].some((key) => !["after", "limit"].includes(key) || query.getAll(key).length !== 1)) {
46
+ throw new Fault(400, "invalid_input", "Unknown or repeated inbox query parameter.");
47
+ }
48
+ const after = query.get("after") ?? "0";
49
+ const limit = query.get("limit") ?? String(INBOX_COUNT);
50
+ if (!/^(0|[1-9]\d{0,15})$/.test(after) ||
51
+ !Number.isSafeInteger(Number(after)) ||
52
+ !/^[1-9]\d{0,2}$/.test(limit) ||
53
+ Number(limit) > INBOX_COUNT) {
54
+ throw new Fault(400, "invalid_input", "after must be a nonnegative safe integer cursor; limit must be 1..256.");
55
+ }
56
+ return { route: "GET /v1/inbox", after: Number(after), limit: Number(limit) };
34
57
  }
35
58
  function timeout(value) {
36
59
  const ms = value === undefined ? ASK_TIMEOUT : value;
@@ -43,10 +66,11 @@ export async function operationFor(req, route, signal) {
43
66
  const input = req.method === "POST" ? object(await body(req, signal), fields[route]) : {};
44
67
  switch (route) {
45
68
  case "GET /v1/list":
46
- case "GET /v1/inbox":
47
69
  case "POST /v1/register":
48
70
  case "GET /v1/sessions":
49
71
  return { route };
72
+ case "GET /v1/inbox":
73
+ return inboxQuery(req);
50
74
  case "POST /v1/start":
51
75
  return { route, name: label(input.name, "session name", 64).trim() };
52
76
  case "POST /v1/stop":