archivo del paquete→ .shtukbase/packages/gate/hooks/lib.ts
hooks/lib.ts
27 líneas, 1.3 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // The state of the gate for one session: the findings that were red when it started (not the agent's) and how many
- // times the gate has already sent the agent back. Kept in .shtukbase/state/gate/<session>.json.
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { stateDir } from '../../hooks/lib/hooklib.ts';
- export type State = { baseline: string[] | null; returns: number };
- /** After this many returns the gate lets the agent stop and says why: a loop of returns costs more than it holds. */
- export const MAX_RETURNS = 3;
- const file = (root: string, session: string) => join(stateDir(root, 'gate'), `${session.replace(/[^A-Za-z0-9_.-]/g, '_') || 'none'}.json`);
- export function load(root: string, session: string): State {
- const f = file(root, session);
- if (!existsSync(f)) return { baseline: null, returns: 0 };
- try {
- const s = JSON.parse(readFileSync(f, 'utf8'));
- return { baseline: Array.isArray(s.baseline) ? s.baseline.map(String) : null, returns: Number(s.returns) || 0 };
- } catch {
- return { baseline: null, returns: 0 };
- }
- }
- export function save(root: string, session: string, s: State): void {
- mkdirSync(stateDir(root, 'gate'), { recursive: true });
- writeFileSync(file(root, session), JSON.stringify(s, null, 2) + '\n');
- }