Files
PaperClipAI/tests/runner-e2e/product-journeys/agent-settings-flow.ts
T
DottaandPaperclip 00a792999c test(e2e): retain failed agent-settings task identity (#23371)
## Thinking Path

> - Paperclip lets people manage agents and their saved work.
> - Product E2E tests check whether saved agent instructions affect the
next task.
> - A failed work run must keep a failed test result.
> - The observer currently saves task identity only after the task
completes.
> - A failed run leaves no task or run ID in its retained settings
evidence.
> - This change saves the observed identity before the failure check.
> - Engineers can then find the failed run without storing raw
instruction or provider data.

## Linked Issues or Issue Description

**What happened?**

The saved-instructions journey reads a failed native run and throws
`agent_settings_native_run_failed`. Its private report then writes
`issueId: null` and `runIds: []`. The observer already read the task and
run, but loses their identity because it fills completed evidence only
after a successful wait.

**Expected behavior**

Keep the created task ID, requested run IDs and observed status/native
phase. Keep the test failed. Do not record instruction text, raw errors,
output excerpts, account configuration or provider payloads.

**Steps to reproduce**

Run the existing agent-settings flow fixture with a failed native run.
Read `agent-settings-observations.json`. Before this change its task ID
is null. The new regression fails on that value. A denied run-detail
read also loses its requested identity.

Related work: #23041 retains question-journey failure evidence. #23108
adds Skill Studio browser-step codes. #23288 changes reset and
repository observation waits. None covers the saved-instructions
observer. This change does not repair the underlying native run failure.

## What Changed

- Keep the created task identity before the first task observation.
- Save requested run IDs before detail reads. Keep status and native
phase from each completed read.
- Keep this diagnostic separate from completed evidence and all grading
checks.
- Test failed runs, denied detail reads, successful completion and
raw-data exclusion.
- Document the private evidence fields.

## Verification

- The new failed-run assertion failed before the fix: expected task ID
`task`, received null. The other nine flow checks passed.
- After the fix, 89 checks pass in `agent-settings-flow.test.ts`,
`agent-settings-evidence.test.ts` and `observation-read.test.ts`.
- Command: `pnpm exec vitest run --config
tests/runner-e2e/vitest.config.ts` with those three files.
- `pnpm test:e2e:runner:typecheck` passes on the final source.
- The real observer and private file writer ran on Linux with simulated
browser/API boundaries. Failed work still fails. A denied detail read
keeps an empty observed-runs list. No second settings save occurs after
either failure.
- `pnpm verify` could not select tests because the managed Git guard
rejects `git ls-files`. This is not a pass. Preflight installed
dependencies and built required packages, then could not find Cargo.
Focused checks ran directly.
- No hosted provider rerun or deployment was made. Local evidence does
not establish a staging pass. Hosted acceptance requires approved
harness adoption.
- Final-head CI and automated reviews are pending.

## Risks

The new record contains private task/run IDs and lifecycle fields. It
uses an explicit field projection and does not copy the full run object.
It cannot make a pending or failed run pass. The change adds no work
runs, retries, requests, settings edits or budget. Existing grading,
cleanup and public projection remain in place. This is a reporting
repair; the underlying provider or infrastructure cause remains open.

## Model Used

OpenAI Codex, GPT-6 family, with reasoning, tool use and code execution.
The runtime does not expose an exact model ID or context-window size.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` /
`Closes: #` / `Refs: #` OR (b) described the issue in-PR following the
relevant issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-12-01 15:51:36 +00:00

184 lines
12 KiB
TypeScript

import { randomBytes } from "node:crypto";
import { mkdir, writeFile } from "node:fs/promises";
import path from "node:path";
import { expect } from "@playwright/test";
import { createTaskThroughUi } from "../user-actions.js";
import { waitFor } from "./agent-flows.js";
import { createObservedReader } from "./observation-read.js";
import { openDocumentAuthoring } from "./task-document-flow.js";
import { AGENT_SETTINGS_CHECKS, gradeAgentSettings, instructionDigest, matchedBoardSaveAudit, settingsInstructionContent,
settingsDocumentKey, settingsProofHeading, settingsTaskPrompt, type AgentSettingsEvidence, type AgentSettingsScope,
type SettingsSnapshot } from "./agent-settings-evidence.js";
import { JourneyError, type JourneyContext, type JourneyEvidence, type TargetSession } from "./types.js";
type Row = Record<string, any>;
const id = "agent-settings-persistence";
export async function runAgentSettings(context: JourneyContext, session: TargetSession, campaignId: string,
evidence: JourneyEvidence, options: { deadlineAt: number; directory: string }) {
const agent = context.agent;
if (!agent || agent.runtimeMode !== "native" || !context.environmentId || !context.capabilities.includes("agent-settings-write")) {
throw new JourneyError("unsupported", "owned_agent_settings_capability_required");
}
const scope: AgentSettingsScope = { campaignId, companyId: context.company.id, userId: context.user.id,
agentId: agent.id, agentName: agent.name, provider: agent.provider, model: agent.model,
environmentId: context.environmentId, reference: randomBytes(16).toString("hex") };
const observations: AgentSettingsEvidence = {};
let taskObservation: { issueId: string; runIds: string[]; runs: Array<{ id: string; status: string; nativePhase: string | null }> } | null = null;
const read = createObservedReader(context.api, options.deadlineAt), agentRoute = `/api/agents/${agent.id}`;
const agentRunsRoute = `/api/companies/${scope.companyId}/heartbeat-runs?agentId=${agent.id}&limit=100`;
const activityRoute = `/api/companies/${scope.companyId}/activity?entityType=agent&entityId=${agent.id}&limit=200`;
const snapshot = async (): Promise<SettingsSnapshot> => {
const [current, bundle, runs, activity] = await Promise.all([
read.get<Row>(agentRoute), read.get<Row>(`${agentRoute}/instructions-bundle`),
read.get<Row[]>(agentRunsRoute), read.get<Row[]>(activityRoute),
]);
if (typeof bundle?.entryFile !== "string" || !bundle.entryFile || !Array.isArray(runs) || runs.length >= 100
|| !Array.isArray(activity) || activity.length >= 200
|| activity.some(row => row.companyId !== scope.companyId || row.entityType !== "agent" || row.entityId !== agent.id)) {
throw new JourneyError("failed", "agent_settings_read_coverage_unavailable");
}
const file = await read.get<Row>(`${agentRoute}/instructions-bundle/file?path=${encodeURIComponent(bundle.entryFile)}`);
return { agent: current, bundle, file, runs, activity };
};
const openInstructions = async (entry: string, expectedContent: string, reload = false) => {
await context.page.goto(`${context.api.origin}/${context.company.issuePrefix}/agents/${agent.id}/instructions`, { waitUntil: "domcontentloaded" });
if (reload) await context.page.reload();
const segments = entry.split("/");
const treeRow = (filePath: string) => context.page.locator(`[role="treeitem"][data-file-tree-path=${JSON.stringify(filePath)}]`);
for (let depth = 1; depth < segments.length; depth++) {
const directory = treeRow(segments.slice(0, depth).join("/"));
await expect(directory).toBeVisible();
if (await directory.getAttribute("aria-expanded") !== "true") await directory.click();
}
const entryRow = treeRow(entry);
await expect(entryRow).toBeVisible();
await entryRow.click();
const edit = context.page.getByRole("button", { name: `Edit ${entry}`, exact: true });
await expect(edit).toBeVisible();
if (!expectedContent.trim()) {
await edit.click();
const editor = context.page.getByRole("textbox", { name: "Instruction file editor", exact: true });
await expect(editor).toHaveValue(expectedContent);
const renderedContent = await editor.inputValue();
await context.page.getByRole("button", { name: `Stop editing ${entry}`, exact: true }).click();
return { edit, renderedContent };
}
const raw = context.page.getByTestId("instructions-raw-source");
await expect(raw).toBeVisible();
const renderedContent = await raw.textContent();
if (renderedContent !== expectedContent) throw new JourneyError("failed", "agent_settings_rendered_source_mismatch");
return { edit, renderedContent };
};
const saveThroughUi = async (beforePoint: SettingsSnapshot, content: string) => {
const entry = beforePoint.bundle.entryFile, before = beforePoint.file.content;
const { edit } = await openInstructions(entry, before);
await edit.click();
const editor = context.page.getByRole("textbox", { name: "Instruction file editor", exact: true });
await expect(editor).toHaveValue(before);
await editor.fill(content);
const save = context.page.getByRole("button", { name: "Save changes", exact: true });
await expect(save).toBeEnabled();
await save.click();
await expect(save).toBeDisabled({ timeout: 30_000 });
// Disabled can also mean saving. Confirm persisted bytes and the new audit
// before navigating, including during restoration.
return waitFor(options.deadlineAt, snapshot, point => point.file.content === content
&& Boolean(matchedBoardSaveAudit(point, beforePoint, scope)), "agent_settings_save_not_persisted");
};
const check = (name: typeof AGENT_SETTINGS_CHECKS[number]) => {
const result = gradeAgentSettings(scope, observations).find(row => row.id === name)!;
evidence.checks.push(result);
if (!result.passed) throw new JourneyError("failed", name.replaceAll("-", "_"));
};
let failed = false;
try {
observations.original = await snapshot();
const original = observations.original, entry = original.bundle.entryFile;
if (original.agent.id !== agent.id || original.agent.companyId !== scope.companyId || original.agent.name !== agent.name
|| !agent.name.includes(campaignId) || original.agent.adapterType !== "paperclip_runner"
|| original.agent.adapterConfig?.provider !== agent.provider || original.agent.adapterConfig?.model !== agent.model
|| original.bundle.mode !== "managed" || original.bundle.editable !== true || original.file.editable !== true
|| typeof original.file.content !== "string" || original.file.content.includes("\r")
|| original.file.content.includes(settingsProofHeading) || original.runs.length !== 0) {
throw new JourneyError("failed", "agent_settings_fixture_not_fresh_owned");
}
const edited = settingsInstructionContent(original.file.content, scope.reference);
observations.saved = await saveThroughUi(original, edited);
check("agent-settings-browser-save");
const reopenedSource = await openInstructions(entry, edited, true);
observations.reloaded = { ...await snapshot(), renderedContent: reopenedSource.renderedContent };
check("agent-settings-reload-persisted");
const title = `Journey QA agent settings ${campaignId}`;
const task = await createTaskThroughUi({ page: context.page, origin: context.api.origin, issuePrefix: context.company.issuePrefix,
agentName: agent.name, title, prompt: settingsTaskPrompt, workMode: "standard", requireExplicitTitle: true,
beforeDispatch: () => session.own({ kind: "issue", name: title, journeyId: id }) });
taskObservation = { issueId: task.issueId, runIds: [], runs: [] };
await session.own({ kind: "issue", name: title, id: task.issueId, journeyId: id });
const route = `/api/issues/${task.issueId}`, documentRoute = `${route}/documents/${settingsDocumentKey}`;
const state = await waitFor(options.deadlineAt, async () => {
const [issue, summaries, interactions] = await Promise.all([
read.get<Row>(route), read.get<Row[]>(`${route}/runs`), read.get<Row[]>(`${route}/interactions`),
]);
if (!Array.isArray(summaries) || summaries.length > 1 || !Array.isArray(interactions)) throw new JourneyError("failed", "agent_settings_run_coverage_mismatch");
const point = taskObservation!;
point.runIds = summaries.map(row => row.runId);
point.runs = [];
const runs = await Promise.all(summaries.map(async row => {
const run = await read.get<Row>(`/api/heartbeat-runs/${row.runId}`);
point.runs.push({ id: row.runId, status: run.status, nativePhase: run.nativePhase ?? null });
return run;
}));
if (runs.some(run => ["failed", "cancelled", "timed_out"].includes(run.status))) throw new JourneyError("failed", "agent_settings_native_run_failed");
return { issue, runs, interactions };
}, point => point.issue.status === "done" && point.runs.length === 1 && point.runs[0]!.status === "succeeded"
&& point.runs[0]!.nativePhase === "committed" && point.interactions.every(row => row.status !== "pending"), "agent_settings_task_not_completed");
const document = await read.get<Row>(documentRoute);
const revision = await read.get<Row>(`${documentRoute}/revisions/${document.latestRevisionId}`);
observations.task = { ...state, document, revision };
observations.afterWork = await snapshot();
check("agent-settings-instruction-only-input");
check("configured-model-and-environment");
check("agent-settings-result-attributed");
await context.page.goto(`${context.api.origin}/${context.company.issuePrefix}/issues/${state.issue.identifier ?? task.issueId}`, { waitUntil: "domcontentloaded" });
await context.page.reload();
await openDocumentAuthoring(context);
const displayed = context.page.locator(`#document-${settingsDocumentKey}`);
await expect(displayed).toContainText(scope.reference);
observations.task.reopenedDocument = await read.get<Row>(documentRoute);
observations.task.reopenedRevision = await read.get<Row>(`${documentRoute}/revisions/${document.latestRevisionId}`);
observations.task.renderedReference = (await displayed.textContent())?.includes(scope.reference) ? scope.reference : undefined;
check("agent-settings-result-reopened");
await saveThroughUi(observations.afterWork, original.file.content);
const restoredSource = await openInstructions(entry, original.file.content);
observations.restored = { ...await snapshot(), renderedContent: restoredSource.renderedContent };
check("agent-settings-original-restored");
// The shared screenshot policy permits only the task result, not settings.
await context.page.goto(`${context.api.origin}/${context.company.issuePrefix}/issues/${state.issue.identifier ?? task.issueId}`, { waitUntil: "domcontentloaded" });
await openDocumentAuthoring(context);
await expect(context.page.locator(`#document-${settingsDocumentKey}`)).toContainText(scope.reference);
} catch (error) { failed = true; throw error; }
finally {
try {
await read.preserve(options.directory);
await mkdir(options.directory, { recursive: true, mode: 0o700 });
await writeFile(path.join(options.directory, "agent-settings-observations.json"), JSON.stringify({
companyId: scope.companyId, agentId: scope.agentId, reference: scope.reference,
checkpoints: Object.fromEntries(["original", "saved", "reloaded", "afterWork", "restored"].map(key => {
const point = observations[key as Exclude<keyof AgentSettingsEvidence, "task">];
return [key, point ? { entryFile: point.file.path, contentHash: point.file.contentHash,
independentlyComputedHash: typeof point.file.content === "string" ? instructionDigest(point.file.content) : null,
revisionId: point.file.revision?.id ?? null, runIds: point.runs.map(row => row.id ?? row.runId) } : null];
})),
issueId: taskObservation?.issueId ?? null, runIds: taskObservation?.runIds ?? [], taskObservation,
documentId: observations.task?.document.id ?? null, documentRevisionId: observations.task?.revision.id ?? null,
saveAudits: { saved: matchedBoardSaveAudit(observations.saved, observations.original, scope),
restored: matchedBoardSaveAudit(observations.restored, observations.afterWork, scope) },
checks: gradeAgentSettings(scope, observations),
}, null, 2), { mode: 0o600 });
} catch (error) { if (!failed) throw error; }
}
}