managarten/services/mana-persona-runner/src/config.ts
Till JS a1caeaa7f3 feat(personas): M3.a — scaffold mana-persona-runner service on :3070
First concrete piece of M3 (docs/plans/mana-mcp-and-personas.md). The
tick loop itself and the Claude Agent SDK + MCP integration are M3.b;
the action/feedback persistence endpoints are M3.c. This commit just
stands up the service so the remaining pieces have a shell to land in.

Service shape (Bun/Hono on :3070)

- src/config.ts
    Env-driven configuration: auth URL, MCP URL, service key for
    action/feedback callbacks (M3.c), Anthropic API key, deterministic
    PERSONA_SEED_SECRET (must match scripts/personas/password.ts so the
    runner can log back in without any stored credentials), tick
    interval and concurrency, RUNNER_PAUSED kill-switch. Production
    start asserts all secrets are set and the dev fallback secret is
    rotated.

- src/password.ts
    Bit-for-bit identical HMAC-SHA256 password derivation to
    scripts/personas/password.ts. Duplicated deliberately: the two
    sides can't share code (one is a repo-root utility script, the
    other is a workspace service) but must stay in sync — comment
    at the top calls this out.

- src/clients/auth.ts
    Two upstream calls the runner needs for one tick: POST /auth/login
    and GET /api/auth/organization/list. loginAndResolvePersonalSpace()
    wraps both and picks the persona's auto-created personal space as
    the write target (throws if none exists — Spaces-Foundation should
    always have seeded one on signup).

- src/index.ts
    Hono app: /health, /metrics (stub), and a dev-only /diag/login
    endpoint that takes a persona email, derives the password, logs
    in, resolves the personal space, and returns {userId, spaceId} as
    an end-to-end sanity check. Disabled in production.

No tick loop yet — RUNNER_PAUSED prints an info line on boot, but
nothing fires. The dispatcher + Claude Agent SDK + MCP client land in
M3.b; the internal POST callbacks into mana-auth for persona_actions /
persona_feedback land in M3.c.

Infra

- Port 3070 added to docs/PORT_SCHEMA.md.
- Service listed in root CLAUDE.md next to mana-mcp.
- services/mana-persona-runner/CLAUDE.md documents what's built today,
  what lands in M3.b/c, and the local diag smoke recipe.

Boot smoke verified: /health returns ok + paused/interval/concurrency,
/diag/login without email returns 400.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 14:00:43 +02:00

86 lines
2.9 KiB
TypeScript

/**
* Service configuration. Every value env-overridable so dev/staging/prod
* can dial them independently without a code change.
*/
export interface Config {
port: number;
/** mana-auth base URL — for login, spaces list, persistence callbacks. */
authUrl: string;
/** mana-mcp base URL — where Claude talks to the tool registry. */
mcpUrl: string;
/** Service key for /api/v1/internal/* callbacks into mana-auth. */
serviceKey: string;
/** Anthropic API key that drives each persona's Claude call. */
anthropicApiKey: string;
/** Deterministic per-persona password seed. Must match whatever is
* in scripts/personas/password.ts — the same function derives the
* same password here, so the runner can log in without storing any
* per-persona credentials. */
personaSeedSecret: string;
/** How often the tick loop runs. Default 60 s — fine-grained enough
* that a persona with tickCadence='hourly' stays on schedule, cheap
* enough that dispatching 10 personas never backs up. */
tickIntervalMs: number;
/** Max personas running in parallel per tick. Scoped to Claude API
* rate limits — conservative default 2, tier-dependent. */
concurrency: number;
/**
* Pause the loop without redeploying. Useful when the user wants
* persona activity to stop (e.g. during a demo) but the service
* should stay up so health checks don't page.
*/
paused: boolean;
}
function intEnv(name: string, fallback: number): number {
const raw = process.env[name];
if (!raw) return fallback;
const n = Number(raw);
if (!Number.isInteger(n) || n <= 0) {
throw new Error(`${name} must be a positive integer, got "${raw}"`);
}
return n;
}
function boolEnv(name: string, fallback: boolean): boolean {
const raw = process.env[name];
if (raw == null) return fallback;
return raw === '1' || raw.toLowerCase() === 'true';
}
export function loadConfig(): Config {
return {
port: intEnv('PORT', 3070),
authUrl: process.env.MANA_AUTH_URL ?? 'http://localhost:3001',
mcpUrl: process.env.MANA_MCP_URL ?? 'http://localhost:3069',
serviceKey: process.env.MANA_SERVICE_KEY ?? '',
anthropicApiKey: process.env.ANTHROPIC_API_KEY ?? '',
personaSeedSecret: process.env.PERSONA_SEED_SECRET ?? 'dev-persona-seed-secret-rotate-in-prod',
tickIntervalMs: intEnv('TICK_INTERVAL_MS', 60_000),
concurrency: intEnv('PERSONA_CONCURRENCY', 2),
paused: boolEnv('RUNNER_PAUSED', false),
};
}
export function assertProductionSecrets(config: Config): void {
if (process.env.NODE_ENV !== 'production') return;
const missing: string[] = [];
if (!config.serviceKey) missing.push('MANA_SERVICE_KEY');
if (!config.anthropicApiKey) missing.push('ANTHROPIC_API_KEY');
if (config.personaSeedSecret === 'dev-persona-seed-secret-rotate-in-prod') {
missing.push('PERSONA_SEED_SECRET (dev fallback in prod)');
}
if (missing.length > 0) {
throw new Error(
`mana-persona-runner production start blocked — missing/unsafe: ${missing.join(', ')}`
);
}
}