Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ export class RuntimeService {
}

for await (const event of adapter.streamEvents(task.id)) {
await this.store.appendEvent(event);
if (event.type === "task.log" && event.message) {
await this.store.appendLog(task.id, `${event.message}\n`);
const validatedEvent = this.validateAdapterEvent(task.id, event);
if (!validatedEvent) continue;
await this.store.appendEvent(validatedEvent);
if (validatedEvent.type === "task.log" && validatedEvent.message) {
await this.store.appendLog(task.id, `${validatedEvent.message}\n`);
}
}

Expand Down Expand Up @@ -288,6 +290,16 @@ export class RuntimeService {
: undefined;
}

private validateAdapterEvent(taskId: string, event: RuntimeEvent): RuntimeEvent | undefined {
if (event.taskId === taskId) return event;
void this.store.appendEvent(this.event(taskId, "task.log", "Ignored adapter event with mismatched taskId.", {
expectedTaskId: taskId,
receivedTaskId: event.taskId,
eventType: event.type
}));
return undefined;
}

private event(taskId: string, type: RuntimeEvent["type"], message?: string, payload?: Record<string, unknown>): RuntimeEvent {
return { taskId, type, message, payload, timestamp: nowIso() };
}
Expand Down
39 changes: 38 additions & 1 deletion test/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ function mockAdapter(events: RuntimeEvent[], cleanup: CleanupResult = { status:
}),
provision: async () => ({}),
startTask: async () => ({ result: { ok: true } }),
streamEvents: async function* () { yield* events; },
streamEvents: async function* (taskId: string) {
for (const event of events) {
yield { ...event, taskId: event.taskId === "unused" ? taskId : event.taskId };
}
},
cancel: async () => ({ status: "cancelled" }),
cleanup: async () => cleanup
};
Expand Down Expand Up @@ -111,6 +115,39 @@ describe("RuntimeService", () => {
})).rejects.toMatchObject({ code: "policy.denied" });
});

it("ignores adapter events for a different task id", async () => {
const store = new MemoryStore();
const request: DispatchRequest = {
provider: "aws",
accountProfile: "dev-aws",
capability: "agent-runtime",
taskType: "agent.run",
target: { mode: "session" },
input: { instruction: "run" }
};
const service = new RuntimeService({
config: {
accounts: { "dev-aws": { provider: "aws", credentialSource: "aws-sdk-default" } },
backends: {}
},
store,
adapters: [mockAdapter([
{ taskId: "wrong_task", type: "task.log", message: "wrong task log", timestamp: nowIso() },
{ taskId: "unused", type: "task.log", message: "correct task log", timestamp: nowIso() }
])]
});

const handle = await service.dispatchTask(request);
await waitForStatus(service, handle.taskId, "succeeded");

expect(store.events.get("wrong_task")).toBeUndefined();
expect(store.logs.get(handle.taskId)).not.toContain("wrong task log");
expect(store.logs.get(handle.taskId)).toContain("correct task log");
expect(store.events.get(handle.taskId)).toEqual(expect.arrayContaining([
expect.objectContaining({ type: "task.log", message: "Ignored adapter event with mismatched taskId." })
]));
});

it("persists runtime cleanup status after successful runtime tasks", async () => {
const store = new MemoryStore();
const request: DispatchRequest = {
Expand Down
Loading