Repository navigation
fix(server): Claude's push notifications reach the desktop and web apps #38108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MarconLP
wants to merge
8
commits into
pingdotgg:main
Choose a base branch
from
MarconLP:fix/claude-push-notification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+596
−56
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
df183b3
fix(server): Claude's push notifications reach the desktop and web apps
MarconLP 7538a4c
fix(web): agent alerts stop when notifications are off or the thread …
MarconLP 9ffe79a
fix(server): tell Claude its alert was passed to a client, not shown
MarconLP e92eb89
fix(server): say an alert's presentation is not confirmed
MarconLP 3eba2f3
fix(server): count an alert subscriber and register its cleanup in on…
MarconLP a432d86
fix: merge main into claude push notification branch
maria-rcks edf8eb6
fix(web): silence agent alerts for the focused thread
maria-rcks e83b4f1
fix(web): retain agent alerts during shell synchronization
maria-rcks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import { type AgentAlert, ThreadId } from "@t3tools/contracts"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Fiber from "effect/Fiber"; | ||
| import * as Queue from "effect/Queue"; | ||
| import * as Stream from "effect/Stream"; | ||
|
|
||
| import * as AgentAlerts from "./AgentAlerts.ts"; | ||
|
|
||
| const alert = { | ||
| id: "tool-1", | ||
| threadId: ThreadId.make("thread-1"), | ||
| body: "fleet.lock stuck", | ||
| } satisfies AgentAlert; | ||
|
|
||
| describe("AgentAlerts", () => { | ||
| it.effect("reports no delivery when no client is subscribed", () => | ||
| Effect.gen(function* () { | ||
| const alerts = yield* AgentAlerts.AgentAlerts; | ||
| expect(yield* alerts.offer(alert)).toBe(false); | ||
| }).pipe(Effect.provide(AgentAlerts.layer)), | ||
| ); | ||
|
|
||
| it.effect("delivers to every subscribed client until it disconnects", () => | ||
| Effect.gen(function* () { | ||
| const alerts = yield* AgentAlerts.AgentAlerts; | ||
| const first = yield* Queue.unbounded<AgentAlert>(); | ||
| const second = yield* Queue.unbounded<AgentAlert>(); | ||
| const firstClient = yield* alerts.stream.pipe( | ||
| Stream.runForEach((item) => Queue.offer(first, item)), | ||
| Effect.forkScoped({ startImmediately: true }), | ||
| ); | ||
| const secondClient = yield* alerts.stream.pipe( | ||
| Stream.runForEach((item) => Queue.offer(second, item)), | ||
| Effect.forkScoped({ startImmediately: true }), | ||
| ); | ||
|
|
||
| expect(yield* alerts.offer(alert)).toBe(true); | ||
| expect(yield* Queue.take(first)).toEqual(alert); | ||
| expect(yield* Queue.take(second)).toEqual(alert); | ||
|
|
||
| yield* Fiber.interrupt(firstClient); | ||
| expect(yield* alerts.offer(alert)).toBe(true); | ||
| expect(yield* Queue.take(second)).toEqual(alert); | ||
|
|
||
| yield* Fiber.interrupt(secondClient); | ||
| expect(yield* alerts.offer(alert)).toBe(false); | ||
| }).pipe(Effect.provide(AgentAlerts.layer), Effect.scoped), | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { AgentAlert } from "@t3tools/contracts"; | ||
| import * as Context from "effect/Context"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as PubSub from "effect/PubSub"; | ||
| import * as Ref from "effect/Ref"; | ||
| import * as Stream from "effect/Stream"; | ||
|
|
||
| export class AgentAlerts extends Context.Service< | ||
| AgentAlerts, | ||
| { | ||
| /** | ||
| * Hands the alert to every subscribed client. False when none is subscribed. True means a | ||
| * client took it, not that it put it on screen. | ||
| */ | ||
| readonly offer: (alert: AgentAlert) => Effect.Effect<boolean>; | ||
| readonly stream: Stream.Stream<AgentAlert>; | ||
| } | ||
| >()("t3/notifications/AgentAlerts") {} | ||
|
|
||
| const make = Effect.gen(function* () { | ||
| const subscribers = yield* Ref.make(0); | ||
| const alerts = yield* PubSub.unbounded<AgentAlert>(); | ||
|
|
||
| const stream = Stream.unwrap( | ||
| Effect.gen(function* () { | ||
| // Subscribe before counting: counted first, an offer in between reports a | ||
| // delivery that the subscription never receives. | ||
| const subscription = yield* PubSub.subscribe(alerts); | ||
| // One step, so an interrupt cannot leave the count raised with no cleanup registered. | ||
| yield* Effect.acquireRelease( | ||
| Ref.update(subscribers, (count) => count + 1), | ||
| () => Ref.update(subscribers, (count) => count - 1), | ||
| ); | ||
| return Stream.fromSubscription(subscription); | ||
| }), | ||
| ); | ||
|
|
||
| const offer: AgentAlerts["Service"]["offer"] = Effect.fn("AgentAlerts.offer")(function* (alert) { | ||
| if ((yield* Ref.get(subscribers)) === 0) return false; | ||
| yield* PubSub.publish(alerts, alert); | ||
| return true; | ||
| }); | ||
|
|
||
| return AgentAlerts.of({ offer, stream }); | ||
| }); | ||
|
|
||
| export const layer = Layer.effect(AgentAlerts, make); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
apps/server/src/orchestration-v2/Adapters/claudePushNotification.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { describe, it } from "@effect/vitest"; | ||
| import * as NodeAssert from "node:assert/strict"; | ||
|
|
||
| import { makeClaudePushNotificationHook } from "./claudePushNotification.ts"; | ||
|
|
||
| const USER_PRESENT = { | ||
| message: "Not sent — this terminal is active.", | ||
| pushSent: false, | ||
| localSent: false, | ||
| disabledReason: "user_present", | ||
| sentAt: "2026-11-17T12:00:00.000Z", | ||
| }; | ||
|
|
||
| function run( | ||
| hook: ReturnType<typeof makeClaudePushNotificationHook>, | ||
| input: { readonly message?: unknown; readonly response?: unknown; readonly tool?: string }, | ||
| ) { | ||
| return hook( | ||
| { | ||
| hook_event_name: "PostToolUse", | ||
| tool_name: input.tool ?? "PushNotification", | ||
| tool_input: { message: input.message ?? "fleet.lock stuck", status: "proactive" }, | ||
| tool_response: input.response ?? USER_PRESENT, | ||
| tool_use_id: "tool-1", | ||
| session_id: "session-1", | ||
| transcript_path: "", | ||
| cwd: "/workspace", | ||
| }, | ||
| "tool-1", | ||
| { signal: new AbortController().signal }, | ||
| ); | ||
| } | ||
|
|
||
| describe("claudePushNotification", () => { | ||
| it("delivers a push Claude refused as user_present and reports a local send", async () => { | ||
| const delivered: Array<{ readonly id: string; readonly body: string }> = []; | ||
| const hook = makeClaudePushNotificationHook(async (alert) => { | ||
| delivered.push(alert); | ||
| return true; | ||
| }); | ||
|
|
||
| NodeAssert.deepEqual(await run(hook, {}), { | ||
| hookSpecificOutput: { | ||
| hookEventName: "PostToolUse", | ||
| updatedToolOutput: { | ||
| message: "Sent through T3 Code.", | ||
| pushSent: false, | ||
| localSent: true, | ||
| disabledReason: "no_transport", | ||
| sentAt: "2026-11-17T12:00:00.000Z", | ||
| }, | ||
| additionalContext: | ||
| "T3 Code passed this notification to a connected T3 Code client with notifications on. Whether that client put it on screen is not confirmed.", | ||
| }, | ||
| }); | ||
| NodeAssert.deepEqual(delivered, [{ id: "tool-1", body: "fleet.lock stuck" }]); | ||
| }); | ||
|
|
||
| it("does not tell the model the user is present when no client took the alert", async () => { | ||
| const hook = makeClaudePushNotificationHook(async () => false); | ||
|
|
||
| NodeAssert.deepEqual(await run(hook, {}), { | ||
| hookSpecificOutput: { | ||
| hookEventName: "PostToolUse", | ||
| updatedToolOutput: { | ||
| message: "Not sent. No T3 Code client with notifications on is connected.", | ||
| pushSent: false, | ||
| localSent: false, | ||
| disabledReason: "no_transport", | ||
| sentAt: "2026-11-17T12:00:00.000Z", | ||
| }, | ||
| additionalContext: | ||
| "This notification reached nobody: no T3 Code client with notifications on is connected.", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("sends one line of at most 200 characters", async () => { | ||
| const bodies: Array<string> = []; | ||
| const hook = makeClaudePushNotificationHook(async (alert) => { | ||
| bodies.push(alert.body); | ||
| return true; | ||
| }); | ||
|
|
||
| await run(hook, { message: " line\n\nbreak " }); | ||
| await run(hook, { message: "x".repeat(240) }); | ||
|
|
||
| NodeAssert.equal(bodies[0], "line break"); | ||
| NodeAssert.equal(bodies[1]?.length, 200); | ||
| NodeAssert.equal(bodies[1]?.endsWith("…"), true); | ||
| }); | ||
|
|
||
| it("leaves every other result as Claude wrote it", async () => { | ||
| const delivered: Array<string> = []; | ||
| const hook = makeClaudePushNotificationHook(async (alert) => { | ||
| delivered.push(alert.body); | ||
| return true; | ||
| }); | ||
|
|
||
| for (const response of [ | ||
| { message: "Sent", pushSent: true, localSent: true }, | ||
| { message: "Notifications are off.", pushSent: false, disabledReason: "config_off" }, | ||
| { message: "No push transport.", pushSent: false, disabledReason: "no_transport" }, | ||
| ]) { | ||
| NodeAssert.deepEqual(await run(hook, { response }), {}); | ||
| } | ||
| NodeAssert.deepEqual(await run(hook, { tool: "Bash" }), {}); | ||
| NodeAssert.deepEqual(await run(hook, { message: " " }), {}); | ||
| NodeAssert.deepEqual(await run(hook, { message: 42 }), {}); | ||
| NodeAssert.deepEqual(delivered, []); | ||
| }); | ||
| }); |
75 changes: 75 additions & 0 deletions
75
apps/server/src/orchestration-v2/Adapters/claudePushNotification.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import type { HookCallback, SyncHookJSONOutput } from "@anthropic-ai/claude-agent-sdk"; | ||
|
|
||
| export const CLAUDE_PUSH_NOTIFICATION_TOOL = "PushNotification"; | ||
| // The tool asks the model for under 200 characters; phones cut longer text. | ||
| const MAX_MESSAGE_LENGTH = 200; | ||
|
|
||
| function readMessage(toolInput: unknown): string | null { | ||
| if (typeof toolInput !== "object" || toolInput === null || !("message" in toolInput)) { | ||
| return null; | ||
| } | ||
| if (typeof toolInput.message !== "string") return null; | ||
| const oneLine = toolInput.message.trim().replace(/\s+/g, " "); | ||
| if (oneLine.length === 0) return null; | ||
| return oneLine.length > MAX_MESSAGE_LENGTH | ||
| ? `${oneLine.slice(0, MAX_MESSAGE_LENGTH - 1)}…` | ||
| : oneLine; | ||
| } | ||
|
|
||
| type ToolResponse = Readonly<Record<string, unknown>>; | ||
|
|
||
| function refusedAsUserPresent(toolResponse: unknown): toolResponse is ToolResponse { | ||
| return ( | ||
| typeof toolResponse === "object" && | ||
| toolResponse !== null && | ||
| "disabledReason" in toolResponse && | ||
| toolResponse.disabledReason === "user_present" | ||
| ); | ||
| } | ||
|
|
||
| function toolOutput(toolResponse: ToolResponse, delivered: boolean): SyncHookJSONOutput { | ||
| return { | ||
| hookSpecificOutput: { | ||
| hookEventName: "PostToolUse", | ||
| // Claude Code writes the model's result text from these fields and ignores `message`. | ||
| // `no_transport` is its closest wording: a local notification was or was not sent, and | ||
| // no mobile push was. `additionalContext` tells the model what T3 did: it knows a client | ||
| // took the alert, not that the client put it on screen. | ||
| updatedToolOutput: { | ||
| ...toolResponse, | ||
| message: delivered | ||
| ? "Sent through T3 Code." | ||
| : "Not sent. No T3 Code client with notifications on is connected.", | ||
| pushSent: false, | ||
| localSent: delivered, | ||
| disabledReason: "no_transport", | ||
| }, | ||
| additionalContext: delivered | ||
| ? "T3 Code passed this notification to a connected T3 Code client with notifications on. Whether that client put it on screen is not confirmed." | ||
| : "This notification reached nobody: no T3 Code client with notifications on is connected.", | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Claude Code decides delivery inside the CLI and counts an Agent SDK session as an open | ||
| * terminal, so from a T3 thread the tool always answers `user_present` and sends nothing. | ||
| * This takes over that one refusal: T3 delivers the message and the model hears what happened. | ||
| */ | ||
| export function makeClaudePushNotificationHook( | ||
| deliver: (alert: { readonly id: string; readonly body: string }) => Promise<boolean>, | ||
| ): HookCallback { | ||
| return async (input) => { | ||
| if ( | ||
| input.hook_event_name !== "PostToolUse" || | ||
| input.tool_name !== CLAUDE_PUSH_NOTIFICATION_TOOL || | ||
| // A real send, or a refusal for another reason, reaches the model unchanged. | ||
| !refusedAsUserPresent(input.tool_response) | ||
| ) { | ||
| return {}; | ||
| } | ||
| const body = readMessage(input.tool_input); | ||
| if (body === null) return {}; | ||
| return toolOutput(input.tool_response, await deliver({ id: input.tool_use_id, body })); | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.