T-3: a bundle's finding writer publishes with ack and listens with deliver new through the runtime channel

Builds: design 47 G §559 (the finding-calls word check waits on a design question)

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
This commit is contained in:
2026-11-11 13:55:44 +01:00
parent 9dda6f7540
commit 37a288b749
6 changed files with 132 additions and 9 deletions
+13 -5
View File
@@ -14,7 +14,7 @@
*/
import type { ToolDefinition } from "../contracts/index.js";
import { collectTools } from "../tools/index.js";
import { useBroker, type Broker, type Envelope } from "../messaging/index.js";
import { useBroker, type Broker, type Envelope, type PublishOptions, type SubscribeOptions } from "../messaging/index.js";
import { useStateChannel, type StateChange } from "../state/index.js";
import { useRuntime } from "../runtime/index.js";
import { runAs } from "../callers/index.js";
@@ -97,15 +97,23 @@ function startSession(): Session {
// redelivered until it expires — the consumer's filter is the controller's and may be wider.
const listeners: { pattern: string; handler: (env: Envelope<unknown>) => Promise<void> }[] = [];
const viaRuntime: Broker = {
publish: async <T>(env: Envelope<T>) => {
await ask("mesh/publish", env);
publish: async <T>(env: Envelope<T>, options?: PublishOptions) => {
if (!options?.ack) {
await ask("mesh/publish", env);
return;
}
// The runtime answers an acknowledged publish with the bus's sequence, or `{error}` (design 47 G §559).
const answer = (await ask("mesh/publish", { ...env, ack: true })) as { error?: unknown } | null;
if (answer && typeof answer === "object" && answer.error !== undefined) {
throw new Error(`the bus did not acknowledge ${env.key}: ${String(answer.error)}`);
}
},
request: (async <Req, Res>(key: string, body: Req): Promise<Res> =>
(await ask("mesh/ask", { key, body })) as Res) as Broker["request"],
handle: (async () => {
throw new Error("a launched bundle answers through its tools, not by binding subjects (novox/hq ADR 0193)");
}) as Broker["handle"],
subscribe: (async <T>(pattern: string, handler: (env: Envelope<T>) => Promise<void>) => {
subscribe: (async <T>(pattern: string, handler: (env: Envelope<T>) => Promise<void>, options?: SubscribeOptions) => {
const listener = { pattern, handler: handler as (env: Envelope<unknown>) => Promise<void> };
listeners.push(listener);
const remove = (): void => {
@@ -113,7 +121,7 @@ function startSession(): Session {
if (at >= 0) listeners.splice(at, 1);
};
try {
await ask("mesh/subscribe", { pattern });
await ask("mesh/subscribe", options?.deliver ? { pattern, deliver: options.deliver } : { pattern });
} catch (err) {
// Refused: nothing is bound, and a handler left behind would be handed each event twice once
// the module asks again (novox/hq issue 276).