ShtukbaseStandard
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.

  1. // The state of the gate for one session: the findings that were red when it started (not the agent's) and how many
  2. // times the gate has already sent the agent back. Kept in .shtukbase/state/gate/<session>.json.
  3. import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { stateDir } from '../../hooks/lib/hooklib.ts';
  6. export type State = { baseline: string[] | null; returns: number };
  7. /** After this many returns the gate lets the agent stop and says why: a loop of returns costs more than it holds. */
  8. export const MAX_RETURNS = 3;
  9. const file = (root: string, session: string) => join(stateDir(root, 'gate'), `${session.replace(/[^A-Za-z0-9_.-]/g, '_') || 'none'}.json`);
  10. export function load(root: string, session: string): State {
  11. const f = file(root, session);
  12. if (!existsSync(f)) return { baseline: null, returns: 0 };
  13. try {
  14. const s = JSON.parse(readFileSync(f, 'utf8'));
  15. return { baseline: Array.isArray(s.baseline) ? s.baseline.map(String) : null, returns: Number(s.returns) || 0 };
  16. } catch {
  17. return { baseline: null, returns: 0 };
  18. }
  19. }
  20. export function save(root: string, session: string, s: State): void {
  21. mkdirSync(stateDir(root, 'gate'), { recursive: true });
  22. writeFileSync(file(root, session), JSON.stringify(s, null, 2) + '\n');
  23. }