-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(shields): wait for Hermes inference convergence #8530
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 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
07c09c3
fix(shields): wait for Hermes inference convergence
prekshivyas 4320522
refactor(shields): preserve source dependency budgets
prekshivyas ac02503
test(shields): type the convergence runner seam
prekshivyas dd9e718
Merge remote-tracking branch 'origin/main' into agent/fix-shields-inf…
prekshivyas 34c454c
fix(shields): harden inference convergence recovery
prekshivyas f6c63f2
test(shields): verify convergence rollback posture
prekshivyas a838a26
test(shields): reject malformed transition modes
prekshivyas 4219a63
test(shields): keep transition fixture linear
prekshivyas 0c47ef2
Merge remote-tracking branch 'origin/main' into agent/fix-shields-inf…
prekshivyas 6424aa7
Merge remote-tracking branch 'origin/main' into agent/fix-shields-inf…
prekshivyas 3a89844
fix(shields): execute convergence through OpenShell
prekshivyas de29bd6
Merge remote-tracking branch 'origin/main' into agent/fix-shields-inf…
prekshivyas 3e15114
chore(policy): format shared OpenShell argv
prekshivyas 435dabe
Merge remote-tracking branch 'origin/main' into agent/fix-shields-inf…
prekshivyas ec2b6f9
docs(shields): explain convergence boundary
prekshivyas a10ab75
Merge remote-tracking branch 'origin/main' into agent/fix-shields-inf…
prekshivyas 31ea118
Merge branch 'main' into agent/fix-shields-inference-convergence
cv 229814c
test(policy): cover resolved OpenShell argv
prekshivyas e3cb9e0
Merge remote-tracking branch 'origin/main' into agent/fix-shields-inf…
prekshivyas 3a31307
test(e2e): assert Hermes route after Shields down
prekshivyas ab01ee7
Merge branch 'main' into agent/fix-shields-inference-convergence
cv d2bf447
Merge branch 'main' into agent/fix-shields-inference-convergence
cv 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { waitForHermesInferenceRouteConvergence } from "./inference-convergence"; | ||
|
|
||
| function probe(status: number, output: string) { | ||
| return { status, stdout: output, stderr: "" }; | ||
| } | ||
|
|
||
| describe("Hermes inference convergence after a Shields policy transition", () => { | ||
| it("returns on the first healthy route probe", () => { | ||
| const run = vi.fn((_command: readonly string[], _options: object) => probe(0, "OK 200")); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = waitForHermesInferenceRouteConvergence("hermes-box", { run, sleep }); | ||
|
|
||
| expect(result).toEqual({ ok: true, attempts: 1, httpStatus: 200 }); | ||
| expect(sleep).not.toHaveBeenCalled(); | ||
| expect(run.mock.calls[0]?.[0]).toEqual([ | ||
| "sandbox", | ||
| "exec", | ||
| "--name", | ||
| "hermes-box", | ||
| "--", | ||
| "sh", | ||
| "-c", | ||
| expect.stringContaining("https://inference.local/v1/models"), | ||
| ]); | ||
| }); | ||
|
|
||
| it("waits for a transient HTTP 503 to converge", () => { | ||
| const run = vi | ||
| .fn((_command: readonly string[], _options: object) => probe(0, "OK 200")) | ||
| .mockReturnValueOnce(probe(0, "BROKEN 503")) | ||
| .mockReturnValueOnce(probe(0, "OK 200")); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = waitForHermesInferenceRouteConvergence("hermes-box", { | ||
| retryDelayMs: 750, | ||
| run, | ||
| sleep, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ ok: true, attempts: 2, httpStatus: 200 }); | ||
| expect(sleep).toHaveBeenCalledOnce(); | ||
| expect(sleep).toHaveBeenCalledWith(750); | ||
| }); | ||
|
|
||
| it("fails after the bounded probe budget instead of reporting Shields down ready", () => { | ||
| const run = vi.fn((_command: readonly string[], _options: object) => probe(0, "BROKEN 503")); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = waitForHermesInferenceRouteConvergence("hermes-box", { | ||
| maxAttempts: 3, | ||
| run, | ||
| sleep, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ ok: false, attempts: 3, httpStatus: 503 }); | ||
| expect(run).toHaveBeenCalledTimes(3); | ||
| expect(sleep).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("does not accept preambled probe output as convergence even when the command succeeds", () => { | ||
| const run = vi.fn((_command: readonly string[], _options: object) => ({ | ||
| status: 0, | ||
| stdout: "attacker preamble\nOK 200", | ||
| stderr: "", | ||
| })); | ||
|
|
||
| const result = waitForHermesInferenceRouteConvergence("hermes-box", { | ||
| maxAttempts: 1, | ||
| run, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ ok: false, attempts: 1, httpStatus: 0 }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| Number.NaN, | ||
| Number.POSITIVE_INFINITY, | ||
| ])("uses the bounded default attempt budget for non-finite maxAttempts (%s)", (maxAttempts) => { | ||
| const run = vi.fn((_command: readonly string[], _options: object) => probe(0, "BROKEN 503")); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = waitForHermesInferenceRouteConvergence("hermes-box", { | ||
| maxAttempts, | ||
| run, | ||
| sleep, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ ok: false, attempts: 4, httpStatus: 503 }); | ||
| expect(run).toHaveBeenCalledTimes(4); | ||
| expect(sleep).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it.each([ | ||
| Number.NaN, | ||
| Number.POSITIVE_INFINITY, | ||
| ])("uses the default delay for non-finite retryDelayMs (%s)", (retryDelayMs) => { | ||
| const run = vi | ||
| .fn((_command: readonly string[], _options: object) => probe(0, "OK 200")) | ||
| .mockReturnValueOnce(probe(0, "BROKEN 503")); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = waitForHermesInferenceRouteConvergence("hermes-box", { | ||
| retryDelayMs, | ||
| run, | ||
| sleep, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ ok: true, attempts: 2, httpStatus: 200 }); | ||
| expect(sleep).toHaveBeenCalledOnce(); | ||
| expect(sleep).toHaveBeenCalledWith(500); | ||
| }); | ||
| }); |
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,84 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { | ||
| buildSandboxInferenceRouteProbeArgs, | ||
| parseSandboxInferenceRouteProbeResult, | ||
| } from "../actions/sandbox/connect-inference-route-probe"; | ||
|
|
||
| const DEFAULT_MAX_ATTEMPTS = 4; | ||
| const DEFAULT_RETRY_DELAY_MS = 500; | ||
| const INFERENCE_ROUTE_PROBE_TIMEOUT_MS = 10_000; | ||
|
|
||
| interface InferenceRouteCommandResult { | ||
| status: number | null; | ||
| stdout?: string | Buffer | null; | ||
| stderr?: string | Buffer | null; | ||
| } | ||
|
|
||
| type RunInferenceRoute = ( | ||
| command: readonly string[], | ||
| options: { ignoreError: true; suppressOutput: true; timeout: number }, | ||
| ) => InferenceRouteCommandResult; | ||
|
|
||
| function sleepMs(milliseconds: number): void { | ||
| if (milliseconds <= 0 || !Number.isFinite(milliseconds)) return; | ||
| Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds); | ||
| } | ||
|
|
||
| export interface InferenceRouteConvergenceResult { | ||
| ok: boolean; | ||
| attempts: number; | ||
| httpStatus: number; | ||
| } | ||
|
|
||
| export interface InferenceRouteConvergenceOptions { | ||
| maxAttempts?: number; | ||
| retryDelayMs?: number; | ||
| run: RunInferenceRoute; | ||
| sleep?: (milliseconds: number) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Require the Hermes inference route to be usable after a live policy | ||
| * replacement. OpenShell's `policy set --wait` confirms the policy version is | ||
| * active, but the inference proxy can briefly continue returning HTTP 503 | ||
| * after that acknowledgement. Shields down must not report completion during | ||
| * that gap because callers immediately resume agent work. | ||
| */ | ||
| export function waitForHermesInferenceRouteConvergence( | ||
| sandboxName: string, | ||
| options: InferenceRouteConvergenceOptions, | ||
| ): InferenceRouteConvergenceResult { | ||
| const configuredMaxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS; | ||
| const configuredRetryDelayMs = options.retryDelayMs ?? DEFAULT_RETRY_DELAY_MS; | ||
| const maxAttempts = Number.isFinite(configuredMaxAttempts) | ||
| ? Math.max(1, Math.trunc(configuredMaxAttempts)) | ||
| : DEFAULT_MAX_ATTEMPTS; | ||
| const retryDelayMs = Number.isFinite(configuredRetryDelayMs) | ||
| ? Math.max(0, Math.trunc(configuredRetryDelayMs)) | ||
| : DEFAULT_RETRY_DELAY_MS; | ||
| const sleep = options.sleep ?? sleepMs; | ||
| let httpStatus = 0; | ||
|
|
||
| for (let attempt = 1; attempt <= maxAttempts; attempt += 1) { | ||
| const probe = options.run( | ||
| buildSandboxInferenceRouteProbeArgs(sandboxName, { name: "hermes" }), | ||
| { | ||
| ignoreError: true, | ||
| suppressOutput: true, | ||
| timeout: INFERENCE_ROUTE_PROBE_TIMEOUT_MS, | ||
| }, | ||
| ); | ||
| const parsed = parseSandboxInferenceRouteProbeResult({ | ||
| status: probe.status, | ||
| output: String(probe.stdout ?? ""), | ||
| stderr: String(probe.stderr ?? ""), | ||
| }); | ||
| httpStatus = parsed.httpStatus; | ||
| if (parsed.healthy) return { ok: true, attempts: attempt, httpStatus }; | ||
| if (attempt < maxAttempts) sleep(retryDelayMs); | ||
| } | ||
|
|
||
| return { ok: false, attempts: maxAttempts, httpStatus }; | ||
| } |
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.