|
| 1 | +/** |
| 2 | + * Regression test for the `output.parts` in-place mutation contract. |
| 3 | + * |
| 4 | + * Background: the OpenCode runtime invokes `command.execute.before` like |
| 5 | + * |
| 6 | + * yield* plugin.trigger("command.execute.before", input, { parts }) |
| 7 | + * |
| 8 | + * where the second argument is an inline wrapper around the outer `parts` |
| 9 | + * array. After the trigger returns, OpenCode discards the wrapper and uses |
| 10 | + * its outer `parts` variable to call `prompt(...)`. |
| 11 | + * |
| 12 | + * If the handler REASSIGNS `output.parts = [newArray]`, only the wrapper's |
| 13 | + * property changes — the outer `parts` reference still points at the old, |
| 14 | + * empty array and the LLM never sees the handler's text. This was the bug |
| 15 | + * fixed by switching the handler to in-place mutation (`splice`). |
| 16 | + * |
| 17 | + * Reference: upstream `packages/opencode/src/session/prompt.ts` discards |
| 18 | + * the trigger return value and reads from the local `parts` variable; |
| 19 | + * upstream `packages/opencode/src/plugin/index.ts` `Plugin.trigger` does |
| 20 | + * not propagate the wrapper object back. |
| 21 | + * |
| 22 | + * Note: This test does NOT assert LLM suppression. Current OpenCode runs |
| 23 | + * `prompt()` unconditionally after the hook (upstream issue |
| 24 | + * anomalyco/opencode#9306). All we can do plugin-side is ensure the |
| 25 | + * handler text reaches the LLM as its input. |
| 26 | + */ |
| 27 | + |
| 28 | +import { afterEach, beforeEach, describe, expect, it } from 'bun:test'; |
| 29 | +import * as fs from 'node:fs'; |
| 30 | +import * as os from 'node:os'; |
| 31 | +import * as path from 'node:path'; |
| 32 | +import { swarmState } from '../state'; |
| 33 | +import { createSwarmCommandHandler } from './index'; |
| 34 | + |
| 35 | +function makeTempDir(): string { |
| 36 | + return fs.mkdtempSync(path.join(os.tmpdir(), 'cmd-parts-mutation-test-')); |
| 37 | +} |
| 38 | + |
| 39 | +function makeSession(id: string): void { |
| 40 | + swarmState.agentSessions.set(id, { |
| 41 | + agentName: 'architect', |
| 42 | + lastToolCallTime: Date.now(), |
| 43 | + lastAgentEventTime: Date.now(), |
| 44 | + delegationActive: false, |
| 45 | + activeInvocationId: 0, |
| 46 | + lastInvocationIdByAgent: {}, |
| 47 | + windows: {}, |
| 48 | + lastCompactionHint: 0, |
| 49 | + architectWriteCount: 0, |
| 50 | + lastCoderDelegationTaskId: null, |
| 51 | + currentTaskId: null, |
| 52 | + gateLog: new Map(), |
| 53 | + reviewerCallCount: new Map(), |
| 54 | + lastGateFailure: null, |
| 55 | + partialGateWarningsIssuedForTask: new Set(), |
| 56 | + selfFixAttempted: false, |
| 57 | + selfCodingWarnedAtCount: 0, |
| 58 | + catastrophicPhaseWarnings: new Set(), |
| 59 | + qaSkipCount: 0, |
| 60 | + qaSkipTaskIds: [], |
| 61 | + taskWorkflowStates: new Map(), |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +describe('createSwarmCommandHandler — output.parts in-place mutation', () => { |
| 66 | + let tempDir: string; |
| 67 | + const sessionId = 'parts-mutation-test-session'; |
| 68 | + |
| 69 | + beforeEach(() => { |
| 70 | + tempDir = makeTempDir(); |
| 71 | + makeSession(sessionId); |
| 72 | + // Pre-create first-run sentinel so welcome banner does not appear |
| 73 | + const swarmDir = path.join(tempDir, '.swarm'); |
| 74 | + fs.mkdirSync(swarmDir, { recursive: true }); |
| 75 | + fs.writeFileSync( |
| 76 | + path.join(swarmDir, '.first-run-complete'), |
| 77 | + `first-run-complete: ${new Date().toISOString()}\n`, |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + afterEach(() => { |
| 82 | + swarmState.agentSessions.delete(sessionId); |
| 83 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('mutates the existing parts array in place rather than reassigning', async () => { |
| 87 | + const handler = createSwarmCommandHandler(tempDir, {}); |
| 88 | + const originalPartsRef: unknown[] = []; |
| 89 | + const output = { parts: originalPartsRef }; |
| 90 | + |
| 91 | + await handler( |
| 92 | + { command: 'swarm', arguments: 'help', sessionID: sessionId }, |
| 93 | + output, |
| 94 | + ); |
| 95 | + |
| 96 | + expect(output.parts).toBe(originalPartsRef); |
| 97 | + expect(output.parts).toHaveLength(1); |
| 98 | + const text = (output.parts[0] as { text: string }).text; |
| 99 | + expect(typeof text).toBe('string'); |
| 100 | + expect(text.length).toBeGreaterThan(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it('mutates in place for swarm-* shortcut commands too', async () => { |
| 104 | + const handler = createSwarmCommandHandler(tempDir, {}); |
| 105 | + const originalPartsRef: unknown[] = []; |
| 106 | + const output = { parts: originalPartsRef }; |
| 107 | + |
| 108 | + await handler( |
| 109 | + { command: 'swarm-agents', arguments: '', sessionID: sessionId }, |
| 110 | + output, |
| 111 | + ); |
| 112 | + |
| 113 | + expect(output.parts).toBe(originalPartsRef); |
| 114 | + expect(output.parts).toHaveLength(1); |
| 115 | + }); |
| 116 | + |
| 117 | + it('mutates in place for the not-found path', async () => { |
| 118 | + const handler = createSwarmCommandHandler(tempDir, {}); |
| 119 | + const originalPartsRef: unknown[] = []; |
| 120 | + const output = { parts: originalPartsRef }; |
| 121 | + |
| 122 | + await handler( |
| 123 | + { |
| 124 | + command: 'swarm', |
| 125 | + arguments: 'definitely-not-a-real-subcommand', |
| 126 | + sessionID: sessionId, |
| 127 | + }, |
| 128 | + output, |
| 129 | + ); |
| 130 | + |
| 131 | + expect(output.parts).toBe(originalPartsRef); |
| 132 | + expect(output.parts).toHaveLength(1); |
| 133 | + const text = (output.parts[0] as { text: string }).text; |
| 134 | + expect(text).toContain('not found'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('does NOT mutate parts when the command is not a swarm command', async () => { |
| 138 | + const handler = createSwarmCommandHandler(tempDir, {}); |
| 139 | + const originalPartsRef: unknown[] = []; |
| 140 | + const output = { parts: originalPartsRef }; |
| 141 | + |
| 142 | + await handler( |
| 143 | + { command: 'unrelated', arguments: '', sessionID: sessionId }, |
| 144 | + output, |
| 145 | + ); |
| 146 | + |
| 147 | + expect(output.parts).toBe(originalPartsRef); |
| 148 | + expect(output.parts).toHaveLength(0); |
| 149 | + }); |
| 150 | + |
| 151 | + it('replaces pre-existing parts entries on a swarm command', async () => { |
| 152 | + const handler = createSwarmCommandHandler(tempDir, {}); |
| 153 | + const originalPartsRef: unknown[] = [ |
| 154 | + { type: 'text', text: 'pre-existing user input' }, |
| 155 | + ]; |
| 156 | + const output = { parts: originalPartsRef }; |
| 157 | + |
| 158 | + await handler( |
| 159 | + { command: 'swarm', arguments: 'help', sessionID: sessionId }, |
| 160 | + output, |
| 161 | + ); |
| 162 | + |
| 163 | + expect(output.parts).toBe(originalPartsRef); |
| 164 | + expect(output.parts).toHaveLength(1); |
| 165 | + const text = (output.parts[0] as { text: string }).text; |
| 166 | + expect(text).not.toContain('pre-existing user input'); |
| 167 | + }); |
| 168 | +}); |
0 commit comments