Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : SP Wallet8 * File : services/research-runner/src/config.ts9 * Module : services10 * Repository : https://git.spboucher.ai/sp-wallet11 * Project Website : https://wallet.spboucher.ai12 * Created : 2026-11-1113 * Last Modified : 2026-11-1114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// Configuration of the research runner. It runs on a research node, NOT next to the wallet API: it needs no wallet secret,44// no database URL and no signer address — only the API's public URL, its own shared secret, a laboratory checkout with its45// Python environment, and (for the real coding agent) a model credential that stays in THIS process.46import { homedir } from 'node:os';47import path from 'node:path';4849export interface AgentConfig {50 /** 'claude': Claude Code CLI, headless. 'command': any argv — the deterministic fake agent of the tests. */51 kind: 'claude' | 'command';52 /** claude: path of the CLI */53 bin: string;54 model: string | null;55 /** command: the argv; `{work}`, `{ro}` and `{prompt}` are replaced */56 argv: string[];57}58export interface RunnerConfig {59 apiUrl: string;60 /** shared secret of the runner endpoints (never the owner session, never the signer secret) */61 secret: string;62 runnerId: string;63 /** job directories are created here; must be outside every application checkout */64 jobsRoot: string;65 /** the runner's own state (ledger copies, sandbox profiles): never readable or writable by a job */66 stateRoot: string;67 /** a checkout of the laboratory (the `research/` directory) */68 labDir: string;69 /** 'commit': the job directory gets `git archive <commit>` of the laboratory; 'worktree': a copy of the directory as it is (development) */70 labPin: 'commit' | 'worktree';71 /** interpreter of the laboratory's prepared virtualenv */72 python: string;73 /** more read-only toolchain paths for the sandbox */74 readable: string[];75 agent: AgentConfig;76 /** 'required': refuse to run a job when the OS sandbox is not available. 'off' exists for platforms without one and says so in every result. */77 sandbox: 'required' | 'off';78 model: { upstream: string; credential: [name: string, value: string] | null };79 pollMs: number; tickMs: number; logFlushMs: number;80 keepJobs: boolean;81}8283const need = (env: NodeJS.ProcessEnv, k: string): string => { const v = env[k]; if (!v) throw new Error(`missing env ${k}`); return v; };8485export function configFromEnv(env: NodeJS.ProcessEnv = process.env): RunnerConfig {86 const home = path.resolve(env.SPW_RUNNER_HOME ?? path.join(homedir(), 'sp-wallet-research'));87 const labDir = path.resolve(need(env, 'SPW_RUNNER_LAB_DIR'));88 const secret = need(env, 'SPW_RUNNER_SECRET');89 if (secret.length < 32) throw new Error('SPW_RUNNER_SECRET must be at least 32 characters');90 const kind = (env.SPW_RUNNER_AGENT ?? 'claude') === 'command' ? 'command' : 'claude';91 // the model credential: read here once, handed to the gateway, and removed from the environment children could inherit92 const credential: [string, string] | null = env.ANTHROPIC_API_KEY ? ['x-api-key', env.ANTHROPIC_API_KEY]93 : env.CLAUDE_CODE_OAUTH_TOKEN ? ['authorization', `Bearer ${env.CLAUDE_CODE_OAUTH_TOKEN}`] : null;94 if (kind === 'claude' && !credential) throw new Error('the Claude Code agent needs ANTHROPIC_API_KEY (or CLAUDE_CODE_OAUTH_TOKEN) in the RUNNER\'s environment; the job never sees it');95 delete env.ANTHROPIC_API_KEY; delete env.CLAUDE_CODE_OAUTH_TOKEN;96 return {97 apiUrl: need(env, 'SPW_RUNNER_API_URL').replace(/\/+$/, ''), secret, runnerId: (env.SPW_RUNNER_ID ?? 'runner').slice(0, 40),98 jobsRoot: path.join(home, 'jobs'), stateRoot: path.join(home, 'state'), labDir,99 labPin: env.SPW_RUNNER_LAB_PIN === 'worktree' ? 'worktree' : 'commit',100 python: path.resolve(env.SPW_RUNNER_PYTHON ?? path.join(labDir, '.venv/bin/python')),101 readable: (env.SPW_RUNNER_READABLE ?? '').split(':').filter(Boolean),102 agent: { kind, bin: env.SPW_RUNNER_CLAUDE_BIN ?? '/opt/homebrew/bin/claude', model: env.SPW_RUNNER_MODEL ?? null, argv: kind === 'command' ? (JSON.parse(need(env, 'SPW_RUNNER_AGENT_CMD')) as string[]) : [] },103 sandbox: env.SPW_RUNNER_SANDBOX === 'off' ? 'off' : 'required',104 model: { upstream: env.SPW_RUNNER_MODEL_UPSTREAM ?? 'https://api.anthropic.com', credential },105 pollMs: Number(env.SPW_RUNNER_POLL_MS ?? 5000), tickMs: 500, logFlushMs: 2000, keepJobs: env.SPW_RUNNER_KEEP_JOBS === '1',106 };107}108