@indigoai-us/hq-cli 5.77.12 → 5.77.14

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.
Files changed (34) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/dist/commands/outposts-heartbeat.d.ts +96 -0
  3. package/dist/commands/outposts-heartbeat.js +188 -0
  4. package/dist/commands/outposts.js +3 -0
  5. package/dist/commands/pack-install.js +9 -0
  6. package/dist/commands/pkg-install.js +6 -0
  7. package/dist/commands/run.js +4 -0
  8. package/dist/commands/secrets.js +5 -0
  9. package/dist/outpost/session-heartbeat-publisher.d.ts +76 -0
  10. package/dist/outpost/session-heartbeat-publisher.js +117 -0
  11. package/dist/outpost/session-heartbeat.d.ts +210 -0
  12. package/dist/outpost/session-heartbeat.js +657 -0
  13. package/dist/utils/vault-api.d.ts +8 -1
  14. package/dist/utils/vault-api.js +3 -2
  15. package/package.json +3 -1
  16. package/src/commands/outposts-heartbeat.test.ts +299 -0
  17. package/src/commands/outposts-heartbeat.ts +310 -0
  18. package/src/commands/outposts.ts +4 -0
  19. package/src/commands/pack-install.ts +9 -0
  20. package/src/commands/packs-update-api-key.test.ts +105 -0
  21. package/src/commands/pkg-install.dispatch.test.ts +33 -1
  22. package/src/commands/pkg-install.ts +6 -0
  23. package/src/commands/run.ts +6 -0
  24. package/src/commands/secrets.test.ts +13 -0
  25. package/src/commands/secrets.ts +9 -0
  26. package/src/outpost/session-heartbeat-bounds.test.ts +195 -0
  27. package/src/outpost/session-heartbeat-guard.test.ts +105 -0
  28. package/src/outpost/session-heartbeat-publisher.test.ts +178 -0
  29. package/src/outpost/session-heartbeat-publisher.ts +186 -0
  30. package/src/outpost/session-heartbeat-retain-guard.test.ts +126 -0
  31. package/src/outpost/session-heartbeat.test.ts +459 -0
  32. package/src/outpost/session-heartbeat.ts +877 -0
  33. package/src/packaging.test.ts +45 -0
  34. package/src/utils/vault-api.ts +13 -2
package/CHANGELOG.md CHANGED
@@ -2,6 +2,54 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [5.77.14]
6
+
7
+ ### Fixed
8
+
9
+ - Extend `HQ_API_KEY` fail-closed: reject metadata-only `hq secrets get`
10
+ without `--reveal`, and hard-error `hq run` / `hq install` when a vault API
11
+ key is set (Cognito session required). (#273)
12
+ - Keep the Cognito-only gate on the top-level `hq install` route only — not
13
+ inside shared `installPack()` — so `hq packs update` cannot un-wire a pack
14
+ and then abort mid-flight under `HQ_API_KEY`. (#273)
15
+
16
+ ## [5.77.13]
17
+
18
+ ### Added
19
+
20
+ - `hq outposts heartbeat` — the Outpost session-heartbeat runner. The unit on
21
+ every box invoked `outpost-session-heartbeat-runner`, a command that was
22
+ never written in any package, so each tick logged `command not found` behind
23
+ a `|| echo … continuing` while systemd and the box audit both reported
24
+ healthy. The enumeration and publisher modules existed in hq-pro (a server
25
+ repo) with no entrypoint; they move here and the cadence runs in-process —
26
+ no bash loop, no per-tick `npx …@latest`, no per-tick `hq auth refresh`.
27
+ That removes ~35k process launches per box per day. (#274)
28
+
29
+ ### Fixed
30
+
31
+ - Heartbeat payloads no longer exceed the AWS IoT 128 KiB limit. A box with
32
+ 9,340 transcripts produced ~1.9 MB and was rejected outright; `ended`
33
+ sessions are no longer published and the remainder is bounded by serialized
34
+ bytes, newest first, with `totalSessions`/`truncated` on the envelope so a
35
+ filtered list cannot read as a complete one. (#274)
36
+ - Transcript bodies are read only for sessions that will be published — the
37
+ enumerator previously did a 16 KiB bounded read on every file before
38
+ filtering (~150 MB of disk per tick on a large box). (#274)
39
+ - The no-secrets guard no longer matches ordinary paths. `sk-` and `asia` were
40
+ bare substrings, so a directory named `task-runner` or `flask-app` made the
41
+ guard throw and silently stopped the box reporting; markers are now
42
+ credential-shaped. (#274)
43
+ - `nodeFileSystem.readDir` propagates operational errors instead of treating
44
+ every failure as an empty directory — an EACCES previously published an
45
+ empty session list and refreshed the liveness marker as if collection had
46
+ succeeded. (#274)
47
+ - The realtime-credentials request is bounded, and the cadence sleep races the
48
+ abort signal so SIGTERM stops the loop cooperatively. (#274)
49
+ - `vaultApiFetch` accepts an optional `baseUrl` so identity resolves against
50
+ the same control plane as the credentials fetch on non-default
51
+ deployments. (#274)
52
+
5
53
  ## [5.77.12]
6
54
 
7
55
  ### Added
@@ -0,0 +1,96 @@
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 type { Command } from "commander";
33
+ import { type FileSystemPort, type PublishPort, type SessionsHeartbeatPayload } from "../outpost/session-heartbeat.js";
34
+ /** Everything the loop touches, injected so it is testable without a box. */
35
+ export interface HeartbeatLoopDeps {
36
+ fs: FileSystemPort;
37
+ publish: PublishPort;
38
+ /** Resolve the caller's canonical `prs_*` id. Called once per process. */
39
+ getPersonUid: () => Promise<string>;
40
+ /**
41
+ * Wait between ticks. MUST return early when `signal` aborts — otherwise a
42
+ * SIGTERM arriving mid-sleep is not noticed until the full interval elapses,
43
+ * and systemd force-kills the process instead of it stopping cooperatively.
44
+ */
45
+ sleep: (ms: number, signal?: AbortSignal) => Promise<void>;
46
+ now: () => Date;
47
+ log: (line: string) => void;
48
+ /**
49
+ * Record that a heartbeat actually LANDED. Called only after a successful
50
+ * publish — never on failure, or the freshness signal it feeds would always
51
+ * read fresh and be worth nothing.
52
+ */
53
+ onPublished?: (payload: SessionsHeartbeatPayload) => Promise<void>;
54
+ }
55
+ export interface HeartbeatLoopOptions {
56
+ /** Emit a single heartbeat and return. */
57
+ once?: boolean;
58
+ /** Cadence between ticks. Defaults to {@link DEFAULT_HEARTBEAT_INTERVAL_SECONDS}. */
59
+ intervalSeconds?: number;
60
+ /** Home directory to scan (defaults to the process HOME). */
61
+ home?: string;
62
+ /** Print each published payload as JSON. */
63
+ json?: boolean;
64
+ /** Stop the loop cooperatively. */
65
+ signal?: AbortSignal;
66
+ /** Bound the loop — used by tests; production runs until aborted. */
67
+ maxTicks?: number;
68
+ }
69
+ /**
70
+ * Resolve the cadence from the flag, then the environment, then the default.
71
+ * Floored at 1s: a sub-second cadence hammers the fabric for no benefit, and a
72
+ * zero/negative value would spin the loop hot.
73
+ */
74
+ export declare function resolveIntervalSeconds(flag: string | undefined, env?: NodeJS.ProcessEnv): number;
75
+ /**
76
+ * Where the box records its last LANDED heartbeat. The post-provision audit
77
+ * reads this file's freshness, because "systemd says the unit is active" is not
78
+ * evidence that anything was published — that gap is what let a heartbeat fail
79
+ * every five seconds for nine days while every dashboard stayed green.
80
+ *
81
+ * Lives under the service user's home so the unit (User=ec2-user) can write it
82
+ * without extra tmpfiles.d wiring; the SSM collector reads it as root.
83
+ */
84
+ export declare const DEFAULT_HEARTBEAT_STATE_FILE: string;
85
+ /** Persist the liveness marker the box audit reads. */
86
+ export declare function writeHeartbeatState(path: string, payload: SessionsHeartbeatPayload): Promise<void>;
87
+ /**
88
+ * Run the heartbeat loop. Returns the number of ticks performed.
89
+ *
90
+ * A "tick" is one attempt, whether or not it published — the count is the
91
+ * loop's liveness, not its success rate.
92
+ */
93
+ export declare function runHeartbeatLoop(options: HeartbeatLoopOptions, deps: HeartbeatLoopDeps): Promise<number>;
94
+ /** Wire the production dependencies and register the subcommand. */
95
+ export declare function registerHeartbeatCommand(outposts: Command): void;
96
+ //# sourceMappingURL=outposts-heartbeat.d.ts.map
@@ -0,0 +1,188 @@
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
@@ -31,6 +31,7 @@ import * as yaml from "js-yaml";
31
31
  import { loadCachedTokens } from "@indigoai-us/hq-cloud";
32
32
  import { ensureCognitoToken } from "../utils/cognito-session.js";
33
33
  import { vaultApiFetch } from "../utils/vault-api.js";
34
+ import { registerHeartbeatCommand } from "./outposts-heartbeat.js";
34
35
  import { OUTPOST_PRICE_CENTS, confirmChargeOrExit, parseBillingPayload, surfaceBillingBlocked, } from "../utils/billing-gate.js";
35
36
  /** Decode hq-pro's `{ capped, limit, outposts }` cap envelope, if present. */
36
37
  export function parseCappedPayload(body) {
@@ -738,6 +739,8 @@ export function registerOutpostsCommand(program, selfDeployOverrides = {}) {
738
739
  const outposts = program
739
740
  .command("outposts")
740
741
  .description("Manage your personal HQ Outposts (EC2 boxes)");
742
+ // On-box session heartbeat (systemd). Hidden — operators never run it.
743
+ registerHeartbeatCommand(outposts);
741
744
  outposts
742
745
  .command("self-deploy", { hidden: true })
743
746
  .description("Configure this EC2 host as a locally self-hosted HQ outpost")
@@ -1613,6 +1613,15 @@ export function runScanPackages(hqRoot, opts = {}) {
1613
1613
  }
1614
1614
  }
1615
1615
  export async function installPack(source, opts = {}) {
1616
+ // NOTE: the HQ_API_KEY fail-closed gate is NOT enforced here. `installPack`
1617
+ // is a shared primitive called both by the top-level `hq install` CLI route
1618
+ // (where Cognito-only is the right gate — checked there instead) AND by
1619
+ // `hq packs update` (packs.ts:runUpdate), which un-wires an existing pack's
1620
+ // contributions BEFORE re-installing. Throwing from inside `installPack`
1621
+ // would leave an update mid-flight (unwired, not reinstalled) whenever
1622
+ // HQ_API_KEY happened to be set. Restricting the assertion to the install
1623
+ // entrypoint keeps `hq packs update` fail-closed at its own call site
1624
+ // instead (checked before it un-wires anything).
1616
1625
  const transport = classify(source);
1617
1626
  const hqRoot = findHqRoot();
1618
1627
  // `readHqVersion` (shared with `hq packs`) reads the CANONICAL
@@ -23,6 +23,7 @@ import { resolveDefaultHqRoot } from '../utils/cognito-session.js';
23
23
  import { getRegistryUrl, RegistryClient, } from '../utils/registry-client.js';
24
24
  import { verifySha256, verifyRsaSignature } from '../utils/integrity.js';
25
25
  import { addToRegistry } from '../utils/registry.js';
26
+ import { assertCognitoOnlyCommand } from '../utils/resolve-vault-credential.js';
26
27
  import { MARKETPLACE_PREFIX, installPack, sourceMatchesPackPattern, } from './pack-install.js';
27
28
  export function registerPackageInstallCommand(parent) {
28
29
  parent
@@ -35,6 +36,11 @@ export function registerPackageInstallCommand(parent) {
35
36
  .option('--branch', 'Follow a ref instead of SHA-pinning (git content-pack flow)')
36
37
  .action(async (source, opts) => {
37
38
  try {
39
+ // Gate the top-level `hq install` entrypoint only — `installPack`
40
+ // itself is also called by `hq packs update` (packs.ts), which must
41
+ // stay able to fail closed at its OWN call site (before it un-wires
42
+ // an existing pack) rather than mid-flight inside installPack.
43
+ assertCognitoOnlyCommand('hq install');
38
44
  if (sourceMatchesPackPattern(source)) {
39
45
  await installPack(source, {
40
46
  company: opts.company,
@@ -3,6 +3,7 @@ import * as path from 'node:path';
3
3
  import * as fs from 'node:fs';
4
4
  import { internal } from 'varlock';
5
5
  import { ensureCognitoToken } from '../utils/cognito-session.js';
6
+ import { peekHqApiKey } from '../utils/resolve-vault-credential.js';
6
7
  import { computeSha256 } from '../utils/integrity.js';
7
8
  import { vaultApiFetch, getCompanyUid } from '../utils/vault-api.js';
8
9
  import { discoverSchemas } from '../run/discover-schemas.js';
@@ -38,6 +39,9 @@ export function registerRunCommand(program) {
38
39
  .allowUnknownOption(true)
39
40
  .action(async (opts) => {
40
41
  try {
42
+ if (peekHqApiKey() !== undefined) {
43
+ throw new Error('HQ_API_KEY is set; `hq run` requires a Cognito session. Unset HQ_API_KEY or use `hq secrets exec`.');
44
+ }
41
45
  const dashIndex = process.argv.indexOf('--');
42
46
  const childArgs = dashIndex !== -1 ? process.argv.slice(dashIndex + 1) : [];
43
47
  if (!opts.check && childArgs.length === 0) {
@@ -732,6 +732,11 @@ export function registerSecretsCommand(program) {
732
732
  try {
733
733
  const cred = await resolveVaultCredential();
734
734
  if (cred.kind === "api-key") {
735
+ if (!opts.reveal) {
736
+ console.error(chalk.red("HQ_API_KEY is set; `hq secrets get` without --reveal is not supported for API keys. " +
737
+ "Use --reveal to fetch the value, or unset HQ_API_KEY for metadata-only reads."));
738
+ process.exit(1);
739
+ }
735
740
  const res = await vaultApiFetch({
736
741
  token: cred.token,
737
742
  path: "/v1/keys/secrets/fetch",
@@ -0,0 +1,76 @@
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
@@ -0,0 +1,117 @@
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