Files
PaperClipAI/packages/adapter-utils/src/directory-merge-lock-diagnostics.ts
T
Devin FoleyandClaude Opus 5.5 33269e1fec fix(observability): report directory-merge lock holder diagnostics (#17755)
A directory-merge lock timeout (ERR_WORKSPACE_RESTORE_LOCK_TIMEOUT) already
carries allowlisted holder evidence, but server Sentry capture dropped it.

- Add directoryMergeLockTimeoutDiagnosticsFor in adapter-utils: closed
  labels, booleans and bounded durations; no owner PID or paths.
- server captureException: lock timeouts get an event-local
  directory_merge_lock context and error_code=workspace_restore_lock_timeout
  tag; default grouping and message unchanged.
- run-failure-diagnostics restoreLock* fields use the shared reader
  (same allowlist and order).
- Document the record in doc/workspace-restore-locks.md.

Diagnostics only; does not change lock acquisition, timeouts or retries.

Refs PAPERCLIP-BACKEND-4Q

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:48:46 -08:00

65 lines
2.7 KiB
TypeScript

import { WORKSPACE_RESTORE_LOCK_TIMEOUT_CODE, type DirectoryMergeLockOperation } from "./workspace-restore-merge.js";
const OPERATIONS = [
"agent_directory_prepare",
"agent_directory_release",
"agent_directory_collect",
"agent_directory_checkpoint",
"agent_directory_handoff",
] as const satisfies readonly DirectoryMergeLockOperation[];
// Fails to compile when the lock module adds an operation this list omits.
const OPERATIONS_COMPLETE: [Exclude<DirectoryMergeLockOperation, typeof OPERATIONS[number]>] extends [never] ? true : false = true;
void OPERATIONS_COMPLETE;
const OWNER_STATES = ["alive", "dead", "unknown", "missing", "invalid"] as const;
const MAX_DURATION_MS = 7 * 24 * 60 * 60 * 1000;
/**
* Holder evidence a directory-merge lock timeout carries, reduced to closed
* labels, booleans and bounded durations. It never includes the lock path,
* the owner's process id, or any error message.
*/
export interface DirectoryMergeLockTimeoutDiagnostics {
operation?: DirectoryMergeLockOperation;
ownerState?: typeof OWNER_STATES[number];
knownLocalHolder?: boolean;
ownerSameProcess?: boolean;
ownerPredatesProcess?: boolean;
ownerAgeMs?: number;
waitMs?: number;
}
function read(value: unknown, key: string): unknown {
if (!value || typeof value !== "object") return undefined;
try {
return (value as Record<string, unknown>)[key];
} catch {
return undefined;
}
}
/**
* Returns the allowlisted holder diagnostics when `error` itself is a
* directory-merge lock timeout, otherwise `null`. Invalid or unknown values
* are omitted rather than copied.
*/
export function directoryMergeLockTimeoutDiagnosticsFor(error: unknown): DirectoryMergeLockTimeoutDiagnostics | null {
if (read(error, "code") !== WORKSPACE_RESTORE_LOCK_TIMEOUT_CODE) return null;
const lock = read(error, "workspaceRestoreLock");
const diagnostics: DirectoryMergeLockTimeoutDiagnostics = {};
const operation = read(lock, "operation");
const operationLabel = OPERATIONS.find((value) => value === operation);
if (operationLabel) diagnostics.operation = operationLabel;
const ownerState = read(lock, "ownerState");
const ownerStateLabel = OWNER_STATES.find((value) => value === ownerState);
if (ownerStateLabel) diagnostics.ownerState = ownerStateLabel;
for (const field of ["knownLocalHolder", "ownerSameProcess", "ownerPredatesProcess"] as const) {
const value = read(lock, field);
if (typeof value === "boolean") diagnostics[field] = value;
}
for (const field of ["ownerAgeMs", "waitMs"] as const) {
const value = read(lock, field);
if (typeof value === "number" && Number.isSafeInteger(value) && value >= 0 && value <= MAX_DURATION_MS) diagnostics[field] = value;
}
return diagnostics;
}