diff --git a/lib/plan.js b/lib/plan.js index 67af96c..1ad47f6 100644 --- a/lib/plan.js +++ b/lib/plan.js @@ -16,11 +16,20 @@ const HEADING_RE = /^#\s+\S/ * @returns {import('./types.js').PlanModeService | undefined} */ export function resolvePlanMode(ctx, agent) { + const fromPreset = asPlanModeService(readPresetPlanMode(ctx, agent)) + if (fromPreset) return fromPreset const fromAgent = asPlanModeService(agent?.ctx?.get?.('planMode')) if (fromAgent) return fromAgent return asPlanModeService(ctx.get('planMode')) } +function readPresetPlanMode(ctx, agent) { + if (!agent) return undefined + const agentPresets = ctx.get?.('agentPresets') + if (!agentPresets || typeof agentPresets.serviceFor !== 'function') return undefined + return agentPresets.serviceFor(agent, 'planMode') +} + /** * @param {unknown} value * @returns {import('./types.js').PlanModeService | undefined} @@ -51,12 +60,20 @@ export function shouldInterceptExitPlanMode(exec, planMode) { export function isPlanModeActive(agent, planMode) { if (planMode) return planMode.get(agent).active let active = false - for (const event of agent.session?.events ?? []) { + for (const event of readSessionEvents(agent)) { if (event.type === 'plan/mode') active = event.data?.active === true } return active } +function readSessionEvents(agent) { + const session = agent?.session + if (!session) return [] + if (Array.isArray(session.events)) return session.events + if (typeof session.snapshotEvents !== 'function') return [] + return session.snapshotEvents() +} + /** * @param {unknown} args * @returns {string} diff --git a/tests/plan.test.js b/tests/plan.test.js index 82abfa8..3ef4183 100644 --- a/tests/plan.test.js +++ b/tests/plan.test.js @@ -34,10 +34,12 @@ test('shouldInterceptExitPlanMode ignores every other tool', () => { }, planMode(true)), false) }) -test('shouldInterceptExitPlanMode intercepts from session events when planMode is not on this context', () => { +test('shouldInterceptExitPlanMode intercepts from the session log when planMode is not on this context', () => { const agent = { inject() {}, - session: { events: [{ type: 'plan/mode', data: { active: true } }] }, + session: { + snapshotEvents: () => [{ type: 'plan/mode', data: { active: true } }], + }, } assert.equal(shouldInterceptExitPlanMode({ name: EXIT_PLAN_MODE,