Skip to content

Commit d7ee851

Browse files
committed
fix(plugin-sdk): trim worker live-event declarations
1 parent 628a654 commit d7ee851

6 files changed

Lines changed: 52 additions & 23 deletions

File tree

‎packages/gateway-protocol/src/schema/worker-admission.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,13 @@ export const WorkerLiveEventSchema = Type.Union([
730730
workerLiveObject({ kind: Type.Literal("lifecycle"), payload: WorkerLiveLifecyclePayloadSchema }),
731731
]);
732732

733-
export const WorkerLiveEventParamsSchema = workerLiveObject({
733+
export const WorkerLiveEventParamsSchema: Type.TObject<{
734+
readonly runEpoch: typeof LiveIntegerSchema;
735+
readonly lastAckedSeq: typeof LiveIntegerSchema;
736+
readonly seq: typeof LiveSequenceSchema;
737+
readonly runId: typeof WorkerIdentifierSchema;
738+
readonly event: typeof WorkerLiveEventSchema;
739+
}> = workerLiveObject({
734740
runEpoch: LiveIntegerSchema,
735741
lastAckedSeq: LiveIntegerSchema,
736742
seq: LiveSequenceSchema,
@@ -764,7 +770,12 @@ export const WorkerLiveEventErrorShapeSchema = workerLiveObject({
764770
details: WorkerLiveEventErrorDetailsSchema,
765771
});
766772

767-
export const WorkerLiveEventRequestFrameSchema = workerLiveObject({
773+
export const WorkerLiveEventRequestFrameSchema: Type.TObject<{
774+
readonly type: Type.TLiteral<"req">;
775+
readonly id: typeof WorkerFrameIdSchema;
776+
readonly method: Type.TLiteral<(typeof WORKER_PROTOCOL_METHODS)[2]>;
777+
readonly params: typeof WorkerLiveEventParamsSchema;
778+
}> = workerLiveObject({
768779
type: Type.Literal("req"),
769780
id: WorkerFrameIdSchema,
770781
method: Type.Literal(WORKER_PROTOCOL_METHODS[2]),

‎src/gateway/server-chat.agent-events.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { formatChannelProgressDraftLine } from "../channels/streaming.js";
99
import {
1010
claimAgentRunContext,
1111
emitAgentEventForOwner,
12-
onAgentEvent,
12+
onAgentRuntimeEvent,
1313
registerAgentRunContext,
1414
releaseAgentRunContext,
1515
resetAgentRunContextForTest,
@@ -4121,7 +4121,7 @@ describe("agent event handler", () => {
41214121
{ isControlUiVisible: false, sessionKey: "session-hidden" },
41224122
{ exclusive: true, trackOwner: true },
41234123
)!;
4124-
const stop = onAgentEvent(handler);
4124+
const stop = onAgentRuntimeEvent(handler);
41254125
emitAgentEventForOwner(
41264126
{ runId: "revoked", stream: "lifecycle", data: { phase: "error", error: "retry" } },
41274127
claimId,

‎src/gateway/server-chat.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { normalizeVerboseLevel } from "../auto-reply/thinking.js";
1212
import { getRuntimeConfig } from "../config/io.js";
1313
import {
1414
type AgentEventPayload,
15+
type AgentEventRuntimePayload,
1516
getAgentEventLifecycleGeneration,
1617
getAgentRunContext,
1718
getAgentRunContextOwnerStatus,
@@ -379,8 +380,8 @@ export function createAgentEventHandler({
379380
resolveActiveLifecycleGenerationForRun = () => undefined,
380381
updateRunToolErrorSummary,
381382
resolveSessionActiveRunState,
382-
}: AgentEventHandlerOptions) {
383-
const shouldProcessOwnedEvent = (evt: AgentEventPayload): boolean => {
383+
}: AgentEventHandlerOptions): (event: AgentEventPayload) => void {
384+
const shouldProcessOwnedEvent = (evt: AgentEventRuntimePayload): boolean => {
384385
const claimId = evt.contextClaimId;
385386
if (!claimId) {
386387
return true;
@@ -392,7 +393,7 @@ export function createAgentEventHandler({
392393
// A missing claim means detach or supersession revoked this terminal event.
393394
return getAgentRunContextOwnerStatus(evt.runId, claimId, lifecycleGeneration) === "active";
394395
};
395-
const clearRunContextForEvent = (evt: AgentEventPayload): void => {
396+
const clearRunContextForEvent = (evt: AgentEventRuntimePayload): void => {
396397
if (evt.contextClaimId) {
397398
clearAgentRunContext(evt.runId, evt.lifecycleGeneration, evt.contextClaimId);
398399
return;
@@ -407,7 +408,7 @@ export function createAgentEventHandler({
407408
};
408409
type PendingTerminalLifecycleError = {
409410
timer: NodeJS.Timeout;
410-
event: AgentEventPayload;
411+
event: AgentEventRuntimePayload;
411412
opts?: TerminalLifecycleOptions;
412413
};
413414

@@ -645,7 +646,10 @@ export function createAgentEventHandler({
645646
);
646647
};
647648

648-
const finalizeLifecycleEvent = (evt: AgentEventPayload, opts?: TerminalLifecycleOptions) => {
649+
const finalizeLifecycleEvent = (
650+
evt: AgentEventRuntimePayload,
651+
opts?: TerminalLifecycleOptions,
652+
) => {
649653
if (!shouldProcessOwnedEvent(evt)) {
650654
return;
651655
}
@@ -847,7 +851,7 @@ export function createAgentEventHandler({
847851
};
848852

849853
const scheduleTerminalLifecycleError = (
850-
evt: AgentEventPayload,
854+
evt: AgentEventRuntimePayload,
851855
opts?: TerminalLifecycleOptions,
852856
) => {
853857
clearPendingTerminalLifecycleError(evt.runId);
@@ -1262,7 +1266,8 @@ export function createAgentEventHandler({
12621266
}
12631267
};
12641268

1265-
return (evt: AgentEventPayload) => {
1269+
return (event: AgentEventPayload) => {
1270+
const evt = event as AgentEventRuntimePayload;
12661271
if (!shouldProcessOwnedEvent(evt)) {
12671272
return;
12681273
}

‎src/gateway/server-runtime-subscriptions.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { isAuditLedgerEnabled, resolveAuditMessageMode } from "../audit/audit-co
44
import { createAuditEventRecorder } from "../audit/audit-recorder.js";
55
import { onTrustedMessageAuditEvent } from "../audit/message-audit-events.js";
66
import { getRuntimeConfig } from "../config/io.js";
7-
import { clearAgentRunContext, onAgentAuditEvent, onAgentEvent } from "../infra/agent-events.js";
7+
import {
8+
clearAgentRunContext,
9+
onAgentAuditEvent,
10+
onAgentRuntimeEvent,
11+
} from "../infra/agent-events.js";
812
import { onTrustedToolExecutionEvent } from "../infra/diagnostic-events.js";
913
import { onHeartbeatEvent } from "../infra/heartbeat-events.js";
1014
import type { SubsystemLogger } from "../logging/subsystem.js";
@@ -242,7 +246,7 @@ export function startGatewayEventSubscriptions(params: {
242246
return lifecycleEventHandlerPromise;
243247
};
244248

245-
const unsubscribeAgentEvents = onAgentEvent((evt) => {
249+
const unsubscribeAgentEvents = onAgentRuntimeEvent((evt) => {
246250
if (auditEnabled) {
247251
auditRecorder.record(evt);
248252
}

‎src/gateway/worker-environments/live-events.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
clearAgentRunContext,
1414
emitAgentEvent,
1515
getAgentRunContext,
16-
onAgentEvent,
16+
onAgentRuntimeEvent,
1717
sweepStaleRunContexts,
18-
type AgentEventPayload as Event,
18+
type AgentEventRuntimePayload as Event,
1919
} from "../../infra/agent-events.js";
2020
import type { WorkerConnectionIdentity as Identity } from "./connection-identity.js";
2121
import {
@@ -118,7 +118,7 @@ describe("worker live events", () => {
118118
await create(10);
119119
start();
120120
events = [];
121-
unsubscribe = onAgentEvent((event) => events.push(event));
121+
unsubscribe = onAgentRuntimeEvent((event) => events.push(event));
122122
});
123123

124124
afterEach(async () => {

‎src/infra/agent-events.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ export type AgentEventPayload = {
117117
data: Record<string, unknown>;
118118
/** Internal, non-enumerable gateway lifecycle generation that owns this run. */
119119
lifecycleGeneration?: string;
120-
controlUiVisible?: boolean;
121-
contextClaimId?: string;
122-
deliverySessionKey?: string;
123120
sessionKey?: string;
124121
/**
125122
* sessionId the run was bound to when it started. Lifecycle persistence uses
@@ -130,6 +127,13 @@ export type AgentEventPayload = {
130127
agentId?: string;
131128
};
132129

130+
/** Gateway-only routing metadata stamped onto events after public input validation. */
131+
export type AgentEventRuntimePayload = AgentEventPayload & {
132+
readonly controlUiVisible?: boolean;
133+
readonly contextClaimId?: string;
134+
readonly deliverySessionKey?: string;
135+
};
136+
133137
/** Per-run metadata used to stamp events and gate Control UI visibility. */
134138
export type AgentRunContext = {
135139
sessionKey?: string;
@@ -152,7 +156,7 @@ export type AgentRunContext = {
152156

153157
type AgentEventState = {
154158
seqByRun: Map<string, number>;
155-
listeners: Set<(evt: AgentEventPayload) => void>;
159+
listeners: Set<(evt: AgentEventRuntimePayload) => void>;
156160
auditListeners: Set<(evt: AgentEventPayload) => void>;
157161
runContextById: Map<string, AgentRunContext>;
158162
runContextOwnersById?: Map<
@@ -179,7 +183,7 @@ type AgentEventExecutionContext = {
179183
function getAgentEventState(): AgentEventState {
180184
return resolveGlobalSingleton<AgentEventState>(AGENT_EVENT_STATE_KEY, () => ({
181185
seqByRun: new Map<string, number>(),
182-
listeners: new Set<(evt: AgentEventPayload) => void>(),
186+
listeners: new Set<(evt: AgentEventRuntimePayload) => void>(),
183187
auditListeners: new Set<(evt: AgentEventPayload) => void>(),
184188
runContextById: new Map<string, AgentRunContext>(),
185189
lifecycleGeneration: randomUUID(),
@@ -529,7 +533,7 @@ export function resetAgentRunContextForTest() {
529533
function enrichAgentEvent(
530534
event: Omit<AgentEventPayload, "seq" | "ts">,
531535
claimId?: string,
532-
): AgentEventPayload | undefined {
536+
): AgentEventRuntimePayload | undefined {
533537
const state = getAgentEventState();
534538
const owners = getAgentRunContextOwners(state).get(event.runId);
535539
if (claimId !== undefined) {
@@ -584,7 +588,7 @@ function enrichAgentEvent(
584588
? (ownedLifecycleGeneration ?? state.lifecycleGeneration)
585589
: ownedLifecycleGeneration;
586590
const agentId = event.agentId ?? context?.agentId;
587-
const enriched: AgentEventPayload = {
591+
const enriched: AgentEventRuntimePayload = {
588592
...event,
589593
sessionKey,
590594
...(sessionId ? { sessionId } : {}),
@@ -716,6 +720,11 @@ export function onAgentEvent(listener: (evt: AgentEventPayload) => void) {
716720
return registerListener(state.listeners, listener);
717721
}
718722

723+
/** Subscribes Gateway internals that consume non-public ownership and routing metadata. */
724+
export function onAgentRuntimeEvent(listener: (evt: AgentEventRuntimePayload) => void) {
725+
return registerListener(getAgentEventState().listeners, listener);
726+
}
727+
719728
/** Subscribes to private audit-only agent events; returns an unsubscribe callback. */
720729
export function onAgentAuditEvent(listener: (evt: AgentEventPayload) => void) {
721730
return registerListener(getAgentEventState().auditListeners, listener);

0 commit comments

Comments
 (0)