@tangle-network/agent-app 0.60.62 → 0.60.63
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/dist/app-auth/index.js +3 -3
- package/dist/assistant/index.js +7 -7
- package/dist/chat-routes/index.js +12 -12
- package/dist/chat-store/index.js +3 -3
- package/dist/chunk-5UQJ7F3P.js +41 -0
- package/dist/chunk-5UQJ7F3P.js.map +1 -0
- package/dist/{chunk-YXYQ6MJL.js → chunk-5V6VTO5Q.js} +65 -2
- package/dist/chunk-5V6VTO5Q.js.map +1 -0
- package/dist/chunk-J2O76TTS.js +458 -0
- package/dist/chunk-J2O76TTS.js.map +1 -0
- package/dist/{chunk-2YLSWZ5W.js → chunk-JBEFTXDO.js} +4 -4
- package/dist/{chunk-JGZ7WEPL.js → chunk-JC7YSRBR.js} +15 -1
- package/dist/chunk-JC7YSRBR.js.map +1 -0
- package/dist/{chunk-4WJREKXX.js → chunk-O3ZM5QPC.js} +6 -6
- package/dist/{chunk-FWLCPAST.js → chunk-SH56N4PV.js} +3 -3
- package/dist/{chunk-ZGUXI5SI.js → chunk-XNKLQG26.js} +5 -5
- package/dist/chunk-Y6LE5CAW.js +503 -0
- package/dist/chunk-Y6LE5CAW.js.map +1 -0
- package/dist/design-canvas-react/index.js +4 -4
- package/dist/launch-invariants/auth-lookup-cache.d.ts +57 -0
- package/dist/launch-invariants/catalog.d.ts +51 -0
- package/dist/launch-invariants/cli.d.ts +10 -0
- package/dist/launch-invariants/cli.js +88 -0
- package/dist/launch-invariants/cli.js.map +1 -0
- package/dist/launch-invariants/conformance.d.ts +49 -0
- package/dist/launch-invariants/index.d.ts +19 -0
- package/dist/launch-invariants/index.js +253 -0
- package/dist/launch-invariants/index.js.map +1 -0
- package/dist/launch-invariants/limit-alarms.d.ts +66 -0
- package/dist/launch-invariants/schedule.d.ts +114 -0
- package/dist/launch-invariants/settlement.d.ts +90 -0
- package/dist/launch-invariants/sized-reads.d.ts +50 -0
- package/dist/launch-invariants/testing.d.ts +175 -0
- package/dist/launch-invariants/testing.js +45 -0
- package/dist/launch-invariants/testing.js.map +1 -0
- package/dist/public-consultation/index.d.ts +1 -1
- package/dist/public-consultation/index.js +7 -7
- package/dist/stream/index.d.ts +1 -0
- package/dist/stream/index.js +9 -5
- package/dist/stream/orphaned-turns.d.ts +56 -0
- package/dist/stream/turn-buffer.d.ts +12 -0
- package/dist/teams/invitations-api.js +3 -3
- package/dist/turn-health/index.js +6 -17
- package/dist/turn-health/index.js.map +1 -1
- package/dist/turn-stream/index.js +1 -1
- package/dist/web-react/index.js +13 -13
- package/package.json +13 -2
- package/dist/chunk-JGZ7WEPL.js.map +0 -1
- package/dist/chunk-YXYQ6MJL.js.map +0 -1
- /package/dist/{chunk-2YLSWZ5W.js.map → chunk-JBEFTXDO.js.map} +0 -0
- /package/dist/{chunk-4WJREKXX.js.map → chunk-O3ZM5QPC.js.map} +0 -0
- /package/dist/{chunk-FWLCPAST.js.map → chunk-SH56N4PV.js.map} +0 -0
- /package/dist/{chunk-ZGUXI5SI.js.map → chunk-XNKLQG26.js.map} +0 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The last valid result of each auth lookup (API key, key owner, browser
|
|
3
|
+
* session), served only while the store cannot answer.
|
|
4
|
+
*
|
|
5
|
+
* Every lookup reads the store first, so a revoked or expired credential is
|
|
6
|
+
* refused as soon as the store says so, and that answer drops its entry. When
|
|
7
|
+
* the store errors or stalls past the deadline, a valid result verified less
|
|
8
|
+
* than the TTL ago is served instead: on 2026-10-10 an overloaded D1 failed
|
|
9
|
+
* API-key checks and session reads for requests whose credentials were valid
|
|
10
|
+
* moments before (GTM #1488). The TTL counts from the last successful store
|
|
11
|
+
* read and a served entry never renews it, so a credential revoked during an
|
|
12
|
+
* outage lives at most the TTL past its last verification. Entries are per
|
|
13
|
+
* isolate; revoking or rotating a key must also drop its entry here.
|
|
14
|
+
*
|
|
15
|
+
* The cache serves the verified value itself, never a copy: GTM keyed a
|
|
16
|
+
* WeakMap on the verified key object, a copy missed it, and operator requests
|
|
17
|
+
* answered 401 (GTM #1492).
|
|
18
|
+
*/
|
|
19
|
+
/** Longest a verified result may be served while the store cannot answer. A larger configured TTL is capped here. */
|
|
20
|
+
export declare const AUTH_LOOKUP_TTL_MS = 60000;
|
|
21
|
+
/** A store read slower than this is treated as unavailable; a healthy D1 lookup answers in tens of milliseconds. */
|
|
22
|
+
export declare const AUTH_LOOKUP_DEADLINE_MS = 2500;
|
|
23
|
+
/** The store answered that the credential is not valid; never answered from the cache. */
|
|
24
|
+
export declare class AuthLookupRefused extends Error {
|
|
25
|
+
}
|
|
26
|
+
export interface AuthLookupOptions<V> {
|
|
27
|
+
/** When the credential itself expires (Unix ms); the entry never outlives it. */
|
|
28
|
+
expiresAt?: (value: V) => number | null | undefined;
|
|
29
|
+
}
|
|
30
|
+
export interface AuthLookupCache<V> {
|
|
31
|
+
/**
|
|
32
|
+
* Read through the store. `read` resolves the valid value or null, or throws
|
|
33
|
+
* {@link AuthLookupRefused} (or a `Response`) when the store refuses; any
|
|
34
|
+
* other failure, or no answer by the deadline, falls back to a fresh entry.
|
|
35
|
+
* `key` must not be the secret: use {@link credentialCacheKey}.
|
|
36
|
+
*/
|
|
37
|
+
lookup(key: string, read: () => Promise<V | null>, options?: AuthLookupOptions<V>): Promise<V | null>;
|
|
38
|
+
/** Drop every entry whose value matches, as when a key is revoked or rotated. */
|
|
39
|
+
invalidate(matches: (value: V) => boolean): void;
|
|
40
|
+
/** Drop one entry by its key. */
|
|
41
|
+
forget(key: string): void;
|
|
42
|
+
clear(): void;
|
|
43
|
+
/** The keys held, for a check that no secret is stored. */
|
|
44
|
+
keys(): string[];
|
|
45
|
+
}
|
|
46
|
+
export interface AuthLookupCacheOptions {
|
|
47
|
+
/** Names the lookup in the warning logged when a cached result is served. */
|
|
48
|
+
label: string;
|
|
49
|
+
/** Capped at {@link AUTH_LOOKUP_TTL_MS}. */
|
|
50
|
+
ttlMs?: number;
|
|
51
|
+
deadlineMs?: number;
|
|
52
|
+
now?: () => number;
|
|
53
|
+
warn?: (message: string, detail: Record<string, unknown>) => void;
|
|
54
|
+
}
|
|
55
|
+
export declare function createAuthLookupCache<V>(options: AuthLookupCacheOptions): AuthLookupCache<V>;
|
|
56
|
+
/** A credential's cache key: its SHA-256 in hex, so the cache never holds the secret. */
|
|
57
|
+
export declare function credentialCacheKey(secret: string): Promise<string>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The launch invariants every agent app holds, and the budgets they are held to.
|
|
3
|
+
*
|
|
4
|
+
* Each one is a failure GTM shipped to production on 2026-10-10 and fixed by
|
|
5
|
+
* hand. They are listed here once so every app checks the same rule with the
|
|
6
|
+
* same numbers, and `agent-app-invariants` reports each by its id.
|
|
7
|
+
*/
|
|
8
|
+
/** Budgets the invariants are measured against. */
|
|
9
|
+
export declare const LAUNCH_BUDGETS: {
|
|
10
|
+
/** Most bytes one query may return in a scheduled job or report route: one sized batch with room for its listing. */
|
|
11
|
+
readonly queryBytes: number;
|
|
12
|
+
/** Most the live heap may grow while one scheduled job runs on a day-sized fixture. */
|
|
13
|
+
readonly heapGrowthBytes: number;
|
|
14
|
+
/** Most bytes of a large column one sized read returns; a single larger row is read alone. */
|
|
15
|
+
readonly sizedReadBytes: number;
|
|
16
|
+
/** Most stored events one long turn may write. */
|
|
17
|
+
readonly turnEvents: 5000;
|
|
18
|
+
/** A turn whose owner stopped reaches its terminal state within this long. */
|
|
19
|
+
readonly settleWithinMs: number;
|
|
20
|
+
/** Turn recovery and orphan settlement run at least this often. */
|
|
21
|
+
readonly recoveryEveryMs: number;
|
|
22
|
+
/** Longest a cached auth lookup may be served while the store cannot answer. */
|
|
23
|
+
readonly authCacheTtlMs: 60000;
|
|
24
|
+
/** A platform-limit alarm fires at this share of the limit. */
|
|
25
|
+
readonly alarmRatio: 0.8;
|
|
26
|
+
};
|
|
27
|
+
export type LaunchInvariantId = 'bounded-reads' | 'memory-budget' | 'isolated-jobs' | 'honest-settlement' | 'coalesced-events' | 'auth-survives-d1-stall' | 'authorize-before-stream' | 'limit-alarms';
|
|
28
|
+
export interface LaunchInvariant {
|
|
29
|
+
id: LaunchInvariantId;
|
|
30
|
+
title: string;
|
|
31
|
+
/** The rule, as the conformance report states it. */
|
|
32
|
+
rule: string;
|
|
33
|
+
/** The production failure the rule answers. */
|
|
34
|
+
incident: string;
|
|
35
|
+
}
|
|
36
|
+
export declare const LAUNCH_INVARIANTS: readonly LaunchInvariant[];
|
|
37
|
+
/** The invariant ids in report order. */
|
|
38
|
+
export declare const LAUNCH_INVARIANT_IDS: readonly LaunchInvariantId[];
|
|
39
|
+
/** One checked outcome of one invariant, for one subject (a job, a route, a scenario). */
|
|
40
|
+
export interface InvariantVerdict {
|
|
41
|
+
invariant: LaunchInvariantId;
|
|
42
|
+
/** What was checked: a job name, a route, a scenario. */
|
|
43
|
+
subject: string;
|
|
44
|
+
pass: boolean;
|
|
45
|
+
/** Findings, one line each. On a pass, what was proven. */
|
|
46
|
+
details: string[];
|
|
47
|
+
/** Measurements behind the verdict, for the report. */
|
|
48
|
+
data?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
/** A verdict whose message names every finding, for a test runner's failure output. */
|
|
51
|
+
export declare function describeVerdict(verdict: InvariantVerdict): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type ConformanceReport } from './conformance.js';
|
|
3
|
+
interface CliArgs {
|
|
4
|
+
appDir: string;
|
|
5
|
+
config?: string;
|
|
6
|
+
json?: string;
|
|
7
|
+
}
|
|
8
|
+
/** Run the app's invariant tests and judge the result. */
|
|
9
|
+
export declare function runConformance(args: CliArgs): Promise<ConformanceReport>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
INVARIANT_RESULTS_ENV,
|
|
4
|
+
buildConformanceReport,
|
|
5
|
+
formatConformanceReport,
|
|
6
|
+
readDeploymentFacts,
|
|
7
|
+
readVerdicts
|
|
8
|
+
} from "../chunk-Y6LE5CAW.js";
|
|
9
|
+
import "../chunk-J2O76TTS.js";
|
|
10
|
+
import {
|
|
11
|
+
invokedAsScript
|
|
12
|
+
} from "../chunk-C3CAYGGQ.js";
|
|
13
|
+
|
|
14
|
+
// src/launch-invariants/cli.ts
|
|
15
|
+
import { spawnSync } from "child_process";
|
|
16
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs";
|
|
17
|
+
import { tmpdir } from "os";
|
|
18
|
+
import { join, resolve } from "path";
|
|
19
|
+
import { pathToFileURL } from "url";
|
|
20
|
+
var CONFIG_NAMES = ["launch-invariants.config.mjs", "launch-invariants.config.js", "launch-invariants.config.json"];
|
|
21
|
+
function parseArgs(argv) {
|
|
22
|
+
const args = { appDir: process.cwd() };
|
|
23
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
24
|
+
const arg = argv[index];
|
|
25
|
+
if (arg === "--config") args.config = argv[++index];
|
|
26
|
+
else if (arg === "--json") args.json = argv[++index];
|
|
27
|
+
else if (arg.startsWith("--")) throw new Error(`unknown option ${arg}`);
|
|
28
|
+
else args.appDir = resolve(arg);
|
|
29
|
+
}
|
|
30
|
+
return args;
|
|
31
|
+
}
|
|
32
|
+
async function loadConfig(appDir, explicit) {
|
|
33
|
+
const file = explicit ? resolve(appDir, explicit) : CONFIG_NAMES.map((name) => join(appDir, name)).find((path) => existsSync(path));
|
|
34
|
+
if (!file || !existsSync(file)) throw new Error(`no ${CONFIG_NAMES[0]} in ${appDir}`);
|
|
35
|
+
const config = file.endsWith(".json") ? JSON.parse(readFileSync(file, "utf8")) : (await import(pathToFileURL(file).href)).default;
|
|
36
|
+
if (!config?.product || !config.test) throw new Error(`${file} must export { product, test }`);
|
|
37
|
+
return config;
|
|
38
|
+
}
|
|
39
|
+
async function runConformance(args) {
|
|
40
|
+
const config = await loadConfig(args.appDir, args.config);
|
|
41
|
+
const facts = readDeploymentFacts(args.appDir, config.wrangler);
|
|
42
|
+
const dir = mkdtempSync(join(tmpdir(), "agent-app-invariants-"));
|
|
43
|
+
const results = join(dir, "verdicts.jsonl");
|
|
44
|
+
writeFileSync(results, "");
|
|
45
|
+
try {
|
|
46
|
+
const run = spawnSync(config.test, {
|
|
47
|
+
cwd: args.appDir,
|
|
48
|
+
shell: true,
|
|
49
|
+
stdio: "inherit",
|
|
50
|
+
env: { ...process.env, [INVARIANT_RESULTS_ENV]: results }
|
|
51
|
+
});
|
|
52
|
+
return buildConformanceReport({
|
|
53
|
+
config,
|
|
54
|
+
facts,
|
|
55
|
+
verdicts: readVerdicts(readFileSync(results, "utf8")),
|
|
56
|
+
testExitCode: run.status
|
|
57
|
+
});
|
|
58
|
+
} finally {
|
|
59
|
+
rmSync(dir, { recursive: true, force: true });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function main() {
|
|
63
|
+
let args;
|
|
64
|
+
try {
|
|
65
|
+
args = parseArgs(process.argv.slice(2));
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
68
|
+
return 2;
|
|
69
|
+
}
|
|
70
|
+
let report;
|
|
71
|
+
try {
|
|
72
|
+
report = await runConformance(args);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error(`agent-app-invariants: ${error instanceof Error ? error.message : String(error)}`);
|
|
75
|
+
return 2;
|
|
76
|
+
}
|
|
77
|
+
console.log(formatConformanceReport(report));
|
|
78
|
+
if (args.json) writeFileSync(resolve(args.appDir, args.json), `${JSON.stringify(report, null, 2)}
|
|
79
|
+
`);
|
|
80
|
+
return report.pass ? 0 : 1;
|
|
81
|
+
}
|
|
82
|
+
if (invokedAsScript(import.meta.url, process.argv[1])) {
|
|
83
|
+
void main().then((code) => process.exit(code));
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
runConformance
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/launch-invariants/cli.ts"],"sourcesContent":["#!/usr/bin/env node\n/**\n * `agent-app-invariants` — report which launch invariants an app holds, and\n * fail its CI when one does not.\n *\n * Runs in the app's repo. Add a `launch-invariants.config.mjs` at the root:\n *\n * export default {\n * product: 'gtm',\n * test: 'pnpm exec vitest run tests/launch-invariants.test.ts',\n * }\n *\n * and a script next to typecheck:\n *\n * \"scripts\": { \"invariants\": \"agent-app-invariants\" }\n *\n * Usage: agent-app-invariants [appDir] [--config <file>] [--json <out>]\n * Exits 0 when every invariant holds, 1 when one does not, 2 on a usage error.\n */\nimport { spawnSync } from 'node:child_process'\nimport { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'\nimport { tmpdir } from 'node:os'\nimport { join, resolve } from 'node:path'\nimport { pathToFileURL } from 'node:url'\nimport { invokedAsScript } from '../signoff/invoked-as-script.js'\nimport {\n buildConformanceReport,\n formatConformanceReport,\n readDeploymentFacts,\n readVerdicts,\n type ConformanceReport,\n type LaunchInvariantsConfig,\n} from './conformance.js'\nimport { INVARIANT_RESULTS_ENV } from './testing.js'\n\nconst CONFIG_NAMES = ['launch-invariants.config.mjs', 'launch-invariants.config.js', 'launch-invariants.config.json']\n\ninterface CliArgs {\n appDir: string\n config?: string\n json?: string\n}\n\nfunction parseArgs(argv: readonly string[]): CliArgs {\n const args: CliArgs = { appDir: process.cwd() }\n for (let index = 0; index < argv.length; index += 1) {\n const arg = argv[index]!\n if (arg === '--config') args.config = argv[++index]\n else if (arg === '--json') args.json = argv[++index]\n else if (arg.startsWith('--')) throw new Error(`unknown option ${arg}`)\n else args.appDir = resolve(arg)\n }\n return args\n}\n\nasync function loadConfig(appDir: string, explicit?: string): Promise<LaunchInvariantsConfig> {\n const file = explicit ? resolve(appDir, explicit) : CONFIG_NAMES.map((name) => join(appDir, name)).find((path) => existsSync(path))\n if (!file || !existsSync(file)) throw new Error(`no ${CONFIG_NAMES[0]} in ${appDir}`)\n const config = file.endsWith('.json')\n ? JSON.parse(readFileSync(file, 'utf8')) as LaunchInvariantsConfig\n : ((await import(pathToFileURL(file).href)) as { default: LaunchInvariantsConfig }).default\n if (!config?.product || !config.test) throw new Error(`${file} must export { product, test }`)\n return config\n}\n\n/** Run the app's invariant tests and judge the result. */\nexport async function runConformance(args: CliArgs): Promise<ConformanceReport> {\n const config = await loadConfig(args.appDir, args.config)\n const facts = readDeploymentFacts(args.appDir, config.wrangler)\n const dir = mkdtempSync(join(tmpdir(), 'agent-app-invariants-'))\n const results = join(dir, 'verdicts.jsonl')\n writeFileSync(results, '')\n try {\n const run = spawnSync(config.test, {\n cwd: args.appDir,\n shell: true,\n stdio: 'inherit',\n env: { ...process.env, [INVARIANT_RESULTS_ENV]: results },\n })\n return buildConformanceReport({\n config,\n facts,\n verdicts: readVerdicts(readFileSync(results, 'utf8')),\n testExitCode: run.status,\n })\n } finally {\n rmSync(dir, { recursive: true, force: true })\n }\n}\n\nasync function main(): Promise<number> {\n let args: CliArgs\n try {\n args = parseArgs(process.argv.slice(2))\n } catch (error) {\n console.error(error instanceof Error ? error.message : String(error))\n return 2\n }\n let report: ConformanceReport\n try {\n report = await runConformance(args)\n } catch (error) {\n console.error(`agent-app-invariants: ${error instanceof Error ? error.message : String(error)}`)\n return 2\n }\n console.log(formatConformanceReport(report))\n if (args.json) writeFileSync(resolve(args.appDir, args.json), `${JSON.stringify(report, null, 2)}\\n`)\n return report.pass ? 0 : 1\n}\n\nif (invokedAsScript(import.meta.url, process.argv[1])) {\n void main().then((code) => process.exit(code))\n}\n"],"mappings":";;;;;;;;;;;;;;AAmBA,SAAS,iBAAiB;AAC1B,SAAS,YAAY,aAAa,cAAc,QAAQ,qBAAqB;AAC7E,SAAS,cAAc;AACvB,SAAS,MAAM,eAAe;AAC9B,SAAS,qBAAqB;AAY9B,IAAM,eAAe,CAAC,gCAAgC,+BAA+B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app root. */
|
|
3
|
+
export interface LaunchInvariantsConfig {
|
|
4
|
+
/** The app's name in the report. */
|
|
5
|
+
product: string;
|
|
6
|
+
/** The command that runs the app's invariant tests, e.g. `pnpm exec vitest run tests/launch-invariants.test.ts`. */
|
|
7
|
+
test: string;
|
|
8
|
+
/** Wrangler configs, relative to the app root. Default: the first of wrangler.toml, wrangler.jsonc, wrangler.json. */
|
|
9
|
+
wrangler?: readonly string[];
|
|
10
|
+
/** Invariants the deployment shows do not apply, with the reason. */
|
|
11
|
+
notApplicable?: Partial<Record<LaunchInvariantId, string>>;
|
|
12
|
+
}
|
|
13
|
+
/** What the deployment declares, read from its wrangler configs. */
|
|
14
|
+
export interface DeploymentFacts {
|
|
15
|
+
wranglerFiles: string[];
|
|
16
|
+
configuredCrons: string[];
|
|
17
|
+
hasD1: boolean;
|
|
18
|
+
}
|
|
19
|
+
export type InvariantStatus = 'pass' | 'fail' | 'not-applicable';
|
|
20
|
+
export interface InvariantReport {
|
|
21
|
+
id: LaunchInvariantId;
|
|
22
|
+
title: string;
|
|
23
|
+
status: InvariantStatus;
|
|
24
|
+
/** Why it failed or does not apply; on a pass, what was proven. */
|
|
25
|
+
lines: string[];
|
|
26
|
+
verdicts: number;
|
|
27
|
+
}
|
|
28
|
+
export interface ConformanceReport {
|
|
29
|
+
product: string;
|
|
30
|
+
pass: boolean;
|
|
31
|
+
passed: number;
|
|
32
|
+
total: number;
|
|
33
|
+
testExitCode: number | null;
|
|
34
|
+
invariants: InvariantReport[];
|
|
35
|
+
facts: DeploymentFacts;
|
|
36
|
+
}
|
|
37
|
+
/** Read the crons and D1 bindings the app's wrangler configs declare. */
|
|
38
|
+
export declare function readDeploymentFacts(appDir: string, files?: readonly string[]): DeploymentFacts;
|
|
39
|
+
/** Parse the results file the invariant tests appended to. */
|
|
40
|
+
export declare function readVerdicts(source: string): InvariantVerdict[];
|
|
41
|
+
/** Judge each invariant from the recorded verdicts and the deployment facts. */
|
|
42
|
+
export declare function buildConformanceReport(input: {
|
|
43
|
+
config: LaunchInvariantsConfig;
|
|
44
|
+
facts: DeploymentFacts;
|
|
45
|
+
verdicts: readonly InvariantVerdict[];
|
|
46
|
+
testExitCode: number | null;
|
|
47
|
+
}): ConformanceReport;
|
|
48
|
+
/** The report as the CLI prints it. */
|
|
49
|
+
export declare function formatConformanceReport(report: ConformanceReport): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/launch-invariants` — the rules every agent app holds before launch, and the
|
|
3
|
+
* runtime pieces that hold them.
|
|
4
|
+
*
|
|
5
|
+
* Each invariant is a failure GTM shipped on 2026-10-10 and fixed by hand
|
|
6
|
+
* (see {@link LAUNCH_INVARIANTS}). This subpath carries what runs in the
|
|
7
|
+
* Worker: sized reads, the scheduled dispatcher that gives each job its own
|
|
8
|
+
* invocation, the auth lookup cache that survives a D1 stall, and the 80%
|
|
9
|
+
* platform-limit alarms. `/launch-invariants/testing` carries the checks an app
|
|
10
|
+
* runs in its own suite, and `agent-app-invariants` reports them per invariant.
|
|
11
|
+
*
|
|
12
|
+
* Server-safe: no Node built-ins. See docs/launch-invariants.md.
|
|
13
|
+
*/
|
|
14
|
+
export { describeVerdict, LAUNCH_BUDGETS, LAUNCH_INVARIANT_IDS, LAUNCH_INVARIANTS, type InvariantVerdict, type LaunchInvariant, type LaunchInvariantId, } from './catalog.js';
|
|
15
|
+
export { D1_MAX_BOUND_PARAMETERS, readInSizedBatches, SIZED_READ_BYTES, SIZED_READ_COMPOSITE_ROWS, SIZED_READ_ROWS, sizedBatches, type SizedBatchOptions, type SizedRow, } from './sized-reads.js';
|
|
16
|
+
export { AUTH_LOOKUP_DEADLINE_MS, AUTH_LOOKUP_TTL_MS, AuthLookupRefused, createAuthLookupCache, credentialCacheKey, type AuthLookupCache, type AuthLookupCacheOptions, type AuthLookupOptions, } from './auth-lookup-cache.js';
|
|
17
|
+
export { checkIsolatedJobs, createScheduledDispatch, cronDailyFirings, longestGapMinutes, parseWranglerCrons, slotsOf, type IsolatedJobsInput, type ObservedSlot, type ScheduledDispatch, type ScheduledDispatchOptions, type ScheduledDispatchResult, type ScheduledJob, type ScheduledJobInput, type ScheduledJobOutcome, type ScheduleEntry, type ScheduleSlot, type WranglerCrons, } from './schedule.js';
|
|
18
|
+
export { createLimitAlarms, estimatedBytes, LIMIT_RESOURCES, withD1LimitAlarms, type LimitAlarmOptions, type LimitAlarms, type LimitBudgets, type LimitLevel, type LimitObservation, type LimitResource, } from './limit-alarms.js';
|
|
19
|
+
export { checkSettlementScenarios, checkTurnSettlement, SETTLEMENT_SCENARIOS, type SettledTurnRecord, type SettlementScenario, type TurnSettlementCounts, type TurnTerminalState, } from './settlement.js';
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LAUNCH_BUDGETS,
|
|
3
|
+
LAUNCH_INVARIANTS,
|
|
4
|
+
LAUNCH_INVARIANT_IDS,
|
|
5
|
+
LIMIT_RESOURCES,
|
|
6
|
+
checkIsolatedJobs,
|
|
7
|
+
createLimitAlarms,
|
|
8
|
+
createScheduledDispatch,
|
|
9
|
+
cronDailyFirings,
|
|
10
|
+
describeVerdict,
|
|
11
|
+
estimatedBytes,
|
|
12
|
+
longestGapMinutes,
|
|
13
|
+
parseWranglerCrons,
|
|
14
|
+
slotsOf,
|
|
15
|
+
withD1LimitAlarms
|
|
16
|
+
} from "../chunk-J2O76TTS.js";
|
|
17
|
+
import {
|
|
18
|
+
D1_MAX_BOUND_PARAMETERS,
|
|
19
|
+
SIZED_READ_BYTES,
|
|
20
|
+
SIZED_READ_COMPOSITE_ROWS,
|
|
21
|
+
SIZED_READ_ROWS,
|
|
22
|
+
readInSizedBatches,
|
|
23
|
+
sizedBatches
|
|
24
|
+
} from "../chunk-5UQJ7F3P.js";
|
|
25
|
+
|
|
26
|
+
// src/launch-invariants/auth-lookup-cache.ts
|
|
27
|
+
var AUTH_LOOKUP_TTL_MS = 6e4;
|
|
28
|
+
var AUTH_LOOKUP_DEADLINE_MS = 2500;
|
|
29
|
+
var MAX_ENTRIES = 2e3;
|
|
30
|
+
var AuthLookupRefused = class extends Error {
|
|
31
|
+
};
|
|
32
|
+
function createAuthLookupCache(options) {
|
|
33
|
+
const ttlMs = Math.min(options.ttlMs ?? AUTH_LOOKUP_TTL_MS, AUTH_LOOKUP_TTL_MS);
|
|
34
|
+
const deadlineMs = options.deadlineMs ?? AUTH_LOOKUP_DEADLINE_MS;
|
|
35
|
+
const now = options.now ?? (() => Date.now());
|
|
36
|
+
const warn = options.warn ?? ((message, detail) => console.warn(message, detail));
|
|
37
|
+
const entries = /* @__PURE__ */ new Map();
|
|
38
|
+
return {
|
|
39
|
+
async lookup(key, read, lookupOptions) {
|
|
40
|
+
let value;
|
|
41
|
+
try {
|
|
42
|
+
value = await withDeadline(read(), deadlineMs);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error instanceof AuthLookupRefused || error instanceof Response) {
|
|
45
|
+
entries.delete(key);
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
const entry = entries.get(key);
|
|
49
|
+
if (entry && now() < entry.until) {
|
|
50
|
+
warn(`[auth-cache] ${options.label}: served the last valid result while the store was unavailable`, {
|
|
51
|
+
error: error instanceof Error ? error.message : String(error),
|
|
52
|
+
remainingMs: entry.until - now()
|
|
53
|
+
});
|
|
54
|
+
return entry.value;
|
|
55
|
+
}
|
|
56
|
+
entries.delete(key);
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
if (value === null) {
|
|
60
|
+
entries.delete(key);
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
const expiresAt = lookupOptions?.expiresAt?.(value);
|
|
64
|
+
const until = Math.min(now() + ttlMs, typeof expiresAt === "number" && expiresAt > 0 ? expiresAt : Number.POSITIVE_INFINITY);
|
|
65
|
+
entries.delete(key);
|
|
66
|
+
entries.set(key, { value, until });
|
|
67
|
+
if (entries.size > MAX_ENTRIES) entries.delete(entries.keys().next().value);
|
|
68
|
+
return value;
|
|
69
|
+
},
|
|
70
|
+
invalidate(matches) {
|
|
71
|
+
for (const [key, entry] of entries) if (matches(entry.value)) entries.delete(key);
|
|
72
|
+
},
|
|
73
|
+
forget(key) {
|
|
74
|
+
entries.delete(key);
|
|
75
|
+
},
|
|
76
|
+
clear() {
|
|
77
|
+
entries.clear();
|
|
78
|
+
},
|
|
79
|
+
keys() {
|
|
80
|
+
return [...entries.keys()];
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
async function credentialCacheKey(secret) {
|
|
85
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(secret));
|
|
86
|
+
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
87
|
+
}
|
|
88
|
+
function withDeadline(promise, ms) {
|
|
89
|
+
let timer;
|
|
90
|
+
const deadline = new Promise((_, reject) => {
|
|
91
|
+
timer = setTimeout(() => reject(new Error(`auth lookup did not answer within ${ms} ms`)), ms);
|
|
92
|
+
});
|
|
93
|
+
return Promise.race([promise, deadline]).finally(() => clearTimeout(timer));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/launch-invariants/settlement.ts
|
|
97
|
+
function checkTurnSettlement(turns, options) {
|
|
98
|
+
const within = options.settleWithinMs ?? LAUNCH_BUDGETS.settleWithinMs;
|
|
99
|
+
const findings = [];
|
|
100
|
+
const counts = { turns: turns.length, answered: 0, failed: 0, stopped: 0, open: 0, maxDetectionLagMinutes: 0 };
|
|
101
|
+
for (const turn of turns) {
|
|
102
|
+
const label = `turn ${turn.turnId}`;
|
|
103
|
+
if (turn.terminals.length > 1) {
|
|
104
|
+
findings.push(`${label} has ${turn.terminals.length} terminal states (${turn.terminals.map((terminal2) => terminal2.state).join(" then ")}); a turn ends once`);
|
|
105
|
+
}
|
|
106
|
+
const terminal = turn.terminals[0];
|
|
107
|
+
if (!terminal) {
|
|
108
|
+
counts.open += 1;
|
|
109
|
+
if (turn.ownerEndedAt !== null && options.now - turn.ownerEndedAt > within) {
|
|
110
|
+
findings.push(`${label} is still open ${minutes(options.now - turn.ownerEndedAt)} min after its owner stopped; the most is ${minutes(within)}`);
|
|
111
|
+
}
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
counts[terminal.state] += 1;
|
|
115
|
+
if (turn.ownerEndedAt !== null) {
|
|
116
|
+
const lag = terminal.at - turn.ownerEndedAt;
|
|
117
|
+
counts.maxDetectionLagMinutes = Math.max(counts.maxDetectionLagMinutes, minutes(lag));
|
|
118
|
+
if (lag > within) findings.push(`${label} was settled ${minutes(lag)} min after its owner stopped; the most is ${minutes(within)}`);
|
|
119
|
+
}
|
|
120
|
+
if (terminal.state === "answered" && turn.abortedToolCall) {
|
|
121
|
+
findings.push(`${label} is counted as an answer, but the Sandbox aborted a tool call in it; it stopped mid-work and is a failure`);
|
|
122
|
+
}
|
|
123
|
+
if (terminal.state === "failed") {
|
|
124
|
+
if (!terminal.code) findings.push(`${label} failed without a typed failure code`);
|
|
125
|
+
if (terminal.retryable !== true) findings.push(`${label} failed without offering Retry`);
|
|
126
|
+
if (terminal.attributedTo !== void 0 && terminal.attributedTo !== turn.requestedAt) {
|
|
127
|
+
findings.push(`${label}'s failure is reported at ${new Date(terminal.attributedTo).toISOString()}, not its request time ${new Date(turn.requestedAt).toISOString()}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (terminal.state === "stopped" && terminal.code && /fail|error/i.test(terminal.code)) {
|
|
131
|
+
findings.push(`${label} was stopped by the person but carries failure code ${terminal.code}; a Stop is counted on its own`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
invariant: "honest-settlement",
|
|
136
|
+
subject: options.subject ?? "turns",
|
|
137
|
+
pass: findings.length === 0,
|
|
138
|
+
details: findings.length ? findings : [
|
|
139
|
+
`${counts.turns} turns: ${counts.answered} answered, ${counts.failed} failed with Retry, ${counts.stopped} stopped, ${counts.open} held by a live owner`,
|
|
140
|
+
`longest detection lag ${counts.maxDetectionLagMinutes} min`
|
|
141
|
+
],
|
|
142
|
+
data: { ...counts }
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function minutes(ms) {
|
|
146
|
+
return Math.round(ms / 6e4 * 10) / 10;
|
|
147
|
+
}
|
|
148
|
+
var SETTLEMENT_SCENARIOS = [
|
|
149
|
+
{
|
|
150
|
+
id: "orphaned-stream",
|
|
151
|
+
story: "The Worker streaming the turn died; its stream row is still running, no admission or Workflow holds it, and its 5-minute lease expired 12 minutes ago.",
|
|
152
|
+
requestedMinutesAgo: 30,
|
|
153
|
+
ownerEndedMinutesAgo: 12,
|
|
154
|
+
expect: "failed"
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
id: "abandoned-before-admission",
|
|
158
|
+
story: "The user message was saved 12 minutes ago, then the request died before any completion owner admitted the turn: no reply, no stream, no admission.",
|
|
159
|
+
requestedMinutesAgo: 12,
|
|
160
|
+
ownerEndedMinutesAgo: 12,
|
|
161
|
+
expect: "failed"
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
id: "runtime-restarted",
|
|
165
|
+
story: "The admitted run's runtime restarted mid-turn 12 minutes ago: the session still reports running while its execution ledger failed the run (terminal event interrupted-done).",
|
|
166
|
+
requestedMinutesAgo: 40,
|
|
167
|
+
ownerEndedMinutesAgo: 12,
|
|
168
|
+
expect: "failed"
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
id: "completed-with-aborted-tool",
|
|
172
|
+
story: 'The run reported completed 12 minutes ago, but a tool part ended with "Tool execution aborted".',
|
|
173
|
+
requestedMinutesAgo: 20,
|
|
174
|
+
ownerEndedMinutesAgo: 12,
|
|
175
|
+
expect: "failed"
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: "user-stop",
|
|
179
|
+
story: "The person pressed Stop 12 minutes ago while the turn ran.",
|
|
180
|
+
requestedMinutesAgo: 20,
|
|
181
|
+
ownerEndedMinutesAgo: 12,
|
|
182
|
+
expect: "stopped"
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
id: "answered",
|
|
186
|
+
story: "The turn answered normally 12 minutes ago.",
|
|
187
|
+
requestedMinutesAgo: 20,
|
|
188
|
+
ownerEndedMinutesAgo: 12,
|
|
189
|
+
expect: "answered"
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
id: "live-long-turn",
|
|
193
|
+
story: "A long turn is still running under an owner that renews its lease.",
|
|
194
|
+
requestedMinutesAgo: 90,
|
|
195
|
+
ownerEndedMinutesAgo: null,
|
|
196
|
+
expect: "open"
|
|
197
|
+
}
|
|
198
|
+
];
|
|
199
|
+
function checkSettlementScenarios(records, options) {
|
|
200
|
+
const scenarios = options.scenarios ?? SETTLEMENT_SCENARIOS;
|
|
201
|
+
const findings = [];
|
|
202
|
+
const present = [];
|
|
203
|
+
for (const scenario of scenarios) {
|
|
204
|
+
const record = records[scenario.id];
|
|
205
|
+
if (!record) {
|
|
206
|
+
findings.push(`scenario ${scenario.id}: no turn record was read back`);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
present.push(record);
|
|
210
|
+
const state = record.terminals[0]?.state ?? "open";
|
|
211
|
+
if (state !== scenario.expect) findings.push(`scenario ${scenario.id}: ended ${state}, expected ${scenario.expect} (${scenario.story})`);
|
|
212
|
+
}
|
|
213
|
+
const honest = checkTurnSettlement(present, { now: options.now, subject: "settlement scenarios" });
|
|
214
|
+
if (!honest.pass) findings.push(...honest.details);
|
|
215
|
+
return {
|
|
216
|
+
invariant: "honest-settlement",
|
|
217
|
+
subject: "settlement scenarios",
|
|
218
|
+
pass: findings.length === 0,
|
|
219
|
+
details: findings.length ? findings : [`${scenarios.length} failure scenarios settle to their honest state`, ...honest.details],
|
|
220
|
+
data: honest.data
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
export {
|
|
224
|
+
AUTH_LOOKUP_DEADLINE_MS,
|
|
225
|
+
AUTH_LOOKUP_TTL_MS,
|
|
226
|
+
AuthLookupRefused,
|
|
227
|
+
D1_MAX_BOUND_PARAMETERS,
|
|
228
|
+
LAUNCH_BUDGETS,
|
|
229
|
+
LAUNCH_INVARIANTS,
|
|
230
|
+
LAUNCH_INVARIANT_IDS,
|
|
231
|
+
LIMIT_RESOURCES,
|
|
232
|
+
SETTLEMENT_SCENARIOS,
|
|
233
|
+
SIZED_READ_BYTES,
|
|
234
|
+
SIZED_READ_COMPOSITE_ROWS,
|
|
235
|
+
SIZED_READ_ROWS,
|
|
236
|
+
checkIsolatedJobs,
|
|
237
|
+
checkSettlementScenarios,
|
|
238
|
+
checkTurnSettlement,
|
|
239
|
+
createAuthLookupCache,
|
|
240
|
+
createLimitAlarms,
|
|
241
|
+
createScheduledDispatch,
|
|
242
|
+
credentialCacheKey,
|
|
243
|
+
cronDailyFirings,
|
|
244
|
+
describeVerdict,
|
|
245
|
+
estimatedBytes,
|
|
246
|
+
longestGapMinutes,
|
|
247
|
+
parseWranglerCrons,
|
|
248
|
+
readInSizedBatches,
|
|
249
|
+
sizedBatches,
|
|
250
|
+
slotsOf,
|
|
251
|
+
withD1LimitAlarms
|
|
252
|
+
};
|
|
253
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/launch-invariants/auth-lookup-cache.ts","../../src/launch-invariants/settlement.ts"],"sourcesContent":["/**\n * The last valid result of each auth lookup (API key, key owner, browser\n * session), served only while the store cannot answer.\n *\n * Every lookup reads the store first, so a revoked or expired credential is\n * refused as soon as the store says so, and that answer drops its entry. When\n * the store errors or stalls past the deadline, a valid result verified less\n * than the TTL ago is served instead: on 2026-10-10 an overloaded D1 failed\n * API-key checks and session reads for requests whose credentials were valid\n * moments before (GTM #1488). The TTL counts from the last successful store\n * read and a served entry never renews it, so a credential revoked during an\n * outage lives at most the TTL past its last verification. Entries are per\n * isolate; revoking or rotating a key must also drop its entry here.\n *\n * The cache serves the verified value itself, never a copy: GTM keyed a\n * WeakMap on the verified key object, a copy missed it, and operator requests\n * answered 401 (GTM #1492).\n */\n\n/** Longest a verified result may be served while the store cannot answer. A larger configured TTL is capped here. */\nexport const AUTH_LOOKUP_TTL_MS = 60_000\n/** A store read slower than this is treated as unavailable; a healthy D1 lookup answers in tens of milliseconds. */\nexport const AUTH_LOOKUP_DEADLINE_MS = 2_500\nconst MAX_ENTRIES = 2_000\n\n/** The store answered that the credential is not valid; never answered from the cache. */\nexport class AuthLookupRefused extends Error {}\n\ninterface Entry<V> {\n value: V\n until: number\n}\n\nexport interface AuthLookupOptions<V> {\n /** When the credential itself expires (Unix ms); the entry never outlives it. */\n expiresAt?: (value: V) => number | null | undefined\n}\n\nexport interface AuthLookupCache<V> {\n /**\n * Read through the store. `read` resolves the valid value or null, or throws\n * {@link AuthLookupRefused} (or a `Response`) when the store refuses; any\n * other failure, or no answer by the deadline, falls back to a fresh entry.\n * `key` must not be the secret: use {@link credentialCacheKey}.\n */\n lookup(key: string, read: () => Promise<V | null>, options?: AuthLookupOptions<V>): Promise<V | null>\n /** Drop every entry whose value matches, as when a key is revoked or rotated. */\n invalidate(matches: (value: V) => boolean): void\n /** Drop one entry by its key. */\n forget(key: string): void\n clear(): void\n /** The keys held, for a check that no secret is stored. */\n keys(): string[]\n}\n\nexport interface AuthLookupCacheOptions {\n /** Names the lookup in the warning logged when a cached result is served. */\n label: string\n /** Capped at {@link AUTH_LOOKUP_TTL_MS}. */\n ttlMs?: number\n deadlineMs?: number\n now?: () => number\n warn?: (message: string, detail: Record<string, unknown>) => void\n}\n\nexport function createAuthLookupCache<V>(options: AuthLookupCacheOptions): AuthLookupCache<V> {\n const ttlMs = Math.min(options.ttlMs ?? AUTH_LOOKUP_TTL_MS, AUTH_LOOKUP_TTL_MS)\n const deadlineMs = options.deadlineMs ?? AUTH_LOOKUP_DEADLINE_MS\n const now = options.now ?? (() => Date.now())\n const warn = options.warn ?? ((message, detail) => console.warn(message, detail))\n const entries = new Map<string, Entry<V>>()\n\n return {\n async lookup(key, read, lookupOptions) {\n let value: V | null\n try {\n value = await withDeadline(read(), deadlineMs)\n } catch (error) {\n if (error instanceof AuthLookupRefused || error instanceof Response) {\n entries.delete(key)\n throw error\n }\n const entry = entries.get(key)\n if (entry && now() < entry.until) {\n warn(`[auth-cache] ${options.label}: served the last valid result while the store was unavailable`, {\n error: error instanceof Error ? error.message : String(error),\n remainingMs: entry.until - now(),\n })\n return entry.value\n }\n entries.delete(key)\n throw error\n }\n if (value === null) {\n entries.delete(key)\n return null\n }\n const expiresAt = lookupOptions?.expiresAt?.(value)\n const until = Math.min(now() + ttlMs, typeof expiresAt === 'number' && expiresAt > 0 ? expiresAt : Number.POSITIVE_INFINITY)\n entries.delete(key)\n entries.set(key, { value, until })\n if (entries.size > MAX_ENTRIES) entries.delete(entries.keys().next().value!)\n return value\n },\n invalidate(matches) {\n for (const [key, entry] of entries) if (matches(entry.value)) entries.delete(key)\n },\n forget(key) {\n entries.delete(key)\n },\n clear() {\n entries.clear()\n },\n keys() {\n return [...entries.keys()]\n },\n }\n}\n\n/** A credential's cache key: its SHA-256 in hex, so the cache never holds the secret. */\nexport async function credentialCacheKey(secret: string): Promise<string> {\n const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(secret))\n return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, '0')).join('')\n}\n\nfunction withDeadline<T>(promise: Promise<T>, ms: number): Promise<T> {\n let timer: ReturnType<typeof setTimeout> | undefined\n const deadline = new Promise<never>((_, reject) => {\n timer = setTimeout(() => reject(new Error(`auth lookup did not answer within ${ms} ms`)), ms)\n })\n return Promise.race([promise, deadline]).finally(() => clearTimeout(timer))\n}\n","/**\n * Honest turn settlement, checked over a product's turn records.\n *\n * A turn ends in exactly one terminal state: an answer, a typed failure the\n * person can Retry, or a Stop the person chose, counted on its own. It gets\n * there within 15 minutes of its owner stopping. A failure belongs to the time\n * the turn was requested, not the time it was noticed, and how late it was\n * noticed is measured. A run that completed with a tool call the Sandbox\n * aborted stopped mid-work, so it is a failure.\n *\n * On 2026-10-10 GTM held turns in five places (turn status, admissions,\n * Workflows, Sandbox sessions and the sidecar tracker) and they disagreed:\n * 51 stream rows stayed `running` after their producers died, failure notices\n * arrived 8–11 hours late, and completions with aborted tools counted as\n * answers. {@link checkTurnSettlement} reads one normalized record per turn,\n * however the product derives it, and names every turn that breaks the rule.\n */\nimport { LAUNCH_BUDGETS, type InvariantVerdict } from './catalog.js'\n\nexport type TurnTerminalState = 'answered' | 'failed' | 'stopped'\n\n/** One turn as its product records it, normalized. Times are Unix ms. */\nexport interface SettledTurnRecord {\n turnId: string\n /** When the request was accepted (the user message was saved). */\n requestedAt: number\n /** Every terminal state recorded for the turn, in order; exactly one is honest. */\n terminals: ReadonlyArray<{\n state: TurnTerminalState\n /** When the terminal state was recorded. */\n at: number\n /** A typed failure code, for `failed`. */\n code?: string\n /** Whether the person is offered Retry, for `failed`. */\n retryable?: boolean\n /** The time the failure is reported under; it must be the request time. */\n attributedTo?: number\n }>\n /** When the turn's owner stopped: for a leased stream, when its lease expired. Null while an owner holds it. */\n ownerEndedAt: number | null\n /** A completed run in which the Sandbox aborted a tool call. */\n abortedToolCall?: boolean\n}\n\nexport interface TurnSettlementCounts {\n turns: number\n answered: number\n failed: number\n stopped: number\n open: number\n /** Most minutes between an owner stopping and its turn's terminal state. */\n maxDetectionLagMinutes: number\n}\n\n/**\n * Check turn records against honest settlement at `now`.\n * A turn still open whose owner has not stopped is fine; one whose owner stopped\n * more than {@link LAUNCH_BUDGETS.settleWithinMs} ago is not.\n */\nexport function checkTurnSettlement(\n turns: readonly SettledTurnRecord[],\n options: { now: number; settleWithinMs?: number; subject?: string },\n): InvariantVerdict {\n const within = options.settleWithinMs ?? LAUNCH_BUDGETS.settleWithinMs\n const findings: string[] = []\n const counts: TurnSettlementCounts = { turns: turns.length, answered: 0, failed: 0, stopped: 0, open: 0, maxDetectionLagMinutes: 0 }\n for (const turn of turns) {\n const label = `turn ${turn.turnId}`\n if (turn.terminals.length > 1) {\n findings.push(`${label} has ${turn.terminals.length} terminal states (${turn.terminals.map((terminal) => terminal.state).join(' then ')}); a turn ends once`)\n }\n const terminal = turn.terminals[0]\n if (!terminal) {\n counts.open += 1\n if (turn.ownerEndedAt !== null && options.now - turn.ownerEndedAt > within) {\n findings.push(`${label} is still open ${minutes(options.now - turn.ownerEndedAt)} min after its owner stopped; the most is ${minutes(within)}`)\n }\n continue\n }\n counts[terminal.state] += 1\n if (turn.ownerEndedAt !== null) {\n const lag = terminal.at - turn.ownerEndedAt\n counts.maxDetectionLagMinutes = Math.max(counts.maxDetectionLagMinutes, minutes(lag))\n if (lag > within) findings.push(`${label} was settled ${minutes(lag)} min after its owner stopped; the most is ${minutes(within)}`)\n }\n if (terminal.state === 'answered' && turn.abortedToolCall) {\n findings.push(`${label} is counted as an answer, but the Sandbox aborted a tool call in it; it stopped mid-work and is a failure`)\n }\n if (terminal.state === 'failed') {\n if (!terminal.code) findings.push(`${label} failed without a typed failure code`)\n if (terminal.retryable !== true) findings.push(`${label} failed without offering Retry`)\n if (terminal.attributedTo !== undefined && terminal.attributedTo !== turn.requestedAt) {\n findings.push(`${label}'s failure is reported at ${new Date(terminal.attributedTo).toISOString()}, not its request time ${new Date(turn.requestedAt).toISOString()}`)\n }\n }\n if (terminal.state === 'stopped' && terminal.code && /fail|error/i.test(terminal.code)) {\n findings.push(`${label} was stopped by the person but carries failure code ${terminal.code}; a Stop is counted on its own`)\n }\n }\n return {\n invariant: 'honest-settlement',\n subject: options.subject ?? 'turns',\n pass: findings.length === 0,\n details: findings.length ? findings : [\n `${counts.turns} turns: ${counts.answered} answered, ${counts.failed} failed with Retry, ${counts.stopped} stopped, ${counts.open} held by a live owner`,\n `longest detection lag ${counts.maxDetectionLagMinutes} min`,\n ],\n data: { ...counts },\n }\n}\n\nfunction minutes(ms: number): number {\n return Math.round((ms / 60_000) * 10) / 10\n}\n\n/**\n * The failure shapes GTM shipped on 2026-10-10, as scenarios a product seeds\n * into its own storage, settles with its own scheduled jobs, and reads back as\n * {@link SettledTurnRecord}s. `expect` is the one honest terminal state.\n */\nexport interface SettlementScenario {\n id: string\n /** What happened to the turn, for the product's seed step. */\n story: string\n /** Minutes before the check's `now` that the request was accepted. */\n requestedMinutesAgo: number\n /**\n * Minutes before `now` that the owner stopped (for a stream, its lease\n * expired), or null for a live owner. Every scenario that stopped did so 12\n * minutes ago, so an app that settles within 15 minutes has settled it by `now`.\n */\n ownerEndedMinutesAgo: number | null\n expect: TurnTerminalState | 'open'\n}\n\nexport const SETTLEMENT_SCENARIOS: readonly SettlementScenario[] = [\n {\n id: 'orphaned-stream',\n story: 'The Worker streaming the turn died; its stream row is still running, no admission or Workflow holds it, and its 5-minute lease expired 12 minutes ago.',\n requestedMinutesAgo: 30,\n ownerEndedMinutesAgo: 12,\n expect: 'failed',\n },\n {\n id: 'abandoned-before-admission',\n story: 'The user message was saved 12 minutes ago, then the request died before any completion owner admitted the turn: no reply, no stream, no admission.',\n requestedMinutesAgo: 12,\n ownerEndedMinutesAgo: 12,\n expect: 'failed',\n },\n {\n id: 'runtime-restarted',\n story: 'The admitted run\\'s runtime restarted mid-turn 12 minutes ago: the session still reports running while its execution ledger failed the run (terminal event interrupted-done).',\n requestedMinutesAgo: 40,\n ownerEndedMinutesAgo: 12,\n expect: 'failed',\n },\n {\n id: 'completed-with-aborted-tool',\n story: 'The run reported completed 12 minutes ago, but a tool part ended with \"Tool execution aborted\".',\n requestedMinutesAgo: 20,\n ownerEndedMinutesAgo: 12,\n expect: 'failed',\n },\n {\n id: 'user-stop',\n story: 'The person pressed Stop 12 minutes ago while the turn ran.',\n requestedMinutesAgo: 20,\n ownerEndedMinutesAgo: 12,\n expect: 'stopped',\n },\n {\n id: 'answered',\n story: 'The turn answered normally 12 minutes ago.',\n requestedMinutesAgo: 20,\n ownerEndedMinutesAgo: 12,\n expect: 'answered',\n },\n {\n id: 'live-long-turn',\n story: 'A long turn is still running under an owner that renews its lease.',\n requestedMinutesAgo: 90,\n ownerEndedMinutesAgo: null,\n expect: 'open',\n },\n]\n\n/**\n * Check a product's settlement of {@link SETTLEMENT_SCENARIOS}: each scenario's\n * record ends in its expected state, and all of them settle honestly. The\n * product seeds each scenario at `now`, runs its own recovery and settlement\n * jobs at `now`, and reads the records back keyed by scenario id.\n */\nexport function checkSettlementScenarios(\n records: Readonly<Record<string, SettledTurnRecord | undefined>>,\n options: { now: number; scenarios?: readonly SettlementScenario[] },\n): InvariantVerdict {\n const scenarios = options.scenarios ?? SETTLEMENT_SCENARIOS\n const findings: string[] = []\n const present: SettledTurnRecord[] = []\n for (const scenario of scenarios) {\n const record = records[scenario.id]\n if (!record) {\n findings.push(`scenario ${scenario.id}: no turn record was read back`)\n continue\n }\n present.push(record)\n const state = record.terminals[0]?.state ?? 'open'\n if (state !== scenario.expect) findings.push(`scenario ${scenario.id}: ended ${state}, expected ${scenario.expect} (${scenario.story})`)\n }\n const honest = checkTurnSettlement(present, { now: options.now, subject: 'settlement scenarios' })\n if (!honest.pass) findings.push(...honest.details)\n return {\n invariant: 'honest-settlement',\n subject: 'settlement scenarios',\n pass: findings.length === 0,\n details: findings.length ? findings : [`${scenarios.length} failure scenarios settle to their honest state`, ...honest.details],\n data: honest.data,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAoBO,IAAM,qBAAqB;AAE3B,IAAM,0BAA0B;AACvC,IAAM,cAAc;AAGb,IAAM,oBAAN,cAAgC,MAAM;AAAC;AAuCvC,SAAS,sBAAyB,SAAqD;AAC5F,QAAM,QAAQ,KAAK,IAAI,QAAQ,SAAS,oBAAoB,kBAAkB;AAC9E,QAAM,aAAa,QAAQ,cAAc;AACzC,QAAM,MAAM,QAAQ,QAAQ,MAAM,KAAK,IAAI;AAC3C,QAAM,OAAO,QAAQ,SAAS,CAAC,SAAS,WAAW,QAAQ,KAAK,SAAS,MAAM;AAC/E,QAAM,UAAU,oBAAI,IAAsB;AAE1C,SAAO;AAAA,IACL,MAAM,O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AgC;AACrE;AAAA,IACF;AACA,YAAQ,KAAK,MAAM;AACnB,UAAM,QAAQ,OAAO,UAAU,CAAC,GAAG,SAAS;AAC5C,QAAI,UAAU,SAAS,OAAQ,UAAS,KAAK,YAAY,SAAS,EAAE,WAAW,KAAK,cAAc,SAAS,MAAM,KAAK,SAAS,KAAK,GAAG;AAAA,EACzI;AACA,QAAM,SAAS,oBAAoB,SAAS,EAAE,KAAK,QAAQ,KAAK,SAAS,uBAAuB,CAAC;AACjG,MAAI,CAAC,OAAO,KAAM,UAAS,KAAK,GAAG,OAAO,OAAO;AACjD,SAAO;AAAA,IACL,WAAW;AAAA,IACX,SAAS;AAAA,IACT,MAAM,SAAS,WAAW;AAAA,IAC1B,SAAS,SAAS,SAAS,WAAW,CAAC,GAAG,UAAU,MAAM,mDAAmD,GAAG,OAAO,OAAO;AAAA,IAC9H,MAAM,OAAO;AAAA,EACf;AACF;","names":["terminal"]}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alarms that fire at 80% of a platform limit, before the limit fails a customer.
|
|
3
|
+
*
|
|
4
|
+
* On 2026-10-10 GTM recorded 19 platform-limit events (Worker memory, D1,
|
|
5
|
+
* sandbox disk, snapshot count, key rate) and learned of each one after it had
|
|
6
|
+
* failed someone. Each resource here has a declared limit; an observation at
|
|
7
|
+
* or above 80% of it delivers a warning, and at or above the limit a critical
|
|
8
|
+
* alert, through the same {@link AlertSink} the turn-health sweep uses (wrap it
|
|
9
|
+
* in `createThrottledAlertSink` so a hot loop pages once).
|
|
10
|
+
*
|
|
11
|
+
* Where each observation comes from:
|
|
12
|
+
* - `d1-rows-per-query`: {@link withD1LimitAlarms} reads `meta.rows_read` on every query.
|
|
13
|
+
* - `worker-memory`: workerd exposes no heap reading, so the D1 wrapper observes
|
|
14
|
+
* the bytes each response returns (the reads that overran memory today), and a
|
|
15
|
+
* tail consumer reports an `exceededMemory` outcome at the full limit.
|
|
16
|
+
* - `sandbox-disk` and `snapshot-count`: the Sandbox SDK's box usage and snapshot list, read on a schedule.
|
|
17
|
+
* - `key-rate`: the per-key request counter the operator API keeps.
|
|
18
|
+
*/
|
|
19
|
+
import type { AlertSink } from '../turn-health/sink.js';
|
|
20
|
+
export type LimitResource = 'worker-memory' | 'd1-rows-per-query' | 'sandbox-disk' | 'snapshot-count' | 'key-rate';
|
|
21
|
+
export declare const LIMIT_RESOURCES: readonly LimitResource[];
|
|
22
|
+
/** The limit of each resource, in its own unit: bytes, rows, bytes, snapshots, requests per minute. */
|
|
23
|
+
export type LimitBudgets = Readonly<Record<LimitResource, number>>;
|
|
24
|
+
export type LimitLevel = 'ok' | 'warning' | 'critical';
|
|
25
|
+
export interface LimitObservation {
|
|
26
|
+
resource: LimitResource;
|
|
27
|
+
used: number;
|
|
28
|
+
limit: number;
|
|
29
|
+
/** used / limit. */
|
|
30
|
+
share: number;
|
|
31
|
+
level: LimitLevel;
|
|
32
|
+
}
|
|
33
|
+
export interface LimitAlarms {
|
|
34
|
+
readonly budgets: LimitBudgets;
|
|
35
|
+
/** Record one reading; a warning or critical reading is delivered before this resolves. */
|
|
36
|
+
observe(resource: LimitResource, used: number, context?: {
|
|
37
|
+
subject?: string;
|
|
38
|
+
detail?: string;
|
|
39
|
+
}): Promise<LimitObservation>;
|
|
40
|
+
}
|
|
41
|
+
export interface LimitAlarmOptions {
|
|
42
|
+
product: string;
|
|
43
|
+
budgets: LimitBudgets;
|
|
44
|
+
sink: AlertSink;
|
|
45
|
+
/** Default {@link LAUNCH_BUDGETS.alarmRatio}; a product cannot raise it past the limit itself. */
|
|
46
|
+
ratio?: number;
|
|
47
|
+
now?: () => number;
|
|
48
|
+
}
|
|
49
|
+
export declare function createLimitAlarms(options: LimitAlarmOptions): LimitAlarms;
|
|
50
|
+
/** The D1 surface the wrapper measures; Cloudflare `D1Database` satisfies it. */
|
|
51
|
+
interface D1Measurable {
|
|
52
|
+
prepare(sql: string): unknown;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The D1 binding with each query's rows read and response size observed.
|
|
56
|
+
* Rows read come from `meta.rows_read` (the rows D1 scanned, not only those
|
|
57
|
+
* returned). The response size is estimated from three sampled rows, so the
|
|
58
|
+
* wrapper never serializes a whole response: that copy would double the memory
|
|
59
|
+
* of the very read it watches.
|
|
60
|
+
*/
|
|
61
|
+
export declare function withD1LimitAlarms<D extends D1Measurable>(d1: D, alarms: LimitAlarms, options?: {
|
|
62
|
+
subject?: string;
|
|
63
|
+
}): D;
|
|
64
|
+
/** The serialized size of a result set, from its first, middle and last rows. */
|
|
65
|
+
export declare function estimatedBytes(results: readonly unknown[]): number;
|
|
66
|
+
export {};
|