From 9e3107fcb76cdb194f2ece45f4c8ff2c684028dc Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 25 Nov 2026 16:20:34 +0100 Subject: [PATCH] test(opencode): read v2 children through the timeline-backed store The child tests merged from main build a `ProviderSubagentStore` with no timeline and read `list()` synchronously. On this branch the store keeps children in the agent's timeline, so it takes one and its reads resolve asynchronously. Each `apply` queues on the parent's timeline before its first await, so an awaited `list` observes every event emitted before it. --- .../agent/providers/opencode/v2/agent.test.ts | 16 +++---- .../providers/opencode/v2/children.test.ts | 43 +++++++++---------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/packages/server/src/server/agent/providers/opencode/v2/agent.test.ts b/packages/server/src/server/agent/providers/opencode/v2/agent.test.ts index 895711907e..fa6f42226f 100644 --- a/packages/server/src/server/agent/providers/opencode/v2/agent.test.ts +++ b/packages/server/src/server/agent/providers/opencode/v2/agent.test.ts @@ -6,6 +6,7 @@ import { describe, expect, test } from "vitest"; import { createTestLogger } from "../../../../../test-utils/test-logger.js"; import type { AgentStreamEvent } from "../../../agent-sdk-types.js"; import { ProviderSubagentStore } from "../../../provider-subagents/store.js"; +import { AgentTimeline } from "../../../timeline/index.js"; import { OpenCodeV2AgentClient } from "./agent.js"; import { V2Harness } from "../test-utils/v2-harness.js"; import type { V2Api } from "./api.js"; @@ -833,20 +834,19 @@ describe("OpenCode v2 session lifecycle", () => { { provider: "opencode", cwd: "/tmp/project" }, launchEnvironment === "with" ? { env: { PASEO_AGENT_ID: "parent" } } : undefined, ); - const store = new ProviderSubagentStore(); + const store = new ProviderSubagentStore(new AgentTimeline()); session.subscribe((event) => { - if (event.type === "provider_subagent") store.apply("parent", "opencode", event.event); + if (event.type === "provider_subagent") + void store.apply("parent", "opencode", event.event); }); try { for (const info of [child, sibling, grandchild]) sessions.set(info.id, info); harness.push({ id: child.id, created: 2, type: "session.created", data: child }); - const ids = () => - store - .list("parent") - .map(({ id }) => id) - .sort(); + const ids = async () => (await store.list("parent")).map(({ id }) => id).sort(); await expect.poll(ids).toEqual([child.id, grandchild.id, sibling.id]); - expect(store.list("parent").find(({ id }) => id === child.id)?.status).toBe("running"); + expect((await store.list("parent")).find(({ id }) => id === child.id)?.status).toBe( + "running", + ); sessions.delete(child.id); harness.environments.length = 0; diff --git a/packages/server/src/server/agent/providers/opencode/v2/children.test.ts b/packages/server/src/server/agent/providers/opencode/v2/children.test.ts index 1ea5f02810..7fa5774faf 100644 --- a/packages/server/src/server/agent/providers/opencode/v2/children.test.ts +++ b/packages/server/src/server/agent/providers/opencode/v2/children.test.ts @@ -5,6 +5,7 @@ import type { AgentStreamEvent } from "../../../agent-sdk-types.js"; import { V2Harness } from "../test-utils/v2-harness.js"; import { SessionChildren } from "./children.js"; import { ProviderSubagentStore } from "../../../provider-subagents/store.js"; +import { AgentTimeline } from "../../../timeline/index.js"; function setup() { const harness = new V2Harness(); @@ -54,20 +55,16 @@ function setupTree() { if (!info) throw { _tag: "SessionNotFoundError", sessionID }; return info; }; - const store = new ProviderSubagentStore(); + const store = new ProviderSubagentStore(new AgentTimeline()); const children = new SessionChildren({ client: () => harness.api, id: "session", emit: (event) => { - if (event.type === "provider_subagent") store.apply("parent", "opencode", event.event); + if (event.type === "provider_subagent") void store.apply("parent", "opencode", event.event); }, reconcilePermissions: async () => {}, }); - const ids = () => - store - .list("parent") - .map((child) => child.id) - .sort(); + const ids = async () => (await store.list("parent")).map((child) => child.id).sort(); return { harness, sessions, children, ids, store }; } @@ -85,17 +82,17 @@ describe("OpenCode v2 child presentation", () => { return child; }; harness.api.session.active = async () => (exists ? { child: { type: "running" } } : {}); - const store = new ProviderSubagentStore(); + const store = new ProviderSubagentStore(new AgentTimeline()); const children = new SessionChildren({ client: () => harness.api, id: "session", emit: (event) => { - if (event.type === "provider_subagent") store.apply("parent", "opencode", event.event); + if (event.type === "provider_subagent") void store.apply("parent", "opencode", event.event); }, reconcilePermissions: async () => {}, }); await children.reconcile("session"); - expect(store.list("parent")).toMatchObject([{ id: "child", status: "running" }]); + expect(await store.list("parent")).toMatchObject([{ id: "child", status: "running" }]); exists = false; if (trigger === "reconnect") await children.reconcile("session"); else @@ -106,7 +103,7 @@ describe("OpenCode v2 child presentation", () => { durable: { aggregateID: "child", seq: 1, version: 1 }, data: { sessionID: "child" }, }); - expect(store.list("parent")).toEqual([]); + expect(await store.list("parent")).toEqual([]); }); test.each(["deletion event", "reconnect"])( @@ -114,7 +111,7 @@ describe("OpenCode v2 child presentation", () => { async (trigger) => { const { sessions, children, ids } = setupTree(); await children.reconcile("session"); - expect(ids()).toEqual(["first", "grandchild", "second"]); + expect(await ids()).toEqual(["first", "grandchild", "second"]); sessions.delete("first"); sessions.delete("grandchild"); if (trigger === "reconnect") await children.reconcile("session"); @@ -126,7 +123,7 @@ describe("OpenCode v2 child presentation", () => { durable: { aggregateID: "first", seq: 1, version: 1 }, data: { sessionID: "first" }, }); - expect(ids()).toEqual(["second"]); + expect(await ids()).toEqual(["second"]); }, ); @@ -142,7 +139,7 @@ describe("OpenCode v2 child presentation", () => { }; await children.reconcile("session"); await children.reconcile("first"); - expect(ids()).toEqual(["first", "grandchild", "second"]); + expect(await ids()).toEqual(["first", "grandchild", "second"]); }); test.each(["present", "deleted"])( @@ -159,7 +156,7 @@ describe("OpenCode v2 child presentation", () => { } sessions.get("second")!.time = { created: 2, updated: 2 }; await children.reconcile("session"); - const expected = ids(); + const expected = await ids(); expect(expected).toHaveLength(102); if (grandchild === "deleted") sessions.delete("grandchild"); const remaining = expected.filter((id) => grandchild === "present" || id !== "grandchild"); @@ -181,8 +178,8 @@ describe("OpenCode v2 child presentation", () => { return { data: page, cursor: { next } }; }; await children.reconcile("session"); - expect(ids()).toEqual(remaining); - expect(store.list("parent").find((child) => child.id === "first")).toMatchObject({ + expect(await ids()).toEqual(remaining); + expect((await store.list("parent")).find((child) => child.id === "first")).toMatchObject({ title: "Updated during reconnect", }); await children.observe({ @@ -192,8 +189,8 @@ describe("OpenCode v2 child presentation", () => { durable: { aggregateID: "first", seq: 1, version: 1 }, data: { sessionID: "first" }, }); - expect(ids()).toEqual(remaining); - expect(store.list("parent").find((child) => child.id === "first")).toMatchObject({ + expect(await ids()).toEqual(remaining); + expect((await store.list("parent")).find((child) => child.id === "first")).toMatchObject({ title: "Updated during reconnect", }); }, @@ -209,10 +206,10 @@ describe("OpenCode v2 child presentation", () => { throw new Error("Provider disconnected"); }; await children.reconcile("session"); - expect(ids()).toEqual(["first", "grandchild", "second"]); + expect(await ids()).toEqual(["first", "grandchild", "second"]); harness.api.session.get = get; await children.reconcile("session"); - expect(ids()).toEqual(["second"]); + expect(await ids()).toEqual(["second"]); }); test("keeps missing children when a listing fails before its last page", async () => { @@ -228,10 +225,10 @@ describe("OpenCode v2 child presentation", () => { return list(input); }; await expect(children.reconcile("session")).rejects.toThrow("Provider disconnected"); - expect(ids()).toEqual(["first", "grandchild", "second"]); + expect(await ids()).toEqual(["first", "grandchild", "second"]); harness.api.session.list = list; await children.reconcile("session"); - expect(ids()).toEqual(["second"]); + expect(await ids()).toEqual(["second"]); }); test("restores agent, model, variant and all token categories in the child subtitle", async () => {