@ai-matrx/alchemy 0.9.1 → 0.11.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.
@@ -251,6 +251,148 @@ interface ClickTarget {
251
251
  host?: unknown;
252
252
  }
253
253
 
254
+ /**
255
+ * Operate — the format and delivery contract (Matrx Alchemy CONTRACT §2.3, ALC-16).
256
+ *
257
+ * A format is an ADAPTER: `supports` is synchronous and loads nothing; `load` dynamically
258
+ * imports the engine the first time the format is chosen (print for rich-html/html/pdf/docx/
259
+ * epub, SheetJS for xlsx). Delivery accepts only a SEALED artifact, so the bytes previewed are
260
+ * the bytes copied, downloaded or sent.
261
+ */
262
+
263
+ /** A reference to a declared write target (resolved by the one write door, ALC-17). */
264
+ interface WriteTargetRef {
265
+ surfaceName?: string;
266
+ target: string;
267
+ item?: {
268
+ itemType: string;
269
+ identity: Record<string, string>;
270
+ };
271
+ }
272
+ /**
273
+ * Every write returns one (CONTRACT §2.3; the door is `createWriteDoor`, ALC-17). An applied
274
+ * receipt may carry a `warning` with its `fix` (rule 10: a person's kind mismatch warns, never refuses).
275
+ * A refusal's `reason` is a `WriteRefusalReason` (`./door`).
276
+ */
277
+ type Receipt = {
278
+ status: "applied";
279
+ to: WriteTargetRef;
280
+ sentence: string;
281
+ warning?: string;
282
+ fix?: string;
283
+ } | {
284
+ status: "queued";
285
+ to: WriteTargetRef;
286
+ sentence: string;
287
+ remedy: string;
288
+ } | {
289
+ status: "refused";
290
+ to: WriteTargetRef;
291
+ reason: string;
292
+ sentence: string;
293
+ remedy: string;
294
+ };
295
+
296
+ /**
297
+ * THE ONE WRITE DOOR (Matrx Alchemy ALC-17; Applets CONTRACTS §6 "receipts through the one write door").
298
+ *
299
+ * Every write — a person's edit, a menu row, a destination send, an agent, an applet, an automation,
300
+ * an Action projected from a write target — lands through `door.write`. The door:
301
+ *
302
+ * 1. resolves the DECLARED write target (the surface's own, then its ancestors'); an undeclared
303
+ * target is refused by name;
304
+ * 2. enforces the mode: `stream` takes ops from the target's `streamOps`; ops on any other target
305
+ * need `patchable` (+ `updatesValue`); a value on a `stream` target is refused;
306
+ * 3. checks the kind (rule 10): a person's mismatch WARNS (the receipt is still applied, with a
307
+ * `warning` and a `fix`); an agent's, applet's or automation's malformed write is REFUSED;
308
+ * 4. applies the policy: manual → a person only; ask → a person's own act is the approval, anyone
309
+ * else asks through `approvals`; queued → filed through `review`; auto → applies;
310
+ * 5. applies through the handler: the mounted page's LIVE handler wins over the HEADLESS one;
311
+ * neither → refused `unapplicable` ("open <surface> to apply it");
312
+ * 6. turns stale / missing record / broken protected span into "cannot apply" with a one-click
313
+ * remedy (rule 11).
314
+ *
315
+ * Every outcome is a `Receipt` carrying the system's own sentence. `write` never throws.
316
+ *
317
+ * Who counts as a person: `person`, `menu` and `destination` (a destination send is a person's own
318
+ * click on "Send to …"). `agent`, `applet` and `automation` are not.
319
+ *
320
+ * Open-risk ruling — stream ops into an entity target: an entity write is ONE atomic save. Ops reach
321
+ * an entity target only when it is declared `patchable` (the ops are handed to the handler in one
322
+ * call and commit as one save); a non-patchable entity target refuses ops with the remedy "stream
323
+ * into the editor, then save". Op-by-op persistence into a record does not exist.
324
+ */
325
+
326
+ /** Who is writing. `menu` and `destination` are a person's own act. */
327
+ type WriteCaller = "person" | "agent" | "applet" | "automation" | "destination" | "menu";
328
+ /** One text op for a `stream` (or `patchable`) target — the WidgetHandle op set. */
329
+ interface WriteOp {
330
+ op: StreamOp;
331
+ /** The text the op inserts or replaces with. */
332
+ text?: string;
333
+ /** What `replace` / `insert_before` / `insert_after` / `patch` anchor on. */
334
+ find?: string;
335
+ }
336
+ interface WriteRequest {
337
+ surfaceName: string;
338
+ /** A write target name; `"<itemType>.<name>"` names an item's target. */
339
+ target: string;
340
+ itemType?: string;
341
+ /** Which item, by its declared identity values. Required for an item target. */
342
+ itemIdentity?: Record<string, string>;
343
+ /** A whole value. Exactly one of `value` / `ops`. */
344
+ value?: unknown;
345
+ /** Text ops (stream / patchable targets). */
346
+ ops?: readonly WriteOp[];
347
+ by: WriteCaller;
348
+ runId?: string;
349
+ }
350
+ /** What a handler receives: the request plus the declared target it resolved to. */
351
+ interface WriteApplyRequest {
352
+ surfaceName: string;
353
+ /** The surface that declares the target (an ancestor when inherited). */
354
+ declaredOn: string;
355
+ target: WriteTarget;
356
+ item?: {
357
+ itemType: string;
358
+ identity: Record<string, string>;
359
+ };
360
+ value?: unknown;
361
+ ops?: readonly WriteOp[];
362
+ by: WriteCaller;
363
+ runId?: string;
364
+ }
365
+ /** Rule 11: the three "cannot apply" signals. */
366
+ type CannotApply = "stale" | "missing-record" | "broken-span";
367
+ /**
368
+ * A handler applies the write. Return nothing (or `{ status: "applied" }`) when it landed, or a
369
+ * `cannotApply` signal; THROWING an error whose `code` is a `CannotApply` signal means the same.
370
+ */
371
+ type WriteHandlerResult = void | {
372
+ status: "applied";
373
+ sentence?: string;
374
+ } | {
375
+ status: "cannot-apply";
376
+ cannotApply: CannotApply;
377
+ sentence?: string;
378
+ remedy?: string;
379
+ };
380
+ type WriteHandler = (request: WriteApplyRequest, signal: AbortSignal) => WriteHandlerResult | Promise<WriteHandlerResult>;
381
+ /** Files a `queued` write for an organization's review. */
382
+ interface WriteReviewPort {
383
+ file(request: WriteApplyRequest): Promise<{
384
+ sentence?: string;
385
+ remedy?: string;
386
+ } | void>;
387
+ }
388
+ interface WriteDoor {
389
+ write(request: WriteRequest, signal?: AbortSignal): Promise<Receipt>;
390
+ /** A feature's write handler that works with no page mounted. Refuses a second one for the same target. */
391
+ registerHeadless(surfaceName: string, targetName: string, handler: WriteHandler): () => void;
392
+ /** A mounted page's handler; wins over the headless one while registered. Returns unregister. */
393
+ registerLive(surfaceName: string, targetName: string, handler: WriteHandler): () => void;
394
+ }
395
+
254
396
  /**
255
397
  * Surface Actions — the shapes (Applets CONTRACTS §6, FROZEN; exported here as a superset).
256
398
  *
@@ -282,6 +424,8 @@ interface ActionError {
282
424
  message: string;
283
425
  retryable: boolean;
284
426
  diagnostic?: string;
427
+ /** Superset: the one-click remedy a write door refusal names. */
428
+ remedy?: string;
285
429
  }
286
430
  /** CONTRACTS §0 `Result<T>`. */
287
431
  type ActionResult<T = unknown> = {
@@ -319,6 +463,17 @@ type MandateRunRequest = ({
319
463
  by: ActionCaller;
320
464
  policy: ActionPolicy;
321
465
  };
466
+ /**
467
+ * CONTRACTS §0/§3 `RunRef` — the serializable identity of a started mandate run. A mandate
468
+ * Action answers with THIS, not the run's result: the result arrives later on the event stream
469
+ * `run:<requestId>` (or `IntelligencePort.result(ref)`). Same fields as `@ai-matrx/agents`'s
470
+ * `RunRef`, declared here because alchemy sits below agents and never imports it.
471
+ */
472
+ interface MandateRunRef {
473
+ requestId: string;
474
+ conversationId: string;
475
+ mandateKey: string;
476
+ }
322
477
 
323
478
  /**
324
479
  * `@ai-matrx/alchemy/ports` — the host ports (Matrx Alchemy CONTRACT §2.2).
@@ -338,7 +493,7 @@ type MandateRunRequest = ({
338
493
  * PP-01a `window` (host-rendered windows for a live preparation session)
339
494
  * PP-13a `transferKnobs` (organization default recipes)
340
495
  *
341
- * ALC-17 adds approvals/bodies/receipts.
496
+ * ALC-17 `door` (the one write door), `review` (queued writes)
342
497
  *
343
498
  * ── Absent-port semantics ───────────────────────────────────────────────────
344
499
  *
@@ -374,6 +529,10 @@ type MandateRunRequest = ({
374
529
  * approvals absent → a policy "ask" Action started by an agent, applet or automation is refused
375
530
  * ("needs a person's approval"); a person's own click is the approval
376
531
  * (`runs: "navigate"` goes through `overlay.navigate`; overlay absent → refused)
532
+ * door absent → write-target Actions dispatch to `pageActions` as before, and
533
+ * `Delivery.send` is refused with a remedy; bound → every such write goes
534
+ * through it and returns its receipt
535
+ * review absent → a write to a `queued` target is refused ("review isn't available here")
377
536
  *
378
537
  * Types only: this entry has no runtime code.
379
538
  */
@@ -527,13 +686,20 @@ interface AlchemyHostPorts {
527
686
  pageActions?: PageActionPort;
528
687
  /** Asks a person, in place, to approve an `ask` Action that an agent, applet or automation started. */
529
688
  approvals?: ApprovalPort;
689
+ /** `createWriteDoor(...)`: every write to a declared target lands here and returns a receipt. */
690
+ door?: WriteDoor;
691
+ /** Files a write to a `queued` target for an organization's review. */
692
+ review?: WriteReviewPort;
530
693
  }
531
694
  interface ServerActionPort {
532
695
  run(request: ServerActionRunRequest): Promise<ServerActionRunResponse>;
533
696
  }
534
697
  interface MandatePort {
535
- /** Resolves with the run's result (a kind envelope or JSON); rejects with the system's sentence. */
536
- run(request: MandateRunRequest): Promise<unknown>;
698
+ /**
699
+ * STARTS the run and resolves with its `RunRef` (CONTRACTS §3/§6) — never the run's result, which
700
+ * arrives later on `run:<requestId>` / `IntelligencePort.result(ref)`. Rejects with the system's sentence.
701
+ */
702
+ run(request: MandateRunRequest): Promise<MandateRunRef>;
537
703
  }
538
704
  interface ApprovalRequest {
539
705
  action: string;
@@ -251,6 +251,148 @@ interface ClickTarget {
251
251
  host?: unknown;
252
252
  }
253
253
 
254
+ /**
255
+ * Operate — the format and delivery contract (Matrx Alchemy CONTRACT §2.3, ALC-16).
256
+ *
257
+ * A format is an ADAPTER: `supports` is synchronous and loads nothing; `load` dynamically
258
+ * imports the engine the first time the format is chosen (print for rich-html/html/pdf/docx/
259
+ * epub, SheetJS for xlsx). Delivery accepts only a SEALED artifact, so the bytes previewed are
260
+ * the bytes copied, downloaded or sent.
261
+ */
262
+
263
+ /** A reference to a declared write target (resolved by the one write door, ALC-17). */
264
+ interface WriteTargetRef {
265
+ surfaceName?: string;
266
+ target: string;
267
+ item?: {
268
+ itemType: string;
269
+ identity: Record<string, string>;
270
+ };
271
+ }
272
+ /**
273
+ * Every write returns one (CONTRACT §2.3; the door is `createWriteDoor`, ALC-17). An applied
274
+ * receipt may carry a `warning` with its `fix` (rule 10: a person's kind mismatch warns, never refuses).
275
+ * A refusal's `reason` is a `WriteRefusalReason` (`./door`).
276
+ */
277
+ type Receipt = {
278
+ status: "applied";
279
+ to: WriteTargetRef;
280
+ sentence: string;
281
+ warning?: string;
282
+ fix?: string;
283
+ } | {
284
+ status: "queued";
285
+ to: WriteTargetRef;
286
+ sentence: string;
287
+ remedy: string;
288
+ } | {
289
+ status: "refused";
290
+ to: WriteTargetRef;
291
+ reason: string;
292
+ sentence: string;
293
+ remedy: string;
294
+ };
295
+
296
+ /**
297
+ * THE ONE WRITE DOOR (Matrx Alchemy ALC-17; Applets CONTRACTS §6 "receipts through the one write door").
298
+ *
299
+ * Every write — a person's edit, a menu row, a destination send, an agent, an applet, an automation,
300
+ * an Action projected from a write target — lands through `door.write`. The door:
301
+ *
302
+ * 1. resolves the DECLARED write target (the surface's own, then its ancestors'); an undeclared
303
+ * target is refused by name;
304
+ * 2. enforces the mode: `stream` takes ops from the target's `streamOps`; ops on any other target
305
+ * need `patchable` (+ `updatesValue`); a value on a `stream` target is refused;
306
+ * 3. checks the kind (rule 10): a person's mismatch WARNS (the receipt is still applied, with a
307
+ * `warning` and a `fix`); an agent's, applet's or automation's malformed write is REFUSED;
308
+ * 4. applies the policy: manual → a person only; ask → a person's own act is the approval, anyone
309
+ * else asks through `approvals`; queued → filed through `review`; auto → applies;
310
+ * 5. applies through the handler: the mounted page's LIVE handler wins over the HEADLESS one;
311
+ * neither → refused `unapplicable` ("open <surface> to apply it");
312
+ * 6. turns stale / missing record / broken protected span into "cannot apply" with a one-click
313
+ * remedy (rule 11).
314
+ *
315
+ * Every outcome is a `Receipt` carrying the system's own sentence. `write` never throws.
316
+ *
317
+ * Who counts as a person: `person`, `menu` and `destination` (a destination send is a person's own
318
+ * click on "Send to …"). `agent`, `applet` and `automation` are not.
319
+ *
320
+ * Open-risk ruling — stream ops into an entity target: an entity write is ONE atomic save. Ops reach
321
+ * an entity target only when it is declared `patchable` (the ops are handed to the handler in one
322
+ * call and commit as one save); a non-patchable entity target refuses ops with the remedy "stream
323
+ * into the editor, then save". Op-by-op persistence into a record does not exist.
324
+ */
325
+
326
+ /** Who is writing. `menu` and `destination` are a person's own act. */
327
+ type WriteCaller = "person" | "agent" | "applet" | "automation" | "destination" | "menu";
328
+ /** One text op for a `stream` (or `patchable`) target — the WidgetHandle op set. */
329
+ interface WriteOp {
330
+ op: StreamOp;
331
+ /** The text the op inserts or replaces with. */
332
+ text?: string;
333
+ /** What `replace` / `insert_before` / `insert_after` / `patch` anchor on. */
334
+ find?: string;
335
+ }
336
+ interface WriteRequest {
337
+ surfaceName: string;
338
+ /** A write target name; `"<itemType>.<name>"` names an item's target. */
339
+ target: string;
340
+ itemType?: string;
341
+ /** Which item, by its declared identity values. Required for an item target. */
342
+ itemIdentity?: Record<string, string>;
343
+ /** A whole value. Exactly one of `value` / `ops`. */
344
+ value?: unknown;
345
+ /** Text ops (stream / patchable targets). */
346
+ ops?: readonly WriteOp[];
347
+ by: WriteCaller;
348
+ runId?: string;
349
+ }
350
+ /** What a handler receives: the request plus the declared target it resolved to. */
351
+ interface WriteApplyRequest {
352
+ surfaceName: string;
353
+ /** The surface that declares the target (an ancestor when inherited). */
354
+ declaredOn: string;
355
+ target: WriteTarget;
356
+ item?: {
357
+ itemType: string;
358
+ identity: Record<string, string>;
359
+ };
360
+ value?: unknown;
361
+ ops?: readonly WriteOp[];
362
+ by: WriteCaller;
363
+ runId?: string;
364
+ }
365
+ /** Rule 11: the three "cannot apply" signals. */
366
+ type CannotApply = "stale" | "missing-record" | "broken-span";
367
+ /**
368
+ * A handler applies the write. Return nothing (or `{ status: "applied" }`) when it landed, or a
369
+ * `cannotApply` signal; THROWING an error whose `code` is a `CannotApply` signal means the same.
370
+ */
371
+ type WriteHandlerResult = void | {
372
+ status: "applied";
373
+ sentence?: string;
374
+ } | {
375
+ status: "cannot-apply";
376
+ cannotApply: CannotApply;
377
+ sentence?: string;
378
+ remedy?: string;
379
+ };
380
+ type WriteHandler = (request: WriteApplyRequest, signal: AbortSignal) => WriteHandlerResult | Promise<WriteHandlerResult>;
381
+ /** Files a `queued` write for an organization's review. */
382
+ interface WriteReviewPort {
383
+ file(request: WriteApplyRequest): Promise<{
384
+ sentence?: string;
385
+ remedy?: string;
386
+ } | void>;
387
+ }
388
+ interface WriteDoor {
389
+ write(request: WriteRequest, signal?: AbortSignal): Promise<Receipt>;
390
+ /** A feature's write handler that works with no page mounted. Refuses a second one for the same target. */
391
+ registerHeadless(surfaceName: string, targetName: string, handler: WriteHandler): () => void;
392
+ /** A mounted page's handler; wins over the headless one while registered. Returns unregister. */
393
+ registerLive(surfaceName: string, targetName: string, handler: WriteHandler): () => void;
394
+ }
395
+
254
396
  /**
255
397
  * Surface Actions — the shapes (Applets CONTRACTS §6, FROZEN; exported here as a superset).
256
398
  *
@@ -282,6 +424,8 @@ interface ActionError {
282
424
  message: string;
283
425
  retryable: boolean;
284
426
  diagnostic?: string;
427
+ /** Superset: the one-click remedy a write door refusal names. */
428
+ remedy?: string;
285
429
  }
286
430
  /** CONTRACTS §0 `Result<T>`. */
287
431
  type ActionResult<T = unknown> = {
@@ -319,6 +463,17 @@ type MandateRunRequest = ({
319
463
  by: ActionCaller;
320
464
  policy: ActionPolicy;
321
465
  };
466
+ /**
467
+ * CONTRACTS §0/§3 `RunRef` — the serializable identity of a started mandate run. A mandate
468
+ * Action answers with THIS, not the run's result: the result arrives later on the event stream
469
+ * `run:<requestId>` (or `IntelligencePort.result(ref)`). Same fields as `@ai-matrx/agents`'s
470
+ * `RunRef`, declared here because alchemy sits below agents and never imports it.
471
+ */
472
+ interface MandateRunRef {
473
+ requestId: string;
474
+ conversationId: string;
475
+ mandateKey: string;
476
+ }
322
477
 
323
478
  /**
324
479
  * `@ai-matrx/alchemy/ports` — the host ports (Matrx Alchemy CONTRACT §2.2).
@@ -338,7 +493,7 @@ type MandateRunRequest = ({
338
493
  * PP-01a `window` (host-rendered windows for a live preparation session)
339
494
  * PP-13a `transferKnobs` (organization default recipes)
340
495
  *
341
- * ALC-17 adds approvals/bodies/receipts.
496
+ * ALC-17 `door` (the one write door), `review` (queued writes)
342
497
  *
343
498
  * ── Absent-port semantics ───────────────────────────────────────────────────
344
499
  *
@@ -374,6 +529,10 @@ type MandateRunRequest = ({
374
529
  * approvals absent → a policy "ask" Action started by an agent, applet or automation is refused
375
530
  * ("needs a person's approval"); a person's own click is the approval
376
531
  * (`runs: "navigate"` goes through `overlay.navigate`; overlay absent → refused)
532
+ * door absent → write-target Actions dispatch to `pageActions` as before, and
533
+ * `Delivery.send` is refused with a remedy; bound → every such write goes
534
+ * through it and returns its receipt
535
+ * review absent → a write to a `queued` target is refused ("review isn't available here")
377
536
  *
378
537
  * Types only: this entry has no runtime code.
379
538
  */
@@ -527,13 +686,20 @@ interface AlchemyHostPorts {
527
686
  pageActions?: PageActionPort;
528
687
  /** Asks a person, in place, to approve an `ask` Action that an agent, applet or automation started. */
529
688
  approvals?: ApprovalPort;
689
+ /** `createWriteDoor(...)`: every write to a declared target lands here and returns a receipt. */
690
+ door?: WriteDoor;
691
+ /** Files a write to a `queued` target for an organization's review. */
692
+ review?: WriteReviewPort;
530
693
  }
531
694
  interface ServerActionPort {
532
695
  run(request: ServerActionRunRequest): Promise<ServerActionRunResponse>;
533
696
  }
534
697
  interface MandatePort {
535
- /** Resolves with the run's result (a kind envelope or JSON); rejects with the system's sentence. */
536
- run(request: MandateRunRequest): Promise<unknown>;
698
+ /**
699
+ * STARTS the run and resolves with its `RunRef` (CONTRACTS §3/§6) — never the run's result, which
700
+ * arrives later on `run:<requestId>` / `IntelligencePort.result(ref)`. Rejects with the system's sentence.
701
+ */
702
+ run(request: MandateRunRequest): Promise<MandateRunRef>;
537
703
  }
538
704
  interface ApprovalRequest {
539
705
  action: string;
@@ -251,6 +251,148 @@ interface ClickTarget {
251
251
  host?: unknown;
252
252
  }
253
253
 
254
+ /**
255
+ * Operate — the format and delivery contract (Matrx Alchemy CONTRACT §2.3, ALC-16).
256
+ *
257
+ * A format is an ADAPTER: `supports` is synchronous and loads nothing; `load` dynamically
258
+ * imports the engine the first time the format is chosen (print for rich-html/html/pdf/docx/
259
+ * epub, SheetJS for xlsx). Delivery accepts only a SEALED artifact, so the bytes previewed are
260
+ * the bytes copied, downloaded or sent.
261
+ */
262
+
263
+ /** A reference to a declared write target (resolved by the one write door, ALC-17). */
264
+ interface WriteTargetRef {
265
+ surfaceName?: string;
266
+ target: string;
267
+ item?: {
268
+ itemType: string;
269
+ identity: Record<string, string>;
270
+ };
271
+ }
272
+ /**
273
+ * Every write returns one (CONTRACT §2.3; the door is `createWriteDoor`, ALC-17). An applied
274
+ * receipt may carry a `warning` with its `fix` (rule 10: a person's kind mismatch warns, never refuses).
275
+ * A refusal's `reason` is a `WriteRefusalReason` (`./door`).
276
+ */
277
+ type Receipt = {
278
+ status: "applied";
279
+ to: WriteTargetRef;
280
+ sentence: string;
281
+ warning?: string;
282
+ fix?: string;
283
+ } | {
284
+ status: "queued";
285
+ to: WriteTargetRef;
286
+ sentence: string;
287
+ remedy: string;
288
+ } | {
289
+ status: "refused";
290
+ to: WriteTargetRef;
291
+ reason: string;
292
+ sentence: string;
293
+ remedy: string;
294
+ };
295
+
296
+ /**
297
+ * THE ONE WRITE DOOR (Matrx Alchemy ALC-17; Applets CONTRACTS §6 "receipts through the one write door").
298
+ *
299
+ * Every write — a person's edit, a menu row, a destination send, an agent, an applet, an automation,
300
+ * an Action projected from a write target — lands through `door.write`. The door:
301
+ *
302
+ * 1. resolves the DECLARED write target (the surface's own, then its ancestors'); an undeclared
303
+ * target is refused by name;
304
+ * 2. enforces the mode: `stream` takes ops from the target's `streamOps`; ops on any other target
305
+ * need `patchable` (+ `updatesValue`); a value on a `stream` target is refused;
306
+ * 3. checks the kind (rule 10): a person's mismatch WARNS (the receipt is still applied, with a
307
+ * `warning` and a `fix`); an agent's, applet's or automation's malformed write is REFUSED;
308
+ * 4. applies the policy: manual → a person only; ask → a person's own act is the approval, anyone
309
+ * else asks through `approvals`; queued → filed through `review`; auto → applies;
310
+ * 5. applies through the handler: the mounted page's LIVE handler wins over the HEADLESS one;
311
+ * neither → refused `unapplicable` ("open <surface> to apply it");
312
+ * 6. turns stale / missing record / broken protected span into "cannot apply" with a one-click
313
+ * remedy (rule 11).
314
+ *
315
+ * Every outcome is a `Receipt` carrying the system's own sentence. `write` never throws.
316
+ *
317
+ * Who counts as a person: `person`, `menu` and `destination` (a destination send is a person's own
318
+ * click on "Send to …"). `agent`, `applet` and `automation` are not.
319
+ *
320
+ * Open-risk ruling — stream ops into an entity target: an entity write is ONE atomic save. Ops reach
321
+ * an entity target only when it is declared `patchable` (the ops are handed to the handler in one
322
+ * call and commit as one save); a non-patchable entity target refuses ops with the remedy "stream
323
+ * into the editor, then save". Op-by-op persistence into a record does not exist.
324
+ */
325
+
326
+ /** Who is writing. `menu` and `destination` are a person's own act. */
327
+ type WriteCaller = "person" | "agent" | "applet" | "automation" | "destination" | "menu";
328
+ /** One text op for a `stream` (or `patchable`) target — the WidgetHandle op set. */
329
+ interface WriteOp {
330
+ op: StreamOp;
331
+ /** The text the op inserts or replaces with. */
332
+ text?: string;
333
+ /** What `replace` / `insert_before` / `insert_after` / `patch` anchor on. */
334
+ find?: string;
335
+ }
336
+ interface WriteRequest {
337
+ surfaceName: string;
338
+ /** A write target name; `"<itemType>.<name>"` names an item's target. */
339
+ target: string;
340
+ itemType?: string;
341
+ /** Which item, by its declared identity values. Required for an item target. */
342
+ itemIdentity?: Record<string, string>;
343
+ /** A whole value. Exactly one of `value` / `ops`. */
344
+ value?: unknown;
345
+ /** Text ops (stream / patchable targets). */
346
+ ops?: readonly WriteOp[];
347
+ by: WriteCaller;
348
+ runId?: string;
349
+ }
350
+ /** What a handler receives: the request plus the declared target it resolved to. */
351
+ interface WriteApplyRequest {
352
+ surfaceName: string;
353
+ /** The surface that declares the target (an ancestor when inherited). */
354
+ declaredOn: string;
355
+ target: WriteTarget;
356
+ item?: {
357
+ itemType: string;
358
+ identity: Record<string, string>;
359
+ };
360
+ value?: unknown;
361
+ ops?: readonly WriteOp[];
362
+ by: WriteCaller;
363
+ runId?: string;
364
+ }
365
+ /** Rule 11: the three "cannot apply" signals. */
366
+ type CannotApply = "stale" | "missing-record" | "broken-span";
367
+ /**
368
+ * A handler applies the write. Return nothing (or `{ status: "applied" }`) when it landed, or a
369
+ * `cannotApply` signal; THROWING an error whose `code` is a `CannotApply` signal means the same.
370
+ */
371
+ type WriteHandlerResult = void | {
372
+ status: "applied";
373
+ sentence?: string;
374
+ } | {
375
+ status: "cannot-apply";
376
+ cannotApply: CannotApply;
377
+ sentence?: string;
378
+ remedy?: string;
379
+ };
380
+ type WriteHandler = (request: WriteApplyRequest, signal: AbortSignal) => WriteHandlerResult | Promise<WriteHandlerResult>;
381
+ /** Files a `queued` write for an organization's review. */
382
+ interface WriteReviewPort {
383
+ file(request: WriteApplyRequest): Promise<{
384
+ sentence?: string;
385
+ remedy?: string;
386
+ } | void>;
387
+ }
388
+ interface WriteDoor {
389
+ write(request: WriteRequest, signal?: AbortSignal): Promise<Receipt>;
390
+ /** A feature's write handler that works with no page mounted. Refuses a second one for the same target. */
391
+ registerHeadless(surfaceName: string, targetName: string, handler: WriteHandler): () => void;
392
+ /** A mounted page's handler; wins over the headless one while registered. Returns unregister. */
393
+ registerLive(surfaceName: string, targetName: string, handler: WriteHandler): () => void;
394
+ }
395
+
254
396
  /**
255
397
  * Surface Actions — the shapes (Applets CONTRACTS §6, FROZEN; exported here as a superset).
256
398
  *
@@ -282,6 +424,8 @@ interface ActionError {
282
424
  message: string;
283
425
  retryable: boolean;
284
426
  diagnostic?: string;
427
+ /** Superset: the one-click remedy a write door refusal names. */
428
+ remedy?: string;
285
429
  }
286
430
  /** CONTRACTS §0 `Result<T>`. */
287
431
  type ActionResult<T = unknown> = {
@@ -319,6 +463,17 @@ type MandateRunRequest = ({
319
463
  by: ActionCaller;
320
464
  policy: ActionPolicy;
321
465
  };
466
+ /**
467
+ * CONTRACTS §0/§3 `RunRef` — the serializable identity of a started mandate run. A mandate
468
+ * Action answers with THIS, not the run's result: the result arrives later on the event stream
469
+ * `run:<requestId>` (or `IntelligencePort.result(ref)`). Same fields as `@ai-matrx/agents`'s
470
+ * `RunRef`, declared here because alchemy sits below agents and never imports it.
471
+ */
472
+ interface MandateRunRef {
473
+ requestId: string;
474
+ conversationId: string;
475
+ mandateKey: string;
476
+ }
322
477
 
323
478
  /**
324
479
  * `@ai-matrx/alchemy/ports` — the host ports (Matrx Alchemy CONTRACT §2.2).
@@ -338,7 +493,7 @@ type MandateRunRequest = ({
338
493
  * PP-01a `window` (host-rendered windows for a live preparation session)
339
494
  * PP-13a `transferKnobs` (organization default recipes)
340
495
  *
341
- * ALC-17 adds approvals/bodies/receipts.
496
+ * ALC-17 `door` (the one write door), `review` (queued writes)
342
497
  *
343
498
  * ── Absent-port semantics ───────────────────────────────────────────────────
344
499
  *
@@ -374,6 +529,10 @@ type MandateRunRequest = ({
374
529
  * approvals absent → a policy "ask" Action started by an agent, applet or automation is refused
375
530
  * ("needs a person's approval"); a person's own click is the approval
376
531
  * (`runs: "navigate"` goes through `overlay.navigate`; overlay absent → refused)
532
+ * door absent → write-target Actions dispatch to `pageActions` as before, and
533
+ * `Delivery.send` is refused with a remedy; bound → every such write goes
534
+ * through it and returns its receipt
535
+ * review absent → a write to a `queued` target is refused ("review isn't available here")
377
536
  *
378
537
  * Types only: this entry has no runtime code.
379
538
  */
@@ -527,13 +686,20 @@ interface AlchemyHostPorts {
527
686
  pageActions?: PageActionPort;
528
687
  /** Asks a person, in place, to approve an `ask` Action that an agent, applet or automation started. */
529
688
  approvals?: ApprovalPort;
689
+ /** `createWriteDoor(...)`: every write to a declared target lands here and returns a receipt. */
690
+ door?: WriteDoor;
691
+ /** Files a write to a `queued` target for an organization's review. */
692
+ review?: WriteReviewPort;
530
693
  }
531
694
  interface ServerActionPort {
532
695
  run(request: ServerActionRunRequest): Promise<ServerActionRunResponse>;
533
696
  }
534
697
  interface MandatePort {
535
- /** Resolves with the run's result (a kind envelope or JSON); rejects with the system's sentence. */
536
- run(request: MandateRunRequest): Promise<unknown>;
698
+ /**
699
+ * STARTS the run and resolves with its `RunRef` (CONTRACTS §3/§6) — never the run's result, which
700
+ * arrives later on `run:<requestId>` / `IntelligencePort.result(ref)`. Rejects with the system's sentence.
701
+ */
702
+ run(request: MandateRunRequest): Promise<MandateRunRef>;
537
703
  }
538
704
  interface ApprovalRequest {
539
705
  action: string;