summaryrefslogtreecommitdiff
path: root/tests/opencode/test-bootstrap-caching.mjs
blob: 386b7b1eff084063b764d9442102e754f7e907ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import fs from 'fs';
import { pathToFileURL } from 'url';

const [, , pluginPath, scenario] = process.argv;

if (!pluginPath || !['present', 'missing'].includes(scenario)) {
  console.error('Usage: node test-bootstrap-caching.mjs PLUGIN_PATH present|missing');
  process.exit(2);
}

let existsCount = 0;
let readCount = 0;

const originalExistsSync = fs.existsSync;
const originalReadFileSync = fs.readFileSync;

fs.existsSync = function (...args) {
  if (isBootstrapSkillPath(args[0])) {
    existsCount += 1;
  }
  return originalExistsSync.apply(this, args);
};

fs.readFileSync = function (...args) {
  if (isBootstrapSkillPath(args[0])) {
    readCount += 1;
  }
  return originalReadFileSync.apply(this, args);
};

const mod = await import(pathToFileURL(pluginPath).href);
const plugin = await mod.SuperpowersPlugin({ client: {}, directory: '.' });
const transform = plugin['experimental.chat.messages.transform'];

// Mapping constants are flavor-specific (#opencode-v2): V1 keeps the 1.18.x
// tool names, V2 teaches the renamed tools. Assert both directly.
const mappingFailures = assertMappingConstants(mod);

const firstOutput = makeOutput(`${scenario} bootstrap first step`);
await transform({}, firstOutput);
const afterFirst = { existsCount, readCount };

const secondOutput = makeOutput(`${scenario} bootstrap second step`);
await transform({}, secondOutput);
const afterSecond = { existsCount, readCount };

// Exercise the V2 path (setup() + ctx.session.hook("context")) with a mock
// ctx so the V2_MAPPING wiring is verified, not just the constant. Run after
// the V1 count snapshots: setup() reads SKILL.md files during registration.
const v2Result = await runV2ContextHook(mod);

const result = {
  scenario,
  firstBootstrapParts: countBootstrapParts(firstOutput),
  secondBootstrapParts: countBootstrapParts(secondOutput),
  staleMentionMapping: bootstrapText(firstOutput).includes('@mention'),
  staleTaskMapping: bootstrapText(firstOutput).includes('`Task` tool with subagents'),
  mapsSubagentToTask: bootstrapText(firstOutput).includes('`task` with `subagent_type: "general"`'),
  mapsMutationToApplyPatch: bootstrapText(firstOutput).includes('`apply_patch`'),
  firstReadCount: afterFirst.readCount,
  secondReadCount: afterSecond.readCount,
  firstExistsCount: afterFirst.existsCount,
  secondExistsCount: afterSecond.existsCount,
  v2BootstrapParts: v2Result.bootstrapParts,
  mapsV2SubagentTool: v2Result.text.includes('`subagent` with `agent: "general"`'),
  mapsV2SessionIDContinuation: v2Result.text.includes('`sessionID` to continue a previous subagent'),
  mapsV2NoTodoTool: v2Result.text.includes('no todo tool'),
  mapsV2MutationToPatch: v2Result.text.includes('`patch` with `patchText`'),
  mapsV2Shell: v2Result.text.includes('`shell`'),
  staleV1ToolsInV2: v2Result.text.includes('`apply_patch`') || v2Result.text.includes('`todowrite`') || v2Result.text.includes('`subagent_type`'),
};

const failures = scenario === 'present'
  ? assertPresentBootstrap(result)
  : assertMissingBootstrap(result);

if (scenario === 'present') {
  failures.push(...assertV2Bootstrap(result));
}
failures.push(...mappingFailures);

if (failures.length > 0) {
  console.error(JSON.stringify(result, null, 2));
  for (const failure of failures) {
    console.error(`FAIL: ${failure}`);
  }
  process.exit(1);
}

console.log(JSON.stringify(result, null, 2));

function isBootstrapSkillPath(filePath) {
  return String(filePath).replaceAll('\\', '/').includes('using-superpowers/SKILL.md');
}

function makeOutput(text) {
  return {
    messages: [{
      info: { role: 'user' },
      parts: [{ type: 'text', text }],
    }],
  };
}

function countBootstrapParts(output) {
  return output.messages[0].parts.filter(
    (part) => part.type === 'text' && part.text.includes('EXTREMELY_IMPORTANT')
  ).length;
}

function bootstrapText(output) {
  return output.messages[0].parts.find(
    (part) => part.type === 'text' && part.text.includes('EXTREMELY_IMPORTANT')
  )?.text || '';
}

function assertPresentBootstrap(result) {
  const failures = [];
  if (result.firstBootstrapParts !== 1) {
    failures.push(`expected first transform to inject one bootstrap part, got ${result.firstBootstrapParts}`);
  }
  if (result.secondBootstrapParts !== 1) {
    failures.push(`expected second transform to inject one bootstrap part, got ${result.secondBootstrapParts}`);
  }
  if (result.firstReadCount !== 1) {
    failures.push(`expected first transform to read SKILL.md once, got ${result.firstReadCount}`);
  }
  if (result.secondReadCount !== result.firstReadCount) {
    failures.push(`expected cached second transform to do no additional reads, got ${result.secondReadCount - result.firstReadCount}`);
  }
  if (result.secondExistsCount !== result.firstExistsCount) {
    failures.push(`expected cached second transform to do no additional exists checks, got ${result.secondExistsCount - result.firstExistsCount}`);
  }
  if (result.staleMentionMapping) {
    failures.push('expected OpenCode bootstrap not to teach @mention subagent syntax');
  }
  if (result.staleTaskMapping) {
    failures.push('expected OpenCode bootstrap not to teach stale Task-tool mapping');
  }
  if (!result.mapsSubagentToTask) {
    failures.push('expected OpenCode bootstrap to map general-purpose subagents to task with subagent_type');
  }
  if (!result.mapsMutationToApplyPatch) {
    failures.push('expected OpenCode bootstrap to map file mutation to apply_patch');
  }
  return failures;
}

function assertMissingBootstrap(result) {
  const failures = [];
  if (result.firstBootstrapParts !== 0) {
    failures.push(`expected no bootstrap when SKILL.md is missing, got ${result.firstBootstrapParts}`);
  }
  if (result.secondBootstrapParts !== 0) {
    failures.push(`expected no bootstrap on second missing-file transform, got ${result.secondBootstrapParts}`);
  }
  if (result.firstReadCount !== 0 || result.secondReadCount !== 0) {
    failures.push(`expected missing file path to avoid reads, got ${result.secondReadCount}`);
  }
  if (result.firstExistsCount < 1) {
    failures.push('expected first transform to check whether SKILL.md exists');
  }
  if (result.secondExistsCount !== result.firstExistsCount) {
    failures.push(`expected missing-file result to be cached, got ${result.secondExistsCount - result.firstExistsCount} extra exists checks`);
  }
  return failures;
}

function assertMappingConstants(mod) {
  const failures = [];
  if (typeof mod.V1_MAPPING !== 'string' || typeof mod.V2_MAPPING !== 'string') {
    failures.push('expected plugin to export V1_MAPPING and V2_MAPPING string constants');
    return failures;
  }
  for (const needle of ['`todowrite`', '`task` with `subagent_type: "general"`', '`apply_patch`', '`bash`']) {
    if (!mod.V1_MAPPING.includes(needle)) {
      failures.push(`expected V1_MAPPING to keep the 1.18.x tool name ${needle}`);
    }
  }
  for (const needle of [
    '`subagent` with `agent: "general"`',
    '`sessionID` to continue a previous subagent',
    'no todo tool',
    '`write`',
    '`edit`',
    '`patch` with `patchText`',
    '`shell`',
    '`read`',
    '`grep`, `glob`',
    '`webfetch`',
    '`websearch`',
  ]) {
    if (!mod.V2_MAPPING.includes(needle)) {
      failures.push(`expected V2_MAPPING to teach the V2 tool ${needle}`);
    }
  }
  for (const stale of ['`todowrite`', '`task` with', '`apply_patch`', '`bash`']) {
    if (mod.V2_MAPPING.includes(stale)) {
      failures.push(`expected V2_MAPPING not to teach the V1-only tool name ${stale}`);
    }
  }
  return failures;
}

// Drive setup() with a mock V2 ctx and fire the captured "context" hook on a
// top-level (parentID-less) session. Returns the injected-part count and the
// injected bootstrap text ('' when nothing was injected).
async function runV2ContextHook(mod) {
  let contextHook = null;
  const ctx = {
    skill: {
      transform: async (fn) => {
        fn({ add: () => {} });
      },
    },
    session: {
      hook: async (name, cb) => {
        if (name === 'context') contextHook = cb;
      },
      get: async ({ sessionID }) => ({ id: sessionID }), // top-level: no parentID
    },
  };
  try {
    await mod.default.setup(ctx);
  } catch (err) {
    console.error('[test] V2 setup() threw:', err);
    return { bootstrapParts: 0, text: '' };
  }
  if (typeof contextHook !== 'function') {
    return { bootstrapParts: 0, text: '' };
  }
  const event = {
    sessionID: 'sess-v2-top',
    messages: [{ role: 'user', content: [{ type: 'text', text: 'v2 bootstrap step' }] }],
  };
  await contextHook(event);
  const parts = event.messages[0].content.filter(
    (part) => part.type === 'text' && part.text.includes('EXTREMELY_IMPORTANT')
  );
  return { bootstrapParts: parts.length, text: parts[0]?.text || '' };
}

function assertV2Bootstrap(result) {
  const failures = [];
  if (result.v2BootstrapParts !== 1) {
    failures.push(`expected V2 context hook to inject one bootstrap part, got ${result.v2BootstrapParts}`);
    return failures;
  }
  if (!result.mapsV2SubagentTool) {
    failures.push('expected V2 bootstrap to map general-purpose subagents to subagent with agent');
  }
  if (!result.mapsV2SessionIDContinuation) {
    failures.push('expected V2 bootstrap to teach sessionID continuation for subagents');
  }
  if (!result.mapsV2NoTodoTool) {
    failures.push('expected V2 bootstrap to state that V2 has no todo tool');
  }
  if (!result.mapsV2MutationToPatch) {
    failures.push('expected V2 bootstrap to map file mutation to patch with patchText');
  }
  if (!result.mapsV2Shell) {
    failures.push('expected V2 bootstrap to map shell commands to the shell tool');
  }
  if (result.staleV1ToolsInV2) {
    failures.push('expected V2 bootstrap not to teach V1-only tool names (apply_patch/todowrite/subagent_type)');
  }
  return failures;
}