-
Notifications
You must be signed in to change notification settings - Fork 11
feat: relay Grok approval notices and explicit one-time responses #80
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "grok-bot-cli": minor | ||
| --- | ||
|
|
||
| Forward pending Grok auto-review and local-tool approval cards as Codex notices without treating chat replies as authorization. Add CLI and MCP commands to inspect requests and explicitly accept once or decline an exact current request. |
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,12 @@ | ||
| import { Agent } from '@agent-bundle/runtime'; | ||
| import type { CliRouteConfig, CliRouteProps } from 'agent-bundle'; | ||
| import { listSchema as inputSchema, resultSchema, listOperation } from '../../core/grok-approval-routes.js'; | ||
| export { inputSchema, resultSchema }; | ||
| export const config = { | ||
| description: 'List current Grok approval cards (latest 200 entries).', positionals: ['target'], | ||
| inputJsonSchema: { type: 'object', properties: { target: { type: 'string' } }, required: ['target'], additionalProperties: false }, | ||
| } satisfies CliRouteConfig; | ||
| export default async function route({ input }: CliRouteProps<typeof inputSchema>) { | ||
| const out = await listOperation(input); | ||
| return <Agent.Result value={out}><Agent.Text>{JSON.stringify(out)}</Agent.Text></Agent.Result>; | ||
| } |
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,17 @@ | ||
| import { Agent } from '@agent-bundle/runtime'; | ||
| import type { CliRouteConfig, CliRouteProps } from 'agent-bundle'; | ||
| import { respondSchema as inputSchema, resultSchema, respondOperation } from '../../core/grok-approval-routes.js'; | ||
| export { inputSchema, resultSchema }; | ||
| export const config = { | ||
| description: 'Explicit user decision: accept a Grok request once or decline; never grant persistent permissions.', | ||
| inputJsonSchema: { | ||
| type: 'object', properties: { | ||
| target: { type: 'string' }, entryId: { type: 'string' }, requestId: { type: 'string' }, | ||
| decision: { type: 'string', enum: ['accept', 'decline'] }, | ||
| }, required: ['target', 'entryId', 'requestId', 'decision'], additionalProperties: false, | ||
| }, | ||
| } satisfies CliRouteConfig; | ||
| export default async function route({ input }: CliRouteProps<typeof inputSchema>) { | ||
| const out = await respondOperation(input); | ||
| return <Agent.Result value={out}><Agent.Text>{JSON.stringify(out)}</Agent.Text></Agent.Result>; | ||
| } |
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,14 @@ | ||
| import { z } from 'zod'; | ||
| import { connectGateway, listGrokApprovals, respondGrokApproval } from './gateway.js'; | ||
| import { grokApprovalResponseSchema } from './grok-approvals.js'; | ||
| import { withRedactedErrors } from '../gbot.js'; | ||
|
|
||
| export const listSchema = z.strictObject({ target: z.string().min(1).max(1024) }); | ||
| export const respondSchema = grokApprovalResponseSchema; | ||
| export const resultSchema = z.record(z.string(), z.json()); | ||
| export const listOperation = (input: z.infer<typeof listSchema>) => withRedactedErrors(async () => | ||
| listGrokApprovals(await connectGateway(), listSchema.parse(input).target)); | ||
| export const respondOperation = (input: z.infer<typeof respondSchema>) => withRedactedErrors(async () => { | ||
| const { target, ...response } = respondSchema.parse(input); | ||
| return respondGrokApproval(await connectGateway(), target, response); | ||
| }); |
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,32 @@ | ||
| import { z } from "zod"; | ||
| import { redactSecrets } from "./url-policy.js"; | ||
|
|
||
| const id = z.string().min(1).max(1024); | ||
| export const grokApprovalResponseSchema = z.strictObject({ | ||
| target: id, | ||
| entryId: id, | ||
| requestId: id, | ||
| decision: z.enum(["accept", "decline"]), | ||
| }); | ||
|
|
||
| export function grokApproval(entry) { | ||
| if (entry?.kind !== "send-message") return null; | ||
| const type = entry.message?.type; | ||
| const card = type === "auto-review-approval" ? entry.message.approval | ||
| : type === "local-tool-permission" ? entry.message.ask : null; | ||
| if (card?.status !== "pending" || !id.safeParse(card.requestId).success || !id.safeParse(entry.id).success) return null; | ||
| const details = {}; | ||
| let truncated = false; | ||
| for (const key of ["reason", "command", "summary", "action", "description", "target", "machineId", "surface", "workingDirectory"]) { | ||
| if (typeof card[key] !== "string") continue; | ||
| const text = redactSecrets(card[key]); | ||
| truncated ||= text.length > 2048; | ||
| details[key] = text.slice(0, 2048); | ||
| } | ||
| return { entryId: entry.id, requestId: card.requestId, type, ...details, truncated }; | ||
| } | ||
|
|
||
| export function grokApprovalNotice(entry, target) { | ||
| const approval = grokApproval(entry); | ||
| return approval ? `Grok approval pending. Requires an explicit user decision; never approve automatically.\n${JSON.stringify({ botTarget: target, ...approval })}\nUse gbot_grok_respond with target=${JSON.stringify(target)}, entryId, requestId and decision accept (once) or decline.${approval.truncated ? " Details are truncated; acceptance requires the owning Grok UI." : ""} A chat reply is not authorization.` : null; | ||
| } |
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,12 @@ | ||
| import { Agent } from '@agent-bundle/runtime'; | ||
| import { defineTool } from 'agent-bundle/routes'; | ||
| import { listSchema as inputSchema, resultSchema, listOperation } from '../../../core/grok-approval-routes.js'; | ||
| export { inputSchema }; | ||
| export default defineTool({ | ||
| title: 'Pending Grok approvals', description: 'List pending auto-review and local-tool approval cards in the latest 200 entries for a Grok bot. Older or unsupported requests require the owning Grok UI.', | ||
| annotations: { readOnlyHint: true }, inputSchema, resultSchema, | ||
| inputJsonSchema: { type: 'object', properties: { target: { type: 'string' } }, required: ['target'], additionalProperties: false }, | ||
| }, async input => { | ||
| const out = await listOperation(input); | ||
| return <Agent.Result value={out}><Agent.Text>{JSON.stringify(out)}</Agent.Text></Agent.Result>; | ||
| }); |
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,17 @@ | ||
| import { Agent } from '@agent-bundle/runtime'; | ||
| import { defineTool } from 'agent-bundle/routes'; | ||
| import { respondSchema as inputSchema, resultSchema, respondOperation } from '../../../core/grok-approval-routes.js'; | ||
| export { inputSchema }; | ||
| export default defineTool({ | ||
| title: 'Respond to Grok approval', description: 'Only after an explicit user decision: accept one current Grok approval once or decline it. Exact target, entryId and approval requestId required. Never auto-approve or grant persistent permissions. Success acknowledges response delivery, not execution.', | ||
| annotations: { readOnlyHint: false }, inputSchema, resultSchema, | ||
| inputJsonSchema: { | ||
| type: 'object', properties: { | ||
| target: { type: 'string' }, entryId: { type: 'string' }, requestId: { type: 'string' }, | ||
| decision: { type: 'string', enum: ['accept', 'decline'] }, | ||
| }, required: ['target', 'entryId', 'requestId', 'decision'], additionalProperties: false, | ||
| }, | ||
| }, async input => { | ||
| const out = await respondOperation(input); | ||
| return <Agent.Result value={out}><Agent.Text>{JSON.stringify(out)}</Agent.Text></Agent.Result>; | ||
| }); |
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,52 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import { listGrokApprovals, respondGrokApproval } from "../src/core/gateway.js"; | ||
|
|
||
| test("Grok responses require a current exact card and only grant once or reject", async t => { | ||
| const original = globalThis.fetch; | ||
| const testEnv = process.env.GROK_BOT_TEST; | ||
| process.env.GROK_BOT_TEST = '1'; | ||
| t.after(() => { | ||
| globalThis.fetch = original; | ||
| if (testEnv === undefined) delete process.env.GROK_BOT_TEST; | ||
| else process.env.GROK_BOT_TEST = testEnv; | ||
| }); | ||
| const calls = []; | ||
| const approval = { requestId: "ask", status: "pending", command: "npm publish" }; | ||
| const entries = [{ id: "card", kind: "send-message", message: { type: "auto-review-approval", approval } }]; | ||
| globalThis.fetch = async (url, options) => { | ||
| const method = url.split("/").at(-1); | ||
| const body = JSON.parse(options.body); | ||
| calls.push({ method, body }); | ||
| const result = method === "listAgents" ? { agents: [{ id: "bot", name: "Router" }] } | ||
| : method === "getAgentTranscriptTail" ? { entries } : {}; | ||
| return new Response(JSON.stringify(result)); | ||
| }; | ||
| const session = { gatewayUrl: "http://127.0.0.1:1", gatewayToken: "fixture" }; | ||
| assert.equal((await listGrokApprovals(session, "bot")).approvals[0].requestId, "ask"); | ||
| for (const patch of [{ requestId: "foreign" }, { entryId: "foreign" }, { decision: "always" }]) { | ||
| await assert.rejects(respondGrokApproval(session, "bot", { entryId: "card", requestId: "ask", decision: "accept", ...patch })); | ||
| } | ||
| assert.equal(calls.filter(c => c.method.startsWith("resolve")).length, 0); | ||
| await respondGrokApproval(session, "bot", { entryId: "card", requestId: "ask", decision: "accept" }); | ||
| assert.deepEqual(calls.at(-1), { method: "resolveAutoReviewApproval", body: { | ||
| agentId: "bot", entryId: "card", requestId: "ask", resolution: "approved", | ||
| } }); | ||
| approval.status = "expired"; | ||
| await assert.rejects(respondGrokApproval(session, "bot", { entryId: "card", requestId: "ask", decision: "accept" })); | ||
| entries[0].message = { type: "local-tool-permission", ask: { requestId: "local", status: "pending", action: "run-command", target: "rm ./output.txt", machineId: "ubuntu" } }; | ||
| assert.deepEqual((await listGrokApprovals(session, "bot")).approvals[0], { | ||
| entryId: "card", requestId: "local", type: "local-tool-permission", action: "run-command", | ||
| target: "rm ./output.txt", machineId: "ubuntu", truncated: false, | ||
| }); | ||
| await respondGrokApproval(session, "bot", { entryId: "card", requestId: "local", decision: "decline" }); | ||
| assert.equal(calls.at(-1).method, "resolveLocalToolPermission"); | ||
| assert.equal(calls.at(-1).body.resolution, "deny"); | ||
| await respondGrokApproval(session, "bot", { entryId: "card", requestId: "local", decision: "accept" }); | ||
| assert.equal(calls.at(-1).body.resolution, "allow-once"); | ||
| entries[0].message.ask.target = "x".repeat(3000); | ||
| assert.equal((await listGrokApprovals(session, "bot")).approvals[0].truncated, true); | ||
| const before = calls.filter(c => c.method.startsWith("resolve")).length; | ||
| await assert.rejects(respondGrokApproval(session, "bot", { entryId: "card", requestId: "local", decision: "accept" }), /truncated/); | ||
| assert.equal(calls.filter(c => c.method.startsWith("resolve")).length, before); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When Grok raises an approval while processing an automatically returned Codex answer, the card's outer
requestIdmatches agrok-returnID inown, so the preceding guard skips the entry beforegrokApprovalNoticeruns. The poll then advances its cursor, permanently hiding that pending approval from Codex. Approval cards need to be classified before applying the normal own-response suppression.Useful? React with 👍 / 👎.