Compare commits

...
Author SHA1 Message Date
marcieandorch-01 9aa4983cf2 fix: use canonical dogfood seat identity (#1490)
ci/woodpecker/push/publish Pipeline was successful
Co-authored-by: marcie <[email protected]>
2026-08-30 23:43:22 +00:00
marcieandorch-01 736b0affc1 compose: add wrapper-first dogfood workspace (#1488)
ci/woodpecker/push/publish Pipeline was successful
Co-authored-by: marcie <[email protected]>
2026-08-30 22:29:30 +00:00
orch-01 e18d13d36f fleet: split-home-safe mosaic launcher (T110, P5-RM-009 stack side) (#1480)
ci/woodpecker/push/publish Pipeline was successful
2026-08-30 10:05:40 +00:00
marcieandorch-01 60bc5d2022 compose: pin MOSAIC_STORAGE_TIER=standalone (A5d formal-run fix) (#1486)
ci/woodpecker/push/publish Pipeline was successful
Co-authored-by: marcie <[email protected]>
2026-08-30 07:49:35 +00:00
marcie ea91cfc421 compose: stack profile — one-command standalone deployment (A5) (#1485)
ci/woodpecker/push/publish Pipeline was successful
2026-08-30 06:03:39 +00:00
marcie acf640d00f docs: containerization plan + PRD D15 (tiered deployment, standalone v1 bar) (#1484)
ci/woodpecker/push/publish Pipeline was successful
2026-08-30 05:21:42 +00:00
fredandmarcie 431ead3a18 feat(gateway,cli): agent enrollment command family (M4-4b) (#1483)
ci/woodpecker/push/publish Pipeline was successful
Co-authored-by: fred <[email protected]>
2026-08-30 04:30:05 +00:00
fred 143ba0f57a db: agent enrollment schema (M4-4a, migration 0021) (#1482)
ci/woodpecker/push/publish Pipeline was canceled
2026-08-30 01:47:06 +00:00
fred ee815a72b1 docs: agent enrollment command family v1 design (M4-4-0) (#1481)
ci/woodpecker/push/publish Pipeline was canceled
2026-08-30 01:21:00 +00:00
marcieandorch-01 94d626dff9 mosaic comms: socket resolution is tool-owned (B2) (#1476)
ci/woodpecker/push/publish Pipeline was successful
Co-authored-by: marcie <[email protected]>
2026-08-29 23:35:37 +00:00
marcieandorch-01 ee6c842918 R3: --body-file <path>/- across body/comment carriers (#1474)
ci/woodpecker/push/publish Pipeline was canceled
Co-authored-by: marcie <[email protected]>
2026-08-29 22:49:20 +00:00
marcieandorch-01 ba3b854d50 D1/D3: --number canonical on issue wrappers, --labels alias on list wrappers (#1475)
ci/woodpecker/push/publish Pipeline was canceled
Co-authored-by: marcie <[email protected]>
2026-08-29 22:29:16 +00:00
fred c7a7fd07cc brain/gateway: prohibit mission_tasks.status as a write source (M4-3a phase 1) (#1479)
ci/woodpecker/push/publish Pipeline was canceled
2026-08-29 21:59:13 +00:00
fred 5399c6b7e7 docs: P0 field-map currency verification at next@abb0c936 (M4-3a-0) (#1478)
ci/woodpecker/push/publish Pipeline was canceled
2026-08-29 21:17:05 +00:00
marcieandorch-01 e09b8783b4 P1b: read-only viewers join the R1/R4 usage contract (#1472)
ci/woodpecker/push/publish Pipeline was canceled
Co-authored-by: marcie <[email protected]>
2026-08-29 21:03:07 +00:00
72 changed files with 11704 additions and 470 deletions
+15 -155
View File
@@ -1,156 +1,16 @@
# ─────────────────────────────────────────────────────────────────────────────
# Mosaic — Environment Variables Reference
# Copy this file to .env and fill in the values for your deployment.
# Lines beginning with # are comments; optional vars are commented out.
# ─────────────────────────────────────────────────────────────────────────────
# Mosaic Stack standalone deployment (compose `stack` profile)
# Copy to .env and adjust. Port overrides exist because the defaults
# collide with common host services (and with the dev compose itself).
PG_HOST_PORT=5433
VALKEY_HOST_PORT=6380
GATEWAY_HOST_PORT=14242
# Registry image override (defaults to a local build of docker/gateway.Dockerfile):
# GATEWAY_IMAGE=git.mosaicstack.dev/mosaicstack/stack/gateway:sha-acf640d
# ─── Database (PostgreSQL 17 + pgvector) ─────────────────────────────────────
# Full connection string used by the gateway, ORM, and migration runner.
# Port 5433 avoids conflict with a host-side PostgreSQL instance.
DATABASE_URL=postgresql://mosaic:mosaic@localhost:5433/mosaic
# Docker Compose host-port override for the PostgreSQL container (default: 5433)
# PG_HOST_PORT=5433
# ─── Queue (Valkey 8 / Redis-compatible) ─────────────────────────────────────
# Port 6380 avoids conflict with a host-side Redis/Valkey instance.
VALKEY_URL=redis://localhost:6380
# Docker Compose host-port override for the Valkey container (default: 6380)
# VALKEY_HOST_PORT=6380
# ─── Gateway ─────────────────────────────────────────────────────────────────
# TCP port the NestJS/Fastify gateway listens on (default: 14242)
GATEWAY_PORT=14242
# Comma-separated list of allowed CORS origins.
# Must include the web app origin in production.
GATEWAY_CORS_ORIGIN=http://localhost:3000
# ─── Auth (BetterAuth) ───────────────────────────────────────────────────────
# REQUIRED — random secret used to sign sessions and tokens.
# Generate with: openssl rand -base64 32
BETTER_AUTH_SECRET=change-me-to-a-random-32-char-string
# Public base URL of the gateway (used by BetterAuth for callback URLs)
BETTER_AUTH_URL=http://localhost:14242
# ─── Web App (SPA) ───────────────────────────────────────────────────────────
# Directory holding the built SPA bundle (vite build output). When set, the
# gateway serves the SPA same-origin; when unset (dev), run the Vite dev
# server (pnpm --filter @mosaicstack/web dev), which proxies to the gateway.
# safe-default: unset in dev — SPA serving is an opt-in production concern
#WEB_DIST_DIR=apps/web/dist
# ─── OpenTelemetry ───────────────────────────────────────────────────────────
# OTLP HTTP endpoint (otel-collector or any OpenTelemetry-compatible backend)
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
# Service name shown in traces
OTEL_SERVICE_NAME=mosaic-gateway
# ─── AI Providers ────────────────────────────────────────────────────────────
# Ollama (local models — set OLLAMA_BASE_URL to enable)
# OLLAMA_BASE_URL=http://localhost:11434
# OLLAMA_HOST is a legacy alias for OLLAMA_BASE_URL
# OLLAMA_HOST=http://localhost:11434
# Comma-separated list of Ollama model IDs to register (default: llama3.2,codellama,mistral)
# OLLAMA_MODELS=llama3.2,codellama,mistral
# Anthropic (claude-sonnet-4-6, claude-opus-4-6, claude-haiku-4-5)
# ANTHROPIC_API_KEY=sk-ant-...
# OpenAI (gpt-4o, gpt-4o-mini, o3-mini)
# OPENAI_API_KEY=sk-...
# Z.ai / GLM (glm-4.5, glm-4.5-air, glm-4.5-flash)
# ZAI_API_KEY=...
# Custom providers — JSON array of provider configs
# Format: [{"id":"<id>","baseUrl":"<url>","apiKey":"<key>","models":[{"id":"<model-id>","name":"<label>"}]}]
# MOSAIC_CUSTOM_PROVIDERS=
# ─── Embedding Service ───────────────────────────────────────────────────────
# OpenAI-compatible embeddings endpoint (default: OpenAI)
# EMBEDDING_API_URL=https://api.openai.com/v1
# EMBEDDING_MODEL=text-embedding-3-small
# ─── Log Summarization Service ───────────────────────────────────────────────
# OpenAI-compatible chat completions endpoint for log summarization (default: OpenAI)
# SUMMARIZATION_API_URL=https://api.openai.com/v1
# SUMMARIZATION_MODEL=gpt-4o-mini
# Cron schedule for summarization job (default: every 6 hours)
# SUMMARIZATION_CRON=0 */6 * * *
# Cron schedule for log tier management (default: daily at 03:00)
# TIER_MANAGEMENT_CRON=0 3 * * *
# ─── Agent ───────────────────────────────────────────────────────────────────
# Filesystem sandbox root for agent file tools (default: process.cwd())
# AGENT_FILE_SANDBOX_DIR=/var/lib/mosaic/sandbox
# Comma-separated list of tool names available to non-admin users.
# Leave unset to allow all tools for all authenticated users.
# AGENT_USER_TOOLS=read_file,list_directory,search_files
# System prompt injected into every agent session (optional)
# AGENT_SYSTEM_PROMPT=You are a helpful assistant.
# ─── MCP Servers ─────────────────────────────────────────────────────────────
# JSON array of MCP server configs — set to enable MCP tool integration.
# Each entry: {"name":"<id>","url":"<http-or-sse-url>"}
# MCP_SERVERS=[{"name":"my-mcp","url":"http://localhost:3100/sse"}]
# ─── Coordinator ─────────────────────────────────────────────────────────────
# Root directory used to scope coordinator (worktree/repo) operations.
# Defaults to the monorepo root auto-detected from process.cwd().
# MOSAIC_WORKSPACE_ROOT=/home/user/projects/mosaic
# ─── Discord Plugin (optional — set DISCORD_BOT_TOKEN to enable) ─────────────
# DISCORD_BOT_TOKEN=
# DISCORD_GUILD_ID=
# DISCORD_GATEWAY_URL=http://localhost:14242
# ─── Telegram Plugin (optional — set TELEGRAM_BOT_TOKEN to enable) ───────────
# TELEGRAM_BOT_TOKEN=
# TELEGRAM_GATEWAY_URL=http://localhost:14242
# ─── SSO Providers (add credentials to enable) ───────────────────────────────
# --- Authentik (optional — set AUTHENTIK_CLIENT_ID to enable) ---
# AUTHENTIK_ISSUER=https://auth.example.com/application/o/mosaic/
# AUTHENTIK_CLIENT_ID=
# AUTHENTIK_CLIENT_SECRET=
# --- WorkOS (optional — set WORKOS_CLIENT_ID to enable) ---
# WORKOS_ISSUER=https://your-company.authkit.app
# WORKOS_CLIENT_ID=client_...
# WORKOS_CLIENT_SECRET=sk_live_...
# --- Keycloak (optional — set KEYCLOAK_CLIENT_ID to enable) ---
# KEYCLOAK_ISSUER=https://auth.example.com/realms/master
# Legacy alternative if you prefer to compose the issuer from separate vars:
# KEYCLOAK_URL=https://auth.example.com
# KEYCLOAK_REALM=master
# KEYCLOAK_CLIENT_ID=mosaic
# KEYCLOAK_CLIENT_SECRET=
# The web login page discovers configured providers dynamically from
# GET /api/sso/providers. No NEXT_PUBLIC_* provider feature flag is required.
# Optional explicit dogfood overlay (docker-compose.dogfood.yml).
# All three paths are required when that overlay is used. Use a dedicated
# next-based worktree, its canonical clone's .git directory, and the external
# home of the unprivileged code-dogfood-01 functional seat.
# MOSAIC_DOGFOOD_WORKTREE=/home/example/src/mosaic-stack-worktrees/dogfood-1487
# MOSAIC_DOGFOOD_COMMON_GIT_DIR=/home/example/src/mosaic-stack/.git
# MOSAIC_DOGFOOD_SEAT_HOME=/home/example/.mosaic/fleet/agents/code-dogfood-01
+45
View File
@@ -208,6 +208,51 @@ mosaic telemetry upload # Dry-run unless opted in
Consent state is persisted in config. Remote upload is a no-op until you run `mosaic telemetry opt-in`.
## Standalone container deployment
The `stack` profile runs PostgreSQL, Valkey, the gateway, and the bundled webUI. Copy
`.env.example` to `.env`, generate `BETTER_AUTH_SECRET`, then start the profile:
```bash
cp .env.example .env
printf 'BETTER_AUTH_SECRET=%s\n' "$(openssl rand -hex 32)" >> .env
docker compose --profile stack up -d
```
The optional dogfood overlay gives one dedicated in-stack agent a writable stack
worktree and its own read-only credential slot. It does not mount the fleet brain or
any other seat. Prepare a `next`-based worktree and an unprivileged
`code-dogfood-01` functional seat outside the container, then set these paths in
`.env`:
```dotenv
MOSAIC_DOGFOOD_WORKTREE=/path/to/mosaic-stack-worktrees/dogfood-1487
MOSAIC_DOGFOOD_COMMON_GIT_DIR=/path/to/mosaic-stack/.git
MOSAIC_DOGFOOD_SEAT_HOME=/path/to/.mosaic/fleet/agents/code-dogfood-01
```
The common Git directory must match the worktree's `.git` pointer. The seat home
must contain only that seat's credential at
`secrets/gitea-mosaicstack-code-dogfood-01.token`. Never place the token value in
`.env`. Start the overlay with:
```bash
docker compose \
-f docker-compose.yml \
-f docker-compose.dogfood.yml \
--profile stack up -d
```
The overlay removes the general shell tool for every session, including admins.
File tools stay inside the mounted checkout. Two dedicated delivery tools stage
explicit paths, run the CI queue guard, push through `git-credential-mosaic`, and
open PRs through `pr-create.sh`. They resolve only the `code-dogfood-01` slot and fail
if it is absent. The overlay enables Docker's init process so the R4 helper can
establish the gateway's seat lineage below PID 1.
This deployment route is separate from the local source-development restrictions
below.
## Development
### Prerequisites
+4 -2
View File
@@ -27,10 +27,11 @@ import { McpClientService } from '../mcp-client/mcp-client.service.js';
import { SkillLoaderService } from './skill-loader.service.js';
import { createBrainTools } from './tools/brain-tools.js';
import { createCoordTools } from './tools/coord-tools.js';
import { createDeliveryTools } from './tools/delivery-tools.js';
import { createMemoryTools } from './tools/memory-tools.js';
import { createFileTools } from './tools/file-tools.js';
import { createGitTools } from './tools/git-tools.js';
import { createShellTools } from './tools/shell-tools.js';
import { createShellToolsIfEnabled } from './tools/shell-tools.js';
import { createWebTools } from './tools/web-tools.js';
import { createSearchTools } from './tools/search-tools.js';
import type { SessionInfoDto, SessionMetrics } from './session.dto.js';
@@ -167,7 +168,8 @@ export class AgentService implements OnModuleDestroy {
),
...createFileTools(sandboxDir),
...createGitTools(sandboxDir),
...createShellTools(sandboxDir),
...createShellToolsIfEnabled(sandboxDir),
...createDeliveryTools(sandboxDir),
...createWebTools(),
...createSearchTools(),
];
@@ -0,0 +1,210 @@
import { afterEach, describe, expect, it } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import type { ToolDefinition } from '@mariozechner/pi-coding-agent';
import { createFileTools } from './file-tools.js';
import { createShellTools, createShellToolsIfEnabled } from './shell-tools.js';
import {
createDeliveryTools,
type DeliveryToolEnvironment,
type ProcessResult,
type ProcessRunner,
} from './delivery-tools.js';
const tempDirs: string[] = [];
function tempDir(prefix: string): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
tempDirs.push(dir);
return dir;
}
function textOf(result: unknown): string {
const typed = result as { content: Array<{ text: string }> };
return typed.content.map((item) => item.text).join('\n');
}
async function execute(tool: ToolDefinition, params: Record<string, unknown>): Promise<unknown> {
return (
tool.execute as unknown as (id: string, input: Record<string, unknown>) => Promise<unknown>
)('test-call', params);
}
function ok(stdout = ''): ProcessResult {
return { exitCode: 0, stdout, stderr: '', timedOut: false };
}
function deliveryEnv(extra: Partial<DeliveryToolEnvironment> = {}): DeliveryToolEnvironment {
return {
AGENT_DELIVERY_ENABLED: 'true',
MOSAIC_GIT_TOOLS_DIR: '/opt/mosaic/tools/git',
MOSAIC_GIT_IDENTITY: 'code-dogfood-01',
MOSAIC_AGENT_NAME: 'code-dogfood-01',
MOSAIC_BRAIN_HOME: '/opt/mosaic/brain',
MOSAIC_INTEGRATION_TRUNK: 'next',
HOME: '/home/node',
PATH: '/usr/bin:/bin',
...extra,
};
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
describe('dogfood execution boundary', () => {
it('removes shell_exec mechanically while its first-token bypass red control stays live', async () => {
const sandbox = tempDir('mosaic-shell-boundary-');
expect(createShellToolsIfEnabled(sandbox, { AGENT_SHELL_ENABLED: 'false' })).toEqual([]);
const redControl = createShellTools(sandbox)[0]!;
const result = await execute(redControl, { command: 'env printf FIRST_TOKEN_BYPASS' });
expect(textOf(result)).toContain('FIRST_TOKEN_BYPASS');
});
it('refuses an outside-sandbox token-shaped read and proves the path guard is the enforcement', async () => {
const root = tempDir('mosaic-file-boundary-');
const sandbox = path.join(root, 'workspace', 'stack');
const token = path.join(
root,
'brain',
'fleet',
'agents',
'code-dogfood-01',
'secrets',
'gitea-mosaicstack-code-dogfood-01.token',
);
fs.mkdirSync(sandbox, { recursive: true });
fs.mkdirSync(path.dirname(token), { recursive: true });
fs.writeFileSync(token, 'OUTSIDE_SANDBOX_SENTINEL');
const read = createFileTools(sandbox).find((tool) => tool.name === 'fs_read_file')!;
const refused = await execute(read, { path: token });
expect(textOf(refused)).toContain('Path escape attempt blocked');
expect(textOf(refused)).not.toContain('OUTSIDE_SANDBOX_SENTINEL');
fs.symlinkSync(token, path.join(sandbox, 'credential.token'));
const symlinkRefused = await execute(read, { path: 'credential.token' });
expect(textOf(symlinkRefused)).toContain('Path escape attempt blocked');
expect(textOf(symlinkRefused)).not.toContain('OUTSIDE_SANDBOX_SENTINEL');
const redRead = createFileTools(root).find((tool) => tool.name === 'fs_read_file')!;
const redControl = await execute(redRead, { path: token });
expect(textOf(redControl)).toContain('OUTSIDE_SANDBOX_SENTINEL');
});
});
describe('delivery tools', () => {
it('stay absent unless explicitly enabled and reject identity mismatch', () => {
const sandbox = tempDir('mosaic-delivery-disabled-');
expect(createDeliveryTools(sandbox, {})).toEqual([]);
expect(() =>
createDeliveryTools(sandbox, deliveryEnv({ MOSAIC_AGENT_NAME: 'another-seat' })),
).toThrow('matching safe MOSAIC agent and git identities');
});
it('publishes through execFile-only git and queue operations with a scrubbed environment', async () => {
const sandbox = tempDir('mosaic-delivery-publish-');
fs.writeFileSync(path.join(sandbox, 'change.md'), 'change');
const calls: Array<{ file: string; args: readonly string[]; env: NodeJS.ProcessEnv }> = [];
const runner: ProcessRunner = async (file, args, options) => {
calls.push({ file, args, env: options.env });
if (args[0] === 'branch') return ok('feat/1487-dogfood-proof\n');
return ok();
};
const hostile = {
...deliveryEnv(),
BASH_ENV: '/tmp/injected',
'BASH_FUNC_read%%': '() { :; }',
GITEA_TOKEN: 'must-not-cross',
} as DeliveryToolEnvironment;
const publish = createDeliveryTools(sandbox, hostile, runner).find(
(tool) => tool.name === 'git_publish_branch',
)!;
const result = await execute(publish, {
issue: 1487,
paths: ['change.md'],
commitMessage: 'docs: dogfood proof (#1487)',
});
expect(textOf(result)).toBe('Published branch feat/1487-dogfood-proof as code-dogfood-01.');
expect(calls.map((call) => call.file)).toEqual([
'/usr/bin/git',
'/usr/bin/git',
'/usr/bin/git',
'/opt/mosaic/tools/git/ci-queue-wait.sh',
'/usr/bin/git',
]);
expect(calls[3]!.args).toEqual(['--purpose', 'push', '-B', 'feat/1487-dogfood-proof']);
expect(calls[4]!.args).toEqual(['push', '--set-upstream', 'origin', 'feat/1487-dogfood-proof']);
for (const call of calls) {
expect(call.file).not.toMatch(/(?:^|\/)sh$/);
expect(call.env).not.toHaveProperty('BASH_ENV');
expect(Object.keys(call.env).some((key) => key.startsWith('BASH_FUNC_'))).toBe(false);
expect(call.env).not.toHaveProperty('GITEA_TOKEN');
expect(call.env.MOSAIC_GIT_IDENTITY).toBe('code-dogfood-01');
}
});
it('opens PRs only through pr-create.sh against next', async () => {
const sandbox = tempDir('mosaic-delivery-pr-');
const calls: Array<{ file: string; args: readonly string[] }> = [];
const runner: ProcessRunner = async (file, args) => {
calls.push({ file, args });
if (args[0] === 'branch') return ok('feat/1487-dogfood-proof\n');
return ok('https://git.mosaicstack.dev/mosaicstack/stack/pulls/999\n');
};
const openPr = createDeliveryTools(sandbox, deliveryEnv(), runner).find(
(tool) => tool.name === 'git_open_pull_request',
)!;
const result = await execute(openPr, {
issue: 1487,
title: 'docs: dogfood proof',
body: 'Measured from the in-stack agent.',
});
expect(textOf(result)).toContain('/pulls/999');
expect(calls[1]!.file).toBe('/opt/mosaic/tools/git/pr-create.sh');
expect(calls[1]!.args).toEqual([
'-t',
'docs: dogfood proof',
'-b',
'Measured from the in-stack agent.',
'-B',
'next',
'-H',
'feat/1487-dogfood-proof',
'-i',
'1487',
]);
});
it('blocks publish paths outside the sandbox before staging', async () => {
const root = tempDir('mosaic-delivery-path-');
const sandbox = path.join(root, 'sandbox');
const outside = path.join(root, 'outside.md');
fs.mkdirSync(sandbox);
fs.writeFileSync(outside, 'OUTSIDE_DELIVERY_SENTINEL');
const calls: Array<{ file: string; args: readonly string[] }> = [];
const runner: ProcessRunner = async (file, args) => {
calls.push({ file, args });
return args[0] === 'branch' ? ok('feat/1487-dogfood-proof\n') : ok();
};
const publish = createDeliveryTools(sandbox, deliveryEnv(), runner).find(
(tool) => tool.name === 'git_publish_branch',
)!;
const result = await execute(publish, {
issue: 1487,
paths: [outside],
commitMessage: 'docs: must not publish',
});
expect(textOf(result)).toContain('Path escape attempt blocked');
expect(textOf(result)).not.toContain('OUTSIDE_DELIVERY_SENTINEL');
expect(calls).toHaveLength(1);
});
});
@@ -0,0 +1,282 @@
import { Type } from '@sinclair/typebox';
import type { ToolDefinition } from '@mariozechner/pi-coding-agent';
import { spawn } from 'node:child_process';
import path from 'node:path';
import { guardPath, SandboxEscapeError } from './path-guard.js';
const PROCESS_TIMEOUT_MS = 120_000;
const MAX_OUTPUT_BYTES = 100 * 1024;
const SAFE_IDENTITY = /^[a-z0-9][a-z0-9-]{0,62}$/;
const SAFE_BRANCH = /^(?:feat|fix|docs|test)\/[a-z0-9][a-z0-9._/-]*$/i;
export interface ProcessResult {
exitCode: number | null;
stdout: string;
stderr: string;
timedOut: boolean;
}
export type ProcessRunner = (
file: string,
args: readonly string[],
options: { cwd: string; env: NodeJS.ProcessEnv; timeoutMs: number },
) => Promise<ProcessResult>;
export interface DeliveryToolEnvironment {
AGENT_DELIVERY_ENABLED?: string;
MOSAIC_GIT_TOOLS_DIR?: string;
MOSAIC_GIT_IDENTITY?: string;
MOSAIC_AGENT_NAME?: string;
MOSAIC_BRAIN_HOME?: string;
MOSAIC_CREDENTIAL_SPOOL?: string;
MOSAIC_CREDENTIAL_LINEAGE_FENCE?: string;
MOSAIC_INTEGRATION_TRUNK?: string;
HOME?: string;
PATH?: string;
LANG?: string;
LC_ALL?: string;
}
function runProcess(
file: string,
args: readonly string[],
options: { cwd: string; env: NodeJS.ProcessEnv; timeoutMs: number },
): Promise<ProcessResult> {
return new Promise((resolve) => {
const child = spawn(file, [...args], {
cwd: options.cwd,
env: options.env,
shell: false,
stdio: ['ignore', 'pipe', 'pipe'],
});
let stdout = '';
let stderr = '';
let timedOut = false;
let outputBytes = 0;
const append = (current: string, chunk: Buffer): string => {
const remaining = MAX_OUTPUT_BYTES - outputBytes;
if (remaining <= 0) return current;
outputBytes += chunk.length;
return current + chunk.subarray(0, remaining).toString();
};
child.stdout.on('data', (chunk: Buffer) => {
stdout = append(stdout, chunk);
});
child.stderr.on('data', (chunk: Buffer) => {
stderr = append(stderr, chunk);
});
const timer = setTimeout(() => {
timedOut = true;
child.kill('SIGTERM');
}, options.timeoutMs);
child.on('error', (error) => {
clearTimeout(timer);
resolve({ exitCode: null, stdout, stderr: `${stderr}${String(error)}`, timedOut });
});
child.on('close', (exitCode) => {
clearTimeout(timer);
resolve({ exitCode, stdout, stderr, timedOut });
});
});
}
function cleanEnvironment(env: DeliveryToolEnvironment): NodeJS.ProcessEnv {
const clean: NodeJS.ProcessEnv = {
GIT_TERMINAL_PROMPT: '0',
};
for (const key of [
'HOME',
'PATH',
'LANG',
'LC_ALL',
'MOSAIC_GIT_IDENTITY',
'MOSAIC_AGENT_NAME',
'MOSAIC_BRAIN_HOME',
'MOSAIC_CREDENTIAL_SPOOL',
'MOSAIC_CREDENTIAL_LINEAGE_FENCE',
] as const) {
const value = env[key];
if (value !== undefined) clean[key] = value;
}
return clean;
}
function textResult(text: string): {
content: Array<{ type: 'text'; text: string }>;
details: undefined;
} {
return { content: [{ type: 'text', text }], details: undefined };
}
function describeFailure(label: string, result: ProcessResult): string {
if (result.timedOut) return `${label} timed out`;
const diagnostic = result.stderr.trim() || result.stdout.trim() || 'no diagnostic output';
return `${label} failed (exit ${result.exitCode ?? 'null'}): ${diagnostic}`;
}
function currentBranchPattern(issue: number): RegExp {
return new RegExp(`^(?:feat|fix|docs|test)/${issue}(?:[-/].+)$`, 'i');
}
export function createDeliveryTools(
sandboxDir: string,
sourceEnv: DeliveryToolEnvironment = process.env,
runner: ProcessRunner = runProcess,
): ToolDefinition[] {
if (sourceEnv.AGENT_DELIVERY_ENABLED !== 'true') return [];
const identity = sourceEnv.MOSAIC_GIT_IDENTITY ?? '';
const agentName = sourceEnv.MOSAIC_AGENT_NAME ?? '';
const toolsDir = sourceEnv.MOSAIC_GIT_TOOLS_DIR ?? '';
const baseBranch = sourceEnv.MOSAIC_INTEGRATION_TRUNK ?? 'next';
if (!SAFE_IDENTITY.test(identity) || identity !== agentName) {
throw new Error('Delivery tools require matching safe MOSAIC agent and git identities');
}
if (!path.isAbsolute(toolsDir)) {
throw new Error('Delivery tools require an absolute MOSAIC_GIT_TOOLS_DIR');
}
if (!SAFE_BRANCH.test(`feat/${baseBranch}`) || baseBranch.includes('/')) {
throw new Error('Delivery tools require a safe integration branch name');
}
const env = cleanEnvironment(sourceEnv);
const queueGuard = path.join(toolsDir, 'ci-queue-wait.sh');
const prCreate = path.join(toolsDir, 'pr-create.sh');
const run = (file: string, args: readonly string[], timeoutMs = PROCESS_TIMEOUT_MS) =>
runner(file, args, { cwd: sandboxDir, env, timeoutMs });
const readBranch = async (): Promise<{ branch?: string; error?: string }> => {
const result = await run('/usr/bin/git', ['branch', '--show-current'], 15_000);
if (result.exitCode !== 0) return { error: describeFailure('git branch', result) };
const branch = result.stdout.trim();
if (!SAFE_BRANCH.test(branch))
return { error: `Unsafe delivery branch: ${branch || '<empty>'}` };
if (branch === baseBranch || branch === 'main') {
return { error: `Refusing delivery from protected branch ${branch}` };
}
return { branch };
};
const publish: ToolDefinition = {
name: 'git_publish_branch',
label: 'Publish Git Branch',
description:
'Stage explicit files in the current sandbox branch, commit them as the dedicated dogfood identity, run the CI queue guard, and push the branch. No shell or raw provider API is used.',
parameters: Type.Object({
issue: Type.Integer({ minimum: 1, description: 'Tracking issue number' }),
paths: Type.Array(Type.String(), {
minItems: 1,
maxItems: 100,
description: 'Files to stage, relative to the sandbox root',
}),
commitMessage: Type.String({ minLength: 1, maxLength: 4000 }),
}),
async execute(_toolCallId, params) {
const { issue, paths, commitMessage } = params as {
issue: number;
paths: string[];
commitMessage: string;
};
const branchResult = await readBranch();
if (!branchResult.branch) return textResult(`Error: ${branchResult.error}`);
const branch = branchResult.branch;
if (!currentBranchPattern(issue).test(branch)) {
return textResult(`Error: branch ${branch} does not carry issue ${issue}`);
}
const relativePaths: string[] = [];
try {
const sandboxRoot = guardPath('.', sandboxDir);
for (const candidate of paths) {
const resolved = guardPath(candidate, sandboxDir);
const relative = path.relative(sandboxRoot, resolved);
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) {
throw new SandboxEscapeError(candidate, sandboxDir, resolved);
}
relativePaths.push(relative);
}
} catch (error) {
return textResult(`Error: ${error instanceof Error ? error.message : String(error)}`);
}
const add = await run('/usr/bin/git', ['add', '--', ...relativePaths], 30_000);
if (add.exitCode !== 0) return textResult(`Error: ${describeFailure('git add', add)}`);
const commit = await run(
'/usr/bin/git',
[
'-c',
`user.name=${identity}`,
'-c',
`user.email=${identity}@mosaic.invalid`,
'commit',
'-m',
commitMessage,
'--',
...relativePaths,
],
60_000,
);
if (commit.exitCode !== 0)
return textResult(`Error: ${describeFailure('git commit', commit)}`);
const queue = await run(queueGuard, ['--purpose', 'push', '-B', branch]);
if (queue.exitCode !== 0) {
return textResult(`Error: ${describeFailure('CI queue guard', queue)}`);
}
const push = await run(
'/usr/bin/git',
['push', '--set-upstream', 'origin', branch],
PROCESS_TIMEOUT_MS,
);
if (push.exitCode !== 0) return textResult(`Error: ${describeFailure('git push', push)}`);
return textResult(`Published branch ${branch} as ${identity}.`);
},
};
const openPr: ToolDefinition = {
name: 'git_open_pull_request',
label: 'Open Pull Request',
description:
'Open a pull request from the current sandbox branch through the Mosaic pr-create wrapper. The wrapper targets the configured integration branch and links the tracking issue.',
parameters: Type.Object({
issue: Type.Integer({ minimum: 1, description: 'Tracking issue number' }),
title: Type.String({ minLength: 1, maxLength: 240 }),
body: Type.String({ maxLength: 20_000 }),
}),
async execute(_toolCallId, params) {
const { issue, title, body } = params as { issue: number; title: string; body: string };
const branchResult = await readBranch();
if (!branchResult.branch) return textResult(`Error: ${branchResult.error}`);
const branch = branchResult.branch;
if (!currentBranchPattern(issue).test(branch)) {
return textResult(`Error: branch ${branch} does not carry issue ${issue}`);
}
const result = await run(prCreate, [
'-t',
title,
'-b',
body,
'-B',
baseBranch,
'-H',
branch,
'-i',
String(issue),
]);
if (result.exitCode !== 0) {
return textResult(`Error: ${describeFailure('pr-create wrapper', result)}`);
}
return textResult(result.stdout.trim() || `Pull request opened from ${branch}.`);
},
};
return [publish, openPr];
}
+2 -2
View File
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox';
import type { ToolDefinition } from '@mariozechner/pi-coding-agent';
import { readFile, writeFile, readdir, stat } from 'node:fs/promises';
import { guardPath, guardPathUnsafe, SandboxEscapeError } from './path-guard.js';
import { guardPath, guardWritePath, SandboxEscapeError } from './path-guard.js';
const MAX_READ_BYTES = 512 * 1024; // 512 KB read limit
const MAX_WRITE_BYTES = 1024 * 1024; // 1 MB write limit
@@ -92,7 +92,7 @@ export function createFileTools(baseDir: string): ToolDefinition[] {
};
let safePath: string;
try {
safePath = guardPathUnsafe(path, baseDir);
safePath = guardWritePath(path, baseDir);
} catch (err) {
if (err instanceof SandboxEscapeError) {
return {
+2 -1
View File
@@ -1,8 +1,9 @@
export { createBrainTools } from './brain-tools.js';
export { createCoordTools } from './coord-tools.js';
export { createDeliveryTools } from './delivery-tools.js';
export { createFileTools } from './file-tools.js';
export { createGitTools } from './git-tools.js';
export { createSearchTools } from './search-tools.js';
export { createShellTools } from './shell-tools.js';
export { createShellTools, createShellToolsIfEnabled } from './shell-tools.js';
export { createWebTools } from './web-tools.js';
export { createSkillTools } from './skill-tools.js';
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { guardPath, guardPathUnsafe, SandboxEscapeError } from './path-guard.js';
import { guardPath, guardPathUnsafe, guardWritePath, SandboxEscapeError } from './path-guard.js';
import path from 'node:path';
import os from 'node:os';
import fs from 'node:fs';
@@ -101,4 +101,55 @@ describe('guardPath', () => {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
});
it('rejects a symlink inside the sandbox that resolves outside it', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'path-guard-test-'));
const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'path-guard-outside-'));
try {
const target = path.join(outside, 'credential.token');
fs.writeFileSync(target, 'OUTSIDE_SYMLINK_SENTINEL');
fs.symlinkSync(target, path.join(tmpDir, 'credential.token'));
expect(() => guardPath('credential.token', tmpDir)).toThrow(SandboxEscapeError);
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
fs.rmSync(outside, { recursive: true, force: true });
}
});
});
describe('guardWritePath', () => {
it('allows a new file under an existing real sandbox directory', () => {
const sandbox = fs.mkdtempSync(path.join(os.tmpdir(), 'path-write-test-'));
try {
expect(guardWritePath('new.txt', sandbox)).toBe(path.join(sandbox, 'new.txt'));
} finally {
fs.rmSync(sandbox, { recursive: true, force: true });
}
});
it('rejects writes through a file symlink that resolves outside the sandbox', () => {
const sandbox = fs.mkdtempSync(path.join(os.tmpdir(), 'path-write-test-'));
const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'path-write-outside-'));
try {
const target = path.join(outside, 'credential.token');
fs.writeFileSync(target, 'OUTSIDE_WRITE_SENTINEL');
fs.symlinkSync(target, path.join(sandbox, 'credential.token'));
expect(() => guardWritePath('credential.token', sandbox)).toThrow(SandboxEscapeError);
} finally {
fs.rmSync(sandbox, { recursive: true, force: true });
fs.rmSync(outside, { recursive: true, force: true });
}
});
it('rejects new files under a directory symlink that leaves the sandbox', () => {
const sandbox = fs.mkdtempSync(path.join(os.tmpdir(), 'path-write-test-'));
const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'path-write-outside-'));
try {
fs.symlinkSync(outside, path.join(sandbox, 'outside'));
expect(() => guardWritePath('outside/new.txt', sandbox)).toThrow(SandboxEscapeError);
} finally {
fs.rmSync(sandbox, { recursive: true, force: true });
fs.rmSync(outside, { recursive: true, force: true });
}
});
});
+48 -32
View File
@@ -1,47 +1,63 @@
import path from 'node:path';
import fs from 'node:fs';
/**
* Resolves a user-provided path and verifies it is inside the allowed sandbox directory.
* Throws SandboxEscapeError if the resolved path is outside the sandbox.
*
* Uses realpathSync to resolve symlinks in the sandbox root. The user-supplied path
* is checked for containment AFTER lexical resolution but BEFORE resolving any symlinks
* within the user path — so symlink escape attempts are caught too.
*
* @param userPath - The path provided by the agent (may be relative or absolute)
* @param sandboxDir - The allowed root directory (already validated on session creation)
* @returns The resolved absolute path, guaranteed to be within sandboxDir
*/
export function guardPath(userPath: string, sandboxDir: string): string {
const resolved = path.resolve(sandboxDir, userPath);
const sandboxResolved = fs.realpathSync.native(sandboxDir);
function isContained(candidate: string, root: string): boolean {
return candidate === root || candidate.startsWith(root + path.sep);
}
// Normalize both paths to resolve any symlinks in the sandbox root itself.
// For the user path, we check containment BEFORE resolving symlinks in the path
// (so we catch symlink escape attempts too — the resolved path must still be under sandbox)
if (!resolved.startsWith(sandboxResolved + path.sep) && resolved !== sandboxResolved) {
function assertLexicalContainment(userPath: string, sandboxDir: string): string {
const resolved = path.resolve(sandboxDir, userPath);
const sandboxAbsolute = path.resolve(sandboxDir);
if (!isContained(resolved, sandboxAbsolute)) {
throw new SandboxEscapeError(userPath, sandboxDir, resolved);
}
return resolved;
}
/**
* Validates a path without resolving symlinks in the user-provided portion.
* Use for paths that may not exist yet (creates, writes).
*
* Performs a lexical containment check only using path.resolve.
* Resolve an existing path and verify both its lexical path and real symlink
* target remain inside the sandbox.
*/
export function guardPath(userPath: string, sandboxDir: string): string {
const resolved = assertLexicalContainment(userPath, sandboxDir);
const sandboxReal = fs.realpathSync.native(sandboxDir);
const resolvedReal = fs.realpathSync.native(resolved);
if (!isContained(resolvedReal, sandboxReal)) {
throw new SandboxEscapeError(userPath, sandboxDir, resolvedReal);
}
return resolvedReal;
}
/**
* Resolve a writable file path whose parent already exists. Existing targets
* are resolved fully. New targets use the real parent directory, which blocks
* writes through a parent symlink that leaves the sandbox.
*/
export function guardWritePath(userPath: string, sandboxDir: string): string {
const resolved = assertLexicalContainment(userPath, sandboxDir);
const sandboxReal = fs.realpathSync.native(sandboxDir);
let writableReal: string;
try {
writableReal = fs.realpathSync.native(resolved);
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (code !== 'ENOENT') throw error;
const parentReal = fs.realpathSync.native(path.dirname(resolved));
writableReal = path.join(parentReal, path.basename(resolved));
}
if (!isContained(writableReal, sandboxReal)) {
throw new SandboxEscapeError(userPath, sandboxDir, writableReal);
}
return writableReal;
}
/**
* Lexical-only validation for non-filesystem pathspecs such as `git diff --`
* targets, where the path may name a deleted file and Git does not dereference
* a tracked symlink.
*/
export function guardPathUnsafe(userPath: string, sandboxDir: string): string {
const resolved = path.resolve(sandboxDir, userPath);
const sandboxAbs = path.resolve(sandboxDir);
if (!resolved.startsWith(sandboxAbs + path.sep) && resolved !== sandboxAbs) {
throw new SandboxEscapeError(userPath, sandboxDir, resolved);
}
return resolved;
return assertLexicalContainment(userPath, sandboxDir);
}
export class SandboxEscapeError extends Error {
@@ -128,6 +128,14 @@ function runCommand(
});
}
export function createShellToolsIfEnabled(
sandboxDir: string | undefined,
env: NodeJS.ProcessEnv = process.env,
): ToolDefinition[] {
if (env['AGENT_SHELL_ENABLED'] === 'false') return [];
return createShellTools(sandboxDir);
}
export function createShellTools(sandboxDir?: string): ToolDefinition[] {
const defaultCwd = sandboxDir ?? process.cwd();
+2
View File
@@ -25,6 +25,7 @@ import { HarnessModule } from './harness/harness.module.js';
import { ReloadModule } from './reload/reload.module.js';
import { WorkspaceModule } from './workspace/workspace.module.js';
import { HierarchyModule } from './hierarchy/hierarchy.module.js';
import { EnrollmentModule } from './enrollment/enrollment.module.js';
import { QueueModule } from './queue/queue.module.js';
import { FederationModule } from './federation/federation.module.js';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
@@ -67,6 +68,7 @@ const federationEnabled = loadConfig(resolveGatewayConfigPath()).tier === 'feder
ReloadModule,
WorkspaceModule,
HierarchyModule,
EnrollmentModule,
...(federationEnabled ? [FederationModule] : []),
],
controllers: [HealthController],
@@ -0,0 +1,508 @@
import { mkdtemp, rm } from 'node:fs/promises';
import { randomUUID } from 'node:crypto';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
import { Test, type TestingModule } from '@nestjs/testing';
import { Logger, ValidationPipe, type ExecutionContext } from '@nestjs/common';
import { FastifyAdapter, type NestFastifyApplication } from '@nestjs/platform-fastify';
import supertest from 'supertest';
import { unseal } from '@mosaicstack/auth';
import {
agentAuditEvents,
agentIdempotencyFence,
agentOutbox,
agents,
and,
createPgliteDb,
eq,
providerCredentials,
runPgliteMigrations,
sql,
users,
type DbHandle,
} from '@mosaicstack/db';
import { DB } from '../database/database.module.js';
import { AuthGuard } from '../auth/auth.guard.js';
import { HarnessRegistry } from '../harness/harness.registry.js';
import { HARNESS_REGISTRY } from '../harness/harness.tokens.js';
import { FakeHarnessAdapter } from '../harness/testing/fake-harness.adapter.js';
import { EnrollmentController } from './enrollment.controller.js';
import {
EnrollmentRepository,
type EnrollAgentInput,
type EnrollmentResult,
type EnrolledAgentView,
} from './enrollment.repository.js';
import { EnrollmentService } from './enrollment.service.js';
/**
* Command-level witnesses for the agent enrollment family (M4-4b) — design
* docs/plans/2026-08-29-agent-enrollment-command-design.md §5 items 19 and
* 11 (item 10, CLI parity, lives in packages/mosaic). Schema-level
* constraints are witnessed in packages/db/src/agent-enrollment.witness.test.ts.
*
* The suite runs the REAL repository/service/controller graph over PGlite,
* with only AuthGuard overridden (a session store is out of scope; the
* override binds request.user exactly as the real guard does). The §6.3
* static companions — no `any`-typed boundary pass-through, a single audit
* emitter (EnrollmentRepository.appendEvent) — are code-surface properties
* reviewed on the PR, not runtime probes.
*/
describe('enrollment commands integration', (): void => {
let dataDir: string;
let handle: DbHandle;
let moduleRef: TestingModule;
let app: NestFastifyApplication;
let http: ReturnType<typeof supertest>;
let repo: EnrollmentRepository;
let previousAuthSecret: string | undefined;
const OWNER = 'enr-owner';
const ADMIN = 'enr-admin';
const STRANGER = 'enr-stranger';
const HARNESS = 'fake-harness';
/** Never-echo probe value (§5.1). Unique enough that any leak is unambiguous. */
const SECRET = `enr-secret-value-${randomUUID()}`;
/** The HTTP-leg acting user; the overridden guard binds it per request. */
let currentUserId = OWNER;
const enrollInput = (overrides: Partial<EnrollAgentInput> = {}): EnrollAgentInput => ({
actorId: OWNER,
harness: HARNESS,
name: `Agent ${randomUUID().slice(0, 8)}`,
persona: null,
model: 'anthropic/claude-test',
provider: `prov-${randomUUID().slice(0, 8)}`,
credential: { mode: 'intake', type: 'api_key', value: SECRET },
idempotencyKey: randomUUID(),
...overrides,
});
function expectOk<T>(result: EnrollmentResult<T>): { ok: true; correlationId: string } & T {
if (!result.ok) throw new Error(`expected ok, got ${JSON.stringify(result)}`);
return result;
}
function expectFail<T>(
result: EnrollmentResult<T>,
error: string,
): { ok: false; error: string; message: string; correlationId: string } {
if (result.ok) throw new Error(`expected ${error}, got ok`);
expect(result.error).toBe(error);
return result;
}
const fenceForKey = (key: string) =>
handle.db
.select()
.from(agentIdempotencyFence)
.where(eq(agentIdempotencyFence.idempotencyKey, key));
const eventsForAgent = (agentId: string) =>
handle.db.select().from(agentAuditEvents).where(eq(agentAuditEvents.agentId, agentId));
const agentsNamed = (name: string) =>
handle.db.select().from(agents).where(eq(agents.name, name));
const credentialsFor = (userId: string, provider: string) =>
handle.db
.select()
.from(providerCredentials)
.where(
and(eq(providerCredentials.userId, userId), eq(providerCredentials.provider, provider)),
);
const allOutbox = () => handle.db.select().from(agentOutbox);
beforeAll(async (): Promise<void> => {
previousAuthSecret = process.env['BETTER_AUTH_SECRET'];
process.env['BETTER_AUTH_SECRET'] = 'enrollment-witness-sealing-key';
dataDir = await mkdtemp(join(tmpdir(), 'mosaic-gateway-enrollment-commands-'));
handle = createPgliteDb(dataDir);
await runPgliteMigrations(handle);
const registry = new HarnessRegistry();
registry.register(new FakeHarnessAdapter({ id: HARNESS }));
moduleRef = await Test.createTestingModule({
controllers: [EnrollmentController],
providers: [
EnrollmentRepository,
EnrollmentService,
{ provide: DB, useValue: handle.db },
{ provide: HARNESS_REGISTRY, useValue: registry },
],
})
.overrideGuard(AuthGuard)
.useValue({
canActivate: (ctx: ExecutionContext): boolean => {
const request = ctx.switchToHttp().getRequest<{ user?: unknown }>();
request.user = { id: currentUserId };
return true;
},
})
.compile();
app = moduleRef.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
// Mirror main.ts exactly — the closure witnesses depend on these options.
app.useGlobalPipes(
new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }),
);
await app.init();
await app.getHttpAdapter().getInstance().ready();
http = supertest(app.getHttpServer());
repo = moduleRef.get(EnrollmentRepository);
await handle.db.insert(users).values([
{ id: OWNER, name: 'Owner', email: `${OWNER}@example.com` },
{ id: ADMIN, name: 'Admin', email: `${ADMIN}@example.com`, role: 'admin' },
{ id: STRANGER, name: 'Stranger', email: `${STRANGER}@example.com` },
]);
});
afterAll(async (): Promise<void> => {
await app?.close();
await handle.close();
await rm(dataDir, { recursive: true, force: true });
if (previousAuthSecret === undefined) delete process.env['BETTER_AUTH_SECRET'];
else process.env['BETTER_AUTH_SECRET'] = previousAuthSecret;
});
// ── §5.7 wizard-facing zero-mutation (runs FIRST: no call → zero rows) ────
it('zero-mutation: with no enrollment invocation the family tables hold zero rows', async () => {
expect(await handle.db.select().from(agents)).toHaveLength(0);
expect(await handle.db.select().from(agentAuditEvents)).toHaveLength(0);
expect(await handle.db.select().from(agentOutbox)).toHaveLength(0);
expect(await handle.db.select().from(agentIdempotencyFence)).toHaveLength(0);
});
// ── §5.1 never-echo + §5.2 sealed single-copy ─────────────────────────────
it('never echoes the intake credential value: HTTP result, audit, outbox, fence, and logs are clean', async () => {
const logSink: string[] = [];
const logSpies = (['log', 'error', 'warn', 'debug', 'verbose'] as const).map((method) =>
vi.spyOn(Logger.prototype, method).mockImplementation((...args: unknown[]) => {
logSink.push(args.map(String).join(' '));
}),
);
try {
currentUserId = OWNER;
const provider = `prov-echo-${randomUUID().slice(0, 8)}`;
const res = await http.post('/api/enrollment/agents').send({
harness: HARNESS,
name: 'Echo Probe',
persona: 'a persona',
model: 'anthropic/claude-test',
provider,
credential: { mode: 'intake', type: 'api_key', value: SECRET },
idempotencyKey: randomUUID(),
});
expect(res.status).toBe(201);
expect(res.text).not.toContain(SECRET);
const agentId = (res.body as { agent: EnrolledAgentView }).agent.id;
const events = await eventsForAgent(agentId);
expect(events).toHaveLength(1);
expect(JSON.stringify(events)).not.toContain(SECRET);
expect(JSON.stringify(await allOutbox())).not.toContain(SECRET);
const fences = await handle.db
.select()
.from(agentIdempotencyFence)
.where(eq(agentIdempotencyFence.outcomeAgentId, agentId));
expect(fences).toHaveLength(1);
expect(JSON.stringify(fences)).not.toContain(SECRET);
expect(logSink.join('\n')).not.toContain(SECRET);
// §5.2 sealed single-copy: exactly one provider_credentials row, sealed
// at rest, and it round-trips through unseal — no plaintext column.
const creds = await credentialsFor(OWNER, provider);
expect(creds).toHaveLength(1);
expect(creds[0]?.encryptedValue).not.toBe(SECRET);
expect(creds[0]?.encryptedValue).not.toContain(SECRET);
expect(unseal(creds[0]?.encryptedValue as string)).toBe(SECRET);
} finally {
logSpies.forEach((spy) => spy.mockRestore());
}
});
it('the agents table itself has no credential-bearing column (§5.2)', async () => {
const result = (await handle.db.execute(
sql`select column_name from information_schema.columns where table_name = 'agents'`,
)) as unknown as { rows?: Array<{ column_name: string }> } & Array<{ column_name: string }>;
const names = (result.rows ?? result).map((row) => row.column_name);
expect(names.length).toBeGreaterThan(0);
for (const name of names) {
expect(name).not.toMatch(/credential|secret|token|api_key/i);
}
});
// ── §5.3 reference resolution ─────────────────────────────────────────────
it('refuses an unresolvable credential reference with precondition_failed and creates nothing', async () => {
const input = enrollInput({ credential: { mode: 'reference' } });
const result = await repo.enroll(input);
expectFail(result, 'precondition_failed');
expect(await agentsNamed(input.name)).toHaveLength(0);
expect(await fenceForKey(input.idempotencyKey)).toHaveLength(0);
});
it('resolves a reference credential stored earlier for (actor, provider)', async () => {
const provider = `prov-ref-${randomUUID().slice(0, 8)}`;
const seeded = expectOk(await repo.enroll(enrollInput({ provider })));
const result = expectOk(
await repo.enroll(enrollInput({ provider, credential: { mode: 'reference' } })),
);
expect(result.agent.id).not.toBe(seeded.agent.id);
expect(await credentialsFor(OWNER, provider)).toHaveLength(1);
});
// ── §5.4 harness refusals, both codes ────────────────────────────────────
it('refuses a syntactically invalid harness as validation_failed and a registry miss as precondition_failed', async () => {
const blank = await repo.enroll(enrollInput({ harness: ' ' }));
expectFail(blank, 'validation_failed');
const miss = await repo.enroll(enrollInput({ harness: 'well-formed-but-unregistered' }));
expectFail(miss, 'precondition_failed');
currentUserId = OWNER;
const httpBlank = await http.post('/api/enrollment/agents').send({
harness: '',
name: 'H',
model: 'm',
provider: 'p',
credential: { mode: 'reference' },
idempotencyKey: randomUUID(),
});
expect(httpBlank.status).toBe(400);
});
// ── §5.5 idempotency set (contract 3 §4.3) ───────────────────────────────
it('actor-bound replay returns the recorded outcome and executes nothing new', async () => {
const input = enrollInput();
const first = expectOk(await repo.enroll(input));
const replay = expectOk(await repo.enroll({ ...input, correlationId: randomUUID() }));
expect(replay.agent.id).toBe(first.agent.id);
expect(await agentsNamed(input.name)).toHaveLength(1);
expect(await fenceForKey(input.idempotencyKey)).toHaveLength(1);
const events = await eventsForAgent(first.agent.id);
expect(events.filter((e) => e.eventType === 'agent.enrolled')).toHaveLength(1);
// A passing replay appends exactly the non-mutation access event.
const replayed = events.filter((e) => e.eventType === 'agent.enrollment.replayed');
expect(replayed).toHaveLength(1);
expect((replayed[0]?.payload as { fenceId?: string }).fenceId).toBeDefined();
});
it('payload-digest mismatch on a recorded key refuses with the single bounded conflict shape', async () => {
const input = enrollInput();
expectOk(await repo.enroll(input));
const mismatch = await repo.enroll({ ...input, name: `${input.name} CHANGED` });
const failure = expectFail(mismatch, 'conflict');
expect(failure.message).toBe('idempotency conflict');
});
it('replay-mode and scope mismatches on the recorded fence each refuse as the same constant conflict', async () => {
const modeInput = enrollInput();
expectOk(await repo.enroll(modeInput));
await handle.db
.update(agentIdempotencyFence)
.set({ replayMode: 'shared' })
.where(eq(agentIdempotencyFence.idempotencyKey, modeInput.idempotencyKey));
const modeFailure = expectFail(await repo.enroll(modeInput), 'conflict');
const scopeInput = enrollInput();
expectOk(await repo.enroll(scopeInput));
await handle.db
.update(agentIdempotencyFence)
.set({ authorizationScope: 'some-other-scope' })
.where(eq(agentIdempotencyFence.idempotencyKey, scopeInput.idempotencyKey));
const scopeFailure = expectFail(await repo.enroll(scopeInput), 'conflict');
expect(modeFailure.message).toBe(scopeFailure.message);
});
it('a different actor replaying an actor-bound key is refused conflict, learning nothing', async () => {
const input = enrollInput();
expectOk(await repo.enroll(input));
const failure = expectFail(await repo.enroll({ ...input, actorId: STRANGER }), 'conflict');
expect(failure.message).toBe('idempotency conflict');
});
it('a replay is re-authorized fresh: revoked target authority refuses instead of replaying', async () => {
const input = enrollInput();
const first = expectOk(await repo.enroll(input));
// Simulate the legacy CRUD DELETE path removing the outcome agent: the
// submitter no longer holds read authority on the referenced row.
await handle.db.delete(agents).where(eq(agents.id, first.agent.id));
expectFail(await repo.enroll(input), 'conflict');
});
it('a shared replay-mode declaration is refused validation_failed with nothing executed and no fence row', async () => {
const input = enrollInput({ replayMode: 'shared' });
expectFail(await repo.enroll(input), 'validation_failed');
expect(await agentsNamed(input.name)).toHaveLength(0);
expect(await fenceForKey(input.idempotencyKey)).toHaveLength(0);
currentUserId = OWNER;
const key = randomUUID();
const res = await http.post('/api/enrollment/agents').send({
harness: HARNESS,
name: 'Shared Probe',
model: 'm',
provider: 'p',
credential: { mode: 'reference' },
idempotencyKey: key,
replayMode: 'shared',
});
expect(res.status).toBe(400);
expect(await fenceForKey(key)).toHaveLength(0);
});
it('two concurrent same-key submissions produce exactly one mutation, the loser resolving as a replay', async () => {
const input = enrollInput();
const [a, b] = await Promise.all([
repo.enroll(input),
repo.enroll({ ...input, correlationId: randomUUID() }),
]);
const okA = expectOk(a);
const okB = expectOk(b);
expect(okA.agent.id).toBe(okB.agent.id);
expect(await agentsNamed(input.name)).toHaveLength(1);
expect(await fenceForKey(input.idempotencyKey)).toHaveLength(1);
const events = await eventsForAgent(okA.agent.id);
expect(events.filter((e) => e.eventType === 'agent.enrolled')).toHaveLength(1);
expect(events.filter((e) => e.eventType === 'agent.enrollment.replayed')).toHaveLength(1);
});
// ── §5.6 same-tx atomicity fault injection ───────────────────────────────
it('rolls everything back on failure at each write point — no orphan credential survives', async () => {
const injectionPoints = [
'writeSealedCredential',
'insertAgentRow',
'insertFenceRow',
'appendEvent',
'insertOutboxRow',
] as const;
for (const point of injectionPoints) {
const input = enrollInput();
const spy = vi.spyOn(repo, point).mockImplementationOnce(() => {
throw new Error(`injected ${point} fault`);
});
try {
const result = await repo.enroll(input);
expectFail(result, 'internal_fault');
expect(await agentsNamed(input.name)).toHaveLength(0);
expect(await fenceForKey(input.idempotencyKey)).toHaveLength(0);
// Injection at fence/audit/outbox fires AFTER the sealed credential
// write's statement ran — the rollback must leave no orphan row.
expect(await credentialsFor(OWNER, input.provider)).toHaveLength(0);
} finally {
spy.mockRestore();
}
}
});
// ── §5.8 is_system closure ───────────────────────────────────────────────
it('rejects an is_system injection attempt at the DTO boundary', async () => {
currentUserId = OWNER;
const key = randomUUID();
const res = await http.post('/api/enrollment/agents').send({
harness: HARNESS,
name: 'System Probe',
model: 'm',
provider: 'p',
credential: { mode: 'reference' },
idempotencyKey: key,
isSystem: true,
});
expect(res.status).toBe(400);
expect(await fenceForKey(key)).toHaveLength(0);
});
// ── §5.9 correlation + no-existence-oracle ───────────────────────────────
it('carries a submitted correlation id into the result, the audit event, and the outbox record', async () => {
const correlationId = randomUUID();
const input = enrollInput({ correlationId });
const result = expectOk(await repo.enroll(input));
expect(result.correlationId).toBe(correlationId);
const events = await eventsForAgent(result.agent.id);
expect(events).toHaveLength(1);
expect(events[0]?.correlationId).toBe(correlationId);
const outboxRows = await handle.db
.select()
.from(agentOutbox)
.where(eq(agentOutbox.eventId, events[0]?.id as string));
expect(outboxRows).toHaveLength(1);
expect(outboxRows[0]?.correlationId).toBe(correlationId);
// Refusals carry the correlation envelope too (contract 5 §4.3).
const refusal = expectFail(
await repo.enroll({ ...input, name: 'changed name', correlationId }),
'conflict',
);
expect(refusal.correlationId).toBe(correlationId);
});
it('agent.enrollment.get returns owner and admin reads with the correlation envelope, no idempotency key', async () => {
const enrolled = expectOk(await repo.enroll(enrollInput()));
const correlationId = randomUUID();
const asOwner = expectOk(await repo.getEnrollment(OWNER, enrolled.agent.id, correlationId));
expect(asOwner.correlationId).toBe(correlationId);
expect(asOwner.agent.id).toBe(enrolled.agent.id);
const asAdmin = expectOk(await repo.getEnrollment(ADMIN, enrolled.agent.id));
expect(asAdmin.correlationId).toMatch(/^[0-9a-f-]{36}$/);
currentUserId = OWNER;
const wire = randomUUID();
const res = await http.get(`/api/enrollment/agents/${enrolled.agent.id}?correlationId=${wire}`);
expect(res.status).toBe(200);
expect((res.body as { correlationId: string }).correlationId).toBe(wire);
});
it('no existence oracle: unauthorized get of a real agent and get of a missing id are indistinguishable', async () => {
const enrolled = expectOk(await repo.enroll(enrollInput()));
currentUserId = STRANGER;
const unauthorized = await http.get(`/api/enrollment/agents/${enrolled.agent.id}`);
const missing = await http.get(`/api/enrollment/agents/${randomUUID()}`);
expect(unauthorized.status).toBe(404);
expect(missing.status).toBe(404);
const strip = (body: Record<string, unknown>): Record<string, unknown> =>
Object.fromEntries(Object.entries(body).filter(([key]) => key !== 'correlationId'));
expect(strip(unauthorized.body as Record<string, unknown>)).toEqual(
strip(missing.body as Record<string, unknown>),
);
});
// ── §5.11 fail-closed ────────────────────────────────────────────────────
it('fails closed as internal_fault when the store is unreachable, with no fallback write', async () => {
const before = (await handle.db.select().from(agents)).length;
const txSpy = vi.spyOn(handle.db, 'transaction').mockImplementationOnce(() => {
throw new Error('injected store outage');
});
try {
expectFail(await repo.enroll(enrollInput()), 'internal_fault');
} finally {
txSpy.mockRestore();
}
const selectSpy = vi.spyOn(handle.db, 'select').mockImplementationOnce(() => {
throw new Error('injected store outage');
});
try {
expectFail(await repo.getEnrollment(OWNER, randomUUID()), 'internal_fault');
} finally {
selectSpy.mockRestore();
}
expect((await handle.db.select().from(agents)).length).toBe(before);
});
});
@@ -0,0 +1,61 @@
import {
Body,
Controller,
Get,
Param,
ParseUUIDPipe,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '../auth/auth.guard.js';
import { CurrentUser } from '../auth/current-user.decorator.js';
import { EnrollAgentDto, GetEnrollmentQueryDto } from './enrollment.dto.js';
import { EnrollmentRepository } from './enrollment.repository.js';
import { EnrollmentService } from './enrollment.service.js';
/**
* The agent enrollment command family's closed HTTP surface (design
* docs/plans/2026-08-29-agent-enrollment-command-design.md §3): one command,
* one query. Authentication failures are the guard's (401); everything else
* is the repository's closed enum mapped by EnrollmentService.
*/
@Controller('api/enrollment')
@UseGuards(AuthGuard)
export class EnrollmentController {
constructor(
private readonly repository: EnrollmentRepository,
private readonly service: EnrollmentService,
) {}
/** agent.enroll (§3.1). */
@Post('agents')
async enroll(@CurrentUser() user: { id: string }, @Body() dto: EnrollAgentDto) {
return this.service.unwrap(
await this.repository.enroll({
actorId: user.id,
harness: dto.harness,
name: dto.name,
persona: dto.persona ?? null,
model: dto.model,
provider: dto.provider,
credential: dto.credential,
idempotencyKey: dto.idempotencyKey,
correlationId: dto.correlationId,
replayMode: dto.replayMode,
}),
);
}
/** agent.enrollment.get (§3.2): owner-or-admin; unauthorized and missing fold to one not_found. */
@Get('agents/:id')
async getEnrollment(
@CurrentUser() user: { id: string },
@Param('id', ParseUUIDPipe) id: string,
@Query() query: GetEnrollmentQueryDto,
) {
return this.service.unwrap(
await this.repository.getEnrollment(user.id, id, query.correlationId),
);
}
}
@@ -0,0 +1,107 @@
import { Type } from 'class-transformer';
import {
IsIn,
IsOptional,
IsString,
IsUUID,
MaxLength,
MinLength,
ValidateIf,
ValidateNested,
} from 'class-validator';
/**
* Agent enrollment command DTOs (design
* docs/plans/2026-08-29-agent-enrollment-command-design.md §3.1/§3.2,
* contract 5 §4.1 typed boundary).
*
* The global ValidationPipe runs with whitelist + forbidNonWhitelisted, so
* closure is contract surface here exactly as in the hierarchy DTOs:
* - EnrollAgentDto declares NO isSystem field — `is_system` is never
* settable through this command (design §3.1 rule 4); the pipe refuses it.
* - replayMode admits ONLY 'actor-bound': `shared` is seed-only (contract 3
* §4.3), so a shared declaration is refused `validation_failed` at the
* boundary, executes nothing, and records no fence row (design §3.1).
* Every class here must be registered in PIPE_GUARDED_DTOS so the boot-time
* assertion proves the pipe sees the decorators.
*/
/**
* Credential input, discriminated on `mode` (design §3.1):
* - `{ mode: 'reference' }` — a stored credential for (actor, provider)
* must already exist; `type`/`value` must be ABSENT (the repository
* refuses a reference that smuggles a value).
* - `{ mode: 'intake', type: 'api_key', value }` — the value is sealed
* into the credential store inside the enrollment transaction and is
* never echoed anywhere (§3.1 rule 1).
*/
export class EnrollCredentialDto {
@IsIn(['reference', 'intake'])
mode!: 'reference' | 'intake';
@ValidateIf((o: EnrollCredentialDto) => o.mode === 'intake')
@IsIn(['api_key'])
type?: 'api_key';
@ValidateIf((o: EnrollCredentialDto) => o.mode === 'intake')
@IsString()
@MinLength(1)
@MaxLength(4096)
value?: string;
}
export class EnrollAgentDto {
/** Registered harness name; a well-formed name missing from the registry is `precondition_failed`. */
@IsString()
@MinLength(1)
@MaxLength(200)
harness!: string;
@IsString()
@MinLength(1)
@MaxLength(200)
name!: string;
/** Stored as the agent's system prompt; null/absent leaves it unset. */
@IsOptional()
@IsString()
@MaxLength(20000)
persona?: string | null;
/** Provider-qualified model id. */
@IsString()
@MinLength(1)
@MaxLength(200)
model!: string;
/** Names the credential's provider. */
@IsString()
@MinLength(1)
@MaxLength(200)
provider!: string;
@ValidateNested()
@Type(() => EnrollCredentialDto)
credential!: EnrollCredentialDto;
/** REQUIRED — contract 3 §4.3, ratified into contract 5 §4 via §7 item 4. */
@IsUUID()
idempotencyKey!: string;
/** Optional; generated when absent (contract 5 §4.3). */
@IsOptional()
@IsUUID()
correlationId?: string;
/** Only 'actor-bound' is admissible on this family — see module doc. */
@IsOptional()
@IsIn(['actor-bound'])
replayMode?: 'actor-bound';
}
/** Query envelope for agent.enrollment.get (design §3.2): correlation only, no idempotency key. */
export class GetEnrollmentQueryDto {
@IsOptional()
@IsUUID()
correlationId?: string;
}
@@ -0,0 +1,22 @@
import { Module } from '@nestjs/common';
import { HarnessModule } from '../harness/harness.module.js';
import { EnrollmentController } from './enrollment.controller.js';
import { EnrollmentRepository } from './enrollment.repository.js';
import { EnrollmentService } from './enrollment.service.js';
/**
* Agent enrollment command family (M4-4b; design
* docs/plans/2026-08-29-agent-enrollment-command-design.md). Imports
* HarnessModule for the live harness registry — the validation source for
* the `harness` field (a well-formed name the registry does not know is a
* precondition failure). EnrollmentRepository is the family's sole writer;
* every mutation runs fence-check → mutate → audit + outbox in one
* transaction.
*/
@Module({
imports: [HarnessModule],
controllers: [EnrollmentController],
providers: [EnrollmentRepository, EnrollmentService],
exports: [EnrollmentRepository],
})
export class EnrollmentModule {}
@@ -0,0 +1,538 @@
import { createHash, randomUUID } from 'node:crypto';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { seal } from '@mosaicstack/auth';
import {
agentAuditEvents,
agentIdempotencyFence,
agentOutbox,
agents,
and,
eq,
providerCredentials,
users,
type Db,
} from '@mosaicstack/db';
import { DB } from '../database/database.module.js';
import type { HarnessRegistry } from '../harness/harness.registry.js';
import { HARNESS_REGISTRY } from '../harness/harness.tokens.js';
/**
* Agent enrollment command repository (design
* docs/plans/2026-08-29-agent-enrollment-command-design.md §3; contract 5 §4
* envelope; contract 3 §4.3 idempotency fence, ratified via §7 item 4).
*
* The ONLY writer of the enrollment family's tables (`agent_audit_events`,
* `agent_outbox`, `agent_idempotency_fence`) and the only path that sets
* `agents.harness`/`agents.enrolled_at`. Every enroll runs one transaction:
* fence check → (replay | credential handling → agent insert → fence insert →
* audit event + outbox), so state, fence, event, and outbox commit or roll
* back together (§3.1 rule 6).
*
* Authorization (v1, §3.1 rule 4) is the AuthGuard-authenticated actor — no
* hierarchy grant is consulted because v1 enrollment binds no hierarchy node.
* The recorded fence authorization scope is therefore the constant
* platform-user identity domain (§3.1 rule 5).
*
* Never-echo (§3.1 rule 1): the credential value reaches exactly one sink —
* the sealed store write — and appears in no result, audit payload, outbox
* row, or log line. Log lines here carry correlation ids and error names
* only, never request fields.
*
* The single-write helper methods (writeSealedCredential, insertAgentRow,
* insertFenceRow, appendEvent, insertOutboxRow) are ordinary decomposition;
* the atomicity witnesses (§5.6) spy on them to inject faults at each write
* point without any test-only production switch.
*/
export const ENROLLMENT_OPERATION = 'agent.enroll';
/** §3.1 rule 5: v1 authorization is grant-free, so the scope is the authenticated-user identity domain. */
const AUTHORIZATION_SCOPE = 'platform-user';
/** The single bounded collision shape (§3.1 rule 5): constant, identifying no record. */
const CONFLICT_MESSAGE = 'idempotency conflict';
/** One fixed message for every not_found cause — missing and unauthorized are indistinguishable (§3.2). */
const NOT_FOUND_MESSAGE = 'agent not found';
/** Closed per-family error enum (§3.3). 401 is produced by AuthGuard; 403 folds to not_found (§3.2). */
export type EnrollmentErrorCode =
| 'validation_failed'
| 'authentication_failed'
| 'authorization_refused'
| 'not_found'
| 'conflict'
| 'precondition_failed'
| 'internal_fault';
export interface EnrollmentFailure {
readonly ok: false;
readonly error: EnrollmentErrorCode;
readonly message: string;
/** Refusals carry the correlation id too (contract 5 §4.3 end-to-end traceability). */
readonly correlationId: string;
}
export type EnrollmentResult<T> =
| ({ readonly ok: true; readonly correlationId: string } & T)
| EnrollmentFailure;
/** The persisted agent row; the table stores no credential material (§3.1 rule 1). */
export interface EnrolledAgentView {
readonly id: string;
readonly name: string;
readonly provider: string;
readonly model: string;
readonly status: string;
readonly harness: string | null;
readonly persona: string | null;
readonly ownerId: string | null;
readonly enrolledAt: string | null;
readonly createdAt: string;
}
export interface EnrollCredentialInput {
readonly mode: 'reference' | 'intake';
readonly type?: 'api_key';
readonly value?: string;
}
export interface EnrollAgentInput {
readonly actorId: string;
readonly harness: string;
readonly name: string;
readonly persona?: string | null;
readonly model: string;
readonly provider: string;
readonly credential: EnrollCredentialInput;
readonly idempotencyKey: string;
readonly correlationId?: string;
/** Defense in depth below the DTO: anything but 'actor-bound' is refused (seed-only rule). */
readonly replayMode?: string;
}
type Tx = Pick<Db, 'insert' | 'select' | 'update' | 'delete'>;
type AgentRow = typeof agents.$inferSelect;
type FenceRow = typeof agentIdempotencyFence.$inferSelect;
/** Raised inside the transaction when the fence insert lost a same-key race (§3.1 rule 5 concurrency). */
class ConcurrentEnrollmentError extends Error {
constructor() {
super('concurrent enrollment lost the fence race');
this.name = 'ConcurrentEnrollmentError';
}
}
function agentView(row: AgentRow): EnrolledAgentView {
return {
id: row.id,
name: row.name,
provider: row.provider,
model: row.model,
status: row.status,
harness: row.harness,
persona: row.systemPrompt,
ownerId: row.ownerId,
enrolledAt: row.enrolledAt?.toISOString() ?? null,
createdAt: row.createdAt.toISOString(),
};
}
/** Key-order-independent serialization (jsonb precedent in hierarchy-audit). */
function canonicalJson(value: unknown): string {
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(',')}]`;
if (value !== null && typeof value === 'object') {
const record = value as Record<string, unknown>;
const body = Object.keys(record)
.sort()
.map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`)
.join(',');
return `{${body}}`;
}
return JSON.stringify(value);
}
interface NormalizedEnrollment {
readonly actorId: string;
readonly harness: string;
readonly name: string;
readonly persona: string | null;
readonly model: string;
readonly provider: string;
readonly credential: EnrollCredentialInput;
readonly idempotencyKey: string;
readonly correlationId: string;
readonly digest: string;
}
/**
* Canonicalized-payload digest (§3.1 rule 5). The input EXCLUDES the
* credential value by construction: it covers mode and declared type only —
* plaintext never reaches the hash.
*/
function digestOf(
input: Omit<NormalizedEnrollment, 'actorId' | 'idempotencyKey' | 'correlationId' | 'digest'>,
): string {
const canonical = canonicalJson({
harness: input.harness,
name: input.name,
persona: input.persona,
model: input.model,
provider: input.provider,
credential: { mode: input.credential.mode, type: input.credential.type ?? null },
});
return createHash('sha256').update(canonical).digest('hex');
}
@Injectable()
export class EnrollmentRepository {
private readonly logger = new Logger(EnrollmentRepository.name);
constructor(
@Inject(DB) private readonly db: Db,
@Inject(HARNESS_REGISTRY) private readonly registry: HarnessRegistry,
) {}
async enroll(input: EnrollAgentInput): Promise<EnrollmentResult<{ agent: EnrolledAgentView }>> {
const correlationId = input.correlationId ?? randomUUID();
const fail = (error: EnrollmentErrorCode, message: string): EnrollmentFailure => ({
ok: false,
error,
message,
correlationId,
});
const harness = input.harness.trim();
const name = input.name.trim();
if (harness.length === 0) return fail('validation_failed', 'harness must be non-empty');
if (name.length === 0 || name.length > 200) {
return fail('validation_failed', 'name must be non-empty and at most 200 characters');
}
if (input.replayMode !== undefined && input.replayMode !== 'actor-bound') {
// Seed-only rule (contract 3 §4.3): refused with nothing executed and no fence row.
return fail('validation_failed', 'replayMode must be actor-bound');
}
if (input.credential.mode === 'reference') {
if (input.credential.type !== undefined || input.credential.value !== undefined) {
return fail('validation_failed', 'a reference credential carries no type or value');
}
} else if (
input.credential.type !== 'api_key' ||
typeof input.credential.value !== 'string' ||
input.credential.value.length === 0
) {
return fail('validation_failed', 'an intake credential requires type api_key and a value');
}
// Syntactic validity ends above; a well-formed name the live registry
// does not know is a precondition failure (§3.1 table).
if (!this.registry.has(harness)) {
return fail('precondition_failed', 'harness is not registered');
}
const normalized: NormalizedEnrollment = {
actorId: input.actorId,
harness,
name,
persona: input.persona ?? null,
model: input.model,
provider: input.provider,
credential: input.credential,
idempotencyKey: input.idempotencyKey,
correlationId,
digest: digestOf({
harness,
name,
persona: input.persona ?? null,
model: input.model,
provider: input.provider,
credential: input.credential,
}),
};
// Two attempts: a fence-race loser's transaction rolls back and the retry
// resolves through the replay path against the winner's committed row —
// or executes afresh if the winner aborted (§3.1 rule 5 concurrency). A
// unique-violation race never surfaces as an unhandled internal fault.
for (let attempt = 0; attempt < 2; attempt += 1) {
try {
return await this.db.transaction(async (tx) => this.enrollTx(tx, normalized));
} catch (error) {
if (error instanceof ConcurrentEnrollmentError && attempt === 0) continue;
if (error instanceof ConcurrentEnrollmentError) {
return fail('conflict', CONFLICT_MESSAGE);
}
// §4.4 fail-closed: whatever broke, the transaction rolled back and
// the refusal is the internal-fault class — no fallback write or read.
this.logger.error(
`agent.enroll failed closed (correlation=${correlationId}): ${
error instanceof Error ? error.name : 'unknown error'
}`,
);
return fail('internal_fault', 'internal fault');
}
}
return fail('internal_fault', 'internal fault');
}
private async enrollTx(
tx: Tx,
input: NormalizedEnrollment,
): Promise<EnrollmentResult<{ agent: EnrolledAgentView }>> {
const fence = await this.fenceFor(tx, input.idempotencyKey);
if (fence) return this.replay(tx, fence, input);
if (input.credential.mode === 'reference') {
// §3.1 rule 3: the reference must resolve for (actor, provider).
const existing = await tx
.select({ id: providerCredentials.id })
.from(providerCredentials)
.where(
and(
eq(providerCredentials.userId, input.actorId),
eq(providerCredentials.provider, input.provider),
),
)
.limit(1);
if (existing.length === 0) {
return {
ok: false,
error: 'precondition_failed',
message: 'credential reference does not resolve',
correlationId: input.correlationId,
};
}
} else {
// §3.1 rule 2: sealed-store write inside THIS transaction — a later
// failure rolls it back, leaving no orphan credential.
await this.writeSealedCredential(
tx,
input.actorId,
input.provider,
input.credential.value as string,
);
}
const agentRow = await this.insertAgentRow(tx, input);
const fenceRow = await this.insertFenceRow(tx, input, agentRow.id);
if (!fenceRow) {
// A same-(operation, key) winner committed first; abandon our writes.
throw new ConcurrentEnrollmentError();
}
await this.appendEvent(tx, {
eventType: 'agent.enrolled',
actorId: input.actorId,
agentId: agentRow.id,
correlationId: input.correlationId,
// §3.1 rule 6 payload: harness, provider, name, credentialMode — no credential material.
payload: {
harness: input.harness,
provider: input.provider,
name: input.name,
credentialMode: input.credential.mode,
},
});
return { ok: true, correlationId: input.correlationId, agent: agentView(agentRow) };
}
/**
* Replay path (§3.1 rule 5): a fresh submission of a recorded
* (operation, key). The actor is re-authorized exactly as a fresh
* submission (v1: authenticated actor — the guard already ran); then mode,
* scope, digest, and recorded-actor equality; then target-result read
* authority (owner or admin) on the referenced agent. ANY failure refuses
* with the single bounded conflict shape — constant, identifying no record.
* A passing replay executes nothing and appends only the non-mutation
* access event (with its outbox record — one outbox row per event).
*/
private async replay(
tx: Tx,
fence: FenceRow,
input: NormalizedEnrollment,
): Promise<EnrollmentResult<{ agent: EnrolledAgentView }>> {
const collision: EnrollmentFailure = {
ok: false,
error: 'conflict',
message: CONFLICT_MESSAGE,
correlationId: input.correlationId,
};
if (fence.replayMode !== 'actor-bound') return collision;
if (fence.authorizationScope !== AUTHORIZATION_SCOPE) return collision;
if (fence.payloadDigest !== input.digest) return collision;
if (fence.actorId !== input.actorId) return collision;
const rows = await tx.select().from(agents).where(eq(agents.id, fence.outcomeAgentId)).limit(1);
const agentRow = rows[0];
if (!agentRow) return collision;
const authorized =
agentRow.ownerId === input.actorId || (await this.isPlatformAdmin(tx, input.actorId));
if (!authorized) return collision;
await this.appendEvent(tx, {
eventType: 'agent.enrollment.replayed',
actorId: input.actorId,
agentId: agentRow.id,
correlationId: input.correlationId,
payload: { fenceId: fence.id },
});
return { ok: true, correlationId: input.correlationId, agent: agentView(agentRow) };
}
/**
* agent.enrollment.get (§3.2): owner-or-admin read. Unauthorized and
* missing fold to the same not_found wire shape (no existence oracle).
*/
async getEnrollment(
actorId: string,
agentId: string,
correlationId?: string,
): Promise<EnrollmentResult<{ agent: EnrolledAgentView }>> {
const resolvedCorrelation = correlationId ?? randomUUID();
try {
const rows = await this.db.select().from(agents).where(eq(agents.id, agentId)).limit(1);
const row = rows[0];
if (row) {
const authorized =
row.ownerId === actorId || (await this.isPlatformAdmin(this.db, actorId));
if (authorized) {
return { ok: true, correlationId: resolvedCorrelation, agent: agentView(row) };
}
}
return {
ok: false,
error: 'not_found',
message: NOT_FOUND_MESSAGE,
correlationId: resolvedCorrelation,
};
} catch (error) {
this.logger.error(
`agent.enrollment.get failed closed (correlation=${resolvedCorrelation}): ${
error instanceof Error ? error.name : 'unknown error'
}`,
);
return {
ok: false,
error: 'internal_fault',
message: 'internal fault',
correlationId: resolvedCorrelation,
};
}
}
private async fenceFor(tx: Tx, idempotencyKey: string): Promise<FenceRow | null> {
const rows = await tx
.select()
.from(agentIdempotencyFence)
.where(
and(
eq(agentIdempotencyFence.operation, ENROLLMENT_OPERATION),
eq(agentIdempotencyFence.idempotencyKey, idempotencyKey),
),
)
.limit(1);
return rows[0] ?? null;
}
private async isPlatformAdmin(tx: Tx, actorId: string): Promise<boolean> {
const rows = await tx
.select({ role: users.role })
.from(users)
.where(eq(users.id, actorId))
.limit(1);
return rows[0]?.role === 'admin';
}
/**
* Sealed intake write, mirroring ProviderCredentialsService.store semantics
* (seal-at-rest, one row per (userId, provider)) but on the enrollment
* transaction (§3.1 rule 2). The plaintext exists only in this frame.
*/
async writeSealedCredential(
tx: Tx,
userId: string,
provider: string,
value: string,
): Promise<void> {
const encryptedValue = seal(value);
await tx
.insert(providerCredentials)
.values({ userId, provider, credentialType: 'api_key', encryptedValue, metadata: null })
.onConflictDoUpdate({
target: [providerCredentials.userId, providerCredentials.provider],
set: {
credentialType: 'api_key',
encryptedValue,
metadata: null,
updatedAt: new Date(),
},
});
}
async insertAgentRow(tx: Tx, input: NormalizedEnrollment): Promise<AgentRow> {
const rows = await tx
.insert(agents)
.values({
name: input.name,
provider: input.provider,
model: input.model,
harness: input.harness,
systemPrompt: input.persona,
// §3.1 rule 4: owner is the authenticated actor; is_system stays default false.
ownerId: input.actorId,
enrolledAt: new Date(),
})
.returning();
const row = rows[0];
if (!row) throw new Error('agent insert returned no row');
return row;
}
async insertFenceRow(
tx: Tx,
input: NormalizedEnrollment,
outcomeAgentId: string,
): Promise<FenceRow | null> {
const rows = await tx
.insert(agentIdempotencyFence)
.values({
operation: ENROLLMENT_OPERATION,
idempotencyKey: input.idempotencyKey,
actorId: input.actorId,
authorizationScope: AUTHORIZATION_SCOPE,
payloadDigest: input.digest,
replayMode: 'actor-bound',
outcomeAgentId,
})
.onConflictDoNothing()
.returning();
return rows[0] ?? null;
}
/** Append one audit event and its outbox record on the caller's transaction (one outbox row per event). */
async appendEvent(
tx: Tx,
input: {
eventType: 'agent.enrolled' | 'agent.enrollment.replayed';
actorId: string;
agentId: string;
correlationId: string;
payload: Record<string, unknown>;
causationId?: string;
},
): Promise<void> {
const inserted = await tx
.insert(agentAuditEvents)
.values({
eventType: input.eventType,
actorId: input.actorId,
agentId: input.agentId,
correlationId: input.correlationId,
causationId: input.causationId ?? null,
payload: input.payload,
})
.returning();
const event = inserted[0];
if (!event) throw new Error('agent audit event insert returned no row');
await this.insertOutboxRow(tx, event.id, input.correlationId);
}
async insertOutboxRow(tx: Tx, eventId: string, correlationId: string): Promise<void> {
await tx.insert(agentOutbox).values({ eventId, correlationId });
}
}
@@ -0,0 +1,45 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import type {
EnrollmentErrorCode,
EnrollmentFailure,
EnrollmentResult,
} from './enrollment.repository.js';
/**
* Maps enrollment result unions onto the closed HTTP status set (design
* docs/plans/2026-08-29-agent-enrollment-command-design.md §3.3, contract 5
* §4.2). Every refusal body carries the correlation id (contract 5 §4.3
* end-to-end traceability) alongside the enum code. `not_found` carries one
* fixed message for every cause — missing agent and unauthorized caller are
* indistinguishable on the wire (§3.2).
*/
const HTTP_STATUS: Record<EnrollmentErrorCode, HttpStatus> = {
validation_failed: HttpStatus.BAD_REQUEST,
authentication_failed: HttpStatus.UNAUTHORIZED,
authorization_refused: HttpStatus.FORBIDDEN,
not_found: HttpStatus.NOT_FOUND,
conflict: HttpStatus.CONFLICT,
precondition_failed: HttpStatus.UNPROCESSABLE_ENTITY,
internal_fault: HttpStatus.INTERNAL_SERVER_ERROR,
};
@Injectable()
export class EnrollmentService {
unwrap<T>(result: EnrollmentResult<T>): { ok: true; correlationId: string } & T {
if (result.ok) return result;
throw this.toException(result);
}
private toException(failure: EnrollmentFailure): HttpException {
const status = HTTP_STATUS[failure.error];
return new HttpException(
{
statusCode: status,
error: failure.error,
message: failure.message,
correlationId: failure.correlationId,
},
status,
);
}
}
@@ -108,11 +108,13 @@ export class MissionsController {
) {
const mission = await this.brain.missions.findByIdAndUser(missionId, user.id);
if (!mission) throw new NotFoundException('Mission not found');
// dto.status is deliberately not forwarded: mission_tasks.status is
// write-prohibited through the N-1 window (SHARED-CONTRACT §5.1 phase 1);
// the repo strips it as well.
return this.brain.missionTasks.create({
missionId,
taskId: dto.taskId,
userId: user.id,
status: dto.status,
description: dto.description,
notes: dto.notes,
pr: dto.pr,
+12
View File
@@ -77,6 +77,12 @@ export class CreateMissionTaskDto {
@IsUUID()
taskId?: string;
/**
* @deprecated Accepted for N-1 wire compatibility but ignored: mission_tasks.status
* is write-prohibited through the migration window (SHARED-CONTRACT §5.1 phase 1).
* The field stays declared because the global ValidationPipe runs with
* forbidNonWhitelisted, and removing it would 400 frozen legacy consumers.
*/
@IsOptional()
@IsIn(taskStatuses)
status?: 'not-started' | 'in-progress' | 'blocked' | 'done' | 'cancelled';
@@ -102,6 +108,12 @@ export class UpdateMissionTaskDto {
@IsUUID()
taskId?: string;
/**
* @deprecated Accepted for N-1 wire compatibility but ignored: mission_tasks.status
* is write-prohibited through the migration window (SHARED-CONTRACT §5.1 phase 1).
* The field stays declared because the global ValidationPipe runs with
* forbidNonWhitelisted, and removing it would 400 frozen legacy consumers.
*/
@IsOptional()
@IsIn(taskStatuses)
status?: 'not-started' | 'in-progress' | 'blocked' | 'done' | 'cancelled';
+30
View File
@@ -13,6 +13,11 @@ import {
TransferEstateDto,
TransferPlatformProjectDto,
} from './hierarchy/hierarchy.dto.js';
import {
EnrollAgentDto,
EnrollCredentialDto,
GetEnrollmentQueryDto,
} from './enrollment/enrollment.dto.js';
/**
* Boot-time self-check: the global ValidationPipe must be able to SEE the
@@ -105,6 +110,31 @@ export const PIPE_GUARDED_DTOS: Array<{
target: ChangeGrantDto,
properties: ['role', 'idempotencyKey'],
},
{
name: 'EnrollAgentDto',
target: EnrollAgentDto,
properties: [
'harness',
'name',
'persona',
'model',
'provider',
'credential',
'idempotencyKey',
'correlationId',
'replayMode',
],
},
{
name: 'EnrollCredentialDto',
target: EnrollCredentialDto,
properties: ['mode', 'type', 'value'],
},
{
name: 'GetEnrollmentQueryDto',
target: GetEnrollmentQueryDto,
properties: ['correlationId'],
},
];
export class PipeMetatypeCheckError extends Error {
+37
View File
@@ -0,0 +1,37 @@
# Explicit, single-seat dogfood mode for stack-containerization B2.
# Use with docker-compose.yml. The base stack remains credential-free.
services:
gateway:
# The R4 credential helper establishes ownership from process ancestry and
# intentionally does not trust PID 1. Keep gateway Node below Docker's init.
init: true
environment:
# Identity and credential layout match a fleet seat. This fixed name prevents
# an operator from mounting one seat while attributing actions to another.
MOSAIC_AGENT_NAME: code-dogfood-01
MOSAIC_GIT_IDENTITY: code-dogfood-01
MOSAIC_BRAIN_HOME: /opt/mosaic/brain
AGENT_FILE_SANDBOX_DIR: /workspace/stack
# Disable the general shell before admin/user allowlist resolution. Delivery
# uses execFile-only tools bound to the queue and PR wrappers below.
AGENT_SHELL_ENABLED: 'false'
AGENT_DELIVERY_ENABLED: 'true'
MOSAIC_GIT_TOOLS_DIR: /opt/mosaic/tools/git
MOSAIC_INTEGRATION_TRUNK: next
AGENT_USER_TOOLS: fs_read_file,fs_write_file,fs_list_directory,fs_edit_file,git_status,git_log,git_diff,git_publish_branch,git_open_pull_request
volumes:
# Mount a dedicated worktree, never the canonical clone or divergent local main.
- type: bind
source: ${MOSAIC_DOGFOOD_WORKTREE:?set to a dedicated next-based stack worktree}
target: /workspace/stack
# A Git worktree's .git file points into the canonical clone's common Git
# directory. Mount that directory at its original absolute path so Git can
# resolve the pointer. File tools cannot traverse outside /workspace/stack.
- type: bind
source: ${MOSAIC_DOGFOOD_COMMON_GIT_DIR:?set to the canonical stack clone .git directory}
target: ${MOSAIC_DOGFOOD_COMMON_GIT_DIR:?set to the canonical stack clone .git directory}
# Only this seat home enters the container. Other fleet credentials stay outside.
- type: bind
source: ${MOSAIC_DOGFOOD_SEAT_HOME:?set to the external code-dogfood-01 seat directory}
target: /opt/mosaic/brain/fleet/agents/code-dogfood-01
read_only: true
+38
View File
@@ -47,6 +47,44 @@ services:
environment:
COLLECTOR_OTLP_ENABLED: 'true'
gateway:
# Standalone-tier application service (compose `stack` profile).
# Default image = local build of docker/gateway.Dockerfile (works with
# no registry auth); override GATEWAY_IMAGE to a CI-published sha tag
# for registry deploys (git.mosaicstack.dev/mosaicstack/stack/gateway:sha-XXXXXXX).
profiles: [stack]
image: ${GATEWAY_IMAGE:-mosaic-gateway:dev}
build:
context: .
dockerfile: docker/gateway.Dockerfile
ports:
- '${GATEWAY_HOST_PORT:-14242}:14242'
environment:
GATEWAY_PORT: '14242'
DATABASE_URL: postgresql://mosaic:mosaic@postgres:5432/mosaic
VALKEY_URL: valkey://valkey:6379
# The compose IS the standalone tier by declaration (mode contract:
# mode chosen at install); pinning skips cross-container probe races.
MOSAIC_STORAGE_TIER: standalone
# Standalone-tier secrets: generated at install (see .env.example).
# Enterprise tier replaces these with Vault/Openbao plumbing.
BETTER_AUTH_SECRET: '${BETTER_AUTH_SECRET:?set in .env — openssl rand -hex 32}'
volumes:
- gateway_workspaces:/opt/mosaic/.workspaces
depends_on:
postgres:
condition: service_healthy
valkey:
condition: service_healthy
healthcheck:
test: ['CMD-SHELL', 'wget -qO- http://127.0.0.1:14242/health || exit 1']
interval: 10s
timeout: 5s
retries: 12
start_period: 20s
volumes:
gateway_workspaces:
pg_data:
valkey_data:
+20 -2
View File
@@ -29,11 +29,29 @@ ENV NODE_ENV=production
# $MOSAIC_ROOT/.workspaces (apps/gateway/src/workspace/workspace.service.ts);
# mount a volume over /opt/mosaic to persist workspaces across container restarts.
# Intentionally unpinned: Alpine's signed repository is the trust anchor; pinning
# git was declined so routine base-image security updates remain maintainable.
RUN apk add --no-cache git \
# packages was declined so routine base-image security updates remain maintainable.
# bash/curl/python3 are runtime dependencies of the provider-neutral Mosaic git
# wrappers. jq supports wrapper discovery for non-canonical Gitea hosts.
RUN apk add --no-cache bash curl git jq python3 \
&& mkdir -p /opt/mosaic/.workspaces \
&& chown -R node:node /opt/mosaic /app
ENV MOSAIC_ROOT=/opt/mosaic
# Dogfood agents use the same fail-closed credential helper, queue guard, and
# PR-create wrapper as fleet seats. Copy only those operations and their shared
# dependencies. Merge and infrastructure tools stay out of the image.
COPY --from=builder /app/packages/mosaic/framework/tools/git/pr-create.sh /opt/mosaic/tools/git/pr-create.sh
COPY --from=builder /app/packages/mosaic/framework/tools/git/ci-queue-wait.sh /opt/mosaic/tools/git/ci-queue-wait.sh
COPY --from=builder /app/packages/mosaic/framework/tools/git/detect-platform.sh /opt/mosaic/tools/git/detect-platform.sh
COPY --from=builder /app/packages/mosaic/framework/tools/git/repo-decl.sh /opt/mosaic/tools/git/repo-decl.sh
COPY --from=builder /app/packages/mosaic/framework/tools/git/git-credential-mosaic /opt/mosaic/tools/git/git-credential-mosaic
# R4 hardening (P0-SEC, brain 15f6979a): the credential helper is a pair.
# python entrypoint (allowlist envp, execve boundary) + the bash implementation
# it execs. The entrypoint derives the .impl path from its own directory, so the
# pair sits side by side; system gitconfig keeps pointing at the entrypoint.
COPY --from=builder /app/packages/mosaic/framework/tools/git/git-credential-mosaic.impl /opt/mosaic/tools/git/git-credential-mosaic.impl
COPY --from=builder /app/packages/mosaic/framework/tools/_lib/credentials.sh /opt/mosaic/tools/_lib/credentials.sh
COPY --from=builder /app/packages/mosaic/framework/tools/structure/validate-repo-json.sh /opt/mosaic/tools/structure/validate-repo-json.sh
RUN git config --system credential.helper /opt/mosaic/tools/git/git-credential-mosaic
# Use the pnpm deploy output — resolves all deps into a flat, self-contained node_modules
COPY --chown=node:node --from=builder /deploy/node_modules ./node_modules
COPY --chown=node:node --from=builder /deploy/package.json ./package.json
+34 -17
View File
@@ -197,24 +197,41 @@ conflict must amend one of them explicitly, never fork a third document
- A second writable task store beside PostgreSQL (native-kanban-sot invariants).
- Fully-designed federation in v1 (D3 — roadmap placeholder only).
### 12. Decision registry
### D15 — Tiered containerized deployment (2026-08-30, containerization lane)
| ID | Decision (short form) |
| --- | ------------------------------------------------------------------------------------------------------------------ |
| D1 | Open-source, AI-first, self-hosted platform for agentic management + life OS |
| D2 | Hierarchy company→estate→project→workspace→kanban; bubble-up; granular RBAC |
| D3 | Standalone vs Enterprise; one-way conversion; per-user brains + Vault required in Enterprise; federation deferred |
| D4 | Re-runnable, extensible, per-mode onboarding wizards |
| D5 | North star = this rewrite of docs/PRD.md; stack docs/ = product SSOT |
| D6 | Only product-relevant material migrates from brains; operational records stay and link |
| D7 | Spec-inventory sweep launched immediately (executed; INPUTS baseline frozen by operator ruling T2, 2026-08-25) |
| D8 | webUI sits over official framework tooling; CLI primary |
| D9 | Not a hosted business; company = organizational separation for one operator |
| D10 | better-auth is the account system of record; external IdPs via OIDC |
| D11 | Small v1 slice; ALL phases on the documented roadmap from day one |
| D12 | HARD RULE: webUI never bypasses tooling; missing tool ⇒ build the tool first |
| D13 | workspace_id stays the hard isolation unit; hierarchy is parent structure above; kanban SOT amended, not rewritten |
| D14 | Sensitive profile data in the user's own brain only; postgres holds structure/consent/pointers |
The stack ships a tiered deployment target, additive to the architecture
gate (D8): (1) Standalone tier — docker compose is the canonical
single-host deployment: postgres, valkey, openbao, gateway, appservice
and the served webUI in one composition, with migrations, health checks,
and a documented install/upgrade path; the registry (CI-published
images) is the only deployment source. (2) Enterprise tier — Kubernetes
manifests for the same service set, phase-gated on the standalone tier
holding its acceptance bar. The v1 acceptance bar for the standalone
tier: compose-up healthy; webUI hosts agent chat; an in-stack agent can
open a PR to this repo; CI validates it; the running deployment adopts
the merged change (pull + restart). Federation (D3 clause) remains
deferred and unforeclosed. Implementation plan:
docs/plans/2026-08-30_containerization.md.
## 12. Decision registry
| ID | Decision (short form) |
| --- | ------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------- |
| D1 | Open-source, AI-first, self-hosted platform for agentic management + life OS |
| D2 | Hierarchy company→estate→project→workspace→kanban; bubble-up; granular RBAC |
| D3 | Standalone vs Enterprise; one-way conversion; per-user brains + Vault required in Enterprise; federation deferred |
| D4 | Re-runnable, extensible, per-mode onboarding wizards |
| D5 | North star = this rewrite of docs/PRD.md; stack docs/ = product SSOT |
| D6 | Only product-relevant material migrates from brains; operational records stay and link |
| D7 | Spec-inventory sweep launched immediately (executed; INPUTS baseline frozen by operator ruling T2, 2026-08-25) |
| D8 | webUI sits over official framework tooling; CLI primary |
| D9 | Not a hosted business; company = organizational separation for one operator |
| D10 | better-auth is the account system of record; external IdPs via OIDC |
| D11 | Small v1 slice; ALL phases on the documented roadmap from day one |
| D12 | HARD RULE: webUI never bypasses tooling; missing tool ⇒ build the tool first |
| D13 | workspace_id stays the hard isolation unit; hierarchy is parent structure above; kanban SOT amended, not rewritten |
| D14 | Sensitive profile data in the user's own brain only; postgres holds structure/consent/pointers |
| D15 | Tiered containerized deployment: compose standalone tier (five-point v1 bar) + phase-gated k8s enterprise tier; registry-only image source | 2026-08-30 containerization lane; plan docs/plans/2026-08-30_containerization.md |
The full decision texts are recorded in the operator decision log (USC estate
brain, webui-audit lane, `GRILL.md`).
+1
View File
@@ -14,6 +14,7 @@
| [`KBN-101-DB-ROLE-SPLIT.md`](./KBN-101-DB-ROLE-SPLIT.md) | rc.16 direct-Drizzle current storage-wrapper hold: legacy N-1/uncertified/non-operative pending -02/-03/-06/-08; exact README commented/user-guide executable forms fail before masking and source-consistency rejects runner-delegation copy; held future bootstrap → TLS/roles → run → verify → readiness; plus prior production boundary, pgvector owner, attestation, inventory, manifests, DDL classifier, TLS/bootstrap, activation, and certification contract; foundation prerequisite of KBN-100 and real-role gate before KBN-105 |
| [`KBN-101-ENVELOPE-A.md`](./KBN-101-ENVELOPE-A.md) | KBN-101 Envelope A (v6) — RATIFIED, part of the frozen SSOT: rc.20 declarative sink-RBAC + per-role connection-selection + RLS `WITH CHECK`/`USING` write-source + `FORCE ROW LEVEL SECURITY` + sink-resident `task_status_write_override`; adds owner card KBN-101-10 + responsibility-widenings; authority Jason B1 + Mos OPTION A/Q1/Q2 |
| [`SHARED-CONTRACT.md`](./SHARED-CONTRACT.md) | Remediated v1 integration contract: proof authority, exact failures/routes/DTOs/MCP ownership, concrete current-main field migration map, relational invariants, Coordinator split, recovery delivery |
| [`P0-MAP-CURRENCY-2026-08-29.md`](./P0-MAP-CURRENCY-2026-08-29.md) | REQ-MIG-001 lane-opening verification: SHARED-CONTRACT §5 field map re-verified byte-identical at `next` @ `abb0c936`; workspaces/audit-pattern refinements; measured `mission_tasks.status` writer inventory and the pre-expand stop-write work item |
| [`contracts/kanban-schema.v1.ts`](./contracts/kanban-schema.v1.ts) | Drizzle target declarations including exact owner/principal membership, project congruence, tags/archive, proposals, persisted assignments, monotonic fences, durable retry, immutable evidence/audit |
| [`contracts/mechanical-coordinator.v1.ts`](./contracts/mechanical-coordinator.v1.ts) | Pure snapshot decision engine separated from persistence/service adapter; ID-bound approvals, bigint-safe fences, durable retry/quarantine, artifact-backed checkpoints, exact failures |
| [`contracts/health-state.v1.ts`](./contracts/health-state.v1.ts) | Discriminated public health, separate branded transaction-local write proof, and non-overlapping denial/transport/version-conflict mappings |
@@ -0,0 +1,117 @@
---
kind: verification
status: active
---
# P0 Field-Map Currency Verification — 2026-08-29
**Purpose:** REQ-MIG-001 (native-kanban-sot.md §5) accepts only when "P0 publishes
the current `origin/main` field-by-field expand/backfill/compatibility/switch/contract
map before any schema lane starts." That map exists: [`SHARED-CONTRACT.md`](./SHARED-CONTRACT.md)
§5, inspected at `packages/db/src/schema.ts` @ `e72388b2cbfe400842fe940fa6cabf984ed43711`
(2026-07-13). The M4-3 schema lane (expand migration 0021+) now opens against the
integration trunk `next`. This document re-verifies the map's currency at the
lane-opening head and records the measured pre-expand writer inventory. It amends
nothing normative in SHARED-CONTRACT.md; where the two disagree, SHARED-CONTRACT.md
wins.
## 1. Currency verification (measured)
- Map pin: `e72388b2cbfe400842fe940fa6cabf984ed43711` (2026-07-13, `main`).
- Lane-opening head: `abb0c936011c7f6b8c0bcc90a20a865d5e8a40e9` (`origin/next`,
2026-08-29).
- Measurement: `git diff e72388b2 abb0c936 -- packages/db/src/schema.ts` reports
**300 insertions, 0 deletions** — no existing declaration changed.
- The additions: the new declarations `logicalAgentConnectorLeases`,
`connectorLeaseAuditLog`, and the hierarchy layer (`companies`, `estates`,
`platformProjects`, `workspaces`, `hierarchyGrants`, `hierarchyAuditEvents`,
`hierarchyOutbox`, plus their enums and constant arrays); a nullable `issuer`
column on the unmapped BetterAuth `accounts` table (shipped as
`drizzle/0017_accounts_issuer.sql`); and expanded `drizzle-orm` imports
(`sql`, `AnyPgColumn`, `unique`, `check`, `bigint`). None touch a mapped
source.
- Stronger literal fact: REQ-MIG-001's acceptance names `origin/main`. Measured
pin → `origin/main` (`7102ccb9`, 2026-08-13): **63 insertions, 0 deletions**
for `schema.ts`, and `origin/main` is an ancestor of `abb0c936`. The map is
therefore current at `origin/main` itself, and at the trunk head beyond it.
**Consequence:** every source column mapped in SHARED-CONTRACT.md §5.4 —
`teams`/`team_members`, `projects`, `missions`, `tasks`, `mission_tasks`,
`agents`, fleet `backlog` — is byte-identical to the declaration the map
inspected. The field map is current as written. No row changes.
## 2. Refinements available since the pin (context, not map changes)
1. **The `workspaces` table exists.** The map predates contract 1's hierarchy
layer; its "bootstrap workspace" backfill step now has a shipped target:
`workspaces` (uuid PK, chained under platform projects per
`docs/requirements/hierarchy-schema.md`; hierarchy core in
`drizzle/0018_clean_cobalt_man.sql`, audit/outbox in
`0019_volatile_killraven.sql`, visibility in
`0020_special_betty_brant.sql`). New `workspace_id` columns FK there.
2. **The audit/outbox envelope pattern is shipped.** `hierarchyAuditEvents` +
`hierarchyOutbox` implement same-transaction semantic event + outbox. The
task lane's `task_events`/`task_outbox` mirror the pattern but are
workspace-scoped with the composite `(workspace_id, id)` key required by
§5.3 and REQ-SOT-004. The hierarchy tables are a pattern reference, never a
shared store for task events.
3. **Trunk designation.** The integration trunk is `next` (`.mosaic/repo.json`).
§1 measures currency at both the literal `origin/main` REQ-MIG-001 names and
the trunk head pinned above, so no reinterpretation of the acceptance text
is needed.
4. **Migration ownership.** SHARED-CONTRACT.md §6 assigns schema/migration
ownership to the mission seat `coder2`. Seat identity is operational fleet
state, not resolvable from this repository, and is outside this document's
scope. The invariant §6 protects binds regardless of seat and is restated
here as binding on the M4-3 schema lane: exactly one lane generates
migrations at a time; expand is additive; no drop/rename/narrow; constraints
validate before NOT NULL.
## 3. Pre-expand writer inventory (measured 2026-08-29 at `abb0c936`)
SHARED-CONTRACT.md §5.1 phase 1 requires an N-1 patch that stops
`mission_tasks.status` as a write source, plus a writer inventory, before any
expand DDL.
- **Sole authoring write path:** `packages/brain/src/mission-tasks.ts`
`create`/`update` (Drizzle insert/update on `mission_tasks`), invoked by
`apps/gateway/src/missions/missions.controller.ts`. `update` accepts
`Partial<NewMissionTask>`, so `status` is writable through both DTOs today.
The same module also exposes `remove`/`removeByMission` DELETE paths —
immaterial to `status` writes, listed for inventory completeness.
- **Storage-layer surfaces that touch the column without authoring it**
(added 2026-08-29 after independent review of the phase-1 patch):
`packages/storage/src/migrate-tier.ts` copies whole `mission_tasks` rows
between storage tiers and must preserve the stored `status` verbatim — row
transport, exempt from the write prohibition (stripping there would corrupt
data inside the N-1 window). The generic table-keyed storage adapters
(`adapters/postgres.ts`, `adapters/pglite.ts`) register `mission_tasks` in
their table maps but have no caller that targets it: measured at this head,
every runtime adapter caller passes a fixed collection constant
(preferences/insights). Neither surface authors a new `status` value.
- **Read-only consumers of `mission_tasks`:** federation verb services
(`get-query.service.ts`, `list-query.service.ts`) select only. The MCP
`brain_*` tools do not touch `mission_tasks` at all; `brain_create_task` /
`brain_update_task` write the separately mapped `tasks` table, a legitimate
N-1 writer through the compatibility window.
- The ratified contract 5 decision
(`docs/requirements/tool-gateway-mapping.md` §3.2, ruled 2026-08-27) freezes
the legacy endpoints — including MCP `brain_*` task mutations — for new
consumers, while existing consumers keep working until each surface's owning
contract retires it. It does not stop existing writes.
**Standing work item:** the phase-1 stop-write patch (reject or ignore `status`
on `mission_tasks` create/update) MUST land before the expand DDL of migration
lane M4-3a. It is N-1-safe per the §5.4 row for `mission_tasks.status` (linked
status is ignored; the column stays declared and readable through the whole
N-1 window; retirement only after no readers).
## 4. Lane opening
With this verification merged, REQ-MIG-001's P0-map precondition is satisfied
for the M4-3 schema lane at pinned head `abb0c936`. The ordered phases (§5.1),
mission candidate-key DDL order (§5.2), audit/proposal DDL order (§5.3), field
map (§5.4), and required migration tests (§5.5) bind as written. External
import machinery (jarvis-brain/Vikunja shadow import, REQ-MIG-001) and client
cutover (REQ-MIG-002) remain out of scope for M4-3; the legacy surface stays
frozen for new consumers meanwhile (`tool-gateway-mapping.md` §3.2 decision).
@@ -0,0 +1,315 @@
---
kind: spec
status: active
audience: developer
---
# Agent Enrollment Command Family — v1 Design (M4-4-0)
Status: design note (implementation-facing; amends no contract).
Authority chain: tool-gateway-mapping.md §3.1 rank-4 row + §4 envelope
(ruled 2026-08-27), onboarding-wizard.md §3.5 (D11 minimal enrollment),
custody-schema.md §5.2 at revision 13 (agent-grantee FK bound to the
live `agents` table — a binding introduced at rev 4 and standing
verbatim), PRD §9 D11. Where this note and a ratified contract disagree,
the contract wins.
## 1. What the contracts bind (and what they leave open)
There is no standalone enrollment contract. The rank-4 family is defined
by composition:
1. **Contract 5 §3.1 rank 4:** "Enroll one agent: harness, credential
reference/API-key intake (values never echoed), name/persona,
assignment scope (contract 3 §3.5)."
2. **Contract 5 §4 — all five sub-clauses:** §4.1 typed request/result
DTOs validated at the Gateway boundary (expected-version only where
an owning contract defines one); §4.2 closed per-family error enum
(validation, authentication, authorization, not-found, conflict,
precondition, internal) with HTTP mappings; §4.3 audit linkage — the
envelope contributes correlation: every request accepts/generates a
correlation id, carried into the audit events **and returned in the
result**, with no second audit stream; §4.4 fail-closed — an
operation that cannot evaluate its authorization or reach its owning
tool refuses, never degrading to a fallback read or direct data
access; §4.5 CLI parity — the family MUST be invocable through the
official CLI against the same Gateway commands with the same
request/result/error contracts (a Gateway command without CLI
exposure is a tracked conformance gap).
**Idempotency keys are NOT contract 5 §4.3:** the idempotency-key
envelope is contract 3 §4.3, ratified as a drafting addition to
contract 5 §4's command envelope via contract 3 §7 item 4. Its fence
and replay rules bind as written there; §3.1 rule 5 below designs to
them.
3. **Contract 3 §3.5:** the wizard's enrollment step is minimal (one
harness, API-key login, agent name and persona — D11), uses ONLY this
family, and is skippable. Wizard witness §6.10: a run that skips the
step produces zero enrollment-family mutations.
4. **Custody-schema §5.2 (rev 13; binding introduced at rev 4):**
contract 7's agent-grantee FK references the live `agents` table
(`agents.id`, uuid); an enrollment surface with its own table would
force a contract-7 amendment.
**Assignment scope (open point, pinned here):** the rank-4 row cites
contract 3 §3.5, which defines no assignment semantics; the PRD's full
enrollment vision (Part I, Standalone flow) includes "account
assignment", but the D11 v1 slice is exactly "one harness, API key,
name/persona". v1 therefore scopes assignment to the two bindings the
minimal slice already implies — the enrolling user becomes the agent's
owner (`agents.owner_id`), and the credential reference names which of
that user's stored provider credentials the agent uses. Richer
assignment (multi-account, comms auto-enroll, workspace placement) is
deferred with the rest of the PRD's full flow (D11); when a contract
defines it, this family extends by ordinary amendment of the design.
The deferral rests on contract 3 §3.5's explicit delegation of
enrollment specifics to this family — not on reading the D11 list as
exhaustive (it is not: the §3.1 `model`/`provider` fields are required
by the live table's NOT NULL columns, though D11 does not name them).
## 2. Current state (measured 2026-08-29 at `origin/next` = `94d626df`)
- `agents` table (packages/db `schema.ts`): id uuid PK, name, provider,
model, status enum, project_id (legacy `projects`, ON DELETE SET
NULL), owner_id → users, system_prompt, allowed_tools, skills,
is_system, config jsonb, timestamps. No harness column (provider and
model describe the LLM backend, not the harness), no audit coupling.
- Sole write path: `packages/brain/src/agents.ts` repository (the only
module issuing `insert(agents)`), with three write consumers: the
legacy `/api/agents` CRUD controller
(`apps/gateway/src/agent/agent-configs.controller.ts`), the `/agent
new` chat command (`apps/gateway/src/commands/command-executor.service.ts`
`brain.agents.create`), and workspace bootstrap
(`apps/gateway/src/workspace/project-bootstrap.service.ts`). All
three keep serving existing consumers; none is touched by M4-4.
- Sealed credential store exists: `ProviderCredentialsService`
(apps/gateway/src/agent/) — one row per (userId, provider), values
sealed at rest, decrypt server-side only, summaries never carry
values.
- Harness registry exists (`apps/gateway/src/harness/`), the validation
source for the harness field.
- Implementation pattern: the merged hierarchy module (M4-1) —
transaction-scoped command context, in-tx authorization, discriminated
result unions, same-transaction semantic audit event + transactional
outbox, no-oracle not_found folding.
**F1 — contract-5 mapping note (disposition, not an amendment):**
`/api/agents` appears nowhere in contract 5 — neither as a P0 row nor in
the §3.2 legacy non-substitutes list (the ruled §3.2 freeze names
specific endpoints, and `/api/agents` is not among them). The operative
constraints are §3.3's amendment-only rule for new mapping rows and §5's
closure rule: this design adds no new consumer to `/api/agents` and
builds the rank-4 family as the P1 path for enrollment. Adding the
missing P0 row is a contract amendment for a future S2 pass; nothing in
M4-4 depends on it.
## 3. Command family surface (v1)
One command, one query. Module: `apps/gateway/src/enrollment/`
(`enrollment.module.ts`), mirroring the hierarchy module's shape.
### 3.1 `agent.enroll` (mutation)
Request DTO (shared types package, class-validator at the boundary):
| Field | Type | Rule |
| ---------------- | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `harness` | string | syntactically invalid (empty/malformed) → `validation_failed`; well-formed but not in the harness registry → `precondition_failed` |
| `correlationId` | string (uuid) | optional; generated when absent (contract 5 §4.3); carried into audit events and returned in the result |
| `replayMode` | 'actor-bound' | optional, default `actor-bound`. `shared` is seed-only (contract 3 §4.3 binds it to the §3.4 canonical seed key set and "no other operation can carry a shared declaration"; §7 item 4 closes it); a `shared` declaration here is refused `validation_failed`, executes nothing, and records no fence row |
| `name` | string | non-empty, trimmed, ≤ 200 chars |
| `persona` | string \| null | optional; stored as the agent's system prompt |
| `model` | string | non-empty (provider-qualified model id) |
| `provider` | string | non-empty; names the credential's provider |
| `credential` | discriminated union | `{ mode: 'reference' }` — a credential for (actor, provider) MUST already exist; `{ mode: 'intake', type: 'api_key', value: string }` — value is sealed into the credential store in the same flow |
| `idempotencyKey` | string (uuid) | required (contract 3 §4.3, ratified into contract 5 §4 via contract 3 §7 item 4) |
Rules:
1. **Never echoed.** The credential value appears in no result DTO, no
audit event, no outbox payload, and no log line. The result carries
only `{ provider, credentialMode }`.
2. **Intake = the existing sealed store, inside the transaction.**
`intake` writes through the sealed-store path
(`ProviderCredentialsService.store` semantics: seal-at-rest, upsert
per (userId, provider)) **in the same transaction** as the agent
insert — a failure after the credential write rolls everything back,
leaving no orphan credential. Enrollment persists no second copy and
no plaintext.
3. **Reference must resolve.** `reference` with no stored credential for
(actor, provider) refuses with `precondition_failed` (nothing is
created).
4. **Ownership.** `owner_id` = the authenticated actor. v1 authorization
is AuthGuard-authenticated user; no hierarchy grant is required
because v1 enrollment binds no hierarchy node (§1 assignment-scope
pin). `is_system` is never settable through this command.
5. **Idempotency fence (contract 3 §4.3, in full).** The command layer
records, in a uniqueness-constrained fence table in the same
transaction as the mutation and its audit event: the key, the
operation identifier (`agent.enroll`), the acting principal, the
authorization scope, a digest of the canonicalized request payload
(the digest input EXCLUDES the credential value — it covers
provider + credentialMode, never plaintext), the declared replay
mode (always `actor-bound` for this family — the `shared` refusal
in the table above means no shared fence row can exist here; the
column is kept for envelope-shape fidelity and mode-mismatch
collision checks), and a reference to the committed outcome (the
agent id). The recorded **authorization scope** for this family is
pinned to the acting principal's platform-user scope (v1
authorization is grant-free per rule 4, so the scope is the
authenticated-user identity domain — recorded so the §4.3
scope-equality check has a defined value). Fence uniqueness is the
pair (operation identifier, key). **Replay:** a submission whose
(operation, key) is recorded is first authorized exactly as a fresh
submission; then replay-mode, scope, and digest equality are
checked (a mismatch on any — including scope — is a collision);
then **target-result authorization** — the submitter must hold, at
replay time, read authority on the referenced agent row under
§3.2's rule (owner or admin) — plus recorded-actor equality
(`actor-bound`). A passing replay executes nothing, returns the
recorded outcome, and appends a replay access event (non-mutation
audit class: accessing principal, current correlation id,
fence-row reference). Any equality or authorization failure refuses
with the single bounded `conflict` shape — constant, identifying no
record — preserving the no-existence-oracle rule. **Concurrency
(contract 3 §4.3's rule, ratified via §7 item 4):** two submissions
with the same (operation, key) serialize on the fence's unique
constraint — exactly one executes; the loser waits for the winner's
transaction, and is then handled as a replay if it committed
(through the full replay path above) or executes afresh if it
aborted. A unique-violation race never surfaces as an unhandled
internal fault.
6. **Audit + outbox, same transaction.** Insert into `agents` +
sealed credential write (intake mode) + fence row + semantic audit
event (`agent.enrolled`: actor, agent id, harness, provider, name,
credentialMode — no credential material) + outbox row commit
atomically, hierarchy-pattern style. Audit rows reference the agent
by **snapshot id, not FK** — mirroring the hierarchy audit tables'
deliberate FK-free linkage so audit history survives agent deletion
through the legacy CRUD DELETE path.
Result union: `enrolled { agent, correlationId }` | refusal from the
§3.3 enum (refusals also carry the correlation id, per contract 5
§4.3's end-to-end traceability). `agent` in the result is the persisted
row minus nothing sensitive (the table stores no credential material).
### 3.2 `agent.enrollment.get` (query)
By agent id; actor must be the owner (or admin). Unauthorized and
missing fold to the same `not_found` wire shape (contract 2
no-existence-oracle rule, applied family-wide for uniformity).
The query carries the same non-state envelope as the mutation
(contract 5 §4.3; contract 3's envelope reconciliation confirms closed
query responses carry it): typed request DTO with an optional
`correlationId` (generated when absent) and a typed result —
`found { agent, correlationId }` | `not_found` (the folded shape,
also carrying the correlation id). Queries take no idempotency key
(the fence binds mutations).
### 3.3 Error enum (closed, §4.2)
`validation_failed` 400 · `authentication_failed` 401 ·
`authorization_refused` 403 (owner-only paths; folded to `not_found`
where §3.2 applies) · `not_found` 404 · `conflict` 409 (the single
bounded idempotency refusal shape of §3.1 rule 5) · `precondition_failed`
422 (unresolvable credential reference; well-formed harness not in the
registry — syntactic invalidity is `validation_failed` per the §3.1
table) · `internal_fault` 500 (also the §4.4 fail-closed class when the
owning tool is unreachable; unauthorized-fallback behavior is
prohibited).
## 4. Schema delta (migration 0021, additive-only)
Extend `agents` — no new agent table, preserving custody-schema §5.2's
FK binding without amendment:
- `harness` text NULL — registered harness name; NULL for pre-existing
rows (legacy rows predate the concept).
- `enrolled_at` timestamptz NULL — set by `agent.enroll`; NULL marks a
legacy (non-enrolled) row. No backfill: enrollment is a fact this
command creates, not one to invent for existing rows.
New tables, mirroring the hierarchy audit/outbox pair (pattern reuse,
separate store): `agent_audit_events` (append-only: id, event_type,
actor id, agent id — snapshot value, no FK, per §3.1 rule 6 —
correlation id, causation id, payload jsonb, created_at; per-agent
ordering index), `agent_outbox` (hierarchy-outbox shape), and
`agent_idempotency_fence` (contract 3 §4.3 shape: operation identifier,
key, acting principal, authorization scope, canonicalized-payload
digest, replay mode, committed-outcome reference (agent id), created_at;
UNIQUE (operation identifier, key)). Persona reuses the existing
`system_prompt` column; no version column (no ratified expected-version
rule names `agents` — §4.1 binds only where the owning contract defines
one).
Witnesses (real PostgreSQL, lane standard): append-only enforcement,
same-tx atomicity (agent row + credential write + fence row + audit +
outbox all-or-nothing under injected failure at multiple points,
including after the credential write), fence uniqueness on
(operation, key).
Sequencing: additive DDL via the same migration path as 00180020
(hierarchy). The docs/native-kanban-sot/SHARED-CONTRACT.md §5.3 DDL
gate binds the kanban lane's audit/proposal DDL, not this lane; if a
pending operator ruling on migration sequencing changes mechanics
lane-wide, re-check before generating 0021.
## 5. Witnesses the implementation slice must ship
1. Never-echo: enroll via `intake`, assert the value string is absent
from the HTTP result, the audit row, the outbox payload, and captured
logs.
2. Sealed-store single-copy: after intake, the credential exists only in
`provider_credentials` (sealed), and `agents` has no credential
column at all.
3. Reference-resolution refusal (`precondition_failed`, no row created).
4. Harness refusals, both codes: syntactically invalid →
`validation_failed`; well-formed registry miss →
`precondition_failed` (against the live registry).
5. Idempotency (contract 3 §4.3 set): actor-bound replay returns the
recorded outcome and executes nothing (no new agent/audit/outbox
mutation rows; a replay access event is appended); payload-digest
mismatch, replay-mode mismatch, scope mismatch, and different-actor
actor-bound replay each refuse with the single bounded `conflict`
shape; a replay is re-authorized fresh (a submitter whose
authorization was revoked since the original is refused, not
replayed); a `shared` declaration on `agent.enroll` is refused
`validation_failed` with nothing executed and no fence row
recorded (seed-only rule); two concurrent same-(operation, key)
submissions produce exactly one mutation, the loser resolving
through the replay path (no unhandled unique-violation fault).
6. Same-tx atomicity fault injection (agent / credential write / fence
/ audit / outbox), including a failure injected after the intake
credential write commits its statement — everything rolls back, no
orphan credential.
7. Wizard-facing zero-mutation witness (contract 3 §6.10 shape): no
call → zero rows in `agents`/`agent_audit_events`/`agent_outbox`/
`agent_idempotency_fence` attributable to the family.
8. `is_system` injection attempt is rejected by DTO validation.
9. Correlation-id witness (contract 5 §6.3): a correlation id submitted
on `agent.enroll` appears in its audit event(s) and in the result;
the same holds for `agent.enrollment.get`'s result; the §6.3 static
companions (no `any`-typed boundary pass-through; single audit
emitter) apply. §6.3's no-existence-oracle probe: an unauthorized
`agent.enrollment.get` of an existing agent and a get of a
nonexistent id return indistinguishable results.
10. CLI-parity witness (contract 5 §6.4): a CLI smoke invocation of
`agent.enroll` and `agent.enrollment.get` against the Gateway
succeeds with the same typed results the web client receives. The
implementation slice therefore SHIPS CLI exposure for both
operations (contract 5 §4.5 — a Gateway command without CLI
exposure is a tracked conformance gap; this design refuses to open
one).
11. Fail-closed witness (contract 5 §6.5): with the owning tool or
grant state unreachable (fault injection), the operation returns
the internal-fault or authorization-refusal class and performs no
fallback read/write.
## 6. Out of scope
Wizard orchestration (M4-6); any UI (D8/D12); un-enroll/update lifecycle
(no contract requires it in v1 — the legacy write surfaces named in §2
keep serving existing consumers); OAuth login, multi-account, comms
auto-enroll, model recommendation (PRD full flow, deferred by D11);
contract amendments (F1 recorded above for a future S2 pass). CLI
exposure is explicitly IN scope (witness 10 — contract 5 §4.5 binds it).
+99
View File
@@ -0,0 +1,99 @@
# Plan — Stack Containerization (tiered deployment)
Status: DRAFT for review. Charter: fleet/lanes/stack-containerization
(brain) NORTH-STAR.md; PRD amendment in the same PR adds D15.
Supersedes nothing; sequences the absorbed M4 remainder per its lane.
## Measured baseline (origin/next @ 143ba0f5, 2026-08-30)
- `docker-compose.yml`: dev infrastructure only — postgres (pgvector),
valkey, otel-collector, jaeger. No application services.
- `docker-compose.federated.yml`: standalone overlay for the FEDERATED
storage tier (own postgres/valkey; port-conflicts the base stack by
design). Not an app deployment.
- `docker/gateway.Dockerfile`, `docker/appservice.Dockerfile`:
multi-stage production builds (node:22-alpine) EXIST; the gateway image
includes the web SPA bundle (#1444).
- CI (`publish.yml`) builds and publishes these images (next-channel
prereleases + main stable), and runs `verify:release` fail-closed.
- Gap: no stack-level composition wires gateway+appservice+data plane
into one deployable unit; no blessed install/upgrade path; no
in-container agent-runtime story for the dogfood loop.
## Target (PRD D15 amendment)
Tiered deployment, additive to the existing architecture:
1. **Standalone tier (v1 bar)**: `docker compose up` on one host brings
postgres, valkey, openbao, gateway, appservice (and the webUI the
gateway serves) to healthy; migrations apply; the webUI hosts agent
chat; an in-stack agent can read this repo and open a PR; CI
validates; the deployment adopts merged images (pull + restart).
2. **Enterprise tier (post-v1)**: Kubernetes manifests (or Helm) for the
same service set, phase-gated on the standalone bar holding.
## Phases
### Phase A — blessed standalone compose
- A1 Compose service definitions for gateway + appservice joining the
existing infra compose (profiles: `dev` keeps today's behavior;
`stack` adds the app tier), with health checks and dependency order.
- A2 Migrations on boot (or an explicit migrate step) with idempotency
and version pinning; init-db.sql folded into pg-init.
- A3 Openbao in the compose set (secret plumbing for the app tier).
- A4 `.env.example` + `mosaic.config.json` defaults documented for the
standalone mode; mode recorded per the mode-conversion contract.
- A5 Smoke: `docker compose --profile stack up` green on a scratch host;
webUI served; agent chat reachable; failures catalogued and fixed.
- Acceptance: the five-point NORTH-STAR bar measured live.
### Phase B — component completion
- Interface assumption (velma verdict A1, P5-RM-005/006): in-stack
dogfood agents inherit SEAT-GRADE identity — credential-slot
isolation, wrapper-first enforcement, no privileged coordination
identity, evidence by references that resolve outside the container
lifetime.
- Decompose JIT from A5's catalogue. Known candidates: agent runtime
bits (brain/tool access paths in-container), repo credentials for the
dogfood agent, watch/comms surfaces inside the deployment.
### Phase C — CI/CD parity
- Publish pipeline is the only image source (already true); add the
deployment-side pull/upgrade path (compose pull + migrate + restart =
next iteration); document the promotion flow next -> registry ->
deployment.
### Phase D — coordinator integration (GATED)
- Gate (velma verdict C2): blocked until the checkpoint-and-lease child
of the guides-proposed control-plane refactor — core + WU-P1-CHECKPOINT
(schema, freshness, incarnation, clean-replacement resume; D57-D60
lineage) — carries an independent target-bound PASS. Wiring restarts
against the core alone re-creates the stale-incarnation failure class
D57-D60 closed. Transitive: inherits the T108 gates (P0 exit + Jason
P1 authorization).
- Scope (velma verdict C1): lifecycle actions (start/stop/restart/
health/recovery) executed by the SHIPPED coord client over the one
typed coordination contract (request id, actor identity, epoch,
revision, lease, correlation; typed stale rejection; worker role
boundary). No second coordination interface gets designed here —
containerization consumes the coordination contract, never defines it.
### Phase E — enterprise tier
- k8s manifests/Helm for the same set; phase-gated on Phase A holding.
### Absorbed M4 remainder
- M4-3 pivot: KBN-101 foundation first (per ruling R6), then expand DDL.
- M4-5: lands inside Phase B/C where natural.
- M4-6 (composes M4-1+M4-4): last, as designed.
## Non-goals (v1)
- No Kubernetes in v1; no multi-host federation; no replacement of the
fleet's brain-based seats (the stack is an additional operator
surface); no on-host image builds for deployment (registry only).
+4
View File
@@ -13,6 +13,10 @@ status: active
- [Documentation structure README implementation](2026-08-10-docs-structure-readme.md) — completed implementation plan for the documentation contract and atlas.
- [Documentation catalog and truth audit](2026-08-10-docs-catalog-audit.md) — audit method, evidence statuses, deliverables, and acceptance criteria.
## Feature design plans
- [Agent enrollment command design](2026-08-29-agent-enrollment-command-design.md) — v1 rank-4 enrollment command family: contract composition, command surface, schema delta, witnesses (M4-4-0).
After a plan is delivered, update the canonical guide, contract, decision, or index. Do not cite a plan as proof that intended behavior shipped.
## Related
+79
View File
@@ -0,0 +1,79 @@
import { describe, it, expect, vi } from 'vitest';
import { createMissionTasksRepo } from './mission-tasks.js';
/**
* SHARED-CONTRACT §5.5 "mission_tasks.status write prohibition": this repo is
* the sole path that authors mission_tasks.status from caller input (storage
* tier migration is row transport and preserves stored values; the generic
* storage adapters have no mission_tasks caller), and it must never forward a
* caller-supplied status to the database on create or update. Callers keep
* working (the field is accepted and ignored), so these tests assert on what
* reaches the Drizzle chain, not on rejection.
*/
function makeInsertDb(returned: unknown[]) {
const values = vi.fn((_v: unknown) => ({ returning: vi.fn().mockResolvedValue(returned) }));
return { db: { insert: vi.fn(() => ({ values })) }, values };
}
function makeUpdateDb(returned: unknown[]) {
const set = vi.fn((_v: unknown) => ({
where: vi.fn(() => ({ returning: vi.fn().mockResolvedValue(returned) })),
}));
return { db: { update: vi.fn(() => ({ set })) }, set };
}
describe('createMissionTasksRepo — status write prohibition', () => {
it('create strips a caller-supplied status before insert', async () => {
const { db, values } = makeInsertDb([{ id: 'mt1', status: 'not-started' }]);
const repo = createMissionTasksRepo(db as never);
const result = await repo.create({
missionId: 'm1',
userId: 'u1',
status: 'done',
description: 'd',
} as never);
expect(values).toHaveBeenCalledTimes(1);
const inserted = values.mock.calls[0]![0] as Record<string, unknown>;
expect('status' in inserted).toBe(false);
expect(inserted.missionId).toBe('m1');
expect(inserted.description).toBe('d');
expect(result.id).toBe('mt1');
});
it('create without status still inserts (DB default applies)', async () => {
const { db, values } = makeInsertDb([{ id: 'mt2' }]);
const repo = createMissionTasksRepo(db as never);
await repo.create({ missionId: 'm1', userId: 'u1' } as never);
const inserted = values.mock.calls[0]![0] as Record<string, unknown>;
expect('status' in inserted).toBe(false);
});
it('update strips a caller-supplied status but keeps the other fields', async () => {
const { db, set } = makeUpdateDb([{ id: 'mt1', notes: 'n' }]);
const repo = createMissionTasksRepo(db as never);
const result = await repo.update('mt1', { status: 'done', notes: 'n' } as never);
expect(set).toHaveBeenCalledTimes(1);
const updated = set.mock.calls[0]![0] as Record<string, unknown>;
expect('status' in updated).toBe(false);
expect(updated.notes).toBe('n');
expect(updated.updatedAt).toBeInstanceOf(Date);
expect(result?.id).toBe('mt1');
});
it('update with only status degenerates to a timestamp-only update', async () => {
const { db, set } = makeUpdateDb([{ id: 'mt1' }]);
const repo = createMissionTasksRepo(db as never);
await repo.update('mt1', { status: 'blocked' } as never);
const updated = set.mock.calls[0]![0] as Record<string, unknown>;
expect(Object.keys(updated)).toEqual(['updatedAt']);
});
});
+20 -2
View File
@@ -3,6 +3,24 @@ import { eq, and, type Db, missionTasks } from '@mosaicstack/db';
export type MissionTask = typeof missionTasks.$inferSelect;
export type NewMissionTask = typeof missionTasks.$inferInsert;
// SHARED-CONTRACT §5.1 phase 1 / §5.4: mission_tasks.status is prohibited as a
// write source through the N-1 window. This repo is the sole path that authors
// status from caller input, so the field is stripped here — accepted and
// ignored rather than rejected, because the legacy surface is frozen with
// existing consumers kept working (tool-gateway-mapping.md §3.2). Two other
// surfaces touch the column and are deliberately NOT stripped:
// packages/storage/migrate-tier.ts copies whole rows between storage tiers and
// must preserve the stored value verbatim, and the generic table-keyed storage
// adapters register mission_tasks but have no caller that targets it (runtime
// callers use fixed collection constants). Neither authors a new status. The
// column keeps its DB default, stays declared and readable, and is retired
// only after no readers remain.
function stripStatus<T extends { status?: unknown }>(data: T): Omit<T, 'status'> {
const rest = { ...data };
delete rest.status;
return rest;
}
export function createMissionTasksRepo(db: Db) {
return {
async findByMission(missionId: string): Promise<MissionTask[]> {
@@ -30,14 +48,14 @@ export function createMissionTasksRepo(db: Db) {
},
async create(data: NewMissionTask): Promise<MissionTask> {
const rows = await db.insert(missionTasks).values(data).returning();
const rows = await db.insert(missionTasks).values(stripStatus(data)).returning();
return rows[0]!;
},
async update(id: string, data: Partial<NewMissionTask>): Promise<MissionTask | undefined> {
const rows = await db
.update(missionTasks)
.set({ ...data, updatedAt: new Date() })
.set({ ...stripStatus(data), updatedAt: new Date() })
.where(eq(missionTasks.id, id))
.returning();
return rows[0];
@@ -0,0 +1,47 @@
CREATE TYPE "public"."agent_outbox_status" AS ENUM('pending', 'processing', 'delivered');--> statement-breakpoint
CREATE TABLE "agent_audit_events" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"seq" bigint GENERATED ALWAYS AS IDENTITY (sequence name "agent_audit_events_seq_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
"event_type" text NOT NULL,
"actor_id" text NOT NULL,
"agent_id" uuid NOT NULL,
"correlation_id" text NOT NULL,
"causation_id" uuid,
"payload" jsonb NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "agent_audit_events_type_check" CHECK (event_type IN ('agent.enrolled', 'agent.enrollment.replayed'))
);
--> statement-breakpoint
CREATE TABLE "agent_idempotency_fence" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"operation" text NOT NULL,
"idempotency_key" text NOT NULL,
"actor_id" text NOT NULL,
"authorization_scope" text NOT NULL,
"payload_digest" text NOT NULL,
"replay_mode" text DEFAULT 'actor-bound' NOT NULL,
"outcome_agent_id" uuid NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "agent_idempotency_fence_replay_mode_check" CHECK (replay_mode IN ('actor-bound', 'shared'))
);
--> statement-breakpoint
CREATE TABLE "agent_outbox" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"event_id" uuid NOT NULL,
"correlation_id" text NOT NULL,
"status" "agent_outbox_status" DEFAULT 'pending' NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
"delivered_at" timestamp with time zone
);
--> statement-breakpoint
ALTER TABLE "agents" ADD COLUMN "harness" text;--> statement-breakpoint
ALTER TABLE "agents" ADD COLUMN "enrolled_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "agent_audit_events" ADD CONSTRAINT "agent_audit_events_causation_id_agent_audit_events_id_fk" FOREIGN KEY ("causation_id") REFERENCES "public"."agent_audit_events"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "agent_outbox" ADD CONSTRAINT "agent_outbox_event_id_agent_audit_events_id_fk" FOREIGN KEY ("event_id") REFERENCES "public"."agent_audit_events"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "agent_audit_events_seq_idx" ON "agent_audit_events" USING btree ("seq");--> statement-breakpoint
CREATE INDEX "agent_audit_events_agent_seq_idx" ON "agent_audit_events" USING btree ("agent_id","seq");--> statement-breakpoint
CREATE INDEX "agent_audit_events_correlation_idx" ON "agent_audit_events" USING btree ("correlation_id");--> statement-breakpoint
CREATE UNIQUE INDEX "agent_idempotency_fence_operation_key_idx" ON "agent_idempotency_fence" USING btree ("operation","idempotency_key");--> statement-breakpoint
CREATE UNIQUE INDEX "agent_outbox_event_idx" ON "agent_outbox" USING btree ("event_id");--> statement-breakpoint
CREATE INDEX "agent_outbox_status_created_idx" ON "agent_outbox" USING btree ("status","created_at");
File diff suppressed because it is too large Load Diff
+7
View File
@@ -148,6 +148,13 @@
"when": 1787963521142,
"tag": "0020_special_betty_brant",
"breakpoints": true
},
{
"idx": 21,
"version": "7",
"when": 1788053011351,
"tag": "0021_agent_enrollment",
"breakpoints": true
}
]
}
@@ -0,0 +1,412 @@
/**
* Agent enrollment schema witnesses — M4-4a, the schema-level half of the
* witness list in docs/plans/2026-08-29-agent-enrollment-command-design.md §5.
*
* Witnesses the guarantees migration 0021's tables themselves carry: the
* event-type CHECK, monotonic per-agent append order (`seq`), deletion-safe
* linkage (no foreign key from the events or fence tables into `agents` —
* rows survive a legacy CRUD DELETE of the agent), the causation self-FK,
* the outbox's FK/uniqueness/status shape, the fence's UNIQUE
* (operation, key) and replay-mode CHECK, and the nullable enrollment
* columns on `agents` (legacy rows insert without them). The command-level
* witnesses (never-echo, same-tx atomicity, replay semantics, correlation,
* CLI parity, fail-closed) belong to the M4-4b implementation slice.
*
* Two legs run the same witness body:
* - PGlite (WASM Postgres): always runs.
* - Real PostgreSQL: runs when DATABASE_URL is set — the binding witness;
* CI migrates ci-postgres before `pnpm test`.
*/
import { randomUUID } from 'node:crypto';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { sql } from 'drizzle-orm';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { createDb } from './client.js';
import { createPgliteDb } from './client-pglite.js';
import { runPgliteMigrations } from './migrate.js';
import { agentAuditEvents, agentIdempotencyFence, agentOutbox, agents } from './schema.js';
type AnyDb = {
db: {
insert: (t: unknown) => { values: (v: unknown) => Promise<unknown> };
execute: (q: unknown) => Promise<{ rows?: unknown[] } | unknown[]>;
};
close: () => Promise<void>;
};
/** Match a constraint failure anywhere along drizzle's cause chain. */
async function expectViolation(p: Promise<unknown>, re: RegExp, label = ''): Promise<void> {
let err: unknown;
try {
await p;
} catch (e) {
err = e;
}
expect(err, label || 'expected the statement to be refused').toBeDefined();
const messages: string[] = [];
let cur: unknown = err;
while (cur instanceof Error) {
messages.push(cur.message);
cur = (cur as { cause?: unknown }).cause;
}
expect(messages.join(' | '), label).toMatch(re);
}
function rows(res: { rows?: unknown[] } | unknown[]): Record<string, unknown>[] {
return (Array.isArray(res) ? res : (res.rows ?? [])) as Record<string, unknown>[];
}
/** Unique per-run prefix so real-PG runs never collide and clean up safely. */
const T = `agent-e-${randomUUID().slice(0, 8)}`;
type EventInsert = typeof agentAuditEvents.$inferInsert;
function eventRow(overrides: Partial<EventInsert> = {}): EventInsert {
return {
eventType: 'agent.enrolled',
actorId: `${T}-actor`,
agentId: randomUUID(),
correlationId: `${T}-corr-${randomUUID()}`,
payload: {
harness: 'claude-code',
provider: 'anthropic',
name: 'x',
credentialMode: 'reference',
},
...overrides,
};
}
type FenceInsert = typeof agentIdempotencyFence.$inferInsert;
function fenceRow(overrides: Partial<FenceInsert> = {}): FenceInsert {
return {
operation: 'agent.enroll',
idempotencyKey: `${T}-${randomUUID()}`,
actorId: `${T}-actor`,
authorizationScope: 'platform-user',
payloadDigest: `${T}-digest`,
outcomeAgentId: randomUUID(),
...overrides,
};
}
function witnessSuite(getHandle: () => AnyDb): void {
const db = () => getHandle().db as unknown as ReturnType<typeof createDb>['db'];
afterAll(async () => {
const d = db();
await d.execute(sql`DELETE FROM agent_outbox WHERE correlation_id LIKE ${T + '%'}`);
// Caused events first: the causation self-FK is RESTRICT.
await d.execute(
sql`DELETE FROM agent_audit_events WHERE correlation_id LIKE ${T + '%'} AND causation_id IS NOT NULL`,
);
await d.execute(sql`DELETE FROM agent_audit_events WHERE correlation_id LIKE ${T + '%'}`);
await d.execute(sql`DELETE FROM agent_idempotency_fence WHERE actor_id LIKE ${T + '%'}`);
await d.execute(sql`DELETE FROM agents WHERE name LIKE ${T + '%'}`);
});
// ── agents: nullable enrollment columns (no backfill semantics) ────────────
it('legacy agent rows insert without enrollment columns; enrolled rows carry both', async () => {
const legacyId = randomUUID();
await db()
.insert(agents)
.values({
id: legacyId,
name: `${T}-legacy`,
provider: 'anthropic',
model: 'claude-fable-5',
});
const legacy = rows(
await db().execute(sql`SELECT harness, enrolled_at FROM agents WHERE id = ${legacyId}`),
)[0]!;
expect(legacy['harness']).toBeNull();
expect(legacy['enrolled_at']).toBeNull();
const enrolledId = randomUUID();
await db()
.insert(agents)
.values({
id: enrolledId,
name: `${T}-enrolled`,
provider: 'anthropic',
model: 'claude-fable-5',
harness: 'claude-code',
enrolledAt: new Date(),
});
const enrolled = rows(
await db().execute(sql`SELECT harness, enrolled_at FROM agents WHERE id = ${enrolledId}`),
)[0]!;
expect(enrolled['harness']).toBe('claude-code');
expect(enrolled['enrolled_at']).not.toBeNull();
});
// ── agent_audit_events: CHECK, ordering, deletion-safe linkage ─────────────
it('accepts both declared event types and refuses an undeclared one', async () => {
await db()
.insert(agentAuditEvents)
.values(eventRow({ eventType: 'agent.enrolled' }));
await db()
.insert(agentAuditEvents)
.values(eventRow({ eventType: 'agent.enrollment.replayed' }));
await expectViolation(
db()
.insert(agentAuditEvents)
.values(eventRow({ eventType: 'agent.deleted' })),
/type_check|violates check/i,
'undeclared event type must be refused',
);
});
it('assigns strictly increasing seq in insert order for one agent', async () => {
const agentId = randomUUID();
const c1 = `${T}-seq-1-${randomUUID()}`;
const c2 = `${T}-seq-2-${randomUUID()}`;
await db()
.insert(agentAuditEvents)
.values(eventRow({ agentId, correlationId: c1 }));
await db()
.insert(agentAuditEvents)
.values(eventRow({ agentId, eventType: 'agent.enrollment.replayed', correlationId: c2 }));
const res = rows(
await db().execute(
sql`SELECT correlation_id, seq FROM agent_audit_events WHERE agent_id = ${agentId} ORDER BY seq ASC`,
),
);
expect(res.map((r) => r['correlation_id'])).toEqual([c1, c2]);
expect(Number(res[1]!['seq'])).toBeGreaterThan(Number(res[0]!['seq']));
});
it('has no foreign key into agents, and events survive agent deletion', async () => {
const fks = rows(
await db().execute(sql`
SELECT ccu.table_name AS referenced_table
FROM information_schema.table_constraints tc
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name AND ccu.constraint_schema = tc.constraint_schema
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = 'agent_audit_events'
`),
);
// The causation self-FK is the ONLY foreign key on the events table.
expect([...new Set(fks.map((r) => r['referenced_table']))]).toEqual(['agent_audit_events']);
const agentId = randomUUID();
await db()
.insert(agents)
.values({ id: agentId, name: `${T}-doomed`, provider: 'anthropic', model: 'claude-fable-5' });
const corr = `${T}-survive-${randomUUID()}`;
await db()
.insert(agentAuditEvents)
.values(eventRow({ agentId, correlationId: corr }));
await db().execute(sql`DELETE FROM agents WHERE id = ${agentId}`);
const after = rows(
await db().execute(
sql`SELECT agent_id FROM agent_audit_events WHERE correlation_id = ${corr}`,
),
);
expect(after).toHaveLength(1);
expect(after[0]!['agent_id']).toBe(agentId);
});
it('enforces the causation self-FK and RESTRICTs deleting a cause', async () => {
await expectViolation(
db()
.insert(agentAuditEvents)
.values(eventRow({ causationId: randomUUID() })),
/foreign key/i,
'causation must reference an existing event',
);
const causeCorr = `${T}-cause-${randomUUID()}`;
await db()
.insert(agentAuditEvents)
.values(eventRow({ correlationId: causeCorr }));
const cause = rows(
await db().execute(
sql`SELECT id FROM agent_audit_events WHERE correlation_id = ${causeCorr}`,
),
)[0]!;
await db()
.insert(agentAuditEvents)
.values(
eventRow({
eventType: 'agent.enrollment.replayed',
causationId: cause['id'] as string,
}),
);
await expectViolation(
db().execute(sql`DELETE FROM agent_audit_events WHERE id = ${cause['id'] as string}`),
/foreign key/i,
'a cause with dependent events must not be deletable',
);
});
// ── agent_outbox shape ─────────────────────────────────────────────────────
it('outbox rows require an existing event, one outbox row per event, closed status enum', async () => {
await expectViolation(
db()
.insert(agentOutbox)
.values({ eventId: randomUUID(), correlationId: `${T}-corr` }),
/foreign key/i,
'outbox must reference an existing event',
);
const corr = `${T}-ob-${randomUUID()}`;
await db()
.insert(agentAuditEvents)
.values(eventRow({ correlationId: corr }));
const event = rows(
await db().execute(sql`SELECT id FROM agent_audit_events WHERE correlation_id = ${corr}`),
)[0]!;
const eventId = event['id'] as string;
await db().insert(agentOutbox).values({ eventId, correlationId: corr });
await expectViolation(
db()
.insert(agentOutbox)
.values({ eventId, correlationId: `${T}-ob2` }),
/duplicate key|unique/i,
'one outbox record per event',
);
await expectViolation(
db().execute(
sql`INSERT INTO agent_outbox (event_id, correlation_id, status)
VALUES (${eventId}, ${`${T}-ob3`}, 'failed')`,
),
/invalid input value for enum|22P02/i,
'status outside pending/processing/delivered must be refused',
);
});
it('outbox FK RESTRICTs event deletion while the outbox row exists', async () => {
const corr = `${T}-obr-${randomUUID()}`;
await db()
.insert(agentAuditEvents)
.values(eventRow({ correlationId: corr }));
const event = rows(
await db().execute(sql`SELECT id FROM agent_audit_events WHERE correlation_id = ${corr}`),
)[0]!;
await db()
.insert(agentOutbox)
.values({ eventId: event['id'] as string, correlationId: corr });
await expectViolation(
db().execute(sql`DELETE FROM agent_audit_events WHERE id = ${event['id'] as string}`),
/foreign key/i,
);
});
// ── agent_idempotency_fence: (operation, key) uniqueness, mode CHECK ───────
it('refuses a duplicate (operation, key) pair but allows the same key under another operation', async () => {
const key = `${T}-fence-${randomUUID()}`;
await db()
.insert(agentIdempotencyFence)
.values(fenceRow({ idempotencyKey: key }));
await expectViolation(
db()
.insert(agentIdempotencyFence)
.values(fenceRow({ idempotencyKey: key })),
/duplicate key|unique/i,
'fence uniqueness is (operation, key)',
);
// Same key, different operation identifier: a distinct fence.
await db()
.insert(agentIdempotencyFence)
.values(fenceRow({ idempotencyKey: key, operation: 'agent.other' }));
});
it('defaults replay mode to actor-bound and refuses an undeclared mode', async () => {
const key = `${T}-mode-${randomUUID()}`;
await db()
.insert(agentIdempotencyFence)
.values(fenceRow({ idempotencyKey: key }));
const row = rows(
await db().execute(
sql`SELECT replay_mode FROM agent_idempotency_fence WHERE idempotency_key = ${key}`,
),
)[0]!;
expect(row['replay_mode']).toBe('actor-bound');
await expectViolation(
db()
.insert(agentIdempotencyFence)
.values(fenceRow({ replayMode: 'unbound' as 'actor-bound' })),
/replay_mode_check|violates check/i,
'a mode outside actor-bound/shared must be refused',
);
});
it('fence has no foreign key at all, and rows survive agent deletion', async () => {
const fks = rows(
await db().execute(sql`
SELECT ccu.table_name AS referenced_table
FROM information_schema.table_constraints tc
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name AND ccu.constraint_schema = tc.constraint_schema
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = 'agent_idempotency_fence'
`),
);
expect(fks).toHaveLength(0);
const agentId = randomUUID();
await db()
.insert(agents)
.values({
id: agentId,
name: `${T}-fdoomed`,
provider: 'anthropic',
model: 'claude-fable-5',
});
const key = `${T}-fsurvive-${randomUUID()}`;
await db()
.insert(agentIdempotencyFence)
.values(fenceRow({ idempotencyKey: key, outcomeAgentId: agentId }));
await db().execute(sql`DELETE FROM agents WHERE id = ${agentId}`);
const after = rows(
await db().execute(
sql`SELECT outcome_agent_id FROM agent_idempotency_fence WHERE idempotency_key = ${key}`,
),
);
expect(after).toHaveLength(1);
expect(after[0]!['outcome_agent_id']).toBe(agentId);
});
}
// ── Leg 1: PGlite (always runs — local witness signal) ───────────────────────
describe('agent enrollment schema witnesses — PGlite', () => {
let dir: string;
let handle: ReturnType<typeof createPgliteDb>;
beforeAll(async () => {
dir = mkdtempSync(join(tmpdir(), 'agent-enroll-witness-'));
handle = createPgliteDb(dir);
await runPgliteMigrations(handle);
});
afterAll(async () => {
await handle.close();
rmSync(dir, { recursive: true, force: true });
});
witnessSuite(() => handle as unknown as AnyDb);
});
// ── Leg 2: real PostgreSQL (binding witness, ci-postgres in CI) ──────────────
const hasPostgres = Boolean(process.env['DATABASE_URL']);
describe.skipIf(!hasPostgres)('agent enrollment schema witnesses — real PostgreSQL', () => {
let handle: ReturnType<typeof createDb>;
beforeAll(() => {
handle = createDb(process.env['DATABASE_URL']!);
});
afterAll(async () => {
await handle.close();
});
witnessSuite(() => handle as unknown as AnyDb);
});
+111
View File
@@ -302,6 +302,11 @@ export const agents = pgTable(
skills: jsonb('skills').$type<string[]>(),
isSystem: boolean('is_system').notNull().default(false),
config: jsonb('config'),
// Enrollment (M4-4, docs/plans/2026-08-29-agent-enrollment-command-design.md §4).
// NULL on both marks a legacy (non-enrolled) row; no backfill — enrollment
// is a fact the rank-4 command creates, not one to invent for existing rows.
harness: text('harness'),
enrolledAt: timestamp('enrolled_at', { withTimezone: true }),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
},
@@ -1279,3 +1284,109 @@ export const hierarchyOutbox = pgTable(
index('hierarchy_outbox_status_created_idx').on(t.status, t.createdAt),
],
);
// ---------------------------------------------------------------------------
// Agent enrollment (M4-4) — rank-4 command family audit/outbox/fence stores.
// Design: docs/plans/2026-08-29-agent-enrollment-command-design.md §4.
// Pattern reuse from the hierarchy audit/outbox pair, separate store. Audit
// rows reference the agent by snapshot id, deliberately with NO FK, so audit
// history survives agent deletion through the legacy CRUD DELETE path.
// Idempotency for this family lives in agent_idempotency_fence (contract 3
// §4.3 envelope, ratified into contract 5 §4 via contract 3 §7 item 4) — the
// audit and outbox tables carry no idempotency key of their own.
export const AGENT_AUDIT_EVENT_TYPES = [
// Semantic mutation event of agent.enroll.
'agent.enrolled',
// Non-mutation access class: a passing idempotent replay appends this and
// nothing else (accessing principal, current correlation id, fence-row
// reference in the payload).
'agent.enrollment.replayed',
] as const;
export const agentAuditEvents = pgTable(
'agent_audit_events',
{
id: uuid('id').primaryKey().defaultRandom(),
// Global append order; per-agent ordering is a filter on agent_id ordered
// by seq.
seq: bigint('seq', { mode: 'number' }).notNull().generatedAlwaysAsIdentity(),
eventType: text('event_type').notNull(),
// No FK: audit events outlive every principal and every target.
actorId: text('actor_id').notNull(),
agentId: uuid('agent_id').notNull(),
correlationId: text('correlation_id').notNull(),
causationId: uuid('causation_id').references((): AnyPgColumn => agentAuditEvents.id, {
onDelete: 'restrict',
}),
// Immutable snapshot at event time; never carries credential material
// (§3.1 rule 1: actor, agent id, harness, provider, name, credentialMode).
payload: jsonb('payload').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
},
(t) => [
uniqueIndex('agent_audit_events_seq_idx').on(t.seq),
index('agent_audit_events_agent_seq_idx').on(t.agentId, t.seq),
index('agent_audit_events_correlation_idx').on(t.correlationId),
check(
'agent_audit_events_type_check',
sql`event_type IN ('agent.enrolled', 'agent.enrollment.replayed')`,
),
],
);
export const agentOutboxStatusEnum = pgEnum('agent_outbox_status', [
'pending',
'processing',
'delivered',
]);
export const agentOutbox = pgTable(
'agent_outbox',
{
id: uuid('id').primaryKey().defaultRandom(),
// FK into the append-only events table: never dangles, RESTRICT is safe.
eventId: uuid('event_id')
.notNull()
.references(() => agentAuditEvents.id, { onDelete: 'restrict' }),
correlationId: text('correlation_id').notNull(),
status: agentOutboxStatusEnum('status').notNull().default('pending'),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
deliveredAt: timestamp('delivered_at', { withTimezone: true }),
},
(t) => [
uniqueIndex('agent_outbox_event_idx').on(t.eventId),
index('agent_outbox_status_created_idx').on(t.status, t.createdAt),
],
);
// Contract 3 §4.3 fence shape. Uniqueness is (operation, key); the recorded
// replay mode is always 'actor-bound' for this family (`shared` is seed-only
// and refused at validation — design §3.1), but the column keeps the ratified
// envelope shape and serves the mode-mismatch collision check. The payload
// digest input EXCLUDES the credential value (design §3.1 rule 5).
export const agentIdempotencyFence = pgTable(
'agent_idempotency_fence',
{
id: uuid('id').primaryKey().defaultRandom(),
operation: text('operation').notNull(),
idempotencyKey: text('idempotency_key').notNull(),
// No FK: fence rows outlive principals, mirroring the audit tables.
actorId: text('actor_id').notNull(),
authorizationScope: text('authorization_scope').notNull(),
payloadDigest: text('payload_digest').notNull(),
replayMode: text('replay_mode').notNull().default('actor-bound'),
// Committed-outcome reference (the enrolled agent's id). Snapshot value,
// no FK: the fence must keep answering replays after a legacy DELETE.
outcomeAgentId: uuid('outcome_agent_id').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
},
(t) => [
uniqueIndex('agent_idempotency_fence_operation_key_idx').on(t.operation, t.idempotencyKey),
check(
'agent_idempotency_fence_replay_mode_check',
sql`replay_mode IN ('actor-bound', 'shared')`,
),
],
);
+150
View File
@@ -0,0 +1,150 @@
#!/usr/bin/env bash
# mosaic — fleet launcher (shipped-first, split-home safe).
#
# T110 / P5-RM-009 stack side. Carries the T106 brain launcher contract
# (shipped-first, worktree dev opt-in, OFF pass-through, typed failure) with
# one split-home correction: the SHIPPED npm mosaic is resolved from the real
# user's home (passwd database), never from $HOME. Under split-home seat
# layouts HOME is a seat home: it carries no npm prefix, and a
# $HOME/.npm-global there would be a plantable descriptor, so the $HOME
# candidate is consulted only when the passwd lookup itself fails, and then
# only with a full symlink-component refusal (secure descriptor traversal).
#
# Contract:
# 1. SHIPPED npm mosaic is the default. Candidate order:
# a. <real-home>/.npm-global/bin/mosaic — real home from the passwd
# database. The final component may be npm's own bin symlink into
# lib/node_modules; that indirection is npm's layout, not a plant.
# b. $HOME/.npm-global/bin/mosaic — ONLY when the passwd lookup
# fails, and then only when the candidate is a trusted-shape
# absolute path: relative HOME and parent-escape (..) components
# are refused outright, and every remaining component must be a
# non-symlink (secure descriptor traversal). Refused candidates
# are never executed.
# 2. Worktree build is DEV OPT-IN: used only when MOSAIC_CLI_WORKTREE is
# explicitly set. Health-checked via --version; ANY doubt (absent,
# unreadable, or failing) falls back to the shipped npm mosaic with a
# warning on stderr. With no environment set, worktree candidates are
# never consulted — stale worktree builds cannot regain precedence.
# 3. MOSAIC_FLEET_CLI_OFF keeps its pass-through semantics: set (any
# value) forces pure pass-through. The dev path is not consulted even
# when MOSAIC_CLI_WORKTREE is also set.
# 4. Typed failure: with no runnable candidate the launcher prints one
# stderr line naming what was checked and exits 127.
# 5. NEVER writes to the mosaic home or the npm prefix. Deployment to the
# fleet goes through the real channel (PR to next -> mosaic update).
#
# Env:
# MOSAIC_CLI_WORKTREE dev opt-in: path to a stack worktree whose
# packages/mosaic/dist/cli.js is used (health-checked,
# shipped fallback on doubt)
# MOSAIC_FLEET_CLI_OFF set (any value) to force pure pass-through
#
# Component walk note: the descriptor guard splits on "/" without quoting so
# multi-byte HOME paths with spaces are not supported for the FALLBACK
# candidate; the passwd candidate needs no walk (trusted derivation).
set -u
fail() {
echo "mosaic: $*" >&2
exit 127
}
# Real user home from the passwd database (HOME-independent).
real_home() {
getent passwd "$(id -u)" 2>/dev/null | cut -d: -f6
}
# True when any component of an ABSOLUTE candidate path is a symlink. Only
# ever called after fallback_candidate_usable's absolute-shape check.
path_has_symlink_component() {
local path="$1" dir base acc="" part
dir="$(dirname -- "$path")"
base="$(basename -- "$path")"
local IFS='/'
for part in $dir; do
acc="$acc/$part"
[ -L "$acc" ] && return 0
done
[ -L "$dir/$base" ] && return 0
return 1
}
# Reject the untrusted $HOME fallback candidate unless it is a trusted-shape
# absolute path: absolute, no parent-escape (..) components, and no symlink
# components anywhere on the path. Every rejection is named on stderr so the
# typed failure explains itself. This is the launcher's descriptor guard; the
# suite's mutation control (guard bypassed) must plant-exec, proving the guard
# is what stands between a hostile HOME and code execution.
fallback_candidate_usable() {
local candidate="$1"
case "$candidate" in
/*) ;;
*)
echo "mosaic: refusing \$HOME candidate $candidate: relative path is untrusted without a passwd home" >&2
return 1
;;
esac
if printf '%s' "$candidate" | grep -qE '(^|/)\.\.(/|$)'; then
echo "mosaic: refusing \$HOME candidate $candidate: parent-escape component" >&2
return 1
fi
if path_has_symlink_component "$candidate"; then
echo "mosaic: refusing \$HOME candidate $candidate: symlink component (untrusted without a passwd home)" >&2
return 1
fi
return 0
}
# Print shipped candidates in contract order. Refusals are reported on stderr
# so the typed failure names the cause.
shipped_candidates() {
local rh home_candidate
rh="$(real_home)"
if [ -n "$rh" ]; then
printf '%s\n' "$rh/.npm-global/bin/mosaic"
return 0
fi
# passwd lookup failed: the only fallback is $HOME, descriptor-guarded.
if [ -n "${HOME:-}" ]; then
home_candidate="$HOME/.npm-global/bin/mosaic"
if fallback_candidate_usable "$home_candidate"; then
printf '%s\n' "$home_candidate"
fi
fi
return 0
}
resolve_shipped() {
local candidate
while IFS= read -r candidate; do
[ -n "$candidate" ] || continue
if [ -x "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
done < <(shipped_candidates)
return 1
}
# Dev opt-in only: an explicit MOSAIC_CLI_WORKTREE reaches the worktree build,
# and pure pass-through (MOSAIC_FLEET_CLI_OFF) outranks it.
if [ -n "${MOSAIC_CLI_WORKTREE:-}" ] && [ -z "${MOSAIC_FLEET_CLI_OFF:-}" ]; then
CLI="$MOSAIC_CLI_WORKTREE/packages/mosaic/dist/cli.js"
if [ -r "$CLI" ]; then
if v="$(node "$CLI" --version 2>/dev/null)" && [ -n "$v" ]; then
exec node "$CLI" "$@"
fi
echo "mosaic: worktree build at $CLI failed its health check; using shipped npm mosaic" >&2
else
echo "mosaic: worktree build at $CLI absent or unreadable; using shipped npm mosaic" >&2
fi
fi
SHIPPED="$(resolve_shipped)" || true
if [ -n "${SHIPPED:-}" ]; then
exec "$SHIPPED" "$@"
fi
fail "no runnable CLI (shipped npm mosaic absent from the passwd-home npm prefix and \$HOME; worktree build requires MOSAIC_CLI_WORKTREE)"
+193
View File
@@ -0,0 +1,193 @@
#!/usr/bin/env bash
# Hermetic suite for the fleet/bin/mosaic launcher (T110 / P5-RM-009).
#
# Arms cover the plan acceptance: split-home shipped-first positive, typed
# failure on missing candidates, stale-worktree non-precedence, OFF
# pass-through, and secure-descriptor refusal on the untrusted $HOME
# fallback. No network, no real npm install, no node package build: the
# "shipped mosaic" is a stub script and getent is PATH-stubbed (set
# GETENT_STUB=fail to make the passwd lookup fail, exercising the guarded
# $HOME fallback).
set -euo pipefail
SCRIPT_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
LAUNCHER="$SCRIPT_DIR/mosaic"
fail() {
echo "FAIL: $*" >&2
exit 1
}
[ -f "$LAUNCHER" ] || fail "missing launcher"
[ -x "$LAUNCHER" ] || fail "launcher is not executable"
bash -n "$LAUNCHER" || fail "launcher fails bash -n"
WORK=$(mktemp -d)
cleanup() { rm -rf "$WORK"; }
trap cleanup EXIT
REAL_HOME="$WORK/real-home"
SEAT_HOME="$WORK/seat-home"
STUB_BIN="$WORK/stub-bin"
mkdir -p "$REAL_HOME/.npm-global/bin" "$SEAT_HOME" "$STUB_BIN"
cat >"$REAL_HOME/.npm-global/bin/mosaic" <<'SH'
#!/bin/sh
echo "0.0.0-shipped-stub"
SH
chmod +x "$REAL_HOME/.npm-global/bin/mosaic"
# PATH-stubbed getent: reports the real home for the current uid, unless
# GETENT_STUB=fail is in the launcher environment (exercises the guarded
# $HOME fallback path).
cat >"$STUB_BIN/getent" <<SH
#!/bin/sh
if [ "\${GETENT_STUB:-}" = "fail" ]; then exit 2; fi
if [ "\$1" = "passwd" ]; then
echo "stub:x:$(id -u):$(id -g):stub:$REAL_HOME:/bin/sh"
exit 0
fi
exit 2
SH
chmod +x "$STUB_BIN/getent"
run_launcher() { # run_launcher <home> [VAR=value ...] -- [args...]
local home="$1"; shift
[ "${1:-}" = "--" ] && shift
env -i PATH="$STUB_BIN:/usr/bin:/bin" HOME="$home" TERM="${TERM:-dumb}" "$LAUNCHER" "$@"
}
# A1 — acceptance 1: split-home positive. HOME is an empty seat home; the
# shipped mosaic resolves through the passwd real home.
out="$(printf '' | run_launcher "$SEAT_HOME" -- --version)"
[ "$out" = "0.0.0-shipped-stub" ] || fail "A1 split-home positive: got '$out', want shipped stub version"
# A3 — acceptance 3: a stale worktree build is NEVER consulted without the
# explicit opt-in, even when a worktree exists on disk.
WT="$WORK/stale-wt"
mkdir -p "$WT/packages/mosaic/dist"
printf 'console.log("0.0.0-stale-worktree")\n' >"$WT/packages/mosaic/dist/cli.js"
out="$(printf '' | run_launcher "$SEAT_HOME" -- --version)"
[ "$out" = "0.0.0-shipped-stub" ] || fail "A3 stale worktree regained precedence without opt-in: got '$out'"
# A2 (opt-in healthy) — explicit MOSAIC_CLI_WORKTREE reaches the worktree.
out="$(printf '' | env MOSAIC_CLI_WORKTREE="$WT" HOME="$SEAT_HOME" PATH="$STUB_BIN:/usr/bin:/bin" "$LAUNCHER" --version)"
[ "$out" = "0.0.0-stale-worktree" ] || fail "A2 opt-in worktree not used: got '$out'"
# A2b (opt-in unhealthy) — absent dist falls back to shipped with a warning.
out2="$(printf '' | env MOSAIC_CLI_WORKTREE="$WORK/empty-wt" HOME="$SEAT_HOME" PATH="$STUB_BIN:/usr/bin:/bin" "$LAUNCHER" --version 2>/dev/null)"
[ "$out2" = "0.0.0-shipped-stub" ] || fail "A2b unhealthy worktree fallback output: '$out2'"
err2="$(printf '' | env MOSAIC_CLI_WORKTREE="$WORK/empty-wt" HOME="$SEAT_HOME" PATH="$STUB_BIN:/usr/bin:/bin" "$LAUNCHER" --version 2>&1 >/dev/null)"
case "$err2" in *"absent or unreadable"*|*"health check"*) ;; *) fail "A2b unhealthy worktree fallback warning missing: '$err2'" ;; esac
# A4 — OFF pass-through: worktree opt-in is ignored when OFF is set.
out="$(printf '' | env MOSAIC_FLEET_CLI_OFF=1 MOSAIC_CLI_WORKTREE="$WT" HOME="$SEAT_HOME" PATH="$STUB_BIN:/usr/bin:/bin" "$LAUNCHER" --version)"
[ "$out" = "0.0.0-shipped-stub" ] || fail "A4 OFF did not force pass-through: got '$out'"
# A5 — acceptance 4: typed failure when no candidate exists (passwd lookup
# fails, seat home carries no npm prefix). Expect 127 + documented message.
set +e
err="$(printf '' | env GETENT_STUB=fail HOME="$SEAT_HOME" PATH="$STUB_BIN:/usr/bin:/bin" "$LAUNCHER" --version 2>&1 >/dev/null)"
rc=$?
set -e
[ "$rc" = "127" ] || fail "A5 typed failure rc: got $rc, want 127"
case "$err" in *"no runnable CLI"*) ;; *) fail "A5 typed failure message missing: '$err'" ;; esac
# A6 — secure descriptor traversal, ABSOLUTE symlink plant (corrected per
# rev-code-02 B3: the symlink points at $PLANT/.npm-global so the candidate
# resolves EXACTLY to the planted executable). passwd lookup fails and a
# symlink-planted $HOME/.npm-global is refused without execution.
PLANT="$WORK/planted-target"
mkdir -p "$PLANT/.npm-global/bin"
cat >"$PLANT/.npm-global/bin/mosaic" <<SH
#!/bin/sh
touch "$WORK/planted-sentinel"
echo "0.0.0-planted"
SH
chmod +x "$PLANT/.npm-global/bin/mosaic"
ln -s "$PLANT/.npm-global" "$SEAT_HOME/.npm-global"
set +e
err="$(printf '' | env GETENT_STUB=fail HOME="$SEAT_HOME" PATH="$STUB_BIN:/usr/bin:/bin" "$LAUNCHER" --version 2>&1)"
rc=$?
set -e
[ "$rc" = "127" ] || fail "A6 planted descriptor was followed (rc $rc, out '$err')"
case "$err" in *"symlink component"*) ;; *) fail "A6 refusal diagnostic missing: '$err'" ;; esac
[ ! -e "$WORK/planted-sentinel" ] || fail "A6 planted mosaic EXECUTED"
# A6b — mutation control (rev-code-02 B3): a copy of the launcher with the
# descriptor guard bypassed MUST execute the plant under the identical hostile
# arm. If the mutant stays clean, the plant path is wrong and A6 proves
# nothing.
MUTANT="$WORK/mutant-mosaic"
sed 's/if fallback_candidate_usable "\$home_candidate"; then/if true; then/' "$LAUNCHER" >"$MUTANT"
chmod +x "$MUTANT"
[ "$(grep -c 'if true; then' "$MUTANT")" -eq 1 ] || fail "A6b mutant not created (guard call not replaced)"
set +e
mout="$(printf '' | env GETENT_STUB=fail HOME="$SEAT_HOME" PATH="$STUB_BIN:/usr/bin:/bin" "$MUTANT" --version 2>&1)"
mrc=$?
set -e
[ "$mrc" = "0" ] || fail "A6b mutant did not execute the plant (rc $mrc, out '$mout') - A6 proves nothing"
[ -e "$WORK/planted-sentinel" ] || fail "A6b mutant ran but sentinel absent - plant path wrong, A6 proves nothing"
# A7 — relative-HOME hostile arm (rev-code-02 B2): a relative HOME whose name
# is a symlink in the launcher CWD must be refused outright, never resolved
# against the working directory.
CWD_SANDBOX="$WORK/cwd-sandbox"
REL_PLANT="$WORK/relative-plant"
mkdir -p "$CWD_SANDBOX" "$REL_PLANT/.npm-global/bin"
cat >"$REL_PLANT/.npm-global/bin/mosaic" <<SH
#!/bin/sh
touch "$WORK/relative-sentinel"
echo "0.0.0-relative-planted"
SH
chmod +x "$REL_PLANT/.npm-global/bin/mosaic"
ln -s "$REL_PLANT" "$CWD_SANDBOX/relative-home"
set +e
rout="$(cd "$CWD_SANDBOX" && printf '' | env GETENT_STUB=fail HOME="relative-home" PATH="$STUB_BIN:/usr/bin:/bin" "$LAUNCHER" --version 2>&1)"
rrc=$?
set -e
[ "$rrc" = "127" ] || fail "A7 relative HOME was followed (rc $rrc, out '$rout')"
case "$rout" in *"relative path"*) ;; *) fail "A7 relative-refusal diagnostic missing: '$rout'" ;; esac
[ ! -e "$WORK/relative-sentinel" ] || fail "A7 relative plant EXECUTED"
# A7b — mutation control for the absolute-shape check: the same mutant (guard
# bypassed) MUST execute the relative plant under the identical arm.
set +e
rmout="$(cd "$CWD_SANDBOX" && printf '' | env GETENT_STUB=fail HOME="relative-home" PATH="$STUB_BIN:/usr/bin:/bin" "$MUTANT" --version 2>&1)"
rmrc=$?
set -e
[ "$rmrc" = "0" ] || fail "A7b mutant did not execute the relative plant (rc $rmrc, out '$rmout') - A7 proves nothing"
[ -e "$WORK/relative-sentinel" ] || fail "A7b mutant ran but relative sentinel absent - arm wrong, A7 proves nothing"
# A8 — parent-escape hostile arm (rev-code-02 delta, B2 remains): an absolute
# HOME containing a literal '..' component must be refused by the
# parent-escape check — the traversal would otherwise land on a planted tree
# OUTSIDE the seat home with no symlink involved.
ESC_BASE="$WORK/escape-base"
ESC_TARGET="$WORK/escape-target"
mkdir -p "$ESC_BASE" "$ESC_TARGET/.npm-global/bin"
cat >"$ESC_TARGET/.npm-global/bin/mosaic" <<SH
#!/bin/sh
touch "$WORK/escape-sentinel"
echo "0.0.0-escape-planted"
SH
chmod +x "$ESC_TARGET/.npm-global/bin/mosaic"
set +e
eout="$(printf '' | env GETENT_STUB=fail HOME="$ESC_BASE/../escape-target" PATH="$STUB_BIN:/usr/bin:/bin" "$LAUNCHER" --version 2>&1)"
erc=$?
set -e
[ "$erc" = "127" ] || fail "A8 parent-escape HOME was followed (rc $erc, out '$eout')"
case "$eout" in *"parent-escape component"*) ;; *) fail "A8 parent-escape diagnostic missing: '$eout'" ;; esac
[ ! -e "$WORK/escape-sentinel" ] || fail "A8 escape plant EXECUTED"
# A8b — mutation control: the guard-bypassed copy MUST execute the parent-
# escape plant under the identical arm (sentinel present, rc 0), proving the
# parent-escape check is what stands.
set +e
emout="$(printf '' | env GETENT_STUB=fail HOME="$ESC_BASE/../escape-target" PATH="$STUB_BIN:/usr/bin:/bin" "$MUTANT" --version 2>&1)"
emrc=$?
set -e
[ "$emrc" = "0" ] || fail "A8b mutant did not execute the escape plant (rc $emrc, out '$emout') - A8 proves nothing"
[ -e "$WORK/escape-sentinel" ] || fail "A8b mutant ran but escape sentinel absent - arm wrong, A8 proves nothing"
echo "mosaic launcher suite: all arms passed"
@@ -46,7 +46,12 @@ systemd/**
templates/**
tools/**
# Fleet: only the framework-seeded fleet subtrees are framework-owned.
# fleet/bin is exact-entry on purpose (T110 B1): the estate's fleet/bin carries
# operator-owned executables this package does not ship; a subtree glob here
# would make keep-mode update prune them.
fleet/README.md
fleet/bin/mosaic
fleet/bin/test-mosaic-launcher.sh
fleet/examples/**
fleet/profiles/**
fleet/roles/**
@@ -1,220 +1,64 @@
#!/bin/bash
# git-credential-mosaic — git credential helper. Resolves a Gitea token from the
# Mosaic credential store at runtime so remote URLs never embed secrets.
#!/usr/bin/python3
# git-credential-mosaic — production entrypoint (P0-SEC R4, rev-code-02 B1).
#
# Install (one-time, per clone or globally):
# git config credential.helper "$HOME/.config/mosaic/tools/git/git-credential-mosaic"
# WHY THIS IS NOT BASH: three review rounds falsified every in-bash startup
# guard. A non-interactive bash sources $BASH_ENV and imports exported
# functions BEFORE the first script line, so read(), unset(), exit(),
# declare(), printf() — every callable — can be shadows that fake the
# ancestry, defeat the scrub, or forge diagnostics (rev-code-02 probes 1 and
# 2, artifacts fc49e9d9 lineage). No in-language dispatch survives that.
#
# Per-agent identity (Gate-16 author != reviewer separation):
# git config mosaic.gitIdentity <agent-id> # per-worktree, persists on disk
# # or: export MOSAIC_GIT_IDENTITY=<agent-id>
# This entrypoint is unshapable at the bash level: python does not read
# BASH_ENV and imports no bash functions, and the interpreter is pinned by
# absolute shebang (no PATH resolution). It builds the child environment BY
# ALLOWLIST and execve's the bash implementation directly — the child bash
# starts with no BASH_ENV, no BASH_FUNC_*, no SHELLOPTS/BASHOPTS, and exactly
# the variables the credential protocol needs. stdin/stdout/stderr and argv
# pass through untouched.
#
# ── WHY THIS FAILS CLOSED ──────────────────────────────────────────────────────
# This helper used to end by emitting the shared account's token for any request
# it could not resolve to an identity. A seat with no identity, or with an
# identity whose token was never provisioned, therefore received the most
# privileged credential configured on the host — silently, and indistinguishably
# from correct operation. Every record it then created (commit, push, PR, review)
# was attributed to that shared account, so author != reviewer separation was
# unenforceable and the true actor was unrecoverable after the fact.
#
# Under-provisioning must fail loudly, not impersonate. A refused git operation
# is recoverable in one command; a merged pull request attributed to the wrong
# principal is not.
#
# ── CONTRACT ───────────────────────────────────────────────────────────────────
# identity : MOSAIC_GIT_IDENTITY > git config mosaic.gitIdentity > the
# username git supplies on stdin
# store : chosen by what the identity IS, with no precedence and no
# cross-store fallback (see "Credential store selection" below)
# hit : emit username + password, exit 0
# miss : emit NOTHING, spool a durable escalation record, explain on
# stderr, exit 1 — git surfaces the failure and nothing is attributed
# unknown host : exit 0 with no output, no record (passthrough for non-Mosaic
# remotes handled by another helper)
#
# Backward compatibility is preserved for exactly one case: a host with no fleet
# and no identity requested still gets the shared account, because on such a host
# the shared account is the operator's own and there is no attribution to lose.
# A host that HAS a fleet has agents whose records must be distinguishable, so
# the shared fallback is refused there.
#
# A token is never written to stderr, to the escalation record, or to any log.
# The implementation file (git-credential-mosaic.impl) refuses to run without
# the clean-mode marker, so it cannot be invoked directly as a shaped-entry
# bypass of this wrapper.
[ "$1" = "get" ] || exit 0
import os
import sys
host=""; username_in=""
while IFS= read -r line; do
[ -z "$line" ] && break
case "$line" in
host=*) host=${line#host=};;
username=*) username_in=${line#username=};;
esac
done
IMPL = os.path.join(os.path.dirname(os.path.realpath(__file__)), "git-credential-mosaic.impl")
# Absolute-path candidates ONLY — never PATH resolution (an attacker-shaped
# PATH must not choose the interpreter). /usr/bin/bash is the fleet-host
# layout; /bin/bash is alpine and other FHS variants (found by the T125
# gateway-image verification: the hardcoded /usr/bin/bash made every call
# exit 127 inside node:22-alpine).
BASH_CANDIDATES = ("/usr/bin/bash", "/bin/bash")
BASH = next((p for p in BASH_CANDIDATES if os.access(p, os.X_OK)), None)
# Recognized Gitea hosts carry the per-identity token scheme. Anything else is
# declined quietly — another helper owns it, and refusing would break it.
case "$host" in
git.uscllc.com) idpfx=gitea-usc;;
git.mosaicstack.dev) idpfx=gitea-mosaicstack;;
*) exit 0;;
esac
# Allowlist: everything else in the environment dies at this boundary. Adding
# a variable here is a security decision — it crosses into a shell that no
# longer has any startup shaping, but it also becomes the only context the
# implementation can see.
KEEP = (
"HOME",
"PATH",
"LANG",
"MOSAIC_GIT_IDENTITY",
"MOSAIC_AGENT_NAME",
"MOSAIC_BRAIN_HOME",
"MOSAIC_CREDENTIAL_SPOOL",
"MOSAIC_CREDENTIAL_LINEAGE_FENCE",
)
ident="$MOSAIC_GIT_IDENTITY"; ident_src="MOSAIC_GIT_IDENTITY"
if [ -z "$ident" ]; then
ident=$(git config --get mosaic.gitIdentity 2>/dev/null)
ident_src="git config mosaic.gitIdentity"
fi
if [ -z "$ident" ]; then
ident="$username_in"
ident_src="the username git supplied"
fi
env = {"_MOSAIC_HELPER_CLEAN": "1"}
for name in KEEP:
value = os.environ.get(name)
if value is not None:
env[name] = value
# ── Credential store selection ────────────────────────────────────────────────
# An identity is a SEAT or it is a SERVICE, and which one it is determines where
# its credential lives. There is no precedence rule between the two stores and no
# fallback from one to the other: a seat whose slot is empty fails closed rather
# than reading a service credential that happens to share its name.
#
# seat — <brain>/fleet/agents/<ident>/ exists
# credential at <brain>/fleet/agents/<ident>/secrets/<idpfx>-<ident>.token
# service — it does not
# credential at ~/.config/mosaic/secrets/gitea-tokens/<idpfx>-<ident>.token
#
# One credential, one location. Two copies of one credential diverge, and the
# stale copy fails in a way that reads as a revoked token rather than as drift.
#
# Brain-home resolution mirrors packages/mosaic/src/fleet/brain-home.ts and
# tools/fleet/start-agent-session.sh: MOSAIC_BRAIN_HOME wins, else ~/.mosaic.
brain_home="${MOSAIC_BRAIN_HOME:-$HOME/.mosaic}"
svc_store="$HOME/.config/mosaic/secrets/gitea-tokens"
idtok=""; ident_kind=""
if [ -n "$ident" ]; then
if [ -d "$brain_home/fleet/agents/$ident" ]; then
ident_kind="seat"
idtok="$brain_home/fleet/agents/$ident/secrets/${idpfx}-${ident}.token"
else
ident_kind="service identity"
idtok="$svc_store/${idpfx}-${ident}.token"
fi
if [ -r "$idtok" ]; then
echo "username=${ident}"
echo "password=$(cat "$idtok")"
exit 0
fi
fi
# ── Shared-account fallback: ONLY on a host with no fleet and no identity ──────
# `fleet/agents` existing is the same signal brain-home.ts uses to decide a brain
# is active. Where there are seats, records must be attributable, so an
# unresolvable request is refused instead of borrowing the shared account.
fleet_present=0
[ -d "$brain_home/fleet/agents" ] && fleet_present=1
if [ -z "$ident" ] && [ "$fleet_present" -eq 0 ]; then
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=../_lib/credentials.sh
source "$script_dir/../_lib/credentials.sh"
load_credentials "$idpfx" >/dev/null 2>&1 || exit 0
# GITEA_USER is not populated by load_credentials (it exports GITEA_URL and
# GITEA_TOKEN only). Gitea's git-over-HTTP auth authenticates from the token in
# the password field, not from the username string, so any non-empty
# placeholder works — deliberately NOT a real account name, since framework
# files stay operator-agnostic (tools/quality/scripts/verify-sanitized.sh).
echo "username=${GITEA_USER:-git}"
echo "password=$GITEA_TOKEN"
exit 0
fi
# ── FAIL CLOSED ───────────────────────────────────────────────────────────────
if [ -z "$ident" ]; then
reason="no-identity"
else
reason="no-token-for-identity"
fi
seat="${MOSAIC_AGENT_NAME:-unknown}"
ts=$(date -u +%Y-%m-%dT%H:%M:%SZ)
# The escalation RECORD is durable and unconditional; any notification built on
# top of it is best-effort. Record and alert are deduplicated separately — a cap
# on the alert alone lets the spool grow without bound exactly while the operator
# is being told nothing, so the louder the failure the quieter it gets.
#
# A record field is arbitrary operator-supplied text: an identity comes from git
# config or the environment, and cwd is whatever directory git ran in. Either can
# contain a quote or a backslash, which would make the line unparseable JSON --
# and a spool that silently stops parsing is worse than no spool, because the
# operator only discovers it while reading the record that explains an outage.
json_escape() {
local s=$1
s=${s//\\/\\\\}
s=${s//\"/\\\"}
s=${s//$'\t'/\\t}
s=${s//$'\r'/\\r}
s=${s//$'\n'/\\n}
printf '%s' "$s"
}
spool="${MOSAIC_CREDENTIAL_SPOOL:-$HOME/.local/state/mosaic-credential-escalations}"
spool_record=""
if mkdir -p "$spool" 2>/dev/null; then
chmod 700 "$spool" 2>/dev/null
spoolfile="$spool/$(date -u +%Y%m%d).jsonl"
dedupe="$spool/.spooled-${seat}-${ident:-none}-${reason}-$(date -u +%Y%m%d%H%M)"
if [ ! -e "$dedupe" ]; then
: > "$dedupe" 2>/dev/null
printf '{"ts":"%s","reason":"%s","identity":"%s","identity_source":"%s","kind":"%s","seat":"%s","host":"%s","cwd":"%s"}\n' \
"$(json_escape "$ts")" "$(json_escape "$reason")" \
"$(json_escape "${ident:-<unset>}")" "$(json_escape "$ident_src")" \
"$(json_escape "${ident_kind:-none}")" "$(json_escape "$seat")" \
"$(json_escape "$host")" "$(json_escape "$PWD")" \
>> "$spoolfile" 2>/dev/null
chmod 600 "$spoolfile" 2>/dev/null
fi
# Name the record only if one is actually on disk. Printing the path
# unconditionally sends the operator to a file that does not exist on exactly
# the hosts where the spool could not be created.
[ -s "$spoolfile" ] && spool_record="$spoolfile"
find "$spool" -maxdepth 1 -name '.spooled-*' -mmin +120 -delete 2>/dev/null
fi
cat >&2 <<EOF
git-credential-mosaic: REFUSED (fail-closed).
host : ${host}
identity : ${ident:-<unset>}${ident:+ (from ${ident_src}; resolved as a ${ident_kind})}
reason : ${reason}
EOF
if [ -n "$ident" ]; then
cat >&2 <<EOF
expected : ${idtok}
EOF
fi
cat >&2 <<EOF
No per-identity credential resolved. This helper does NOT fall back to the shared
account: that fallback makes every record it creates attributable to one
principal, which is unrecoverable once a pull request has merged under it.
Fix (pick one):
export MOSAIC_GIT_IDENTITY=<agent-id> # process-scoped
git config mosaic.gitIdentity <agent-id> # per-repo/worktree, persists
Then provision that identity's credential at the path named above. An identity
with a directory under \${MOSAIC_BRAIN_HOME:-\$HOME/.mosaic}/fleet/agents/ is a
seat and is read ONLY from its own secrets/ slot; any other identity is read from
~/.config/mosaic/secrets/gitea-tokens/. There is no fallback between the two.
If this identity legitimately needs git access and has none, ask the orchestrator
to provision one.
EOF
if [ -n "$spool_record" ]; then
echo " record: ${spool_record}" >&2
else
echo " record: NOT WRITTEN — spool unavailable at ${spool}" >&2
fi
exit 1
argv = [BASH, IMPL] + sys.argv[1:]
if BASH is None:
sys.stderr.write("git-credential-mosaic: no executable bash at " + " or ".join(BASH_CANDIDATES) + "\n")
sys.exit(127)
try:
os.execve(BASH, argv, env)
except OSError as exc:
sys.stderr.write(f"git-credential-mosaic: entrypoint exec failed: {exc}\n")
sys.exit(127)
@@ -0,0 +1,480 @@
#!/bin/bash
# git-credential-mosaic — git credential helper. Resolves a Gitea token from the
# Mosaic credential store at runtime so remote URLs never embed secrets.
#
# Install (one-time, per clone or globally):
# git config credential.helper "$HOME/.config/mosaic/tools/git/git-credential-mosaic"
#
# Per-agent identity (Gate-16 author != reviewer separation):
# git config mosaic.gitIdentity <agent-id> # per-worktree, persists on disk
# # or: export MOSAIC_GIT_IDENTITY=<agent-id>
#
# ── WHY THIS FAILS CLOSED ──────────────────────────────────────────────────────
# This helper used to end by emitting the shared account's token for any request
# it could not resolve to an identity. A seat with no identity, or with an
# identity whose token was never provisioned, therefore received the most
# privileged credential configured on the host — silently, and indistinguishably
# from correct operation. Every record it then created (commit, push, PR, review)
# was attributed to that shared account, so author != reviewer separation was
# unenforceable and the true actor was unrecoverable after the fact.
#
# Under-provisioning must fail loudly, not impersonate. A refused git operation
# is recoverable in one command; a merged pull request attributed to the wrong
# principal is not.
#
# ── CONTRACT ───────────────────────────────────────────────────────────────────
# identity : MOSAIC_GIT_IDENTITY > git config mosaic.gitIdentity > the
# username git supplies on stdin
# ownership: a FLEET SEAT caller may resolve ONLY its own identity, where
# the CALLER is established by process ANCESTRY, not by the
# current environment: every ancestor's /proc/<pid>/environ is
# frozen at exec, so a child can rewrite its own MOSAIC_AGENT_NAME
# but can never make an ancestor disagree with what the launcher
# gave it (P5-RM-006; the dual-variable override was measured by
# rev-code-02 F1). An anonymous caller (no lineage, no consensus)
# may resolve NOTHING on a fleet host — seat or service
# (rev-code-02 F2). Non-fleet hosts keep the documented legacy
# paths below.
# perms : a slot whose mode lets group or other read it (anything but
# ?00) is refused — a loose slot is provisioning drift, and
# serving from it silently widens every seat's exposure on a
# single-account host.
# store : chosen by what the identity IS, with no precedence and no
# cross-store fallback (see "Credential store selection" below)
# hit : emit username + password, exit 0
# miss : emit NOTHING, spool a durable escalation record, explain on
# stderr, exit 1 — git surfaces the failure and nothing is attributed
# unknown host : exit 0 with no output, no record (passthrough for non-Mosaic
# remotes handled by another helper)
#
# Backward compatibility is preserved for exactly one case: a host with no fleet
# and no identity requested still gets the shared account, because on such a host
# the shared account is the operator's own and there is no attribution to lose.
# A host that HAS a fleet has agents whose records must be distinguishable, so
# the shared fallback is refused there.
#
# A token is never written to stderr, to the escalation record, or to any log.
[ "$1" = "get" ] || exit 0
# ── The shared refusal path ──────────────────────────────────────────────────
# Every fail-closed exit funnels through refuse(): a durable escalation record
# (deduped, JSON-escaped), a stderr diagnostic naming host/identity/reason,
# caller-supplied guidance when the refusing site has specific advice, exit 1.
# Defined here because the ownership gate below must be able to reach it.
refuse() {
local guidance="${1:-}"
local seat ts spool spool_record spoolfile dedupe
seat="${MOSAIC_AGENT_NAME:-unknown}"
ts=$(date -u +%Y-%m-%dT%H:%M:%SZ)
# A record field is arbitrary operator-supplied text: an identity comes from git
# config or the environment, and cwd is whatever directory git ran in. Either can
# contain a quote or a backslash, which would make the line unparseable JSON --
# and a spool that silently stops parsing is worse than no spool, because the
# operator only discovers it while reading the record that explains an outage.
json_escape() {
local s=$1
s=${s//\\/\\\\}
s=${s//\"/\\\"}
s=${s//$'\t'/\\t}
s=${s//$'\r'/\\r}
s=${s//$'\n'/\\n}
printf '%s' "$s"
}
spool="${MOSAIC_CREDENTIAL_SPOOL:-$HOME/.local/state/mosaic-credential-escalations}"
spool_record=""
if mkdir -p "$spool" 2>/dev/null; then
chmod 700 "$spool" 2>/dev/null
spoolfile="$spool/$(date -u +%Y%m%d).jsonl"
dedupe="$spool/.spooled-${seat}-${ident:-none}-${reason}-$(date -u +%Y%m%d%H%M)"
if [ ! -e "$dedupe" ]; then
: > "$dedupe" 2>/dev/null
printf '{"ts":"%s","reason":"%s","identity":"%s","identity_source":"%s","kind":"%s","seat":"%s","host":"%s","cwd":"%s"}\n' \
"$(json_escape "$ts")" "$(json_escape "$reason")" \
"$(json_escape "${ident:-<unset>}")" "$(json_escape "$ident_src")" \
"$(json_escape "${ident_kind:-none}")" "$(json_escape "$seat")" \
"$(json_escape "$host")" "$(json_escape "$PWD")" \
>> "$spoolfile" 2>/dev/null
chmod 600 "$spoolfile" 2>/dev/null
fi
# Name the record only if one is actually on disk. Printing the path
# unconditionally sends the operator to a file that does not exist on exactly
# the hosts where the spool could not be created.
[ -s "$spoolfile" ] && spool_record="$spoolfile"
find "$spool" -maxdepth 1 -name '.spooled-*' -mmin +120 -delete 2>/dev/null
fi
while IFS= builtin read -r _diag_line; do builtin printf '%s\n' "$_diag_line" >&2; done <<EOF
git-credential-mosaic: REFUSED (fail-closed).
host : ${host}
identity : ${ident:-<unset>}${ident:+ (from ${ident_src}; resolved as a ${ident_kind})}
reason : ${reason}
EOF
if [ -n "$ident" ]; then
while IFS= builtin read -r _diag_line; do builtin printf '%s\n' "$_diag_line" >&2; done <<EOF
expected : ${idtok}
EOF
fi
while IFS= builtin read -r _diag_line; do builtin printf '%s\n' "$_diag_line" >&2; done <<EOF
${guidance}
EOF
if [ -n "$spool_record" ]; then
echo " record: ${spool_record}" >&2
else
echo " record: NOT WRITTEN — spool unavailable at ${spool}" >&2
fi
exit 1
}
# ── Bash environment injection guard (rev-code-02 R3, B1) ───────────────────
# Non-interactive bash sources $BASH_ENV at startup and imports exported
# functions from BASH_FUNC_* environment entries; either can define a read()
# or printf() that shadows the builtin the ancestry walker and diagnostics
# rely on — measured live by the reviewer's fixture (BASH_ENV read() rewrote
# every ancestry entry). A legitimate fleet seat environment carries neither
# (verified: zero BASH_FUNC_* in seat envs), so their presence in a helper
# request is an injection attempt: scrub the shadows first (so even the
# refusal machinery cannot be subverted), then refuse fail-closed.
# Imported functions are detected by ENUMERATION, not env-var names: bash
# consumes BASH_FUNC_* variables while importing the functions, so the
# environment no longer shows them (measured). At this point the script has
# defined exactly one function of its own (refuse); anything else in the
# function table arrived from the caller's environment. BASH_ENV is checked
# directly (it remains visible after sourcing).
_injected=0
_inj_names=""
while IFS=' ' builtin read -r _decl _kind _fn; do
[ -n "$_fn" ] || continue
case "$_fn" in
refuse) ;;
*) _injected=1; _inj_names="$_inj_names $_fn";;
esac
done < <(declare -F)
_inj_vars="${!BASH_FUNC_@}"
if [ -n "$_inj_vars" ]; then
_injected=1
for _iv in $_inj_vars; do
case "$_iv" in
BASH_FUNC_*%%) _ifn="${_iv#BASH_FUNC_}"; _ifn="${_ifn%%%}";;
BASH_FUNC_*) _ifn="${_iv#BASH_FUNC_}";;
*) _ifn="";;
esac
[ -n "$_ifn" ] && { unset -f "$_ifn" 2>/dev/null; _inj_names="$_inj_names $_ifn"; }
done
fi
if [ "$_injected" = 1 ] || [ -n "${BASH_ENV:-}" ]; then
while IFS=' ' builtin read -r _decl _kind _fn; do
[ "$_fn" = refuse ] || unset -f "$_fn" 2>/dev/null
done < <(declare -F)
unset BASH_ENV 2>/dev/null
reason="bash-environment-injection-refused"
refuse "The helper's bash startup state was externally shaped: BASH_ENV is
set and/or exported BASH_FUNC_* functions are present in the request
environment. Non-interactive bash sources BASH_ENV and imports those
functions BEFORE any script line runs, so builtins this helper's security
decisions rely on could be shadowed. Nothing resolves from a shaped request
environment. If this surprised a legitimate workflow, the caller environment
must be cleaned (no BASH_ENV, no exported functions) before invoking git."
fi
host=""; username_in=""
while IFS= builtin read -r line; do
[ -z "$line" ] && break
case "$line" in
host=*) host=${line#host=};;
username=*) username_in=${line#username=};;
esac
done
# Recognized Gitea hosts carry the per-identity token scheme. Anything else is
# declined quietly — another helper owns it, and refusing would break it.
case "$host" in
git.uscllc.com) idpfx=gitea-usc;;
git.mosaicstack.dev) idpfx=gitea-mosaicstack;;
*) exit 0;;
esac
# ── Clean-entrypoint assert (P0-SEC R4) ─────────────────────────────────────
# This implementation only runs behind the python entrypoint
# (git-credential-mosaic), which execve's it with an allowlist environment:
# no BASH_ENV, no imported functions, nothing shapable at bash startup. A
# direct invocation without the marker is a bypass attempt on that boundary
# and refuses. Placed after refuse() and the host parse so the refusal path
# exists when it fires (an earlier placement died on 'refuse: command not
# found' — the failure mode is real, keep this after every definition it
# calls).
if [ "${_MOSAIC_HELPER_CLEAN:-}" != "1" ]; then
reason="direct-entrypoint-refused"
refuse "This implementation refuses to run outside the production
entrypoint. git-credential-mosaic (the python wrapper in this directory)
execve's it with a hand-built, unshapable environment; invoking the .impl
directly bypasses that boundary. Credential requests go through git, which
invokes the wrapper named in gitconfig."
fi
ident="$MOSAIC_GIT_IDENTITY"; ident_src="MOSAIC_GIT_IDENTITY"
if [ -z "$ident" ]; then
ident=$(git config --get mosaic.gitIdentity 2>/dev/null)
ident_src="git config mosaic.gitIdentity"
fi
if [ -z "$ident" ]; then
ident="$username_in"
ident_src="the username git supplied"
fi
# ── Credential store selection ────────────────────────────────────────────────
# An identity is a SEAT or it is a SERVICE, and which one it is determines where
# its credential lives. There is no precedence rule between the two stores and no
# fallback from one to the other: a seat whose slot is empty fails closed rather
# than reading a service credential that happens to share its name.
#
# seat — <brain>/fleet/agents/<ident>/ exists
# credential at <brain>/fleet/agents/<ident>/secrets/<idpfx>-<ident>.token
# service — it does not
# credential at ~/.config/mosaic/secrets/gitea-tokens/<idpfx>-<ident>.token
#
# One credential, one location. Two copies of one credential diverge, and the
# stale copy fails in a way that reads as a revoked token rather than as drift.
#
# Brain-home resolution mirrors packages/mosaic/src/fleet/brain-home.ts and
# tools/fleet/start-agent-session.sh: MOSAIC_BRAIN_HOME wins, else ~/.mosaic.
brain_home="${MOSAIC_BRAIN_HOME:-$HOME/.mosaic}"
svc_store="$HOME/.config/mosaic/secrets/gitea-tokens"
# ── Caller-identity ownership (P5-RM-006) ──────────────────────────────────────
# A credential request is honourable only when the CALLER owns the identity it
# asks for. On a fleet host every seat shares one unix account, so the
# launcher-established MOSAIC_AGENT_NAME is the only attribution signal the
# helper has. Two measured paths made the old contract unsafe:
#
# - a seat exporting MOSAIC_GIT_IDENTITY=<another-seat> resolved that seat's
# token through the normal precedence chain (T97 G2, jarvis V2 probe), and
# - an anonymous caller (no seat name) inherited the host gitconfig's
# username=jarvis line and resolved jarvis's slot (T94: five watcher units
# flapping on exactly this class).
#
# Ownership rules, fail-closed on fleet hosts only; a host with no fleet keeps
# the legacy contract unchanged:
# 1. a SEAT caller may resolve only its own identity;
# 2. an anonymous caller may not resolve any SEAT identity (service
# identities remain available to non-seat automation such as CI).
# [P5-RM-006r1 ancestry binding begin]
# ── Caller identity from exec-frozen ancestry (rev-code-02 F1/F2) ───────────
# Walk /proc self->root collecting MOSAIC_AGENT_NAME from each ancestor's
# frozen environ. Rules:
# - any DISAGREEMENT (an ancestor value != the current value, or ancestors
# disagreeing among themselves) is a rewrite -> spoof-refused, nothing
# resolves. A child can inject variables downward but cannot alter an
# ancestor's exec-frozen environ, so the launcher-established value always
# participates in the comparison.
# - consensus (all ancestors that carry the var agree with the current env,
# or with each other when the current env is empty) -> caller = that value.
# - no ancestor carries it -> the current claim is unlineaged: caller is
# anonymous regardless of what the environment says. A name with no
# lineage is a claim, not an identity.
# The walk stops at PID 1, at a missing /proc entry, or INCLUSIVE at an
# ancestor that carries MOSAIC_CREDENTIAL_LINEAGE_FENCE with an EMPTY agent
# name — the test-suite lineage root. A fence beside a non-empty name is
# IGNORED and the walk continues, so an attacker cannot fence off the true
# ancestry by planting the marker next to a victim name.
trusted_caller() {
# PATH-HARDENED (rev-code-02 R1 F1): every /proc read below uses ONLY bash
# builtins (read/case/parameter expansion). The first implementation piped
# through PATH-resolved tr/sed/head/grep, and a caller that prepends hostile
# utilities to PATH in the same invocation that overrides the identity
# variables could forge the ancestry itself. Builtins cannot be shadowed.
local pid ppid v entry line fence
local -a vals=()
pid=$$
while :; do
v=""
fence=0
if [ -r "/proc/$pid/environ" ]; then
# Read inside a captured subshell whose stderr is closed: opening
# /proc/<pid>/environ can fail with EACCES on ancestors that are
# readable-by-mode but not openable (session managers), and that open
# failure prints from the SHELL, immune to loop-level 2>/dev/null
# (measured). The subshell makes the skip silent; NUL separators are
# converted to newlines for the parent's builtin parse.
_env_text=$( { while IFS= builtin read -r -d '' _e; do builtin printf '%s\n' "$_e"; done < "/proc/$pid/environ"; } 2>/dev/null )
while IFS= builtin read -r entry; do
[ -n "$entry" ] || continue
case "$entry" in
MOSAIC_AGENT_NAME=*) v="${entry#MOSAIC_AGENT_NAME=}";;
MOSAIC_CREDENTIAL_LINEAGE_FENCE=*) fence=1;;
esac
done <<EOF_ENV
$_env_text
EOF_ENV
fi
if [ "$pid" != "$$" ]; then
[ -n "$v" ] && vals+=("$v")
if [ "$fence" = 1 ] && [ -z "$v" ]; then
break
fi
fi
ppid=""
if [ -r "/proc/$pid/status" ]; then
while IFS= builtin read -r line; do
case "$line" in
PPid:*) ppid="${line#PPid:}"; ppid="${ppid//[[:space:]]/}";;
esac
done < "/proc/$pid/status"
fi
case "$ppid" in ''|0|1) break;; esac
pid=$ppid
done
local self="${MOSAIC_AGENT_NAME:-}" i consensus=""
if [ "${#vals[@]}" -gt 0 ]; then
consensus="${vals[0]}"
for i in "${vals[@]}"; do
if [ "$i" != "$consensus" ]; then
printf 'SPOOF'
return
fi
done
if [ -n "$self" ] && [ "$self" != "$consensus" ]; then
printf 'SPOOF'
return
fi
fi
printf '%s' "$consensus"
}
if [ -d "$brain_home/fleet/agents" ]; then
caller="$(trusted_caller)"
if [ "$caller" = "SPOOF" ]; then
reason="caller-identity-spoof-refused"
refuse "The MOSAIC_AGENT_NAME lineage disagrees within this process tree:
an ancestor established by exec carries a different value than the request.
A child process can rewrite its own environment but never an ancestor's
frozen environ, so disagreement is a rewrite, not a race. Nothing resolves
under a rewritten caller identity. If this surprised a legitimate workflow,
run git from the seat's own session, not from a rewritten environment."
fi
if [ -n "$caller" ] && [ -d "$brain_home/fleet/agents/$caller" ]; then
if [ -n "$ident" ] && [ "$ident" != "$caller" ]; then
reason="cross-seat-identity-refused"
refuse "A seat may resolve only its own credential slot. Caller seat is
'$caller' (ancestry-established); the request names '$ident'. Overriding
MOSAIC_GIT_IDENTITY (or a git config / URL username) to another seat's name is
exactly the path this refusal exists to close. If '$ident' auth is genuinely
required, that seat runs the operation itself or the orchestrator provisions
an explicit grant."
fi
else
# Anonymous caller on a fleet host (no lineage, or the lineage root is not
# a seat): NOTHING resolves — seat slots (T94 jarvis@ class) or legacy
# service credentials (rev-code-02 F2: credentialed services are seats;
# the legacy store is vestigial and not anonymously reachable).
if [ -n "$ident" ]; then
ident_kind="${ident_kind:-}"
[ -d "$brain_home/fleet/agents/$ident" ] && ident_kind="seat" || ident_kind="service identity"
reason="anonymous-credential-refused"
refuse "This caller has no seat lineage on a fleet host and asked for
'$ident' (a ${ident_kind}). Anonymous callers resolve nothing on fleet hosts:
seat credentials must never serve an unattributable caller, and credentialed
services are seats with their own sessions (the legacy service store is
vestigial). Run from the owning seat's session."
fi
fi
fi
# [P5-RM-006r1 ancestry binding end]
idtok=""; ident_kind=""
if [ -n "$ident" ]; then
if [ -d "$brain_home/fleet/agents/$ident" ]; then
ident_kind="seat"
idtok="$brain_home/fleet/agents/$ident/secrets/${idpfx}-${ident}.token"
else
ident_kind="service identity"
idtok="$svc_store/${idpfx}-${ident}.token"
fi
if [ -r "$idtok" ]; then
# P5-RM-006 seat permissions: a SEAT slot readable by group or other is
# provisioning drift, and on a single-account fleet host it widens every
# seat's exposure at once. Refuse rather than serve from a loose slot; the
# record names the path so the provisioning fix is one chmod away.
# Scoped to seat slots: the framework service store is operator-managed
# and outside this work unit's permission surface.
if [ "${ident_kind:-}" = "seat" ]; then
# command -p resolves stat from the POSIX default PATH (system
# directories), never the caller's PATH (rev-code-02 R3 B2: a shadowed
# stat reported a 0644 slot as 600 and the helper served it). Output is
# shape-validated: anything that is not 3-4 octal digits refuses.
slot_mode="$(command -p stat -c '%a' "$idtok" 2>/dev/null || true)"
case "$slot_mode" in
[0-7][0-7][0-7]|[0-7][0-7][0-7][0-7]) ;;
*) slot_mode="unverifiable";;
esac
if [ "${slot_mode:1:2}" != "00" ]; then
reason="slot-permission-violation"
refuse "Slot $idtok has mode ${slot_mode:-unknown}; expected owner-only
(0600 or stricter). Tighten it: chmod 600 '$idtok'. This refusal is the seat
permissions half of P5-RM-006: a loose slot on a shared-account host is every
seat's exposure, so the helper declines to serve from it. Mode inspection uses
command -p (trusted PATH) and fails closed on unverifiable output."
fi
fi
echo "username=${ident}"
echo "password=$(<"$idtok")"
exit 0
fi
fi
# ── Shared-account fallback: ONLY on a host with no fleet and no identity ──────
# `fleet/agents` existing is the same signal brain-home.ts uses to decide a brain
# is active. Where there are seats, records must be attributable, so an
# unresolvable request is refused instead of borrowing the shared account.
fleet_present=0
[ -d "$brain_home/fleet/agents" ] && fleet_present=1
if [ -z "$ident" ] && [ "$fleet_present" -eq 0 ]; then
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=../_lib/credentials.sh
source "$script_dir/../_lib/credentials.sh"
load_credentials "$idpfx" >/dev/null 2>&1 || exit 0
# GITEA_USER is not populated by load_credentials (it exports GITEA_URL and
# GITEA_TOKEN only). Gitea's git-over-HTTP auth authenticates from the token in
# the password field, not from the username string, so any non-empty
# placeholder works — deliberately NOT a real account name, since framework
# files stay operator-agnostic (tools/quality/scripts/verify-sanitized.sh).
echo "username=${GITEA_USER:-git}"
echo "password=$GITEA_TOKEN"
exit 0
fi
# ── FAIL CLOSED ───────────────────────────────────────────────────────────────
# The escalation RECORD is durable and unconditional; any notification built on
# top of it is best-effort (see refuse()). Record and alert are deduplicated
# separately — a cap on the alert alone lets the spool grow without bound
# exactly while the operator is being told nothing, so the louder the failure
# the quieter it gets.
if [ -z "$ident" ]; then
reason="no-identity"
else
reason="no-token-for-identity"
fi
refuse "No per-identity credential resolved. This helper does NOT fall back to the shared
account: that fallback makes every record it creates attributable to one
principal, which is unrecoverable once a pull request has merged under it.
Fix (pick one):
export MOSAIC_GIT_IDENTITY=<agent-id> # process-scoped
git config mosaic.gitIdentity <agent-id> # per-repo/worktree, persists
Then provision that identity's credential at the path named above. An identity
with a directory under \${MOSAIC_BRAIN_HOME:-\$HOME/.mosaic}/fleet/agents/ is a
seat and is read ONLY from its own secrets/ slot; any other identity is read from
~/.config/mosaic/secrets/gitea-tokens/. There is no fallback between the two.
If this identity legitimately needs git access and has none, ask the orchestrator
to provision one."
@@ -47,7 +47,7 @@ usage_error() {
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-i|--issue)
-i|--issue|--number)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
ISSUE="$2"
shift 2
@@ -12,6 +12,7 @@ source "$SCRIPT_DIR/detect-platform.sh"
# Parse arguments
ISSUE_NUMBER=""
COMMENT=""
BODY_FILE=""
# Usage-error contract (R4, 2026-08-28): usage errors print to STDERR and exit 2,
# distinct from provider, credential, and verification failures (exit 1), so a
@@ -24,7 +25,7 @@ usage_error() {
while [[ $# -gt 0 ]]; do
case $1 in
-i|--issue)
-i|--issue|--number)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
ISSUE_NUMBER="$2"
shift 2
@@ -36,11 +37,18 @@ while [[ $# -gt 0 ]]; do
COMMENT="$2"
shift 2
;;
--body-file)
# R3: body from file (or '-' = stdin); mutually exclusive with --body.
[[ $# -ge 2 && "$2" != --* ]] || usage_error "option $1 requires a path (or - for stdin)"
BODY_FILE="$2"
shift 2
;;
-h|--help)
echo "Usage: issue-close.sh -i <issue_number> [-b <comment>]"
echo ""
echo "Options:"
echo " -i, --issue Issue number (required)"
echo " -n, --number Issue number (required; canonical)"
echo " -i, --issue Alias for --number"
echo " -b, --body Comment to add before closing (optional; canonical)"
echo " -c, --comment Alias for --body"
echo " -h, --help Show this help"
@@ -54,6 +62,19 @@ while [[ $# -gt 0 ]]; do
esac
done
# R3 (2026-08-29): resolve --body-file into COMMENT (file or stdin '-');
# exclusive with an explicit --body/--comment value.
if [[ -n "$BODY_FILE" ]]; then
[[ -z "$COMMENT" ]] || usage_error "--body-file and --body are mutually exclusive"
if [[ "$BODY_FILE" == "-" ]]; then
COMMENT=$(cat) || usage_error "could not read body from stdin"
else
[[ -r "$BODY_FILE" ]] || usage_error "body file not readable: $BODY_FILE"
COMMENT=$(cat "$BODY_FILE") || usage_error "could not read body file: $BODY_FILE"
fi
fi
if [[ -z "$ISSUE_NUMBER" ]]; then
usage_error "issue number is required (-i/--issue)"
fi
@@ -31,6 +31,7 @@ source "$SCRIPT_DIR/detect-platform.sh"
# Parse arguments
ISSUE_NUMBER=""
COMMENT=""
BODY_FILE=""
LOGIN_OVERRIDE=""
# Usage-error contract (R4, 2026-08-28): usage errors print to STDERR and exit 2,
@@ -45,7 +46,7 @@ usage_error() {
while [[ $# -gt 0 ]]; do
case $1 in
-i|--issue)
-i|--issue|--number)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
ISSUE_NUMBER="$2"
shift 2
@@ -58,6 +59,12 @@ while [[ $# -gt 0 ]]; do
COMMENT="$2"
shift 2
;;
--body-file)
# R3: body from file (or '-' = stdin); mutually exclusive with --body.
[[ $# -ge 2 && "$2" != --* ]] || usage_error "option $1 requires a path (or - for stdin)"
BODY_FILE="$2"
shift 2
;;
-l|--login)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
LOGIN_OVERRIDE="$2"
@@ -67,7 +74,8 @@ while [[ $# -gt 0 ]]; do
echo "Usage: issue-comment.sh -i <issue_number> -b <comment> [--login <name>]"
echo ""
echo "Options:"
echo " -i, --issue Issue number (required)"
echo " -n, --number Issue number (required; canonical)"
echo " -i, --issue Alias for --number"
echo " -b, --body Comment text (required; canonical)"
echo " -c, --comment Alias for --body"
echo " -l, --login Override the detected Gitea tea login for this call"
@@ -82,6 +90,19 @@ while [[ $# -gt 0 ]]; do
esac
done
# R3 (2026-08-29): resolve --body-file into COMMENT (file or stdin '-');
# exclusive with an explicit --body/--comment value.
if [[ -n "$BODY_FILE" ]]; then
[[ -z "$COMMENT" ]] || usage_error "--body-file and --body are mutually exclusive"
if [[ "$BODY_FILE" == "-" ]]; then
COMMENT=$(cat) || usage_error "could not read body from stdin"
else
[[ -r "$BODY_FILE" ]] || usage_error "body file not readable: $BODY_FILE"
COMMENT=$(cat "$BODY_FILE") || usage_error "could not read body file: $BODY_FILE"
fi
fi
if [[ -z "$ISSUE_NUMBER" ]]; then
usage_error "issue number is required (-i/--issue)"
fi
@@ -10,6 +10,7 @@ source "$SCRIPT_DIR/detect-platform.sh"
# Default values
TITLE=""
BODY=""
BODY_FILE=""
LABELS=""
MILESTONE=""
INTERACTIVE=false
@@ -100,6 +101,12 @@ while [[ $# -gt 0 ]]; do
BODY="$2"
shift 2
;;
--body-file)
# R3: body from file (or '-' = stdin); mutually exclusive with --body.
[[ $# -ge 2 && "$2" != --* ]] || usage_error "option $1 requires a path (or - for stdin)"
BODY_FILE="$2"
shift 2
;;
-l|--labels)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
LABELS="$2"
@@ -124,6 +131,19 @@ while [[ $# -gt 0 ]]; do
esac
done
# R3 (2026-08-29): resolve --body-file into BODY (file or stdin '-');
# exclusive with an explicit --body/--comment value.
if [[ -n "$BODY_FILE" ]]; then
[[ -z "$BODY" ]] || usage_error "--body-file and --body are mutually exclusive"
if [[ "$BODY_FILE" == "-" ]]; then
BODY=$(cat) || usage_error "could not read body from stdin"
else
[[ -r "$BODY_FILE" ]] || usage_error "body file not readable: $BODY_FILE"
BODY=$(cat "$BODY_FILE") || usage_error "could not read body file: $BODY_FILE"
fi
fi
if [[ "$INTERACTIVE" == true ]]; then
[[ -n "$TITLE" ]] || read -r -p "Issue title: " TITLE
[[ -n "$BODY" ]] || read -r -p "Issue body (optional): " BODY || true
@@ -11,6 +11,7 @@ source "$SCRIPT_DIR/detect-platform.sh"
ISSUE_NUMBER=""
TITLE=""
BODY=""
BODY_FILE=""
LABELS=""
MILESTONE=""
@@ -25,7 +26,7 @@ usage_error() {
while [[ $# -gt 0 ]]; do
case $1 in
-i|--issue)
-i|--issue|--number)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
ISSUE_NUMBER="$2"
shift 2
@@ -40,6 +41,12 @@ while [[ $# -gt 0 ]]; do
BODY="$2"
shift 2
;;
--body-file)
# R3: body from file (or '-' = stdin); mutually exclusive with --body.
[[ $# -ge 2 && "$2" != --* ]] || usage_error "option $1 requires a path (or - for stdin)"
BODY_FILE="$2"
shift 2
;;
-l|--labels)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
LABELS="$2"
@@ -54,7 +61,8 @@ while [[ $# -gt 0 ]]; do
echo "Usage: issue-edit.sh -i <issue_number> [-t <title>] [-b <body>] [-l <labels>] [-m <milestone>]"
echo ""
echo "Options:"
echo " -i, --issue Issue number (required)"
echo " -n, --number Issue number (required; canonical)"
echo " -i, --issue Alias for --number"
echo " -t, --title New title"
echo " -b, --body New body/description"
echo " -l, --labels Labels (comma-separated, replaces existing)"
@@ -70,6 +78,19 @@ while [[ $# -gt 0 ]]; do
esac
done
# R3 (2026-08-29): resolve --body-file into BODY (file or stdin '-');
# exclusive with an explicit --body/--comment value.
if [[ -n "$BODY_FILE" ]]; then
[[ -z "$BODY" ]] || usage_error "--body-file and --body are mutually exclusive"
if [[ "$BODY_FILE" == "-" ]]; then
BODY=$(cat) || usage_error "could not read body from stdin"
else
[[ -r "$BODY_FILE" ]] || usage_error "body file not readable: $BODY_FILE"
BODY=$(cat "$BODY_FILE") || usage_error "could not read body file: $BODY_FILE"
fi
fi
if [[ -z "$ISSUE_NUMBER" ]]; then
usage_error "issue number is required (-i/--issue)"
fi
@@ -54,7 +54,7 @@ while [[ $# -gt 0 ]]; do
STATE="$2"
shift 2
;;
-l|--label)
-l|--label|--labels)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
LABEL="$2"
shift 2
@@ -11,6 +11,7 @@ source "$SCRIPT_DIR/detect-platform.sh"
# Parse arguments
ISSUE_NUMBER=""
COMMENT=""
BODY_FILE=""
# Usage-error contract (R4, 2026-08-28): usage errors print to STDERR and exit 2,
# distinct from provider, credential, and verification failures (exit 1), so a
@@ -23,7 +24,7 @@ usage_error() {
while [[ $# -gt 0 ]]; do
case $1 in
-i|--issue)
-i|--issue|--number)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
ISSUE_NUMBER="$2"
shift 2
@@ -35,11 +36,18 @@ while [[ $# -gt 0 ]]; do
COMMENT="$2"
shift 2
;;
--body-file)
# R3: body from file (or '-' = stdin); mutually exclusive with --body.
[[ $# -ge 2 && "$2" != --* ]] || usage_error "option $1 requires a path (or - for stdin)"
BODY_FILE="$2"
shift 2
;;
-h|--help)
echo "Usage: issue-reopen.sh -i <issue_number> [-b <comment>]"
echo ""
echo "Options:"
echo " -i, --issue Issue number (required)"
echo " -n, --number Issue number (required; canonical)"
echo " -i, --issue Alias for --number"
echo " -b, --body Comment to add when reopening (optional; canonical)"
echo " -c, --comment Alias for --body"
echo " -h, --help Show this help"
@@ -53,6 +61,19 @@ while [[ $# -gt 0 ]]; do
esac
done
# R3 (2026-08-29): resolve --body-file into COMMENT (file or stdin '-');
# exclusive with an explicit --body/--comment value.
if [[ -n "$BODY_FILE" ]]; then
[[ -z "$COMMENT" ]] || usage_error "--body-file and --body are mutually exclusive"
if [[ "$BODY_FILE" == "-" ]]; then
COMMENT=$(cat) || usage_error "could not read body from stdin"
else
[[ -r "$BODY_FILE" ]] || usage_error "body file not readable: $BODY_FILE"
COMMENT=$(cat "$BODY_FILE") || usage_error "could not read body file: $BODY_FILE"
fi
fi
if [[ -z "$ISSUE_NUMBER" ]]; then
usage_error "issue number is required (-i/--issue)"
fi
@@ -81,7 +81,7 @@ if comments:
while [[ $# -gt 0 ]]; do
case $1 in
-i|--issue)
-i|--issue|--number)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
ISSUE_NUMBER="$2"
shift 2
@@ -90,7 +90,8 @@ while [[ $# -gt 0 ]]; do
echo "Usage: issue-view.sh -i <issue_number>"
echo ""
echo "Options:"
echo " -i, --issue Issue number (required)"
echo " -n, --number Issue number (required; canonical)"
echo " -i, --issue Alias for --number"
echo ""
echo "Comments are always included (tea --comments / Gitea API /comments)."
echo " -h, --help Show this help"
@@ -11,6 +11,7 @@ source "$SCRIPT_DIR/detect-platform.sh"
# Parse arguments
PR_NUMBER=""
COMMENT=""
BODY_FILE=""
# Usage-error contract (R4, 2026-08-28): usage errors print to STDERR and exit 2,
# distinct from provider, credential, and verification failures (exit 1), so a
@@ -35,6 +36,12 @@ while [[ $# -gt 0 ]]; do
COMMENT="$2"
shift 2
;;
--body-file)
# R3: body from file (or '-' = stdin); mutually exclusive with --body.
[[ $# -ge 2 && "$2" != --* ]] || usage_error "option $1 requires a path (or - for stdin)"
BODY_FILE="$2"
shift 2
;;
-h|--help)
echo "Usage: pr-close.sh -n <pr_number> [-b <comment>]"
echo ""
@@ -53,6 +60,19 @@ while [[ $# -gt 0 ]]; do
esac
done
# R3 (2026-08-29): resolve --body-file into COMMENT (file or stdin '-');
# exclusive with an explicit --body/--comment value.
if [[ -n "$BODY_FILE" ]]; then
[[ -z "$COMMENT" ]] || usage_error "--body-file and --body are mutually exclusive"
if [[ "$BODY_FILE" == "-" ]]; then
COMMENT=$(cat) || usage_error "could not read body from stdin"
else
[[ -r "$BODY_FILE" ]] || usage_error "body file not readable: $BODY_FILE"
COMMENT=$(cat "$BODY_FILE") || usage_error "could not read body file: $BODY_FILE"
fi
fi
if [[ -z "$PR_NUMBER" ]]; then
usage_error "PR number is required (-n/--number)"
fi
@@ -10,6 +10,7 @@ source "$SCRIPT_DIR/detect-platform.sh"
# Default values
TITLE=""
BODY=""
BODY_FILE=""
BASE_BRANCH=""
HEAD_BRANCH=""
LABELS=""
@@ -158,6 +159,12 @@ while [[ $# -gt 0 ]]; do
BODY="$2"
shift 2
;;
--body-file)
# R3: body from file (or '-' = stdin); mutually exclusive with --body.
[[ $# -ge 2 && "$2" != --* ]] || usage_error "option $1 requires a path (or - for stdin)"
BODY_FILE="$2"
shift 2
;;
-B|--base)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
BASE_BRANCH="$2"
@@ -197,6 +204,19 @@ while [[ $# -gt 0 ]]; do
esac
done
# R3 (2026-08-29): resolve --body-file into BODY (file or stdin '-');
# exclusive with an explicit --body/--comment value.
if [[ -n "$BODY_FILE" ]]; then
[[ -z "$BODY" ]] || usage_error "--body-file and --body are mutually exclusive"
if [[ "$BODY_FILE" == "-" ]]; then
BODY=$(cat) || usage_error "could not read body from stdin"
else
[[ -r "$BODY_FILE" ]] || usage_error "body file not readable: $BODY_FILE"
BODY=$(cat "$BODY_FILE") || usage_error "could not read body file: $BODY_FILE"
fi
fi
# If no title but issue provided, generate title
if [[ -z "$TITLE" ]] && [[ -n "$ISSUE" ]]; then
TITLE="Fixes #$ISSUE"
+16 -4
View File
@@ -13,21 +13,33 @@ OUTPUT_FILE=""
REPO_OVERRIDE=""
HOST_OVERRIDE=""
# Usage-error contract (R4): usage errors print to STDERR and exit 2,
# distinct from provider, credential, and verification failures (exit 1).
usage_error() {
echo "Error: $*" >&2
echo "Usage: pr-diff.sh -n <pr_number> [-r owner/repo] [--host host] [-o <output_file>] (see --help)" >&2
exit 2
}
while [[ $# -gt 0 ]]; do
case $1 in
-n|--number)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
PR_NUMBER="$2"
shift 2
;;
-o|--output)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
OUTPUT_FILE="$2"
shift 2
;;
-r|--repo)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
REPO_OVERRIDE="$2"
shift 2
;;
--host)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
HOST_OVERRIDE="$2"
shift 2
;;
@@ -40,18 +52,18 @@ while [[ $# -gt 0 ]]; do
echo " --host Gitea host for --repo API calls (or set GITEA_HOST/GITEA_URL)"
echo " -o, --output Output file (optional, prints to stdout if omitted)"
echo " -h, --help Show this help"
echo ""
echo "Exit codes: 0 success; 2 usage error (stderr); 1 provider/credential failure."
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
usage_error "unknown option: $1"
;;
esac
done
if [[ -z "$PR_NUMBER" ]]; then
echo "Error: PR number is required (-n)" >&2
exit 1
usage_error "PR number is required (-n/--number)"
fi
if [[ -n "$REPO_OVERRIDE" ]]; then
@@ -11,6 +11,7 @@ source "$SCRIPT_DIR/detect-platform.sh"
PR_NUMBER=""
TITLE=""
BODY=""
BODY_FILE=""
BASE_BRANCH=""
DRAFT_MODE=""
LOGIN_OVERRIDE=""
@@ -65,6 +66,7 @@ while [[ $# -gt 0 ]]; do
-n|--number) [[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"; PR_NUMBER="${2:-}"; shift 2 ;;
-t|--title) [[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"; TITLE="${2:-}"; shift 2 ;;
-b|--body) [[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"; BODY="${2:-}"; shift 2 ;;
--body-file) [[ $# -ge 2 && "$2" != --* ]] || usage_error "option $1 requires a path (or - for stdin)"; BODY_FILE="$2"; shift 2 ;;
-B|--base) [[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"; BASE_BRANCH="${2:-}"; shift 2 ;;
--draft)
[[ "$DRAFT_MODE" != "ready" ]] || { echo "Error: --draft and --ready are mutually exclusive" >&2; exit 2; }
@@ -80,6 +82,19 @@ while [[ $# -gt 0 ]]; do
esac
done
# R3 (2026-08-29): resolve --body-file into BODY (file or stdin '-');
# exclusive with an explicit --body value.
if [[ -n "$BODY_FILE" ]]; then
[[ -z "$BODY" ]] || usage_error "--body-file and --body are mutually exclusive"
if [[ "$BODY_FILE" == "-" ]]; then
BODY=$(cat) || usage_error "could not read body from stdin"
else
[[ -r "$BODY_FILE" ]] || usage_error "body file not readable: $BODY_FILE"
BODY=$(cat "$BODY_FILE") || usage_error "could not read body file: $BODY_FILE"
fi
fi
[[ -n "$PR_NUMBER" ]] || { echo "Error: Pull request number is required (-n)" >&2; exit 2; }
[[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]] || { echo "Error: Pull request number must be a positive integer" >&2; exit 2; }
if [[ -z "$TITLE" && -z "$BODY" && -z "$BASE_BRANCH" && -z "$DRAFT_MODE" ]]; then
+15 -4
View File
@@ -34,29 +34,41 @@ Examples:
$(basename "$0") -s merged -a username # Merged PRs by user
$(basename "$0") --repo ddk/ai-bma # List PRs from anywhere
EOF
exit "${1:-1}"
exit "${1:-2}"
}
# Parse arguments
# Usage-error contract (R4): usage errors print to STDERR and exit 2,
# distinct from provider, credential, and verification failures (exit 1).
usage_error() {
echo "Error: $*" >&2
usage >&2
}
while [[ $# -gt 0 ]]; do
case $1 in
-s|--state)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
STATE="$2"
shift 2
;;
-l|--label)
-l|--label|--labels)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
LABEL="$2"
shift 2
;;
-a|--author)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
AUTHOR="$2"
shift 2
;;
-n|--limit)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
LIMIT="$2"
shift 2
;;
-r|--repo)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
REPO_OVERRIDE="$2"
shift 2
;;
@@ -64,8 +76,7 @@ while [[ $# -gt 0 ]]; do
usage 0
;;
*)
echo "Unknown option: $1" >&2
usage
usage_error "unknown option: $1"
;;
esac
done
@@ -12,13 +12,23 @@ source "$SCRIPT_DIR/detect-platform.sh"
PR_NUMBER=""
OUTPUT_FILE=""
# Usage-error contract (R4): usage errors print to STDERR and exit 2,
# distinct from provider, credential, and verification failures (exit 1).
usage_error() {
echo "Error: $*" >&2
echo "Usage: pr-metadata.sh -n <pr_number> [-o <output_file>] (see --help)" >&2
exit 2
}
while [[ $# -gt 0 ]]; do
case $1 in
-n|--number)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
PR_NUMBER="$2"
shift 2
;;
-o|--output)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
OUTPUT_FILE="$2"
shift 2
;;
@@ -29,18 +39,18 @@ while [[ $# -gt 0 ]]; do
echo " -n, --number PR number (required)"
echo " -o, --output Output file (optional, prints to stdout if omitted)"
echo " -h, --help Show this help"
echo ""
echo "Exit codes: 0 success; 2 usage error (stderr); 1 provider/credential failure."
exit 0
;;
*)
echo "Unknown option: $1" >&2
exit 1
usage_error "unknown option: $1"
;;
esac
done
if [[ -z "$PR_NUMBER" ]]; then
echo "Error: PR number is required (-n)" >&2
exit 1
usage_error "PR number is required (-n/--number)"
fi
write_metadata() {
@@ -39,6 +39,7 @@ source "$SCRIPT_DIR/detect-platform.sh"
PR_NUMBER=""
ACTION=""
COMMENT=""
BODY_FILE=""
LOGIN_OVERRIDE=""
REPO_OVERRIDE=""
HOST_OVERRIDE=""
@@ -71,6 +72,12 @@ while [[ $# -gt 0 ]]; do
COMMENT="$2"
shift 2
;;
--body-file)
# R3: body from file (or '-' = stdin); mutually exclusive with --body.
[[ $# -ge 2 && "$2" != --* ]] || usage_error "option $1 requires a path (or - for stdin)"
BODY_FILE="$2"
shift 2
;;
-l|--login)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
LOGIN_OVERRIDE="$2"
@@ -108,6 +115,19 @@ while [[ $# -gt 0 ]]; do
esac
done
# R3 (2026-08-29): resolve --body-file into COMMENT (file or stdin '-');
# exclusive with an explicit --body/--comment value.
if [[ -n "$BODY_FILE" ]]; then
[[ -z "$COMMENT" ]] || usage_error "--body-file and --body are mutually exclusive"
if [[ "$BODY_FILE" == "-" ]]; then
COMMENT=$(cat) || usage_error "could not read body from stdin"
else
[[ -r "$BODY_FILE" ]] || usage_error "body file not readable: $BODY_FILE"
COMMENT=$(cat "$BODY_FILE") || usage_error "could not read body file: $BODY_FILE"
fi
fi
if [[ -z "$PR_NUMBER" ]]; then
usage_error "PR number is required (-n/--number)"
fi
+14 -4
View File
@@ -11,13 +11,23 @@ source "$SCRIPT_DIR/detect-platform.sh"
PR_NUMBER=""
REPO_OVERRIDE=""
# Usage-error contract (R4): usage errors print to STDERR and exit 2,
# distinct from provider, credential, and verification failures (exit 1).
usage_error() {
echo "Error: $*" >&2
echo "Usage: pr-view.sh -n <pr_number> [-r owner/repo] (see --help)" >&2
exit 2
}
while [[ $# -gt 0 ]]; do
case $1 in
-n|--number)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
PR_NUMBER="$2"
shift 2
;;
-r|--repo)
[[ $# -ge 2 && "$2" != - && "$2" != --* && ! "$2" =~ ^-[[:alnum:]] ]] || usage_error "option $1 requires a value (option-like values are rejected; bare - is reserved)"
REPO_OVERRIDE="$2"
shift 2
;;
@@ -28,18 +38,18 @@ while [[ $# -gt 0 ]]; do
echo " -n, --number PR number (required)"
echo " -r, --repo Repository slug (default: infer from git origin)"
echo " -h, --help Show this help"
echo ""
echo "Exit codes: 0 success; 2 usage error (stderr); 1 provider/credential failure."
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
usage_error "unknown option: $1"
;;
esac
done
if [[ -z "$PR_NUMBER" ]]; then
echo "Error: PR number is required (-n)"
exit 1
usage_error "PR number is required (-n/--number)"
fi
if [[ -n "$REPO_OVERRIDE" ]]; then
@@ -34,20 +34,23 @@ REPO_DIR="$WORK_DIR/repo"
BRAIN_DIR="$WORK_DIR/brain"
SPOOL_DIR="$WORK_DIR/spool"
SVC_STORE="$FAKE_HOME/.config/mosaic/secrets/gitea-tokens"
# Mirror the real deployed layout (~/.config/mosaic/tools/{git,_lib}/) under the
# Mirror the real deployed layout (~/.mosaic/tools/{git,_lib}/) under the
# fake HOME: git-credential-mosaic resolves its credentials.sh sibling via a
# script-relative path (BASH_SOURCE), so the copy must live next to a stubbed
# _lib/credentials.sh, not the real one, to keep this test hermetic.
HELPER="$FAKE_HOME/.config/mosaic/tools/git/git-credential-mosaic"
HELPER="$FAKE_HOME/.mosaic/tools/git/git-credential-mosaic"
IMPL="$FAKE_HOME/.mosaic/tools/git/git-credential-mosaic.impl"
rm -rf "$WORK_DIR"
mkdir -p "$SVC_STORE" \
"$FAKE_HOME/.config/mosaic/tools/git" \
"$FAKE_HOME/.config/mosaic/tools/_lib" \
"$FAKE_HOME/.mosaic/tools/git" \
"$FAKE_HOME/.mosaic/tools/_lib" \
"$REPO_DIR" "$BRAIN_DIR"
cp "$SCRIPT_DIR/git-credential-mosaic" "$HELPER"
chmod +x "$HELPER"
cp "$SCRIPT_DIR/git-credential-mosaic.impl" "$IMPL"
chmod +x "$IMPL"
git -C "$REPO_DIR" init -q
git -C "$REPO_DIR" config user.email "[email protected]"
@@ -55,7 +58,7 @@ git -C "$REPO_DIR" config user.name "Test"
# Fake shared-account credential loader — stands in for
# tools/_lib/credentials.sh's load_credentials(), scoped to this test only.
cat > "$FAKE_HOME/.config/mosaic/tools/_lib/credentials.sh" <<'SH'
cat > "$FAKE_HOME/.mosaic/tools/_lib/credentials.sh" <<'SH'
load_credentials() {
case "$1" in
gitea-mosaicstack) GITEA_URL="https://git.mosaicstack.dev"; GITEA_TOKEN="shared-mosaicstack-token"; export GITEA_URL GITEA_TOKEN; return 0 ;;
@@ -82,7 +85,7 @@ run_helper() {
(
cd "$REPO_DIR"
env -i HOME="$FAKE_HOME" PATH="$PATH" MOSAIC_CREDENTIAL_SPOOL="$SPOOL_DIR" "$@" \
bash "$HELPER" get <<EOF
"$HELPER" get <<EOF
host=$host
username=$username_in
@@ -90,6 +93,80 @@ EOF
)
}
# ── Lineage harness (rev-code-02 F1/F2 rework) ─────────────────────────────
# Establishes a seat CALLER the way production does — frozen into an
# ancestor's exec environment — instead of injecting MOSAIC_AGENT_NAME into
# the helper's own env. Two scripts are generated into WORK_DIR:
#
# lineage-root.sh (pid A): invoked with a fence marker and NO agent name.
# With a non-empty caller arg it forks the carrier (pid C); with an
# empty caller it forks the helper directly (deterministic anonymous
# lineage even when the suite itself runs inside a seat).
# lineage-carrier.sh (pid C): MOSAIC_AGENT_NAME=<caller> frozen at exec;
# forks the helper (pid D) with a fully controlled env.
#
# The helper's walk then sees exactly: self -> C(caller) or D-direct ->
# A(fence, empty name -> stop). EXTRA assignments ride pid D's environment
# (that is where a rewrite would live — which is the point of the F1 arms).
cat > "$WORK_DIR/lineage-root.sh" <<'LINROOT'
#!/usr/bin/env bash
# pid A — lineage root. Args: <caller> <carrier-script> <helper> <spool>
# <brain> <repo> [extra KEY=VALUE...]
set -u
caller="$1"; carrier="$2"; helper="$3"; spool="$4"; brain="$5"; repo="$6"; shift 6
if [ -n "$caller" ]; then
env MOSAIC_AGENT_NAME="$caller" PATH="$PATH" HOME="$HOME" \
bash "$carrier" "$helper" "$spool" "$brain" "$repo" "$@"
else
env -i HOME="$HOME" PATH="$PATH" MOSAIC_CREDENTIAL_SPOOL="$spool" \
MOSAIC_BRAIN_HOME="$brain" "$@" "$helper" get
fi
LINROOT
cat > "$WORK_DIR/lineage-carrier.sh" <<'LINCARR'
#!/usr/bin/env bash
# pid C — the caller's frozen environment. Forks the helper (pid D).
set -u
helper="$1"; spool="$2"; brain="$3"; repo="$4"; shift 4
cd "$repo"
env -i HOME="$HOME" PATH="$PATH" MOSAIC_CREDENTIAL_SPOOL="$spool" \
MOSAIC_BRAIN_HOME="$brain" "$@" "$helper" get
LINCARR
chmod +x "$WORK_DIR/lineage-root.sh" "$WORK_DIR/lineage-carrier.sh"
run_lineage() {
# run_lineage <caller|empty-for-anonymous> [helper-env KEY=VALUE...]
local caller="$1"; shift
printf 'host=git.mosaicstack.dev\nusername=probe\n\n' | \
env -i HOME="$FAKE_HOME" PATH="$PATH" MOSAIC_CREDENTIAL_LINEAGE_FENCE=1 \
bash "$WORK_DIR/lineage-root.sh" "$caller" "$WORK_DIR/lineage-carrier.sh" \
"$HELPER" "$SPOOL_DIR" "$BRAIN_DIR" "$REPO_DIR" "$@"
}
assert_refused_lineage() {
# assert_refused_lineage <desc> <caller> <want-reason> [helper-env...]
local desc="$1" caller="$2" want="$3"; shift 3
local stderr_file="$WORK_DIR/stderr-lin.tmp" rc stdout
: > "$stderr_file"
set +e
stdout=$(run_lineage "$caller" "$@" 2>"$stderr_file")
rc=$?
set -e
local stderr; stderr=$(cat "$stderr_file")
if [[ "$rc" -eq 0 ]]; then
echo "FAIL: $desc — expected nonzero exit, got 0 (stdout='$stdout')" >&2; fail=1
fi
if [[ -n "$stdout" ]]; then
echo "FAIL: $desc — expected empty stdout, got '$stdout'" >&2; fail=1
fi
if [[ -n "$want" && "$stderr" != *"$want"* ]]; then
echo "FAIL: $desc — stderr lacks '$want':" >&2; echo "$stderr" >&2; fail=1
fi
if [[ "$stdout$stderr" == *"seatG-slot-token"* || "$stdout$stderr" == *"seatE-slot-token"* \
|| "$stdout$stderr" == *"shared-mosaicstack-token"* || "$stdout$stderr" == *"shared-usc-token"* ]]; then
echo "FAIL: $desc — a slot or shared token VALUE appeared in output" >&2; fail=1
fi
}
# A refusal must be observable in four independent ways: nonzero exit, EMPTY
# stdout, a stderr diagnostic naming the identity and host, and — the assertion
# that actually catches a regression to the old behavior — NO shared token value
@@ -222,7 +299,10 @@ fi
# ---------------------------------------------------------------------------
mkdir -p "$BRAIN_DIR/fleet/agents/seatE/secrets"
echo -n "seatE-slot-token" > "$BRAIN_DIR/fleet/agents/seatE/secrets/gitea-mosaicstack-seatE.token"
out=$(run_helper "git.mosaicstack.dev" "seatE" MOSAIC_BRAIN_HOME="$BRAIN_DIR")
chmod 600 "$BRAIN_DIR/fleet/agents/seatE/secrets/gitea-mosaicstack-seatE.token"
# Seat arms run through the lineage harness below (rev-code-02 F1/F2 rework):
# a seat caller must be established by ancestry, not by the helper's own env.
out=$(run_lineage seatE MOSAIC_AGENT_NAME=seatE MOSAIC_GIT_IDENTITY=seatE)
assert_eq "seat reads its own slot: username" "username=seatE" "$(echo "$out" | grep '^username=')"
assert_eq "seat reads its own slot: password" "password=seatE-slot-token" "$(echo "$out" | grep '^password=')"
@@ -236,8 +316,8 @@ assert_eq "seat reads its own slot: password" "password=seatE-slot-token" "$(ech
# ---------------------------------------------------------------------------
mkdir -p "$BRAIN_DIR/fleet/agents/seatF/secrets"
echo -n "seatF-SERVICE-STORE-token" > "$SVC_STORE/gitea-mosaicstack-seatF.token"
assert_fail_closed "seat with empty slot does NOT fall back to the framework store" \
"git.mosaicstack.dev" "seatF" "fleet/agents/seatF/secrets" MOSAIC_BRAIN_HOME="$BRAIN_DIR"
assert_refused_lineage "seat with empty slot does NOT fall back to the framework store" \
seatF no-token-for-identity MOSAIC_AGENT_NAME=seatF MOSAIC_GIT_IDENTITY=seatF
: > "$WORK_DIR/stderr.tmp"
set +e
xstore_out=$(run_helper "git.mosaicstack.dev" "seatF" MOSAIC_BRAIN_HOME="$BRAIN_DIR" 2>"$WORK_DIR/stderr.tmp")
@@ -288,7 +368,7 @@ assert_eq "unknown host on a fleet host: still passthrough, not a refusal" "" "$
# 13. Non-"get" verb (store/erase) -> exit 0, no output (git-credential
# protocol: this helper only implements get).
# ---------------------------------------------------------------------------
store_out=$(cd "$REPO_DIR" && env -i HOME="$FAKE_HOME" PATH="$PATH" bash "$HELPER" store <<EOF
store_out=$(cd "$REPO_DIR" && env -i HOME="$FAKE_HOME" PATH="$PATH" "$HELPER" store <<EOF
host=git.mosaicstack.dev
username=no-such-agent
password=whatever
@@ -310,7 +390,7 @@ hostile_spool="$WORK_DIR/spool-hostile"
cd "$hostile_dir"
env -i HOME="$FAKE_HOME" PATH="$PATH" MOSAIC_CREDENTIAL_SPOOL="$hostile_spool" \
MOSAIC_GIT_IDENTITY=no-such-agent \
bash "$HELPER" get <<EOF >/dev/null 2>&1
"$HELPER" get <<EOF >/dev/null 2>&1
host=git.mosaicstack.dev
username=no-such-agent
@@ -349,7 +429,7 @@ nospool_err=$(
cd "$REPO_DIR"
env -i HOME="$FAKE_HOME" PATH="$PATH" MOSAIC_CREDENTIAL_SPOOL="$unwritable_spool" \
MOSAIC_GIT_IDENTITY=no-such-agent \
bash "$HELPER" get <<EOF 2>&1 >/dev/null
"$HELPER" get <<EOF 2>&1 >/dev/null
host=git.mosaicstack.dev
username=no-such-agent
@@ -365,6 +445,193 @@ if [[ "$nospool_err" != *"NOT WRITTEN"* ]]; then
fail=1
fi
# ---------------------------------------------------------------------------
# 11. P5-RM-006 (+r1 rework) — caller identity from exec-frozen ancestry.
# A seat caller is established by lineage, not by the helper's own env;
# disagreement anywhere in the lineage is a rewrite and refuses; an
# anonymous caller resolves NOTHING on a fleet host (seat or service);
# a loose seat-slot mode refuses. Enforcement-removal red control at 11h.
# ---------------------------------------------------------------------------
mkdir -p "$BRAIN_DIR/fleet/agents/seatG/secrets"
echo -n "seatG-slot-token" > "$BRAIN_DIR/fleet/agents/seatG/secrets/gitea-mosaicstack-seatG.token"
chmod 600 "$BRAIN_DIR/fleet/agents/seatG/secrets/gitea-mosaicstack-seatG.token"
# 11a. Cross-seat negative (lineage seatE, env ident=seatG): still refused.
assert_refused_lineage "seat cannot override identity to another seat's slot" \
seatE cross-seat-identity-refused MOSAIC_GIT_IDENTITY=seatG
# 11b. Seat asking for a SERVICE identity: cross-seat territory.
assert_refused_lineage "seat cannot resolve a service identity either" \
seatE cross-seat-identity-refused MOSAIC_GIT_IDENTITY=agentA
# 11c. Anonymous caller asking for a seat slot is refused.
assert_refused_lineage "anonymous caller cannot resolve a seat slot on a fleet host" \
"" anonymous-credential-refused MOSAIC_GIT_IDENTITY=seatG
# 11d. Anonymous caller asking for a SERVICE identity: ALSO refused
# (rev-code-02 F2 — credentialed services are seats; the legacy store is
# not anonymously reachable on fleet hosts).
assert_refused_lineage "anonymous caller cannot resolve a legacy service credential either" \
"" anonymous-credential-refused MOSAIC_GIT_IDENTITY=agentA
# 11e. Slot permissions: a group-readable slot is refused; mode restored -> serves.
chmod 644 "$BRAIN_DIR/fleet/agents/seatG/secrets/gitea-mosaicstack-seatG.token"
assert_refused_lineage "loose slot mode is refused" \
seatG slot-permission-violation MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG
chmod 600 "$BRAIN_DIR/fleet/agents/seatG/secrets/gitea-mosaicstack-seatG.token"
out=$(run_lineage seatG MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG)
assert_eq "mode restored to 600: seatG serves again" "password=seatG-slot-token" "$(echo "$out" | grep '^password=')"
# 11f. rev-code-02 F1 repro: dual-variable override (caller lineage seatE,
# helper env carrying MOSAIC_AGENT_NAME=seatG AND MOSAIC_GIT_IDENTITY=seatG).
assert_refused_lineage "F1: dual MOSAIC_AGENT_NAME+MOSAIC_GIT_IDENTITY override refused" \
seatE caller-identity-spoof-refused MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG
# 11g. Stripped lineage still serves the rightful seat: caller frozen at the
# ancestor, helper env clean (self empty), own ident.
out=$(run_lineage seatE MOSAIC_GIT_IDENTITY=seatE)
assert_eq "lineage consensus with stripped self still serves the owning seat" \
"password=seatE-slot-token" "$(echo "$out" | grep '^password=')"
# 11h. RED CONTROL: delete the ancestry binding between markers from a copy
# of the IMPLEMENTATION (run directly with the clean marker — a red control
# measures the binding itself, deliberately not through the wrapper);
# the F1 dual-override request must then RESOLVE seatG's token — the
# exact measured failure — proving the binding is the enforcement.
RED_HELPER="$WORK_DIR/red/git-credential-mosaic.impl"
mkdir -p "$WORK_DIR/red"
sed '/P5-RM-006r1 ancestry binding begin/,/P5-RM-006r1 ancestry binding end/d' "$IMPL" > "$RED_HELPER"
chmod +x "$RED_HELPER"
if cmp -s "$IMPL" "$RED_HELPER"; then
echo "FAIL: red control is vacuous — marker deletion removed nothing" >&2
fail=1
fi
set +e
red_out=$(printf 'host=git.mosaicstack.dev\nusername=probe\n\n' | \
env -i HOME="$FAKE_HOME" PATH="$PATH" MOSAIC_CREDENTIAL_LINEAGE_FENCE=1 \
bash "$WORK_DIR/lineage-root.sh" seatE "$WORK_DIR/lineage-carrier.sh" \
"$RED_HELPER" "$SPOOL_DIR" "$BRAIN_DIR" "$REPO_DIR" \
_MOSAIC_HELPER_CLEAN=1 MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG 2>/dev/null)
set -e
if [[ "$(echo "$red_out" | grep '^password=')" != "password=seatG-slot-token" ]]; then
echo "FAIL: red control — with the binding removed, the F1 dual-override should have resolved seatG's token, got: $red_out" >&2
fail=1
else
echo "ok: red control — binding removed -> F1 dual-override resolves the victim token (the binding is the enforcement)"
fi
# 11h2. rev-code-02 R1 F1: PATH-shadowed tr/sed/head/grep must not forge the
# ancestry. The dual-override request runs with a hostile PATH whose
# utilities claim the victim name for every /proc read; the walker uses
# only bash builtins, so the shadows never execute and the refusal holds.
HOSTILE_BIN="$WORK_DIR/hostile-bin"
mkdir -p "$HOSTILE_BIN"
for tool in tr sed head grep cat stat; do
printf '#!/usr/bin/env bash\ncat >/dev/null\necho "MOSAIC_AGENT_NAME=seatG"\nexit 0\n' > "$HOSTILE_BIN/$tool"
chmod +x "$HOSTILE_BIN/$tool"
done
assert_refused_lineage "F1-R1: hostile PATH utilities cannot forge ancestry (dual override still refused)" \
seatE caller-identity-spoof-refused \
MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG PATH="$HOSTILE_BIN:$PATH"
# 11i. Service automation integration arm (rev-code-02 R2 bar): a SERVICE
# seat bound by lineage resolves its own slot — the brain-git-sync shape.
mkdir -p "$BRAIN_DIR/fleet/agents/svc-fixture/secrets"
echo -n "svc-fixture-slot-token" > "$BRAIN_DIR/fleet/agents/svc-fixture/secrets/gitea-mosaicstack-svc-fixture.token"
chmod 600 "$BRAIN_DIR/fleet/agents/svc-fixture/secrets/gitea-mosaicstack-svc-fixture.token"
out=$(run_lineage svc-fixture MOSAIC_AGENT_NAME=svc-fixture MOSAIC_GIT_IDENTITY=svc-fixture)
assert_eq "service automation with bound seat lineage resolves its own slot" \
"password=svc-fixture-slot-token" "$(echo "$out" | grep '^password=')"
# 11j. rev-code-02 R3 B1: BASH_ENV shaping. A read() shadow defined through
# BASH_ENV must be refused before any resolution — the guard scrubs and
# refuses with bash-environment-injection-refused.
INJ_SH="$WORK_DIR/inj-read.sh"
printf 'read() { builtin read -r _x || return 0; printf "MOSAIC_AGENT_NAME=seatG\\n"; return 0; }\n' > "$INJ_SH"
assert_refused_lineage "F1-R3: BASH_ENV read() shadow is dropped at the wrapper boundary (identity gate governs)" \
seatE caller-identity-spoof-refused \
MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG BASH_ENV="$INJ_SH"
# 11k. rev-code-02 R3 B1: exported functions (BASH_FUNC_* import) refused too.
assert_refused_lineage "F1-R3: exported BASH_FUNC_* import never crosses the wrapper boundary (identity gate governs)" \
seatE caller-identity-spoof-refused \
MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG \
'BASH_FUNC_read%%=() { builtin read -r _x || return 0; printf "MOSAIC_AGENT_NAME=seatG\\n"; return 0; }'
# 11l. rev-code-02 R3 B2: hostile stat cannot launder a loose slot. Own-slot
# lineage (legit caller), 0644 slot, PATH-shadowed stat reporting 600 —
# mode inspection must come from the trusted PATH and still refuse.
chmod 644 "$BRAIN_DIR/fleet/agents/svc-fixture/secrets/gitea-mosaicstack-svc-fixture.token"
printf '#!/usr/bin/env bash\necho 600\n' > "$HOSTILE_BIN/stat"
assert_refused_lineage "F2-R3: hostile stat cannot make a 0644 slot pass as 600 (own-slot path)" \
svc-fixture slot-permission-violation \
MOSAIC_AGENT_NAME=svc-fixture MOSAIC_GIT_IDENTITY=svc-fixture PATH="$HOSTILE_BIN:$PATH"
chmod 600 "$BRAIN_DIR/fleet/agents/svc-fixture/secrets/gitea-mosaicstack-svc-fixture.token"
# 11m. rev-code-02 R3 probe 1 through the PRODUCTION ENTRYPOINT: BASH_ENV
# defines unset()/exit() no-ops (defeating in-bash scrub/termination).
# The python wrapper never passes BASH_ENV across the boundary, so the
# implementation cannot be shaped and the dual override still refuses.
INJ_P1="$WORK_DIR/inj-probe1.sh"
cat > "$INJ_P1" <<'P1'
unset() { return 0; }
exit() { return 0; }
read() { builtin read -r _x || return 0; printf 'MOSAIC_AGENT_NAME=seatG\n'; return 0; }
P1
assert_refused_lineage "F1-R4 probe1: BASH_ENV unset/exit no-ops cannot shape the helper (wrapper boundary)" \
seatE caller-identity-spoof-refused \
MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG BASH_ENV="$INJ_P1"
# 11n. rev-code-02 R3 probe 2 through the PRODUCTION ENTRYPOINT: declare()
# hides imported functions, unsets the marker, printf() forges the
# ancestry. Dropped at the wrapper boundary; refusal holds.
INJ_P2="$WORK_DIR/inj-probe2.sh"
cat > "$INJ_P2" <<'P2'
declare() { return 0; }
printf() { builtin printf '%s' "MOSAIC_AGENT_NAME=seatG"; return 0; }
read() { builtin read -r _x || return 0; printf 'MOSAIC_AGENT_NAME=seatG\n'; return 0; }
P2
assert_refused_lineage "F1-R4 probe2: declare-hide + printf-forge cannot shape the helper (wrapper boundary)" \
seatE caller-identity-spoof-refused \
MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG BASH_ENV="$INJ_P2"
# 11o. RED CONTROL for the wrapper boundary (enforcement-removal): invoke the
# IMPLEMENTATION directly, bypassing the wrapper, with the PROBE-1 shape
# (unset/exit no-ops) and a forged clean marker — exactly the falsified
# in-bash world the reviewer measured: the refusal prints, exit is
# no-oped, execution continues, and the forged ancestry SERVES seatG.
# The wrapper boundary is the enforcement; this arm proves it bites.
set +e
bypass_out=$(cd "$REPO_DIR" && printf 'host=git.mosaicstack.dev\nusername=probe\n\n' | \
env -i HOME="$FAKE_HOME" PATH="$PATH" MOSAIC_CREDENTIAL_SPOOL="$SPOOL_DIR" \
MOSAIC_BRAIN_HOME="$BRAIN_DIR" _MOSAIC_HELPER_CLEAN=1 BASH_ENV="$INJ_P1" \
MOSAIC_AGENT_NAME=seatG MOSAIC_GIT_IDENTITY=seatG \
bash "$IMPL" get 2>/dev/null)
set -e
if [[ "$(echo "$bypass_out" | grep -c '^password=')" -lt 1 ]]; then
echo "FAIL: wrapper red control — direct shaped .impl should have served (wrapper is the enforcement), got: $bypass_out" >&2
fail=1
else
echo "ok: red control — wrapper bypassed + probe1 shape serves (the wrapper boundary is the enforcement)"
fi
# 11p. Direct .impl invocation WITHOUT the clean marker: refused by the
# implementation's own entrypoint assert.
set +e
direct_out=$(cd "$REPO_DIR" && printf 'host=git.mosaicstack.dev\nusername=probe\n\n' | \
env -i HOME="$FAKE_HOME" PATH="$PATH" MOSAIC_CREDENTIAL_SPOOL="$SPOOL_DIR" \
MOSAIC_BRAIN_HOME="$BRAIN_DIR" MOSAIC_GIT_IDENTITY=seatE \
bash "$IMPL" get 2>"$WORK_DIR/stderr-direct.tmp")
direct_rc=$?
set -e
if [[ "$direct_rc" -eq 0 || -n "$direct_out" ]]; then
echo "FAIL: direct .impl without marker must refuse (got rc=$direct_rc out='$direct_out')" >&2
fail=1
elif ! grep -q 'direct-entrypoint-refused' "$WORK_DIR/stderr-direct.tmp"; then
echo "FAIL: direct .impl refusal lacks direct-entrypoint-refused" >&2
fail=1
fi
if [[ "$fail" -eq 0 ]]; then
echo "git-credential-mosaic identity resolution regression passed"
fi
@@ -161,6 +161,30 @@ rc=0
grep -q "GitHub comment write failed" "$ERR_FILE" || fail "GitHub path: normalized error missing from stderr"
grep -q "^gh issue comment" "$PROBE_LOG" || fail "GitHub path: gh write was not invoked"
# R3 body-file arms (2026-08-29): --body-file <path> and '-' (stdin).
BF_FILE="$WORK_DIR/body.md"
printf 'line one\nline two\n' > "$BF_FILE"
# File loads the body: parse acceptance then credential-class failure
# (sandboxed runner: rc nonzero and NOT 2).
rc=0
run_wrapper_sandboxed -i 5 --body-file "$BF_FILE" >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -ne 0 ]] || fail "body-file arm unexpectedly succeeded in the sandbox"
[[ "$rc" -ne 2 ]] || fail "body-file arm misclassified credential failure as a usage error"
# Stdin form loads the body the same way.
rc=0
printf 'from stdin' | run_wrapper_sandboxed -i 5 --body-file - >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -ne 0 && "$rc" -ne 2 ]] || fail "body-file stdin arm rc=$rc (want nonzero, not 2)"
# Mutually exclusive with --body: rc 2.
expect_rc 2 "body-file + body exclusive" -i 5 --body-file "$BF_FILE" -b explicit
expect_stderr "mutually exclusive" "exclusivity message on stderr"
# Missing file: rc 2 naming the path.
expect_rc 2 "missing body file" -i 5 --body-file "$WORK_DIR/nope.md"
expect_stderr "not readable" "missing-file message on stderr"
# 7. No provider contact from any usage-error arm (arm 6b's deliberate gh
# invocation is the only permitted entry in the probe log).
if grep -v '^gh issue comment' "$PROBE_LOG" | grep -q .; then
@@ -0,0 +1,132 @@
#!/usr/bin/env bash
# Usage-error contract for pr-diff.sh (R4, 2026-08-28).
#
# issue-edit already uses long-flag-first parsing (-i/--issue, -t/--title,
# -b/--body, -l/--labels, -m/--milestone); this adds the rc=2 usage-error
# contract, value checks, and the no-provider-contact proof. Required: -i.
#
# Arms:
# 1. --help and -h exit 0 and print usage.
# 2. Unknown option exits 2 with the message on stderr.
# 3. Missing required -i exits 2 (stderr).
# 4. A value-less flag (-i -b -c and long forms) exits 2 (stderr).
# 5. -b and -c both pass parsing (sandboxed runner: the run then fails
# at credential resolution, nonzero and NOT 2) — no real token is
# ever read and no provider is contacted.
# 6. No arm performs any provider request (PATH shims record every
# invocation; the probe log must stay empty).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORK_DIR="${MOSAIC_TEST_WORK_DIR:-$PWD/.mosaic-test-work/pr-diff-usage}"
BIN_DIR="$WORK_DIR/bin"
PROBE_LOG="$WORK_DIR/provider-probes.log"
OUT_FILE="$WORK_DIR/out.log"
ERR_FILE="$WORK_DIR/err.log"
cleanup() {
rm -rf "$WORK_DIR"
}
trap cleanup EXIT
mkdir -p "$BIN_DIR"
: > "$PROBE_LOG"
# Unlike the issue suites, these stubs FAIL (exit 99): pr-close has an
# API fallback that treats a successful curl as a closed PR, so exit-0
# stubs would let the sandbox arms "succeed" (measured 2026-08-28).
for tool in gh tea curl; do
cat > "$BIN_DIR/$tool" <<STUB
#!/usr/bin/env bash
echo "$tool \$*" >> "$PROBE_LOG"
exit 99
STUB
chmod +x "$BIN_DIR/$tool"
done
run_wrapper() {
( cd "$WORK_DIR" && PATH="$BIN_DIR:$PATH" "$SCRIPT_DIR/pr-diff.sh" "$@" )
}
# Hermetic variant: neutralizes every identity/credential source the wrapper
# consults so parse-acceptance arms fail at credential resolution in ANY cwd
# repo (see test-issue-comment-usage-contract.sh for the measured incident).
run_wrapper_sandboxed() {
mkdir -p "$WORK_DIR/home" "$WORK_DIR/xdg"
(
cd "$WORK_DIR"
PATH="$BIN_DIR:$PATH" HOME="$WORK_DIR/home" XDG_CONFIG_HOME="$WORK_DIR/xdg" \
MOSAIC_GIT_IDENTITY="" MOSAIC_BRAIN_HOME="" \
"$SCRIPT_DIR/pr-diff.sh" "$@"
)
}
fail() {
echo "FAIL: $*" >&2
echo "--- stderr ---" >&2
cat "$ERR_FILE" >&2
exit 1
}
expect_rc() { # expect_rc <want> <desc> <args...>
local want="$1" desc="$2" rc=0
shift 2
run_wrapper "$@" >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -eq "$want" ]] || fail "$desc: rc=$rc, want $want"
}
expect_stderr() { # expect_stderr <pattern> <desc>
grep -q "$1" "$ERR_FILE" || fail "$2: stderr missing '$1'"
}
# 1. Help exits 0 and prints usage.
expect_rc 0 "--help exits 0" --help
grep -q "Usage: pr-diff.sh" "$OUT_FILE" || fail "--help did not print usage"
expect_rc 0 "-h exits 0" -h
# 2. Unknown option: rc 2, stderr.
expect_rc 2 "unknown option exits 2" --bogus
expect_stderr "[Uu]nknown option" "unknown option names itself on stderr"
# 3. Missing required PR number: rc 2, stderr.
expect_rc 2 "missing -n exits 2"
expect_stderr "PR number is required" "missing -n message on stderr"
# 4. Value-less flags: rc 2 with "requires a value" on stderr.
for flag in -n -o -r --number --output --repo --host; do
expect_rc 2 "value-less $flag exits 2" "$flag"
expect_stderr "requires a value" "value-less $flag message on stderr"
done
# 4a. An option-like value is a MISSING value, not a value (codex PR #1464:
# -b --help previously consumed --help as the body and performed the write).
expect_rc 2 "option-like value rejected" -n --help
expect_rc 2 "short flag value rejected" -n -h
expect_stderr "requires a value" "short flag value message on stderr"
expect_stderr "requires a value" "option-like value message on stderr"
# 4b. Parser-failure arms (1-4) must have performed ZERO provider contact.
if [[ -s "$PROBE_LOG" ]]; then
echo "FAIL: a parser-failure arm contacted a provider:" >&2
cat "$PROBE_LOG" >&2
exit 1
fi
# 5. Alias acceptance under the sandbox: both -b and -c carry a value past
# parsing; the run fails at credential resolution nonzero and NOT 2.
for flag in -n; do
rc=0
run_wrapper_sandboxed -n 5 >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -ne 0 ]] || fail "$flag arm unexpectedly succeeded in the sandbox"
[[ "$rc" -ne 2 ]] || fail "$flag arm misclassified credential failure as a usage error"
done
# 6. Post-sandbox provider assertions are intentionally NOT applied here:
# pr-close's gitea path attempts a tea WRITE (tea pr comment) when a
# comment parses, then falls back to the API. Hermeticity for this
# wrapper comes from the FAILING stubs (exit 99), not from non-contact —
# the arm above proves only parse acceptance and non-usage classification.
# Parser-failure arms (1-4) remain zero-contact (asserted at 4b).
echo "pr-diff.sh usage-contract regression passed (R1/R4)"
@@ -0,0 +1,131 @@
#!/usr/bin/env bash
# Usage-error contract for pr-list.sh (R4, 2026-08-28).
#
# issue-edit already uses long-flag-first parsing (-i/--issue, -t/--title,
# -b/--body, -l/--labels, -m/--milestone); this adds the rc=2 usage-error
# contract, value checks, and the no-provider-contact proof. Required: -i.
#
# Arms:
# 1. --help and -h exit 0 and print usage.
# 2. Unknown option exits 2 with the message on stderr.
# 3. Missing required -i exits 2 (stderr).
# 4. A value-less flag (-i -b -c and long forms) exits 2 (stderr).
# 5. -b and -c both pass parsing (sandboxed runner: the run then fails
# at credential resolution, nonzero and NOT 2) — no real token is
# ever read and no provider is contacted.
# 6. No arm performs any provider request (PATH shims record every
# invocation; the probe log must stay empty).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORK_DIR="${MOSAIC_TEST_WORK_DIR:-$PWD/.mosaic-test-work/pr-list-usage}"
BIN_DIR="$WORK_DIR/bin"
PROBE_LOG="$WORK_DIR/provider-probes.log"
OUT_FILE="$WORK_DIR/out.log"
ERR_FILE="$WORK_DIR/err.log"
cleanup() {
rm -rf "$WORK_DIR"
}
trap cleanup EXIT
mkdir -p "$BIN_DIR"
: > "$PROBE_LOG"
# Unlike the issue suites, these stubs FAIL (exit 99): pr-close has an
# API fallback that treats a successful curl as a closed PR, so exit-0
# stubs would let the sandbox arms "succeed" (measured 2026-08-28).
for tool in gh tea curl; do
cat > "$BIN_DIR/$tool" <<STUB
#!/usr/bin/env bash
echo "$tool \$*" >> "$PROBE_LOG"
exit 99
STUB
chmod +x "$BIN_DIR/$tool"
done
run_wrapper() {
( cd "$WORK_DIR" && PATH="$BIN_DIR:$PATH" "$SCRIPT_DIR/pr-list.sh" "$@" )
}
# Hermetic variant: neutralizes every identity/credential source the wrapper
# consults so parse-acceptance arms fail at credential resolution in ANY cwd
# repo (see test-issue-comment-usage-contract.sh for the measured incident).
run_wrapper_sandboxed() {
mkdir -p "$WORK_DIR/home" "$WORK_DIR/xdg"
(
cd "$WORK_DIR"
PATH="$BIN_DIR:$PATH" HOME="$WORK_DIR/home" XDG_CONFIG_HOME="$WORK_DIR/xdg" \
MOSAIC_GIT_IDENTITY="" MOSAIC_BRAIN_HOME="" \
"$SCRIPT_DIR/pr-list.sh" "$@"
)
}
fail() {
echo "FAIL: $*" >&2
echo "--- stderr ---" >&2
cat "$ERR_FILE" >&2
exit 1
}
expect_rc() { # expect_rc <want> <desc> <args...>
local want="$1" desc="$2" rc=0
shift 2
run_wrapper "$@" >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -eq "$want" ]] || fail "$desc: rc=$rc, want $want"
}
expect_stderr() { # expect_stderr <pattern> <desc>
grep -q "$1" "$ERR_FILE" || fail "$2: stderr missing '$1'"
}
# 1. Help exits 0 and prints usage.
expect_rc 0 "--help exits 0" --help
grep -q "Usage: pr-list.sh" "$OUT_FILE" || fail "--help did not print usage"
expect_rc 0 "-h exits 0" -h
# 2. Unknown option: rc 2, stderr.
expect_rc 2 "unknown option exits 2" --bogus
expect_stderr "[Uu]nknown option" "unknown option names itself on stderr"
# 3. Missing required PR number: rc 2, stderr.
# 4. Value-less flags: rc 2 with "requires a value" on stderr.
for flag in -s -l -a -n -r --state --label --author --limit --repo; do
expect_rc 2 "value-less $flag exits 2" "$flag"
expect_stderr "requires a value" "value-less $flag message on stderr"
done
# 4a. An option-like value is a MISSING value, not a value (codex PR #1464:
# -b --help previously consumed --help as the body and performed the write).
expect_rc 2 "option-like value rejected" -s --help
expect_rc 2 "short flag value rejected" -s -h
expect_stderr "requires a value" "short flag value message on stderr"
expect_stderr "requires a value" "option-like value message on stderr"
# 4b. Parser-failure arms (1-4) must have performed ZERO provider contact.
if [[ -s "$PROBE_LOG" ]]; then
echo "FAIL: a parser-failure arm contacted a provider:" >&2
cat "$PROBE_LOG" >&2
exit 1
fi
# 5. Alias acceptance under the sandbox: both -b and -c carry a value past
# parsing; the run fails at credential resolution nonzero and NOT 2.
for flag in -s; do
rc=0
run_wrapper_sandboxed -s open >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -ne 0 ]] || fail "$flag arm unexpectedly succeeded in the sandbox"
[[ "$rc" -ne 2 ]] || fail "$flag arm misclassified credential failure as a usage error"
done
# 6. Post-sandbox provider assertions are intentionally NOT applied here:
# pr-close's gitea path attempts a tea WRITE (tea pr comment) when a
# comment parses, then falls back to the API. Hermeticity for this
# wrapper comes from the FAILING stubs (exit 99), not from non-contact —
# the arm above proves only parse acceptance and non-usage classification.
# Parser-failure arms (1-4) remain zero-contact (asserted at 4b).
echo "pr-list.sh usage-contract regression passed (R1/R4)"
@@ -0,0 +1,132 @@
#!/usr/bin/env bash
# Usage-error contract for pr-metadata.sh (R4, 2026-08-28).
#
# issue-edit already uses long-flag-first parsing (-i/--issue, -t/--title,
# -b/--body, -l/--labels, -m/--milestone); this adds the rc=2 usage-error
# contract, value checks, and the no-provider-contact proof. Required: -i.
#
# Arms:
# 1. --help and -h exit 0 and print usage.
# 2. Unknown option exits 2 with the message on stderr.
# 3. Missing required -i exits 2 (stderr).
# 4. A value-less flag (-i -b -c and long forms) exits 2 (stderr).
# 5. -b and -c both pass parsing (sandboxed runner: the run then fails
# at credential resolution, nonzero and NOT 2) — no real token is
# ever read and no provider is contacted.
# 6. No arm performs any provider request (PATH shims record every
# invocation; the probe log must stay empty).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORK_DIR="${MOSAIC_TEST_WORK_DIR:-$PWD/.mosaic-test-work/pr-metadata-usage}"
BIN_DIR="$WORK_DIR/bin"
PROBE_LOG="$WORK_DIR/provider-probes.log"
OUT_FILE="$WORK_DIR/out.log"
ERR_FILE="$WORK_DIR/err.log"
cleanup() {
rm -rf "$WORK_DIR"
}
trap cleanup EXIT
mkdir -p "$BIN_DIR"
: > "$PROBE_LOG"
# Unlike the issue suites, these stubs FAIL (exit 99): pr-close has an
# API fallback that treats a successful curl as a closed PR, so exit-0
# stubs would let the sandbox arms "succeed" (measured 2026-08-28).
for tool in gh tea curl; do
cat > "$BIN_DIR/$tool" <<STUB
#!/usr/bin/env bash
echo "$tool \$*" >> "$PROBE_LOG"
exit 99
STUB
chmod +x "$BIN_DIR/$tool"
done
run_wrapper() {
( cd "$WORK_DIR" && PATH="$BIN_DIR:$PATH" "$SCRIPT_DIR/pr-metadata.sh" "$@" )
}
# Hermetic variant: neutralizes every identity/credential source the wrapper
# consults so parse-acceptance arms fail at credential resolution in ANY cwd
# repo (see test-issue-comment-usage-contract.sh for the measured incident).
run_wrapper_sandboxed() {
mkdir -p "$WORK_DIR/home" "$WORK_DIR/xdg"
(
cd "$WORK_DIR"
PATH="$BIN_DIR:$PATH" HOME="$WORK_DIR/home" XDG_CONFIG_HOME="$WORK_DIR/xdg" \
MOSAIC_GIT_IDENTITY="" MOSAIC_BRAIN_HOME="" \
"$SCRIPT_DIR/pr-metadata.sh" "$@"
)
}
fail() {
echo "FAIL: $*" >&2
echo "--- stderr ---" >&2
cat "$ERR_FILE" >&2
exit 1
}
expect_rc() { # expect_rc <want> <desc> <args...>
local want="$1" desc="$2" rc=0
shift 2
run_wrapper "$@" >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -eq "$want" ]] || fail "$desc: rc=$rc, want $want"
}
expect_stderr() { # expect_stderr <pattern> <desc>
grep -q "$1" "$ERR_FILE" || fail "$2: stderr missing '$1'"
}
# 1. Help exits 0 and prints usage.
expect_rc 0 "--help exits 0" --help
grep -q "Usage: pr-metadata.sh" "$OUT_FILE" || fail "--help did not print usage"
expect_rc 0 "-h exits 0" -h
# 2. Unknown option: rc 2, stderr.
expect_rc 2 "unknown option exits 2" --bogus
expect_stderr "[Uu]nknown option" "unknown option names itself on stderr"
# 3. Missing required PR number: rc 2, stderr.
expect_rc 2 "missing -n exits 2"
expect_stderr "PR number is required" "missing -n message on stderr"
# 4. Value-less flags: rc 2 with "requires a value" on stderr.
for flag in -n -o --number --output; do
expect_rc 2 "value-less $flag exits 2" "$flag"
expect_stderr "requires a value" "value-less $flag message on stderr"
done
# 4a. An option-like value is a MISSING value, not a value (codex PR #1464:
# -b --help previously consumed --help as the body and performed the write).
expect_rc 2 "option-like value rejected" -n --help
expect_rc 2 "short flag value rejected" -n -h
expect_stderr "requires a value" "short flag value message on stderr"
expect_stderr "requires a value" "option-like value message on stderr"
# 4b. Parser-failure arms (1-4) must have performed ZERO provider contact.
if [[ -s "$PROBE_LOG" ]]; then
echo "FAIL: a parser-failure arm contacted a provider:" >&2
cat "$PROBE_LOG" >&2
exit 1
fi
# 5. Alias acceptance under the sandbox: both -b and -c carry a value past
# parsing; the run fails at credential resolution nonzero and NOT 2.
for flag in -n; do
rc=0
run_wrapper_sandboxed -n 5 >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -ne 0 ]] || fail "$flag arm unexpectedly succeeded in the sandbox"
[[ "$rc" -ne 2 ]] || fail "$flag arm misclassified credential failure as a usage error"
done
# 6. Post-sandbox provider assertions are intentionally NOT applied here:
# pr-close's gitea path attempts a tea WRITE (tea pr comment) when a
# comment parses, then falls back to the API. Hermeticity for this
# wrapper comes from the FAILING stubs (exit 99), not from non-contact —
# the arm above proves only parse acceptance and non-usage classification.
# Parser-failure arms (1-4) remain zero-contact (asserted at 4b).
echo "pr-metadata.sh usage-contract regression passed (R1/R4)"
@@ -0,0 +1,132 @@
#!/usr/bin/env bash
# Usage-error contract for pr-view.sh (R4, 2026-08-28).
#
# issue-edit already uses long-flag-first parsing (-i/--issue, -t/--title,
# -b/--body, -l/--labels, -m/--milestone); this adds the rc=2 usage-error
# contract, value checks, and the no-provider-contact proof. Required: -i.
#
# Arms:
# 1. --help and -h exit 0 and print usage.
# 2. Unknown option exits 2 with the message on stderr.
# 3. Missing required -i exits 2 (stderr).
# 4. A value-less flag (-i -b -c and long forms) exits 2 (stderr).
# 5. -b and -c both pass parsing (sandboxed runner: the run then fails
# at credential resolution, nonzero and NOT 2) — no real token is
# ever read and no provider is contacted.
# 6. No arm performs any provider request (PATH shims record every
# invocation; the probe log must stay empty).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORK_DIR="${MOSAIC_TEST_WORK_DIR:-$PWD/.mosaic-test-work/pr-view-usage}"
BIN_DIR="$WORK_DIR/bin"
PROBE_LOG="$WORK_DIR/provider-probes.log"
OUT_FILE="$WORK_DIR/out.log"
ERR_FILE="$WORK_DIR/err.log"
cleanup() {
rm -rf "$WORK_DIR"
}
trap cleanup EXIT
mkdir -p "$BIN_DIR"
: > "$PROBE_LOG"
# Unlike the issue suites, these stubs FAIL (exit 99): pr-close has an
# API fallback that treats a successful curl as a closed PR, so exit-0
# stubs would let the sandbox arms "succeed" (measured 2026-08-28).
for tool in gh tea curl; do
cat > "$BIN_DIR/$tool" <<STUB
#!/usr/bin/env bash
echo "$tool \$*" >> "$PROBE_LOG"
exit 99
STUB
chmod +x "$BIN_DIR/$tool"
done
run_wrapper() {
( cd "$WORK_DIR" && PATH="$BIN_DIR:$PATH" "$SCRIPT_DIR/pr-view.sh" "$@" )
}
# Hermetic variant: neutralizes every identity/credential source the wrapper
# consults so parse-acceptance arms fail at credential resolution in ANY cwd
# repo (see test-issue-comment-usage-contract.sh for the measured incident).
run_wrapper_sandboxed() {
mkdir -p "$WORK_DIR/home" "$WORK_DIR/xdg"
(
cd "$WORK_DIR"
PATH="$BIN_DIR:$PATH" HOME="$WORK_DIR/home" XDG_CONFIG_HOME="$WORK_DIR/xdg" \
MOSAIC_GIT_IDENTITY="" MOSAIC_BRAIN_HOME="" \
"$SCRIPT_DIR/pr-view.sh" "$@"
)
}
fail() {
echo "FAIL: $*" >&2
echo "--- stderr ---" >&2
cat "$ERR_FILE" >&2
exit 1
}
expect_rc() { # expect_rc <want> <desc> <args...>
local want="$1" desc="$2" rc=0
shift 2
run_wrapper "$@" >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -eq "$want" ]] || fail "$desc: rc=$rc, want $want"
}
expect_stderr() { # expect_stderr <pattern> <desc>
grep -q "$1" "$ERR_FILE" || fail "$2: stderr missing '$1'"
}
# 1. Help exits 0 and prints usage.
expect_rc 0 "--help exits 0" --help
grep -q "Usage: pr-view.sh" "$OUT_FILE" || fail "--help did not print usage"
expect_rc 0 "-h exits 0" -h
# 2. Unknown option: rc 2, stderr.
expect_rc 2 "unknown option exits 2" --bogus
expect_stderr "[Uu]nknown option" "unknown option names itself on stderr"
# 3. Missing required PR number: rc 2, stderr.
expect_rc 2 "missing -n exits 2"
expect_stderr "PR number is required" "missing -n message on stderr"
# 4. Value-less flags: rc 2 with "requires a value" on stderr.
for flag in -n -r --number --repo; do
expect_rc 2 "value-less $flag exits 2" "$flag"
expect_stderr "requires a value" "value-less $flag message on stderr"
done
# 4a. An option-like value is a MISSING value, not a value (codex PR #1464:
# -b --help previously consumed --help as the body and performed the write).
expect_rc 2 "option-like value rejected" -n --help
expect_rc 2 "short flag value rejected" -n -h
expect_stderr "requires a value" "short flag value message on stderr"
expect_stderr "requires a value" "option-like value message on stderr"
# 4b. Parser-failure arms (1-4) must have performed ZERO provider contact.
if [[ -s "$PROBE_LOG" ]]; then
echo "FAIL: a parser-failure arm contacted a provider:" >&2
cat "$PROBE_LOG" >&2
exit 1
fi
# 5. Alias acceptance under the sandbox: both -b and -c carry a value past
# parsing; the run fails at credential resolution nonzero and NOT 2.
for flag in -n; do
rc=0
run_wrapper_sandboxed -n 5 >"$OUT_FILE" 2>"$ERR_FILE" || rc=$?
[[ "$rc" -ne 0 ]] || fail "$flag arm unexpectedly succeeded in the sandbox"
[[ "$rc" -ne 2 ]] || fail "$flag arm misclassified credential failure as a usage error"
done
# 6. Post-sandbox provider assertions are intentionally NOT applied here:
# pr-close's gitea path attempts a tea WRITE (tea pr comment) when a
# comment parses, then falls back to the API. Hermeticity for this
# wrapper comes from the FAILING stubs (exit 99), not from non-contact —
# the arm above proves only parse acceptance and non-usage classification.
# Parser-failure arms (1-4) remain zero-contact (asserted at 4b).
echo "pr-view.sh usage-contract regression passed (R1/R4)"
File diff suppressed because one or more lines are too long
@@ -0,0 +1,218 @@
import { Command } from 'commander';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { registerAgentCommand, runEnroll, runGetEnrollment } from './agent.js';
import { enrollAgent, fetchEnrollment } from '../tui/gateway-api.js';
/**
* CLI-parity witness for the agent enrollment family (design
* docs/plans/2026-08-29-agent-enrollment-command-design.md §5 item 10;
* contract 5 §4.5): `mosaic agent enroll` / `mosaic agent enrollment <id>`
* invoke the same gateway commands with the same request/result/error
* contracts the web client uses. The gateway side of the same routes is
* witnessed in apps/gateway/src/enrollment/enrollment-commands.integration.test.ts.
*/
const gateway = 'https://gateway.example.test';
const auth = { gateway, cookie: 'session=test' };
const agentBody = {
id: '3fca4f6a-1111-4222-8333-444455556666',
name: 'Nova',
provider: 'anthropic',
model: 'claude-test',
status: 'idle',
harness: 'fake-harness',
persona: null,
ownerId: 'user-1',
enrolledAt: '2026-08-29T00:00:00.000Z',
createdAt: '2026-08-29T00:00:00.000Z',
};
const okResponse = (correlationId: string) =>
new Response(JSON.stringify({ ok: true, correlationId, agent: agentBody }), {
status: 201,
headers: { 'Content-Type': 'application/json' },
});
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
process.exitCode = undefined;
});
describe('agent enrollment CLI registration', (): void => {
it('registers enroll and enrollment subcommands under `mosaic agent`', () => {
const program = new Command();
const cmd = registerAgentCommand(program);
const names = cmd.commands.map((c) => c.name());
expect(names).toContain('enroll');
expect(names).toContain('enrollment');
});
it('the enroll subcommand exposes no argv flag that carries a credential value', () => {
const program = new Command();
const cmd = registerAgentCommand(program);
const enroll = cmd.commands.find((c) => c.name() === 'enroll');
expect(enroll).toBeDefined();
const flags = (enroll as Command).options.map((o) => o.flags);
// --credential selects the MODE only; the intake value arrives via stdin.
expect(flags).toContain('--credential <mode>');
for (const flag of flags) {
expect(flag).not.toMatch(/value|key <secret>|api-key/i);
}
});
});
describe('agent.enroll CLI parity', (): void => {
it('posts the §3.1 request shape for reference mode and surfaces the typed result', async () => {
const fetchMock = vi.fn(async () => okResponse('corr-ref'));
vi.stubGlobal('fetch', fetchMock);
const log = vi.spyOn(console, 'log').mockImplementation(() => {});
await runEnroll(auth, {
gateway,
harness: 'fake-harness',
name: 'Nova',
model: 'claude-test',
provider: 'anthropic',
credential: 'reference',
idempotencyKey: '9c1a26be-0000-4000-8000-000000000001',
correlationId: 'corr-ref',
});
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
expect(url).toBe(`${gateway}/api/enrollment/agents`);
expect(init.method).toBe('POST');
expect(JSON.parse(init.body as string)).toEqual({
harness: 'fake-harness',
name: 'Nova',
model: 'claude-test',
provider: 'anthropic',
credential: { mode: 'reference' },
idempotencyKey: '9c1a26be-0000-4000-8000-000000000001',
correlationId: 'corr-ref',
});
expect(log.mock.calls.flat().join('\n')).toContain(agentBody.id);
expect(process.exitCode).toBeUndefined();
});
it('intake mode reads the credential from the injected stdin reader, never argv', async () => {
const fetchMock = vi.fn(async () => okResponse('corr-intake'));
vi.stubGlobal('fetch', fetchMock);
vi.spyOn(console, 'log').mockImplementation(() => {});
await runEnroll(
auth,
{
gateway,
harness: 'fake-harness',
name: 'Nova',
model: 'claude-test',
provider: 'anthropic',
credential: 'intake',
idempotencyKey: '9c1a26be-0000-4000-8000-000000000002',
},
async () => 'stdin-provided-key',
);
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
const body = JSON.parse(init.body as string) as { credential: Record<string, string> };
expect(body.credential).toEqual({
mode: 'intake',
type: 'api_key',
value: 'stdin-provided-key',
});
});
it('an empty intake credential refuses locally with no gateway call', async () => {
const fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);
vi.spyOn(console, 'error').mockImplementation(() => {});
await runEnroll(
auth,
{
gateway,
harness: 'fake-harness',
name: 'Nova',
model: 'claude-test',
provider: 'anthropic',
credential: 'intake',
},
async () => '',
);
expect(fetchMock).not.toHaveBeenCalled();
expect(process.exitCode).toBe(1);
});
it('preserves the gateway refusal contract (closed error enum + correlation) in the CLI error', async () => {
vi.stubGlobal(
'fetch',
vi.fn(
async () =>
new Response(
JSON.stringify({
statusCode: 409,
error: 'conflict',
message: 'idempotency conflict',
correlationId: 'corr-409',
}),
{ status: 409, headers: { 'Content-Type': 'application/json' } },
),
),
);
await expect(
enrollAgent(gateway, auth.cookie, {
harness: 'fake-harness',
name: 'Nova',
model: 'claude-test',
provider: 'anthropic',
credential: { mode: 'reference' },
idempotencyKey: '9c1a26be-0000-4000-8000-000000000003',
}),
).rejects.toThrow(
'Failed to enroll agent (409): {"statusCode":409,"error":"conflict",' +
'"message":"idempotency conflict","correlationId":"corr-409"}',
);
});
});
describe('agent.enrollment.get CLI parity', (): void => {
it('reads one enrollment with the correlation envelope on the query string', async () => {
const fetchMock = vi.fn(async () => okResponse('corr-get'));
vi.stubGlobal('fetch', fetchMock);
const log = vi.spyOn(console, 'log').mockImplementation(() => {});
await runGetEnrollment(auth, agentBody.id, 'corr-get');
const [url] = fetchMock.mock.calls[0] as unknown as [string];
expect(url).toBe(`${gateway}/api/enrollment/agents/${agentBody.id}?correlationId=corr-get`);
expect(log.mock.calls.flat().join('\n')).toContain('corr-get');
});
it('preserves the folded not_found contract in the CLI error', async () => {
vi.stubGlobal(
'fetch',
vi.fn(
async () =>
new Response(
JSON.stringify({
statusCode: 404,
error: 'not_found',
message: 'agent not found',
correlationId: 'corr-404',
}),
{ status: 404, headers: { 'Content-Type': 'application/json' } },
),
),
);
await expect(fetchEnrollment(gateway, auth.cookie, agentBody.id)).rejects.toThrow(
'Failed to get enrollment (404): {"statusCode":404,"error":"not_found",' +
'"message":"agent not found","correlationId":"corr-404"}',
);
});
});
+133 -1
View File
@@ -1,3 +1,4 @@
import { randomUUID } from 'node:crypto';
import type { Command } from 'commander';
import { registerFleetAgentCommands, type FleetCommandDeps } from './fleet.js';
import { withAuth } from './with-auth.js';
@@ -9,8 +10,10 @@ import {
deleteAgentConfig,
fetchProjects,
fetchProviders,
enrollAgent,
fetchEnrollment,
} from '../tui/gateway-api.js';
import type { AgentConfigInfo } from '../tui/gateway-api.js';
import type { AgentConfigInfo, EnrolledAgentInfo } from '../tui/gateway-api.js';
function formatAgent(a: AgentConfigInfo): string {
const sys = a.isSystem ? ' [system]' : '';
@@ -75,11 +78,140 @@ export function registerAgentCommand(program: Command, fleetDeps: FleetCommandDe
},
);
registerEnrollmentCommands(cmd);
registerFleetAgentCommands(cmd, fleetDeps);
return cmd;
}
// ── Agent enrollment (design docs/plans/2026-08-29-agent-enrollment-command-design.md §3;
// CLI parity bound by contract 5 §4.5) ──
export interface EnrollCommandOptions {
gateway: string;
harness: string;
name: string;
model: string;
provider: string;
persona?: string;
credential: string;
idempotencyKey?: string;
correlationId?: string;
replayMode?: string;
}
/**
* Read an intake credential value from stdin. Never accepted via argv a
* process argument is world-readable in `ps` for the process lifetime.
*/
export async function readCredentialFromStdin(): Promise<string> {
if (process.stdin.isTTY) {
console.error('Enter API key, then press Enter and Ctrl-D:');
}
const chunks: Buffer[] = [];
for await (const chunk of process.stdin) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
return Buffer.concat(chunks)
.toString('utf8')
.replace(/\r?\n$/, '');
}
function showEnrollment(correlationId: string, agent: EnrolledAgentInfo): void {
console.log(` ID: ${agent.id}`);
console.log(` Name: ${agent.name}`);
console.log(` Harness: ${agent.harness ?? '—'}`);
console.log(` Provider: ${agent.provider}`);
console.log(` Model: ${agent.model}`);
console.log(` Status: ${agent.status}`);
console.log(` Owner: ${agent.ownerId ?? '—'}`);
console.log(` Enrolled: ${agent.enrolledAt ?? '—'}`);
console.log(` Correlation: ${correlationId}`);
}
export async function runEnroll(
auth: { gateway: string; cookie: string },
opts: EnrollCommandOptions,
readSecret: () => Promise<string> = readCredentialFromStdin,
): Promise<void> {
if (opts.credential !== 'reference' && opts.credential !== 'intake') {
console.error(`Unknown credential mode "${opts.credential}" (use reference or intake).`);
process.exitCode = 1;
return;
}
let credential: { mode: 'reference' } | { mode: 'intake'; type: 'api_key'; value: string };
if (opts.credential === 'intake') {
const value = await readSecret();
if (!value) {
console.error('Intake credential requires a non-empty API key on stdin.');
process.exitCode = 1;
return;
}
credential = { mode: 'intake', type: 'api_key', value };
} else {
credential = { mode: 'reference' };
}
const result = await enrollAgent(auth.gateway, auth.cookie, {
harness: opts.harness,
name: opts.name,
model: opts.model,
provider: opts.provider,
...(opts.persona !== undefined ? { persona: opts.persona } : {}),
credential,
idempotencyKey: opts.idempotencyKey ?? randomUUID(),
...(opts.correlationId !== undefined ? { correlationId: opts.correlationId } : {}),
...(opts.replayMode !== undefined ? { replayMode: opts.replayMode } : {}),
});
console.log(`Agent "${result.agent.name}" enrolled.\n`);
showEnrollment(result.correlationId, result.agent);
}
export async function runGetEnrollment(
auth: { gateway: string; cookie: string },
agentId: string,
correlationId?: string,
): Promise<void> {
const result = await fetchEnrollment(auth.gateway, auth.cookie, agentId, correlationId);
showEnrollment(result.correlationId, result.agent);
}
export function registerEnrollmentCommands(cmd: Command): void {
cmd
.command('enroll')
.description('Enroll an agent through the gateway enrollment command (agent.enroll)')
.requiredOption('--harness <id>', 'Harness the agent runs on (must be registered)')
.requiredOption('--name <name>', 'Agent display name')
.requiredOption('--model <model>', 'Model identifier')
.requiredOption('--provider <provider>', 'Provider the credential belongs to')
.option('--persona <text>', 'Agent persona / system prompt')
.option(
'--credential <mode>',
'Credential mode: "reference" (already stored) or "intake" (API key read from stdin, never argv)',
'reference',
)
.option('--idempotency-key <uuid>', 'Idempotency key (generated when omitted)')
.option('--correlation-id <uuid>', 'Correlation id to carry through the audit trail')
.option('--replay-mode <mode>', 'Idempotency replay mode (actor-bound)')
.action(async (opts: Omit<EnrollCommandOptions, 'gateway'>) => {
const parent = cmd.opts<{ gateway: string }>();
const auth = await withAuth(parent.gateway);
await runEnroll(auth, { ...opts, gateway: parent.gateway });
});
cmd
.command('enrollment <agentId>')
.description('Read one enrolled agent (agent.enrollment.get; owner or admin)')
.option('--correlation-id <uuid>', 'Correlation id to carry through the read')
.action(async (agentId: string, opts: { correlationId?: string }) => {
const parent = cmd.opts<{ gateway: string }>();
const auth = await withAuth(parent.gateway);
await runGetEnrollment(auth, agentId, opts.correlationId);
});
}
async function resolveAgent(
gateway: string,
cookie: string,
@@ -15,6 +15,17 @@ import { afterEach, describe, expect, it } from 'vitest';
import { fleetCommsSendArgs, registerCommsCommand, tmuxSendArgs } from './comms.js';
describe('arg translation', () => {
// B2 (2026-08-29): when --socket is omitted, the CLI forwards NO -L and
// agent-send.sh's own resolution governs (explicit -L > MOSAIC_TMUX_SOCKET
// > unique hit > ambiguity refusal). Forwarding a guessed default here
// would defeat that resolution and reintroduce the stale-twin defect.
it('omits -L entirely when --socket is not given (tool-owned resolution)', () => {
const args = tmuxSendArgs('orch-01', 'hello', {});
expect(args).toEqual(['-s', 'orch-01', '-m', 'hello']);
expect(args).not.toContain('-L');
expect(args.join(' ')).not.toContain('-L');
});
it('tmux path: -s/-C/-L/-f/-m per agent-send.sh getopts', () => {
expect(tmuxSendArgs('orch-01', 'hello', {})).toEqual(['-s', 'orch-01', '-m', 'hello']);
expect(
+4 -1
View File
@@ -62,7 +62,10 @@ export function registerCommsCommand(program: Command): void {
.description('send <target> [message...] — same-host tmux unless --site is given')
.option('--class <class>', 'terminal-log | actionable | human | reaction | digest')
.option('--file <path>', 'message body from file (same-host path only)')
.option('--socket <name>', 'tmux socket for the same-host send (e.g. mosaic-fleet)')
.option(
'--socket <name>',
'tmux socket for the same-host send (e.g. mosaic-fleet). OMIT it to let agent-send.sh resolve the socket (explicit -L > MOSAIC_TMUX_SOCKET > unique hit; ambiguity refuses with its exit 4).',
)
.option('--site <site>', 'route via fleet-comms to <site>/<target>')
.option('--comms-repo <path>', 'fleet-comms checkout', defaultCommsRepo())
.argument('<target>', 'destination seat (session name)')
@@ -173,6 +173,8 @@ describe('registerFleetCommand', () => {
expect(agent!.options.map((option) => option.long)).toContain('--list');
expect(agent!.commands.map((command) => command.name()).sort()).toEqual([
'comms-block',
'enroll',
'enrollment',
'reset',
'roster',
'send',
@@ -148,6 +148,26 @@ describe.skipIf(!hasBash)('bash ↔ TS manifest parity (§6.1)', () => {
const manifest = loadManifest(FRAMEWORK_ROOT);
expect(bashSubtreeRoots().sort()).toEqual(frameworkSubtreeRoots(manifest).sort());
});
it('fleet/bin ownership is exact and does not prune existing executables (T110 B1)', () => {
// fleet/bin carries estate executables this package does not ship. A
// subtree glob here would classify them framework-owned and keep-mode
// update would prune them. The manifest must own EXACTLY the two shipped
// launcher files and nothing else in fleet/bin, on BOTH resolvers.
const manifest = loadManifest(FRAMEWORK_ROOT);
const expected: Array<[string, string]> = [
['fleet/bin/mosaic', 'framework'],
['fleet/bin/test-mosaic-launcher.sh', 'framework'],
['fleet/bin/seat-up.sh', 'operator'], // shipped-by-estate, unshipped here
['fleet/bin/launch-seat.sh', 'operator'],
['fleet/bin', 'operator'], // the directory itself is unlisted
];
for (const [path, want] of expected) {
const ts = resolveOwnership(manifest, path);
expect(ts).toBe(want);
expect(bashResolve(path)).toBe(want);
}
});
});
/**
+68
View File
@@ -562,6 +562,74 @@ export async function fetchInteractionHealth(gatewayUrl: string): Promise<unknow
return handleResponse<unknown>(res, 'Failed to get interaction readiness');
}
// ── Agent Enrollment types (design docs/plans/2026-08-29-agent-enrollment-command-design.md §3) ──
export interface EnrolledAgentInfo {
id: string;
name: string;
provider: string;
model: string;
status: string;
harness: string | null;
persona: string | null;
ownerId: string | null;
enrolledAt: string | null;
createdAt: string;
}
export interface EnrollAgentRequest {
harness: string;
name: string;
persona?: string;
model: string;
provider: string;
credential: { mode: 'reference' } | { mode: 'intake'; type: 'api_key'; value: string };
idempotencyKey: string;
correlationId?: string;
replayMode?: string;
}
export interface EnrollmentOutcome {
ok: true;
correlationId: string;
agent: EnrolledAgentInfo;
}
// ── Agent Enrollment endpoints ──
/**
* agent.enroll. The gateway's typed refusal body (closed error enum +
* correlationId) is preserved verbatim in the thrown error, so the CLI
* surfaces the same contract the web client receives.
*/
export async function enrollAgent(
gatewayUrl: string,
sessionCookie: string,
data: EnrollAgentRequest,
): Promise<EnrollmentOutcome> {
const res = await fetch(`${gatewayUrl}/api/enrollment/agents`, {
method: 'POST',
headers: jsonHeaders(sessionCookie, gatewayUrl),
body: JSON.stringify(data),
});
return handleResponse<EnrollmentOutcome>(res, 'Failed to enroll agent');
}
/** agent.enrollment.get: owner-or-admin read of one enrolled agent. */
export async function fetchEnrollment(
gatewayUrl: string,
sessionCookie: string,
agentId: string,
correlationId?: string,
): Promise<EnrollmentOutcome> {
const params = correlationId ? `?${new URLSearchParams({ correlationId }).toString()}` : '';
const res = await fetch(
`${gatewayUrl}/api/enrollment/agents/${encodeURIComponent(agentId)}${params}`,
{ headers: headers(sessionCookie, gatewayUrl) },
);
return handleResponse<EnrollmentOutcome>(res, 'Failed to get enrollment');
}
// ── Conversation Message types ──
export interface ConversationMessage {
+176
View File
@@ -0,0 +1,176 @@
#!/usr/bin/env bash
# Hermetic structural check for the explicit dogfood Compose overlay.
set -euo pipefail
repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
mkdir -p "$tmp/worktree" "$tmp/common.git" "$tmp/seat/secrets"
base_config_json=$(
cd "$repo_root"
BETTER_AUTH_SECRET=test-only-not-a-credential \
docker compose --profile stack config --format json
)
BASE_CONFIG_JSON="$base_config_json" python3 <<'PY'
import json
import os
config = json.loads(os.environ["BASE_CONFIG_JSON"])
gateway = config["services"]["gateway"]
env = gateway["environment"]
for key in (
"MOSAIC_AGENT_NAME",
"MOSAIC_GIT_IDENTITY",
"MOSAIC_BRAIN_HOME",
"AGENT_FILE_SANDBOX_DIR",
"AGENT_USER_TOOLS",
"AGENT_SHELL_ENABLED",
"AGENT_DELIVERY_ENABLED",
"MOSAIC_GIT_TOOLS_DIR",
"MOSAIC_INTEGRATION_TRUNK",
):
assert key not in env, f"base compose unexpectedly sets dogfood variable {key}"
targets = {mount["target"] for mount in gateway["volumes"]}
assert "/workspace/stack" not in targets
assert not any(target.startswith("/opt/mosaic/brain/") for target in targets)
PY
config_json=$(
cd "$repo_root"
BETTER_AUTH_SECRET=test-only-not-a-credential \
MOSAIC_DOGFOOD_WORKTREE="$tmp/worktree" \
MOSAIC_DOGFOOD_COMMON_GIT_DIR="$tmp/common.git" \
MOSAIC_DOGFOOD_SEAT_HOME="$tmp/seat" \
docker compose \
-f docker-compose.yml \
-f docker-compose.dogfood.yml \
--profile stack \
config --format json
)
CONFIG_JSON="$config_json" EXPECT_WORKTREE="$tmp/worktree" EXPECT_COMMON_GIT="$tmp/common.git" EXPECT_SEAT="$tmp/seat" python3 <<'PY'
import json
import os
config = json.loads(os.environ["CONFIG_JSON"])
gateway = config["services"]["gateway"]
assert gateway.get("init") is True, "gateway must run below an init process for R4 lineage"
env = gateway["environment"]
expected_env = {
"MOSAIC_AGENT_NAME": "code-dogfood-01",
"MOSAIC_GIT_IDENTITY": "code-dogfood-01",
"MOSAIC_BRAIN_HOME": "/opt/mosaic/brain",
"AGENT_FILE_SANDBOX_DIR": "/workspace/stack",
"AGENT_SHELL_ENABLED": "false",
"AGENT_DELIVERY_ENABLED": "true",
"MOSAIC_GIT_TOOLS_DIR": "/opt/mosaic/tools/git",
"MOSAIC_INTEGRATION_TRUNK": "next",
}
for key, value in expected_env.items():
assert env.get(key) == value, f"{key}: expected {value!r}, got {env.get(key)!r}"
allowed = set(env["AGENT_USER_TOOLS"].split(","))
assert allowed == {
"fs_read_file",
"fs_write_file",
"fs_list_directory",
"fs_edit_file",
"git_status",
"git_log",
"git_diff",
"git_publish_branch",
"git_open_pull_request",
}, f"unexpected dogfood tool set: {sorted(allowed)}"
assert "shell_exec" not in allowed
mounts = {mount["target"]: mount for mount in gateway["volumes"]}
worktree = mounts["/workspace/stack"]
assert worktree["type"] == "bind"
assert worktree["source"] == os.environ["EXPECT_WORKTREE"]
assert not worktree.get("read_only", False), "dogfood worktree must be writable"
common_git = mounts[os.environ["EXPECT_COMMON_GIT"]]
assert common_git["type"] == "bind"
assert common_git["source"] == os.environ["EXPECT_COMMON_GIT"]
assert not common_git.get("read_only", False), "common Git directory must accept branch updates"
seat = mounts["/opt/mosaic/brain/fleet/agents/code-dogfood-01"]
assert seat["type"] == "bind"
assert seat["source"] == os.environ["EXPECT_SEAT"]
assert seat.get("read_only") is True, "seat credential slot must be read-only"
other_seat_mounts = [
target
for target in mounts
if target.startswith("/opt/mosaic/brain/fleet/agents/")
and target != "/opt/mosaic/brain/fleet/agents/code-dogfood-01"
]
assert other_seat_mounts == [], f"other seat mounts leaked: {other_seat_mounts}"
PY
# Each required path must fail closed rather than falling back to the current checkout.
expect_missing_path() {
local missing=$1 output rc
set +e
case "$missing" in
MOSAIC_DOGFOOD_WORKTREE)
output=$(
cd "$repo_root"
env -u MOSAIC_DOGFOOD_WORKTREE \
BETTER_AUTH_SECRET=test-only-not-a-credential \
MOSAIC_DOGFOOD_COMMON_GIT_DIR="$tmp/common.git" \
MOSAIC_DOGFOOD_SEAT_HOME="$tmp/seat" \
docker compose -f docker-compose.yml -f docker-compose.dogfood.yml \
--profile stack config 2>&1
)
rc=$?
;;
MOSAIC_DOGFOOD_COMMON_GIT_DIR)
output=$(
cd "$repo_root"
env -u MOSAIC_DOGFOOD_COMMON_GIT_DIR \
BETTER_AUTH_SECRET=test-only-not-a-credential \
MOSAIC_DOGFOOD_WORKTREE="$tmp/worktree" \
MOSAIC_DOGFOOD_SEAT_HOME="$tmp/seat" \
docker compose -f docker-compose.yml -f docker-compose.dogfood.yml \
--profile stack config 2>&1
)
rc=$?
;;
MOSAIC_DOGFOOD_SEAT_HOME)
output=$(
cd "$repo_root"
env -u MOSAIC_DOGFOOD_SEAT_HOME \
BETTER_AUTH_SECRET=test-only-not-a-credential \
MOSAIC_DOGFOOD_WORKTREE="$tmp/worktree" \
MOSAIC_DOGFOOD_COMMON_GIT_DIR="$tmp/common.git" \
docker compose -f docker-compose.yml -f docker-compose.dogfood.yml \
--profile stack config 2>&1
)
rc=$?
;;
*)
echo "FAIL: test requested unknown path variable $missing" >&2
exit 1
;;
esac
set -e
if [[ $rc -eq 0 ]]; then
echo "FAIL: dogfood compose accepted missing $missing" >&2
exit 1
fi
if [[ "$output" != *"$missing"* ]]; then
echo "FAIL: missing-path failure did not name $missing" >&2
exit 1
fi
}
expect_missing_path MOSAIC_DOGFOOD_WORKTREE
expect_missing_path MOSAIC_DOGFOOD_COMMON_GIT_DIR
expect_missing_path MOSAIC_DOGFOOD_SEAT_HOME
printf 'dogfood compose verification passed\n'