@fabersep/cache 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+ /** A tool profile: the tools, permission mode and settings a run gets (FR-01.2). */
2
+ export interface Profile {
3
+ /** Built-in tool names; null means the engine's own default tool set. */
4
+ tools: string[] | null;
5
+ /** The engine's permission mode; null leaves it to the engine. */
6
+ permissionMode: string | null;
7
+ }
8
+ export interface ProjectConfig {
9
+ /** Packet files in order, relative to the project folder or starting with `~/`. */
10
+ packet?: string[];
11
+ /** The profile headless CLI runs use when none is given. */
12
+ profile?: string;
13
+ /** Extra profiles; only these may name a permission-bypass mode (FR-10.2). */
14
+ profiles?: Record<string, Profile>;
15
+ model?: string;
16
+ }
17
+ export interface Limits {
18
+ maxPacketBytes: number;
19
+ lockWaitSec: number;
20
+ parallel: number;
21
+ mcpMaxTasks: number;
22
+ mcpMaxTurns: number;
23
+ mcpTimeoutSec: number;
24
+ }
25
+ export interface Config {
26
+ model: string;
27
+ lifetimeMin: number;
28
+ marginMin: number;
29
+ bar: number;
30
+ limits: Limits;
31
+ projects: Record<string, ProjectConfig>;
32
+ }
33
+ export declare const READ_ONLY = "read-only";
34
+ export declare const BYPASS_MODE = "bypassPermissions";
35
+ export declare const BUILTIN_PROFILES: Record<string, Profile>;
36
+ export declare const DEFAULTS: Config;
37
+ /** Default packet: the memory index, the entry file, master PRD and architecture, when present. */
38
+ export declare const DEFAULT_PACKET: string[];
39
+ export declare function cacheHome(): string;
40
+ export declare function configPath(home?: string): string;
41
+ export declare function loadConfig(home?: string): Config;
42
+ export declare function saveConfig(config: Config, home?: string): void;
43
+ export declare function expandHome(p: string): string;
44
+ /** A project is named by its folder; the config is keyed by the absolute path. */
45
+ export declare function resolveProject(project: string): string;
46
+ export declare function projectConfig(config: Config, projectDir: string): ProjectConfig;
47
+ /**
48
+ * The profile for a run. Built-in profiles never bypass permissions; a project profile may only
49
+ * when the project's config names it (FR-10.2). `forceReadOnly` is the MCP path (FR-08.4).
50
+ */
51
+ export declare function resolveProfile(config: Config, projectDir: string, name: string | undefined, forceReadOnly?: boolean): {
52
+ name: string;
53
+ profile: Profile;
54
+ };
@@ -0,0 +1,90 @@
1
+ import * as fs from 'node:fs';
2
+ import * as os from 'node:os';
3
+ import * as path from 'node:path';
4
+ export const READ_ONLY = 'read-only';
5
+ export const BYPASS_MODE = 'bypassPermissions';
6
+ export const BUILTIN_PROFILES = {
7
+ [READ_ONLY]: { tools: ['Read', 'Glob', 'Grep'], permissionMode: 'dontAsk' },
8
+ card: { tools: ['Read', 'Glob', 'Grep', 'Edit', 'Write', 'Bash'], permissionMode: 'acceptEdits' },
9
+ interactive: { tools: null, permissionMode: null },
10
+ };
11
+ export const DEFAULTS = {
12
+ model: 'sonnet',
13
+ lifetimeMin: 60,
14
+ marginMin: 5,
15
+ bar: 0.9,
16
+ limits: {
17
+ maxPacketBytes: 200_000,
18
+ lockWaitSec: 300,
19
+ parallel: 4,
20
+ mcpMaxTasks: 5,
21
+ mcpMaxTurns: 20,
22
+ mcpTimeoutSec: 600,
23
+ },
24
+ projects: {},
25
+ };
26
+ /** Default packet: the memory index, the entry file, master PRD and architecture, when present. */
27
+ export const DEFAULT_PACKET = [
28
+ '~/.config/faber-memory/state/MEMORY.md',
29
+ 'AGENTS.md',
30
+ 'CLAUDE.md',
31
+ 'docs/master-prd.md',
32
+ 'docs/architecture.md',
33
+ ];
34
+ export function cacheHome() {
35
+ return process.env.FABER_CACHE_HOME || path.join(os.homedir(), '.faber', 'cache');
36
+ }
37
+ export function configPath(home = cacheHome()) {
38
+ return path.join(home, 'config.json');
39
+ }
40
+ export function loadConfig(home = cacheHome()) {
41
+ let raw = {};
42
+ try {
43
+ raw = JSON.parse(fs.readFileSync(configPath(home), 'utf8'));
44
+ }
45
+ catch (err) {
46
+ if (err.code !== 'ENOENT') {
47
+ throw new Error(`cannot read ${configPath(home)}: ${err.message}`);
48
+ }
49
+ }
50
+ return {
51
+ ...DEFAULTS,
52
+ ...raw,
53
+ limits: { ...DEFAULTS.limits, ...(raw.limits ?? {}) },
54
+ projects: raw.projects ?? {},
55
+ };
56
+ }
57
+ export function saveConfig(config, home = cacheHome()) {
58
+ fs.mkdirSync(home, { recursive: true });
59
+ const file = configPath(home);
60
+ const tmp = `${file}.${process.pid}.tmp`;
61
+ fs.writeFileSync(tmp, JSON.stringify(config, null, 2) + '\n');
62
+ fs.renameSync(tmp, file);
63
+ }
64
+ export function expandHome(p) {
65
+ return p === '~' || p.startsWith('~/') ? path.join(os.homedir(), p.slice(1)) : p;
66
+ }
67
+ /** A project is named by its folder; the config is keyed by the absolute path. */
68
+ export function resolveProject(project) {
69
+ return path.resolve(expandHome(project));
70
+ }
71
+ export function projectConfig(config, projectDir) {
72
+ return config.projects[projectDir] ?? {};
73
+ }
74
+ /**
75
+ * The profile for a run. Built-in profiles never bypass permissions; a project profile may only
76
+ * when the project's config names it (FR-10.2). `forceReadOnly` is the MCP path (FR-08.4).
77
+ */
78
+ export function resolveProfile(config, projectDir, name, forceReadOnly = false) {
79
+ if (forceReadOnly)
80
+ return { name: READ_ONLY, profile: BUILTIN_PROFILES[READ_ONLY] };
81
+ const pc = projectConfig(config, projectDir);
82
+ const chosen = name ?? pc.profile ?? READ_ONLY;
83
+ const own = pc.profiles?.[chosen];
84
+ if (own)
85
+ return { name: chosen, profile: own };
86
+ const builtin = BUILTIN_PROFILES[chosen];
87
+ if (!builtin)
88
+ throw new Error(`unknown profile "${chosen}"`);
89
+ return { name: chosen, profile: builtin };
90
+ }
@@ -0,0 +1,120 @@
1
+ import { Config, Profile, READ_ONLY } from './config.js';
2
+ import { EngineRun, Mode, RunOptions } from './engines/claude.js';
3
+ import { Packet } from './packet.js';
4
+ import { RunRecord, Usage } from './records.js';
5
+ import { BaseEntry } from './registry.js';
6
+ export declare const ENGINES: readonly ["claude", "agy", "codex"];
7
+ export type Engine = (typeof ENGINES)[number];
8
+ export declare class CacheError extends Error {
9
+ }
10
+ export interface Context {
11
+ home: string;
12
+ config: Config;
13
+ now: () => number;
14
+ runner: (opts: RunOptions) => Promise<EngineRun>;
15
+ log: (msg: string) => void;
16
+ }
17
+ export declare function defaultContext(overrides?: Partial<Context>): Context;
18
+ export interface TaskRequest {
19
+ project: string;
20
+ profile?: string;
21
+ mode?: Mode;
22
+ engine?: string;
23
+ model?: string;
24
+ /** Expected uses of the base (FR-04.3); omitted means "unknown" (FR-04.4). */
25
+ uses?: number;
26
+ /** The MCP path: always the read-only profile (FR-08.4). */
27
+ forceReadOnly?: boolean;
28
+ maxTurns?: number;
29
+ signal?: AbortSignal;
30
+ onSpawn?: (pid: number) => void;
31
+ }
32
+ export interface TaskResult {
33
+ task: string;
34
+ reply: string;
35
+ ok: boolean;
36
+ error?: string;
37
+ record: RunRecord;
38
+ }
39
+ interface Prepared {
40
+ projectDir: string;
41
+ engine: Engine;
42
+ engineVersion: string;
43
+ mode: Mode;
44
+ profileName: string;
45
+ profile: Profile;
46
+ model: string;
47
+ packet: Packet;
48
+ key: string;
49
+ parts: Record<string, unknown>;
50
+ needsWarmup: boolean;
51
+ breakEven: number;
52
+ }
53
+ /** Packet, key and policy for a request; refuses before any engine starts (FR-01.5, FR-01.6). */
54
+ export declare function prepare(ctx: Context, req: TaskRequest, defaultMode?: Mode, defaultProfile?: string): Prepared;
55
+ export declare function isFresh(ctx: Context, entry: BaseEntry | undefined): entry is BaseEntry;
56
+ /** One task (FR-05.1): fork a fresh base, build one when it pays, or run with the packet in front. */
57
+ export declare function runTask(ctx: Context, req: TaskRequest, task: string): Promise<TaskResult>;
58
+ /** A fan-out (FR-05.3): build and warm once, then fork every task with a parallel limit. */
59
+ export declare function runTasks(ctx: Context, req: TaskRequest, tasks: string[]): Promise<TaskResult[]>;
60
+ /** `faber-cache warm` (FR-04.5): one build ahead of a batch, never a schedule. */
61
+ export declare function warm(ctx: Context, req: TaskRequest): Promise<BaseEntry>;
62
+ export interface OpenPlan {
63
+ cwd: string;
64
+ bin: string;
65
+ args: string[];
66
+ command: string;
67
+ entry: BaseEntry;
68
+ prepared: Prepared;
69
+ }
70
+ /** The interactive fork of the warm default-prompt base, built first if needed (FR-06.1). */
71
+ export declare function planOpen(ctx: Context, req: TaskRequest): Promise<OpenPlan>;
72
+ /** Launch the interactive fork in this terminal (FR-06.3): last read updated, one open record. */
73
+ export declare function launchOpen(ctx: Context, plan: OpenPlan): Promise<number>;
74
+ export interface StatusRow {
75
+ key: string;
76
+ engine: string;
77
+ profile: string;
78
+ mode: string;
79
+ model: string;
80
+ engineVersion: string;
81
+ fresh: boolean;
82
+ uses: number;
83
+ lastReadMinAgo: number;
84
+ warm: boolean;
85
+ current: boolean;
86
+ }
87
+ /** `faber-cache status` (FR-12.2): the current key and every registered base of the project. */
88
+ export declare function status(ctx: Context, req: TaskRequest): {
89
+ project: string;
90
+ currentKey: string;
91
+ rows: StatusRow[];
92
+ };
93
+ export interface AgyMeasurementRun {
94
+ index: number;
95
+ delayMin: number;
96
+ at: string;
97
+ ok: boolean;
98
+ session: string | null;
99
+ first: Usage | null;
100
+ total: Usage | null;
101
+ refPrefix: number | null;
102
+ readShare: number | null;
103
+ error?: string;
104
+ }
105
+ export interface AgyMeasurementResult {
106
+ engine: 'agy';
107
+ mode: 'prefix';
108
+ engineVersion: string;
109
+ model: string;
110
+ date: string;
111
+ runs: AgyMeasurementRun[];
112
+ passed: boolean;
113
+ meanReadShare: number | null;
114
+ }
115
+ /** FR-11.5 / AC-57: three independent single-turn agy runs with the same packet at 0, 1 and 10 min. */
116
+ export declare function measureAgy(ctx: Context, req: Omit<TaskRequest, 'engine' | 'mode'>, opts?: {
117
+ delaysMin?: number[];
118
+ sleep?: (ms: number) => Promise<void>;
119
+ }): Promise<AgyMeasurementResult>;
120
+ export { READ_ONLY };