effect-agent 0.1.0-beta.144 → 0.1.0-beta.149

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 (83) hide show
  1. package/dist/{Admin-CQMppCcy.d.mts → Admin-B8ISZyYf.d.mts} +37 -22
  2. package/dist/Agent-1DMSO_Fl.mjs.map +1 -1
  3. package/dist/{Agent-BWhD4Dvu.d.mts → Agent-Cjd_ZveM.d.mts} +7 -3
  4. package/dist/{AgentRuntime-xeCUXutN.mjs → AgentRuntime-D8U9gl9Q.mjs} +23 -18
  5. package/dist/AgentRuntime-D8U9gl9Q.mjs.map +1 -0
  6. package/dist/{AgentRuntime-B4kIzmSF.d.mts → AgentRuntime-sXpKcyxu.d.mts} +5 -5
  7. package/dist/{DurableAgentRuntime-D8GI66mP.mjs → DurableAgentRuntime-DLa4IwFi.mjs} +87 -49
  8. package/dist/DurableAgentRuntime-DLa4IwFi.mjs.map +1 -0
  9. package/dist/{EventSource-w9PonN9X.d.mts → EventSource-CkN05NT3.d.mts} +2 -2
  10. package/dist/{Messaging-Dnmk31yq.d.mts → Messaging-ccQURKCf.d.mts} +3 -3
  11. package/dist/{MessagingHost-CajpPDPG.d.mts → MessagingHost-D04wE-7W.d.mts} +2 -2
  12. package/dist/{Output-DhtWbBcw.d.mts → Output-CFD1CPfU.d.mts} +2 -2
  13. package/dist/{PreparedInputAdmission-BfcLy_k4.d.mts → PreparedInputAdmission-fctZOTnn.d.mts} +3 -3
  14. package/dist/{RunOptions-IlEXLG5F.d.mts → RunOptions-Bv9NpkPZ.d.mts} +10 -2
  15. package/dist/{Subagent-Cl8D1mFM.d.mts → Subagent-VHrANXFy.d.mts} +5 -5
  16. package/dist/{Subagent-8RzJkqJ9.mjs → Subagent-hG9lJ7tJ.mjs} +2 -2
  17. package/dist/{Subagent-8RzJkqJ9.mjs.map → Subagent-hG9lJ7tJ.mjs.map} +1 -1
  18. package/dist/{SubagentHost-B6f4PBC2.d.mts → SubagentHost-C-4A7tA2.d.mts} +2 -2
  19. package/dist/{Subscription-ClkC4_T0.d.mts → Subscription-B16A-48F.d.mts} +2 -2
  20. package/dist/{SubscriptionInput-CMjEKL1z.d.mts → SubscriptionInput-DIU7CiXE.d.mts} +2 -2
  21. package/dist/capabilities/Commands.d.mts +1 -1
  22. package/dist/capabilities/Messaging.d.mts +1 -1
  23. package/dist/capabilities/RunHooks.d.mts +1 -1
  24. package/dist/capabilities/Subagent.d.mts +1 -1
  25. package/dist/capabilities/Subagent.mjs +1 -1
  26. package/dist/core/Agent.d.mts +1 -1
  27. package/dist/durable/Admin.d.mts +1 -1
  28. package/dist/durable/AgentRegistration.d.mts +1 -1
  29. package/dist/durable/Certification.d.mts +1 -1
  30. package/dist/durable/CommittedActivity.d.mts +1 -1
  31. package/dist/durable/DurableAgentRuntime.d.mts +1 -1
  32. package/dist/durable/DurableAgentRuntime.mjs +1 -1
  33. package/dist/durable/EventSource.d.mts +1 -1
  34. package/dist/durable/GitHubWorkflowSource.d.mts +3 -3
  35. package/dist/durable/LifecyclePublication.d.mts +2 -2
  36. package/dist/durable/LifecyclePublication.mjs +32 -12
  37. package/dist/durable/LifecyclePublication.mjs.map +1 -1
  38. package/dist/durable/MessageDelivery.d.mts +3 -3
  39. package/dist/durable/MessageDeliveryStoreConformance.d.mts +1 -1
  40. package/dist/durable/PersistentHistory.d.mts +1 -1
  41. package/dist/durable/PreparedInputAdmission.d.mts +1 -1
  42. package/dist/durable/Recovery.d.mts +1 -1
  43. package/dist/durable/RunJournal.d.mts +1 -1
  44. package/dist/durable/Scheduling.d.mts +1 -1
  45. package/dist/durable/SubmissionLedger.d.mts +1 -1
  46. package/dist/durable/SubmissionLedgerConformance.d.mts +1 -1
  47. package/dist/durable/SubmissionStatus.d.mts +1 -1
  48. package/dist/durable/Subscription.d.mts +1 -1
  49. package/dist/durable/SubscriptionInput.d.mts +1 -1
  50. package/dist/durable/SubscriptionStoreConformance.d.mts +1 -1
  51. package/dist/durable/SubscriptionTools.d.mts +2 -2
  52. package/dist/durable/SubscriptionTransition.d.mts +1 -1
  53. package/dist/durable/Subscriptions.d.mts +5 -5
  54. package/dist/durable/ThreadContextHistory.d.mts +1 -1
  55. package/dist/durable/ThreadInvariants.d.mts +1 -1
  56. package/dist/durable/ThreadProjectionMaintenance.d.mts +1 -1
  57. package/dist/durable/ThreadStore.d.mts +1 -1
  58. package/dist/durable/ThreadStoreConformance.d.mts +1 -1
  59. package/dist/durable/WorkerHost.d.mts +25 -5
  60. package/dist/durable/WorkerHost.mjs.map +1 -1
  61. package/dist/engine/AgentRuntime.d.mts +1 -1
  62. package/dist/engine/AgentRuntime.mjs +1 -1
  63. package/dist/engine/MessagingHost.d.mts +1 -1
  64. package/dist/engine/Output.d.mts +1 -1
  65. package/dist/engine/Output.mjs +1 -1
  66. package/dist/engine/RunOptions.d.mts +1 -1
  67. package/dist/engine/RunOptions.mjs.map +1 -1
  68. package/dist/engine/SubagentHost.d.mts +1 -1
  69. package/dist/index.d.mts +13 -13
  70. package/dist/index.mjs +3 -3
  71. package/dist/{output-contract-CANJJGA5.mjs → output-contract-DS-S8mTv.mjs} +46 -10
  72. package/dist/{output-contract-CANJJGA5.mjs.map → output-contract-DS-S8mTv.mjs.map} +1 -1
  73. package/package.json +1 -1
  74. package/src/core/Agent.ts +6 -2
  75. package/src/durable/DurableAgentRuntime.ts +86 -78
  76. package/src/durable/LifecyclePublication.ts +79 -25
  77. package/src/durable/WorkerHost.ts +23 -1
  78. package/src/durable/internal/worker-host.ts +169 -17
  79. package/src/engine/RunOptions.ts +8 -0
  80. package/src/engine/internal/agent-runtime.ts +80 -18
  81. package/src/engine/internal/output-contract.ts +67 -8
  82. package/dist/AgentRuntime-xeCUXutN.mjs.map +0 -1
  83. package/dist/DurableAgentRuntime-D8GI66mP.mjs.map +0 -1
@@ -58,19 +58,55 @@ const outputSchemaContract = (definition) => {
58
58
  };
59
59
  const sameSystemMessage = Schema.toEquivalence(Prompt.SystemMessage);
60
60
  /**
61
- * Project system instructions and the output contract into one leading block.
62
- * Repeated Runs append instructions to canonical history; letting those copies
63
- * move the contract breaks the preceding user/tool prefix used by provider caches.
64
- * Keep the last occurrence of each equivalent system message, preserving the
65
- * precedence of distinct instructions and their native provider options.
61
+ * Keep supported system instructions in conversation order so changing late guidance
62
+ * cannot invalidate the preceding user/tool cache prefix. Omit an exact repeat
63
+ * only when no distinct system instruction intervened; returning to an earlier
64
+ * instruction after a different one must preserve the new directive's precedence.
65
+ * Anchor the immutable output contract after the initial system block.
66
+ * Restore leading static instructions for caller-supplied conversation-only history.
66
67
  *
67
68
  * This runs after preparation and compaction. Canonical messages, protected
68
69
  * instruction/input spans and compaction coverage retain their original positions.
69
- * Conversation order and message identities are unchanged. A single system block
70
- * also prevents Anthropic's last-system-group conversion from discarding earlier
71
- * application instructions or the output contract.
70
+ * Adapters without this capability retain the grouped-system projection: keep the
71
+ * last equivalent instruction with its native options, then the contract and
72
+ * conversation. This prevents older Anthropic adapters from discarding system groups.
72
73
  */
73
- const prepareModelPrompt = (prompt, contract) => {
74
+ const prepareModelPrompt = (prompt, contract, systemMessagesInHistory, staticInstructions) => {
75
+ if (systemMessagesInHistory) {
76
+ const content = [];
77
+ let lastSystem;
78
+ let ordered = prompt.content;
79
+ if (staticInstructions !== void 0) {
80
+ const firstConversation = prompt.content.findIndex((message) => message.role !== "system");
81
+ const leading = prompt.content.slice(0, firstConversation === -1 ? prompt.content.length : firstConversation);
82
+ const restored = [];
83
+ const instructions = typeof staticInstructions === "string" ? [Prompt.systemMessage({ content: staticInstructions })] : Prompt.make(staticInstructions).content;
84
+ for (const message of instructions) {
85
+ if (message.role !== "system") break;
86
+ if (leading.some((candidate) => candidate.role === "system" && sameSystemMessage(candidate, message))) break;
87
+ const original = prompt.content.find((candidate) => candidate.role === "system" && sameSystemMessage(candidate, message));
88
+ if (original === void 0) break;
89
+ restored.push(original);
90
+ }
91
+ if (restored.length > 0) ordered = [
92
+ ...leading,
93
+ ...restored,
94
+ ...prompt.content.slice(leading.length)
95
+ ];
96
+ }
97
+ for (const message of ordered) {
98
+ if (message.role === "system") {
99
+ if (lastSystem !== void 0 && sameSystemMessage(lastSystem, message)) continue;
100
+ lastSystem = message;
101
+ }
102
+ content.push(message);
103
+ }
104
+ if (contract !== void 0) {
105
+ const firstConversation = content.findIndex((message) => message.role !== "system");
106
+ content.splice(firstConversation === -1 ? content.length : firstConversation, 0, contract);
107
+ }
108
+ return Prompt.fromMessages(content);
109
+ }
74
110
  const systems = [];
75
111
  const conversation = [];
76
112
  const seen = /* @__PURE__ */ new Map();
@@ -96,4 +132,4 @@ const prepareModelPrompt = (prompt, contract) => {
96
132
  //#endregion
97
133
  export { textOutput as i, outputSchemaContract as n, prepareModelPrompt as r, isTextOutput as t };
98
134
 
99
- //# sourceMappingURL=output-contract-CANJJGA5.mjs.map
135
+ //# sourceMappingURL=output-contract-DS-S8mTv.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"output-contract-CANJJGA5.mjs","names":[],"sources":["../src/engine/internal/output-contract.ts"],"sourcesContent":["import { Schema } from \"effect\";\nimport { Prompt, Tool } from \"effect/unstable/ai\";\n\nimport type * as Agent from \"../../core/Agent.ts\";\n\nconst outputFormatAnnotation = \"@effect-agent/engine/Output/format\";\n\n/**\n * Declare ordinary assistant text as an Agent's final-output wire format. Apply this to the\n * complete output Schema after composing transformations. Its encoded type must be string;\n * decoding, checks, transformations, and service requirements remain owned by that Schema.\n * Text is preserved verbatim, including whitespace, quotes, and an empty reply when allowed.\n * Required completion Tools still take precedence. Unmarked Schemas use JSON final output.\n *\n * @example\n * ```ts\n * output: Output.text(Schema.String.check(Schema.isMaxLength(20_000)))\n * ```\n */\nexport const textOutput = <S extends Schema.Top & { readonly Encoded: string }>(\n schema: S,\n): S[\"Rebuild\"] => schema.annotate({ [outputFormatAnnotation]: \"text\" });\n\nexport const isTextOutput = (schema: Schema.Top): boolean =>\n Schema.resolveAnnotations(schema)?.[outputFormatAnnotation] === \"text\";\n\n/**\n * Model-visible final-output contract (RUN-028).\n *\n * For ordinary text completion, the interpreter's only output-conformance\n * point is `decodeFinalOutput`, which validates the final text after the\n * model has already finished. This module renders that Schema's wire contract\n * with the same Effect AI derivation the providers use for Tool parameters\n * and states it as one framework-owned system message on every model request.\n * A required completion Tool instead gets a native-tool directive: its Tool\n * parameter Schema is already carried by the provider request, and ordinary\n * final text is not an allowed completion path.\n *\n * The contract is a per-request projection of the immutable definition —\n * exactly like the Tool schemas the request already carries. It is inserted\n * at model-request materialization (after context preparation) and never\n * into official history, so canonical records, run events, and the committed\n * DN/DC golden are unchanged.\n *\n */\n\n/** Rendering outcome for one definition's output Schema. */\ntype OutputContract =\n | {\n readonly _tag: \"rendered\";\n /** The complete system-message text: directive plus the derived JSON Schema. */\n readonly message: string;\n readonly part: Prompt.SystemMessage;\n }\n | {\n readonly _tag: \"unrenderable\";\n /** Why Effect AI's JSON-Schema derivation rejected the output Schema. */\n readonly reason: string;\n };\n\nconst contractDirective = (definition: Agent.AnyDefinition): string =>\n definition.completion === undefined\n ? \"Final output contract: when the task is complete, the final assistant message must be only \" +\n \"JSON that is valid against this JSON Schema — no prose, no Markdown code fences, nothing \" +\n \"before or after the JSON.\"\n : `Final output contract: when the task is complete without calling the \"${definition.completion.tool}\" completion Tool, the final assistant message must be only ` +\n \"JSON that is valid against this JSON Schema — no prose, no Markdown code fences, nothing \" +\n `before or after the JSON. When calling the \"${definition.completion.tool}\" completion Tool, never place this private Agent output JSON in any Tool argument; follow the Tool's parameter schema instead. Call it as the sole application Tool Call, after receiving any other needed application Tool results. Completed provider Tool results may accompany it. The engine projects the successful completion Tool result into the Agent output.`;\n\nconst requiredCompletionDirective = (tool: string, hasAlternatives: boolean): string =>\n `Final output contract: ${hasAlternatives ? \"otherwise complete\" : \"complete only\"} by calling the required completion Tool ${JSON.stringify(tool)} ` +\n \"as the sole application Tool Call in its batch. Completed provider Tool results may accompany it. Do not emit an ordinary final assistant text answer. \" +\n \"The Tool's canonical parameters and successful result are projected and validated as the Agent output.\";\n\n/**\n * Render the model-visible final-output contract for one definition. The\n * derivation and message identity are stable for an immutable definition.\n * An output Schema the Effect AI derivation cannot represent is reported as\n * `unrenderable`; the caller falls back to the prior behavior — the contract\n * is guidance, and a Schema that decodes but does not render must not become\n * a new failure mode.\n */\nconst rendered = (message: string): OutputContract => ({\n _tag: \"rendered\",\n message,\n part: Prompt.makeMessage(\"system\", { content: message }),\n});\n\nconst renderOutputSchemaContract = (definition: Agent.AnyDefinition): OutputContract => {\n if (definition.completion?.required === true) {\n return rendered(\n requiredCompletionDirective(\n definition.completion.tool,\n (definition.completionFromTools?.length ?? 0) > 0,\n ),\n );\n }\n if (isTextOutput(definition.output)) {\n return rendered(\n \"Final output contract: write the final reply as ordinary assistant text, without JSON wrapping. \" +\n \"An empty reply is valid only when allowed by the output Schema and the task instructions.\" +\n (definition.completion === undefined\n ? \"\"\n : ` When calling the \"${definition.completion.tool}\" completion Tool, follow its parameter schema instead and call it as the sole application Tool Call, after receiving any other needed application Tool results; completed provider Tool results may accompany it; the engine projects its successful result into the Agent output.`),\n );\n }\n try {\n const jsonSchema = Tool.getJsonSchemaFromSchema(definition.output);\n\n return rendered(\n `${contractDirective(definition)}\\n\\n${JSON.stringify(jsonSchema, undefined, 2)}`,\n );\n } catch (cause) {\n return {\n _tag: \"unrenderable\",\n reason: cause instanceof Error ? cause.message : String(cause),\n };\n }\n};\n\n// Native incremental-response tracking recognizes message objects, not rendered text.\n// Weak keys retain neither discarded definitions nor their Schema graphs.\nconst outputContracts = new WeakMap<Agent.AnyDefinition, OutputContract>();\n\nexport const outputSchemaContract = (definition: Agent.AnyDefinition): OutputContract => {\n const cached = outputContracts.get(definition);\n\n if (cached !== undefined) return cached;\n const base = renderOutputSchemaContract(definition);\n const alternatives = definition.completionFromTools ?? [];\n\n const contract =\n base._tag === \"rendered\" && alternatives.length > 0\n ? rendered(\n `The following action Tools may complete the Run from their successful canonical result: ${alternatives.map((declaration) => JSON.stringify(declaration.tool)).join(\", \")}. ` +\n \"Call such a Tool as the sole application Tool Call, following its parameter schema. Completed provider Tool results may accompany it. It may complete the Run only when it satisfies the whole request. \" +\n \"An incomplete or pending result continues the Run. These actions are unavailable during finalization after budget exhaustion.\\n\\n\" +\n base.message,\n )\n : base;\n\n outputContracts.set(definition, contract);\n\n return contract;\n};\n\nconst sameSystemMessage = Schema.toEquivalence(Prompt.SystemMessage);\n\n/**\n * Project system instructions and the output contract into one leading block.\n * Repeated Runs append instructions to canonical history; letting those copies\n * move the contract breaks the preceding user/tool prefix used by provider caches.\n * Keep the last occurrence of each equivalent system message, preserving the\n * precedence of distinct instructions and their native provider options.\n *\n * This runs after preparation and compaction. Canonical messages, protected\n * instruction/input spans and compaction coverage retain their original positions.\n * Conversation order and message identities are unchanged. A single system block\n * also prevents Anthropic's last-system-group conversion from discarding earlier\n * application instructions or the output contract.\n */\nexport const prepareModelPrompt = (\n prompt: Prompt.Prompt,\n contract: Prompt.SystemMessage | undefined,\n): Prompt.Prompt => {\n const systems: Array<Prompt.SystemMessage> = [];\n const conversation: Array<Prompt.Message> = [];\n const seen = new Map<string, Array<Prompt.SystemMessage>>();\n\n for (let index = prompt.content.length - 1; index >= 0; index -= 1) {\n const message = prompt.content[index];\n\n if (message === undefined) continue;\n if (message.role !== \"system\") {\n conversation.push(message);\n continue;\n }\n const variants = seen.get(message.content);\n\n if (variants?.some((previous) => sameSystemMessage(previous, message))) continue;\n if (variants === undefined) seen.set(message.content, [message]);\n else variants.push(message);\n systems.push(message);\n }\n\n return Prompt.fromMessages([\n ...systems.reverse(),\n ...(contract === undefined ? [] : [contract]),\n ...conversation.reverse(),\n ]);\n};\n"],"mappings":";;;AAKA,MAAM,yBAAyB;;;;;;;;;;;;;AAc/B,MAAa,cACX,WACiB,OAAO,SAAS,GAAG,yBAAyB,OAAO,CAAC;AAEvE,MAAa,gBAAgB,WAC3B,OAAO,mBAAmB,MAAM,CAAC,GAAG,4BAA4B;AAoClE,MAAM,qBAAqB,eACzB,WAAW,eAAe,KAAA,IACtB,kNAGA,yEAAyE,WAAW,WAAW,KAAK,mMAErD,WAAW,WAAW,KAAK;AAEhF,MAAM,+BAA+B,MAAc,oBACjD,0BAA0B,kBAAkB,uBAAuB,gBAAgB,2CAA2C,KAAK,UAAU,IAAI,EAAE;;;;;;;;;AAYrJ,MAAM,YAAY,aAAqC;CACrD,MAAM;CACN;CACA,MAAM,OAAO,YAAY,UAAU,EAAE,SAAS,QAAQ,CAAC;AACzD;AAEA,MAAM,8BAA8B,eAAoD;CACtF,IAAI,WAAW,YAAY,aAAa,MACtC,OAAO,SACL,4BACE,WAAW,WAAW,OACrB,WAAW,qBAAqB,UAAU,KAAK,CAClD,CACF;CAEF,IAAI,aAAa,WAAW,MAAM,GAChC,OAAO,SACL,+LAEG,WAAW,eAAe,KAAA,IACvB,KACA,sBAAsB,WAAW,WAAW,KAAK,qRACzD;CAEF,IAAI;EACF,MAAM,aAAa,KAAK,wBAAwB,WAAW,MAAM;EAEjE,OAAO,SACL,GAAG,kBAAkB,UAAU,EAAE,MAAM,KAAK,UAAU,YAAY,KAAA,GAAW,CAAC,GAChF;CACF,SAAS,OAAO;EACd,OAAO;GACL,MAAM;GACN,QAAQ,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;EAC/D;CACF;AACF;AAIA,MAAM,kCAAkB,IAAI,QAA6C;AAEzE,MAAa,wBAAwB,eAAoD;CACvF,MAAM,SAAS,gBAAgB,IAAI,UAAU;CAE7C,IAAI,WAAW,KAAA,GAAW,OAAO;CACjC,MAAM,OAAO,2BAA2B,UAAU;CAClD,MAAM,eAAe,WAAW,uBAAuB,CAAC;CAExD,MAAM,WACJ,KAAK,SAAS,cAAc,aAAa,SAAS,IAC9C,SACE,2FAA2F,aAAa,KAAK,gBAAgB,KAAK,UAAU,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE;;IAGxK,KAAK,OACT,IACA;CAEN,gBAAgB,IAAI,YAAY,QAAQ;CAExC,OAAO;AACT;AAEA,MAAM,oBAAoB,OAAO,cAAc,OAAO,aAAa;;;;;;;;;;;;;;AAenE,MAAa,sBACX,QACA,aACkB;CAClB,MAAM,UAAuC,CAAC;CAC9C,MAAM,eAAsC,CAAC;CAC7C,MAAM,uBAAO,IAAI,IAAyC;CAE1D,KAAK,IAAI,QAAQ,OAAO,QAAQ,SAAS,GAAG,SAAS,GAAG,SAAS,GAAG;EAClE,MAAM,UAAU,OAAO,QAAQ;EAE/B,IAAI,YAAY,KAAA,GAAW;EAC3B,IAAI,QAAQ,SAAS,UAAU;GAC7B,aAAa,KAAK,OAAO;GACzB;EACF;EACA,MAAM,WAAW,KAAK,IAAI,QAAQ,OAAO;EAEzC,IAAI,UAAU,MAAM,aAAa,kBAAkB,UAAU,OAAO,CAAC,GAAG;EACxE,IAAI,aAAa,KAAA,GAAW,KAAK,IAAI,QAAQ,SAAS,CAAC,OAAO,CAAC;OAC1D,SAAS,KAAK,OAAO;EAC1B,QAAQ,KAAK,OAAO;CACtB;CAEA,OAAO,OAAO,aAAa;EACzB,GAAG,QAAQ,QAAQ;EACnB,GAAI,aAAa,KAAA,IAAY,CAAC,IAAI,CAAC,QAAQ;EAC3C,GAAG,aAAa,QAAQ;CAC1B,CAAC;AACH"}
1
+ {"version":3,"file":"output-contract-DS-S8mTv.mjs","names":[],"sources":["../src/engine/internal/output-contract.ts"],"sourcesContent":["import { Schema } from \"effect\";\nimport { Prompt, Tool } from \"effect/unstable/ai\";\n\nimport type * as Agent from \"../../core/Agent.ts\";\n\nconst outputFormatAnnotation = \"@effect-agent/engine/Output/format\";\n\n/**\n * Declare ordinary assistant text as an Agent's final-output wire format. Apply this to the\n * complete output Schema after composing transformations. Its encoded type must be string;\n * decoding, checks, transformations, and service requirements remain owned by that Schema.\n * Text is preserved verbatim, including whitespace, quotes, and an empty reply when allowed.\n * Required completion Tools still take precedence. Unmarked Schemas use JSON final output.\n *\n * @example\n * ```ts\n * output: Output.text(Schema.String.check(Schema.isMaxLength(20_000)))\n * ```\n */\nexport const textOutput = <S extends Schema.Top & { readonly Encoded: string }>(\n schema: S,\n): S[\"Rebuild\"] => schema.annotate({ [outputFormatAnnotation]: \"text\" });\n\nexport const isTextOutput = (schema: Schema.Top): boolean =>\n Schema.resolveAnnotations(schema)?.[outputFormatAnnotation] === \"text\";\n\n/**\n * Model-visible final-output contract (RUN-028).\n *\n * For ordinary text completion, the interpreter's only output-conformance\n * point is `decodeFinalOutput`, which validates the final text after the\n * model has already finished. This module renders that Schema's wire contract\n * with the same Effect AI derivation the providers use for Tool parameters\n * and states it as one framework-owned system message on every model request.\n * A required completion Tool instead gets a native-tool directive: its Tool\n * parameter Schema is already carried by the provider request, and ordinary\n * final text is not an allowed completion path.\n *\n * The contract is a per-request projection of the immutable definition —\n * exactly like the Tool schemas the request already carries. It is inserted\n * at model-request materialization (after context preparation) and never\n * into official history, so canonical records, run events, and the committed\n * DN/DC golden are unchanged.\n *\n */\n\n/** Rendering outcome for one definition's output Schema. */\ntype OutputContract =\n | {\n readonly _tag: \"rendered\";\n /** The complete system-message text: directive plus the derived JSON Schema. */\n readonly message: string;\n readonly part: Prompt.SystemMessage;\n }\n | {\n readonly _tag: \"unrenderable\";\n /** Why Effect AI's JSON-Schema derivation rejected the output Schema. */\n readonly reason: string;\n };\n\nconst contractDirective = (definition: Agent.AnyDefinition): string =>\n definition.completion === undefined\n ? \"Final output contract: when the task is complete, the final assistant message must be only \" +\n \"JSON that is valid against this JSON Schema — no prose, no Markdown code fences, nothing \" +\n \"before or after the JSON.\"\n : `Final output contract: when the task is complete without calling the \"${definition.completion.tool}\" completion Tool, the final assistant message must be only ` +\n \"JSON that is valid against this JSON Schema — no prose, no Markdown code fences, nothing \" +\n `before or after the JSON. When calling the \"${definition.completion.tool}\" completion Tool, never place this private Agent output JSON in any Tool argument; follow the Tool's parameter schema instead. Call it as the sole application Tool Call, after receiving any other needed application Tool results. Completed provider Tool results may accompany it. The engine projects the successful completion Tool result into the Agent output.`;\n\nconst requiredCompletionDirective = (tool: string, hasAlternatives: boolean): string =>\n `Final output contract: ${hasAlternatives ? \"otherwise complete\" : \"complete only\"} by calling the required completion Tool ${JSON.stringify(tool)} ` +\n \"as the sole application Tool Call in its batch. Completed provider Tool results may accompany it. Do not emit an ordinary final assistant text answer. \" +\n \"The Tool's canonical parameters and successful result are projected and validated as the Agent output.\";\n\n/**\n * Render the model-visible final-output contract for one definition. The\n * derivation and message identity are stable for an immutable definition.\n * An output Schema the Effect AI derivation cannot represent is reported as\n * `unrenderable`; the caller falls back to the prior behavior — the contract\n * is guidance, and a Schema that decodes but does not render must not become\n * a new failure mode.\n */\nconst rendered = (message: string): OutputContract => ({\n _tag: \"rendered\",\n message,\n part: Prompt.makeMessage(\"system\", { content: message }),\n});\n\nconst renderOutputSchemaContract = (definition: Agent.AnyDefinition): OutputContract => {\n if (definition.completion?.required === true) {\n return rendered(\n requiredCompletionDirective(\n definition.completion.tool,\n (definition.completionFromTools?.length ?? 0) > 0,\n ),\n );\n }\n if (isTextOutput(definition.output)) {\n return rendered(\n \"Final output contract: write the final reply as ordinary assistant text, without JSON wrapping. \" +\n \"An empty reply is valid only when allowed by the output Schema and the task instructions.\" +\n (definition.completion === undefined\n ? \"\"\n : ` When calling the \"${definition.completion.tool}\" completion Tool, follow its parameter schema instead and call it as the sole application Tool Call, after receiving any other needed application Tool results; completed provider Tool results may accompany it; the engine projects its successful result into the Agent output.`),\n );\n }\n try {\n const jsonSchema = Tool.getJsonSchemaFromSchema(definition.output);\n\n return rendered(\n `${contractDirective(definition)}\\n\\n${JSON.stringify(jsonSchema, undefined, 2)}`,\n );\n } catch (cause) {\n return {\n _tag: \"unrenderable\",\n reason: cause instanceof Error ? cause.message : String(cause),\n };\n }\n};\n\n// Native incremental-response tracking recognizes message objects, not rendered text.\n// Weak keys retain neither discarded definitions nor their Schema graphs.\nconst outputContracts = new WeakMap<Agent.AnyDefinition, OutputContract>();\n\nexport const outputSchemaContract = (definition: Agent.AnyDefinition): OutputContract => {\n const cached = outputContracts.get(definition);\n\n if (cached !== undefined) return cached;\n const base = renderOutputSchemaContract(definition);\n const alternatives = definition.completionFromTools ?? [];\n\n const contract =\n base._tag === \"rendered\" && alternatives.length > 0\n ? rendered(\n `The following action Tools may complete the Run from their successful canonical result: ${alternatives.map((declaration) => JSON.stringify(declaration.tool)).join(\", \")}. ` +\n \"Call such a Tool as the sole application Tool Call, following its parameter schema. Completed provider Tool results may accompany it. It may complete the Run only when it satisfies the whole request. \" +\n \"An incomplete or pending result continues the Run. These actions are unavailable during finalization after budget exhaustion.\\n\\n\" +\n base.message,\n )\n : base;\n\n outputContracts.set(definition, contract);\n\n return contract;\n};\n\nconst sameSystemMessage = Schema.toEquivalence(Prompt.SystemMessage);\n\n/**\n * Keep supported system instructions in conversation order so changing late guidance\n * cannot invalidate the preceding user/tool cache prefix. Omit an exact repeat\n * only when no distinct system instruction intervened; returning to an earlier\n * instruction after a different one must preserve the new directive's precedence.\n * Anchor the immutable output contract after the initial system block.\n * Restore leading static instructions for caller-supplied conversation-only history.\n *\n * This runs after preparation and compaction. Canonical messages, protected\n * instruction/input spans and compaction coverage retain their original positions.\n * Adapters without this capability retain the grouped-system projection: keep the\n * last equivalent instruction with its native options, then the contract and\n * conversation. This prevents older Anthropic adapters from discarding system groups.\n */\nexport const prepareModelPrompt = (\n prompt: Prompt.Prompt,\n contract: Prompt.SystemMessage | undefined,\n systemMessagesInHistory: boolean,\n staticInstructions: Prompt.RawInput | undefined,\n): Prompt.Prompt => {\n if (systemMessagesInHistory) {\n const content: Array<Prompt.Message> = [];\n let lastSystem: Prompt.SystemMessage | undefined;\n let ordered = prompt.content;\n\n if (staticInstructions !== undefined) {\n const firstConversation = prompt.content.findIndex((message) => message.role !== \"system\");\n\n const leading = prompt.content.slice(\n 0,\n firstConversation === -1 ? prompt.content.length : firstConversation,\n );\n\n const restored: Array<Prompt.Message> = [];\n\n const instructions =\n typeof staticInstructions === \"string\"\n ? [Prompt.systemMessage({ content: staticInstructions })]\n : Prompt.make(staticInstructions).content;\n\n for (const message of instructions) {\n if (message.role !== \"system\") break;\n if (\n leading.some(\n (candidate) => candidate.role === \"system\" && sameSystemMessage(candidate, message),\n )\n )\n break;\n\n const original = prompt.content.find(\n (candidate) => candidate.role === \"system\" && sameSystemMessage(candidate, message),\n );\n\n if (original === undefined) break;\n restored.push(original);\n }\n if (restored.length > 0)\n ordered = [...leading, ...restored, ...prompt.content.slice(leading.length)];\n }\n\n for (const message of ordered) {\n if (message.role === \"system\") {\n if (lastSystem !== undefined && sameSystemMessage(lastSystem, message)) continue;\n lastSystem = message;\n }\n content.push(message);\n }\n if (contract !== undefined) {\n const firstConversation = content.findIndex((message) => message.role !== \"system\");\n\n content.splice(firstConversation === -1 ? content.length : firstConversation, 0, contract);\n }\n\n return Prompt.fromMessages(content);\n }\n\n const systems: Array<Prompt.SystemMessage> = [];\n const conversation: Array<Prompt.Message> = [];\n const seen = new Map<string, Array<Prompt.SystemMessage>>();\n\n for (let index = prompt.content.length - 1; index >= 0; index -= 1) {\n const message = prompt.content[index];\n\n if (message === undefined) continue;\n if (message.role !== \"system\") {\n conversation.push(message);\n continue;\n }\n const variants = seen.get(message.content);\n\n if (variants?.some((previous) => sameSystemMessage(previous, message))) continue;\n if (variants === undefined) seen.set(message.content, [message]);\n else variants.push(message);\n systems.push(message);\n }\n\n return Prompt.fromMessages([\n ...systems.reverse(),\n ...(contract === undefined ? [] : [contract]),\n ...conversation.reverse(),\n ]);\n};\n"],"mappings":";;;AAKA,MAAM,yBAAyB;;;;;;;;;;;;;AAc/B,MAAa,cACX,WACiB,OAAO,SAAS,GAAG,yBAAyB,OAAO,CAAC;AAEvE,MAAa,gBAAgB,WAC3B,OAAO,mBAAmB,MAAM,CAAC,GAAG,4BAA4B;AAoClE,MAAM,qBAAqB,eACzB,WAAW,eAAe,KAAA,IACtB,kNAGA,yEAAyE,WAAW,WAAW,KAAK,mMAErD,WAAW,WAAW,KAAK;AAEhF,MAAM,+BAA+B,MAAc,oBACjD,0BAA0B,kBAAkB,uBAAuB,gBAAgB,2CAA2C,KAAK,UAAU,IAAI,EAAE;;;;;;;;;AAYrJ,MAAM,YAAY,aAAqC;CACrD,MAAM;CACN;CACA,MAAM,OAAO,YAAY,UAAU,EAAE,SAAS,QAAQ,CAAC;AACzD;AAEA,MAAM,8BAA8B,eAAoD;CACtF,IAAI,WAAW,YAAY,aAAa,MACtC,OAAO,SACL,4BACE,WAAW,WAAW,OACrB,WAAW,qBAAqB,UAAU,KAAK,CAClD,CACF;CAEF,IAAI,aAAa,WAAW,MAAM,GAChC,OAAO,SACL,+LAEG,WAAW,eAAe,KAAA,IACvB,KACA,sBAAsB,WAAW,WAAW,KAAK,qRACzD;CAEF,IAAI;EACF,MAAM,aAAa,KAAK,wBAAwB,WAAW,MAAM;EAEjE,OAAO,SACL,GAAG,kBAAkB,UAAU,EAAE,MAAM,KAAK,UAAU,YAAY,KAAA,GAAW,CAAC,GAChF;CACF,SAAS,OAAO;EACd,OAAO;GACL,MAAM;GACN,QAAQ,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;EAC/D;CACF;AACF;AAIA,MAAM,kCAAkB,IAAI,QAA6C;AAEzE,MAAa,wBAAwB,eAAoD;CACvF,MAAM,SAAS,gBAAgB,IAAI,UAAU;CAE7C,IAAI,WAAW,KAAA,GAAW,OAAO;CACjC,MAAM,OAAO,2BAA2B,UAAU;CAClD,MAAM,eAAe,WAAW,uBAAuB,CAAC;CAExD,MAAM,WACJ,KAAK,SAAS,cAAc,aAAa,SAAS,IAC9C,SACE,2FAA2F,aAAa,KAAK,gBAAgB,KAAK,UAAU,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE;;IAGxK,KAAK,OACT,IACA;CAEN,gBAAgB,IAAI,YAAY,QAAQ;CAExC,OAAO;AACT;AAEA,MAAM,oBAAoB,OAAO,cAAc,OAAO,aAAa;;;;;;;;;;;;;;;AAgBnE,MAAa,sBACX,QACA,UACA,yBACA,uBACkB;CAClB,IAAI,yBAAyB;EAC3B,MAAM,UAAiC,CAAC;EACxC,IAAI;EACJ,IAAI,UAAU,OAAO;EAErB,IAAI,uBAAuB,KAAA,GAAW;GACpC,MAAM,oBAAoB,OAAO,QAAQ,WAAW,YAAY,QAAQ,SAAS,QAAQ;GAEzF,MAAM,UAAU,OAAO,QAAQ,MAC7B,GACA,sBAAsB,KAAK,OAAO,QAAQ,SAAS,iBACrD;GAEA,MAAM,WAAkC,CAAC;GAEzC,MAAM,eACJ,OAAO,uBAAuB,WAC1B,CAAC,OAAO,cAAc,EAAE,SAAS,mBAAmB,CAAC,CAAC,IACtD,OAAO,KAAK,kBAAkB,CAAC,CAAC;GAEtC,KAAK,MAAM,WAAW,cAAc;IAClC,IAAI,QAAQ,SAAS,UAAU;IAC/B,IACE,QAAQ,MACL,cAAc,UAAU,SAAS,YAAY,kBAAkB,WAAW,OAAO,CACpF,GAEA;IAEF,MAAM,WAAW,OAAO,QAAQ,MAC7B,cAAc,UAAU,SAAS,YAAY,kBAAkB,WAAW,OAAO,CACpF;IAEA,IAAI,aAAa,KAAA,GAAW;IAC5B,SAAS,KAAK,QAAQ;GACxB;GACA,IAAI,SAAS,SAAS,GACpB,UAAU;IAAC,GAAG;IAAS,GAAG;IAAU,GAAG,OAAO,QAAQ,MAAM,QAAQ,MAAM;GAAC;EAC/E;EAEA,KAAK,MAAM,WAAW,SAAS;GAC7B,IAAI,QAAQ,SAAS,UAAU;IAC7B,IAAI,eAAe,KAAA,KAAa,kBAAkB,YAAY,OAAO,GAAG;IACxE,aAAa;GACf;GACA,QAAQ,KAAK,OAAO;EACtB;EACA,IAAI,aAAa,KAAA,GAAW;GAC1B,MAAM,oBAAoB,QAAQ,WAAW,YAAY,QAAQ,SAAS,QAAQ;GAElF,QAAQ,OAAO,sBAAsB,KAAK,QAAQ,SAAS,mBAAmB,GAAG,QAAQ;EAC3F;EAEA,OAAO,OAAO,aAAa,OAAO;CACpC;CAEA,MAAM,UAAuC,CAAC;CAC9C,MAAM,eAAsC,CAAC;CAC7C,MAAM,uBAAO,IAAI,IAAyC;CAE1D,KAAK,IAAI,QAAQ,OAAO,QAAQ,SAAS,GAAG,SAAS,GAAG,SAAS,GAAG;EAClE,MAAM,UAAU,OAAO,QAAQ;EAE/B,IAAI,YAAY,KAAA,GAAW;EAC3B,IAAI,QAAQ,SAAS,UAAU;GAC7B,aAAa,KAAK,OAAO;GACzB;EACF;EACA,MAAM,WAAW,KAAK,IAAI,QAAQ,OAAO;EAEzC,IAAI,UAAU,MAAM,aAAa,kBAAkB,UAAU,OAAO,CAAC,GAAG;EACxE,IAAI,aAAa,KAAA,GAAW,KAAK,IAAI,QAAQ,SAAS,CAAC,OAAO,CAAC;OAC1D,SAAS,KAAK,OAAO;EAC1B,QAAQ,KAAK,OAAO;CACtB;CAEA,OAAO,OAAO,aAAa;EACzB,GAAG,QAAQ,QAAQ;EACnB,GAAI,aAAa,KAAA,IAAY,CAAC,IAAI,CAAC,QAAQ;EAC3C,GAAG,aAAa,QAAQ;CAC1B,CAAC;AACH"}
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"effect-agent","version":"0.1.0-beta.144","devDependencies":{"@effect/platform-node":"4.0.0-rc.117","@effect/vitest":"4.0.0-rc.117","effect":"4.0.0-rc.117","typescript":"7.0.2","vite-plus":"0.3.3"},"peerDependencies":{"effect":"^4.0.0-rc.117"},"exports":{".":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"./activity-store":{"types":"./dist/durable/ActivityStore.d.mts","default":"./dist/durable/ActivityStore.mjs"},"./admin":{"types":"./dist/durable/Admin.d.mts","default":"./dist/durable/Admin.mjs"},"./agent":{"types":"./dist/core/Agent.d.mts","default":"./dist/core/Agent.mjs"},"./agent-error":{"types":"./dist/core/AgentError.d.mts","default":"./dist/core/AgentError.mjs"},"./agent-policy":{"types":"./dist/core/AgentPolicy.d.mts","default":"./dist/core/AgentPolicy.mjs"},"./agent-registration":{"types":"./dist/durable/AgentRegistration.d.mts","default":"./dist/durable/AgentRegistration.mjs"},"./agent-runtime":{"types":"./dist/engine/AgentRuntime.d.mts","default":"./dist/engine/AgentRuntime.mjs"},"./agent-updates":{"types":"./dist/core/AgentUpdates.d.mts","default":"./dist/core/AgentUpdates.mjs"},"./approval":{"types":"./dist/capabilities/Approval.d.mts","default":"./dist/capabilities/Approval.mjs"},"./budget":{"types":"./dist/capabilities/Budget.d.mts","default":"./dist/capabilities/Budget.mjs"},"./code-executor":{"types":"./dist/sandbox/CodeExecutor.d.mts","default":"./dist/sandbox/CodeExecutor.mjs"},"./code-mode":{"types":"./dist/capabilities/CodeMode.d.mts","default":"./dist/capabilities/CodeMode.mjs"},"./commands":{"types":"./dist/capabilities/Commands.d.mts","default":"./dist/capabilities/Commands.mjs"},"./committed-activity":{"types":"./dist/durable/CommittedActivity.d.mts","default":"./dist/durable/CommittedActivity.mjs"},"./compaction":{"types":"./dist/engine/Compaction.d.mts","default":"./dist/engine/Compaction.mjs"},"./context-compactor":{"types":"./dist/engine/ContextCompactor.d.mts","default":"./dist/engine/ContextCompactor.mjs"},"./context-history":{"types":"./dist/engine/ContextHistory.d.mts","default":"./dist/engine/ContextHistory.mjs"},"./context-tools":{"types":"./dist/capabilities/ContextTools.d.mts","default":"./dist/capabilities/ContextTools.mjs"},"./context-window":{"types":"./dist/engine/ContextWindow.d.mts","default":"./dist/engine/ContextWindow.mjs"},"./digest":{"types":"./dist/durable/Digest.d.mts","default":"./dist/durable/Digest.mjs"},"./durable-agent-runtime":{"types":"./dist/durable/DurableAgentRuntime.d.mts","default":"./dist/durable/DurableAgentRuntime.mjs"},"./durable-failpoint":{"types":"./dist/durable/DurableFailpoint.d.mts","default":"./dist/durable/DurableFailpoint.mjs"},"./durable-step":{"types":"./dist/engine/DurableStep.d.mts","default":"./dist/engine/DurableStep.mjs"},"./event-source":{"types":"./dist/durable/EventSource.d.mts","default":"./dist/durable/EventSource.mjs"},"./failure-diagnostic":{"types":"./dist/core/FailureDiagnostic.d.mts","default":"./dist/core/FailureDiagnostic.mjs"},"./git-hub-workflow-source":{"types":"./dist/durable/GitHubWorkflowSource.d.mts","default":"./dist/durable/GitHubWorkflowSource.mjs"},"./id-generator":{"types":"./dist/core/IdGenerator.d.mts","default":"./dist/core/IdGenerator.mjs"},"./identifiers":{"types":"./dist/core/Identifiers.d.mts","default":"./dist/core/Identifiers.mjs"},"./in-memory":{"types":"./dist/InMemory.d.mts","default":"./dist/InMemory.mjs"},"./interactive-browser":{"types":"./dist/sandbox/InteractiveBrowser.d.mts","default":"./dist/sandbox/InteractiveBrowser.mjs"},"./mcp":{"types":"./dist/capabilities/Mcp.d.mts","default":"./dist/capabilities/Mcp.mjs"},"./mcp-client":{"types":"./dist/capabilities/McpClient.d.mts","default":"./dist/capabilities/McpClient.mjs"},"./memory":{"types":"./dist/core/Memory.d.mts","default":"./dist/core/Memory.mjs"},"./memory-namespace":{"types":"./dist/core/MemoryNamespace.d.mts","default":"./dist/core/MemoryNamespace.mjs"},"./memory-notes":{"types":"./dist/capabilities/MemoryNotes.d.mts","default":"./dist/capabilities/MemoryNotes.mjs"},"./memory-reference":{"types":"./dist/core/MemoryReference.d.mts","default":"./dist/core/MemoryReference.mjs"},"./memory-revalidation":{"types":"./dist/core/MemoryRevalidation.d.mts","default":"./dist/core/MemoryRevalidation.mjs"},"./memory-store":{"types":"./dist/core/MemoryStore.d.mts","default":"./dist/core/MemoryStore.mjs"},"./message-delivery":{"types":"./dist/durable/MessageDelivery.d.mts","default":"./dist/durable/MessageDelivery.mjs"},"./messaging":{"types":"./dist/capabilities/Messaging.d.mts","default":"./dist/capabilities/Messaging.mjs"},"./messaging-host":{"types":"./dist/engine/MessagingHost.d.mts","default":"./dist/engine/MessagingHost.mjs"},"./model-context":{"types":"./dist/capabilities/ModelContext.d.mts","default":"./dist/capabilities/ModelContext.mjs"},"./operation-authorizer":{"types":"./dist/durable/OperationAuthorizer.d.mts","default":"./dist/durable/OperationAuthorizer.mjs"},"./output":{"types":"./dist/engine/Output.d.mts","default":"./dist/engine/Output.mjs"},"./page-capture":{"types":"./dist/sandbox/PageCapture.d.mts","default":"./dist/sandbox/PageCapture.mjs"},"./page-crawl":{"types":"./dist/sandbox/PageCrawl.d.mts","default":"./dist/sandbox/PageCrawl.mjs"},"./page-screenshot":{"types":"./dist/sandbox/PageScreenshot.d.mts","default":"./dist/sandbox/PageScreenshot.mjs"},"./persistent-history":{"types":"./dist/durable/PersistentHistory.d.mts","default":"./dist/durable/PersistentHistory.mjs"},"./prepared-input-admission":{"types":"./dist/durable/PreparedInputAdmission.d.mts","default":"./dist/durable/PreparedInputAdmission.mjs"},"./receipt":{"types":"./dist/core/Receipt.d.mts","default":"./dist/core/Receipt.mjs"},"./records":{"types":"./dist/durable/Records.d.mts","default":"./dist/durable/Records.mjs"},"./recovery":{"types":"./dist/durable/Recovery.d.mts","default":"./dist/durable/Recovery.mjs"},"./redaction":{"types":"./dist/capabilities/Redaction.d.mts","default":"./dist/capabilities/Redaction.mjs"},"./remembering":{"types":"./dist/capabilities/Remembering.d.mts","default":"./dist/capabilities/Remembering.mjs"},"./remembering-store":{"types":"./dist/core/RememberingStore.d.mts","default":"./dist/core/RememberingStore.mjs"},"./run-event":{"types":"./dist/core/RunEvent.d.mts","default":"./dist/core/RunEvent.mjs"},"./run-event-sink":{"types":"./dist/engine/RunEventSink.d.mts","default":"./dist/engine/RunEventSink.mjs"},"./run-hooks":{"types":"./dist/capabilities/RunHooks.d.mts","default":"./dist/capabilities/RunHooks.mjs"},"./run-journal":{"types":"./dist/durable/RunJournal.d.mts","default":"./dist/durable/RunJournal.mjs"},"./run-options":{"types":"./dist/engine/RunOptions.d.mts","default":"./dist/engine/RunOptions.mjs"},"./run-policy-usage":{"types":"./dist/core/RunPolicyUsage.d.mts","default":"./dist/core/RunPolicyUsage.mjs"},"./sandbox":{"types":"./dist/sandbox/Sandbox.d.mts","default":"./dist/sandbox/Sandbox.mjs"},"./schedule":{"types":"./dist/durable/Schedule.d.mts","default":"./dist/durable/Schedule.mjs"},"./schedule-transition":{"types":"./dist/durable/ScheduleTransition.d.mts","default":"./dist/durable/ScheduleTransition.mjs"},"./scheduling":{"types":"./dist/durable/Scheduling.d.mts","default":"./dist/durable/Scheduling.mjs"},"./semantic-memory":{"types":"./dist/capabilities/SemanticMemory.d.mts","default":"./dist/capabilities/SemanticMemory.mjs"},"./semantic-memory-index":{"types":"./dist/core/SemanticMemoryIndex.d.mts","default":"./dist/core/SemanticMemoryIndex.mjs"},"./semantic-memory-revalidation":{"types":"./dist/core/SemanticMemoryRevalidation.d.mts","default":"./dist/core/SemanticMemoryRevalidation.mjs"},"./sql-memory-store":{"types":"./dist/durable/SqlMemoryStore.d.mts","default":"./dist/durable/SqlMemoryStore.mjs"},"./subagent":{"types":"./dist/capabilities/Subagent.d.mts","default":"./dist/capabilities/Subagent.mjs"},"./subagent-contract":{"types":"./dist/core/SubagentContract.d.mts","default":"./dist/core/SubagentContract.mjs"},"./subagent-host":{"types":"./dist/engine/SubagentHost.d.mts","default":"./dist/engine/SubagentHost.mjs"},"./subagent-reservations":{"types":"./dist/capabilities/SubagentReservations.d.mts","default":"./dist/capabilities/SubagentReservations.mjs"},"./submission-ledger":{"types":"./dist/durable/SubmissionLedger.d.mts","default":"./dist/durable/SubmissionLedger.mjs"},"./submission-status":{"types":"./dist/durable/SubmissionStatus.d.mts","default":"./dist/durable/SubmissionStatus.mjs"},"./subscription":{"types":"./dist/durable/Subscription.d.mts","default":"./dist/durable/Subscription.mjs"},"./subscription-input":{"types":"./dist/durable/SubscriptionInput.d.mts","default":"./dist/durable/SubscriptionInput.mjs"},"./subscription-tools":{"types":"./dist/durable/SubscriptionTools.d.mts","default":"./dist/durable/SubscriptionTools.mjs"},"./subscription-transition":{"types":"./dist/durable/SubscriptionTransition.d.mts","default":"./dist/durable/SubscriptionTransition.mjs"},"./subscriptions":{"types":"./dist/durable/Subscriptions.d.mts","default":"./dist/durable/Subscriptions.mjs"},"./testing/certification":{"types":"./dist/durable/Certification.d.mts","default":"./dist/durable/Certification.mjs"},"./testing/durable-failpoint-test-control":{"types":"./dist/durable/DurableFailpointTestControl.d.mts","default":"./dist/durable/DurableFailpointTestControl.mjs"},"./testing/message-delivery-store-conformance":{"types":"./dist/durable/MessageDeliveryStoreConformance.d.mts","default":"./dist/durable/MessageDeliveryStoreConformance.mjs"},"./testing/schedule-store-conformance":{"types":"./dist/durable/ScheduleStoreConformance.d.mts","default":"./dist/durable/ScheduleStoreConformance.mjs"},"./testing/submission-ledger-conformance":{"types":"./dist/durable/SubmissionLedgerConformance.d.mts","default":"./dist/durable/SubmissionLedgerConformance.mjs"},"./testing/subscription-store-conformance":{"types":"./dist/durable/SubscriptionStoreConformance.d.mts","default":"./dist/durable/SubscriptionStoreConformance.mjs"},"./testing/thread-store-conformance":{"types":"./dist/durable/ThreadStoreConformance.d.mts","default":"./dist/durable/ThreadStoreConformance.mjs"},"./thread":{"types":"./dist/core/Thread.d.mts","default":"./dist/core/Thread.mjs"},"./thread-context-history":{"types":"./dist/durable/ThreadContextHistory.d.mts","default":"./dist/durable/ThreadContextHistory.mjs"},"./thread-context-history-projection":{"types":"./dist/durable/ThreadContextHistoryProjection.d.mts","default":"./dist/durable/ThreadContextHistoryProjection.mjs"},"./thread-history":{"types":"./dist/engine/ThreadHistory.d.mts","default":"./dist/engine/ThreadHistory.mjs"},"./thread-invariants":{"types":"./dist/durable/ThreadInvariants.d.mts","default":"./dist/durable/ThreadInvariants.mjs"},"./thread-projection":{"types":"./dist/durable/ThreadProjection.d.mts","default":"./dist/durable/ThreadProjection.mjs"},"./thread-projection-maintenance":{"types":"./dist/durable/ThreadProjectionMaintenance.d.mts","default":"./dist/durable/ThreadProjectionMaintenance.mjs"},"./thread-store":{"types":"./dist/durable/ThreadStore.d.mts","default":"./dist/durable/ThreadStore.mjs"},"./tool-broker":{"types":"./dist/engine/ToolBroker.d.mts","default":"./dist/engine/ToolBroker.mjs"},"./tool-discovery":{"types":"./dist/capabilities/ToolDiscovery.d.mts","default":"./dist/capabilities/ToolDiscovery.mjs"},"./tool-exposure":{"types":"./dist/ToolExposure.d.mts","default":"./dist/ToolExposure.mjs"},"./tool-reconciler":{"types":"./dist/durable/ToolReconciler.d.mts","default":"./dist/durable/ToolReconciler.mjs"},"./tool-result":{"types":"./dist/core/ToolResult.d.mts","default":"./dist/core/ToolResult.mjs"},"./usage":{"types":"./dist/core/Usage.d.mts","default":"./dist/core/Usage.mjs"},"./wake-scheduler":{"types":"./dist/durable/WakeScheduler.d.mts","default":"./dist/durable/WakeScheduler.mjs"},"./web-capture":{"types":"./dist/capabilities/WebCapture.d.mts","default":"./dist/capabilities/WebCapture.mjs"},"./web-search":{"types":"./dist/capabilities/WebSearch.d.mts","default":"./dist/capabilities/WebSearch.mjs"},"./worker":{"types":"./dist/core/Worker.d.mts","default":"./dist/core/Worker.mjs"},"./worker-host":{"types":"./dist/durable/WorkerHost.d.mts","default":"./dist/durable/WorkerHost.mjs"},"./lifecycle-publication":{"types":"./dist/durable/LifecyclePublication.d.mts","default":"./dist/durable/LifecyclePublication.mjs"}},"description":"Effect-native agents, execution, subagents, and sandbox contracts in one tree-shakeable package.","license":"MIT","repository":{"type":"git","url":"git+https://github.com/danieljvdm/effect-agent.git","directory":"packages/effect-agent"},"files":["dist","src"],"type":"module","sideEffects":[],"publishConfig":{"access":"public"},"scripts":{"build":"vp pack","check":"tsc --noEmit -p tsconfig.json","test":"vp test --passWithNoTests"}}
1
+ {"name":"effect-agent","version":"0.1.0-beta.149","devDependencies":{"@effect/platform-node":"4.0.0-rc.117","@effect/vitest":"4.0.0-rc.117","effect":"4.0.0-rc.117","typescript":"7.0.2","vite-plus":"0.3.3"},"peerDependencies":{"effect":"^4.0.0-rc.117"},"exports":{".":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"./activity-store":{"types":"./dist/durable/ActivityStore.d.mts","default":"./dist/durable/ActivityStore.mjs"},"./admin":{"types":"./dist/durable/Admin.d.mts","default":"./dist/durable/Admin.mjs"},"./agent":{"types":"./dist/core/Agent.d.mts","default":"./dist/core/Agent.mjs"},"./agent-error":{"types":"./dist/core/AgentError.d.mts","default":"./dist/core/AgentError.mjs"},"./agent-policy":{"types":"./dist/core/AgentPolicy.d.mts","default":"./dist/core/AgentPolicy.mjs"},"./agent-registration":{"types":"./dist/durable/AgentRegistration.d.mts","default":"./dist/durable/AgentRegistration.mjs"},"./agent-runtime":{"types":"./dist/engine/AgentRuntime.d.mts","default":"./dist/engine/AgentRuntime.mjs"},"./agent-updates":{"types":"./dist/core/AgentUpdates.d.mts","default":"./dist/core/AgentUpdates.mjs"},"./approval":{"types":"./dist/capabilities/Approval.d.mts","default":"./dist/capabilities/Approval.mjs"},"./budget":{"types":"./dist/capabilities/Budget.d.mts","default":"./dist/capabilities/Budget.mjs"},"./code-executor":{"types":"./dist/sandbox/CodeExecutor.d.mts","default":"./dist/sandbox/CodeExecutor.mjs"},"./code-mode":{"types":"./dist/capabilities/CodeMode.d.mts","default":"./dist/capabilities/CodeMode.mjs"},"./commands":{"types":"./dist/capabilities/Commands.d.mts","default":"./dist/capabilities/Commands.mjs"},"./committed-activity":{"types":"./dist/durable/CommittedActivity.d.mts","default":"./dist/durable/CommittedActivity.mjs"},"./compaction":{"types":"./dist/engine/Compaction.d.mts","default":"./dist/engine/Compaction.mjs"},"./context-compactor":{"types":"./dist/engine/ContextCompactor.d.mts","default":"./dist/engine/ContextCompactor.mjs"},"./context-history":{"types":"./dist/engine/ContextHistory.d.mts","default":"./dist/engine/ContextHistory.mjs"},"./context-tools":{"types":"./dist/capabilities/ContextTools.d.mts","default":"./dist/capabilities/ContextTools.mjs"},"./context-window":{"types":"./dist/engine/ContextWindow.d.mts","default":"./dist/engine/ContextWindow.mjs"},"./digest":{"types":"./dist/durable/Digest.d.mts","default":"./dist/durable/Digest.mjs"},"./durable-agent-runtime":{"types":"./dist/durable/DurableAgentRuntime.d.mts","default":"./dist/durable/DurableAgentRuntime.mjs"},"./durable-failpoint":{"types":"./dist/durable/DurableFailpoint.d.mts","default":"./dist/durable/DurableFailpoint.mjs"},"./durable-step":{"types":"./dist/engine/DurableStep.d.mts","default":"./dist/engine/DurableStep.mjs"},"./event-source":{"types":"./dist/durable/EventSource.d.mts","default":"./dist/durable/EventSource.mjs"},"./failure-diagnostic":{"types":"./dist/core/FailureDiagnostic.d.mts","default":"./dist/core/FailureDiagnostic.mjs"},"./git-hub-workflow-source":{"types":"./dist/durable/GitHubWorkflowSource.d.mts","default":"./dist/durable/GitHubWorkflowSource.mjs"},"./id-generator":{"types":"./dist/core/IdGenerator.d.mts","default":"./dist/core/IdGenerator.mjs"},"./identifiers":{"types":"./dist/core/Identifiers.d.mts","default":"./dist/core/Identifiers.mjs"},"./in-memory":{"types":"./dist/InMemory.d.mts","default":"./dist/InMemory.mjs"},"./interactive-browser":{"types":"./dist/sandbox/InteractiveBrowser.d.mts","default":"./dist/sandbox/InteractiveBrowser.mjs"},"./mcp":{"types":"./dist/capabilities/Mcp.d.mts","default":"./dist/capabilities/Mcp.mjs"},"./mcp-client":{"types":"./dist/capabilities/McpClient.d.mts","default":"./dist/capabilities/McpClient.mjs"},"./memory":{"types":"./dist/core/Memory.d.mts","default":"./dist/core/Memory.mjs"},"./memory-namespace":{"types":"./dist/core/MemoryNamespace.d.mts","default":"./dist/core/MemoryNamespace.mjs"},"./memory-notes":{"types":"./dist/capabilities/MemoryNotes.d.mts","default":"./dist/capabilities/MemoryNotes.mjs"},"./memory-reference":{"types":"./dist/core/MemoryReference.d.mts","default":"./dist/core/MemoryReference.mjs"},"./memory-revalidation":{"types":"./dist/core/MemoryRevalidation.d.mts","default":"./dist/core/MemoryRevalidation.mjs"},"./memory-store":{"types":"./dist/core/MemoryStore.d.mts","default":"./dist/core/MemoryStore.mjs"},"./message-delivery":{"types":"./dist/durable/MessageDelivery.d.mts","default":"./dist/durable/MessageDelivery.mjs"},"./messaging":{"types":"./dist/capabilities/Messaging.d.mts","default":"./dist/capabilities/Messaging.mjs"},"./messaging-host":{"types":"./dist/engine/MessagingHost.d.mts","default":"./dist/engine/MessagingHost.mjs"},"./model-context":{"types":"./dist/capabilities/ModelContext.d.mts","default":"./dist/capabilities/ModelContext.mjs"},"./operation-authorizer":{"types":"./dist/durable/OperationAuthorizer.d.mts","default":"./dist/durable/OperationAuthorizer.mjs"},"./output":{"types":"./dist/engine/Output.d.mts","default":"./dist/engine/Output.mjs"},"./page-capture":{"types":"./dist/sandbox/PageCapture.d.mts","default":"./dist/sandbox/PageCapture.mjs"},"./page-crawl":{"types":"./dist/sandbox/PageCrawl.d.mts","default":"./dist/sandbox/PageCrawl.mjs"},"./page-screenshot":{"types":"./dist/sandbox/PageScreenshot.d.mts","default":"./dist/sandbox/PageScreenshot.mjs"},"./persistent-history":{"types":"./dist/durable/PersistentHistory.d.mts","default":"./dist/durable/PersistentHistory.mjs"},"./prepared-input-admission":{"types":"./dist/durable/PreparedInputAdmission.d.mts","default":"./dist/durable/PreparedInputAdmission.mjs"},"./receipt":{"types":"./dist/core/Receipt.d.mts","default":"./dist/core/Receipt.mjs"},"./records":{"types":"./dist/durable/Records.d.mts","default":"./dist/durable/Records.mjs"},"./recovery":{"types":"./dist/durable/Recovery.d.mts","default":"./dist/durable/Recovery.mjs"},"./redaction":{"types":"./dist/capabilities/Redaction.d.mts","default":"./dist/capabilities/Redaction.mjs"},"./remembering":{"types":"./dist/capabilities/Remembering.d.mts","default":"./dist/capabilities/Remembering.mjs"},"./remembering-store":{"types":"./dist/core/RememberingStore.d.mts","default":"./dist/core/RememberingStore.mjs"},"./run-event":{"types":"./dist/core/RunEvent.d.mts","default":"./dist/core/RunEvent.mjs"},"./run-event-sink":{"types":"./dist/engine/RunEventSink.d.mts","default":"./dist/engine/RunEventSink.mjs"},"./run-hooks":{"types":"./dist/capabilities/RunHooks.d.mts","default":"./dist/capabilities/RunHooks.mjs"},"./run-journal":{"types":"./dist/durable/RunJournal.d.mts","default":"./dist/durable/RunJournal.mjs"},"./run-options":{"types":"./dist/engine/RunOptions.d.mts","default":"./dist/engine/RunOptions.mjs"},"./run-policy-usage":{"types":"./dist/core/RunPolicyUsage.d.mts","default":"./dist/core/RunPolicyUsage.mjs"},"./sandbox":{"types":"./dist/sandbox/Sandbox.d.mts","default":"./dist/sandbox/Sandbox.mjs"},"./schedule":{"types":"./dist/durable/Schedule.d.mts","default":"./dist/durable/Schedule.mjs"},"./schedule-transition":{"types":"./dist/durable/ScheduleTransition.d.mts","default":"./dist/durable/ScheduleTransition.mjs"},"./scheduling":{"types":"./dist/durable/Scheduling.d.mts","default":"./dist/durable/Scheduling.mjs"},"./semantic-memory":{"types":"./dist/capabilities/SemanticMemory.d.mts","default":"./dist/capabilities/SemanticMemory.mjs"},"./semantic-memory-index":{"types":"./dist/core/SemanticMemoryIndex.d.mts","default":"./dist/core/SemanticMemoryIndex.mjs"},"./semantic-memory-revalidation":{"types":"./dist/core/SemanticMemoryRevalidation.d.mts","default":"./dist/core/SemanticMemoryRevalidation.mjs"},"./sql-memory-store":{"types":"./dist/durable/SqlMemoryStore.d.mts","default":"./dist/durable/SqlMemoryStore.mjs"},"./subagent":{"types":"./dist/capabilities/Subagent.d.mts","default":"./dist/capabilities/Subagent.mjs"},"./subagent-contract":{"types":"./dist/core/SubagentContract.d.mts","default":"./dist/core/SubagentContract.mjs"},"./subagent-host":{"types":"./dist/engine/SubagentHost.d.mts","default":"./dist/engine/SubagentHost.mjs"},"./subagent-reservations":{"types":"./dist/capabilities/SubagentReservations.d.mts","default":"./dist/capabilities/SubagentReservations.mjs"},"./submission-ledger":{"types":"./dist/durable/SubmissionLedger.d.mts","default":"./dist/durable/SubmissionLedger.mjs"},"./submission-status":{"types":"./dist/durable/SubmissionStatus.d.mts","default":"./dist/durable/SubmissionStatus.mjs"},"./subscription":{"types":"./dist/durable/Subscription.d.mts","default":"./dist/durable/Subscription.mjs"},"./subscription-input":{"types":"./dist/durable/SubscriptionInput.d.mts","default":"./dist/durable/SubscriptionInput.mjs"},"./subscription-tools":{"types":"./dist/durable/SubscriptionTools.d.mts","default":"./dist/durable/SubscriptionTools.mjs"},"./subscription-transition":{"types":"./dist/durable/SubscriptionTransition.d.mts","default":"./dist/durable/SubscriptionTransition.mjs"},"./subscriptions":{"types":"./dist/durable/Subscriptions.d.mts","default":"./dist/durable/Subscriptions.mjs"},"./testing/certification":{"types":"./dist/durable/Certification.d.mts","default":"./dist/durable/Certification.mjs"},"./testing/durable-failpoint-test-control":{"types":"./dist/durable/DurableFailpointTestControl.d.mts","default":"./dist/durable/DurableFailpointTestControl.mjs"},"./testing/message-delivery-store-conformance":{"types":"./dist/durable/MessageDeliveryStoreConformance.d.mts","default":"./dist/durable/MessageDeliveryStoreConformance.mjs"},"./testing/schedule-store-conformance":{"types":"./dist/durable/ScheduleStoreConformance.d.mts","default":"./dist/durable/ScheduleStoreConformance.mjs"},"./testing/submission-ledger-conformance":{"types":"./dist/durable/SubmissionLedgerConformance.d.mts","default":"./dist/durable/SubmissionLedgerConformance.mjs"},"./testing/subscription-store-conformance":{"types":"./dist/durable/SubscriptionStoreConformance.d.mts","default":"./dist/durable/SubscriptionStoreConformance.mjs"},"./testing/thread-store-conformance":{"types":"./dist/durable/ThreadStoreConformance.d.mts","default":"./dist/durable/ThreadStoreConformance.mjs"},"./thread":{"types":"./dist/core/Thread.d.mts","default":"./dist/core/Thread.mjs"},"./thread-context-history":{"types":"./dist/durable/ThreadContextHistory.d.mts","default":"./dist/durable/ThreadContextHistory.mjs"},"./thread-context-history-projection":{"types":"./dist/durable/ThreadContextHistoryProjection.d.mts","default":"./dist/durable/ThreadContextHistoryProjection.mjs"},"./thread-history":{"types":"./dist/engine/ThreadHistory.d.mts","default":"./dist/engine/ThreadHistory.mjs"},"./thread-invariants":{"types":"./dist/durable/ThreadInvariants.d.mts","default":"./dist/durable/ThreadInvariants.mjs"},"./thread-projection":{"types":"./dist/durable/ThreadProjection.d.mts","default":"./dist/durable/ThreadProjection.mjs"},"./thread-projection-maintenance":{"types":"./dist/durable/ThreadProjectionMaintenance.d.mts","default":"./dist/durable/ThreadProjectionMaintenance.mjs"},"./thread-store":{"types":"./dist/durable/ThreadStore.d.mts","default":"./dist/durable/ThreadStore.mjs"},"./tool-broker":{"types":"./dist/engine/ToolBroker.d.mts","default":"./dist/engine/ToolBroker.mjs"},"./tool-discovery":{"types":"./dist/capabilities/ToolDiscovery.d.mts","default":"./dist/capabilities/ToolDiscovery.mjs"},"./tool-exposure":{"types":"./dist/ToolExposure.d.mts","default":"./dist/ToolExposure.mjs"},"./tool-reconciler":{"types":"./dist/durable/ToolReconciler.d.mts","default":"./dist/durable/ToolReconciler.mjs"},"./tool-result":{"types":"./dist/core/ToolResult.d.mts","default":"./dist/core/ToolResult.mjs"},"./usage":{"types":"./dist/core/Usage.d.mts","default":"./dist/core/Usage.mjs"},"./wake-scheduler":{"types":"./dist/durable/WakeScheduler.d.mts","default":"./dist/durable/WakeScheduler.mjs"},"./web-capture":{"types":"./dist/capabilities/WebCapture.d.mts","default":"./dist/capabilities/WebCapture.mjs"},"./web-search":{"types":"./dist/capabilities/WebSearch.d.mts","default":"./dist/capabilities/WebSearch.mjs"},"./worker":{"types":"./dist/core/Worker.d.mts","default":"./dist/core/Worker.mjs"},"./worker-host":{"types":"./dist/durable/WorkerHost.d.mts","default":"./dist/durable/WorkerHost.mjs"},"./lifecycle-publication":{"types":"./dist/durable/LifecyclePublication.d.mts","default":"./dist/durable/LifecyclePublication.mjs"}},"description":"Effect-native agents, execution, subagents, and sandbox contracts in one tree-shakeable package.","license":"MIT","repository":{"type":"git","url":"git+https://github.com/danieljvdm/effect-agent.git","directory":"packages/effect-agent"},"files":["dist","src"],"type":"module","sideEffects":[],"publishConfig":{"access":"public"},"scripts":{"build":"vp pack","check":"tsc --noEmit -p tsconfig.json","test":"vp test --passWithNoTests"}}
package/src/core/Agent.ts CHANGED
@@ -152,8 +152,12 @@ export interface Definition<
152
152
  /** Canonical schema used to decode the final model output. */
153
153
  readonly output: OutputSchema;
154
154
  /**
155
- * Evaluated once while preparing each Run. Outgoing system messages form a leading block;
156
- * exact repeats (including provider options) keep their last occurrence. Stored history is unchanged.
155
+ * Evaluated once while preparing each Run. OpenAI, xAI and native adapters advertising support
156
+ * preserve chronological system messages, omitting exact repeats only when no distinct system
157
+ * instruction intervenes. The actual selected model determines support on each call. Other
158
+ * adapters group systems first and keep the last exact repeat, including provider options;
159
+ * Anthropic requires an upstream adapter with mid-conversation system-message support.
160
+ * Stored history is unchanged.
157
161
  */
158
162
  readonly instructions: Instructions;
159
163
  /** Optional projection from decoded input to model-visible native Effect AI prompt content. */
@@ -1266,20 +1266,6 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
1266
1266
  const submissionScheduling = yield* SubmissionScheduling;
1267
1267
  const store = yield* ThreadStore;
1268
1268
 
1269
- const publicationPending = (threadId: ThreadId) =>
1270
- store.lifecyclePublications === undefined
1271
- ? Effect.succeed(false)
1272
- : store.lifecyclePublications.pendingDeadlineFor(threadId).pipe(
1273
- Effect.map(Option.isSome),
1274
- Effect.mapError((cause) =>
1275
- ThreadStoreError.make({
1276
- operation: "read lifecycle publication gate",
1277
- message: "Native lifecycle publication state is unavailable",
1278
- cause,
1279
- }),
1280
- ),
1281
- );
1282
-
1283
1269
  const wake = yield* WakeScheduler;
1284
1270
  const failpoint = yield* DurableRuntimeFailpoint;
1285
1271
  const config = yield* DurableRuntimeConfig;
@@ -4625,7 +4611,6 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
4625
4611
  Effect.gen(function* () {
4626
4612
  const submissionId = submission.submissionId;
4627
4613
 
4628
- if (yield* publicationPending(submission.threadId)) return { _tag: "yielded" as const };
4629
4614
  const runId = runIdForSubmission(submissionId);
4630
4615
  const boundaries: Array<JournalBoundary> = [];
4631
4616
 
@@ -5532,17 +5517,20 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
5532
5517
  // Current instructions govern the continuation; the original user intent, steering and
5533
5518
  // committed history survive. The pending Turn re-enters through the batch continuation,
5534
5519
  // without duplicating the engine's freshly rendered input.
5520
+ // Retained immediate history can contain system messages. Its prefix stays untouched;
5521
+ // only this Run's instruction slots receive the freshly evaluated instructions.
5535
5522
  const instructionView = (
5536
5523
  messages: ReadonlyArray<Prompt.Message>,
5537
5524
  instructions: ReadonlyArray<Prompt.Message>,
5538
5525
  insertWhenAbsent: boolean,
5526
+ priorRunPrefixLength: number,
5539
5527
  ) => {
5540
5528
  const projected: Array<Prompt.Message> = [];
5541
5529
  const lengths = [0];
5542
5530
  let inserted = false;
5543
5531
 
5544
- for (const message of messages) {
5545
- if (message.role === "system") {
5532
+ for (const [index, message] of messages.entries()) {
5533
+ if (index >= priorRunPrefixLength && message.role === "system") {
5546
5534
  if (!inserted) projected.push(...instructions);
5547
5535
  inserted = true;
5548
5536
  } else projected.push(message);
@@ -5556,23 +5544,31 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
5556
5544
  };
5557
5545
  };
5558
5546
 
5559
- const resumeContext: RunContextHook<never, never> = {
5560
- prepare: ({ source }) =>
5547
+ const resumeContext = {
5548
+ prepare: ({ source }: { readonly source: Prompt.Prompt }) =>
5561
5549
  Ref.get(stateRef).pipe(
5562
- Effect.map((state) => ({
5563
- prompt: Prompt.fromMessages([
5564
- ...instructionView(
5565
- resumeProjection.prompt.content,
5566
- source.content
5567
- .slice(0, state.baseLen)
5568
- .filter((message) => message.role === "system"),
5569
- true,
5570
- ).messages,
5571
- ...source.content.slice(state.baseLen ?? source.content.length),
5572
- ]),
5573
- })),
5550
+ Effect.map((state) => {
5551
+ const view = instructionView(
5552
+ resumeProjection.prompt.content,
5553
+ source.content
5554
+ .slice(resumeProjection.historyBefore.content.length, state.baseLen)
5555
+ .filter((message) => message.role === "system"),
5556
+ true,
5557
+ resumeProjection.historyBefore.content.length,
5558
+ );
5559
+
5560
+ return {
5561
+ prompt: Prompt.fromMessages([
5562
+ ...view.messages,
5563
+ ...source.content.slice(state.baseLen ?? source.content.length),
5564
+ ]),
5565
+ priorRunPrefixLength: view.prefixLength(
5566
+ resumeProjection.historyBefore.content.length,
5567
+ ),
5568
+ };
5569
+ }),
5574
5570
  ),
5575
- };
5571
+ } satisfies RunContextHook<never, never>;
5576
5572
 
5577
5573
  const externalContext = runContextPreparation.hook;
5578
5574
 
@@ -5584,13 +5580,17 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
5584
5580
  : {
5585
5581
  prepare: (request) =>
5586
5582
  (journal.committedTurns === 0
5587
- ? Effect.succeed({ prompt: request.source })
5583
+ ? Effect.succeed({
5584
+ prompt: request.source,
5585
+ priorRunPrefixLength: journal.historyBefore.content.length,
5586
+ })
5588
5587
  : resumeContext.prepare(request)
5589
5588
  ).pipe(
5590
- Effect.flatMap(({ prompt }) =>
5589
+ Effect.flatMap(({ prompt, priorRunPrefixLength }) =>
5591
5590
  externalContext.prepare({
5592
5591
  ...request,
5593
5592
  source: prompt,
5593
+ priorRunPrefixLength,
5594
5594
  }),
5595
5595
  ),
5596
5596
  ),
@@ -5885,9 +5885,10 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
5885
5885
  const comparisonView = instructionView(
5886
5886
  sourceJournal.prompt.content,
5887
5887
  state.history?.content
5888
- .slice(0, state.baseLen)
5888
+ .slice(resumeProjection.historyBefore.content.length, state.baseLen)
5889
5889
  .filter((message) => message.role === "system") ?? [],
5890
5890
  false,
5891
+ sourceJournal.historyBefore.content.length,
5891
5892
  );
5892
5893
 
5893
5894
  // Compare each visible message once. A transformed source can authorize only its
@@ -6251,17 +6252,16 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
6251
6252
  return yield* renderInputPrompt(inputPrompt, decodedInput, encodedInput);
6252
6253
  });
6253
6254
 
6254
- const yieldForPublication = Effect.gen(function* () {
6255
- const pending = yield* Effect.exit(publicationPending(submission.threadId));
6256
-
6257
- if (Exit.isSuccess(pending) && !pending.value) return;
6258
- // A completed plain-text response may not yet have reached beforeTurn. Retain it
6259
- // before either a publication wait or its failed state read closes the Attempt.
6260
- yield* recordHalt(commitPendingTurn);
6261
- if (Exit.isFailure(pending)) return yield* recordHalt(Effect.failCause(pending.cause));
6262
- yield* Deferred.succeed(yieldSignal, undefined);
6255
+ // Empty successful drains leave Tool results and RunCompleted in one batch.
6256
+ // A join or interrupted continuation must still retain already returned results.
6257
+ const preserveToolResults = Effect.gen(function* () {
6258
+ const state = yield* Ref.get(stateRef);
6263
6259
 
6264
- return yield* Effect.never;
6260
+ if (
6261
+ state.pendingTurn !== undefined &&
6262
+ knownIds.has(modelResponseRecordId(runId, state.pendingTurn.turn))
6263
+ )
6264
+ yield* commitPendingTurn;
6265
6265
  });
6266
6266
 
6267
6267
  const input: RunInputHook<
@@ -6270,26 +6270,15 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
6270
6270
  > = {
6271
6271
  drain: (policy) =>
6272
6272
  Effect.gen(function* () {
6273
- const state = yield* Ref.get(stateRef);
6274
-
6275
- // A completed Tool batch has returned its outcomes before this steering seam.
6276
- // Persist them before a host ledger read can halt the Attempt and make those
6277
- // ordinary calls appear unknown. A no-tool response still belongs to its later
6278
- // continuation or atomic RunCompleted commit.
6279
- if (
6280
- state.pendingTurn !== undefined &&
6281
- knownIds.has(modelResponseRecordId(runId, state.pendingTurn.turn))
6282
- )
6283
- yield* recordHalt(commitPendingTurn);
6284
-
6285
- yield* yieldForPublication;
6286
-
6287
6273
  const joinedInputs = yield* recordHalt(
6288
6274
  Effect.gen(function* () {
6289
6275
  const limit = policy === "one" ? 1 : MAX_JOIN_DRAIN;
6290
6276
 
6291
- const joinedInputs: Array<Pick<UserInputRecorded, "input" | "messageAdmission">> =
6292
- [];
6277
+ const joinedInputs: Array<
6278
+ Pick<UserInputRecorded, "input" | "messageAdmission"> & {
6279
+ readonly rendered?: Prompt.RawInput;
6280
+ }
6281
+ > = [];
6293
6282
 
6294
6283
  if (joinBacklog === undefined) {
6295
6284
  const hostSnapshot = yield* ledger.loadRecoverySnapshot(
@@ -6331,6 +6320,7 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
6331
6320
  const payload = existing.record.payload;
6332
6321
 
6333
6322
  if (payload._tag !== "UserInputRecorded") continue;
6323
+ yield* preserveToolResults;
6334
6324
  joinedInputs.push(payload);
6335
6325
  }
6336
6326
  if (joinedInputs.length < limit) {
@@ -6362,6 +6352,28 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
6362
6352
  );
6363
6353
  continue;
6364
6354
  }
6355
+
6356
+ const payload = {
6357
+ input: claim.inputPayload,
6358
+ ...(claimSnapshot.submission.messageAdmission === undefined
6359
+ ? {}
6360
+ : { messageAdmission: claimSnapshot.submission.messageAdmission }),
6361
+ };
6362
+
6363
+ const rendered = yield* Effect.result(renderJoinedInput(payload));
6364
+
6365
+ if (Result.isFailure(rendered)) {
6366
+ // The rejecting input gets its own Run and normal typed failure.
6367
+ // Leave it and the unconsumed suffix ready, preserving queue order
6368
+ // and preventing one invalid prompt from failing unrelated receipts.
6369
+ for (const remaining of claims.slice(claims.indexOf(claim))) {
6370
+ yield* ledger.revertJoining(
6371
+ RevertJoiningRequest.make({ submissionId: remaining.submissionId }),
6372
+ );
6373
+ }
6374
+ break;
6375
+ }
6376
+ yield* preserveToolResults;
6365
6377
  const recordId = submissionInputRecordId(claim.submissionId);
6366
6378
  let sequence: CanonicalSequence;
6367
6379
  const existing = joinedInputEnvelopes.get(claim.submissionId);
@@ -6410,24 +6422,23 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
6410
6422
  );
6411
6423
  deliveredJoinInputs.add(claim.submissionId);
6412
6424
  joinedInputs.push({
6413
- input: claim.inputPayload,
6414
- ...(claimSnapshot.submission.messageAdmission === undefined
6415
- ? {}
6416
- : { messageAdmission: claimSnapshot.submission.messageAdmission }),
6425
+ ...payload,
6426
+ rendered: rendered.success,
6417
6427
  });
6418
6428
  }
6419
6429
  }
6420
6430
 
6421
6431
  return joinedInputs;
6422
- }),
6432
+ }).pipe(
6433
+ Effect.onExit((exit) => (Exit.isFailure(exit) ? preserveToolResults : Effect.void)),
6434
+ ),
6423
6435
  );
6424
6436
 
6425
- // Appending a canonical joined input may retain a new publication. Its native
6426
- // receipt remains successful; recovery reattaches it after acknowledgement.
6427
- yield* yieldForPublication;
6428
-
6429
6437
  return yield* Effect.forEach(joinedInputs, (joinedInput) =>
6430
- renderJoinedInput(joinedInput).pipe(
6438
+ (joinedInput.rendered === undefined
6439
+ ? renderJoinedInput(joinedInput)
6440
+ : Effect.succeed(joinedInput.rendered)
6441
+ ).pipe(
6431
6442
  Effect.map((input): RunInputCommand => ({
6432
6443
  kind: "steering",
6433
6444
  input,
@@ -7047,8 +7058,8 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
7047
7058
  history: pending === undefined ? journal.historyBefore : resumeProjection.historyBefore,
7048
7059
  onHistory,
7049
7060
  input,
7050
- // Worker corrections drain together at safe seams, bounded by MAX_JOIN_DRAIN.
7051
- commandDrainPolicy: submission.workerAdmission === undefined ? "one" : "all",
7061
+ // Ready corrections share the next model Turn, bounded by MAX_JOIN_DRAIN.
7062
+ commandDrainPolicy: "all",
7052
7063
  ...(journal.committedTurns > 0 || Schema.is(FrameworkMessage)(submission.messageAdmission)
7053
7064
  ? { retainedInput: submission.inputPayload }
7054
7065
  : {}),
@@ -7108,7 +7119,6 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
7108
7119
  // Preparation may resolve the next Model from canonical Tool results or invoke
7109
7120
  // a compaction Model. Publish the completed Turn before either can observe it.
7110
7121
  yield* recordHalt(commitPendingTurn);
7111
- yield* yieldForPublication;
7112
7122
 
7113
7123
  if (
7114
7124
  yieldAfter !== undefined &&
@@ -7642,6 +7652,7 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
7642
7652
  }),
7643
7653
  ),
7644
7654
  ).pipe(
7655
+ Effect.onExit((exit) => (Exit.hasInterrupts(exit) ? preserveToolResults : Effect.void)),
7645
7656
  // Retain while the Attempt's services, claim renewal and abort watcher are still live.
7646
7657
  // A failed retention halts the coordinator; it is not a failed Tool or a safe suspension.
7647
7658
  Effect.tapCause((cause) =>
@@ -7692,9 +7703,7 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
7692
7703
  const execution = Effect.raceFirst(consume, Effect.raceFirst(abortWatcher, renewal));
7693
7704
 
7694
7705
  const raced = (
7695
- yieldAfter === undefined &&
7696
- submissionScheduling.yieldTo === undefined &&
7697
- store.lifecyclePublications === undefined
7706
+ yieldAfter === undefined && submissionScheduling.yieldTo === undefined
7698
7707
  ? execution
7699
7708
  : Effect.raceFirst(
7700
7709
  execution,
@@ -8572,7 +8581,6 @@ const make = Effect.fn("DurableAgentRuntime.make")(function* (
8572
8581
 
8573
8582
  const attempt = Effect.scoped(
8574
8583
  Effect.gen(function* () {
8575
- if (yield* publicationPending(threadId)) return Option.none();
8576
8584
  const claimed = yield* acquireClaim(threadId, handoff);
8577
8585
 
8578
8586
  if (Option.isNone(claimed)) return Option.none();