The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { atom } from 'nanostores'
// Event-driven "backend data changed" signals — the workspace-events twin for
// gateway-side state. The change watcher in tui_gateway broadcasts
// `pet.changed` / `cron.changed` / `sessions.changed` when its on-disk
// signatures move (see server._broadcast_watched_changes), the gateway-event
// lifecycle handler routes them here, and the surfaces that used to poll (cron
// sidebar/page, messaging lists, pet sprite, live session statuses) subscribe
// to these ticks and refresh — so they move exactly when the backend acts and
// stay idle otherwise.
//
// `$changeEventsAvailable` is the compat gate: gateway.ready advertises
// `change_events: true` on backends that broadcast. Consumers keep their old
// polls as SLOW backstops when it's true and at the legacy cadence when it's
// false (an older backend), so nothing goes dark mid-upgrade.
export const $changeEventsAvailable = atom(false)
export const $cronChangeTick = atom(0)
export const $sessionsChangeTick = atom(0)
export const $platformsChangeTick = atom(0)
export const $projectsChangeTick = atom(0)
export const $pairingChangeTick = atom(0)
/** `pet.info.meta`-shaped payload carried on `pet.changed` — lets the pet skip
* the heavy sprite refetch when the broadcast already says enabled=false. */
export interface PetChangeMeta {
enabled: boolean
slug?: string
displayName?: string
scale?: number
spritesheetRevision?: string
}
export const $petChange = atom<{ meta?: PetChangeMeta; tick: number }>({ tick: 0 })
/** `setup.ready` — the boot bootstrap (free-tier identity + provider resolution)
* finished, so inference readiness and the free-tier verdict may have just
* changed. One-shot: the status snapshot re-reads both legs once instead of
* waiting for its next ambient tick. */
export const $setupReadyTick = atom(0)
/** Monotonic clock for "how fresh is this backend boot?" — bumped on every
* `setup.ready` from the active source and on every gateway switch/wipe, so
* onboarding can tell a round that answered inside the boot window (secrets
* may still be hydrating; a `runtime_check` ok:false is not yet evidence)
* from a steady-state one. Kept here, beside the tick, so both come from the
* same lifecycle seam. */
export const $gatewayBootGeneration = atom(0)
/** When the active source's socket last said `gateway.ready` (the backend dispatches requests from
* then on); null before the first. The onboarding due check counts its way-out budget from it. */
export const $gatewayReadyAt = atom<null | number>(null)
export function noteGatewayReady(): void {
$gatewayReadyAt.set(Date.now())
}
export function setChangeEventsAvailable(available: boolean): void {
$changeEventsAvailable.set(available)
}
export function notifyPetChanged(meta?: PetChangeMeta): void {
$petChange.set({ meta, tick: $petChange.get().tick + 1 })
}
export function notifyCronChanged(): void {
$cronChangeTick.set($cronChangeTick.get() + 1)
}
export function notifySessionsChanged(): void {
$sessionsChangeTick.set($sessionsChangeTick.get() + 1)
}
export function notifyPlatformsChanged(): void {
$platformsChangeTick.set($platformsChangeTick.get() + 1)
}
export function notifyProjectsChanged(): void {
$projectsChangeTick.set($projectsChangeTick.get() + 1)
}
export function notifyPairingChanged(): void {
$pairingChangeTick.set($pairingChangeTick.get() + 1)
}
export function notifySetupReady(): void {
$setupReadyTick.set($setupReadyTick.get() + 1)
$gatewayBootGeneration.set($gatewayBootGeneration.get() + 1)
}
/** Reset on gateway wipe/reconnect — a new backend re-advertises capability on
* its own gateway.ready, and stale ticks must not fire refreshes into stores
* the wipe just cleared. */
export function resetLiveSync(): void {
$changeEventsAvailable.set(false)
$gatewayBootGeneration.set($gatewayBootGeneration.get() + 1)
}