Repository navigation
feat(grok): copy Cloud's Grok Bot routine handling (Phase 1, flag off by default) - #236
AgentRelayBot wants to merge 2 commits into
Conversation
…OK_ROUTINES_CLOUD_COPY Phase 1 of docs/specs/grok-bot-dynamic-prompt.md. A copy, not a migration: Cloud is unchanged and keeps serving its bots. Default off; with the flag off the #136 behaviour in docs/grok-bot.md is unchanged. Copied as is from AgentWorkforce/cloud @ 84578009505b: - grok-routine.ts: grokRoutinePrompt, SHOW_UI_GUIDANCE, grokRoutineHeaders, parseGrokRoutineUrl/Key/Details/AgentName, perBotConnectionRequired (byte-identical apart from RoutineConfigError's base class). - sender-check.ts, and the reply policy (eventSteps, repliesAutomatically, CHANNEL_ASK_TO_REPLY, replyCall) in grok-events-policy.ts. - connect_grok_bot_routine (webhook and polling; name/from/mode; next_steps), disconnect_grok_bot_routine (mode/webhook_url), get_event and get_recent_events descriptions and output. What the copied prompt directly needs: - Migration 0079: routine delivery_mode, reply_mode (default notes-only, decision D2), from_filter, polling_since, routine_url_hash (one grant per routine URL); grok_bot_events, a per-grant event log with message text encrypted under the Control keyring, kept seven days. - The worker ingests each inbox delivery into that log inside the grant fence, sends one Cloud-shaped relay.message / relay.channel_message wake per event (summary only; webhook-id = eventId; X-MCP-Subscription-Id = grant), and acknowledges deliveries only after commit. A failed wake keeps its event id and backs off. - get_event / get_recent_events (tool group grok_events, Grok connections only). The sender is trusted only as a roster agent the person owns. - The setup page accepts one paste, a reply mode and a sender filter. - Copy mode replaces the #136 webhook-only handler (decision D1). Cloud-import verifier manifest regenerated on PostgreSQL 15 with the dry-run harness: the diff is only 0079's objects. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
There was a problem hiding this comment.
Reviewed at 4d81b30. Requesting changes: two defects block the merge, and the branch has to be reworked on top of #231, which lands first.
Blocking
-
Flag off is not unchanged once a polling row exists (
src/mcp/grok-routines.ts,tick). The candidate query selectsdelivery_mode='polling'rows whatever the flag says, and the flag-off branch returns atif (!routine.credentials_ciphertext) return;without movingnext_attempt_at. After an operator turns the flag on, someone connects in polling mode and the flag goes off again, that row is picked every tick forever (oneauthenticatedAgentcall each time), and because the scan isORDER BY next_attempt_at LIMIT 20, twenty such rows starve every webhook routine. Either leave polling rows out of the candidate query when the copy is off, or advancenext_attempt_atin that branch. Needs a test. -
Sender trust rests on agent metadata (
src/mcp/grok-events.ts,senderTrust). An agent is "your agent" when its rostermetadata.owner_hashequalssha256(userId). That value is a plain hash of an id other workspace members can see, and it sits in metadata that is not only written by this service (an agent holding its own token can merge metadata into its own card; #231's gate path relies on exactly that call). Withacknowledgeoranswer, a trusted sender gets an automatic reply, so this decision should not depend on a value the sender may be able to set. Decide trust from rows this service owns: the sender's agent id must match one of this person's hosted participant, desktop identity or binding rows. Needs a test with a roster agent that carries the right hash and is not in those tables. Note that after #231 gated agents carry no owner marker at all, so the metadata check would also never trust a person's own agents on a gate-only install. -
Rework on top of #231 (it merges first and takes migration
0079):- renumber the migration to the next free number, regenerate
scripts/cloud-import-verify/schema-manifest.json, and update the file name indocs/grok-bot.md; copyScanand the post-commit acknowledgement must go through the same client #231 selects for the participant (the gate client and refreshed token for a gated participant) and run after its recovery early-returns; on a gate-only install the routine must never talk to Relaycast with the participant token directly. The roster read should follow the same directory choice;- the copied connect tool calls
joinbefore anygrant()check (main's tool callsstatus()first). With #231's grant-level admission kind, a grant that is not a Grok Bot grant would be joined and enrolled before it is refused. Check the grant first; - add a gate-only test for ingest and acknowledgement.
- renumber the migration to the next free number, regenerate
Should fix in this PR
-
Retention (
copyScan,disconnect, the grant-failure delete intick). Events older than seven days are deleted only insidecopyScanfor a grant with a live routine. Disconnecting, losing the grant check intick, or turning the flag off leaves the encrypted events in place, andget_event/get_recent_eventsdo not filter by age. Delete a grant's events when its routine row goes, and sweep bycreated_atindependent of the flag (the docs promise seven days). -
Turning the flag on changes existing routines silently. Existing webhook routines move to one wake per event with deliveries acknowledged by the worker, while their already-pasted prompt still reads the inbox, which is now always empty: the bot wakes and sees nothing until the person pastes the new prompt. Those rows also keep
routine_url_hashNULL, so the one-URL rule does not cover them. Ingest only for rows saved under the copy, or state the migration step and enforce it. -
Duplicate URL race answers 500. The duplicate check is a SELECT then an INSERT; two saves of the same URL at once make the loser fail the unique index with an error that is not a
GrokRoutineError. Map 23505 on that index to the duplicate answer.
Smaller
- With the flag off,
describe_capabilitiesnow lists the new group as unavailable with the generic "not part of this edition" text. Leave the group out when the flag is off, or give it its own reason. - A wake whose stored payload cannot be decrypted stays pending and blocks later wakes for that grant. Deliveries the bot sent itself are filtered but never acknowledged, so they can fill the 100-item page. The
seqcursor is one sequence across all grants, so its gaps show other tenants' volume; a per-grant cursor avoids that. - Public text. Comments and docs in this diff name a private repository's paths and a commit of it, a private issue number, a production incident date, and an example agent name built from a person's name. The PR description does the same. Please remove these; this repository is public.
Checked and sound: webhook target pinning and redirect: "error"; secrets stay out of errors and logs; event reads are scoped to the grant; ingest is idempotent and acknowledges only after commit; OAuth revocation removes events; the new table and sequence are in src/control-tables.ts; the migration adds constant-default columns only.
I did not run the suite on this head; the points above are from reading the code. I will re-review after the rework.
Merges main (including #231's relaygate path); no history rewrite. Review findings on #236: - Migration renumbered to 0082 (main took 0079-0081); verifier manifest regenerated on PostgreSQL 15, diff only 0082. - Flag off no longer scans Cloud-copy rows. A per-row cloud_copy marker decides behaviour: rows saved before the flag keep inbox-batch wakes when it is turned on, so an already-pasted prompt is never silently broken; saving again with the flag on moves a row to the copy. The setup page marks such a routine. Polling rows are always copy rows (CHECK). - Sender trust comes only from rows this service owns: the person's hosted participants, Desktop identities and bound Amp/Capy bindings in this workspace. Agent metadata is never used; person identities give the sender kind. The scan no longer reads the roster. - Ingest and post-commit acknowledgements use #231's client choice and the renewed token, after its recovery early-returns; a gated participant never talks to Relaycast directly. Gate-only test added. - connect_grok_bot_routine checks the grant (setup allowed) before joining. - Retention: events reference their routine row ON DELETE CASCADE (disconnect, revocation, grant loss), every scan deletes events older than seven days whatever the flag, and reads never return them. - A concurrent duplicate routine-URL save maps the unique-index error to the duplicate answer. The URL hash is stored for every save. - grok_events gets its own capability reason. An undecryptable event is skipped instead of blocking later wakes; the bot's own deliveries are acknowledged; the cursor is a per-grant sequence. - Private references removed from comments, docs and the spec (repository paths, a commit, an issue number, incident dates, a person-derived example agent name). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Addressed in 1d9f17c. Main was merged in rather than rebased, so no history was rewritten. Each finding:
Please re-review. |
Phase 1 of
docs/specs/grok-bot-dynamic-prompt.md(#233). This is a copy, not a migration: Agent Relay Cloud is unchanged and keeps serving every bot connected to it. Everything is behindSELFHOST_GROK_ROUTINES_CLOUD_COPY, default off.Copied as is from Agent Relay Cloud
grokRoutinePrompt,SHOW_UI_GUIDANCE,grokRoutineHeaders,parseGrokRoutineUrl/Key/Details/AgentNameandperBotConnectionRequired. The code and prompt text are identical; only the import path, an error base class and two comments differ.sender-check.tsand the reply policy (eventSteps,repliesAutomatically,CHANNEL_ASK_TO_REPLY,replyCall).connect_grok_bot_routine(webhook and polling;name/from/mode; Cloud'snext_steps) anddisconnect_grok_bot_routine(mode/webhook_url).get_event/get_recent_events, including the app block withSender check: trustedSender …andReply setting: replyMode ….What the copied prompt directly needs
cloud_copy,delivery_mode,reply_mode,from_filter,polling_sinceandroutine_url_hash, which allows one grant per routine URL.grok_bot_events: a log per routine, with message text encrypted under the Control keyring. Events are deleted with their routine and are kept for at most seven days.grant_seqis the grant's own cursor.relay.message/relay.channel_messagewake per event, carrying a 200-character summary, withwebhook-id=eventIdandX-MCP-Subscription-Id= grant. Deliveries are acknowledged after the commit, the bot's own sends included.grok_eventstool group, for Grok connections only while the flag is on.notes-only, decision D2) and a sender filter.Per-routine behaviour
A routine uses the copy only if it was saved, or connected for polling, while the flag was on. Routines saved before keep their inbox-batch behaviour, so turning the flag on never breaks a prompt someone has already pasted. The setup page marks such a routine, and saving it again moves it to the copy. With the flag off, copy routines are not scanned at all.
Decisions
notes-only.Verification
npx tsc --noEmitandnpm run build: pass.test/grok-routines-cloud-copy.test.ts: 12 tests. They now also cover server-owned trust, flag-off non-scanning, unchanged pre-flag routines, retention, unreadable events, the duplicate-URL race, and gate-only ingest and acknowledgement.No merge, deploy or flag flip without Khaliq's direct OK.
🤖 Generated with Claude Code