summaryrefslogtreecommitdiff
path: root/tests/opencode/test-session-bootstrap.mjs
blob: 67b311860075d337710c4585e06aa561c7e0ccc6 (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
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { pathToFileURL } from 'node:url';

const [, , inputPath] = process.argv;
assert.ok(inputPath, 'pass the plugin module path');
const pluginURL = pathToFileURL(fs.realpathSync(inputPath));
const marker = '<EXTREMELY_IMPORTANT>\nYou have superpowers.';
let generation = 0;

function reply(flavor, session) {
  return flavor === 'v1' ? { data: session } : session;
}

function makeEvent(flavor, sessionID) {
  const text = { type: 'text', text: 'Execute the assigned task' };
  return {
    sessionID,
    messages: [flavor === 'v1'
      ? { info: { role: 'user', sessionID }, parts: [text] }
      : { role: 'user', content: [text] }],
  };
}

function bootstrapCount(event) {
  return event.messages.flatMap((message) => message.parts ?? message.content ?? []).filter(
    (part) => part.type === 'text' && part.text.startsWith(marker)
  ).length;
}

async function makeHarness(flavor, fetchSession) {
  const mod = await import(`${pluginURL.href}?session-test=${++generation}`);
  const lookups = [];
  const registered = [];
  const get = async (id) => {
    lookups.push(id);
    return fetchSession(id, lookups.length);
  };
  let invoke;
  if (flavor === 'v1') {
    const hooks = await mod.SuperpowersPlugin({
      client: { session: { get: ({ path: { id } }) => get(id) } },
      directory: '.',
    });
    invoke = (event) => hooks['experimental.chat.messages.transform']({}, event);
  } else {
    await mod.default.setup({
      skill: { transform: async (transform) => transform({ add: (skill) => registered.push(skill) }) },
      session: {
        get: ({ sessionID }) => get(sessionID),
        hook: async (name, callback) => { if (name === 'context') invoke = callback; },
      },
    });
  }
  assert.equal(typeof invoke, 'function');
  return { invoke, lookups, registered };
}

for (const flavor of ['v1', 'v2']) {
  for (const [kind, extra, expected] of [
    ['root', {}, 1],
    ['child', { parentID: 'parent' }, 0],
    ['fork', { fork: { sessionID: 'origin' } }, 1],
  ]) {
    const id = `${flavor}-${kind}`;
    const h = await makeHarness(flavor, () => reply(flavor, { id, ...extra }));
    const event = makeEvent(flavor, id);
    await h.invoke(event);
    assert.equal(bootstrapCount(event), expected, `${id}: first request`);
    await h.invoke(event);
    assert.equal(bootstrapCount(event), expected, `${id}: repeated event`);
    const fresh = makeEvent(flavor, id);
    await h.invoke(fresh);
    assert.equal(bootstrapCount(fresh), expected, `${id}: fresh request`);
    assert.deepEqual(h.lookups, [id], `${id}: cache successful classification`);
    if (flavor === 'v2' && kind === 'child') {
      assert.ok(h.registered.some((skill) => skill.id === 'brainstorming'));
    }
  }

  const failures = [
    ['throws', () => { throw new Error('temporary lookup failure'); }],
    ['missing', () => undefined],
    ['null', () => null],
    ['empty', () => reply(flavor, {})],
    ['wrong-id', () => reply(flavor, { id: 'different-session' })],
    ['invalid-parent', (id) => reply(flavor, { id, parentID: 42 })],
  ];
  if (flavor === 'v1') {
    failures.push(['resolved-http-error', () => ({
      data: undefined,
      error: { name: 'UnknownError', data: { message: 'temporary 503' } },
      response: { ok: false, status: 503 },
    })]);
  }
  for (const [kind, firstResult] of failures) {
    const id = `${flavor}-${kind}`;
    const h = await makeHarness(flavor, (sessionID, call) => call === 1
      ? firstResult(sessionID)
      : reply(flavor, { id: sessionID, parentID: 'parent' }));
    const counts = [];
    for (let step = 0; step < 2; step++) {
      const event = makeEvent(flavor, id);
      await h.invoke(event);
      counts.push(bootstrapCount(event));
    }
    assert.deepEqual(counts, [1, 0], `${id}: recover on the next request`);
    assert.deepEqual(h.lookups, [id, id], `${id}: never cache the failure`);
  }

  const isolated = await makeHarness(flavor, (id) => reply(flavor,
    id === 'child-session' ? { id, parentID: 'parent' } : { id }));
  for (const [id, expected] of [['root-session', 1], ['child-session', 0], ['root-session', 1], ['child-session', 0]]) {
    const event = makeEvent(flavor, id);
    await isolated.invoke(event);
    assert.equal(bootstrapCount(event), expected);
  }
  assert.deepEqual(isolated.lookups, ['root-session', 'child-session']);

  const bounded = await makeHarness(flavor, (id) => reply(flavor, { id, parentID: 'parent' }));
  for (let index = 0; index <= 512; index++) {
    const event = makeEvent(flavor, `eviction-${index}`);
    await bounded.invoke(event);
    assert.equal(bootstrapCount(event), 0);
  }
  const evicted = makeEvent(flavor, 'eviction-0');
  await bounded.invoke(evicted);
  assert.equal(bootstrapCount(evicted), 0);
  assert.equal(bounded.lookups.filter((id) => id === 'eviction-0').length, 2);

  const restarted = await makeHarness(flavor, (id) => reply(flavor, { id, parentID: 'parent' }));
  const afterRestart = makeEvent(flavor, 'eviction-0');
  await restarted.invoke(afterRestart);
  assert.equal(bootstrapCount(afterRestart), 0);
  assert.deepEqual(restarted.lookups, ['eviction-0']);

  const unknown = await makeHarness(flavor, () => { throw new Error('must not look up a missing ID'); });
  const noID = makeEvent(flavor, undefined);
  await unknown.invoke(noID);
  assert.equal(bootstrapCount(noID), 1);
  assert.deepEqual(unknown.lookups, []);

  // #2197: a prompt that merely mentions the bootstrap's tag is not the bootstrap.
  const mentions = await makeHarness(flavor, (id) => reply(flavor, { id }));
  const probe = makeEvent(flavor, `${flavor}-mentions-tag`);
  (probe.messages[0].parts ?? probe.messages[0].content)[0].text =
    "Is there an 'EXTREMELY_IMPORTANT' block in your context?";
  await mentions.invoke(probe);
  assert.equal(bootstrapCount(probe), 1, `${flavor}: prompt mentioning the tag still gets bootstrap`);
  await mentions.invoke(probe);
  assert.equal(bootstrapCount(probe), 1, `${flavor}: prompt mentioning the tag, repeated event`);
}

function compactedEvent(sessionID) {
  return {
    sessionID,
    system: [],
    messages: [{
      role: 'assistant',
      content: [{ type: 'compaction', provider: 'fixture', encrypted: 'opaque-checkpoint' }],
    }],
  };
}

const compactedRoot = await makeHarness('v2', (id) => ({ id }));
const rootEvent = compactedEvent('compacted-root');
const checkpoint = structuredClone(rootEvent.messages[0]);
await compactedRoot.invoke(rootEvent);
assert.equal(bootstrapCount(rootEvent), 1);
assert.deepEqual(rootEvent.messages[0], checkpoint);
assert.equal(rootEvent.messages.length, 2);
assert.equal(rootEvent.messages[1].role, 'user');
assert.deepEqual(rootEvent.system, []);
await compactedRoot.invoke(rootEvent);
assert.equal(bootstrapCount(rootEvent), 1);
assert.equal(rootEvent.messages.length, 2);
const freshRootEvent = compactedEvent('compacted-root');
await compactedRoot.invoke(freshRootEvent);
assert.equal(bootstrapCount(freshRootEvent), 1);
assert.deepEqual(compactedRoot.lookups, ['compacted-root']);

const compactedChild = await makeHarness('v2', (id) => ({ id, parentID: 'parent' }));
const childEvent = compactedEvent('compacted-child');
const originalChild = structuredClone(childEvent);
await compactedChild.invoke(childEvent);
assert.equal(bootstrapCount(childEvent), 0);
assert.deepEqual(childEvent, originalChild);
assert.deepEqual(compactedChild.lookups, ['compacted-child']);

const retryChild = await makeHarness('v2', (id, call) => {
  if (call === 1) throw new Error('temporary lookup failure');
  return { id, parentID: 'parent' };
});
const unknownChild = compactedEvent('retry-compacted-child');
await retryChild.invoke(unknownChild);
assert.equal(bootstrapCount(unknownChild), 1);
const recoveredChild = compactedEvent('retry-compacted-child');
await retryChild.invoke(recoveredChild);
assert.equal(bootstrapCount(recoveredChild), 0);
assert.equal(recoveredChild.messages.length, 1);
assert.equal(retryChild.lookups.length, 2);

const newPromptAfterCheckpoint = compactedEvent('new-prompt-after-checkpoint-root');
newPromptAfterCheckpoint.messages.push({ role: 'user', content: [{ type: 'text', text: 'Continue' }] });
await compactedRoot.invoke(newPromptAfterCheckpoint);
assert.equal(bootstrapCount(newPromptAfterCheckpoint), 1);
assert.equal(newPromptAfterCheckpoint.messages.length, 2);
assert.equal(newPromptAfterCheckpoint.messages[1].content.length, 2);

const retainedUser = compactedEvent('retained-user-root');
retainedUser.messages.unshift({ role: 'user', content: [{ type: 'text', text: 'Keep going' }] });
const retainedCheckpoint = structuredClone(retainedUser.messages[1]);
await compactedRoot.invoke(retainedUser);
assert.equal(bootstrapCount(retainedUser), 1);
assert.equal(retainedUser.messages.length, 2);
assert.equal(retainedUser.messages[0].content.length, 2);
assert.ok(retainedUser.messages[0].content[0].text.startsWith(marker));
assert.equal(retainedUser.messages[0].content[1].text, 'Keep going');
assert.deepEqual(retainedUser.messages[1], retainedCheckpoint);
await compactedRoot.invoke(retainedUser);
assert.equal(bootstrapCount(retainedUser), 1);
assert.equal(retainedUser.messages.length, 2);

const retainedUserChild = compactedEvent('retained-user-child');
retainedUserChild.messages.unshift({ role: 'user', content: [{ type: 'text', text: 'Keep going' }] });
const originalRetainedUserChild = structuredClone(retainedUserChild);
await compactedChild.invoke(retainedUserChild);
assert.equal(bootstrapCount(retainedUserChild), 0);
assert.deepEqual(retainedUserChild, originalRetainedUserChild);
const empty = { sessionID: 'empty', messages: [] };
await compactedRoot.invoke(empty);
assert.deepEqual(empty.messages, []);

console.log('Session classification, recovery and cache lifetime passed');