summaryrefslogtreecommitdiff
path: root/tests/opencode
diff options
context:
space:
mode:
authorJesse Vincent <jesse@fsck.com>2026-10-22 22:21:48 +0000
committerJesse Vincent <jesse@fsck.com>2026-10-22 22:55:41 +0000
commit80b085cfb36c0dfe5f6740a3a4764d16429a8228 (patch)
tree2f0a58b25596836001bb2f4f81078406c306b118 /tests/opencode
parentb58deb3b948d4c305feeac122700100419ee2777 (diff)
fix(opencode): inject the V2 bootstrap on compaction and generate requests
The V2 plugin registered its bootstrap injector only on ctx.session.hook('context'). OpenCode 2 prepares each model request kind through its own hook: normal agent turns through `context`, the compaction (summary) request through `compaction`, and session-scoped one-shot generation through `generate` (packages/core/src/session/model-request.ts). Every built-in V2 plugin registers its handler on all three, and the upstream V1 migration guide says to do the same when a transform must apply to every request kind. So the compaction request's first user message lacked the bootstrap that every prior turn carried, and the prompt diverged right after system + tools. On a provider with prefix caching that is a cold re-prefill of the whole session on its largest request (#2391). Reproduced on real OpenCode 2.1.2 and 2.1.3 with a scripted stub model that pushes the session over the compaction ceiling: before, the compaction request shares 26.5k of 30.8k serialized chars with the previous agent request and differs at the first user message; after, the previous request is a full prefix of the compaction request. The handler body is unchanged (child-session skip, idempotence guard, checkpoint-only append), lifted into a named function and registered on all three hooks, each in its own try/catch. Based on PR #2409. Fixes #2391 Co-authored-by: Aashish <145881415+aashish254@users.noreply.github.com> Claude-Session: https://claude.ai/code/session_01SHANcr4A71nzuU9et4Fnba
Diffstat (limited to 'tests/opencode')
-rwxr-xr-xtests/opencode/run-tests.sh2
-rw-r--r--tests/opencode/test-hook-registration.mjs98
-rwxr-xr-xtests/opencode/test-hook-registration.sh4
3 files changed, 104 insertions, 0 deletions
diff --git a/tests/opencode/run-tests.sh b/tests/opencode/run-tests.sh
index 43e1a3a..4cdf546 100755
--- a/tests/opencode/run-tests.sh
+++ b/tests/opencode/run-tests.sh
@@ -46,6 +46,7 @@ while [[ $# -gt 0 ]]; do
echo " test-plugin-loading.sh Verify plugin installation and structure"
echo " test-bootstrap-caching.sh Verify bootstrap content caching"
echo " test-session-bootstrap.sh Verify session classification and lookup recovery"
+ echo " test-hook-registration.sh Verify V2 bootstrap registers on context, compaction, generate"
echo " test-skill-registration.sh Verify V2 skill registration contract (2.0.4 path field)"
echo " test-tools.sh Test use_skill and find_skills tools (integration)"
echo " test-priority.sh Test skill priority resolution (integration)"
@@ -64,6 +65,7 @@ tests=(
"test-plugin-loading.sh"
"test-bootstrap-caching.sh"
"test-session-bootstrap.sh"
+ "test-hook-registration.sh"
"test-skill-registration.sh"
)
diff --git a/tests/opencode/test-hook-registration.mjs b/tests/opencode/test-hook-registration.mjs
new file mode 100644
index 0000000..9df6a6f
--- /dev/null
+++ b/tests/opencode/test-hook-registration.mjs
@@ -0,0 +1,98 @@
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import { pathToFileURL } from 'node:url';
+
+// Issue #2391: the V2 plugin must register bootstrap injection on
+// `context`, `compaction`, and `generate`. OpenCode dispatches each request
+// shape through a different hook, so a handler on `context` only means
+// compaction and generate requests diverge from the cached prefix at the
+// tail — a cold re-prefill of the whole session on the largest request of
+// its lifetime.
+
+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;
+
+async function captureHooks(fetchSession) {
+ const mod = await import(`${pluginURL.href}?hooks-test=${++generation}`);
+ const hooks = new Map();
+ await mod.default.setup({
+ skill: { transform: async (transform) => transform({ add: () => {} }) },
+ session: {
+ get: ({ sessionID }) => fetchSession(sessionID),
+ hook: async (name, callback) => { hooks.set(name, callback); },
+ },
+ });
+ return hooks;
+}
+
+function event(sessionID) {
+ return {
+ sessionID,
+ messages: [{ role: 'user', content: [{ type: 'text', text: 'continue' }] }],
+ };
+}
+
+const session = (id) => ({ id });
+
+// All three request hooks must be registered.
+for (const required of ['context', 'compaction', 'generate']) {
+ const hooks = await captureHooks(async (id) => session(id));
+ assert.ok(hooks.has(required), `plugin registers '${required}' hook`);
+}
+
+// One shared handler: the three hooks resolve to the same function so
+// behaviour cannot drift between request shapes.
+const shared = await captureHooks(async (id) => session(id));
+assert.equal(
+ shared.get('compaction'),
+ shared.get('context'),
+ 'compaction handler is the context handler',
+);
+assert.equal(
+ shared.get('generate'),
+ shared.get('context'),
+ 'generate handler is the context handler',
+);
+
+// Each hook actually injects the bootstrap into an eligible session.
+for (const name of ['context', 'compaction', 'generate']) {
+ const hooks = await captureHooks(async (id) => session(id));
+ const ev = event(`${name}-root`);
+ await hooks.get(name)(ev);
+ assert.equal(
+ ev.messages[0].content.filter((p) => p.text?.startsWith(marker)).length,
+ 1,
+ `'${name}' hook injects bootstrap exactly once`,
+ );
+}
+
+// Child sessions stay suppressed on every hook (regression guard against
+// only 'context' checking parentID while compaction/generate skip the check).
+for (const name of ['context', 'compaction', 'generate']) {
+ const hooks = await captureHooks(async (id) => ({ id, parentID: 'parent' }));
+ const ev = event(`${name}-child`);
+ await hooks.get(name)(ev);
+ assert.equal(
+ ev.messages[0].content.filter((p) => p.text?.startsWith(marker)).length,
+ 0,
+ `'${name}' hook skips bootstrap for child sessions`,
+ );
+}
+
+// Idempotence: a second delivery of the same event does not double-inject.
+for (const name of ['context', 'compaction', 'generate']) {
+ const hooks = await captureHooks(async (id) => session(id));
+ const ev = event(`${name}-root`);
+ await hooks.get(name)(ev);
+ await hooks.get(name)(ev);
+ assert.equal(
+ ev.messages[0].content.filter((p) => p.text?.startsWith(marker)).length,
+ 1,
+ `'${name}' hook stays idempotent on repeat`,
+ );
+}
+
+console.log('V2 hook registration (context/compaction/generate) passed');
diff --git a/tests/opencode/test-hook-registration.sh b/tests/opencode/test-hook-registration.sh
new file mode 100755
index 0000000..47191ef
--- /dev/null
+++ b/tests/opencode/test-hook-registration.sh
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+set -euo pipefail
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+node "$SCRIPT_DIR/test-hook-registration.mjs" "$SCRIPT_DIR/../../.opencode/plugins/superpowers.js"