AkurAI Build
Menu

popagent

public

Latest change 2fb6f198c4c71ef37dffc8ac5dca8482068a7bfc - Add governed AkurAI Build maintenance by AkurAI Build

export type AutonomyLease = () => void;

/**
 * Process-local execution gate for autonomous work. Persisted task state remains
 * the restart and multi-worker authority; this gate closes the in-process race
 * between an interactive request and a background claim.
 */
export class AutonomyActivityGate {
  private readonly interactive = new Set<symbol>();
  private readonly background = new Set<symbol>();
  private readonly autonomous = new Set<symbol>();

  private acquire(bucket: Set<symbol>, id: string): AutonomyLease {
    const lease = Symbol(id);
    bucket.add(lease);
    let released = false;
    return () => {
      if (released) return;
      released = true;
      bucket.delete(lease);
    };
  }

  get isIdle(): boolean {
    return this.interactive.size === 0
      && this.background.size === 0
      && this.autonomous.size === 0;
  }
  enterInteractive(id: string): AutonomyLease {
    return this.acquire(this.interactive, id);
  }

  enterBackground(id: string): AutonomyLease {
    return this.acquire(this.background, id);
  }

  tryEnterAutonomous(id: string): AutonomyLease | undefined {
    if (!this.isIdle) return undefined;
    return this.acquire(this.autonomous, id);
  }

  snapshot(): { interactive: number; background: number; autonomous: number; idle: boolean } {
    return {
      interactive: this.interactive.size,
      background: this.background.size,
      autonomous: this.autonomous.size,
      idle: this.isIdle,
    };
  }
}

export const autonomyActivity = new AutonomyActivityGate();