diff --git a/cmd/entire/cli/agent/opencode/entire_plugin.ts b/cmd/entire/cli/agent/opencode/entire_plugin.ts index 8544aa59a..a8a30d274 100644 --- a/cmd/entire/cli/agent/opencode/entire_plugin.ts +++ b/cmd/entire/cli/agent/opencode/entire_plugin.ts @@ -75,11 +75,11 @@ export const EntirePlugin: Plugin = async ({ directory }) => { seenUserMessages.clear() messageStore.clear() currentModel = null + await callHook("session-start", { + session_id: session.id, + }) } currentSessionID = session.id - await callHook("session-start", { - session_id: session.id, - }) break } diff --git a/cmd/entire/cli/agent/opencode/hooks_test.go b/cmd/entire/cli/agent/opencode/hooks_test.go index 744714b26..aaf39a593 100644 --- a/cmd/entire/cli/agent/opencode/hooks_test.go +++ b/cmd/entire/cli/agent/opencode/hooks_test.go @@ -101,6 +101,45 @@ func TestInstallHooks_LocalDev(t *testing.T) { } } +func TestInstallHooks_SessionStartIsGuardedBySessionSwitch(t *testing.T) { + dir := t.TempDir() + t.Chdir(dir) + ag := &OpenCodeAgent{} + + if _, err := ag.InstallHooks(context.Background(), false, false); err != nil { + t.Fatalf("install failed: %v", err) + } + + pluginPath := filepath.Join(dir, ".opencode", "plugins", "entire.ts") + data, err := os.ReadFile(pluginPath) + if err != nil { + t.Fatalf("plugin file not created: %v", err) + } + + content := string(data) + guard := "if (currentSessionID !== session.id) {" + hook := `await callHook("session-start", {` + currentSessionAssignment := "currentSessionID = session.id" + + guardIdx := strings.Index(content, guard) + hookIdx := strings.Index(content, hook) + assignIdx := strings.Index(content, currentSessionAssignment) + + if guardIdx == -1 { + t.Fatalf("plugin file missing guard %q", guard) + } + if hookIdx == -1 { + t.Fatalf("plugin file missing session-start hook call %q", hook) + } + if assignIdx == -1 { + t.Fatalf("plugin file missing current session assignment %q", currentSessionAssignment) + } + if !(guardIdx < hookIdx && hookIdx < assignIdx) { + t.Fatalf("expected guarded session-start call before session assignment, got guard=%d hook=%d assignment=%d", + guardIdx, hookIdx, assignIdx) + } +} + func TestInstallHooks_ForceReinstall(t *testing.T) { dir := t.TempDir() t.Chdir(dir)