repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources15d ago| #1 | /** |
| #2 | * ooda/journal.ts — Append-only tick journal |
| #3 | * |
| #4 | * Every tick is written as a single JSON line to journal/ticks.jsonl. |
| #5 | * This is the authoritative external record — the harness's memory. |
| #6 | * State on restart is reconstructed by replaying this file. |
| #7 | * |
| #8 | * Ralph rule: "State lives in git." |
| #9 | */ |
| #10 | |
| #11 | import { appendFileSync, mkdirSync, readFileSync, existsSync } from 'node:fs'; |
| #12 | import { join, dirname } from 'node:path'; |
| #13 | import { fileURLToPath } from 'node:url'; |
| #14 | import type { Decision } from './validate.js'; |
| #15 | import type { Candle } from './state.js'; |
| #16 | |
| #17 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| #18 | const JOURNAL_PATH = join(__dirname, 'journal', 'ticks.jsonl'); |
| #19 | |
| #20 | export interface TickEntry { |
| #21 | tick: number; |
| #22 | now: string; |
| #23 | candles_last3: Candle[]; |
| #24 | book_snapshot: unknown; |
| #25 | decision: Decision; |
| #26 | outcome: 'applied' | 'rejected' | 'killswitch'; |
| #27 | violation?: string; |
| #28 | pnl_lamports?: number; |
| #29 | total_pnl_lamports?: number; |
| #30 | consecutive_losses?: number; |
| #31 | event?: string; |
| #32 | } |
| #33 | |
| #34 | export function appendTick(entry: TickEntry): void { |
| #35 | mkdirSync(dirname(JOURNAL_PATH), { recursive: true }); |
| #36 | appendFileSync(JOURNAL_PATH, JSON.stringify(entry) + '\n', 'utf8'); |
| #37 | } |
| #38 | |
| #39 | /** Read the last N entries from the journal (for observations injection) */ |
| #40 | export function readLastEntries(n = 3): TickEntry[] { |
| #41 | if (!existsSync(JOURNAL_PATH)) return []; |
| #42 | const lines = readFileSync(JOURNAL_PATH, 'utf8') |
| #43 | .split('\n') |
| #44 | .filter(Boolean) |
| #45 | .slice(-n); |
| #46 | return lines.map(l => JSON.parse(l) as TickEntry); |
| #47 | } |
| #48 | |
| #49 | /** Truncate journal (for fresh run) */ |
| #50 | export function clearJournal(): void { |
| #51 | if (existsSync(JOURNAL_PATH)) { |
| #52 | appendFileSync(JOURNAL_PATH, '', 'utf8'); // keep file, mark empty-ish |
| #53 | } |
| #54 | } |
| #55 | |
| #56 | /** Format the journal path for display */ |
| #57 | export function journalPath(): string { |
| #58 | return JOURNAL_PATH; |
| #59 | } |
| #60 |