-
Notifications
You must be signed in to change notification settings - Fork 857
fix(claude): sync agent roster on proxy startup #2202
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3990288
fix(claude): sync agent roster on proxy startup
Ingwannu 0a0dff1
fix(claude): await roster sync during ensure
Ingwannu b141d9c
fix(claude): hold readiness through roster sync
Ingwannu 5df962b
docs: clarify roster definition ownership
Ingwannu 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
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,73 @@ | ||
| import type { OcxConfig } from "../types"; | ||
| import { injectClaudeAgentDefs } from "../claude/agents-inject"; | ||
| import { fetchClaudeContextWindows } from "./claude"; | ||
| import type { ReadinessGate } from "../server/readiness"; | ||
|
|
||
| export interface ClaudeAgentStartupSyncDeps { | ||
| fetchContextWindows?: typeof fetchClaudeContextWindows; | ||
| injectAgentDefs?: typeof injectClaudeAgentDefs; | ||
| warn?: (message: string) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Keep the public readiness gate pending until both startup reconciliations have settled. | ||
| * | ||
| * The Codex sync remains the authority for ready versus failed. Claude roster repair is | ||
| * deliberately best-effort (#2200), but readiness must not become observable between the | ||
| * Codex write and that repair: a service manager could otherwise launch Claude Code against | ||
| * stale `ocx-*.md` files. A small forwarding gate delays only the successful transition; | ||
| * terminal Codex failure is still published immediately. | ||
| */ | ||
| export async function reconcileClientStartupBeforeReady<T>( | ||
| readinessGate: ReadinessGate, | ||
| syncCodex: (deferredGate: ReadinessGate) => Promise<T>, | ||
| syncClaudeRoster: () => Promise<unknown>, | ||
| ): Promise<T> { | ||
| let codexReady = false; | ||
| const deferredGate: ReadinessGate = { | ||
| getStatus: () => readinessGate.getStatus(), | ||
| markReady: () => { codexReady = true; }, | ||
| markFailed: () => readinessGate.markFailed(), | ||
| }; | ||
|
|
||
| const result = await syncCodex(deferredGate); | ||
| await syncClaudeRoster(); | ||
| if (codexReady) readinessGate.markReady(); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Reconcile the generated Claude Code roster after the proxy listener is live. | ||
| * | ||
| * This belongs to the owning CLI lifecycle rather than `startServer`: the latter is also a | ||
| * library/test primitive and must not mutate a developer's real `~/.claude` directory merely | ||
| * because an in-process test server was created. The live Management API supplies the same bounded | ||
| * context-window map used by `ocx claude`; failure keeps startup available and falls back to an | ||
| * unmarked roster. Disabled integrations skip discovery and prune verified-owned definitions. | ||
| */ | ||
| export async function syncClaudeAgentDefsAtProxyStartup( | ||
| config: OcxConfig, | ||
| port: number, | ||
| deps: ClaudeAgentStartupSyncDeps = {}, | ||
| ): Promise<string[] | null> { | ||
| const inject = deps.injectAgentDefs ?? injectClaudeAgentDefs; | ||
| const warn = deps.warn ?? (message => console.warn(message)); | ||
|
|
||
| try { | ||
| if (config.claudeCode?.enabled === false || config.claudeCode?.injectAgents === false) { | ||
| return inject(config, {}); | ||
| } | ||
|
|
||
| let windows: Record<string, number> = {}; | ||
| try { | ||
| windows = await (deps.fetchContextWindows ?? fetchClaudeContextWindows)(config, port); | ||
| } catch { | ||
| // Startup remains best-effort. The next management mutation or `ocx claude` launch can | ||
| // restore context markers after a transient catalog/Management API failure. | ||
| } | ||
| return inject(config, windows); | ||
| } catch (error) { | ||
| warn(`⚠ Claude agent definitions could not be synced at proxy startup: ${error instanceof Error ? error.message : String(error)}`); | ||
| return 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
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,116 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { tmpdir } from "node:os"; | ||
| import { | ||
| reconcileClientStartupBeforeReady, | ||
| syncClaudeAgentDefsAtProxyStartup, | ||
| } from "../src/cli/claude-agent-startup-sync"; | ||
| import { injectClaudeAgentDefs } from "../src/claude/agents-inject"; | ||
| import { createReadinessGate } from "../src/server/readiness"; | ||
| import type { OcxConfig } from "../src/types"; | ||
|
|
||
| const config = (claudeCode: OcxConfig["claudeCode"] = {}): OcxConfig => ({ | ||
| providers: [], | ||
| claudeCode, | ||
| } as OcxConfig); | ||
|
|
||
| describe("Claude agent roster proxy-start synchronization (#2200)", () => { | ||
| test("keeps readiness pending until the best-effort roster fence settles", async () => { | ||
| const gate = createReadinessGate(); | ||
| let releaseRoster!: () => void; | ||
| const rosterPending = new Promise<void>(resolve => { releaseRoster = resolve; }); | ||
|
|
||
| const startup = reconcileClientStartupBeforeReady( | ||
| gate, | ||
| async deferredGate => { | ||
| deferredGate.markReady(); | ||
| return { ran: true }; | ||
| }, | ||
| () => rosterPending, | ||
| ); | ||
|
|
||
| await Promise.resolve(); | ||
| expect(gate.getStatus()).toBe("pending"); | ||
| releaseRoster(); | ||
| expect(await startup).toEqual({ ran: true }); | ||
| expect(gate.getStatus()).toBe("ready"); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test("uses the live proxy context-window map for an enabled roster", async () => { | ||
| const calls: Array<{ port: number; windows?: Record<string, number> }> = []; | ||
| const result = await syncClaudeAgentDefsAtProxyStartup(config(), 10100, { | ||
| fetchContextWindows: async (_cfg, port) => { | ||
| calls.push({ port }); | ||
| return { "google/gemini-3.7-flash": 1_000_000 }; | ||
| }, | ||
| injectAgentDefs: (_cfg, windows) => { | ||
| calls.push({ port: 0, windows }); | ||
| return ["ocx-google-gemini-3-7-flash.md"]; | ||
| }, | ||
| }); | ||
|
|
||
| expect(result).toEqual(["ocx-google-gemini-3-7-flash.md"]); | ||
| expect(calls).toEqual([ | ||
| { port: 10100 }, | ||
| { port: 0, windows: { "google/gemini-3.7-flash": 1_000_000 } }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("disabled integration prunes owned definitions without touching discovery", async () => { | ||
| let fetched = false; | ||
| let injected: Record<string, number> | undefined; | ||
| const result = await syncClaudeAgentDefsAtProxyStartup(config({ injectAgents: false }), 10100, { | ||
| fetchContextWindows: async () => { | ||
| fetched = true; | ||
| return { stale: 1_000_000 }; | ||
| }, | ||
| injectAgentDefs: (_cfg, windows) => { | ||
| injected = windows; | ||
| return []; | ||
| }, | ||
| }); | ||
|
|
||
| expect(result).toEqual([]); | ||
| expect(fetched).toBe(false); | ||
| expect(injected).toEqual({}); | ||
| }); | ||
|
|
||
| test("catalog failure still runs the real injector with an unmarked roster", async () => { | ||
| const dir = mkdtempSync(join(tmpdir(), "ocx-startup-roster-")); | ||
| try { | ||
| const configured = { | ||
| providers: [], | ||
| subagentModels: ["gpt-5.6-sol"], | ||
| claudeCode: { model: "gpt-5.6-sol" }, | ||
| } as OcxConfig; | ||
| const result = await syncClaudeAgentDefsAtProxyStartup(configured, 10100, { | ||
| fetchContextWindows: async () => { throw new Error("catalog unavailable"); }, | ||
| injectAgentDefs: (cfg, windows) => injectClaudeAgentDefs(cfg, windows, dir), | ||
| }); | ||
|
|
||
| expect(result?.sort()).toEqual(["ocx-gpt-5-6-sol.md", "ocx-self.md"]); | ||
| expect(readdirSync(join(dir, "agents")).sort()).toEqual(result?.sort()); | ||
| for (const file of result ?? []) { | ||
| const body = readFileSync(join(dir, "agents", file), "utf8"); | ||
| expect(body).toContain("generated-by: opencodex"); | ||
| expect(body).not.toContain("[1m]"); | ||
| } | ||
| } finally { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| test("write failures are warned and never fail proxy startup", async () => { | ||
| const warnings: string[] = []; | ||
| const result = await syncClaudeAgentDefsAtProxyStartup(config(), 10100, { | ||
| fetchContextWindows: async () => ({}), | ||
| injectAgentDefs: () => { throw new Error("permission denied"); }, | ||
| warn: message => warnings.push(message), | ||
| }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(warnings).toHaveLength(1); | ||
| expect(warnings[0]).toContain("permission denied"); | ||
| }); | ||
| }); | ||
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.