Implementation:1mcp app Agent runtimeScopeOwnership
| Knowledge Sources | |
|---|---|
| Domains | Process_Management, Security |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete ownership management module for runtime scopes that handles claiming, verifying, and reclaiming scope ownership with PID liveness checks and identity matching.
Description
The runtimeScopeOwnership module manages the ownership of runtime scopes (config directories). It provides `claimRuntimeScope` for asserting ownership before starting a runtime, `verifyRuntimeScopeOwnership` for checking if a PID is alive and its identity matches, `reclaimStaleRuntimeScopeOwnership` for safely taking over from a dead owner, and `readRuntimeScopeOwnership` for reading the ownership record. This is critical for unclean shutdown recovery (ADR-0017) and cooperative runtime upgrade (ADR-0018).
Usage
Import this module when you need to:
- Claim a runtime scope before starting a background supervisor
- Verify that a PID is still alive and its identity matches the ownership record
- Reclaim a stale scope after an unclean shutdown
- Read the current ownership record for a scope
Code Reference
Source Location
- Repository: 1MCP Agent
- File: src/core/server/runtimeScopeOwnership.ts (L133-469)
Signature
export function claimRuntimeScope(
configDir: string,
ownership: RuntimeScopeOwnership,
): RuntimeScopeOwnershipRecord;
export function reclaimStaleRuntimeScopeOwnership(
configDir: string,
): RuntimeScopeOwnershipRecord | null;
export function verifyRuntimeScopeOwnership(
configDir: string,
identity: ProcessIdentity,
): boolean;
export function readRuntimeScopeOwnership(
configDir: string,
): RuntimeScopeOwnershipRecord | null;
interface RuntimeScopeOwnership {
kind: 'background-supervisor' | 'foreground-stdio';
}
Import
import {
claimRuntimeScope,
reclaimStaleRuntimeScopeOwnership,
verifyRuntimeScopeOwnership,
readRuntimeScopeOwnership,
} from '@src/core/server/runtimeScopeOwnership.js';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| configDir | string | Yes | Scope config directory |
| ownership | RuntimeScopeOwnership | Yes | Ownership type (background-supervisor or foreground-stdio) |
| identity | ProcessIdentity | Yes | Process identity for verification |
Outputs
| Name | Type | Description |
|---|---|---|
| RuntimeScopeOwnershipRecord | object | PID, claim ID, kind, timestamp |
| null | null | If scope is unowned or reclaim fails |
Usage Examples
Claim and Reclaim
import { claimRuntimeScope, reclaimStaleRuntimeScopeOwnership } from '@src/core/server/runtimeScopeOwnership.js';
// Claim before starting supervisor
const ownership = claimRuntimeScope('/path/to/config', {
kind: 'background-supervisor',
});
// Reclaim after unclean shutdown
const reclaimed = reclaimStaleRuntimeScopeOwnership('/path/to/config');
if (reclaimed) {
// Safe to start fresh runtime
}