@aisac/cli 0.0.0-stage → 0.4.0-beta.1
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/README.md +159 -2
- package/dist/assets/claude/.claude-plugin/plugin.json +147 -0
- package/dist/assets/claude/hooks/context-compaction.ts +6930 -0
- package/dist/assets/claude/hooks/hooks.json +3 -0
- package/dist/assets/claude/hooks/native-host-contract-runtime.ts +18 -0
- package/dist/assets/claude/hooks/native-host-contract.test.ts +51 -0
- package/dist/assets/claude/hooks/provider-window.js +263 -0
- package/dist/assets/claude/hooks/state-fitting.d.mts +47 -0
- package/dist/assets/claude/hooks/state-fitting.mjs +324 -0
- package/dist/assets/codex-context-compaction.mjs +4689 -0
- package/dist/assets/context-selection-fitting.mjs +194 -0
- package/dist/assets/context-state-fitting.mjs +324 -0
- package/dist/assets/context-telemetry-reporter.mjs +305 -0
- package/dist/assets/jev-paid-pilot-terminal.mjs +1066 -0
- package/dist/claude-checkpoint.mjs +1338 -0
- package/dist/claude-compatibility.mjs +506 -0
- package/dist/claude-observation.mjs +1338 -0
- package/dist/cli.mjs +736 -0
- package/dist/codex-compatibility.mjs +481 -0
- package/dist/codex-diagnostics.mjs +447 -0
- package/dist/codex-permissions.mjs +590 -0
- package/dist/codex-protocol-fingerprint.json +131 -0
- package/dist/context-telemetry.mjs +257 -0
- package/dist/engineering.mjs +190 -0
- package/dist/launch.mjs +646 -0
- package/dist/mcp.mjs +962 -0
- package/dist/monitor.mjs +133 -0
- package/dist/package.json +5 -0
- package/dist/platform.mjs +126 -0
- package/dist/policy.mjs +218 -0
- package/dist/state.mjs +408 -0
- package/dist/telemetry-report.mjs +133 -0
- package/package.json +24 -4
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"native": "0.164.0",
|
|
4
|
+
"consumed_pagination": {
|
|
5
|
+
"method": "thread/turns/list",
|
|
6
|
+
"request_schema": "v2/ThreadTurnsListParams.json",
|
|
7
|
+
"response_schema": "v2/ThreadTurnsListResponse.json",
|
|
8
|
+
"request_schema_sha256": "29717980579b77eb4c7cbd1118dd238fcb3e010f3ef5236d74bd4e4f6e02c005",
|
|
9
|
+
"response_schema_sha256": "f46dddf14b7b6e8e484cb998a7d0b2efa3250167b9f2c366b83b22fe494c250f",
|
|
10
|
+
"graph_sha256": "20ba733654372954440b0cbee2964230934d064b9c2c3ee3c4ed850b08f7e370",
|
|
11
|
+
"ordering": "asc",
|
|
12
|
+
"items_view": "full"
|
|
13
|
+
},
|
|
14
|
+
"roots": {
|
|
15
|
+
"v2/ThreadTurnsListParams.json": {
|
|
16
|
+
"methods": null,
|
|
17
|
+
"sha256": "29717980579b77eb4c7cbd1118dd238fcb3e010f3ef5236d74bd4e4f6e02c005"
|
|
18
|
+
},
|
|
19
|
+
"v2/ThreadTurnsListResponse.json": {
|
|
20
|
+
"methods": null,
|
|
21
|
+
"sha256": "f46dddf14b7b6e8e484cb998a7d0b2efa3250167b9f2c366b83b22fe494c250f"
|
|
22
|
+
},
|
|
23
|
+
"ClientRequest.json": {
|
|
24
|
+
"methods": [
|
|
25
|
+
"initialize",
|
|
26
|
+
"thread/start",
|
|
27
|
+
"thread/resume",
|
|
28
|
+
"thread/fork",
|
|
29
|
+
"thread/delete",
|
|
30
|
+
"thread/unsubscribe",
|
|
31
|
+
"thread/compact/start",
|
|
32
|
+
"thread/list",
|
|
33
|
+
"thread/read",
|
|
34
|
+
"thread/turns/list",
|
|
35
|
+
"thread/items/list",
|
|
36
|
+
"thread/inject_items",
|
|
37
|
+
"turn/start",
|
|
38
|
+
"mcpServer/tool/call",
|
|
39
|
+
"config/read",
|
|
40
|
+
"account/read"
|
|
41
|
+
],
|
|
42
|
+
"sha256": "415beba5414aeb1af34954de5c3d76c0f5a1c78f30f0a51cb291c471e920b365"
|
|
43
|
+
},
|
|
44
|
+
"ClientNotification.json": {
|
|
45
|
+
"methods": ["initialized"],
|
|
46
|
+
"sha256": "c6c3ba53650b9dd3aadea19e732ff67b566b162fce7abac361de0d03c91e6624"
|
|
47
|
+
},
|
|
48
|
+
"ServerRequest.json": {
|
|
49
|
+
"methods": [
|
|
50
|
+
"item/commandExecution/requestApproval",
|
|
51
|
+
"item/fileChange/requestApproval",
|
|
52
|
+
"item/tool/requestUserInput",
|
|
53
|
+
"mcpServer/elicitation/request",
|
|
54
|
+
"item/permissions/requestApproval",
|
|
55
|
+
"item/tool/call",
|
|
56
|
+
"account/chatgptAuthTokens/refresh",
|
|
57
|
+
"attestation/generate",
|
|
58
|
+
"currentTime/read",
|
|
59
|
+
"applyPatchApproval",
|
|
60
|
+
"execCommandApproval"
|
|
61
|
+
],
|
|
62
|
+
"sha256": "f6dbc427473ebb6ffd634cb22a45f3330c48ea9719e2c54fb3f9b0c39f76ef22"
|
|
63
|
+
},
|
|
64
|
+
"ServerNotification.json": {
|
|
65
|
+
"methods": [
|
|
66
|
+
"error",
|
|
67
|
+
"thread/started",
|
|
68
|
+
"thread/status/changed",
|
|
69
|
+
"thread/deleted",
|
|
70
|
+
"thread/closed",
|
|
71
|
+
"thread/tokenUsage/updated",
|
|
72
|
+
"turn/started",
|
|
73
|
+
"turn/completed",
|
|
74
|
+
"item/started",
|
|
75
|
+
"item/completed",
|
|
76
|
+
"item/agentMessage/delta",
|
|
77
|
+
"item/mcpToolCall/progress",
|
|
78
|
+
"thread/compacted",
|
|
79
|
+
"warning"
|
|
80
|
+
],
|
|
81
|
+
"sha256": "590a474f19639e9c1b57238354c89ab0ef5f9d4022561ba2ed5f286cdf254181"
|
|
82
|
+
},
|
|
83
|
+
"JSONRPCResponse.json": {
|
|
84
|
+
"methods": null,
|
|
85
|
+
"sha256": "7260ccd0d4c3aeebf068cbbd7e9cb8db122343a981f00c46c3658253a6e9bed5"
|
|
86
|
+
},
|
|
87
|
+
"JSONRPCNotification.json": {
|
|
88
|
+
"methods": null,
|
|
89
|
+
"sha256": "5573b596fa9025a65fc85af984e6f55189a8ab86d3e156efa301ca0eb586f089"
|
|
90
|
+
},
|
|
91
|
+
"v1/InitializeResponse.json": {
|
|
92
|
+
"methods": null,
|
|
93
|
+
"sha256": "3949f87454760303004691e3b22712ffcf3455bf430e132a5e6db510be888306"
|
|
94
|
+
},
|
|
95
|
+
"v2/ConfigReadResponse.json": {
|
|
96
|
+
"methods": null,
|
|
97
|
+
"sha256": "9fc9bdea31211e362cc7d5d66d0c0213c1dafc0b902e3a981866a88328476ea6"
|
|
98
|
+
},
|
|
99
|
+
"v2/McpServerToolCallResponse.json": {
|
|
100
|
+
"methods": null,
|
|
101
|
+
"sha256": "6f67616ad852f8cb2fc34803bfb8a0811817238a9ff74742368d904fb1f67b94"
|
|
102
|
+
},
|
|
103
|
+
"v2/ThreadCompactStartResponse.json": {
|
|
104
|
+
"methods": null,
|
|
105
|
+
"sha256": "30597c637d705b8086eced83a667814d8676d820164e93f55be5e644a085f07a"
|
|
106
|
+
},
|
|
107
|
+
"v2/ThreadForkResponse.json": {
|
|
108
|
+
"methods": null,
|
|
109
|
+
"sha256": "b1e26ab55f9fcbe108d325491d29678040d0776953093626fbcd9608593df31c"
|
|
110
|
+
},
|
|
111
|
+
"v2/ThreadInjectItemsResponse.json": {
|
|
112
|
+
"methods": null,
|
|
113
|
+
"sha256": "30597c637d705b8086eced83a667814d8676d820164e93f55be5e644a085f07a"
|
|
114
|
+
},
|
|
115
|
+
"v2/ThreadReadResponse.json": {
|
|
116
|
+
"methods": null,
|
|
117
|
+
"sha256": "2de20539e09dd7110bd5b705f519288329bacc162d1c14c0c4af33ff27085e24"
|
|
118
|
+
},
|
|
119
|
+
"v2/ThreadResumeResponse.json": {
|
|
120
|
+
"methods": null,
|
|
121
|
+
"sha256": "bf6130a2dd9494a878e406f2d13535930ebc56f1a66cc1f597528537652a6a65"
|
|
122
|
+
},
|
|
123
|
+
"v2/ThreadStartResponse.json": {
|
|
124
|
+
"methods": null,
|
|
125
|
+
"sha256": "b1e26ab55f9fcbe108d325491d29678040d0776953093626fbcd9608593df31c"
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
"requiredClientFlags": ["app-server", "--listen", "stdio://"],
|
|
129
|
+
"requiredTerminalFlags": ["--remote", "unix://"],
|
|
130
|
+
"generatedFrom": "codex app-server generate-json-schema --experimental"
|
|
131
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser safe v1 wire contract for content-free context telemetry.
|
|
3
|
+
*
|
|
4
|
+
* Keep this module free of Node, database and provider imports. Adapters use
|
|
5
|
+
* the same validator as the server boundary so an event rejected locally can
|
|
6
|
+
* never contain a larger or richer shape than the server accepts.
|
|
7
|
+
*/
|
|
8
|
+
export const CONTEXT_TELEMETRY_SCHEMA_VERSION = 1;
|
|
9
|
+
export const CONTEXT_TELEMETRY_MAX_BYTES = 8 * 1024;
|
|
10
|
+
export const CONTEXT_TELEMETRY_RETENTION_DAYS = 30;
|
|
11
|
+
export const CONTEXT_TELEMETRY_FRESHNESS_SECONDS = 180;
|
|
12
|
+
export const CONTEXT_TELEMETRY_PHASES = [
|
|
13
|
+
'connected',
|
|
14
|
+
'usage',
|
|
15
|
+
'disconnected',
|
|
16
|
+
'plan_prepared',
|
|
17
|
+
'selection_completed',
|
|
18
|
+
'notes_written',
|
|
19
|
+
'native_completed',
|
|
20
|
+
'recovery_attached',
|
|
21
|
+
'applied',
|
|
22
|
+
'fallback',
|
|
23
|
+
'failed',
|
|
24
|
+
'uncertain',
|
|
25
|
+
];
|
|
26
|
+
const PHASES = new Set(CONTEXT_TELEMETRY_PHASES);
|
|
27
|
+
const REASONS = new Set([
|
|
28
|
+
'provider_unavailable',
|
|
29
|
+
'insufficient_reduction',
|
|
30
|
+
'protected_context',
|
|
31
|
+
'stale_context',
|
|
32
|
+
'rate_limited',
|
|
33
|
+
'native_error',
|
|
34
|
+
'recovery_pending',
|
|
35
|
+
'recovery_uncertain',
|
|
36
|
+
'unsupported',
|
|
37
|
+
'unknown',
|
|
38
|
+
]);
|
|
39
|
+
const UNITS = new Set(['tokens', 'estimated_tokens', 'characters']);
|
|
40
|
+
const CLIENTS = new Set(['claude-code', 'codex-cli']);
|
|
41
|
+
const OPERATION_PHASES = new Set([
|
|
42
|
+
'plan_prepared',
|
|
43
|
+
'selection_completed',
|
|
44
|
+
'notes_written',
|
|
45
|
+
'native_completed',
|
|
46
|
+
'recovery_attached',
|
|
47
|
+
'applied',
|
|
48
|
+
'fallback',
|
|
49
|
+
'failed',
|
|
50
|
+
'uncertain',
|
|
51
|
+
]);
|
|
52
|
+
const CLAUDE_PHASES = new Set([
|
|
53
|
+
'connected',
|
|
54
|
+
'usage',
|
|
55
|
+
'disconnected',
|
|
56
|
+
'plan_prepared',
|
|
57
|
+
'notes_written',
|
|
58
|
+
'native_completed',
|
|
59
|
+
'recovery_attached',
|
|
60
|
+
'applied',
|
|
61
|
+
'fallback',
|
|
62
|
+
'failed',
|
|
63
|
+
'uncertain',
|
|
64
|
+
]);
|
|
65
|
+
const CODEX_PHASES = new Set([
|
|
66
|
+
'connected',
|
|
67
|
+
'usage',
|
|
68
|
+
'disconnected',
|
|
69
|
+
'selection_completed',
|
|
70
|
+
'notes_written',
|
|
71
|
+
'native_completed',
|
|
72
|
+
'recovery_attached',
|
|
73
|
+
'failed',
|
|
74
|
+
'fallback',
|
|
75
|
+
'uncertain',
|
|
76
|
+
]);
|
|
77
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
78
|
+
const SESSION_KEY = /^[0-9a-f]{64}$/;
|
|
79
|
+
const UTC_TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?Z$/;
|
|
80
|
+
const SEMVER = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
81
|
+
const MAX_SEQUENCE = 2_147_483_647;
|
|
82
|
+
const MAX_COUNT = 1_000_000;
|
|
83
|
+
const COUNT_KEYS = ['eligible', 'protected', 'kept', 'reduced', 'dropped', 'selected'];
|
|
84
|
+
export class ContextTelemetryValidationError extends Error {
|
|
85
|
+
constructor(message) {
|
|
86
|
+
super(message);
|
|
87
|
+
this.name = 'ContextTelemetryValidationError';
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function fail(message) {
|
|
91
|
+
throw new ContextTelemetryValidationError(message);
|
|
92
|
+
}
|
|
93
|
+
function isRecord(value) {
|
|
94
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
95
|
+
}
|
|
96
|
+
function closed(value, allowed) {
|
|
97
|
+
const accepted = new Set(allowed);
|
|
98
|
+
for (const key of Object.keys(value))
|
|
99
|
+
if (!accepted.has(key))
|
|
100
|
+
fail('unknown telemetry field');
|
|
101
|
+
}
|
|
102
|
+
function stringField(value, max, pattern) {
|
|
103
|
+
if (typeof value !== 'string' || value.length === 0 || value.length > max)
|
|
104
|
+
fail('invalid telemetry string');
|
|
105
|
+
if (pattern !== undefined && !pattern.test(value))
|
|
106
|
+
fail('invalid telemetry string');
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
function safeInteger(value, min, max) {
|
|
110
|
+
if (!Number.isSafeInteger(value) || value < min || value > max)
|
|
111
|
+
fail('invalid telemetry integer');
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
function measurement(value) {
|
|
115
|
+
if (!isRecord(value))
|
|
116
|
+
fail('invalid telemetry measurement');
|
|
117
|
+
closed(value, ['unit', 'used', 'capacity']);
|
|
118
|
+
if (typeof value.unit !== 'string' || !UNITS.has(value.unit))
|
|
119
|
+
fail('invalid measurement unit');
|
|
120
|
+
const used = safeInteger(value.used, 0, Number.MAX_SAFE_INTEGER);
|
|
121
|
+
let capacity;
|
|
122
|
+
if (value.capacity !== undefined) {
|
|
123
|
+
capacity = safeInteger(value.capacity, 1, Number.MAX_SAFE_INTEGER);
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
unit: value.unit,
|
|
127
|
+
used,
|
|
128
|
+
...(capacity === undefined ? {} : { capacity }),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function counts(value) {
|
|
132
|
+
if (!isRecord(value))
|
|
133
|
+
fail('invalid telemetry counts');
|
|
134
|
+
closed(value, COUNT_KEYS);
|
|
135
|
+
const result = {};
|
|
136
|
+
for (const key of COUNT_KEYS) {
|
|
137
|
+
if (value[key] !== undefined)
|
|
138
|
+
result[key] = safeInteger(value[key], 0, MAX_COUNT);
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
function bytes(value) {
|
|
143
|
+
try {
|
|
144
|
+
return new TextEncoder().encode(JSON.stringify(value)).byteLength;
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return Number.POSITIVE_INFINITY;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** Validate and return the closed v1 report without adding fields. */
|
|
151
|
+
export function parseContextTelemetryReport(value, now = new Date()) {
|
|
152
|
+
if (bytes(value) > CONTEXT_TELEMETRY_MAX_BYTES)
|
|
153
|
+
fail('telemetry report is too large');
|
|
154
|
+
if (!isRecord(value))
|
|
155
|
+
fail('telemetry report must be an object');
|
|
156
|
+
closed(value, [
|
|
157
|
+
'schema_version',
|
|
158
|
+
'event_id',
|
|
159
|
+
'session_key',
|
|
160
|
+
'instance_id',
|
|
161
|
+
'sequence',
|
|
162
|
+
'observed_at',
|
|
163
|
+
'client',
|
|
164
|
+
'client_version',
|
|
165
|
+
'phase',
|
|
166
|
+
'operation_id',
|
|
167
|
+
'trigger_percent',
|
|
168
|
+
'measurement',
|
|
169
|
+
'before',
|
|
170
|
+
'after',
|
|
171
|
+
'counts',
|
|
172
|
+
'reason',
|
|
173
|
+
]);
|
|
174
|
+
if (value.schema_version !== 1)
|
|
175
|
+
fail('unsupported telemetry schema');
|
|
176
|
+
const eventId = stringField(value.event_id, 36, UUID);
|
|
177
|
+
const sessionKey = stringField(value.session_key, 64, SESSION_KEY);
|
|
178
|
+
const instanceId = stringField(value.instance_id, 36, UUID);
|
|
179
|
+
const sequence = safeInteger(value.sequence, 1, MAX_SEQUENCE);
|
|
180
|
+
const observedAt = stringField(value.observed_at, 30, UTC_TIMESTAMP);
|
|
181
|
+
const observedMillis = Date.parse(observedAt);
|
|
182
|
+
if (!Number.isFinite(observedMillis))
|
|
183
|
+
fail('invalid telemetry timestamp');
|
|
184
|
+
const nowMillis = now.getTime();
|
|
185
|
+
if (observedMillis > nowMillis + 5 * 60_000 || observedMillis < nowMillis - 24 * 60 * 60_000)
|
|
186
|
+
fail('telemetry timestamp outside accepted window');
|
|
187
|
+
if (typeof value.client !== 'string' || !CLIENTS.has(value.client))
|
|
188
|
+
fail('invalid telemetry client');
|
|
189
|
+
const client = value.client;
|
|
190
|
+
const clientVersion = stringField(value.client_version, 40, SEMVER);
|
|
191
|
+
if (typeof value.phase !== 'string' || !PHASES.has(value.phase))
|
|
192
|
+
fail('invalid telemetry phase');
|
|
193
|
+
const phase = value.phase;
|
|
194
|
+
if (!(client === 'claude-code' ? CLAUDE_PHASES : CODEX_PHASES).has(phase))
|
|
195
|
+
fail('phase is incompatible with client');
|
|
196
|
+
const operationId = value.operation_id === undefined ? undefined : stringField(value.operation_id, 36, UUID);
|
|
197
|
+
if (OPERATION_PHASES.has(phase) !== (operationId !== undefined))
|
|
198
|
+
fail('operation id is required for this phase');
|
|
199
|
+
const triggerPercent = value.trigger_percent === undefined ? undefined : value.trigger_percent;
|
|
200
|
+
if (triggerPercent !== undefined &&
|
|
201
|
+
(typeof triggerPercent !== 'number' ||
|
|
202
|
+
!Number.isFinite(triggerPercent) ||
|
|
203
|
+
triggerPercent < 1 ||
|
|
204
|
+
triggerPercent > 95))
|
|
205
|
+
fail('invalid telemetry trigger');
|
|
206
|
+
const reportMeasurement = value.measurement === undefined ? undefined : measurement(value.measurement);
|
|
207
|
+
const before = value.before === undefined ? undefined : measurement(value.before);
|
|
208
|
+
const after = value.after === undefined ? undefined : measurement(value.after);
|
|
209
|
+
const reportCounts = value.counts === undefined ? undefined : counts(value.counts);
|
|
210
|
+
const reason = value.reason === undefined ? undefined : value.reason;
|
|
211
|
+
if (phase === 'usage' && reportMeasurement === undefined)
|
|
212
|
+
fail('usage requires measurement');
|
|
213
|
+
if ((phase === 'connected' || phase === 'disconnected') &&
|
|
214
|
+
(reportMeasurement !== undefined || before !== undefined || after !== undefined))
|
|
215
|
+
fail('session phase cannot carry measurements');
|
|
216
|
+
if (!OPERATION_PHASES.has(phase) &&
|
|
217
|
+
(before !== undefined ||
|
|
218
|
+
after !== undefined ||
|
|
219
|
+
reportCounts !== undefined ||
|
|
220
|
+
reason !== undefined))
|
|
221
|
+
fail('session phase cannot carry operation results');
|
|
222
|
+
if (before !== undefined &&
|
|
223
|
+
after !== undefined &&
|
|
224
|
+
(before.unit !== after.unit || before.capacity !== after.capacity))
|
|
225
|
+
fail('before and after measurements must match');
|
|
226
|
+
if (phase === 'applied' && after === undefined)
|
|
227
|
+
fail('applied requires an actual measurement');
|
|
228
|
+
if (reason !== undefined && (typeof reason !== 'string' || !REASONS.has(reason)))
|
|
229
|
+
fail('invalid telemetry reason');
|
|
230
|
+
return {
|
|
231
|
+
schema_version: 1,
|
|
232
|
+
event_id: eventId,
|
|
233
|
+
session_key: sessionKey,
|
|
234
|
+
instance_id: instanceId,
|
|
235
|
+
sequence,
|
|
236
|
+
observed_at: observedAt,
|
|
237
|
+
client,
|
|
238
|
+
client_version: clientVersion,
|
|
239
|
+
phase,
|
|
240
|
+
...(operationId === undefined ? {} : { operation_id: operationId }),
|
|
241
|
+
...(triggerPercent === undefined ? {} : { trigger_percent: triggerPercent }),
|
|
242
|
+
...(reportMeasurement === undefined ? {} : { measurement: reportMeasurement }),
|
|
243
|
+
...(before === undefined ? {} : { before }),
|
|
244
|
+
...(after === undefined ? {} : { after }),
|
|
245
|
+
...(reportCounts === undefined ? {} : { counts: reportCounts }),
|
|
246
|
+
...(reason === undefined ? {} : { reason: reason }),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
export function isContextTelemetryReport(value, now) {
|
|
250
|
+
try {
|
|
251
|
+
parseContextTelemetryReport(value, now);
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
catch {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AISAC Engineering license check (ADR-0047 wave E3).
|
|
5
|
+
*
|
|
6
|
+
* The server is the only authority. This answer is shown to the user and
|
|
7
|
+
* never unlocks anything locally: every failure is reported as
|
|
8
|
+
* `unavailable`, never as licensed. The account token goes only to the
|
|
9
|
+
* configured AI Brain origin, redirects are refused, and the response is
|
|
10
|
+
* size-bounded and validated before any field is displayed.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const LICENSE_PATH = '/engineering/v1/license';
|
|
14
|
+
export const LICENSE_TIMEOUT_MS = 5_000;
|
|
15
|
+
const MAX_LICENSE_BODY_BYTES = 16 * 1024;
|
|
16
|
+
const MAX_URL_LENGTH = 2_048;
|
|
17
|
+
const ISO_INSTANT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/u;
|
|
18
|
+
// Reasons are displayed, so only a plain identifier is accepted. New server
|
|
19
|
+
// reasons stay readable by old clients without an allowlist update.
|
|
20
|
+
const SAFE_REASON = /^[a-z][a-z0-9_]{0,63}$/u;
|
|
21
|
+
/** @type {ReadonlyMap<number, string>} */
|
|
22
|
+
const UNAVAILABLE_BY_STATUS = new Map([
|
|
23
|
+
[401, 'unauthorized'],
|
|
24
|
+
[404, 'not_offered'],
|
|
25
|
+
[429, 'rate_limited'],
|
|
26
|
+
[503, 'license_unavailable'],
|
|
27
|
+
]);
|
|
28
|
+
const SERVER_ERROR_STATUS = 500;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {{
|
|
32
|
+
* state: 'active' | 'inactive' | 'unavailable',
|
|
33
|
+
* reason: string,
|
|
34
|
+
* validUntil: string | null,
|
|
35
|
+
* purchaseUrl: string | null
|
|
36
|
+
* }} EngineeringLicense
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/** @param {string} reason @returns {EngineeringLicense} */
|
|
40
|
+
export function unavailableLicense(reason) {
|
|
41
|
+
return { state: 'unavailable', reason, validUntil: null, purchaseUrl: null };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @param {string} endpoint */
|
|
45
|
+
export function engineeringLicenseUrl(endpoint) {
|
|
46
|
+
return new URL(LICENSE_PATH, endpoint).toString();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
50
|
+
function isRecord(value) {
|
|
51
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @param {unknown} value */
|
|
55
|
+
function isPurchaseUrl(value) {
|
|
56
|
+
if (typeof value !== 'string' || value.length > MAX_URL_LENGTH) return false;
|
|
57
|
+
try {
|
|
58
|
+
const url = new URL(value);
|
|
59
|
+
return url.protocol === 'https:' && url.username === '' && url.password === '';
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** @param {unknown} value */
|
|
66
|
+
function isValidUntil(value) {
|
|
67
|
+
return (
|
|
68
|
+
value === null ||
|
|
69
|
+
(typeof value === 'string' && ISO_INSTANT.test(value) && Number.isFinite(Date.parse(value)))
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Validate the 200 body of the license route (OpenAPI `EngineeringLicense`).
|
|
75
|
+
* Unknown extra fields are ignored; anything malformed rejects the whole body.
|
|
76
|
+
* @param {string} text
|
|
77
|
+
* @returns {EngineeringLicense | null}
|
|
78
|
+
*/
|
|
79
|
+
export function parseLicenseBody(text) {
|
|
80
|
+
let body;
|
|
81
|
+
try {
|
|
82
|
+
body = JSON.parse(text);
|
|
83
|
+
} catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
if (!isRecord(body) || body.schema_version !== 1 || body.capability !== 'engineering')
|
|
87
|
+
return null;
|
|
88
|
+
const { state, reason, valid_until: validUntil, purchase_url: purchaseUrl } = body;
|
|
89
|
+
if (state !== 'active' && state !== 'inactive') return null;
|
|
90
|
+
if (typeof reason !== 'string' || !SAFE_REASON.test(reason)) return null;
|
|
91
|
+
if (!isValidUntil(validUntil) || !isPurchaseUrl(purchaseUrl)) return null;
|
|
92
|
+
return {
|
|
93
|
+
state,
|
|
94
|
+
reason,
|
|
95
|
+
validUntil: /** @type {string | null} */ (validUntil),
|
|
96
|
+
purchaseUrl: /** @type {string} */ (purchaseUrl),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @param {Response} response */
|
|
101
|
+
async function boundedText(response) {
|
|
102
|
+
const declared = Number(response.headers.get('content-length') ?? '0');
|
|
103
|
+
if (declared > MAX_LICENSE_BODY_BYTES) throw new Error('license_body_too_large');
|
|
104
|
+
if (response.body === null) return '';
|
|
105
|
+
const reader = response.body.getReader();
|
|
106
|
+
/** @type {Uint8Array[]} */
|
|
107
|
+
const chunks = [];
|
|
108
|
+
let size = 0;
|
|
109
|
+
for (;;) {
|
|
110
|
+
const { done, value } = await reader.read();
|
|
111
|
+
if (done) break;
|
|
112
|
+
size += value.byteLength;
|
|
113
|
+
if (size > MAX_LICENSE_BODY_BYTES) {
|
|
114
|
+
await reader.cancel().catch(() => undefined);
|
|
115
|
+
throw new Error('license_body_too_large');
|
|
116
|
+
}
|
|
117
|
+
chunks.push(value);
|
|
118
|
+
}
|
|
119
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** @param {unknown} error */
|
|
123
|
+
function isTimeout(error) {
|
|
124
|
+
return error instanceof Error && (error.name === 'TimeoutError' || error.name === 'AbortError');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** @param {number} status */
|
|
128
|
+
function unavailableReasonFor(status) {
|
|
129
|
+
return (
|
|
130
|
+
UNAVAILABLE_BY_STATUS.get(status) ??
|
|
131
|
+
(status >= SERVER_ERROR_STATUS ? 'server_error' : 'unexpected_status')
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Ask the server whether this account holds the AISAC Engineering license.
|
|
137
|
+
* @param {string} endpoint configured AI Brain MCP endpoint (already validated at setup)
|
|
138
|
+
* @param {string} token account PAT
|
|
139
|
+
* @param {{ fetchImpl?: typeof fetch, timeoutMs?: number }} [options]
|
|
140
|
+
* @returns {Promise<EngineeringLicense>}
|
|
141
|
+
*/
|
|
142
|
+
export async function checkEngineeringLicense(endpoint, token, options = {}) {
|
|
143
|
+
const { fetchImpl = fetch, timeoutMs = LICENSE_TIMEOUT_MS } = options;
|
|
144
|
+
let url;
|
|
145
|
+
try {
|
|
146
|
+
url = engineeringLicenseUrl(endpoint);
|
|
147
|
+
} catch {
|
|
148
|
+
return unavailableLicense('invalid_endpoint');
|
|
149
|
+
}
|
|
150
|
+
const signal = AbortSignal.timeout(timeoutMs);
|
|
151
|
+
let response;
|
|
152
|
+
try {
|
|
153
|
+
response = await fetchImpl(url, {
|
|
154
|
+
method: 'GET',
|
|
155
|
+
redirect: 'error',
|
|
156
|
+
signal,
|
|
157
|
+
headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },
|
|
158
|
+
});
|
|
159
|
+
} catch (error) {
|
|
160
|
+
return unavailableLicense(isTimeout(error) ? 'timeout' : 'unreachable');
|
|
161
|
+
}
|
|
162
|
+
if (response.status !== 200) {
|
|
163
|
+
await response.body?.cancel().catch(() => undefined);
|
|
164
|
+
return unavailableLicense(unavailableReasonFor(response.status));
|
|
165
|
+
}
|
|
166
|
+
let text;
|
|
167
|
+
try {
|
|
168
|
+
text = await boundedText(response);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
return unavailableLicense(isTimeout(error) ? 'timeout' : 'invalid_response');
|
|
171
|
+
}
|
|
172
|
+
return parseLicenseBody(text) ?? unavailableLicense('invalid_response');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* One line for `aisac setup`. Returns null while the server does not offer the
|
|
177
|
+
* capability yet, so memory-only users see no new noise.
|
|
178
|
+
* @param {EngineeringLicense} license
|
|
179
|
+
* @returns {string | null}
|
|
180
|
+
*/
|
|
181
|
+
export function licenseLine(license) {
|
|
182
|
+
if (license.state === 'active') {
|
|
183
|
+
const until = license.validUntil === null ? '' : ` until ${license.validUntil.slice(0, 10)}`;
|
|
184
|
+
return `AISAC Engineering: active${until}.`;
|
|
185
|
+
}
|
|
186
|
+
if (license.state === 'inactive')
|
|
187
|
+
return `AISAC Engineering: inactive (${license.reason}) — ${license.purchaseUrl ?? ''}`.trimEnd();
|
|
188
|
+
if (license.reason === 'not_offered') return null;
|
|
189
|
+
return `AISAC Engineering: unavailable (${license.reason}); memory and context are unaffected.`;
|
|
190
|
+
}
|