Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

3.0 KB
Raw Blame
import type { LedgerDatabase, RefreshNeeds } from './request-groups.mjs';
import type { LabelRunResult } from './work-state-labels.mjs';

export type RefreshState = 'idle' | 'running' | 'ok' | 'failed' | 'waiting-for-agent';
export interface RefreshStepTiming { step: string; agent: string; startedAt: string; finishedAt: string; groups?: number }
export interface RefreshJobOutcome {
  state: 'ok' | 'failed' | 'waiting-for-agent'; agent: string | null; result: string | null; error: string | null; steps?: RefreshStepTiming[];
  /** Due groups an ok run left without a review (refresh-task.ts). They are still due, so the next scheduled check waits as after a failed run. */
  groupsLeftWithoutReview?: number;
}
export interface RefreshStatus {
  state: RefreshState;
  reason?: string;
  agent?: string | null;
  startedAt?: string;
  finishedAt?: string;
  checkedAt?: string;
  nextAt?: string | null;
  /** null when the run failed before it could read what was due. */
  needs?: RefreshNeeds | null;
  result?: string | null;
  error?: string | null;
  steps?: RefreshStepTiming[];
  /** Runs in a row that failed or left groups without a review; the next scheduled check waits longer for each.
   * Absent after a run that finishes everything due. */
  failures?: number;
}

/** The reason of a run the owner started with My Work's Refresh. */
export const OWNER_REFRESH_REASON: 'manual';

/** Milliseconds between scheduled checks for a CLAUDE_MEM_WORK_VIEW_REFRESH_INTERVAL_MIN value; invalid input gives ten minutes. */
export function refreshIntervalMs(minutes: string | number | null | undefined): number;

export class AutoRefreshWorker {
  constructor(options: {
    dir: string;
    openDatabase: () => LedgerDatabase & { close(): void };
    runJob: (job: { needs: RefreshNeeds; reason: string }) => Promise<RefreshJobOutcome>;
    enabled?: () => boolean;
    setupRunning?: () => boolean;
    checkMs?: number;
    now?: () => number;
    log?: (level: 'info' | 'warn', message: string, data?: Record<string, unknown>) => void;
    /** The worker's sync device id ('' when cloud sync never started), so my-focus rows replay in its order. */
    localDeviceId: () => string;
    /** Jev labels for open work-state items (work-state-labels.mjs); null when the install has no Jev. */
    labelWorkState?: (() => Promise<LabelRunResult | null>) | null;
  });
  /** Starts a labeling run unless one is going; the promise never rejects. Null without labelWorkState. */
  labelInBackground(): Promise<LabelRunResult | null> | null;
  /** True from the moment a run starts until it finishes. */
  readonly busy: boolean;
  status(): RefreshStatus | null;
  /** At startup: marks a "running" record no live worker owns as failed. A failed write is logged and retried at the next check. */
  repairInterrupted(): void;
  start(): void;
  close(): void;
  check(): Promise<'closed' | 'disabled' | 'running' | 'backing-off' | 'idle' | RefreshJobOutcome['state']>;
  runNow(options: { reason: string }): Promise<'closed' | 'setup-running' | 'running' | 'idle' | RefreshJobOutcome['state']>;
}