UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

107 lines • 6.42 kB
import type { Api, Model, PromptCacheLifetime } from "@earendil-works/pi-ai"; import type { TokenUsageSnapshot } from "./types.ts"; /** Custom session-entry type carrying the cache-warm continuation story. */ export declare const GOAL_CACHE_WARMUP_ENTRY_TYPE = "goal-cache-warmup"; /** * Accounting fallback for a monitor continuation whose scheduled delay is no * longer known (a held timer restored across a reload). It is deliberately NOT * the armed delay: while a wake source is live the monitor arms the configured * backstop below, never a timer derived from the cache-safe wait. */ export declare const GOAL_MONITOR_CONTINUATION_FALLBACK_DELAY_MS = 240000; /** * Default `promptCache.goalBackstopMaxSeconds` (settings-manager.ts) expressed in ms: * the 5-minute Anthropic prompt-cache TTL minus the 30s safety buffer, so the * periodic re-check lands inside the cache. */ export declare const GOAL_MONITOR_BACKSTOP_DEFAULT_DELAY_MS = 270000; /** * Backstop used instead of the 270s default when the active model's prompt cache is best-effort * (for example direct DeepSeek, code-yeongyu/senpi#831): there is no TTL for the re-check to land * inside, so the goal only needs the long liveness re-check, not a cache-preservation wake. */ export declare const GOAL_MONITOR_BEST_EFFORT_BACKSTOP_SECONDS = 3570; /** * Delay of the goal-monitor backstop, in milliseconds. * * A live wake source normally resumes the goal when it delivers (the drain * fire), but a wake source can be misconfigured - a filter that never matches, * a stream that never ends - so the backstop is the floor underneath it: the * goal re-checks at least this often instead of trusting the source for an * hour. It is configured, never derived from the prompt-cache safe wait, through * `promptCache.goalBackstopMaxSeconds`; a missing, non-finite, or non-positive * setting falls back to the 270s default, and the result is clamped into * [1s, 1h]. */ export declare function resolveGoalMonitorContinuationDelayMs(goalBackstopMaxSeconds: number | undefined): number; /** * Backstop ceiling in seconds for the active model's cache lifetime. The built-in 270s default exists * to land the re-check inside a 5-minute TTL; a best-effort cache has no TTL, so an unconfigured * backstop becomes the long liveness re-check. The settings getter reports an unset value as the * 270s default, so a value equal to it is treated as the default. */ export declare function resolveGoalBackstopMaxSecondsForCache(goalBackstopMaxSeconds: number | undefined, lifetime: PromptCacheLifetime | undefined): number | undefined; /** Prompt-cache lifetime of the active model, from the provider's documented cache contract. */ export declare function resolveGoalPromptCacheLifetime(model: Model<Api> | undefined, env: NodeJS.ProcessEnv): PromptCacheLifetime | undefined; /** Cache context captured when a monitor-wait continuation is scheduled. */ export interface GoalCacheWarmMetrics { /** Prompt-cache TTL of the active model in seconds, when the provider documents one. */ readonly ttlSeconds?: number; /** Set when the provider caches automatically with no expiry contract; no TTL or savings are claimed. */ readonly cacheLifetime?: "best-effort"; /** Cumulative cache-read/write tokens reported across the prior agent run's requests, not unique context size. */ readonly cachedTokens: number; /** Hypothetical read discount at current model prices if the same token volume is reused from cache. */ readonly estimatedSavedUsd?: number; } export type GoalCacheWarmupPhase = "scheduled" | "resumed"; /** * Durable payload appended as a `goal-cache-warmup` custom entry and carried by * the `goal_continuation_scheduled` / `goal_continuation_resumed` pi-events, so * external consumers (for example omo-desktop-app) can render the story later. */ export interface GoalCacheWarmupEntryData { readonly phase: GoalCacheWarmupPhase; readonly goalId: string; /** Display ordinal within the current in-memory Goal/wake epoch; absent on legacy persisted entries. */ readonly iteration?: number; /** Planned continuation delay in milliseconds. */ readonly delayMs: number; /** Epoch milliseconds when the scheduled continuation is expected to resume. */ readonly dueAtMs?: number; /** Actual wait in milliseconds; present on the `resumed` phase only. */ readonly waitedMs?: number; /** Actual resumption trigger; absent on legacy entries whose cause was not recorded. */ readonly wakeCause?: "timer" | "sources-drained"; /** Backward-compatible field containing the total active wake-source count. */ readonly activeMonitorCount: number; /** Full source-keyed snapshot; absent on entries written before wake sources were generalized. */ readonly wakeSources?: Readonly<Record<string, number>>; readonly cache?: GoalCacheWarmMetrics; } /** Live entries always carry an iteration; the ordinal is intentionally not persisted into Goal state. */ export type LiveGoalCacheWarmupEntryData = GoalCacheWarmupEntryData & { readonly iteration: number; }; export type GoalCacheWarmScheduleData = Omit<LiveGoalCacheWarmupEntryData, "phase" | "waitedMs">; export declare function createGoalCacheWarmScheduleData(options: { readonly goalId: string; readonly delayMs: number; readonly scheduledAtMs: number; readonly iteration: number; readonly activeMonitorCount: number; readonly wakeSources: Readonly<Record<string, number>>; readonly cache?: GoalCacheWarmMetrics; }): GoalCacheWarmScheduleData; export declare function estimateCacheWarmMetrics(model: Model<Api> | undefined, env: NodeJS.ProcessEnv, lastTurnUsage: Pick<TokenUsageSnapshot, "cacheRead" | "cacheWrite"> | undefined): GoalCacheWarmMetrics | undefined; export declare function formatWarmTokenCount(tokens: number): string; export declare function formatWakeDuration(ms: number): string; /** * Wake timestamp shown in cache-warm notices. Rendered in the user's local * system timezone with its short zone label (for example `GMT+9`); falls back * to the legacy UTC shape when local timezone formatting is unavailable. */ export declare function formatWakeTimestamp(dueAtMs: number): string; export declare function formatCacheTtl(seconds: number): string; export declare function formatSavedUsd(value: number): string; //# sourceMappingURL=cache-warm.d.ts.map