From 74fa90cecc6f6dfd93dc64893659771001ad2d82 Mon Sep 17 00:00:00 2001 From: Ogulcan Celik Date: Wed, 24 Jun 2026 00:52:07 +0300 Subject: fix: scope opencode hook to main agent and adopt new sessions refs #765 --- .../assets/opencode/herdr-agent-state.js | 31 +++++++++++++++++++--- src/integration/mod.rs | 2 +- 2 files changed, 29 insertions(+), 4 deletions(-) (limited to 'src/integration') diff --git a/src/integration/assets/opencode/herdr-agent-state.js b/src/integration/assets/opencode/herdr-agent-state.js index 6836227b..a46d1d88 100644 --- a/src/integration/assets/opencode/herdr-agent-state.js +++ b/src/integration/assets/opencode/herdr-agent-state.js @@ -2,7 +2,7 @@ // managed by herdr; reinstalling or updating the integration overwrites this file. // add custom hooks/plugins beside this file instead of editing it. // HERDR_INTEGRATION_ID=opencode -// HERDR_INTEGRATION_VERSION=6 +// HERDR_INTEGRATION_VERSION=7 import net from "node:net"; @@ -10,6 +10,11 @@ const SOURCE = "herdr:opencode"; const AGENT = "opencode"; let reportSeq = Date.now() * 1000; +// Subagent (task tool) sessions carry a parentID; the main agent session does +// not. Their lifecycle events would otherwise clobber the pane's real state, so +// learn child session ids from session.created/updated and drop their reports. +const childSessions = new Set(); + function nextReportSeq() { reportSeq += 1; return reportSeq; @@ -81,11 +86,15 @@ function request(method, params) { }); } -function reportSession(sessionID) { +function reportSession(sessionID, sessionStartSource) { if (!sessionID) { return Promise.resolve(); } - return request("pane.report_agent_session", { agent_session_id: sessionID }); + const params = { agent_session_id: sessionID }; + if (sessionStartSource) { + params.session_start_source = sessionStartSource; + } + return request("pane.report_agent_session", params); } function reportState(state, sessionID) { @@ -107,6 +116,9 @@ export const HerdrAgentStatePlugin = async () => { return { "chat.message": async ({ sessionID }) => { + if (sessionID && childSessions.has(sessionID)) { + return; + } await reportState("working", sessionID); }, event: async ({ event }) => { @@ -114,8 +126,21 @@ export const HerdrAgentStatePlugin = async () => { const properties = event?.properties ?? {}; const sessionID = sessionIDFromProperties(properties); + const info = properties.info; + if (info?.id && info.parentID) { + childSessions.add(info.id); + } + if (sessionID && childSessions.has(sessionID)) { + return; + } + switch (type) { case "session.created": + // A root session.created is a genuine new-session start (subagent + // creates are dropped above). Signal it so herdr replaces the pane's + // prior session id instead of treating the change as cross-talk. + await reportSession(sessionID, "new"); + break; case "session.updated": await reportSession(sessionID); break; diff --git a/src/integration/mod.rs b/src/integration/mod.rs index d08ad32e..06561e3d 100644 --- a/src/integration/mod.rs +++ b/src/integration/mod.rs @@ -135,7 +135,7 @@ const DROID_REMOVED_LIFECYCLE_HOOK_EVENTS: [(&str, &str); 9] = [ ]; const OPENCODE_PLUGIN_INSTALL_NAME: &str = "herdr-agent-state.js"; const OPENCODE_PLUGIN_ASSET: &str = include_str!("assets/opencode/herdr-agent-state.js"); -const OPENCODE_INTEGRATION_VERSION: u32 = 6; +const OPENCODE_INTEGRATION_VERSION: u32 = 7; const KILO_PLUGIN_INSTALL_NAME: &str = "herdr-agent-state.js"; const KILO_PLUGIN_ASSET: &str = include_str!("assets/kilo/herdr-agent-state.js"); const KILO_INTEGRATION_VERSION: u32 = 2; -- cgit v1.2.3