From 10725a320339a85492c2755f499ea77c4b175f7e Mon Sep 17 00:00:00 2001 From: Joseph Vidal Date: Mon, 16 Nov 2026 21:09:33 -0500 Subject: [PATCH 1/6] fix(server): a Claude background sleep timer wakes the agent after a restart --- .../Adapters/ClaudeAdapterV2.test.ts | 48 +++++++++++++++++++ .../Adapters/ClaudeAdapterV2.ts | 21 ++++++-- docs/user/thread-sidebar.md | 3 +- docs/user/updating.md | 2 +- 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 6981e80464f41..ac6f6d5453de9 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -12242,6 +12242,44 @@ describe("ClaudeAdapterV2 background wake turns", () => { }), ); yield* Queue.offer(harness.sdkMessages, wakeTaskStarted); + // A sleep is a timer the agent ends its turn on, so a restart must wake it. + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "assistant", + message: { + model: "claude-sonnet-4-6", + id: "msg_background_timer", + type: "message", + role: "assistant", + content: [ + { + type: "tool_use", + id: "toolu_timer", + name: "Bash", + input: { command: "sleep 1740; date", run_in_background: true }, + }, + ], + }, + parent_tool_use_id: null, + uuid: "background-timer-launch", + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "system", + subtype: "task_started", + task_id: "timer", + tool_use_id: "toolu_timer", + description: "Wait 29 minutes before the next check", + is_backgrounded: true, + task_type: "local_bash", + uuid: "00000000-0000-4000-8000-000000000205", + session_id: WAKE_NATIVE_SESSION, + }), + ); yield* Queue.offer(harness.sdkMessages, turnOneResult); yield* harness.awaitTerminal(1); @@ -12252,6 +12290,16 @@ describe("ClaudeAdapterV2 background wake turns", () => { const started = (afterStart.at(-1)?.providerThread.pendingBackgroundTasks ?? [])[0]; assert.equal(started?.taskId, WAKE_TASK_ID); assert.equal(started?.nativeItemId, WAKE_TOOL_USE_ID); + assert.deepEqual( + (afterStart.at(-1)?.providerThread.pendingBackgroundTasks ?? []).map((task) => [ + task.taskId, + task.awaited === true, + ]), + [ + [WAKE_TASK_ID, false], + ["timer", true], + ], + ); const command = harness.events.find( (event) => diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 1ae83853ed26a..c4cead345add8 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -2142,6 +2142,12 @@ function parseClaudeInteger(text: string): number { return Number.parseInt(value, 10); } +// A background command that starts by sleeping is a timer the agent set to check back, which +// Claude wakes it for when it ends. A dev server never starts that way. +function isClaudeSleepTimer(command: string | undefined): boolean { + return command !== undefined && /^\s*sleep\s+\d/.test(command); +} + // Claude Code defers MCP tools behind ToolSearch, so the agent only finds // t3_background_wait when the system prompt says when to reach for it. const CLAUDE_BACKGROUND_WAIT_INSTRUCTIONS = ` @@ -6781,17 +6787,22 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( nativeThreadId: string, task: OrchestrationV2PendingBackgroundTask, ) { - const startedMonitor = yield* Ref.modify( + const startedWait = yield* Ref.modify( pendingBackgroundTasksByNativeThread, (current) => { const roster = new Map(rosterForNativeThread(current, nativeThreadId)); - const startedMonitor = - task.kind === "monitor" && roster.get(task.taskId)?.kind !== "monitor"; + const previous = roster.get(task.taskId); + const startedWait = + task.kind === "monitor" + ? previous?.kind !== "monitor" + : task.kind === "command" && + previous?.command === undefined && + isClaudeSleepTimer(task.command); roster.set(task.taskId, task); - return [startedMonitor, new Map(current).set(nativeThreadId, roster)] as const; + return [startedWait, new Map(current).set(nativeThreadId, roster)] as const; }, ); - if (startedMonitor) { + if (startedWait) { yield* addTaskIdsToNativeThreadSet( awaitedBackgroundTasksByNativeThread, nativeThreadId, diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md index 905f3a5bfbe9c..aa7ddad0b82b1 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -135,7 +135,8 @@ open. Threads that are working or waiting on work they will resume from move into a collapsed **Working** section below the active list. -That includes subagents, watched pull requests with checks still running, Claude monitors, +That includes subagents, watched pull requests with checks still running, Claude monitors and +`sleep` timers, and background commands or other monitors the agent says it is waiting on. A dev server or other process an agent leaves running does not keep a thread there. A thread returns to the top of the active list when it finishes, fails, or needs an approval or answer. The Working section lists the thread you last sent work to first. Pinned threads stay in diff --git a/docs/user/updating.md b/docs/user/updating.md index 388e9035ca770..cbb25110857b8 100644 --- a/docs/user/updating.md +++ b/docs/user/updating.md @@ -73,7 +73,7 @@ T3 Code must start again on that machine; the setting does not enable automatic startup. Terminal commands may still be interrupted, and threads without saved provider resume state need a new message. A thread that finished its turn to wait on a subagent, a background task, a Claude -monitor, or a command or other monitor the agent said it was waiting on is woken and told that this work +monitor or `sleep` timer, or a command or other monitor the agent said it was waiting on is woken and told that this work stopped. Other background shells and monitors, such as a dev server and its watchdog, do not wake it; the thread learns about them with your next message. A thread is not resumed while it waits for your approval, answer, or secret, or From b211f804944f3bd867c87668718cb12378202249 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 02:17:34 +0000 Subject: [PATCH 2/6] docs: keep the Working section sentence on one flow Co-Authored-By: Claude Opus 5.5 --- docs/user/thread-sidebar.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md index aa7ddad0b82b1..759e9124677d0 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -136,8 +136,7 @@ open. Threads that are working or waiting on work they will resume from move into a collapsed **Working** section below the active list. That includes subagents, watched pull requests with checks still running, Claude monitors and -`sleep` timers, -and background commands or other monitors the agent says it is waiting on. A dev server or other process an agent leaves running does not keep a thread +`sleep` timers, and background commands or other monitors the agent says it is waiting on. A dev server or other process an agent leaves running does not keep a thread there. A thread returns to the top of the active list when it finishes, fails, or needs an approval or answer. The Working section lists the thread you last sent work to first. Pinned threads stay in the pinned section. From 88595f700cdd62a51bd40caf63026cf19f30f70a Mon Sep 17 00:00:00 2001 From: Joseph Vidal Date: Tue, 17 Nov 2026 11:13:32 -0500 Subject: [PATCH 3/6] fix(server): a sleep timer with a computed duration is awaited too A background Bash command such as `sleep $((29 * 60)); date` is the same check-back timer as `sleep 1740; date`, so it now gets the restart wake-up as well. --- .../Adapters/ClaudeAdapterV2.test.ts | 40 ++++++++++++------- .../Adapters/ClaudeAdapterV2.ts | 2 +- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index ac6f6d5453de9..507468b1b2d51 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -12259,6 +12259,12 @@ describe("ClaudeAdapterV2 background wake turns", () => { name: "Bash", input: { command: "sleep 1740; date", run_in_background: true }, }, + { + type: "tool_use", + id: "toolu_computed_timer", + name: "Bash", + input: { command: "sleep $((29 * 60)); date", run_in_background: true }, + }, ], }, parent_tool_use_id: null, @@ -12266,20 +12272,25 @@ describe("ClaudeAdapterV2 background wake turns", () => { session_id: WAKE_NATIVE_SESSION, }), ); - yield* Queue.offer( - harness.sdkMessages, - claudeSdkFrame({ - type: "system", - subtype: "task_started", - task_id: "timer", - tool_use_id: "toolu_timer", - description: "Wait 29 minutes before the next check", - is_backgrounded: true, - task_type: "local_bash", - uuid: "00000000-0000-4000-8000-000000000205", - session_id: WAKE_NATIVE_SESSION, - }), - ); + for (const [taskId, uuid] of [ + ["timer", "00000000-0000-4000-8000-000000000205"], + ["computed_timer", "00000000-0000-4000-8000-000000000206"], + ]) { + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "system", + subtype: "task_started", + task_id: taskId, + tool_use_id: `toolu_${taskId}`, + description: "Wait 29 minutes before the next check", + is_backgrounded: true, + task_type: "local_bash", + uuid, + session_id: WAKE_NATIVE_SESSION, + }), + ); + } yield* Queue.offer(harness.sdkMessages, turnOneResult); yield* harness.awaitTerminal(1); @@ -12298,6 +12309,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { [ [WAKE_TASK_ID, false], ["timer", true], + ["computed_timer", true], ], ); diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index c4cead345add8..a8baf4001b15f 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -2145,7 +2145,7 @@ function parseClaudeInteger(text: string): number { // A background command that starts by sleeping is a timer the agent set to check back, which // Claude wakes it for when it ends. A dev server never starts that way. function isClaudeSleepTimer(command: string | undefined): boolean { - return command !== undefined && /^\s*sleep\s+\d/.test(command); + return command !== undefined && /^\s*sleep\s+(?:\d|\$\(\()/.test(command); } // Claude Code defers MCP tools behind ToolSearch, so the agent only finds From 935a33d8c457ab8e2ee64c3c530e21d1e9bfcaa1 Mon Sep 17 00:00:00 2001 From: Joseph Vidal Date: Tue, 17 Nov 2026 16:08:41 -0500 Subject: [PATCH 4/6] fix(server): a sleep timer with a quoted duration is awaited too `sleep "60"`, `sleep '60'` and `sleep "$((29 * 60))"` are the same check-back timers as their unquoted forms. `sleep infinity` still isn't awaited. --- .../Adapters/ClaudeAdapterV2.test.ts | 44 ++++++++----------- .../Adapters/ClaudeAdapterV2.ts | 2 +- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 507468b1b2d51..0dd402c7b5445 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -12243,6 +12243,15 @@ describe("ClaudeAdapterV2 background wake turns", () => { ); yield* Queue.offer(harness.sdkMessages, wakeTaskStarted); // A sleep is a timer the agent ends its turn on, so a restart must wake it. + const sleeps = [ + ["timer", "sleep 1740; date", true], + ["computed_timer", "sleep $((29 * 60)); date", true], + ["quoted_timer", 'sleep "60" && date', true], + ["single_quoted_timer", "sleep '60'; date", true], + ["quoted_computed_timer", 'sleep "$((29 * 60))"; date', true], + ["unit_timer", "sleep 5m; date", true], + ["endless_sleep", "sleep infinity", false], + ] as const; yield* Queue.offer( harness.sdkMessages, claudeSdkFrame({ @@ -12252,30 +12261,19 @@ describe("ClaudeAdapterV2 background wake turns", () => { id: "msg_background_timer", type: "message", role: "assistant", - content: [ - { - type: "tool_use", - id: "toolu_timer", - name: "Bash", - input: { command: "sleep 1740; date", run_in_background: true }, - }, - { - type: "tool_use", - id: "toolu_computed_timer", - name: "Bash", - input: { command: "sleep $((29 * 60)); date", run_in_background: true }, - }, - ], + content: sleeps.map(([taskId, command]) => ({ + type: "tool_use", + id: `toolu_${taskId}`, + name: "Bash", + input: { command, run_in_background: true }, + })), }, parent_tool_use_id: null, uuid: "background-timer-launch", session_id: WAKE_NATIVE_SESSION, }), ); - for (const [taskId, uuid] of [ - ["timer", "00000000-0000-4000-8000-000000000205"], - ["computed_timer", "00000000-0000-4000-8000-000000000206"], - ]) { + for (const [index, [taskId]] of sleeps.entries()) { yield* Queue.offer( harness.sdkMessages, claudeSdkFrame({ @@ -12283,10 +12281,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { subtype: "task_started", task_id: taskId, tool_use_id: `toolu_${taskId}`, - description: "Wait 29 minutes before the next check", + description: "Wait before the next check", is_backgrounded: true, task_type: "local_bash", - uuid, + uuid: `00000000-0000-4000-8000-00000000021${index}`, session_id: WAKE_NATIVE_SESSION, }), ); @@ -12306,11 +12304,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { task.taskId, task.awaited === true, ]), - [ - [WAKE_TASK_ID, false], - ["timer", true], - ["computed_timer", true], - ], + [[WAKE_TASK_ID, false], ...sleeps.map(([taskId, , awaited]) => [taskId, awaited])], ); const command = harness.events.find( diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index a8baf4001b15f..fe972fa01d29d 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -2145,7 +2145,7 @@ function parseClaudeInteger(text: string): number { // A background command that starts by sleeping is a timer the agent set to check back, which // Claude wakes it for when it ends. A dev server never starts that way. function isClaudeSleepTimer(command: string | undefined): boolean { - return command !== undefined && /^\s*sleep\s+(?:\d|\$\(\()/.test(command); + return command !== undefined && /^\s*sleep\s+["']?(?:\d|\$\(\()/.test(command); } // Claude Code defers MCP tools behind ToolSearch, so the agent only finds From b5febb6a5709d5f02fe9e9ac18cda6571ab90463 Mon Sep 17 00:00:00 2001 From: Joseph Vidal Date: Tue, 17 Nov 2026 22:06:43 -0500 Subject: [PATCH 5/6] fix(server): only a sleep that ends in a short report counts as a timer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A background command is a timer only when it is `sleep` with one finite duration (a number, `$((…))`, `$NAME` or `${NAME:-60}`, optionally quoted) followed by nothing or only `date`, `echo`, `printf`, `true` or `:` steps. `sleep 5 && npm run dev` is no longer awaited, and `sleep "${DELAY:-60}"; date` now is. --- .../Adapters/ClaudeAdapterV2.test.ts | 4 +++- .../Adapters/ClaudeAdapterV2.ts | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 0dd402c7b5445..bdf4b2a04dbe4 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -12249,8 +12249,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { ["quoted_timer", 'sleep "60" && date', true], ["single_quoted_timer", "sleep '60'; date", true], ["quoted_computed_timer", 'sleep "$((29 * 60))"; date', true], - ["unit_timer", "sleep 5m; date", true], + ["expanded_timer", 'sleep "${DELAY:-60}"; date', true], + ["unit_timer", 'sleep 5m && echo "Time to check the build"', true], ["endless_sleep", "sleep infinity", false], + ["delayed_server", "sleep 5 && npm run dev", false], ] as const; yield* Queue.offer( harness.sdkMessages, diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index fe972fa01d29d..5b76ac0cb66a2 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -2142,10 +2142,22 @@ function parseClaudeInteger(text: string): number { return Number.parseInt(value, 10); } -// A background command that starts by sleeping is a timer the agent set to check back, which -// Claude wakes it for when it ends. A dev server never starts that way. +const CLAUDE_SLEEP_DURATION = String.raw`(?:\d[\w.]*|\$\(\([\w\s+*/%-]+\)\)|\$\{\w+(?::?[-=]\d[\w.]*)?\}|\$\w+)`; +const CLAUDE_SLEEP_TIMER_START = new RegExp( + String.raw`^\s*sleep\s+(?:${CLAUDE_SLEEP_DURATION}|"${CLAUDE_SLEEP_DURATION}"|'\d[\w.]*')\s*(?:$|;|&&|\n)`, +); +const CLAUDE_SLEEP_TIMER_REPORT = /^(?:(?:date|echo|printf|true|:)(?:\s[^|&`()]*)?)?$/; + +// A background command that only sleeps for a finite time, then at most prints the time or a +// note, is a timer the agent set to check back, which Claude wakes it for when it ends. Any other +// step (`sleep 5 && npm run dev`) may never end, so that command is not a timer. function isClaudeSleepTimer(command: string | undefined): boolean { - return command !== undefined && /^\s*sleep\s+["']?(?:\d|\$\(\()/.test(command); + const start = command === undefined ? null : CLAUDE_SLEEP_TIMER_START.exec(command); + if (command === undefined || start === null) return false; + return command + .slice(start[0].length) + .split(/;|&&|\n/) + .every((step) => CLAUDE_SLEEP_TIMER_REPORT.test(step.trim())); } // Claude Code defers MCP tools behind ToolSearch, so the agent only finds From 0f72e58f58f08c48bd699c00c9e5fa80635675f9 Mon Sep 17 00:00:00 2001 From: Joseph Vidal Date: Wed, 18 Nov 2026 10:11:30 -0500 Subject: [PATCH 6/6] fix(server): a timer's note may contain quoted separators MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The steps after a timer's sleep are now split only at unquoted `;`, `&&` and newlines, so `sleep 60 && echo "Timer finished; check build"` is awaited. Pipes, `||`, a lone `&`, subshells, heredocs, substitutions, `${…}`, `$'…'`, comments and unbalanced quoting still make a command not a timer. --- .../Adapters/ClaudeAdapterV2.test.ts | 2 + .../Adapters/ClaudeAdapterV2.ts | 57 ++++++++++++++++--- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index e43f5b30a55ff..8e56ebe77bb51 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -12514,8 +12514,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { ["quoted_computed_timer", 'sleep "$((29 * 60))"; date', true], ["expanded_timer", 'sleep "${DELAY:-60}"; date', true], ["unit_timer", 'sleep 5m && echo "Time to check the build"', true], + ["quoted_note_timer", 'sleep 60 && echo "Timer finished; check build"', true], ["endless_sleep", "sleep infinity", false], ["delayed_server", "sleep 5 && npm run dev", false], + ["substituted_server", 'sleep 60; echo "$(npm run dev)"', false], ] as const; yield* Queue.offer( harness.sdkMessages, diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 08c11d9e9d265..c5fe96498835d 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -2139,11 +2139,12 @@ function parseClaudeInteger(text: string): number { return Number.parseInt(value, 10); } -const CLAUDE_SLEEP_DURATION = String.raw`(?:\d[\w.]*|\$\(\([\w\s+*/%-]+\)\)|\$\{\w+(?::?[-=]\d[\w.]*)?\}|\$\w+)`; +const CLAUDE_SLEEP_DURATION = String.raw`(?:\d[\w.]*|\$\(\([\d\s+*/%-]+\)\)|\$\{\w+(?::?[-=]\d[\w.]*)?\}|\$\w+)`; const CLAUDE_SLEEP_TIMER_START = new RegExp( - String.raw`^\s*sleep\s+(?:${CLAUDE_SLEEP_DURATION}|"${CLAUDE_SLEEP_DURATION}"|'\d[\w.]*')\s*(?:$|;|&&|\n)`, + String.raw`^[ \t\n]*sleep[ \t]+(?:${CLAUDE_SLEEP_DURATION}|"${CLAUDE_SLEEP_DURATION}"|'\d[\w.]*')[ \t]*(?:$|;|&&|\n)`, ); -const CLAUDE_SLEEP_TIMER_REPORT = /^(?:(?:date|echo|printf|true|:)(?:\s[^|&`()]*)?)?$/; +// Bash separates words only with spaces and tabs, so `\s` would read `true\rnpm` as `true`. +const CLAUDE_SLEEP_TIMER_REPORT = /^[ \t]*(?:$|(?:date|echo|printf|true|:)(?:[ \t]|$))/; // A background command that only sleeps for a finite time, then at most prints the time or a // note, is a timer the agent set to check back, which Claude wakes it for when it ends. Any other @@ -2151,10 +2152,52 @@ const CLAUDE_SLEEP_TIMER_REPORT = /^(?:(?:date|echo|printf|true|:)(?:\s[^|&`()]* function isClaudeSleepTimer(command: string | undefined): boolean { const start = command === undefined ? null : CLAUDE_SLEEP_TIMER_START.exec(command); if (command === undefined || start === null) return false; - return command - .slice(start[0].length) - .split(/;|&&|\n/) - .every((step) => CLAUDE_SLEEP_TIMER_REPORT.test(step.trim())); + const steps = splitClaudeSleepTimerSteps(command.slice(start[0].length)); + return steps !== undefined && steps.every((step) => CLAUDE_SLEEP_TIMER_REPORT.test(step)); +} + +// Splits at unquoted `;`, `&&` and newlines. Gives up on anything a report never needs that could +// run or hide another command: pipes, `||`, a lone `&`, subshells, heredocs, substitutions, `${…}`, +// `$'…'`, comments, and unbalanced quotes or escapes. +function splitClaudeSleepTimerSteps(tail: string): string[] | undefined { + const steps: string[] = []; + let step = ""; + let quote: string | undefined; + for (let index = 0; index < tail.length; index++) { + const char = tail[index] ?? ""; + const next = tail[index + 1] ?? ""; + if (quote === "'") { + if (char === "'") quote = undefined; + step += char; + continue; + } + if (char === "\\") { + if (next === "") return undefined; + step += char + next; + index++; + continue; + } + if (char === "`" || (char === "$" && "({'\"".includes(next) && next !== "")) return undefined; + if (quote === '"') { + if (char === '"') quote = undefined; + step += char; + continue; + } + if (char === "'" || char === '"') { + quote = char; + } else if (char === ";" || char === "\n" || (char === "&" && next === "&")) { + steps.push(step); + step = ""; + if (char === "&") index++; + continue; + } else if (char === "&" ? next !== ">" && tail[index - 1] !== ">" : "|()<#".includes(char)) { + return undefined; + } + step += char; + } + if (quote !== undefined) return undefined; + steps.push(step); + return steps; } // Claude Code defers MCP tools behind ToolSearch, so the agent only finds