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 | * leviathan/src/pulse.ts — Depth-aware tail-flick rhythm |
| #3 | * |
| #4 | * The pulse daemon runs on an interval driven by the current depth tier. |
| #5 | * Deep leviathans pulse every 60s. Shallow every 5min. Shoreline every 15min. |
| #6 | * Beached leviathans do not pulse — the process exits. |
| #7 | * |
| #8 | * This is the "between flicks" scheduler — distinct from the agent loop |
| #9 | * which runs once per pulse invocation. |
| #10 | */ |
| #11 | |
| #12 | import { getTier } from './survival.js'; |
| #13 | import type { Depth } from './types.js'; |
| #14 | |
| #15 | export interface PulseHandle { |
| #16 | stop: () => void; |
| #17 | isRunning: () => boolean; |
| #18 | } |
| #19 | |
| #20 | /** |
| #21 | * Start the pulse daemon. Calls `onTick` at the depth-appropriate interval. |
| #22 | * Returns a handle with `.stop()` to cleanly shut down. |
| #23 | */ |
| #24 | export function startPulse( |
| #25 | getDepth: () => Depth, |
| #26 | onTick: (depth: Depth, tickNumber: number) => Promise<void>, |
| #27 | onBeach: () => void, |
| #28 | ): PulseHandle { |
| #29 | let running = true; |
| #30 | let tickNumber = 0; |
| #31 | let timer: ReturnType<typeof setTimeout> | null = null; |
| #32 | |
| #33 | async function pulse(): Promise<void> { |
| #34 | if (!running) return; |
| #35 | |
| #36 | const depth = getDepth(); |
| #37 | |
| #38 | if (depth === 'beached') { |
| #39 | running = false; |
| #40 | onBeach(); |
| #41 | return; |
| #42 | } |
| #43 | |
| #44 | tickNumber += 1; |
| #45 | const tier = getTier(depth); |
| #46 | |
| #47 | try { |
| #48 | await onTick(depth, tickNumber); |
| #49 | } catch (err) { |
| #50 | process.stderr.write(`[pulse] tick ${tickNumber} error: ${String(err)}\n`); |
| #51 | } |
| #52 | |
| #53 | if (!running) return; |
| #54 | |
| #55 | // Re-read depth after the tick (might have changed) |
| #56 | const nextDepth = getDepth(); |
| #57 | const nextInterval = getTier(nextDepth).pulseIntervalMs; |
| #58 | timer = setTimeout(pulse, nextInterval); |
| #59 | } |
| #60 | |
| #61 | // First pulse: immediate |
| #62 | const firstDepth = getDepth(); |
| #63 | const firstInterval = getTier(firstDepth).pulseIntervalMs; |
| #64 | timer = setTimeout(pulse, 100); // small delay for startup |
| #65 | |
| #66 | return { |
| #67 | stop: () => { |
| #68 | running = false; |
| #69 | if (timer) clearTimeout(timer); |
| #70 | }, |
| #71 | isRunning: () => running, |
| #72 | }; |
| #73 | } |
| #74 | |
| #75 | /** |
| #76 | * Format time until next pulse for status display. |
| #77 | */ |
| #78 | export function formatNextPulse(lastPulse: string, depth: Depth): string { |
| #79 | const tier = getTier(depth); |
| #80 | const elapsed = Date.now() - new Date(lastPulse).getTime(); |
| #81 | const remaining = Math.max(0, tier.pulseIntervalMs - elapsed); |
| #82 | const secs = Math.ceil(remaining / 1000); |
| #83 | return secs < 60 ? `${secs}s` : `${Math.ceil(secs / 60)}m`; |
| #84 | } |
| #85 |