The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix: reject stale session hooks from replaced engine processes (#166)
Dee389d66f8d03c
diff --git a/cli/src/cli.ts b/cli/src/cli.tsindex 77ef7d501..fae382878 100644--- a/cli/src/cli.ts+++ b/cli/src/cli.ts@@ -3166,9 +3166,9 @@ async function runForeground(session: AuthSession): Promise<void> {* process that POSTs is not a descendant of the pane's engine, so no session ever binds. It is not* a Herdr problem; tmux fails identically.*- * The pane is itself proof: the hook knew a runtime id, that runtime carries exactly one agent of- * this engine, and the caller already had to read the 0600 hook credential to be heard at all. So- * fall back to that, and only when it is unambiguous.+ * Keep that exception specific to Cursor. A delayed hook from an exited process can still name+ * a pane now owned by its replacement; the pane and hook credential alone cannot prove that a+ * Codex (or other engine's) old transcript belongs to the new process.*/const onHintedRuntime = new Map<string, RegisteredSession>()for (const runtime of resolved) {@@ -3192,7 +3192,7 @@ async function runForeground(session: AuthSession): Promise<void> {}}}- const choice = chooseHookAgent([...candidates.values()], [...onHintedRuntime.values()])+ const choice = chooseHookAgent([...candidates.values()], [...onHintedRuntime.values()], engine)if (choice.agent) {if (choice.reason === 'runtime') {console.log(`[hooks] ${engine} hook accepted on runtime evidence alone`diff --git a/cli/src/hookServer.spec.ts b/cli/src/hookServer.spec.tsindex ba97ea1c0..3153a5758 100644--- a/cli/src/hookServer.spec.ts+++ b/cli/src/hookServer.spec.ts@@ -4,6 +4,7 @@ import { startHookServer, type HookServerHandlers, chooseHookAgent } from './hooimport { env } from './config/env.js'import { readHookCredential } from './lib/hookAuth.js'import { CommandBarService } from './lib/commandBar.js'+import { ENGINES } from './engines/types.js'let server: Server | null = null@@ -220,23 +221,33 @@ describe('the Harness Store proxy', () => {describe('chooseHookAgent', () => {it('prefers caller ancestry, the evidence that cannot be guessed at', () => {- expect(chooseHookAgent(['strong'], ['weak'])).toEqual({ agent: 'strong', reason: 'ancestry' })+ expect(chooseHookAgent(['strong'], ['weak'], 'codex')).toEqual({ agent: 'strong', reason: 'ancestry' })})it('accepts the runtime alone when ancestry is unavailable and the pane is unambiguous', () => {// Cursor posts its hooks from outside the pane's process tree — on tmux and on Herdr alike — so// demanding ancestry rejected every hook it ever sent and no session bound. The pane is the proof:// the hook named a runtime, and that runtime carries exactly one agent of this engine.- expect(chooseHookAgent([], ['only-agent-on-that-pane'])).toEqual({+ expect(chooseHookAgent([], ['only-agent-on-that-pane'], 'cursor')).toEqual({agent: 'only-agent-on-that-pane', reason: 'runtime',})})it('answers nothing rather than guessing', () => {- expect(chooseHookAgent([], [])).toEqual({ agent: null, reason: 'none' })- expect(chooseHookAgent([], ['a', 'b'])).toEqual({ agent: null, reason: 'ambiguous' })- expect(chooseHookAgent(['a', 'b'], ['c'])).toEqual({ agent: null, reason: 'ambiguous' })+ expect(chooseHookAgent([], [], 'cursor')).toEqual({ agent: null, reason: 'none' })+ expect(chooseHookAgent([], ['a', 'b'], 'cursor')).toEqual({ agent: null, reason: 'ambiguous' })+ expect(chooseHookAgent(['a', 'b'], ['c'], 'cursor')).toEqual({ agent: null, reason: 'ambiguous' })})++ it.each(ENGINES.filter((engine) => engine !== 'cursor'))(+ 'rejects a late %s hook when the pane now belongs to a replacement process',+ (engine) => {+ const replacement = { agentId: 'replacement-agent', sessionId: 'new-session' }+ expect(chooseHookAgent([], [replacement], engine)).toEqual({ agent: null, reason: 'none' })+ expect(chooseHookAgent([replacement], [replacement], engine))+ .toEqual({ agent: replacement, reason: 'ancestry' })+ },+ )})describe('/api/status', () => {diff --git a/cli/src/hookServer.ts b/cli/src/hookServer.tsindex 7e8979d40..8ec6ce4d8 100644--- a/cli/src/hookServer.ts+++ b/cli/src/hookServer.ts@@ -31,17 +31,19 @@ import { handleCommandBarHttp } from './lib/commandBarHttp.js'* on tmux and on Herdr alike, so requiring ancestry rejected every hook that engine ever sent and no* session bound at all.*- * The weaker grade is the runtime itself: the hook named a pane, and that pane carries exactly one agent- * of this engine. Accepted only when it is unambiguous, and only after the caller has already proven it- * can read the 0600 hook credential. Two candidates is not a tie to break — it is a question we cannot- * answer, so we answer nothing.+ * Only Cursor may use the weaker runtime evidence: the hook named a pane carrying exactly one Cursor+ * agent, and the caller already proved it can read the 0600 hook credential. Other engines must match+ * ancestry. A delayed hook from an exited Codex process can name a pane now running a NEW Codex+ * session; trusting that pane alone binds the old transcript to the replacement agent.+ * Two candidates is not a tie to break, so we answer nothing.*/-export function chooseHookAgent<T>(byAncestry: readonly T[], byRuntimeOnly: readonly T[]): {+export function chooseHookAgent<T>(byAncestry: readonly T[], byRuntimeOnly: readonly T[], engine: AgentEngine): {agent: T | nullreason: 'ancestry' | 'runtime' | 'ambiguous' | 'none'} {if (byAncestry.length === 1) return { agent: byAncestry[0], reason: 'ancestry' }if (byAncestry.length > 1) return { agent: null, reason: 'ambiguous' }+ if (engine !== 'cursor') return { agent: null, reason: 'none' }if (byRuntimeOnly.length === 1) return { agent: byRuntimeOnly[0], reason: 'runtime' }return { agent: null, reason: byRuntimeOnly.length ? 'ambiguous' : 'none' }}