@brunobracaioli/aibrain 0.4.0-beta.2 → 0.4.0-beta.4
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 +19 -0
- package/dist/assets/codex-context-compaction.mjs +98 -1
- package/dist/claude-observation.mjs +1 -1
- package/dist/cli.mjs +20 -2
- package/dist/codex-engineering.mjs +165 -0
- package/dist/engineering-pack.mjs +139 -16
- package/dist/extensions-catalog.mjs +140 -0
- package/dist/extensions-cli.mjs +214 -0
- package/dist/extensions-preferences.mjs +122 -0
- package/dist/guardrails/continuity.mjs +239 -0
- package/dist/guardrails/format.mjs +111 -0
- package/dist/guardrails/guards.mjs +397 -0
- package/dist/guardrails/run.mjs +211 -0
- package/dist/guardrails/shell-words.mjs +206 -0
- package/dist/guardrails-plugin.mjs +149 -0
- package/dist/launch.mjs +41 -2
- package/dist/monitor.mjs +21 -0
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,6 +21,25 @@ private folder, which is deleted on exit. Any failure starts Claude without the
|
|
|
21
21
|
pack. `aisac setup --lang pt-BR|en` picks the pack language without a network
|
|
22
22
|
call. The pack is never part of this npm package.
|
|
23
23
|
|
|
24
|
+
`aisac codex` (Codex 0.164.0 or later) loads the same verified pack for that
|
|
25
|
+
session only: its skills are added to the Codex app-server the launch owns
|
|
26
|
+
(call one with `$`, for example `$spec`) and its agents become session agent
|
|
27
|
+
roles. Nothing is written to `~/.codex`, `~/.agents` or the project, and the
|
|
28
|
+
session's folder is deleted on exit. An older Codex starts without the pack and
|
|
29
|
+
says `codex_outdated`.
|
|
30
|
+
|
|
31
|
+
Licensed `aisac claude` sessions also load guardrail hooks as the session plugin
|
|
32
|
+
`aisac-guardrails`.
|
|
33
|
+
- **Default on:** `secrets-guard` refuses secret files by name. Four more hooks
|
|
34
|
+
keep continuity: a repository snapshot at start and after a compaction, a
|
|
35
|
+
docs reminder, and the NOTES/spec/CHANGELOG pass.
|
|
36
|
+
- **Default off:** `git-guard`, `destructive-guard` and `format-on-edit`.
|
|
37
|
+
- **Changing them:** `aisac hooks` lists them, and `aisac hooks enable|disable
|
|
38
|
+
<id>` or `aisac hooks reset` changes the next session. The agent cannot change
|
|
39
|
+
them from inside a session.
|
|
40
|
+
- **Code:** it ships in this package and runs locally. Nothing comes from the
|
|
41
|
+
server.
|
|
42
|
+
|
|
24
43
|
Codex capture also requires the `flock` utility from `util-linux`, normally
|
|
25
44
|
included in Linux/WSL. The launcher and doctor check availability before a
|
|
26
45
|
monitored Codex session starts. Capture coordination is released by the kernel
|
|
@@ -54,6 +54,12 @@ const MAX_OWNED_REQUESTS = 1_000;
|
|
|
54
54
|
const MAX_RETIRED_REQUESTS = 2_000;
|
|
55
55
|
const MAX_CLIENT_REQUEST_BINDINGS = 128;
|
|
56
56
|
const MAX_OWNED_THREAD_IDS = 256;
|
|
57
|
+
// AISAC Engineering (ADR-0047 E4c): the launcher's skills root is added to the
|
|
58
|
+
// owned app-server before the client sees its initialize answer.
|
|
59
|
+
const ENGINEERING_SKILLS_ROOT_ENV = 'AISAC_ENGINEERING_SKILLS_ROOT';
|
|
60
|
+
const ENGINEERING_SKILLS_ACK_TIMEOUT_MS = 2_000;
|
|
61
|
+
const MAX_ENGINEERING_PATH_LENGTH = 4_096;
|
|
62
|
+
const MAX_TRACKED_INITIALIZE_REQUESTS = 4;
|
|
57
63
|
const MAX_RECOVERY_ITEMS = 128;
|
|
58
64
|
const MAX_RECOVERY_SOURCE_INDEX = 65_536;
|
|
59
65
|
const MAX_NOTE_BYTES = 128 * 1024;
|
|
@@ -2707,6 +2713,8 @@ export function createCodexContextProxy({
|
|
|
2707
2713
|
createDiagnosticSink = null,
|
|
2708
2714
|
brainServer = process.env.AI_BRAIN_MCP_SERVER ?? 'brain',
|
|
2709
2715
|
clientVersion = process.env.CODEX_CLIENT_VERSION ?? '0.161.0',
|
|
2716
|
+
engineeringSkillsRoot = null,
|
|
2717
|
+
engineeringAckTimeoutMs = ENGINEERING_SKILLS_ACK_TIMEOUT_MS,
|
|
2710
2718
|
}) {
|
|
2711
2719
|
const contextPolicy = normalizeContextPolicy(policy);
|
|
2712
2720
|
const effectiveThreshold = contextPolicy?.compactAtPercent ?? threshold;
|
|
@@ -2719,6 +2727,15 @@ export function createCodexContextProxy({
|
|
|
2719
2727
|
const telemetryByThread = new Map();
|
|
2720
2728
|
const diagnosticSinks = new Map();
|
|
2721
2729
|
let stopped = false;
|
|
2730
|
+
// AISAC Engineering skills root: 'off' without a root; 'waiting' for the
|
|
2731
|
+
// client's first successful initialize; 'holding' app-server output until
|
|
2732
|
+
// skills/extraRoots/set is answered (or times out); then 'done'.
|
|
2733
|
+
let engineeringState =
|
|
2734
|
+
typeof engineeringSkillsRoot === 'string' && engineeringSkillsRoot.length > 0
|
|
2735
|
+
? 'waiting'
|
|
2736
|
+
: 'off';
|
|
2737
|
+
const initializeRequestKeys = new Set();
|
|
2738
|
+
let engineeringHeld = null;
|
|
2722
2739
|
function emitDiagnostic(event) {
|
|
2723
2740
|
try {
|
|
2724
2741
|
const phase = typeof event?.phase === 'string' ? event.phase : '';
|
|
@@ -4131,6 +4148,11 @@ export function createCodexContextProxy({
|
|
|
4131
4148
|
const owned = ownedRequests.get(message.id);
|
|
4132
4149
|
ownedRequests.delete(message.id);
|
|
4133
4150
|
retire(message.id);
|
|
4151
|
+
if (owned.kind === 'engineering') {
|
|
4152
|
+
// An error leaves the session without the pack skills; it is never shown.
|
|
4153
|
+
releaseEngineering();
|
|
4154
|
+
return false;
|
|
4155
|
+
}
|
|
4134
4156
|
if (
|
|
4135
4157
|
owned.kind === 'read' ||
|
|
4136
4158
|
owned.kind === 'page' ||
|
|
@@ -4364,6 +4386,14 @@ export function createCodexContextProxy({
|
|
|
4364
4386
|
if (!validRequestId) return;
|
|
4365
4387
|
const requestKey = clientRequestKey(requestId);
|
|
4366
4388
|
if (requestKey === null) return;
|
|
4389
|
+
if (
|
|
4390
|
+
message.method === 'initialize' &&
|
|
4391
|
+
engineeringState === 'waiting' &&
|
|
4392
|
+
initializeRequestKeys.size < MAX_TRACKED_INITIALIZE_REQUESTS
|
|
4393
|
+
) {
|
|
4394
|
+
initializeRequestKeys.add(requestKey);
|
|
4395
|
+
return;
|
|
4396
|
+
}
|
|
4367
4397
|
if (['thread/start', 'thread/resume', 'thread/fork'].includes(message.method)) {
|
|
4368
4398
|
const threadId =
|
|
4369
4399
|
isRecord(message.params) && typeof message.params.threadId === 'string'
|
|
@@ -4428,7 +4458,7 @@ export function createCodexContextProxy({
|
|
|
4428
4458
|
let passthrough = false;
|
|
4429
4459
|
let discardOversized = false;
|
|
4430
4460
|
let pausedForBackpressure = false;
|
|
4431
|
-
const
|
|
4461
|
+
const writeOut = (text) => {
|
|
4432
4462
|
if (!stdout.write(text) && !pausedForBackpressure) {
|
|
4433
4463
|
pausedForBackpressure = true;
|
|
4434
4464
|
child.stdout.pause();
|
|
@@ -4438,6 +4468,54 @@ export function createCodexContextProxy({
|
|
|
4438
4468
|
});
|
|
4439
4469
|
}
|
|
4440
4470
|
};
|
|
4471
|
+
const forward = (text) => {
|
|
4472
|
+
if (engineeringHeld === null) {
|
|
4473
|
+
writeOut(text);
|
|
4474
|
+
return;
|
|
4475
|
+
}
|
|
4476
|
+
engineeringHeld.lines.push(text);
|
|
4477
|
+
engineeringHeld.bytes += Buffer.byteLength(text, 'utf8');
|
|
4478
|
+
if (engineeringHeld.bytes > MAX_LINE_BYTES) releaseEngineering();
|
|
4479
|
+
};
|
|
4480
|
+
/** The client's first successful initialize answer, which starts the hold. */
|
|
4481
|
+
function isEngineeringTrigger(message) {
|
|
4482
|
+
if (engineeringState !== 'waiting' || !isRecord(message)) return false;
|
|
4483
|
+
if (message.method !== undefined || !('result' in message)) return false;
|
|
4484
|
+
const key = clientRequestKey(message.id);
|
|
4485
|
+
return key !== null && initializeRequestKeys.has(key);
|
|
4486
|
+
}
|
|
4487
|
+
function holdForEngineering(text) {
|
|
4488
|
+
engineeringState = 'holding';
|
|
4489
|
+
initializeRequestKeys.clear();
|
|
4490
|
+
engineeringHeld = {
|
|
4491
|
+
lines: [text],
|
|
4492
|
+
bytes: Buffer.byteLength(text, 'utf8'),
|
|
4493
|
+
id: null,
|
|
4494
|
+
cancel: null,
|
|
4495
|
+
};
|
|
4496
|
+
try {
|
|
4497
|
+
engineeringHeld.id = sendOwned(
|
|
4498
|
+
'skills/extraRoots/set',
|
|
4499
|
+
{ extraRoots: [engineeringSkillsRoot] },
|
|
4500
|
+
'engineering',
|
|
4501
|
+
null,
|
|
4502
|
+
);
|
|
4503
|
+
} catch {
|
|
4504
|
+
releaseEngineering();
|
|
4505
|
+
return;
|
|
4506
|
+
}
|
|
4507
|
+
engineeringHeld.cancel = scheduleTimeout(() => releaseEngineering(), engineeringAckTimeoutMs);
|
|
4508
|
+
}
|
|
4509
|
+
/** Forward everything held, in order. Idempotent; the session never waits on the pack. */
|
|
4510
|
+
function releaseEngineering() {
|
|
4511
|
+
if (engineeringHeld === null) return;
|
|
4512
|
+
const held = engineeringHeld;
|
|
4513
|
+
engineeringHeld = null;
|
|
4514
|
+
engineeringState = 'done';
|
|
4515
|
+
held.cancel?.();
|
|
4516
|
+
if (held.id !== null && ownedRequests.has(held.id)) cleanupRequest(held.id);
|
|
4517
|
+
for (const text of held.lines) writeOut(text);
|
|
4518
|
+
}
|
|
4441
4519
|
const processLine = (line) => {
|
|
4442
4520
|
if (Buffer.byteLength(line, 'utf8') + 1 > MAX_LINE_BYTES) {
|
|
4443
4521
|
const ownedId = internalRequestId(line);
|
|
@@ -4451,6 +4529,10 @@ export function createCodexContextProxy({
|
|
|
4451
4529
|
try {
|
|
4452
4530
|
const message = JSON.parse(line.endsWith('\r') ? line.slice(0, -1) : line);
|
|
4453
4531
|
observeClientResponse(message);
|
|
4532
|
+
if (isEngineeringTrigger(message)) {
|
|
4533
|
+
holdForEngineering(`${line}\n`);
|
|
4534
|
+
return;
|
|
4535
|
+
}
|
|
4454
4536
|
forwardLine = handle(message);
|
|
4455
4537
|
} catch {
|
|
4456
4538
|
/* preserve malformed external protocol lines */
|
|
@@ -4536,6 +4618,7 @@ export function createCodexContextProxy({
|
|
|
4536
4618
|
}
|
|
4537
4619
|
});
|
|
4538
4620
|
child.stdout.on('end', () => {
|
|
4621
|
+
releaseEngineering();
|
|
4539
4622
|
const tail = inputBuffer + decoder.end();
|
|
4540
4623
|
if (tail && !discardOversized) forward(tail);
|
|
4541
4624
|
inputBuffer = '';
|
|
@@ -4565,6 +4648,7 @@ export function createCodexContextProxy({
|
|
|
4565
4648
|
observeClientRequest,
|
|
4566
4649
|
observeClientResponse,
|
|
4567
4650
|
async stop() {
|
|
4651
|
+
releaseEngineering();
|
|
4568
4652
|
if (stopped) {
|
|
4569
4653
|
await Promise.all(
|
|
4570
4654
|
[...diagnosticSinks.values()].map((sink) =>
|
|
@@ -4594,6 +4678,18 @@ export function createCodexContextProxy({
|
|
|
4594
4678
|
};
|
|
4595
4679
|
}
|
|
4596
4680
|
|
|
4681
|
+
/**
|
|
4682
|
+
* The launcher's AISAC Engineering skills root, or null. Only an absolute,
|
|
4683
|
+
* normalized path without control characters is used.
|
|
4684
|
+
*/
|
|
4685
|
+
export function engineeringSkillsRootFrom(value) {
|
|
4686
|
+
if (typeof value !== 'string' || value.length === 0) return null;
|
|
4687
|
+
if (value.length > MAX_ENGINEERING_PATH_LENGTH || /[\u0000-\u001f\u007f]/u.test(value))
|
|
4688
|
+
return null;
|
|
4689
|
+
if (!value.startsWith('/') || resolve(value) !== value) return null;
|
|
4690
|
+
return value;
|
|
4691
|
+
}
|
|
4692
|
+
|
|
4597
4693
|
async function main() {
|
|
4598
4694
|
if (process.argv.includes('--help')) {
|
|
4599
4695
|
process.stderr.write(
|
|
@@ -4648,6 +4744,7 @@ async function main() {
|
|
|
4648
4744
|
timeoutMs: numberEnv('CODEX_CONTEXT_COMPACT_TIMEOUT_MS', DEFAULT_TIMEOUT_MS),
|
|
4649
4745
|
policy,
|
|
4650
4746
|
createDiagnosticSink: diagnosticFactory,
|
|
4747
|
+
engineeringSkillsRoot: engineeringSkillsRootFrom(process.env[ENGINEERING_SKILLS_ROOT_ENV]),
|
|
4651
4748
|
});
|
|
4652
4749
|
const clientDecoder = new StringDecoder('utf8');
|
|
4653
4750
|
let clientInputBuffer = '';
|
|
@@ -16,7 +16,7 @@ export const MAX_LOG_FILES = 3;
|
|
|
16
16
|
const LOCK_WAIT_MS = 250;
|
|
17
17
|
const LOCK_STALE_MS = 30_000;
|
|
18
18
|
const ADMISSION_MS = 60 * 60 * 1000;
|
|
19
|
-
export const ADAPTER_VERSION = '0.4.0-beta.
|
|
19
|
+
export const ADAPTER_VERSION = '0.4.0-beta.4';
|
|
20
20
|
const MAX_DIAGNOSTIC_COUNT = 10_000_000;
|
|
21
21
|
const MAX_DIAGNOSTIC_BYTES = 128 * 1024 * 1024;
|
|
22
22
|
const CHECKPOINT_BACKENDS = new Set(['native_store', 'private_file']);
|
package/dist/cli.mjs
CHANGED
|
@@ -36,8 +36,10 @@ import {
|
|
|
36
36
|
resolvePackLocale,
|
|
37
37
|
writeStoredLocale,
|
|
38
38
|
} from './engineering-pack.mjs';
|
|
39
|
+
import { extensionsSummary, hooksCommand } from './extensions-cli.mjs';
|
|
40
|
+
import { anyFormatter } from './guardrails/format.mjs';
|
|
39
41
|
|
|
40
|
-
const VERSION = '0.4.0-beta.
|
|
42
|
+
const VERSION = '0.4.0-beta.4';
|
|
41
43
|
// The command is `aisac`; `aibrain` is an alias installed from the same entry.
|
|
42
44
|
const COMMAND = 'aisac';
|
|
43
45
|
const ENDPOINT = 'https://mcp.ai-brain.aisac.cloud/mcp';
|
|
@@ -55,7 +57,10 @@ function usage() {
|
|
|
55
57
|
'Claude shadow: setup --claude-shadow on|off and --claude-diagnostics on|off (preview stays applied=false).',
|
|
56
58
|
);
|
|
57
59
|
console.log(
|
|
58
|
-
'AISAC Engineering: `aisac claude`
|
|
60
|
+
'AISAC Engineering: `aisac claude` and `aisac codex` (Codex 0.164+) load the licensed method; setup --lang pt-BR|en picks its language; setup and doctor report the license; status stays offline. `aibrain` is an alias of `aisac`.',
|
|
61
|
+
);
|
|
62
|
+
console.log(
|
|
63
|
+
'Guardrails: `aisac hooks` lists them (a checklist on a terminal); `aisac hooks enable|disable <id>...` and `aisac hooks reset` change the next `aisac claude` session.',
|
|
59
64
|
);
|
|
60
65
|
}
|
|
61
66
|
|
|
@@ -539,6 +544,16 @@ async function setup(args) {
|
|
|
539
544
|
if (line !== null) console.log(line);
|
|
540
545
|
}
|
|
541
546
|
|
|
547
|
+
/** Extension state plus whether `format-on-edit` has a formatter in this folder (FR8). */
|
|
548
|
+
async function doctorExtensions() {
|
|
549
|
+
const summary = await extensionsSummary(stateRoot());
|
|
550
|
+
if (!summary.hooks.includes('format-on-edit')) return { ...summary, formatOnEdit: 'off' };
|
|
551
|
+
return {
|
|
552
|
+
...summary,
|
|
553
|
+
formatOnEdit: (await anyFormatter(process.cwd())) ? 'ready' : 'no_formatter_found',
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
|
|
542
557
|
/** @param {boolean} json */
|
|
543
558
|
async function doctor(json) {
|
|
544
559
|
const platform = platformCheck();
|
|
@@ -633,6 +648,7 @@ async function doctor(json) {
|
|
|
633
648
|
},
|
|
634
649
|
engineering,
|
|
635
650
|
engineeringPack: await engineeringPackState(),
|
|
651
|
+
extensions: await doctorExtensions(),
|
|
636
652
|
paidOrPilot: probe?.ok
|
|
637
653
|
? 'provider_free_probe_ok; paid entitlement or pilot invitation remains server-enforced'
|
|
638
654
|
: config
|
|
@@ -669,6 +685,7 @@ async function status(json) {
|
|
|
669
685
|
config?.claudeObservation?.disclosedAt !== undefined,
|
|
670
686
|
},
|
|
671
687
|
engineeringPack: await engineeringPackState(),
|
|
688
|
+
extensions: await extensionsSummary(stateRoot()),
|
|
672
689
|
monitoredSession: null,
|
|
673
690
|
monitoringStatus: 'not_observed',
|
|
674
691
|
};
|
|
@@ -717,6 +734,7 @@ async function main(argv) {
|
|
|
717
734
|
else console.log(records);
|
|
718
735
|
return;
|
|
719
736
|
}
|
|
737
|
+
if (command === 'hooks') return hooksCommand(rest);
|
|
720
738
|
if (command === 'disable') {
|
|
721
739
|
if (rest.length > 0) throw new Error('disable_does_not_accept_options');
|
|
722
740
|
return removeOwnedSetup();
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AISAC Engineering for Codex (ADR-0047 wave E4c, spec FR4).
|
|
5
|
+
*
|
|
6
|
+
* Codex has no session plugin directory, so the method reaches only the
|
|
7
|
+
* app-server this launch owns, and nothing is written to `~/.codex`,
|
|
8
|
+
* `~/.agents` or the project:
|
|
9
|
+
* - skills: the adapter sends `skills/extraRoots/set` with the pack's
|
|
10
|
+
* `skills/` directory right after the client's initialize (in-memory in that
|
|
11
|
+
* process);
|
|
12
|
+
* - agents: one role file per pack agent, declared with `-c agents.<name>.*`
|
|
13
|
+
* overrides on the owned app-server (the session-flags config layer).
|
|
14
|
+
* The launcher hands the monitor one JSON value; the monitor validates it
|
|
15
|
+
* again before turning it into arguments.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
19
|
+
|
|
20
|
+
export const CODEX_ENGINEERING_ENV = 'AISAC_ENGINEERING_CODEX';
|
|
21
|
+
export const CODEX_ENGINEERING_SKILLS_ROOT_ENV = 'AISAC_ENGINEERING_SKILLS_ROOT';
|
|
22
|
+
export const CODEX_AGENTS_DIRECTORY = 'codex-agents';
|
|
23
|
+
const SKILLS_DIRECTORY = 'skills';
|
|
24
|
+
const PACK_DIRECTORY = 'engineering';
|
|
25
|
+
const RUNTIME_DIRECTORY = 'runtime';
|
|
26
|
+
const CODEX_LAUNCH_DIRECTORY = /^codex-[A-Za-z0-9]{6}$/u;
|
|
27
|
+
const SCHEMA_VERSION = 1;
|
|
28
|
+
const MAX_SPEC_BYTES = 16 * 1024;
|
|
29
|
+
const MAX_AGENTS = 16;
|
|
30
|
+
const MAX_PATH_LENGTH = 4096;
|
|
31
|
+
export const MAX_AGENT_DESCRIPTION_LENGTH = 1024;
|
|
32
|
+
export const AGENT_NAME = /^[a-z][a-z0-9-]{0,63}$/u;
|
|
33
|
+
const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f]/u;
|
|
34
|
+
/** First Codex release whose session mechanism was verified (spike Q4, 2026-11-03). */
|
|
35
|
+
const MIN_CODEX_VERSION = /** @type {const} */ ([0, 164, 0]);
|
|
36
|
+
|
|
37
|
+
/** @typedef {{ name: string, description: string, configFile: string }} CodexAgentRole */
|
|
38
|
+
/** @typedef {{ schemaVersion: 1, skillsRoot: string, agents: CodexAgentRole[] }} CodexEngineering */
|
|
39
|
+
|
|
40
|
+
/** @param {string} code @returns {never} */
|
|
41
|
+
function fail(code) {
|
|
42
|
+
throw new Error(`codex_engineering_${code}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
46
|
+
function isRecord(value) {
|
|
47
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Whether this Codex version receives the pack. A null or malformed version
|
|
52
|
+
* never does.
|
|
53
|
+
* @param {string | null | undefined} version
|
|
54
|
+
*/
|
|
55
|
+
export function supportsCodexEngineering(version) {
|
|
56
|
+
const match = /^(\d+)\.(\d+)\.(\d+)$/u.exec(version ?? '');
|
|
57
|
+
if (match === null) return false;
|
|
58
|
+
const parts = match.slice(1).map(Number);
|
|
59
|
+
for (let index = 0; index < MIN_CODEX_VERSION.length; index += 1) {
|
|
60
|
+
const part = /** @type {number} */ (parts[index]);
|
|
61
|
+
const minimum = MIN_CODEX_VERSION[index] ?? 0;
|
|
62
|
+
if (part !== minimum) return part > minimum;
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @param {unknown} value */
|
|
68
|
+
function isCleanAbsolutePath(value) {
|
|
69
|
+
return (
|
|
70
|
+
typeof value === 'string' &&
|
|
71
|
+
value.length > 0 &&
|
|
72
|
+
value.length <= MAX_PATH_LENGTH &&
|
|
73
|
+
!CONTROL_CHARACTERS.test(value) &&
|
|
74
|
+
value.startsWith('/') &&
|
|
75
|
+
resolve(value) === value
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {unknown} value
|
|
81
|
+
* @param {{ stateDir?: string }} [options] when given, the pack must sit in a
|
|
82
|
+
* Codex launch directory of that state directory
|
|
83
|
+
* @returns {CodexEngineering}
|
|
84
|
+
*/
|
|
85
|
+
export function validateCodexEngineering(value, options = {}) {
|
|
86
|
+
if (!isRecord(value)) fail('invalid');
|
|
87
|
+
const keys = Object.keys(value).sort().join(',');
|
|
88
|
+
if (keys !== 'agents,schemaVersion,skillsRoot') fail('invalid');
|
|
89
|
+
if (value.schemaVersion !== SCHEMA_VERSION) fail('schema');
|
|
90
|
+
const skillsRoot = value.skillsRoot;
|
|
91
|
+
if (!isCleanAbsolutePath(skillsRoot)) fail('path');
|
|
92
|
+
const skills = /** @type {string} */ (skillsRoot);
|
|
93
|
+
if (basename(skills) !== SKILLS_DIRECTORY) fail('path');
|
|
94
|
+
const base = dirname(skills);
|
|
95
|
+
if (basename(base) !== PACK_DIRECTORY) fail('path');
|
|
96
|
+
if (options.stateDir !== undefined) {
|
|
97
|
+
const launch = dirname(base);
|
|
98
|
+
if (
|
|
99
|
+
!CODEX_LAUNCH_DIRECTORY.test(basename(launch)) ||
|
|
100
|
+
dirname(launch) !== join(resolve(options.stateDir), RUNTIME_DIRECTORY)
|
|
101
|
+
)
|
|
102
|
+
fail('path');
|
|
103
|
+
}
|
|
104
|
+
if (!Array.isArray(value.agents) || value.agents.length > MAX_AGENTS) fail('agents');
|
|
105
|
+
const seen = new Set();
|
|
106
|
+
/** @type {CodexAgentRole[]} */
|
|
107
|
+
const agents = [];
|
|
108
|
+
for (const agent of value.agents) {
|
|
109
|
+
if (!isRecord(agent) || Object.keys(agent).sort().join(',') !== 'configFile,description,name')
|
|
110
|
+
fail('agents');
|
|
111
|
+
const { name, description, configFile } = agent;
|
|
112
|
+
if (typeof name !== 'string' || !AGENT_NAME.test(name) || seen.has(name)) fail('agents');
|
|
113
|
+
if (
|
|
114
|
+
typeof description !== 'string' ||
|
|
115
|
+
description.trim().length === 0 ||
|
|
116
|
+
description.length > MAX_AGENT_DESCRIPTION_LENGTH ||
|
|
117
|
+
CONTROL_CHARACTERS.test(description)
|
|
118
|
+
)
|
|
119
|
+
fail('agents');
|
|
120
|
+
if (configFile !== join(base, CODEX_AGENTS_DIRECTORY, `${name}.toml`)) fail('agents');
|
|
121
|
+
seen.add(name);
|
|
122
|
+
agents.push({ name, description, configFile });
|
|
123
|
+
}
|
|
124
|
+
return { schemaVersion: SCHEMA_VERSION, skillsRoot: skills, agents };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Parse the monitor's environment value; null when absent.
|
|
129
|
+
* @param {string | undefined} raw
|
|
130
|
+
* @param {{ stateDir?: string }} [options]
|
|
131
|
+
* @returns {CodexEngineering | null}
|
|
132
|
+
*/
|
|
133
|
+
export function parseCodexEngineering(raw, options = {}) {
|
|
134
|
+
if (raw === undefined || raw === '') return null;
|
|
135
|
+
if (Buffer.byteLength(raw, 'utf8') > MAX_SPEC_BYTES) fail('too_large');
|
|
136
|
+
let value;
|
|
137
|
+
try {
|
|
138
|
+
value = JSON.parse(raw);
|
|
139
|
+
} catch {
|
|
140
|
+
fail('invalid');
|
|
141
|
+
}
|
|
142
|
+
return validateCodexEngineering(value, options);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A TOML basic string. JSON escapes are TOML escapes; DEL is escaped too.
|
|
147
|
+
* @param {string} value
|
|
148
|
+
*/
|
|
149
|
+
export function tomlBasicString(value) {
|
|
150
|
+
return JSON.stringify(value).replace(/\u007f/gu, '\\u007F');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Session-only `-c` overrides for the owned app-server, one role per agent.
|
|
155
|
+
* @param {CodexEngineering} engineering
|
|
156
|
+
* @returns {string[]}
|
|
157
|
+
*/
|
|
158
|
+
export function codexEngineeringConfigArgs(engineering) {
|
|
159
|
+
return engineering.agents.flatMap((agent) => [
|
|
160
|
+
'-c',
|
|
161
|
+
`agents.${agent.name}.description=${tomlBasicString(agent.description)}`,
|
|
162
|
+
'-c',
|
|
163
|
+
`agents.${agent.name}.config_file=${tomlBasicString(agent.configFile)}`,
|
|
164
|
+
]);
|
|
165
|
+
}
|