@indigoai-us/hq-cli 5.77.12 → 5.77.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,210 @@
1
+ /**
2
+ * Outpost on-box session heartbeat emitter — mission-control US-009.
3
+ *
4
+ * Runs ON the Outpost VM (not in a Lambda). On a fixed cadence it:
5
+ * 1. enumerates the box's local Claude Code (`~/.claude/projects/**\/<uuid>.jsonl`)
6
+ * and Codex (`~/.codex/session_index.jsonl` + `sessions/YYYY/MM/DD/rollout-*.jsonl`)
7
+ * sessions using cheap scandir + stat + BOUNDED tail/head reads only —
8
+ * it NEVER full-parses a multi-MB transcript;
9
+ * 2. summarizes them into a compact `AgentSession[]` payload with
10
+ * `origin="outpost"`, mirroring the local reader logic from US-002/US-003;
11
+ * 3. publishes that payload to the realtime fabric topic `hq/{personUid}/sessions`
12
+ * using the same on-box credential pattern the rest of the box uses
13
+ * (a server-minted, per-identity-scoped STS session vended by
14
+ * `POST /v1/realtime/credentials`, then an MQTT-over-WSS publish).
15
+ *
16
+ * Security (US-009 acceptance): the payload carries ONLY the AgentSession
17
+ * fields below — never a transcript body, prompt, token, API key, env var, or
18
+ * credential. `assertNoSecretsInPayload` is the runtime guard, and the unit
19
+ * tests assert the no-secrets-in-payload guarantee against adversarial
20
+ * fixtures.
21
+ *
22
+ * This module is intentionally dependency-light and pure-logic where it can be:
23
+ * the filesystem, clock, and publish transport are all injected so the
24
+ * enumeration → payload mapping and the no-secrets guarantee are unit-testable
25
+ * without a real VM, real MQTT, or real STS.
26
+ */
27
+ /** Which agent tool produced the session. */
28
+ export type AgentTool = "claude" | "codex";
29
+ /** Where the session physically lives. The outpost emitter always emits `outpost`. */
30
+ export type AgentOrigin = "local" | "outpost";
31
+ /**
32
+ * Session liveness taxonomy (US-001). Derived best-effort from a last-activity
33
+ * mtime window. `awaiting_input` is not inferable from on-disk artifacts alone
34
+ * on the box, so the emitter only ever produces `running | idle | ended`; the
35
+ * desktop merges/cross-checks and may surface `awaiting_input` for local PIDs.
36
+ */
37
+ export type AgentStatus = "running" | "awaiting_input" | "idle" | "ended";
38
+ /**
39
+ * Unified, compact agent session summary. This is the ONLY shape that crosses
40
+ * the wire — no transcript bodies, no secrets. Matches the Rust struct +
41
+ * TS type defined in the hq-sync repo (US-001).
42
+ */
43
+ export interface AgentSession {
44
+ /** Stable session id (the `<uuid>` for Claude, the rollout/index id for Codex). */
45
+ id: string;
46
+ tool: AgentTool;
47
+ origin: AgentOrigin;
48
+ /** Working directory the session is running in, if known. */
49
+ cwd: string | null;
50
+ /** Project slug/name (last path segment of cwd, or decoded Claude project dir). */
51
+ project: string | null;
52
+ /** Owning company slug, if resolvable from HQ workspace metadata. */
53
+ company: string | null;
54
+ /** Model id last seen for the session, if observed in a bounded read. */
55
+ model: string | null;
56
+ status: AgentStatus;
57
+ /** ISO-8601 first-seen / creation time, if known. */
58
+ startedAt: string | null;
59
+ /** ISO-8601 last-activity time (file mtime is the liveness signal). */
60
+ lastActivityAt: string | null;
61
+ /** Provenance of this record — the on-box file we summarized. Path only, never content. */
62
+ source: string;
63
+ }
64
+ /** Liveness thresholds (seconds). Mirrors the desktop liveness engine (US-004). */
65
+ export interface LivenessThresholds {
66
+ /** ≤ this since last activity ⇒ `running`. */
67
+ runningWithinSeconds: number;
68
+ /** ≤ this (and > running) ⇒ `idle`; beyond ⇒ `ended`. */
69
+ idleWithinSeconds: number;
70
+ }
71
+ /**
72
+ * Default cadence matches the desktop polling interval (~5s). Configurable via
73
+ * the `OUTPOST_SESSIONS_HEARTBEAT_INTERVAL_SECONDS` env var so dev/staging can
74
+ * dial it without a rebuild — read by `resolveCadenceSeconds`.
75
+ */
76
+ export declare const DEFAULT_HEARTBEAT_INTERVAL_SECONDS = 5;
77
+ /**
78
+ * Default liveness windows. `running` ⇐ activity within the last 2 cadence
79
+ * ticks (10s); `idle` out to 15m; older ⇒ `ended`. Kept generous so a session
80
+ * mid-think between writes isn't flapped to `ended`.
81
+ */
82
+ export declare const DEFAULT_LIVENESS_THRESHOLDS: LivenessThresholds;
83
+ /** A directory entry as returned by the filesystem port. */
84
+ export interface DirEntry {
85
+ name: string;
86
+ isDirectory: boolean;
87
+ isFile: boolean;
88
+ }
89
+ /** Minimal stat surface used by the enumerator. */
90
+ export interface FileStat {
91
+ /** Last-modification time, ms since epoch. */
92
+ mtimeMs: number;
93
+ /** Birth/creation time, ms since epoch (may equal mtime on filesystems w/o btime). */
94
+ birthtimeMs: number;
95
+ size: number;
96
+ }
97
+ /**
98
+ * Filesystem port — abstracts node:fs so tests drive an in-memory tree and the
99
+ * real emitter uses `nodeFileSystem`. Every read here is bounded.
100
+ */
101
+ export interface FileSystemPort {
102
+ /** Returns [] when the dir is missing — enumeration must not throw on absence. */
103
+ readDir(path: string): Promise<DirEntry[]>;
104
+ stat(path: string): Promise<FileStat>;
105
+ /** Whole-file read — used ONLY for the tiny Codex index, never for transcripts. */
106
+ readTextFile(path: string): Promise<string>;
107
+ /**
108
+ * Bounded read: at most `maxBytes` from the END of the file (tail) or the
109
+ * START (head). Implementations MUST NOT load the whole file. Returns "" on
110
+ * any error (missing/locked) — enumeration is best-effort.
111
+ */
112
+ readBounded(path: string, maxBytes: number, from: "head" | "tail"): Promise<string>;
113
+ }
114
+ /** Publishes the compact payload to the realtime topic. Injected for tests. */
115
+ export type PublishPort = (topic: string, payload: SessionsHeartbeatPayload) => Promise<void>;
116
+ /** The full envelope published to `hq/{personUid}/sessions`. */
117
+ export interface SessionsHeartbeatPayload {
118
+ /** Schema discriminator for the desktop subscriber. */
119
+ type: "sessions";
120
+ /** Always `outpost` from this emitter. */
121
+ origin: "outpost";
122
+ /** ISO-8601 emit time. */
123
+ emittedAt: string;
124
+ /** The compact session summaries — live only, newest first, size-bounded. */
125
+ sessions: AgentSession[];
126
+ /**
127
+ * How many sessions the box actually has on disk, including the `ended`
128
+ * archive that is deliberately not published. Present so a consumer can tell
129
+ * "this box has 15 sessions" from "this box has 9,340 and we sent the live
130
+ * 15" — a filtered list that looks complete is worse than no list.
131
+ */
132
+ totalSessions?: number;
133
+ /** True when the byte budget forced sessions to be dropped. */
134
+ truncated?: boolean;
135
+ }
136
+ /**
137
+ * Serialized-payload ceiling, in bytes.
138
+ *
139
+ * AWS IoT Core hard-rejects publishes over 128 KiB (131,072) — the box's first
140
+ * real heartbeat died on exactly that. This budget sits under it with headroom
141
+ * for the envelope and for any field a future schema adds.
142
+ */
143
+ export declare const IOT_PAYLOAD_BUDGET_BYTES: number;
144
+ export interface HeartbeatConfig {
145
+ /** Caller's canonical HQ person id (`prs_*`). Topic = `hq/{personUid}/sessions`. */
146
+ personUid: string;
147
+ /** Home directory to scan (defaults to the process HOME). */
148
+ home?: string;
149
+ /** Liveness thresholds (defaults to {@link DEFAULT_LIVENESS_THRESHOLDS}). */
150
+ thresholds?: LivenessThresholds;
151
+ /** Clock injection for deterministic tests. */
152
+ now?: () => Date;
153
+ }
154
+ export interface HeartbeatDeps {
155
+ fs: FileSystemPort;
156
+ publish: PublishPort;
157
+ }
158
+ /** The sessions topic for a person. `hq/{personUid}/sessions`. */
159
+ export declare function sessionsTopicForPerson(personUid: string): string;
160
+ /** Resolve the heartbeat cadence (seconds) from env, clamped to a sane floor. */
161
+ export declare function resolveCadenceSeconds(env?: NodeJS.ProcessEnv): number;
162
+ /**
163
+ * Map an mtime to a status given the thresholds and `now`. On the box we have
164
+ * no per-session PID cross-check (that's the desktop's job), so we only emit
165
+ * `running | idle | ended` — the desktop refines from there.
166
+ */
167
+ export declare function deriveStatus(lastActivityMs: number, nowMs: number, thresholds?: LivenessThresholds): AgentStatus;
168
+ /** Decode Claude's `-`-joined project dir back to a best-effort cwd. */
169
+ export declare function decodeClaudeProjectDir(dirName: string): string;
170
+ interface CodexIndexRecord {
171
+ id: string;
172
+ cwd: string | null;
173
+ model: string | null;
174
+ timestamp: string | null;
175
+ /** Relative path under ~/.codex, when the index records it. */
176
+ path: string | null;
177
+ }
178
+ /** Parse the small newline-delimited Codex index into records. */
179
+ export declare function parseCodexIndex(text: string): CodexIndexRecord[];
180
+ /**
181
+ * Project an arbitrary session-like object down to EXACTLY the whitelisted
182
+ * AgentSession fields. Any extra key (e.g. a transcript snippet, token, env
183
+ * var) is dropped here — this is the structural half of the no-secrets
184
+ * guarantee.
185
+ */
186
+ export declare function toCompactSession(s: AgentSession): AgentSession;
187
+ /**
188
+ * Runtime guard: throw if the payload carries any non-whitelisted key OR any
189
+ * value that looks like a secret. The behavioral half of the no-secrets
190
+ * guarantee — defense in depth on top of `toCompactSession`. Called before
191
+ * every publish.
192
+ */
193
+ export declare function assertNoSecretsInPayload(payload: SessionsHeartbeatPayload): void;
194
+ /**
195
+ * Enumerate the box's Claude + Codex sessions and build the compact,
196
+ * secret-free payload. Pure w.r.t. the injected fs/clock — does NOT publish.
197
+ */
198
+ export declare function collectSessions(config: HeartbeatConfig, deps: Pick<HeartbeatDeps, "fs">): Promise<SessionsHeartbeatPayload>;
199
+ /**
200
+ * One heartbeat tick: collect → guard → publish to `hq/{personUid}/sessions`.
201
+ * Best-effort and non-fatal by contract — a publish failure must not crash the
202
+ * box's heartbeat loop (the desktop falls back to the S3-vault heartbeat /
203
+ * stale-timeout, US-011). Returns the payload that was published (or attempted)
204
+ * so callers/tests can assert on it; re-throws nothing.
205
+ */
206
+ export declare function emitHeartbeatOnce(config: HeartbeatConfig, deps: HeartbeatDeps): Promise<SessionsHeartbeatPayload>;
207
+ /** Production FileSystemPort backed by node:fs with bounded positioned reads. */
208
+ export declare const nodeFileSystem: FileSystemPort;
209
+ export {};
210
+ //# sourceMappingURL=session-heartbeat.d.ts.map