-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathprovider-inference.test-support.ts
More file actions
257 lines (250 loc) · 9.53 KB
/
Copy pathprovider-inference.test-support.ts
File metadata and controls
257 lines (250 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Shared test scaffolding for handleProviderInferenceState specs. Extracted so
// the context-window regression cases can live in their own file without
// duplicating the deps/session factories (PR #6293 PRA-6). Not a *.test.ts
// file, so Vitest does not collect it as a suite.
import { vi } from "vitest";
import type {
CurrentGatewayRouteCompatibilityCheck,
CurrentGatewayRouteDiscoveryPreflight,
} from "../../../inference/gateway-route-compatibility";
import { createSession, type Session, type SessionUpdates } from "../../../state/onboard-session";
import {
normalizeReasoningEffort,
REASONING_EFFORT_ENV,
resolveReasoningEffortRequest,
} from "../../reasoning-mode";
import {
createProviderRecoveryReceiptLedger,
mintProviderRecoveryReceipt,
type ProviderRecoveryReceipt,
} from "../../rebuild-route-handoff";
import type { ProviderInferenceStateOptions, ProviderSelectionResult } from "./provider-inference";
export type Gpu = { type: string } | null;
export type Agent = { name: string; inference?: { provider_type?: string } } | null;
export type Host = { cpus?: number };
export const baseSelection: ProviderSelectionResult = {
model: "nvidia/test",
provider: "nvidia-prod",
endpointUrl: "https://integrate.api.nvidia.com/v1",
credentialEnv: "NVIDIA_INFERENCE_API_KEY",
hermesAuthMethod: null,
hermesToolGateways: [],
preferredInferenceApi: "openai-responses",
compatibleEndpointReasoning: null,
compatibleEndpointReasoningEffort: null,
nimContainer: null,
};
/** Mint and activate a provider-recovery receipt bound to one session, as the rebuild assembly would. */
export function activatedRecoveryReceipt(input: {
sandboxName: string;
sessionId: string;
gatewayName?: string;
provider?: string;
model?: string;
endpointUrl?: string | null;
preferredInferenceApi?: string | null;
nowMs?: number;
ledger?: ReturnType<typeof createProviderRecoveryReceiptLedger>;
}): {
receipt: ProviderRecoveryReceipt;
ledger: ReturnType<typeof createProviderRecoveryReceiptLedger>;
} {
const gatewayName = input.gatewayName ?? "nemoclaw";
const provider = input.provider ?? "compatible-endpoint";
const model = input.model ?? "mock/channels-rebuild";
const nowMs = input.nowMs ?? 0;
const target = {
sandboxName: input.sandboxName,
gatewayName,
provider,
model,
route: {
provider,
model,
endpointUrl: input.endpointUrl ?? "https://compatible.example.test/v1",
preferredInferenceApi: input.preferredInferenceApi ?? "openai-completions",
source: "registry" as const,
},
};
const ledger = input.ledger ?? createProviderRecoveryReceiptLedger();
const minted = mintProviderRecoveryReceipt(target, {
nonce: `nonce-${input.sandboxName}-${input.sessionId}`,
expiresAtMs: Number.MAX_SAFE_INTEGER,
});
const receipt = ledger.activate(minted, { target, sessionId: input.sessionId, nowMs });
if (!receipt) throw new Error("test recovery receipt failed to activate");
return { receipt, ledger };
}
export function createDeps(
overrides: Partial<ProviderInferenceStateOptions<Gpu, Agent, Host>["deps"]> = {},
) {
const calls = {
checkGatewayRouteCompatibility: vi.fn<CurrentGatewayRouteCompatibilityCheck>(() => ({
ok: true,
})),
preflightGatewayRouteDiscovery: vi.fn<CurrentGatewayRouteDiscoveryPreflight>(() => ({
ok: true,
requiredModel: null,
requiredEndpointUrl: null,
requiredInferenceApi: null,
})),
setupNim: vi.fn(async () => ({ ...baseSelection })),
setupInference: vi.fn(async () => ({ ok: true as const })),
startStep: vi.fn(async () => undefined),
complete: vi.fn(async () => createSession()),
rejected: vi.fn(async () => createSession()),
skipped: vi.fn(),
recoverProvider: vi.fn(
async (
_gatewayName: string,
_provider: string | null | undefined,
credentialEnv: string | null | undefined,
) => ({
forceInferenceSetup: false,
credentialEnv: credentialEnv ?? null,
}),
),
recoverManagedLlamaCpp: vi.fn(async () => false),
surfaceReady: vi.fn(() => true),
recordSkip: vi.fn(async () => createSession()),
repairEvent: vi.fn(async () => createSession()),
hydrate: vi.fn(),
repair: vi.fn(),
routeReady: vi.fn((_gatewayName: string, _provider: string, _model: string) => false),
reconcileRouter: vi.fn(async () => undefined),
reupsertRoutedProvider: vi.fn(
(
_gatewayName: string,
_provider: string,
endpointUrl: string | null,
_credentialEnv: string | null,
) => ({
ok: true as const,
endpointUrl: "http://host.openshell.internal:4000/v1",
}),
),
reserveRoute: vi.fn(() => true),
updateSandbox: vi.fn(),
checkpointSandboxIdentity: vi.fn(async () => undefined),
prepareLocalProviderForInference: vi.fn(async () => null),
promptName: vi.fn(async () => "my-assistant"),
prompt: vi.fn(async () => "1"),
log: vi.fn(),
error: vi.fn(),
exit: vi.fn((code: number): never => {
throw new Error(`exit ${code}`);
}),
deleteEnv: vi.fn(),
};
return {
calls,
deps: {
checkGatewayRouteCompatibility: calls.checkGatewayRouteCompatibility,
preflightGatewayRouteDiscovery: calls.preflightGatewayRouteDiscovery,
getSandboxRecoveryAuthority: (): "missing" => "missing",
withGatewayRouteMutationLock: async <T>(
_gatewayName: string,
operation: () => Promise<T> | T,
) => await operation(),
normalizeHermesAuthMethod: (value: string | null | undefined) =>
value === "oauth" || value === "api_key" ? value : null,
setupNim: calls.setupNim,
setupInference: calls.setupInference,
startRecordedStep: calls.startStep,
recordStepComplete: calls.complete,
recordStepRejected: calls.rejected,
toSessionUpdates: (updates: Record<string, unknown>) => updates as SessionUpdates,
skippedStepMessage: calls.skipped,
ensureManagedLlamaCppResumeReady: calls.recoverManagedLlamaCpp,
ensureResumeProviderReady: calls.recoverProvider,
isResumeProviderSurfaceReady: calls.surfaceReady,
recordStateSkipped: calls.recordSkip,
recordRepairEvent: calls.repairEvent,
hydrateCredentialEnv: calls.hydrate,
configureCompatibleEndpointReasoning: async (value?: string | null) =>
value === "true" ? "true" : "false",
clearCompatibleEndpointReasoning: () => null,
configureCompatibleEndpointReasoningEffort: async (
value?: unknown,
env: NodeJS.ProcessEnv = process.env,
allowRequestFallback = true,
) => {
const configured =
normalizeReasoningEffort(value) ??
(allowRequestFallback ? resolveReasoningEffortRequest(null, env).effort : null);
if (configured) {
env[REASONING_EFFORT_ENV] = configured;
} else {
delete env[REASONING_EFFORT_ENV];
}
return configured;
},
clearCompatibleEndpointReasoningEffort: () => null,
repairLocalInferenceSystemdOverrideOrExit: calls.repair,
isNonInteractive: () => true,
getOpenshellBinary: () => "/usr/bin/openshell",
needsBedrockRuntimeAdapter: () => false,
isInferenceRouteReady: calls.routeReady,
isRoutedInferenceProvider: (provider: string) => provider === "nvidia-router",
reconcileModelRouter: calls.reconcileRouter,
reupsertRoutedProvider: calls.reupsertRoutedProvider,
reserveSandboxInferenceRoute: calls.reserveRoute,
registryUpdateSandbox: calls.updateSandbox,
checkpointSandboxIdentity: calls.checkpointSandboxIdentity,
prepareLocalProviderForInference: calls.prepareLocalProviderForInference,
promptValidatedSandboxName: calls.promptName,
assessHost: () => ({ cpus: 8 }),
formatSandboxBuildEstimateNote: () => "estimate",
formatOnboardConfigSummary: (options: {
provider: string;
model: string;
sandboxName: string;
}) => `summary:${options.provider}/${options.model}/${options.sandboxName}`,
prompt: calls.prompt,
cliName: () => "nemoclaw",
log: calls.log,
error: calls.error,
exitProcess: calls.exit,
deleteEnv: calls.deleteEnv,
...overrides,
},
};
}
export function baseOptions(
deps: ProviderInferenceStateOptions<Gpu, Agent, Host>["deps"],
session: Session | null = createSession(),
): ProviderInferenceStateOptions<Gpu, Agent, Host> {
return {
gatewayName: "nemoclaw",
resume: false,
fresh: false,
session,
gpu: { type: "nvidia" },
sandboxName: null,
agent: null,
initial: {
model: session?.model ?? null,
provider: session?.provider ?? null,
endpointUrl: session?.endpointUrl ?? null,
credentialEnv: session?.credentialEnv ?? null,
hermesAuthMethod: session?.hermesAuthMethod ?? null,
hermesToolGateways: session?.hermesToolGateways ?? [],
preferredInferenceApi: session?.preferredInferenceApi ?? null,
compatibleEndpointReasoning: session?.compatibleEndpointReasoning ?? null,
compatibleEndpointReasoningEffort: session?.compatibleEndpointReasoningEffort ?? null,
nimContainer: session?.nimContainer ?? null,
webSearchConfig: session?.webSearchConfig ?? null,
},
selectedMessagingChannels: [],
env: {},
constants: {
hermesProviderName: "hermes-provider",
hermesApiKeyAuthMethod: "api_key",
hermesApiKeyCredentialEnv: "NOUS_API_KEY",
},
deps,
};
}