Files
PaperClipAI/ui/src/pages/ExecutionWorkspaceDetail.provision-status.test.ts
T
dcac49a4fd feat(workspaces): defer isolated setup until runtime start (#10653)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Isolated workspaces give each task a safe and reproducible checkout.
> - The existing setup cloned the development database before an agent
needed to run the app.
> - This made worktree creation slower and heavier for tasks that never
start a service.
> - Runtime services already use one server start path for heartbeat,
operator, and startup recovery flows.
> - This pull request moves heavy setup to that start path and keeps
worktree creation lean.
> - The benefit is faster isolated workspace creation with the same
reliable runtime setup when a service starts.

## Linked Issues or Issue Description

Related pull request: #10652 covers the initial deferred
database-seeding slice. This pull request supersedes it with end-to-end
runtime provisioning and safe cleanup.

**What existing behavior does this improve?**

This improves isolated worktree creation, runtime service startup, and
isolated instance cleanup.

**Subsystem affected**

Cross-cutting: CLI worktree setup, server runtime orchestration, shared
workspace contracts, and development scripts.

**Current behavior**

Paperclip seeds an isolated development database during worktree
creation. It can also leave an isolated instance directory after
workspace teardown. This work happens even when no runtime service
starts.

**Proposed behavior**

Paperclip creates the worktree with a lean eager setup. It runs an
idempotent runtime provision command before the first managed service
spawn. Concurrent starts share one provision attempt. Teardown removes
the isolated instance safely.

**Reason and benefit**

Many agent tasks only edit and test code. They do not need a running
Paperclip instance. Deferring the database seed reduces workspace
startup cost while preserving automatic setup for tasks that start the
app.

**Breaking changes**

None. The new runtime provision command is optional. Existing workspace
behavior is unchanged when it is absent.

## What Changed

- Split Paperclip worktree setup into a lean eager script and an
idempotent runtime provision script.
- Added `runtimeProvisionCommand` to project, issue, realized workspace,
and persisted workspace contracts.
- Added a per-workspace provision mutex before local service spawn for
heartbeat, operator, and startup recovery flows.
- Added a persisted `provisioning` service state and the
`workspace_runtime_provision` operation phase.
- Kept provision time outside the service readiness timeout and made
failed attempts visible and retryable.
- Reclaimed isolated instance data during safe workspace teardown.
- Serialized deferred database seeding across processes and bound
teardown to the instance root captured in persisted workspace metadata.
- Added tests for config flow, concurrency, retry, no-op behavior,
readiness timing, scripts, CLI commands, and cleanup.
- Documented the eager and runtime provisioning contracts.

## Verification

- `pnpm -r typecheck`
- `pnpm build`
- `pnpm test:run` (server: 3,201 passed; UI: 3,345 passed; the CLI phase
exposed one environment-sensitive AWS doctor assertion because the agent
runtime injects static AWS credentials)
- `env -u AWS_ACCESS_KEY_ID -u AWS_SECRET_ACCESS_KEY pnpm exec vitest
run cli/src/__tests__/secrets.test.ts -t 'passes AWS doctor checks when
non-secret provider config is present'`
- Focused runtime tests cover serialized provisioning, retry after
stderr failure, absent-command no-op behavior, operation logging,
persisted state order, and readiness timeout exclusion.
- Focused CLI and cleanup tests cover concurrent seed serialization,
stale-lock fail-closed behavior, persisted instance ownership, and
rewritten sibling pointers.

## Risks

- A faulty runtime provision script blocks service startup. Paperclip
records stderr, marks the service failed, and retries on the next start.
- Concurrent service requests share an in-process provision attempt,
while the seed command uses an atomic filesystem lock across processes.
A stale lock fails closed and requires an operator to verify no seed is
running before removing it.
- Isolated instance cleanup is destructive. The cleanup service
validates ownership and path containment before removal.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- OpenAI Codex, `gpt-5.6-sol`, with agentic reasoning, tool use, and
code execution. The service does not expose the 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
- [x] All Paperclip CI gates are green
- [x] 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>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-02 10:37:10 -05:00

93 lines
3.4 KiB
TypeScript

import type { WorkspaceOperation } from "@paperclipai/shared";
import { describe, expect, it } from "vitest";
import { resolveRuntimeProvisionStatus } from "./ExecutionWorkspaceDetail";
function operation(overrides: Partial<WorkspaceOperation> = {}): WorkspaceOperation {
return {
id: overrides.id ?? "op-1",
companyId: "company-1",
executionWorkspaceId: "ews-1",
heartbeatRunId: null,
issueId: null,
phase: overrides.phase ?? "workspace_runtime_provision",
command: overrides.command ?? "bash ./scripts/provision-worktree-runtime.sh",
cwd: null,
status: overrides.status ?? "succeeded",
exitCode: overrides.exitCode ?? 0,
logStore: null,
logRef: null,
logBytes: null,
logSha256: null,
logCompressed: false,
stdoutExcerpt: null,
stderrExcerpt: null,
metadata: null,
startedAt: overrides.startedAt ?? new Date("2026-08-01T00:00:00.000Z"),
finishedAt: "finishedAt" in overrides ? overrides.finishedAt! : new Date("2026-08-01T00:01:00.000Z"),
createdAt: new Date("2026-08-01T00:00:00.000Z"),
updatedAt: new Date("2026-08-01T00:01:00.000Z"),
};
}
describe("resolveRuntimeProvisionStatus", () => {
it("reports eager when no runtime provision command is configured", () => {
expect(resolveRuntimeProvisionStatus({ runtimeProvisionCommand: null, operations: [] })).toEqual({
kind: "eager",
});
expect(resolveRuntimeProvisionStatus({ runtimeProvisionCommand: " ", operations: undefined })).toEqual({
kind: "eager",
});
});
it("reports deferred when configured but no provision operation has run", () => {
expect(
resolveRuntimeProvisionStatus({
runtimeProvisionCommand: "bash ./seed.sh",
operations: [operation({ phase: "workspace_provision" })],
}),
).toEqual({ kind: "deferred" });
});
it("reports provisioned with the finished time when the latest op succeeded", () => {
const finishedAt = new Date("2026-08-01T12:00:00.000Z");
expect(
resolveRuntimeProvisionStatus({
runtimeProvisionCommand: "bash ./seed.sh",
operations: [operation({ status: "succeeded", finishedAt })],
}),
).toEqual({ kind: "provisioned", at: finishedAt });
});
it("reports provisioning while the op is running", () => {
const startedAt = new Date("2026-08-01T12:00:00.000Z");
expect(
resolveRuntimeProvisionStatus({
runtimeProvisionCommand: "bash ./seed.sh",
operations: [operation({ status: "running", startedAt, finishedAt: null })],
}),
).toEqual({ kind: "provisioning", at: startedAt });
});
it("reports failed with the finished time when the latest op failed", () => {
const finishedAt = new Date("2026-08-01T12:05:00.000Z");
expect(
resolveRuntimeProvisionStatus({
runtimeProvisionCommand: "bash ./seed.sh",
operations: [operation({ status: "failed", finishedAt, exitCode: 1 })],
}),
).toEqual({ kind: "failed", at: finishedAt });
});
it("uses only the latest runtime provision op (operations are most-recent first)", () => {
const latest = new Date("2026-08-02T00:00:00.000Z");
const status = resolveRuntimeProvisionStatus({
runtimeProvisionCommand: "bash ./seed.sh",
operations: [
operation({ id: "op-new", status: "failed", finishedAt: latest, exitCode: 1 }),
operation({ id: "op-old", status: "succeeded" }),
],
});
expect(status).toEqual({ kind: "failed", at: latest });
});
});