@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
81 lines (80 loc) • 3.75 kB
TypeScript
import { type InboxItem } from "./owner-inbox.js";
import type { OwnerInboxPush } from "./push.js";
/** What the sweep needs from `ApprovalPushService`, injected so tests never
* reach a provider. Both calls re-check reachability, registration and the
* `ownerInbox` kind at send time. */
export interface OwnerInboxPushSender {
ownerInboxPushDevices(): string[];
notifyOwnerInbox(value: OwnerInboxPush, deviceID: string): Promise<boolean>;
}
/** A failed candidate/device pair waits this long before another attempt. */
export declare const OWNER_INBOX_RETRY_MS = 60000;
/** The internal tick snapshot adds first-open eligibility to the public row. */
type ObservedItem = InboxItem & {
firstOpen?: boolean;
blocks?: boolean;
};
export interface OwnerInboxPushOptions {
/** Fresh local store read under the Inbox lock. */
isOpenForPush: (id: string) => Promise<boolean>;
/** `<bridge>/owner-inbox-push.json` by default. */
file?: string;
/** Test seam; the ledger's deadlines and the retry interval read this. */
now?: () => number;
/** Test seam for the corrupt-ledger error. */
log?: (message: string) => void;
/** The open manual row that takes the first backfill slot; defaults to
* `PHREN_OWNER_INBOX_BACKFILL_ID` read on each sweep. */
backfillId?: string;
}
/**
* Alerts the owner's registered phones about new open rows this Hook owns.
* Fed from the Hook's successful local reconciliation. It checks the local
* store under its lock and releases it before calling the provider.
*/
export declare class OwnerInboxPushSweep {
private readonly sender;
private readonly file;
private readonly now;
private readonly log;
private readonly backfillId;
private readonly isOpenForPush;
private chain;
private active;
private queued?;
/** The newest snapshot seen, including one queued while a sweep runs; each
* provider call rechecks it so a closed row is never sent late. */
private latest;
/** `${id}:${deviceID}` → earliest epoch ms the pair may be attempted again. */
private readonly retryAfter;
/** Acceptances whose receipt write failed, so a live Hook retries persistence
* without sending the same phone another alert. */
private readonly accepted;
private corruptReported;
constructor(sender: OwnerInboxPushSender, options: OwnerInboxPushOptions);
/**
* One reconciliation's open rows. Single-flight: a tick that arrives while
* a sweep runs replaces the snapshot the next pass uses instead of starting
* a second sender run; callers share the same promise.
*/
observe(openItems: ObservedItem[]): Promise<void>;
private load;
private sweep;
/** Merge acceptances whose receipt write failed into a freshly loaded ledger. */
private mergeAccepted;
/** The initial snapshot: up to three rows updated in the preceding day,
* newest first with a UUID tie-break. The override row takes the first slot
* regardless of age so a known row (OVH) is still backfilled. A same-id
* reopen never takes a seed slot. */
private seed;
/** Add rows absent from the initial snapshot and drop expired entries.
* Retired rows are either in that snapshot or past their 24-hour window. */
private reconcile;
/** Up to three distinct candidate rows per sweep, oldest first: a new row by
* its creation time and a backfill by its own updatedAt (they all share the
* initialization deadline). Each selected row is offered to all of its
* eligible phones. Recheck persisted state, the newest snapshot and the
* deadline before each call. A row consumes a slot on its first attempt. */
private deliver;
}
export {};