@bastani/atomic 0.9.35-alpha.1 → 0.9.35-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/CHANGELOG.md +13 -0
- package/dist/builtin/workflows/builtin/{chunk-s7y3hw0n.js → chunk-3xffzzbg.js} +1 -1
- package/dist/builtin/workflows/builtin/{chunk-0d0pd4fz.js → chunk-7fvrnzhb.js} +1 -1
- package/dist/builtin/workflows/builtin/{chunk-119gt64q.js → chunk-8c3ae7tt.js} +8 -1
- package/dist/builtin/workflows/builtin/{chunk-y68758re.js → chunk-rhkg3d8e.js} +1 -1
- package/dist/builtin/workflows/builtin/goal.js +2 -2
- package/dist/builtin/workflows/builtin/index.js +3 -3
- package/dist/builtin/workflows/builtin/ralph.js +2 -2
- package/dist/builtin/workflows/package.json +1 -1
- package/dist/builtin/workflows/src/extension/index.bundle.mjs +510 -35
- package/dist/builtin/workflows/src/index.js +87 -29
- package/dist/core/agent-session-accessors.d.ts.map +1 -1
- package/dist/core/agent-session-accessors.js +7 -1
- package/dist/core/agent-session-accessors.js.map +1 -1
- package/dist/core/agent-session-custom-message-commit.js +15 -15
- package/dist/core/agent-session-custom-message-commit.js.map +1 -1
- package/dist/core/agent-session-events.d.ts.map +1 -1
- package/dist/core/agent-session-events.js +4 -0
- package/dist/core/agent-session-events.js.map +1 -1
- package/dist/core/agent-session-extension-bindings.d.ts.map +1 -1
- package/dist/core/agent-session-extension-bindings.js +61 -1
- package/dist/core/agent-session-extension-bindings.js.map +1 -1
- package/dist/core/agent-session-message-queue.d.ts.map +1 -1
- package/dist/core/agent-session-message-queue.js +3 -1
- package/dist/core/agent-session-message-queue.js.map +1 -1
- package/dist/core/agent-session-methods.d.ts +5 -3
- package/dist/core/agent-session-methods.d.ts.map +1 -1
- package/dist/core/agent-session-methods.js.map +1 -1
- package/dist/core/agent-session-persistent-custom-messages.js +1 -1
- package/dist/core/agent-session-persistent-custom-messages.js.map +1 -1
- package/dist/core/agent-session-prompt.d.ts +3 -1
- package/dist/core/agent-session-prompt.d.ts.map +1 -1
- package/dist/core/agent-session-prompt.js +79 -9
- package/dist/core/agent-session-prompt.js.map +1 -1
- package/dist/core/agent-session-summary.d.ts.map +1 -1
- package/dist/core/agent-session-summary.js +17 -2
- package/dist/core/agent-session-summary.js.map +1 -1
- package/dist/core/agent-session-tree.js +1 -1
- package/dist/core/agent-session-tree.js.map +1 -1
- package/dist/core/agent-session.d.ts +1 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +1 -0
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage-backends.d.ts +1 -0
- package/dist/core/auth-storage-backends.d.ts.map +1 -1
- package/dist/core/auth-storage-backends.js +37 -23
- package/dist/core/auth-storage-backends.js.map +1 -1
- package/dist/core/compaction/session-summarization.d.ts +1 -0
- package/dist/core/compaction/session-summarization.d.ts.map +1 -1
- package/dist/core/compaction/session-summarization.js +7 -1
- package/dist/core/compaction/session-summarization.js.map +1 -1
- package/dist/core/extensions/agent-events.d.ts +15 -7
- package/dist/core/extensions/agent-events.d.ts.map +1 -1
- package/dist/core/extensions/agent-events.js.map +1 -1
- package/dist/core/extensions/context-types.d.ts +6 -0
- package/dist/core/extensions/context-types.d.ts.map +1 -1
- package/dist/core/extensions/context-types.js.map +1 -1
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/runner-context.d.ts +4 -0
- package/dist/core/extensions/runner-context.d.ts.map +1 -1
- package/dist/core/extensions/runner-context.js +8 -0
- package/dist/core/extensions/runner-context.js.map +1 -1
- package/dist/core/extensions/runner-events.d.ts +1 -1
- package/dist/core/extensions/runner-events.d.ts.map +1 -1
- package/dist/core/extensions/runner-events.js +4 -3
- package/dist/core/extensions/runner-events.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +3 -1
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +8 -2
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/runtime-types.d.ts +4 -0
- package/dist/core/extensions/runtime-types.d.ts.map +1 -1
- package/dist/core/extensions/runtime-types.js.map +1 -1
- package/dist/core/extensions/workflow-run-control.d.ts +12 -2
- package/dist/core/extensions/workflow-run-control.d.ts.map +1 -1
- package/dist/core/extensions/workflow-run-control.js.map +1 -1
- package/dist/core/package-manager-resource-patterns.d.ts.map +1 -1
- package/dist/core/package-manager-resource-patterns.js +4 -2
- package/dist/core/package-manager-resource-patterns.js.map +1 -1
- package/dist/core/session-manager-archive.d.ts +2 -0
- package/dist/core/session-manager-archive.d.ts.map +1 -1
- package/dist/core/session-manager-archive.js +41 -19
- package/dist/core/session-manager-archive.js.map +1 -1
- package/dist/core/session-manager-core.d.ts +1 -0
- package/dist/core/session-manager-core.d.ts.map +1 -1
- package/dist/core/session-manager-core.js +8 -4
- package/dist/core/session-manager-core.js.map +1 -1
- package/dist/core/session-manager-list.d.ts.map +1 -1
- package/dist/core/session-manager-list.js +4 -10
- package/dist/core/session-manager-list.js.map +1 -1
- package/dist/core/session-manager-types.d.ts +2 -1
- package/dist/core/session-manager-types.d.ts.map +1 -1
- package/dist/core/session-manager-types.js.map +1 -1
- package/dist/core/session-manager.d.ts +1 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +3 -1
- package/dist/core/tools/read.js.map +1 -1
- package/dist/core/workflow-summary-lifecycle.d.ts +4 -0
- package/dist/core/workflow-summary-lifecycle.d.ts.map +1 -0
- package/dist/core/workflow-summary-lifecycle.js +40 -0
- package/dist/core/workflow-summary-lifecycle.js.map +1 -0
- package/dist/extensions/codemode/execute.d.ts.map +1 -1
- package/dist/extensions/codemode/execute.js +20 -6
- package/dist/extensions/codemode/execute.js.map +1 -1
- package/dist/extensions/mcp/cli.d.ts.map +1 -1
- package/dist/extensions/mcp/cli.js +4 -7
- package/dist/extensions/mcp/cli.js.map +1 -1
- package/dist/extensions/mcp/client/client.d.ts.map +1 -1
- package/dist/extensions/mcp/client/client.js +5 -4
- package/dist/extensions/mcp/client/client.js.map +1 -1
- package/dist/extensions/mcp/client/index.d.ts +2 -2
- package/dist/extensions/mcp/client/index.d.ts.map +1 -1
- package/dist/extensions/mcp/client/index.js.map +1 -1
- package/dist/extensions/mcp/client/protocol/content.d.ts +4 -1
- package/dist/extensions/mcp/client/protocol/content.d.ts.map +1 -1
- package/dist/extensions/mcp/client/protocol/content.js.map +1 -1
- package/dist/extensions/mcp/client/transports/streamable-http.d.ts +2 -2
- package/dist/extensions/mcp/client/transports/streamable-http.d.ts.map +1 -1
- package/dist/extensions/mcp/client/transports/streamable-http.js +7 -5
- package/dist/extensions/mcp/client/transports/streamable-http.js.map +1 -1
- package/dist/extensions/mcp/client/transports/transport.d.ts +4 -1
- package/dist/extensions/mcp/client/transports/transport.d.ts.map +1 -1
- package/dist/extensions/mcp/client/transports/transport.js.map +1 -1
- package/dist/extensions/mcp/index.d.ts.map +1 -1
- package/dist/extensions/mcp/index.js +2 -4
- package/dist/extensions/mcp/index.js.map +1 -1
- package/dist/extensions/mcp/runtime.d.ts +1 -8
- package/dist/extensions/mcp/runtime.d.ts.map +1 -1
- package/dist/extensions/mcp/runtime.js +2 -41
- package/dist/extensions/mcp/runtime.js.map +1 -1
- package/dist/extensions/mcp/tools.js +1 -1
- package/dist/extensions/mcp/tools.js.map +1 -1
- package/dist/index-extensions.d.ts +1 -1
- package/dist/index-extensions.d.ts.map +1 -1
- package/dist/index-extensions.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/modes/interactive/components/session-selector-list.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector-list.js +2 -1
- package/dist/modes/interactive/components/session-selector-list.js.map +1 -1
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +5 -0
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-client-api.d.ts +7 -2
- package/dist/modes/rpc/rpc-client-api.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client-api.js +13 -0
- package/dist/modes/rpc/rpc-client-api.js.map +1 -1
- package/dist/modes/rpc/rpc-command-handler.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-command-handler.js +19 -0
- package/dist/modes/rpc/rpc-command-handler.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +50 -1
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/docs/extensions/authoring.md +2 -0
- package/docs/extensions/events.md +9 -3
- package/docs/guides/non-interactive.md +2 -0
- package/docs/mcp-servers.md +2 -0
- package/docs/packages/reference.md +2 -0
- package/docs/rpc/protocol.md +15 -0
- package/docs/rpc.md +1 -0
- package/docs/sdk.md +9 -1
- package/docs/sessions.md +2 -2
- package/docs/workflows/operations.md +11 -3
- package/npm-shrinkwrap.json +32 -32
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-session-persistent-custom-messages.js","sourceRoot":"","sources":["../../src/core/agent-session-persistent-custom-messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kCAAkC,EAAE,MAAM,wCAAwC,CAAC;AAG5F,OAAO,EAAE,4BAA4B,EAAE,MAAM,0BAA0B,CAAC;AAMxE,MAAM,CAAC,MAAM,oCAAoC,GAAG,2CAA2C,CAAC;AAYhG,SAAS,iBAAiB,CAAC,OAAqB;IAC/C,OAAO,CAAC,iCAAiC,KAAK,EAAE,CAAC;IACjD,OAAO,OAAO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C,GAAG,OAAO,CAAC,QAAQ,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC9E,KAAK,MAAM,KAAK,IAAI,iBAAiB,CAAC,OAAO,CAAC,EAAE,CAAC;QAChD,IAAI,KAAK,CAAC,KAAK,KAAK,QAAQ,IAAI,iBAAiB,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;YACtE,OAAO,CAAC,kBAAkB,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC3D,CAAC;IACF,CAAC;AACF,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,wCAAwC,CACvD,MAAoB,EACpB,MAAoB,EACpB,WAA+B;IAE/B,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC,QAAQ,EAAE,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC1F,MAAM,QAAQ,GAAsC,EAAE,CAAC;IACvD,MAAM,KAAK,GAAsC,EAAE,CAAC;IACpD,KAAK,MAAM,KAAK,IAAI,iBAAiB,CAAC,MAAM,CAAC,EAAE,CAAC;QAC/C,IAAI,KAAK,CAAC,KAAK,KAAK,QAAQ,IAAI,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;;YACvF,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IACD,MAAM,CAAC,iCAAiC,GAAG,QAAQ,CAAC;IACpD,iBAAiB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC;AAC7C,CAAC","sourcesContent":["import { resolveWorkflowStageDeliveryTarget } from \"./agent-session-delivery-forwarding.ts\";\nimport type { AgentSessionInternalSurface as AgentSession } from \"./agent-session-methods.ts\";\nimport type { DrainedAgentQueues } from \"./agent-session-types.js\";\nimport { customMessageExcludesContext } from \"./agent-session-types.js\";\nimport type { CustomMessage, StageAdmittedCustomMessage } from \"./messages.ts\";\n\ntype ProtectedDelivery = \"steer\" | \"followUp\";\ntype ProtectedPhase = \"queued\" | \"consumed-unpersisted\" | \"persistence-failed\";\n\nexport const PROTECTED_RECONCILIATION_CUSTOM_TYPE = \"atomic:protected-streaming-reconciliation\";\n\ninterface ProtectedReconciliationDetails {\n\tprotectedReconciliationOf: string;\n}\n\nexport interface ProtectedStreamingCustomMessage {\n\tmessage: CustomMessage;\n\tdelivery: ProtectedDelivery;\n\tphase: ProtectedPhase;\n}\n\nfunction protectedMessages(session: AgentSession): ProtectedStreamingCustomMessage[] {\n\tsession._protectedStreamingCustomMessages ??= [];\n\treturn session._protectedStreamingCustomMessages;\n}\n\nfunction appendDurableDisplayCard(\n\tsession: AgentSession,\n\tmessage: CustomMessage,\n\tdelivery: ProtectedDelivery,\n): { readonly card: CustomMessage & { excludeFromContext: true }; readonly intentEntryId: string } {\n\tconst card = { ...message, excludeFromContext: true } satisfies CustomMessage & { excludeFromContext: true };\n\tconst admitted = message as StageAdmittedCustomMessage;\n\t// A retry after a failed flush must complete the already-appended occurrence\n\t// rather than append a second visible card for the same stable key.\n\tconst partial =\n\t\tadmitted.stageAdmissionKey === undefined\n\t\t\t? undefined\n\t\t\t: session.sessionManager\n\t\t\t\t\t.getBranch()\n\t\t\t\t\t.find(\n\t\t\t\t\t\t(entry) =>\n\t\t\t\t\t\t\tentry.type === \"custom_message\" &&\n\t\t\t\t\t\t\tentry.stageAdmissionKey === admitted.stageAdmissionKey &&\n\t\t\t\t\t\t\tprotectedDelivery(entry.protectedReconciliation) !== undefined,\n\t\t\t\t\t);\n\t// The card and recovery marker share one append, so either both survive a\n\t// process exit or neither does.\n\tconst intentEntryId =\n\t\tpartial?.id ??\n\t\tsession.sessionManager.appendCustomMessageEntry(\n\t\t\tcard.customType,\n\t\t\tcard.content,\n\t\t\tcard.display,\n\t\t\tcard.details,\n\t\t\ttrue,\n\t\t\t{ delivery },\n\t\t\tadmitted.stageAdmissionKey,\n\t\t);\n\tif (admitted.stageAdmissionKey !== undefined) session.sessionManager.flush();\n\tsession._entryIdsByMessage.set(card, intentEntryId);\n\tsession._refreshFinalizedContext();\n\treturn { card, intentEntryId };\n}\n\nfunction emitDurableDisplayCard(session: AgentSession, card: CustomMessage): void {\n\t// The card is already live and file-backed, and its protected reconciliation\n\t// is registered. Public listeners remain fallible, but their errors cannot\n\t// revoke admission or make the producer retry this committed occurrence.\n\ttry {\n\t\tsession._emit({ type: \"message_start\", message: card });\n\t} catch {}\n\ttry {\n\t\tsession._emit({ type: \"message_end\", message: card });\n\t} catch {}\n}\n\nfunction createProtectedReconciliation(message: CustomMessage, intentEntryId: string): CustomMessage {\n\treturn {\n\t\trole: \"custom\",\n\t\tcustomType: PROTECTED_RECONCILIATION_CUSTOM_TYPE,\n\t\tcontent: message.content,\n\t\tdisplay: false,\n\t\tdetails: { protectedReconciliationOf: intentEntryId } satisfies ProtectedReconciliationDetails,\n\t\ttimestamp: message.timestamp,\n\t};\n}\n\nfunction completedReconciliationIntent(details: unknown): string | undefined {\n\tif (details === null || typeof details !== \"object\") return undefined;\n\tconst intent = (details as { protectedReconciliationOf?: unknown }).protectedReconciliationOf;\n\treturn typeof intent === \"string\" ? intent : undefined;\n}\n\nfunction protectedDelivery(marker: unknown): ProtectedDelivery | undefined {\n\tif (marker === null || typeof marker !== \"object\") return undefined;\n\tconst delivery = (marker as { delivery?: unknown }).delivery;\n\treturn delivery === \"steer\" || delivery === \"followUp\" ? delivery : undefined;\n}\n\n/** Requeue each durable card intent that has no persisted hidden completion. */\nexport function recoverProtectedStreamingCustomMessages(session: AgentSession): number {\n\tconst branch = session.sessionManager.getBranch();\n\tconst completed = new Set(\n\t\tbranch.flatMap((entry) =>\n\t\t\tentry.type === \"custom_message\" && entry.customType === PROTECTED_RECONCILIATION_CUSTOM_TYPE\n\t\t\t\t? [completedReconciliationIntent(entry.details)].filter((intent): intent is string => intent !== undefined)\n\t\t\t\t: [],\n\t\t),\n\t);\n\tconst scheduled = new Set(\n\t\tprotectedMessages(session).flatMap((entry) => {\n\t\t\tconst intent = completedReconciliationIntent(entry.message.details);\n\t\t\treturn intent === undefined ? [] : [intent];\n\t\t}),\n\t);\n\tlet recovered = 0;\n\tfor (const entry of branch) {\n\t\tif (entry.type !== \"custom_message\") continue;\n\t\tconst delivery = protectedDelivery(entry.protectedReconciliation);\n\t\tif (delivery === undefined || completed.has(entry.id) || scheduled.has(entry.id)) continue;\n\t\tconst parsedTimestamp = Date.parse(entry.timestamp);\n\t\tconst card: CustomMessage = {\n\t\t\trole: \"custom\",\n\t\t\tcustomType: entry.customType,\n\t\t\tcontent: entry.content,\n\t\t\tdisplay: entry.display,\n\t\t\tdetails: entry.details,\n\t\t\ttimestamp: Number.isFinite(parsedTimestamp) ? parsedTimestamp : Date.now(),\n\t\t};\n\t\tconst reconciliation = createProtectedReconciliation(card, entry.id);\n\t\tprotectedMessages(session).push({ message: reconciliation, delivery, phase: \"queued\" });\n\t\tsession._queueAgentMessage(reconciliation, delivery);\n\t\trecovered += 1;\n\t}\n\treturn recovered;\n}\n\ninterface ProtectedAdmission {\n\towner: AgentSession;\n\tcards: CustomMessage[];\n\treconciliations: CustomMessage[];\n}\n\nasync function prepareProtectedAdmission(\n\tsession: AgentSession,\n\tmessages: CustomMessage[],\n\tdelivery: ProtectedDelivery,\n): Promise<ProtectedAdmission> {\n\t// While the initiating workflow tool is still executing, its assistant call\n\t// may be ahead of SessionManager's async event writer. Wait only in that\n\t// protocol-sensitive phase; at ordinary turn boundaries the hidden steer must\n\t// be queued synchronously so agent-core's next native poll observes it.\n\tif (session.agent.state.pendingToolCalls.size > 0) await session._agentEventQueue;\n\tconst owner = resolveWorkflowStageDeliveryTarget(session);\n\tconst prepared = messages.map((message) => {\n\t\tconst { card, intentEntryId } = appendDurableDisplayCard(owner, message, delivery);\n\t\treturn { card, reconciliation: createProtectedReconciliation(message, intentEntryId) };\n\t});\n\tconst cards = prepared.map((entry) => entry.card);\n\tconst reconciliations = prepared.map((entry) => entry.reconciliation);\n\tprotectedMessages(owner).push(\n\t\t...reconciliations.map((message) => ({ message, delivery, phase: \"queued\" as const })),\n\t);\n\treturn { owner, cards, reconciliations };\n}\n\nfunction emitProtectedAdmission(admission: ProtectedAdmission): AgentSession {\n\tlet owner = admission.owner;\n\tfor (const card of admission.cards) {\n\t\towner = resolveWorkflowStageDeliveryTarget(owner);\n\t\temitDurableDisplayCard(owner, card);\n\t}\n\treturn resolveWorkflowStageDeliveryTarget(owner);\n}\n\n/** Persist one visible card and register its hidden model-facing turn. */\nexport async function admitProtectedStreamingCustomMessage(\n\tsession: AgentSession,\n\tmessage: CustomMessage,\n\tdelivery: ProtectedDelivery,\n): Promise<CustomMessage> {\n\tconst admission = await prepareProtectedAdmission(session, [message], delivery);\n\temitProtectedAdmission(admission);\n\treturn admission.reconciliations[0]!;\n}\n\n/** Queue one hidden turn before public listeners can transfer its ownership. */\nexport async function queueProtectedStreamingCustomMessage(\n\tsession: AgentSession,\n\tmessage: CustomMessage,\n\tdelivery: ProtectedDelivery,\n\tpriority = false,\n): Promise<void> {\n\tconst admission = await prepareProtectedAdmission(session, [message], delivery);\n\tadmission.owner._queueAgentMessage(admission.reconciliations[0]!, priority ? \"interrupt\" : delivery);\n\temitProtectedAdmission(admission);\n}\n\n/** Queue a whole hidden batch before public listeners can transfer ownership. */\nexport async function queueProtectedStreamingCustomMessages(\n\tsession: AgentSession,\n\tmessages: CustomMessage[],\n\tdelivery: ProtectedDelivery,\n): Promise<void> {\n\tconst admission = await prepareProtectedAdmission(session, messages, delivery);\n\tfor (const reconciliation of admission.reconciliations) {\n\t\tadmission.owner._queueAgentMessage(reconciliation, delivery);\n\t}\n\temitProtectedAdmission(admission);\n}\n\n/** Start or queue an idle protected batch before exposing its display cards. */\nexport async function triggerProtectedStreamingCustomMessages(\n\tsession: AgentSession,\n\tmessages: CustomMessage[],\n\tdelivery: ProtectedDelivery,\n): Promise<void> {\n\tconst admission = await prepareProtectedAdmission(session, messages, delivery);\n\tfor (const reconciliation of admission.reconciliations) {\n\t\tadmission.owner._queueAgentMessage(reconciliation, delivery);\n\t}\n\tconst owner = emitProtectedAdmission(admission);\n\tif (owner.isStreaming || owner._queuedMessagesPaused) return;\n\tfor (const reconciliation of admission.reconciliations) {\n\t\tremoveQueuedProtectedReference(owner, reconciliation);\n\t}\n\tconst turn = owner._runAgentPrompt(admission.reconciliations);\n\tvoid turn.catch(() => {});\n}\n\n/** Record that agent-core drained and consumed the hidden reconciliation. */\nexport function markProtectedStreamingCustomMessageConsumed(session: AgentSession, message: CustomMessage): boolean {\n\tconst entry = protectedMessages(session).find((candidate) => candidate.message === message);\n\tif (!entry) return false;\n\tentry.phase = \"consumed-unpersisted\";\n\treturn true;\n}\n\nexport function markProtectedStreamingCustomMessagePersistenceFailed(\n\tsession: AgentSession,\n\tmessage: CustomMessage,\n): void {\n\tconst entry = protectedMessages(session).find((candidate) => candidate.message === message);\n\tif (entry?.phase === \"consumed-unpersisted\") entry.phase = \"persistence-failed\";\n}\n\nexport function isProtectedStreamingCustomMessage(session: AgentSession, message: CustomMessage): boolean {\n\treturn protectedMessages(session).some((entry) => entry.message === message);\n}\n\n/**\n * Persist a consumed hidden reconciliation exactly once. Its visible card was\n * already committed at admission, so a transient failure never re-adds a card\n * or re-injects another provider message.\n */\nexport function persistProtectedStreamingCustomMessage(session: AgentSession, message: CustomMessage): boolean {\n\tconst pending = protectedMessages(session);\n\tconst index = pending.findIndex((entry) => entry.message === message);\n\tif (index === -1) return false;\n\tconst entry = pending[index];\n\tconst admitted = message as StageAdmittedCustomMessage;\n\tif (entry.phase !== \"consumed-unpersisted\" && entry.phase !== \"persistence-failed\") return true;\n\tsession.sessionManager.appendCustomMessageEntry(\n\t\tmessage.customType,\n\t\tmessage.content,\n\t\tmessage.display,\n\t\tmessage.details,\n\t\tcustomMessageExcludesContext(message),\n\t\tundefined,\n\t\tadmitted.stageAdmissionKey,\n\t);\n\tpending.splice(index, 1);\n\treturn true;\n}\n\n/** Retry only hidden reconciliation persistence; never queue another alias. */\nexport function retryConsumedProtectedStreamingCustomMessages(session: AgentSession): void {\n\tfor (const entry of [...protectedMessages(session)]) {\n\t\tif (entry.phase !== \"persistence-failed\") continue;\n\t\ttry {\n\t\t\tpersistProtectedStreamingCustomMessage(session, entry.message);\n\t\t} catch {\n\t\t\t// The visible lifecycle card is already durable. Keep this consumed phase\n\t\t\t// for a later event rather than duplicating either the card or model turn.\n\t\t}\n\t}\n}\n\n/** Flush consumed reconciliations before session state can be discarded. */\nexport function flushConsumedProtectedStreamingCustomMessages(session: AgentSession): void {\n\tfor (const entry of [...protectedMessages(session)]) {\n\t\tif (entry.phase === \"queued\") continue;\n\t\t// Do not swallow the final write failure: callers must keep this session\n\t\t// alive rather than discard the only remaining recovery state.\n\t\tpersistProtectedStreamingCustomMessage(session, entry.message);\n\t}\n}\n\nfunction removeReference<T>(items: T[], message: T): void {\n\tlet index = items.indexOf(message);\n\twhile (index >= 0) {\n\t\titems.splice(index, 1);\n\t\tindex = items.indexOf(message);\n\t}\n}\n\nfunction removeQueuedProtectedReference(session: AgentSession, message: CustomMessage): void {\n\tconst hold = session._activeInterruptQueueHold;\n\tif (hold !== undefined) {\n\t\tremoveReference(hold.steering, message);\n\t\tremoveReference(hold.followUp, message);\n\t}\n\tconst queues = session._drainQueuedAgentMessages();\n\tsession._restoreQueuedAgentMessages({\n\t\tsteering: queues.steering.filter((candidate) => candidate !== message),\n\t\tfollowUp: queues.followUp.filter((candidate) => candidate !== message),\n\t});\n}\n\nfunction isPausedHeldProtectedReference(session: AgentSession, message: CustomMessage): boolean {\n\tconst hold = session._activeInterruptQueueHold;\n\treturn (\n\t\tsession._queuedMessagesPaused &&\n\t\thold !== undefined &&\n\t\t(hold.steering.includes(message) || hold.followUp.includes(message))\n\t);\n}\n\n/**\n * Persist only reconciliation input already sealed inside an accepted pause\n * hold. Other queued input can still race an active protocol turn and must keep\n * the historical fail-closed disposal behavior.\n */\nexport function prepareProtectedStreamingCustomMessagesForDisposal(session: AgentSession): void {\n\tconst pending = protectedMessages(session);\n\tconst queued = pending.filter((entry) => entry.phase === \"queued\");\n\tif (queued.some((entry) => !isPausedHeldProtectedReference(session, entry.message))) {\n\t\tthrow new Error(\"Cannot dispose a session with a queued protected reconciliation\");\n\t}\n\tfor (const entry of queued) {\n\t\tconst admitted = entry.message as StageAdmittedCustomMessage;\n\t\tsession.sessionManager.appendCustomMessageEntry(\n\t\t\tentry.message.customType,\n\t\t\tentry.message.content,\n\t\t\tentry.message.display,\n\t\t\tentry.message.details,\n\t\t\tcustomMessageExcludesContext(entry.message),\n\t\t\tundefined,\n\t\t\tadmitted.stageAdmissionKey,\n\t\t);\n\t\tremoveQueuedProtectedReference(session, entry.message);\n\t\tconst index = pending.indexOf(entry);\n\t\tif (index >= 0) pending.splice(index, 1);\n\t}\n\tflushConsumedProtectedStreamingCustomMessages(session);\n}\n\n/** Restore only protected references actually removed from native queues/hold. */\nexport function restoreProtectedStreamingCustomMessages(session: AgentSession, removed: DrainedAgentQueues): void {\n\tconst removedReferences = new Set([...removed.steering, ...removed.followUp]);\n\tfor (const entry of protectedMessages(session)) {\n\t\tif (entry.phase === \"queued\" && removedReferences.has(entry.message)) {\n\t\t\tsession._queueAgentMessage(entry.message, entry.delivery);\n\t\t}\n\t}\n}\n\n/** Move protection only for message references that transfer with native queues. */\nexport function transferProtectedStreamingCustomMessages(\n\tsource: AgentSession,\n\ttarget: AgentSession,\n\ttransferred: DrainedAgentQueues,\n): void {\n\tconst transferredReferences = new Set([...transferred.steering, ...transferred.followUp]);\n\tconst retained: ProtectedStreamingCustomMessage[] = [];\n\tconst moved: ProtectedStreamingCustomMessage[] = [];\n\tfor (const entry of protectedMessages(source)) {\n\t\tif (entry.phase === \"queued\" && transferredReferences.has(entry.message)) moved.push(entry);\n\t\telse retained.push(entry);\n\t}\n\tsource._protectedStreamingCustomMessages = retained;\n\tprotectedMessages(target).unshift(...moved);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"agent-session-persistent-custom-messages.js","sourceRoot":"","sources":["../../src/core/agent-session-persistent-custom-messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kCAAkC,EAAE,MAAM,wCAAwC,CAAC;AAG5F,OAAO,EAAE,4BAA4B,EAAE,MAAM,0BAA0B,CAAC;AAMxE,MAAM,CAAC,MAAM,oCAAoC,GAAG,2CAA2C,CAAC;AAYhG,SAAS,iBAAiB,CAAC,OAAqB;IAC/C,OAAO,CAAC,iCAAiC,KAAK,EAAE,CAAC;IACjD,OAAO,OAAO,CAAC,iCAAiC,CAAC;AAClD,CAAC;AAED,SAAS,wBAAwB,CAChC,OAAqB,EACrB,OAAsB,EACtB,QAA2B;IAE3B,MAAM,IAAI,GAAG,EAAE,GAAG,OAAO,EAAE,kBAAkB,EAAE,IAAI,EAAyD,CAAC;IAC7G,MAAM,QAAQ,GAAG,OAAqC,CAAC;IACvD,6EAA6E;IAC7E,oEAAoE;IACpE,MAAM,OAAO,GACZ,QAAQ,CAAC,iBAAiB,KAAK,SAAS;QACvC,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,OAAO,CAAC,cAAc;aACrB,SAAS,EAAE;aACX,IAAI,CACJ,CAAC,KAAK,EAAE,EAAE,CACT,KAAK,CAAC,IAAI,KAAK,gBAAgB;YAC/B,KAAK,CAAC,iBAAiB,KAAK,QAAQ,CAAC,iBAAiB;YACtD,iBAAiB,CAAC,KAAK,CAAC,uBAAuB,CAAC,KAAK,SAAS,CAC/D,CAAC;IACN,0EAA0E;IAC1E,gCAAgC;IAChC,MAAM,aAAa,GAClB,OAAO,EAAE,EAAE;QACX,OAAO,CAAC,cAAc,CAAC,wBAAwB,CAC9C,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,OAAO,EACZ,IAAI,EACJ,EAAE,QAAQ,EAAE,EACZ,QAAQ,CAAC,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AO,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;;YACvF,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IACD,MAAM,CAAC,iCAAiC,GAAG,QAAQ,CAAC;IACpD,iBAAiB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC;AAC7C,CAAC","sourcesContent":["import { resolveWorkflowStageDeliveryTarget } from \"./agent-session-delivery-forwarding.ts\";\nimport type { AgentSessionInternalSurface as AgentSession } from \"./agent-session-methods.ts\";\nimport type { DrainedAgentQueues } from \"./agent-session-types.js\";\nimport { customMessageExcludesContext } from \"./agent-session-types.js\";\nimport type { CustomMessage, StageAdmittedCustomMessage } from \"./messages.ts\";\n\ntype ProtectedDelivery = \"steer\" | \"followUp\";\ntype ProtectedPhase = \"queued\" | \"consumed-unpersisted\" | \"persistence-failed\";\n\nexport const PROTECTED_RECONCILIATION_CUSTOM_TYPE = \"atomic:protected-streaming-reconciliation\";\n\ninterface ProtectedReconciliationDetails {\n\tprotectedReconciliationOf: string;\n}\n\nexport interface ProtectedStreamingCustomMessage {\n\tmessage: CustomMessage;\n\tdelivery: ProtectedDelivery;\n\tphase: ProtectedPhase;\n}\n\nfunction protectedMessages(session: AgentSession): ProtectedStreamingCustomMessage[] {\n\tsession._protectedStreamingCustomMessages ??= [];\n\treturn session._protectedStreamingCustomMessages;\n}\n\nfunction appendDurableDisplayCard(\n\tsession: AgentSession,\n\tmessage: CustomMessage,\n\tdelivery: ProtectedDelivery,\n): { readonly card: CustomMessage & { excludeFromContext: true }; readonly intentEntryId: string } {\n\tconst card = { ...message, excludeFromContext: true } satisfies CustomMessage & { excludeFromContext: true };\n\tconst admitted = message as StageAdmittedCustomMessage;\n\t// A retry after a failed flush must complete the already-appended occurrence\n\t// rather than append a second visible card for the same stable key.\n\tconst partial =\n\t\tadmitted.stageAdmissionKey === undefined\n\t\t\t? undefined\n\t\t\t: session.sessionManager\n\t\t\t\t\t.getBranch()\n\t\t\t\t\t.find(\n\t\t\t\t\t\t(entry) =>\n\t\t\t\t\t\t\tentry.type === \"custom_message\" &&\n\t\t\t\t\t\t\tentry.stageAdmissionKey === admitted.stageAdmissionKey &&\n\t\t\t\t\t\t\tprotectedDelivery(entry.protectedReconciliation) !== undefined,\n\t\t\t\t\t);\n\t// The card and recovery marker share one append, so either both survive a\n\t// process exit or neither does.\n\tconst intentEntryId =\n\t\tpartial?.id ??\n\t\tsession.sessionManager.appendCustomMessageEntry(\n\t\t\tcard.customType,\n\t\t\tcard.content,\n\t\t\tcard.display,\n\t\t\tcard.details,\n\t\t\ttrue,\n\t\t\t{ delivery },\n\t\t\tadmitted.stageAdmissionKey,\n\t\t);\n\tif (admitted.stageAdmissionKey !== undefined) session.sessionManager.flush();\n\tsession._entryIdsByMessage.set(card, intentEntryId);\n\tsession._refreshFinalizedContext();\n\treturn { card, intentEntryId };\n}\n\nfunction emitDurableDisplayCard(session: AgentSession, card: CustomMessage): void {\n\t// The card is already live and file-backed, and its protected reconciliation\n\t// is registered. Public listeners remain fallible, but their errors cannot\n\t// revoke admission or make the producer retry this committed occurrence.\n\ttry {\n\t\tsession._emit({ type: \"message_start\", message: card });\n\t} catch {}\n\ttry {\n\t\tsession._emit({ type: \"message_end\", message: card });\n\t} catch {}\n}\n\nfunction createProtectedReconciliation(message: CustomMessage, intentEntryId: string): CustomMessage {\n\treturn {\n\t\trole: \"custom\",\n\t\tcustomType: PROTECTED_RECONCILIATION_CUSTOM_TYPE,\n\t\tcontent: message.content,\n\t\tdisplay: false,\n\t\tdetails: { protectedReconciliationOf: intentEntryId } satisfies ProtectedReconciliationDetails,\n\t\ttimestamp: message.timestamp,\n\t};\n}\n\nfunction completedReconciliationIntent(details: unknown): string | undefined {\n\tif (details === null || typeof details !== \"object\") return undefined;\n\tconst intent = (details as { protectedReconciliationOf?: unknown }).protectedReconciliationOf;\n\treturn typeof intent === \"string\" ? intent : undefined;\n}\n\nfunction protectedDelivery(marker: unknown): ProtectedDelivery | undefined {\n\tif (marker === null || typeof marker !== \"object\") return undefined;\n\tconst delivery = (marker as { delivery?: unknown }).delivery;\n\treturn delivery === \"steer\" || delivery === \"followUp\" ? delivery : undefined;\n}\n\n/** Requeue each durable card intent that has no persisted hidden completion. */\nexport function recoverProtectedStreamingCustomMessages(session: AgentSession): number {\n\tconst branch = session.sessionManager.getBranch();\n\tconst completed = new Set(\n\t\tbranch.flatMap((entry) =>\n\t\t\tentry.type === \"custom_message\" && entry.customType === PROTECTED_RECONCILIATION_CUSTOM_TYPE\n\t\t\t\t? [completedReconciliationIntent(entry.details)].filter((intent): intent is string => intent !== undefined)\n\t\t\t\t: [],\n\t\t),\n\t);\n\tconst scheduled = new Set(\n\t\tprotectedMessages(session).flatMap((entry) => {\n\t\t\tconst intent = completedReconciliationIntent(entry.message.details);\n\t\t\treturn intent === undefined ? [] : [intent];\n\t\t}),\n\t);\n\tlet recovered = 0;\n\tfor (const entry of branch) {\n\t\tif (entry.type !== \"custom_message\") continue;\n\t\tconst delivery = protectedDelivery(entry.protectedReconciliation);\n\t\tif (delivery === undefined || completed.has(entry.id) || scheduled.has(entry.id)) continue;\n\t\tconst parsedTimestamp = Date.parse(entry.timestamp);\n\t\tconst card: CustomMessage = {\n\t\t\trole: \"custom\",\n\t\t\tcustomType: entry.customType,\n\t\t\tcontent: entry.content,\n\t\t\tdisplay: entry.display,\n\t\t\tdetails: entry.details,\n\t\t\ttimestamp: Number.isFinite(parsedTimestamp) ? parsedTimestamp : Date.now(),\n\t\t};\n\t\tconst reconciliation = createProtectedReconciliation(card, entry.id);\n\t\tprotectedMessages(session).push({ message: reconciliation, delivery, phase: \"queued\" });\n\t\tsession._queueAgentMessage(reconciliation, delivery);\n\t\trecovered += 1;\n\t}\n\treturn recovered;\n}\n\ninterface ProtectedAdmission {\n\towner: AgentSession;\n\tcards: CustomMessage[];\n\treconciliations: CustomMessage[];\n}\n\nasync function prepareProtectedAdmission(\n\tsession: AgentSession,\n\tmessages: CustomMessage[],\n\tdelivery: ProtectedDelivery,\n): Promise<ProtectedAdmission> {\n\t// While the initiating workflow tool is still executing, its assistant call\n\t// may be ahead of SessionManager's async event writer. Wait only in that\n\t// protocol-sensitive phase; at ordinary turn boundaries the hidden steer must\n\t// be queued synchronously so agent-core's next native poll observes it.\n\tif (session.agent.state.pendingToolCalls.size > 0) await session._agentEventQueue;\n\tconst owner = resolveWorkflowStageDeliveryTarget(session);\n\tconst prepared = messages.map((message) => {\n\t\tconst { card, intentEntryId } = appendDurableDisplayCard(owner, message, delivery);\n\t\treturn { card, reconciliation: createProtectedReconciliation(message, intentEntryId) };\n\t});\n\tconst cards = prepared.map((entry) => entry.card);\n\tconst reconciliations = prepared.map((entry) => entry.reconciliation);\n\tprotectedMessages(owner).push(\n\t\t...reconciliations.map((message) => ({ message, delivery, phase: \"queued\" as const })),\n\t);\n\treturn { owner, cards, reconciliations };\n}\n\nfunction emitProtectedAdmission(admission: ProtectedAdmission): AgentSession {\n\tlet owner = admission.owner;\n\tfor (const card of admission.cards) {\n\t\towner = resolveWorkflowStageDeliveryTarget(owner);\n\t\temitDurableDisplayCard(owner, card);\n\t}\n\treturn resolveWorkflowStageDeliveryTarget(owner);\n}\n\n/** Persist one visible card and register its hidden model-facing turn. */\nexport async function admitProtectedStreamingCustomMessage(\n\tsession: AgentSession,\n\tmessage: CustomMessage,\n\tdelivery: ProtectedDelivery,\n): Promise<CustomMessage> {\n\tconst admission = await prepareProtectedAdmission(session, [message], delivery);\n\temitProtectedAdmission(admission);\n\treturn admission.reconciliations[0]!;\n}\n\n/** Queue one hidden turn before public listeners can transfer its ownership. */\nexport async function queueProtectedStreamingCustomMessage(\n\tsession: AgentSession,\n\tmessage: CustomMessage,\n\tdelivery: ProtectedDelivery,\n\tpriority = false,\n): Promise<void> {\n\tconst admission = await prepareProtectedAdmission(session, [message], delivery);\n\tadmission.owner._queueAgentMessage(admission.reconciliations[0]!, priority ? \"interrupt\" : delivery);\n\temitProtectedAdmission(admission);\n}\n\n/** Queue a whole hidden batch before public listeners can transfer ownership. */\nexport async function queueProtectedStreamingCustomMessages(\n\tsession: AgentSession,\n\tmessages: CustomMessage[],\n\tdelivery: ProtectedDelivery,\n): Promise<void> {\n\tconst admission = await prepareProtectedAdmission(session, messages, delivery);\n\tfor (const reconciliation of admission.reconciliations) {\n\t\tadmission.owner._queueAgentMessage(reconciliation, delivery);\n\t}\n\temitProtectedAdmission(admission);\n}\n\n/** Start or queue an idle protected batch before exposing its display cards. */\nexport async function triggerProtectedStreamingCustomMessages(\n\tsession: AgentSession,\n\tmessages: CustomMessage[],\n\tdelivery: ProtectedDelivery,\n): Promise<void> {\n\tconst admission = await prepareProtectedAdmission(session, messages, delivery);\n\tfor (const reconciliation of admission.reconciliations) {\n\t\tadmission.owner._queueAgentMessage(reconciliation, delivery);\n\t}\n\tconst owner = emitProtectedAdmission(admission);\n\tif (owner.isStreaming || owner._isPreparingRun || owner._queuedMessagesPaused) return;\n\tfor (const reconciliation of admission.reconciliations) {\n\t\tremoveQueuedProtectedReference(owner, reconciliation);\n\t}\n\tconst turn = owner._runAgentPrompt(admission.reconciliations);\n\tvoid turn.catch(() => {});\n}\n\n/** Record that agent-core drained and consumed the hidden reconciliation. */\nexport function markProtectedStreamingCustomMessageConsumed(session: AgentSession, message: CustomMessage): boolean {\n\tconst entry = protectedMessages(session).find((candidate) => candidate.message === message);\n\tif (!entry) return false;\n\tentry.phase = \"consumed-unpersisted\";\n\treturn true;\n}\n\nexport function markProtectedStreamingCustomMessagePersistenceFailed(\n\tsession: AgentSession,\n\tmessage: CustomMessage,\n): void {\n\tconst entry = protectedMessages(session).find((candidate) => candidate.message === message);\n\tif (entry?.phase === \"consumed-unpersisted\") entry.phase = \"persistence-failed\";\n}\n\nexport function isProtectedStreamingCustomMessage(session: AgentSession, message: CustomMessage): boolean {\n\treturn protectedMessages(session).some((entry) => entry.message === message);\n}\n\n/**\n * Persist a consumed hidden reconciliation exactly once. Its visible card was\n * already committed at admission, so a transient failure never re-adds a card\n * or re-injects another provider message.\n */\nexport function persistProtectedStreamingCustomMessage(session: AgentSession, message: CustomMessage): boolean {\n\tconst pending = protectedMessages(session);\n\tconst index = pending.findIndex((entry) => entry.message === message);\n\tif (index === -1) return false;\n\tconst entry = pending[index];\n\tconst admitted = message as StageAdmittedCustomMessage;\n\tif (entry.phase !== \"consumed-unpersisted\" && entry.phase !== \"persistence-failed\") return true;\n\tsession.sessionManager.appendCustomMessageEntry(\n\t\tmessage.customType,\n\t\tmessage.content,\n\t\tmessage.display,\n\t\tmessage.details,\n\t\tcustomMessageExcludesContext(message),\n\t\tundefined,\n\t\tadmitted.stageAdmissionKey,\n\t);\n\tpending.splice(index, 1);\n\treturn true;\n}\n\n/** Retry only hidden reconciliation persistence; never queue another alias. */\nexport function retryConsumedProtectedStreamingCustomMessages(session: AgentSession): void {\n\tfor (const entry of [...protectedMessages(session)]) {\n\t\tif (entry.phase !== \"persistence-failed\") continue;\n\t\ttry {\n\t\t\tpersistProtectedStreamingCustomMessage(session, entry.message);\n\t\t} catch {\n\t\t\t// The visible lifecycle card is already durable. Keep this consumed phase\n\t\t\t// for a later event rather than duplicating either the card or model turn.\n\t\t}\n\t}\n}\n\n/** Flush consumed reconciliations before session state can be discarded. */\nexport function flushConsumedProtectedStreamingCustomMessages(session: AgentSession): void {\n\tfor (const entry of [...protectedMessages(session)]) {\n\t\tif (entry.phase === \"queued\") continue;\n\t\t// Do not swallow the final write failure: callers must keep this session\n\t\t// alive rather than discard the only remaining recovery state.\n\t\tpersistProtectedStreamingCustomMessage(session, entry.message);\n\t}\n}\n\nfunction removeReference<T>(items: T[], message: T): void {\n\tlet index = items.indexOf(message);\n\twhile (index >= 0) {\n\t\titems.splice(index, 1);\n\t\tindex = items.indexOf(message);\n\t}\n}\n\nfunction removeQueuedProtectedReference(session: AgentSession, message: CustomMessage): void {\n\tconst hold = session._activeInterruptQueueHold;\n\tif (hold !== undefined) {\n\t\tremoveReference(hold.steering, message);\n\t\tremoveReference(hold.followUp, message);\n\t}\n\tconst queues = session._drainQueuedAgentMessages();\n\tsession._restoreQueuedAgentMessages({\n\t\tsteering: queues.steering.filter((candidate) => candidate !== message),\n\t\tfollowUp: queues.followUp.filter((candidate) => candidate !== message),\n\t});\n}\n\nfunction isPausedHeldProtectedReference(session: AgentSession, message: CustomMessage): boolean {\n\tconst hold = session._activeInterruptQueueHold;\n\treturn (\n\t\tsession._queuedMessagesPaused &&\n\t\thold !== undefined &&\n\t\t(hold.steering.includes(message) || hold.followUp.includes(message))\n\t);\n}\n\n/**\n * Persist only reconciliation input already sealed inside an accepted pause\n * hold. Other queued input can still race an active protocol turn and must keep\n * the historical fail-closed disposal behavior.\n */\nexport function prepareProtectedStreamingCustomMessagesForDisposal(session: AgentSession): void {\n\tconst pending = protectedMessages(session);\n\tconst queued = pending.filter((entry) => entry.phase === \"queued\");\n\tif (queued.some((entry) => !isPausedHeldProtectedReference(session, entry.message))) {\n\t\tthrow new Error(\"Cannot dispose a session with a queued protected reconciliation\");\n\t}\n\tfor (const entry of queued) {\n\t\tconst admitted = entry.message as StageAdmittedCustomMessage;\n\t\tsession.sessionManager.appendCustomMessageEntry(\n\t\t\tentry.message.customType,\n\t\t\tentry.message.content,\n\t\t\tentry.message.display,\n\t\t\tentry.message.details,\n\t\t\tcustomMessageExcludesContext(entry.message),\n\t\t\tundefined,\n\t\t\tadmitted.stageAdmissionKey,\n\t\t);\n\t\tremoveQueuedProtectedReference(session, entry.message);\n\t\tconst index = pending.indexOf(entry);\n\t\tif (index >= 0) pending.splice(index, 1);\n\t}\n\tflushConsumedProtectedStreamingCustomMessages(session);\n}\n\n/** Restore only protected references actually removed from native queues/hold. */\nexport function restoreProtectedStreamingCustomMessages(session: AgentSession, removed: DrainedAgentQueues): void {\n\tconst removedReferences = new Set([...removed.steering, ...removed.followUp]);\n\tfor (const entry of protectedMessages(session)) {\n\t\tif (entry.phase === \"queued\" && removedReferences.has(entry.message)) {\n\t\t\tsession._queueAgentMessage(entry.message, entry.delivery);\n\t\t}\n\t}\n}\n\n/** Move protection only for message references that transfer with native queues. */\nexport function transferProtectedStreamingCustomMessages(\n\tsource: AgentSession,\n\ttarget: AgentSession,\n\ttransferred: DrainedAgentQueues,\n): void {\n\tconst transferredReferences = new Set([...transferred.steering, ...transferred.followUp]);\n\tconst retained: ProtectedStreamingCustomMessage[] = [];\n\tconst moved: ProtectedStreamingCustomMessage[] = [];\n\tfor (const entry of protectedMessages(source)) {\n\t\tif (entry.phase === \"queued\" && transferredReferences.has(entry.message)) moved.push(entry);\n\t\telse retained.push(entry);\n\t}\n\tsource._protectedStreamingCustomMessages = retained;\n\tprotectedMessages(target).unshift(...moved);\n}\n"]}
|
|
@@ -2,6 +2,8 @@ import type { ImageContent, TextContent } from "@bastani/pi-ai/compat";
|
|
|
2
2
|
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
3
3
|
import type { AgentSessionInternalSurface as AgentSession } from "./agent-session-methods.ts";
|
|
4
4
|
import type { PromptDisposition, PromptOptions, QueuedInputDisposition } from "./agent-session-types.js";
|
|
5
|
+
import type { CustomMessage } from "./messages.ts";
|
|
6
|
+
export declare function waitForCustomMessagePreparation(session: AgentSession): Promise<void>;
|
|
5
7
|
type UserMessageDeliveryAction = "prompt" | "steer" | "followUp" | "handled";
|
|
6
8
|
type PromptOptionsWithWorkflowDelivery = PromptOptions & {
|
|
7
9
|
readonly __workflowDelivery?: {
|
|
@@ -14,7 +16,7 @@ type PromptOptionsWithWorkflowDelivery = PromptOptions & {
|
|
|
14
16
|
/** Dispatch registered extension slash commands without changing the raw queue pause gate. */
|
|
15
17
|
export declare function tryExecuteSessionSlashCommand(session: Pick<AgentSession, "_tryExecuteExtensionCommand">, text: string): Promise<boolean>;
|
|
16
18
|
export declare function prompt(this: AgentSession, text: string, options?: PromptOptions): Promise<void>;
|
|
17
|
-
export declare function _runAgentPrompt(this: AgentSession, messages: AgentMessage | AgentMessage[], promptStarted?: () => void, reportDispatch?: (disposition: PromptDisposition) => void): Promise<void>;
|
|
19
|
+
export declare function _runAgentPrompt(this: AgentSession, messages: AgentMessage | AgentMessage[], promptStarted?: () => void, reportDispatch?: (disposition: PromptDisposition) => void, prepared?: boolean, customStarter?: CustomMessage): Promise<void>;
|
|
18
20
|
export declare function _runAgentContinue(this: AgentSession): Promise<void>;
|
|
19
21
|
export declare function _continueQueuedAgentMessages(this: AgentSession): Promise<void>;
|
|
20
22
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-session-prompt.d.ts","sourceRoot":"","sources":["../../src/core/agent-session-prompt.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAIlE,OAAO,KAAK,EAAE,2BAA2B,IAAI,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC9F,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"agent-session-prompt.d.ts","sourceRoot":"","sources":["../../src/core/agent-session-prompt.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAIlE,OAAO,KAAK,EAAE,2BAA2B,IAAI,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC9F,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAOzG,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAanD,wBAAsB,+BAA+B,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAE1F;AAED,KAAK,yBAAyB,GAAG,QAAQ,GAA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AEH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAiC5E;AA6CD;;;;;;;GAOG;AAEH,wBAAsB,KAAK,CAC1B,IAAI,EAAE,YAAY,EAClB,IAAI,EAAE,MAAM,EACZ,MAAM,CAAC,EAAE,YAAY,EAAE,EACvB,OAAO,CAAC,EAAE,IAAI,CAAC,aAAa,EAAE,QAAQ,CAAC,GACrC,OAAO,CAAC,sBAAsB,CAAC,CAEjC;AAED;;;;;;GAMG;AAEH,wBAAsB,QAAQ,CAC7B,IAAI,EAAE,YAAY,EAClB,IAAI,EAAE,MAAM,EACZ,MAAM,CAAC,EAAE,YAAY,EAAE,EACvB,OAAO,CAAC,EAAE,IAAI,CAAC,aAAa,EAAE,QAAQ,CAAC,GACrC,OAAO,CAAC,sBAAsB,CAAC,CAEjC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,eAAe,CACpC,IAAI,EAAE,YAAY,EAClB,OAAO,EAAE,MAAM,GAAG,CAAC,WAAW,GAAG,YAAY,CAAC,EAAE,EAChD,OAAO,CAAC,EAAE;IACT,SAAS,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC;IACjC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,kBAAkB,CAAC,EAAE,iCAAiC,CAAC,oBAAoB,CAAC,CAAC;CAC7E,GACC,OAAO,CAAC,IAAI,CAAC,CAgCf;AAED;;;;GAIG;AAEH,eAAO,MAAM,yBAAyB;;;;;;;;;;CAUrC,CAAC"}
|
|
@@ -7,6 +7,10 @@ import { runCallback } from "./callback-activity.js";
|
|
|
7
7
|
import { expandPromptTemplate } from "./prompt-templates.js";
|
|
8
8
|
import { assertSessionOpen, sessionGenerationClosing, sessionLifetime, sessionQuitSignal, trackSessionWork, } from "./session-lifecycle-work.js";
|
|
9
9
|
import { getSkillCatalog } from "./skill-catalog.js";
|
|
10
|
+
const customPreparations = new WeakMap();
|
|
11
|
+
export async function waitForCustomMessagePreparation(session) {
|
|
12
|
+
await customPreparations.get(session);
|
|
13
|
+
}
|
|
10
14
|
function assistantHasRecoverableOutput(message) {
|
|
11
15
|
if (message.role !== "assistant")
|
|
12
16
|
return false;
|
|
@@ -94,6 +98,7 @@ async function promptInternal(text, options) {
|
|
|
94
98
|
const preflightResult = options?.preflightResult;
|
|
95
99
|
const workflowDelivery = options?.__workflowDelivery;
|
|
96
100
|
let messages;
|
|
101
|
+
let preparingRun = false;
|
|
97
102
|
try {
|
|
98
103
|
// Authorize workflow delivery before commands or input extensions can perform side effects.
|
|
99
104
|
// A later terminal transition cannot retroactively reject input accepted at this boundary.
|
|
@@ -149,7 +154,7 @@ async function promptInternal(text, options) {
|
|
|
149
154
|
// If streaming, queue via steer() or followUp() based on option. The run is still
|
|
150
155
|
// open while agent_before_settle handlers decide on its continuation, so input sent
|
|
151
156
|
// from that boundary queues for the continuation instead of starting a second run.
|
|
152
|
-
if (this.isStreaming || this._isBeforeSettle) {
|
|
157
|
+
if (this.isStreaming || this._isPreparingRun || this._isBeforeSettle) {
|
|
153
158
|
if (!options?.streamingBehavior) {
|
|
154
159
|
throw new Error("Agent is already processing. Specify streamingBehavior ('steer' or 'followUp') to queue the message.");
|
|
155
160
|
}
|
|
@@ -199,6 +204,8 @@ async function promptInternal(text, options) {
|
|
|
199
204
|
}
|
|
200
205
|
throw new Error(formatNoApiKeyFoundMessage(this.model.provider));
|
|
201
206
|
}
|
|
207
|
+
this._isPreparingRun = "user";
|
|
208
|
+
preparingRun = true;
|
|
202
209
|
// Check if we need to compact before sending (catches aborted responses)
|
|
203
210
|
const lastAssistant = this._findLastAssistantMessage();
|
|
204
211
|
if (lastAssistant) {
|
|
@@ -253,6 +260,8 @@ async function promptInternal(text, options) {
|
|
|
253
260
|
messages.unshift(updateMessage);
|
|
254
261
|
}
|
|
255
262
|
catch (error) {
|
|
263
|
+
if (preparingRun)
|
|
264
|
+
this._isPreparingRun = false;
|
|
256
265
|
preflightResult?.(false);
|
|
257
266
|
throw error;
|
|
258
267
|
}
|
|
@@ -264,7 +273,7 @@ async function promptInternal(text, options) {
|
|
|
264
273
|
dispatched = true;
|
|
265
274
|
preflightResult?.(true, disposition);
|
|
266
275
|
};
|
|
267
|
-
const turn = this._runAgentPrompt(messages, workflowDelivery?.promptStarted, reportDispatch);
|
|
276
|
+
const turn = this._runAgentPrompt(messages, workflowDelivery?.promptStarted, reportDispatch, true);
|
|
268
277
|
workflowDelivery?.delivered?.("prompt");
|
|
269
278
|
try {
|
|
270
279
|
await turn;
|
|
@@ -277,7 +286,7 @@ async function promptInternal(text, options) {
|
|
|
277
286
|
throw error;
|
|
278
287
|
}
|
|
279
288
|
}
|
|
280
|
-
export async function _runAgentPrompt(messages, promptStarted, reportDispatch) {
|
|
289
|
+
export async function _runAgentPrompt(messages, promptStarted, reportDispatch, prepared = false, customStarter) {
|
|
281
290
|
const lifetime = sessionLifetime(this);
|
|
282
291
|
const owner = resolveWorkflowStageDeliveryTarget(this);
|
|
283
292
|
if (owner !== this) {
|
|
@@ -288,14 +297,31 @@ export async function _runAgentPrompt(messages, promptStarted, reportDispatch) {
|
|
|
288
297
|
reportDispatch?.("queued");
|
|
289
298
|
return;
|
|
290
299
|
}
|
|
291
|
-
return owner._runAgentPrompt(messages, promptStarted, reportDispatch);
|
|
300
|
+
return owner._runAgentPrompt(messages, promptStarted, reportDispatch, prepared, customStarter);
|
|
292
301
|
}
|
|
293
302
|
if (this._isEmittingAgentSettled) {
|
|
294
|
-
this._deferredSettledActions.push(async () => await this._runAgentPrompt(messages, promptStarted, reportDispatch));
|
|
303
|
+
this._deferredSettledActions.push(async () => await this._runAgentPrompt(messages, promptStarted, reportDispatch, prepared, customStarter));
|
|
295
304
|
return;
|
|
296
305
|
}
|
|
297
306
|
if (this._activePromptCount === 0 && !this.isStreaming)
|
|
298
307
|
this._agentRunAbortRequested = false;
|
|
308
|
+
const startingMessages = Array.isArray(messages) ? messages : [messages];
|
|
309
|
+
const customMessage = !prepared
|
|
310
|
+
? (customStarter ?? startingMessages.find((message) => message.role === "custom"))
|
|
311
|
+
: undefined;
|
|
312
|
+
const ownsPreparation = prepared || customMessage !== undefined;
|
|
313
|
+
const preparation = customMessage ? Promise.withResolvers() : undefined;
|
|
314
|
+
if (preparation) {
|
|
315
|
+
customPreparations.set(this, preparation.promise);
|
|
316
|
+
this._isPreparingRun = "custom";
|
|
317
|
+
}
|
|
318
|
+
const releasePreparation = () => {
|
|
319
|
+
if (preparation && customPreparations.get(this) === preparation.promise) {
|
|
320
|
+
customPreparations.delete(this);
|
|
321
|
+
this._isPreparingRun = false;
|
|
322
|
+
preparation.resolve();
|
|
323
|
+
}
|
|
324
|
+
};
|
|
299
325
|
this._activePromptCount += 1;
|
|
300
326
|
try {
|
|
301
327
|
if (this._subagentMessageAdmission) {
|
|
@@ -309,11 +335,12 @@ export async function _runAgentPrompt(messages, promptStarted, reportDispatch) {
|
|
|
309
335
|
if (pendingPriority)
|
|
310
336
|
await pendingPriority;
|
|
311
337
|
if (this._agentRunAbortRequested) {
|
|
312
|
-
if (this.isStreaming || this._queuedMessagesPaused) {
|
|
338
|
+
if (this.agent.state.isStreaming || this._queuedMessagesPaused) {
|
|
313
339
|
reportDispatch?.("handled");
|
|
314
340
|
return;
|
|
315
341
|
}
|
|
316
|
-
this.
|
|
342
|
+
if (!this._isPreparingRun)
|
|
343
|
+
this._agentRunAbortRequested = false;
|
|
317
344
|
}
|
|
318
345
|
// An explicit stop may win during input preflight or priority preparation.
|
|
319
346
|
// Preserve the prepared input without opening a native turn past that gate.
|
|
@@ -324,11 +351,51 @@ export async function _runAgentPrompt(messages, promptStarted, reportDispatch) {
|
|
|
324
351
|
reportDispatch?.("queued");
|
|
325
352
|
return;
|
|
326
353
|
}
|
|
354
|
+
if (customMessage?.role === "custom") {
|
|
355
|
+
const lastAssistant = this._findLastAssistantMessage();
|
|
356
|
+
if (lastAssistant)
|
|
357
|
+
await this._checkCompaction(lastAssistant, false);
|
|
358
|
+
const selectedToolsBefore = this._baseSystemPromptOptions.selectedTools ?? [];
|
|
359
|
+
const result = await this._extensionRunner.emitBeforeAgentStart("", undefined, this._baseSystemPromptOptions, customMessage);
|
|
360
|
+
const nextTools = result.systemPromptOptions.selectedTools;
|
|
361
|
+
const handlerEditedTools = nextTools.length !== selectedToolsBefore.length ||
|
|
362
|
+
nextTools.some((name, index) => name !== selectedToolsBefore[index]);
|
|
363
|
+
if (!handlerEditedTools)
|
|
364
|
+
result.systemPromptOptions.selectedTools = this.getActiveToolNames();
|
|
365
|
+
messages = [
|
|
366
|
+
...startingMessages,
|
|
367
|
+
...this._pendingNextTurnMessages,
|
|
368
|
+
...result.messages.map((message) => ({
|
|
369
|
+
...message,
|
|
370
|
+
role: "custom",
|
|
371
|
+
content: message.content ?? [],
|
|
372
|
+
timestamp: Date.now(),
|
|
373
|
+
})),
|
|
374
|
+
];
|
|
375
|
+
this._pendingNextTurnMessages = [];
|
|
376
|
+
const updateMessage = this._preparePromptAndToolLoadout(result.systemPromptOptions);
|
|
377
|
+
this._runSystemPromptOptions = result.systemPromptOptions;
|
|
378
|
+
if (updateMessage)
|
|
379
|
+
messages.unshift(updateMessage);
|
|
380
|
+
}
|
|
381
|
+
if (this._queuedMessagesPaused) {
|
|
382
|
+
const items = Array.isArray(messages) ? messages : [messages];
|
|
383
|
+
for (const message of items)
|
|
384
|
+
this._queueAgentMessage(message, "steer");
|
|
385
|
+
reportDispatch?.("queued");
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (ownsPreparation) {
|
|
389
|
+
this._flushPendingBashMessages();
|
|
390
|
+
this._flushPendingCustomMessages();
|
|
391
|
+
this._isPreparingRun = false;
|
|
392
|
+
}
|
|
327
393
|
if (this._disposed || lifetime.aborted || sessionGenerationClosing.has(this))
|
|
328
394
|
throw Object.assign(new Error("Session is closed"), { code: "SessionClosed" });
|
|
329
395
|
this._pendingToolNames.clear();
|
|
330
396
|
reportDispatch?.("started");
|
|
331
397
|
const turn = this.agent.prompt(messages);
|
|
398
|
+
releasePreparation();
|
|
332
399
|
if (this.isStreaming)
|
|
333
400
|
promptStarted?.();
|
|
334
401
|
await turn;
|
|
@@ -350,6 +417,9 @@ export async function _runAgentPrompt(messages, promptStarted, reportDispatch) {
|
|
|
350
417
|
await runBeforeSettleContinuations(this);
|
|
351
418
|
}
|
|
352
419
|
finally {
|
|
420
|
+
releasePreparation();
|
|
421
|
+
if (ownsPreparation && !preparation)
|
|
422
|
+
this._isPreparingRun = false;
|
|
353
423
|
this._runSystemPromptOptions = undefined;
|
|
354
424
|
this._cacheWarmer?.onAgentSettled();
|
|
355
425
|
await this._agentEventQueue;
|
|
@@ -528,11 +598,11 @@ async function answerAdmittedQueuedMessage(session) {
|
|
|
528
598
|
// Publish this turn before it starts. It begins after the pause abort boundary
|
|
529
599
|
// resolves, so a submission that only waited for that boundary would race it
|
|
530
600
|
// and be rejected by the streaming guard while the queue is still paused.
|
|
531
|
-
const recovery = (async () => {
|
|
601
|
+
const recovery = Promise.resolve().then(async () => {
|
|
532
602
|
await session.agent.continue();
|
|
533
603
|
await session.waitForRetry();
|
|
534
604
|
await session._agentEventQueue;
|
|
535
|
-
})
|
|
605
|
+
});
|
|
536
606
|
const settled = recovery.catch(() => undefined);
|
|
537
607
|
session._admittedRecoveryTurn = settled;
|
|
538
608
|
try {
|