feat(fleet): brain-home split — fleet state under ~/.mosaic, templates stay config-home
ci/woodpecker/pr/ci Pipeline was successful
ci/woodpecker/pr/ci Pipeline was successful
Three-tree model per canon STRUCTURE-CANON §2 (first carried by the USC estate brain): seat launch envs, roles.local overrides, and profile working copies are user-owned fleet state and resolve from the brain home (~/.mosaic) when one is active; roster.yaml, baseline roles, run/, and services stay under the config home (~/.config/mosaic). Resolution (packages/mosaic/src/fleet/brain-home.ts, mirrored in tools/fleet/start-agent-session.sh): 1. MOSAIC_BRAIN_HOME env — explicit, always wins 2. canonical ~/.mosaic — adopted only when MOSAIC_HOME is the default ~/.config/mosaic AND ~/.mosaic/fleet/agents exists (custom --mosaic-home never adopts: tests/sandboxes stay hermetic) 3. else legacy single-tree behavior Call sites wired: resolveFleetPaths, fleet regen, fleet-agent-crud, fleet-migration, personas overrideDir, profiles dir, reconciler projections, generated-env boundary (validate + ensure + reject split state across trees). Tests: brain-home.spec (8), generated-env-boundary brain case, bash launcher brain + no-brain control cases; full fleet surface 733 green. Pre-existing unrelated failure noted: uninstall.spec 'missing mosaicHome' throws EACCES on this host with and without this change.
This commit is contained in:
@@ -12,6 +12,33 @@ The default tmux socket is `mosaic-fleet` so fleet commands do not touch the
|
|||||||
default tmux server. The roster is the desired-state authority; generated environment files are
|
default tmux server. The roster is the desired-state authority; generated environment files are
|
||||||
rebuildable projections, never a second source of configuration.
|
rebuildable projections, never a second source of configuration.
|
||||||
|
|
||||||
|
## Brain-home split (fleet state vs framework templates)
|
||||||
|
|
||||||
|
When a mosaic-brain clone is present, fleet **state** resolves from the brain
|
||||||
|
home while framework templates and dispatch state stay in the config home
|
||||||
|
(three-tree model, canon `docs/STRUCTURE-CANON.md` §2):
|
||||||
|
|
||||||
|
| Path | Without brain (legacy) | With brain |
|
||||||
|
| ------------------------------------------------------------------------------- | ------------------------------------- | ------------------------------ |
|
||||||
|
| `fleet/agents/<seat>.env.*` | `~/.config/mosaic/fleet/agents/` | `~/.mosaic/fleet/agents/` |
|
||||||
|
| `fleet/roles.local/` (overrides) | `~/.config/mosaic/fleet/roles.local/` | `~/.mosaic/fleet/roles.local/` |
|
||||||
|
| `fleet/profiles/` (working copies) | `~/.config/mosaic/fleet/profiles/` | `~/.mosaic/fleet/profiles/` |
|
||||||
|
| `fleet/roster.yaml`, `fleet/roles/` (baseline), `fleet/run/`, `fleet/services/` | `~/.config/mosaic/fleet/…` | unchanged (config home) |
|
||||||
|
|
||||||
|
Activation (`packages/mosaic/src/fleet/brain-home.ts`, mirrored in
|
||||||
|
`tools/fleet/start-agent-session.sh`):
|
||||||
|
|
||||||
|
1. `MOSAIC_BRAIN_HOME` env var — explicit, always wins.
|
||||||
|
2. Canonical `~/.mosaic` — adopted only when `MOSAIC_HOME` is the default
|
||||||
|
`~/.config/mosaic` AND `~/.mosaic/fleet/agents` exists. Custom
|
||||||
|
`--mosaic-home` values (tests, sandboxes, canaries) never adopt, keeping
|
||||||
|
them hermetic.
|
||||||
|
3. Otherwise the config home (legacy single-tree behavior).
|
||||||
|
|
||||||
|
Seat env dirs under a brain are subject to the same privacy boundary (0700
|
||||||
|
dirs, 0600 files); `.env.generated` files are structure-valuable and tracked
|
||||||
|
in the brain repo, hand-maintained `.env`/`.env.local` stay ignored and private.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
- `examples/minimal.yaml` starts one local canary slot.
|
- `examples/minimal.yaml` starts one local canary slot.
|
||||||
|
|||||||
@@ -80,6 +80,26 @@ safe_path "$MOSAIC_HOME" || fail_env unsafe-path MOSAIC_HOME "$MOSAIC_HOME"
|
|||||||
|
|
||||||
FLEET_DIR="$MOSAIC_HOME/fleet"
|
FLEET_DIR="$MOSAIC_HOME/fleet"
|
||||||
AGENT_ENV_DIR="$FLEET_DIR/agents"
|
AGENT_ENV_DIR="$FLEET_DIR/agents"
|
||||||
|
|
||||||
|
# Brain-home split (canon docs/STRUCTURE-CANON.md §2): seat launch envs live
|
||||||
|
# under the brain home's fleet/agents when a brain is active; roster, roles
|
||||||
|
# baseline, and runtime state (fleet/run) stay under MOSAIC_HOME.
|
||||||
|
# Resolution mirrors packages/mosaic/src/fleet/brain-home.ts:
|
||||||
|
# 1. MOSAIC_BRAIN_HOME env (explicit, always wins)
|
||||||
|
# 2. ~/.mosaic — adopted only when MOSAIC_HOME is the default config home AND
|
||||||
|
# ~/.mosaic/fleet/agents exists
|
||||||
|
# 3. MOSAIC_HOME (legacy single-tree)
|
||||||
|
BRAIN_HOME="${MOSAIC_BRAIN_HOME:-}"
|
||||||
|
if [ -z "$BRAIN_HOME" ]; then
|
||||||
|
BRAIN_HOME="$MOSAIC_HOME"
|
||||||
|
if [ "$(cd "$MOSAIC_HOME" 2>/dev/null && pwd -P)" = "$HOME/.config/mosaic" ] \
|
||||||
|
&& [ -d "$HOME/.mosaic/fleet/agents" ]; then
|
||||||
|
BRAIN_HOME="$HOME/.mosaic"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
if [ "$BRAIN_HOME" != "$MOSAIC_HOME" ]; then
|
||||||
|
AGENT_ENV_DIR="$BRAIN_HOME/fleet/agents"
|
||||||
|
fi
|
||||||
assert_managed_directory "$MOSAIC_HOME"
|
assert_managed_directory "$MOSAIC_HOME"
|
||||||
assert_managed_directory "$FLEET_DIR"
|
assert_managed_directory "$FLEET_DIR"
|
||||||
assert_private_directory "$AGENT_ENV_DIR"
|
assert_private_directory "$AGENT_ENV_DIR"
|
||||||
|
|||||||
@@ -167,6 +167,54 @@ if echo "$valid_args" | grep -qF 'bash -c'; then
|
|||||||
fail "launcher constructed a shell command payload"
|
fail "launcher constructed a shell command payload"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# ── Brain-home split (canon §2) ─────────────────────────────────────────
|
||||||
|
# When MOSAIC_HOME is the default config home under $HOME and the host carries
|
||||||
|
# $HOME/.mosaic/fleet/agents, seat envs resolve from the brain tree; the config
|
||||||
|
# home still owns fleet/run (holder-owner) and remains a managed boundary.
|
||||||
|
: > "$TMUX_CALLS"
|
||||||
|
HOME_BRAIN="$ROOT/brain-home"
|
||||||
|
CONFIG_HOME="$HOME_BRAIN/.config/mosaic"
|
||||||
|
BRAIN="$HOME_BRAIN/.mosaic"
|
||||||
|
mkdir -p "$CONFIG_HOME/fleet/run" "$BRAIN/fleet/agents" "$HOME_BRAIN/work"
|
||||||
|
chmod 700 "$CONFIG_HOME" "$CONFIG_HOME/fleet" "$CONFIG_HOME/fleet/run" \
|
||||||
|
"$BRAIN/fleet/agents" "$HOME_BRAIN/work"
|
||||||
|
printf '123e4567-e89b-12d3-a456-426614174000\n' > "$CONFIG_HOME/fleet/run/holder-owner"
|
||||||
|
chmod 600 "$CONFIG_HOME/fleet/run/holder-owner"
|
||||||
|
cat > "$BRAIN/fleet/agents/coder-brain.env.generated" <<EOF
|
||||||
|
MOSAIC_AGENT_NAME=coder-brain
|
||||||
|
MOSAIC_AGENT_CLASS=code
|
||||||
|
MOSAIC_AGENT_RUNTIME=pi
|
||||||
|
MOSAIC_AGENT_MODEL=openai-codex/gpt-5.6-sol
|
||||||
|
MOSAIC_AGENT_REASONING=high
|
||||||
|
MOSAIC_AGENT_TOOL_POLICY=code
|
||||||
|
MOSAIC_AGENT_WORKDIR=$HOME_BRAIN/work
|
||||||
|
MOSAIC_TMUX_SOCKET=mosaic-test
|
||||||
|
EOF
|
||||||
|
chmod 600 "$BRAIN/fleet/agents/coder-brain.env.generated"
|
||||||
|
install_pane_binaries "$HOME_BRAIN"
|
||||||
|
HOME="$HOME_BRAIN" PATH="$FAKE_BIN:$PATH" MOSAIC_TEST_TMUX_CALLS="$TMUX_CALLS" \
|
||||||
|
MOSAIC_TEST_PANE_PID=$$ MOSAIC_TEST_HOME="$HOME_BRAIN" \
|
||||||
|
MOSAIC_TEST_FLEET_OWNER=123e4567-e89b-12d3-a456-426614174000 \
|
||||||
|
MOSAIC_HOME="$CONFIG_HOME" "$START" coder-brain
|
||||||
|
brain_args=$(tr '\0' '\n' < "$TMUX_CALLS")
|
||||||
|
echo "$brain_args" | grep -qF new-session || fail "brain-home generated projection did not reach tmux"
|
||||||
|
echo "$brain_args" | grep -qF 'coder-brain' || fail "brain-home agent env was not the launch source"
|
||||||
|
[ -f "$BRAIN/fleet/agents/coder-brain.env.generated" ] || fail "brain generated env vanished"
|
||||||
|
|
||||||
|
# Negative control: the SAME default-config-home shape but without
|
||||||
|
# ~/.mosaic/fleet/agents — the config-home env tree is used directly (legacy).
|
||||||
|
: > "$TMUX_CALLS"
|
||||||
|
HOME_NOBRAIN="$ROOT/brainless-home"
|
||||||
|
CONFIG_HOME_NOBRAIN="$HOME_NOBRAIN/.config/mosaic"
|
||||||
|
write_generated "$CONFIG_HOME_NOBRAIN" "coder-legacy"
|
||||||
|
install_pane_binaries "$HOME_NOBRAIN"
|
||||||
|
HOME="$HOME_NOBRAIN" PATH="$FAKE_BIN:$PATH" MOSAIC_TEST_TMUX_CALLS="$TMUX_CALLS" \
|
||||||
|
MOSAIC_TEST_PANE_PID=$$ MOSAIC_TEST_HOME="$HOME_NOBRAIN" \
|
||||||
|
MOSAIC_TEST_FLEET_OWNER=123e4567-e89b-12d3-a456-426614174000 \
|
||||||
|
MOSAIC_HOME="$CONFIG_HOME_NOBRAIN" "$START" coder-legacy
|
||||||
|
legacy_args=$(tr '\0' '\n' < "$TMUX_CALLS")
|
||||||
|
echo "$legacy_args" | grep -qF new-session || fail "legacy single-tree launch regressed"
|
||||||
|
|
||||||
# The pane must start through an absolute clean-environment boundary. Its
|
# The pane must start through an absolute clean-environment boundary. Its
|
||||||
# runtime command remains an argv vector, but no holder/session environment
|
# runtime command remains an argv vector, but no holder/session environment
|
||||||
# control variable can pass through the pane command.
|
# control variable can pass through the pane command.
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { readFile } from 'node:fs/promises';
|
import { readFile } from 'node:fs/promises';
|
||||||
import { join, resolve } from 'node:path';
|
import { join, resolve } from 'node:path';
|
||||||
|
import { fleetAgentEnvDir, fleetRolesLocalDir } from '../fleet/brain-home.js';
|
||||||
import type { Command } from 'commander';
|
import type { Command } from 'commander';
|
||||||
import {
|
import {
|
||||||
executeFleetAgentMutation,
|
executeFleetAgentMutation,
|
||||||
@@ -149,9 +150,9 @@ async function executeCommand(
|
|||||||
request,
|
request,
|
||||||
mosaicHome,
|
mosaicHome,
|
||||||
rosterPath,
|
rosterPath,
|
||||||
agentEnvDir: join(mosaicHome, 'fleet', 'agents'),
|
agentEnvDir: fleetAgentEnvDir(mosaicHome),
|
||||||
rolesDir: join(mosaicHome, 'fleet', 'roles'),
|
rolesDir: join(mosaicHome, 'fleet', 'roles'),
|
||||||
overrideDir: join(mosaicHome, 'fleet', 'roles.local'),
|
overrideDir: fleetRolesLocalDir(mosaicHome),
|
||||||
dryRun: forceDryRun || opts.dryRun === true,
|
dryRun: forceDryRun || opts.dryRun === true,
|
||||||
...(deps.projectionApplier === undefined ? {} : { projectionApplier: deps.projectionApplier }),
|
...(deps.projectionApplier === undefined ? {} : { projectionApplier: deps.projectionApplier }),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { readFile } from 'node:fs/promises';
|
import { readFile } from 'node:fs/promises';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
|
import { fleetAgentEnvDir, fleetRolesLocalDir } from '../fleet/brain-home.js';
|
||||||
import type { Command } from 'commander';
|
import type { Command } from 'commander';
|
||||||
import {
|
import {
|
||||||
parseV1MigrationObservations,
|
parseV1MigrationObservations,
|
||||||
@@ -120,11 +121,11 @@ export function registerFleetMigrationCommand(
|
|||||||
observations,
|
observations,
|
||||||
personaDirs: {
|
personaDirs: {
|
||||||
rolesDir: deps.rolesDir ?? join(mosaicHome, 'fleet', 'roles'),
|
rolesDir: deps.rolesDir ?? join(mosaicHome, 'fleet', 'roles'),
|
||||||
overrideDir: deps.overrideDir ?? join(mosaicHome, 'fleet', 'roles.local'),
|
overrideDir: deps.overrideDir ?? fleetRolesLocalDir(mosaicHome),
|
||||||
},
|
},
|
||||||
environment: {
|
environment: {
|
||||||
mosaicHome,
|
mosaicHome,
|
||||||
agentEnvDir: join(mosaicHome, 'fleet', 'agents'),
|
agentEnvDir: fleetAgentEnvDir(mosaicHome),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
printJson(preview);
|
printJson(preview);
|
||||||
|
|||||||
@@ -30,19 +30,21 @@ import { lstat, readFile, readdir, stat } from 'node:fs/promises';
|
|||||||
import { homedir } from 'node:os';
|
import { homedir } from 'node:os';
|
||||||
import { basename, isAbsolute, join, sep } from 'node:path';
|
import { basename, isAbsolute, join, sep } from 'node:path';
|
||||||
import type { Command } from 'commander';
|
import type { Command } from 'commander';
|
||||||
|
import { fleetRolesLocalDir } from '../fleet/brain-home.js';
|
||||||
|
|
||||||
function defaultMosaicHome(): string {
|
function defaultMosaicHome(): string {
|
||||||
return process.env['MOSAIC_HOME'] ?? join(homedir(), '.config', 'mosaic');
|
return process.env['MOSAIC_HOME'] ?? join(homedir(), '.config', 'mosaic');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Baseline persona role contracts (reseeded on update). */
|
/** Baseline persona role contracts (reseeded on update; config home — framework). */
|
||||||
export function defaultRolesDir(mosaicHome = defaultMosaicHome()): string {
|
export function defaultRolesDir(mosaicHome = defaultMosaicHome()): string {
|
||||||
return join(mosaicHome, 'fleet', 'roles');
|
return join(mosaicHome, 'fleet', 'roles');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** PRESERVE-protected override layer (survives update; wins on merge). */
|
/** PRESERVE-protected override layer (survives update; wins on merge).
|
||||||
|
* Brain home (`~/.mosaic/fleet/roles.local`) when a brain is active. */
|
||||||
export function defaultOverrideDir(mosaicHome = defaultMosaicHome()): string {
|
export function defaultOverrideDir(mosaicHome = defaultMosaicHome()): string {
|
||||||
return join(mosaicHome, 'fleet', 'roles.local');
|
return fleetRolesLocalDir(mosaicHome);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import { homedir } from 'node:os';
|
|||||||
import { basename, join } from 'node:path';
|
import { basename, join } from 'node:path';
|
||||||
import type { Command } from 'commander';
|
import type { Command } from 'commander';
|
||||||
import YAML from 'yaml';
|
import YAML from 'yaml';
|
||||||
|
import { fleetProfilesDir } from '../fleet/brain-home.js';
|
||||||
import {
|
import {
|
||||||
defaultOverrideDir,
|
defaultOverrideDir,
|
||||||
extractClassesFromDir,
|
extractClassesFromDir,
|
||||||
@@ -36,9 +37,10 @@ function defaultMosaicHome(): string {
|
|||||||
return process.env['MOSAIC_HOME'] ?? join(homedir(), '.config', 'mosaic');
|
return process.env['MOSAIC_HOME'] ?? join(homedir(), '.config', 'mosaic');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Directory holding the seeded profile yaml files. */
|
/** Directory holding the seeded profile yaml files — brain home when active
|
||||||
|
* (user working copies, committed), else the config home seed. */
|
||||||
export function defaultProfilesDir(mosaicHome = defaultMosaicHome()): string {
|
export function defaultProfilesDir(mosaicHome = defaultMosaicHome()): string {
|
||||||
return join(mosaicHome, 'fleet', 'profiles');
|
return fleetProfilesDir(mosaicHome);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Directory holding the persona role contracts. */
|
/** Directory holding the persona role contracts. */
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { homedir } from 'node:os';
|
|||||||
import { join, relative, resolve } from 'node:path';
|
import { join, relative, resolve } from 'node:path';
|
||||||
import type { Command } from 'commander';
|
import type { Command } from 'commander';
|
||||||
import type { CommandRunner } from './fleet.js';
|
import type { CommandRunner } from './fleet.js';
|
||||||
|
import { fleetAgentEnvDir } from '../fleet/brain-home.js';
|
||||||
import {
|
import {
|
||||||
applyPreparedGeneratedAgentEnvironmentProjection,
|
applyPreparedGeneratedAgentEnvironmentProjection,
|
||||||
prepareGeneratedAgentEnvironmentProjection,
|
prepareGeneratedAgentEnvironmentProjection,
|
||||||
@@ -153,7 +154,7 @@ export async function executeFleetRegen(
|
|||||||
options: FleetRegenOptions,
|
options: FleetRegenOptions,
|
||||||
): Promise<FleetRegenResult> {
|
): Promise<FleetRegenResult> {
|
||||||
const mosaicHome = defaultMosaicHome(deps);
|
const mosaicHome = defaultMosaicHome(deps);
|
||||||
const agentEnvDir = join(mosaicHome, 'fleet', 'agents');
|
const agentEnvDir = fleetAgentEnvDir(mosaicHome);
|
||||||
const rosterPath = join(mosaicHome, 'fleet', 'roster.yaml');
|
const rosterPath = join(mosaicHome, 'fleet', 'roster.yaml');
|
||||||
const readRoster = deps.readRoster ?? defaultReadRoster(deps, mosaicHome);
|
const readRoster = deps.readRoster ?? defaultReadRoster(deps, mosaicHome);
|
||||||
const prepare = deps.prepareProjection ?? prepareGeneratedAgentEnvironmentProjection;
|
const prepare = deps.prepareProjection ?? prepareGeneratedAgentEnvironmentProjection;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
import { randomUUID } from 'node:crypto';
|
import { randomUUID } from 'node:crypto';
|
||||||
import { homedir, hostname, userInfo } from 'node:os';
|
import { homedir, hostname, userInfo } from 'node:os';
|
||||||
import { dirname, join, resolve } from 'node:path';
|
import { dirname, join, resolve } from 'node:path';
|
||||||
|
import { fleetAgentEnvDir } from '../fleet/brain-home.js';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { spawn } from 'node:child_process';
|
import { spawn } from 'node:child_process';
|
||||||
import * as readline from 'node:readline';
|
import * as readline from 'node:readline';
|
||||||
@@ -158,7 +159,7 @@ export function resolveFleetPaths(mosaicHome = defaultMosaicHome()): FleetPaths
|
|||||||
fleetToolsDir: join(mosaicHome, 'tools', 'fleet'),
|
fleetToolsDir: join(mosaicHome, 'tools', 'fleet'),
|
||||||
tmuxToolsDir: join(mosaicHome, 'tools', 'tmux'),
|
tmuxToolsDir: join(mosaicHome, 'tools', 'tmux'),
|
||||||
systemdUserDir: join(homedir(), '.config', 'systemd', 'user'),
|
systemdUserDir: join(homedir(), '.config', 'systemd', 'user'),
|
||||||
agentEnvDir: join(mosaicHome, 'fleet', 'agents'),
|
agentEnvDir: fleetAgentEnvDir(mosaicHome),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,114 @@
|
|||||||
|
import { mkdir, mkdtemp, rm } from 'node:fs/promises';
|
||||||
|
import { homedir, tmpdir } from 'node:os';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||||
|
import {
|
||||||
|
brainHomeIsActive,
|
||||||
|
fleetAgentEnvDir,
|
||||||
|
fleetProfilesDir,
|
||||||
|
fleetRolesLocalDir,
|
||||||
|
fleetStateDir,
|
||||||
|
resolveBrainHome,
|
||||||
|
type BrainHomeOptions,
|
||||||
|
} from './brain-home.js';
|
||||||
|
|
||||||
|
describe('fleet brain-home resolution', (): void => {
|
||||||
|
let cleanup: string | undefined;
|
||||||
|
|
||||||
|
const savedBrainEnv = process.env['MOSAIC_BRAIN_HOME'];
|
||||||
|
|
||||||
|
beforeEach((): void => {
|
||||||
|
delete process.env['MOSAIC_BRAIN_HOME'];
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async (): Promise<void> => {
|
||||||
|
if (savedBrainEnv === undefined) {
|
||||||
|
delete process.env['MOSAIC_BRAIN_HOME'];
|
||||||
|
} else {
|
||||||
|
process.env['MOSAIC_BRAIN_HOME'] = savedBrainEnv;
|
||||||
|
}
|
||||||
|
if (cleanup !== undefined) {
|
||||||
|
await rm(cleanup, { recursive: true, force: true });
|
||||||
|
cleanup = undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function makeTmp(): Promise<string> {
|
||||||
|
const root = await mkdtemp(join(tmpdir(), 'mosaic-brain-home-'));
|
||||||
|
cleanup = root;
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
it('MOSAIC_BRAIN_HOME env wins over every other signal', (): void => {
|
||||||
|
process.env['MOSAIC_BRAIN_HOME'] = '/explicit/brain';
|
||||||
|
expect(resolveBrainHome('/any/mosaic-home')).toBe('/explicit/brain');
|
||||||
|
expect(fleetAgentEnvDir('/any/mosaic-home')).toBe('/explicit/brain/fleet/agents');
|
||||||
|
expect(brainHomeIsActive('/any/mosaic-home')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('injected envBrainHome wins identically (test seam)', (): void => {
|
||||||
|
const opts: BrainHomeOptions = { envBrainHome: '/injected/brain' };
|
||||||
|
expect(resolveBrainHome('/any/mosaic-home', opts)).toBe('/injected/brain');
|
||||||
|
expect(fleetAgentEnvDir('/any/mosaic-home', opts)).toBe('/injected/brain/fleet/agents');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('a non-default mosaicHome never adopts the canonical brain (hermetic legacy)', (): void => {
|
||||||
|
const mosaicHome = '/tmp/not-the-default-config-home';
|
||||||
|
expect(resolveBrainHome(mosaicHome)).toBe(mosaicHome);
|
||||||
|
expect(brainHomeIsActive(mosaicHome)).toBe(false);
|
||||||
|
expect(fleetAgentEnvDir(mosaicHome)).toBe(join(mosaicHome, 'fleet', 'agents'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('the default config home adopts the brain when it carries fleet/agents', async (): Promise<void> => {
|
||||||
|
const root = await makeTmp();
|
||||||
|
const brain = join(root, 'brain');
|
||||||
|
await mkdir(join(brain, 'fleet', 'agents'), { recursive: true });
|
||||||
|
const configHome = join(root, 'config', 'mosaic');
|
||||||
|
const opts: BrainHomeOptions = { homes: { brain, configDefault: configHome } };
|
||||||
|
|
||||||
|
expect(resolveBrainHome(configHome, opts)).toBe(brain);
|
||||||
|
expect(fleetAgentEnvDir(configHome, opts)).toBe(join(brain, 'fleet', 'agents'));
|
||||||
|
expect(fleetRolesLocalDir(configHome, opts)).toBe(join(brain, 'fleet', 'roles.local'));
|
||||||
|
expect(fleetProfilesDir(configHome, opts)).toBe(join(brain, 'fleet', 'profiles'));
|
||||||
|
expect(fleetStateDir(configHome, opts)).toBe(join(brain, 'fleet'));
|
||||||
|
expect(brainHomeIsActive(configHome, opts)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('the default config home stays legacy when no brain exists', async (): Promise<void> => {
|
||||||
|
const root = await makeTmp();
|
||||||
|
const configHome = join(root, 'config', 'mosaic');
|
||||||
|
const opts: BrainHomeOptions = {
|
||||||
|
homes: { brain: join(root, 'brain'), configDefault: configHome },
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(resolveBrainHome(configHome, opts)).toBe(configHome);
|
||||||
|
expect(brainHomeIsActive(configHome, opts)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('an empty MOSAIC_BRAIN_HOME is ignored, not treated as set', (): void => {
|
||||||
|
process.env['MOSAIC_BRAIN_HOME'] = ' ';
|
||||||
|
expect(resolveBrainHome('/tmp/legacy-home')).toBe('/tmp/legacy-home');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('adoption requires fleet/agents specifically, not any brain content', async (): Promise<void> => {
|
||||||
|
const root = await makeTmp();
|
||||||
|
const brain = join(root, 'brain');
|
||||||
|
await mkdir(join(brain, 'fleet'), { recursive: true }); // fleet without agents
|
||||||
|
const configHome = join(root, 'config', 'mosaic');
|
||||||
|
const opts: BrainHomeOptions = { homes: { brain, configDefault: configHome } };
|
||||||
|
|
||||||
|
expect(resolveBrainHome(configHome, opts)).toBe(configHome);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('real-home control: a host brain is adopted only through the default home', (): void => {
|
||||||
|
// Control on the un-injected path: this host carries ~/.mosaic/fleet/agents,
|
||||||
|
// so the default config home resolves to the brain or legacy — both valid
|
||||||
|
// canonical endpoints — while a non-default home never adopts.
|
||||||
|
const defaultHome = join(homedir(), '.config', 'mosaic');
|
||||||
|
const resolved = resolveBrainHome(defaultHome);
|
||||||
|
expect([defaultHome, join(homedir(), '.mosaic')]).toContain(resolved);
|
||||||
|
expect(resolveBrainHome(join(homedir(), 'elsewhere', 'mosaic'))).toBe(
|
||||||
|
join(homedir(), 'elsewhere', 'mosaic'),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
import { existsSync } from 'node:fs';
|
||||||
|
import { homedir } from 'node:os';
|
||||||
|
import { join, resolve } from 'node:path';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overridable resolution inputs (tests inject tmp homes; production reads
|
||||||
|
* the environment and the real home directory).
|
||||||
|
*/
|
||||||
|
export interface BrainHomeOptions {
|
||||||
|
/** Explicit brain home; defaults to `MOSAIC_BRAIN_HOME`. */
|
||||||
|
readonly envBrainHome?: string;
|
||||||
|
/**
|
||||||
|
* Canonical homes used for adoption. Defaults derive from the real
|
||||||
|
* `homedir()`: `{ brain: ~/.mosaic, configDefault: ~/.config/mosaic }`.
|
||||||
|
*/
|
||||||
|
readonly homes?: { readonly brain: string; readonly configDefault: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Brain-home resolution — the three-tree fleet split (stack canon
|
||||||
|
* `docs/STRUCTURE-CANON.md` §2, first carried by the USC estate brain):
|
||||||
|
*
|
||||||
|
* config home (~/.config/mosaic) framework templates + dispatch state:
|
||||||
|
* fleet/roles (baseline), fleet/roster.yaml,
|
||||||
|
* fleet/run (heartbeats), fleet/services
|
||||||
|
* brain home (~/.mosaic) user-owned fleet state, committed:
|
||||||
|
* fleet/agents/<seat>.env.*, fleet/roles.local,
|
||||||
|
* fleet/profiles working copies
|
||||||
|
*
|
||||||
|
* Resolution order:
|
||||||
|
* 1. `MOSAIC_BRAIN_HOME` env (explicit, always wins)
|
||||||
|
* 2. canonical `~/.mosaic` — adopted ONLY when mosaicHome is the real
|
||||||
|
* default config home AND `~/.mosaic/fleet/agents` exists. Custom
|
||||||
|
* `--mosaic-home` values (tests, sandboxes, canaries) never trigger
|
||||||
|
* adoption, keeping them hermetic and deterministic.
|
||||||
|
* 3. mosaicHome itself (legacy single-tree behavior).
|
||||||
|
*/
|
||||||
|
export function resolveBrainHome(mosaicHome: string, options: BrainHomeOptions = {}): string {
|
||||||
|
const explicit = options.envBrainHome ?? process.env['MOSAIC_BRAIN_HOME'];
|
||||||
|
if (explicit !== undefined && explicit.trim() !== '') {
|
||||||
|
return explicit;
|
||||||
|
}
|
||||||
|
const homes = options.homes ?? {
|
||||||
|
brain: join(homedir(), '.mosaic'),
|
||||||
|
configDefault: join(homedir(), '.config', 'mosaic'),
|
||||||
|
};
|
||||||
|
if (resolve(mosaicHome) !== resolve(homes.configDefault)) {
|
||||||
|
return mosaicHome;
|
||||||
|
}
|
||||||
|
return existsSync(join(homes.brain, 'fleet', 'agents')) ? homes.brain : mosaicHome;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** True when fleet state resolves somewhere other than the config home. */
|
||||||
|
export function brainHomeIsActive(mosaicHome: string, options: BrainHomeOptions = {}): boolean {
|
||||||
|
return resolve(resolveBrainHome(mosaicHome, options)) !== resolve(mosaicHome);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Fleet state root (brain home when active, else the config home). */
|
||||||
|
export function fleetStateDir(mosaicHome: string, options: BrainHomeOptions = {}): string {
|
||||||
|
return join(resolveBrainHome(mosaicHome, options), 'fleet');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Seat launch envs — `<brainHome>/fleet/agents` when a brain is active. */
|
||||||
|
export function fleetAgentEnvDir(mosaicHome: string, options: BrainHomeOptions = {}): string {
|
||||||
|
return join(fleetStateDir(mosaicHome, options), 'agents');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** PRESERVE-protected persona override layer — `<brainHome>/fleet/roles.local`. */
|
||||||
|
export function fleetRolesLocalDir(mosaicHome: string, options: BrainHomeOptions = {}): string {
|
||||||
|
return join(fleetStateDir(mosaicHome, options), 'roles.local');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** System-type profiles (user working copies) — `<brainHome>/fleet/profiles`. */
|
||||||
|
export function fleetProfilesDir(mosaicHome: string, options: BrainHomeOptions = {}): string {
|
||||||
|
return join(fleetStateDir(mosaicHome, options), 'profiles');
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import { lstat, open, readFile, unlink, type FileHandle } from 'node:fs/promises
|
|||||||
import { randomUUID } from 'node:crypto';
|
import { randomUUID } from 'node:crypto';
|
||||||
import { homedir } from 'node:os';
|
import { homedir } from 'node:os';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
|
import { fleetAgentEnvDir } from './brain-home.js';
|
||||||
import {
|
import {
|
||||||
applyPreparedAgentEnvironmentProjection,
|
applyPreparedAgentEnvironmentProjection,
|
||||||
prepareAgentEnvironmentProjection,
|
prepareAgentEnvironmentProjection,
|
||||||
@@ -617,7 +618,7 @@ function defaultPrepareProjections(
|
|||||||
(agent: FleetRosterV2Agent): Promise<PreparedAgentEnvironmentProjection> =>
|
(agent: FleetRosterV2Agent): Promise<PreparedAgentEnvironmentProjection> =>
|
||||||
prepareAgentEnvironmentProjection({
|
prepareAgentEnvironmentProjection({
|
||||||
mosaicHome,
|
mosaicHome,
|
||||||
agentEnvDir: join(mosaicHome, 'fleet', 'agents'),
|
agentEnvDir: fleetAgentEnvDir(mosaicHome),
|
||||||
agentName: agent.name,
|
agentName: agent.name,
|
||||||
generated: projectRosterV2AgentGeneratedEnv(roster, agent),
|
generated: projectRosterV2AgentGeneratedEnv(roster, agent),
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -176,6 +176,52 @@ describe('generated fleet agent environment boundary', (): void => {
|
|||||||
expect((await stat(result.generatedPath)).mode & 0o777).toBe(0o600);
|
expect((await stat(result.generatedPath)).mode & 0o777).toBe(0o600);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('brain home: accepts and writes projections under MOSAIC_BRAIN_HOME/fleet/agents', async (): Promise<void> => {
|
||||||
|
const savedBrainHome = process.env['MOSAIC_BRAIN_HOME'];
|
||||||
|
try {
|
||||||
|
cleanup = await mkdtemp(join(tmpdir(), 'mosaic-generated-env-'));
|
||||||
|
const mosaicHome = join(cleanup, 'config-home');
|
||||||
|
const brainHome = join(cleanup, 'brain');
|
||||||
|
const agentEnvDir = join(brainHome, 'fleet', 'agents');
|
||||||
|
process.env['MOSAIC_BRAIN_HOME'] = brainHome;
|
||||||
|
|
||||||
|
const result = await writeAgentEnvironmentProjection({
|
||||||
|
mosaicHome,
|
||||||
|
agentEnvDir,
|
||||||
|
agentName: 'coder0',
|
||||||
|
generated: generatedValues,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Projection landed in the brain tree, not under the config home.
|
||||||
|
expect(result.generatedPath).toBe(join(agentEnvDir, 'coder0.env.generated'));
|
||||||
|
expect((await stat(join(brainHome, 'fleet'))).mode & 0o777).toBe(0o700);
|
||||||
|
expect((await stat(agentEnvDir)).mode & 0o777).toBe(0o700);
|
||||||
|
expect((await stat(result.generatedPath)).mode & 0o777).toBe(0o600);
|
||||||
|
await expect(stat(join(mosaicHome, 'fleet'))).rejects.toThrow();
|
||||||
|
|
||||||
|
// A config-home agentEnvDir is now REJECTED while the brain is active —
|
||||||
|
// the boundary must not silently split state across two trees.
|
||||||
|
let rejected: unknown;
|
||||||
|
try {
|
||||||
|
await writeAgentEnvironmentProjection({
|
||||||
|
mosaicHome,
|
||||||
|
agentEnvDir: join(mosaicHome, 'fleet', 'agents'),
|
||||||
|
agentName: 'coder1',
|
||||||
|
generated: { ...generatedValues, MOSAIC_AGENT_NAME: 'coder1' },
|
||||||
|
});
|
||||||
|
} catch (caught: unknown) {
|
||||||
|
rejected = caught;
|
||||||
|
}
|
||||||
|
expect(rejected).toBeInstanceOf(AgentEnvBoundaryError);
|
||||||
|
} finally {
|
||||||
|
if (savedBrainHome === undefined) {
|
||||||
|
delete process.env['MOSAIC_BRAIN_HOME'];
|
||||||
|
} else {
|
||||||
|
process.env['MOSAIC_BRAIN_HOME'] = savedBrainHome;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it('regenerates desired keys, relocates safe legacy local data, and quarantines forbidden legacy input', async (): Promise<void> => {
|
it('regenerates desired keys, relocates safe legacy local data, and quarantines forbidden legacy input', async (): Promise<void> => {
|
||||||
cleanup = await mkdtemp(join(tmpdir(), 'mosaic-generated-env-'));
|
cleanup = await mkdtemp(join(tmpdir(), 'mosaic-generated-env-'));
|
||||||
const mosaicHome = join(cleanup, 'mosaic');
|
const mosaicHome = join(cleanup, 'mosaic');
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { createHash, randomUUID } from 'node:crypto';
|
|||||||
import { chmod, lstat, mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
import { chmod, lstat, mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
||||||
import { homedir } from 'node:os';
|
import { homedir } from 'node:os';
|
||||||
import { dirname, join, resolve } from 'node:path';
|
import { dirname, join, resolve } from 'node:path';
|
||||||
|
import { fleetAgentEnvDir, resolveBrainHome } from './brain-home.js';
|
||||||
import { compareCodePoints } from './deterministic-order.js';
|
import { compareCodePoints } from './deterministic-order.js';
|
||||||
|
|
||||||
export type AgentEnvironmentKind = 'generated' | 'local';
|
export type AgentEnvironmentKind = 'generated' | 'local';
|
||||||
@@ -528,12 +529,15 @@ async function validatePrivateProjectionDirectory(
|
|||||||
mosaicHome: string,
|
mosaicHome: string,
|
||||||
agentEnvDir: string,
|
agentEnvDir: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const fleetDir = join(mosaicHome, 'fleet');
|
// Brain-home split (canon §2): seat envs live under the brain home's
|
||||||
const expectedAgentEnvDir = join(fleetDir, 'agents');
|
// fleet/agents when a brain is active; roster + templates stay config-home.
|
||||||
|
const expectedAgentEnvDir = fleetAgentEnvDir(mosaicHome);
|
||||||
if (resolve(agentEnvDir) !== resolve(expectedAgentEnvDir)) {
|
if (resolve(agentEnvDir) !== resolve(expectedAgentEnvDir)) {
|
||||||
throw new AgentEnvBoundaryError('unsafe-directory', '(directory)', agentEnvDir);
|
throw new AgentEnvBoundaryError('unsafe-directory', '(directory)', agentEnvDir);
|
||||||
}
|
}
|
||||||
await assertManagedDirectoryIfPresent(mosaicHome, false);
|
const stateHome = resolveBrainHome(mosaicHome);
|
||||||
|
const fleetDir = join(stateHome, 'fleet');
|
||||||
|
await assertManagedDirectoryIfPresent(stateHome, false);
|
||||||
await assertManagedDirectoryIfPresent(fleetDir, false);
|
await assertManagedDirectoryIfPresent(fleetDir, false);
|
||||||
await assertManagedDirectoryIfPresent(agentEnvDir, true);
|
await assertManagedDirectoryIfPresent(agentEnvDir, true);
|
||||||
}
|
}
|
||||||
@@ -543,8 +547,9 @@ async function ensurePrivateProjectionDirectory(
|
|||||||
agentEnvDir: string,
|
agentEnvDir: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await validatePrivateProjectionDirectory(mosaicHome, agentEnvDir);
|
await validatePrivateProjectionDirectory(mosaicHome, agentEnvDir);
|
||||||
const fleetDir = join(mosaicHome, 'fleet');
|
const stateHome = resolveBrainHome(mosaicHome);
|
||||||
await ensureManagedDirectory(mosaicHome, false);
|
const fleetDir = join(stateHome, 'fleet');
|
||||||
|
await ensureManagedDirectory(stateHome, false);
|
||||||
await ensureManagedDirectory(fleetDir, false);
|
await ensureManagedDirectory(fleetDir, false);
|
||||||
await ensureManagedDirectory(agentEnvDir, true);
|
await ensureManagedDirectory(agentEnvDir, true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user