Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1mcp app Agent runtimeScopeOwnership

From Leeroopedia
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
}

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment