diff --git a/tests/runner-e2e/product-journeys/task-dependency-resume-evidence.ts b/tests/runner-e2e/product-journeys/task-dependency-resume-evidence.ts index 4a9fc619ec7..5d5c2be8fda 100644 --- a/tests/runner-e2e/product-journeys/task-dependency-resume-evidence.ts +++ b/tests/runner-e2e/product-journeys/task-dependency-resume-evidence.ts @@ -36,6 +36,12 @@ export function dependencyNewAgentRuns(runs: Row[], startingRuns: Row[]) { const priorIds = new Set(startingRuns.map(run => run.id)); return runs.filter(run => !priorIds.has(run.id)); } +export function dependencyRunProjectionsAgree(point: DependencyPoint, startingRuns: Row[], expectedRun?: Row) { + const newRuns = dependencyNewAgentRuns(point.agentRuns, startingRuns); + return point.dependentRuns.length === 1 && newRuns.length === 1 + && runFacts(point.dependentRuns[0]!) === runFacts(newRuns[0]!) + && (!expectedRun || runFacts(point.dependentRuns[0]!) === runFacts(expectedRun)); +} export function dependencyRunIssueId(run: Row) { const ids = [run.issueId, run.nativeIssueId, run.contextSnapshot?.issueId, run.contextSnapshot?.taskId].filter(value => value != null); return ids.length && ids.every(nonempty) && new Set(ids).size === 1 ? ids[0] as string : undefined; diff --git a/tests/runner-e2e/product-journeys/task-dependency-resume.md b/tests/runner-e2e/product-journeys/task-dependency-resume.md index f8f18dee135..446b29b2437 100644 --- a/tests/runner-e2e/product-journeys/task-dependency-resume.md +++ b/tests/runner-e2e/product-journeys/task-dependency-resume.md @@ -32,7 +32,14 @@ blocker completion audit. No agent work is permitted on the human-owned blocker. The case requires `company`, `board-user`, `task-write`, and `agent`. It declares one work run, uses the existing bounded case budget, and shares the original -campaign deadline. The native agent must have nonblank provider and model +campaign deadline. +The task and agent run reads can straddle completion. The completion wait requires +both projections to agree on the same run facts before saving the finished point. +After reopening, it applies the same settled check: done task, one succeeded and +committed run, agreeing projections, saved document and revision, and no active locks. +The reopened run facts must also match the already-settled finished run. +It uses the original deadline and does not replace either independent observation. +The native agent must have nonblank provider and model identifiers before any observation read or task creation. An incomplete execution identity returns `unsupported` without task or agent work. It saves the agent's starting run history and retains those diff --git a/tests/runner-e2e/product-journeys/task-dependency-resume.test.ts b/tests/runner-e2e/product-journeys/task-dependency-resume.test.ts index b6a4397a988..c21cf49f64d 100644 --- a/tests/runner-e2e/product-journeys/task-dependency-resume.test.ts +++ b/tests/runner-e2e/product-journeys/task-dependency-resume.test.ts @@ -1,3 +1,4 @@ +import { AsyncLocalStorage } from "node:async_hooks"; import { mkdir, mkdtemp, readFile, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; @@ -20,12 +21,25 @@ vi.mock("@playwright/test", () => ({ expect: Object.assign((locator: any) => ({ import { taskDependencyResume } from "./task-dependency-resume.js"; interface FixtureOptions { priorRuns?: number; unrelatedActivity?: number; ownedActivity?: number; agentActivity?: number; missingActivity?: boolean; activePrior?: boolean; - closedPanel?: "properties" | "toggle"; missingProperty?: boolean; missingStatusChoice?: boolean } + laggingAgentReads?: number; reopenedLaggingAgentReads?: number; reopenedStale?: "task-run" | "both-runs" | "task-status" | "document" | "locks" | "native-phase" | "run-facts"; reopenedStaleReads?: number; closedPanel?: "properties" | "toggle"; missingProperty?: boolean; missingStatusChoice?: boolean } async function fixture(premature = false, options: FixtureOptions = {}) { const directory = await mkdtemp(path.join(os.tmpdir(), "dependency-flow-")); const f = dependencyEvidenceFixture(), s = f.scope; const blocker = { ...f.created.blocker }, dependent = { ...f.created.dependent }; const actions: string[] = [], owned: any[] = [], writes: unknown[] = [], reads: string[] = []; + const projection = new AsyncLocalStorage<"task" | "agent">(); + const agentReads = { finished: 0, reopened: 0 }; + const detailReads: Array<{ projection: string; stage: string; status: string }> = []; + let reopened = false; + const staleReads: Record = {}; + const stale = (key: string) => reopened && (staleReads[key] = (staleReads[key] ?? 0) + 1) <= (options.reopenedStaleReads ?? 0); + // Carry the list's projection through both summary normalization and detail mapping. + // The concurrent detail requests use the same URL; call order must not identify them. + const projectedRows = (rows: any[], source: "task" | "agent"): any[] => { + Object.defineProperty(rows, "map", { value: (callback: (...args: any[]) => any) => + projectedRows(Array.prototype.map.call(rows, (...args: any[]) => projection.run(source, () => callback(...args))), source) }); + return rows; + }; let completed = false, assigned = false, panelOpen = !options.closedPanel; const reference = () => /review reference is ([a-f0-9]{32})/.exec(blocker.description)?.[1]; const output = () => JSON.stringify({ reference: reference(), blockerIdentifier: s.blockerIdentifier }); @@ -89,23 +103,45 @@ async function fixture(premature = false, options: FixtureOptions = {}) { }, getByPlaceholder(name: string) { return locator(name); }, getByTestId(name: string) { return locator(name); }, - async goto(url: string) { actions.push(`goto:${url}`); }, async reload() { actions.push("reload"); }, + async goto(url: string) { actions.push(`goto:${url}`); }, async reload() { actions.push("reload"); if (completed) reopened = true; }, }; const api: any = { origin: "https://qa.example.test", async get(route: string) { reads.push(route); if (route === `/api/issues/${s.blockerId}`) return structuredClone(blocker); - if (route === `/api/issues/${s.dependentId}`) return structuredClone(dependent); + if (route === `/api/issues/${s.dependentId}`) { + const task = structuredClone(dependent); + if (options.reopenedStale === "task-status" && stale("task-status")) task.status = "in_progress"; + if (options.reopenedStale === "locks" && stale("locks")) task.executionRunId = "run"; + return task; + } if (route === `/api/issues/${s.blockerId}/runs`) return []; - if (route === `/api/issues/${s.dependentId}/runs`) return runs().map(r => ({ runId: r.id })); - if (route.includes("/heartbeat-runs?")) return [...runs(), ...structuredClone(priorRuns)]; - if (route === "/api/heartbeat-runs/run") return runs()[0]; + if (route === `/api/issues/${s.dependentId}/runs`) return projectedRows(runs().map(r => ({ runId: r.id })), "task"); + if (route.includes("/heartbeat-runs?")) return projectedRows([...runs(), ...structuredClone(priorRuns)], "agent"); + if (route === "/api/heartbeat-runs/run") { + const run = runs()[0], source = projection.getStore(), stage = reopened ? "reopened" : "finished"; + if (!source) throw new Error("run_detail_projection_missing"); + if (source === "agent") { + agentReads[stage]++; + const lag = reopened ? options.reopenedLaggingAgentReads : options.laggingAgentReads; + if (agentReads[stage] <= (lag ?? 0)) run.status = "running"; + } + if ((options.reopenedStale === "both-runs" || options.reopenedStale === "task-run" && source === "task") + && stale(source)) run.status = "running"; + if (options.reopenedStale === "native-phase" && stale(source)) run.nativePhase = "running"; + if (options.reopenedStale === "run-facts" && stale(source)) run.startedAt = "2019-12-31T23:59:00Z"; + detailReads.push({ projection: source, stage, status: run.status }); + return run; + } if (route === `/api/issues/${s.blockerId}/activity`) return completed ? structuredClone(f.reopened.activity) : []; if (route === `/api/issues/${s.dependentId}/activity`) return activity(options.ownedActivity ?? 0, s.dependentId); if (route === `/api/companies/${s.companyId}/activity?agentId=${s.agentId}&limit=500`) { return options.missingActivity ? undefined : activity(options.agentActivity ?? 0, "prior-task", s.agentId); } if (route === `/api/companies/${s.companyId}/activity?limit=500`) return activity(options.unrelatedActivity ?? 0, "unrelated-task").slice(0, 500); - if (route === `/api/issues/${s.dependentId}/documents`) return completed ? [taskDocument()] : []; + if (route === `/api/issues/${s.dependentId}/documents`) { + if (options.reopenedStale === "document" && stale("document")) return []; + return completed ? [taskDocument()] : []; + } if (route.endsWith("/documents/dependency-proof")) return taskDocument(); if (route.endsWith("/revisions/revision")) return revision(); throw new Error(`unexpected_read:${route}`); @@ -115,7 +151,7 @@ async function fixture(premature = false, options: FixtureOptions = {}) { capabilities: ["company", "board-user", "task-write", "agent"], agent: { id: s.agentId, name: "Fixture agent", provider: s.provider, model: s.model, runtimeMode: "native" } } as JourneyContext; const session = { own: vi.fn(async resource => { owned.push(resource); }) } as unknown as TargetSession; const evidence = { checks: [] } as unknown as JourneyEvidence; - return { directory, context, session, evidence, actions, owned, writes, reads }; + return { directory, context, session, evidence, actions, owned, writes, reads, detailReads, staleReads }; } afterEach(() => { vi.useRealTimers(); controls.create.mockReset(); }); describe("dependency browser flow orchestration", () => { @@ -206,6 +242,7 @@ describe("dependency browser flow orchestration", () => { }); it.each([ ["empty history", {}], + ["agent detail still running after task detail completed", { laggingAgentReads: 2 }], ["completed earlier campaign runs", { priorRuns: 3 }], ["more than 100 earlier campaign runs", { priorRuns: 150 }], ["more than 500 unrelated company events", { unrelatedActivity: 800 }], @@ -218,6 +255,10 @@ describe("dependency browser flow orchestration", () => { { deadlineAt: Date.now() + 60_000, directory: f.directory }); await vi.advanceTimersByTimeAsync(11_000); await operation; expect(f.evidence.checks).toHaveLength(8); expect(f.evidence.checks.every(check => check.passed)).toBe(true); + if ("laggingAgentReads" in options) { + expect(f.detailReads.filter(read => read.projection === "agent" && read.status === "running")).toHaveLength(options.laggingAgentReads); + expect(f.detailReads.filter(read => read.projection === "task").every(read => read.status === "succeeded")).toBe(true); + } expect(f.writes).toEqual([]); expect(f.actions.indexOf("click:Blocked")).toBeLessThan(f.actions.indexOf("click:agent-choice")); expect(f.actions.indexOf("click:agent-choice")).toBeLessThan(f.actions.indexOf("click:Done")); @@ -235,6 +276,65 @@ describe("dependency browser flow orchestration", () => { expect(f.reads).not.toContain("/api/companies/company/activity?limit=500"); } finally { await rm(f.directory, { recursive: true, force: true }); } }); + it.each([2, Infinity])("waits for reopened-only agent lag (%s reads) within the original deadline", async lag => { + const f = await fixture(false, { reopenedLaggingAgentReads: lag }); + vi.useFakeTimers(); vi.setSystemTime(new Date("2020-01-01T00:00:00Z")); + const startedAt = Date.now(); + try { + const operation = taskDependencyResume(f.context, f.session, "journey-123-abcdef01", f.evidence, + { deadlineAt: startedAt + 13_000, directory: f.directory }); + const outcome = lag === Infinity + ? expect(operation).rejects.toMatchObject({ code: "dependency_reopen_deadline" }) + : expect(operation).resolves.toBeUndefined(); + await vi.advanceTimersByTimeAsync(13_000); await outcome; + const saved = JSON.parse(await readFile(path.join(f.directory, "task-dependency-observations.json"), "utf8")); + expect(saved.finished.dependentRuns[0].status).toBe("succeeded"); + expect(saved.finished.agentRuns[0].status).toBe("succeeded"); + expect(f.detailReads.filter(read => read.projection === "task").every(read => read.status === "succeeded")).toBe(true); + expect(f.detailReads.filter(read => read.projection === "agent" && read.stage === "reopened" && read.status === "running")).toHaveLength(lag === Infinity ? 3 : lag); + expect(saved.reopened.agentRuns[0].status).toBe(lag === Infinity ? "running" : "succeeded"); + expect(f.evidence.checks).toHaveLength(lag === Infinity ? 0 : 8); + expect(f.evidence.checks.every(check => check.passed)).toBe(true); + expect(f.writes).toEqual([]); + } finally { await rm(f.directory, { recursive: true, force: true }); } + }); + it.each((["task-run", "both-runs", "task-status", "document", "locks", "native-phase", "run-facts"] as const) + .flatMap(stale => [2, Infinity].map(lag => ({ stale, lag }))))( + "waits for reopened $stale staleness ($lag reads) within the original deadline", async ({ stale, lag }) => { + const f = await fixture(false, { reopenedStale: stale, reopenedStaleReads: lag }); + vi.useFakeTimers(); vi.setSystemTime(new Date("2020-01-01T00:00:00Z")); + try { + const operation = taskDependencyResume(f.context, f.session, "journey-123-abcdef01", f.evidence, + { deadlineAt: Date.now() + 13_000, directory: f.directory }); + const outcome = lag === Infinity + ? expect(operation).rejects.toMatchObject({ code: "dependency_reopen_deadline" }) + : expect(operation).resolves.toBeUndefined(); + await vi.advanceTimersByTimeAsync(13_000); await outcome; + const saved = JSON.parse(await readFile(path.join(f.directory, "task-dependency-observations.json"), "utf8")); + expect(saved.finished.dependentRuns[0].status).toBe("succeeded"); + expect(saved.finished.agentRuns[0].status).toBe("succeeded"); + expect(Object.values(f.staleReads)).not.toHaveLength(0); + expect(Object.values(f.staleReads).every(count => count === 3)).toBe(true); + expect(f.evidence.checks).toHaveLength(lag === Infinity ? 0 : 8); + expect(f.evidence.checks.every(check => check.passed)).toBe(true); + expect(saved.stage).toBe(lag === Infinity ? "reopen_result" : "complete"); + expect(f.writes).toEqual([]); + } finally { await rm(f.directory, { recursive: true, force: true }); } + }); + it("keeps a permanently conflicting agent projection failed at the original deadline", async () => { + const f = await fixture(false, { laggingAgentReads: Infinity }); + vi.useFakeTimers(); vi.setSystemTime(new Date("2020-01-01T00:00:00Z")); + try { + const operation = expect(taskDependencyResume(f.context, f.session, "journey-123-abcdef01", f.evidence, + { deadlineAt: Date.now() + 13_000, directory: f.directory })).rejects.toMatchObject({ code: "dependency_resume_deadline" }); + await vi.advanceTimersByTimeAsync(13_000); await operation; + const saved = JSON.parse(await readFile(path.join(f.directory, "task-dependency-observations.json"), "utf8")); + expect(saved.finished.dependentRuns[0].status).toBe("succeeded"); + expect(saved.finished.agentRuns[0].status).toBe("running"); + expect(f.evidence.checks).toEqual([]); + expect(f.writes).toEqual([]); + } finally { await rm(f.directory, { recursive: true, force: true }); } + }); it.each(["properties", "toggle"] as const)("opens a closed properties pane through its %s control before linking", async closedPanel => { const f = await fixture(false, { closedPanel }); vi.useFakeTimers(); vi.setSystemTime(new Date("2020-01-01T00:00:00Z")); try { diff --git a/tests/runner-e2e/product-journeys/task-dependency-resume.ts b/tests/runner-e2e/product-journeys/task-dependency-resume.ts index 98bc71e6a88..3bb2eeae9ba 100644 --- a/tests/runner-e2e/product-journeys/task-dependency-resume.ts +++ b/tests/runner-e2e/product-journeys/task-dependency-resume.ts @@ -7,7 +7,7 @@ import { HEARTBEAT_RUN_LIST_MAX_LIMIT } from "../../../packages/shared/src/const import { createTaskThroughUi } from "../user-actions.js"; import { createObservedReader } from "./observation-read.js"; import { openDocumentAuthoring } from "./task-document-flow.js"; -import { DEPENDENCY_IDLE_MS, dependencyNewAgentRuns, gradeTaskDependencyResume, type DependencyEvidence, type DependencyPoint, type DependencyScope } from "./task-dependency-resume-evidence.js"; +import { DEPENDENCY_IDLE_MS, dependencyNewAgentRuns, dependencyRunProjectionsAgree, gradeTaskDependencyResume, type DependencyEvidence, type DependencyPoint, type DependencyScope } from "./task-dependency-resume-evidence.js"; import { JourneyError, type JourneyContext, type JourneyEvidence, type TargetSession } from "./types.js"; type Row = Record; @@ -189,24 +189,32 @@ export async function taskDependencyResume(context: JourneyContext, session: Tar await setStatus(context, "Done"); await expect.poll(async () => (await read.get(`/api/issues/${blocker.issueId}`)).status).toBe("done"); observations.released = await observe(); + const settled = (point: DependencyPoint | undefined, expectedRun?: Row): point is DependencyPoint => Boolean(point + && point.dependent.status === "done" && point.dependentRuns.length === 1 + && point.dependentRuns[0]?.status === "succeeded" && point.dependentRuns[0]?.nativePhase === "committed" + && point.document && point.revision && dependencyRunProjectionsAgree(point, startingRuns, expectedRun) + && !point.dependent.executionRunId && !point.dependent.checkoutRunId); stage = "await_automatic_resume"; while (Date.now() < options.deadlineAt) { const point = await observe(); observations.finished = point; - if (point.dependent.status === "done" && point.dependentRuns.length === 1 && point.dependentRuns[0]?.status === "succeeded" - && point.dependentRuns[0]?.nativePhase === "committed" && point.document && point.revision - && !point.dependent.executionRunId && !point.dependent.checkoutRunId) break; + if (settled(point)) break; await new Promise(resolve => setTimeout(resolve, 1000)); } - if (!observations.finished || observations.finished.dependent.status !== "done" || observations.finished.dependentRuns[0]?.status !== "succeeded" - || observations.finished.dependentRuns[0]?.nativePhase !== "committed" || !observations.finished.document || !observations.finished.revision) { - throw new JourneyError("failed", "dependency_resume_deadline"); - } + if (!settled(observations.finished)) throw new JourneyError("failed", "dependency_resume_deadline"); + const finishedRun = observations.finished.dependentRuns[0]!; stage = "reopen_result"; await page.goto(url(dependentSaved.identifier ?? dependent.issueId), { waitUntil: "domcontentloaded" }); await page.reload(); await openDocumentAuthoring(context); const document = page.locator(`#document-${documentKey}`); await expect(document).toContainText(reference); observations.reopened = await observe(); + while (!settled(observations.reopened, finishedRun)) { + const remaining = options.deadlineAt - Date.now(); + if (remaining <= 0) throw new JourneyError("failed", "dependency_reopen_deadline"); + await new Promise(resolve => setTimeout(resolve, Math.min(1000, remaining))); + if (Date.now() >= options.deadlineAt) throw new JourneyError("failed", "dependency_reopen_deadline"); + observations.reopened = await observe(); + } observations.reopened.resultVisible = await document.isVisible() && (await document.textContent())?.includes(reference) === true; const checks = gradeTaskDependencyResume({ ...observations, scope } as DependencyEvidence); evidence.checks.push(...checks);