@aisac/cli 0.4.0-beta.21 → 0.4.0-beta.23

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/launch.mjs CHANGED
@@ -46,6 +46,19 @@ import {
46
46
  CODEX_ENGINEERING_SKILLS_ROOT_ENV,
47
47
  supportsCodexEngineering,
48
48
  } from './codex-engineering.mjs';
49
+ import {
50
+ MANAGER_SERVER_NAME,
51
+ ROLE_ENVIRONMENT_VARIABLES,
52
+ closeRole,
53
+ managerAllowRules,
54
+ managerMcpEntry,
55
+ prepareRole,
56
+ roleLaunchLine,
57
+ supportsWorkerRole,
58
+ workerEnvironment,
59
+ } from './manager/launch-role.mjs';
60
+ import { MANAGER_ROLE_ENV } from './manager/bus.mjs';
61
+ import { prepareManagerPlugin } from './manager/hooks.mjs';
49
62
  import {
50
63
  CODEX_LAUNCH_ENVELOPE,
51
64
  CODEX_MONITOR_CWD,
@@ -166,6 +179,9 @@ function childEnvironment(root) {
166
179
  delete environment[CODEX_HOOKS_ENV];
167
180
  delete environment[GUARDRAILS_SESSION_ENV];
168
181
  delete environment[REEXEC_ENV];
182
+ // A role is given by this launch alone (spec aisac-engineering-manager D2): one
183
+ // inherited from an outer session, such as a manager's shell, never reaches the host.
184
+ for (const variable of ROLE_ENVIRONMENT_VARIABLES) delete environment[variable];
169
185
  environment.AIBRAIN_STATE_DIR = resolve(root);
170
186
  return environment;
171
187
  }
@@ -483,11 +499,14 @@ export async function readSessionServers(root, client, environment) {
483
499
  * @property {typeof prepareCodexGuardrails} [prepareCodexGuardrails] Codex guardrail hooks (E6b)
484
500
  * @property {typeof refreshReleaseManifest} [refreshReleaseManifest] Release manifest refresh (ADR-0056 R2)
485
501
  * @property {typeof resolveHostExecutable} [resolveHost] Managed or system host resolution (ADR-0056 R4)
502
+ * @property {typeof prepareRole} [prepareRole] Manager or worker role registration (ADR-0058)
503
+ * @property {typeof prepareManagerPlugin} [prepareManagerPlugin] Manager hook nudges plugin writer (ADR-0058)
486
504
  */
487
505
 
488
506
  /**
489
507
  * @typedef {object} LaunchOptions
490
508
  * @property {import('./release-settings.mjs').HostMode | null} [hostMode] `--host managed|system` for this launch (FR5)
509
+ * @property {import('./manager/launch-role.mjs').RoleRequest | null} [role] `--manager`, or `--worker <name>`, with `--room` (ADR-0058)
491
510
  */
492
511
 
493
512
  /**
@@ -503,6 +522,8 @@ export async function launchClaude(config, root, args, dependencies = {}, launch
503
522
  const writeGuardrails = dependencies.prepareGuardrails ?? prepareGuardrails;
504
523
  const readServers = dependencies.readSessionServers ?? readSessionServers;
505
524
  const resolveHost = dependencies.resolveHost ?? resolveHostExecutable;
525
+ const registerRole = dependencies.prepareRole ?? prepareRole;
526
+ const writeManagerPlugin = dependencies.prepareManagerPlugin ?? prepareManagerPlugin;
506
527
  const validated = assertConfig(config);
507
528
  const launchArgs = await parseClaudeLaunchArgs(args, process.cwd());
508
529
  assertClaudeArgs(launchArgs.args);
@@ -686,12 +707,6 @@ export async function launchClaude(config, root, args, dependencies = {}, launch
686
707
  deny: mergedDeny,
687
708
  },
688
709
  };
689
- try {
690
- await privateJson(settingsPath, settings);
691
- } catch (error) {
692
- await rm(directory, { recursive: true, force: true });
693
- throw error;
694
- }
695
710
  // The pack never blocks the launch: every failure starts Claude without it.
696
711
  const engineering = await preparePack({
697
712
  endpoint: validated.endpoint,
@@ -718,9 +733,59 @@ export async function launchClaude(config, root, args, dependencies = {}, launch
718
733
  : await readServers(root, 'claude', process.env).catch(() => null);
719
734
  for (const line of servers === null ? [] : mcpLaunchLines(servers, 'claude'))
720
735
  process.stderr.write(`${line}\n`);
736
+ // The manager role (spec aisac-engineering-manager FR1, FR6-FR8) needs the same
737
+ // confirmed license and never blocks the launch; a second manager of the room
738
+ // stops it here, before any host starts.
739
+ /** @type {Awaited<ReturnType<typeof prepareRole>> | null} */
740
+ let role = null;
741
+ /** @type {{ pluginRoot: string } | null} */
742
+ let managerPlugin = null;
743
+ if (launchOptions.role != null) {
744
+ try {
745
+ role = await registerRole({
746
+ root,
747
+ request: launchOptions.role,
748
+ host: { name: 'claude', version: launchHost.version },
749
+ cwd: process.cwd(),
750
+ locale,
751
+ licensed: engineering.pluginRoot !== null,
752
+ qualified: true,
753
+ });
754
+ } catch (error) {
755
+ await rm(directory, { recursive: true, force: true }).catch(() => undefined);
756
+ throw error;
757
+ }
758
+ process.stderr.write(`${roleLaunchLine(role, locale)}\n`);
759
+ if (role.active) {
760
+ managerPlugin = await writeManagerPlugin({
761
+ directory,
762
+ packageRoot: validated.packageRoot,
763
+ }).catch(() => null);
764
+ // The read tools and `manager_decide` are pre-allowed; sending and cancelling
765
+ // are left to the host's permission flow (FR8).
766
+ for (const rule of managerAllowRules()) {
767
+ if (!mergedAllow.includes(rule)) mergedAllow.push(rule);
768
+ requiredAllow.push(rule);
769
+ }
770
+ }
771
+ }
772
+ /** Undo what this launch registered and wrote, when no host will run. */
773
+ const abandon = async () => {
774
+ await closeRole(role);
775
+ await rm(directory, { recursive: true, force: true }).catch(() => undefined);
776
+ };
777
+ try {
778
+ await privateJson(settingsPath, settings);
779
+ } catch (error) {
780
+ await abandon();
781
+ throw error;
782
+ }
721
783
  const mcp = {
722
784
  mcpServers: {
723
785
  ...(servers === null ? {} : claudeMcpEntries(servers.ready)),
786
+ ...(role !== null && role.active
787
+ ? { [MANAGER_SERVER_NAME]: managerMcpEntry(role, validated.packageRoot, process.execPath) }
788
+ : {}),
724
789
  [MCP_ALIAS]: {
725
790
  type: 'http',
726
791
  url: validated.endpoint,
@@ -731,7 +796,7 @@ export async function launchClaude(config, root, args, dependencies = {}, launch
731
796
  try {
732
797
  await privateJson(mcpPath, mcp);
733
798
  } catch (error) {
734
- await rm(directory, { recursive: true, force: true });
799
+ await abandon();
735
800
  throw error;
736
801
  }
737
802
  // The managed copy never updates itself (FR5); a system launch keeps the
@@ -741,6 +806,8 @@ export async function launchClaude(config, root, args, dependencies = {}, launch
741
806
  ? managedHostEnvironment(childEnvironment(root))
742
807
  : childEnvironment(root);
743
808
  environment.CLAUDE_CODE_ENABLE_FUNCTION_HOOKS = '1';
809
+ // The manager's hooks and `aisac workers --wait` read the room from here (FR9).
810
+ if (role !== null && role.active) environment[MANAGER_ROLE_ENV] = role.environmentValue;
744
811
  /** @type {import('node:child_process').ChildProcess} */
745
812
  let child;
746
813
  try {
@@ -757,16 +824,20 @@ export async function launchClaude(config, root, args, dependencies = {}, launch
757
824
  pluginRoot,
758
825
  ...(engineering.pluginRoot === null ? [] : ['--plugin-dir', engineering.pluginRoot]),
759
826
  ...(guardrails === null ? [] : ['--plugin-dir', guardrails.pluginRoot]),
827
+ ...(managerPlugin === null ? [] : ['--plugin-dir', managerPlugin.pluginRoot]),
760
828
  ...launchArgs.args,
761
829
  ],
762
830
  { cwd: process.cwd(), env: environment, stdio: 'inherit' },
763
831
  );
764
832
  } catch (error) {
765
- await rm(directory, { recursive: true, force: true }).catch(() => undefined);
833
+ await abandon();
766
834
  throw error;
767
835
  }
768
836
  // Listen for exit before any await, so a child that exits at once is not missed.
769
- const finished = waitForChild(child, directory);
837
+ const finished = waitForChild(child, directory).then(async (code) => {
838
+ await closeRole(role);
839
+ return code;
840
+ });
770
841
  await recordChild(directory, child.pid, launchHost);
771
842
  // Garbage collection after a successful launch (FR5): the versions the manifest
772
843
  // still carries lock sets for stay, as does any version a live launch runs.
@@ -795,12 +866,14 @@ export async function launchClaude(config, root, args, dependencies = {}, launch
795
866
  * @param {string} root
796
867
  * @param {string[]} args
797
868
  * @param {LaunchDependencies} [dependencies]
869
+ * @param {LaunchOptions} [launchOptions]
798
870
  */
799
- export async function launchCodex(config, root, args, dependencies = {}) {
871
+ export async function launchCodex(config, root, args, dependencies = {}, launchOptions = {}) {
800
872
  const preparePack = dependencies.preparePack ?? prepareEngineeringPack;
801
873
  const refreshManifest = dependencies.refreshReleaseManifest ?? refreshReleaseManifest;
802
874
  const readServers = dependencies.readSessionServers ?? readSessionServers;
803
875
  const writeCodexGuardrails = dependencies.prepareCodexGuardrails ?? prepareCodexGuardrails;
876
+ const registerRole = dependencies.prepareRole ?? prepareRole;
804
877
  const validated = assertConfig(config);
805
878
  assertCodexArgs(args);
806
879
  const captureLockPlatform = captureLockPlatformCheck();
@@ -898,6 +971,32 @@ export async function launchCodex(config, root, args, dependencies = {}) {
898
971
  process.stderr.write(`${guardrails.line}\n`);
899
972
  }
900
973
  }
974
+ // The worker role (spec aisac-engineering-manager FR1, D14) needs the license the
975
+ // pack confirmed and a Codex from the first release measured with it on; this launch
976
+ // has already refused a Codex whose protocol it does not know. The role never blocks
977
+ // the launch. A name another live session of the room has stops it here, before any
978
+ // host starts.
979
+ /** @type {Awaited<ReturnType<typeof prepareRole>> | null} */
980
+ let role = null;
981
+ if (launchOptions.role != null) {
982
+ const locale = resolvePackLocale(await readStoredLocale(root), process.env);
983
+ try {
984
+ role = await registerRole({
985
+ root,
986
+ request: launchOptions.role,
987
+ host: { name: 'codex', version: compatibility.version },
988
+ cwd: launchPlan.effectiveCwd,
989
+ locale,
990
+ licensed: engineering.codex != null,
991
+ qualified: supportsWorkerRole(compatibility.version),
992
+ });
993
+ } catch (error) {
994
+ await rm(directory, { recursive: true, force: true }).catch(() => undefined);
995
+ throw error;
996
+ }
997
+ process.stderr.write(`${roleLaunchLine(role, locale)}\n`);
998
+ if (role.active) Object.assign(environment, workerEnvironment(role, packageRoot));
999
+ }
901
1000
  environment.AI_BRAIN_MCP_SERVER = MCP_ALIAS;
902
1001
  environment.CODEX_BIN = executable;
903
1002
  environment.CODEX_MONITOR_COMMAND = monitor;
@@ -915,18 +1014,33 @@ export async function launchCodex(config, root, args, dependencies = {}) {
915
1014
  environment.CODEX_CONTEXT_TELEMETRY_ENABLED = validated.telemetry?.enabled === true ? '1' : '0';
916
1015
  environment.CODEX_CONTEXT_TELEMETRY_DISCLOSED =
917
1016
  validated.telemetry?.enabled === true && validated.telemetry?.disclosedAt != null ? '1' : '0';
918
- if (validated.schemaVersion === 2 && validated.context !== undefined) {
919
- environment.CODEX_CONTEXT_POLICY_JSON = codexPolicyJson(validated.context);
920
- } else {
921
- delete environment.CODEX_CONTEXT_POLICY_JSON;
1017
+ /** Undo what this launch registered and wrote, when no host will run. */
1018
+ const abandon = async () => {
1019
+ await closeRole(role);
1020
+ await rm(directory, { recursive: true, force: true }).catch(() => undefined);
1021
+ };
1022
+ /** @type {import('node:child_process').ChildProcess} */
1023
+ let child;
1024
+ try {
1025
+ if (validated.schemaVersion === 2 && validated.context !== undefined) {
1026
+ environment.CODEX_CONTEXT_POLICY_JSON = codexPolicyJson(validated.context);
1027
+ } else {
1028
+ delete environment.CODEX_CONTEXT_POLICY_JSON;
1029
+ }
1030
+ child = spawn(node, [terminal, '--', ...launchPlan.nativeArgs], {
1031
+ cwd: process.cwd(),
1032
+ env: environment,
1033
+ stdio: 'inherit',
1034
+ });
1035
+ } catch (error) {
1036
+ await abandon();
1037
+ throw error;
922
1038
  }
923
- const child = spawn(node, [terminal, '--', ...launchPlan.nativeArgs], {
924
- cwd: process.cwd(),
925
- env: environment,
926
- stdio: 'inherit',
927
- });
928
1039
  // Listen for exit before any await, so a child that exits at once is not missed.
929
- const finished = waitForChild(child, directory);
1040
+ const finished = waitForChild(child, directory).then(async (code) => {
1041
+ await closeRole(role);
1042
+ return code;
1043
+ });
930
1044
  await recordChild(directory, child.pid, launchHost);
931
1045
  return finished;
932
1046
  }