-
Notifications
You must be signed in to change notification settings - Fork 716
fix(cli): bind local diagnostic reads to runtime #1448
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
Wibias
merged 3 commits into
lidge-jun:dev
from
luvs01:agent/bind-doctor-local-read-capability
Aug 12, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,100 @@ | ||
| import { createHmac, timingSafeEqual } from "node:crypto"; | ||
| import { isLocalAttestationSecret } from "./local-management-attestation"; | ||
|
|
||
| export const LOCAL_MANAGEMENT_EXPECTED_PID_HEADER = "x-opencodex-local-expected-pid"; | ||
| export const LOCAL_MANAGEMENT_NONCE_HEADER = "x-opencodex-local-nonce"; | ||
| export const LOCAL_MANAGEMENT_CAPABILITY_EXPIRES_AT_HEADER = "x-opencodex-local-expires-at"; | ||
| export const LOCAL_MANAGEMENT_CAPABILITY_HEADER = "x-opencodex-local-capability"; | ||
| export const LOCAL_MANAGEMENT_CAPABILITY_TTL_MS = 10_000; | ||
|
|
||
| export const LOCAL_MANAGEMENT_READ_PATHS = { | ||
| codexAccounts: "/api/codex-auth/accounts", | ||
| systemMemory: "/api/system/memory", | ||
| } as const; | ||
|
|
||
| export type LocalManagementReadPath = | ||
| typeof LOCAL_MANAGEMENT_READ_PATHS[keyof typeof LOCAL_MANAGEMENT_READ_PATHS]; | ||
|
|
||
| const BASE64URL_256 = /^[A-Za-z0-9_-]{43}$/; | ||
| const LOCAL_READ_METHOD = "GET"; | ||
|
|
||
| export type ExpectedLocalManagementPid = | ||
| | { kind: "absent" } | ||
| | { kind: "invalid" } | ||
| | { kind: "present"; pid: number }; | ||
|
|
||
| export function parseExpectedLocalManagementPid(value: string | null): ExpectedLocalManagementPid { | ||
| if (value === null) return { kind: "absent" }; | ||
| if (!/^[1-9]\d*$/.test(value)) return { kind: "invalid" }; | ||
| const pid = Number(value); | ||
| return Number.isSafeInteger(pid) ? { kind: "present", pid } : { kind: "invalid" }; | ||
| } | ||
|
|
||
| function isLocalManagementReadPath(path: string): path is LocalManagementReadPath { | ||
| return path === LOCAL_MANAGEMENT_READ_PATHS.codexAccounts | ||
| || path === LOCAL_MANAGEMENT_READ_PATHS.systemMemory; | ||
| } | ||
|
|
||
| function localReadCapabilityPayload( | ||
| nonce: string, | ||
| method: string, | ||
| path: string, | ||
| pid: number, | ||
| port: number, | ||
| expiresAt: number, | ||
| ): string | null { | ||
| if (!BASE64URL_256.test(nonce)) return null; | ||
| if (method !== LOCAL_READ_METHOD || !isLocalManagementReadPath(path)) return null; | ||
| if (!Number.isSafeInteger(pid) || pid <= 0) return null; | ||
| if (!Number.isInteger(port) || port <= 0 || port > 65535) return null; | ||
| if (!Number.isSafeInteger(expiresAt) || expiresAt <= 0) return null; | ||
| return `opencodex-local-management-read-v1\n${nonce}\n${method}\n${path}\n${pid}\n${port}\n${expiresAt}`; | ||
| } | ||
|
|
||
| /** Process-scoped authorization for one allowlisted local management GET. */ | ||
| export function createLocalManagementReadCapability( | ||
| secret: string, | ||
| nonce: string, | ||
| method: string, | ||
| path: string, | ||
| pid: number, | ||
| port: number, | ||
| expiresAt: number, | ||
| ): string | null { | ||
| if (!isLocalAttestationSecret(secret)) return null; | ||
| const payload = localReadCapabilityPayload(nonce, method, path, pid, port, expiresAt); | ||
| if (!payload) return null; | ||
| return createHmac("sha256", secret).update(payload).digest("base64url"); | ||
| } | ||
|
|
||
| export function verifyLocalManagementReadCapability( | ||
| secret: string, | ||
| nonce: string | null, | ||
| method: string, | ||
| path: string, | ||
| pid: number, | ||
| port: number, | ||
| expiresAt: number, | ||
| capability: string | null, | ||
| now = Date.now(), | ||
| ): boolean { | ||
| if (!nonce || !capability || !BASE64URL_256.test(capability)) return false; | ||
| if ( | ||
| !Number.isSafeInteger(now) | ||
| || expiresAt <= now | ||
| || expiresAt > now + LOCAL_MANAGEMENT_CAPABILITY_TTL_MS | ||
| ) return false; | ||
| const expected = createLocalManagementReadCapability( | ||
| secret, | ||
| nonce, | ||
| method, | ||
| path, | ||
| pid, | ||
| port, | ||
| expiresAt, | ||
| ); | ||
| if (!expected) return false; | ||
| const expectedBytes = Buffer.from(expected); | ||
| const actualBytes = Buffer.from(capability); | ||
| return expectedBytes.length === actualBytes.length && timingSafeEqual(expectedBytes, actualBytes); | ||
| } |
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.
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.
📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value
fetchServiceMemoryadvertises atimeoutMsoption that it discards.The parameter type is
LocalManagementReadDeps, which declarestimeoutMs?: number. At lines 620-623 the spread...depsis applied first, thentimeoutMs: SERVICE_MEMORY_TIMEOUT_MSoverwrites it. A caller that passestimeoutMstherefore gets silent no-op behavior, and the type signature does not communicate that. Make the contract explicit in one of two ways.♻️ Option 1 (preferred): remove the knob from the accepted type
♻️ Option 2: honor a caller-supplied timeout with the doctor default
const read = await fetchBoundLocalManagementRead(target, LOCAL_MANAGEMENT_READ_PATHS.systemMemory, { ...deps, - timeoutMs: SERVICE_MEMORY_TIMEOUT_MS, + timeoutMs: deps.timeoutMs ?? SERVICE_MEMORY_TIMEOUT_MS, });📝 Committable suggestion
🤖 Prompt for AI Agents