@brunobracaioli/aibrain 0.1.0-beta.1 → 0.3.0-beta.13
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 +87 -6
- package/dist/assets/claude/.claude-plugin/plugin.json +103 -1
- package/dist/assets/claude/hooks/context-compaction.ts +6263 -677
- 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 +2874 -262
- 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 +591 -125
- package/dist/claude-checkpoint.mjs +1338 -0
- package/dist/claude-observation.mjs +1336 -0
- package/dist/cli.mjs +396 -29
- package/dist/codex-compatibility.mjs +467 -0
- package/dist/codex-diagnostics.mjs +426 -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/launch.mjs +369 -23
- package/dist/mcp.mjs +577 -5
- package/dist/monitor.mjs +61 -1
- package/dist/package.json +1 -1
- package/dist/platform.mjs +33 -3
- package/dist/policy.mjs +218 -0
- package/dist/state.mjs +132 -25
- package/dist/telemetry-report.mjs +133 -0
- package/package.json +1 -1
package/dist/monitor.mjs
CHANGED
|
@@ -6,6 +6,12 @@ import { constants } from 'node:fs';
|
|
|
6
6
|
import { spawn } from 'node:child_process';
|
|
7
7
|
import { dirname, join } from 'node:path';
|
|
8
8
|
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import {
|
|
10
|
+
CODEX_APP_SERVER_PROFILE,
|
|
11
|
+
CODEX_LAUNCH_ENVELOPE,
|
|
12
|
+
ownedAppServerConfigArgs,
|
|
13
|
+
parseCodexLaunchEnvelope,
|
|
14
|
+
} from './codex-permissions.mjs';
|
|
9
15
|
|
|
10
16
|
const root = dirname(fileURLToPath(import.meta.url));
|
|
11
17
|
const endpoint = process.env.AIBRAIN_MCP_URL?.trim();
|
|
@@ -25,14 +31,67 @@ if (parsed.username || parsed.password || parsed.hash)
|
|
|
25
31
|
if (parsed.search) throw new Error('aibrain_monitor_endpoint_query');
|
|
26
32
|
const canonical = join(root, 'assets', 'codex-context-compaction.mjs');
|
|
27
33
|
await access(canonical, constants.R_OK);
|
|
34
|
+
const launchEnvelope = parseCodexLaunchEnvelope(process.env[CODEX_LAUNCH_ENVELOPE]);
|
|
35
|
+
const policyText = process.env.CODEX_CONTEXT_POLICY_JSON;
|
|
36
|
+
let policy;
|
|
37
|
+
if (policyText === undefined) {
|
|
38
|
+
policy = null;
|
|
39
|
+
} else {
|
|
40
|
+
if (Buffer.byteLength(policyText, 'utf8') > 8 * 1024)
|
|
41
|
+
throw new Error('aibrain_monitor_policy_too_large');
|
|
42
|
+
try {
|
|
43
|
+
policy = JSON.parse(policyText);
|
|
44
|
+
} catch {
|
|
45
|
+
throw new Error('aibrain_monitor_policy_invalid');
|
|
46
|
+
}
|
|
47
|
+
const allowed = new Set([
|
|
48
|
+
'schemaVersion',
|
|
49
|
+
'compactAtPercent',
|
|
50
|
+
'keepThreshold',
|
|
51
|
+
'preserveRecentMessages',
|
|
52
|
+
'maxStateTokens',
|
|
53
|
+
'maxRequestTokens',
|
|
54
|
+
'goal',
|
|
55
|
+
]);
|
|
56
|
+
if (
|
|
57
|
+
policy === null ||
|
|
58
|
+
typeof policy !== 'object' ||
|
|
59
|
+
Array.isArray(policy) ||
|
|
60
|
+
Object.keys(policy).some((key) => !allowed.has(key)) ||
|
|
61
|
+
policy.schemaVersion !== 2 ||
|
|
62
|
+
!Number.isFinite(policy.compactAtPercent) ||
|
|
63
|
+
policy.compactAtPercent < 1 ||
|
|
64
|
+
policy.compactAtPercent > 95 ||
|
|
65
|
+
!Number.isFinite(policy.keepThreshold) ||
|
|
66
|
+
policy.keepThreshold < 0 ||
|
|
67
|
+
policy.keepThreshold > 1 ||
|
|
68
|
+
!Number.isSafeInteger(policy.preserveRecentMessages) ||
|
|
69
|
+
policy.preserveRecentMessages < 1 ||
|
|
70
|
+
policy.preserveRecentMessages > 100 ||
|
|
71
|
+
!Number.isSafeInteger(policy.maxStateTokens) ||
|
|
72
|
+
policy.maxStateTokens < 1000 ||
|
|
73
|
+
policy.maxStateTokens > 25000 ||
|
|
74
|
+
!Number.isSafeInteger(policy.maxRequestTokens) ||
|
|
75
|
+
policy.maxRequestTokens < 1000 ||
|
|
76
|
+
policy.maxRequestTokens > 30000 ||
|
|
77
|
+
policy.maxRequestTokens <= policy.maxStateTokens + 256 ||
|
|
78
|
+
(policy.goal !== undefined && typeof policy.goal !== 'string')
|
|
79
|
+
)
|
|
80
|
+
throw new Error('aibrain_monitor_policy_invalid');
|
|
81
|
+
}
|
|
28
82
|
/** @type {NodeJS.ProcessEnv} */
|
|
29
83
|
const env = {
|
|
30
84
|
...process.env,
|
|
31
85
|
AI_BRAIN_MCP_SERVER: 'aibrain_context',
|
|
32
86
|
CODEX_RECOVERY_MCP_URL: parsed.toString(),
|
|
33
87
|
CODEX_RECOVERY_MCP_TOKEN: token,
|
|
34
|
-
CODEX_CONTEXT_COMPACT_AT:
|
|
88
|
+
CODEX_CONTEXT_COMPACT_AT: String(policy?.compactAtPercent ?? 85),
|
|
35
89
|
};
|
|
90
|
+
delete env[CODEX_LAUNCH_ENVELOPE];
|
|
91
|
+
delete env[CODEX_APP_SERVER_PROFILE];
|
|
92
|
+
if (launchEnvelope?.profile !== undefined) env[CODEX_APP_SERVER_PROFILE] = launchEnvelope.profile;
|
|
93
|
+
if (policy !== null) env.CODEX_CONTEXT_POLICY_JSON = JSON.stringify(policy);
|
|
94
|
+
else delete env.CODEX_CONTEXT_POLICY_JSON;
|
|
36
95
|
delete env.JEV_API_KEY;
|
|
37
96
|
delete env.TYPESAFE_API_KEY;
|
|
38
97
|
const child = spawn(
|
|
@@ -49,6 +108,7 @@ const child = spawn(
|
|
|
49
108
|
`mcp_servers.aibrain_context.url=${parsed.toString()}`,
|
|
50
109
|
'-c',
|
|
51
110
|
'mcp_servers.aibrain_context.bearer_token_env_var="CODEX_RECOVERY_MCP_TOKEN"',
|
|
111
|
+
...(launchEnvelope === null ? [] : ownedAppServerConfigArgs(launchEnvelope)),
|
|
52
112
|
],
|
|
53
113
|
{ cwd: process.cwd(), env, stdio: 'inherit' },
|
|
54
114
|
);
|
package/dist/package.json
CHANGED
package/dist/platform.mjs
CHANGED
|
@@ -70,13 +70,43 @@ export function nativeVersion(executable, environment = process.env) {
|
|
|
70
70
|
export function isTestedNativeVersion(client, version) {
|
|
71
71
|
const value = version.trim();
|
|
72
72
|
if (client === 'claude')
|
|
73
|
-
return /^(?:(?:claude(?:\s+code)?)\s+)?2\.1\.(?:294|295|296)(?:\s+\(Claude\s+Code\))?$/iu.test(
|
|
73
|
+
return /^(?:(?:claude(?:\s+code)?)\s+)?2\.1\.(?:294|295|296|299|300|301|302)(?:\s+\(Claude\s+Code\))?$/iu.test(
|
|
74
74
|
value,
|
|
75
75
|
);
|
|
76
|
-
return /^(?:codex(?:-cli)?\s+)?0\.16(?:1\.0|2\.0)$/iu.test(value);
|
|
76
|
+
return /^(?:codex(?:-cli)?\s+)?0\.16(?:1\.0|2\.0|2\.1|3\.0)$/iu.test(value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const TESTED_NATIVE_VERSIONS = Object.freeze({
|
|
80
|
+
claude: Object.freeze([
|
|
81
|
+
'2.1.294',
|
|
82
|
+
'2.1.295',
|
|
83
|
+
'2.1.296',
|
|
84
|
+
'2.1.299',
|
|
85
|
+
'2.1.300',
|
|
86
|
+
'2.1.301',
|
|
87
|
+
'2.1.302',
|
|
88
|
+
]),
|
|
89
|
+
codex: Object.freeze(['0.161.0', '0.162.0', '0.162.1', '0.163.0']),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
/** @param {'claude'|'codex'} client @param {string} version */
|
|
93
|
+
export function nativeVersionDiagnostic(client, version) {
|
|
94
|
+
const detected = version.trim();
|
|
95
|
+
const versionPattern =
|
|
96
|
+
client === 'claude'
|
|
97
|
+
? /^(?:(?:claude(?:\s+code)?)\s+)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})(-[A-Za-z0-9.-]+)?(?:\s+\(Claude\s+Code\))?$/iu
|
|
98
|
+
: /^(?:codex(?:-cli)?\s+)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})(-[A-Za-z0-9.-]+)?$/iu;
|
|
99
|
+
const versionMatch = detected.match(versionPattern);
|
|
100
|
+
const safeDetected = versionMatch
|
|
101
|
+
? `${versionMatch[1]}.${versionMatch[2]}.${versionMatch[3]}${versionMatch[4] ? '-prerelease' : ''}`
|
|
102
|
+
: 'unavailable';
|
|
103
|
+
const label = client === 'claude' ? 'Claude Code' : 'Codex CLI';
|
|
104
|
+
const tested = TESTED_NATIVE_VERSIONS[client].join(', ');
|
|
105
|
+
return `${client}_version_unverified: ${label} detected version ${safeDetected}; tested stable versions: ${tested}. Update AI Brain to a compatible version, then run aibrain doctor --json or install a qualified stable native version.`;
|
|
77
106
|
}
|
|
78
107
|
|
|
79
108
|
/** @param {'claude'|'codex'} client @param {string} version */
|
|
80
109
|
export function assertTestedNativeVersion(client, version) {
|
|
81
|
-
if (!isTestedNativeVersion(client, version))
|
|
110
|
+
if (!isTestedNativeVersion(client, version))
|
|
111
|
+
throw new Error(nativeVersionDiagnostic(client, version));
|
|
82
112
|
}
|
package/dist/policy.mjs
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/** @typedef {'upstream' | 'conservative'} ContextProfile */
|
|
4
|
+
/** @typedef {'compatible' | 'read_only'} ToolPolicy */
|
|
5
|
+
|
|
6
|
+
/** @typedef {{
|
|
7
|
+
* profile: ContextProfile,
|
|
8
|
+
* compactAtPercent: number,
|
|
9
|
+
* keepThreshold: number,
|
|
10
|
+
* preserveRecentMessages: number,
|
|
11
|
+
* minReductionRatio: number,
|
|
12
|
+
* truncateHeadChars: number,
|
|
13
|
+
* maxStateTokens: number,
|
|
14
|
+
* maxRequestTokens: number,
|
|
15
|
+
* toolPolicy: ToolPolicy,
|
|
16
|
+
* goal?: string
|
|
17
|
+
* }} ContextPolicy */
|
|
18
|
+
|
|
19
|
+
export const POLICY_VERSION = 2;
|
|
20
|
+
export const MAX_GOAL_CHARS = 4000;
|
|
21
|
+
|
|
22
|
+
/** @type {Readonly<Record<ContextProfile, Omit<ContextPolicy, 'profile' | 'goal'>>>} */
|
|
23
|
+
export const PROFILE_DEFAULTS = Object.freeze({
|
|
24
|
+
upstream: Object.freeze({
|
|
25
|
+
compactAtPercent: 60,
|
|
26
|
+
keepThreshold: 0.5,
|
|
27
|
+
preserveRecentMessages: 6,
|
|
28
|
+
minReductionRatio: 0.25,
|
|
29
|
+
truncateHeadChars: 300,
|
|
30
|
+
maxStateTokens: 25_000,
|
|
31
|
+
maxRequestTokens: 30_000,
|
|
32
|
+
toolPolicy: 'compatible',
|
|
33
|
+
}),
|
|
34
|
+
conservative: Object.freeze({
|
|
35
|
+
compactAtPercent: 85,
|
|
36
|
+
keepThreshold: 0.2,
|
|
37
|
+
preserveRecentMessages: 6,
|
|
38
|
+
minReductionRatio: 0.25,
|
|
39
|
+
truncateHeadChars: 300,
|
|
40
|
+
maxStateTokens: 25_000,
|
|
41
|
+
maxRequestTokens: 30_000,
|
|
42
|
+
toolPolicy: 'read_only',
|
|
43
|
+
}),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
47
|
+
function record(value) {
|
|
48
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @param {unknown} value @returns {value is ContextProfile} */
|
|
52
|
+
export function isProfile(value) {
|
|
53
|
+
return value === 'upstream' || value === 'conservative';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** @param {unknown} value @returns {value is ToolPolicy} */
|
|
57
|
+
export function isToolPolicy(value) {
|
|
58
|
+
return value === 'compatible' || value === 'read_only';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @param {number} value @param {number} min @param {number} max @param {string} name */
|
|
62
|
+
function finiteRange(value, min, max, name) {
|
|
63
|
+
if (!Number.isFinite(value) || value < min || value > max) throw new Error(`invalid_${name}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** @param {number} value @param {number} min @param {number} max @param {string} name */
|
|
67
|
+
function integerRange(value, min, max, name) {
|
|
68
|
+
if (!Number.isSafeInteger(value) || value < min || value > max)
|
|
69
|
+
throw new Error(`invalid_${name}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @param {unknown} value @param {string} name */
|
|
73
|
+
function boundedGoal(value, name) {
|
|
74
|
+
if (value === undefined) return undefined;
|
|
75
|
+
if (
|
|
76
|
+
typeof value !== 'string' ||
|
|
77
|
+
value.length > MAX_GOAL_CHARS ||
|
|
78
|
+
/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/u.test(value)
|
|
79
|
+
)
|
|
80
|
+
throw new Error(`invalid_${name}`);
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** @param {ContextProfile} profile @param {Partial<Omit<ContextPolicy, 'profile'>>} overrides @param {string | undefined} goal */
|
|
85
|
+
export function makePolicy(profile, overrides = {}, goal = overrides.goal) {
|
|
86
|
+
if (!isProfile(profile)) throw new Error('invalid_context_profile');
|
|
87
|
+
const base = PROFILE_DEFAULTS[profile];
|
|
88
|
+
const policy = {
|
|
89
|
+
profile,
|
|
90
|
+
compactAtPercent: overrides.compactAtPercent ?? base.compactAtPercent,
|
|
91
|
+
keepThreshold: overrides.keepThreshold ?? base.keepThreshold,
|
|
92
|
+
preserveRecentMessages: overrides.preserveRecentMessages ?? base.preserveRecentMessages,
|
|
93
|
+
minReductionRatio: overrides.minReductionRatio ?? base.minReductionRatio,
|
|
94
|
+
truncateHeadChars: overrides.truncateHeadChars ?? base.truncateHeadChars,
|
|
95
|
+
maxStateTokens: overrides.maxStateTokens ?? base.maxStateTokens,
|
|
96
|
+
maxRequestTokens: overrides.maxRequestTokens ?? base.maxRequestTokens,
|
|
97
|
+
toolPolicy: overrides.toolPolicy ?? base.toolPolicy,
|
|
98
|
+
...(goal === undefined ? {} : { goal }),
|
|
99
|
+
};
|
|
100
|
+
finiteRange(policy.compactAtPercent, 1, 95, 'compact_at');
|
|
101
|
+
finiteRange(policy.keepThreshold, 0, 1, 'keep_threshold');
|
|
102
|
+
integerRange(policy.preserveRecentMessages, 1, 100, 'preserve_recent');
|
|
103
|
+
finiteRange(policy.minReductionRatio, 0, 1, 'min_reduction');
|
|
104
|
+
integerRange(policy.truncateHeadChars, 0, 4096, 'truncate_head_chars');
|
|
105
|
+
integerRange(policy.maxStateTokens, 1000, 25_000, 'max_state_tokens');
|
|
106
|
+
integerRange(policy.maxRequestTokens, 1000, 30_000, 'max_request_tokens');
|
|
107
|
+
if (policy.maxRequestTokens <= policy.maxStateTokens + 256)
|
|
108
|
+
throw new Error('max_request_tokens_must_exceed_state_plus_256');
|
|
109
|
+
if (!isToolPolicy(policy.toolPolicy)) throw new Error('invalid_tool_policy');
|
|
110
|
+
boundedGoal(policy.goal, 'goal');
|
|
111
|
+
return /** @type {ContextPolicy} */ (policy);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @param {unknown} value @returns {value is ContextPolicy} */
|
|
115
|
+
export function isValidPolicy(value) {
|
|
116
|
+
if (!record(value) || !isProfile(value.profile)) return false;
|
|
117
|
+
const required = [
|
|
118
|
+
'profile',
|
|
119
|
+
'compactAtPercent',
|
|
120
|
+
'keepThreshold',
|
|
121
|
+
'preserveRecentMessages',
|
|
122
|
+
'minReductionRatio',
|
|
123
|
+
'truncateHeadChars',
|
|
124
|
+
'maxStateTokens',
|
|
125
|
+
'maxRequestTokens',
|
|
126
|
+
'toolPolicy',
|
|
127
|
+
];
|
|
128
|
+
if (required.some((key) => !Object.hasOwn(value, key))) return false;
|
|
129
|
+
if (
|
|
130
|
+
typeof value.compactAtPercent !== 'number' ||
|
|
131
|
+
typeof value.keepThreshold !== 'number' ||
|
|
132
|
+
typeof value.preserveRecentMessages !== 'number' ||
|
|
133
|
+
typeof value.minReductionRatio !== 'number' ||
|
|
134
|
+
typeof value.truncateHeadChars !== 'number' ||
|
|
135
|
+
typeof value.maxStateTokens !== 'number' ||
|
|
136
|
+
typeof value.maxRequestTokens !== 'number' ||
|
|
137
|
+
!isToolPolicy(value.toolPolicy)
|
|
138
|
+
)
|
|
139
|
+
return false;
|
|
140
|
+
if (Object.hasOwn(value, 'goal') && typeof value.goal !== 'string') return false;
|
|
141
|
+
try {
|
|
142
|
+
makePolicy(
|
|
143
|
+
value.profile,
|
|
144
|
+
/** @type {Partial<Omit<ContextPolicy, 'profile'>>} */ (value),
|
|
145
|
+
typeof value.goal === 'string' ? value.goal : undefined,
|
|
146
|
+
);
|
|
147
|
+
const keys = new Set([
|
|
148
|
+
'profile',
|
|
149
|
+
'compactAtPercent',
|
|
150
|
+
'keepThreshold',
|
|
151
|
+
'preserveRecentMessages',
|
|
152
|
+
'minReductionRatio',
|
|
153
|
+
'truncateHeadChars',
|
|
154
|
+
'maxStateTokens',
|
|
155
|
+
'maxRequestTokens',
|
|
156
|
+
'toolPolicy',
|
|
157
|
+
'goal',
|
|
158
|
+
]);
|
|
159
|
+
return Object.keys(value).every((key) => keys.has(key));
|
|
160
|
+
} catch {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** @param {ContextPolicy} policy */
|
|
166
|
+
export function compactWireOptions(policy) {
|
|
167
|
+
return {
|
|
168
|
+
keep_threshold: policy.keepThreshold,
|
|
169
|
+
preserve_recent_messages: policy.preserveRecentMessages,
|
|
170
|
+
min_reduction_ratio: policy.minReductionRatio,
|
|
171
|
+
truncate_head_chars: policy.truncateHeadChars,
|
|
172
|
+
max_state_tokens: policy.maxStateTokens,
|
|
173
|
+
max_request_tokens: policy.maxRequestTokens,
|
|
174
|
+
tool_policy: policy.toolPolicy,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** @param {ContextPolicy} policy */
|
|
179
|
+
export function selectionWireOptions(policy) {
|
|
180
|
+
return {
|
|
181
|
+
keep_threshold: policy.keepThreshold,
|
|
182
|
+
preserve_recent_items: policy.preserveRecentMessages,
|
|
183
|
+
max_state_tokens: policy.maxStateTokens,
|
|
184
|
+
max_request_tokens: policy.maxRequestTokens,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** @param {ContextPolicy} policy */
|
|
189
|
+
export function codexPolicyJson(policy) {
|
|
190
|
+
const serialized = JSON.stringify({
|
|
191
|
+
schemaVersion: POLICY_VERSION,
|
|
192
|
+
compactAtPercent: policy.compactAtPercent,
|
|
193
|
+
keepThreshold: policy.keepThreshold,
|
|
194
|
+
preserveRecentMessages: policy.preserveRecentMessages,
|
|
195
|
+
maxStateTokens: policy.maxStateTokens,
|
|
196
|
+
maxRequestTokens: policy.maxRequestTokens,
|
|
197
|
+
...(policy.goal === undefined ? {} : { goal: policy.goal }),
|
|
198
|
+
});
|
|
199
|
+
if (Buffer.byteLength(serialized, 'utf8') > 8 * 1024)
|
|
200
|
+
throw new Error('context_policy_too_large_for_codex_monitor');
|
|
201
|
+
return serialized;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** @param {ContextPolicy} policy */
|
|
205
|
+
export function publicPolicy(policy) {
|
|
206
|
+
return {
|
|
207
|
+
profile: policy.profile,
|
|
208
|
+
compactAtPercent: policy.compactAtPercent,
|
|
209
|
+
keepThreshold: policy.keepThreshold,
|
|
210
|
+
preserveRecentMessages: policy.preserveRecentMessages,
|
|
211
|
+
minReductionRatio: policy.minReductionRatio,
|
|
212
|
+
truncateHeadChars: policy.truncateHeadChars,
|
|
213
|
+
maxStateTokens: policy.maxStateTokens,
|
|
214
|
+
maxRequestTokens: policy.maxRequestTokens,
|
|
215
|
+
toolPolicy: policy.toolPolicy,
|
|
216
|
+
goalPresent: policy.goal !== undefined,
|
|
217
|
+
};
|
|
218
|
+
}
|
package/dist/state.mjs
CHANGED
|
@@ -12,12 +12,13 @@ import {
|
|
|
12
12
|
import { dirname, join, resolve } from 'node:path';
|
|
13
13
|
import { homedir } from 'node:os';
|
|
14
14
|
import { createHash } from 'node:crypto';
|
|
15
|
+
import { isValidPolicy } from './policy.mjs';
|
|
15
16
|
|
|
16
17
|
/** @typedef {'claude' | 'codex' | 'both'} ClientSelection */
|
|
17
|
-
/** @typedef {{ ok: true, providerBatches: 0, providerModel: null }} Probe */
|
|
18
|
+
/** @typedef {{ ok: true, providerBatches: 0, providerModel: null, protocol?: 'v1' | 'v2' }} Probe */
|
|
18
19
|
/**
|
|
19
20
|
* @typedef {{
|
|
20
|
-
* schemaVersion: 1,
|
|
21
|
+
* schemaVersion: 1 | 2,
|
|
21
22
|
* packageVersion: string,
|
|
22
23
|
* disclosureVersion: string,
|
|
23
24
|
* disclosedAt: string,
|
|
@@ -28,11 +29,15 @@ import { createHash } from 'node:crypto';
|
|
|
28
29
|
* packageRoot: string,
|
|
29
30
|
* token: string,
|
|
30
31
|
* tokenSha256: string,
|
|
31
|
-
* probe: Probe
|
|
32
|
+
* probe: Probe,
|
|
33
|
+
* context?: import('./policy.mjs').ContextPolicy,
|
|
34
|
+
* telemetry?: { enabled: boolean, disclosedAt: string | null },
|
|
35
|
+
* claudeObservation?: { shadowEnabled: boolean, diagnosticsEnabled: boolean, disclosedAt: string | null }
|
|
32
36
|
* }} Config
|
|
33
37
|
*/
|
|
34
38
|
|
|
35
|
-
export const DISCLOSURE_VERSION = 'aibrain-paid-context-
|
|
39
|
+
export const DISCLOSURE_VERSION = 'aibrain-paid-context-v2-2026-10-10';
|
|
40
|
+
export const LEGACY_DISCLOSURE_VERSION = 'aibrain-paid-context-v1-2026-10-09';
|
|
36
41
|
|
|
37
42
|
/** @param {unknown} error @returns {error is NodeJS.ErrnoException} */
|
|
38
43
|
function isNodeError(error) {
|
|
@@ -117,15 +122,49 @@ function isRecord(value) {
|
|
|
117
122
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
118
123
|
}
|
|
119
124
|
|
|
125
|
+
/** @param {unknown} value */
|
|
126
|
+
function isDisclosure(value) {
|
|
127
|
+
return value === DISCLOSURE_VERSION || value === LEGACY_DISCLOSURE_VERSION;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** @param {unknown} value */
|
|
131
|
+
function isTelemetry(value) {
|
|
132
|
+
return (
|
|
133
|
+
isRecord(value) &&
|
|
134
|
+
typeof value.enabled === 'boolean' &&
|
|
135
|
+
(value.disclosedAt === null ||
|
|
136
|
+
(typeof value.disclosedAt === 'string' &&
|
|
137
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u.test(value.disclosedAt) &&
|
|
138
|
+
Number.isFinite(Date.parse(value.disclosedAt)))) &&
|
|
139
|
+
Object.keys(value).every((key) => ['enabled', 'disclosedAt'].includes(key))
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** @param {unknown} value */
|
|
144
|
+
function isClaudeObservation(value) {
|
|
145
|
+
return (
|
|
146
|
+
isRecord(value) &&
|
|
147
|
+
typeof value.shadowEnabled === 'boolean' &&
|
|
148
|
+
typeof value.diagnosticsEnabled === 'boolean' &&
|
|
149
|
+
(value.disclosedAt === null ||
|
|
150
|
+
(typeof value.disclosedAt === 'string' &&
|
|
151
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u.test(value.disclosedAt) &&
|
|
152
|
+
Number.isFinite(Date.parse(value.disclosedAt)))) &&
|
|
153
|
+
Object.keys(value).every((key) =>
|
|
154
|
+
['shadowEnabled', 'diagnosticsEnabled', 'disclosedAt'].includes(key),
|
|
155
|
+
)
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
120
159
|
/** @param {unknown} value */
|
|
121
160
|
function isConfig(value) {
|
|
122
161
|
if (!isRecord(value)) return false;
|
|
123
162
|
const endpoint = value.endpoint;
|
|
124
163
|
const clientSelection = value.clientSelection;
|
|
125
164
|
if (
|
|
126
|
-
value.schemaVersion !== 1 ||
|
|
165
|
+
(value.schemaVersion !== 1 && value.schemaVersion !== 2) ||
|
|
127
166
|
typeof value.packageVersion !== 'string' ||
|
|
128
|
-
value.disclosureVersion
|
|
167
|
+
!isDisclosure(value.disclosureVersion) ||
|
|
129
168
|
typeof value.disclosedAt !== 'string' ||
|
|
130
169
|
!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u.test(value.disclosedAt) ||
|
|
131
170
|
!Number.isFinite(Date.parse(value.disclosedAt)) ||
|
|
@@ -146,10 +185,21 @@ function isConfig(value) {
|
|
|
146
185
|
value.probe.providerBatches !== 0 ||
|
|
147
186
|
value.probe.providerModel !== null ||
|
|
148
187
|
Object.keys(value.probe).some(
|
|
149
|
-
(key) => !['ok', 'providerBatches', 'providerModel'].includes(key),
|
|
150
|
-
)
|
|
188
|
+
(key) => !['ok', 'providerBatches', 'providerModel', 'protocol'].includes(key),
|
|
189
|
+
) ||
|
|
190
|
+
(value.schemaVersion === 2 && value.probe.protocol !== 'v2') ||
|
|
191
|
+
(value.schemaVersion === 1 && value.probe.protocol !== undefined)
|
|
192
|
+
)
|
|
193
|
+
return false;
|
|
194
|
+
if (
|
|
195
|
+
value.schemaVersion === 2 &&
|
|
196
|
+
(value.disclosureVersion !== DISCLOSURE_VERSION || !isValidPolicy(value.context))
|
|
151
197
|
)
|
|
152
198
|
return false;
|
|
199
|
+
if (value.schemaVersion === 1 && value.context !== undefined) return false;
|
|
200
|
+
if (value.telemetry !== undefined && !isTelemetry(value.telemetry)) return false;
|
|
201
|
+
if (value.claudeObservation !== undefined && !isClaudeObservation(value.claudeObservation))
|
|
202
|
+
return false;
|
|
153
203
|
const allowed = new Set([
|
|
154
204
|
'schemaVersion',
|
|
155
205
|
'packageVersion',
|
|
@@ -163,6 +213,9 @@ function isConfig(value) {
|
|
|
163
213
|
'token',
|
|
164
214
|
'tokenSha256',
|
|
165
215
|
'probe',
|
|
216
|
+
'context',
|
|
217
|
+
'telemetry',
|
|
218
|
+
'claudeObservation',
|
|
166
219
|
]);
|
|
167
220
|
return Object.keys(value).every((key) => allowed.has(key));
|
|
168
221
|
}
|
|
@@ -172,9 +225,9 @@ export function isValidConfig(value) {
|
|
|
172
225
|
return isConfig(value);
|
|
173
226
|
}
|
|
174
227
|
|
|
175
|
-
/** @returns {Promise<Config | null>} */
|
|
176
|
-
export async function readConfig() {
|
|
177
|
-
const root = stateRoot();
|
|
228
|
+
/** @param {string | undefined} [rootOverride] @returns {Promise<Config | null>} */
|
|
229
|
+
export async function readConfig(rootOverride) {
|
|
230
|
+
const root = rootOverride === undefined ? stateRoot() : resolve(rootOverride);
|
|
178
231
|
await assertSafePath(root);
|
|
179
232
|
const path = join(root, 'config.json');
|
|
180
233
|
try {
|
|
@@ -237,6 +290,73 @@ export async function tokenDigest(token) {
|
|
|
237
290
|
return createHash('sha256').update(token, 'utf8').digest('hex');
|
|
238
291
|
}
|
|
239
292
|
|
|
293
|
+
/** @param {{endpoint: string, token: string, telemetry?: {enabled: boolean, disclosedAt?: string | null}}} config */
|
|
294
|
+
export function telemetryConfigBinding(config) {
|
|
295
|
+
if (
|
|
296
|
+
!isRecord(config) ||
|
|
297
|
+
typeof config.endpoint !== 'string' ||
|
|
298
|
+
typeof config.token !== 'string' ||
|
|
299
|
+
!isRecord(config.telemetry) ||
|
|
300
|
+
config.telemetry.enabled !== true ||
|
|
301
|
+
typeof config.telemetry.disclosedAt !== 'string'
|
|
302
|
+
)
|
|
303
|
+
throw new Error('telemetry_state_mismatch');
|
|
304
|
+
let endpoint;
|
|
305
|
+
try {
|
|
306
|
+
endpoint = new URL(config.endpoint).toString();
|
|
307
|
+
} catch {
|
|
308
|
+
throw new Error('telemetry_state_mismatch');
|
|
309
|
+
}
|
|
310
|
+
return createHash('sha256')
|
|
311
|
+
.update(`${endpoint}\u0000${config.token}\u0000${config.telemetry.disclosedAt}`, 'utf8')
|
|
312
|
+
.digest('hex');
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** @param {{endpoint: string, token: string, claudeObservation?: {shadowEnabled?: boolean, diagnosticsEnabled?: boolean, disclosedAt?: string | null}}} config */
|
|
316
|
+
export function claudeObservationConfigBinding(config) {
|
|
317
|
+
if (
|
|
318
|
+
!isRecord(config) ||
|
|
319
|
+
typeof config.endpoint !== 'string' ||
|
|
320
|
+
typeof config.token !== 'string' ||
|
|
321
|
+
!isRecord(config.claudeObservation) ||
|
|
322
|
+
typeof config.claudeObservation.disclosedAt !== 'string'
|
|
323
|
+
)
|
|
324
|
+
throw new Error('claude_observation_state_mismatch');
|
|
325
|
+
let endpoint;
|
|
326
|
+
try {
|
|
327
|
+
endpoint = new URL(config.endpoint).toString();
|
|
328
|
+
} catch {
|
|
329
|
+
throw new Error('claude_observation_state_mismatch');
|
|
330
|
+
}
|
|
331
|
+
return createHash('sha256')
|
|
332
|
+
.update(`${endpoint}\u0000${config.token}\u0000${config.claudeObservation.disclosedAt}`, 'utf8')
|
|
333
|
+
.digest('hex');
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Bind launcher-managed Claude checkpoints to the authenticated account and
|
|
338
|
+
* disclosure, independently of shadow/diagnostics opt-ins.
|
|
339
|
+
* @param {{endpoint: string, token: string, disclosedAt?: string}} config
|
|
340
|
+
*/
|
|
341
|
+
export function claudeCheckpointConfigBinding(config) {
|
|
342
|
+
if (
|
|
343
|
+
!isRecord(config) ||
|
|
344
|
+
typeof config.endpoint !== 'string' ||
|
|
345
|
+
typeof config.token !== 'string' ||
|
|
346
|
+
typeof config.disclosedAt !== 'string'
|
|
347
|
+
)
|
|
348
|
+
throw new Error('claude_checkpoint_state_mismatch');
|
|
349
|
+
let endpoint;
|
|
350
|
+
try {
|
|
351
|
+
endpoint = new URL(config.endpoint).toString();
|
|
352
|
+
} catch {
|
|
353
|
+
throw new Error('claude_checkpoint_state_mismatch');
|
|
354
|
+
}
|
|
355
|
+
return createHash('sha256')
|
|
356
|
+
.update(`${endpoint}\u0000${config.token}\u0000${config.disclosedAt}`, 'utf8')
|
|
357
|
+
.digest('hex');
|
|
358
|
+
}
|
|
359
|
+
|
|
240
360
|
/** @param {string} path */
|
|
241
361
|
export async function readPrivateToken(path) {
|
|
242
362
|
const resolved = resolve(path);
|
|
@@ -278,22 +398,9 @@ export async function removeOwnedSetup() {
|
|
|
278
398
|
throw new Error('foreign_config_preserved');
|
|
279
399
|
}
|
|
280
400
|
if (
|
|
281
|
-
!
|
|
282
|
-
value.schemaVersion !== 1 ||
|
|
401
|
+
!isConfig(value) ||
|
|
283
402
|
typeof value.packageVersion !== 'string' ||
|
|
284
403
|
!/^\d+\.\d+\.\d+-beta\./u.test(value.packageVersion) ||
|
|
285
|
-
typeof value.disclosureVersion !== 'string' ||
|
|
286
|
-
!/^aibrain-paid-context-v1-/u.test(value.disclosureVersion) ||
|
|
287
|
-
typeof value.endpoint !== 'string' ||
|
|
288
|
-
!validEndpoint(value.endpoint) ||
|
|
289
|
-
typeof value.clientSelection !== 'string' ||
|
|
290
|
-
!['claude', 'codex', 'both'].includes(value.clientSelection) ||
|
|
291
|
-
typeof value.token !== 'string' ||
|
|
292
|
-
value.token.length < 1 ||
|
|
293
|
-
value.token.length > 16 * 1024 ||
|
|
294
|
-
/[\u0000-\u001f\u007f-\u009f]/u.test(value.token) ||
|
|
295
|
-
typeof value.tokenSha256 !== 'string' ||
|
|
296
|
-
!/^[a-f0-9]{64}$/u.test(value.tokenSha256) ||
|
|
297
404
|
(await tokenDigest(value.token)) !== value.tokenSha256
|
|
298
405
|
)
|
|
299
406
|
throw new Error('foreign_config_preserved');
|