@indigoai-us/hq-cli 5.101.5 → 5.101.7
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.
- package/CHANGELOG.md +34 -0
- package/dist/commands/mesh.d.ts +12 -0
- package/dist/commands/mesh.js +173 -0
- package/dist/commands/outposts.d.ts +18 -232
- package/dist/commands/outposts.js +27 -1290
- package/dist/lib/mesh/api.d.ts +88 -0
- package/dist/lib/mesh/api.js +319 -0
- package/dist/lib/mesh/cache.d.ts +10 -0
- package/dist/lib/mesh/cache.js +42 -0
- package/dist/main.js +4 -0
- package/package.json +9 -3
- package/dist/commands/outposts-heartbeat.d.ts +0 -96
- package/dist/commands/outposts-heartbeat.js +0 -188
- package/dist/outpost/session-heartbeat-publisher.d.ts +0 -76
- package/dist/outpost/session-heartbeat-publisher.js +0 -117
- package/dist/outpost/session-heartbeat.d.ts +0 -210
- package/dist/outpost/session-heartbeat.js +0 -657
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `hq outposts heartbeat` — publish this box's agent-session summary to the
|
|
3
|
-
* realtime fabric.
|
|
4
|
-
*
|
|
5
|
-
* Runs ON an Outpost, under systemd. It enumerates the box's local Claude Code
|
|
6
|
-
* and Codex sessions with cheap scandir + stat + bounded reads, projects them
|
|
7
|
-
* down to the compact secret-free `AgentSession[]` payload, and publishes to
|
|
8
|
-
* `hq/{personUid}/sessions` so Mission Control can see what the box is doing.
|
|
9
|
-
*
|
|
10
|
-
* ## Why the cadence lives in here
|
|
11
|
-
*
|
|
12
|
-
* The original box-side design was a bash `while` loop that, every 5 seconds,
|
|
13
|
-
* ran `hq auth refresh` and then `npx -y --package=@indigoai-us/hq-cloud@latest
|
|
14
|
-
* outpost-session-heartbeat-runner`. That is ~35k process launches a day, and
|
|
15
|
-
* every `npx` re-resolves the package and re-reads it off disk — on a t3 box
|
|
16
|
-
* whose EBS budget is the scarce resource, that is a meaningful, permanent tax
|
|
17
|
-
* for a job that should be nearly free.
|
|
18
|
-
*
|
|
19
|
-
* So the loop lives in-process instead:
|
|
20
|
-
* - identity is resolved ONCE per process, not once per tick;
|
|
21
|
-
* - the Cognito session refreshes itself on demand (`ensureCognitoToken`
|
|
22
|
-
* returns the cached token until it is close to expiry), so there is no
|
|
23
|
-
* separate per-tick `hq auth refresh`;
|
|
24
|
-
* - the IoT client is cached across ticks by the publisher and only rebuilt
|
|
25
|
-
* when its credentials near expiry.
|
|
26
|
-
*
|
|
27
|
-
* The loop is deliberately unkillable-by-transients: a failed publish, a failed
|
|
28
|
-
* enumeration, or a not-yet-warm session are all logged and retried on the next
|
|
29
|
-
* tick. Nothing here should ever take the service down, because a service that
|
|
30
|
-
* exits looks identical to a service that is quietly failing.
|
|
31
|
-
*/
|
|
32
|
-
import { mkdir, writeFile } from "node:fs/promises";
|
|
33
|
-
import { homedir } from "node:os";
|
|
34
|
-
import { dirname, join } from "node:path";
|
|
35
|
-
import { ensureCognitoToken } from "../utils/cognito-session.js";
|
|
36
|
-
import { DEFAULT_VAULT_API_URL } from "../utils/cognito-session.js";
|
|
37
|
-
import { resolveCallerPersonUid } from "../utils/vault-api.js";
|
|
38
|
-
import { DEFAULT_HEARTBEAT_INTERVAL_SECONDS, collectSessions, nodeFileSystem, sessionsTopicForPerson, } from "../outpost/session-heartbeat.js";
|
|
39
|
-
import { createIotPublishPort, defaultRealtimeCredentialsFetcher, } from "../outpost/session-heartbeat-publisher.js";
|
|
40
|
-
/**
|
|
41
|
-
* Resolve the cadence from the flag, then the environment, then the default.
|
|
42
|
-
* Floored at 1s: a sub-second cadence hammers the fabric for no benefit, and a
|
|
43
|
-
* zero/negative value would spin the loop hot.
|
|
44
|
-
*/
|
|
45
|
-
export function resolveIntervalSeconds(flag, env = process.env) {
|
|
46
|
-
for (const raw of [flag, env.OUTPOST_SESSIONS_HEARTBEAT_INTERVAL_SECONDS]) {
|
|
47
|
-
if (raw === undefined || raw === null || raw === "")
|
|
48
|
-
continue;
|
|
49
|
-
const parsed = Number.parseInt(raw, 10);
|
|
50
|
-
if (!Number.isFinite(parsed))
|
|
51
|
-
return DEFAULT_HEARTBEAT_INTERVAL_SECONDS;
|
|
52
|
-
return Math.max(1, parsed);
|
|
53
|
-
}
|
|
54
|
-
return DEFAULT_HEARTBEAT_INTERVAL_SECONDS;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Where the box records its last LANDED heartbeat. The post-provision audit
|
|
58
|
-
* reads this file's freshness, because "systemd says the unit is active" is not
|
|
59
|
-
* evidence that anything was published — that gap is what let a heartbeat fail
|
|
60
|
-
* every five seconds for nine days while every dashboard stayed green.
|
|
61
|
-
*
|
|
62
|
-
* Lives under the service user's home so the unit (User=ec2-user) can write it
|
|
63
|
-
* without extra tmpfiles.d wiring; the SSM collector reads it as root.
|
|
64
|
-
*/
|
|
65
|
-
export const DEFAULT_HEARTBEAT_STATE_FILE = join(homedir(), ".hq", "outpost-session-heartbeat.json");
|
|
66
|
-
/** Persist the liveness marker the box audit reads. */
|
|
67
|
-
export async function writeHeartbeatState(path, payload) {
|
|
68
|
-
await mkdir(dirname(path), { recursive: true });
|
|
69
|
-
await writeFile(path, `${JSON.stringify({
|
|
70
|
-
lastPublishAt: payload.emittedAt,
|
|
71
|
-
sessions: payload.sessions.length,
|
|
72
|
-
})}\n`, "utf8");
|
|
73
|
-
}
|
|
74
|
-
/** Structured one-line log — journald gets JSON, not prose. */
|
|
75
|
-
function logLine(step, outcome, extra, now) {
|
|
76
|
-
return JSON.stringify({
|
|
77
|
-
service: "outpost-session-heartbeat",
|
|
78
|
-
step,
|
|
79
|
-
outcome,
|
|
80
|
-
...extra,
|
|
81
|
-
timestamp: now().toISOString(),
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Run the heartbeat loop. Returns the number of ticks performed.
|
|
86
|
-
*
|
|
87
|
-
* A "tick" is one attempt, whether or not it published — the count is the
|
|
88
|
-
* loop's liveness, not its success rate.
|
|
89
|
-
*/
|
|
90
|
-
export async function runHeartbeatLoop(options, deps) {
|
|
91
|
-
const intervalMs = (options.intervalSeconds ?? DEFAULT_HEARTBEAT_INTERVAL_SECONDS) * 1000;
|
|
92
|
-
const maxTicks = options.once ? 1 : options.maxTicks;
|
|
93
|
-
// Resolved once and reused. Re-resolving per tick was the original sin.
|
|
94
|
-
let personUid = null;
|
|
95
|
-
let ticks = 0;
|
|
96
|
-
for (;;) {
|
|
97
|
-
ticks += 1;
|
|
98
|
-
try {
|
|
99
|
-
if (!personUid)
|
|
100
|
-
personUid = await deps.getPersonUid();
|
|
101
|
-
const payload = await collectSessions({ personUid, home: options.home, now: deps.now }, { fs: deps.fs });
|
|
102
|
-
const topic = sessionsTopicForPerson(personUid);
|
|
103
|
-
await deps.publish(topic, payload);
|
|
104
|
-
// Ordering matters: the marker is written only once the publish has
|
|
105
|
-
// resolved, so a stale marker means "nothing landed", not "nothing ran".
|
|
106
|
-
await deps.onPublished?.(payload);
|
|
107
|
-
if (options.json)
|
|
108
|
-
deps.log(JSON.stringify(payload));
|
|
109
|
-
deps.log(logLine("publish", "ok", { topic, sessions: payload.sessions.length }, deps.now));
|
|
110
|
-
}
|
|
111
|
-
catch (err) {
|
|
112
|
-
// Every failure mode lands here on purpose: a transient must never take
|
|
113
|
-
// the service down, because systemd restarting a hot-failing loop looks
|
|
114
|
-
// exactly like a healthy one.
|
|
115
|
-
deps.log(logLine("tick", "error", { message: err instanceof Error ? err.message : String(err) }, deps.now));
|
|
116
|
-
}
|
|
117
|
-
if (options.once)
|
|
118
|
-
break;
|
|
119
|
-
if (maxTicks !== undefined && ticks >= maxTicks)
|
|
120
|
-
break;
|
|
121
|
-
if (options.signal?.aborted)
|
|
122
|
-
break;
|
|
123
|
-
await deps.sleep(intervalMs, options.signal);
|
|
124
|
-
if (options.signal?.aborted)
|
|
125
|
-
break;
|
|
126
|
-
}
|
|
127
|
-
return ticks;
|
|
128
|
-
}
|
|
129
|
-
/** Wire the production dependencies and register the subcommand. */
|
|
130
|
-
export function registerHeartbeatCommand(outposts) {
|
|
131
|
-
outposts
|
|
132
|
-
.command("heartbeat", { hidden: true })
|
|
133
|
-
.description("Publish this box's agent-session summary to the realtime fabric (runs on the Outpost, under systemd)")
|
|
134
|
-
.option("--once", "Emit a single heartbeat and exit")
|
|
135
|
-
.option("--interval <seconds>", "Cadence in seconds (default 5; env OUTPOST_SESSIONS_HEARTBEAT_INTERVAL_SECONDS)")
|
|
136
|
-
.option("--home <path>", "Home directory to scan for sessions")
|
|
137
|
-
.option("--api-base-url <url>", "HQ API base URL")
|
|
138
|
-
.option("--state-file <path>", `Liveness marker read by the box audit (default ${DEFAULT_HEARTBEAT_STATE_FILE})`)
|
|
139
|
-
.option("--json", "Print each published payload as JSON")
|
|
140
|
-
.action(async (opts) => {
|
|
141
|
-
const apiBaseUrl = opts.apiBaseUrl ??
|
|
142
|
-
process.env.HQAPI_BASE_URL ??
|
|
143
|
-
DEFAULT_VAULT_API_URL;
|
|
144
|
-
const getJwt = () => ensureCognitoToken({ interactive: false });
|
|
145
|
-
const deps = {
|
|
146
|
-
fs: nodeFileSystem,
|
|
147
|
-
publish: createIotPublishPort({
|
|
148
|
-
fetchCredentials: defaultRealtimeCredentialsFetcher({
|
|
149
|
-
apiBaseUrl,
|
|
150
|
-
getJwt,
|
|
151
|
-
}),
|
|
152
|
-
}),
|
|
153
|
-
// Same base as the credentials fetch. Resolving identity against the
|
|
154
|
-
// default plane while vending credentials from another sends a
|
|
155
|
-
// deployment-specific token to the wrong control plane, which is
|
|
156
|
-
// rejected — and the loop then retries forever without publishing.
|
|
157
|
-
getPersonUid: async () => resolveCallerPersonUid(await getJwt(), apiBaseUrl),
|
|
158
|
-
sleep: (ms, signal) => new Promise((resolve) => {
|
|
159
|
-
if (signal?.aborted)
|
|
160
|
-
return resolve();
|
|
161
|
-
const t = setTimeout(done, ms);
|
|
162
|
-
function done() {
|
|
163
|
-
clearTimeout(t);
|
|
164
|
-
signal?.removeEventListener("abort", done);
|
|
165
|
-
resolve();
|
|
166
|
-
}
|
|
167
|
-
signal?.addEventListener("abort", done, { once: true });
|
|
168
|
-
}),
|
|
169
|
-
now: () => new Date(),
|
|
170
|
-
log: (line) => console.error(line),
|
|
171
|
-
onPublished: (payload) => writeHeartbeatState(opts.stateFile ?? DEFAULT_HEARTBEAT_STATE_FILE, payload),
|
|
172
|
-
};
|
|
173
|
-
// systemd sends SIGTERM on stop/restart — finish the in-flight tick,
|
|
174
|
-
// then exit cleanly rather than being killed mid-publish.
|
|
175
|
-
const controller = new AbortController();
|
|
176
|
-
const stop = () => controller.abort();
|
|
177
|
-
process.once("SIGTERM", stop);
|
|
178
|
-
process.once("SIGINT", stop);
|
|
179
|
-
await runHeartbeatLoop({
|
|
180
|
-
once: opts.once,
|
|
181
|
-
intervalSeconds: resolveIntervalSeconds(opts.interval),
|
|
182
|
-
home: opts.home,
|
|
183
|
-
json: opts.json,
|
|
184
|
-
signal: controller.signal,
|
|
185
|
-
}, deps);
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
//# sourceMappingURL=outposts-heartbeat.js.map
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Outpost on-box realtime publisher — mission-control US-009.
|
|
3
|
-
*
|
|
4
|
-
* The box already holds a Cognito session (seeded from the caller's refresh
|
|
5
|
-
* token in user-data, kept fresh by the box's auth timers — see provision.ts).
|
|
6
|
-
* This module turns that session into a `PublishPort` for the session-heartbeat
|
|
7
|
-
* emitter using the EXACT on-box credential pattern the rest of the realtime
|
|
8
|
-
* fabric uses (docs/realtime-fabric.md):
|
|
9
|
-
*
|
|
10
|
-
* 1. POST {HQAPI}/v1/realtime/credentials with the box's Cognito JWT.
|
|
11
|
-
* The Lambda resolves the caller's `personUid` from the verified JWT
|
|
12
|
-
* (never request input) and vends short-lived STS creds whose session
|
|
13
|
-
* policy scopes `iot:Connect/Publish/...` to `hq/{personUid}/*` only.
|
|
14
|
-
* 2. SigV4-sign an IoT Data-plane publish with those creds to
|
|
15
|
-
* `hq/{personUid}/sessions`.
|
|
16
|
-
*
|
|
17
|
-
* No new auth surface, no embedded long-lived key, no per-device cert — the
|
|
18
|
-
* per-identity STS session policy is the isolation boundary (US-010).
|
|
19
|
-
*
|
|
20
|
-
* The HTTP fetch + IoT client are injected so this is unit-testable without a
|
|
21
|
-
* live endpoint; `defaultRealtimeCredentialsFetcher` and the IoT publish are
|
|
22
|
-
* the production wiring.
|
|
23
|
-
*/
|
|
24
|
-
import { IoTDataPlaneClient } from "@aws-sdk/client-iot-data-plane";
|
|
25
|
-
import type { PublishPort } from "./session-heartbeat.js";
|
|
26
|
-
import { sessionsTopicForPerson } from "./session-heartbeat.js";
|
|
27
|
-
/** Shape returned by `POST /v1/realtime/credentials` (mirrors the handler). */
|
|
28
|
-
export interface RealtimeCredentialsResponse {
|
|
29
|
-
credentials: {
|
|
30
|
-
accessKeyId: string;
|
|
31
|
-
secretAccessKey: string;
|
|
32
|
-
sessionToken: string;
|
|
33
|
-
expiration: string;
|
|
34
|
-
};
|
|
35
|
-
iotEndpoint: string;
|
|
36
|
-
region: string;
|
|
37
|
-
/** The caller's own topic — `hq/{personUid}/...`. */
|
|
38
|
-
topic: string;
|
|
39
|
-
expiresAt: string;
|
|
40
|
-
}
|
|
41
|
-
/** Fetches scoped realtime credentials for the box. Injected for tests. */
|
|
42
|
-
export type RealtimeCredentialsFetcher = () => Promise<RealtimeCredentialsResponse>;
|
|
43
|
-
/** Ceiling on a single credentials request. */
|
|
44
|
-
export declare const DEFAULT_CREDENTIALS_TIMEOUT_MS = 10000;
|
|
45
|
-
/**
|
|
46
|
-
* Build the production credentials fetcher. Reads the box's current Cognito
|
|
47
|
-
* id/access token via the injected `getJwt` and POSTs it to the
|
|
48
|
-
* realtime-credentials endpoint.
|
|
49
|
-
*/
|
|
50
|
-
export declare function defaultRealtimeCredentialsFetcher(opts: {
|
|
51
|
-
apiBaseUrl: string;
|
|
52
|
-
getJwt: () => Promise<string>;
|
|
53
|
-
fetchImpl?: typeof fetch;
|
|
54
|
-
/** Bound the request. Defaults to {@link DEFAULT_CREDENTIALS_TIMEOUT_MS}. */
|
|
55
|
-
timeoutMs?: number;
|
|
56
|
-
}): RealtimeCredentialsFetcher;
|
|
57
|
-
/**
|
|
58
|
-
* Create a `PublishPort` that vends scoped creds (refreshing before expiry) and
|
|
59
|
-
* publishes the compact payload to the box's own sessions topic over MQTT/IoT.
|
|
60
|
-
*
|
|
61
|
-
* @param fetchCredentials vends per-identity-scoped STS creds + IoT endpoint
|
|
62
|
-
* @param makeClient builds an IoT client from creds (injected for tests)
|
|
63
|
-
* @param now clock injection
|
|
64
|
-
*/
|
|
65
|
-
export declare function createIotPublishPort(opts: {
|
|
66
|
-
fetchCredentials: RealtimeCredentialsFetcher;
|
|
67
|
-
makeClient?: (args: {
|
|
68
|
-
endpoint: string;
|
|
69
|
-
region: string;
|
|
70
|
-
credentials: RealtimeCredentialsResponse["credentials"];
|
|
71
|
-
}) => IoTDataPlaneClient;
|
|
72
|
-
now?: () => Date;
|
|
73
|
-
}): PublishPort;
|
|
74
|
-
/** Re-export for the runner so it imports one module. */
|
|
75
|
-
export { sessionsTopicForPerson };
|
|
76
|
-
//# sourceMappingURL=session-heartbeat-publisher.d.ts.map
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Outpost on-box realtime publisher — mission-control US-009.
|
|
3
|
-
*
|
|
4
|
-
* The box already holds a Cognito session (seeded from the caller's refresh
|
|
5
|
-
* token in user-data, kept fresh by the box's auth timers — see provision.ts).
|
|
6
|
-
* This module turns that session into a `PublishPort` for the session-heartbeat
|
|
7
|
-
* emitter using the EXACT on-box credential pattern the rest of the realtime
|
|
8
|
-
* fabric uses (docs/realtime-fabric.md):
|
|
9
|
-
*
|
|
10
|
-
* 1. POST {HQAPI}/v1/realtime/credentials with the box's Cognito JWT.
|
|
11
|
-
* The Lambda resolves the caller's `personUid` from the verified JWT
|
|
12
|
-
* (never request input) and vends short-lived STS creds whose session
|
|
13
|
-
* policy scopes `iot:Connect/Publish/...` to `hq/{personUid}/*` only.
|
|
14
|
-
* 2. SigV4-sign an IoT Data-plane publish with those creds to
|
|
15
|
-
* `hq/{personUid}/sessions`.
|
|
16
|
-
*
|
|
17
|
-
* No new auth surface, no embedded long-lived key, no per-device cert — the
|
|
18
|
-
* per-identity STS session policy is the isolation boundary (US-010).
|
|
19
|
-
*
|
|
20
|
-
* The HTTP fetch + IoT client are injected so this is unit-testable without a
|
|
21
|
-
* live endpoint; `defaultRealtimeCredentialsFetcher` and the IoT publish are
|
|
22
|
-
* the production wiring.
|
|
23
|
-
*/
|
|
24
|
-
import { IoTDataPlaneClient, PublishCommand, } from "@aws-sdk/client-iot-data-plane";
|
|
25
|
-
import { assertNoSecretsInPayload, sessionsTopicForPerson, } from "./session-heartbeat.js";
|
|
26
|
-
/** Ceiling on a single credentials request. */
|
|
27
|
-
export const DEFAULT_CREDENTIALS_TIMEOUT_MS = 10_000;
|
|
28
|
-
/**
|
|
29
|
-
* Build the production credentials fetcher. Reads the box's current Cognito
|
|
30
|
-
* id/access token via the injected `getJwt` and POSTs it to the
|
|
31
|
-
* realtime-credentials endpoint.
|
|
32
|
-
*/
|
|
33
|
-
export function defaultRealtimeCredentialsFetcher(opts) {
|
|
34
|
-
const doFetch = opts.fetchImpl ?? fetch;
|
|
35
|
-
return async () => {
|
|
36
|
-
const jwt = await opts.getJwt();
|
|
37
|
-
const res = await doFetch(`${opts.apiBaseUrl}/v1/realtime/credentials`, {
|
|
38
|
-
method: "POST",
|
|
39
|
-
// Bounded. An endpoint that accepts the connection but never answers
|
|
40
|
-
// would otherwise park the tick forever: the loop stops beating, the
|
|
41
|
-
// liveness marker goes stale, and SIGTERM cannot finish the in-flight
|
|
42
|
-
// tick — a hang that reads exactly like a healthy quiet box.
|
|
43
|
-
signal: AbortSignal.timeout(opts.timeoutMs ?? DEFAULT_CREDENTIALS_TIMEOUT_MS),
|
|
44
|
-
headers: {
|
|
45
|
-
authorization: `Bearer ${jwt}`,
|
|
46
|
-
"content-type": "application/json",
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
if (!res.ok) {
|
|
50
|
-
throw new Error(`realtime/credentials returned ${res.status} ${res.statusText}`);
|
|
51
|
-
}
|
|
52
|
-
return (await res.json());
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Create a `PublishPort` that vends scoped creds (refreshing before expiry) and
|
|
57
|
-
* publishes the compact payload to the box's own sessions topic over MQTT/IoT.
|
|
58
|
-
*
|
|
59
|
-
* @param fetchCredentials vends per-identity-scoped STS creds + IoT endpoint
|
|
60
|
-
* @param makeClient builds an IoT client from creds (injected for tests)
|
|
61
|
-
* @param now clock injection
|
|
62
|
-
*/
|
|
63
|
-
export function createIotPublishPort(opts) {
|
|
64
|
-
const now = opts.now ?? (() => new Date());
|
|
65
|
-
const makeClient = opts.makeClient ??
|
|
66
|
-
(({ endpoint, region, credentials }) => {
|
|
67
|
-
const url = endpoint.startsWith("http")
|
|
68
|
-
? endpoint
|
|
69
|
-
: `https://${endpoint}`;
|
|
70
|
-
return new IoTDataPlaneClient({
|
|
71
|
-
endpoint: url,
|
|
72
|
-
region,
|
|
73
|
-
credentials: {
|
|
74
|
-
accessKeyId: credentials.accessKeyId,
|
|
75
|
-
secretAccessKey: credentials.secretAccessKey,
|
|
76
|
-
sessionToken: credentials.sessionToken,
|
|
77
|
-
},
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
let cached = null;
|
|
81
|
-
// Refresh creds this many ms before they actually expire so a publish never
|
|
82
|
-
// races expiry mid-flight.
|
|
83
|
-
const REFRESH_SKEW_MS = 60_000;
|
|
84
|
-
async function clientFor() {
|
|
85
|
-
const nowMs = now().getTime();
|
|
86
|
-
if (cached && cached.expiresAtMs - REFRESH_SKEW_MS > nowMs) {
|
|
87
|
-
return { client: cached.client };
|
|
88
|
-
}
|
|
89
|
-
const vended = await opts.fetchCredentials();
|
|
90
|
-
const client = makeClient({
|
|
91
|
-
endpoint: vended.iotEndpoint,
|
|
92
|
-
region: vended.region,
|
|
93
|
-
credentials: vended.credentials,
|
|
94
|
-
});
|
|
95
|
-
cached = {
|
|
96
|
-
client,
|
|
97
|
-
endpoint: vended.iotEndpoint,
|
|
98
|
-
accessKeyId: vended.credentials.accessKeyId,
|
|
99
|
-
expiresAtMs: Date.parse(vended.credentials.expiration),
|
|
100
|
-
};
|
|
101
|
-
return { client };
|
|
102
|
-
}
|
|
103
|
-
return async (topic, payload) => {
|
|
104
|
-
// Re-guard at the transport boundary — a publisher must never ship a
|
|
105
|
-
// payload that fails the no-secrets contract, regardless of caller.
|
|
106
|
-
assertNoSecretsInPayload(payload);
|
|
107
|
-
const { client } = await clientFor();
|
|
108
|
-
await client.send(new PublishCommand({
|
|
109
|
-
topic,
|
|
110
|
-
qos: 0,
|
|
111
|
-
payload: Buffer.from(JSON.stringify(payload)),
|
|
112
|
-
}));
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
/** Re-export for the runner so it imports one module. */
|
|
116
|
-
export { sessionsTopicForPerson };
|
|
117
|
-
//# sourceMappingURL=session-heartbeat-publisher.js.map
|
|
@@ -1,210 +0,0 @@
|
|
|
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
|