mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-30 17:14:54 +01:00
feat(workspaces): sign the workspace login handoff and gate readiness (#11671)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Managed worktree services run isolated Paperclip instances with cloned databases. > - A reachable service was reported as ready even when its database, runtime identity, or login path was not usable. > - The first candidate added verified database seeding and managed repair in #11665. > - This pull request consolidates that candidate with signed login handoff and a complete readiness contract. > - Post-QA fixes close five defects in repair identity, repair responses, UI retry, seed journal handling, and seed-source trust. > - The benefit is a workspace that either opens safely or reports one accurate recovery action. ## Linked Issues or Issue Description No public GitHub issue exists for this work, so the problem is described here. **What happened** Managed workspace URLs could return HTTP 200 and report ready while login failed. QA also found cases where repair used the wrong instance identity, returned a generic error, left the UI stuck, rejected a safe journal lag, or trusted a mutable workspace manifest. **Expected behavior** Opening a ready workspace signs the board user in to the correct isolated instance. Provisioning and repair use a registered source and report a structured recovery state. **Actual behavior** Entry depended on a password copied into the clone. Several failure paths could publish stale readiness, hide the repair precondition, or trust state that the workspace could modify. **Additional context** This pull request includes the commits first published in #11665. That pull request keeps the original base head for review history. This consolidated pull request is the merge candidate. Related open readiness work includes #11575 and #11621. ## What Changed - Adds a short-lived, signed, single-use login ticket. It binds the user, workspace, instance, and runtime origin. - Exchanges the ticket through Better Auth. It creates the session and cookie through the supported adapter path. - Adds protected workspace readiness fields for the database, clone data, login handoff, seed phase, and runtime identity. - Fails readiness closed when the guest has no company or execution-workspace binding. - Binds ticket issuance to the exact cloned user and active company membership selected for the handoff. - Verifies every current active board identity through the exact-user handoff before publication or reuse. - Gates managed runtime publication on the readiness contract and the recorded worktree instance identity. - Refreshes runtime work products from the live runtime row after a port change. - Adds one workspace access card with ready, degraded, repairing, and failed states. - Uses the runtime response identity for repair. It returns structured repair precondition errors. - Lets a valid source journal lag converge during provisioning. - Binds seed and repair manifests to a source registered outside the agent-writable worktree. - Clears recovered UI errors so a successful retry can open the workspace. - Makes runtime tests register canonical sources and avoid ports owned by live host listeners. - Keeps Vitest on source suites when compiled `dist` trees exist. - Isolates CLI and adapter tests from ambient AWS and runtime API environment variables. - Preserves a 404 response for cross-company workspace ID lookups before runtime authorization. - Makes concurrent single-flight coverage independent of path-canonicalization scheduling order. ## Verification The following checks passed on the integrated head: ```sh pnpm -r typecheck pnpm build pnpm check:token-gates pnpm --filter @paperclipai/db check:migrations ``` - The server source lane passed 420 files and 4,953 tests. Five tests were skipped. - The CLI lane passed 57 files and 385 tests. - The database lane passed 26 files and 97 tests. - The shared package passed 58 files and 506 tests. - The adapter utility lane passed 640 tests. Four tests were skipped. - The Claude adapter passed 220 tests. One test was skipped. - The Codex adapter passed 323 tests. - The OpenClaw adapter passed 13 tests. - The OpenCode adapter passed 42 tests. - The plugin SDK passed 45 tests. - The workspace runtime suite passed 124 tests. - The caller-scoped readiness and handoff suite passed 52 tests. - The workspace provisioning shell suite passed 7 tests. - The runtime exposure suite passed 17 tests while live host mappings occupied fixed test ports. - `git diff --check` passed and the worktree is clean. The serialized route lane will run in GitHub CI with its normal shards. No deployment or active-workspace migration was performed. ## Risks - This is a medium-risk authentication and runtime-readiness change. - The login ticket uses exact origin, workspace, instance, and user binding. It has a short expiry and a one-time nonce. - Runtime publication is stricter. A real readiness, identity, per-user handoff, or control-plane database disagreement now blocks publication. - This pull request supersedes #11665 as the merge candidate. Close #11665 after this pull request merges. - No new database migration is included. The lockfile and workflow files are unchanged. - Deployment and active-workspace migration are intentionally outside this pull request. > 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 Claude Opus 5 (`claude-opus-5[1m]`), 1M context, extended thinking, tool use, and code execution produced the main candidate. OpenAI GPT-5 (`gpt-5`) through Codex, with agentic reasoning, tool use, and code execution, integrated the post-QA fixes and hardened the test gates. The Codex context-window size was not exposed. ## 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>
This commit is contained in:
1 parent
6aaef2998f
commit
a2bf936f9a
59 files changed
+7754
-243
No files matched your search
@@ -1,3 +1,6 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { accessSync, constants as fsConstants, existsSync, readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { Router, type Request, type Response } from "express";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
@@ -11,6 +14,10 @@ import {
|
||||
workspaceRuntimeControlTargetSchema,
|
||||
} from "@paperclipai/shared";
|
||||
import type { WorkspaceRuntimeDesiredState, WorkspaceRuntimeServiceStateMap } from "@paperclipai/shared";
|
||||
import {
|
||||
resolveCanonicalWorktreeSeedSource,
|
||||
type CanonicalWorktreeSeedSource,
|
||||
} from "@paperclipai/shared/worktree-seed-source";
|
||||
import { validate } from "../middleware/validate.js";
|
||||
import {
|
||||
accessService,
|
||||
@@ -45,9 +52,25 @@ import { appendWithCap } from "../adapters/utils.js";
|
||||
import { environmentRuntimeService } from "../services/environment-runtime.js";
|
||||
import type { PluginWorkerManager } from "../services/plugin-worker-manager.js";
|
||||
import { runExclusiveWorkspaceRuntimeControl } from "../services/workspace-operations.js";
|
||||
import { resolveManagedWorkspaceInstanceId } from "../services/managed-workspace-identity.js";
|
||||
import { isVerifiedWorktreeSeedManifest } from "../worktree-seed-manifest.js";
|
||||
import {
|
||||
issueWorkspaceLoginHandoff,
|
||||
workspaceLoginHandoffFailureStatus,
|
||||
} from "../services/workspace-login-handoff-issuer.js";
|
||||
import { conflict, unprocessable } from "../errors.js";
|
||||
|
||||
const WORKSPACE_CONTROL_OUTPUT_MAX_CHARS = 256 * 1024;
|
||||
|
||||
function isReadableFile(filePath: string) {
|
||||
try {
|
||||
accessSync(filePath, fsConstants.R_OK);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: PluginWorkerManager } = {}) {
|
||||
const router = Router();
|
||||
const svc = executionWorkspaceService(db);
|
||||
@@ -139,6 +162,87 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
res.json(readiness);
|
||||
});
|
||||
|
||||
/**
|
||||
* Mint a single-use workspace login handoff for the calling board user.
|
||||
*
|
||||
* Board-only: the ticket carries a user identity, so an agent key — which has
|
||||
* no user to sign in — must never be able to mint one. Nothing in the request
|
||||
* body influences what the ticket is bound to; only the landing path is taken
|
||||
* from the caller, and it is reduced to a same-origin path before signing.
|
||||
*/
|
||||
router.post("/execution-workspaces/:id/login-handoff", async (req, res) => {
|
||||
const id = req.params.id as string;
|
||||
assertBoard(req);
|
||||
const workspace = await getAccessibleResource(req, res, svc.getById(id), "Execution workspace not found");
|
||||
if (!workspace) return;
|
||||
// Opening a workspace board is a runtime-control-grade action: it hands the
|
||||
// caller an authenticated session inside the cloned instance.
|
||||
if (!(await assertRuntimeManageAllowed(req, res, workspace.companyId))) return;
|
||||
|
||||
const requestedNext = typeof (req.body as { next?: unknown } | null)?.next === "string"
|
||||
? (req.body as { next: string }).next
|
||||
: null;
|
||||
const result = await issueWorkspaceLoginHandoff({
|
||||
db,
|
||||
companyId: workspace.companyId,
|
||||
executionWorkspace: { id: workspace.id, cwd: workspace.cwd },
|
||||
actor: {
|
||||
userId: req.actor.userId ?? null,
|
||||
userEmail: req.actor.userEmail ?? null,
|
||||
source: req.actor.source ?? null,
|
||||
},
|
||||
next: requestedNext,
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
logger.warn(
|
||||
{
|
||||
executionWorkspaceId: workspace.id,
|
||||
reason: result.failure.reason,
|
||||
detail: "detail" in result.failure ? result.failure.detail : null,
|
||||
},
|
||||
"workspace login handoff was not issued",
|
||||
);
|
||||
res.status(workspaceLoginHandoffFailureStatus(result.failure)).json({
|
||||
error: "workspace_login_handoff_unavailable",
|
||||
// The UI turns this into the labeled snapshot-local credential fallback
|
||||
// or a repair prompt, so the machine reason has to survive the boundary.
|
||||
reason: result.failure.reason,
|
||||
mode: "credentials",
|
||||
...("detail" in result.failure ? { detail: result.failure.detail } : {}),
|
||||
...("readiness" in result.failure ? { readiness: result.failure.readiness } : {}),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await logActivity(db, {
|
||||
companyId: workspace.companyId,
|
||||
// Auditing issuance is a requirement of the handoff design: the nonce ties
|
||||
// this record to the guest's own accept/reject record. The ticket itself is
|
||||
// never recorded.
|
||||
action: "workspace_login_handoff_issued",
|
||||
entityType: "execution_workspace",
|
||||
entityId: workspace.id,
|
||||
actorType: "user",
|
||||
actorId: result.issuance.userId,
|
||||
details: {
|
||||
nonce: result.issuance.nonce,
|
||||
instanceId: result.issuance.instanceId,
|
||||
origin: result.issuance.origin,
|
||||
expiresAt: result.issuance.expiresAt,
|
||||
},
|
||||
}).catch((error) => {
|
||||
logger.warn({ err: error, executionWorkspaceId: workspace.id }, "failed to audit workspace login handoff");
|
||||
});
|
||||
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
res.status(201).json({
|
||||
url: result.issuance.url,
|
||||
expiresAt: result.issuance.expiresAt,
|
||||
mode: "handoff",
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/execution-workspaces/:id/workspace-operations", async (req, res) => {
|
||||
const id = req.params.id as string;
|
||||
const workspace = await getAccessibleResource(req, res, svc.getById(id), "Execution workspace not found");
|
||||
@@ -151,7 +255,7 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
async function handleExecutionWorkspaceRuntimeCommand(req: Request, res: Response) {
|
||||
const id = req.params.id as string;
|
||||
const action = String(req.params.action ?? "").trim().toLowerCase();
|
||||
if (action !== "start" && action !== "stop" && action !== "restart" && action !== "run") {
|
||||
if (action !== "start" && action !== "stop" && action !== "restart" && action !== "repair" && action !== "run") {
|
||||
res.status(404).json({ error: "Workspace command action not found" });
|
||||
return;
|
||||
}
|
||||
@@ -166,14 +270,6 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
sourceIssueId: existing.sourceIssueId,
|
||||
});
|
||||
|
||||
// Recover any managed runtime-control operation this workspace was stranded with, then
|
||||
// refuse only if one is genuinely still live. Authorization above still gates the caller,
|
||||
// so recovery never widens who may control the workspace.
|
||||
await workspaceOperationsSvc.assertRuntimeControlAvailable({
|
||||
executionWorkspaceId: existing.id,
|
||||
action,
|
||||
});
|
||||
|
||||
const workspaceCwd = existing.cwd;
|
||||
if (!workspaceCwd) {
|
||||
res.status(422).json({ error: "Execution workspace needs a local path before Paperclip can run workspace commands" });
|
||||
@@ -195,6 +291,7 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
and(
|
||||
eq(projectWorkspaces.id, existing.projectWorkspaceId),
|
||||
eq(projectWorkspaces.companyId, existing.companyId),
|
||||
eq(projectWorkspaces.projectId, existing.projectId),
|
||||
),
|
||||
)
|
||||
.then((rows) => rows[0] ?? null)
|
||||
@@ -221,6 +318,7 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
const configuredServices = effectiveRuntimeConfig
|
||||
? listConfiguredRuntimeServiceEntries({ workspaceRuntime: effectiveRuntimeConfig })
|
||||
: [];
|
||||
const repairRestartsRuntimeServices = action === "repair" && configuredServices.length > 0;
|
||||
const workspaceCommand = effectiveRuntimeConfig
|
||||
? findWorkspaceCommandDefinition(effectiveRuntimeConfig, target.workspaceCommandId ?? null)
|
||||
: null;
|
||||
@@ -267,6 +365,78 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
return;
|
||||
}
|
||||
|
||||
let repairSeedSource: CanonicalWorktreeSeedSource | null = null;
|
||||
let repairPreviousAttemptId: string | null = null;
|
||||
let repairCliArgs: string[] | null = null;
|
||||
if (action === "repair") {
|
||||
const manifestPath = path.join(workspaceCwd, ".paperclip", "seed-manifest.json");
|
||||
let manifest: {
|
||||
attemptId?: unknown;
|
||||
source?: { configPath?: unknown; instanceId?: unknown };
|
||||
targetInstanceId?: unknown;
|
||||
};
|
||||
try {
|
||||
manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
||||
} catch {
|
||||
throw unprocessable("Workspace seed manifest is malformed; repair source identity cannot be trusted.", {
|
||||
code: "workspace_repair_precondition_failed",
|
||||
reason: "seed_manifest_malformed",
|
||||
repairPhase: "precondition_validation",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
if (!projectWorkspace?.cwd) {
|
||||
throw new Error("Workspace repair requires a registered base project workspace.");
|
||||
}
|
||||
const expectedTargetInstanceId = resolveManagedWorkspaceInstanceId(workspaceCwd);
|
||||
if (!expectedTargetInstanceId) {
|
||||
throw new Error("Workspace repair cannot resolve the registered target instance.");
|
||||
}
|
||||
repairSeedSource = resolveCanonicalWorktreeSeedSource({
|
||||
registeredBaseWorkspaceCwd: projectWorkspace.cwd,
|
||||
targetConfigPath: path.join(workspaceCwd, ".paperclip", "config.json"),
|
||||
expectedTargetInstanceId,
|
||||
manifestSource: manifest.source,
|
||||
manifestTargetInstanceId: manifest.targetInstanceId,
|
||||
});
|
||||
repairPreviousAttemptId = typeof manifest.attemptId === "string" ? manifest.attemptId : null;
|
||||
|
||||
const baseWorkspaceCwd = repairSeedSource.baseWorkspaceCwd;
|
||||
if (!baseWorkspaceCwd) {
|
||||
throw new Error("Workspace repair source is not bound to a registered base project workspace.");
|
||||
}
|
||||
const cliRunner = path.join(baseWorkspaceCwd, "cli", "node_modules", "tsx", "dist", "cli.mjs");
|
||||
const cliEntry = path.join(baseWorkspaceCwd, "cli", "src", "index.ts");
|
||||
const cliDist = path.join(baseWorkspaceCwd, "cli", "dist", "index.js");
|
||||
repairCliArgs = isReadableFile(cliRunner) && isReadableFile(cliEntry)
|
||||
? [cliRunner, cliEntry]
|
||||
: isReadableFile(cliDist)
|
||||
? [cliDist]
|
||||
: null;
|
||||
if (!repairCliArgs) {
|
||||
throw new Error("Workspace repair cannot find a runnable Paperclip CLI in the base workspace.");
|
||||
}
|
||||
} catch (error) {
|
||||
throw unprocessable(
|
||||
error instanceof Error ? error.message : "Workspace repair source validation failed.",
|
||||
{
|
||||
code: "workspace_repair_precondition_failed",
|
||||
reason: "source_registration_invalid",
|
||||
repairPhase: "precondition_validation",
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// This check can reconcile a stale operation row, so repair source validation must
|
||||
// precede it. Invalid manifest diagnostics must fail before any operation or service
|
||||
// mutation, not merely before the reseed child process is spawned.
|
||||
await workspaceOperationsSvc.assertRuntimeControlAvailable({
|
||||
executionWorkspaceId: existing.id,
|
||||
action,
|
||||
});
|
||||
|
||||
const actor = getActorInfo(req);
|
||||
const recorder = workspaceOperationsSvc.createRecorder({
|
||||
companyId: existing.companyId,
|
||||
@@ -327,7 +497,11 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
}
|
||||
|
||||
const recordRuntimeControlOperation = () => recorder.recordOperation({
|
||||
phase: action === "stop" ? "workspace_teardown" : "workspace_provision",
|
||||
phase: action === "stop"
|
||||
? "workspace_teardown"
|
||||
: action === "repair"
|
||||
? "workspace_repair"
|
||||
: "workspace_provision",
|
||||
command: workspaceCommand?.command ?? `workspace command ${action}`,
|
||||
cwd: existing.cwd,
|
||||
metadata: {
|
||||
@@ -339,7 +513,7 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
runtimeServiceId: selectedRuntimeServiceId,
|
||||
serviceIndex: selectedServiceIndex,
|
||||
},
|
||||
run: async () => {
|
||||
run: async (reportProgress) => {
|
||||
const ensureWorkspaceAvailable = async () =>
|
||||
await ensurePersistedExecutionWorkspaceAvailable({
|
||||
base: {
|
||||
@@ -429,7 +603,242 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
else stderr = appendWithCap(stderr, chunk, WORKSPACE_CONTROL_OUTPUT_MAX_CHARS);
|
||||
};
|
||||
|
||||
if (action === "stop" || action === "restart") {
|
||||
if (action === "repair") {
|
||||
type RepairPhase =
|
||||
| "managed_stop"
|
||||
| "precondition_validation"
|
||||
| "target_backup"
|
||||
| "full_reseed"
|
||||
| "managed_restart"
|
||||
| "readiness_validation";
|
||||
const repairDiagnostics: Array<{
|
||||
phase: RepairPhase;
|
||||
status: "started" | "succeeded" | "failed";
|
||||
at: string;
|
||||
}> = [];
|
||||
let repairPhase: RepairPhase = "managed_stop";
|
||||
const repairPreconditionError = (
|
||||
status: 409 | 422,
|
||||
reason:
|
||||
| "seed_manifest_malformed"
|
||||
| "seed_manifest_instance_mismatch"
|
||||
| "source_instance_unavailable"
|
||||
| "paperclip_cli_unavailable",
|
||||
message: string,
|
||||
) => {
|
||||
const details = {
|
||||
code: "workspace_repair_precondition_failed",
|
||||
reason,
|
||||
repairPhase,
|
||||
};
|
||||
return status === 409
|
||||
? conflict(message, details)
|
||||
: unprocessable(message, details);
|
||||
};
|
||||
const reportRepairPhase = async (
|
||||
phase: RepairPhase,
|
||||
status: "started" | "succeeded" | "failed",
|
||||
) => {
|
||||
repairPhase = phase;
|
||||
repairDiagnostics.push({ phase, status, at: new Date().toISOString() });
|
||||
if (repairDiagnostics.length > 32) repairDiagnostics.splice(0, repairDiagnostics.length - 32);
|
||||
await reportProgress({
|
||||
metadata: {
|
||||
repairPhase,
|
||||
repairDiagnostics: [...repairDiagnostics],
|
||||
databaseOnly: true,
|
||||
worktreePreserved: true,
|
||||
},
|
||||
system: `Workspace repair ${phase}: ${status}.\n`,
|
||||
});
|
||||
};
|
||||
|
||||
try {
|
||||
await reportRepairPhase("managed_stop", "started");
|
||||
await stopRuntimeServicesForExecutionWorkspace({
|
||||
db,
|
||||
executionWorkspaceId: existing.id,
|
||||
workspaceCwd,
|
||||
});
|
||||
await reportRepairPhase("managed_stop", "succeeded");
|
||||
if (!repairSeedSource?.baseWorkspaceCwd || !repairCliArgs) {
|
||||
throw new Error("Workspace repair source preflight did not complete.");
|
||||
}
|
||||
const manifestPath = path.join(workspaceCwd, ".paperclip", "seed-manifest.json");
|
||||
const sourceConfigPath = repairSeedSource.configPath;
|
||||
const baseWorkspaceCwd = repairSeedSource.baseWorkspaceCwd;
|
||||
|
||||
await reportRepairPhase("target_backup", "started");
|
||||
const child = spawn(process.execPath, [
|
||||
...repairCliArgs,
|
||||
"worktree",
|
||||
"reseed",
|
||||
"--from-config",
|
||||
sourceConfigPath,
|
||||
"--to",
|
||||
workspaceCwd,
|
||||
"--seed-mode",
|
||||
"full",
|
||||
"--yes",
|
||||
"--backup-target",
|
||||
], {
|
||||
cwd: baseWorkspaceCwd,
|
||||
env: {
|
||||
...process.env,
|
||||
PAPERCLIP_SEED_EXPECTED_COMPANY_ID: existing.companyId,
|
||||
PAPERCLIP_WORKSPACE_BASE_CWD: baseWorkspaceCwd,
|
||||
PAPERCLIP_PROJECT_WORKSPACE_ID: existing.projectWorkspaceId ?? "",
|
||||
},
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
child.stdout?.on("data", (chunk: Buffer) => {
|
||||
void onLog("stdout", chunk.toString("utf8"));
|
||||
});
|
||||
child.stderr?.on("data", (chunk: Buffer) => {
|
||||
void onLog("stderr", chunk.toString("utf8"));
|
||||
});
|
||||
let repairCommandTimedOut = false;
|
||||
let repairCommandForceTimeout: NodeJS.Timeout | null = null;
|
||||
const repairCommandTimeout = setTimeout(() => {
|
||||
repairCommandTimedOut = true;
|
||||
child.kill("SIGTERM");
|
||||
repairCommandForceTimeout = setTimeout(() => child.kill("SIGKILL"), 5_000);
|
||||
repairCommandForceTimeout.unref?.();
|
||||
}, 20 * 60_000);
|
||||
repairCommandTimeout.unref?.();
|
||||
|
||||
let reseedObserved = false;
|
||||
const manifestPoll = setInterval(() => {
|
||||
if (reseedObserved || !existsSync(manifestPath)) return;
|
||||
try {
|
||||
const current = JSON.parse(readFileSync(manifestPath, "utf8")) as {
|
||||
attemptId?: unknown;
|
||||
state?: unknown;
|
||||
};
|
||||
if (
|
||||
typeof current.attemptId === "string"
|
||||
&& current.attemptId !== repairPreviousAttemptId
|
||||
&& (current.state === "pending" || current.state === "running" || current.state === "failed")
|
||||
) {
|
||||
reseedObserved = true;
|
||||
}
|
||||
} catch {
|
||||
// The atomic manifest writer makes this unlikely; the terminal
|
||||
// read below remains authoritative.
|
||||
}
|
||||
}, 100);
|
||||
manifestPoll.unref?.();
|
||||
const exitCode = await new Promise<number>((resolve, reject) => {
|
||||
child.once("error", reject);
|
||||
child.once("exit", (code) => resolve(code ?? 1));
|
||||
}).finally(() => {
|
||||
clearInterval(manifestPoll);
|
||||
clearTimeout(repairCommandTimeout);
|
||||
if (repairCommandForceTimeout) clearTimeout(repairCommandForceTimeout);
|
||||
});
|
||||
if (reseedObserved) {
|
||||
await reportRepairPhase("target_backup", "succeeded");
|
||||
await reportRepairPhase("full_reseed", "started");
|
||||
}
|
||||
if (exitCode !== 0) {
|
||||
let seedFailurePhase: string | null = null;
|
||||
try {
|
||||
const failedManifest = JSON.parse(readFileSync(manifestPath, "utf8")) as {
|
||||
phase?: unknown;
|
||||
state?: unknown;
|
||||
};
|
||||
seedFailurePhase = failedManifest.state === "failed" && typeof failedManifest.phase === "string"
|
||||
? failedManifest.phase
|
||||
: null;
|
||||
} catch {
|
||||
// The outer repair phase remains exact when no seed phase was persisted.
|
||||
}
|
||||
await reportProgress({
|
||||
metadata: { seedFailurePhase },
|
||||
system: seedFailurePhase ? `Workspace seed failed during ${seedFailurePhase}.\n` : null,
|
||||
});
|
||||
throw new Error(
|
||||
repairCommandTimedOut
|
||||
? `Workspace database repair command timed out during ${repairPhase}.`
|
||||
: `Workspace database repair command failed during ${repairPhase}.`,
|
||||
);
|
||||
}
|
||||
if (!reseedObserved) {
|
||||
await reportRepairPhase("target_backup", "succeeded");
|
||||
await reportRepairPhase("full_reseed", "started");
|
||||
}
|
||||
|
||||
const manifest = JSON.parse(readFileSync(manifestPath, "utf8")) as Record<string, unknown>;
|
||||
if (!isVerifiedWorktreeSeedManifest(manifest)) {
|
||||
throw new Error("Workspace reseed returned without a verified terminal manifest.");
|
||||
}
|
||||
const expectedInstanceId = resolveManagedWorkspaceInstanceId(workspaceCwd);
|
||||
if (manifest.targetInstanceId !== expectedInstanceId) {
|
||||
throw repairPreconditionError(
|
||||
422,
|
||||
"seed_manifest_instance_mismatch",
|
||||
"Verified seed manifest belongs to a different workspace instance.",
|
||||
);
|
||||
}
|
||||
resolveCanonicalWorktreeSeedSource({
|
||||
registeredBaseWorkspaceCwd: baseWorkspaceCwd,
|
||||
targetConfigPath: path.join(workspaceCwd, ".paperclip", "config.json"),
|
||||
expectedTargetInstanceId: repairSeedSource.targetInstanceId,
|
||||
manifestSource: manifest.source as { configPath?: unknown; instanceId?: unknown } | undefined,
|
||||
manifestTargetInstanceId: manifest.targetInstanceId,
|
||||
});
|
||||
await reportRepairPhase("full_reseed", "succeeded");
|
||||
|
||||
await reportRepairPhase("managed_restart", "started");
|
||||
let startedServices: Awaited<ReturnType<typeof startRuntimeServicesForWorkspaceControl>> = [];
|
||||
if (repairRestartsRuntimeServices) {
|
||||
const availableWorkspace = await ensureWorkspaceAvailable();
|
||||
if (!availableWorkspace) {
|
||||
throw new Error("Execution workspace needs a local path before Paperclip can restart it.");
|
||||
}
|
||||
startedServices = await startRuntimeServicesForWorkspaceControl({
|
||||
db,
|
||||
actor: {
|
||||
id: actor.agentId ?? null,
|
||||
name: actor.actorType === "user" ? "Board" : "Agent",
|
||||
companyId: existing.companyId,
|
||||
},
|
||||
issue: existing.sourceIssueId
|
||||
? { id: existing.sourceIssueId, identifier: null, title: existing.name }
|
||||
: null,
|
||||
workspace: availableWorkspace,
|
||||
executionWorkspaceId: existing.id,
|
||||
config: {
|
||||
workspaceRuntime: effectiveRuntimeConfig,
|
||||
runtimeProvisionCommand:
|
||||
existing.config?.runtimeProvisionCommand
|
||||
?? projectPolicy?.workspaceStrategy?.runtimeProvisionCommand
|
||||
?? null,
|
||||
},
|
||||
adapterEnv: {},
|
||||
onLog,
|
||||
recorder,
|
||||
});
|
||||
}
|
||||
runtimeServiceCount = startedServices.length;
|
||||
await reportRepairPhase("managed_restart", "succeeded");
|
||||
|
||||
await reportRepairPhase("readiness_validation", "started");
|
||||
if (
|
||||
repairRestartsRuntimeServices
|
||||
&& (
|
||||
startedServices.length === 0
|
||||
|| startedServices.some((service) => service.status !== "running" || service.healthStatus !== "healthy")
|
||||
)
|
||||
) {
|
||||
throw new Error("Managed restart did not produce healthy runtime services.");
|
||||
}
|
||||
await reportRepairPhase("readiness_validation", "succeeded");
|
||||
} catch (error) {
|
||||
await reportRepairPhase(repairPhase, "failed");
|
||||
throw error;
|
||||
}
|
||||
} else if (action === "stop" || action === "restart") {
|
||||
await stopRuntimeServicesForExecutionWorkspace({
|
||||
db,
|
||||
executionWorkspaceId: existing.id,
|
||||
@@ -481,7 +890,7 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
throw error;
|
||||
}
|
||||
runtimeServiceCount = startedServices.length;
|
||||
} else {
|
||||
} else if (action !== "repair") {
|
||||
runtimeServiceCount = selectedRuntimeServiceId ? Math.max(0, (existing.runtimeServices?.length ?? 1) - 1) : 0;
|
||||
}
|
||||
|
||||
@@ -504,7 +913,9 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
config: { workspaceRuntime: effectiveRuntimeConfig },
|
||||
currentDesiredState,
|
||||
currentServiceStates: existing.config?.serviceStates ?? null,
|
||||
action,
|
||||
action: action === "repair"
|
||||
? repairRestartsRuntimeServices ? "start" : "stop"
|
||||
: action,
|
||||
serviceIndex: selectedServiceIndex,
|
||||
});
|
||||
const metadata = mergeExecutionWorkspaceConfig(existing.metadata as Record<string, unknown> | null, {
|
||||
@@ -522,12 +933,17 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
? "Stopped execution workspace runtime services.\n"
|
||||
: action === "restart"
|
||||
? "Restarted execution workspace runtime services.\n"
|
||||
: "Started execution workspace runtime services.\n",
|
||||
: action === "repair"
|
||||
? repairRestartsRuntimeServices
|
||||
? "Repaired the isolated workspace database and restarted healthy runtime services.\n"
|
||||
: "Repaired the isolated workspace database; no managed runtime services were configured to restart.\n"
|
||||
: "Started execution workspace runtime services.\n",
|
||||
metadata: {
|
||||
runtimeServiceCount,
|
||||
workspaceCommandId: workspaceCommand?.id ?? target.workspaceCommandId ?? null,
|
||||
runtimeServiceId: selectedRuntimeServiceId,
|
||||
serviceIndex: selectedServiceIndex,
|
||||
runtimeRestarted: action === "repair" ? repairRestartsRuntimeServices : undefined,
|
||||
},
|
||||
};
|
||||
},
|
||||
@@ -565,7 +981,9 @@ export function executionWorkspaceRoutes(db: Db, opts: { pluginWorkerManager?: P
|
||||
// The operation is already terminal here. This also catches the recorder's own time
|
||||
// budget expiring on a start that never settles, which is the one failure the inner
|
||||
// handler cannot see — reconcile there too so no listener or desired state is left over.
|
||||
if (action === "start" || action === "restart") await reconcileFailedRuntimeStart(error);
|
||||
if (action === "start" || action === "restart" || action === "repair") {
|
||||
await reconcileFailedRuntimeStart(error);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user