import { executeDevLoopsCommand, inspectResultSeverity } from '../lib/dev-loops-core.mjs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { createExtensionCoreRuntime } from './checks.ts'; import { createPostMergeUpdateHook } from './post-merge-update.ts'; import { createPiExtensionAdapter, type ExtensionAPI } from './pi-extension-adapter.ts'; import { loadDevLoopConfig, resolveRoleModel } from '@dev-loops/core/config'; import { createChildRoleGateRegistrar, loadRegisterRequiredChildExtensions, } from './required-child-extensions.ts'; import { buildEntrypointLines, buildHelpLines, buildInspectLines, buildInspectNotification, buildNotificationMessage, buildWidgetLines, type DevLoopsAction, type InspectAction, } from './presentation.ts'; type ExtensionRuntimeOverrides = NonNullable[1]> & { postMergeUpdateHook?: ReturnType; /** Test seam: override the `pi-subagents` required-child-extension loader. */ loadRegisterRequiredChildExtensions?: typeof loadRegisterRequiredChildExtensions; }; const STATUS_KEY = 'dev-loops'; const WIDGET_KEY = 'dev-loops.setup'; // `syncPackagedAgents` remaps the harness-neutral `tools:` frontmatter to Pi // builtins at session-start sync time (#1583); the pure transform + IO live in // a dedicated module so they stay testable offline. Import for local use AND // re-export so tests can reach the same binding the session_start handler calls. import { checkAgentSetRendered, findProjectRoot, isDevLoopsAgentsCheckout, renderProjectAgents, syncPackagedAgents } from './sync-packaged-agents.ts'; export { syncPackagedAgents }; import { decidePiToolCall, isReadOnlyPiRole, resolvePiRole } from './readonly-role-gate.ts'; // The calling session's own role for one handler call: resolved from its system prompt, with the // session id scoping the untagged native-async-child fail-closed row (readonly-role-gate.ts). const resolveSessionRole = (ctx: { getSystemPrompt?: () => string; getSessionId?: () => string }) => resolvePiRole({ systemPrompt: ctx?.getSystemPrompt?.(), sessionId: ctx?.getSessionId?.() }); async function dispatchDevLoopIntent(ctx: { sendUserMessage?: (message: string) => unknown }, intent: string) { await ctx.sendUserMessage?.(`/skill:dev-loop ${intent}`); } export default function (pi: ExtensionAPI, runtimeOverrides: ExtensionRuntimeOverrides = {}) { // Wrap the Pi harness at the entry boundary; everything below talks to the neutral seam. const adapter = createPiExtensionAdapter(pi); const postMergeUpdateHook = runtimeOverrides.postMergeUpdateHook ?? createPostMergeUpdateHook({ exec: adapter.exec }); // A foreground (`async: false`) child never loads ambient extensions, so the read-only // role gate stayed inert in dispatched judge/reviewer children. Register the extension // itself as a required child extension so every child loads it; see // skills/docs/cross-harness-regression-contract.md "Read-only role enforcement on Pi". const childRoleGate = createChildRoleGateRegistrar({ // This module is the extension entry (`pi.extensions`), so its own URL is the path a // child must load to run the same `tool_call` handler. extensionPath: fileURLToPath(import.meta.url), load: runtimeOverrides.loadRegisterRequiredChildExtensions ?? loadRegisterRequiredChildExtensions, }); adapter.on('session_start', async (_event, ctx) => { postMergeUpdateHook.onSessionStart(); try { const projectRoot = ctx.cwd ? findProjectRoot(ctx.cwd) : undefined; const devLoopsCheckout = !!projectRoot && isDevLoopsAgentsCheckout(projectRoot); // The unpinned project sync would strip provisioned `model:` pins in a dev-loops checkout; render there instead. syncPackagedAgents({ projectRoot: devLoopsCheckout ? undefined : projectRoot }); // A fresh dev-loops clone has no `.pi/agents`; render it from the checkout's own agents/ (decision record 0173). if (projectRoot && devLoopsCheckout) { // Same pinned-model resolution as provisioning; an invalid config pins nothing; children inherit. const { config, errors } = await loadDevLoopConfig({ repoRoot: projectRoot }); const safeConfig = errors && errors.length > 0 ? null : config; renderProjectAgents(projectRoot, path.join(projectRoot, 'agents'), (role: string) => resolveRoleModel(safeConfig, { role, harness: 'pi' })); } } catch { // Best-effort agent sync — do not break session start } try { await childRoleGate.register(ctx.getSessionId?.()); } catch { // Best-effort child-extension registration — never break session start } ctx.ui.setStatus(STATUS_KEY, undefined); }); adapter.on('session_shutdown', () => { childRoleGate.disposeAll(); }); // Read-only role enforcement on Pi (cross-harness-regression-contract.md): block shell for // read-only roles; Pi honours `{ block, reason }` from `tool_call`. adapter.on('tool_call', (event, ctx) => { const { toolName, input } = event as { toolName?: string; input?: { command?: unknown } }; if (toolName === 'subagent' && ctx.cwd) { // refuse a child launch when a rendered dev-loops agent file carries raw non-Pi tools; // the launch cwd may sit at the top level or inside `options`. type Sub = { cwd?: unknown; options?: Sub }; const sub = (input ?? {}) as Sub; const launchCwd = [sub.options?.cwd, sub.cwd].find((c): c is string => typeof c === 'string' && c.length > 0); const reason = checkAgentSetRendered(launchCwd ? path.resolve(ctx.cwd, launchCwd) : ctx.cwd); if (reason) return { block: true, reason }; } return decidePiToolCall({ toolName, input, agentType: resolveSessionRole(ctx) }); }); // A read-only child must never run the mutation-capable post-merge hooks: `isMergeCapableCommand` // splits shell segments without quote awareness, so a reviewer's allowed read form such as // `grep '; gh pr merge 42 ;' README.md` is misread as a merge and would queue post-merge updates, // main-checkout sync and cleanup even though no merge ran. The `tool_call` gate already denies a // read-only session any real merge, so these hooks have nothing legitimate to do there. const readOnlySession = (ctx: { getSystemPrompt?: () => string; getSessionId?: () => string }) => isReadOnlyPiRole(resolveSessionRole(ctx)); adapter.on('tool_result', async (event, ctx) => { if (readOnlySession(ctx)) return; await postMergeUpdateHook.onToolResult(event, ctx); }); adapter.on('user_bash', async (event, ctx) => { if (readOnlySession(ctx)) return undefined; return postMergeUpdateHook.onUserBash(event, ctx); }); adapter.on('agent_end', async (event, ctx) => { if (readOnlySession(ctx)) return; await postMergeUpdateHook.onAgentEnd(event, ctx); }); adapter.registerCommand('dev-loops', { description: 'Run a dev-loop entrypoint or manage readiness: /dev-loops [start |auto |continue [issue|pr]|start-spike |info |status|doctor|gates|hide|inspect ...]', handler: async (args, ctx) => { const result = await executeDevLoopsCommand({ input: args, surface: 'extension', runtime: createExtensionCoreRuntime(adapter, runtimeOverrides), }); switch (result.kind) { case 'hide': ctx.ui.setWidget(WIDGET_KEY, undefined); ctx.ui.notify('dev-loops widget hidden', 'info'); return; case 'help': ctx.ui.setWidget(WIDGET_KEY, buildHelpLines(), { placement: 'belowEditor' }); ctx.ui.notify('dev-loops help', 'info'); return; case 'entrypoint': ctx.ui.setWidget(WIDGET_KEY, buildEntrypointLines(result.action, result.intent), { placement: 'belowEditor' }); await dispatchDevLoopIntent(ctx, result.intent); ctx.ui.notify(`dev-loops ${result.action}: ${result.intent}`, 'info'); return; case 'start_spike': ctx.ui.setWidget(WIDGET_KEY, buildEntrypointLines('start-spike', result.intent), { placement: 'belowEditor' }); await dispatchDevLoopIntent(ctx, result.intent); ctx.ui.notify(`dev-loops start-spike: ${result.intent}`, 'info'); return; case 'checks': ctx.ui.setWidget(WIDGET_KEY, buildWidgetLines(result.action as Extract, result.checks), { placement: 'belowEditor', }); ctx.ui.notify(buildNotificationMessage(result.action as Extract, result.checks), 'info'); return; case 'gates': ctx.ui.notify('Gate angles printed to console. Run `dev-loops gates` in a terminal to see review prompts.', 'info'); return; case 'inspect_result': { // Severity is shared with the CLI surface so both classify identically. const notificationLevel = inspectResultSeverity(result); ctx.ui.setWidget(WIDGET_KEY, buildInspectLines(result.action as InspectAction, result), { placement: 'belowEditor', }); ctx.ui.notify(buildInspectNotification(result.action as InspectAction, result.state), notificationLevel); return; } case 'malformed': ctx.ui.setWidget(WIDGET_KEY, [result.message, ...buildHelpLines()], { placement: 'belowEditor' }); ctx.ui.notify(`dev-loops ${result.usageAction ?? 'help'}: invalid arguments`, 'error'); return; case 'unsupported': { const message = result.message || 'This command is not supported here.'; ctx.ui.setWidget(WIDGET_KEY, [message, ...buildHelpLines()], { placement: 'belowEditor' }); ctx.ui.notify(message, 'error'); return; } default: { const exhaustiveCheck: never = result; throw new Error(`Unhandled extension result: ${JSON.stringify(exhaustiveCheck)}`); } } }, }); }