@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.
- package/CHANGELOG.md +48 -0
- package/dist/commands/outposts-heartbeat.d.ts +96 -0
- package/dist/commands/outposts-heartbeat.js +188 -0
- package/dist/commands/outposts.js +3 -0
- package/dist/commands/pack-install.js +9 -0
- package/dist/commands/pkg-install.js +6 -0
- package/dist/commands/run.js +4 -0
- package/dist/commands/secrets.js +5 -0
- package/dist/outpost/session-heartbeat-publisher.d.ts +76 -0
- package/dist/outpost/session-heartbeat-publisher.js +117 -0
- package/dist/outpost/session-heartbeat.d.ts +210 -0
- package/dist/outpost/session-heartbeat.js +657 -0
- package/dist/utils/vault-api.d.ts +8 -1
- package/dist/utils/vault-api.js +3 -2
- package/package.json +3 -1
- package/src/commands/outposts-heartbeat.test.ts +299 -0
- package/src/commands/outposts-heartbeat.ts +310 -0
- package/src/commands/outposts.ts +4 -0
- package/src/commands/pack-install.ts +9 -0
- package/src/commands/packs-update-api-key.test.ts +105 -0
- package/src/commands/pkg-install.dispatch.test.ts +33 -1
- package/src/commands/pkg-install.ts +6 -0
- package/src/commands/run.ts +6 -0
- package/src/commands/secrets.test.ts +13 -0
- package/src/commands/secrets.ts +9 -0
- package/src/outpost/session-heartbeat-bounds.test.ts +195 -0
- package/src/outpost/session-heartbeat-guard.test.ts +105 -0
- package/src/outpost/session-heartbeat-publisher.test.ts +178 -0
- package/src/outpost/session-heartbeat-publisher.ts +186 -0
- package/src/outpost/session-heartbeat-retain-guard.test.ts +126 -0
- package/src/outpost/session-heartbeat.test.ts +459 -0
- package/src/outpost/session-heartbeat.ts +877 -0
- package/src/packaging.test.ts +45 -0
- package/src/utils/vault-api.ts +13 -2
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
/**
|
|
3
|
+
* Regression (Codex P1 follow-up on hq-cli#273): `hq packs update` un-wires a
|
|
4
|
+
* pack's existing contributions BEFORE calling `installPack()` to re-wire
|
|
5
|
+
* them. The Cognito-only `HQ_API_KEY` fail-closed gate added for `hq install`
|
|
6
|
+
* must live ONLY at that top-level CLI route (pkg-install.ts) — NOT inside
|
|
7
|
+
* the shared `installPack()` primitive `hq packs update` also calls. If it
|
|
8
|
+
* lived inside `installPack()`, a vault-key session would un-wire a pack and
|
|
9
|
+
* then throw before re-installing it, leaving the pack unwired with no
|
|
10
|
+
* symlinks and no rollback.
|
|
11
|
+
*/
|
|
12
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
13
|
+
import * as os from 'os';
|
|
14
|
+
import { Command } from 'commander';
|
|
15
|
+
|
|
16
|
+
const { listInstalledPacks, unwirePack, installPack, resolveLatest } = vi.hoisted(() => ({
|
|
17
|
+
listInstalledPacks: vi.fn(),
|
|
18
|
+
unwirePack: vi.fn(() => ({ unlinked: [], skipped: [] })),
|
|
19
|
+
installPack: vi.fn(async () => {}),
|
|
20
|
+
resolveLatest: vi.fn(async () => ({
|
|
21
|
+
transport: 'git',
|
|
22
|
+
current: 'aaa',
|
|
23
|
+
latest: 'bbb',
|
|
24
|
+
updateAvailable: true,
|
|
25
|
+
})),
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
vi.mock('./pack-install.js', () => ({
|
|
29
|
+
classify: vi.fn(() => 'git'),
|
|
30
|
+
resolveLatest,
|
|
31
|
+
resolveLatestMarketplace: vi.fn(),
|
|
32
|
+
runScanPackages: vi.fn(),
|
|
33
|
+
installPack,
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
vi.mock('../utils/pack-contributions.js', () => ({
|
|
37
|
+
contributionLinks: vi.fn(() => []),
|
|
38
|
+
linkStatus: vi.fn(() => 'live'),
|
|
39
|
+
listInstalledPacks,
|
|
40
|
+
findDependentPacks: vi.fn(() => []),
|
|
41
|
+
readPackManifest: vi.fn(() => ({ manifest: null })),
|
|
42
|
+
unwirePack,
|
|
43
|
+
unwirePackMcp: vi.fn(),
|
|
44
|
+
readHqVersion: vi.fn(() => '15.0.0'),
|
|
45
|
+
readRecommendedPackages: vi.fn(() => []),
|
|
46
|
+
packagesDir: vi.fn((root: string) => `${root}/core/packages`),
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
// resolveRoot() falls back to findHqRoot() when --hq-root isn't passed; stub
|
|
50
|
+
// it to a real, existing directory so packs.ts never chdir()s into one.
|
|
51
|
+
vi.mock('../utils/manifest.js', () => ({
|
|
52
|
+
findHqRoot: vi.fn(() => os.tmpdir()),
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
import { registerPacksCommand } from './packs.js';
|
|
56
|
+
|
|
57
|
+
async function runUpdateCli(...args: string[]): Promise<void> {
|
|
58
|
+
const program = new Command();
|
|
59
|
+
program.exitOverride();
|
|
60
|
+
registerPacksCommand(program);
|
|
61
|
+
await program.parseAsync(['packs', 'update', ...args], { from: 'user' });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe('hq packs update leaves no pack unwired under HQ_API_KEY', () => {
|
|
65
|
+
beforeEach(() => {
|
|
66
|
+
listInstalledPacks.mockReturnValue([
|
|
67
|
+
{
|
|
68
|
+
name: 'hq-pack-demo',
|
|
69
|
+
dir: '/tmp/hq/core/packages/hq-pack-demo',
|
|
70
|
+
manifest: {
|
|
71
|
+
name: 'hq-pack-demo',
|
|
72
|
+
version: '1.0.0',
|
|
73
|
+
source: 'https://example.com/demo.git#aaa',
|
|
74
|
+
contributes: { skills: ['demo'] },
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
]);
|
|
78
|
+
unwirePack.mockClear();
|
|
79
|
+
installPack.mockClear();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
afterEach(() => {
|
|
83
|
+
delete process.env.HQ_API_KEY;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('still calls installPack after unwirePack when HQ_API_KEY is set', async () => {
|
|
87
|
+
process.env.HQ_API_KEY = 'hqk_probe';
|
|
88
|
+
await runUpdateCli('hq-pack-demo', '--json');
|
|
89
|
+
|
|
90
|
+
expect(unwirePack).toHaveBeenCalledTimes(1);
|
|
91
|
+
// The regression: installPack must run to completion right after
|
|
92
|
+
// unwirePack -- it must NOT throw due to a Cognito-only gate, which would
|
|
93
|
+
// leave the pack unwired with nothing re-installed in its place.
|
|
94
|
+
expect(installPack).toHaveBeenCalledTimes(1);
|
|
95
|
+
const unwireOrder = unwirePack.mock.invocationCallOrder[0];
|
|
96
|
+
const installOrder = installPack.mock.invocationCallOrder[0];
|
|
97
|
+
expect(unwireOrder).toBeLessThan(installOrder);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('still updates without HQ_API_KEY set (baseline, unchanged behavior)', async () => {
|
|
101
|
+
await runUpdateCli('hq-pack-demo', '--json');
|
|
102
|
+
expect(unwirePack).toHaveBeenCalledTimes(1);
|
|
103
|
+
expect(installPack).toHaveBeenCalledTimes(1);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* listings transport (the live install path), not the legacy Cognito-gated
|
|
5
5
|
* registry flow whose backend (/packages, /entitlements) was never deployed.
|
|
6
6
|
*/
|
|
7
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
7
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
8
8
|
import { Command } from 'commander';
|
|
9
9
|
|
|
10
10
|
// vi.mock is hoisted above imports/consts, so build the mocks via vi.hoisted()
|
|
@@ -48,6 +48,10 @@ describe('hq install dispatch — bare slug routes to marketplace', () => {
|
|
|
48
48
|
sourceMatchesPackPattern.mockClear();
|
|
49
49
|
});
|
|
50
50
|
|
|
51
|
+
afterEach(() => {
|
|
52
|
+
delete process.env.HQ_API_KEY;
|
|
53
|
+
});
|
|
54
|
+
|
|
51
55
|
it('routes a bare slug through the marketplace transport', async () => {
|
|
52
56
|
await runInstall('email-assistant');
|
|
53
57
|
expect(installPack).toHaveBeenCalledTimes(1);
|
|
@@ -69,3 +73,31 @@ describe('hq install dispatch — bare slug routes to marketplace', () => {
|
|
|
69
73
|
expect(installPack.mock.calls[0][0]).toBe('@indigoai-us/hq-pack-demo');
|
|
70
74
|
});
|
|
71
75
|
});
|
|
76
|
+
|
|
77
|
+
describe('hq install dispatch — HQ_API_KEY fail-closed gate', () => {
|
|
78
|
+
beforeEach(() => {
|
|
79
|
+
installPack.mockClear();
|
|
80
|
+
sourceMatchesPackPattern.mockClear();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
afterEach(() => {
|
|
84
|
+
delete process.env.HQ_API_KEY;
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('rejects at the CLI entrypoint before calling installPack when HQ_API_KEY is set', async () => {
|
|
88
|
+
process.env.HQ_API_KEY = 'hqk_probe';
|
|
89
|
+
const exitSpy = vi
|
|
90
|
+
.spyOn(process, 'exit')
|
|
91
|
+
.mockImplementation((code?: number | string | null) => {
|
|
92
|
+
throw new Error(`process.exit(${code})`);
|
|
93
|
+
});
|
|
94
|
+
try {
|
|
95
|
+
await expect(runInstall('@indigoai-us/hq-pack-demo')).rejects.toThrow('process.exit(1)');
|
|
96
|
+
// The gate must fire BEFORE installPack -- a vault-key session must
|
|
97
|
+
// never reach the pack-fetch/wire path via `hq install`.
|
|
98
|
+
expect(installPack).not.toHaveBeenCalled();
|
|
99
|
+
} finally {
|
|
100
|
+
exitSpy.mockRestore();
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
} from '../utils/registry-client.js';
|
|
29
29
|
import { verifySha256, verifyRsaSignature } from '../utils/integrity.js';
|
|
30
30
|
import { addToRegistry } from '../utils/registry.js';
|
|
31
|
+
import { assertCognitoOnlyCommand } from '../utils/resolve-vault-credential.js';
|
|
31
32
|
import {
|
|
32
33
|
MARKETPLACE_PREFIX,
|
|
33
34
|
installPack,
|
|
@@ -51,6 +52,11 @@ export function registerPackageInstallCommand(parent: Command): void {
|
|
|
51
52
|
opts: { company?: string; allowHooks?: boolean; allowMcp?: boolean; branch?: boolean }
|
|
52
53
|
) => {
|
|
53
54
|
try {
|
|
55
|
+
// Gate the top-level `hq install` entrypoint only — `installPack`
|
|
56
|
+
// itself is also called by `hq packs update` (packs.ts), which must
|
|
57
|
+
// stay able to fail closed at its OWN call site (before it un-wires
|
|
58
|
+
// an existing pack) rather than mid-flight inside installPack.
|
|
59
|
+
assertCognitoOnlyCommand('hq install');
|
|
54
60
|
if (sourceMatchesPackPattern(source)) {
|
|
55
61
|
await installPack(source, {
|
|
56
62
|
company: opts.company,
|
package/src/commands/run.ts
CHANGED
|
@@ -4,6 +4,7 @@ import * as path from 'node:path';
|
|
|
4
4
|
import * as fs from 'node:fs';
|
|
5
5
|
import { internal } from 'varlock';
|
|
6
6
|
import { ensureCognitoToken } from '../utils/cognito-session.js';
|
|
7
|
+
import { peekHqApiKey } from '../utils/resolve-vault-credential.js';
|
|
7
8
|
import { computeSha256 } from '../utils/integrity.js';
|
|
8
9
|
import { vaultApiFetch, getCompanyUid } from '../utils/vault-api.js';
|
|
9
10
|
import { discoverSchemas } from '../run/discover-schemas.js';
|
|
@@ -52,6 +53,11 @@ export function registerRunCommand(program: Command): void {
|
|
|
52
53
|
check?: boolean;
|
|
53
54
|
}) => {
|
|
54
55
|
try {
|
|
56
|
+
if (peekHqApiKey() !== undefined) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
'HQ_API_KEY is set; `hq run` requires a Cognito session. Unset HQ_API_KEY or use `hq secrets exec`.',
|
|
59
|
+
);
|
|
60
|
+
}
|
|
55
61
|
const dashIndex = process.argv.indexOf('--');
|
|
56
62
|
const childArgs = dashIndex !== -1 ? process.argv.slice(dashIndex + 1) : [];
|
|
57
63
|
|
|
@@ -1945,6 +1945,19 @@ describe("HQ_API_KEY consume path", () => {
|
|
|
1945
1945
|
expect(errText).toMatch(/must start with 'hqk_'/);
|
|
1946
1946
|
});
|
|
1947
1947
|
|
|
1948
|
+
it("rejects secrets get without --reveal when HQ_API_KEY is set", async () => {
|
|
1949
|
+
process.env.HQ_API_KEY = "hqk_valid_key";
|
|
1950
|
+
const program = buildProgram();
|
|
1951
|
+
await expect(
|
|
1952
|
+
program.parseAsync(["node", "hq", "secrets", "get", "FOO"]),
|
|
1953
|
+
).rejects.toThrow(/__EXIT__:1/);
|
|
1954
|
+
expect(vaultApiFetch).not.toHaveBeenCalled();
|
|
1955
|
+
const errText = errSpy.mock.calls
|
|
1956
|
+
.map((call) => call.map(String).join(" "))
|
|
1957
|
+
.join("\n");
|
|
1958
|
+
expect(errText).toMatch(/without --reveal is not supported/);
|
|
1959
|
+
});
|
|
1960
|
+
|
|
1948
1961
|
it("gets a secret via /v1/keys/secrets/fetch when HQ_API_KEY is set", async () => {
|
|
1949
1962
|
process.env.HQ_API_KEY = "hqk_valid_key";
|
|
1950
1963
|
vi.mocked(vaultApiFetch).mockResolvedValueOnce(
|
package/src/commands/secrets.ts
CHANGED
|
@@ -1055,6 +1055,15 @@ export function registerSecretsCommand(program: Command): void {
|
|
|
1055
1055
|
const cred = await resolveVaultCredential();
|
|
1056
1056
|
|
|
1057
1057
|
if (cred.kind === "api-key") {
|
|
1058
|
+
if (!opts.reveal) {
|
|
1059
|
+
console.error(
|
|
1060
|
+
chalk.red(
|
|
1061
|
+
"HQ_API_KEY is set; `hq secrets get` without --reveal is not supported for API keys. " +
|
|
1062
|
+
"Use --reveal to fetch the value, or unset HQ_API_KEY for metadata-only reads.",
|
|
1063
|
+
),
|
|
1064
|
+
);
|
|
1065
|
+
process.exit(1);
|
|
1066
|
+
}
|
|
1058
1067
|
const res = await vaultApiFetch({
|
|
1059
1068
|
token: cred.token,
|
|
1060
1069
|
path: "/v1/keys/secrets/fetch",
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payload bounding for the Outpost session heartbeat.
|
|
3
|
+
*
|
|
4
|
+
* ## Why this exists
|
|
5
|
+
*
|
|
6
|
+
* The heartbeat enumerates every Claude transcript and Codex rollout on the
|
|
7
|
+
* box. On a long-lived Outpost that is an archive, not a status: the box this
|
|
8
|
+
* was first run against had **8,258 Claude transcripts and 1,082 Codex
|
|
9
|
+
* rollouts — 9,340 sessions — of which 15 had any activity in the last 15
|
|
10
|
+
* minutes.** Publishing all of them produced a payload of roughly 1.9 MB.
|
|
11
|
+
*
|
|
12
|
+
* AWS IoT Core rejects anything over 128 KB, so the very first real publish
|
|
13
|
+
* failed with `payload larger than 131072 bytes`. Every subsequent one would
|
|
14
|
+
* have too. The heartbeat is a liveness signal; it must carry what is live.
|
|
15
|
+
*
|
|
16
|
+
* Two rules, both tested here:
|
|
17
|
+
* 1. `ended` sessions are not published — they are history, and re-sending
|
|
18
|
+
* thousands of them every five seconds is the bug.
|
|
19
|
+
* 2. Whatever survives that filter is still bounded by BYTES, because a busy
|
|
20
|
+
* box could exceed the limit on live sessions alone. When the bound bites
|
|
21
|
+
* it is reported, never silent — a truncated payload that looks complete
|
|
22
|
+
* is how you get a wrong dashboard instead of a missing one.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { describe, expect, it } from "vitest";
|
|
26
|
+
import {
|
|
27
|
+
DEFAULT_LIVENESS_THRESHOLDS,
|
|
28
|
+
IOT_PAYLOAD_BUDGET_BYTES,
|
|
29
|
+
collectSessions,
|
|
30
|
+
type DirEntry,
|
|
31
|
+
type FileStat,
|
|
32
|
+
type FileSystemPort,
|
|
33
|
+
} from "./session-heartbeat.js";
|
|
34
|
+
|
|
35
|
+
const NOW = new Date("2026-07-29T12:00:00.000Z");
|
|
36
|
+
const NOW_MS = NOW.getTime();
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A fake Claude tree with `count` transcripts, each aged `ageSecondsFor(i)`.
|
|
40
|
+
* Codex enumeration sees an empty tree.
|
|
41
|
+
*/
|
|
42
|
+
function claudeTreeFs(
|
|
43
|
+
count: number,
|
|
44
|
+
ageSecondsFor: (i: number) => number,
|
|
45
|
+
): FileSystemPort {
|
|
46
|
+
const names = Array.from(
|
|
47
|
+
{ length: count },
|
|
48
|
+
(_, i) =>
|
|
49
|
+
`${i.toString(16).padStart(8, "0")}-aaaa-bbbb-cccc-dddddddddddd.jsonl`,
|
|
50
|
+
);
|
|
51
|
+
const ageOf = new Map<string, number>();
|
|
52
|
+
names.forEach((n, i) => ageOf.set(n, ageSecondsFor(i)));
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
async readDir(path: string): Promise<DirEntry[]> {
|
|
56
|
+
if (path.endsWith("/.claude/projects")) {
|
|
57
|
+
return [{ name: "-home-ec2-user-hq", isDirectory: true, isFile: false }];
|
|
58
|
+
}
|
|
59
|
+
if (path.endsWith("/.claude/projects/-home-ec2-user-hq")) {
|
|
60
|
+
return names.map((name) => ({
|
|
61
|
+
name,
|
|
62
|
+
isDirectory: false,
|
|
63
|
+
isFile: true,
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
return [];
|
|
67
|
+
},
|
|
68
|
+
async stat(path: string): Promise<FileStat> {
|
|
69
|
+
const name = path.split("/").pop() ?? "";
|
|
70
|
+
const age = ageOf.get(name) ?? 0;
|
|
71
|
+
const mtimeMs = NOW_MS - age * 1000;
|
|
72
|
+
return { mtimeMs, birthtimeMs: mtimeMs, size: 1024 };
|
|
73
|
+
},
|
|
74
|
+
async readTextFile() {
|
|
75
|
+
return "";
|
|
76
|
+
},
|
|
77
|
+
async readBounded() {
|
|
78
|
+
// A realistic cwd/model sniff, so each record is a realistic size.
|
|
79
|
+
return JSON.stringify({
|
|
80
|
+
cwd: "/home/ec2-user/hq/repos/private/some-longish-repo-name",
|
|
81
|
+
model: "claude-opus-4-8",
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const config = { personUid: "prs_01TEST", home: "/home/ec2-user", now: () => NOW };
|
|
88
|
+
|
|
89
|
+
describe("payload bounding", () => {
|
|
90
|
+
it("publishes live sessions and drops the ended archive", async () => {
|
|
91
|
+
// 500 sessions: the first 5 active, the rest ancient — the real shape of
|
|
92
|
+
// a box that has been running for weeks.
|
|
93
|
+
const fs = claudeTreeFs(500, (i) => (i < 5 ? 2 : 60 * 60 * 24 * 30));
|
|
94
|
+
|
|
95
|
+
const payload = await collectSessions(config, { fs });
|
|
96
|
+
|
|
97
|
+
expect(payload.sessions).toHaveLength(5);
|
|
98
|
+
expect(payload.sessions.every((s) => s.status !== "ended")).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("reports how many sessions exist, not just how many it sent", async () => {
|
|
102
|
+
const fs = claudeTreeFs(500, (i) => (i < 5 ? 2 : 60 * 60 * 24 * 30));
|
|
103
|
+
|
|
104
|
+
const payload = await collectSessions(config, { fs });
|
|
105
|
+
|
|
106
|
+
// Silent filtering reads as "this box has 5 sessions". It has 500.
|
|
107
|
+
expect(payload.totalSessions).toBe(500);
|
|
108
|
+
expect(payload.sessions.length).toBeLessThan(payload.totalSessions);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("keeps idle sessions — only `ended` is archive", async () => {
|
|
112
|
+
// 2s → running, 5min → idle, 30d → ended.
|
|
113
|
+
const fs = claudeTreeFs(3, (i) => [2, 300, 60 * 60 * 24 * 30][i]!);
|
|
114
|
+
|
|
115
|
+
const payload = await collectSessions(config, { fs });
|
|
116
|
+
|
|
117
|
+
const statuses = payload.sessions.map((s) => s.status).sort();
|
|
118
|
+
expect(statuses).toEqual(["idle", "running"]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("stays under the IoT limit even when every session is live", async () => {
|
|
122
|
+
// 5,000 sessions all active — the size bound, not the status filter, is
|
|
123
|
+
// what has to hold here.
|
|
124
|
+
const fs = claudeTreeFs(5000, () => 2);
|
|
125
|
+
|
|
126
|
+
const payload = await collectSessions(config, { fs });
|
|
127
|
+
const bytes = Buffer.byteLength(JSON.stringify(payload), "utf8");
|
|
128
|
+
|
|
129
|
+
expect(bytes).toBeLessThanOrEqual(IOT_PAYLOAD_BUDGET_BYTES);
|
|
130
|
+
// The real AWS IoT Core ceiling. The budget must leave headroom under it.
|
|
131
|
+
expect(bytes).toBeLessThan(131072);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("flags truncation rather than quietly shortening the list", async () => {
|
|
135
|
+
const fs = claudeTreeFs(5000, () => 2);
|
|
136
|
+
|
|
137
|
+
const payload = await collectSessions(config, { fs });
|
|
138
|
+
|
|
139
|
+
expect(payload.truncated).toBe(true);
|
|
140
|
+
expect(payload.totalSessions).toBe(5000);
|
|
141
|
+
expect(payload.sessions.length).toBeLessThan(5000);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("does not flag truncation when everything fits", async () => {
|
|
145
|
+
const fs = claudeTreeFs(3, () => 2);
|
|
146
|
+
|
|
147
|
+
const payload = await collectSessions(config, { fs });
|
|
148
|
+
|
|
149
|
+
expect(payload.truncated).toBe(false);
|
|
150
|
+
expect(payload.sessions).toHaveLength(3);
|
|
151
|
+
expect(payload.totalSessions).toBe(3);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("reads transcript bodies ONLY for sessions it will publish", async () => {
|
|
155
|
+
// The enumerator statted every file AND did a 16 KiB bounded read on each,
|
|
156
|
+
// before anything was filtered. On the first real box that was 9,340 reads
|
|
157
|
+
// per tick, every 5 seconds — ~150 MB of disk for 15 useful records, on
|
|
158
|
+
// the exact resource that had already taken the machine down once.
|
|
159
|
+
const reads: string[] = [];
|
|
160
|
+
const base = claudeTreeFs(500, (i) => (i < 5 ? 2 : 60 * 60 * 24 * 30));
|
|
161
|
+
const fs: FileSystemPort = {
|
|
162
|
+
...base,
|
|
163
|
+
async readBounded(path, maxBytes, from) {
|
|
164
|
+
reads.push(path);
|
|
165
|
+
return base.readBounded(path, maxBytes, from);
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const payload = await collectSessions(config, { fs });
|
|
170
|
+
|
|
171
|
+
expect(payload.sessions).toHaveLength(5);
|
|
172
|
+
// Five live sessions ⇒ five reads, not five hundred.
|
|
173
|
+
expect(reads).toHaveLength(5);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("keeps the MOST RECENTLY ACTIVE sessions when it has to choose", async () => {
|
|
177
|
+
// Index 0 is freshest, index N oldest — all still within `idle`.
|
|
178
|
+
const fs = claudeTreeFs(5000, (i) => 1 + i * (1 / 10));
|
|
179
|
+
|
|
180
|
+
const payload = await collectSessions(config, { fs });
|
|
181
|
+
|
|
182
|
+
expect(payload.truncated).toBe(true);
|
|
183
|
+
// Dropping the newest and keeping stale ones would make the live view
|
|
184
|
+
// wrong in the one way that matters.
|
|
185
|
+
const kept = payload.sessions
|
|
186
|
+
.map((s) => Date.parse(s.lastActivityAt!))
|
|
187
|
+
.sort((a, b) => a - b);
|
|
188
|
+
const oldestKept = kept[0]!;
|
|
189
|
+
const cutoff =
|
|
190
|
+
NOW_MS - DEFAULT_LIVENESS_THRESHOLDS.idleWithinSeconds * 1000;
|
|
191
|
+
expect(oldestKept).toBeGreaterThan(cutoff);
|
|
192
|
+
// The single freshest session must always survive.
|
|
193
|
+
expect(Math.max(...kept)).toBe(NOW_MS - 1000);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The no-secrets guard must catch credentials WITHOUT catching ordinary paths.
|
|
3
|
+
*
|
|
4
|
+
* Both halves matter equally, and the second is easy to get wrong. The guard
|
|
5
|
+
* throws, and a throw fails the whole tick — so a pattern that matches a
|
|
6
|
+
* legitimate directory name silently disables reporting for the entire box
|
|
7
|
+
* until that session ages out.
|
|
8
|
+
*
|
|
9
|
+
* The original marker list held `sk-` and `asia` as bare substrings. `sk-`
|
|
10
|
+
* matches `task-runner`, `flask-app`, `disk-usage`, `risk-model`. Anyone who
|
|
11
|
+
* created a repo with one of those names would have taken the heartbeat down
|
|
12
|
+
* across the box, with the service still reporting active — the exact failure
|
|
13
|
+
* shape this whole change exists to remove.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, expect, it } from "vitest";
|
|
17
|
+
import {
|
|
18
|
+
assertNoSecretsInPayload,
|
|
19
|
+
type AgentSession,
|
|
20
|
+
type SessionsHeartbeatPayload,
|
|
21
|
+
} from "./session-heartbeat.js";
|
|
22
|
+
|
|
23
|
+
function payloadWith(over: Partial<AgentSession>): SessionsHeartbeatPayload {
|
|
24
|
+
const session: AgentSession = {
|
|
25
|
+
id: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
|
|
26
|
+
tool: "claude",
|
|
27
|
+
origin: "outpost",
|
|
28
|
+
cwd: "/home/ec2-user/hq",
|
|
29
|
+
project: "hq",
|
|
30
|
+
company: null,
|
|
31
|
+
model: "claude-opus-4-8",
|
|
32
|
+
status: "running",
|
|
33
|
+
startedAt: "2026-07-29T12:00:00.000Z",
|
|
34
|
+
lastActivityAt: "2026-07-29T12:00:00.000Z",
|
|
35
|
+
source: "/home/ec2-user/.claude/projects/-home-ec2-user-hq/x.jsonl",
|
|
36
|
+
...over,
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
type: "sessions",
|
|
40
|
+
origin: "outpost",
|
|
41
|
+
emittedAt: "2026-07-29T12:00:00.000Z",
|
|
42
|
+
sessions: [session],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe("ordinary paths must never trip the guard", () => {
|
|
47
|
+
// Every one of these contains `sk-` or `asia` and would have thrown before.
|
|
48
|
+
const innocentPaths = [
|
|
49
|
+
"/home/ec2-user/hq/repos/private/task-runner",
|
|
50
|
+
"/home/ec2-user/hq/repos/public/flask-app",
|
|
51
|
+
"/home/ec2-user/hq/tools/disk-usage",
|
|
52
|
+
"/home/ec2-user/hq/models/risk-model",
|
|
53
|
+
"/home/ec2-user/hq/regions/asia-pacific",
|
|
54
|
+
"/home/ec2-user/hq/repos/private/password-manager",
|
|
55
|
+
"/home/ec2-user/hq/kiosk-display",
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
for (const cwd of innocentPaths) {
|
|
59
|
+
it(`allows ${cwd.split("/").pop()}`, () => {
|
|
60
|
+
const project = cwd.split("/").pop()!;
|
|
61
|
+
expect(() =>
|
|
62
|
+
assertNoSecretsInPayload(payloadWith({ cwd, project, source: `${cwd}/s.jsonl` })),
|
|
63
|
+
).not.toThrow();
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
it("allows a model id that merely starts with the letters sk", () => {
|
|
68
|
+
expect(() =>
|
|
69
|
+
assertNoSecretsInPayload(payloadWith({ model: "sk-lite-preview" })),
|
|
70
|
+
).not.toThrow();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("real credentials must still be caught", () => {
|
|
75
|
+
const leaks: [string, Partial<AgentSession>][] = [
|
|
76
|
+
["an Anthropic key", { model: "sk-ant-api03-AbCdEfGhIjKlMnOpQrStUv" }],
|
|
77
|
+
["an OpenAI-style key", { model: "sk-AbCdEfGhIjKlMnOpQrStUvWxYz012345" }],
|
|
78
|
+
["a long-lived AWS key id", { project: "AKIAIOSFODNN7EXAMPLE" }],
|
|
79
|
+
["an STS temp key id", { project: "ASIAIOSFODNN7EXAMPLE" }],
|
|
80
|
+
["a PEM private key", { cwd: "-----BEGIN RSA PRIVATE KEY-----" }],
|
|
81
|
+
["an authorization header", { model: "authorization: xyz" }],
|
|
82
|
+
["a bearer token", { model: "Bearer abcdefghijklmnop0123" }],
|
|
83
|
+
["an instance token header", { project: "x-outpost-instance-token" }],
|
|
84
|
+
["a refresh token field", { model: 'refresh_token="abc"' }],
|
|
85
|
+
["a password field", { model: "password=hunter2" }],
|
|
86
|
+
["a secret access key", { project: "secretaccesskey" }],
|
|
87
|
+
["a session token", { project: "sessiontoken" }],
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
for (const [what, over] of leaks) {
|
|
91
|
+
it(`rejects ${what}`, () => {
|
|
92
|
+
expect(() => assertNoSecretsInPayload(payloadWith(over))).toThrow(
|
|
93
|
+
/refusing to publish/,
|
|
94
|
+
);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("non-whitelisted fields are still rejected", () => {
|
|
100
|
+
it("refuses a payload carrying an extra field", () => {
|
|
101
|
+
const p = payloadWith({});
|
|
102
|
+
(p.sessions[0] as unknown as Record<string, unknown>).transcript = "secret";
|
|
103
|
+
expect(() => assertNoSecretsInPayload(p)).toThrow(/non-whitelisted/);
|
|
104
|
+
});
|
|
105
|
+
});
|