The Open Framework for Building Agents
refactor(eve): run stream hooks inside publishChannelEvent (#3793)
e0-gh-vercel-connect[bot]c2f3d0aec3dd
diff --git a/packages/eve/src/execution/publish-channel-event.ts b/packages/eve/src/execution/publish-channel-event.tsindex f83fb0c67..d85d6f088 100644--- a/packages/eve/src/execution/publish-channel-event.ts+++ b/packages/eve/src/execution/publish-channel-event.ts@@ -4,6 +4,7 @@ import {type ChannelAdapterContext,} from "#channel/adapter.js";import type { ContextContainer } from "#context/container.js";+import { dispatchStreamEventHooks } from "#context/hook-lifecycle.js";import { setChannelContext } from "#execution/channel-context.js";import {encodeMessageStreamEvent,@@ -11,16 +12,37 @@ import {type MessageStreamEvent,type UnstampedMessageStreamEvent,} from "#protocol/message.js";+import { BundleKey } from "#runtime/sessions/runtime-context-keys.js";-/** Publishes an already-routed event and returns its durable stream identity. */-export async function publishChannelEvent(input: {+interface ChannelEventInput {readonly adapter: ChannelAdapter;readonly adapterCtx: ChannelAdapterContext;readonly ctx: ContextContainer;readonly deliveryIds?: readonly string[];readonly event: UnstampedMessageStreamEvent;readonly writer: WritableStreamDefaultWriter<Uint8Array>;-}): Promise<MessageStreamEvent> {+}++/**+ * Publishes an already-routed event, then runs its stream-event hooks on the+ * stamped event. Callers with subscribed hooks must hold an active context scope.+ */+export async function publishChannelEvent(input: ChannelEventInput): Promise<MessageStreamEvent> {+ const stamped = await writeChannelEvent(input);+ await dispatchStreamEventHooks({+ ctx: input.ctx,+ registry: input.ctx.require(BundleKey).hookRegistry,+ event: stamped,+ });+ return stamped;+}++/**+ * Publishes without hooks. Only `createSessionEventSink` uses this: turn steps+ * run memory lifecycle before hooks, and subagent notifications release the+ * writer before hooks. Both dispatch hooks themselves after `sink.emit`.+ */+export async function writeChannelEvent(input: ChannelEventInput): Promise<MessageStreamEvent> {const event = await callAdapterEventHandler(input.adapter, input.event, input.adapterCtx);setChannelContext(input.ctx, { ...input.adapter, state: { ...input.adapterCtx.state } });const stamped = stampMessageStreamEvent(event, input.deliveryIds);diff --git a/packages/eve/src/execution/session/event-sink.ts b/packages/eve/src/execution/session/event-sink.tsindex ebafe7a8f..7bdd69ed2 100644--- a/packages/eve/src/execution/session/event-sink.ts+++ b/packages/eve/src/execution/session/event-sink.ts@@ -3,7 +3,7 @@ import type { ChannelAdapter } from "#channel/adapter.js";import type { ContextContainer } from "#context/container.js";import { ScheduleIdKey, TurnDeliveryIdsKey, TurnTaskDeliveryKey } from "#context/keys.js";import * as activityCohort from "#execution/activity-cohort.js";-import { publishChannelEvent } from "#execution/publish-channel-event.js";+import { writeChannelEvent } from "#execution/publish-channel-event.js";import { observeSessionActivity } from "#execution/session-activity-projection.js";import { scheduledLaunchDeliveryEvent } from "#execution/scheduled-launch-delivery.js";import { forwardTaskEventToSessionCallback } from "#execution/task-event-callback.js";@@ -60,7 +60,7 @@ export function createSessionEventSink(input: SessionEventSinkInput): SessionEvesuppressed: true,};}- const stamped = await publishChannelEvent({+ const stamped = await writeChannelEvent({adapter,adapterCtx,ctx,diff --git a/packages/eve/src/execution/settle-cancelled-turn-step.ts b/packages/eve/src/execution/settle-cancelled-turn-step.tsindex ef64598c8..959d08a49 100644--- a/packages/eve/src/execution/settle-cancelled-turn-step.ts+++ b/packages/eve/src/execution/settle-cancelled-turn-step.ts@@ -1,11 +1,9 @@import { getPendingCoordinationBatch } from "#harness/coordination.js";import { buildAdapterContext } from "#channel/adapter-context.js";-import { callAdapterEventHandler } from "#channel/adapter.js";-import { dispatchStreamEventHooks } from "#context/hook-lifecycle.js";import { TurnDeliveryIdsKey } from "#context/keys.js";import { withContextScope } from "#context/run-step.js";import { deserializeContext, serializeContext } from "#context/serialize.js";-import { setChannelContext } from "#execution/channel-context.js";+import { publishChannelEvent } from "#execution/publish-channel-event.js";import { observeSessionActivity } from "#execution/session-activity-projection.js";import {createDurableSessionState,@@ -38,11 +36,7 @@ import {} from "#harness/workflow-tool-runs.js";import { bindSessionInstrumentation } from "#instrumentation/runtime.js";import { getTurnUsageState, toUsage } from "#harness/turn-tag-state.js";-import {- encodeMessageStreamEvent,- type UnstampedMessageStreamEvent,- stampMessageStreamEvent,-} from "#protocol/message.js";+import type { UnstampedMessageStreamEvent } from "#protocol/message.js";import { BundleKey, ChannelKey } from "#runtime/sessions/runtime-context-keys.js";import { resolveEffectiveAgentRuntime } from "#execution/effective-agent-config.js";import type { TokenUsage } from "#shared/token-usage.js";@@ -105,17 +99,15 @@ export async function settleCancelledTurnStep(input: {try {const scoped = await withContextScope(ctx, session, async (enrichedSession) => {const baseEmit = async (event: UnstampedMessageStreamEvent): Promise<void> => {- const transformed = await callAdapterEventHandler(adapter, event, adapterCtx);- setChannelContext(ctx, { ...adapter, state: { ...adapterCtx.state } });- // Stamp once: the persisted chunk and the hooks must agree on the id.- const stamped = stampMessageStreamEvent(transformed, ctx.get(TurnDeliveryIdsKey));- await writer.write(encodeMessageStreamEvent(stamped));- void observeSessionActivity({ ctx, event: stamped, sessionId: session.sessionId });- await dispatchStreamEventHooks({+ const stamped = await publishChannelEvent({+ adapter,+ adapterCtx,ctx,- event: stamped,- registry: bundle.hookRegistry,+ writer,+ event,+ deliveryIds: ctx.get(TurnDeliveryIdsKey),});+ void observeSessionActivity({ ctx, event: stamped, sessionId: session.sessionId });};const emit =instrumentation?.createHandleEvent({diff --git a/packages/eve/src/subagents/event-proxy-step.ts b/packages/eve/src/subagents/event-proxy-step.tsindex fc1e6dd5b..78aa167d6 100644--- a/packages/eve/src/subagents/event-proxy-step.ts+++ b/packages/eve/src/subagents/event-proxy-step.ts@@ -4,7 +4,6 @@ import type {SubagentInputRequestHookPayload,} from "#channel/types.js";import type { ContextContainer } from "#context/container.js";-import { dispatchStreamEventHooks } from "#context/hook-lifecycle.js";import { ModeKey } from "#context/keys.js";import { withContextScope } from "#context/run-step.js";import { deserializeContext, serializeContext } from "#context/serialize.js";@@ -128,8 +127,7 @@ export async function emitProxiedSubagentEvent(input: {const emit = async (event: UnstampedMessageStreamEvent): Promise<void> => {// The child event is already routed; do not forward it again or apply// the parent's scheduled-turn suppression from createSessionEventSink.- const stamped = await publishChannelEvent({ adapter, adapterCtx, ctx, event, writer });- await dispatchStreamEventHooks({ ctx, registry: bundle.hookRegistry, event: stamped });+ await publishChannelEvent({ adapter, adapterCtx, ctx, event, writer });};const scopeResult = await withContextScope(ctx, session, async (enrichedSession) => {