mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 23:56:43 +01:00
feat(agents): persist agent files across tasks without revision history (#14420)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - An agent needs personal files across tasks and sessions. > - AGENTS.md is one file in that directory. Supporting files need the same persistence. > - The Instructions Editor and agent runs must share one current directory. > - Concurrent runs should apply only the files they change. The last sync of the same file wins. > - This pull request uses existing file transport and removes temporary copies after sync. > - Old instruction-only sessions keep their restore contract. New saves do not create revision history. ## Linked Issues or Issue Description Refs #14325. This replaces its revision-oriented design with persistent agent files. Keep #14325 unmerged. Transport prerequisite #14416 merged first at `d172197117a14b80a1eb2d2835a0e7cce2679656`. This PR now targets master and remains below 100 changed files. Related work: #4513 and #8798 cover instruction tooling. This change handles run synchronization, cross-task personal files, browser editing, and old-session restoration. ## What Changed - Keep one current directory per company and agent. Point AGENT_HOME at a temporary working copy for each active run. Keep task files and provider HOME separate. - Restore text, binary files, and nested folders through workspace transport. Exclude remote agent files from task Git snapshots with a self-ignoring file inside the reserved runtime directory; never write through repository-controlled Git metadata. - Collect after the provider and child processes have stopped. Keep resumable conversation state. - Apply changed and deleted files under the agent lock. The last sync wins for the same file. Unrelated concurrent changes survive. - Remove temporary copies after successful sync, rejected sync, and staging failure. Register ownership before copying so restart recovery can remove interrupted preparation. Retry transient synchronization up to three times. Preserve the original remote lease reference until deletion succeeds; restart cleanup never acquires a replacement sandbox. Do not create captured directories or a conflict-review queue for new runs. - Keep browser editing, stale-draft protection, and streaming binary downloads. Keep the instruction entry and text editor limited to 1 MiB. - Keep historical agent-folder sync failures on their affected runs instead of repeating them above current saved instructions. Preserve legacy candidate review and current browser-save errors. Avoid duplicate quota warnings while retaining separate sync failures when they describe a different problem. - Require target-scoped caller grants for peer instruction access, while preserving self edits, responsible-user checks, and protected-change consent. - Treat full storage as a nonblocking run warning, never an agent pause or run-admission failure. Restore already-over-quota saved folders so ordinary agent cleanup can recover; warn on each run until cleanup. The run detail view shows the warning. - Allow 256 MiB per file, 2 GiB per directory, and 100,000 entries. Hash large files as streams. Check editor-save quotas with metadata instead of hashing unrelated files. - Preserve old native inputs, instruction-only copies, paths, digests, and pending legacy candidates. Adopt old revision heads once. New writes do not append history rows. - Add idempotent migration 0287 and verify upgrades from the preview tables and receipts. - Add nine interactive stories under **Agents / Persistent files**, including automatic incoming edits, stale browser drafts, and storage-limit diagnostics. ## Verification - Merge candidate: `4f5390107ec6ffd80a76d1d2e85530e66f21d079`, after merging current master and the landed transport prerequisite. Integration required no manual conflict resolution; the feature remains 99 changed files. Full workspace typecheck, production build, token gates, and 715 focused tests passed on this merge candidate. Fresh Greptile review is 5/5 with no unresolved findings. All 55 checks passed, with four conditional skips, including the build, typecheck, browser E2E, and canary dry run. A single retry recovered four jobs interrupted by runner shutdowns; no source changes were required. - Historical-warning UI fix: all 6,834 UI tests across 640 files passed, including regression coverage for three old failures, legacy preserved edits, and warnings scoped to the affected run. Full workspace typecheck, production build, Storybook build, and token gates passed. Browser-verified Storybook playtests passed for Historical Failures After Successful Save, Storage Limit, and Full Storage Run Warning. - Review follow-ups at `4e20c9fb2`: all 18 focused tests passed, including external Git directories, linked worktrees, symlinks, hardlinks, and distinct I/O failures alongside storage warnings. Server and UI typechecks, token gates, and the production build passed. - Storage warning regressions at `0724f3012`: all 33 directory tests and all five heartbeat-list tests passed, with no skips in their successful runs. They cover repeated runs while full, an already-over-quota saved folder, cleanup, warnings retained after unrelated save failures, and bounded warnings in large result JSON. Server typecheck passed after the final warning fixes. - Full workspace typecheck, production build, and token gates passed during this follow-up. Product E2E harness: 631 tests passed across 52 files; harness typecheck passed. Earlier native session/context and directory/legacy collection suites passed 537 tests; Runner unit/transport suites passed 329 tests. - **Real E2E at `0724f3012` (before this follow-up):** legacy local Codex and native Daytona Codex each passed six tasks, one server restart, seven independent assertions, and cleanup verification. Both prove browser-to-agent edits, agent-to-browser edits, nested/binary restoration, per-file last-sync-wins, a successful run after an oversized save rejection, and cleanup clearing the warning. - Native local Codex also passed the six-task quota flow before the final warning-retention fixes. That pass began at `918d1ed02` while the bounded-result warning fix was being edited, so it is not claimed as exact-final-head evidence. Its final-head rerun failed during embedded PostgreSQL bootstrap before any provider run: the macOS host had 87,365 of 87,381 SysV semaphores occupied. No unrelated services or kernel limits were changed. - The final-source report intentionally records **2/3 cells passed**, preserving the blocked native-local attempt: `tests/runner-e2e/results/agent-files-quota-final-20260928-report/`. Earlier failed attempts and provenance notes remain under `tests/runner-e2e/results/agent-files-quota-final-20260928-input/` and the original campaign directories. - Daytona used immutable image `ghcr.io/paperclipai/paperclip-daytona-runner@sha256:5643f0d801417cae3581833a1a3bc6715b325e028602738d2652c44cac5dc6bf` and its exact Linux runner binary. Controller source is `0724f3012`; image source is recorded separately. - Legacy-session compatibility and all three ACP Stop/resume browser regressions passed on the prior validated feature head `169fab46d5af21caa2269b4c1b29b69c933a6951`. They assert the same provider session is retained and interrupted writes are not replayed. Migration upgrade tests also passed earlier. - Nine interactive stories are under **Agents / Persistent files**, including **Full Storage Run Warning**. Its playtest and visual browser inspection passed; the warning states that runs continue and the editor remains available. - Prior-head checks on `4e20c9fb2`: 55 passed, two conditional jobs skipped, no failures or pending checks. All eight browser E2E shards and their aggregate passed. Fresh Greptile review is 5/5 with no findings; all review threads are resolved, the security scan passed, and GitHub reports no merge conflicts. - The broad local follow-up test run was interrupted after host semaphore exhaustion affected isolated PostgreSQL instances. It also encountered the existing macOS long-path fixture failure and two timeout failures. This is not a claim that the full local suite passed. Logs are retained; focused storage/warning tests passed. ## Risks - A later sync can overwrite an earlier edit to the same file, including a saved browser edit. There is no text merge or retained version. This is the intended last-sync-wins policy. - A save that exceeds a storage limit is rejected and its temporary copy is discarded. The run itself continues normally, and later runs restore the last saved files with a warning until cleanup. Transient sync failures get bounded retries. An I/O failure partway through a sync can leave some files updated; a failed receipt does not claim whole-folder success. - Larger folders increase copy time, network traffic, and temporary disk usage. Active runs still need working copies. Terminal runs do not accumulate archives. Operators must provision disk for agents and configured concurrency; these limits are not company-wide quotas. - A restored old native session remains instruction-only until a fresh session starts. Its original conflict fence and existing pending candidates remain compatible. - Provider processes close at the collection boundary. Conversation resume remains available, but warm process reuse is lost. - Backups must include the instance filesystem and database. External bundles keep their existing behavior until explicitly moved to managed storage. ## Model Used OpenAI Codex, GPT-6 family. The session does not expose a more specific model ID or context-window size. Reasoning, code execution, and browser tools assisted this change. Real provider E2E uses `gpt-5.6-sol`. ## 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: Fry (Paperclip) <noreply@paperclip.ing>
This commit is contained in:
1 parent
d172197117
commit
3ca196b0a6
99 files changed
+7948
-261
No files matched your search
@@ -0,0 +1,258 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { agents, environmentLeases, environments, agentInstructionWorkingCopies as copies, type Db } from "@paperclipai/db";
|
||||
import { syncDirectoryToSsh, restoreWorkspaceFromSshExecution } from "@paperclipai/adapter-utils/ssh";
|
||||
import { prepareAdapterExecutionTargetRuntime, runAdapterExecutionTargetShellCommand, type AdapterExecutionTarget, type PreparedAdapterExecutionTargetRuntime } from "@paperclipai/adapter-utils/execution-target";
|
||||
import { withDirectoryMergeLock, directorySnapshotSha256, parseDirectorySnapshot, serializeDirectorySnapshot } from "@paperclipai/adapter-utils/workspace-restore-merge";
|
||||
import { AGENT_FILES_CONTRACT, AgentFileLimitError, agentFileStore, agentStorageWarning, inspectAgentDirectory } from "./agent-file-store.js";
|
||||
import { agentInstructionsBundleMode, deriveBundleState, resolveManagedInstructionsRoot } from "./agent-instructions.js";
|
||||
import { instructionGitExcludeProgram } from "./agent-instruction-files.js";
|
||||
import { resolveInstructionActor } from "./agent-instruction-authorization.js";
|
||||
import { HttpError, conflict, notFound } from "../errors.js";
|
||||
import type { AuthorizationActor } from "./authorization.js";
|
||||
import type { EnvironmentRuntimeService } from "./environment-runtime.js";
|
||||
import type { Environment, EnvironmentLease } from "@paperclipai/shared";
|
||||
import { hasRemoteTerminationReceipt } from "./remote-execution-termination.js";
|
||||
|
||||
type Copy = typeof copies.$inferSelect;
|
||||
const completed = new Set(["saved", "unchanged", "resolved", "unavailable"]);
|
||||
const transports = new Map<string, PreparedAdapterExecutionTargetRuntime>();
|
||||
const key = (row: Pick<Copy, "companyId" | "runId">) => `${row.companyId}:${row.runId}`;
|
||||
export function isAgentDirectoryCopy(row: Pick<Copy, "receipt"> | null): boolean {
|
||||
return row?.receipt?.schema === AGENT_FILES_CONTRACT;
|
||||
}
|
||||
function baseline(row: Copy) {
|
||||
const snapshot = parseDirectorySnapshot(row.receipt?.baseline);
|
||||
if (!snapshot || directorySnapshotSha256(snapshot) !== row.baseHash) throw new Error("Agent directory baseline is invalid");
|
||||
return snapshot;
|
||||
}
|
||||
function actor(row: Copy): AuthorizationActor {
|
||||
return { type: "agent", companyId: row.companyId, agentId: row.agentId, runId: row.runId, onBehalfOfUserId: row.responsibleUserId };
|
||||
}
|
||||
export function agentDirectoryWorkingCopyService(db: Db, get: (companyId: string, runId: string) => Promise<Copy | null>, patch: (row: Copy, values: Partial<typeof copies.$inferInsert>) => Promise<Copy>, environmentRuntime?: EnvironmentRuntimeService) {
|
||||
const store = agentFileStore(db);
|
||||
async function transport(row: Copy, target: AdapterExecutionTarget, recovering: boolean) {
|
||||
if (target.kind !== "remote" || row.location !== `remote:${target.environmentId ?? ""}`) throw new Error("Agent directory environment changed");
|
||||
if (recovering && target.transport === "ssh") throw new Error("The original SSH collector is unavailable");
|
||||
if (target.transport === "ssh") {
|
||||
// Reuse SSH's plain directory transfer without its task-workspace suffix
|
||||
// or Git-history discovery. The registered root is the exact writable root.
|
||||
await syncDirectoryToSsh({ spec: target.spec, localDir: row.localRoot, remoteDir: row.executionRoot, exclude: [".paperclip-runtime"] });
|
||||
return { target, workspaceRemoteDir: row.executionRoot, runtimeRootDir: null,
|
||||
assetDirs: {}, additionalSourceDirs: {}, additionalSourceFailures: [], workspaceSyncSnapshot: null,
|
||||
restoreWorkspace: () => restoreWorkspaceFromSshExecution({ spec: target.spec, localDir: row.localRoot,
|
||||
remoteDir: row.executionRoot, baselineSnapshot: { ...baseline(row), exclude: [".paperclip-runtime"] }, restoreGitHistory: false }) };
|
||||
}
|
||||
return prepareAdapterExecutionTargetRuntime({ target, runId: row.runId, adapterKey: "agent-files",
|
||||
workspaceLocalDir: row.localRoot, workspaceRemoteDir: row.executionRoot,
|
||||
syncWorkspace: true, workspaceInboundMode: recovering ? "adopt_remote" : undefined,
|
||||
workspaceBaseline: baseline(row), workspaceGitSnapshot: null, workspaceFileMode: "all",
|
||||
workspaceExclude: [".paperclip-runtime", ".paperclip-runtime/**"] });
|
||||
}
|
||||
async function prepare(input: { companyId: string; agentId: string; runId: string; target?: AdapterExecutionTarget | null; cwd: string }) {
|
||||
const [agent] = await db.select().from(agents).where(and(eq(agents.id, input.agentId), eq(agents.companyId, input.companyId)));
|
||||
if (!agent) throw notFound("Agent not found");
|
||||
if (agentInstructionsBundleMode(agent) !== "managed") return null;
|
||||
const bound = await resolveInstructionActor(db, { type: "agent", companyId: input.companyId, agentId: input.agentId, runId: input.runId });
|
||||
const root = resolveManagedInstructionsRoot(agent);
|
||||
const localRoot = path.join(path.dirname(root), "file-sync", "runs", input.runId, "live");
|
||||
// Local copies live outside the task cwd. Remote copies use the reserved,
|
||||
// excluded runtime area inside the provider's confined workspace. They are
|
||||
// synchronized independently; provider HOME is never reinterpreted.
|
||||
const executionRoot = input.target?.kind === "remote"
|
||||
? path.posix.join(input.target.remoteCwd, ".paperclip-runtime", "agent-files", input.agentId, input.runId)
|
||||
: localRoot;
|
||||
const location = input.target?.kind === "remote" ? `remote:${input.target.environmentId ?? ""}` : "local";
|
||||
let row = await get(input.companyId, input.runId);
|
||||
if (row && (row.localRoot !== localRoot || row.executionRoot !== executionRoot || row.agentId !== input.agentId || row.location !== location)) throw conflict("Agent directory belongs to a different execution environment");
|
||||
if (row && !completed.has(row.state) && row.state !== "preparing") {
|
||||
if (input.target?.kind === "remote" && !transports.has(key(row))) transports.set(key(row), await transport(row, input.target, true));
|
||||
return row;
|
||||
}
|
||||
// Register ownership before creating any bytes, so a crash during the copy
|
||||
// leaves a recoverable preparing receipt instead of an orphan directory.
|
||||
const preparing = { entryFile: deriveBundleState(agent).entryFile, baseRevisionId: null, baseHash: "preparing",
|
||||
localRoot, executionRoot, location, state: "preparing", candidateBase64: null, candidateHash: null,
|
||||
receipt: { schema: AGENT_FILES_CONTRACT, ...(input.target?.kind === "remote"
|
||||
? { cleanup: { leaseId: input.target.leaseId ?? null, remoteCwd: input.target.remoteCwd } } : {}) }, processStoppedAt: null, attempts: 0,
|
||||
nextAttemptAt: null, errorCode: null, errorMessage: null };
|
||||
if (row) row = await patch(row, preparing);
|
||||
else {
|
||||
await db.insert(copies).values({ runId: input.runId, companyId: input.companyId, agentId: input.agentId, responsibleUserId: bound.onBehalfOfUserId!, ...preparing });
|
||||
row = (await get(input.companyId, input.runId))!;
|
||||
}
|
||||
const { snapshot, storageWarning } = await store.locked(input.companyId, input.agentId, bound, false, async (_tx, _agent, canonical) => {
|
||||
// Storage quotas govern saves, never admission to a future run. Restore
|
||||
// existing bytes so the agent can work normally and remove excess files.
|
||||
// Path/link validation remains mandatory even for an over-quota folder.
|
||||
const inspected = await inspectAgentDirectory(canonical, false);
|
||||
// A stopped lifecycle has already accounted for all its bytes. No live
|
||||
// or pending candidate ever enters this replacement path.
|
||||
await fs.rm(localRoot, { recursive: true, force: true });
|
||||
await fs.mkdir(path.dirname(localRoot), { recursive: true, mode: 0o700 });
|
||||
try {
|
||||
await fs.cp(canonical, localRoot, { recursive: true, preserveTimestamps: true });
|
||||
return inspected;
|
||||
} catch (error) {
|
||||
await fs.rm(path.dirname(localRoot), { recursive: true, force: true });
|
||||
throw error;
|
||||
}
|
||||
}).catch(async error => {
|
||||
await fs.rm(path.dirname(localRoot), { recursive: true, force: true });
|
||||
throw error;
|
||||
});
|
||||
const values = { entryFile: deriveBundleState(agent).entryFile, baseRevisionId: null, baseHash: directorySnapshotSha256(snapshot),
|
||||
localRoot, executionRoot, location, state: "preparing", candidateBase64: null, candidateHash: null,
|
||||
receipt: { ...preparing.receipt, baseline: serializeDirectorySnapshot(snapshot), storageWarning },
|
||||
processStoppedAt: null, attempts: 0, nextAttemptAt: null, errorCode: null, errorMessage: null };
|
||||
try {
|
||||
row = await patch(row, values);
|
||||
} catch (error) {
|
||||
await fs.rm(path.dirname(localRoot), { recursive: true, force: true });
|
||||
throw error;
|
||||
}
|
||||
try {
|
||||
if (input.target?.kind === "remote") {
|
||||
const quote = (value: string) => `'${value.replaceAll("'", `'"'"'`)}'`;
|
||||
const excluded = await runAdapterExecutionTargetShellCommand(input.runId, input.target,
|
||||
`node -e ${quote(instructionGitExcludeProgram)} ${quote(input.target.remoteCwd)}`,
|
||||
{ cwd: input.target.remoteCwd, env: {}, timeoutSec: 15 });
|
||||
if (excluded.exitCode !== 0 || excluded.timedOut) throw new Error("Could not exclude agent files from task Git staging");
|
||||
transports.set(key(row), await transport(row, input.target, false));
|
||||
}
|
||||
return await patch(row, { state: "prepared" });
|
||||
} catch (error) {
|
||||
// Preparation failed before a provider could start using this copy.
|
||||
row = await patch(row, { state: "unavailable", processStoppedAt: new Date(), errorCode: "AGENT_FILES_PREPARE_FAILED",
|
||||
errorMessage: "Agent files could not be staged. The temporary copy was discarded.", nextAttemptAt: null });
|
||||
await release(row, input.target);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function retrieve(row: Copy, target?: AdapterExecutionTarget | null) {
|
||||
if (row.location !== "local") {
|
||||
let runtime = transports.get(key(row));
|
||||
if (!runtime) {
|
||||
if (!target) throw new Error("Agent directory transport is unavailable");
|
||||
runtime = await transport(row, target, true);
|
||||
transports.set(key(row), runtime);
|
||||
}
|
||||
await runtime.restoreWorkspace();
|
||||
}
|
||||
return inspectAgentDirectory(row.localRoot);
|
||||
}
|
||||
async function hasChanges(_row: Copy, _target?: AdapterExecutionTarget | null) {
|
||||
// A whole-directory collector needs a quiescent provider, including its
|
||||
// child processes. Checkpoint and close at the turn boundary before reading
|
||||
// either local or remote files. The provider conversation remains resumable;
|
||||
// ordinary file edits do not change the session configuration digest.
|
||||
return true;
|
||||
}
|
||||
async function collectStopped(row: Copy, target?: AdapterExecutionTarget | null) {
|
||||
if (completed.has(row.state)) { await release(row); return (await get(row.companyId, row.runId))!; }
|
||||
row = await patch(row, { processStoppedAt: row.processStoppedAt ?? new Date() });
|
||||
// The stopped working copy is the only temporary tree. Retry transient I/O
|
||||
// at this boundary, then clean it up; there are no preserved run snapshots.
|
||||
let inspected: Awaited<ReturnType<typeof retrieve>> | undefined;
|
||||
for (;;) {
|
||||
row = await patch(row, { attempts: row.attempts + 1 });
|
||||
try {
|
||||
inspected ??= await retrieve(row, target);
|
||||
const { snapshot } = inspected;
|
||||
const candidateHash = directorySnapshotSha256(snapshot);
|
||||
let storageWarning = inspected.storageWarning;
|
||||
if (candidateHash === row.baseHash) {
|
||||
row = await patch(row, { state: "unchanged", errorCode: null, errorMessage: null, nextAttemptAt: null });
|
||||
} else {
|
||||
({ storageWarning } = await store.apply({ companyId: row.companyId, agentId: row.agentId, sourceDir: row.localRoot, baseline: baseline(row) }, actor(row)));
|
||||
row = await patch(row, { state: "saved", candidateHash, errorCode: null, errorMessage: null, nextAttemptAt: null });
|
||||
}
|
||||
row = await patch(row, { receipt: { ...row.receipt, storageWarning } });
|
||||
break;
|
||||
} catch (error) {
|
||||
const retryable = !(error instanceof HttpError) || error.status >= 500;
|
||||
if (retryable && row.attempts < 3) continue;
|
||||
row = await patch(row, { state: "unavailable", nextAttemptAt: null,
|
||||
receipt: { ...row.receipt, storageWarning: error instanceof AgentFileLimitError ? agentStorageWarning(error.message) : row.receipt?.storageWarning ?? null },
|
||||
errorCode: error instanceof AgentFileLimitError ? "AGENT_FILES_LIMIT_EXCEEDED" : "AGENT_FILES_SAVE_FAILED",
|
||||
errorMessage: error instanceof HttpError && error.status === 422
|
||||
? `${error.message}. This run's agent-folder changes were not saved; the temporary copy is discarded.`
|
||||
: "Agent-file synchronization failed. No successful save is claimed; the temporary copy is discarded.",
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
await release(row);
|
||||
return (await get(row.companyId, row.runId))!;
|
||||
}
|
||||
async function release(row: Copy, target?: AdapterExecutionTarget | null) {
|
||||
// Collection and environment teardown can both release the same copy.
|
||||
// A compact successful cleanup receipt is final, even after restart.
|
||||
if (completed.has(row.state) && row.processStoppedAt && row.receipt?.cleanupPending === false) return;
|
||||
const runtime = transports.get(key(row));
|
||||
transports.delete(key(row));
|
||||
let cleanupPending = false;
|
||||
await runtime?.cleanupWorkspaceSnapshot?.().catch(() => { cleanupPending = true; });
|
||||
const cleanupTarget = runtime?.target ?? target;
|
||||
if (row.processStoppedAt && completed.has(row.state) && cleanupTarget?.kind === "remote") {
|
||||
const expected = path.posix.join(cleanupTarget.remoteCwd, ".paperclip-runtime", "agent-files", row.agentId, row.runId);
|
||||
if (row.executionRoot !== expected) throw new Error("Agent directory cleanup path changed");
|
||||
const quoted = `'${expected.replaceAll("'", `'"'"'`)}'`;
|
||||
const remoteCleanupFailed = await runAdapterExecutionTargetShellCommand(row.runId, cleanupTarget, `rm -rf -- ${quoted}`,
|
||||
{ cwd: cleanupTarget.remoteCwd, env: {}, timeoutSec: 15 }).then(result => result.exitCode !== 0 || result.timedOut, () => true);
|
||||
cleanupPending ||= remoteCleanupFailed;
|
||||
} else if (row.processStoppedAt && completed.has(row.state) && row.location !== "local") {
|
||||
// Rebind only the original host-owned lease. Never acquire a replacement
|
||||
// sandbox or persist transport credentials in a working-copy receipt.
|
||||
cleanupPending ||= !await cleanupRemoteAfterRestart(row).catch(() => false);
|
||||
}
|
||||
if (completed.has(row.state) && row.processStoppedAt) {
|
||||
try { await fs.rm(path.dirname(row.localRoot), { recursive: true, force: true }); }
|
||||
catch {
|
||||
// Leave the baseline marker so restart recovery retries failed cleanup.
|
||||
await patch(row, { receipt: { ...row.receipt, cleanupPending: true } });
|
||||
return;
|
||||
}
|
||||
await patch(row, { receipt: { schema: AGENT_FILES_CONTRACT, state: row.state, appliedCandidateHash: row.candidateHash, storageWarning: row.receipt?.storageWarning ?? null, cleanupPending,
|
||||
...(cleanupPending ? { cleanup: row.receipt?.cleanup } : {}) },
|
||||
nextAttemptAt: cleanupPending ? new Date(Date.now() + 30_000) : null });
|
||||
}
|
||||
}
|
||||
async function cleanupRemoteAfterRestart(row: Copy): Promise<boolean> {
|
||||
const cleanup = row.receipt?.cleanup as { leaseId?: string; remoteCwd?: string } | undefined;
|
||||
// Older preview receipts did not record the lease ID. Accept only a unique
|
||||
// lease still bound to this run and environment in that compatibility case.
|
||||
const leases = await db.select().from(environmentLeases).where(and(
|
||||
eq(environmentLeases.companyId, row.companyId),
|
||||
cleanup?.leaseId ? eq(environmentLeases.id, cleanup.leaseId) : and(
|
||||
eq(environmentLeases.environmentId, row.location.slice("remote:".length)), eq(environmentLeases.heartbeatRunId, row.runId)),
|
||||
));
|
||||
if (leases.length !== 1) return false;
|
||||
const lease = leases[0]!;
|
||||
const termination = lease.metadata?.remoteExecutionTermination as { state?: string } | undefined;
|
||||
if (hasRemoteTerminationReceipt(lease) && termination?.state === "destroyed") return true;
|
||||
if (!environmentRuntime || !lease.environmentId || row.location !== `remote:${lease.environmentId}`) return false;
|
||||
const [environment] = await db.select().from(environments).where(eq(environments.id, lease.environmentId));
|
||||
if (!environment) return false;
|
||||
const remoteCwd = cleanup?.remoteCwd ?? lease.metadata?.remoteCwd;
|
||||
if (typeof remoteCwd !== "string" || row.executionRoot !== path.posix.join(remoteCwd, ".paperclip-runtime", "agent-files", row.agentId, row.runId)) return false;
|
||||
const result = await environmentRuntime.execute({ environment: environment as Environment, lease: lease as EnvironmentLease, command: "rm", args: ["-rf", "--", row.executionRoot],
|
||||
cwd: remoteCwd, env: {}, timeoutMs: 15_000, bypassSession: true });
|
||||
return result.exitCode === 0 && !result.timedOut;
|
||||
}
|
||||
async function serial<T>(row: Copy, fn: (current: Copy) => Promise<T>): Promise<T> {
|
||||
// Duplicate stop callbacks and restart recovery must not race while moving
|
||||
// the stopped working copy. This lock is outside the writable tree.
|
||||
return withDirectoryMergeLock(path.resolve(row.localRoot, "../../.."), async () => {
|
||||
const current = await get(row.companyId, row.runId);
|
||||
if (!current) throw notFound("Agent directory copy not found");
|
||||
return fn(current);
|
||||
});
|
||||
}
|
||||
return { prepare, hasChanges,
|
||||
collectStopped: (row: Copy, target?: AdapterExecutionTarget | null) => serial(row, current => collectStopped(current, target)),
|
||||
release: (row: Copy) => serial(row, release),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import fs from "node:fs/promises";
|
||||
import { constants } from "node:fs";
|
||||
import { Readable } from "node:stream";
|
||||
import path from "node:path";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { agents, activityLog, agentInstructionHeads, agentInstructionRevisions, type Db } from "@paperclipai/db";
|
||||
import { captureDirectorySnapshot, mergeDirectoryWithBaseline, type DirectorySnapshot } from "@paperclipai/adapter-utils/workspace-restore-merge";
|
||||
import { HttpError, conflict, notFound, unprocessable } from "../errors.js";
|
||||
import { authorizeInstructionCommit, authorizeInstructionRead } from "./agent-instruction-authorization.js";
|
||||
import { assertInstructionPathSafe, instructionPath, instructionBytes, materializeInstructionBytes, readInstructionBytes, MAX_INSTRUCTION_BYTES } from "./agent-instruction-files.js";
|
||||
import { agentInstructionsBundleMode, deriveBundleState, resolveManagedInstructionsRoot } from "./agent-instructions.js";
|
||||
import type { AuthorizationActor } from "./authorization.js";
|
||||
|
||||
type Tx = Parameters<Parameters<Db["transaction"]>[0]>[0];
|
||||
type Agent = typeof agents.$inferSelect;
|
||||
export const AGENT_FILES_CONTRACT = "paperclip.agent-files.v1";
|
||||
export const MAX_AGENT_FILE_BYTES = 256 * 1024 * 1024;
|
||||
export const MAX_AGENT_DIRECTORY_BYTES = 2 * 1024 * 1024 * 1024;
|
||||
export const MAX_AGENT_DIRECTORY_ENTRIES = 100_000;
|
||||
export const fileHash = (bytes: Uint8Array) => createHash("sha256").update(bytes).digest("hex");
|
||||
|
||||
export class AgentFileLimitError extends HttpError {
|
||||
constructor(message: string) {
|
||||
super(422, message, { code: "AGENT_FILES_LIMIT_EXCEEDED" });
|
||||
}
|
||||
}
|
||||
function assertFileSize(size: number, relative: string) {
|
||||
if (size > MAX_AGENT_FILE_BYTES) throw new AgentFileLimitError(`Agent file "${relative}" exceeds the 256 MiB per-file limit`);
|
||||
}
|
||||
function assertDirectorySize(size: number, count: number) {
|
||||
if (size > MAX_AGENT_DIRECTORY_BYTES) throw new AgentFileLimitError("Agent directory exceeds the 2 GiB total storage limit");
|
||||
if (count > MAX_AGENT_DIRECTORY_ENTRIES) throw new AgentFileLimitError("Agent directory exceeds the 100,000-entry limit (files and folders)");
|
||||
}
|
||||
export function agentStorageWarning(detail: string) {
|
||||
return `Agent storage is full. ${detail}. Runs can continue; remove or shrink files in AGENT_HOME to free space. Changes exceeding the storage limits will not be saved.`;
|
||||
}
|
||||
function storageWarning(size: number, count: number, fullFile?: string) {
|
||||
if (size >= MAX_AGENT_DIRECTORY_BYTES) return agentStorageWarning("The agent folder has reached its 2 GiB limit");
|
||||
if (count >= MAX_AGENT_DIRECTORY_ENTRIES) return agentStorageWarning("The agent folder has reached its 100,000-entry limit");
|
||||
if (fullFile) return agentStorageWarning(`Agent file "${fullFile}" has reached its 256 MiB limit`);
|
||||
return null;
|
||||
}
|
||||
|
||||
export function agentFilePath(value: string): string {
|
||||
const relative = instructionPath(value);
|
||||
if (relative.split("/").includes(".paperclip-runtime") || relative === "promptTemplate.legacy.md") {
|
||||
throw unprocessable(`${relative} is reserved and cannot be used for agent files`);
|
||||
}
|
||||
return relative;
|
||||
}
|
||||
|
||||
/** Compatibility ETag for clients whose old schema requires a UUID. This is a
|
||||
* content token, not a revision ID: no snapshot or history row is created. */
|
||||
export function agentFileToken(bytes: Uint8Array): string {
|
||||
return agentFileTokenFromHash(fileHash(bytes));
|
||||
}
|
||||
export function agentFileTokenFromHash(h: string): string {
|
||||
return `${h.slice(0, 8)}-${h.slice(8, 12)}-8${h.slice(13, 16)}-a${h.slice(17, 20)}-${h.slice(20, 32)}`;
|
||||
}
|
||||
|
||||
/** Call under the agent row lock. Import deployed revision heads once, then the
|
||||
* directory is authoritative. The adoption marker lives OUTSIDE agent files. */
|
||||
export async function adoptAgentFiles(tx: Tx, agent: Agent): Promise<string> {
|
||||
if (agentInstructionsBundleMode(agent) === "external") throw unprocessable("External instructions must be migrated to managed storage first");
|
||||
const root = resolveManagedInstructionsRoot(agent);
|
||||
const markerRoot = path.join(path.dirname(root), "file-sync");
|
||||
const marker = "adopted.json";
|
||||
await assertInstructionPathSafe(root, deriveBundleState(agent).entryFile);
|
||||
await fs.mkdir(root, { recursive: true, mode: 0o700 });
|
||||
const filename = await assertInstructionPathSafe(markerRoot, marker);
|
||||
const adopted = await fs.readFile(filename, "utf8").catch((error: NodeJS.ErrnoException) => {
|
||||
if (error.code === "ENOENT") return null;
|
||||
throw error;
|
||||
});
|
||||
if (adopted !== null) {
|
||||
if (JSON.parse(adopted).schema !== AGENT_FILES_CONTRACT) throw new Error("Unsupported agent file contract");
|
||||
return root;
|
||||
}
|
||||
const legacy = await tx.select({ entryFile: agentInstructionHeads.entryFile, contentBase64: agentInstructionRevisions.contentBase64 })
|
||||
.from(agentInstructionHeads).innerJoin(agentInstructionRevisions, eq(agentInstructionHeads.revisionId, agentInstructionRevisions.id))
|
||||
.where(and(eq(agentInstructionHeads.companyId, agent.companyId), eq(agentInstructionHeads.agentId, agent.id)));
|
||||
for (const entry of legacy) await materializeInstructionBytes(root, entry.entryFile, Buffer.from(entry.contentBase64, "base64"));
|
||||
await materializeInstructionBytes(markerRoot, marker, Buffer.from(JSON.stringify({ schema: AGENT_FILES_CONTRACT })));
|
||||
return root;
|
||||
}
|
||||
|
||||
async function openAgentFile(root: string, relative: string) {
|
||||
const filename = await assertInstructionPathSafe(root, relative);
|
||||
const handle = await fs.open(filename, constants.O_RDONLY | constants.O_NOFOLLOW).catch((error: NodeJS.ErrnoException) => {
|
||||
if (error.code === "ENOENT") return null;
|
||||
throw error;
|
||||
});
|
||||
if (!handle) return null;
|
||||
try {
|
||||
const stat = await handle.stat();
|
||||
if (!stat.isFile() || stat.nlink !== 1) throw unprocessable("Agent files must be regular files without links");
|
||||
assertFileSize(stat.size, relative);
|
||||
return { handle, size: stat.size };
|
||||
} catch (error) { await handle.close(); throw error; }
|
||||
}
|
||||
|
||||
/** Hash large files incrementally. Only editor-sized content is retained in RAM. */
|
||||
export async function inspectAgentFile(root: string, relative: string, bufferLimit = MAX_INSTRUCTION_BYTES) {
|
||||
const opened = await openAgentFile(root, relative);
|
||||
if (!opened) return null;
|
||||
const hash = createHash("sha256");
|
||||
let chunks: Buffer[] | null = opened.size <= bufferLimit ? [] : null;
|
||||
let size = 0;
|
||||
try {
|
||||
for await (const chunk of opened.handle.createReadStream()) {
|
||||
size += chunk.length;
|
||||
assertFileSize(size, relative);
|
||||
hash.update(chunk);
|
||||
if (size > bufferLimit) chunks = null;
|
||||
chunks?.push(chunk);
|
||||
}
|
||||
return { size, hash: hash.digest("hex"), bytes: chunks ? Buffer.concat(chunks) : null };
|
||||
} finally { await opened.handle.close(); }
|
||||
}
|
||||
|
||||
export async function readAgentFile(root: string, relative: string): Promise<Buffer | null> {
|
||||
return (await inspectAgentFile(root, relative, MAX_AGENT_FILE_BYTES))?.bytes ?? null;
|
||||
}
|
||||
|
||||
/** Validate before staging and again after provider stop; never follow links or
|
||||
* silently skip an unsupported file. Bounds apply to bytes, including binaries. */
|
||||
async function scanAgentFiles(root: string, enforceLimits = true) {
|
||||
await assertInstructionPathSafe(root, ".path-check");
|
||||
let size = 0, count = 0;
|
||||
const entries = new Set<string>();
|
||||
let fullFile: string | undefined;
|
||||
async function walk(dir: string) {
|
||||
for (const item of await fs.readdir(path.join(root, dir), { withFileTypes: true })) {
|
||||
const relative = agentFilePath(dir ? `${dir}/${item.name}` : item.name);
|
||||
const stat = await fs.lstat(path.join(root, relative));
|
||||
count++;
|
||||
if (enforceLimits) assertDirectorySize(size, count);
|
||||
entries.add(relative);
|
||||
if (stat.isDirectory()) await walk(relative);
|
||||
else if (stat.isFile() && stat.nlink === 1) {
|
||||
size += stat.size;
|
||||
if (stat.size >= MAX_AGENT_FILE_BYTES) fullFile ??= relative;
|
||||
if (enforceLimits) {
|
||||
assertFileSize(stat.size, relative);
|
||||
assertDirectorySize(size, count);
|
||||
}
|
||||
} else throw unprocessable("Agent directories support regular files and directories, without links or special files");
|
||||
}
|
||||
}
|
||||
await walk("");
|
||||
return { size, entries, storageWarning: storageWarning(size, count, fullFile) };
|
||||
}
|
||||
|
||||
export async function inspectAgentDirectory(root: string, enforceLimits = true) {
|
||||
const usage = await scanAgentFiles(root, enforceLimits);
|
||||
return { snapshot: await captureDirectorySnapshot(root), storageWarning: usage.storageWarning };
|
||||
}
|
||||
export async function snapshotAgentFiles(root: string, enforceLimits = true): Promise<DirectorySnapshot> {
|
||||
return (await inspectAgentDirectory(root, enforceLimits)).snapshot;
|
||||
}
|
||||
|
||||
export function agentFileStore(db: Db) {
|
||||
async function locked<T>(companyId: string, agentId: string, actor: AuthorizationActor, write: boolean,
|
||||
fn: (tx: Tx, agent: Agent, root: string, bound: AuthorizationActor) => Promise<T>) {
|
||||
return db.transaction(async tx => {
|
||||
const [agent] = await tx.select().from(agents).where(and(eq(agents.companyId, companyId), eq(agents.id, agentId))).for("update");
|
||||
if (!agent) throw notFound("Agent not found");
|
||||
const bound = write ? await authorizeInstructionCommit(tx, actor, agent) : await authorizeInstructionRead(tx, actor, agent);
|
||||
const root = await adoptAgentFiles(tx, agent);
|
||||
return fn(tx, agent, root, bound);
|
||||
});
|
||||
}
|
||||
async function audit(tx: Tx, agent: Agent, actor: AuthorizationActor, details: Record<string, unknown>) {
|
||||
await tx.insert(activityLog).values({ companyId: agent.companyId, actorType: actor.type === "board" ? "user" : "agent",
|
||||
actorId: (actor.type === "board" ? actor.userId : actor.agentId)!, agentId: actor.type === "agent" ? actor.agentId : null,
|
||||
runId: actor.runId, responsibleUserId: actor.type === "board" ? actor.userId : actor.onBehalfOfUserId,
|
||||
action: "agent.files_updated", entityType: "agent", entityId: agent.id, details });
|
||||
}
|
||||
return {
|
||||
locked,
|
||||
download: async (companyId: string, agentId: string, relative: string, actor: AuthorizationActor) => {
|
||||
const opened: { file: Awaited<ReturnType<typeof openAgentFile>> } = { file: null };
|
||||
try {
|
||||
const file = await locked(companyId, agentId, actor, false, async (_tx, _agent, root) => {
|
||||
opened.file = await openAgentFile(root, instructionPath(relative));
|
||||
return opened.file;
|
||||
});
|
||||
// Transfer outside the database lock. The open descriptor pins the file
|
||||
// across concurrent atomic replacements; end bounds concurrent growth.
|
||||
if (!file) return null;
|
||||
if (file.size === 0) { await file.handle.close(); return { size: 0, stream: Readable.from([]) }; }
|
||||
return { size: file.size, stream: file.handle.createReadStream({ end: file.size - 1 }) };
|
||||
} catch (error) { await opened.file?.handle.close(); throw error; }
|
||||
},
|
||||
read: (companyId: string, agentId: string, relative: string, actor: AuthorizationActor) =>
|
||||
locked(companyId, agentId, actor, false, (_tx, _agent, root) => readAgentFile(root, instructionPath(relative))),
|
||||
write: (input: { companyId: string; agentId: string; path: string; bytes: Buffer | null; baseHash: string | null }, actor: AuthorizationActor) =>
|
||||
locked(input.companyId, input.agentId, actor, true, async (tx, agent, root, bound) => {
|
||||
const relative = agentFilePath(input.path);
|
||||
if (input.bytes) assertFileSize(input.bytes.length, relative);
|
||||
const previous = await inspectAgentFile(root, relative);
|
||||
const currentHash = previous?.hash ?? null;
|
||||
const incomingHash = input.bytes === null ? null : fileHash(input.bytes);
|
||||
if (currentHash === incomingHash) return { contentHash: currentHash, changed: false };
|
||||
if (currentHash !== input.baseHash) throw conflict("This file changed since it was read. Reload before saving.", { code: "AGENT_FILE_CONFLICT", path: relative, currentHash });
|
||||
if (input.bytes === null && relative === deriveBundleState(agent).entryFile) throw unprocessable("The configured instruction entry cannot be deleted");
|
||||
if (input.bytes !== null) {
|
||||
if (relative === deriveBundleState(agent).entryFile) instructionBytes(input.bytes);
|
||||
// A quota check needs metadata only. Do not hash unrelated large
|
||||
// files while holding the editor's row lock for a one-file save.
|
||||
const snapshot = await scanAgentFiles(root);
|
||||
const total = snapshot.size + input.bytes.length - (previous?.size ?? 0);
|
||||
const newEntries = relative.split("/").map((_part, i, parts) => parts.slice(0, i + 1).join("/")).filter(name => !snapshot.entries.has(name)).length;
|
||||
assertDirectorySize(total, snapshot.entries.size + newEntries);
|
||||
}
|
||||
if (input.bytes === null) await fs.unlink(await assertInstructionPathSafe(root, relative));
|
||||
else await materializeInstructionBytes(root, relative, input.bytes);
|
||||
await audit(tx, agent, bound, { path: relative, contentHash: incomingHash });
|
||||
return { contentHash: incomingHash, changed: true };
|
||||
}),
|
||||
apply: (input: { companyId: string; agentId: string; sourceDir: string; baseline: DirectorySnapshot }, actor: AuthorizationActor) =>
|
||||
locked(input.companyId, input.agentId, actor, true, async (tx, agent, root, bound) => {
|
||||
const incoming = await snapshotAgentFiles(input.sourceDir);
|
||||
const entry = await readInstructionBytes(input.sourceDir, deriveBundleState(agent).entryFile);
|
||||
if (entry === null) throw unprocessable("The configured instruction entry cannot be deleted");
|
||||
instructionBytes(entry);
|
||||
// Previously saved/imported bytes must remain readable, including when
|
||||
// over quota. Enforce limits on the incoming and resulting tree so a
|
||||
// run can delete files to recover instead of being locked out forever.
|
||||
const current = await snapshotAgentFiles(root, false);
|
||||
// Rebase only the run's changed paths onto the current tree. This makes
|
||||
// same-file edits/deletions last-sync-wins while untouched files retain
|
||||
// changes from other runs. Ancestors may need recreating after a writer
|
||||
// replaced a directory with a file.
|
||||
const entries = new Map(input.baseline.entries);
|
||||
const changed = [...new Set([...input.baseline.entries, ...incoming.entries].map(([name]) => name))]
|
||||
.filter(name => JSON.stringify(input.baseline.entries.get(name)) !== JSON.stringify(incoming.entries.get(name)));
|
||||
for (const name of changed) {
|
||||
const present = current.entries.get(name);
|
||||
if (present) entries.set(name, present); else entries.delete(name);
|
||||
if (incoming.entries.has(name)) {
|
||||
for (let parent = path.posix.dirname(name); parent !== "."; parent = path.posix.dirname(parent)) {
|
||||
if (current.entries.get(parent)?.kind === "dir") continue;
|
||||
const before = current.entries.get(parent);
|
||||
if (before) entries.set(parent, before); else entries.delete(parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
const applyBaseline = { ...input.baseline, entries };
|
||||
const finalEntries = new Map([...current.entries].map(([name, value]) => [name, { value, root }]));
|
||||
for (const name of changed) if (!incoming.entries.has(name)) finalEntries.delete(name);
|
||||
for (const [name, value] of incoming.entries) {
|
||||
if (JSON.stringify(input.baseline.entries.get(name)) === JSON.stringify(value)) continue;
|
||||
if (value.kind !== "dir") for (const child of finalEntries.keys()) if (child.startsWith(`${name}/`)) finalEntries.delete(child);
|
||||
finalEntries.set(name, { value, root: input.sourceDir });
|
||||
for (let parent = path.posix.dirname(name); parent !== "."; parent = path.posix.dirname(parent)) {
|
||||
if (finalEntries.get(parent)?.value.kind !== "dir") finalEntries.set(parent, { value: { kind: "dir" }, root: input.sourceDir });
|
||||
}
|
||||
}
|
||||
// An unchanged file created by another run can keep a removed folder
|
||||
// alive. Count those surviving parent directories in quota preflight.
|
||||
for (const name of finalEntries.keys()) {
|
||||
for (let parent = path.posix.dirname(name); parent !== "."; parent = path.posix.dirname(parent)) {
|
||||
if (!finalEntries.has(parent)) finalEntries.set(parent, { value: { kind: "dir" }, root });
|
||||
}
|
||||
}
|
||||
let total = 0;
|
||||
let fullFile: string | undefined;
|
||||
for (const [name, item] of finalEntries) if (item.value.kind === "file") {
|
||||
const size = (await fs.stat(path.join(item.root, name))).size;
|
||||
assertFileSize(size, name);
|
||||
if (size >= MAX_AGENT_FILE_BYTES) fullFile ??= name;
|
||||
total += size;
|
||||
}
|
||||
assertDirectorySize(total, finalEntries.size);
|
||||
await mergeDirectoryWithBaseline({ ...input, baseline: applyBaseline, targetDir: root });
|
||||
await audit(tx, agent, bound, { sourceRunId: actor.runId, contract: AGENT_FILES_CONTRACT });
|
||||
return { storageWarning: storageWarning(total, finalEntries.size, fullFile) };
|
||||
}),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import {
|
||||
agentApiKeys,
|
||||
authUsers,
|
||||
companyMemberships,
|
||||
heartbeatRuns,
|
||||
runIdentityContexts,
|
||||
type Db,
|
||||
} from "@paperclipai/db";
|
||||
import { forbidden } from "../errors.js";
|
||||
import {
|
||||
authorizationService,
|
||||
authorizationDeniedDetails,
|
||||
type AuthorizationActor,
|
||||
} from "./authorization.js";
|
||||
import {
|
||||
agentInstructionsChangeTargetKey,
|
||||
changeConsentGateService,
|
||||
} from "./change-consent-gate.js";
|
||||
|
||||
type Connection = Db | Parameters<Parameters<Db["transaction"]>[0]>[0];
|
||||
|
||||
/** Accept ONLY a server-authenticated actor, never request/tool JSON. Rebind run/key provenance on every call. */
|
||||
export async function resolveInstructionActor(
|
||||
db: Connection,
|
||||
actor: AuthorizationActor,
|
||||
): Promise<AuthorizationActor> {
|
||||
if (actor.type === "board") {
|
||||
if (!actor.userId)
|
||||
throw forbidden("Instruction edits require an authenticated user");
|
||||
// Discard caller membership caches so revoked access is observed.
|
||||
return {
|
||||
type: "board",
|
||||
userId: actor.userId,
|
||||
source: actor.source,
|
||||
ignoreInstanceAdmin: actor.ignoreInstanceAdmin,
|
||||
};
|
||||
}
|
||||
if (actor.type !== "agent" || !actor.agentId || !actor.companyId)
|
||||
throw forbidden("Instruction identity is missing");
|
||||
let responsibleUserId: string | null = null;
|
||||
let keyScope = actor.keyScope;
|
||||
if (actor.keyId) {
|
||||
const [key] = await db
|
||||
.select()
|
||||
.from(agentApiKeys)
|
||||
.where(
|
||||
and(
|
||||
eq(agentApiKeys.id, actor.keyId),
|
||||
eq(agentApiKeys.companyId, actor.companyId),
|
||||
eq(agentApiKeys.agentId, actor.agentId),
|
||||
isNull(agentApiKeys.revokedAt),
|
||||
),
|
||||
);
|
||||
if (!key)
|
||||
throw forbidden("Instruction API key is missing or revoked", {
|
||||
code: "INSTRUCTION_IDENTITY_INVALID",
|
||||
});
|
||||
responsibleUserId = key.responsibleUserId;
|
||||
keyScope = key.scopeConfig ?? undefined;
|
||||
}
|
||||
if (actor.runId) {
|
||||
const [run] = await db
|
||||
.select()
|
||||
.from(heartbeatRuns)
|
||||
.where(
|
||||
and(
|
||||
eq(heartbeatRuns.id, actor.runId),
|
||||
eq(heartbeatRuns.companyId, actor.companyId),
|
||||
eq(heartbeatRuns.agentId, actor.agentId),
|
||||
),
|
||||
);
|
||||
if (!run)
|
||||
throw forbidden("Instruction run identity does not match", {
|
||||
code: "INSTRUCTION_IDENTITY_INVALID",
|
||||
});
|
||||
if (actor.keyId && responsibleUserId !== run.responsibleUserId)
|
||||
throw forbidden("Key and run responsible identity do not match", {
|
||||
code: "INSTRUCTION_IDENTITY_INVALID",
|
||||
});
|
||||
responsibleUserId = run.responsibleUserId;
|
||||
if (run.activeIdentityContextId) {
|
||||
const [context] = await db
|
||||
.select()
|
||||
.from(runIdentityContexts)
|
||||
.where(
|
||||
and(
|
||||
eq(runIdentityContexts.id, run.activeIdentityContextId),
|
||||
eq(runIdentityContexts.runId, run.id),
|
||||
eq(runIdentityContexts.companyId, run.companyId),
|
||||
eq(runIdentityContexts.status, "accepted"),
|
||||
),
|
||||
);
|
||||
if (!context)
|
||||
throw forbidden("Instruction run identity is unavailable", {
|
||||
code: "INSTRUCTION_IDENTITY_INVALID",
|
||||
});
|
||||
responsibleUserId = context.responsibleUserId;
|
||||
}
|
||||
}
|
||||
if (
|
||||
!responsibleUserId ||
|
||||
(actor.onBehalfOfUserId && actor.onBehalfOfUserId !== responsibleUserId)
|
||||
) {
|
||||
throw forbidden("A current server-bound responsible user is required", {
|
||||
code: "INSTRUCTION_IDENTITY_INVALID",
|
||||
});
|
||||
}
|
||||
const [membership] = await db
|
||||
.select({ userId: authUsers.id })
|
||||
.from(companyMemberships)
|
||||
.innerJoin(authUsers, eq(authUsers.id, companyMemberships.principalId))
|
||||
.where(
|
||||
and(
|
||||
eq(companyMemberships.companyId, actor.companyId),
|
||||
eq(companyMemberships.principalType, "user"),
|
||||
eq(companyMemberships.principalId, responsibleUserId),
|
||||
eq(companyMemberships.status, "active"),
|
||||
),
|
||||
);
|
||||
if (!membership)
|
||||
throw forbidden("The responsible user is unavailable in this company", {
|
||||
code: "RESPONSIBLE_USER_UNAVAILABLE",
|
||||
});
|
||||
return {
|
||||
type: "agent",
|
||||
agentId: actor.agentId,
|
||||
companyId: actor.companyId,
|
||||
runId: actor.runId,
|
||||
keyId: actor.keyId,
|
||||
keyScope,
|
||||
source: actor.source,
|
||||
onBehalfOfUserId: responsibleUserId,
|
||||
};
|
||||
}
|
||||
|
||||
/** Instruction content is narrower than general same-company agent visibility. */
|
||||
export async function authorizeInstructionRead(
|
||||
db: Connection,
|
||||
actor: AuthorizationActor,
|
||||
target: { companyId: string; id: string },
|
||||
) {
|
||||
const bound = await resolveInstructionActor(db, actor);
|
||||
const access = authorizationService(db);
|
||||
const resource = { type: "agent" as const, companyId: target.companyId, agentId: target.id };
|
||||
const peer = bound.type === "agent" && bound.agentId !== target.id;
|
||||
const decision = await access.decide({
|
||||
actor: bound,
|
||||
action: peer ? "agent_config:read" : "agent:read",
|
||||
resource,
|
||||
scope: { targetAgentId: target.id },
|
||||
});
|
||||
if (!decision.allowed) throw forbidden(decision.explanation, authorizationDeniedDetails(decision));
|
||||
return bound;
|
||||
}
|
||||
|
||||
/** Current target edit access + agent containment + protected-change consent, shared by all writers. */
|
||||
export async function authorizeInstructionCommit(
|
||||
db: Connection,
|
||||
actor: AuthorizationActor,
|
||||
target: { companyId: string; id: string },
|
||||
) {
|
||||
const bound = await resolveInstructionActor(db, actor);
|
||||
const access = authorizationService(db);
|
||||
const input = {
|
||||
actor: bound,
|
||||
action: "agent_instructions:update" as const,
|
||||
resource: {
|
||||
type: "agent" as const,
|
||||
companyId: target.companyId,
|
||||
agentId: target.id,
|
||||
},
|
||||
scope: { targetAgentId: target.id, requiresChangeGrant: true },
|
||||
};
|
||||
let decision = await access.decide(input);
|
||||
if (
|
||||
!decision.allowed &&
|
||||
decision.reason === "deny_missing_consent" &&
|
||||
bound.type === "agent"
|
||||
) {
|
||||
await changeConsentGateService(db).assertConsented({
|
||||
companyId: target.companyId,
|
||||
actorAgentId: bound.agentId,
|
||||
actorRunId: bound.runId,
|
||||
targetKeys: [agentInstructionsChangeTargetKey(target.id)],
|
||||
});
|
||||
decision = await access.decide({
|
||||
...input,
|
||||
scope: { ...input.scope, consentedChange: true },
|
||||
});
|
||||
}
|
||||
if (!decision.allowed)
|
||||
throw forbidden(decision.explanation, authorizationDeniedDetails(decision));
|
||||
return bound;
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
import fs from "node:fs/promises";
|
||||
import { constants } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { unprocessable } from "../errors.js";
|
||||
|
||||
export const MAX_INSTRUCTION_BYTES = 1024 * 1024;
|
||||
export function instructionPath(value: string): string {
|
||||
if (
|
||||
!value ||
|
||||
value.length > 512 ||
|
||||
value.includes("\0") ||
|
||||
value.includes("\\") ||
|
||||
path.posix.isAbsolute(value) ||
|
||||
/^[a-z]:/i.test(value) ||
|
||||
value.split("/").some((part) => !part || part === "." || part === "..")
|
||||
) {
|
||||
throw unprocessable("Use a relative instructions path without traversal", {
|
||||
code: "INSTRUCTION_PATH_INVALID",
|
||||
});
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function instructionBytes(content: string | Uint8Array): Buffer {
|
||||
const byteLength =
|
||||
typeof content === "string"
|
||||
? Buffer.byteLength(content, "utf8")
|
||||
: content.byteLength;
|
||||
if (byteLength > MAX_INSTRUCTION_BYTES) {
|
||||
throw unprocessable("Instructions exceed the 1 MiB limit", {
|
||||
code: "INSTRUCTION_CONTENT_INVALID",
|
||||
});
|
||||
}
|
||||
const bytes =
|
||||
typeof content === "string"
|
||||
? Buffer.from(content, "utf8")
|
||||
: Buffer.from(content);
|
||||
try {
|
||||
// Preserve BOM; reject invalid UTF-8 and unpaired JS surrogates instead of replacing bytes.
|
||||
const decoded = new TextDecoder("utf-8", {
|
||||
fatal: true,
|
||||
ignoreBOM: true,
|
||||
}).decode(bytes);
|
||||
if (typeof content === "string" && decoded !== content)
|
||||
throw new Error("invalid Unicode");
|
||||
} catch {
|
||||
throw unprocessable("Instructions must be valid UTF-8", {
|
||||
code: "INSTRUCTION_CONTENT_INVALID",
|
||||
});
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
export async function assertInstructionPathSafe(
|
||||
root: string,
|
||||
entryFile: string,
|
||||
) {
|
||||
instructionPath(entryFile);
|
||||
let absolute = path.resolve(root, entryFile);
|
||||
// macOS exposes its system temporary directories through root-owned aliases.
|
||||
// Resolve only those fixed OS aliases, never a link in operator/agent storage.
|
||||
if (process.platform === "darwin") {
|
||||
const alias = absolute.split(path.sep)[1];
|
||||
if (alias === "var" || alias === "tmp" || alias === "etc") {
|
||||
const systemPath = `/${alias}`;
|
||||
const stat = await fs.lstat(systemPath);
|
||||
if (stat.isSymbolicLink() && stat.uid === 0 && await fs.realpath(systemPath) === `/private/${alias}`) {
|
||||
absolute = `/private${absolute}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
let current = path.parse(absolute).root;
|
||||
const parts = absolute.slice(current.length).split(path.sep);
|
||||
for (const [i, part] of parts.entries()) {
|
||||
current = path.join(current, part);
|
||||
const stat = await fs
|
||||
.lstat(current)
|
||||
.catch((error: NodeJS.ErrnoException) => {
|
||||
if (error.code === "ENOENT") return null;
|
||||
throw error;
|
||||
});
|
||||
if (!stat) continue;
|
||||
if (
|
||||
stat.isSymbolicLink() ||
|
||||
(i < parts.length - 1 ? !stat.isDirectory() : !stat.isFile())
|
||||
) {
|
||||
throw unprocessable(
|
||||
"Instructions require regular files and directories without symlinks",
|
||||
{ code: "INSTRUCTION_PATH_INVALID" },
|
||||
);
|
||||
}
|
||||
}
|
||||
return absolute;
|
||||
}
|
||||
|
||||
export async function readInstructionBytes(
|
||||
root: string,
|
||||
entryFile: string,
|
||||
): Promise<Buffer | null> {
|
||||
const absolute = await assertInstructionPathSafe(root, entryFile);
|
||||
const file = await fs
|
||||
.open(absolute, constants.O_RDONLY | constants.O_NOFOLLOW)
|
||||
.catch((error: NodeJS.ErrnoException) => {
|
||||
if (error.code === "ENOENT") return null;
|
||||
throw error;
|
||||
});
|
||||
if (!file) return null;
|
||||
try {
|
||||
const stat = await file.stat();
|
||||
if (!stat.isFile() || stat.size > MAX_INSTRUCTION_BYTES) {
|
||||
throw unprocessable(
|
||||
"Instructions must be a regular UTF-8 file of at most 1 MiB",
|
||||
{ code: "INSTRUCTION_CONTENT_INVALID" },
|
||||
);
|
||||
}
|
||||
const buffer = Buffer.alloc(MAX_INSTRUCTION_BYTES + 1);
|
||||
let length = 0;
|
||||
while (length < buffer.length) {
|
||||
const result = await file.read(
|
||||
buffer,
|
||||
length,
|
||||
buffer.length - length,
|
||||
length,
|
||||
);
|
||||
if (result.bytesRead === 0) break;
|
||||
length += result.bytesRead;
|
||||
}
|
||||
return instructionBytes(buffer.subarray(0, length));
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
}
|
||||
|
||||
/** Call while holding the canonical agent lock. Never writes through a symlink. */
|
||||
export async function materializeInstructionBytes(
|
||||
root: string,
|
||||
entryFile: string,
|
||||
bytes: Buffer,
|
||||
) {
|
||||
const absolute = await assertInstructionPathSafe(root, entryFile);
|
||||
await fs.mkdir(path.dirname(absolute), { recursive: true });
|
||||
await assertInstructionPathSafe(root, entryFile);
|
||||
const temporary = path.join(
|
||||
path.dirname(absolute),
|
||||
`.instruction-${randomUUID()}.tmp`,
|
||||
);
|
||||
try {
|
||||
const file = await fs.open(temporary, "wx", 0o600);
|
||||
try {
|
||||
await file.writeFile(bytes);
|
||||
await file.sync();
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
await assertInstructionPathSafe(root, entryFile);
|
||||
await fs.rename(temporary, absolute);
|
||||
} finally {
|
||||
await fs.rm(temporary, { force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// Keep the reserved runtime directory out of Git using a self-ignoring file
|
||||
// inside that directory. Never write to a gitdir or exclude path derived from
|
||||
// repository-controlled metadata; legitimate worktrees can keep external gitdirs.
|
||||
export const instructionGitExcludeProgram = String.raw`
|
||||
const fs=require('node:fs'),path=require('node:path'),crypto=require('node:crypto');
|
||||
const cwd=fs.realpathSync(process.argv[1]);
|
||||
const root=path.join(cwd,'.paperclip-runtime'),filename=path.join(root,'.gitignore');
|
||||
const stat=(name)=>{try{return fs.lstatSync(name)}catch(e){if(e.code==='ENOENT')return null;throw e}};
|
||||
const directory=stat(root);
|
||||
if(directory && (directory.isSymbolicLink() || !directory.isDirectory()))throw Error('Unsafe runtime exclusion directory');
|
||||
if(!directory)fs.mkdirSync(root,{mode:0o700});
|
||||
const existing=stat(filename);
|
||||
if(existing && (existing.isSymbolicLink() || !existing.isFile()))throw Error('Unsafe runtime exclusion file');
|
||||
const temporary=path.join(root,'.ignore-'+crypto.randomUUID()+'.tmp');
|
||||
try{fs.writeFileSync(temporary,'*\n',{flag:'wx',mode:0o600});fs.renameSync(temporary,filename)}finally{fs.rmSync(temporary,{force:true})}
|
||||
`;
|
||||
@@ -0,0 +1,429 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { adoptAgentFiles, agentFileToken } from "./agent-file-store.js";
|
||||
import { and, desc, eq, getTableColumns, lt, or, sql } from "drizzle-orm";
|
||||
import {
|
||||
activityLog,
|
||||
agents,
|
||||
plugins,
|
||||
pluginManagedResources,
|
||||
agentInstructionHeads as heads,
|
||||
agentInstructionRevisions as revisions,
|
||||
type Db,
|
||||
} from "@paperclipai/db";
|
||||
import type {
|
||||
AgentInstructionCommitReceipt,
|
||||
AgentInstructionDiff,
|
||||
AgentInstructionHistory,
|
||||
AgentInstructionSnapshot,
|
||||
AgentInstructionSource,
|
||||
} from "@paperclipai/shared";
|
||||
import { conflict, forbidden, notFound, unprocessable } from "../errors.js";
|
||||
import {
|
||||
agentInstructionsBundleMode,
|
||||
deriveBundleState,
|
||||
resolveManagedInstructionsRoot,
|
||||
} from "./agent-instructions.js";
|
||||
import {
|
||||
instructionBytes,
|
||||
instructionPath,
|
||||
assertInstructionPathSafe,
|
||||
readInstructionBytes,
|
||||
materializeInstructionBytes,
|
||||
} from "./agent-instruction-files.js";
|
||||
import {
|
||||
authorizeInstructionCommit,
|
||||
authorizeInstructionRead,
|
||||
} from "./agent-instruction-authorization.js";
|
||||
import type { AuthorizationActor } from "./authorization.js";
|
||||
|
||||
type Tx = Parameters<Parameters<Db["transaction"]>[0]>[0];
|
||||
type Revision = typeof revisions.$inferSelect;
|
||||
type PluginResetActor = { type: "plugin"; pluginId: string; pluginKey: string; agentKey: string };
|
||||
type RevisionActor = AuthorizationActor | PluginResetActor;
|
||||
const { contentBase64: _contentColumn, ...revisionMetadataColumns } =
|
||||
getTableColumns(revisions);
|
||||
export type InstructionTarget = { companyId: string; agentId: string };
|
||||
export type InstructionCommitInput = InstructionTarget & {
|
||||
entryFile: string;
|
||||
content: string | Uint8Array;
|
||||
baseRevisionId: string | null;
|
||||
source: Exclude<AgentInstructionSource, "seed" | "restore">;
|
||||
};
|
||||
function metadata(row: Omit<Revision, "contentBase64">) {
|
||||
const { createdAt, source, ...fields } = row;
|
||||
return {
|
||||
...fields,
|
||||
source: source as AgentInstructionSource,
|
||||
createdAt: createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
function snapshot(row: Revision): AgentInstructionSnapshot {
|
||||
const { contentBase64, ...fields } = row;
|
||||
return {
|
||||
revision: metadata(fields),
|
||||
content: Buffer.from(contentBase64, "base64").toString("utf8"),
|
||||
};
|
||||
}
|
||||
function owner(target: InstructionTarget, entryFile: string) {
|
||||
return and(
|
||||
eq(revisions.companyId, target.companyId),
|
||||
eq(revisions.agentId, target.agentId),
|
||||
eq(revisions.entryFile, entryFile),
|
||||
);
|
||||
}
|
||||
function headOwner(target: InstructionTarget, entryFile: string) {
|
||||
return and(
|
||||
eq(heads.companyId, target.companyId),
|
||||
eq(heads.agentId, target.agentId),
|
||||
eq(heads.entryFile, entryFile),
|
||||
);
|
||||
}
|
||||
|
||||
export function agentInstructionRevisionService(db: Db) {
|
||||
async function lockTarget(
|
||||
tx: Tx,
|
||||
target: InstructionTarget,
|
||||
actor?: AuthorizationActor,
|
||||
) {
|
||||
if (actor?.type === "agent" && actor.companyId !== target.companyId)
|
||||
throw notFound("Agent not found");
|
||||
const [agent] = await tx
|
||||
.select()
|
||||
.from(agents)
|
||||
.where(
|
||||
and(
|
||||
eq(agents.id, target.agentId),
|
||||
eq(agents.companyId, target.companyId),
|
||||
),
|
||||
)
|
||||
.for("update");
|
||||
if (!agent) throw notFound("Agent not found");
|
||||
if (agentInstructionsBundleMode(agent) === "external") {
|
||||
throw unprocessable(
|
||||
"External host instructions do not support revision write-back. Ask an instance administrator to migrate this bundle to managed storage.",
|
||||
{ code: "INSTRUCTION_MANAGED_BUNDLE_REQUIRED" },
|
||||
);
|
||||
}
|
||||
const state = deriveBundleState(agent);
|
||||
const entryFile = instructionPath(state.entryFile);
|
||||
return { agent, entryFile, root: resolveManagedInstructionsRoot(agent) };
|
||||
}
|
||||
async function authorizeRead(
|
||||
tx: Tx,
|
||||
actor: AuthorizationActor,
|
||||
target: InstructionTarget,
|
||||
) {
|
||||
return authorizeInstructionRead(tx, actor, { companyId: target.companyId, id: target.agentId });
|
||||
}
|
||||
// Trusted host-only reset authority. No HTTP or agent tool accepts this actor.
|
||||
async function authorizePluginReset(tx: Tx, actor: PluginResetActor, target: InstructionTarget, agent: typeof agents.$inferSelect) {
|
||||
const [plugin] = await tx.select().from(plugins).where(and(eq(plugins.id, actor.pluginId), eq(plugins.pluginKey, actor.pluginKey)));
|
||||
const marker = agent.metadata?.paperclipManagedResource as Record<string, unknown> | undefined;
|
||||
const [binding] = await tx.select().from(pluginManagedResources).where(and(
|
||||
eq(pluginManagedResources.pluginId, actor.pluginId), eq(pluginManagedResources.pluginKey, actor.pluginKey),
|
||||
eq(pluginManagedResources.companyId, target.companyId), eq(pluginManagedResources.resourceKind, "agent"),
|
||||
eq(pluginManagedResources.resourceKey, actor.agentKey), eq(pluginManagedResources.resourceId, target.agentId),
|
||||
));
|
||||
if (!plugin || plugin.status !== "ready" || !plugin.manifestJson.capabilities.includes("agents.managed")
|
||||
|| !plugin.manifestJson.agents?.some((declaration) => declaration.agentKey === actor.agentKey)
|
||||
|| !binding || marker?.pluginId !== actor.pluginId || marker.pluginKey !== actor.pluginKey
|
||||
|| marker.resourceKind !== "agent" || marker.resourceKey !== actor.agentKey) {
|
||||
throw forbidden("Plugin reset is limited to its declared, bound managed agent");
|
||||
}
|
||||
const declared = plugin.manifestJson.agents!.find((declaration) => declaration.agentKey === actor.agentKey)!;
|
||||
return { ...actor, declaredEntryFile: instructionPath(declared.instructions?.entryFile ?? "AGENTS.md") };
|
||||
}
|
||||
async function readForPluginReset(target: InstructionTarget, actor: PluginResetActor) {
|
||||
return db.transaction(async (tx) => {
|
||||
const state = await lockTarget(tx, target);
|
||||
const bound = await authorizePluginReset(tx, actor, target, state.agent);
|
||||
// Preserve the formerly configured entry before an explicit stock reset.
|
||||
await currentFile(tx, target, state, bound);
|
||||
const row = await currentFile(tx, target, { ...state, entryFile: bound.declaredEntryFile }, bound);
|
||||
return { snapshot: row ? snapshot(row) : null, configuredEntryFile: state.entryFile };
|
||||
});
|
||||
}
|
||||
async function head(tx: Tx, target: InstructionTarget, entryFile: string) {
|
||||
const [result] = await tx
|
||||
.select({ revision: revisions })
|
||||
.from(heads)
|
||||
.innerJoin(revisions, eq(revisions.id, heads.revisionId))
|
||||
.where(headOwner(target, entryFile));
|
||||
return result?.revision ?? null;
|
||||
}
|
||||
// The deployed revision tables are read-only upgrade input. Current bytes
|
||||
// live in the agent directory; old UUID clients receive a content ETag.
|
||||
async function currentFile(tx: Tx, target: InstructionTarget, state: Awaited<ReturnType<typeof lockTarget>>, actor?: RevisionActor) {
|
||||
await adoptAgentFiles(tx, state.agent);
|
||||
const bytes = await readInstructionBytes(state.root, state.entryFile);
|
||||
if (bytes === null) return null;
|
||||
return currentRow(target, state.entryFile, bytes, actor);
|
||||
}
|
||||
function currentRow(target: InstructionTarget, entryFile: string, bytes: Buffer, actor?: RevisionActor): Revision {
|
||||
return { ...target, id: agentFileToken(bytes), entryFile, contentBase64: bytes.toString("base64"),
|
||||
contentHash: createHash("sha256").update(bytes).digest("hex"), byteLength: bytes.length,
|
||||
parentRevisionId: null, baseRevisionId: null, restoredFromRevisionId: null, source: "api",
|
||||
actorAgentId: actor?.type === "agent" ? actor.agentId ?? null : null,
|
||||
actorUserId: actor?.type === "board" ? actor.userId ?? null : null,
|
||||
responsibleUserId: actor?.type === "board" ? actor.userId ?? null : actor?.type === "agent" ? actor.onBehalfOfUserId ?? null : null,
|
||||
sourceRunId: actor && actor.type !== "plugin" ? actor.runId ?? null : null, createdAt: new Date(0) };
|
||||
}
|
||||
async function getRevision(
|
||||
tx: Tx,
|
||||
target: InstructionTarget,
|
||||
entryFile: string,
|
||||
id: string,
|
||||
) {
|
||||
const [row] = await tx
|
||||
.select()
|
||||
.from(revisions)
|
||||
.where(and(owner(target, entryFile), eq(revisions.id, id)));
|
||||
if (!row) throw notFound("Instruction revision not found");
|
||||
return row;
|
||||
}
|
||||
async function readCurrent(
|
||||
target: InstructionTarget,
|
||||
actor: AuthorizationActor,
|
||||
): Promise<AgentInstructionSnapshot | null> {
|
||||
return db.transaction(async (tx) => {
|
||||
const state = await lockTarget(tx, target, actor);
|
||||
const bound = await authorizeRead(tx, actor, target);
|
||||
const row = await currentFile(tx, target, state, bound);
|
||||
return row ? snapshot(row) : null;
|
||||
});
|
||||
}
|
||||
/** Trusted orchestration read for the already authorized run target. This
|
||||
* never seeds or writes, and is not exposed through HTTP or model tools. */
|
||||
async function readCommittedForRuntime(target: InstructionTarget): Promise<AgentInstructionSnapshot | null> {
|
||||
return db.transaction(async (tx) => {
|
||||
const state = await lockTarget(tx, target);
|
||||
const current = await currentFile(tx, target, state);
|
||||
return current ? snapshot(current) : null;
|
||||
});
|
||||
}
|
||||
/** Rebuild disk from the current committed head under the same lock as commits. Safe after restart. */
|
||||
async function materializeCurrent(target: InstructionTarget): Promise<void> {
|
||||
await db.transaction(async (tx) => {
|
||||
const state = await lockTarget(tx, target);
|
||||
await adoptAgentFiles(tx, state.agent);
|
||||
});
|
||||
}
|
||||
async function commitInternal(
|
||||
input:
|
||||
| InstructionCommitInput
|
||||
| (Omit<InstructionCommitInput, "content" | "source"> & {
|
||||
restoreRevisionId: string;
|
||||
}),
|
||||
actor: RevisionActor,
|
||||
configuredEntryFile?: string,
|
||||
): Promise<AgentInstructionCommitReceipt> {
|
||||
instructionPath(input.entryFile);
|
||||
const bytes = "content" in input ? instructionBytes(input.content) : null;
|
||||
const result = await db.transaction(async (tx) => {
|
||||
const state = await lockTarget(tx, input, actor.type === "plugin" ? undefined : actor);
|
||||
const bound = actor.type === "plugin"
|
||||
? await authorizePluginReset(tx, actor, input, state.agent)
|
||||
: await authorizeInstructionCommit(tx, actor, state.agent);
|
||||
if (bound.type === "plugin") {
|
||||
if (input.entryFile !== bound.declaredEntryFile) throw forbidden("Plugin reset must restore its declared entry file");
|
||||
if (configuredEntryFile !== state.entryFile) throw conflict("Configured instruction entry changed; read the current entry and retry", { code: "INSTRUCTION_ENTRY_CHANGED", entryFile: state.entryFile });
|
||||
state.entryFile = input.entryFile;
|
||||
}
|
||||
if (state.entryFile !== input.entryFile)
|
||||
throw conflict(
|
||||
"Configured instruction entry changed; read the current entry and retry",
|
||||
{ code: "INSTRUCTION_ENTRY_CHANGED", entryFile: state.entryFile },
|
||||
);
|
||||
await assertInstructionPathSafe(state.root, state.entryFile);
|
||||
const current = await currentFile(tx, input, state, bound);
|
||||
const restored =
|
||||
"restoreRevisionId" in input
|
||||
? await getRevision(
|
||||
tx,
|
||||
input,
|
||||
input.entryFile,
|
||||
input.restoreRevisionId,
|
||||
)
|
||||
: null;
|
||||
const candidate = bytes ?? Buffer.from(restored!.contentBase64, "base64");
|
||||
const bindEntry = async () => {
|
||||
if (
|
||||
!state.agent.adapterConfig.instructionsRootPath ||
|
||||
!state.agent.adapterConfig.instructionsBundleMode ||
|
||||
(bound.type === "plugin" && configuredEntryFile !== input.entryFile)
|
||||
) {
|
||||
await tx
|
||||
.update(agents)
|
||||
.set({
|
||||
adapterConfig: {
|
||||
...state.agent.adapterConfig,
|
||||
instructionsBundleMode: "managed",
|
||||
instructionsRootPath: state.root,
|
||||
instructionsEntryFile: state.entryFile,
|
||||
instructionsFilePath: `${state.root}/${state.entryFile}`,
|
||||
},
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(agents.id, state.agent.id));
|
||||
}
|
||||
};
|
||||
// An exact replay can safely return the durable receipt even after its base
|
||||
// advanced. Restore also deduplicates identical content, preserving history.
|
||||
if (current && current.contentBase64 === candidate.toString("base64")) {
|
||||
await bindEntry();
|
||||
return { row: current, changed: false };
|
||||
}
|
||||
// A pre-upgrade run can still pin a historical UUID. Compare its bytes,
|
||||
// never let the old head project over a newer directory.
|
||||
const legacyBase = input.baseRevisionId && current?.id !== input.baseRevisionId
|
||||
? await tx.select().from(revisions).where(and(owner(input, input.entryFile), eq(revisions.id, input.baseRevisionId))).then(rows => rows[0])
|
||||
: null;
|
||||
if ((current?.id ?? null) !== input.baseRevisionId && !(legacyBase && legacyBase.contentHash === current?.contentHash)) {
|
||||
throw conflict(
|
||||
"Instructions changed since the base revision. Read the current entry before saving.",
|
||||
{
|
||||
code: "INSTRUCTION_REVISION_CONFLICT",
|
||||
baseRevisionId: input.baseRevisionId,
|
||||
currentRevisionId: current?.id ?? null,
|
||||
},
|
||||
);
|
||||
}
|
||||
await materializeInstructionBytes(state.root, state.entryFile, candidate);
|
||||
const row = currentRow(input, input.entryFile, candidate, bound);
|
||||
await tx.insert(activityLog).values({ companyId: input.companyId,
|
||||
actorType: bound.type === "plugin" ? "plugin" : bound.type === "board" ? "user" : "agent",
|
||||
actorId: (bound.type === "plugin" ? bound.pluginId : bound.type === "board" ? bound.userId : bound.agentId)!,
|
||||
agentId: bound.type === "agent" ? bound.agentId : null, runId: row.sourceRunId,
|
||||
responsibleUserId: row.responsibleUserId, action: "agent.files_updated", entityType: "agent", entityId: input.agentId,
|
||||
details: { path: input.entryFile, contentHash: row.contentHash, source: restored ? "legacy_restore" : (input as InstructionCommitInput).source } });
|
||||
await bindEntry();
|
||||
return { row, changed: true };
|
||||
});
|
||||
return { ...snapshot(result.row), changed: result.changed, materialization: "current" };
|
||||
}
|
||||
|
||||
async function history(
|
||||
target: InstructionTarget & {
|
||||
entryFile: string;
|
||||
cursor?: string;
|
||||
limit?: number;
|
||||
},
|
||||
actor: AuthorizationActor,
|
||||
): Promise<AgentInstructionHistory> {
|
||||
const limit = Math.max(1, Math.min(100, target.limit ?? 50));
|
||||
return db.transaction(async (tx) => {
|
||||
const state = await lockTarget(tx, target, actor);
|
||||
const bound = await authorizeRead(tx, actor, target);
|
||||
await currentFile(tx, target, state, bound);
|
||||
instructionPath(target.entryFile);
|
||||
const cursor = target.cursor
|
||||
? await getRevision(tx, target, target.entryFile, target.cursor)
|
||||
: null;
|
||||
// Keep PostgreSQL timestamp precision; JS Date truncates microseconds and
|
||||
// would skip revisions in the cursor's fractional millisecond.
|
||||
const cursorTime = cursor
|
||||
? sql`(select created_at from agent_instruction_revisions where id = ${cursor.id})`
|
||||
: null;
|
||||
const rows = await tx
|
||||
.select(revisionMetadataColumns)
|
||||
.from(revisions)
|
||||
.where(
|
||||
and(
|
||||
owner(target, target.entryFile),
|
||||
cursor && cursorTime
|
||||
? or(
|
||||
lt(revisions.createdAt, cursorTime),
|
||||
and(
|
||||
eq(revisions.createdAt, cursorTime),
|
||||
lt(revisions.id, cursor.id),
|
||||
),
|
||||
)
|
||||
: undefined,
|
||||
),
|
||||
)
|
||||
.orderBy(desc(revisions.createdAt), desc(revisions.id))
|
||||
.limit(limit + 1);
|
||||
return {
|
||||
revisions: rows.slice(0, limit).map(metadata),
|
||||
nextCursor: rows.length > limit ? rows[limit - 1]!.id : null,
|
||||
};
|
||||
});
|
||||
}
|
||||
async function readRevision(
|
||||
target: InstructionTarget & { entryFile: string; revisionId: string },
|
||||
actor: AuthorizationActor,
|
||||
) {
|
||||
return db.transaction(async (tx) => {
|
||||
await lockTarget(tx, target, actor);
|
||||
await authorizeRead(tx, actor, target);
|
||||
return snapshot(
|
||||
await getRevision(
|
||||
tx,
|
||||
target,
|
||||
instructionPath(target.entryFile),
|
||||
target.revisionId,
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
async function diff(
|
||||
target: InstructionTarget & {
|
||||
entryFile: string;
|
||||
fromRevisionId: string;
|
||||
toRevisionId: string;
|
||||
},
|
||||
actor: AuthorizationActor,
|
||||
): Promise<AgentInstructionDiff> {
|
||||
const from = await readRevision(
|
||||
{ ...target, revisionId: target.fromRevisionId },
|
||||
actor,
|
||||
);
|
||||
const to = await readRevision(
|
||||
{ ...target, revisionId: target.toRevisionId },
|
||||
actor,
|
||||
);
|
||||
// Linear, bounded exact replacement diff. Code points keep surrogate pairs intact.
|
||||
const a = Array.from(from.content),
|
||||
b = Array.from(to.content);
|
||||
let start = 0,
|
||||
end = 0;
|
||||
while (start < a.length && start < b.length && a[start] === b[start])
|
||||
start++;
|
||||
while (
|
||||
end < a.length - start &&
|
||||
end < b.length - start &&
|
||||
a[a.length - end - 1] === b[b.length - end - 1]
|
||||
)
|
||||
end++;
|
||||
return {
|
||||
from,
|
||||
to,
|
||||
prefix: a.slice(0, start).join(""),
|
||||
removed: a.slice(start, a.length - end).join(""),
|
||||
added: b.slice(start, b.length - end).join(""),
|
||||
suffix: end ? a.slice(-end).join("") : "",
|
||||
};
|
||||
}
|
||||
return {
|
||||
readCommittedForRuntime,
|
||||
readCurrent,
|
||||
readForPluginReset,
|
||||
commitPluginReset: (input: Omit<InstructionCommitInput, "source"> & { configuredEntryFile: string }, actor: PluginResetActor) =>
|
||||
commitInternal({ ...input, source: "api" }, actor, input.configuredEntryFile),
|
||||
readRevision,
|
||||
history,
|
||||
diff,
|
||||
materializeCurrent,
|
||||
commit: (input: InstructionCommitInput, actor: AuthorizationActor) =>
|
||||
commitInternal(input, actor),
|
||||
restore: (
|
||||
input: InstructionTarget & {
|
||||
entryFile: string;
|
||||
baseRevisionId: string;
|
||||
revisionId: string;
|
||||
},
|
||||
actor: AuthorizationActor,
|
||||
) =>
|
||||
commitInternal({ ...input, restoreRevisionId: input.revisionId }, actor),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
import { agentDirectoryWorkingCopyService, isAgentDirectoryCopy } from "./agent-directory-working-copies.js";
|
||||
import { createHash } from "node:crypto";
|
||||
import { execFile as execFileCallback } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { and, asc, desc, eq, inArray, lte, or, isNull, isNotNull, sql } from "drizzle-orm";
|
||||
import { agents, heartbeatRuns, agentInstructionWorkingCopies as copies, type Db } from "@paperclipai/db";
|
||||
import {
|
||||
prepareAdapterExecutionTargetRuntime,
|
||||
runAdapterExecutionTargetShellCommand,
|
||||
type AdapterExecutionTarget,
|
||||
} from "@paperclipai/adapter-utils/execution-target";
|
||||
import { conflict, notFound } from "../errors.js";
|
||||
import { agentInstructionsService, agentInstructionsBundleMode } from "./agent-instructions.js";
|
||||
import { agentInstructionRevisionService } from "./agent-instruction-revisions.js";
|
||||
import { resolveInstructionActor } from "./agent-instruction-authorization.js";
|
||||
import { assertInstructionPathSafe, instructionBytes, instructionPath, materializeInstructionBytes, MAX_INSTRUCTION_BYTES, readInstructionBytes, instructionGitExcludeProgram } from "./agent-instruction-files.js";
|
||||
import { hasNativeLocalProcessStop } from "./native-local-process-stop.js";
|
||||
import { remoteExecutionHasStopped } from "./remote-execution-termination.js";
|
||||
import type { AuthorizationActor } from "./authorization.js";
|
||||
import type { EnvironmentRuntimeService } from "./environment-runtime.js";
|
||||
|
||||
const execFile = promisify(execFileCallback);
|
||||
type Copy = typeof copies.$inferSelect;
|
||||
const hash = (bytes: Uint8Array) => createHash("sha256").update(bytes).digest("hex");
|
||||
const quote = (value: string) => `'${value.replaceAll("'", `'"'"'`)}'`;
|
||||
const completed = new Set(["saved", "unchanged", "resolved"]);
|
||||
const MAX_COLLECTION_ATTEMPTS = 3;
|
||||
// Only orchestration can register a live target; tools and HTTP callers cannot
|
||||
// supply one. These handles are optional: durable captured bytes survive restart.
|
||||
const liveTargets = new Map<string, AdapterExecutionTarget>();
|
||||
const targetKey = (companyId: string, runId: string) => `${companyId}:${runId}`;
|
||||
|
||||
export function instructionWorkingCopyGuidance(copy: Pick<Copy, "executionRoot" | "entryFile" | "receipt">) {
|
||||
if (isAgentDirectoryCopy(copy)) return `Your persistent agent directory is ${copy.executionRoot} (AGENT_HOME). Your instruction entry is ${copy.executionRoot}/${copy.entryFile}. Read and write your own files and subfolders there. This directory belongs to this agent across tasks and sessions; task files belong in the task working directory. Paperclip restores this directory before execution and saves changes after the provider stops. Regular files, including binary files, persist; symlinks and special files are unsupported. Check the agent-files save receipt before claiming persistence; Only files you change or delete are synchronized. If another run changes the same file, the last completed synchronization wins. Temporary copies are removed after synchronization; there is no per-run file history. Storage allows 256 MiB per file, 2 GiB total, and 100,000 entries; the instruction entry must remain UTF-8 and at most 1 MiB. Reaching a storage limit never prevents this or future tasks from running. Remove or shrink files to free space; changes that exceed the limits will not be saved.${typeof copy.receipt?.storageWarning === "string" ? `\n\n${copy.receipt.storageWarning}` : ""}`;
|
||||
return `Your editable agent instruction file is ${copy.executionRoot}/${copy.entryFile}. Edit this registered private copy normally. After this run stops, Paperclip saves changed content as a persistent revision if your responsible user still has permission and the baseline has not changed. Check the run's instruction-save receipt before claiming persistence. Use read_agent_instructions, update_agent_instructions, get_agent_instruction_history, and restore_agent_instructions for immediate saves and history. Read first and pin the returned revision. Preserve conflicts; never silently retry against a newer head. Repository instructions, skills, and the loaded prompt are separate and are not collected.`;
|
||||
}
|
||||
|
||||
/** Remote reads use the registered entry only; never scan for files called AGENTS.md. */
|
||||
export function instructionCollectionScript(root: string, entry: string) {
|
||||
instructionPath(entry);
|
||||
return `node -e ${quote([
|
||||
"const fs=require('node:fs'),path=require('node:path')",
|
||||
"const root=process.argv[1],entry=process.argv[2],max=Number(process.argv[3])",
|
||||
"const absolute=path.resolve(root,entry),parts=absolute.split('/').filter(Boolean)",
|
||||
"let current='/'",
|
||||
"for(let i=0;i<parts.length;i++){current=path.join(current,parts[i]);let s;try{s=fs.lstatSync(current)}catch(e){if(e.code==='ENOENT'){process.stdout.write(JSON.stringify({missing:true}));process.exit(0)}throw e}if(s.isSymbolicLink()||(i<parts.length-1?!s.isDirectory():!s.isFile()))throw Error('INSTRUCTION_PATH_INVALID')}",
|
||||
"const fd=fs.openSync(absolute,fs.constants.O_RDONLY|fs.constants.O_NOFOLLOW)",
|
||||
"try{const s=fs.fstatSync(fd);if(!s.isFile()||s.size>max)throw Error('INSTRUCTION_CONTENT_INVALID');const bytes=Buffer.alloc(max+1);let n=0;while(n<bytes.length){const count=fs.readSync(fd,bytes,n,bytes.length-n,n);if(!count)break;n+=count}if(n>max)throw Error('INSTRUCTION_CONTENT_INVALID');process.stdout.write(JSON.stringify({contentBase64:bytes.subarray(0,n).toString('base64')}))}finally{fs.closeSync(fd)}",
|
||||
].join(";"))} ${quote(root)} ${quote(entry)} ${MAX_INSTRUCTION_BYTES}`;
|
||||
}
|
||||
|
||||
|
||||
export function agentInstructionWorkingCopyService(db: Db, options: { environmentRuntime?: EnvironmentRuntimeService } = {}) {
|
||||
const revisions = agentInstructionRevisionService(db);
|
||||
const scope = (companyId: string, runId: string) => and(eq(copies.companyId, companyId), eq(copies.runId, runId));
|
||||
async function get(companyId: string, runId: string) {
|
||||
const [row] = await db.select().from(copies).where(scope(companyId, runId));
|
||||
return row ?? null;
|
||||
}
|
||||
async function patch(row: Copy, values: Partial<typeof copies.$inferInsert>) {
|
||||
const [updated] = await db.update(copies).set({ ...values, updatedAt: new Date() }).where(and(
|
||||
scope(row.companyId, row.runId), eq(copies.state, row.state), eq(copies.attempts, row.attempts), eq(copies.baseHash, row.baseHash),
|
||||
)).returning();
|
||||
// A late cleanup must not overwrite an explicit resolution or a newer
|
||||
// baseline. Canonical content has its own independent head CAS.
|
||||
return updated ?? (await get(row.companyId, row.runId))!;
|
||||
}
|
||||
function actorFor(row: Copy): AuthorizationActor {
|
||||
return { type: "agent", companyId: row.companyId, agentId: row.agentId, runId: row.runId, onBehalfOfUserId: row.responsibleUserId };
|
||||
}
|
||||
const directories = agentDirectoryWorkingCopyService(db, get, patch, options.environmentRuntime);
|
||||
async function prepare(input: { companyId: string; agentId: string; runId: string; target?: AdapterExecutionTarget | null; cwd: string; legacy?: boolean }) {
|
||||
const existing = await get(input.companyId, input.runId);
|
||||
if (isAgentDirectoryCopy(existing) || (!existing && !input.legacy)) return directories.prepare(input);
|
||||
let refreshStoppedCopy = false;
|
||||
const workspace = await fs.realpath(input.cwd);
|
||||
const expectedLocalRoot = path.join(workspace, ".paperclip-runtime", `instruction-edits-${input.runId}`, "instructions");
|
||||
if (existing && existing.localRoot !== expectedLocalRoot) throw conflict("The registered instruction copy belongs to a different run workspace");
|
||||
const location = input.target?.kind === "remote" ? `remote:${input.target.environmentId ?? ""}` : "local";
|
||||
if (existing && (existing.agentId !== input.agentId || existing.location !== location)) {
|
||||
throw conflict("The registered instruction copy belongs to a different run environment");
|
||||
}
|
||||
// Preserve in-flight bytes and their CAS baseline. A completed, stopped copy
|
||||
// can start a fresh lifecycle only after its recorded bytes are accounted for.
|
||||
if (existing && existing.state !== "preparing") {
|
||||
if (["conflict", "pending_commit", "unavailable"].includes(existing.state)) {
|
||||
throw conflict("Resolve this run's preserved instruction candidate before editing its working copy again");
|
||||
}
|
||||
if (input.target) liveTargets.set(targetKey(input.companyId, input.runId), input.target);
|
||||
if (completed.has(existing.state) && existing.processStoppedAt) {
|
||||
const privateBytes = await readCandidate(existing, input.target);
|
||||
// Refresh only bytes whose previous lifecycle is complete. A changed
|
||||
// private file must keep its old CAS fence; never silently rebase edits.
|
||||
refreshStoppedCopy = privateBytes === null || hash(privateBytes) === (existing.candidateHash ?? existing.baseHash);
|
||||
if (!refreshStoppedCopy && privateBytes !== null) {
|
||||
// A failed staging attempt can already have replaced the file before
|
||||
// the completed row advances. Exact current canonical bytes are also
|
||||
// accounted for; any other private edit still keeps its old fence.
|
||||
const current = await revisions.readCommittedForRuntime({ companyId: input.companyId, agentId: input.agentId });
|
||||
refreshStoppedCopy = current?.revision.entryFile === existing.entryFile
|
||||
&& current.revision.contentHash === hash(privateBytes);
|
||||
}
|
||||
}
|
||||
if (!refreshStoppedCopy) {
|
||||
if (completed.has(existing.state) || existing.state === "unchanged_turn") {
|
||||
// unchanged_turn is a live warm-owner observation, not stop proof.
|
||||
return patch(existing, { state: "prepared", candidateBase64: null, candidateHash: null,
|
||||
receipt: null, processStoppedAt: null, attempts: 0, nextAttemptAt: null });
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
}
|
||||
const [agent] = await db.select().from(agents).where(and(eq(agents.id, input.agentId), eq(agents.companyId, input.companyId)));
|
||||
if (!agent) throw notFound("Agent not found");
|
||||
if (agentInstructionsBundleMode(agent) !== "managed") return null;
|
||||
const bound = await resolveInstructionActor(db, { type: "agent", companyId: input.companyId, agentId: input.agentId, runId: input.runId });
|
||||
const baseline = existing?.baseRevisionId && !refreshStoppedCopy
|
||||
? await revisions.readRevision({ companyId: input.companyId, agentId: input.agentId, entryFile: existing.entryFile, revisionId: existing.baseRevisionId }, bound).catch(async (error) => {
|
||||
if ((error as { status?: number }).status !== 404) throw error;
|
||||
const current = await revisions.readCurrent({ companyId: input.companyId, agentId: input.agentId }, bound);
|
||||
if (current?.revision.id !== existing.baseRevisionId) throw conflict("The instruction baseline changed during preparation");
|
||||
return current;
|
||||
})
|
||||
: await revisions.readCurrent({ companyId: input.companyId, agentId: input.agentId }, bound);
|
||||
if (!baseline) return null;
|
||||
const exported = await agentInstructionsService().exportFiles(agent, { rejectSymlinks: true });
|
||||
const entryFile = instructionPath(baseline?.revision.entryFile ?? exported.entryFile);
|
||||
const content = instructionBytes(baseline?.content ?? exported.files[entryFile] ?? "");
|
||||
const localRoot = existing?.localRoot ?? expectedLocalRoot;
|
||||
const target = input.target?.kind === "remote" ? input.target : null;
|
||||
const executionRoot = target
|
||||
? path.posix.join(target.remoteCwd, ".paperclip-runtime", `instruction-edits-${input.runId}`, "instructions")
|
||||
: localRoot;
|
||||
if (!existing) {
|
||||
await db.insert(copies).values({
|
||||
runId: input.runId, companyId: input.companyId, agentId: input.agentId,
|
||||
responsibleUserId: bound.onBehalfOfUserId!, entryFile,
|
||||
baseRevisionId: baseline?.revision.id ?? null, baseHash: hash(content),
|
||||
localRoot, executionRoot, location, state: "preparing",
|
||||
});
|
||||
}
|
||||
const row = (await get(input.companyId, input.runId))!;
|
||||
await assertInstructionPathSafe(localRoot, entryFile);
|
||||
await execFile(process.execPath, ["-e", instructionGitExcludeProgram, workspace], { timeout: 15_000 });
|
||||
await fs.mkdir(localRoot, { recursive: true, mode: 0o700 });
|
||||
for (const [name, text] of Object.entries({ ...exported.files, [entryFile]: content.toString("utf8") })) {
|
||||
const relative = instructionPath(name);
|
||||
const filename = await assertInstructionPathSafe(localRoot, relative);
|
||||
await fs.mkdir(path.dirname(filename), { recursive: true, mode: 0o700 });
|
||||
// Atomic replacement also allows interrupted preparation to restage an
|
||||
// already-read-only sibling without writing through its existing inode.
|
||||
await materializeInstructionBytes(localRoot, relative, instructionBytes(text));
|
||||
await fs.chmod(filename, name === entryFile ? 0o600 : 0o400);
|
||||
}
|
||||
if (target) {
|
||||
const staged = await prepareAdapterExecutionTargetRuntime({
|
||||
target, runId: input.runId, adapterKey: `instruction-edits-${input.runId}`,
|
||||
workspaceLocalDir: input.cwd, syncWorkspace: false,
|
||||
assets: [{ key: "instructions", localDir: localRoot, followSymlinks: false }],
|
||||
});
|
||||
if (staged.assetDirs.instructions !== executionRoot) throw new Error("Instruction copy staging path changed");
|
||||
const excluded = await runAdapterExecutionTargetShellCommand(input.runId, target,
|
||||
`node -e ${quote(instructionGitExcludeProgram)} ${quote(target.remoteCwd)}`, { cwd: target.remoteCwd, env: {}, timeoutSec: 15 });
|
||||
if (excluded.exitCode !== 0 || excluded.timedOut) throw new Error("Could not exclude private instructions from Git staging");
|
||||
}
|
||||
// A refresh does not supersede the completed turn until every local and
|
||||
// remote staging step succeeds. Keep its receipt and stop evidence intact
|
||||
// if preparation throws or the controller stops midway through staging.
|
||||
const prepared = await patch(row, { state: "prepared", ...(refreshStoppedCopy ? {
|
||||
entryFile, baseRevisionId: baseline.revision.id, baseHash: hash(content),
|
||||
candidateBase64: null, candidateHash: null, receipt: null, processStoppedAt: null,
|
||||
attempts: 0, nextAttemptAt: null, errorCode: null, errorMessage: null,
|
||||
} : {}) });
|
||||
if (input.target) liveTargets.set(targetKey(input.companyId, input.runId), input.target);
|
||||
return prepared;
|
||||
}
|
||||
|
||||
async function readCandidate(row: Copy, target?: AdapterExecutionTarget | null) {
|
||||
if (row.location === "local") return readInstructionBytes(row.localRoot, row.entryFile);
|
||||
if (target?.kind !== "remote") throw new Error("The original execution environment is needed to retrieve this instruction copy");
|
||||
if (row.location !== `remote:${target.environmentId ?? ""}`) throw new Error("Instruction copy execution environment changed");
|
||||
const expected = path.posix.join(target.remoteCwd, ".paperclip-runtime", `instruction-edits-${row.runId}`, "instructions");
|
||||
if (expected !== row.executionRoot) throw new Error("Instruction copy execution environment changed");
|
||||
const result = await runAdapterExecutionTargetShellCommand(row.runId, target, instructionCollectionScript(row.executionRoot, row.entryFile), { cwd: target.remoteCwd, env: {}, timeoutSec: 30 });
|
||||
if (result.exitCode !== 0 || result.timedOut) throw new Error("Could not safely retrieve the stopped run's instruction file");
|
||||
const payload = JSON.parse(result.stdout) as { missing?: boolean; contentBase64?: string };
|
||||
if (payload.missing) return null;
|
||||
if (typeof payload.contentBase64 !== "string" || payload.contentBase64.length > Math.ceil(MAX_INSTRUCTION_BYTES / 3) * 4) throw new Error("Instruction response exceeds the collection bound");
|
||||
return instructionBytes(Buffer.from(payload.contentBase64, "base64"));
|
||||
}
|
||||
|
||||
/** A terminal-turn probe is not a save: changed/invalid copies require a
|
||||
* stopped provider before capture. Unchanged warm providers remain reusable. */
|
||||
async function hasChanges(input: { companyId: string; runId: string; target?: AdapterExecutionTarget | null }) {
|
||||
const row = await get(input.companyId, input.runId);
|
||||
if (!row || completed.has(row.state)) return false;
|
||||
if (isAgentDirectoryCopy(row)) return directories.hasChanges(row, input.target);
|
||||
try {
|
||||
const content = await readCandidate(row, input.target);
|
||||
if (content !== null && hash(content) === row.baseHash) {
|
||||
// This is an unchanged turn receipt, not stopped-process collection.
|
||||
// No candidate bytes or new revision are persisted from a live owner.
|
||||
await patch(row, { state: "unchanged_turn", nextAttemptAt: null });
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch { return true; }
|
||||
}
|
||||
|
||||
async function acknowledgeExplicitSave(input: { companyId: string; agentId: string; runId: string; entryFile: string; revisionId: string; contentHash: string }) {
|
||||
const row = await get(input.companyId, input.runId);
|
||||
if (!row || isAgentDirectoryCopy(row) || row.agentId !== input.agentId || row.entryFile !== input.entryFile || row.state !== "prepared") return;
|
||||
const target = liveTargets.get(targetKey(row.companyId, row.runId));
|
||||
const content = await readCandidate(row, target).catch(() => null);
|
||||
// Never advance a stale local edit past an unrelated explicit update.
|
||||
if (content === null || hash(content) !== input.contentHash) return;
|
||||
await db.update(copies).set({ baseRevisionId: input.revisionId, baseHash: input.contentHash, updatedAt: new Date() })
|
||||
.where(and(scope(row.companyId, row.runId), eq(copies.state, "prepared"), eq(copies.baseHash, row.baseHash)));
|
||||
}
|
||||
|
||||
async function commitCandidate(row: Copy) {
|
||||
if (isAgentDirectoryCopy(row)) return directories.collectStopped(row);
|
||||
if (row.candidateBase64 === null || completed.has(row.state) || row.state === "conflict") return row;
|
||||
try {
|
||||
const receipt = await revisions.commit({
|
||||
companyId: row.companyId, agentId: row.agentId, entryFile: row.entryFile,
|
||||
baseRevisionId: row.baseRevisionId, content: Buffer.from(row.candidateBase64, "base64"), source: "cleanup",
|
||||
}, actorFor(row));
|
||||
return patch(row, { state: "saved", receipt: { ...receipt }, errorCode: null, errorMessage: null, nextAttemptAt: null });
|
||||
} catch (error) {
|
||||
const detail = error as { status?: number; details?: { code?: string }; message?: string };
|
||||
const code = detail.details?.code ?? "INSTRUCTION_SAVE_FAILED";
|
||||
const retryable = !detail.status || detail.status >= 500;
|
||||
const message = detail.status === 403
|
||||
? "Current permissions do not allow this instruction save. The candidate was preserved."
|
||||
: detail.status === 409
|
||||
? "Instructions changed after this run started. Review the preserved candidate against the current revision."
|
||||
: "Instruction edits were preserved but not saved. Review the candidate before retrying.";
|
||||
return patch(row, { state: retryable ? "pending_commit" : "conflict", errorCode: code, errorMessage: message, nextAttemptAt: retryable && row.attempts < MAX_COLLECTION_ATTEMPTS ? new Date(Date.now() + 30_000) : null });
|
||||
}
|
||||
}
|
||||
|
||||
/** Caller must have joined the owned provider process before granting this proof. */
|
||||
async function collectStopped(input: { companyId: string; runId: string; target?: AdapterExecutionTarget | null }) {
|
||||
let row = await get(input.companyId, input.runId);
|
||||
if (!row) return row;
|
||||
if (isAgentDirectoryCopy(row)) return directories.collectStopped(row, input.target);
|
||||
if (completed.has(row.state) || row.state === "conflict") return row;
|
||||
row = await patch(row, { processStoppedAt: row.processStoppedAt ?? new Date(), attempts: row.attempts + 1 });
|
||||
if (completed.has(row.state) || row.state === "conflict") return row;
|
||||
if (row.candidateBase64 !== null) return commitCandidate(row);
|
||||
try {
|
||||
const candidate = await readCandidate(row, input.target);
|
||||
if (candidate === null) {
|
||||
return patch(row, { state: "unavailable", errorCode: "INSTRUCTION_FILE_MISSING", errorMessage: "The registered instruction file was removed. Canonical instructions were kept; no edits were saved.", nextAttemptAt: null });
|
||||
}
|
||||
if (hash(candidate) === row.baseHash) return patch(row, { state: "unchanged", nextAttemptAt: null });
|
||||
// Save the candidate durably before attempting authorization/CAS or releasing the environment.
|
||||
row = await patch(row, { state: "pending_commit", candidateBase64: candidate.toString("base64"), candidateHash: hash(candidate), nextAttemptAt: new Date() });
|
||||
return commitCandidate(row);
|
||||
} catch (error) {
|
||||
return patch(row, { state: row.attempts < MAX_COLLECTION_ATTEMPTS ? "pending_collection" : "unavailable", errorCode: "INSTRUCTION_COLLECTION_FAILED", errorMessage: "The registered instruction copy could not be read safely. Canonical instructions were kept; no save receipt exists.", nextAttemptAt: row.attempts < MAX_COLLECTION_ATTEMPTS ? new Date(Date.now() + 30_000) : null });
|
||||
}
|
||||
}
|
||||
|
||||
/** Restarts can retry persisted bytes without touching a live filesystem or launching a model. */
|
||||
async function recoverCaptured() {
|
||||
const pending = await db.select().from(copies).where(and(
|
||||
eq(copies.state, "pending_commit"),
|
||||
or(isNull(copies.nextAttemptAt), lte(copies.nextAttemptAt, new Date())),
|
||||
lte(copies.attempts, MAX_COLLECTION_ATTEMPTS - 1),
|
||||
)).limit(20);
|
||||
for (const row of pending) {
|
||||
const result = await commitCandidate(await patch(row, { attempts: row.attempts + 1 }));
|
||||
if (isAgentDirectoryCopy(result) && completed.has(result.state)) await directories.release(result);
|
||||
}
|
||||
// A crash between a durable save and release must not retain run snapshots
|
||||
// forever. Only discard copies with recorded stop proof and a terminal receipt.
|
||||
const cleanup = await db.select().from(copies).where(and(
|
||||
inArray(copies.state, [...completed, "unavailable"]), isNotNull(copies.processStoppedAt),
|
||||
sql`${copies.receipt}->>'schema' = 'paperclip.agent-files.v1'`,
|
||||
or(sql`${copies.receipt} ? 'baseline'`, sql`${copies.receipt}->>'cleanupPending' = 'true'`),
|
||||
or(isNull(copies.nextAttemptAt), lte(copies.nextAttemptAt, new Date())),
|
||||
)).orderBy(asc(copies.updatedAt)).limit(20);
|
||||
for (const row of cleanup) await directories.release(row);
|
||||
return pending.length;
|
||||
}
|
||||
|
||||
/** Recover only the registered copy after independently recorded process-stop
|
||||
* proof. Terminal run status is never a substitute for that proof. Remote
|
||||
* receipts mean the environment is stopped; do not restart it just to read. */
|
||||
async function recoverStopped() {
|
||||
const pending = await db.select({ copy: copies, runtimeMode: heartbeatRuns.runtimeMode }).from(copies)
|
||||
.innerJoin(heartbeatRuns, and(eq(heartbeatRuns.companyId, copies.companyId), eq(heartbeatRuns.id, copies.runId)))
|
||||
.where(and(or(inArray(copies.state, ["prepared", "pending_collection"]),
|
||||
and(eq(copies.state, "preparing"), sql`${copies.receipt}->>'schema' = 'paperclip.agent-files.v1'`)),
|
||||
inArray(heartbeatRuns.status, ["succeeded", "failed", "cancelled", "timed_out", "interrupted"]),
|
||||
lte(copies.attempts, MAX_COLLECTION_ATTEMPTS - 1))).limit(20);
|
||||
for (const { copy: row, runtimeMode } of pending) {
|
||||
if (row.state === "preparing" && isAgentDirectoryCopy(row)) {
|
||||
// A provider cannot launch until preparation records "prepared". With
|
||||
// the owning run terminal, this is an interrupted staging copy only.
|
||||
await directories.release(await patch(row, { state: "unavailable", processStoppedAt: new Date(),
|
||||
errorCode: "AGENT_FILES_PREPARE_FAILED", errorMessage: "Agent-file preparation was interrupted. The temporary copy was discarded.", nextAttemptAt: null }));
|
||||
} else if (row.location === "local" && (row.processStoppedAt || runtimeMode === "native" &&
|
||||
await hasNativeLocalProcessStop(db, row.companyId, row.runId))) {
|
||||
const result = await collectStopped({ companyId: row.companyId, runId: row.runId });
|
||||
if (result && isAgentDirectoryCopy(result) && completed.has(result.state)) await directories.release(result);
|
||||
} else if (row.location !== "local" && await remoteExecutionHasStopped(db, row.companyId, row.runId)) {
|
||||
const unavailable = await reportUnavailable(row.companyId, row.runId);
|
||||
if (unavailable && isAgentDirectoryCopy(unavailable)) {
|
||||
await directories.release(await patch(unavailable, { processStoppedAt: unavailable.processStoppedAt ?? new Date() }));
|
||||
}
|
||||
} else if (row.state === "prepared") {
|
||||
await patch(row, { state: "pending_collection", errorCode: "INSTRUCTION_STOP_UNCONFIRMED",
|
||||
errorMessage: "The provider's stop has not been confirmed. Instruction collection is pending; no save is claimed.", nextAttemptAt: null });
|
||||
}
|
||||
}
|
||||
return pending.length;
|
||||
}
|
||||
|
||||
async function list(companyId: string, agentId: string, actor: AuthorizationActor) {
|
||||
await revisions.readCurrent({ companyId, agentId }, actor);
|
||||
const rows = await db.select().from(copies).where(and(eq(copies.companyId, companyId), eq(copies.agentId, agentId), inArray(copies.state, ["conflict", "pending_collection", "pending_commit", "unavailable"]))).orderBy(desc(copies.createdAt)).limit(50);
|
||||
return rows.map(row => ({ runId: row.runId, entryFile: row.entryFile, baseRevisionId: row.baseRevisionId,
|
||||
baseHash: row.baseHash, state: row.state as "conflict" | "pending_collection" | "pending_commit" | "unavailable",
|
||||
candidateHash: row.candidateHash, contract: isAgentDirectoryCopy(row) ? "agent_files" : "legacy",
|
||||
content: row.candidateBase64 === null ? null : Buffer.from(row.candidateBase64, "base64").toString("utf8"),
|
||||
errorCode: row.errorCode, errorMessage: row.errorMessage, createdAt: row.createdAt.toISOString(), updatedAt: row.updatedAt.toISOString() }));
|
||||
}
|
||||
|
||||
async function resolve(input: { companyId: string; agentId: string; runId: string; baseRevisionId: string | null; content: string }, actor: AuthorizationActor) {
|
||||
const row = await get(input.companyId, input.runId);
|
||||
if (!row || row.agentId !== input.agentId) throw notFound("Instruction candidate not found");
|
||||
if (completed.has(row.state)) throw conflict("This instruction candidate has already been resolved");
|
||||
if (isAgentDirectoryCopy(row)) throw conflict("Agent-file synchronization failures have no preserved candidate");
|
||||
const receipt = await revisions.commit({ companyId: input.companyId, agentId: input.agentId, entryFile: row.entryFile, content: input.content, baseRevisionId: input.baseRevisionId, source: "api" }, actor);
|
||||
await patch(row, { state: "resolved", receipt: { ...receipt }, nextAttemptAt: null });
|
||||
return receipt;
|
||||
}
|
||||
|
||||
/** A controller may report loss before disposal, never fabricate a save or
|
||||
* read a possibly live provider. Already captured candidates remain intact. */
|
||||
async function reportUnavailable(companyId: string, runId: string) {
|
||||
const row = await get(companyId, runId);
|
||||
if (!row || completed.has(row.state) || row.state === "unchanged_turn" || row.candidateBase64 !== null || (isAgentDirectoryCopy(row) && row.candidateHash !== null) || row.state === "conflict") return row;
|
||||
if (isAgentDirectoryCopy(row) && row.state === "unavailable") return row;
|
||||
return patch(row, { state: "unavailable", errorCode: "INSTRUCTION_COLLECTION_UNAVAILABLE",
|
||||
errorMessage: "The registered instruction copy could not be retrieved safely before environment release. No instruction save is claimed.", nextAttemptAt: null });
|
||||
}
|
||||
|
||||
async function release(companyId: string, runId: string) { liveTargets.delete(targetKey(companyId, runId)); const row = await get(companyId, runId); if (row && isAgentDirectoryCopy(row)) await directories.release(row); }
|
||||
return { prepare, get, hasChanges, acknowledgeExplicitSave, collectStopped, recoverCaptured, recoverStopped, list, resolve, reportUnavailable, release };
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
import { inspectAgentFile, fileHash, agentFilePath, MAX_AGENT_FILE_BYTES } from "./agent-file-store.js";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { agentInstructionHeads, agents, type Db } from "@paperclipai/db";
|
||||
import { instructionPath, assertInstructionPathSafe, instructionBytes, readInstructionBytes } from "./agent-instruction-files.js";
|
||||
import { notFound, unprocessable } from "../errors.js";
|
||||
import { resolveHomeAwarePath, resolvePaperclipInstanceRoot } from "../home-paths.js";
|
||||
|
||||
@@ -36,6 +40,8 @@ type AgentLike = {
|
||||
|
||||
type AgentInstructionsFileSummary = {
|
||||
path: string;
|
||||
contentHash?: string;
|
||||
binary?: boolean;
|
||||
size: number;
|
||||
language: string;
|
||||
markdown: boolean;
|
||||
@@ -53,6 +59,7 @@ type AgentInstructionsFileDetail = AgentInstructionsFileSummary & {
|
||||
type AgentInstructionsBundle = {
|
||||
agentId: string;
|
||||
companyId: string;
|
||||
persistence?: "agent_files";
|
||||
mode: BundleMode | null;
|
||||
rootPath: string | null;
|
||||
managedRootPath: string;
|
||||
@@ -112,11 +119,7 @@ function isMarkdown(relativePath: string) {
|
||||
}
|
||||
|
||||
function normalizeRelativeFilePath(candidatePath: string): string {
|
||||
const normalized = path.posix.normalize(candidatePath.replaceAll("\\", "/")).replace(/^\/+/, "");
|
||||
if (!normalized || normalized === "." || normalized === ".." || normalized.startsWith("../")) {
|
||||
throw unprocessable("Instructions file path must stay within the bundle root");
|
||||
}
|
||||
return normalized;
|
||||
return instructionPath(candidatePath);
|
||||
}
|
||||
|
||||
function resolvePathWithinRoot(rootPath: string, relativePath: string): string {
|
||||
@@ -130,7 +133,7 @@ function resolvePathWithinRoot(rootPath: string, relativePath: string): string {
|
||||
return absolutePath;
|
||||
}
|
||||
|
||||
function resolveManagedInstructionsRoot(agent: AgentLike): string {
|
||||
export function resolveManagedInstructionsRoot(agent: AgentLike): string {
|
||||
return path.resolve(
|
||||
resolvePaperclipInstanceRoot(),
|
||||
"companies",
|
||||
@@ -153,7 +156,7 @@ function resolveLegacyInstructionsPath(candidatePath: string, config: Record<str
|
||||
}
|
||||
|
||||
async function statIfExists(targetPath: string) {
|
||||
return fs.stat(targetPath).catch(() => null);
|
||||
return fs.lstat(targetPath).catch(() => null);
|
||||
}
|
||||
|
||||
function shouldIgnoreInstructionsEntry(entry: { name: string; isDirectory(): boolean; isFile(): boolean }) {
|
||||
@@ -172,14 +175,14 @@ function shouldIgnoreInstructionsEntry(entry: { name: string; isDirectory(): boo
|
||||
|
||||
async function listFilesRecursive(
|
||||
rootPath: string,
|
||||
options?: { rejectSymlinks?: boolean },
|
||||
options?: { rejectSymlinks?: boolean; legacyExcludes?: boolean },
|
||||
): Promise<string[]> {
|
||||
const output: string[] = [];
|
||||
|
||||
async function walk(currentPath: string, relativeDir: string) {
|
||||
const entries = await fs.readdir(currentPath, { withFileTypes: true }).catch(() => []);
|
||||
for (const entry of entries) {
|
||||
if (shouldIgnoreInstructionsEntry(entry)) continue;
|
||||
if (options?.legacyExcludes && shouldIgnoreInstructionsEntry(entry)) continue;
|
||||
const absolutePath = path.join(currentPath, entry.name);
|
||||
const relativePath = normalizeRelativeFilePath(
|
||||
relativeDir ? path.posix.join(relativeDir, entry.name) : entry.name,
|
||||
@@ -206,13 +209,21 @@ async function listFilesRecursive(
|
||||
async function readFileSummary(rootPath: string, relativePath: string, entryFile: string): Promise<AgentInstructionsFileSummary> {
|
||||
const absolutePath = resolvePathWithinRoot(rootPath, relativePath);
|
||||
const stat = await fs.stat(absolutePath);
|
||||
// External bundles may contain large assets; listing must not read them.
|
||||
const file = stat.size > MAX_AGENT_FILE_BYTES ? null : await inspectAgentFile(rootPath, relativePath);
|
||||
return summarizeFile(relativePath, entryFile, file?.size ?? stat.size, file?.bytes ?? null, file?.hash);
|
||||
}
|
||||
|
||||
function summarizeFile(relativePath: string, entryFile: string, size: number, bytes: Buffer | null, hash?: string): AgentInstructionsFileSummary {
|
||||
let binary = bytes === null;
|
||||
try { if (bytes?.includes(0)) binary = true; new TextDecoder("utf-8", { fatal: true }).decode(bytes ?? undefined); } catch { binary = true; }
|
||||
return {
|
||||
path: relativePath,
|
||||
size: stat.size,
|
||||
size,
|
||||
language: inferLanguage(relativePath),
|
||||
markdown: isMarkdown(relativePath),
|
||||
isEntryFile: relativePath === entryFile,
|
||||
editable: true,
|
||||
editable: !binary, binary, contentHash: hash,
|
||||
deprecated: false,
|
||||
virtual: false,
|
||||
};
|
||||
@@ -231,7 +242,7 @@ async function readLegacyInstructions(agent: AgentLike, config: Record<string, u
|
||||
return asString(config[PROMPT_KEY]) ?? "";
|
||||
}
|
||||
|
||||
function deriveBundleState(agent: AgentLike): BundleState {
|
||||
export function deriveBundleState(agent: AgentLike): BundleState {
|
||||
const config = asRecord(agent.adapterConfig);
|
||||
const warnings: string[] = [];
|
||||
const storedModeRaw = config[MODE_KEY];
|
||||
@@ -245,21 +256,23 @@ function deriveBundleState(agent: AgentLike): BundleState {
|
||||
const storedEntryRaw = asString(config[ENTRY_KEY]);
|
||||
if (storedEntryRaw) {
|
||||
try {
|
||||
entryFile = normalizeRelativeFilePath(storedEntryRaw);
|
||||
} catch {
|
||||
warnings.push(`Ignored invalid instructions entry file "${storedEntryRaw}".`);
|
||||
// Historical config accepted normalized relative paths. Keep that read
|
||||
// compatibility; new API writes still use strict instructionPath validation.
|
||||
entryFile = normalizeRelativeFilePath(path.posix.normalize(storedEntryRaw.replaceAll("\\", "/")).replace(/^\/+/, ""));
|
||||
} catch (error) {
|
||||
warnings.push(error instanceof Error ? error.message : String(error));
|
||||
}
|
||||
}
|
||||
|
||||
if (!rootPath && legacyInstructionsPath) {
|
||||
try {
|
||||
const resolvedLegacyPath = resolveLegacyInstructionsPath(legacyInstructionsPath, config);
|
||||
rootPath = path.dirname(resolvedLegacyPath);
|
||||
entryFile = path.basename(resolvedLegacyPath);
|
||||
mode = resolvedLegacyPath.startsWith(`${resolveManagedInstructionsRoot(agent)}${path.sep}`)
|
||||
|| resolvedLegacyPath === path.join(resolveManagedInstructionsRoot(agent), entryFile)
|
||||
? "managed"
|
||||
: "external";
|
||||
const managedRoot = resolveManagedInstructionsRoot(agent);
|
||||
const managedRelative = path.relative(managedRoot, resolvedLegacyPath);
|
||||
const inManagedRoot = managedRelative !== ".." && !managedRelative.startsWith(`..${path.sep}`) && !path.isAbsolute(managedRelative);
|
||||
rootPath = inManagedRoot ? managedRoot : path.dirname(resolvedLegacyPath);
|
||||
entryFile = inManagedRoot ? instructionPath(managedRelative.split(path.sep).join("/")) : path.basename(resolvedLegacyPath);
|
||||
mode = inManagedRoot ? "managed" : "external";
|
||||
if (!path.isAbsolute(legacyInstructionsPath)) {
|
||||
warnings.push("Using legacy relative instructionsFilePath; migrate this agent to a managed or absolute external bundle.");
|
||||
}
|
||||
@@ -303,7 +316,7 @@ async function recoverManagedBundleState(agent: AgentLike, state: BundleState):
|
||||
const files = await listFilesRecursive(managedRootPath);
|
||||
if (files.length === 0) return state;
|
||||
|
||||
const recoveredEntryFile = files.includes(state.entryFile)
|
||||
const recoveredEntryFile = asString(state.config[ENTRY_KEY]) ? state.entryFile : files.includes(state.entryFile)
|
||||
? state.entryFile
|
||||
: files.includes(ENTRY_FILE_DEFAULT)
|
||||
? ENTRY_FILE_DEFAULT
|
||||
@@ -370,6 +383,7 @@ function toBundle(agent: AgentLike, state: BundleState, files: AgentInstructions
|
||||
return {
|
||||
agentId: agent.id,
|
||||
companyId: agent.companyId,
|
||||
...(state.mode === "managed" ? { persistence: "agent_files" as const } : {}),
|
||||
mode: state.mode,
|
||||
rootPath: state.rootPath,
|
||||
managedRootPath: resolveManagedInstructionsRoot(agent),
|
||||
@@ -443,7 +457,8 @@ async function writeBundleFiles(
|
||||
) {
|
||||
for (const [relativePath, content] of Object.entries(files)) {
|
||||
const normalizedPath = normalizeRelativeFilePath(relativePath);
|
||||
const absolutePath = resolvePathWithinRoot(rootPath, normalizedPath);
|
||||
instructionBytes(content);
|
||||
const absolutePath = await assertInstructionPathSafe(rootPath, normalizedPath);
|
||||
const existingStat = await statIfExists(absolutePath);
|
||||
if (existingStat?.isFile() && !options?.overwriteExisting) continue;
|
||||
await fs.mkdir(path.dirname(absolutePath), { recursive: true });
|
||||
@@ -464,19 +479,26 @@ export function syncInstructionsBundleConfigFromFilePath(
|
||||
return next;
|
||||
}
|
||||
const resolvedPath = resolveLegacyInstructionsPath(instructionsFilePath, adapterConfig);
|
||||
const rootPath = path.dirname(resolvedPath);
|
||||
const entryFile = path.basename(resolvedPath);
|
||||
const mode: BundleMode = resolvedPath.startsWith(`${resolveManagedInstructionsRoot(agent)}${path.sep}`)
|
||||
|| resolvedPath === path.join(resolveManagedInstructionsRoot(agent), entryFile)
|
||||
? "managed"
|
||||
: "external";
|
||||
const managedRoot = resolveManagedInstructionsRoot(agent);
|
||||
const relative = path.relative(managedRoot, resolvedPath);
|
||||
const managed = relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
||||
const rootPath = managed ? managedRoot : path.dirname(resolvedPath);
|
||||
const entryFile = managed ? instructionPath(relative.split(path.sep).join("/")) : path.basename(resolvedPath);
|
||||
const mode: BundleMode = managed ? "managed" : "external";
|
||||
return applyBundleConfig(next, { mode, rootPath, entryFile });
|
||||
}
|
||||
|
||||
export function agentInstructionsService() {
|
||||
export function agentInstructionsService(db?: Db) {
|
||||
async function assertUnversionedEntry(agent: AgentLike, entryFile: string, connection: Db | Parameters<Parameters<Db["transaction"]>[0]>[0] | undefined = db) {
|
||||
if (!connection) throw unprocessable("Bundle initialization requires the database-backed instructions service");
|
||||
const [head] = await connection.select().from(agentInstructionHeads).where(and(eq(agentInstructionHeads.companyId, agent.companyId),
|
||||
eq(agentInstructionHeads.agentId, agent.id), eq(agentInstructionHeads.entryFile, entryFile)));
|
||||
if (head) throw unprocessable("This entry has revision history. Use the instruction content API with its baseRevisionId to update it.", { code: "INSTRUCTION_REVISION_REQUIRED" });
|
||||
}
|
||||
async function getBundle(agent: AgentLike): Promise<AgentInstructionsBundle> {
|
||||
const state = await recoverManagedBundleState(agent, deriveBundleState(agent));
|
||||
if (!state.rootPath) return toBundle(agent, state, []);
|
||||
await assertInstructionPathSafe(state.rootPath, state.entryFile);
|
||||
const stat = await statIfExists(state.rootPath);
|
||||
if (!stat?.isDirectory()) {
|
||||
return toBundle(agent, {
|
||||
@@ -484,8 +506,12 @@ export function agentInstructionsService() {
|
||||
warnings: [...state.warnings, `Instructions root does not exist: ${state.rootPath}`],
|
||||
}, []);
|
||||
}
|
||||
const files = await listFilesRecursive(state.rootPath);
|
||||
const summaries = await Promise.all(files.map((relativePath) => readFileSummary(state.rootPath!, relativePath, state.entryFile)));
|
||||
const files = await listFilesRecursive(state.rootPath, { legacyExcludes: state.mode === "external" });
|
||||
const summaries: AgentInstructionsFileSummary[] = [];
|
||||
// Bound open descriptors and text buffers even for a large personal folder.
|
||||
for (let index = 0; index < files.length; index += 8) {
|
||||
summaries.push(...await Promise.all(files.slice(index, index + 8).map(relativePath => readFileSummary(state.rootPath!, relativePath, state.entryFile))));
|
||||
}
|
||||
return toBundle(agent, state, summaries);
|
||||
}
|
||||
|
||||
@@ -507,24 +533,11 @@ export function agentInstructionsService() {
|
||||
};
|
||||
}
|
||||
if (!state.rootPath) throw notFound("Agent instructions bundle is not configured");
|
||||
const absolutePath = resolvePathWithinRoot(state.rootPath, relativePath);
|
||||
const [content, stat] = await Promise.all([
|
||||
fs.readFile(absolutePath, "utf8").catch(() => null),
|
||||
fs.stat(absolutePath).catch(() => null),
|
||||
]);
|
||||
if (content === null || !stat?.isFile()) throw notFound("Instructions file not found");
|
||||
const normalizedPath = normalizeRelativeFilePath(relativePath);
|
||||
return {
|
||||
path: normalizedPath,
|
||||
size: stat.size,
|
||||
language: inferLanguage(normalizedPath),
|
||||
markdown: isMarkdown(normalizedPath),
|
||||
isEntryFile: normalizedPath === state.entryFile,
|
||||
editable: true,
|
||||
deprecated: false,
|
||||
virtual: false,
|
||||
content,
|
||||
};
|
||||
await assertInstructionPathSafe(state.rootPath, relativePath);
|
||||
const file = await inspectAgentFile(state.rootPath, relativePath);
|
||||
if (file === null) throw notFound("Instructions file not found");
|
||||
const summary = summarizeFile(relativePath, state.entryFile, file.size, file.bytes, file.hash);
|
||||
return { ...summary, content: summary.binary ? "" : file.bytes!.toString("utf8") };
|
||||
}
|
||||
|
||||
async function ensureWritableBundle(
|
||||
@@ -551,11 +564,12 @@ export function agentInstructionsService() {
|
||||
});
|
||||
await fs.mkdir(managedRoot, { recursive: true });
|
||||
|
||||
const entryPath = resolvePathWithinRoot(managedRoot, entryFile);
|
||||
const entryPath = await assertInstructionPathSafe(managedRoot, entryFile);
|
||||
const entryStat = await statIfExists(entryPath);
|
||||
if (!entryStat?.isFile()) {
|
||||
const legacyInstructions = await readLegacyInstructions(agent, current.config);
|
||||
if (legacyInstructions.trim().length > 0) {
|
||||
await assertUnversionedEntry(agent, entryFile);
|
||||
await fs.mkdir(path.dirname(entryPath), { recursive: true });
|
||||
await fs.writeFile(entryPath, legacyInstructions, "utf8");
|
||||
}
|
||||
@@ -595,9 +609,13 @@ export function agentInstructionsService() {
|
||||
nextRootPath = resolvedRoot;
|
||||
}
|
||||
|
||||
await assertInstructionPathSafe(nextRootPath, nextEntryFile);
|
||||
await fs.mkdir(nextRootPath, { recursive: true });
|
||||
|
||||
const existingFiles = await listFilesRecursive(nextRootPath);
|
||||
if (nextMode === "managed" && !existingFiles.includes(nextEntryFile)) {
|
||||
await assertUnversionedEntry(agent, nextEntryFile);
|
||||
}
|
||||
const exported = await exportFiles(agent);
|
||||
if (existingFiles.length === 0) {
|
||||
await writeBundleFiles(nextRootPath, exported.files);
|
||||
@@ -618,7 +636,7 @@ export function agentInstructionsService() {
|
||||
return { bundle: nextBundle, adapterConfig: nextConfig };
|
||||
}
|
||||
|
||||
async function writeFile(
|
||||
async function writeFileUnversioned(
|
||||
agent: AgentLike,
|
||||
relativePath: string,
|
||||
content: string,
|
||||
@@ -642,7 +660,14 @@ export function agentInstructionsService() {
|
||||
return { bundle, file, adapterConfig };
|
||||
}
|
||||
|
||||
const configured = await recoverManagedBundleState(agent, current);
|
||||
if (normalizeRelativeFilePath(relativePath) === configured.entryFile) {
|
||||
throw unprocessable("Entry edits require the canonical instruction commit service and baseRevisionId", { code: "INSTRUCTION_REVISION_REQUIRED" });
|
||||
}
|
||||
if (configured.mode !== "external") agentFilePath(relativePath);
|
||||
const prepared = await ensureWritableBundle(agent, options);
|
||||
instructionBytes(content);
|
||||
await assertInstructionPathSafe(prepared.state.rootPath!, relativePath);
|
||||
const absolutePath = resolvePathWithinRoot(prepared.state.rootPath!, relativePath);
|
||||
await fs.mkdir(path.dirname(absolutePath), { recursive: true });
|
||||
await fs.writeFile(absolutePath, content, "utf8");
|
||||
@@ -654,7 +679,7 @@ export function agentInstructionsService() {
|
||||
return { bundle, file, adapterConfig: prepared.adapterConfig };
|
||||
}
|
||||
|
||||
async function deleteFile(agent: AgentLike, relativePath: string): Promise<{
|
||||
async function deleteFileUnversioned(agent: AgentLike, relativePath: string): Promise<{
|
||||
bundle: AgentInstructionsBundle;
|
||||
adapterConfig: Record<string, unknown>;
|
||||
}> {
|
||||
@@ -668,13 +693,35 @@ export function agentInstructionsService() {
|
||||
if (normalizedPath === state.entryFile) {
|
||||
throw unprocessable("Cannot delete the bundle entry file");
|
||||
}
|
||||
const absolutePath = resolvePathWithinRoot(state.rootPath, normalizedPath);
|
||||
const absolutePath = await assertInstructionPathSafe(state.rootPath, normalizedPath);
|
||||
await fs.rm(absolutePath, { force: true });
|
||||
const adapterConfig = buildPersistedBundleConfig(derived, state);
|
||||
const bundle = await getBundle({ ...agent, adapterConfig });
|
||||
return { bundle, adapterConfig };
|
||||
}
|
||||
|
||||
// Reload configuration under the commit lock: a previously supporting file
|
||||
// may have become the configured entry since the route loaded its agent.
|
||||
async function withCurrentAgent<T>(agent: AgentLike, relativePath: string, operation: (current: AgentLike) => Promise<T>) {
|
||||
if (!db) return operation(agent);
|
||||
return db.transaction(async (tx) => {
|
||||
const [current] = await tx.select().from(agents)
|
||||
.where(and(eq(agents.id, agent.id), eq(agents.companyId, agent.companyId))).for("update");
|
||||
if (!current) throw notFound("Agent not found");
|
||||
if (relativePath !== LEGACY_PROMPT_TEMPLATE_PATH) {
|
||||
// Historical entries also remain projections if the configured entry changes.
|
||||
await assertUnversionedEntry(current, normalizeRelativeFilePath(relativePath), tx);
|
||||
}
|
||||
return operation(current);
|
||||
});
|
||||
}
|
||||
async function writeFile(...args: Parameters<typeof writeFileUnversioned>) {
|
||||
return withCurrentAgent(args[0], args[1], (current) => writeFileUnversioned(current, args[1], args[2], args[3]));
|
||||
}
|
||||
async function deleteFile(...args: Parameters<typeof deleteFileUnversioned>) {
|
||||
return withCurrentAgent(args[0], args[1], (current) => deleteFileUnversioned(current, args[1]));
|
||||
}
|
||||
|
||||
async function exportFiles(agent: AgentLike, options?: { rejectSymlinks?: boolean }): Promise<{
|
||||
files: Record<string, string>;
|
||||
entryFile: string;
|
||||
@@ -684,7 +731,7 @@ export function agentInstructionsService() {
|
||||
if (state.rootPath) {
|
||||
const stat = await statIfExists(state.rootPath);
|
||||
if (stat?.isDirectory()) {
|
||||
const relativePaths = await listFilesRecursive(state.rootPath, options);
|
||||
const relativePaths = await listFilesRecursive(state.rootPath, { ...options, legacyExcludes: true });
|
||||
const files = Object.fromEntries(await Promise.all(relativePaths.map(async (relativePath) => {
|
||||
const absolutePath = resolvePathWithinRoot(state.rootPath!, relativePath);
|
||||
const content = await fs.readFile(absolutePath, "utf8");
|
||||
@@ -704,7 +751,7 @@ export function agentInstructionsService() {
|
||||
};
|
||||
}
|
||||
|
||||
async function materializeManagedBundle(
|
||||
async function materializeManagedBundleUnversioned(
|
||||
agent: AgentLike,
|
||||
files: Record<string, string>,
|
||||
options?: {
|
||||
@@ -716,6 +763,14 @@ export function agentInstructionsService() {
|
||||
const rootPath = resolveManagedInstructionsRoot(agent);
|
||||
const entryFile = options?.entryFile ? normalizeRelativeFilePath(options.entryFile) : ENTRY_FILE_DEFAULT;
|
||||
|
||||
for (const [relativePath, content] of Object.entries(files)) {
|
||||
instructionBytes(content);
|
||||
await assertInstructionPathSafe(rootPath, agentFilePath(relativePath));
|
||||
}
|
||||
const previous = await readInstructionBytes(rootPath, entryFile);
|
||||
if (previous && !previous.equals(instructionBytes(files[entryFile] ?? ""))) {
|
||||
throw unprocessable("Existing entry content must be saved through the canonical revision API before replacing a bundle", { code: "INSTRUCTION_REVISION_REQUIRED" });
|
||||
}
|
||||
if (options?.replaceExisting) {
|
||||
await fs.rm(rootPath, { recursive: true, force: true });
|
||||
}
|
||||
@@ -744,6 +799,24 @@ export function agentInstructionsService() {
|
||||
return { bundle, adapterConfig };
|
||||
}
|
||||
|
||||
async function materializeManagedBundle(
|
||||
...args: Parameters<typeof materializeManagedBundleUnversioned>
|
||||
) {
|
||||
if (!db) throw unprocessable("Bundle initialization requires the database-backed instructions service");
|
||||
return db.transaction(async (tx) => {
|
||||
const agent = args[0];
|
||||
const [owner] = await tx.select({ id: agents.id }).from(agents).where(and(eq(agents.id, agent.id), eq(agents.companyId, agent.companyId))).for("update");
|
||||
if (!owner) throw notFound("Agent not found");
|
||||
await assertUnversionedEntry(agent, args[2]?.entryFile ? normalizeRelativeFilePath(args[2].entryFile) : ENTRY_FILE_DEFAULT, tx);
|
||||
if (args[2]?.replaceExisting) {
|
||||
const [existingHead] = await tx.select({ id: agentInstructionHeads.revisionId }).from(agentInstructionHeads)
|
||||
.where(and(eq(agentInstructionHeads.companyId, agent.companyId), eq(agentInstructionHeads.agentId, agent.id))).limit(1);
|
||||
if (existingHead) throw unprocessable("Replacing this bundle would remove a versioned entry. Use canonical content commits.", { code: "INSTRUCTION_REVISION_REQUIRED" });
|
||||
}
|
||||
return materializeManagedBundleUnversioned(...args);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
getBundle,
|
||||
readFile,
|
||||
|
||||
@@ -62,6 +62,7 @@ export type AuthorizationAction =
|
||||
| PermissionKey
|
||||
| "agent_config:read"
|
||||
| "agent_config:update"
|
||||
| "agent_instructions:update"
|
||||
| "skill_config:update"
|
||||
| "agent:read"
|
||||
| "agent:wake"
|
||||
@@ -148,7 +149,7 @@ function companyIdForResource(resource: AuthorizationResource) {
|
||||
}
|
||||
|
||||
function permissionForAction(action: AuthorizationAction): PermissionKey | null {
|
||||
if (action === "agent_config:read" || action === "agent_config:update" || action === "skill_config:update") {
|
||||
if (action === "agent_config:read" || action === "agent_config:update" || action === "agent_instructions:update" || action === "skill_config:update") {
|
||||
return null;
|
||||
}
|
||||
if (
|
||||
@@ -998,6 +999,7 @@ export function authorizationService(db: Db | DbTransaction) {
|
||||
input.action === "decision_triage:manage" ||
|
||||
input.action === "agent_config:read" ||
|
||||
input.action === "agent_config:update" ||
|
||||
input.action === "agent_instructions:update" ||
|
||||
input.action === "skill_config:update" ||
|
||||
input.action === "inbox:manage" ||
|
||||
input.action === "runtime:manage" ||
|
||||
@@ -1734,7 +1736,7 @@ export function authorizationService(db: Db | DbTransaction) {
|
||||
if (input.action === "agent_config:read") {
|
||||
return decideWithAgentConfigReadGrant("user", input.actor.userId);
|
||||
}
|
||||
if (input.action === "agent_config:update") {
|
||||
if (input.action === "agent_config:update" || input.action === "agent_instructions:update") {
|
||||
return decideWithProtectedChangeGrants("user", input.actor.userId, {
|
||||
direct: "agents:configure",
|
||||
suggest: "agents:suggest-changes",
|
||||
@@ -2227,6 +2229,19 @@ export function authorizationService(db: Db | DbTransaction) {
|
||||
return decideWithAgentConfigReadGrant("agent", actorAgentId);
|
||||
}
|
||||
|
||||
if (input.action === "agent_instructions:update") {
|
||||
if (!isSimpleAssignableAgentStatus(actorAgent.status) || !input.actor.onBehalfOfUserId) {
|
||||
return deny({ action: input.action, reason: "deny_missing_membership", explanation: "Instruction edits require an active agent and a responsible user." });
|
||||
}
|
||||
// Explicit configure/suggest restrictions still govern content changes.
|
||||
// Only self edits may fall back to the responsible user's target access.
|
||||
const restricted = await decideWithProtectedChangeGrants("agent", actorAgentId, {
|
||||
direct: "agents:configure", suggest: "agents:suggest-changes",
|
||||
});
|
||||
if (restricted.reason !== "deny_no_grant" || input.resource.type !== "agent" || input.resource.agentId !== actorAgentId) return restricted;
|
||||
return allow({ action: input.action, reason: "allow_self", explanation: "Own instruction content edit, subject to the responsible user's target edit access." });
|
||||
}
|
||||
|
||||
if (input.action === "agent_config:update") {
|
||||
return decideWithProtectedChangeGrants("agent", actorAgentId, {
|
||||
direct: "agents:configure",
|
||||
@@ -2391,7 +2406,7 @@ export function authorizationService(db: Db | DbTransaction) {
|
||||
responsibleUserId,
|
||||
}, "responsible-user authorization intersection denied");
|
||||
|
||||
return responsibleUserAuthzShadowMode() ? agentDecision : denied;
|
||||
return input.action !== "agent_instructions:update" && responsibleUserAuthzShadowMode() ? agentDecision : denied;
|
||||
}
|
||||
|
||||
async function decide(input: {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import fs from "node:fs/promises";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
import path from "node:path";
|
||||
@@ -5,12 +6,16 @@ import { fileURLToPath } from "node:url";
|
||||
import { readPaperclipSkillSyncPreference, writePaperclipSkillSyncPreference } from "@paperclipai/adapter-utils/server-utils";
|
||||
import { and, desc, eq, ne } from "drizzle-orm";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { agents, builtInManagedResources, companies, issueThreadInteractions, issues, routines, routineTriggers } from "@paperclipai/db";
|
||||
import { activityLog, agents, builtInManagedResources, companies, issueThreadInteractions, issues, routines, routineTriggers } from "@paperclipai/db";
|
||||
import { syncRoutineVariablesWithTemplate } from "@paperclipai/shared";
|
||||
import type { Agent, Approval, CompanySkill, PermissionKey, Routine, RoutineTrigger, RoutineVariable } from "@paperclipai/shared";
|
||||
import { conflict, HttpError, notFound, unprocessable } from "../errors.js";
|
||||
import { logActivity } from "./activity-log.js";
|
||||
import { agentInstructionsService } from "./agent-instructions.js";
|
||||
import { adoptAgentFiles, agentFilePath, fileHash, readAgentFile, snapshotAgentFiles } from "./agent-file-store.js";
|
||||
import { assertInstructionPathSafe, instructionBytes, materializeInstructionBytes } from "./agent-instruction-files.js";
|
||||
import { agentInstructionRevisionService } from "./agent-instruction-revisions.js";
|
||||
import type { AuthorizationActor } from "./authorization.js";
|
||||
import { agentInstructionsBundleMode, agentInstructionsService } from "./agent-instructions.js";
|
||||
import { agentService } from "./agents.js";
|
||||
import { approvalService } from "./approvals.js";
|
||||
import {
|
||||
@@ -802,7 +807,7 @@ export function builtInAgentService(db: Db) {
|
||||
const agentSvc = agentService(db);
|
||||
const accessSvc = accessService(db);
|
||||
const approvalSvc = approvalService(db);
|
||||
const instructionsSvc = agentInstructionsService();
|
||||
const instructionsSvc = agentInstructionsService(db);
|
||||
const skillSvc = companySkillService(db);
|
||||
const routineSvc = routineService(db);
|
||||
|
||||
@@ -916,9 +921,9 @@ export function builtInAgentService(db: Db) {
|
||||
stockVersion: string;
|
||||
stockHash: string;
|
||||
defaultsJson: Record<string, unknown>;
|
||||
}) {
|
||||
}, connection: Db | Parameters<Parameters<Db["transaction"]>[0]>[0] = db) {
|
||||
const now = new Date();
|
||||
return db
|
||||
return connection
|
||||
.insert(builtInManagedResources)
|
||||
.values(input)
|
||||
.onConflictDoUpdate({
|
||||
@@ -952,10 +957,15 @@ export function builtInAgentService(db: Db) {
|
||||
return currentFiles;
|
||||
}
|
||||
|
||||
async function materializeInstructions(agent: Agent, definition: BuiltInAgentDefinition, mode: "reconcile" | "reset") {
|
||||
async function materializeInstructions(agent: Agent, definition: BuiltInAgentDefinition, mode: "reconcile" | "reset", actor?: AuthorizationActor) {
|
||||
const bundle = definition.bundle!;
|
||||
const stock = stockHash(bundle.instructions.files);
|
||||
const binding = await getManagedResourceBinding(agent.companyId, definition.key, "instructions", "AGENTS.md");
|
||||
// Stock operations also read the committed projection after interrupted writes.
|
||||
if (agentInstructionsBundleMode(agent) === "managed") {
|
||||
try { await agentInstructionRevisionService(db).materializeCurrent({ companyId: agent.companyId, agentId: agent.id }); }
|
||||
catch (error) { if (!(error instanceof HttpError && error.status === 404)) throw error; }
|
||||
}
|
||||
const currentFiles = await currentInstructionFiles(agent, bundle);
|
||||
const currentHash = Object.values(currentFiles).some((value) => value === null) ? null : stockHash(currentFiles);
|
||||
const currentState = stockState({
|
||||
@@ -969,6 +979,100 @@ export function builtInAgentService(db: Db) {
|
||||
changedFiles: changedFileList(currentFiles, bundle.instructions.files),
|
||||
});
|
||||
|
||||
if (!actor && mode === "reconcile" && agentInstructionsBundleMode(agent) === "managed"
|
||||
&& binding && (binding.stockHash !== stock || binding.defaultsJson.pendingInstructionsUpdate)) {
|
||||
const bindingWhere = and(eq(builtInManagedResources.companyId, agent.companyId),
|
||||
eq(builtInManagedResources.bundleKey, definition.key), eq(builtInManagedResources.resourceKind, "instructions"),
|
||||
eq(builtInManagedResources.resourceKey, "AGENTS.md"));
|
||||
const incoming = Object.fromEntries(Object.entries(bundle.instructions.files)
|
||||
.map(([file, content]) => [agentFilePath(file), instructionBytes(content)]));
|
||||
// Persist only the pending operation's baseline hashes before touching
|
||||
// files. If a write or DB commit fails, the next reconciliation can accept
|
||||
// already-applied bytes and retry. This is not instruction revision history.
|
||||
const prepared = await db.transaction(async (tx) => {
|
||||
const [current] = await tx.select().from(agents).where(and(eq(agents.companyId, agent.companyId), eq(agents.id, agent.id))).for("update");
|
||||
if (!current) throw notFound("Built-in agent not found");
|
||||
if (agentInstructionsBundleMode(current) !== "managed") return false;
|
||||
const root = await adoptAgentFiles(tx, current);
|
||||
const [latest] = await tx.select().from(builtInManagedResources).where(bindingWhere);
|
||||
if (!latest) return false;
|
||||
const pending = latest.defaultsJson.pendingInstructionsUpdate as {
|
||||
stockHash?: string; baseHashes?: Record<string, string | null>; nextHashes?: Record<string, string | null>;
|
||||
} | undefined;
|
||||
if (pending?.stockHash === stock) return true;
|
||||
if (!pending && latest.stockHash === stock) return false;
|
||||
const previousPaths = latest.defaultsJson.files;
|
||||
if (!Array.isArray(previousPaths) || !previousPaths.every((file): file is string => typeof file === "string")) return false;
|
||||
const ownedPaths = new Set(previousPaths);
|
||||
if (pending) {
|
||||
// A newer release (or rollback) can supersede an interrupted update.
|
||||
// Recognize only the previous operation's baseline or intended bytes;
|
||||
// never mistake an intervening operator edit for partially applied stock.
|
||||
if (!pending.baseHashes || !pending.nextHashes) return false;
|
||||
for (const [file, baseHash] of Object.entries(pending.baseHashes)) {
|
||||
if (!(file in pending.nextHashes)) return false;
|
||||
const bytes = await readAgentFile(root, agentFilePath(file));
|
||||
const hash = bytes === null ? null : fileHash(bytes);
|
||||
if (hash !== baseHash && hash !== pending.nextHashes[file]) return false;
|
||||
ownedPaths.add(file);
|
||||
}
|
||||
} else {
|
||||
const previousFiles: Record<string, string | null> = {};
|
||||
for (const file of previousPaths) {
|
||||
try { previousFiles[file] = (await instructionsSvc.readFile(current, file)).content; }
|
||||
catch { previousFiles[file] = null; }
|
||||
}
|
||||
if (stockHash(previousFiles) !== latest.stockHash) return false;
|
||||
}
|
||||
await snapshotAgentFiles(root);
|
||||
const baseHashes: Record<string, string | null> = {};
|
||||
const nextHashes: Record<string, string | null> = {};
|
||||
for (const file of new Set([...ownedPaths, ...Object.keys(incoming)])) {
|
||||
const bytes = await readAgentFile(root, agentFilePath(file));
|
||||
// A newly declared stock path must not overwrite a personal file.
|
||||
if (!ownedPaths.has(file) && bytes !== null && !bytes.equals(incoming[file]!)) return false;
|
||||
baseHashes[file] = bytes === null ? null : fileHash(bytes);
|
||||
nextHashes[file] = incoming[file] ? fileHash(incoming[file]) : null;
|
||||
}
|
||||
await tx.update(builtInManagedResources).set({ defaultsJson: { ...latest.defaultsJson,
|
||||
pendingInstructionsUpdate: { stockHash: stock, baseHashes, nextHashes } }, updatedAt: new Date() }).where(bindingWhere);
|
||||
return true;
|
||||
});
|
||||
if (!prepared) return currentState;
|
||||
return db.transaction(async (tx) => {
|
||||
const [current] = await tx.select().from(agents).where(and(eq(agents.companyId, agent.companyId), eq(agents.id, agent.id))).for("update");
|
||||
if (!current) throw notFound("Built-in agent not found");
|
||||
if (agentInstructionsBundleMode(current) !== "managed") return currentState;
|
||||
const root = await adoptAgentFiles(tx, current);
|
||||
const [latest] = await tx.select().from(builtInManagedResources).where(bindingWhere);
|
||||
const pending = latest?.defaultsJson.pendingInstructionsUpdate as { stockHash?: string; baseHashes?: Record<string, string | null> } | undefined;
|
||||
if (pending?.stockHash !== stock || !pending.baseHashes) return currentState;
|
||||
await snapshotAgentFiles(root);
|
||||
// Preflight every changed or removed path before mutation; preserve any
|
||||
// operator edit made between preparation, failure, and this retry.
|
||||
for (const [file, baseHash] of Object.entries(pending.baseHashes)) {
|
||||
const bytes = await readAgentFile(root, agentFilePath(file));
|
||||
const hash = bytes === null ? null : fileHash(bytes);
|
||||
const nextHash = incoming[file] ? fileHash(incoming[file]) : null;
|
||||
if (hash !== baseHash && hash !== nextHash) return currentState;
|
||||
}
|
||||
for (const [file, bytes] of Object.entries(incoming)) await materializeInstructionBytes(root, file, bytes);
|
||||
for (const file of Object.keys(pending.baseHashes)) {
|
||||
if (!(file in incoming)) await fs.rm(await assertInstructionPathSafe(root, file), { force: true });
|
||||
}
|
||||
await upsertManagedResourceBinding({ companyId: agent.companyId, bundleKey: definition.key,
|
||||
resourceKind: "instructions", resourceKey: "AGENTS.md", resourceId: agent.id,
|
||||
stockVersion: bundle.stockVersion, stockHash: stock,
|
||||
defaultsJson: { entryFile: bundle.instructions.entryFile, files: Object.keys(bundle.instructions.files) },
|
||||
}, tx);
|
||||
await tx.insert(activityLog).values({ companyId: agent.companyId, actorType: "system", actorId: "built-in-reconcile",
|
||||
action: "agent.files_updated", entityType: "agent", entityId: agent.id,
|
||||
details: { source: "built-in-stock-update", bundleKey: definition.key, stockHash: stock } });
|
||||
return stockState({ resourceKind: "instructions", resourceKey: "AGENTS.md", resourceId: agent.id,
|
||||
stockVersion: bundle.stockVersion, latestStockHash: stock, currentHash: stock, bindingStockHash: stock });
|
||||
});
|
||||
}
|
||||
|
||||
const shouldWrite =
|
||||
mode === "reset"
|
||||
|| currentState.stockStatus === "missing"
|
||||
@@ -992,13 +1096,36 @@ export function builtInAgentService(db: Db) {
|
||||
return currentState;
|
||||
}
|
||||
|
||||
const materialized = await instructionsSvc.materializeManagedBundle(agent, bundle.instructions.files, {
|
||||
entryFile: bundle.instructions.entryFile,
|
||||
replaceExisting: true,
|
||||
clearLegacyPromptTemplate: true,
|
||||
});
|
||||
let adapterConfig: Record<string, unknown>;
|
||||
if (currentFiles[bundle.instructions.entryFile] !== null && currentFiles[bundle.instructions.entryFile] !== undefined) {
|
||||
if (!actor && mode === "reconcile") return currentState;
|
||||
if (!actor) throw unprocessable("Resetting existing instructions requires an authenticated operator", { code: "INSTRUCTION_IDENTITY_INVALID" });
|
||||
const revisions = agentInstructionRevisionService(db);
|
||||
const target = { companyId: agent.companyId, agentId: agent.id };
|
||||
const baseline = await revisions.readCurrent(target, actor);
|
||||
const receipt = await revisions.commit({ ...target, entryFile: bundle.instructions.entryFile,
|
||||
baseRevisionId: baseline?.revision.id ?? null, content: bundle.instructions.files[bundle.instructions.entryFile] ?? "",
|
||||
source: actor.type === "board" ? "board" : "api" }, actor);
|
||||
if (receipt.materialization === "pending") throw conflict("Instruction revision saved; retry reset to repair its disk copy", { revisionId: receipt.revision.id });
|
||||
const refreshed = await agentSvc.getById(agent.id);
|
||||
if (!refreshed) throw notFound("Built-in agent not found");
|
||||
for (const [file, content] of Object.entries(bundle.instructions.files)) {
|
||||
if (file !== bundle.instructions.entryFile) await instructionsSvc.writeFile(refreshed, file, content);
|
||||
}
|
||||
// Preserve personal files outside this built-in bundle's declared paths.
|
||||
adapterConfig = { ...refreshed.adapterConfig };
|
||||
delete adapterConfig.promptTemplate;
|
||||
delete adapterConfig.bootstrapPromptTemplate;
|
||||
} else {
|
||||
const materialized = await instructionsSvc.materializeManagedBundle(agent, bundle.instructions.files, {
|
||||
entryFile: bundle.instructions.entryFile,
|
||||
replaceExisting: false,
|
||||
clearLegacyPromptTemplate: true,
|
||||
});
|
||||
adapterConfig = materialized.adapterConfig;
|
||||
}
|
||||
const updated = await agentSvc.update(agent.id, {
|
||||
adapterConfig: materialized.adapterConfig,
|
||||
adapterConfig,
|
||||
}, {
|
||||
allowBuiltInAgentMetadata: true,
|
||||
recordRevision: { source: `built-in-bundle:${mode}:instructions` },
|
||||
@@ -1480,13 +1607,14 @@ export function builtInAgentService(db: Db) {
|
||||
definition: BuiltInAgentDefinition,
|
||||
mode: "reconcile" | "reset",
|
||||
resources?: Array<"instructions" | "skill" | "routine">,
|
||||
actor?: AuthorizationActor,
|
||||
) {
|
||||
if (!definition.bundle) return [];
|
||||
const selected = new Set(resources ?? ["instructions", "skill", "routine"]);
|
||||
const existingStates = await bundleResourceStates(agent.companyId, definition, agent);
|
||||
const byKind = new Map(existingStates.map((state) => [state.resourceKind, state]));
|
||||
const instruction = selected.has("instructions")
|
||||
? await materializeInstructions(agent, definition, mode)
|
||||
? await materializeInstructions(agent, definition, mode, actor)
|
||||
: byKind.get("instructions")!;
|
||||
const refreshedAgent = await agentSvc.getById(agent.id) as Agent | null;
|
||||
if (!refreshedAgent) throw notFound("Built-in agent not found");
|
||||
@@ -1847,7 +1975,7 @@ export function builtInAgentService(db: Db) {
|
||||
return state(definition, updated as Agent);
|
||||
}
|
||||
|
||||
async function reset(companyId: string, key: string, input: { resources?: Array<"agent" | "instructions" | "skill" | "routine"> } = {}) {
|
||||
async function reset(companyId: string, key: string, input: { resources?: Array<"agent" | "instructions" | "skill" | "routine"> } = {}, actor?: AuthorizationActor) {
|
||||
const definition = requireBuiltInAgentDefinition(key);
|
||||
const resetAgentDefaults = !input.resources || input.resources.includes("agent");
|
||||
const current = resetAgentDefaults
|
||||
@@ -1862,6 +1990,7 @@ export function builtInAgentService(db: Db) {
|
||||
definition,
|
||||
"reset",
|
||||
input.resources ? selectedBundleResources ?? [] : undefined,
|
||||
actor,
|
||||
);
|
||||
return state(definition, await agentSvc.getById(current.agent.id) as Agent, resources);
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ function expandTargetKeysForLegacyCompatibility(targetKeys: string[]) {
|
||||
return [...expanded];
|
||||
}
|
||||
|
||||
export function changeConsentGateService(db: Db) {
|
||||
export function changeConsentGateService(db: Db | Parameters<Parameters<Db["transaction"]>[0]>[0]) {
|
||||
return {
|
||||
assertConsented: async (input: {
|
||||
companyId: string;
|
||||
|
||||
@@ -3549,7 +3549,7 @@ export function companyPortabilityService(db: Db, storage?: StorageService) {
|
||||
const companies = companyService(db);
|
||||
const agents = agentService(db);
|
||||
const assetRecords = assetService(db);
|
||||
const instructions = agentInstructionsService();
|
||||
const instructions = agentInstructionsService(db);
|
||||
const access = accessService(db);
|
||||
const projects = projectService(db);
|
||||
const issues = issueService(db);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { isAgentDirectoryCopy } from "./agent-directory-working-copies.js";
|
||||
|
||||
import type { PaperclipTurnContext } from "@paperclipai/adapter-utils/server-utils";
|
||||
import { restoreNativeWorkspaceBestEffort } from "./native-runtime/native-workspace-best-effort.js";
|
||||
import {
|
||||
@@ -67,6 +69,7 @@ import {
|
||||
startAdapterExecutionTargetPaperclipBridge,
|
||||
} from "@paperclipai/adapter-utils/execution-target";
|
||||
import { agentService } from "./agents.js";
|
||||
import { agentInstructionWorkingCopyService, instructionWorkingCopyGuidance } from "./agent-instruction-working-copies.js";
|
||||
import { normalizeLegacyRunnerProvider } from "@paperclipai/adapter-utils";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
@@ -3442,6 +3445,15 @@ const heartbeatRunSafeResultJsonColumn = sql<Record<string, unknown> | null>`
|
||||
'error', left(${heartbeatRuns.resultJson} ->> 'error', ${HEARTBEAT_RUN_RESULT_SUMMARY_MAX_CHARS}),
|
||||
'stdout', left(${heartbeatRuns.resultJson} ->> 'stdout', ${HEARTBEAT_RUN_RESULT_OUTPUT_MAX_CHARS}),
|
||||
'stderr', left(${heartbeatRuns.resultJson} ->> 'stderr', ${HEARTBEAT_RUN_RESULT_OUTPUT_MAX_CHARS}),
|
||||
'instructionSave', case when jsonb_typeof(${heartbeatRuns.resultJson} -> 'instructionSave') = 'object'
|
||||
then jsonb_strip_nulls(jsonb_build_object(
|
||||
'state', left(${heartbeatRuns.resultJson} #>> '{instructionSave,state}', 32),
|
||||
'contract', left(${heartbeatRuns.resultJson} #>> '{instructionSave,contract}', 32),
|
||||
'entryFile', left(${heartbeatRuns.resultJson} #>> '{instructionSave,entryFile}', 512),
|
||||
'errorCode', left(${heartbeatRuns.resultJson} #>> '{instructionSave,errorCode}', 128),
|
||||
'errorMessage', left(${heartbeatRuns.resultJson} #>> '{instructionSave,errorMessage}', 1024),
|
||||
'storageWarning', left(${heartbeatRuns.resultJson} #>> '{instructionSave,storageWarning}', 1024)
|
||||
)) end,
|
||||
'workspaceRestoreFailure', case when ${heartbeatRuns.resultJson} ->> 'workspaceRestoreFailure'
|
||||
in ('restore_permission_denied', 'restore_lock_timeout', 'restore_unsafe_archive', 'restore_failed')
|
||||
then ${heartbeatRuns.resultJson} -> 'workspaceRestoreFailure' end,
|
||||
@@ -9500,6 +9512,7 @@ export function heartbeatService(
|
||||
environmentRuntimeService(db, {
|
||||
pluginWorkerManager: options.pluginWorkerManager,
|
||||
});
|
||||
const instructionCopies = agentInstructionWorkingCopyService(db, { environmentRuntime });
|
||||
const envOrchestrator = environmentRunOrchestrator(db, {
|
||||
pluginWorkerManager: options.pluginWorkerManager,
|
||||
environmentRuntime,
|
||||
@@ -18866,6 +18879,14 @@ export function heartbeatService(
|
||||
async function reapOrphanedRuns(opts?: { staleThresholdMs?: number }) {
|
||||
const staleThresholdMs = opts?.staleThresholdMs ?? 0;
|
||||
const now = new Date();
|
||||
// Recovery never launches a provider or infers stopped ownership from
|
||||
// terminal status. Uncaptured local copies require durable stop evidence.
|
||||
await instructionCopies.recoverStopped().catch(error => {
|
||||
logger.warn({ err: error }, "failed to recover stopped instruction copies");
|
||||
});
|
||||
await instructionCopies.recoverCaptured().catch(error => {
|
||||
logger.warn({ err: error }, "failed to retry captured instruction revisions");
|
||||
});
|
||||
|
||||
// Complete persisted native results before generic orphan recovery. The
|
||||
// reconciler reads the durable workspace barrier and persisted runtime
|
||||
@@ -22348,6 +22369,29 @@ export function heartbeatService(
|
||||
await bindIssueToPersistedExecutionWorkspace(persistedExecutionWorkspace);
|
||||
const workspaceRealization = realizationResult.workspaceRealization;
|
||||
const executionTarget = realizationResult.executionTarget;
|
||||
let instructionCopy: Awaited<ReturnType<typeof instructionCopies.prepare>> = null;
|
||||
let instructionSave: Record<string, unknown> | null = null;
|
||||
const collectStoppedInstructions = async () => {
|
||||
if (!instructionCopy) return;
|
||||
let saved = await instructionCopies.collectStopped({ companyId: agent.companyId, runId: run.id, target: executionTarget });
|
||||
// Capture before disposal. Exhausted bounded collection leaves a durable
|
||||
// explicit loss report, never a claim that missing bytes were saved.
|
||||
while (saved?.state === "pending_collection" && saved.attempts < 3) {
|
||||
saved = await instructionCopies.collectStopped({ companyId: agent.companyId, runId: run.id, target: executionTarget });
|
||||
}
|
||||
if (!saved) return;
|
||||
const receipt = parseObject(saved.receipt);
|
||||
const storageWarning = readNonEmptyString(receipt.storageWarning);
|
||||
instructionSave = { state: saved.state, entryFile: saved.entryFile,
|
||||
...(isAgentDirectoryCopy(saved) ? { contract: "agent_files", appliedCandidateHash: saved.candidateHash }
|
||||
: { revisionId: parseObject(receipt.revision).id ?? null }), storageWarning, errorCode: saved.errorCode, errorMessage: saved.errorMessage };
|
||||
await appendRunEvent(run, { eventType: "instruction_save", stream: "system",
|
||||
level: !storageWarning && ["saved", "unchanged", "resolved"].includes(saved.state) ? "info" : "warn",
|
||||
message: storageWarning ?? (saved.state === "saved" ? "Agent files saved."
|
||||
: saved.state === "unchanged" ? "Instruction working copy is unchanged."
|
||||
: saved.errorMessage ?? "Instruction edits were not saved. Review the preserved candidate in the agent instruction editor."),
|
||||
payload: instructionSave });
|
||||
};
|
||||
if (managedAiRuntime && aiBinding) {
|
||||
try { await assertManagedAiProjectAuth({ ...resolvedConfig, cwd: executionWorkspace.cwd }, aiBinding.provider, executionTarget); }
|
||||
catch { throw new ConfigurationIncompleteFailure("Project authentication conflicts with this agent’s managed AI connection", { configurationIncomplete: { reason: "ai_connection_incompatible", actionUrl: `/agents/${agent.id}/runtime` } }); }
|
||||
@@ -23157,6 +23201,62 @@ export function heartbeatService(
|
||||
target: executionTarget,
|
||||
workspaceId: persistedExecutionWorkspace?.id ?? null,
|
||||
});
|
||||
const hasInstructionFilesystem = nativeRuntimeResolution.kind !== "native"
|
||||
? adapter.supportsInstructionsBundle === true
|
||||
: !["claude_managed_agents_api", "aws_agentcore_harness_api"].includes(nativeRuntimeResolution.profile.backend);
|
||||
if (hasInstructionFilesystem) {
|
||||
try {
|
||||
// Missing contract fields on a restored session mean the deployed
|
||||
// legacy format. New sessions opt into whole-directory persistence.
|
||||
const priorFileRun = taskSessionForRun?.lastRunId
|
||||
? await db.select({ profile: heartbeatRuns.runnerProfileJson }).from(heartbeatRuns).where(and(
|
||||
eq(heartbeatRuns.id, taskSessionForRun.lastRunId), eq(heartbeatRuns.companyId, agent.companyId), eq(heartbeatRuns.agentId, agent.id))).then(rows => rows[0])
|
||||
: null;
|
||||
const savedFileInput = parseObject(parseObject(run.runnerProfileJson).nativeExecutionInput);
|
||||
const priorFileInput = Object.keys(savedFileInput).length ? savedFileInput : parseObject(parseObject(priorFileRun?.profile).nativeExecutionInput);
|
||||
const priorWorkingCopy = parseObject(parseObject(parseObject(priorFileInput.runtimeContext).instructions).workingCopy);
|
||||
instructionCopy = await instructionCopies.prepare({
|
||||
companyId: agent.companyId, agentId: agent.id, runId: run.id,
|
||||
target: executionTarget, cwd: executionWorkspace.cwd,
|
||||
legacy: Object.keys(priorFileInput).length > 0 && priorWorkingCopy.kind !== "agent_files",
|
||||
});
|
||||
} catch (error) {
|
||||
if ((error as { status?: number }).status !== 403) throw error;
|
||||
// Missing write identity must not break a background run's read-only
|
||||
// prompt. It must also never imply that ordinary file edits will save.
|
||||
await appendRunEvent(run, { eventType: "instruction_save", stream: "system", level: "warn",
|
||||
message: "Persistent instruction editing is unavailable. Use an authenticated user with instruction edit access and a managed instruction bundle.",
|
||||
payload: { state: "unavailable", code: "INSTRUCTION_COPY_UNAVAILABLE" } });
|
||||
const guidance = "No editable agent instruction working copy is registered for this turn. Use authenticated agent file tools for persistent edits; do not edit a private copy named in an earlier turn or claim its changes will persist.";
|
||||
for (const key of ["paperclipTaskMarkdown", "paperclipTaskMarkdownCompact"]) {
|
||||
context[key] = [readNonEmptyString(context[key]), guidance].filter(Boolean).join("\n\n");
|
||||
}
|
||||
}
|
||||
if (instructionCopy) {
|
||||
const storageWarning = readNonEmptyString(instructionCopy.receipt?.storageWarning);
|
||||
if (storageWarning) {
|
||||
instructionSave = { state: "prepared", contract: "agent_files", storageWarning };
|
||||
// This is an advisory on the run, never an agent pause, execution
|
||||
// failure, or scheduling gate. Keep it visible while work runs.
|
||||
await db.update(heartbeatRuns).set({ resultJson: sql`coalesce(${heartbeatRuns.resultJson}, '{}'::jsonb) || ${JSON.stringify({ instructionSave })}::jsonb` }).where(eq(heartbeatRuns.id, run.id));
|
||||
await appendRunEvent(run, { eventType: "instruction_save", stream: "system", level: "warn",
|
||||
message: storageWarning, payload: instructionSave });
|
||||
}
|
||||
runtimeConfig = { ...runtimeConfig, instructionsFilePath: path.join(instructionCopy.localRoot, instructionCopy.entryFile) };
|
||||
if (isAgentDirectoryCopy(instructionCopy)) {
|
||||
const workspace = parseObject(context.paperclipWorkspace);
|
||||
context.paperclipWorkspace = { ...workspace, agentHome: instructionCopy.executionRoot,
|
||||
// Keep the pre-existing permission root stable for ACP session
|
||||
// identity. The per-run copy is already under the company root.
|
||||
agentHomeForPermissions: workspace.agentHome,
|
||||
};
|
||||
}
|
||||
const guidance = instructionWorkingCopyGuidance(instructionCopy);
|
||||
for (const key of ["paperclipTaskMarkdown", "paperclipTaskMarkdownCompact"]) {
|
||||
context[key] = [readNonEmptyString(context[key]), guidance].filter(Boolean).join("\n\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
let nativeExecution: NativeExecutionInput | null = null;
|
||||
let nativeRunnerInstanceId: string | null = null;
|
||||
if (nativeRuntimeResolution.kind === "native") {
|
||||
@@ -23586,6 +23686,7 @@ export function heartbeatService(
|
||||
runId: run.id,
|
||||
runtimeConfig,
|
||||
runtimeSkillEntries,
|
||||
instructionWorkingCopy: instructionCopy ? { rootPath: instructionCopy.executionRoot, entryPath: instructionCopy.entryFile, ...(isAgentDirectoryCopy(instructionCopy) ? { kind: "agent_files" as const } : {}) } : undefined,
|
||||
});
|
||||
const nativeExecutionWithCheckpoint =
|
||||
buildNativeExecutionWithCheckpoint({
|
||||
@@ -24293,6 +24394,10 @@ export function heartbeatService(
|
||||
},
|
||||
onLog,
|
||||
onEvent: onAdapterEvent,
|
||||
instructionWorkingCopy: instructionCopy ? {
|
||||
hasChanges: () => instructionCopies.hasChanges({ companyId: agent.companyId, runId: run.id, target: executionTarget }),
|
||||
collectStopped: collectStoppedInstructions,
|
||||
} : undefined,
|
||||
preparationSpans: nativeRunnerPreparationSpans,
|
||||
// Bootstrap with executable/home discovery while keeping
|
||||
// configured provider values and the server-selected
|
||||
@@ -24306,6 +24411,7 @@ export function heartbeatService(
|
||||
process.env,
|
||||
executionWorkspace.cwd,
|
||||
),
|
||||
...(instructionCopy && isAgentDirectoryCopy(instructionCopy) ? { AGENT_HOME: instructionCopy.executionRoot } : {}),
|
||||
...(nativeMcpServer
|
||||
? {
|
||||
PAPERCLIP_NATIVE_MCP_NAME: nativeMcpServer.name,
|
||||
@@ -24486,6 +24592,7 @@ export function heartbeatService(
|
||||
issueId,
|
||||
);
|
||||
},
|
||||
onProviderStopped: collectStoppedInstructions,
|
||||
onDispatch: markDispatchStarted,
|
||||
signal: executionControl.controller.signal,
|
||||
...(executionTarget?.kind === "remote" && executionTarget.transport === "sandbox" ? {
|
||||
@@ -24534,6 +24641,7 @@ export function heartbeatService(
|
||||
if (!guardedDispatch.dispatched) return;
|
||||
adapterResult = await guardedDispatch.resultPromise;
|
||||
}
|
||||
if (instructionSave) adapterResult.resultJson = { ...adapterResult.resultJson, instructionSave };
|
||||
adapterResult = applyWorkspaceRestoreFailure(adapterResult);
|
||||
// A returned result can include a failed restore. Keep the workspace
|
||||
// barrier closed until required files have been restored.
|
||||
@@ -24769,6 +24877,7 @@ export function heartbeatService(
|
||||
"failed to revoke heartbeat-run MCP gateway tokens",
|
||||
);
|
||||
}
|
||||
await instructionCopies.release(agent.companyId, run.id);
|
||||
}
|
||||
// Reconcile the referenced-project set against the real remote staging outcome. A referenced
|
||||
// project can pass authorization and clone locally at run prep, then fail to stage into the
|
||||
@@ -26160,6 +26269,15 @@ export function heartbeatService(
|
||||
},
|
||||
);
|
||||
}
|
||||
// A retained or unverified process stays above this release boundary.
|
||||
// If no stopped-copy capture occurred, preserve an explicit loss report.
|
||||
const uncapturedInstructions = await instructionCopies.reportUnavailable(run.companyId, run.id);
|
||||
if (uncapturedInstructions?.state === "unavailable") {
|
||||
await appendRunEvent(run, { eventType: "instruction_save", stream: "system", level: "warn",
|
||||
message: "Instruction edits could not be recovered before environment release. No instruction save is claimed.",
|
||||
payload: { state: "unavailable", code: uncapturedInstructions.errorCode } });
|
||||
}
|
||||
await instructionCopies.release(run.companyId, run.id);
|
||||
await releaseEnvironmentLeasesForRun({
|
||||
runId: run.id,
|
||||
companyId: run.companyId,
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
||||
import { agents, authUsers, companies, companyMemberships, principalPermissionGrants, heartbeatRuns, issues, agentInstructionRevisions, createDb } from "@paperclipai/db";
|
||||
import { startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js";
|
||||
import { resolveManagedInstructionsRoot } from "../agent-instructions.js";
|
||||
import { PaperclipRunnerToolAuthority } from "./paperclip-runner-tool-authority.js";
|
||||
|
||||
describe("canonical instruction tools through native authority", () => {
|
||||
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
||||
let db: ReturnType<typeof createDb>;
|
||||
let home: string;
|
||||
const previousHome = process.env.PAPERCLIP_HOME;
|
||||
let companyId: string, agentId: string, targetAgentId: string, userId: string, runId: string, issueId: string, root: string;
|
||||
let authority: PaperclipRunnerToolAuthority;
|
||||
const entryFile = "policy/ENTRY.md";
|
||||
const original = "\uFEFF# Original\r\n\0☃\n";
|
||||
beforeAll(async () => {
|
||||
home = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), "instruction-tool-home-")));
|
||||
process.env.PAPERCLIP_HOME = home;
|
||||
database = await startEmbeddedPostgresTestDatabase("instruction-tool-db-");
|
||||
db = createDb(database.connectionString);
|
||||
}, 90_000);
|
||||
afterAll(async () => {
|
||||
if (previousHome === undefined) delete process.env.PAPERCLIP_HOME; else process.env.PAPERCLIP_HOME = previousHome;
|
||||
await database?.cleanup();
|
||||
if (home) await fs.rm(home, { recursive: true, force: true });
|
||||
});
|
||||
beforeEach(async () => {
|
||||
[companyId, agentId, targetAgentId, userId, runId, issueId] = Array.from({ length: 6 }, () => randomUUID());
|
||||
await db.insert(companies).values({ id: companyId, name: "Tool scope", issuePrefix: randomUUID().slice(0, 8) });
|
||||
await db.insert(authUsers).values({ id: userId, name: "Editor", email: `${userId}@example.test`, createdAt: new Date(), updatedAt: new Date() });
|
||||
root = resolveManagedInstructionsRoot({ id: targetAgentId, companyId, name: "Target", adapterConfig: {} });
|
||||
await db.insert(agents).values([
|
||||
{ id: agentId, companyId, name: "Caller", status: "active" },
|
||||
{ id: targetAgentId, companyId, name: "Target", adapterConfig: { instructionsBundleMode: "managed", instructionsRootPath: root, instructionsEntryFile: entryFile } },
|
||||
]);
|
||||
await db.insert(companyMemberships).values([
|
||||
{ companyId, principalType: "user", principalId: userId, membershipRole: "operator" },
|
||||
{ companyId, principalType: "agent", principalId: agentId, membershipRole: "member" },
|
||||
]);
|
||||
await db.insert(principalPermissionGrants).values([
|
||||
{ companyId, principalType: "user", principalId: userId, permissionKey: "agents:configure", scope: { agentIds: [targetAgentId] } },
|
||||
{ companyId, principalType: "agent", principalId: agentId, permissionKey: "agents:configure", scope: { agentIds: [targetAgentId] } },
|
||||
]);
|
||||
await db.insert(issues).values({ id: issueId, companyId, title: "Maintain instructions", status: "in_progress", workMode: "standard", assigneeAgentId: agentId });
|
||||
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, status: "running", runtimeMode: "native", nativeIssueId: issueId, invocationSource: "on_demand", responsibleUserId: userId });
|
||||
await db.update(issues).set({ executionRunId: runId }).where(eq(issues.id, issueId));
|
||||
authority = new PaperclipRunnerToolAuthority(db, { companyId, agentId, issueId, runId });
|
||||
await fs.mkdir(path.dirname(path.join(root, entryFile)), { recursive: true });
|
||||
await fs.writeFile(path.join(root, entryFile), original);
|
||||
});
|
||||
const call = (tool: string, args: Record<string, unknown> = {}) => authority.execute({ tool, callId: randomUUID(), arguments: { targetAgentId, ...args } }) as Promise<any>;
|
||||
it("bridges old native instruction tools to current files without writing history", async () => {
|
||||
const first = await call("read_agent_instructions");
|
||||
expect(first).toMatchObject({ entryFile, content: original });
|
||||
const changed = await call("update_agent_instructions", { entryFile, content: "changed\r\n", baseRevisionId: first.revision.id });
|
||||
expect(changed.revision).toMatchObject({ responsibleUserId: userId, actorAgentId: agentId, sourceRunId: runId });
|
||||
expect((await call("get_agent_instruction_history", { entryFile })).revisions).toHaveLength(0);
|
||||
expect(await fs.readFile(path.join(root, entryFile), "utf8")).toBe("changed\r\n");
|
||||
await expect(call("update_agent_instructions", { entryFile, content: "stale", baseRevisionId: first.revision.id })).rejects.toMatchObject({ status: 409 });
|
||||
expect((await call("read_agent_instructions")).content).toBe("changed\r\n");
|
||||
});
|
||||
it("rejects supplied identity and missing CAS bases before changing content", async () => {
|
||||
const first = await call("read_agent_instructions");
|
||||
for (const identity of ["companyId", "agentId", "runId", "responsibleUserId", "onBehalfOfUserId", "actor"]) {
|
||||
await expect(call("update_agent_instructions", { entryFile, content: "forged", baseRevisionId: first.revision.id, [identity]: randomUUID() })).rejects.toMatchObject({ status: 400 });
|
||||
}
|
||||
await expect(call("update_agent_instructions", { entryFile, content: "missing base" })).rejects.toMatchObject({ status: 400 });
|
||||
expect((await call("read_agent_instructions")).content).toBe(original);
|
||||
expect(await db.select().from(agentInstructionRevisions).where(eq(agentInstructionRevisions.agentId, targetAgentId))).toHaveLength(0);
|
||||
});
|
||||
it("rechecks responsible-user access and rejects a cross-company target", async () => {
|
||||
const first = await call("read_agent_instructions");
|
||||
await db.delete(principalPermissionGrants).where(eq(principalPermissionGrants.principalId, userId));
|
||||
await expect(call("update_agent_instructions", { entryFile, content: "revoked", baseRevisionId: first.revision.id })).rejects.toMatchObject({ status: 403 });
|
||||
const foreignCompanyId = randomUUID(), foreignAgentId = randomUUID();
|
||||
await db.insert(companies).values({ id: foreignCompanyId, name: "Foreign", issuePrefix: randomUUID().slice(0, 8) });
|
||||
await db.insert(agents).values({ id: foreignAgentId, companyId: foreignCompanyId, name: "Foreign" });
|
||||
await expect(call("read_agent_instructions", { targetAgentId: foreignAgentId })).rejects.toMatchObject({ status: 404 });
|
||||
await expect(call("update_agent_instructions", { targetAgentId: foreignAgentId, entryFile, content: "foreign", baseRevisionId: null })).rejects.toMatchObject({ status: 404 });
|
||||
await db.update(heartbeatRuns).set({ status: "cancelled" }).where(eq(heartbeatRuns.id, runId));
|
||||
await expect(call("read_agent_instructions")).rejects.toThrow("paperclip_runner_tool_binding_not_authorized");
|
||||
expect(await fs.readFile(path.join(root, entryFile), "utf8")).toBe(original);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { PaperclipRunnerToolAuthority } from "./paperclip-runner-tool-authority.js";
|
||||
|
||||
const binding = { companyId: "00000000-0000-4000-8000-000000000001", agentId: "00000000-0000-4000-8000-000000000002", issueId: "00000000-0000-4000-8000-000000000003", runId: "00000000-0000-4000-8000-000000000004" };
|
||||
|
||||
describe("dedicated canonical instruction tools", () => {
|
||||
it("advertises read, CAS update, history, and restore on ordinary native runs", () => {
|
||||
const authority = new PaperclipRunnerToolAuthority({} as Db, binding);
|
||||
const tools = authority.definitions();
|
||||
expect(tools.map((tool) => tool.name)).toEqual(expect.arrayContaining([
|
||||
"read_agent_instructions", "update_agent_instructions", "get_agent_instruction_history", "restore_agent_instructions",
|
||||
]));
|
||||
for (const name of ["update_agent_instructions", "restore_agent_instructions"]) {
|
||||
const tool = tools.find((candidate) => candidate.name === name)!;
|
||||
expect(tool.inputSchema).toMatchObject({ required: expect.arrayContaining(["baseRevisionId"]), additionalProperties: false });
|
||||
expect(tool.inputSchema).not.toHaveProperty("properties.responsibleUserId");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,76 @@
|
||||
import type { AgentInstructionCommitReceipt } from "@paperclipai/shared";
|
||||
import { z } from "zod";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { badRequest, notFound } from "../../errors.js";
|
||||
import { agentInstructionRevisionService } from "../agent-instruction-revisions.js";
|
||||
import { agentInstructionWorkingCopyService } from "../agent-instruction-working-copies.js";
|
||||
import { deriveBundleState } from "../agent-instructions.js";
|
||||
import { agentService } from "../agents.js";
|
||||
import type { AuthorizationActor } from "../authorization.js";
|
||||
|
||||
const target = { targetAgentId: z.string().uuid().optional() };
|
||||
const entryFile = z.string().min(1).max(4096);
|
||||
const baseRevisionId = z.string().uuid().nullable();
|
||||
const schemas = {
|
||||
read_agent_instructions: z.object({ ...target, entryFile: entryFile.optional(), revisionId: z.string().uuid().optional() }).strict()
|
||||
.refine((input) => Boolean(input.entryFile) === Boolean(input.revisionId), "Historical reads require both entryFile and revisionId"),
|
||||
update_agent_instructions: z.object({ ...target, entryFile, content: z.string().max(1024 * 1024), baseRevisionId }).strict(),
|
||||
get_agent_instruction_history: z.object({ ...target, entryFile, cursor: z.string().min(1).max(2048).optional(), limit: z.number().int().min(1).max(100).optional() }).strict(),
|
||||
restore_agent_instructions: z.object({ ...target, entryFile, revisionId: z.string().uuid(), baseRevisionId: z.string().uuid() }).strict(),
|
||||
};
|
||||
|
||||
export type AgentInstructionToolName = keyof typeof schemas;
|
||||
|
||||
/** Called only after the native authority validates its server-bound active run. */
|
||||
export async function executeAgentInstructionTool(input: {
|
||||
db: Db;
|
||||
binding: { companyId: string; agentId: string; runId: string };
|
||||
tool: AgentInstructionToolName;
|
||||
arguments: unknown;
|
||||
}) {
|
||||
// Never accept an actor, responsible user, company, or source run from tool JSON.
|
||||
const actor: AuthorizationActor = { type: "agent", source: "agent_jwt", companyId: input.binding.companyId, agentId: input.binding.agentId, runId: input.binding.runId };
|
||||
const service = agentInstructionRevisionService(input.db);
|
||||
const parse = <T>(schema: z.ZodType<T>): T => {
|
||||
const result = schema.safeParse(input.arguments);
|
||||
if (!result.success) throw badRequest("Invalid instruction tool arguments", result.error.flatten());
|
||||
return result.data;
|
||||
};
|
||||
const scope = (targetAgentId?: string) => ({ companyId: input.binding.companyId, agentId: targetAgentId ?? input.binding.agentId });
|
||||
const acknowledge = async (receipt: AgentInstructionCommitReceipt, targetAgentId?: string) => {
|
||||
// The commit is already durable. A failed baseline refresh keeps the old CAS
|
||||
// fence, so later cleanup preserves a conflict instead of overwriting it.
|
||||
await agentInstructionWorkingCopyService(input.db).acknowledgeExplicitSave({
|
||||
...scope(targetAgentId), runId: input.binding.runId, entryFile: receipt.revision.entryFile,
|
||||
revisionId: receipt.revision.id, contentHash: receipt.revision.contentHash,
|
||||
}).catch(() => undefined);
|
||||
return receipt;
|
||||
};
|
||||
switch (input.tool) {
|
||||
case "read_agent_instructions": {
|
||||
const args = parse(schemas.read_agent_instructions);
|
||||
const target = scope(args.targetAgentId);
|
||||
if (args.entryFile && args.revisionId) return service.readRevision({ ...target, entryFile: args.entryFile, revisionId: args.revisionId }, actor);
|
||||
const snapshot = await service.readCurrent(target, actor);
|
||||
if (snapshot) return { ...snapshot, entryFile: snapshot.revision.entryFile };
|
||||
// An unseeded empty entry still needs its configured filename for the first CAS.
|
||||
const agent = await agentService(input.db).getById(target.agentId);
|
||||
if (!agent || agent.companyId !== target.companyId) throw notFound("Agent not found");
|
||||
return { entryFile: deriveBundleState(agent).entryFile, revision: null, content: null };
|
||||
}
|
||||
case "update_agent_instructions": {
|
||||
const args = parse(schemas.update_agent_instructions);
|
||||
const receipt = await service.commit({ ...scope(args.targetAgentId), entryFile: args.entryFile, content: args.content, baseRevisionId: args.baseRevisionId, source: "tool" }, actor);
|
||||
return acknowledge(receipt, args.targetAgentId);
|
||||
}
|
||||
case "get_agent_instruction_history": {
|
||||
const args = parse(schemas.get_agent_instruction_history);
|
||||
return service.history({ ...scope(args.targetAgentId), entryFile: args.entryFile, cursor: args.cursor, limit: args.limit }, actor);
|
||||
}
|
||||
case "restore_agent_instructions": {
|
||||
const args = parse(schemas.restore_agent_instructions);
|
||||
const receipt = await service.restore({ ...scope(args.targetAgentId), entryFile: args.entryFile, revisionId: args.revisionId, baseRevisionId: args.baseRevisionId }, actor);
|
||||
return acknowledge(receipt, args.targetAgentId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5870,6 +5870,57 @@ describe("native session same-turn steering", () => {
|
||||
});
|
||||
|
||||
describe("native warm session supervision", () => {
|
||||
it.each([
|
||||
{ changed: false, closeFails: false },
|
||||
{ changed: true, closeFails: false },
|
||||
{ changed: true, closeFails: true },
|
||||
])("collects changed instructions only after the owned warm provider stops (changed=$changed, close fails=$closeFails)", async ({ changed, closeFails }) => {
|
||||
const identity = `instruction-close-${changed}-${closeFails}`;
|
||||
let releaseClose!: () => void;
|
||||
const closed = new Promise<void>((resolve) => { releaseClose = resolve; });
|
||||
const close = vi.fn(async () => { await closed; if (closeFails) throw new Error("instruction provider close failed"); });
|
||||
const collectStopped = vi.fn(async () => {});
|
||||
const hasChanges = vi.fn(async () => changed);
|
||||
const warmExecution = { ...execution,
|
||||
binding: { ...execution.binding, runId: identity, executionWorkspaceId: identity },
|
||||
session: { ...execution.session, normalizedSessionId: identity, lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: 60_000 } },
|
||||
} as NativeExecutionInputV1;
|
||||
state.execute.mockReset().mockImplementationOnce(async (options) => {
|
||||
expect(options.requireSessionCloseBeforeReturn).toBe(true);
|
||||
expect(options.onSessionClosed).toBe(collectStopped);
|
||||
await options.onSession?.({ close });
|
||||
return { result: { summary: "completed" }, terminal: { runTerminalState: "succeeded" },
|
||||
turnId: identity, normalizedSessionId: identity, providerSessionId: identity,
|
||||
driverKind: "test", driverVersion: "1", nativeEventCount: 1, highestContiguousSourceSeq: 1, usage: null };
|
||||
});
|
||||
const running = executePaperclipNativeSession({ db: leaseDb(warmExecution), execution: warmExecution,
|
||||
runnerInstanceId: identity, runnerExecutionTarget: { kind: "remote", transport: "sandbox", environmentId: identity, remoteCwd: `/tmp/${identity}` },
|
||||
instructionWorkingCopy: { hasChanges, collectStopped } });
|
||||
const observed = running.then(() => null, error => error);
|
||||
try {
|
||||
if (changed) {
|
||||
await vi.waitFor(() => expect(close).toHaveBeenCalledOnce());
|
||||
expect(collectStopped).not.toHaveBeenCalled();
|
||||
releaseClose();
|
||||
const error = await observed;
|
||||
if (closeFails) {
|
||||
expect(error?.message).toBe("instruction provider close failed");
|
||||
expect(collectStopped).not.toHaveBeenCalled();
|
||||
} else {
|
||||
expect(error).toBeNull();
|
||||
expect(collectStopped).toHaveBeenCalledOnce();
|
||||
}
|
||||
} else {
|
||||
expect(await observed).toBeNull();
|
||||
expect(close).not.toHaveBeenCalled();
|
||||
expect(collectStopped).not.toHaveBeenCalled();
|
||||
}
|
||||
} finally {
|
||||
releaseClose();
|
||||
await closeWarmNativeSessionsForEnvironment({ environmentId: identity, reason: "test cleanup" });
|
||||
}
|
||||
});
|
||||
|
||||
describe("warm session identity transitions", () => {
|
||||
let previousHome: string | undefined;
|
||||
let isolatedHome: string;
|
||||
|
||||
@@ -7167,6 +7167,12 @@ export async function executePaperclipNativeSession(input: {
|
||||
chatAttachmentReadScope?: NativeChatAttachmentReadScope;
|
||||
onLog?: (stream: "stdout" | "stderr", chunk: string) => Promise<void>;
|
||||
onEvent?: (event: AdapterRuntimeEvent) => Promise<void>;
|
||||
/** Only this run's registered private instruction entry.
|
||||
* Probe at terminal; persist only after owned shutdown. */
|
||||
instructionWorkingCopy?: {
|
||||
hasChanges: () => Promise<boolean>;
|
||||
collectStopped: () => Promise<void>;
|
||||
};
|
||||
/** Persist task-level continuity before a durable goal can outlive this run. */
|
||||
onGoalCheckpoint?: (snapshot: PersistedNativeSession) => Promise<void>;
|
||||
sessionGoalControl?: NativeSessionGoalControl | null;
|
||||
@@ -8305,7 +8311,8 @@ async function executePaperclipNativeSessionWithinScope(
|
||||
resumeSessionGoalHeartbeat: input.resumeSessionGoalHeartbeat,
|
||||
// Every durable runner must finish its bounded suspension before
|
||||
// the next run verifies and rotates the saved authority.
|
||||
requireSessionCloseBeforeReturn: runnerdBackend !== null,
|
||||
requireSessionCloseBeforeReturn: runnerdBackend !== null || input.instructionWorkingCopy !== undefined,
|
||||
onSessionClosed: input.instructionWorkingCopy?.collectStopped,
|
||||
onCheckpoint: async (snapshot) => {
|
||||
if (warmSessionId !== null && warmConfigDigest !== null) {
|
||||
await persistWarmNativeCheckpoint(
|
||||
@@ -9065,12 +9072,22 @@ async function executePaperclipNativeSessionWithinScope(
|
||||
// A following run cannot attach until the prior run's durable finalization
|
||||
// is committed. Provider completion alone is not an authority boundary.
|
||||
if (warmSessionId !== null && lifecyclePolicy.mode === "warm") {
|
||||
const instructionCopy = input.instructionWorkingCopy;
|
||||
const ownedSession = warmNativeSessions.get(warmSessionId);
|
||||
const collectInstructions = Boolean(instructionCopy && ownedSession?.ownerToken === warmSessionOwnerToken &&
|
||||
await instructionCopy.hasChanges());
|
||||
if (collectInstructions && ownedSession) {
|
||||
// Keep the unchanged warm path intact. A changed private instruction copy
|
||||
// requires the existing checkpoint-and-close boundary before collection.
|
||||
ownedSession.closeOnReleaseReason = "registered instruction edits require stopped-provider collection";
|
||||
}
|
||||
await releaseWarmNativeSession(
|
||||
warmSessionId,
|
||||
warmSessionOwnerToken,
|
||||
lifecyclePolicy.idleTimeoutMs,
|
||||
false,
|
||||
);
|
||||
if (collectInstructions) await instructionCopy!.collectStopped();
|
||||
}
|
||||
const adapterResult: AdapterExecutionResult = {
|
||||
exitCode: native.terminal.runTerminalState === "succeeded" ? 0 : 1,
|
||||
|
||||
@@ -99,7 +99,7 @@ describe("PaperclipRunnerToolAuthority", () => {
|
||||
issueId,
|
||||
runId,
|
||||
});
|
||||
expect(authority.definitions()).toHaveLength(30);
|
||||
expect(authority.definitions()).toHaveLength(34);
|
||||
const questions = authority.definitions().find(tool => tool.name === "request_human_input")!;
|
||||
expect(questions.description).toContain("ask only the next unanswered question");
|
||||
expect(questions.description).toContain("Never infer answers");
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { executeAgentInstructionTool } from "./agent-instruction-tools.js";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { publicChatTaskUrl } from "../chat-task-url.js";
|
||||
import type { createAssignedMcpTools } from "./assigned-mcp-tools.js";
|
||||
@@ -83,6 +84,7 @@ import {
|
||||
} from "./chat-attachment-read.js";
|
||||
|
||||
const IMPLEMENTED_OPERATIONS = new Set([
|
||||
"read_agent_instructions", "update_agent_instructions", "get_agent_instruction_history", "restore_agent_instructions",
|
||||
"search_api", "call_api", "hire_agent",
|
||||
"get_task_context", "get_task_history", "search_tasks", "report_progress",
|
||||
"request_human_input",
|
||||
@@ -368,6 +370,13 @@ export class PaperclipRunnerToolAuthority {
|
||||
throw new Error("paperclip_runner_tool_mode_denied");
|
||||
}
|
||||
switch (call.tool) {
|
||||
case "read_agent_instructions":
|
||||
case "update_agent_instructions":
|
||||
case "get_agent_instruction_history":
|
||||
case "restore_agent_instructions":
|
||||
return executeAgentInstructionTool({ db: this.db, binding: {
|
||||
companyId: this.binding.companyId, agentId: this.binding.agentId, runId: this.binding.runId,
|
||||
}, tool: call.tool, arguments: call.arguments });
|
||||
case "create_skill": {
|
||||
const apiUrl = this.binding.apiUrl ?? process.env.PAPERCLIP_API_URL;
|
||||
const token = createLocalAgentJwt(this.binding.agentId, this.binding.companyId, context.actor.adapterType, this.binding.runId, context.run.responsibleUserId);
|
||||
|
||||
@@ -42,6 +42,10 @@ function words(text: string): string[] {
|
||||
}
|
||||
|
||||
function dedicatedTools(method: string, path: string): string[] {
|
||||
if (/\/agents\/\{[^}]+\}\/instructions-bundle\/file$/.test(path)) return method === "GET" ? ["read_agent_instructions"] : method === "PUT" ? ["update_agent_instructions"] : [];
|
||||
if (/\/agents\/\{[^}]+\}\/instructions-bundle\/history$/.test(path) && method === "GET") return ["get_agent_instruction_history"];
|
||||
if (/\/agents\/\{[^}]+\}\/instructions-bundle\/revision\/\{[^}]+\}$/.test(path) && method === "GET") return ["read_agent_instructions"];
|
||||
if (/\/agents\/\{[^}]+\}\/instructions-bundle\/restore$/.test(path) && method === "POST") return ["restore_agent_instructions"];
|
||||
if (/\/companies\/\{[^}]+\}\/skills$/.test(path) && method === "POST") return ["create_skill"];
|
||||
if (/\/companies\/\{[^}]+\}\/agent-hires$/.test(path) && method === "POST") return ["hire_agent"];
|
||||
if (/\/projects$/.test(path)) return method === "GET" ? ["list_projects"] : method === "POST" ? ["create_project"] : [];
|
||||
|
||||
@@ -72,6 +72,10 @@ describe("runner API catalog", () => {
|
||||
expect(runnerApiOperation(createProject).dedicatedTools).toEqual(["create_project"]);
|
||||
expect(runnerApiOperation(projects).dedicatedTools).toEqual(["list_projects"]);
|
||||
expect(runnerApiOperation("GET /api/companies/{companyId}/project-repositories").dedicatedTools).toEqual(["list_project_repositories"]);
|
||||
expect(runnerApiOperation("GET /api/agents/{id}/instructions-bundle/file").dedicatedTools).toEqual(["read_agent_instructions"]);
|
||||
expect(runnerApiOperation("PUT /api/agents/{id}/instructions-bundle/file").dedicatedTools).toEqual(["update_agent_instructions"]);
|
||||
expect(runnerApiOperation("GET /api/agents/{id}/instructions-bundle/history").dedicatedTools).toEqual(["get_agent_instruction_history"]);
|
||||
expect(runnerApiOperation("POST /api/agents/{id}/instructions-bundle/restore").dedicatedTools).toEqual(["restore_agent_instructions"]);
|
||||
expect(runnerApiOperation("POST /api/companies/{companyId}/agent-hires").dedicatedTools).toEqual(["hire_agent"]);
|
||||
expect(runnerApiOperation("POST /api/companies/{companyId}/agent-hires").dedicatedToolGuidance).toContain("inherits the caller's native runtime");
|
||||
});
|
||||
|
||||
@@ -10,6 +10,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const serviceMocks = vi.hoisted(() => ({
|
||||
exportFiles: vi.fn(),
|
||||
readCommittedForRuntime: vi.fn(),
|
||||
getEffectiveProfilesForAgent: vi.fn(),
|
||||
githubBotConnectionIdsForRun: vi.fn(),
|
||||
}));
|
||||
@@ -20,6 +21,11 @@ vi.mock("../chat-github-tools.js", () => ({
|
||||
|
||||
vi.mock("../agent-instructions.js", () => ({
|
||||
agentInstructionsService: () => ({ exportFiles: serviceMocks.exportFiles }),
|
||||
agentInstructionsBundleMode: (agent: { adapterConfig?: { instructionsBundleMode?: string } }) => agent.adapterConfig?.instructionsBundleMode ?? null,
|
||||
}));
|
||||
|
||||
vi.mock("../agent-instruction-revisions.js", () => ({
|
||||
agentInstructionRevisionService: () => ({ readCommittedForRuntime: serviceMocks.readCommittedForRuntime }),
|
||||
}));
|
||||
|
||||
vi.mock("../tool-access.js", () => ({
|
||||
@@ -50,6 +56,7 @@ async function makeTreeWritable(target: string): Promise<void> {
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
serviceMocks.readCommittedForRuntime.mockResolvedValue(null);
|
||||
serviceMocks.githubBotConnectionIdsForRun.mockResolvedValue(new Set());
|
||||
previousPaperclipHome = process.env.PAPERCLIP_HOME;
|
||||
previousInstanceId = process.env.PAPERCLIP_INSTANCE_ID;
|
||||
@@ -90,6 +97,60 @@ afterEach(async () => {
|
||||
});
|
||||
|
||||
describe("buildNativeRuntimeContext", () => {
|
||||
it("uses committed instructions when the disk projection is stale", async () => {
|
||||
serviceMocks.exportFiles.mockResolvedValue({
|
||||
entryFile: "AGENTS.md",
|
||||
files: { "AGENTS.md": "Old disk projection\n", "reference.md": "Sibling reference\n" },
|
||||
});
|
||||
serviceMocks.readCommittedForRuntime.mockResolvedValue({
|
||||
revision: { entryFile: "AGENTS.md" }, content: "Saved canonical instructions\n",
|
||||
});
|
||||
const context = await buildNativeRuntimeContext({
|
||||
db: {} as Db,
|
||||
agent: { id: "agent-1", companyId: "company-1", name: "Reviewer", adapterType: "paperclip_runner", adapterConfig: { instructionsBundleMode: "managed" } },
|
||||
runId: "run-1", runtimeConfig: {}, runtimeSkillEntries: [],
|
||||
});
|
||||
expect(await readFile(path.join(context.instructions.bundle.rootPath, context.instructions.entryPath), "utf8"))
|
||||
.toBe("Saved canonical instructions\n");
|
||||
expect(await readFile(path.join(context.instructions.bundle.rootPath, "reference.md"), "utf8"))
|
||||
.toBe("Sibling reference\n");
|
||||
expect(serviceMocks.readCommittedForRuntime).toHaveBeenCalledWith({ companyId: "company-1", agentId: "agent-1" });
|
||||
});
|
||||
|
||||
it("does not substitute canonical managed instructions into an external bundle", async () => {
|
||||
serviceMocks.exportFiles.mockResolvedValue({ entryFile: "AGENTS.md", files: { "AGENTS.md": "External instructions\n" } });
|
||||
const context = await buildNativeRuntimeContext({
|
||||
db: {} as Db,
|
||||
agent: { id: "agent-1", companyId: "company-1", name: "Reviewer", adapterConfig: { instructionsBundleMode: "external" } },
|
||||
runId: "run-1", runtimeConfig: {}, runtimeSkillEntries: [],
|
||||
});
|
||||
expect(await readFile(path.join(context.instructions.bundle.rootPath, context.instructions.entryPath), "utf8"))
|
||||
.toBe("External instructions\n");
|
||||
expect(serviceMocks.readCommittedForRuntime).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps the pinned prompt immutable while exposing the registered editable instruction copy", async () => {
|
||||
serviceMocks.exportFiles.mockResolvedValue({
|
||||
entryFile: "instructions/CHARter.md",
|
||||
files: { "instructions/CHARter.md": "Original instructions.\n" },
|
||||
});
|
||||
const workingRoot = path.join(temporaryRoots.at(-1)!, "private-run-copy");
|
||||
await mkdir(path.join(workingRoot, "instructions"), { recursive: true });
|
||||
await writeFile(path.join(workingRoot, "instructions/CHARter.md"), "Original instructions.\n");
|
||||
const context = await buildNativeRuntimeContext({
|
||||
db: {} as Db,
|
||||
agent: { id: "agent-1", companyId: "company-1", name: "Reviewer", adapterType: "paperclip_runner", adapterConfig: {} },
|
||||
runId: "run-1",
|
||||
runtimeConfig: {},
|
||||
runtimeSkillEntries: [],
|
||||
instructionWorkingCopy: { rootPath: workingRoot, entryPath: "instructions/CHARter.md" },
|
||||
});
|
||||
expect(context.instructions.workingCopy).toEqual({ rootPath: workingRoot, entryPath: "instructions/CHARter.md" });
|
||||
await writeFile(path.join(workingRoot, "instructions/CHARter.md"), "Persist this next time.\n");
|
||||
expect(await readFile(path.join(context.instructions.bundle.rootPath, context.instructions.entryPath), "utf8"))
|
||||
.toBe("Original instructions.\n");
|
||||
});
|
||||
|
||||
it.each(["disabled", "degraded"] as const)(
|
||||
"omits an unavailable native MCP connection when it is %s without aborting runtime context creation",
|
||||
async (unavailableState) => {
|
||||
|
||||
@@ -21,7 +21,8 @@ import {
|
||||
type NativeRuntimeContextSnapshot,
|
||||
} from "../../vendor/paperclip-runner/index.js";
|
||||
import { resolvePaperclipInstanceRoot } from "../../home-paths.js";
|
||||
import { agentInstructionsService } from "../agent-instructions.js";
|
||||
import { agentInstructionsService, agentInstructionsBundleMode } from "../agent-instructions.js";
|
||||
import { agentInstructionRevisionService } from "../agent-instruction-revisions.js";
|
||||
import { toolAccessService } from "../tool-access.js";
|
||||
import { filterResolvedGitHubConnectionsForRun } from "../git-credentials.js";
|
||||
|
||||
@@ -145,8 +146,24 @@ export async function materializeAsset(files: AssetFile[]): Promise<NativeRuntim
|
||||
return { schema: NATIVE_RUNTIME_ASSET_SCHEMA, digest: assetDigest, manifestDigest, rootPath, fileCount: manifestFiles.length, totalBytes };
|
||||
}
|
||||
|
||||
async function materializeInstructionBundle(agent: RuntimeAgent) {
|
||||
async function materializeInstructionBundle(db: Db, agent: RuntimeAgent, agentFiles = false) {
|
||||
if (agentFiles) {
|
||||
const current = await agentInstructionRevisionService(db).readCommittedForRuntime({ companyId: agent.companyId, agentId: agent.id });
|
||||
if (!current) throw new Error("Configured instruction entry is missing");
|
||||
const entryPath = safeRelativePath(current.revision.entryFile, "instruction entry path");
|
||||
return { entryPath, bundle: await materializeAsset([{ path: entryPath, content: Buffer.from(current.content, "utf8"), mode: 0o444 }]) };
|
||||
}
|
||||
const exported = await agentInstructionsService().exportFiles(agent, { rejectSymlinks: true });
|
||||
if (agentInstructionsBundleMode(agent) === "managed") {
|
||||
// The database head remains authoritative when a saved revision could not
|
||||
// update its disk projection. Overlay the exact committed snapshot directly;
|
||||
// a repair followed by a separate export can race another pending commit.
|
||||
const current = await agentInstructionRevisionService(db).readCommittedForRuntime({ companyId: agent.companyId, agentId: agent.id });
|
||||
if (current) {
|
||||
exported.entryFile = current.revision.entryFile;
|
||||
exported.files[current.revision.entryFile] = current.content;
|
||||
}
|
||||
}
|
||||
const entryPath = safeRelativePath(exported.entryFile, "instruction entry path");
|
||||
if (!(entryPath in exported.files)) throw new Error(`configured instruction entry is missing: ${entryPath}`);
|
||||
const files = Object.entries(exported.files).map(([relativePath, content]) => ({ path: safeRelativePath(relativePath, "instruction path"), content: Buffer.from(content, "utf8"), mode: 0o444 }));
|
||||
@@ -222,12 +239,17 @@ export async function resolveNativeRuntimeMcpSnapshot(input: { db: Db; agent: Pi
|
||||
return { assignmentSetId: `sha256:${assignmentDigest}`, digest: assignmentDigest, bindingId: assignment.connections.length ? `native-mcp:${input.runId}` : null };
|
||||
}
|
||||
|
||||
export async function buildNativeRuntimeContext(input: { db: Db; agent: RuntimeAgent; runId: string; runtimeConfig: Record<string, unknown>; runtimeSkillEntries: PaperclipSkillEntry[] }): Promise<NativeRuntimeContextSnapshot> {
|
||||
export async function buildNativeRuntimeContext(input: { db: Db; agent: RuntimeAgent; runId: string; runtimeConfig: Record<string, unknown>; runtimeSkillEntries: PaperclipSkillEntry[]; instructionWorkingCopy?: { rootPath: string; entryPath: string; kind?: "agent_files" } }): Promise<NativeRuntimeContextSnapshot> {
|
||||
const [instructions, skills, mcp] = await Promise.all([
|
||||
materializeInstructionBundle(input.agent),
|
||||
materializeInstructionBundle(input.db, input.agent, input.instructionWorkingCopy?.kind === "agent_files"),
|
||||
materializeSelectedSkills(input.runtimeConfig, input.runtimeSkillEntries, input.agent.adapterType === "paperclip_runner"),
|
||||
resolveNativeRuntimeMcpSnapshot({ db: input.db, agent: input.agent, runId: input.runId }),
|
||||
]);
|
||||
const snapshot = { prompt: { revision: PAPERCLIP_EXECUTION_PROMPT_REVISION, text: PAPERCLIP_EXECUTION_PROMPT, digest: nativeRuntimePromptDigest() }, instructions, skills, mcp };
|
||||
const snapshot = {
|
||||
prompt: { revision: PAPERCLIP_EXECUTION_PROMPT_REVISION, text: PAPERCLIP_EXECUTION_PROMPT, digest: nativeRuntimePromptDigest() },
|
||||
instructions: { ...instructions, ...(input.instructionWorkingCopy ? { workingCopy: input.instructionWorkingCopy } : {}) },
|
||||
skills,
|
||||
mcp,
|
||||
};
|
||||
return parseNativeRuntimeContext({ ...snapshot, aggregateDigest: canonicalNativeRuntimeContextDigest(snapshot) });
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import {
|
||||
companies,
|
||||
pluginEntities,
|
||||
pluginManagedResources,
|
||||
plugins,
|
||||
activityLog,
|
||||
} from "@paperclipai/db";
|
||||
import type {
|
||||
Agent,
|
||||
@@ -12,11 +14,13 @@ import type {
|
||||
PluginManagedAgentDeclaration,
|
||||
PluginManagedAgentResolution,
|
||||
} from "@paperclipai/shared";
|
||||
import { notFound } from "../errors.js";
|
||||
import { isUuidLike } from "@paperclipai/shared";
|
||||
import { conflict, forbidden, notFound } from "../errors.js";
|
||||
import { agentService } from "./agents.js";
|
||||
import { approvalService } from "./approvals.js";
|
||||
import { logActivity } from "./activity-log.js";
|
||||
import { agentInstructionsBundleMode, agentInstructionsService } from "./agent-instructions.js";
|
||||
import { agentInstructionRevisionService } from "./agent-instruction-revisions.js";
|
||||
|
||||
const MANAGED_AGENT_ENTITY_TYPE = "managed_agent";
|
||||
const DEFAULT_MANAGED_AGENT_ADAPTER_TYPE = "process";
|
||||
@@ -180,7 +184,7 @@ export function pluginManagedAgentService(
|
||||
) {
|
||||
const agentSvc = agentService(db);
|
||||
const approvalSvc = approvalService(db);
|
||||
const instructions = agentInstructionsService();
|
||||
const instructions = agentInstructionsService(db);
|
||||
|
||||
function declarationFor(agentKey: string) {
|
||||
const declaration = options.manifest?.agents?.find((agent) => agent.agentKey === agentKey);
|
||||
@@ -190,8 +194,8 @@ export function pluginManagedAgentService(
|
||||
return declaration;
|
||||
}
|
||||
|
||||
async function getBinding(companyId: string, agentKey: string) {
|
||||
return db
|
||||
async function getBinding(companyId: string, agentKey: string, client: Pick<Db, "select"> = db) {
|
||||
return client
|
||||
.select()
|
||||
.from(pluginEntities)
|
||||
.where(
|
||||
@@ -210,6 +214,7 @@ export function pluginManagedAgentService(
|
||||
agentId: string,
|
||||
extraData: Record<string, unknown> = {},
|
||||
effectiveAdapterType?: string,
|
||||
client: Pick<Db, "select" | "insert" | "update"> = db,
|
||||
) {
|
||||
const adapterType = effectiveAdapterType ?? (await resolveManagedAdapterType(companyId, declaration));
|
||||
const defaultsJson = {
|
||||
@@ -227,7 +232,7 @@ export function pluginManagedAgentService(
|
||||
budgetMonthlyCents: declaration.budgetMonthlyCents ?? 0,
|
||||
instructions: declaration.instructions ?? null,
|
||||
};
|
||||
const managedResource = await db
|
||||
const managedResource = await client
|
||||
.select({ id: pluginManagedResources.id })
|
||||
.from(pluginManagedResources)
|
||||
.where(and(
|
||||
@@ -238,12 +243,12 @@ export function pluginManagedAgentService(
|
||||
))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (managedResource) {
|
||||
await db
|
||||
await client
|
||||
.update(pluginManagedResources)
|
||||
.set({ resourceId: agentId, defaultsJson, updatedAt: new Date() })
|
||||
.where(eq(pluginManagedResources.id, managedResource.id));
|
||||
} else {
|
||||
await db.insert(pluginManagedResources).values({
|
||||
await client.insert(pluginManagedResources).values({
|
||||
companyId,
|
||||
pluginId: options.pluginId,
|
||||
pluginKey: options.pluginKey,
|
||||
@@ -265,9 +270,9 @@ export function pluginManagedAgentService(
|
||||
lastReconciledAt: new Date().toISOString(),
|
||||
...extraData,
|
||||
};
|
||||
const existing = await getBinding(companyId, declaration.agentKey);
|
||||
const existing = await getBinding(companyId, declaration.agentKey, client);
|
||||
if (existing) {
|
||||
return db
|
||||
return client
|
||||
.update(pluginEntities)
|
||||
.set({
|
||||
scopeKind: "company",
|
||||
@@ -281,7 +286,7 @@ export function pluginManagedAgentService(
|
||||
.returning()
|
||||
.then((rows) => rows[0]);
|
||||
}
|
||||
return db
|
||||
return client
|
||||
.insert(pluginEntities)
|
||||
.values({
|
||||
pluginId: options.pluginId,
|
||||
@@ -305,6 +310,46 @@ export function pluginManagedAgentService(
|
||||
return rows.find((row) => rowIsManagedAgent(row, options.pluginKey, declaration.agentKey)) ?? null;
|
||||
}
|
||||
|
||||
async function relinkManagedAgent(companyId: string, declaration: PluginManagedAgentDeclaration, agentId: string) {
|
||||
const adapterType = await resolveManagedAdapterType(companyId, declaration);
|
||||
return db.transaction(async (tx) => {
|
||||
const [agent] = await tx.select().from(agents).where(and(
|
||||
eq(agents.id, agentId), eq(agents.companyId, companyId), ne(agents.status, "terminated"),
|
||||
)).for("update");
|
||||
if (!agent || !rowIsManagedAgent(agent, options.pluginKey, declaration.agentKey)) {
|
||||
throw conflict("Managed agent ownership changed before relink");
|
||||
}
|
||||
const [plugin] = await tx.select().from(plugins).where(and(
|
||||
eq(plugins.id, options.pluginId), eq(plugins.pluginKey, options.pluginKey),
|
||||
)).for("share");
|
||||
if (!plugin || plugin.status !== "ready" || !plugin.manifestJson.capabilities.includes("agents.managed")
|
||||
|| !plugin.manifestJson.agents?.some((entry) => entry.agentKey === declaration.agentKey)) {
|
||||
throw forbidden("Plugin relink is limited to its registered managed agent declaration");
|
||||
}
|
||||
const marker = agent.metadata!.paperclipManagedResource as Record<string, unknown>;
|
||||
const previousPluginId = marker.pluginId;
|
||||
if (typeof previousPluginId !== "string" || !isUuidLike(previousPluginId)) throw forbidden("Managed agent has no plugin owner");
|
||||
if (previousPluginId !== options.pluginId) {
|
||||
const [previousPlugin] = await tx.select({ id: plugins.id }).from(plugins).where(eq(plugins.id, previousPluginId));
|
||||
if (previousPlugin) throw forbidden("Managed agent still belongs to another installed plugin");
|
||||
}
|
||||
// Hard uninstall cascades the bindings, but deliberately leaves the agent
|
||||
// and its canonical history. Transfer only its proven stable-key marker to
|
||||
// the replacement installation, atomically with the new scoped bindings.
|
||||
await upsertBinding(companyId, declaration, agentId, {}, adapterType, tx);
|
||||
const [relinked] = await tx.update(agents).set({
|
||||
metadata: managedMetadata(options.pluginId, options.pluginKey, declaration, agent.metadata),
|
||||
updatedAt: new Date(),
|
||||
}).where(and(eq(agents.id, agentId), eq(agents.companyId, companyId))).returning();
|
||||
await tx.insert(activityLog).values({
|
||||
companyId, actorType: "plugin", actorId: options.pluginId,
|
||||
action: "plugin.managed_agent.relinked", entityType: "agent", entityId: agentId,
|
||||
details: { sourcePluginKey: options.pluginKey, managedResourceKey: declaration.agentKey, previousPluginId },
|
||||
});
|
||||
return relinked as Agent;
|
||||
});
|
||||
}
|
||||
|
||||
async function companyAdapterUsage(companyId: string) {
|
||||
const rows = await db
|
||||
.select({ adapterType: agents.adapterType })
|
||||
@@ -336,23 +381,39 @@ export function pluginManagedAgentService(
|
||||
const declared = declaredInstructionFiles(declaration, variables);
|
||||
if (!declared) return agent;
|
||||
|
||||
const materialized = await instructions.materializeManagedBundle(
|
||||
agent,
|
||||
declared.files,
|
||||
{
|
||||
entryFile: declared.entryFile,
|
||||
replaceExisting: materializeOptions.replaceExisting,
|
||||
clearLegacyPromptTemplate: true,
|
||||
},
|
||||
);
|
||||
let adapterConfig: Record<string, unknown>;
|
||||
if (materializeOptions.replaceExisting) {
|
||||
const revisions = agentInstructionRevisionService(db);
|
||||
const target = { companyId, agentId: agent.id };
|
||||
const actor = { type: "plugin" as const, pluginId: options.pluginId, pluginKey: options.pluginKey, agentKey: declaration.agentKey };
|
||||
const baseline = await revisions.readForPluginReset(target, actor);
|
||||
const receipt = await revisions.commitPluginReset({ ...target, entryFile: declared.entryFile,
|
||||
baseRevisionId: baseline.snapshot?.revision.id ?? null, configuredEntryFile: baseline.configuredEntryFile, content: declared.files[declared.entryFile] ?? "" }, actor);
|
||||
if (receipt.materialization === "pending") throw conflict("Instruction revision saved; retry reset to repair its disk copy", { revisionId: receipt.revision.id });
|
||||
const refreshed = await agentSvc.getById(agent.id);
|
||||
if (!refreshed) throw notFound("Managed agent not found");
|
||||
for (const [file, content] of Object.entries(declared.files)) {
|
||||
if (file !== declared.entryFile) await instructions.writeFile(refreshed, file, content);
|
||||
}
|
||||
// A stock reset owns declared instruction paths, not the agent's other
|
||||
// persistent files (including a formerly configured entry).
|
||||
adapterConfig = { ...refreshed.adapterConfig };
|
||||
delete adapterConfig.promptTemplate;
|
||||
delete adapterConfig.bootstrapPromptTemplate;
|
||||
} else {
|
||||
const materialized = await instructions.materializeManagedBundle(agent, declared.files, {
|
||||
entryFile: declared.entryFile, replaceExisting: false, clearLegacyPromptTemplate: true,
|
||||
});
|
||||
adapterConfig = materialized.adapterConfig;
|
||||
}
|
||||
const updated = await agentSvc.update(agent.id, {
|
||||
adapterConfig: materialized.adapterConfig,
|
||||
adapterConfig,
|
||||
}, {
|
||||
recordRevision: {
|
||||
source: `plugin:${optionsForRevisionSource()}:managed-agent-instructions`,
|
||||
},
|
||||
});
|
||||
return (updated as Agent | null) ?? { ...agent, adapterConfig: materialized.adapterConfig };
|
||||
return (updated as Agent | null) ?? { ...agent, adapterConfig };
|
||||
}
|
||||
|
||||
async function managedInstructionDefaultDrift(
|
||||
@@ -375,8 +436,7 @@ export function pluginManagedAgentService(
|
||||
return { entryFile: declared.entryFile, changedFiles: [declared.entryFile] };
|
||||
}
|
||||
|
||||
const paths = new Set([...Object.keys(declared.files), ...Object.keys(exported.files)]);
|
||||
const changedFiles = [...paths]
|
||||
const changedFiles = Object.keys(declared.files)
|
||||
.filter((filePath) => (exported.files[filePath] ?? null) !== (declared.files[filePath] ?? null))
|
||||
.sort((left, right) => left.localeCompare(right));
|
||||
if (exported.entryFile !== declared.entryFile && !changedFiles.includes(declared.entryFile)) {
|
||||
@@ -433,7 +493,7 @@ export function pluginManagedAgentService(
|
||||
spentMonthlyCents: 0,
|
||||
lastHeartbeatAt: null,
|
||||
}) as Agent;
|
||||
created = await materializeDeclaredInstructions(companyId, created, declaration, { replaceExisting: true });
|
||||
created = await materializeDeclaredInstructions(companyId, created, declaration, { replaceExisting: false });
|
||||
|
||||
let approvalId: string | null = null;
|
||||
if (requiresApproval) {
|
||||
@@ -574,9 +634,7 @@ export function pluginManagedAgentService(
|
||||
|
||||
const relinkCandidate = await findRelinkCandidate(companyId, declaration);
|
||||
if (relinkCandidate) {
|
||||
await upsertBinding(companyId, declaration, relinkCandidate.id);
|
||||
const relinkedAgent = await agentSvc.getById(relinkCandidate.id) as Agent | null;
|
||||
if (!relinkedAgent) throw notFound("Managed agent not found");
|
||||
const relinkedAgent = await relinkManagedAgent(companyId, declaration, relinkCandidate.id);
|
||||
const agent = await backfillManagedPauseReason(
|
||||
companyId,
|
||||
declaration,
|
||||
@@ -596,8 +654,19 @@ export function pluginManagedAgentService(
|
||||
? reconciled.agent.metadata
|
||||
: {};
|
||||
const adapterType = await resolveManagedAdapterType(companyId, declaration);
|
||||
// Reset content through the canonical CAS path before changing defaults.
|
||||
// A conflict must leave the existing adapter configuration untouched.
|
||||
const withInstructions = await materializeDeclaredInstructions(companyId, reconciled.agent, declaration, { replaceExisting: true });
|
||||
const defaults = declarationPatch(declaration, { adapterType });
|
||||
const adapterConfig = { ...defaults.adapterConfig } as Record<string, unknown>;
|
||||
if (declaration.instructions) {
|
||||
for (const key of ["instructionsBundleMode", "instructionsRootPath", "instructionsEntryFile", "instructionsFilePath"]) {
|
||||
if (withInstructions.adapterConfig[key] !== undefined) adapterConfig[key] = withInstructions.adapterConfig[key];
|
||||
}
|
||||
}
|
||||
const updated = await agentSvc.update(reconciled.agent.id, {
|
||||
...declarationPatch(declaration, { adapterType }),
|
||||
...defaults,
|
||||
adapterConfig,
|
||||
metadata: managedMetadata(options.pluginId, options.pluginKey, declaration, currentMetadata),
|
||||
}, {
|
||||
recordRevision: {
|
||||
@@ -605,7 +674,7 @@ export function pluginManagedAgentService(
|
||||
},
|
||||
});
|
||||
if (!updated) throw notFound("Managed agent not found");
|
||||
const updatedAgent = await materializeDeclaredInstructions(companyId, updated as Agent, declaration, { replaceExisting: true });
|
||||
const updatedAgent = updated as Agent;
|
||||
await upsertBinding(companyId, declaration, updatedAgent.id, {}, adapterType);
|
||||
await logActivity(db, {
|
||||
companyId,
|
||||
|
||||
Reference in new issue
Block a user