@aisac/cli 0.0.0-stage → 0.4.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +166 -2
  2. package/dist/assets/claude/.claude-plugin/plugin.json +147 -0
  3. package/dist/assets/claude/hooks/context-compaction.ts +6930 -0
  4. package/dist/assets/claude/hooks/hooks.json +3 -0
  5. package/dist/assets/claude/hooks/native-host-contract-runtime.ts +18 -0
  6. package/dist/assets/claude/hooks/native-host-contract.test.ts +51 -0
  7. package/dist/assets/claude/hooks/provider-window.js +263 -0
  8. package/dist/assets/claude/hooks/state-fitting.d.mts +47 -0
  9. package/dist/assets/claude/hooks/state-fitting.mjs +324 -0
  10. package/dist/assets/codex-context-compaction.mjs +4689 -0
  11. package/dist/assets/context-selection-fitting.mjs +194 -0
  12. package/dist/assets/context-state-fitting.mjs +324 -0
  13. package/dist/assets/context-telemetry-reporter.mjs +305 -0
  14. package/dist/assets/jev-paid-pilot-terminal.mjs +1066 -0
  15. package/dist/claude-checkpoint.mjs +1338 -0
  16. package/dist/claude-compatibility.mjs +506 -0
  17. package/dist/claude-observation.mjs +1338 -0
  18. package/dist/cli.mjs +771 -0
  19. package/dist/codex-compatibility.mjs +481 -0
  20. package/dist/codex-diagnostics.mjs +447 -0
  21. package/dist/codex-permissions.mjs +590 -0
  22. package/dist/codex-protocol-fingerprint.json +131 -0
  23. package/dist/context-telemetry.mjs +257 -0
  24. package/dist/engineering-pack-public-key.json +10 -0
  25. package/dist/engineering-pack.mjs +722 -0
  26. package/dist/engineering.mjs +194 -0
  27. package/dist/launch.mjs +690 -0
  28. package/dist/mcp.mjs +962 -0
  29. package/dist/monitor.mjs +133 -0
  30. package/dist/package.json +5 -0
  31. package/dist/platform.mjs +126 -0
  32. package/dist/policy.mjs +218 -0
  33. package/dist/state.mjs +408 -0
  34. package/dist/telemetry-report.mjs +133 -0
  35. package/package.json +24 -4
@@ -0,0 +1,194 @@
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
+ export function isRecord(value) {
51
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
52
+ }
53
+
54
+ /** @param {unknown} value @returns {value is string} */
55
+ export 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
+ /**
101
+ * Read a response body as UTF-8, refusing anything above `limit` bytes.
102
+ * @param {Response} response
103
+ * @param {number} [limit]
104
+ */
105
+ export async function readBoundedText(response, limit = MAX_LICENSE_BODY_BYTES) {
106
+ const declared = Number(response.headers.get('content-length') ?? '0');
107
+ if (declared > limit) throw new Error('body_too_large');
108
+ if (response.body === null) return '';
109
+ const reader = response.body.getReader();
110
+ /** @type {Uint8Array[]} */
111
+ const chunks = [];
112
+ let size = 0;
113
+ for (;;) {
114
+ const { done, value } = await reader.read();
115
+ if (done) break;
116
+ size += value.byteLength;
117
+ if (size > limit) {
118
+ await reader.cancel().catch(() => undefined);
119
+ throw new Error('body_too_large');
120
+ }
121
+ chunks.push(value);
122
+ }
123
+ return Buffer.concat(chunks).toString('utf8');
124
+ }
125
+
126
+ /** @param {unknown} error */
127
+ export function isTimeout(error) {
128
+ return error instanceof Error && (error.name === 'TimeoutError' || error.name === 'AbortError');
129
+ }
130
+
131
+ /** @param {number} status */
132
+ export function unavailableReasonFor(status) {
133
+ return (
134
+ UNAVAILABLE_BY_STATUS.get(status) ??
135
+ (status >= SERVER_ERROR_STATUS ? 'server_error' : 'unexpected_status')
136
+ );
137
+ }
138
+
139
+ /**
140
+ * Ask the server whether this account holds the AISAC Engineering license.
141
+ * @param {string} endpoint configured AI Brain MCP endpoint (already validated at setup)
142
+ * @param {string} token account PAT
143
+ * @param {{ fetchImpl?: typeof fetch, timeoutMs?: number }} [options]
144
+ * @returns {Promise<EngineeringLicense>}
145
+ */
146
+ export async function checkEngineeringLicense(endpoint, token, options = {}) {
147
+ const { fetchImpl = fetch, timeoutMs = LICENSE_TIMEOUT_MS } = options;
148
+ let url;
149
+ try {
150
+ url = engineeringLicenseUrl(endpoint);
151
+ } catch {
152
+ return unavailableLicense('invalid_endpoint');
153
+ }
154
+ const signal = AbortSignal.timeout(timeoutMs);
155
+ let response;
156
+ try {
157
+ response = await fetchImpl(url, {
158
+ method: 'GET',
159
+ redirect: 'error',
160
+ signal,
161
+ headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },
162
+ });
163
+ } catch (error) {
164
+ return unavailableLicense(isTimeout(error) ? 'timeout' : 'unreachable');
165
+ }
166
+ if (response.status !== 200) {
167
+ await response.body?.cancel().catch(() => undefined);
168
+ return unavailableLicense(unavailableReasonFor(response.status));
169
+ }
170
+ let text;
171
+ try {
172
+ text = await readBoundedText(response);
173
+ } catch (error) {
174
+ return unavailableLicense(isTimeout(error) ? 'timeout' : 'invalid_response');
175
+ }
176
+ return parseLicenseBody(text) ?? unavailableLicense('invalid_response');
177
+ }
178
+
179
+ /**
180
+ * One line for `aisac setup`. Returns null while the server does not offer the
181
+ * capability yet, so memory-only users see no new noise.
182
+ * @param {EngineeringLicense} license
183
+ * @returns {string | null}
184
+ */
185
+ export function licenseLine(license) {
186
+ if (license.state === 'active') {
187
+ const until = license.validUntil === null ? '' : ` until ${license.validUntil.slice(0, 10)}`;
188
+ return `AISAC Engineering: active${until}.`;
189
+ }
190
+ if (license.state === 'inactive')
191
+ return `AISAC Engineering: inactive (${license.reason}) — ${license.purchaseUrl ?? ''}`.trimEnd();
192
+ if (license.reason === 'not_offered') return null;
193
+ return `AISAC Engineering: unavailable (${license.reason}); memory and context are unaffected.`;
194
+ }