@intentic/extension-api 1.430.0 → 1.432.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 (63) hide show
  1. package/README.md +130 -4
  2. package/dist/api.d.ts +1 -0
  3. package/dist/api.d.ts.map +1 -1
  4. package/dist/backend-reach.d.ts +23 -0
  5. package/dist/backend-reach.d.ts.map +1 -0
  6. package/dist/backend-reach.js +241 -0
  7. package/dist/backend-reach.js.map +1 -0
  8. package/dist/index.d.ts +1 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +1 -0
  11. package/dist/index.js.map +1 -1
  12. package/dist/runtime.d.ts +12 -2
  13. package/dist/runtime.d.ts.map +1 -1
  14. package/dist/runtime.js +110 -132
  15. package/dist/runtime.js.map +1 -1
  16. package/dist/server-reach.d.ts +58 -0
  17. package/dist/server-reach.d.ts.map +1 -0
  18. package/dist/server-reach.js +2 -0
  19. package/dist/server-reach.js.map +1 -0
  20. package/dist/server.d.ts +186 -1
  21. package/dist/server.d.ts.map +1 -1
  22. package/dist/sleep-fake.d.ts +15 -0
  23. package/dist/sleep-fake.d.ts.map +1 -0
  24. package/dist/sleep-fake.js +99 -0
  25. package/dist/sleep-fake.js.map +1 -0
  26. package/dist/sleep-reach.d.ts +7 -0
  27. package/dist/sleep-reach.d.ts.map +1 -0
  28. package/dist/sleep-reach.js +104 -0
  29. package/dist/sleep-reach.js.map +1 -0
  30. package/dist/testing.d.ts +64 -9
  31. package/dist/testing.d.ts.map +1 -1
  32. package/dist/testing.js +322 -13
  33. package/dist/testing.js.map +1 -1
  34. package/dist/turn-reach.d.ts +7 -0
  35. package/dist/turn-reach.d.ts.map +1 -0
  36. package/dist/turn-reach.js +140 -0
  37. package/dist/turn-reach.js.map +1 -0
  38. package/dist/version.d.ts +1 -1
  39. package/dist/version.d.ts.map +1 -1
  40. package/dist/version.js +1 -1
  41. package/dist/version.js.map +1 -1
  42. package/dist/wire.d.ts +5 -0
  43. package/dist/wire.d.ts.map +1 -0
  44. package/dist/wire.js +45 -0
  45. package/dist/wire.js.map +1 -0
  46. package/package.json +15 -4
  47. package/src/api.ts +3 -0
  48. package/src/backend-reach.integration.test.ts +154 -0
  49. package/src/backend-reach.ts +335 -0
  50. package/src/caller.integration.test.ts +108 -0
  51. package/src/index.ts +1 -0
  52. package/src/runtime.ts +184 -157
  53. package/src/server-reach.ts +104 -0
  54. package/src/server.ts +364 -3
  55. package/src/sleep-fake.ts +140 -0
  56. package/src/sleep-reach.integration.test.ts +119 -0
  57. package/src/sleep-reach.ts +147 -0
  58. package/src/surface.json +18932 -1
  59. package/src/testing.ts +506 -23
  60. package/src/turn-reach.integration.test.ts +166 -0
  61. package/src/turn-reach.ts +198 -0
  62. package/src/version.ts +121 -1
  63. package/src/wire.ts +64 -0
package/src/server.ts CHANGED
@@ -1,8 +1,21 @@
1
1
  import type { sandboxContract } from "@intentic/sandbox-contract";
2
2
  import type { Conversion, Granularity } from "@intentic/sandbox-contract/documents";
3
3
  import type { ToolEffect } from "@intentic/sandbox-contract/peer-mcp-server";
4
+ import type { WebhookSignatureScheme } from "@intentic/sandbox-contract/schemas/automations";
5
+ import type { MemberTier } from "@intentic/sandbox-contract/schemas/shared";
4
6
  import type { ContractRouterClient } from "@orpc/contract";
5
7
  import type { Disposable } from "./disposable.js";
8
+ import type {
9
+ ActivityRow,
10
+ CiFeedEvent,
11
+ ExecOptions,
12
+ ExecResult,
13
+ RunSettled,
14
+ SandboxActivity,
15
+ SleepBoot,
16
+ SleepHandler,
17
+ SleepHeardMessage,
18
+ } from "./server-reach.js";
6
19
 
7
20
  // The effect vocabulary the daemon's own tools and every device's tools declare, re-exported so a tool here says what it
8
21
  // does in the same words.
@@ -31,12 +44,105 @@ export interface ToolResult {
31
44
  readonly isError?: boolean;
32
45
  }
33
46
 
47
+ // (2026-11-26) A sandbox browser account the calling turn holds (a browser card, or an identity's own browser): its id,
48
+ // which `api.browser.call` names, and the sites it signs in on, the only ones an extension drives its browser on (its
49
+ // sign-in and home pages' hosts, widened to the operator's sign-in family: x.com stands for twitter.com too).
50
+ export interface ToolGrantAccount {
51
+ readonly id: string;
52
+ readonly kind: "browser" | "identity";
53
+ readonly sites: readonly string[];
54
+ }
55
+
56
+ // (2026-11-26) What the turn that called a tool holds, read by the daemon from that turn's own grants (its role, and the
57
+ // reach of the people it is for): never a credential, never more than the turn holds.
58
+ export interface ToolCallGrants {
59
+ // The sandbox's signed-in browser accounts, for `api.browser.call`.
60
+ readonly accounts: readonly ToolGrantAccount[];
61
+ // The owner's own browsers (their `webext` cards), for `api.ownerBrowser.call`.
62
+ readonly browsers: readonly string[];
63
+ // The cards of this extension's own kinds the turn holds.
64
+ readonly cards: readonly string[];
65
+ }
66
+
34
67
  // What one call is handed beside its arguments.
35
68
  export interface ToolCallContext {
36
69
  // Aborted when the agent's client gives up on the call, or the host's deadline for it passes.
37
70
  readonly signal: AbortSignal;
38
71
  // The conversation the calling turn belongs to, when it has one.
39
72
  readonly conversationId?: string | undefined;
73
+ // (2026-11-26) What the calling turn holds, so a tool can refuse itself ("this turn holds no chatgpt.com account");
74
+ // the daemon refuses anything outside it anyway. Absent where no daemon told it (a test, a daemon before 2.55.0).
75
+ readonly grants?: ToolCallGrants | undefined;
76
+ // (2026-11-26) This call's ticket onto its turn, minted by the daemon for this extension and this call and good only
77
+ // while it runs: what `api.browser.call`, `api.ownerBrowser.call` and `api.jobs.start` hand back. Opaque.
78
+ readonly ticket?: string | undefined;
79
+ }
80
+
81
+ // (2026-11-26) What reaches the calling turn's grants: a tool's call while it runs (its ToolCallContext), or a job it
82
+ // started (RunningJob) for as long as the job runs.
83
+ export interface TurnStanding {
84
+ readonly grants?: ToolCallGrants | undefined;
85
+ readonly ticket?: string | undefined;
86
+ // Ends the request when aborted.
87
+ readonly signal?: AbortSignal | undefined;
88
+ }
89
+
90
+ // (2026-11-26) One call of a page tool in the sandbox's signed-in browser of an account the turn holds: `tool` as the
91
+ // agent's `browser` server names it (`browser_navigate`, `browser_evaluate`, `browser_click`, …), `args` its arguments
92
+ // without `account`. Only the page's own tools, and only on the account's sites: a navigation elsewhere is refused, a
93
+ // call on a page elsewhere is refused, and a call that leaves them has its answer withheld.
94
+ export interface BrowserCall {
95
+ readonly account: string;
96
+ readonly tool: string;
97
+ readonly args?: ToolArguments | undefined;
98
+ }
99
+
100
+ // (2026-11-26) One call of a tool of the owner's own browser the turn holds (`open`, `read`, `click`, …, as the agent's
101
+ // server for that browser names them), through the same door as the agent's: the browser's own site switches apply.
102
+ export interface OwnerBrowserCall {
103
+ readonly browser: string;
104
+ readonly tool: string;
105
+ readonly args?: ToolArguments | undefined;
106
+ }
107
+
108
+ // A tool's arguments, as a tool of the agent's is called with them.
109
+ export type ToolArguments = Parameters<ToolDefinition["call"]>[0];
110
+
111
+ // (2026-11-26) What a job's work came to: `output` is what `wait` and the conversation's wake hand the agent, cut in the
112
+ // middle past the job's bound.
113
+ export interface JobEnd {
114
+ readonly ok: boolean;
115
+ readonly output: string;
116
+ }
117
+
118
+ // (2026-11-26) A job's work while it runs: its id, the grants and ticket of the call that started it (good for as long
119
+ // as the job runs; the sandbox's own browsers only while that call's turn still runs, the owner's browser throughout),
120
+ // and a line onto what the job shows meanwhile. `signal` aborts once the job is stopped, by the agent or a person.
121
+ export interface RunningJob {
122
+ readonly id: string;
123
+ readonly grants: ToolCallGrants;
124
+ readonly ticket: string;
125
+ readonly signal: AbortSignal;
126
+ say(line: string): void;
127
+ }
128
+
129
+ // (2026-11-26) A background job a tool starts in the calling conversation, as a `run_in_background` command is one:
130
+ // `command` is how `wait` and the transcript name it, `description` what the conversation's card shows, `started` its
131
+ // output until it ends (what it is doing, and what to do if a restart takes it down meanwhile). In a `lane`, at most
132
+ // `most` (1 when absent) of that lane run at once, first come first served, and never two naming one `key` at once.
133
+ export interface JobSpec {
134
+ readonly command: string;
135
+ readonly description: string;
136
+ readonly started: string;
137
+ readonly lane?: { readonly name: string; readonly key?: string | undefined; readonly most?: number | undefined } | undefined;
138
+ // The work, run once the job's turn in its lane comes; a throw ends the job as failed with its message.
139
+ readonly run: (job: RunningJob) => Promise<JobEnd>;
140
+ }
141
+
142
+ // (2026-11-26) A job filed: its id, which the agent's `wait` takes, and how many runs of its lane were ahead of it.
143
+ export interface StartedJob {
144
+ readonly id: string;
145
+ readonly ahead: number;
40
146
  }
41
147
 
42
148
  export interface ToolDefinition {
@@ -57,6 +163,71 @@ export interface ToolDefinition {
57
163
  // the host answers 404.
58
164
  export type BackendRouteHandler = (request: Request) => Promise<Response | undefined>;
59
165
 
166
+ // (2026-11-26) Who is calling through `/x/<id>`, as the daemon verified them (`api.caller(request)`): a person signed in,
167
+ // a repository's operator panel, a program holding a control token, another extension's backend, or nobody at all on a
168
+ // sandbox that runs without sign-in. Read off an assertion the daemon minted for this extension and this request, good
169
+ // for two minutes, which no extension can mint: `asCaller` hands it back to the daemon to act as that person.
170
+ export type ExtensionCallerWho =
171
+ | {
172
+ readonly kind: "person";
173
+ readonly email: string;
174
+ readonly name?: string | undefined;
175
+ // Their access tier: guest, viewer, collaborator, writer, maintainer or owner.
176
+ readonly tier: MemberTier;
177
+ // The area ids their access is fenced to; absent for the whole workspace.
178
+ readonly areas?: readonly string[] | undefined;
179
+ // The parts of the sandbox they were given to run (people, secrets, privacy, ownAccounts).
180
+ readonly manages?: readonly string[] | undefined;
181
+ }
182
+ | { readonly kind: "panel" }
183
+ | { readonly kind: "program"; readonly id: string; readonly label: string }
184
+ | { readonly kind: "extension"; readonly id: string }
185
+ | { readonly kind: "local" };
186
+
187
+ export type ExtensionCaller = ExtensionCallerWho & {
188
+ // The workspace folders (relative to `workspaceRoot`) this caller may read: absent for the whole workspace, empty for
189
+ // none of it. Only a person with areas is fenced; every other kind reads it whole, as the daemon's own routes answer it.
190
+ readonly folders?: readonly string[] | undefined;
191
+ // Whether a workspace-relative path is inside `folders`, by segment (`app2` is not inside `app`); a path that climbs
192
+ // out of the workspace is inside nothing.
193
+ readonly reaches: (path: string) => boolean;
194
+ // When the assertion stops being believed (ms); `asCaller` past it is refused.
195
+ readonly expiresAt: number;
196
+ // The assertion itself, as the daemon minted it.
197
+ readonly assertion: string;
198
+ };
199
+
200
+ // (2026-11-26) What the daemon checked of a call through one of this extension's public routes before handing it over
201
+ // (`contributes.publicRoutes`), as `api.publicRoutes.serve` hears it.
202
+ export interface PublicRouteVerification {
203
+ // The declared route it landed on, as the manifest spells its path.
204
+ readonly route: string;
205
+ // The automation an automation-scoped route is for: on this extension's own listener provider, and switched on.
206
+ readonly automationId?: string | undefined;
207
+ // The browser's Origin, on the automation's allowed origins when the route checks them.
208
+ readonly origin?: string | undefined;
209
+ // Admitted by the automation's door key in place of a listed Origin; the key itself is not handed on.
210
+ readonly keyed?: boolean | undefined;
211
+ // Signed by the provider with the route's secret under this scheme, and the provider's id for the delivery, which no
212
+ // earlier call the door took carried.
213
+ readonly signature?: { readonly scheme: WebhookSignatureScheme; readonly deliveryId?: string | undefined } | undefined;
214
+ // The caller id a solved proof-of-work challenge was signed to.
215
+ readonly proofOfWork?: { readonly caller: string } | undefined;
216
+ // (2026-11-26) A Cloudflare Turnstile token passed, verified by the daemon with Cloudflare under the route's secret.
217
+ readonly turnstile?: true | undefined;
218
+ // The caller's address as the edge forwarded it: advisory, never an identity.
219
+ readonly remoteIp?: string | undefined;
220
+ // When the daemon took it (ms).
221
+ readonly at: number;
222
+ }
223
+
224
+ // (2026-11-27) What `api.sleep` hands its callers (server-reach.ts).
225
+ export type { SleepBoot, SleepHandler, SleepHeardMessage, SleepNotice } from "./server-reach.js";
226
+
227
+ // One call through a public route, path relative to /door/<id>, its door key (if any) already taken off. Return
228
+ // `undefined` for "not mine": the host answers 404.
229
+ export type PublicRouteHandler = (request: Request, verified: PublicRouteVerification) => Promise<Response | undefined> | Response | undefined;
230
+
60
231
  // An action in the approvals queue that names this extension as its `performer`, handed over by the daemon only after
61
232
  // it found a person's yes on record matching the item as it stands, and spent that yes. The workspace file is never
62
233
  // what decides: an item marked approved without a recorded yes, or changed since, never reaches the backend.
@@ -90,6 +261,81 @@ export type ApprovedActionOutcome =
90
261
 
91
262
  export type ApprovedActionHandler = (action: ApprovedAction, context: { readonly signal: AbortSignal }) => Promise<ApprovedActionOutcome>;
92
263
 
264
+ // (2026-11-26) A post in the approvals queue for a platform one of this extension's cards publishes on
265
+ // (`channel.publish`, the post's `platform` naming the card's id), handed over by the daemon's approvals executor only
266
+ // after it found a person's yes on record matching the post as it stands, and spent that yes. Its words are the ones the
267
+ // person approved: publish them as they are, or refuse.
268
+ export interface ApprovedPost {
269
+ // The item's id in the approvals queue.
270
+ readonly id: string;
271
+ // The card id the post names, lowercased (`discord`).
272
+ readonly platform: string;
273
+ // Exactly what to publish.
274
+ readonly content: string;
275
+ // A headline, where the post carries one.
276
+ readonly title?: string | undefined;
277
+ // Where it goes, matching the card's declared `channel.publish.target` pattern.
278
+ readonly target: string;
279
+ // Who said yes (their sign-in) and when, as the daemon recorded it, as for an action.
280
+ readonly approver?: string | undefined;
281
+ readonly approvedAt: number;
282
+ // The sandbox's action rules as the daemon reads them now, organisation pins applied.
283
+ readonly actionRules: Readonly<Record<string, string>>;
284
+ }
285
+
286
+ // What publishing it came to, in the queue's three states; a `done` result is the row's link (the message's URL) or line.
287
+ export type ApprovedPostHandler = (post: ApprovedPost, context: { readonly signal: AbortSignal }) => Promise<ApprovedActionOutcome>;
288
+
289
+ // (2026-11-26) One automation's fire, handed to the handler its target names: an automation saved with
290
+ // `target: { kind: "extension", extension: "<publisher.name>", handler }` by this extension itself fires it on its trigger,
291
+ // under the scheduler's admission, held wakes and fire journal, with no agent. `id` is the same on every try: a handler
292
+ // told twice (the daemon stopped before it heard the answer) can tell.
293
+ export interface AutomationFire {
294
+ readonly id: string;
295
+ readonly automationId: string;
296
+ readonly handler: string;
297
+ // When the automation fired (ms). A fire made while the backend was down is handed over once it is back, late.
298
+ readonly firedAt: number;
299
+ // How many earlier fires of the same automation this one stands for, fired while the backend was down.
300
+ readonly coalesced: number;
301
+ // The trigger's payload (a webhook's body, a workspace event) and what the automation's check saw, when there.
302
+ readonly payload?: string | undefined;
303
+ readonly observed?: string | undefined;
304
+ // The automation's own words (its `prompt`).
305
+ readonly prompt: string;
306
+ }
307
+
308
+ // Answers once the fire is taken; a throw is recorded on the automation's run as an error and the fire is not tried
309
+ // again. Bounded by the daemon's ten-minute deadline: long work holds itself (`api.work.hold`) and returns.
310
+ export type AutomationFireHandler = (fire: AutomationFire, context: { readonly signal: AbortSignal }) => void | Promise<void>;
311
+
312
+ // (2026-11-26) One check of a watch kind this extension declares (`contributes.watchKinds`), handed to the handler
313
+ // `api.checks.serve` registered for it, from the daemon's automations engine alone through a door of the backend host no
314
+ // person's, panel's or agent's token reaches: on an automation's trigger, when the composer's Check now is pressed, and
315
+ // when an agent proposes a watch of this kind.
316
+ export interface WatchCheck {
317
+ readonly kind: string;
318
+ // The source's parameters as the automation stores them, already checked against the kind's declared fields.
319
+ readonly params: Readonly<Record<string, string | boolean>>;
320
+ // What the last passing check saw, cut to the daemon's bound: a check that words what changed reads it. Absent
321
+ // before the first pass, and for a check tried by hand.
322
+ readonly last?: string | undefined;
323
+ // The credential-gateway addresses of the cards the kind names in `capabilities`, as the automation's role reaches
324
+ // them, under the names a turn's shell sees them by (`GITHUB_API_URL_<CARD>`). Never a credential: the gateway signs
325
+ // what is sent through them. Empty when no such card is connected, or the role reaches none.
326
+ readonly reach: Readonly<Record<string, string>>;
327
+ // The automation it checks for; absent for a check tried by hand.
328
+ readonly automationId?: string | undefined;
329
+ }
330
+
331
+ // What a check came to. A pass carries what it saw: what `fireOn: change` compares and the wake is told, so only what
332
+ // names the thing watched belongs in it (a release's tag, not its notes). A miss carries what it is still waiting for, in
333
+ // a sentence the automation's run history shows.
334
+ export type WatchCheckAnswer = { readonly pass: true; readonly output: string } | { readonly pass: false; readonly detail: string };
335
+
336
+ // Answers within the daemon's deadline (a minute); a throw reads as a check that did not pass, its message the detail.
337
+ export type WatchCheckHandler = (check: WatchCheck, context: { readonly signal: AbortSignal }) => WatchCheckAnswer | Promise<WatchCheckAnswer>;
338
+
93
339
  // An extension's own setting values, keyed as its manifest's `contributes.settings` declares them.
94
340
  export type ExtensionSettingValues = Readonly<Record<string, string | number | boolean>>;
95
341
 
@@ -178,11 +424,81 @@ export interface ExtensionServerApi {
178
424
  // The repository set itself moved (a clone, a scaffold, a delete); answers the whole set.
179
425
  onDidChangeRepos(listener: (repos: readonly string[]) => void): Disposable;
180
426
  };
427
+ // (2026-11-26) What the sandbox tells this extension's code without an agent turn, each kind declared in
428
+ // `permissions.events` (a listener of an undeclared kind throws, naming the field). Over the same stream as
429
+ // `workspace`; a stream that dropped and came back may have missed a run ending, which a reader catches up on by
430
+ // reading the conversation.
431
+ readonly events: {
432
+ // A run ending in a conversation this extension started through POST /agent with its own token (`run`).
433
+ onRunSettled(listener: (run: RunSettled) => void): Disposable;
434
+ // Whether any agent turn is running in the sandbox (`activity`): told as the stream opens, then each time it flips.
435
+ onActivity(listener: (activity: SandboxActivity) => void): Disposable;
436
+ // (2026-11-26) Each run that finished and each job that ended on the workspace's forges, as the core heard it from
437
+ // a webhook or its poller (`ci`): one frame a batch. An empty batch follows a gap in the stream, when frames may
438
+ // have been missed: a reader catches up from the forge's own word (GET /ci/runs) then.
439
+ onCi(listener: (events: readonly CiFeedEvent[]) => void): Disposable;
440
+ };
441
+ // (2026-11-26) Work in flight (`permissions.work`): while a hold stands the sandbox stays awake and an update or a
442
+ // restart waits for it, as for an agent's turn, named by its label. Disposing lets it go, and so does this backend
443
+ // dying; after a daemon restart the runtime holds again. Throws when the manifest does not declare `work`.
444
+ readonly work: {
445
+ hold(label: string): Disposable;
446
+ };
447
+ // (2026-11-26) Runs one command to its end under a workload class the manifest declares (`permissions.exec`),
448
+ // admitted by the sandbox's memory budget, and for `toolchain` queued with every agent's builds and tests, instead
449
+ // of competing with them. Never a shell: `argv` runs as given. Rejects when the class is not declared or the daemon
450
+ // refuses the request; a command that fails answers with its exit code.
451
+ readonly exec: (argv: readonly string[], options?: ExecOptions) => Promise<ExecResult>;
452
+ // (2026-11-26) Rows in the sandbox's activity feed, as this extension's own: the daemon names the extension on each
453
+ // from its token. Reached without declaring anything; rejects with the daemon's words when refused.
454
+ readonly activity: {
455
+ append(row: ActivityRow): Promise<void>;
456
+ };
457
+ // (2026-11-27) Hosted sleep, for code that must hear what arrives while the sandbox sleeps (a chat gateway, a webhook
458
+ // receiver). A sandbox the platform hosts stops when nobody uses it; a request at one of its wake doors starts it
459
+ // again, its sender answered 503 with `Retry-After` while it boots, so only a sender that retries is heard (Slack and
460
+ // Telegram do; GitHub does not redeliver unasked). The backend and every declared process get it, undeclared.
461
+ readonly sleep: {
462
+ // The full public URL of one of this extension's own public routes declaring `wakes: true`, path as the manifest
463
+ // spells it (`*/events`), its `*` filled with `automationId` for an automation's route, and the wake secret in
464
+ // its query (`?wake=`, which the door takes off before the backend hears the call). The same URL on every call
465
+ // and after every restart, so it is the one to paste into a provider's webhook setting (Slack's Request URL,
466
+ // Telegram's setWebhook). A call there wakes the sandbox only while an automation on the extension's listener
467
+ // provider is switched on (the one it names, for an automation's route). Rejects for a route not declared so, and
468
+ // on a sandbox with no public address.
469
+ wakeUrl(route: string, options?: { readonly automationId?: string | undefined }): Promise<string>;
470
+ // Runs `handler` right before the hosted machine sleeps, after the platform has saved its wake doors: the moment
471
+ // to point a provider at a wake URL (Telegram's setWebhook in place of long polling). Every handler of every
472
+ // extension runs at once and all of them share one deadline, `notice.deadline` (ms; about ten seconds), after
473
+ // which the machine stops whether or not they finished. Once asked, the sleep is never called off. Disposing
474
+ // stops hearing; nothing is told on a sandbox that never sleeps (one on somebody's own computer).
475
+ beforeSleep(handler: SleepHandler): Disposable;
476
+ // What this boot follows (`SleepBoot`): whether the last stop was a hosted sleep (the moment to undo what
477
+ // `beforeSleep` did), and whether a request at a wake door started it. Resolves within about fifteen seconds of
478
+ // the daemon's boot; a daemon from before it answers false for both.
479
+ boot(): Promise<SleepBoot>;
480
+ // (2026-11-27, 2.58.0) What the platform heard for this extension's own connections while the sandbox slept,
481
+ // oldest first: a card declaring `sleepWatch` whose owner let the platform hold its credential (Discord's bot)
482
+ // has the platform note where each message for it is. Fetch each one and answer it as if it had just arrived.
483
+ // Handed over once: asking forgets them. Empty on a sandbox the platform does not host; rejects when the
484
+ // platform could not say, leaving them there to ask again.
485
+ heard(): Promise<readonly SleepHeardMessage[]>;
486
+ };
181
487
  readonly routes: {
182
488
  // Serves this extension's route namespace; the daemon proxies /x/<id>/* here through its ordinary auth. A
183
489
  // second mount replaces the first.
184
490
  mount(handler: BackendRouteHandler): void;
185
491
  };
492
+ // (2026-11-26) Who a request through `/x/<id>` comes from, as the daemon verified them; undefined for a request the
493
+ // daemon did not attach one to (a daemon from before 2.53.0), one minted for another extension, or one past its
494
+ // window.
495
+ readonly caller: (request: Request) => ExtensionCaller | undefined;
496
+ // (2026-11-26) The routes anyone on the internet may call (`contributes.publicRoutes`), served at /door/<id>/<path>:
497
+ // reached only once the daemon's own protections passed the call, never through `/x/<id>`, whose handler hears only
498
+ // people signed in. A second serve replaces the first.
499
+ readonly publicRoutes: {
500
+ serve(handler: PublicRouteHandler): void;
501
+ };
186
502
  // The agent's tools (`contributes.tools`). The host owns the MCP transport, its deadlines and the card lookup: it
187
503
  // answers the handshake, lists what `tools` returns for the card a server was mounted for (undefined for an
188
504
  // extension-level server), and runs a call with its arguments. `tools` runs per request, so what it returns may
@@ -194,8 +510,41 @@ export interface ExtensionServerApi {
194
510
  // `performer` names this extension reaches `perform` from the daemon's approvals executor alone, through a door of
195
511
  // the backend host that no /x route, panel or agent token reaches, after the daemon has matched and spent the yes.
196
512
  // The credentials stay in the backend, read from the daemon. A second perform replaces the first.
513
+ // (2026-11-26) `publish` is the same door for an approved post on a platform a card of this extension declares it
514
+ // publishes on (`channel.publish`), in place of an agent turn driving the logged-in browser. A second publish
515
+ // replaces the first.
197
516
  readonly approvals: {
198
517
  perform(handler: ApprovedActionHandler): void;
518
+ publish(handler: ApprovedPostHandler): void;
519
+ };
520
+ // (2026-11-26) The handlers an automation fires in this backend (`permissions.fires` declares each name): reached
521
+ // from the daemon's automations engine alone, through a door of the backend host no /x route, panel or agent token
522
+ // reaches. A second handler for one name replaces the first; registering an undeclared name throws.
523
+ readonly automations: {
524
+ onFire(handler: string, listener: AutomationFireHandler): Disposable;
525
+ };
526
+ // (2026-11-26) What its agent tools may do with the grants of the turn calling them, each a power the manifest declares
527
+ // in `permissions.turn` and the daemon refuses undeclared, and anything the turn does not hold either way. Each takes
528
+ // the call's context (or a running job's), and rejects with the daemon's words when refused.
529
+ // The sandbox's signed-in browser of an account the turn holds, on that account's sites (`browser`). The turn's own
530
+ // browser: the account's page is the one the agent's `browser` tools drive, so a call takes its tab.
531
+ readonly browser: {
532
+ call(standing: TurnStanding, call: BrowserCall): Promise<ToolResult>;
533
+ };
534
+ // The owner's own browser the turn holds (`owner-browser`).
535
+ readonly ownerBrowser: {
536
+ call(standing: TurnStanding, call: OwnerBrowserCall): Promise<ToolResult>;
537
+ };
538
+ // A background job in the calling conversation (`jobs`), answered once filed: its work runs in this backend while
539
+ // the agent goes on, and the agent collects its end with `wait`, or is woken by it.
540
+ readonly jobs: {
541
+ start(standing: TurnStanding, spec: JobSpec): Promise<StartedJob>;
542
+ };
543
+ // (2026-11-26) The checks of the watch kinds this extension declares (`contributes.watchKinds`), one handler per kind:
544
+ // reached from the daemon's automations engine alone, through a door of the backend host. A second handler for one
545
+ // kind replaces the first; serving a kind the manifest does not declare throws.
546
+ readonly checks: {
547
+ serve(kind: string, handler: WatchCheckHandler): Disposable;
199
548
  };
200
549
  // Authenticated transport to the daemon's own routes; every call is checked against the manifest's
201
550
  // `permissions.daemon` allowlist.
@@ -206,9 +555,17 @@ export interface ExtensionServerApi {
206
555
  // For what the contract does not carry: bytes (`/workspace/raw`) and the daemon's hand-written routes.
207
556
  request(path: string, init?: RequestInit): Promise<Response>;
208
557
  json<T>(path: string, init?: RequestInit): Promise<T>;
558
+ // (2026-11-26) The same doors carrying a caller's assertion beside the extension's token, for the routes that act
559
+ // as whoever pressed: POST /agent starts the turn as that person, held to their access as it stands then. Every
560
+ // other route answers by the extension's own reach. The daemon refuses an assertion past its window, minted for
561
+ // another extension or in another sandbox.
562
+ asCaller(caller: ExtensionCaller): DaemonDoors;
209
563
  };
210
564
  }
211
565
 
566
+ // Authenticated transport to the daemon's own routes, as `api.daemon` and `api.daemon.asCaller(caller)` both carry it.
567
+ export type DaemonDoors = Omit<ExtensionServerApi["daemon"], "asCaller">;
568
+
212
569
  export interface ExtensionServerContext {
213
570
  // This extension's routing id, its /x/<id> namespace segment.
214
571
  readonly extensionId: string;
@@ -216,9 +573,13 @@ export interface ExtensionServerContext {
216
573
 
217
574
  // What a declared process (`contributes.processes`) gets, from `connectExtensionProcess` in
218
575
  // `@intentic/extension-api/runtime`: the backend's api without the halves only the backend host can serve. A process
219
- // answers on its own port, so it mounts no routes, serves tools there (`contributes.tools.process`) and carries out no
220
- // approved action.
221
- export type ExtensionProcessApi = Omit<ExtensionServerApi, "routes" | "tools" | "approvals">;
576
+ // answers on its own port, so it mounts no routes, serves tools there (`contributes.tools.process`), carries out no
577
+ // approved action, is woken by no automation, answers no watch's check and no public route, hears no `/x` caller, and
578
+ // (serving no tool through the host) reaches no turn; it hears, holds and runs the way the backend does.
579
+ export type ExtensionProcessApi = Omit<
580
+ ExtensionServerApi,
581
+ "routes" | "tools" | "approvals" | "automations" | "checks" | "caller" | "publicRoutes" | "browser" | "ownerBrowser" | "jobs"
582
+ >;
222
583
 
223
584
  // Shape of the manifest `server` bundle's default (or named) export; `activateServer` runs once per activation, which
224
585
  // is once per backend-host start unless the extension returns a `deactivate`, after which a change to it alone
@@ -0,0 +1,140 @@
1
+ import type { PublicRouteContribution } from "@intentic/extension-manifest";
2
+ import {
3
+ EXTENSION_SLEEP_HOOK_MS,
4
+ type ExtensionSleepBoot,
5
+ type ExtensionSleepFrame,
6
+ type ExtensionSleepHeard,
7
+ ExtensionSleepReadySchema,
8
+ ExtensionWakeUrlAskSchema,
9
+ extensionSleepBootUrl,
10
+ extensionSleepHeardUrl,
11
+ extensionSleepReadyUrl,
12
+ extensionSleepWatchUrl,
13
+ extensionWakeUrlUrl,
14
+ publicDoorPath,
15
+ WAKE_DOOR_PARAM,
16
+ } from "@intentic/sandbox-contract/extension-protocol";
17
+
18
+ // (2026-11-27) The daemon's side of `api.sleep` for `fakeExtensionApi` (testing.ts): wake URLs judged against the
19
+ // manifest's `wakes` routes as the daemon judges them, the boot facts the test sets, and a sleep handed to every
20
+ // before-sleep watch, resolved once each answered ready or the hook's deadline passed, as the daemon waits.
21
+
22
+ // Where the fake sandbox answers, and the wake secret every URL it hands out carries.
23
+ export const FAKE_PUBLIC_URL = "https://sandbox-fake.sbx.test";
24
+ export const FAKE_WAKE_SECRET = "fake-wake-secret";
25
+
26
+ type WakesRoute = Pick<PublicRouteContribution, "path" | "automation" | "wakes">;
27
+
28
+ const json = <T>(body: T, status = 200): Response => Response.json(body, { status });
29
+
30
+ // The concrete path a wake URL names, or why there is none (the daemon's own refusal words).
31
+ const concretePath = (routes: readonly WakesRoute[], route: string, automationId: string | undefined): { readonly path: string } | { readonly error: string } => {
32
+ const declared = routes.find((each) => each.path === route && each.wakes === true);
33
+ if (declared === undefined) {
34
+ return { error: `no public route "${route}" declares \`wakes: true\`` };
35
+ }
36
+ if ((declared.automation === true) !== (automationId !== undefined)) {
37
+ return { error: declared.automation === true ? `the route "${route}" is an automation's: name it` : `the route "${route}" is no automation's` };
38
+ }
39
+ return { path: route.replace("*", automationId ?? "*") };
40
+ };
41
+
42
+ export interface FakeSleep {
43
+ // The extension's sleep doors; undefined for a request that is for none of them.
44
+ readonly door: (request: Request, url: URL, signal: AbortSignal | null) => Promise<Response | undefined>;
45
+ // The hosted machine about to sleep now: every before-sleep watch told, resolved once each answered ready or the
46
+ // deadline passed. Answers how many watches answered.
47
+ readonly now: () => Promise<number>;
48
+ // What `api.sleep.boot()` answers; neither by default.
49
+ boot: ExtensionSleepBoot;
50
+ // Every wake URL handed out, in order.
51
+ readonly wakeUrls: readonly string[];
52
+ // (2026-11-27) What `api.sleep.heard()` answers next; it is handed over once, as the platform does, and then empty.
53
+ heard: ExtensionSleepHeard["heard"];
54
+ }
55
+
56
+ export const fakeSleep = (id: string, routes: readonly WakesRoute[]): FakeSleep => {
57
+ const encoder = new TextEncoder();
58
+ const watches = new Set<ReadableStreamDefaultController<Uint8Array>>();
59
+ const waiting = new Map<string, () => void>();
60
+ const wakeUrls: string[] = [];
61
+ let sleeps = 0;
62
+ const write = (watch: ReadableStreamDefaultController<Uint8Array>, frame: ExtensionSleepFrame): void => {
63
+ try {
64
+ watch.enqueue(encoder.encode(`${JSON.stringify(frame)}\n`));
65
+ } catch {
66
+ // allow(silent-catch): a watch the extension already let go of is no longer one to tell
67
+ watches.delete(watch);
68
+ }
69
+ };
70
+ const watchStream = (signal: AbortSignal | null): Response => {
71
+ let held: ReadableStreamDefaultController<Uint8Array> | undefined;
72
+ const drop = (): void => {
73
+ if (held !== undefined) {
74
+ watches.delete(held);
75
+ }
76
+ };
77
+ const body = new ReadableStream<Uint8Array>({
78
+ start: (controller) => {
79
+ held = controller;
80
+ watches.add(controller);
81
+ write(controller, { kind: "heartbeat" });
82
+ },
83
+ cancel: drop,
84
+ });
85
+ signal?.addEventListener("abort", drop);
86
+ return new Response(body, { headers: { "content-type": "application/x-ndjson" } });
87
+ };
88
+ const wakeUrl = async (request: Request): Promise<Response> => {
89
+ const asked = ExtensionWakeUrlAskSchema.safeParse(await request.json());
90
+ const named = asked.success ? concretePath(routes, asked.data.route, asked.data.automationId) : { error: "a wake URL is asked for { route, automationId? }" };
91
+ if ("error" in named) {
92
+ return json(named, 400);
93
+ }
94
+ const minted = `${FAKE_PUBLIC_URL}${publicDoorPath(id, named.path)}?${WAKE_DOOR_PARAM}=${FAKE_WAKE_SECRET}`;
95
+ wakeUrls.push(minted);
96
+ return json({ url: minted });
97
+ };
98
+ const ready = async (request: Request): Promise<Response> => {
99
+ const answered = ExtensionSleepReadySchema.safeParse(await request.json());
100
+ if (answered.success) {
101
+ waiting.get(answered.data.id)?.();
102
+ }
103
+ return json({ ok: true });
104
+ };
105
+ // Each door by its `METHOD path`, as the daemon registers them.
106
+ const doors = new Map<string, (request: Request, signal: AbortSignal | null) => Promise<Response> | Response>([
107
+ [`POST ${extensionWakeUrlUrl()}`, wakeUrl],
108
+ [`GET ${extensionSleepBootUrl()}`, () => json(fake.boot)],
109
+ [`POST ${extensionSleepWatchUrl()}`, (_request, signal) => watchStream(signal)],
110
+ [`POST ${extensionSleepReadyUrl()}`, ready],
111
+ [
112
+ `POST ${extensionSleepHeardUrl()}`,
113
+ () => {
114
+ const heard = fake.heard;
115
+ fake.heard = [];
116
+ return json({ heard });
117
+ },
118
+ ],
119
+ ]);
120
+ const fake: FakeSleep = {
121
+ boot: { afterSleep: false, requestWake: false },
122
+ wakeUrls,
123
+ heard: [],
124
+ door: async (request, url, signal) => doors.get(`${request.method} ${url.pathname}`)?.(request, signal),
125
+ now: async () => {
126
+ sleeps += 1;
127
+ const told = [...watches].map((watch, index) => {
128
+ const sleepId = `sleep-${sleeps}-${index}`;
129
+ const answered = new Promise<boolean>((resolve) => {
130
+ waiting.set(sleepId, () => resolve(true));
131
+ setTimeout(() => resolve(false), EXTENSION_SLEEP_HOOK_MS).unref();
132
+ });
133
+ write(watch, { kind: "sleep", id: sleepId, deadline: Date.now() + EXTENSION_SLEEP_HOOK_MS });
134
+ return answered;
135
+ });
136
+ return (await Promise.all(told)).filter(Boolean).length;
137
+ },
138
+ };
139
+ return fake;
140
+ };