Repository navigation
feat(chatd): push chat events to the iOS app over APNs - #416
Conversation
webpush_subscription defines no update action, so moving a device to the other APNs environment checks create, like the upsert. The APNs test route answers 204 with no body, so it declares no @produce.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
A bad key path or key file after a config edit must not stop the daily server. The dispatcher logs the reason and hands chatd upstream's dispatcher, so the APNs routes answer 404 as they do without a key.
One rule for every APNs misconfiguration: log it and leave push off. Validate no longer rejects partial APNs options; the dispatcher logs the missing flags at error level and hands chatd upstream's dispatcher.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 54f7cc6056
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Mike authorized the maintainer on 2026-11-23 to take this PR over and merge it when it passes the gates. The maintainer now owns it, and a workstream will finish it on this branch with no force-push. State at takeover: head Plan:
This cannot be tested end to end here: that needs a real APNs key and an iOS device. The merge comment will say so. |
…0, authorize test pushes, coalesce token refresh
… export shim Move the dispatcher and test push tests into fork_push_dispatcher_internal_test.go so no export_test.go shim is needed, add tests for canonical tokens, the test push authorization and rejections that race a newer registration, and follow the log message and dbauthz lint rules.
@codex review Head 9ed58fe: the five accepted findings are fixed (lower case tokens with a CHECK, conditional delete and environment move on rejection, test push authorization, one re-sign for concurrent refreshes, internal tests instead of the export shim). |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9ed58fe6fc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…trust the 410 timestamp
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d596a8d15d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
… require the APNs topic Start APNs before web push and run the send timeout on a quartz clock.
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Merged as 53f1cd0 (squash), under Mike's order of 2026-11-23 that the lead finishes and merges this PR. Gates on head 453fa09, checked by the lead:
Scope and ops notes:
Not exercised: a real APNs key, a real iOS device, real APNs responses for DeviceTokenNotForTopic and the 410 timestamp (only mocked), and concurrent registrations racing past the 10-device cap (only the cut at dispatch is tested for that case). |
Chat pushes now reach the Coder for iOS app over APNs, in the
fork-apns-pushexperiment. coderd talks to APNs itself, so there is no relay and no Notification Service Extension. With the experiment off or no APNs key, chatd gets upstream's web push dispatcher unchanged.What sends a push
Nothing new. The new dispatcher wraps upstream's web push dispatcher and receives what chatd already sends:
waitingorpaused,error,ask_user_questionpush.Sub-agent chats,
requires_actionand interrupts still send nothing.chatd.gois not edited.Dispatchstarts the APNs sends on a goroutine, then sends the web push and returns its result, so APNs never fails or slows the web push or the turn, and a slow web push never delays APNs. The goroutine's context is the caller's without cancel, plus a 10 s timeout on the dispatcher's clock. Each device is sent on its own goroutine under that timeout, so one stalled device does not use up the time of the others. API close waits for sends in flight.Each device picks the events it wants.
Dispatchcarries no status, so the dispatcher reads the chat's status:erroragent_errorsonwaiting,pausedfinished_turnsorquestionsonrunning, ...)questionsonA turn that ends with a question looks like a finished turn to the server, which is why
questionsalso gets finished turns. One race is accepted: if a new turn starts before a finished-turn push reads the status, that push is treated as a question.Web push off. When web push is off and APNs is set up, the dispatcher's
PublicKey()returns a fixed non-empty value. chatd uses the key only to decide whether to send at all (chatd.gowebpushConfigured). The browser routes and buildinfo read upstream's dispatcher, so they never see this value.APNs
golang-jwt/jwt/v4, kept for 40 minutes, and remade once on403 ExpiredProviderToken. Concurrent sends that hit the same expired token share one re-sign: the client re-signs only if its cached token is still the one that failed. Requests go over HTTP/2 throughnet/http. No new module.apns-push-type: alert, priority 10, expiration 24 h,apns-collapse-id= chat ID.aps.alert(chat title and body),thread-id, andsound: chime.cafwhen the device asks for sound. Alsolink: coder-ios://chat/<id>anddeployment(the access URL). A body that would push the payload past 4096 bytes is cut to the longest prefix of whole runes that fits, plus an ellipsis.410deletes the device.400 BadDeviceTokenretries once in the other environment and stores that environment if it works; otherwise the device is deleted.400 DeviceTokenNotForTopicdeletes the device without a retry.updated_atis not later than a bound. For a410with a timestamp the bound is that timestamp, so only a registration after APNs saw the token go invalid keeps its row. Otherwise the bound is the row the push read, so a device that registered again while a push was in flight keeps its row.429and5xxare logged and dropped.Options
--chat-apns-key-fileCODER_CHAT_APNS_KEY_FILE--chat-apns-key-idCODER_CHAT_APNS_KEY_ID--chat-apns-team-idCODER_CHAT_APNS_TEAM_ID--chat-apns-topicCODER_CHAT_APNS_TOPICAPNs needs the key file, key ID, team ID and topic. The topic has no default: an APNs key can push only to the apps of its own team.
Ops note: with
fork-apns-pushon, setCODER_CHAT_APNS_TOPICto the iOS app's bundle ID. Without it APNs stays off and coderd logs that--chat-apns-topicis missing. The key option is a path, so the key never appears in the deployment config. No APNs misconfiguration stops the server: with the experiment on, a missing option, or a key file that cannot be read or is not a PKCS #8 P-256 key, logs an error at startup that names the missing options or the load error and turns APNs off. chatd then keeps upstream's dispatcher and the APNs routes answer 404, as they do without a key.API
These are fork routes, behind
RequireExperiment. They act on the caller and authorize aswebpush_subscriptionowned by the caller, like upstream's browser subscriptions.POST /api/v2/chats/fork/push/apnsdevice_token(hex),environment(sandboxorproduction),finished_turns,questions,agent_errors,soundDELETE /api/v2/chats/fork/push/apnsdevice_tokenPOST /api/v2/chats/fork/push/apns/testdevice_tokennotification_message:createon themselves, like upstream's web push test, else 403.Device tokens must be hex, up to 200 characters. The routes store and look them up in lower case, so one device never has two rows. A user keeps at most 10 devices: a registration deletes that user's oldest others in the same statement, and a push goes only to the newest 10, since two registrations at once can pass the limit.
All three answer 404 "APNs is not configured." without a key. The app reads a 403 or 404 as "no server push" and keeps its local notifications.
Schema
One chatd node,
chat_apns_devices. The device token is the key, with aCHECKthat it is lower case hex, the table referencesuserswithON DELETE CASCADE, and it is indexed by user. Its queries are inforkdb, authorized in dbauthz and timed in dbmetrics:create, like the upsert, becausewebpush_subscriptiondefines noupdateaction.Hook lines in upstream files
coderd/coderd.gogets three hook lines: the chatd config takesapi.forkChatPushDispatcher(options.WebPushDispatcher),APIgains aforkPushfield, andClosecallsapi.closeForkPushDispatcher()after chatd closes. The design named only the first one. The field and the close hook are there so the test route can reach the APNs client and shutdown can wait for sends in flight.Scope decisions
chatd.go. The race above is accepted.Tests
fork_apns_internal_test.gochecks the client against a fake APNs (httptestwith HTTP/2 and TLS):ExpiredProviderToken,410with and without a timestamp,429and500,fork_push_dispatcher_internal_test.go(packagecoderd, real database behind dbauthz, fake APNs, fake web push):410deletes,BadDeviceTokenmoves the device to production,DeviceTokenNotForTopicdeletes without a retry,429and500keep the device, web push is still sent, and no token is in any log line,410timestamp after a new registration deletes it, in 10 cases,fork_push_devices_test.gouses coderdtest and never sends a push:notification_message:creategets 403 on the test push.TestForkQuerierhas cases for the six queries.TestUpsertChatAPNsDeviceKeepsTheNewestchecks the per-user limit, a re-registration and a token moving between users. The codersdk option and experiment tests are extended.typesGenerated.ts, CLI and config docs, API docs, and the server help golden files.Not exercised: a real APNs key and an iOS device.