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:777genius Agent teams ai ReviewPersistenceScopeLock

From Leeroopedia
Knowledge Sources
Domains Code_Review, Concurrency
Last Updated 2026-10-22 00:00 GMT

Overview

Concrete tool for serializing concurrent writes to review decision scopes using SQLite-based lease locks with heartbeat ownership verification, provided by the Agent Teams application.

Description

The withReviewPersistenceScopeLock function acquires an exclusive lease on a review decision scope (task or agent) from a SQLite database, executes the protected operation, and releases the lease. A heartbeat interval refreshes the lease during long operations; if the heartbeat detects the lease was lost (e.g., another process took over), the operation result is rejected.

Key behaviors:

  • SQLite lease database: Uses `better-sqlite3` to store lease records with owner tokens, enabling cross-process mutual exclusion.
  • Heartbeat verification: A `setInterval` heartbeat periodically verifies lease ownership; if ownership is lost, `ownershipLost` is set and the operation result is rejected after completion.
  • Configurable timeouts: `acquireTimeoutMs` (how long to wait for the lease), `retryIntervalMs` (how often to retry acquisition), and `heartbeatIntervalMs` (how often to verify ownership).
  • Logical scope lock: `withReviewPersistenceLogicalScopeLock()` acquires a lock on a logical scope key (used to serialize retention/recovery discovery before the exact-scope lock).
  • Safe teardown: `closeReviewPersistenceScopeLockDatabasesForTests()` closes all SQLite databases for test cleanup.

Usage

  • Protecting review saves: Wrap `ReviewDecisionStore.save()` calls in `withReviewPersistenceScopeLock()` to prevent concurrent writes to the same scope.
  • Logical vs. exact scope: Use the logical lock for retention/recovery discovery, then the exact-scope lock for the actual write.

Code Reference

Source Location

  • Repository: agent-teams
  • File: src/main/services/team/ReviewPersistenceScopeLock.ts

Signature

export interface ReviewPersistenceScopeLockOptions {
  acquireTimeoutMs?: number;
  retryIntervalMs?: number;
  heartbeatIntervalMs?: number;
}

export async function withReviewPersistenceScopeLock<T>(
  teamName: string,
  persistenceScope: ReviewDecisionPersistenceScope,
  operation: () => Promise<T>,
  options?: ReviewPersistenceScopeLockOptions
): Promise<T>;

export async function withReviewPersistenceLogicalScopeLock<T>(
  teamName: string,
  scopeKey: string,
  operation: () => Promise<T>,
  options?: ReviewPersistenceScopeLockOptions
): Promise<T>;

export function closeReviewPersistenceScopeLockDatabasesForTests(): void;

Import

import { withReviewPersistenceScopeLock } from '@main/services/team/ReviewPersistenceScopeLock';

I/O Contract

Inputs

Name Type Required Description
teamName string Yes Team name (validated for safety)
persistenceScope ReviewDecisionPersistenceScope Yes Scope to lock (contains scopeKey and scopeToken)
operation () => Promise<T> Yes Protected operation to execute under lock
options.acquireTimeoutMs number No Max time to wait for lease (default: configured)
options.heartbeatIntervalMs number No How often to verify ownership (default: configured)

Outputs

Name Type Description
return Promise<T> Result of the operation, or error if lock was lost
side effect SQLite lease Lease acquired, heartbeated, and released

Usage Examples

Protecting a Review Save

import { withReviewPersistenceScopeLock } from '@main/services/team/ReviewPersistenceScopeLock';
import { ReviewDecisionStore } from '@main/services/team/ReviewDecisionStore';

const store = new ReviewDecisionStore();

// Acquire a lock on the task scope before saving
const revision = await withReviewPersistenceScopeLock(
  'my-team',
  { scopeKey: 'task-123', scopeToken: scopeToken },
  () => store.save('my-team', 'task-123', {
    scopeToken,
    hunkDecisions: { 'hunk-1': 'accept' },
    fileDecisions: { 'file.ts': 'accept' },
  })
);

With Custom Timeouts

const result = await withReviewPersistenceScopeLock(
  'my-team',
  scope,
  () => longRunningOperation(),
  {
    acquireTimeoutMs: 10_000,   // Wait up to 10s for the lock
    retryIntervalMs: 200,       // Retry every 200ms
    heartbeatIntervalMs: 1_000, // Check ownership every 1s
  }
);

Logical Scope Lock for Discovery

import { withReviewPersistenceLogicalScopeLock } from '@main/services/team/ReviewPersistenceScopeLock';

// Serialize retention/recovery discovery before the exact-scope lock
await withReviewPersistenceLogicalScopeLock(
  'my-team',
  'task-123',
  async () => {
    // Discovery logic here...
  }
);

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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