diff --git a/tests/runner-e2e/CATALOG-PRESERVATION.md b/tests/runner-e2e/CATALOG-PRESERVATION.md index 161cbc3a8ac..e721b9ce9c8 100644 --- a/tests/runner-e2e/CATALOG-PRESERVATION.md +++ b/tests/runner-e2e/CATALOG-PRESERVATION.md @@ -34,6 +34,21 @@ the real board mutation guard and exact company/task/comment/byte checks. Neithe overlay changes canonical messages, native/human counts, overall caps or an exhausted case's result. The production core fixes remain excluded. +The document and decision-audience overlay preserves exact public document +identity, revision and bytes through the shipped Plans control, and reads a +pending addressed decision through the actual authenticated recipient's feed. +It requires both the owner and recipient identities. Its measured final failures +remain unchanged and exhausted; source preservation gives them no new cycle. + +The outbound-email observer accepts ordinary standing-chat work with a null +parent only when the canonical source run, creator, public audit and real native +assignment admission agree. It also preserves the supported public issue-create +wake shape. The email task still links explicitly to that work and the exact +owned thread/endpoint; original draft, recipient, native send, provider receipt, +duplicate and truthful delivery checks remain. Actual authorized AgentMail +resources and input delivery are separate prerequisites, with no source-only +runtime pass or implicit credential-transfer permission. + Readiness is separate from importability. Declared unsupported fresh contexts, public capability/profile gaps and external resource/authorization requirements remain explicit. The preserved plugin configure/disable fixture still has a known diff --git a/tests/runner-e2e/chat-catalog-domains/internal-decision-search-observer.test.ts b/tests/runner-e2e/chat-catalog-domains/internal-decision-search-observer.test.ts new file mode 100644 index 00000000000..028f49fcf8d --- /dev/null +++ b/tests/runner-e2e/chat-catalog-domains/internal-decision-search-observer.test.ts @@ -0,0 +1,68 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import type { ChatCatalogContext } from "../chat-catalog-contract.js"; +const person = vi.hoisted(() => ({ id: "riley", request: vi.fn(), dispose: vi.fn(async () => {}) })); +vi.mock("./identity-auth.js", () => ({ createAuthenticatedPerson: async () => person, enrollPrerequisitePerson: async () => {} })); +vi.mock("./internal-privacy.js", () => ({ mainPerson: async () => "owner" })); +vi.mock("./internal-support.js", async importOriginal => ({ ...await importOriginal(), + observeInternal: async (_ctx: unknown, state: Record) => ({ companyId: "company", state }) })); +import { decisionSearchCases } from "./internal-decision-search.js"; + +function control() { + const records = new Map>(); let issueCount = 0, questionCount = 0; + const ownerFeed = { companyId: "company", items: [{ subject: { id: "hire" } }] }; + const resolverFeed = { companyId: "company", items: [{ subject: { id: "question-1" }, relatedIssue: { id: "task-1" }, resolverAudience: { addresseeUserId: "riley", effectiveResolverPolicy: "human_only" } }] }; + const api = { + baseURL: "https://fixture.test", + get: vi.fn(async (path: string) => { + if (path.endsWith("/attention?all=true&includeDismissed=true")) return ownerFeed; + if (path === "/api/approvals/hire") return records.get("hire"); + if (path.endsWith("/interactions")) return [...records.values()].filter(row => row.issueId === path.split("/")[3]); + throw new Error(`Unexpected public read ${path}`); + }), + post: vi.fn(async (path: string, body: Record) => { + if (path.endsWith("/projects")) return { id: "project", companyId: "company", ...body }; + if (path.endsWith("/issues")) return { id: `task-${++issueCount}`, companyId: "company", ...body }; + if (path.endsWith("/interactions")) { + const row = { id: `question-${++questionCount}`, companyId: "company", issueId: path.split("/")[3], status: "pending", effectiveResolverPolicy: "human_only", ...body }; + records.set(row.id, row); return row; + } + if (path.endsWith("/respond")) { + const id = path.split("/")[5]!; const row = { ...records.get(id), status: "answered", result: body, resolvedByUserId: "owner" }; + records.set(id, row); return row; + } + if (path.endsWith("/approvals")) { const row = { id: "hire", companyId: "company", status: "pending", ...body }; records.set("hire", row); return row; } + throw new Error(`Unexpected public mutation ${path}`); + }), + postSensitive: vi.fn(async () => ({ id: "probe", token: "synthetic-attempt-private-probe" })), + delete: vi.fn(async () => {}), + }; + const ctx = { input: { api, nonce: "fixture", privateSecrets: [], page: { context: () => ({ browser: () => ({}) }) }, fixtures: { company: { id: "company" }, agent: { id: "ceo" } } } } as unknown as ChatCatalogContext; + person.request.mockImplementation(async () => Response.json(resolverFeed)); + return { ctx, api, ownerFeed, resolverFeed, definition: decisionSearchCases[0]! }; +} +beforeEach(() => { + person.id = "riley"; person.request.mockReset(); person.dispose.mockClear(); + vi.stubGlobal("fetch", vi.fn(async (url: URL, options: RequestInit) => { + expect(url.href).toBe("https://fixture.test/api/companies/company/attention?all=true"); + expect(options.headers).toEqual({ Authorization: "Bearer synthetic-attempt-private-probe" }); + return new Response(null, { status: 403 }); + })); +}); +afterEach(() => vi.unstubAllGlobals()); +it("actual observer independently reads Riley's session without borrowing the current human API", async () => { + const c = control(); const state = await c.definition.setup(c.ctx); const writes = c.api.post.mock.calls.length; + const o = await c.definition.observe(c.ctx, state) as { visibleAttention: unknown; resolverAttention: unknown; resolverUserId: string; permissionStatus: number }; + expect(person.request).toHaveBeenCalledExactlyOnceWith("/api/companies/company/attention?all=true&includeDismissed=true", "GET"); + expect(c.api.get.mock.calls.filter(([path]) => path.includes("/attention"))).toEqual([["/api/companies/company/attention?all=true&includeDismissed=true"]]); + expect(o.visibleAttention).toEqual(c.ownerFeed); expect(o.resolverAttention).toEqual(c.resolverFeed); expect(o.resolverUserId).toBe("riley"); expect(o.permissionStatus).toBe(403); + expect(c.api.post.mock.calls).toHaveLength(writes); + await c.definition.cleanup?.(c.ctx, state); expect(person.dispose).toHaveBeenCalledOnce(); +}); +it.each(["owner-substitution", "different-person", "unauthenticated-feed"])("actual observer rejects %s", async kind => { + const c = control(); const state = await c.definition.setup(c.ctx); + if (kind === "owner-substitution") person.id = "owner"; + if (kind === "different-person") person.id = "other"; + if (kind === "unauthenticated-feed") person.request.mockResolvedValue(new Response(null, { status: 403 })); + await expect(c.definition.observe(c.ctx, state)).rejects.toThrow(); + await c.definition.cleanup?.(c.ctx, state); +}); diff --git a/tests/runner-e2e/chat-catalog-domains/internal-decision-search.test.ts b/tests/runner-e2e/chat-catalog-domains/internal-decision-search.test.ts index 591b52a4000..50c0b6ea4c1 100644 --- a/tests/runner-e2e/chat-catalog-domains/internal-decision-search.test.ts +++ b/tests/runner-e2e/chat-catalog-domains/internal-decision-search.test.ts @@ -2,13 +2,14 @@ import { describe, expect, it } from "vitest"; import { gradeDecisionSearch } from "./internal-decision-search.js"; export function validDecisionSearch() { const t = { id: "question-task", title: "Riley launch question", status: "backlog" }; const history = { id: "history-task", title: "Completed launch information", status: "backlog" }; - const hiring = { id: "hire", status: "pending", decidedByUserId: null }; const pendingQuestion = { id: "question", status: "pending", addresseeUserId: "riley" }; + const hiring = { id: "hire", companyId: "company", status: "pending", decidedByUserId: null }; const pendingQuestion = { id: "question", companyId: "company", issueId: t.id, status: "pending", addresseeUserId: "riley", effectiveResolverPolicy: "human_only" }; const completedQuestion = { id: "completed", status: "answered", resolvedByUserId: "owner", result: { answers: ["received"] } }; return { scenarioId: "inbox-search.show-decisions", origin: "https://fixture.test", companyId: "company", leadId: "ceo", state: { task: structuredClone(t), historyTask: structuredClone(history), hiring: structuredClone(hiring), pendingQuestion: structuredClone(pendingQuestion), completedQuestion: structuredClone(completedQuestion), ownerId: "owner", rileyId: "riley" }, tasks: [t, history], projects: [], goals: [], comments: [], activity: [] as Array<{ agentId: string; action: string }>, documents: [], revisions: [], reply: "Launch designer: pending hiring approval for you as the human operator.\nRiley launch question: waiting for Riley to answer; it is addressed to Riley.\nCompleted launch information: answered history, no action remains.", links: ["/RUN/approvals/hire", "/RUN/issues/question-task", "/RUN/issues/history-task"], humanQueries: [], runs: [], hiring, pendingQuestion, completedQuestion, - visibleAttention: { items: [{ subject: { id: "hire" } }, { subject: { id: "question" }, resolverAudience: { addresseeUserId: "riley", effectiveResolverPolicy: "human_only" } }] }, permissionStatus: 403 }; + visibleAttention: { companyId: "company", items: [{ subject: { id: "hire" } }] }, + resolverUserId: "riley", resolverAttention: { companyId: "company", items: [{ subject: { id: "question" }, relatedIssue: { id: t.id }, resolverAudience: { addresseeUserId: "riley", effectiveResolverPolicy: "human_only" } }] }, permissionStatus: 403 }; } describe("decision source and resolver calibration", () => { it("accepts pending hiring, Riley's question and answered history without acting", () => expect(gradeDecisionSearch(validDecisionSearch()).passed).toBe(true)); @@ -18,4 +19,18 @@ describe("decision source and resolver calibration", () => { it("rejects wrong source or foreign links", () => { const o = validDecisionSearch(); o.links[0] = "https://foreign.test/RUN/approvals/hire"; expect(gradeDecisionSearch(o).passed).toBe(false); }); it("rejects universal resolution claims", () => { const o = validDecisionSearch(); o.reply += "\nYou can approve every visible item."; expect(gradeDecisionSearch(o).passed).toBe(false); }); it("rejects changed completed answer and board-feed privilege substitution", () => { const o = validDecisionSearch(); o.completedQuestion.result.answers = ["changed"]; expect(gradeDecisionSearch(o).passed).toBe(false); const admin = validDecisionSearch(); admin.permissionStatus = 200; expect(gradeDecisionSearch(admin).passed).toBe(false); }); + it.each(["missing-resolver-feed", "missing-principal", "foreign-feed", "borrowed-user", "wrong-source", "wrong-task", "wrong-resolver", "wrong-policy", "current-user-leak", "foreign-question"])("rejects incorrect independent audience custody: %s", kind => { + const o = validDecisionSearch(); + if (kind === "missing-resolver-feed") Object.assign(o, { resolverAttention: undefined }); + if (kind === "missing-principal") { Object.assign(o.state, { rileyId: undefined }); Object.assign(o, { resolverUserId: undefined }); Object.assign(o.pendingQuestion, { addresseeUserId: undefined }); Object.assign(o.resolverAttention.items[0]!.resolverAudience, { addresseeUserId: undefined }); } + if (kind === "foreign-feed") o.resolverAttention.companyId = "foreign"; + if (kind === "borrowed-user") o.resolverUserId = "owner"; + if (kind === "wrong-source") o.resolverAttention.items[0]!.subject.id = "other-question"; + if (kind === "wrong-task") o.resolverAttention.items[0]!.relatedIssue.id = "other-task"; + if (kind === "wrong-resolver") o.resolverAttention.items[0]!.resolverAudience.addresseeUserId = "owner"; + if (kind === "wrong-policy") o.pendingQuestion.effectiveResolverPolicy = "not_creator"; + if (kind === "current-user-leak") o.visibleAttention.items.push({ subject: { id: "question" } }); + if (kind === "foreign-question") o.pendingQuestion.companyId = "foreign"; + expect(gradeDecisionSearch(o).passed).toBe(false); + }); }); diff --git a/tests/runner-e2e/chat-catalog-domains/internal-decision-search.ts b/tests/runner-e2e/chat-catalog-domains/internal-decision-search.ts index b74db57aa9f..e6b9ea065da 100644 --- a/tests/runner-e2e/chat-catalog-domains/internal-decision-search.ts +++ b/tests/runner-e2e/chat-catalog-domains/internal-decision-search.ts @@ -5,8 +5,9 @@ import { makeDefinition } from "./internal-definition.js"; import { createAuthenticatedPerson, enrollPrerequisitePerson } from "./identity-auth.js"; import { mainPerson } from "./internal-privacy.js"; import { checks, entity, hasLink, observation, observeInternal, project, sameTask, task, type Entity, type InternalObservation } from "./internal-support.js"; -interface Request extends Entity { addresseeUserId?: string; resolvedByUserId?: string; result?: unknown; payload?: unknown; decidedByUserId?: string | null } -interface DecisionObservation extends InternalObservation { hiring: Request; pendingQuestion?: Request; completedQuestion?: Request; visibleAttention: unknown; permissionStatus: number } +interface Request extends Entity { addresseeUserId?: string; resolvedByUserId?: string; result?: unknown; payload?: unknown; decidedByUserId?: string | null; effectiveResolverPolicy?: string } +interface DecisionObservation extends InternalObservation { hiring: Request; pendingQuestion?: Request; completedQuestion?: Request; visibleAttention: unknown; resolverAttention?: unknown; resolverUserId?: string; permissionStatus: number } +interface AttentionFeed { companyId?: string; items?: Array<{ subject?: { id?: string }; relatedIssue?: { id?: string }; resolverAudience?: { addresseeUserId?: string | null; effectiveResolverPolicy?: string } }> } function approvalLink(o: InternalObservation, id: string) { return o.links.some(href => { try { const u = new URL(href, o.origin); return u.origin === o.origin && u.pathname.split("/").slice(-2).join("/") === `approvals/${id}`; } catch { return false; } }); } @@ -18,12 +19,16 @@ export function gradeDecisionSearch(value: unknown) { const c = checks(); const o = observation(value) as DecisionObservation | undefined; c.add("actual-source-decision-state", !!o && !!o.hiring && !!o.pendingQuestion && !!o.completedQuestion && o.visibleAttention !== undefined, "Actual hiring, addressed question, answered history and human-visible attention records are required."); if (!o || !o.hiring || !o.pendingQuestion || !o.completedQuestion) return c.result(); - const feed = o.visibleAttention as { items?: Array<{ subject?: { id?: string }; resolverAudience?: { addresseeUserId?: string | null; effectiveResolverPolicy?: string } }> }; + const feed = o.visibleAttention as AttentionFeed | undefined; const resolverFeed = o.resolverAttention as AttentionFeed | undefined; const hire = o.state.hiring as Request; const pending = o.state.pendingQuestion as Request; const done = o.state.completedQuestion as Request; const t = o.state.task as Entity; const history = o.state.historyTask as Entity; - c.add("visible-sources-and-real-resolver", Array.isArray(feed?.items) && feed.items.some(row => row.subject?.id === hire?.id) - && feed.items.some(row => row.subject?.id === pending?.id && row.resolverAudience?.addresseeUserId === o.state.rileyId && row.resolverAudience?.effectiveResolverPolicy === "human_only") - && o.state.rileyId !== o.state.ownerId, "The real human-visible queue contains both pending sources and the server-evaluated audience names the other human as question resolver."); + c.add("visible-sources-and-real-resolver", feed?.companyId === o.companyId && Array.isArray(feed.items) && feed.items.some(row => row.subject?.id === hire?.id) + && !feed.items.some(row => row.subject?.id === pending?.id) && resolverFeed?.companyId === o.companyId && Array.isArray(resolverFeed.items) + && resolverFeed.items.some(row => row.subject?.id === pending?.id && row.relatedIssue?.id === t.id && row.resolverAudience?.addresseeUserId === o.state.rileyId && row.resolverAudience?.effectiveResolverPolicy === "human_only") + && typeof o.state.rileyId === "string" && o.state.rileyId.length > 0 && typeof o.state.ownerId === "string" && o.state.ownerId.length > 0 + && o.resolverUserId === o.state.rileyId && o.state.rileyId !== o.state.ownerId && o.hiring.companyId === o.companyId + && o.pendingQuestion.companyId === o.companyId && o.pendingQuestion.issueId === t.id && o.pendingQuestion.effectiveResolverPolicy === "human_only", + "The current person's queue retains the hiring decision and excludes Riley's addressed question; Riley's independently authenticated queue and the accessible public question bind its exact source and human resolver."); c.add("list-does-not-resolve", o.hiring.id === hire?.id && o.hiring.status === "pending" && !o.hiring.decidedByUserId && JSON.stringify(o.hiring.payload) === JSON.stringify(hire.payload) && o.pendingQuestion.id === pending?.id && o.pendingQuestion.status === "pending" && !o.pendingQuestion.result && JSON.stringify(o.pendingQuestion.payload) === JSON.stringify(pending.payload) && o.pendingQuestion.addresseeUserId === o.state.rileyId && o.completedQuestion.id === done?.id && o.completedQuestion.status === "answered" @@ -71,15 +76,18 @@ function showDecisions(): ChatCatalogCase { }, gradeDecisionSearch); return { ...base, authentication: "human-session", sourceFiles: [...base.sourceFiles, "chat-catalog-domains/internal-decision-search.ts", "chat-catalog-domains/internal-privacy.ts", "chat-catalog-domains/identity-auth.ts", "../../packages/shared/src/validators/issue.ts"], async observe(ctx, state): Promise { - if (!key) throw new Error("Missing attempt-private decision probe"); + if (!key || !riley || riley.id !== state.rileyId || riley.id === state.ownerId) throw new Error("Missing distinct authenticated decision resolver or attempt-private probe"); const o = await observeInternal(ctx, state); const root = `/api/companies/${o.companyId}`; - const [hiring, pendingRows, completedRows, visibleAttention] = await Promise.all([ + const [hiring, pendingRows, completedRows, visibleAttention, resolverAttention] = await Promise.all([ ctx.input.api.get(`/api/approvals/${entity(state, "hiring").id}`), ctx.input.api.get(`/api/issues/${entity(state, "task").id}/interactions`), ctx.input.api.get(`/api/issues/${entity(state, "historyTask").id}/interactions`), ctx.input.api.get(`${root}/attention?all=true&includeDismissed=true`), + (async () => { const response = await riley!.request(`${root}/attention?all=true&includeDismissed=true`, "GET"); + if (!response.ok) { await response.body?.cancel(); throw new Error("The actual addressed person's public decision queue is unavailable"); } + return response.json(); })(), ]); const url = new URL(`${root}/attention?all=true`, ctx.input.api.baseURL); const response = await fetch(url, { redirect: "error", signal: AbortSignal.timeout(15_000), headers: { Authorization: `Bearer ${key.token}` } }); await response.body?.cancel(); - return { ...o, hiring, pendingQuestion: pendingRows.find(row => row.id === entity(state, "pendingQuestion").id), completedQuestion: completedRows.find(row => row.id === entity(state, "completedQuestion").id), visibleAttention, permissionStatus: response.status }; + return { ...o, hiring, pendingQuestion: pendingRows.find(row => row.id === entity(state, "pendingQuestion").id), completedQuestion: completedRows.find(row => row.id === entity(state, "completedQuestion").id), visibleAttention, resolverAttention, resolverUserId: riley.id, permissionStatus: response.status }; }, async verifyResult(ctx, state, value) { const o = value as DecisionObservation; await base.verifyResult(ctx, state, value); const source = ctx.input.page.url(); diff --git a/tests/runner-e2e/chat-catalog-domains/internal-restore-concurrency.ts b/tests/runner-e2e/chat-catalog-domains/internal-restore-concurrency.ts index 86218ee7a01..2d2b8f50161 100644 --- a/tests/runner-e2e/chat-catalog-domains/internal-restore-concurrency.ts +++ b/tests/runner-e2e/chat-catalog-domains/internal-restore-concurrency.ts @@ -2,6 +2,7 @@ import { expect } from "@playwright/test"; import { randomUUID } from "node:crypto"; import type { ChatCatalogCase, ChatCatalogContext } from "../chat-catalog-contract.js"; import { pollUntil } from "../api.js"; +import { projectMarkdownToText } from "../../../packages/shared/src/document-anchors.js"; import { createAuthenticatedPerson, enrollPrerequisitePerson } from "./identity-auth.js"; import { checks, entity, observation, observeInternal, sameTask, hasLink, type Entity, type InternalObservation } from "./internal-support.js"; interface Revision extends Entity { createdByUserId?: string | null } @@ -93,13 +94,18 @@ export function withRestoreConcurrency(primary: ChatCatalogCase): ChatCatalogCas return { ...o, phaseReply: comments.filter(row => row.authorAgentId === o.leadId && !ids.has(row.createdByRunId ?? "") && o.runs.some(run => run.id === row.createdByRunId)).map(row => row.body ?? "").join("\n") }; }, async verifyResult(ctx, state, value) { const o = value as ConcurrentRestore; const t = entity(state, "task"); const doc = o.documents.find(row => row.key === "plan"); - if (!doc?.latestRevisionNumber) throw new Error("Missing actual current document revision"); const source = ctx.input.page.url(); + if (!doc?.latestRevisionNumber || typeof doc.body !== "string") throw new Error("Missing actual current document revision"); const source = ctx.input.page.url(); + const current = await ctx.input.api.get(`/api/issues/${t.id}/documents/plan`); + if (current.id !== doc.id || current.issueId !== t.id || current.key !== "plan" || current.latestRevisionId !== doc.latestRevisionId + || current.latestRevisionNumber !== doc.latestRevisionNumber || current.body !== doc.body) throw new Error("Current task document identity, revision or bytes changed before UI verification"); const returned = o.links.find(href => hasLink({ ...o, links: [href] }, "issues", t)); try { - await ctx.input.page.goto(new URL(returned ?? `/${ctx.input.fixtures.company.issuePrefix}/issues/${t.identifier ?? t.id}`, o.origin).href, { waitUntil: "domcontentloaded" }); + const target = new URL(returned ?? `/${ctx.input.fixtures.company.issuePrefix}/issues/${t.identifier ?? t.id}`, o.origin); target.hash = "#document-plan"; + await ctx.input.page.goto(target.href, { waitUntil: "domcontentloaded" }); await expect(ctx.input.page.getByRole("heading", { name: t.title!, exact: true }).first()).toBeVisible(); - const expand = ctx.input.page.getByRole("button", { name: "Expand plan document", exact: true }); if (await expand.isVisible()) await expand.click(); - await expect(ctx.input.page.getByRole("button", { name: `rev ${doc.latestRevisionNumber}`, exact: true }).first()).toBeVisible(); + const plan = ctx.input.page.getByTestId("issue-plan-document"); await expect(plan).toBeVisible(); + await expect(plan.getByText(new RegExp(`^Revision ${doc.latestRevisionNumber}\\b`)).first()).toBeVisible(); + await expect(plan).toContainText(projectMarkdownToText(doc.body).text); await ctx.input.evidence("chat-catalog-concurrent-plan-current-revision-opened.json", { taskId: t.id, revisionId: doc.latestRevisionId, revisionNumber: doc.latestRevisionNumber, url: ctx.input.page.url() }); } finally { await ctx.input.page.goto(source, { waitUntil: "domcontentloaded" }); await expect(ctx.input.page.getByTestId("task-chat-composer-input")).toBeVisible(); } }, diff --git a/tests/runner-e2e/chat-catalog-domains/internal-restore-ui.test.ts b/tests/runner-e2e/chat-catalog-domains/internal-restore-ui.test.ts new file mode 100644 index 00000000000..553e35952c1 --- /dev/null +++ b/tests/runner-e2e/chat-catalog-domains/internal-restore-ui.test.ts @@ -0,0 +1,117 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { createRequire } from "node:module"; +import { readFileSync } from "node:fs"; +import { runInNewContext } from "node:vm"; +import { transformSync } from "esbuild"; +import type { ChatCatalogCase, ChatCatalogContext } from "../chat-catalog-contract.js"; +import type { Entity, InternalObservation } from "./internal-support.js"; +import { withRestoreConcurrency } from "./internal-restore-concurrency.js"; +import { ISSUE_CONTINUATION_SUMMARY_DOCUMENT_KEY } from "../../../packages/shared/src/constants.js"; + +const uiRequire = createRequire(new URL("../../../ui/package.json", import.meta.url)); +const { createElement } = uiRequire("react"); +const { renderToStaticMarkup } = uiRequire("react-dom/server"); +const Markdown = uiRequire("react-markdown").default; +const { JSDOM } = createRequire(uiRequire.resolve("vitest/package.json"))("jsdom"); +function uiModule(file: string, require: (name: string) => unknown) { + const source = readFileSync(new URL(`../../../ui/src/${file}`, import.meta.url), "utf8"); + const module = { exports: {} as Record unknown> }; + runInNewContext(transformSync(source, { loader: "tsx", jsx: "automatic", format: "cjs" }).code, { module, exports: module.exports, require, URL, URLSearchParams }); + return module.exports; +} +const annotationHash = uiModule("lib/document-annotation-hash.ts", name => { throw new Error(`Unexpected hash dependency ${name}`); }); +const { resolveIssueDocumentDeepLink } = uiModule("lib/issue-document-deep-link.ts", name => { + if (name === "@paperclipai/shared") return { ISSUE_CONTINUATION_SUMMARY_DOCUMENT_KEY }; + if (name === "./document-annotation-hash") return annotationHash; + throw new Error(`Unexpected document route dependency ${name}`); +}) as unknown as { resolveIssueDocumentDeepLink(hash: string): { kind: string; documentKey?: string } | null }; +const fixture = { document: {} as Record }; +const planSource = readFileSync(new URL("../../../ui/src/components/issue-properties/IssuePropertiesPlansTab.tsx", import.meta.url), "utf8"); +const compiledPlans = transformSync(planSource, { loader: "tsx", jsx: "automatic", format: "cjs" }).code; +const plansModule = { exports: {} as { IssuePropertiesPlansTab?: unknown } }; +runInNewContext(compiledPlans, { module: plansModule, exports: plansModule.exports, require: (name: string) => { + if (name === "react" || name === "react/jsx-runtime") return uiRequire(name); + if (name === "@tanstack/react-query") return { useQuery: () => ({ data: [] }) }; + if (name === "@/api/issues") return { issuesApi: {} }; + if (name === "@/lib/queryKeys") return { queryKeys: { issues: { acceptedPlanDecompositions: () => [], interactions: () => [] } } }; + if (name === "@/components/IssuePlanDecompositionsSection") return { IssuePlanDecompositionsSection: () => null }; + if (name === "@/components/MarkdownBody") return { MarkdownBody: ({ children }: { children: string }) => createElement(Markdown, {}, children) }; + if (name === "@/components/IssueDocumentAnnotations") return { DocumentAnnotationsCountChip: () => null, IssueDocumentAnnotations: ({ children }: { children: unknown }) => children }; + if (name === "@/hooks/useIssuePlanDocument") return { useIssuePlanDocument: () => ({ data: fixture.document, isLoading: false }) }; + if (name === "@/lib/router") return { useLocation: () => ({ hash: "#document-plan" }) }; + throw new Error(`Unexpected Plans render dependency ${name}`); +} }); +const IssuePropertiesPlansTab = plansModule.exports.IssuePropertiesPlansTab; +vi.mock("@playwright/test", () => ({ expect: (locator: Locator) => ({ + toBeVisible: async () => expect(await locator.isVisible()).toBe(true), + toContainText: async (text: string) => expect(locator.text().replace(/\s+/g, " ")).toContain(text.replace(/\s+/g, " ")), +}) })); + +class Locator { + constructor(private readonly nodes: Element[]) {} + first() { return new Locator(this.nodes.slice(0, 1)); } + async isVisible() { return this.nodes.some(node => !node.closest("[hidden]")); } + text() { return this.nodes.map(node => node.textContent ?? "").join(" "); } + getByText(text: RegExp) { return new Locator(this.nodes.flatMap(node => Array.from(node.querySelectorAll("*"))).filter(node => text.test(node.textContent ?? ""))); } + async click() { throw new Error("No classic document controls exist in the real Plans surface"); } +} +function pageFor(body = "# Plan\nBuild accessible signup.\nVerify keyboard operation.", revision = 7) { + fixture.document = { id: "doc", issueId: "task", key: "plan", body, latestRevisionId: "v7", latestRevisionNumber: revision, updatedAt: "2026-10-26T12:00:00Z" }; + const plan = renderToStaticMarkup(createElement(IssuePropertiesPlansTab, { issue: { id: "task" } as never })); + let url = "https://fixture.test/RUN/issues/chat"; + let dom: { window: { document: Document } } = new JSDOM(''); + const navigations: string[] = []; + return { + navigations, + page: { + url: () => url, + goto: async (next: string) => { + url = next; navigations.push(next); + const target = new URL(next); + const route = resolveIssueDocumentDeepLink(target.hash); + dom = new JSDOM(target.pathname.endsWith("/RUN-1") + ? `

Website task

${route?.kind === "properties-pane" && route.documentKey === "plan" ? plan : ""}` + : ''); + }, + getByRole: (role: string, options: { name: string }) => new Locator(Array.from(dom.window.document.querySelectorAll(role === "heading" ? "h1,h2,h3" : "button")).filter(node => node.textContent === options.name)), + getByTestId: (id: string) => new Locator(Array.from(dom.window.document.querySelectorAll(`[data-testid="${id}"]`))), + }, + }; +} +function control(options: { document?: Partial; renderedBody?: string; renderedRevision?: number; observationDocument?: Partial } = {}) { + const view = pageFor(options.renderedBody, options.renderedRevision); + const current = { ...fixture.document, ...options.document }; + const o = { origin: "https://fixture.test", documents: [{ ...fixture.document, ...options.observationDocument }], links: ["/RUN/issues/RUN-1"] } as unknown as InternalObservation; + const state = { task: { id: "task", identifier: "RUN-1", title: "Website task" } }; + const api = { get: vi.fn(async () => current) }, evidence = vi.fn(async () => {}); + const ctx = { input: { page: view.page, api, evidence, fixtures: { company: { issuePrefix: "RUN" } } } } as unknown as ChatCatalogContext; + const primary = { sourceFiles: [], grade: () => ({ passed: true, checks: [] }) } as unknown as ChatCatalogCase; + return { ...view, api, evidence, verify: () => withRestoreConcurrency(primary).verifyResult(ctx, state, o) }; +} +beforeEach(() => vi.stubGlobal("fetch", () => { throw new Error("No network permitted in pure restore UI controls"); })); +afterEach(() => vi.unstubAllGlobals()); +describe("actual supported Plans restoration verification", () => { + it("opens the real Plans surface with its saved revision and rendered markdown", async () => { + const c = control(); await c.verify(); + expect(c.navigations[0]).toBe("https://fixture.test/RUN/issues/RUN-1#document-plan"); + expect(c.navigations.at(-1)).toBe("https://fixture.test/RUN/issues/chat"); + expect(c.api.get).toHaveBeenCalledWith("/api/issues/task/documents/plan"); + expect(c.evidence).toHaveBeenCalledOnce(); + }); + it.each([ + { document: { id: "other-doc" } }, { document: { issueId: "other-task" } }, + { document: { key: "brief" } }, { document: { latestRevisionId: "old" } }, + { document: { latestRevisionNumber: 6 } }, { document: { body: "Wrong current bytes" } }, + ])("rejects wrong public document/revision/bytes %j", async options => { + const c = control(options); await expect(c.verify()).rejects.toThrow(); expect(c.evidence).not.toHaveBeenCalled(); + }); + it("rejects a stale rendered revision despite matching saved API identity", async () => { + const c = control({ renderedRevision: 6, observationDocument: { latestRevisionNumber: 7 }, document: { latestRevisionNumber: 7 } }); + await expect(c.verify()).rejects.toThrow(); expect(c.evidence).not.toHaveBeenCalled(); + }); + it("rejects wrong rendered body despite matching saved API bytes", async () => { + const body = "# Plan\nThe saved correct restoration."; + const c = control({ renderedBody: "# Plan\nDifferent document body.", observationDocument: { body }, document: { body } }); + await expect(c.verify()).rejects.toThrow(); expect(c.evidence).not.toHaveBeenCalled(); + }); +}); diff --git a/tests/runner-e2e/chat-catalog-domains/internal.test.ts b/tests/runner-e2e/chat-catalog-domains/internal.test.ts index ef8db9e9f10..414d053d586 100644 --- a/tests/runner-e2e/chat-catalog-domains/internal.test.ts +++ b/tests/runner-e2e/chat-catalog-domains/internal.test.ts @@ -84,11 +84,12 @@ function valid(scenario: string): InternalObservation { o.links = ["/ACORN/activity/commentary"]; o.reply = "I cannot browse board-only commentary. You can inspect the attributed feedback in the human commentary view."; break; } case "inbox-search.show-decisions": { - const pending = { id: "question", status: "pending", addresseeUserId: "riley" }; const completed = { id: "complete", status: "answered", resolvedByUserId: "owner", result: { answers: ["received"] } }; - const history = { id: "history", title: "Completed launch information", status: "backlog" }; const hiring = { id: "hire", status: "pending" }; + const pending = { id: "question", companyId: o.companyId, issueId: o.tasks[0]!.id, status: "pending", addresseeUserId: "riley", effectiveResolverPolicy: "human_only" }; const completed = { id: "complete", status: "answered", resolvedByUserId: "owner", result: { answers: ["received"] } }; + const history = { id: "history", title: "Completed launch information", status: "backlog" }; const hiring = { id: "hire", companyId: o.companyId, status: "pending" }; o.tasks[0]!.title = "Riley launch question"; o.state.task = structuredClone(o.tasks[0]); o.tasks.push(history); Object.assign(o.state, { ownerId: "owner", rileyId: "riley", historyTask: structuredClone(history), hiring: structuredClone(hiring), pendingQuestion: structuredClone(pending), completedQuestion: structuredClone(completed) }); - Object.assign(o, { hiring, pendingQuestion: pending, completedQuestion: completed, visibleAttention: { items: [{ subject: { id: "hire" } }, { subject: { id: "question" }, resolverAudience: { addresseeUserId: "riley", effectiveResolverPolicy: "human_only" } }] }, permissionStatus: 403 }); + Object.assign(o, { hiring, pendingQuestion: pending, completedQuestion: completed, visibleAttention: { companyId: o.companyId, items: [{ subject: { id: "hire" } }] }, resolverUserId: "riley", + resolverAttention: { companyId: o.companyId, items: [{ subject: { id: "question" }, relatedIssue: { id: o.tasks[0]!.id }, resolverAudience: { addresseeUserId: "riley", effectiveResolverPolicy: "human_only" } }] }, permissionStatus: 403 }); o.links.push("/ACORN/issues/history", "/ACORN/approvals/hire"); o.reply = "Launch designer: pending approval for you as human operator.\nRiley launch question: waiting for Riley to answer.\nCompleted launch information: answered history, no action remains."; break; } diff --git a/tests/runner-e2e/chat-catalog-domains/runtime-email-outbound.test.ts b/tests/runner-e2e/chat-catalog-domains/runtime-email-outbound.test.ts index bf5d3602b26..b12893e780a 100644 --- a/tests/runner-e2e/chat-catalog-domains/runtime-email-outbound.test.ts +++ b/tests/runner-e2e/chat-catalog-domains/runtime-email-outbound.test.ts @@ -1,8 +1,113 @@ -import { expect, it } from "vitest"; -import { gradeOutboundEmail, truthfulEmailOutcome } from "./runtime-email-outbound.js"; +import { expect, it, vi } from "vitest"; +import { gradeOutboundEmail, truthfulEmailOutcome, runtimeOutboundEmailCases, outboundOrdinaryHandoff, outboundThreadMatches } from "./runtime-email-outbound.js"; function valid() { return { companyId: "company", leadId: "ceo", scenarioId: "chat-channels.outbound-email", origin: "http://127.0.0.1:42123", reply: "", agents: [], tasks: [], activity: [], links: [], humanQueries: [], runs: [1,2,3,4,5].map(id => ({ id: String(id), agentId: "ceo", status: "succeeded" })), state: { actualHumanDraftBound: true, actualDraftTransmitted: true, exactAuthorizedRecipient: true, feedbackRequested: true, normalTaskBound: true, actualNativeSendBound: true, nativeLedgerCausal: true, independentProviderCount: 1, originalProviderReceiptMatched: true, authenticDuplicateBound: true, duplicateReleased: true, finalIndependentProviderCount: 1, oneSavedPublication: true, actualThreadLinked: true, factualReplyBound: true, factualReply: "The provider accepted the original launch email; its conversation and message receipt are saved with the task. Recipient delivery remains unconfirmed.", actualOutcome: "sent" } }; } it("accepts actual authorized draft, delegated native send, authentic duplicate and one physical provider message", () => expect(gradeOutboundEmail(valid()).passed).toBe(true)); it("accepts substantive natural outcome paraphrases without requiring a fixed URL or wording", () => { const o = valid(); o.state.factualReply = "Your launch draft was sent for review and the original provider message is recorded. I cannot confirm whether it was delivered."; expect(gradeOutboundEmail(o).passed).toBe(true); }); it("rejects inaccessible/wrong draft, wrong recipient and an acknowledgement without task-native send", () => { for (const field of ["actualDraftTransmitted", "exactAuthorizedRecipient", "actualNativeSendBound", "nativeLedgerCausal"]) { const o = valid(); (o.state as Record)[field] = false; expect(gradeOutboundEmail(o).passed).toBe(false); } }); it("rejects forged/unobserved duplicate proof or an extra physical provider message", () => { const o = valid(); o.state.authenticDuplicateBound = false; expect(gradeOutboundEmail(o).passed).toBe(false); const p = valid(); p.state.finalIndependentProviderCount = 2; expect(gradeOutboundEmail(p).passed).toBe(false); }); it("distinguishes actual recipient delivery from accepted send and requires the correct factual reply phase", () => { expect(truthfulEmailOutcome("It was sent and was delivered.", "sent")).toBe(false); expect(truthfulEmailOutcome("It was sent and was delivered.", "delivered")).toBe(true); const o = valid(); o.state.factualReplyBound = false; expect(gradeOutboundEmail(o).passed).toBe(false); }); + +const stopAfterObservation = new Error("stopped after the actual outbound observer load"); +const poll = vi.hoisted(() => ({ observed: undefined as unknown })); +vi.mock("../api.js", async original => ({ ...await original(), pollUntil: async (input: { load(): Promise }) => { + poll.observed = await input.load(); throw stopAfterObservation; +} })); +function actualHandoff() { + const companyId = "company", sourceId = "chat", leadId = "ceo", canonicalId = "canonical"; + const work = { id: "work", companyId, parentId: null, workMode: "standard", assigneeAgentId: leadId, createdByAgentId: leadId, + status: "done", originKind: "manual", originRunId: canonicalId, originId: "paperclip-runner:create-task:chat:launch", originFingerprint: "a".repeat(64) }; + const runs = [{ id: "setup", companyId, agentId: leadId, runtimeMode: "native", status: "succeeded", contextSnapshot: { issueId: sourceId, wakeCommentIds: ["draft"] } }, + { id: canonicalId, companyId, agentId: leadId, runtimeMode: "native", status: "succeeded", contextSnapshot: { issueId: sourceId, wakeCommentIds: ["canonical-human"] } }, + { id: "work-run", companyId, agentId: leadId, issueId: work.id, wakeupRequestId: "assignment-wakeup", runtimeMode: "native", status: "succeeded", contextSnapshot: { issueId: work.id, source: "paperclip_runner.create_task", wakeSource: "assignment", wakeReason: "issue_assigned", parentIssueId: null } }, + { id: "completion", companyId, agentId: leadId, runtimeMode: "native", status: "succeeded", contextSnapshot: { issueId: sourceId, chatCompletionUpdates: [{ id: work.id, status: "done" }] } }]; + const activity = [{ id: "created", companyId, entityId: work.id, entityType: "issue", action: "issue.created", actorType: "agent", actorId: leadId, agentId: leadId, runId: canonicalId, details: { source: "paperclip_runner_protocol" } }]; + const email = { id: "email-task", companyId, parentId: work.id, assigneeAgentId: leadId }; + const thread = { issueId: email.id, conversationId: "thread", endpoint: { id: "endpoint", companyId, assignedAgentId: leadId, status: "active" }, publications: [{ id: "publication", outcome: "sent" }] }; + return { companyId, sourceId, leadId, work, runs, activity, email, thread }; +} +async function observeActualHandoff(input = actualHandoff()) { + const reads: string[] = []; + const api = { get: async (url: string) => { reads.push(url); + if (url === `/api/companies/${input.companyId}/issues`) return [input.work, input.email]; + if (url === `/api/companies/${input.companyId}/activity?limit=1000`) return input.activity; + if (url === `/api/issues/${input.sourceId}/comments?order=asc`) return [{ id: "draft", authorUserId: "person" }, { id: "canonical-human", authorUserId: "person" }]; + if (url === `/api/companies/${input.companyId}/email/tasks/${input.email.id}`) return input.thread; + throw new Error(`Unexpected source-only public read: ${url}`); + } }; + const ctx = { input: { api, fixtures: { company: { id: input.companyId }, agent: { id: input.leadId } } }, + refreshIssue: async () => undefined, allRuns: async () => input.runs }; + await expect(runtimeOutboundEmailCases[0]!.act!(ctx as never, { sourceId: input.sourceId, personId: "person", setupRunId: "setup", endpoint: { id: "endpoint" } })).rejects.toBe(stopAfterObservation); + return { value: poll.observed as { work?: { id: string }; thread?: { issueId: string } }, reads }; +} +it("actual outbound observer accepts a public standing-chat native handoff whose ordinary work has no parent", async () => { + const { value } = await observeActualHandoff(); expect(value.work?.id).toBe("work"); expect(value.thread?.issueId).toBe("email-task"); +}); + +function pureHandoff(input = actualHandoff()) { return outboundOrdinaryHandoff({ ...input, personId: "person", setupRunId: "setup", tasks: [input.work, input.email], + comments: [{ id: "draft", authorUserId: "person" }, { id: "canonical-human", authorUserId: "person" }] }); } +it("rejects wrong originating actor, company, canonical run, idempotency and missing public audit evidence", () => { + for (const mutate of [(v: ReturnType) => { v.work.createdByAgentId = "foreign"; }, (v: ReturnType) => { v.work.companyId = "foreign"; }, + (v: ReturnType) => { v.work.originRunId = "setup"; }, (v: ReturnType) => { v.work.originId = "paperclip-runner:create-task:other:launch"; }, (v: ReturnType) => { v.work.originFingerprint = "unbound"; }, + (v: ReturnType) => { v.activity[0]!.actorId = "foreign"; }, (v: ReturnType) => { v.activity[0]!.runId = "setup"; }, (v: ReturnType) => { v.activity[0]!.entityId = "other"; }, (v: ReturnType) => { v.activity.length = 0; }]) { + const input = actualHandoff(); mutate(input); expect(pureHandoff(input)).toBeUndefined(); + } +}); +it("rejects wrong or absent actual native work admission and a different canonical human/source", () => { + for (const mutate of [(v: ReturnType) => { v.runs[2]!.agentId = "foreign"; }, (v: ReturnType) => { v.runs[2]!.companyId = "foreign"; }, + (v: ReturnType) => { v.runs[2]!.issueId = "other"; }, (v: ReturnType) => { v.runs[2]!.wakeupRequestId = ""; }, (v: ReturnType) => { v.runs[2]!.runtimeMode = "legacy"; }, + (v: ReturnType) => { v.runs[2]!.contextSnapshot.wakeReason = "unrelated"; }, (v: ReturnType) => { v.runs[1]!.contextSnapshot.issueId = "other"; }, + (v: ReturnType) => { v.runs[1]!.contextSnapshot.wakeCommentIds = ["draft"]; }, (v: ReturnType) => { v.runs.splice(2, 1); }]) { + const input = actualHandoff(); mutate(input); expect(pureHandoff(input)).toBeUndefined(); + } +}); +it("does not accept missing principal identity or a third human question as canonical delegation", () => { + const input = actualHandoff(), base = { ...input, personId: "person", setupRunId: "setup", tasks: [input.work], comments: [{ id: "draft", authorUserId: "person" }, { id: "canonical-human", authorUserId: "person" }] }; + expect(outboundOrdinaryHandoff({ ...base, personId: "" })).toBeUndefined(); + expect(outboundOrdinaryHandoff({ ...base, comments: [...base.comments, { id: "third", authorUserId: "person" }] })).toBeUndefined(); + expect(outboundOrdinaryHandoff({ ...base, comments: [base.comments[0]!, { ...base.comments[1]!, authorUserId: "foreign" }] })).toBeUndefined(); +}); +it("requires one unique task/run provenance join and keeps the explicit email-child/thread binding", () => { + const v = actualHandoff(), base = { ...v, personId: "person", setupRunId: "setup", tasks: [v.work], comments: [{ id: "draft", authorUserId: "person" }, { id: "canonical-human", authorUserId: "person" }] }; + expect(pureHandoff(v)?.work.id).toBe(v.work.id); + expect(outboundOrdinaryHandoff({ ...base, runs: [...v.runs, { ...v.runs[2]!, id: "other-admission" }] })).toBeUndefined(); + expect(outboundThreadMatches(v.thread as never, v.email, v.work.id, v.companyId, v.leadId, "endpoint")).toBe(true); + for (const mutate of [(i: ReturnType) => { i.email.parentId = i.sourceId; }, (i: ReturnType) => { i.thread.issueId = "other"; }, + (i: ReturnType) => { i.thread.endpoint.companyId = "foreign"; }, (i: ReturnType) => { i.thread.endpoint.assignedAgentId = "foreign"; }, (i: ReturnType) => { i.thread.endpoint.id = "other"; }]) { + const bad = actualHandoff(); mutate(bad); expect(outboundThreadMatches(bad.thread as never, bad.email, bad.work.id, bad.companyId, bad.leadId, "endpoint")).toBe(false); + } +}); +it("actual callback does not select a wrong originating run or unrelated email thread", async () => { + const wrong = actualHandoff(); wrong.work.originRunId = "setup"; expect((await observeActualHandoff(wrong)).value.work).toBeUndefined(); + const thread = actualHandoff(); thread.thread.issueId = "other"; expect((await observeActualHandoff(thread)).value.thread).toBeUndefined(); +}); + +it("accepts a supported current-source child only with the same provenance and rejects a foreign parent", () => { + const child = actualHandoff(); (child.work as { parentId: string | null }).parentId = child.sourceId; + (child.runs[2]!.contextSnapshot as { parentIssueId: string | null }).parentIssueId = child.sourceId; + expect(pureHandoff(child)?.work.id).toBe(child.work.id); + (child.work as { parentId: string | null }).parentId = "unrelated-parent"; + (child.runs[2]!.contextSnapshot as { parentIssueId: string | null }).parentIssueId = "unrelated-parent"; + expect(pureHandoff(child)).toBeUndefined(); +}); + +it("accepts the supported public issue-create assignment producer with canonical actor/run audit even without protocol-only idempotency fields", () => { + const input = actualHandoff(); input.work.originId = ""; input.work.originFingerprint = ""; + input.runs[2]!.contextSnapshot.source = "issue.create"; input.activity[0]!.details = {} as { source: string }; + expect(pureHandoff(input)?.work.id).toBe("work"); +}); +it("does not mix a public-API wake with protocol audit or accept an unknown task producer", () => { + const mixed = actualHandoff(); mixed.runs[2]!.contextSnapshot.source = "issue.create"; expect(pureHandoff(mixed)).toBeUndefined(); + const unknown = actualHandoff(); unknown.runs[2]!.contextSnapshot.source = "unrelated"; expect(pureHandoff(unknown)).toBeUndefined(); +}); + +it("accepts the actual issue.create wake projection which does not publish protocol parentIssueId", () => { + const input = actualHandoff(); input.work.originId = ""; input.work.originFingerprint = ""; + input.runs[2]!.contextSnapshot.source = "issue.create"; input.activity[0]!.details = {} as { source: string }; + delete (input.runs[2]!.contextSnapshot as Record).parentIssueId; + expect(pureHandoff(input)?.work.id).toBe("work"); +}); + +it("still rejects a native protocol wake with a mismatched parent even for the correct saved ordinary task", () => { + const input = actualHandoff(); (input.runs[2]!.contextSnapshot as { parentIssueId: string | null }).parentIssueId = input.sourceId; + expect(pureHandoff(input)).toBeUndefined(); +}); diff --git a/tests/runner-e2e/chat-catalog-domains/runtime-email-outbound.ts b/tests/runner-e2e/chat-catalog-domains/runtime-email-outbound.ts index e52aa9cf4c2..af0130b8498 100644 --- a/tests/runner-e2e/chat-catalog-domains/runtime-email-outbound.ts +++ b/tests/runner-e2e/chat-catalog-domains/runtime-email-outbound.ts @@ -2,7 +2,7 @@ import { createHash, randomUUID } from "node:crypto"; import { expect } from "@playwright/test"; import type { EmailPublicationSummary, EmailThreadSummary } from "../../../packages/shared/src/types/email.js"; import type { ChatCatalogContext } from "../chat-catalog-contract.js"; -import { sendChatMessage } from "../chat-flow.js"; +import { sendChatMessage, type ChatRun } from "../chat-flow.js"; import { pollUntil } from "../api.js"; import { collectRunEvents } from "../run-observations.js"; import { checks, type Entity } from "./internal-support.js"; @@ -22,6 +22,41 @@ export function gradeOutboundEmail(value: unknown) { c.add("linked-task-thread-and-truthful-reported-outcome", s.actualThreadLinked === true && s.factualReplyBound === true && truthfulEmailOutcome(String(s.factualReply), String(s.actualOutcome)), "The saved returned conversation remains linked to the delegated task; a later run-attributed chat answer reports real acceptance and distinguishes unproven delivery."); return c.result(); } +type OutboundTask = Entity & { workMode?: string; originKind?: string; originId?: string | null; originRunId?: string | null; originFingerprint?: string | null }; +type OutboundRun = ChatRun & { issueId?: string | null; wakeupRequestId?: string | null }; +type OutboundActivity = Entity & { runId?: string | null; entityType?: string }; +/** Standing-chat create_task creates ordinary work with parentId=null. Bind its actual public provenance, not an invented hierarchy. */ +export function outboundOrdinaryHandoff(input: { companyId: string; sourceId: string; leadId: string; personId: string; setupRunId: string; + tasks: OutboundTask[]; runs: OutboundRun[]; comments: Entity[]; activity: OutboundActivity[] }) { + const { companyId, sourceId, leadId, personId, setupRunId, tasks, runs, comments, activity } = input; + if (![companyId, sourceId, leadId, personId, setupRunId].every(value => typeof value === "string" && value.length > 0)) return undefined; + const human = comments.filter(row => !row.authorAgentId); + if (human.length !== 2 || human.some(row => row.authorUserId !== personId)) return undefined; + const canonical = runs.filter(row => row.id !== setupRunId && row.companyId === companyId && row.agentId === leadId && row.runtimeMode === "native" + && row.contextSnapshot?.issueId === sourceId && Array.isArray(row.contextSnapshot.wakeCommentIds) && row.contextSnapshot.wakeCommentIds.includes(human[1]!.id)); + if (canonical.length !== 1) return undefined; + const candidates = tasks.flatMap(work => { + if ((work.parentId != null && work.parentId !== sourceId) || work.id === sourceId || work.companyId !== companyId || work.workMode !== "standard" || work.createdByAgentId !== leadId || work.assigneeAgentId !== leadId + || work.originKind !== "manual" || work.originRunId !== canonical[0]!.id) return []; + const created = activity.filter(row => row.companyId === companyId && row.entityType === "issue" && row.entityId === work.id && row.action === "issue.created" + && row.actorType === "agent" && row.actorId === leadId && row.agentId === leadId && row.runId === canonical[0]!.id); + const admitted = runs.filter(row => row.companyId === companyId && row.agentId === leadId && row.runtimeMode === "native" && row.issueId === work.id + && !!row.wakeupRequestId && row.contextSnapshot?.issueId === work.id + && row.contextSnapshot.wakeSource === "assignment" && row.contextSnapshot.wakeReason === "issue_assigned"); + if (admitted.length !== 1) return []; + const producer = admitted[0]!.contextSnapshot?.source; + const protocol = producer === "paperclip_runner.create_task" && admitted[0]!.contextSnapshot?.parentIssueId === (work.parentId ?? null) && work.originId?.startsWith(`paperclip-runner:create-task:${sourceId}:`) + && work.originId.length > `paperclip-runner:create-task:${sourceId}:`.length && /^[a-f0-9]{64}$/.test(work.originFingerprint ?? "") + && created.some(row => row.details?.source === "paperclip_runner_protocol"); + const publicIssueCreate = producer === "issue.create" && created.some(row => row.details?.source === undefined); + return protocol || publicIssueCreate ? [{ work, run: admitted[0]!, canonical: canonical[0]! }] : []; + }); + return candidates.length === 1 ? candidates[0] : undefined; +} +export function outboundThreadMatches(thread: EmailThreadSummary, emailTask: Entity, workId: string, companyId: string, leadId: string, endpointId: string) { + return emailTask.companyId === companyId && emailTask.parentId === workId && emailTask.assigneeAgentId === leadId && thread.issueId === emailTask.id + && thread.endpoint.id === endpointId && thread.endpoint.companyId === companyId && thread.endpoint.assignedAgentId === leadId && thread.endpoint.status === "active"; +} const requirement = externalRequirements["chat-channels.outbound-email"]!; export const runtimeOutboundEmailCases = [runtimeDefinition("chat-channels.outbound-email", async ctx => { try { @@ -45,13 +80,18 @@ export const runtimeOutboundEmailCases = [runtimeDefinition("chat-channels.outbo async act(ctx, s) { const api = ctx.input.api, companyId = ctx.input.fixtures.company.id, sourceId = String(s.sourceId), endpointId = (s.endpoint as { id: string }).id; const sent = await pollUntil({ label: "Actual delegated task sends the authorized launch draft and returns ordinary completion", deadlineAt: Date.now() + 360000, load: async () => { - await ctx.refreshIssue(); const runs = await ctx.allRuns(), tasks = await api.get(`/api/companies/${companyId}/issues`), work = tasks.find(row => row.parentId === sourceId && row.assigneeAgentId === ctx.input.fixtures.agent.id), emails = work ? tasks.filter(row => row.parentId === work.id) : [], threads = await Promise.all(emails.map(row => api.get(`/api/companies/${companyId}/email/tasks/${row.id}`))); return { runs, tasks, work, thread: threads.filter((row): row is EmailThreadSummary => !!row)[0] }; + await ctx.refreshIssue(); const runs = await ctx.allRuns(), tasks = await api.get(`/api/companies/${companyId}/issues`); + const [comments, activity] = await Promise.all([api.get(`/api/issues/${sourceId}/comments?order=asc`), api.get(`/api/companies/${companyId}/activity?limit=1000`)]); + const handoff = outboundOrdinaryHandoff({ companyId, sourceId, leadId: ctx.input.fixtures.agent.id, personId: String(s.personId ?? ""), setupRunId: String(s.setupRunId ?? ""), tasks, runs, comments, activity }); + const work = handoff?.work, emails = work ? tasks.filter(row => row.parentId === work.id && row.companyId === companyId) : []; + const threads = await Promise.all(emails.map(async emailTask => { const thread = await api.get(`/api/companies/${companyId}/email/tasks/${emailTask.id}`); return thread && outboundThreadMatches(thread, emailTask, work!.id, companyId, ctx.input.fixtures.agent.id, endpointId) ? thread : undefined; })); + const matching = threads.filter((row): row is EmailThreadSummary => !!row); return { runs, tasks, work, handoff, thread: matching.length === 1 ? matching[0] : undefined }; }, accept: v => ctx.issue()?.conversationState === "waiting" && v.runs.length === 4 && v.runs.every(row => row.status === "succeeded") && v.work?.status === "done" && v.thread?.publications.length === 1 && ["sent", "delivered"].includes(v.thread.publications[0]!.outcome), reject: v => v.runs.length > 4 ? "Email delegation exceeded its declared causal run bound" : undefined }); - const thread = sent.thread!, publication = await api.get(`/api/companies/${companyId}/email/deliveries/${thread.publications[0]!.id}`), work = sent.work!, run = sent.runs.find(row => row.contextSnapshot?.issueId === work.id), request = publication.request!; + const thread = sent.thread!, publication = await api.get(`/api/companies/${companyId}/email/deliveries/${thread.publications[0]!.id}`), work = sent.work!, run = sent.handoff!.run, request = publication.request!; s.emailTaskId = thread.issueId; s.workTaskId = work.id; s.publicationId = publication.id; s.actualOutcome = publication.outcome; s.actualDraftTransmitted = normalized(request.text).includes(normalized(String(s.draft))); s.feedbackRequested = /feedback|review|thoughts|comments|suggestions|what (?:do you|you) think/i.test(request.text); s.exactAuthorizedRecipient = JSON.stringify(request.to) === JSON.stringify([(s.emailRecipient as { email: string }).email]) && !request.cc?.length && !request.bcc?.length; - s.normalTaskBound = work.companyId === companyId && (work as Entity & { workMode?: string }).workMode === "standard" && work.parentId === sourceId && work.assigneeAgentId === ctx.input.fixtures.agent.id && !!run && run.agentId === work.assigneeAgentId && run.runtimeMode === "native"; + s.normalTaskBound = !!sent.handoff && sent.handoff.work.id === work.id && sent.handoff.run.id === run.id; const events = run ? await collectRunEvents<{ seq?: number; eventType?: string; payload?: unknown }>((afterSeq, limit) => api.get(`/api/heartbeat-runs/${run.id}/events?afterSeq=${afterSeq}&limit=${limit}`)) : []; s.actualNativeSendBound = actualOriginalEmailSend(events, { companyId, taskId: work.id, endpointId, publicationId: publication.id }); const direct = await inspectOwnedEmailPublication(ctx, publication, String(s.startedAt)); s.independentProviderCount = direct.matchedSent?.length; s.originalProviderReceiptMatched = direct.originalMessageMatched; const duplicateRequest = duplicateRequests.get(ctx)!, root = process.env.PAPERCLIP_RUNNER_E2E_TEMP_ROOT!; @@ -66,7 +106,7 @@ export const runtimeOutboundEmailCases = [runtimeDefinition("chat-channels.outbo s.factualReplyBound = human.length === 3 && human[2]?.body === followup && factual.contextSnapshot?.issueId === sourceId && Array.isArray(factual.contextSnapshot.wakeCommentIds) && factual.contextSnapshot.wakeCommentIds.length === 1 && factual.contextSnapshot.wakeCommentIds[0] === human[2]?.id; s.factualReply = comments.filter(row => row.createdByRunId === factual.id && row.authorAgentId === ctx.input.fixtures.agent.id).map(row => row.body).join("\n"); s.nativeLedgerCausal = !!canonical && !!run && completion.length === 1 && new Set([s.setupRunId, canonical.id, run.id, completion[0]!.id, factual.id]).size === 5 && final.every(row => [s.setupRunId, canonical.id, run.id, completion[0]!.id, factual.id].includes(row.id)); - const saved = await api.get(`/api/companies/${companyId}/email/tasks/${thread.issueId}`), emailTask = await api.get(`/api/issues/${thread.issueId}`); const ordinary = (await api.get>(`/api/companies/${companyId}/issues`)).filter(row => row.workMode !== "chat"); s.oneSavedPublication = saved.publications.length === 1 && saved.publications[0]?.id === publication.id && ordinary.length === 2 && ordinary.every(row => [work.id, thread.issueId].includes(row.id)); s.actualThreadLinked = emailTask.companyId === companyId && emailTask.parentId === work.id && saved.conversationId === publication.conversationId && saved.endpoint.id === endpointId; + const saved = await api.get(`/api/companies/${companyId}/email/tasks/${thread.issueId}`), emailTask = await api.get(`/api/issues/${thread.issueId}`); const ordinary = (await api.get>(`/api/companies/${companyId}/issues`)).filter(row => row.workMode !== "chat"); s.oneSavedPublication = saved.publications.length === 1 && saved.publications[0]?.id === publication.id && ordinary.length === 2 && ordinary.every(row => [work.id, thread.issueId].includes(row.id)); s.actualThreadLinked = outboundThreadMatches(saved, emailTask, work.id, companyId, ctx.input.fixtures.agent.id, endpointId) && saved.conversationId === publication.conversationId; const last = await inspectOwnedEmailPublication(ctx, publication, String(s.startedAt)); s.finalIndependentProviderCount = last.matchedSent?.length; (s.providerProbes as unknown[]).push(...[direct, last].map(row => ({ phase: "actual-outbound-provider-receipt", directProviderProbeCount: row.directProviderProbeCount, status: "verified", usage: "unavailable", cost: "unavailable" }))); }, diff --git a/tests/runner-e2e/chat-catalog-preservation.json b/tests/runner-e2e/chat-catalog-preservation.json index d4bebdfb23d..ba29c1bb1a0 100644 --- a/tests/runner-e2e/chat-catalog-preservation.json +++ b/tests/runner-e2e/chat-catalog-preservation.json @@ -65,6 +65,14 @@ { "name": "task-two-public-prerequisites", "sourceRevision": "ef45d57ab8919854e7203ee789f6441db1470b87" + }, + { + "name": "document-restore-decision-audience", + "sourceRevision": "8bf05430d65f1bd354b3eceea4e6a7963aca6a6c" + }, + { + "name": "outbound-standing-native-provenance", + "sourceRevision": "7db3308ea4e48f4d87130e384a672b1ebd5f5394" } ], "productionPrerequisites": [ @@ -119,7 +127,7 @@ "attempts": "All original raw grades, source fingerprints, ordinals, usage, billing, privacy and cleanup remain bound to their original measurements. Exhausted cases remain exhausted.", "sidebarOrdering": "The frozen legacy Projects manual-order surface is absent from both current39e layouts. No injected classic UI, fabricated ordering or feature revival is included." }, - "preservationParentRevision": "063925683cd79590f3c5a9e32d741607b8096fbb", + "preservationParentRevision": "372b192327e6a02c6ab3da4961521b8632a60fb6", "latestAcceptedEvalOnlyOverlays": { "sdk-two-public-setup": { "sourceRevision": "8cb209e726237644f1312d2385da460083d0a035", @@ -136,6 +144,30 @@ "task-coordination.delegate-with-context" ], "contracts": "Actual ready-to-paused owners/version/manual holds with original deadline/no work; owned human browser Origin on original exact attachment upload with unchanged production guard." + }, + "document-restore-decision-audience": { + "sourceRevision": "8bf05430d65f1bd354b3eceea4e6a7963aca6a6c", + "scenarioIds": [ + "documents-artifacts.restore-document", + "inbox-search.show-decisions" + ], + "contracts": "Exact public document id/task/key/revision/bytes plus actual Plans UI identity; independently authenticated addressed Riley pending-decision feed with explicit owner/Riley principal guards. Actual returned final failures stay exhausted and source preservation does not change outcomes." + }, + "outbound-standing-native-provenance": { + "sourceRevision": "7db3308ea4e48f4d87130e384a672b1ebd5f5394", + "scenarioIds": [ + "chat-channels.outbound-email" + ], + "contracts": "Canonical source-human/native run, actual ordinary task creating actor/originRunId, public creation audit and unique native assignment admission. Standing-chat parentNULL and actual public issue.create omitted parent projection supported; explicit email-child/thread/endpoint and all original provider-receipt/duplicate/draft controls preserved. Authorized AgentMail input remains an external gate." } - } + }, + "ordinaryComposedHeadRuntimeQualification": 0, + "excludedCandidatePrerequisitesUnchanged": [ + 18103, + 18105, + 18033, + 17917, + 17848 + ], + "acceptedSourceDoesNotCureSpentRawFailures": true }