mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-28 21:46:15 +01:00
Refresh run config before reusing workspaces (#8797)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Agent runs are assembled by the heartbeat service from agent config, project workspaces, environment config, secret bindings, skills, and runtime session state. > - The heartbeat service intentionally reuses adapter sessions, execution workspaces, and sandbox leases when that preserves useful state. > - Reuse becomes incorrect when the effective next-run config changes after a saved session, workspace, or lease was created. > - Stale reuse can make a later run appear pinned to old agent, environment, secret, instruction, or workspace settings. > - This pull request records non-sensitive fingerprints for the effective session, workspace, and lease config at run boundaries. > - When those fingerprints drift, Paperclip refreshes persisted runtime config or starts fresh execution instead of reusing stale state. > - The benefit is predictable next-run config freshness without storing raw secret values, full env maps, provider credentials, or private path details. ## Linked Issues or Issue Description - Refs #8058 - Related PRs checked during dedup search: #4968, #4155, #84, #8480. These cover nearby workspace/session routing or model-config freshness areas, but do not duplicate this effective run config fingerprinting path. ## What Changed - Added effective run config fingerprinting for session, workspace, and lease reuse decisions, with canonicalization that ignores generated runtime noise and redacts sensitive values. - Updated heartbeat reuse logic to compare stored and next-run fingerprints, reset stale saved sessions, refresh persisted workspace config snapshots, replace stale reused workspaces when required, and avoid stale sandbox lease reuse. - Included plain environment value drift via value hashes, without storing the raw env values. - Root-bound instruction content hashing so legacy direct absolute instruction paths are represented but not read for config fingerprints. - Batched secret/version metadata lookups for environment lease fingerprinting. - Added workspace operation/run result freshness metadata so operators can inspect non-sensitive decision categories. - Surfaced config freshness labels and next-run copy in the UI and docs. - Added focused coverage for fingerprint redaction, session reset decisions, workspace refresh/replace behavior, environment lease drift, and persisted workspace restoration. ## Verification - `git diff --check` - Sensitive-data scan before push: - `git diff --unified=0 origin/master...HEAD | rg -n --pcre2 "(AWS_ACCESS_KEY_ID|AWS_SECRET_ACCESS_KEY|ghp_[A-Za-z0-9_]{20,}|github_pat_[A-Za-z0-9_]{20,}|sk-[A-Za-z0-9]{20,}|-----BEGIN (RSA |OPENSSH |EC |DSA )?PRIVATE KEY-----|AKIA[0-9A-Z]{16})"` - `git diff --unified=0 origin/master...HEAD | rg -n --pcre2 "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"` - `pnpm exec vitest run server/src/__tests__/effective-run-config-fingerprints.test.ts server/src/__tests__/heartbeat-workspace-session.test.ts server/src/__tests__/environment-runtime.test.ts` - `pnpm --filter @paperclipai/server typecheck` - `pnpm -r typecheck` - `pnpm --filter @paperclipai/db clean` - `pnpm test:run` - `pnpm build` - UI screenshots from Cutter: - https://artifacts.cutter.sh/8797/run-2f4827c-2026-06-30T18-57-25/preview/change-01.png - https://artifacts.cutter.sh/8797/run-2f4827c-2026-06-30T18-57-25/preview/change-02.png - https://artifacts.cutter.sh/8797/run-2f4827c-2026-06-30T18-57-25/preview/change-03.png ## Risks - Medium: overly broad fingerprints could start fresh sessions, workspaces, or sandbox leases more often than necessary. - Medium: missing a config category would allow stale reuse to persist for that category. - Medium: legacy direct absolute instruction paths are no longer content-hashed unless they are paired with an absolute managed instructions root. - Low data risk: fingerprint metadata stores hashes and category names, not raw secrets, raw env values, provider credentials, or private path details. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used OpenAI GPT-5 via Codex CLI / Codex coding agent, tool-enabled with shell, Git, GitHub CLI, local test execution, and code editing. The exact deployed model variant and context window are not exposed by this environment. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing> Co-authored-by: Cody <cody@paperclip.ing>
This commit is contained in:
15 files changed
+3325
-249
No files matched your search
@@ -0,0 +1,492 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import type { RuntimeSecretManifestEntry } from "./secrets.js";
|
||||
|
||||
export const EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION = 1;
|
||||
export const EFFECTIVE_RUN_CONFIG_FINGERPRINT_ALGORITHM = "sha256";
|
||||
export const EFFECTIVE_RUN_CONFIG_FINGERPRINT_CATEGORIES = ["session", "workspace", "lease"] as const;
|
||||
|
||||
export type EffectiveRunConfigFingerprintCategory =
|
||||
(typeof EFFECTIVE_RUN_CONFIG_FINGERPRINT_CATEGORIES)[number];
|
||||
|
||||
export type EffectiveRunConfigChangedCategory = EffectiveRunConfigFingerprintCategory;
|
||||
|
||||
export type EffectiveRunConfigCanonicalValue =
|
||||
| null
|
||||
| boolean
|
||||
| number
|
||||
| string
|
||||
| EffectiveRunConfigCanonicalValue[]
|
||||
| { [key: string]: EffectiveRunConfigCanonicalValue };
|
||||
|
||||
export interface EffectiveRunConfigSecretVersionMetadata {
|
||||
configPath: string;
|
||||
envKey: string | null;
|
||||
secretId: string;
|
||||
bindingId?: string | null;
|
||||
version: number | string;
|
||||
provider?: string | null;
|
||||
providerVersionRef?: string | null;
|
||||
outcome?: "success" | "failure" | null;
|
||||
}
|
||||
|
||||
export type EffectiveRunConfigSecretManifestEntry =
|
||||
| RuntimeSecretManifestEntry
|
||||
| EffectiveRunConfigSecretVersionMetadata;
|
||||
|
||||
export interface EffectiveRunConfigFingerprintInput {
|
||||
session?: unknown;
|
||||
workspace?: unknown;
|
||||
lease?: unknown;
|
||||
secretManifest?: readonly EffectiveRunConfigSecretManifestEntry[];
|
||||
}
|
||||
|
||||
export interface EffectiveRunConfigFingerprint {
|
||||
version: typeof EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION;
|
||||
category: EffectiveRunConfigFingerprintCategory;
|
||||
algorithm: typeof EFFECTIVE_RUN_CONFIG_FINGERPRINT_ALGORITHM;
|
||||
fingerprint: string;
|
||||
canonicalJson: string;
|
||||
}
|
||||
|
||||
export interface EffectiveRunConfigFingerprints {
|
||||
version: typeof EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION;
|
||||
categories: readonly EffectiveRunConfigFingerprintCategory[];
|
||||
sessionFingerprint: EffectiveRunConfigFingerprint;
|
||||
workspaceFingerprint: EffectiveRunConfigFingerprint;
|
||||
leaseFingerprint: EffectiveRunConfigFingerprint;
|
||||
}
|
||||
|
||||
export interface EffectiveRunConfigFingerprintDiff {
|
||||
version: typeof EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION;
|
||||
hasChanges: boolean;
|
||||
changedCategories: EffectiveRunConfigChangedCategory[];
|
||||
changed: Record<EffectiveRunConfigChangedCategory, boolean>;
|
||||
}
|
||||
|
||||
const OMIT = Symbol("omit-from-effective-run-config-fingerprint");
|
||||
const REDACTED_VALUE: EffectiveRunConfigCanonicalValue = { type: "redacted", present: true };
|
||||
|
||||
const GENERATED_RUNTIME_ENV_KEY_RE = /^PAPERCLIP_/;
|
||||
const SENSITIVE_CONFIG_KEY_RE =
|
||||
/(?:api[_-]?key|access[_-]?token|auth(?:orization)?|bearer|cookie|credential|jwt|password|passwd|private[_-]?key|secret|token)$/i;
|
||||
const VOLATILE_CONFIG_KEYS = new Set([
|
||||
"checkoutRunId",
|
||||
"executionRunId",
|
||||
"externalRunId",
|
||||
"heartbeatRunId",
|
||||
"invocationId",
|
||||
"leaseId",
|
||||
"providerLeaseId",
|
||||
"requestId",
|
||||
"runId",
|
||||
"sessionDisplayId",
|
||||
"sessionId",
|
||||
"spanId",
|
||||
"traceId",
|
||||
]);
|
||||
const HOST_NOISE_KEYS = new Set([
|
||||
"agentHome",
|
||||
"homeDir",
|
||||
"hostCwd",
|
||||
"localHome",
|
||||
"tempDir",
|
||||
"tmpDir",
|
||||
"userHome",
|
||||
]);
|
||||
const SESSION_HOST_PATH_KEYS = new Set([
|
||||
"cwd",
|
||||
"localPath",
|
||||
"remoteCwd",
|
||||
"workspaceCwd",
|
||||
"workspacePath",
|
||||
"workspaceRemoteDir",
|
||||
"worktreePath",
|
||||
]);
|
||||
|
||||
type SecretManifestIndex = {
|
||||
byConfigPath: Map<string, EffectiveRunConfigSecretVersionMetadata>;
|
||||
byEnvKey: Map<string, EffectiveRunConfigSecretVersionMetadata>;
|
||||
};
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value)
|
||||
&& typeof value === "object"
|
||||
&& !Array.isArray(value)
|
||||
&& !(value instanceof Date);
|
||||
}
|
||||
|
||||
function readString(value: unknown) {
|
||||
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
||||
}
|
||||
|
||||
function readVersion(value: unknown) {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
return readString(value);
|
||||
}
|
||||
|
||||
function normalizeSecretManifestEntry(
|
||||
entry: EffectiveRunConfigSecretManifestEntry,
|
||||
): EffectiveRunConfigSecretVersionMetadata | null {
|
||||
const record = entry as Record<string, unknown>;
|
||||
const secretId = readString(record.secretId);
|
||||
const version = readVersion(record.version);
|
||||
if (!secretId || version === null) return null;
|
||||
|
||||
const normalized: EffectiveRunConfigSecretVersionMetadata = {
|
||||
configPath: readString(record.configPath) ?? "",
|
||||
envKey: readString(record.envKey),
|
||||
secretId,
|
||||
version,
|
||||
};
|
||||
const bindingId = readString(record.bindingId);
|
||||
const provider = readString(record.provider);
|
||||
const providerVersionRef = readString(record.providerVersionRef);
|
||||
const outcome = record.outcome === "success" || record.outcome === "failure"
|
||||
? record.outcome
|
||||
: null;
|
||||
if (bindingId !== null) normalized.bindingId = bindingId;
|
||||
if (provider !== null) normalized.provider = provider;
|
||||
if (providerVersionRef !== null) normalized.providerVersionRef = providerVersionRef;
|
||||
if (outcome !== null) normalized.outcome = outcome;
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function buildSecretManifestIndex(
|
||||
manifest: readonly EffectiveRunConfigSecretManifestEntry[] | undefined,
|
||||
): SecretManifestIndex {
|
||||
const byConfigPath = new Map<string, EffectiveRunConfigSecretVersionMetadata>();
|
||||
const byEnvKey = new Map<string, EffectiveRunConfigSecretVersionMetadata>();
|
||||
for (const entry of manifest ?? []) {
|
||||
const normalized = normalizeSecretManifestEntry(entry);
|
||||
if (!normalized) continue;
|
||||
if (normalized.configPath) byConfigPath.set(normalized.configPath, normalized);
|
||||
if (normalized.envKey) byEnvKey.set(normalized.envKey, normalized);
|
||||
}
|
||||
return { byConfigPath, byEnvKey };
|
||||
}
|
||||
|
||||
function canonicalSecretMetadata(
|
||||
metadata: EffectiveRunConfigSecretVersionMetadata,
|
||||
): EffectiveRunConfigCanonicalValue {
|
||||
return omitNullish({
|
||||
type: "secret_ref",
|
||||
configPath: metadata.configPath || undefined,
|
||||
envKey: metadata.envKey ?? undefined,
|
||||
secretId: metadata.secretId,
|
||||
bindingId: metadata.bindingId ?? undefined,
|
||||
version: metadata.version,
|
||||
provider: metadata.provider ?? undefined,
|
||||
providerVersionRef: metadata.providerVersionRef ?? undefined,
|
||||
outcome: metadata.outcome ?? undefined,
|
||||
});
|
||||
}
|
||||
|
||||
function isSecretRefBinding(value: unknown): value is Record<string, unknown> {
|
||||
return isPlainObject(value)
|
||||
&& value.type === "secret_ref"
|
||||
&& typeof value.secretId === "string"
|
||||
&& value.secretId.trim().length > 0;
|
||||
}
|
||||
|
||||
function canonicalSecretRefBinding(
|
||||
value: Record<string, unknown>,
|
||||
configPath: string,
|
||||
): EffectiveRunConfigCanonicalValue {
|
||||
return omitNullish({
|
||||
type: "secret_ref",
|
||||
configPath,
|
||||
secretId: readString(value.secretId),
|
||||
versionSelector: readVersion(value.version) ?? "latest",
|
||||
unresolved: true,
|
||||
});
|
||||
}
|
||||
|
||||
function omitNullish(record: Record<string, unknown>): EffectiveRunConfigCanonicalValue {
|
||||
return Object.fromEntries(
|
||||
Object.entries(record).filter(([, value]) => value !== undefined && value !== null),
|
||||
) as EffectiveRunConfigCanonicalValue;
|
||||
}
|
||||
|
||||
function isTimestampNoiseKey(key: string) {
|
||||
return /(created|updated|started|finished|completed|cancelled|resolved|used|heartbeat)At$/i.test(key)
|
||||
&& !/(revision|version)/i.test(key);
|
||||
}
|
||||
|
||||
function shouldOmitObjectKey(
|
||||
category: EffectiveRunConfigFingerprintCategory,
|
||||
key: string,
|
||||
) {
|
||||
if (VOLATILE_CONFIG_KEYS.has(key)) return true;
|
||||
if (HOST_NOISE_KEYS.has(key)) return true;
|
||||
if (isTimestampNoiseKey(key)) return true;
|
||||
if (category === "session" && SESSION_HOST_PATH_KEYS.has(key)) return true;
|
||||
if (category === "lease" && (key === "remoteCwd" || key === "workspaceRemoteDir")) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function stableStringify(value: EffectiveRunConfigCanonicalValue): string {
|
||||
if (Array.isArray(value)) {
|
||||
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
||||
}
|
||||
if (value && typeof value === "object") {
|
||||
const record = value as Record<string, EffectiveRunConfigCanonicalValue>;
|
||||
return `{${Object.keys(record)
|
||||
.sort()
|
||||
.map((key) => `${JSON.stringify(key)}:${stableStringify(record[key] ?? null)}`)
|
||||
.join(",")}}`;
|
||||
}
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
function canonicalizePlainEnvValueForHash(value: unknown): EffectiveRunConfigCanonicalValue {
|
||||
if (value === undefined || value === null) return null;
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((entry) => canonicalizePlainEnvValueForHash(entry));
|
||||
}
|
||||
if (isPlainObject(value)) {
|
||||
const out: Record<string, EffectiveRunConfigCanonicalValue> = {};
|
||||
for (const key of Object.keys(value).sort()) {
|
||||
const next = canonicalizePlainEnvValueForHash(value[key]);
|
||||
if (next !== null) out[key] = next;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
if (typeof value === "number") {
|
||||
return Number.isFinite(value) ? value : null;
|
||||
}
|
||||
if (typeof value === "bigint") {
|
||||
return value.toString();
|
||||
}
|
||||
if (typeof value === "boolean" || typeof value === "string") {
|
||||
return value;
|
||||
}
|
||||
return String(value);
|
||||
}
|
||||
|
||||
function hashPlainEnvValue(value: unknown): string | null {
|
||||
if (value === undefined || value === null) return null;
|
||||
const canonicalJson = stableStringify(canonicalizePlainEnvValueForHash(value));
|
||||
return `sha256:${createHash("sha256").update(canonicalJson).digest("hex")}`;
|
||||
}
|
||||
|
||||
function canonicalizeEnvRecord(
|
||||
envValue: unknown,
|
||||
context: CanonicalizeContext,
|
||||
): EffectiveRunConfigCanonicalValue | typeof OMIT {
|
||||
if (!isPlainObject(envValue)) return {};
|
||||
const canonicalEnv: Record<string, EffectiveRunConfigCanonicalValue> = {};
|
||||
for (const key of Object.keys(envValue).sort()) {
|
||||
if (GENERATED_RUNTIME_ENV_KEY_RE.test(key)) continue;
|
||||
const manifestEntry = context.secrets.byConfigPath.get(`env.${key}`) ?? context.secrets.byEnvKey.get(key);
|
||||
if (manifestEntry) {
|
||||
canonicalEnv[key] = canonicalSecretMetadata(manifestEntry);
|
||||
continue;
|
||||
}
|
||||
const rawBinding = envValue[key];
|
||||
if (isSecretRefBinding(rawBinding)) {
|
||||
canonicalEnv[key] = canonicalSecretRefBinding(rawBinding, `env.${key}`);
|
||||
continue;
|
||||
}
|
||||
canonicalEnv[key] = omitNullish({
|
||||
type: "plain_env",
|
||||
present: rawBinding !== undefined && rawBinding !== null,
|
||||
valueHash: hashPlainEnvValue(rawBinding),
|
||||
});
|
||||
}
|
||||
return Object.keys(canonicalEnv).length > 0 ? canonicalEnv : OMIT;
|
||||
}
|
||||
|
||||
type CanonicalizeContext = {
|
||||
category: EffectiveRunConfigFingerprintCategory;
|
||||
path: string[];
|
||||
secrets: SecretManifestIndex;
|
||||
};
|
||||
|
||||
function canonicalizeValue(
|
||||
value: unknown,
|
||||
context: CanonicalizeContext,
|
||||
): EffectiveRunConfigCanonicalValue | typeof OMIT {
|
||||
if (value === undefined || value instanceof Date) return OMIT;
|
||||
if (value === null) return null;
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((entry, index) => {
|
||||
const next = canonicalizeValue(entry, { ...context, path: [...context.path, String(index)] });
|
||||
return next === OMIT ? null : next;
|
||||
});
|
||||
}
|
||||
if (isSecretRefBinding(value)) {
|
||||
return canonicalSecretRefBinding(value, context.path.join("."));
|
||||
}
|
||||
if (isPlainObject(value)) {
|
||||
const canonicalObject: Record<string, EffectiveRunConfigCanonicalValue> = {};
|
||||
for (const key of Object.keys(value).sort()) {
|
||||
if (key === "env") {
|
||||
const env = canonicalizeEnvRecord(value[key], { ...context, path: [...context.path, key] });
|
||||
if (env !== OMIT) canonicalObject[key] = env;
|
||||
continue;
|
||||
}
|
||||
if (shouldOmitObjectKey(context.category, key)) continue;
|
||||
if (SENSITIVE_CONFIG_KEY_RE.test(key)) {
|
||||
canonicalObject[key] = REDACTED_VALUE;
|
||||
continue;
|
||||
}
|
||||
const next = canonicalizeValue(value[key], { ...context, path: [...context.path, key] });
|
||||
if (next !== OMIT) canonicalObject[key] = next;
|
||||
}
|
||||
return Object.keys(canonicalObject).length > 0 ? canonicalObject : {};
|
||||
}
|
||||
if (typeof value === "number") {
|
||||
return Number.isFinite(value) ? value : null;
|
||||
}
|
||||
if (typeof value === "bigint") {
|
||||
return value.toString();
|
||||
}
|
||||
if (typeof value === "boolean" || typeof value === "string") {
|
||||
return value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function canonicalizeEffectiveRunConfigCategory(input: {
|
||||
category: EffectiveRunConfigFingerprintCategory;
|
||||
value: unknown;
|
||||
secretManifest?: readonly EffectiveRunConfigSecretManifestEntry[];
|
||||
}): EffectiveRunConfigCanonicalValue {
|
||||
const canonical = canonicalizeValue(input.value ?? {}, {
|
||||
category: input.category,
|
||||
path: [],
|
||||
secrets: buildSecretManifestIndex(input.secretManifest),
|
||||
});
|
||||
return canonical === OMIT ? {} : canonical;
|
||||
}
|
||||
|
||||
function createCategoryFingerprint(input: {
|
||||
category: EffectiveRunConfigFingerprintCategory;
|
||||
value: unknown;
|
||||
secretManifest?: readonly EffectiveRunConfigSecretManifestEntry[];
|
||||
}): EffectiveRunConfigFingerprint {
|
||||
const canonicalValue = canonicalizeEffectiveRunConfigCategory(input);
|
||||
const canonicalJson = stableStringify({
|
||||
version: EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION,
|
||||
category: input.category,
|
||||
value: canonicalValue,
|
||||
});
|
||||
const digest = createHash("sha256").update(canonicalJson).digest("hex");
|
||||
return {
|
||||
version: EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION,
|
||||
category: input.category,
|
||||
algorithm: EFFECTIVE_RUN_CONFIG_FINGERPRINT_ALGORITHM,
|
||||
fingerprint: `v${EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION}:sha256:${digest}`,
|
||||
canonicalJson,
|
||||
};
|
||||
}
|
||||
|
||||
function createCategoryFingerprintFromCanonicalValue(input: {
|
||||
category: EffectiveRunConfigFingerprintCategory;
|
||||
value: EffectiveRunConfigCanonicalValue;
|
||||
}): EffectiveRunConfigFingerprint {
|
||||
const canonicalJson = stableStringify({
|
||||
version: EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION,
|
||||
category: input.category,
|
||||
value: input.value,
|
||||
});
|
||||
const digest = createHash("sha256").update(canonicalJson).digest("hex");
|
||||
return {
|
||||
version: EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION,
|
||||
category: input.category,
|
||||
algorithm: EFFECTIVE_RUN_CONFIG_FINGERPRINT_ALGORITHM,
|
||||
fingerprint: `v${EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION}:sha256:${digest}`,
|
||||
canonicalJson,
|
||||
};
|
||||
}
|
||||
|
||||
function canonicalRecord(value: EffectiveRunConfigCanonicalValue) {
|
||||
return value && typeof value === "object" && !Array.isArray(value)
|
||||
? value as Record<string, EffectiveRunConfigCanonicalValue>
|
||||
: {};
|
||||
}
|
||||
|
||||
export function createEffectiveRunConfigSubcategoryFingerprints<T extends string>(input: {
|
||||
category: EffectiveRunConfigFingerprintCategory;
|
||||
value: Record<T, unknown>;
|
||||
subcategories: readonly T[];
|
||||
secretManifest?: readonly EffectiveRunConfigSecretManifestEntry[];
|
||||
}): Record<T, string> {
|
||||
const canonicalValue = canonicalizeEffectiveRunConfigCategory({
|
||||
category: input.category,
|
||||
value: input.value,
|
||||
secretManifest: input.secretManifest,
|
||||
});
|
||||
const record = canonicalRecord(canonicalValue);
|
||||
return Object.fromEntries(
|
||||
input.subcategories.map((subcategory) => {
|
||||
const value = Object.prototype.hasOwnProperty.call(record, subcategory)
|
||||
? { [subcategory]: record[subcategory] ?? null }
|
||||
: {};
|
||||
return [
|
||||
subcategory,
|
||||
createCategoryFingerprintFromCanonicalValue({
|
||||
category: input.category,
|
||||
value,
|
||||
}).fingerprint,
|
||||
];
|
||||
}),
|
||||
) as Record<T, string>;
|
||||
}
|
||||
|
||||
export function createEffectiveRunConfigFingerprints(
|
||||
input: EffectiveRunConfigFingerprintInput,
|
||||
): EffectiveRunConfigFingerprints {
|
||||
return {
|
||||
version: EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION,
|
||||
categories: EFFECTIVE_RUN_CONFIG_FINGERPRINT_CATEGORIES,
|
||||
sessionFingerprint: createCategoryFingerprint({
|
||||
category: "session",
|
||||
value: input.session,
|
||||
secretManifest: input.secretManifest,
|
||||
}),
|
||||
workspaceFingerprint: createCategoryFingerprint({
|
||||
category: "workspace",
|
||||
value: input.workspace,
|
||||
secretManifest: input.secretManifest,
|
||||
}),
|
||||
leaseFingerprint: createCategoryFingerprint({
|
||||
category: "lease",
|
||||
value: input.lease,
|
||||
secretManifest: input.secretManifest,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
function fingerprintForCategory(
|
||||
fingerprints: EffectiveRunConfigFingerprints,
|
||||
category: EffectiveRunConfigFingerprintCategory,
|
||||
) {
|
||||
switch (category) {
|
||||
case "session":
|
||||
return fingerprints.sessionFingerprint.fingerprint;
|
||||
case "workspace":
|
||||
return fingerprints.workspaceFingerprint.fingerprint;
|
||||
case "lease":
|
||||
return fingerprints.leaseFingerprint.fingerprint;
|
||||
}
|
||||
}
|
||||
|
||||
export function diffEffectiveRunConfigFingerprints(
|
||||
previous: EffectiveRunConfigFingerprints,
|
||||
next: EffectiveRunConfigFingerprints,
|
||||
): EffectiveRunConfigFingerprintDiff {
|
||||
const changed = Object.fromEntries(
|
||||
EFFECTIVE_RUN_CONFIG_FINGERPRINT_CATEGORIES.map((category) => [
|
||||
category,
|
||||
fingerprintForCategory(previous, category) !== fingerprintForCategory(next, category),
|
||||
]),
|
||||
) as Record<EffectiveRunConfigChangedCategory, boolean>;
|
||||
const changedCategories = EFFECTIVE_RUN_CONFIG_FINGERPRINT_CATEGORIES.filter((category) => changed[category]);
|
||||
return {
|
||||
version: EFFECTIVE_RUN_CONFIG_FINGERPRINT_VERSION,
|
||||
hasChanges: changedCategories.length > 0,
|
||||
changedCategories,
|
||||
changed,
|
||||
};
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { environmentLeases } from "@paperclipai/db";
|
||||
import { companySecrets, companySecretVersions, environmentLeases } from "@paperclipai/db";
|
||||
import type {
|
||||
Environment,
|
||||
EnvironmentLease,
|
||||
@@ -18,10 +18,16 @@ import type {
|
||||
import { ensureSshWorkspaceReady } from "@paperclipai/adapter-utils/ssh";
|
||||
import { environmentService } from "./environments.js";
|
||||
import {
|
||||
collectEnvironmentSecretRefs,
|
||||
parseEnvironmentDriverConfig,
|
||||
resolveEnvironmentDriverConfigForRuntime,
|
||||
stripSandboxProviderEnvelope,
|
||||
} from "./environment-config.js";
|
||||
import {
|
||||
createEffectiveRunConfigFingerprints,
|
||||
type EffectiveRunConfigFingerprint,
|
||||
type EffectiveRunConfigSecretVersionMetadata,
|
||||
} from "./effective-run-config-fingerprints.js";
|
||||
import {
|
||||
acquireSandboxProviderLease,
|
||||
destroySandboxProviderLease,
|
||||
@@ -255,6 +261,146 @@ function reusableRuntimeFingerprint(input: {
|
||||
.digest("hex");
|
||||
}
|
||||
|
||||
function serializeLeaseFingerprint(
|
||||
fingerprint: EffectiveRunConfigFingerprint | null | undefined,
|
||||
): Record<string, unknown> | null {
|
||||
if (!fingerprint) return null;
|
||||
return {
|
||||
version: fingerprint.version,
|
||||
category: fingerprint.category,
|
||||
algorithm: fingerprint.algorithm,
|
||||
fingerprint: fingerprint.fingerprint,
|
||||
};
|
||||
}
|
||||
|
||||
function readLeaseFingerprint(value: unknown): string | null {
|
||||
return isRecord(value) ? readString(value.fingerprint) : null;
|
||||
}
|
||||
|
||||
async function buildEnvironmentSecretMetadataForLeaseFingerprint(input: {
|
||||
db: Db;
|
||||
companyId: string;
|
||||
environment: Environment;
|
||||
}): Promise<EffectiveRunConfigSecretVersionMetadata[]> {
|
||||
const refs = await collectEnvironmentSecretRefs({
|
||||
db: input.db,
|
||||
environment: input.environment,
|
||||
});
|
||||
if (refs.length === 0) return [];
|
||||
|
||||
const secretIds = [...new Set(refs.map((ref) => ref.secretId))];
|
||||
const secretRows = await input.db
|
||||
.select()
|
||||
.from(companySecrets)
|
||||
.where(inArray(companySecrets.id, secretIds));
|
||||
const secretsById = new Map(
|
||||
secretRows
|
||||
.filter((secret) => secret.companyId === input.companyId)
|
||||
.map((secret) => [secret.id, secret]),
|
||||
);
|
||||
|
||||
const versionRequests = refs.flatMap((ref) => {
|
||||
const secret = secretsById.get(ref.secretId);
|
||||
if (!secret) return [];
|
||||
const resolvedVersion = ref.versionSelector === "latest" || ref.versionSelector === undefined
|
||||
? secret.latestVersion
|
||||
: ref.versionSelector;
|
||||
return typeof resolvedVersion === "number"
|
||||
? [{ secretId: secret.id, version: resolvedVersion }]
|
||||
: [];
|
||||
});
|
||||
const versionSecretIds = [...new Set(versionRequests.map((request) => request.secretId))];
|
||||
const versions = [...new Set(versionRequests.map((request) => request.version))];
|
||||
const versionRows = versionSecretIds.length > 0 && versions.length > 0
|
||||
? await input.db
|
||||
.select()
|
||||
.from(companySecretVersions)
|
||||
.where(
|
||||
and(
|
||||
inArray(companySecretVersions.secretId, versionSecretIds),
|
||||
inArray(companySecretVersions.version, versions),
|
||||
),
|
||||
)
|
||||
: [];
|
||||
const versionsBySecretAndNumber = new Map(
|
||||
versionRows.map((row) => [`${row.secretId}:${row.version}`, row]),
|
||||
);
|
||||
|
||||
const metadata: EffectiveRunConfigSecretVersionMetadata[] = [];
|
||||
for (const ref of refs) {
|
||||
const secret = secretsById.get(ref.secretId);
|
||||
if (!secret) {
|
||||
metadata.push({
|
||||
configPath: ref.configPath,
|
||||
envKey: null,
|
||||
secretId: ref.secretId,
|
||||
version: typeof ref.versionSelector === "number" ? ref.versionSelector : "unresolved",
|
||||
outcome: "failure",
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const resolvedVersion = ref.versionSelector === "latest" || ref.versionSelector === undefined
|
||||
? secret.latestVersion
|
||||
: ref.versionSelector;
|
||||
const versionRow = typeof resolvedVersion === "number"
|
||||
? versionsBySecretAndNumber.get(`${secret.id}:${resolvedVersion}`) ?? null
|
||||
: null;
|
||||
|
||||
metadata.push({
|
||||
configPath: ref.configPath,
|
||||
envKey: null,
|
||||
secretId: secret.id,
|
||||
version: resolvedVersion,
|
||||
provider: secret.provider,
|
||||
providerVersionRef: versionRow?.providerVersionRef ?? null,
|
||||
outcome: versionRow ? "success" : "failure",
|
||||
});
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
async function buildReusableSandboxLeaseFingerprint(input: {
|
||||
db: Db;
|
||||
companyId: string;
|
||||
environment: Environment;
|
||||
executionWorkspaceId: string | null;
|
||||
agentId: string | null;
|
||||
adapterType: string | null;
|
||||
provider: string;
|
||||
providerConfig: Record<string, unknown>;
|
||||
providerPlugin?: {
|
||||
id: string;
|
||||
pluginKey: string;
|
||||
packageName: string;
|
||||
version: string;
|
||||
} | null;
|
||||
}): Promise<EffectiveRunConfigFingerprint> {
|
||||
const secretMetadata = await buildEnvironmentSecretMetadataForLeaseFingerprint({
|
||||
db: input.db,
|
||||
companyId: input.companyId,
|
||||
environment: input.environment,
|
||||
});
|
||||
return createEffectiveRunConfigFingerprints({
|
||||
lease: {
|
||||
companyId: input.companyId,
|
||||
environment: {
|
||||
id: input.environment.id,
|
||||
driver: input.environment.driver,
|
||||
},
|
||||
executionWorkspaceId: input.executionWorkspaceId,
|
||||
agentId: input.agentId,
|
||||
adapterType: input.adapterType,
|
||||
provider: input.provider,
|
||||
providerPlugin: input.providerPlugin ?? null,
|
||||
providerConfig: input.providerConfig,
|
||||
secrets: secretMetadata,
|
||||
},
|
||||
secretManifest: secretMetadata,
|
||||
}).leaseFingerprint;
|
||||
}
|
||||
|
||||
function buildReusableSandboxLeaseScope(input: {
|
||||
companyId: string;
|
||||
environmentId: string;
|
||||
@@ -263,6 +409,7 @@ function buildReusableSandboxLeaseScope(input: {
|
||||
adapterType: string | null;
|
||||
provider: string;
|
||||
config: Record<string, unknown>;
|
||||
leaseFingerprint?: EffectiveRunConfigFingerprint | null;
|
||||
providerMetadata?: Record<string, unknown> | null;
|
||||
}): Record<string, unknown> | null {
|
||||
if (!input.executionWorkspaceId || !input.agentId) return null;
|
||||
@@ -285,6 +432,9 @@ function buildReusableSandboxLeaseScope(input: {
|
||||
adapterType,
|
||||
config: input.config,
|
||||
}),
|
||||
...(input.leaseFingerprint
|
||||
? { leaseFingerprint: serializeLeaseFingerprint(input.leaseFingerprint) }
|
||||
: {}),
|
||||
...(remoteCwd ? { remoteCwd } : {}),
|
||||
...(workspaceSentinel ? { workspaceSentinel } : {}),
|
||||
};
|
||||
@@ -299,24 +449,48 @@ function reusableSandboxLeaseScopeMatches(input: {
|
||||
adapterType: string | null;
|
||||
provider: string;
|
||||
config: Record<string, unknown>;
|
||||
leaseFingerprint?: EffectiveRunConfigFingerprint | null;
|
||||
allowLegacyRuntimeFingerprint?: boolean;
|
||||
}): boolean {
|
||||
if (!input.executionWorkspaceId || !input.agentId) return false;
|
||||
const scope = input.lease.metadata?.reusableSandboxLease;
|
||||
if (!isRecord(scope)) return false;
|
||||
const adapterType = input.adapterType ?? null;
|
||||
return (
|
||||
const baseScopeMatches =
|
||||
scope.companyId === input.companyId &&
|
||||
scope.environmentId === input.environmentId &&
|
||||
scope.executionWorkspaceId === input.executionWorkspaceId &&
|
||||
scope.agentId === input.agentId &&
|
||||
scope.adapterType === adapterType &&
|
||||
scope.provider === input.provider &&
|
||||
scope.runtimeFingerprint === reusableRuntimeFingerprint({
|
||||
provider: input.provider,
|
||||
adapterType,
|
||||
config: input.config,
|
||||
})
|
||||
);
|
||||
scope.provider === input.provider;
|
||||
if (!baseScopeMatches) return false;
|
||||
|
||||
const expectedLeaseFingerprint = input.leaseFingerprint?.fingerprint ?? null;
|
||||
if (expectedLeaseFingerprint) {
|
||||
const storedLeaseFingerprint = readLeaseFingerprint(scope.leaseFingerprint);
|
||||
if (storedLeaseFingerprint) {
|
||||
return storedLeaseFingerprint === expectedLeaseFingerprint;
|
||||
}
|
||||
if (!input.allowLegacyRuntimeFingerprint) return false;
|
||||
}
|
||||
|
||||
return scope.runtimeFingerprint === reusableRuntimeFingerprint({
|
||||
provider: input.provider,
|
||||
adapterType,
|
||||
config: input.config,
|
||||
});
|
||||
}
|
||||
|
||||
function reusableLeaseCanBeResumed(input: {
|
||||
lease: Pick<EnvironmentLease, "status" | "heartbeatRunId">;
|
||||
heartbeatRunId: string | null;
|
||||
}): boolean {
|
||||
if (input.lease.status === "released" || input.lease.status === "retained") return true;
|
||||
return input.lease.status === "active" && input.heartbeatRunId !== null && input.lease.heartbeatRunId === input.heartbeatRunId;
|
||||
}
|
||||
|
||||
function reusableLeaseCanBeCleanedUp(lease: Pick<EnvironmentLease, "status">): boolean {
|
||||
return lease.status === "released" || lease.status === "retained";
|
||||
}
|
||||
|
||||
export function findReusableSandboxLeaseId(input: {
|
||||
@@ -531,6 +705,23 @@ function createSandboxEnvironmentDriver(
|
||||
};
|
||||
}
|
||||
|
||||
async function cleanupObsoleteReusableSandboxLeases(input: {
|
||||
environment: Environment;
|
||||
leases: EnvironmentLease[];
|
||||
reusableLeases: EnvironmentLease[];
|
||||
}) {
|
||||
const reusableIds = new Set(input.reusableLeases.map((lease) => lease.id));
|
||||
for (const lease of input.leases) {
|
||||
if (reusableIds.has(lease.id)) continue;
|
||||
if (!reusableLeaseCanBeCleanedUp(lease)) continue;
|
||||
await destroyReusableSandboxLease({
|
||||
environment: input.environment,
|
||||
lease,
|
||||
failureReason: "lease_fingerprint_mismatch",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
driver: "sandbox",
|
||||
|
||||
@@ -573,14 +764,38 @@ function createSandboxEnvironmentDriver(
|
||||
|
||||
const workerConfig = stripSandboxProviderEnvelope(parsed.config);
|
||||
const storedConfig = storedParsed.config;
|
||||
const providerConfigForLease = sandboxConfigForLeaseMetadata(storedConfig);
|
||||
const supportsReusableLeases = pluginProvider.resolved.driver.supportsReusableLeases === true;
|
||||
const leaseFingerprint =
|
||||
supportsReusableLeases &&
|
||||
parsed.config.reuseLease &&
|
||||
input.heartbeatRunId !== null &&
|
||||
input.executionWorkspaceId !== null &&
|
||||
input.agentId !== null
|
||||
? await buildReusableSandboxLeaseFingerprint({
|
||||
db,
|
||||
companyId: input.companyId,
|
||||
environment: input.environment,
|
||||
executionWorkspaceId: input.executionWorkspaceId,
|
||||
agentId: input.agentId,
|
||||
adapterType: input.adapterType,
|
||||
provider: parsed.config.provider,
|
||||
providerConfig: providerConfigForLease,
|
||||
providerPlugin: {
|
||||
id: pluginProvider.resolved.plugin.id,
|
||||
pluginKey: pluginProvider.resolved.plugin.pluginKey,
|
||||
packageName: pluginProvider.resolved.plugin.packageName,
|
||||
version: pluginProvider.resolved.plugin.version,
|
||||
},
|
||||
})
|
||||
: null;
|
||||
// Ad-hoc tests (heartbeatRunId === null) must never resume an existing
|
||||
// provider lease. If they did, releasing the test lease at the end of
|
||||
// the probe would tear down the live heartbeat run that owns it.
|
||||
// We also filter out leases whose policy is not reuse_by_environment
|
||||
// and whose status is not reusable so non-reusable, cleanup-pending,
|
||||
// or terminal rows cannot be matched.
|
||||
const reusableExistingLeases =
|
||||
const reusableCandidateLeases =
|
||||
supportsReusableLeases &&
|
||||
parsed.config.reuseLease &&
|
||||
input.heartbeatRunId !== null &&
|
||||
@@ -589,21 +804,35 @@ function createSandboxEnvironmentDriver(
|
||||
? (await environmentsSvc.listLeases(input.environment.id))
|
||||
.filter((lease) =>
|
||||
lease.leasePolicy === "reuse_by_environment" &&
|
||||
["active", "released", "retained"].includes(lease.status) &&
|
||||
reusableLeaseCanBeResumed({ lease, heartbeatRunId: input.heartbeatRunId }) &&
|
||||
lease.executionWorkspaceId === input.executionWorkspaceId &&
|
||||
lease.metadata?.agentId === input.agentId &&
|
||||
reusableSandboxLeaseScopeMatches({
|
||||
lease,
|
||||
companyId: input.companyId,
|
||||
environmentId: input.environment.id,
|
||||
executionWorkspaceId: input.executionWorkspaceId,
|
||||
agentId: input.agentId,
|
||||
adapterType: input.adapterType,
|
||||
provider: parsed.config.provider,
|
||||
config: sandboxConfigForLeaseMetadata(storedConfig),
|
||||
}),
|
||||
lease.metadata?.agentId === input.agentId,
|
||||
)
|
||||
: [];
|
||||
const reusableExistingLeases = reusableCandidateLeases.filter((lease) =>
|
||||
reusableSandboxLeaseScopeMatches({
|
||||
lease,
|
||||
companyId: input.companyId,
|
||||
environmentId: input.environment.id,
|
||||
executionWorkspaceId: input.executionWorkspaceId,
|
||||
agentId: input.agentId,
|
||||
adapterType: input.adapterType,
|
||||
provider: parsed.config.provider,
|
||||
config: providerConfigForLease,
|
||||
leaseFingerprint,
|
||||
allowLegacyRuntimeFingerprint:
|
||||
lease.status === "active" &&
|
||||
input.heartbeatRunId !== null &&
|
||||
lease.heartbeatRunId === input.heartbeatRunId,
|
||||
}),
|
||||
);
|
||||
if (reusableCandidateLeases.length > reusableExistingLeases.length) {
|
||||
await cleanupObsoleteReusableSandboxLeases({
|
||||
environment: input.environment,
|
||||
leases: reusableCandidateLeases,
|
||||
reusableLeases: reusableExistingLeases,
|
||||
});
|
||||
}
|
||||
const reusableProviderLeaseId =
|
||||
supportsReusableLeases &&
|
||||
parsed.config.reuseLease &&
|
||||
@@ -694,7 +923,8 @@ function createSandboxEnvironmentDriver(
|
||||
agentId: input.agentId,
|
||||
adapterType: input.adapterType,
|
||||
provider: parsed.config.provider,
|
||||
config: sandboxConfigForLeaseMetadata(storedConfig),
|
||||
config: providerConfigForLease,
|
||||
leaseFingerprint,
|
||||
providerMetadata: sanitizedProviderMetadata,
|
||||
})
|
||||
: null;
|
||||
@@ -730,38 +960,72 @@ function createSandboxEnvironmentDriver(
|
||||
// so non-reusable, cleanup-pending, or terminal rows can never be matched.
|
||||
const builtinSandboxProvider = getBuiltinSandboxProvider(parsed.config.provider);
|
||||
const supportsReusableLeases = builtinSandboxProvider?.supportsReusableLeases === true;
|
||||
const providerConfigForLease = sandboxConfigForLeaseMetadata(parsed.config);
|
||||
const leaseFingerprint =
|
||||
supportsReusableLeases &&
|
||||
parsed.config.reuseLease &&
|
||||
input.heartbeatRunId !== null &&
|
||||
input.executionWorkspaceId !== null &&
|
||||
input.agentId !== null
|
||||
? await buildReusableSandboxLeaseFingerprint({
|
||||
db,
|
||||
companyId: input.companyId,
|
||||
environment: input.environment,
|
||||
executionWorkspaceId: input.executionWorkspaceId,
|
||||
agentId: input.agentId,
|
||||
adapterType: input.adapterType,
|
||||
provider: parsed.config.provider,
|
||||
providerConfig: providerConfigForLease,
|
||||
})
|
||||
: null;
|
||||
const reusableCandidateLeases =
|
||||
supportsReusableLeases &&
|
||||
parsed.config.reuseLease &&
|
||||
input.heartbeatRunId !== null &&
|
||||
input.executionWorkspaceId !== null &&
|
||||
input.agentId !== null
|
||||
? (await environmentsSvc.listLeases(input.environment.id))
|
||||
.filter((lease) =>
|
||||
lease.leasePolicy === "reuse_by_environment" &&
|
||||
reusableLeaseCanBeResumed({ lease, heartbeatRunId: input.heartbeatRunId }) &&
|
||||
lease.executionWorkspaceId === input.executionWorkspaceId &&
|
||||
lease.metadata?.agentId === input.agentId,
|
||||
)
|
||||
: [];
|
||||
const reusableExistingLeases = reusableCandidateLeases.filter((lease) =>
|
||||
reusableSandboxLeaseScopeMatches({
|
||||
lease,
|
||||
companyId: input.companyId,
|
||||
environmentId: input.environment.id,
|
||||
executionWorkspaceId: input.executionWorkspaceId,
|
||||
agentId: input.agentId,
|
||||
adapterType: input.adapterType,
|
||||
provider: parsed.config.provider,
|
||||
config: providerConfigForLease,
|
||||
leaseFingerprint,
|
||||
allowLegacyRuntimeFingerprint:
|
||||
lease.status === "active" &&
|
||||
input.heartbeatRunId !== null &&
|
||||
lease.heartbeatRunId === input.heartbeatRunId,
|
||||
}),
|
||||
);
|
||||
if (reusableCandidateLeases.length > reusableExistingLeases.length) {
|
||||
await cleanupObsoleteReusableSandboxLeases({
|
||||
environment: input.environment,
|
||||
leases: reusableCandidateLeases,
|
||||
reusableLeases: reusableExistingLeases,
|
||||
});
|
||||
}
|
||||
const reusableProviderLeaseId =
|
||||
supportsReusableLeases &&
|
||||
parsed.config.reuseLease &&
|
||||
input.heartbeatRunId !== null &&
|
||||
input.executionWorkspaceId !== null &&
|
||||
input.agentId !== null
|
||||
? (await environmentsSvc
|
||||
.listLeases(input.environment.id)
|
||||
.then((leases) =>
|
||||
findReusableSandboxLeaseId({
|
||||
config: parsed.config,
|
||||
leases: leases.filter((lease) =>
|
||||
lease.leasePolicy === "reuse_by_environment" &&
|
||||
["active", "released", "retained"].includes(lease.status) &&
|
||||
lease.executionWorkspaceId === input.executionWorkspaceId &&
|
||||
lease.metadata?.agentId === input.agentId &&
|
||||
reusableSandboxLeaseScopeMatches({
|
||||
lease,
|
||||
companyId: input.companyId,
|
||||
environmentId: input.environment.id,
|
||||
executionWorkspaceId: input.executionWorkspaceId,
|
||||
agentId: input.agentId,
|
||||
adapterType: input.adapterType,
|
||||
provider: parsed.config.provider,
|
||||
config: sandboxConfigForLeaseMetadata(parsed.config),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
))
|
||||
? findReusableSandboxLeaseId({ config: parsed.config, leases: reusableExistingLeases })
|
||||
: null;
|
||||
const reusableLease = reusableProviderLeaseId
|
||||
? (await environmentsSvc.listLeases(input.environment.id)).find((lease) => lease.providerLeaseId === reusableProviderLeaseId)
|
||||
? reusableExistingLeases.find((lease) => lease.providerLeaseId === reusableProviderLeaseId)
|
||||
: null;
|
||||
|
||||
let providerLease;
|
||||
@@ -806,7 +1070,8 @@ function createSandboxEnvironmentDriver(
|
||||
agentId: input.agentId,
|
||||
adapterType: input.adapterType,
|
||||
provider: parsed.config.provider,
|
||||
config: sandboxConfigForLeaseMetadata(parsed.config),
|
||||
config: providerConfigForLease,
|
||||
leaseFingerprint,
|
||||
providerMetadata: providerLease.metadata,
|
||||
})
|
||||
: null;
|
||||
|
||||
+1117
-157
File diff suppressed because it is too large.
Load diff
@@ -244,6 +244,7 @@ export type RuntimeSecretManifestEntry = {
|
||||
secretKey: string;
|
||||
version: number;
|
||||
provider: SecretProvider;
|
||||
providerVersionRef?: string | null;
|
||||
outcome: "success" | "failure";
|
||||
errorCode?: string | null;
|
||||
};
|
||||
@@ -696,6 +697,7 @@ export function secretService(db: Db) {
|
||||
secretKey: secret.key,
|
||||
version: resolvedVersion,
|
||||
provider: providerId,
|
||||
providerVersionRef: versionRow.providerVersionRef,
|
||||
outcome: "success",
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1471,6 +1471,9 @@ export async function ensurePersistedExecutionWorkspaceAvailable(input: {
|
||||
const provisionCommand = asString(input.workspace.config?.provisionCommand, "").trim();
|
||||
|
||||
if (strategy !== "git_worktree") {
|
||||
if (!await directoryExists(cwd)) {
|
||||
return null;
|
||||
}
|
||||
return realized;
|
||||
}
|
||||
const repoRoot = await runGit(["rev-parse", "--show-toplevel"], input.base.baseCwd);
|
||||
|
||||
Reference in new issue
Block a user