agl 42.1.0 → 42.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +44 -0
- package/dist_serve/bundle.js +797 -785
- package/dist_serve/bundle.js.map +2 -2
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/aglapi/aglapisurface.document.js +124 -10
- package/dist_ts/classes.accountsactions.d.ts +3 -1
- package/dist_ts/classes.accountsactions.js +16 -1
- package/dist_ts/classes.authstore.js +9 -1
- package/dist_ts/classes.cli.js +19 -49
- package/dist_ts/classes.configurationauthority.js +2 -2
- package/dist_ts/classes.controllerservice.d.ts +20 -2
- package/dist_ts/classes.controllerservice.js +42 -6
- package/dist_ts/classes.flexservice.js +8 -3
- package/dist_ts/classes.inferencegateway.d.ts +30 -2
- package/dist_ts/classes.inferencegateway.js +115 -20
- package/dist_ts/classes.inferencerouter.d.ts +16 -0
- package/dist_ts/classes.inferencerouter.js +5 -3
- package/dist_ts/classes.inferenceservice.js +7 -3
- package/dist_ts/classes.ticketmodels.js +4 -3
- package/dist_ts/classes.ticketnotifier.js +9 -2
- package/dist_ts/classes.ticketstore.js +9 -5
- package/dist_ts/classes.upgradecoordinator.d.ts +6 -1
- package/dist_ts/classes.upgradecoordinator.js +9 -2
- package/dist_ts/classes.upgradetransaction.d.ts +27 -1
- package/dist_ts/classes.upgradetransaction.js +92 -2
- package/dist_ts/configuration/functions.master.d.ts +7 -2
- package/dist_ts/configuration/functions.master.js +7 -4
- package/dist_ts/driver/classes.drivereventrelay.d.ts +1 -1
- package/dist_ts/driver/classes.drivereventrelay.js +3 -3
- package/dist_ts/fleet/classes.boardpump.d.ts +1 -1
- package/dist_ts/fleet/classes.boardpump.js +7 -4
- package/dist_ts/fleet/classes.claudeworkers.d.ts +6 -0
- package/dist_ts/fleet/classes.claudeworkers.js +17 -7
- package/dist_ts/fleet/classes.deploymodels.d.ts +19 -0
- package/dist_ts/fleet/classes.deploymodels.js +140 -0
- package/dist_ts/fleet/functions.validation.d.ts +8 -1
- package/dist_ts/fleet/functions.validation.js +65 -3
- package/dist_ts/fleet/interfaces.workers.d.ts +6 -0
- package/dist_ts/fleet/interfaces.workers.js +65 -2
- package/dist_ts/functions.fleetworkercli.d.ts +16 -3
- package/dist_ts/functions.fleetworkercli.js +21 -7
- package/dist_ts/functions.inferencecli.js +4 -2
- package/dist_ts/functions.mcptickettools.d.ts +1 -0
- package/dist_ts/functions.ticketvalidation.d.ts +36 -3
- package/dist_ts/functions.ticketvalidation.js +45 -14
- package/dist_ts/interfaces.auth.d.ts +1 -1
- package/dist_ts/interfaces.auth.js +2 -1
- package/dist_ts_authswitch/authority-contract.d.ts +23 -0
- package/dist_ts_authswitch/authority-contract.js +1 -1
- package/dist_ts_authswitch/classes.authoritybroker.d.ts +12 -0
- package/dist_ts_authswitch/classes.authoritybroker.js +72 -22
- package/dist_ts_authswitch/classes.authoritycli.d.ts +1 -0
- package/dist_ts_authswitch/classes.authoritycli.js +26 -1
- package/dist_ts_authswitch/classes.authorityclient.d.ts +3 -1
- package/dist_ts_authswitch/classes.authorityclient.js +10 -4
- package/dist_ts_authswitch/classes.authoritydaemon.js +7 -1
- package/dist_ts_authswitch/classes.authorityimport.js +2 -2
- package/dist_ts_authswitch/classes.authoritymodels.d.ts +3 -0
- package/dist_ts_authswitch/classes.authoritymodels.js +10 -3
- package/dist_ts_authswitch/classes.authorityusage.js +5 -3
- package/dist_ts_authswitch/classes.claudeauthority.js +2 -2
- package/dist_ts_authswitch/testsupport.js +4 -4
- package/dist_ts_authswitch/ts_migration/0012_account_claude_code.d.ts +11 -0
- package/dist_ts_authswitch/ts_migration/0012_account_claude_code.js +18 -0
- package/dist_ts_authswitch/ts_migration/index.js +3 -1
- package/dist_ts_interfaces/deployments.d.ts +104 -0
- package/dist_ts_interfaces/deployments.js +8 -0
- package/dist_ts_interfaces/fleet.d.ts +2 -0
- package/dist_ts_interfaces/index.d.ts +1 -0
- package/dist_ts_interfaces/index.js +2 -1
- package/dist_ts_interfaces/interfaces.d.ts +1 -1
- package/dist_ts_interfaces/interfaces.js +1 -1
- package/dist_ts_interfaces/requests.accounts.d.ts +18 -0
- package/dist_ts_interfaces/requests.accounts.js +1 -1
- package/dist_ts_interfaces/requests.d.ts +2 -2
- package/dist_ts_interfaces/requests.js +1 -1
- package/dist_ts_interfaces/tickets.d.ts +53 -4
- package/dist_ts_interfaces/tickets.js +6 -2
- package/dist_ts_migration/index.d.ts +1 -0
- package/dist_ts_migration/index.js +2 -1
- package/dist_ts_migration/v41_ticketmigrationmarkers.d.ts +19 -0
- package/dist_ts_migration/v41_ticketmigrationmarkers.js +85 -0
- package/docs/authswitch.md +9 -1
- package/package.json +5 -5
- package/readme.md +48 -12
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/aglapi/aglapisurface.document.ts +123 -9
- package/ts/classes.accountsactions.ts +24 -0
- package/ts/classes.authstore.ts +8 -0
- package/ts/classes.cli.ts +17 -54
- package/ts/classes.configurationauthority.ts +1 -1
- package/ts/classes.controllerservice.ts +49 -6
- package/ts/classes.flexservice.ts +7 -2
- package/ts/classes.inferencegateway.ts +133 -19
- package/ts/classes.inferencerouter.ts +20 -2
- package/ts/classes.inferenceservice.ts +6 -3
- package/ts/classes.ticketmodels.ts +3 -2
- package/ts/classes.ticketnotifier.ts +8 -1
- package/ts/classes.ticketstore.ts +8 -4
- package/ts/classes.upgradecoordinator.ts +14 -4
- package/ts/classes.upgradetransaction.ts +118 -0
- package/ts/configuration/functions.master.ts +6 -3
- package/ts/driver/classes.drivereventrelay.ts +2 -2
- package/ts/fleet/classes.boardpump.ts +7 -4
- package/ts/fleet/classes.claudeworkers.ts +16 -6
- package/ts/fleet/classes.deploymodels.ts +44 -0
- package/ts/fleet/functions.validation.ts +50 -2
- package/ts/fleet/interfaces.workers.ts +56 -1
- package/ts/functions.fleetworkercli.ts +26 -8
- package/ts/functions.inferencecli.ts +3 -1
- package/ts/functions.ticketvalidation.ts +48 -13
- package/ts/interfaces.auth.ts +1 -0
- package/ts_authswitch/authority-contract.ts +18 -0
- package/ts_authswitch/classes.authoritybroker.ts +67 -19
- package/ts_authswitch/classes.authoritycli.ts +29 -0
- package/ts_authswitch/classes.authorityclient.ts +12 -4
- package/ts_authswitch/classes.authoritydaemon.ts +8 -1
- package/ts_authswitch/classes.authorityimport.ts +1 -1
- package/ts_authswitch/classes.authoritymodels.ts +5 -1
- package/ts_authswitch/classes.authorityusage.ts +5 -2
- package/ts_authswitch/classes.claudeauthority.ts +1 -1
- package/ts_authswitch/native-contracts.md +10 -0
- package/ts_authswitch/testsupport.ts +3 -3
- package/ts_authswitch/ts_migration/0012_account_claude_code.ts +17 -0
- package/ts_authswitch/ts_migration/index.ts +2 -0
- package/ts_interfaces/deployments.ts +98 -0
- package/ts_interfaces/fleet.ts +2 -0
- package/ts_interfaces/index.ts +1 -0
- package/ts_interfaces/interfaces.ts +1 -1
- package/ts_interfaces/requests.accounts.ts +16 -0
- package/ts_interfaces/requests.ts +2 -0
- package/ts_interfaces/tickets.ts +34 -4
- package/ts_migration/index.ts +1 -0
- package/ts_migration/v41_ticketmigrationmarkers.ts +28 -0
- package/ts_web/00_commitinfo_data.ts +1 -1
- package/ts_web/classes.accountsreadmodel.ts +12 -1
- package/ts_web/elements.accounts.ts +44 -0
- package/ts_web/elements.ticketboard.ts +15 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as plugins from './plugins.js';
|
|
2
|
-
import { inferenceCredentials, inferencePlanPattern, inferenceProviders, type TInferenceCredential,
|
|
2
|
+
import { aglProxyConfinedClient, inferenceCredentials, inferencePlanPattern, inferenceProviders, type TInferenceCredential,
|
|
3
3
|
type TInferenceProvider } from './functions.proxycontract.js';
|
|
4
4
|
import { InferenceRouter, InferenceRoutingError, inferenceSpendLimitText, type IInferenceAccountUnavailable,
|
|
5
5
|
type IInferenceClaudeRoute, type IInferenceRoute, type IInferenceRoutingPolicy,
|
|
@@ -40,8 +40,34 @@ export interface IInferenceGatewayRequestFailure {
|
|
|
40
40
|
code: string;
|
|
41
41
|
protocol?: TProtocol;
|
|
42
42
|
error?: string;
|
|
43
|
+
/**
|
|
44
|
+
* For a request refused before its client was known (AGL-304): the path it asked for, without its query, and the
|
|
45
|
+
* client's own name for itself (`user-agent`), each printable ASCII and cut short (`refusalText`), so a looping
|
|
46
|
+
* client can be found from the journal. Never a header value beyond those, never the credential.
|
|
47
|
+
*/
|
|
48
|
+
path?: string;
|
|
49
|
+
userAgent?: string;
|
|
50
|
+
/** For a 401: what the request presented (`TInferenceGatewayCredentialRefusal`). */
|
|
51
|
+
credential?: TInferenceGatewayCredentialRefusal;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What a refused request presented as its gateway credential: none, one the gateway cannot read (not a bearer token or
|
|
56
|
+
* `x-api-key`, or over 512 characters), one no client holds, or one of a client of the other listener.
|
|
57
|
+
*/
|
|
58
|
+
export type TInferenceGatewayCredentialRefusal = 'missing' | 'malformed' | 'unknown' | 'other_listener';
|
|
59
|
+
|
|
60
|
+
/** A 401 that says what the request presented (`TInferenceGatewayCredentialRefusal`); its answer stays the same. */
|
|
61
|
+
class InferenceCredentialRefusal extends InferenceRoutingError {
|
|
62
|
+
constructor(public readonly credential: TInferenceGatewayCredentialRefusal) {
|
|
63
|
+
super(401, 'authentication_error', 'The gateway credential is invalid or expired.');
|
|
64
|
+
}
|
|
43
65
|
}
|
|
44
66
|
|
|
67
|
+
/** A header value or path as a refusal line may carry it: printable ASCII only, at most `maximumArg` characters. */
|
|
68
|
+
const refusalText = (valueArg: string, maximumArg: number): string =>
|
|
69
|
+
valueArg.replace(/[^\x20-\x7e]/gu, '?').replace(/"/gu, '\'').slice(0, maximumArg);
|
|
70
|
+
|
|
45
71
|
export interface IInferenceGatewayOptions {
|
|
46
72
|
router: InferenceRouter;
|
|
47
73
|
signatureKey: Uint8Array;
|
|
@@ -110,16 +136,61 @@ export interface IInferenceGatewayStreamCut {
|
|
|
110
136
|
error: string;
|
|
111
137
|
}
|
|
112
138
|
|
|
139
|
+
/**
|
|
140
|
+
* The highest `sequence_number` a Responses stream relayed so far, read across chunk boundaries (AGL-375): a
|
|
141
|
+
* `response.failed` the gateway ends a cut stream with continues the numbering, as the AI SDK's chunk schema, which
|
|
142
|
+
* Flex validates every event against, requires the number on it.
|
|
143
|
+
*/
|
|
144
|
+
class ResponsesSequence {
|
|
145
|
+
private readonly decoder = new TextDecoder();
|
|
146
|
+
/** The end of the text read so far, so a number split between two chunks is still read whole. */
|
|
147
|
+
private tail = '';
|
|
148
|
+
private highest = -1;
|
|
149
|
+
|
|
150
|
+
public feed(chunkArg: Uint8Array): void {
|
|
151
|
+
const text = this.tail + this.decoder.decode(chunkArg, { stream: true });
|
|
152
|
+
for (const match of text.matchAll(/"sequence_number"\s*:\s*(\d{1,15})/gu)) {
|
|
153
|
+
this.highest = Math.max(this.highest, Number(match[1]));
|
|
154
|
+
}
|
|
155
|
+
this.tail = text.slice(-40);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** The number the next event takes: one past the highest relayed, 0 when none was. */
|
|
159
|
+
public get next(): number { return this.highest + 1; }
|
|
160
|
+
}
|
|
161
|
+
|
|
113
162
|
const writeStreamCut = (cutArg: IInferenceGatewayStreamCut): void => {
|
|
114
163
|
process.stderr.write(`AGL proxy cut a ${cutArg.protocol} stream of account ${cutArg.account} after ${cutArg.bytes} bytes: `
|
|
115
164
|
+ `${cutArg.cause} (${cutArg.error})\n`);
|
|
116
165
|
};
|
|
117
166
|
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
167
|
+
const refusalRepeatWindowMs = 60_000;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The proxy's journal line per refusal, the same line at most once a minute (AGL-304): a client that repeats a refused
|
|
171
|
+
* request in a loop gets its line once, and the next one after the minute says how many like it the minute held.
|
|
172
|
+
*/
|
|
173
|
+
export const inferenceRequestFailureWriter = (writeArg: (textArg: string) => void = (textArg) => { process.stderr.write(textArg); },
|
|
174
|
+
nowArg: () => number = Date.now): ((failureArg: IInferenceGatewayRequestFailure) => void) => {
|
|
175
|
+
/** Each line's last write and the like ones since. */
|
|
176
|
+
const written = new Map<string, { at: number; since: number }>();
|
|
177
|
+
return (failureArg) => {
|
|
178
|
+
const line = `AGL proxy refused ${failureArg.protocol ?? 'http'} request: HTTP ${failureArg.status} ${failureArg.code}`
|
|
179
|
+
+ `${failureArg.error ? ` (${failureArg.error})` : ''}`
|
|
180
|
+
+ `${failureArg.path === undefined ? '' : ` for ${failureArg.path}`}`
|
|
181
|
+
+ `${failureArg.userAgent === undefined ? '' : ` from "${failureArg.userAgent}"`}`
|
|
182
|
+
+ `${failureArg.credential === undefined ? '' : `, credential ${failureArg.credential}`}`;
|
|
183
|
+
const now = nowArg();
|
|
184
|
+
const last = written.get(line);
|
|
185
|
+
if (last && now - last.at < refusalRepeatWindowMs) { last.since += 1; return; }
|
|
186
|
+
if (written.size >= 256) for (const [key, entry] of written) if (now - entry.at >= refusalRepeatWindowMs) written.delete(key);
|
|
187
|
+
written.set(line, { at: now, since: 0 });
|
|
188
|
+
writeArg(`${line}${last?.since ? ` (and ${last.since} more like it in the minute before)` : ''}\n`);
|
|
189
|
+
};
|
|
121
190
|
};
|
|
122
191
|
|
|
192
|
+
const writeRequestFailure = inferenceRequestFailureWriter();
|
|
193
|
+
|
|
123
194
|
/** A failure's class and system code; never its message, which can quote what the provider sent. */
|
|
124
195
|
const describeStreamFailure = (errorArg: unknown): string => {
|
|
125
196
|
if (errorArg instanceof InferenceOutputError) return `${errorArg.name} ${errorArg.providerCode ?? errorArg.message}`;
|
|
@@ -207,6 +278,20 @@ interface IClient {
|
|
|
207
278
|
/** Codex's Responses, Messages answered by a ChatGPT pool, or Claude Code's own Messages sent to a Claude pool. */
|
|
208
279
|
type TProtocol = 'responses' | 'messages' | 'claude';
|
|
209
280
|
|
|
281
|
+
/**
|
|
282
|
+
* The clients AGL runs Claude Code on: `agl proxy credential claude` and `agl proxy run claude`, the controller's own
|
|
283
|
+
* Claude Code sessions, and confined runs (`aglProxyConfinedClient`).
|
|
284
|
+
*/
|
|
285
|
+
const claudeCodeSessionScopes = new Set(['client:claude', `client:${aglProxyConfinedClient}`]);
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Whether a Claude pool's request comes from Claude Code (AGL-1146): its client is one AGL runs Claude Code on, or the
|
|
289
|
+
* request names Claude Code itself (`user-agent: claude-cli/…`, as Claude Code and the Agent SDK send it). The header
|
|
290
|
+
* can only narrow the accounts a request may use, never widen them.
|
|
291
|
+
*/
|
|
292
|
+
export const inferenceClaudeCodeRequest = (sessionScopeArg: string, headersArg: Headers): boolean =>
|
|
293
|
+
claudeCodeSessionScopes.has(sessionScopeArg) || /^claude-cli\//i.test(headersArg.get('user-agent') ?? '');
|
|
294
|
+
|
|
210
295
|
/** Claude Code's request headers sent on to Anthropic beside the body; its credential never is. */
|
|
211
296
|
const forwardedClaudeRequestHeaders = new Set(['accept', 'anthropic-version', 'anthropic-beta', 'user-agent', 'x-app',
|
|
212
297
|
'x-claude-code-session-id']);
|
|
@@ -352,6 +437,9 @@ const readErrorBody = async (responseArg: Response): Promise<string | undefined>
|
|
|
352
437
|
} catch { return undefined; }
|
|
353
438
|
};
|
|
354
439
|
|
|
440
|
+
/** The phrase of Anthropic's 400 for an API key whose organization has no credit left (`inferencePaymentCause`). */
|
|
441
|
+
const anthropicCreditBalanceTooLow = /credit balance is too low/i;
|
|
442
|
+
|
|
355
443
|
/** OpenAI's `error.code` values of a 429 that no retry heals: no prepaid credit left, or a spend or usage limit reached. */
|
|
356
444
|
const openAiQuotaCodes = new Set(['credit_balance_exhausted', 'organization_spend_limit_exceeded',
|
|
357
445
|
'project_spend_limit_exceeded', 'organization_usage_limit_exceeded']);
|
|
@@ -367,8 +455,11 @@ const openAiQuotaCodes = new Set(['credit_balance_exhausted', 'organization_spen
|
|
|
367
455
|
* with HTTP 429 whose `error.code` names the cause (`credit_balance_exhausted`, `organization_spend_limit_exceeded`,
|
|
368
456
|
* `project_spend_limit_exceeded`, `organization_usage_limit_exceeded`) and whose `error.type` can be
|
|
369
457
|
* `insufficient_quota`; retrying does not help (https://developers.openai.com/api/docs/guides/error-codes). A Claude
|
|
370
|
-
* account's subscription states only the billing error
|
|
371
|
-
*
|
|
458
|
+
* account's subscription states only the billing error. Undocumented but observed (2026-10-29, AGL-1147): Anthropic
|
|
459
|
+
* refuses every request on an API key whose organization has no credit left with HTTP 400 `invalid_request_error`
|
|
460
|
+
* whose message says the credit balance is too low (`anthropicCreditBalanceTooLow`, the phrase Claude Code itself reads as
|
|
461
|
+
* a billing error); that is a billing problem of the key. Any other 400, a spend limit set in the Claude Console among
|
|
462
|
+
* them, is not read as one.
|
|
372
463
|
*/
|
|
373
464
|
export const inferencePaymentCause = (providerArg: TInferenceProvider, credentialArg: TInferenceCredential,
|
|
374
465
|
statusArg: number, bodyArg: unknown): TInferencePaymentCause | undefined => {
|
|
@@ -376,6 +467,8 @@ export const inferencePaymentCause = (providerArg: TInferenceProvider, credentia
|
|
|
376
467
|
if (!error) return undefined;
|
|
377
468
|
if (providerArg === 'anthropic') {
|
|
378
469
|
if (statusArg === 402 && error.type === 'billing_error') return 'billing';
|
|
470
|
+
if (credentialArg === 'api_key' && statusArg === 400 && error.type === 'invalid_request_error'
|
|
471
|
+
&& typeof error.message === 'string' && anthropicCreditBalanceTooLow.test(error.message)) return 'billing';
|
|
379
472
|
if (credentialArg === 'api_key' && statusArg === 429 && error.type === 'rate_limit_error' && record(error.details)
|
|
380
473
|
&& error.details.error_code === 'enforced_spend_limit_reached') return 'spend_cap';
|
|
381
474
|
return undefined;
|
|
@@ -389,10 +482,12 @@ export const inferencePaymentCause = (providerArg: TInferenceProvider, credentia
|
|
|
389
482
|
|
|
390
483
|
/**
|
|
391
484
|
* Whether a provider answer on the route may state a payment problem of its account (`inferencePaymentCause`), so the
|
|
392
|
-
* gateway reads its body before a byte reaches the client: a Claude account's 402, an API key's
|
|
485
|
+
* gateway reads its body before a byte reaches the client: a Claude account's 402, an API key's 429, an Anthropic API
|
|
486
|
+
* key's 400 or 402.
|
|
393
487
|
*/
|
|
394
488
|
const mayStatePaymentProblem = (routeArg: IInferenceRoute | IInferenceClaudeRoute, statusArg: number): boolean =>
|
|
395
|
-
routeArg.credential === 'api_key'
|
|
489
|
+
routeArg.credential === 'api_key'
|
|
490
|
+
? statusArg === 429 || (routeArg.provider === 'anthropic' && (statusArg === 400 || statusArg === 402))
|
|
396
491
|
: statusArg === 402 && routeArg.provider === 'anthropic';
|
|
397
492
|
|
|
398
493
|
/**
|
|
@@ -703,10 +798,15 @@ export class InferenceGateway {
|
|
|
703
798
|
const token = authorization?.startsWith('Bearer ') ? authorization.slice(7) : requestArg.headers.get('x-api-key');
|
|
704
799
|
const presented = token && token.length <= 512 ? digest(token) : undefined;
|
|
705
800
|
let client: IClient | undefined;
|
|
801
|
+
let otherListener = false;
|
|
706
802
|
if (presented) for (const candidate of this.clients) {
|
|
707
|
-
if (plugins.crypto.timingSafeEqual(candidate.digest, presented)
|
|
803
|
+
if (!plugins.crypto.timingSafeEqual(candidate.digest, presented)) continue;
|
|
804
|
+
if (candidate.route === routeArg) client = candidate; else otherListener = true;
|
|
805
|
+
}
|
|
806
|
+
if (!client) {
|
|
807
|
+
throw new InferenceCredentialRefusal(otherListener ? 'other_listener' : presented ? 'unknown'
|
|
808
|
+
: authorization === null && !requestArg.headers.has('x-api-key') ? 'missing' : 'malformed');
|
|
708
809
|
}
|
|
709
|
-
if (!client) throw new InferenceRoutingError(401, 'authentication_error', 'The gateway credential is invalid or expired.');
|
|
710
810
|
return client;
|
|
711
811
|
}
|
|
712
812
|
|
|
@@ -743,6 +843,9 @@ export class InferenceGateway {
|
|
|
743
843
|
const pathname = url.pathname.replace(/^\/(openai|anthropic)(?=\/)/, '');
|
|
744
844
|
// A Claude pool serves Claude Code's own Messages requests; a ChatGPT pool serves Responses and translated Messages.
|
|
745
845
|
const claude = client.policy.provider === 'anthropic';
|
|
846
|
+
// A Claude Code request is never placed on an API key that may not serve Claude Code (AGL-1146).
|
|
847
|
+
const policy: IInferenceRoutingPolicy = claude && inferenceClaudeCodeRequest(client.sessionScope, request.headers)
|
|
848
|
+
? { ...client.policy, claudeCode: true } : client.policy;
|
|
746
849
|
if (claude) {
|
|
747
850
|
protocol = 'claude';
|
|
748
851
|
if (!anthropic) throw new InferenceRoutingError(404, 'not_found',
|
|
@@ -750,7 +853,7 @@ export class InferenceGateway {
|
|
|
750
853
|
}
|
|
751
854
|
if (request.method === 'GET' && pathname === '/v1/models') {
|
|
752
855
|
if (claude) {
|
|
753
|
-
const models = await this.options.router.claudeModels(
|
|
856
|
+
const models = await this.options.router.claudeModels(policy, signal);
|
|
754
857
|
return Response.json({
|
|
755
858
|
data: models.map((model) => ({ type: 'model', id: model.modelId, display_name: model.displayName,
|
|
756
859
|
created_at: model.createdAt ?? '1970-01-01T00:00:00Z',
|
|
@@ -807,7 +910,7 @@ export class InferenceGateway {
|
|
|
807
910
|
// Of the query, only Claude Code's `beta=true` marker travels upstream.
|
|
808
911
|
const claudeSearch = url.searchParams.get('beta') === 'true' ? '?beta=true' : '';
|
|
809
912
|
if (claude && pathname === '/v1/messages/count_tokens') {
|
|
810
|
-
return await this.claudeCountTokens(admitted, sessionKey, model, bytes, claudeSearch, request.headers, signal,
|
|
913
|
+
return await this.claudeCountTokens(admitted, policy, sessionKey, model, bytes, claudeSearch, request.headers, signal,
|
|
811
914
|
(routeArg) => { releaseRoute = routeArg.release; });
|
|
812
915
|
}
|
|
813
916
|
// What of the request has no price: an API key with a monthly spend limit takes no such request. Only a Responses
|
|
@@ -835,7 +938,7 @@ export class InferenceGateway {
|
|
|
835
938
|
let response: Response;
|
|
836
939
|
let quotaError: InferenceRoutingError | undefined;
|
|
837
940
|
const acquire = (accountIdArg: string | undefined): Promise<IInferenceRoute | IInferenceClaudeRoute> => claude
|
|
838
|
-
? this.options.router.acquireClaude(model,
|
|
941
|
+
? this.options.router.acquireClaude(model, policy, signal, { exclude: excluded, excludedBecause,
|
|
839
942
|
...(unpriced === undefined ? {} : { unpriced }),
|
|
840
943
|
// A Claude session carries no state bound to its account: one placed on an API key returns to a Claude account
|
|
841
944
|
// as soon as one can take its request.
|
|
@@ -1028,8 +1131,13 @@ export class InferenceGateway {
|
|
|
1028
1131
|
const error = errorArg instanceof InferenceRoutingError ? errorArg
|
|
1029
1132
|
: new InferenceRoutingError(503, 'gateway_unavailable', 'The gateway could not complete the request. Check the account service and provider availability.');
|
|
1030
1133
|
if (!requestArg.signal.aborted && !client?.abort.signal.aborted && !this.abort.signal.aborted) {
|
|
1134
|
+
// A request refused before its client is known names what it asked for and what it presented (AGL-304).
|
|
1135
|
+
const userAgent = requestArg.headers.get('user-agent');
|
|
1031
1136
|
this.reportRequestFailure({ status: error.status, code: error.code, protocol,
|
|
1032
|
-
...(errorArg instanceof InferenceRoutingError ? {} : { error: describeStreamFailure(errorArg) })
|
|
1137
|
+
...(errorArg instanceof InferenceRoutingError ? {} : { error: describeStreamFailure(errorArg) }),
|
|
1138
|
+
...(client === undefined ? { path: refusalText(new URL(requestArg.url).pathname, 128),
|
|
1139
|
+
...(userAgent === null ? {} : { userAgent: refusalText(userAgent, 96) }) } : {}),
|
|
1140
|
+
...(errorArg instanceof InferenceCredentialRefusal ? { credential: errorArg.credential } : {}) });
|
|
1033
1141
|
}
|
|
1034
1142
|
return errorResponse(error, protocol);
|
|
1035
1143
|
} finally {
|
|
@@ -1198,19 +1306,20 @@ export class InferenceGateway {
|
|
|
1198
1306
|
* limit, or by any account of the pool when the session has none there or its account takes no request for a reason
|
|
1199
1307
|
* of its own, and it never moves the session.
|
|
1200
1308
|
*/
|
|
1201
|
-
private async claudeCountTokens(clientArg: IClient,
|
|
1309
|
+
private async claudeCountTokens(clientArg: IClient, policyArg: IInferenceRoutingPolicy, sessionKeyArg: string,
|
|
1310
|
+
modelArg: string,
|
|
1202
1311
|
bytesArg: Uint8Array<ArrayBuffer>, searchArg: string, headersArg: Headers, signalArg: AbortSignal,
|
|
1203
1312
|
holdArg: (routeArg: IInferenceClaudeRoute) => void): Promise<Response> {
|
|
1204
1313
|
const binding = await this.options.affinity.session(sessionKeyArg, signalArg);
|
|
1205
1314
|
let route: IInferenceClaudeRoute;
|
|
1206
1315
|
try {
|
|
1207
|
-
route = await this.options.router.acquireClaude(modelArg,
|
|
1316
|
+
route = await this.options.router.acquireClaude(modelArg, policyArg, signalArg,
|
|
1208
1317
|
{ ignoreLimits: true, ...(binding ? { accountId: binding.accountId } : {}) });
|
|
1209
1318
|
} catch (errorArg) {
|
|
1210
1319
|
if (!binding || !(sessionAccountOutsidePool(errorArg) || sessionAccountUnavailable(errorArg, binding.accountId))) {
|
|
1211
1320
|
throw errorArg;
|
|
1212
1321
|
}
|
|
1213
|
-
route = await this.options.router.acquireClaude(modelArg,
|
|
1322
|
+
route = await this.options.router.acquireClaude(modelArg, policyArg, signalArg, { ignoreLimits: true });
|
|
1214
1323
|
}
|
|
1215
1324
|
holdArg(route);
|
|
1216
1325
|
const response = await this.claudeMessages(route, '/v1/messages/count_tokens', searchArg, headersArg, bytesArg, signalArg);
|
|
@@ -1311,6 +1420,8 @@ export class InferenceGateway {
|
|
|
1311
1420
|
/** The provider's body was read to its end. */
|
|
1312
1421
|
let whole = false;
|
|
1313
1422
|
let relayed = 0;
|
|
1423
|
+
// A cut Responses stream ends in a response.failed numbered after what the client already has (AGL-375).
|
|
1424
|
+
const sequence = streamArg.responsesSse ? new ResponsesSequence() : undefined;
|
|
1314
1425
|
const report = this.options.reportStreamCut ?? writeStreamCut;
|
|
1315
1426
|
const failure = (errorArg: unknown): InferenceRoutingError => {
|
|
1316
1427
|
const cause = errorArg instanceof InferenceOutputError ? errorArg.code : 'provider_stream_failed';
|
|
@@ -1363,12 +1474,15 @@ export class InferenceGateway {
|
|
|
1363
1474
|
// the listener's abort once that request is collected.
|
|
1364
1475
|
if (cancelled) return;
|
|
1365
1476
|
if (result.done) { whole = true; controllerArg.close(); finish(); }
|
|
1366
|
-
else {
|
|
1477
|
+
else {
|
|
1478
|
+
relayed += result.value.byteLength; controllerArg.enqueue(result.value); tap?.chunk(result.value); sequence?.feed(result.value);
|
|
1479
|
+
}
|
|
1367
1480
|
} catch (errorArg) {
|
|
1368
1481
|
const error = failure(errorArg);
|
|
1369
1482
|
if (streamArg.responsesSse && !signalArg.aborted && !cancelled) {
|
|
1370
1483
|
controllerArg.enqueue(new TextEncoder().encode(`\n\nevent: response.failed\ndata: ${JSON.stringify({
|
|
1371
|
-
type: 'response.failed',
|
|
1484
|
+
type: 'response.failed', sequence_number: sequence?.next ?? 0,
|
|
1485
|
+
response: { status: 'failed', error: { code: error.code, message: error.message } },
|
|
1372
1486
|
})}\n\n`));
|
|
1373
1487
|
controllerArg.close();
|
|
1374
1488
|
} else {
|
|
@@ -84,6 +84,11 @@ export interface IInferenceAccount {
|
|
|
84
84
|
* a limit may not (`spend_unread`). A spend reading carries the limit the key is judged against.
|
|
85
85
|
*/
|
|
86
86
|
spendLimitMicroUsd: number | null;
|
|
87
|
+
/**
|
|
88
|
+
* Whether the account may serve Claude Code (`IAuthSwitchAccount.claudeCode`): a key set to `false` takes no request
|
|
89
|
+
* of a policy with `claudeCode`, and every other request as before. Always `true` for a sign-in.
|
|
90
|
+
*/
|
|
91
|
+
claudeCode: boolean;
|
|
87
92
|
/**
|
|
88
93
|
* The login the account's requests are served on (the account service's gateway login of it), as the pool was read;
|
|
89
94
|
* `null` when the account service names none. An API key's spend is reported on it.
|
|
@@ -103,6 +108,12 @@ export interface IInferenceRoutingPolicy {
|
|
|
103
108
|
credential?: TInferenceCredential;
|
|
104
109
|
/** Omitted means all eligible authority-owned accounts of the provider. */
|
|
105
110
|
accountIds?: readonly string[];
|
|
111
|
+
/**
|
|
112
|
+
* The request comes from Claude Code (`inferenceClaudeCodeRequest`, AGL-1146): an API key that may not serve Claude
|
|
113
|
+
* Code (`IInferenceAccount.claudeCode`) is outside the pool, so a session on one moves. The gateway sets it per
|
|
114
|
+
* request; it is never part of a lease.
|
|
115
|
+
*/
|
|
116
|
+
claudeCode?: boolean;
|
|
106
117
|
/** Exact current provider plan names; omission accepts every entitled plan. A key has no plan, so this leaves keys out. */
|
|
107
118
|
plans?: readonly string[];
|
|
108
119
|
}
|
|
@@ -700,6 +711,11 @@ export interface IInferenceAccountStatus {
|
|
|
700
711
|
placementRank: number | null;
|
|
701
712
|
/** The account lapses within `lapseHorizonMs`: it takes a new session only while no other account can. */
|
|
702
713
|
lastResort: boolean;
|
|
714
|
+
/**
|
|
715
|
+
* Whether the account may serve Claude Code (`IInferenceAccount.claudeCode`); a key that may not still takes every
|
|
716
|
+
* other client's requests at its `placementRank`. A proxy of an earlier AGL reports none.
|
|
717
|
+
*/
|
|
718
|
+
claudeCode?: boolean;
|
|
703
719
|
}
|
|
704
720
|
|
|
705
721
|
/** One model entry of a ChatGPT account's catalog exactly as the Codex backend states it. */
|
|
@@ -978,8 +994,10 @@ export class InferenceRouter {
|
|
|
978
994
|
const provider = policyArg.provider ?? 'openai';
|
|
979
995
|
const tiers = this.tiers(policyArg);
|
|
980
996
|
const now = this.now();
|
|
997
|
+
// A Claude Code request's pool holds no API key that may not serve Claude Code: for it the key left the pool.
|
|
981
998
|
const inPool = (accountArg: IInferenceAccount) => accountArg.providerId === provider
|
|
982
|
-
&& tiers.includes(accountArg.credential)
|
|
999
|
+
&& tiers.includes(accountArg.credential)
|
|
1000
|
+
&& !(policyArg.claudeCode && accountArg.credential === 'api_key' && !accountArg.claudeCode);
|
|
983
1001
|
const lapsed = new Set(everyAccount.filter((account) => inPool(account)
|
|
984
1002
|
&& plugins.authswitchPlacement.authSwitchAccountLifecycle(account.lapsesAt, now) === 'lapsed').map((account) => account.id));
|
|
985
1003
|
const allAccounts = everyAccount.filter((account) => inPool(account) && !lapsed.has(account.id));
|
|
@@ -1937,7 +1955,7 @@ export class InferenceRouter {
|
|
|
1937
1955
|
const base = { id: account.id, label: account.label, provider: account.providerId, credential: account.credential,
|
|
1938
1956
|
maxUsagePercent: account.maxUsagePercent, reserveLiftUntil: account.reserveLiftUntil, inFlight: this.active.get(account.id) ?? 0,
|
|
1939
1957
|
lifecycle: placement.lifecycle, lapsesAt: account.lapsesAt, placementScore: placement.score, placementRank: null,
|
|
1940
|
-
lastResort: placement.eligibility === 'last_resort',
|
|
1958
|
+
lastResort: placement.eligibility === 'last_resort', claudeCode: account.claudeCode,
|
|
1941
1959
|
readingSource: held?.observation ? 'headers' as const : held?.reading ? 'poll' as const : null,
|
|
1942
1960
|
readingObservedAt: held?.reading ? held.observedAt : null,
|
|
1943
1961
|
readingReadAt: held?.reading ? held.readAt : null,
|
|
@@ -154,7 +154,7 @@ export class InferenceService {
|
|
|
154
154
|
const login = findAuthorityGatewayLogin(projection, account.id);
|
|
155
155
|
return pool ? [{ id: account.id, label: account.label, revision: String(account.revision), providerId: pool.provider,
|
|
156
156
|
credential: pool.credential, maxUsagePercent: account.maxUsagePercent, reserveLiftUntil: account.reserveLiftUntil, lapsesAt: account.lapsesAt,
|
|
157
|
-
spendLimitMicroUsd: account.spendLimitMicroUsd, loginId: typeof login === 'string' ? null : login.id }] : [];
|
|
157
|
+
spendLimitMicroUsd: account.spendLimitMicroUsd, claudeCode: account.claudeCode, loginId: typeof login === 'string' ? null : login.id }] : [];
|
|
158
158
|
});
|
|
159
159
|
}
|
|
160
160
|
|
|
@@ -219,8 +219,11 @@ export class InferenceService {
|
|
|
219
219
|
.map(async (accountArg) => [accountArg.id, await this.usage.current(accountArg.id, signalArg)] as const)));
|
|
220
220
|
const accounts = this.router.accountStatus(pool, (accountIdArg) => readings.get(accountIdArg));
|
|
221
221
|
const recent = await this.options.placements.recent(recentPlacementCount, signalArg);
|
|
222
|
-
|
|
223
|
-
|
|
222
|
+
// Claude's next new session is Claude Code's: the best ranked account that may serve it (AGL-1146).
|
|
223
|
+
const next = (providerArg: TInferenceProvider): string | null => accounts
|
|
224
|
+
.filter((accountArg) => accountArg.provider === providerArg && accountArg.placementRank !== null
|
|
225
|
+
&& (providerArg !== 'anthropic' || accountArg.claudeCode !== false))
|
|
226
|
+
.sort((left, right) => left.placementRank! - right.placementRank!)[0]?.id ?? null;
|
|
224
227
|
return { ...this.gateway.status(), baseUrl: this.baseUrl ?? '', usageObservations: this.observer.status(),
|
|
225
228
|
authority: this.ready && this.authority.getProjection().state === 'ready' ? 'ready' : 'unavailable',
|
|
226
229
|
accounts,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as plugins from './plugins.js';
|
|
2
2
|
import type { TTicketData, ITicketComment, ITicketActivity, ITicketQuestion, TTicketFleetEvent, ITicketFleetEventResult } from '../ts_interfaces/tickets.js';
|
|
3
|
-
import { ticketSchema, ticketCommentSchema, ticketActivitySchema, ticketQuestionSchema, ticketFleetEventSchema } from './functions.ticketvalidation.js';
|
|
3
|
+
import { ticketSchema, ticketCommentSchema, ticketActivitySchema, ticketQuestionSchema, ticketFleetEventSchema, ticketReleaseLink } from './functions.ticketvalidation.js';
|
|
4
4
|
|
|
5
5
|
export interface ITicketDocument { id: string; ticket: TTicketData; }
|
|
6
6
|
export interface ITicketBoardDocument { id: string; sequence: number; revision: number; }
|
|
@@ -49,7 +49,8 @@ const assertFleetEventDocument = (valueArg: unknown): void => {
|
|
|
49
49
|
}
|
|
50
50
|
const expectedLink = value.event.kind === 'landed'
|
|
51
51
|
? { kind: 'branch', repo: value.event.repo, branch: value.event.branch, sha: value.event.sha }
|
|
52
|
-
: value.event.kind === 'released' ?
|
|
52
|
+
: value.event.kind === 'released' ? ticketReleaseLink(value.event)
|
|
53
|
+
: undefined;
|
|
53
54
|
const body = typeof value.event.body === 'string' ? [{ type: 'text', text: value.event.body }] : value.event.body;
|
|
54
55
|
if (!plugins.util.isDeepStrictEqual(value.result.ticket.updatedBy, value.event.actor)
|
|
55
56
|
|| (expectedLink && (value.result.ticket.columnId !== value.event.kind
|
|
@@ -37,7 +37,8 @@ const personName = (personArg: ITicketPerson | undefined, recipientArg?: string)
|
|
|
37
37
|
);
|
|
38
38
|
const linkText = (linkArg: TTicketLink): string => (
|
|
39
39
|
linkArg.kind === 'branch' ? `branch ${linkArg.repo} ${linkArg.branch} at ${linkArg.sha.slice(0, 12)}`
|
|
40
|
-
: linkArg.kind === 'commit' ? `commit ${linkArg.repo} ${linkArg.sha.slice(0, 12)}`
|
|
40
|
+
: linkArg.kind === 'commit' ? `commit ${linkArg.repo} ${linkArg.sha.slice(0, 12)}`
|
|
41
|
+
: linkArg.kind === 'affects' ? `found in ${linkArg.repo}@${linkArg.version}` : `release ${linkArg.package ?? linkArg.repo}@${linkArg.version}`
|
|
41
42
|
);
|
|
42
43
|
const fieldText = (keyArg: string, changeArg: { before?: unknown; after?: unknown }, recipientArg?: string): string => {
|
|
43
44
|
if (keyArg === 'description') return 'the description';
|
|
@@ -76,6 +77,12 @@ export const ticketChangeText = (changeArg: ITicketChange, recipientArg?: string
|
|
|
76
77
|
const link = (changes.links?.after as TTicketLink[] | undefined)?.at(-1);
|
|
77
78
|
return link ? `${actor} linked ${linkText(link)}` : `${actor} confirmed an existing link`;
|
|
78
79
|
}
|
|
80
|
+
case 'unlinked': {
|
|
81
|
+
const after = (changes.links?.after as TTicketLink[] | undefined) ?? [];
|
|
82
|
+
const removed = ((changes.links?.before as TTicketLink[] | undefined) ?? [])
|
|
83
|
+
.find((linkArg) => !after.some((keptArg) => JSON.stringify(keptArg) === JSON.stringify(linkArg)));
|
|
84
|
+
return removed ? `${actor} removed the link ${linkText(removed)}` : `${actor} removed a link`;
|
|
85
|
+
}
|
|
79
86
|
case 'asked': {
|
|
80
87
|
const question = changes.question!.after as ITicketQuestion;
|
|
81
88
|
const options = question.options.length
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as plugins from './plugins.js';
|
|
2
2
|
import {
|
|
3
|
-
isTicketAutoLabel, ticketColumns, ticketErrorMessages, type ITicket, type ITicketActor, type ITicketActivity, type ITicketBoard,
|
|
3
|
+
isTicketAutoLabel, ticketColumns, ticketDoneColumns, ticketErrorMessages, type ITicket, type ITicketActor, type ITicketActivity, type ITicketBoard,
|
|
4
4
|
type ITicketChanges, type ITicketCreate, type ITicketFilters, type ITicketMove, type ITicketPerson, type ITicketQuestion,
|
|
5
5
|
type TTicketLink, type TTicketOperation, type TTicketRequest, type TTicketResponse,
|
|
6
6
|
type TTicketFleetEvent, type ITicketFleetEventResult, type ITicketComment, type TTicketData, type ITicketOpenQuestion,
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
import {
|
|
10
10
|
TicketModel, TicketBoardModel, TicketCommentModel, TicketActivityModel, TicketQuestionModel, TicketFleetEventModel,
|
|
11
11
|
} from './classes.ticketmodels.js';
|
|
12
|
-
import { assertTicketRequest, ticketActorSchema, ticketFleetEventSchema } from './functions.ticketvalidation.js';
|
|
12
|
+
import { assertTicketRequest, ticketActorSchema, ticketFleetEventSchema, ticketReleaseLink } from './functions.ticketvalidation.js';
|
|
13
13
|
import { ticketAutoLabelsFor } from './functions.ticketautolabels.js';
|
|
14
14
|
|
|
15
15
|
type TTicketStored = NonNullable<Awaited<ReturnType<typeof TicketModel.exact.findStoredOne>>>;
|
|
@@ -120,7 +120,7 @@ export class TicketStore {
|
|
|
120
120
|
if (event.kind !== 'comment') {
|
|
121
121
|
const link: TTicketLink = event.kind === 'landed'
|
|
122
122
|
? { kind: 'branch', repo: event.repo, branch: event.branch, sha: event.sha }
|
|
123
|
-
:
|
|
123
|
+
: ticketReleaseLink(event);
|
|
124
124
|
await this.perform('link', { ticketId: event.ticketKey, link }, event.actor, transactionArg,
|
|
125
125
|
now, board.sequence + 1, revision, track);
|
|
126
126
|
await this.perform('move', { ticketId: event.ticketKey, columnId: event.kind }, event.actor, transactionArg,
|
|
@@ -521,7 +521,7 @@ export class TicketStore {
|
|
|
521
521
|
if (!actorArg) throw new TicketError('ticket_identity_required', ticketErrorMessages.ticket_identity_required);
|
|
522
522
|
const { role: _role, ...viewer } = actorArg;
|
|
523
523
|
return { revision: revisionArg, tickets, columns: ticketColumns.map((idArg) => ({ id: idArg,
|
|
524
|
-
title: `${idArg[0].toUpperCase()}${idArg.slice(1).replaceAll('_', ' ')}`, ...(idArg
|
|
524
|
+
title: `${idArg[0].toUpperCase()}${idArg.slice(1).replaceAll('_', ' ')}`, ...(ticketDoneColumns.includes(idArg) ? { done: true } : {}) })),
|
|
525
525
|
viewer } satisfies ITicketBoard;
|
|
526
526
|
}
|
|
527
527
|
if (methodArg === 'create') {
|
|
@@ -601,6 +601,10 @@ export class TicketStore {
|
|
|
601
601
|
const stored = await TicketQuestionModel.exact.findStoredOne({ id: `${ticketId}:${input.questionId}` }, { session: sessionArg });
|
|
602
602
|
if (!stored) throw new TicketError('ticket_question_not_found', ticketErrorMessages.ticket_question_not_found);
|
|
603
603
|
const question = structuredClone(stored.question);
|
|
604
|
+
// The answer to a deploy plan's or a maintenance window's question is the owner's go: the owner's alone.
|
|
605
|
+
if (actorArg.role !== 'owner' && (ticket.deployPlan?.questionId === input.questionId || ticket.maintenanceWindow?.questionId === input.questionId)) {
|
|
606
|
+
throw new TicketError('ticket_go_owner_only', ticketErrorMessages.ticket_go_owner_only);
|
|
607
|
+
}
|
|
604
608
|
const choices = question.options.filter((optionArg) => input.choices?.includes(optionArg.id)).map((optionArg) => optionArg.id);
|
|
605
609
|
if (choices.length !== (input.choices?.length ?? 0) || (!question.multiSelect && choices.length > 1)
|
|
606
610
|
|| (input.text !== undefined && !question.allowCustom)) {
|
|
@@ -3915,11 +3915,21 @@ export class UpgradeCoordinator {
|
|
|
3915
3915
|
await writePrivateJson(this.tokenFilePath('grant', optionsArg.token), grant);
|
|
3916
3916
|
}
|
|
3917
3917
|
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
|
|
3921
|
-
|
|
3918
|
+
/**
|
|
3919
|
+
* Revokes the launch grant of `tokenArg`. True when it removed a grant no controller consumed: `consumeLaunchGrant`
|
|
3920
|
+
* renames the grant away before the controller launches a worker, so a revoked grant proves that no worker was
|
|
3921
|
+
* launched for it, and none can be any more (AGL-327). False when there was none, consumed or never written.
|
|
3922
|
+
*/
|
|
3923
|
+
public async removeLaunchGrant(tokenArg: string): Promise<boolean> {
|
|
3924
|
+
const revoked = await plugins.fs.promises.unlink(this.tokenFilePath('grant', tokenArg)).then(
|
|
3925
|
+
() => true,
|
|
3926
|
+
(errorArg) => {
|
|
3927
|
+
if ((errorArg as NodeJS.ErrnoException).code !== 'ENOENT') throw errorArg;
|
|
3928
|
+
return false;
|
|
3929
|
+
},
|
|
3930
|
+
);
|
|
3922
3931
|
await syncDirectory(this.baseDirectory);
|
|
3932
|
+
return revoked;
|
|
3923
3933
|
}
|
|
3924
3934
|
|
|
3925
3935
|
public async consumeLaunchGrant(optionsArg: {
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
unmanagedControllerError,
|
|
19
19
|
queryControllerStatus,
|
|
20
20
|
requestControllerUpgradeFinalize,
|
|
21
|
+
requestControllerUpgradeLaunch,
|
|
21
22
|
requestControllerUpgradePrepareBegin,
|
|
22
23
|
stopRunningController,
|
|
23
24
|
type IControllerServiceUnitControl,
|
|
@@ -1762,6 +1763,123 @@ export async function launchDetachedUpgradeWorker(
|
|
|
1762
1763
|
return await launchDetachedUpgradeWorkerProcess(optionsArg, dependenciesArg);
|
|
1763
1764
|
}
|
|
1764
1765
|
|
|
1766
|
+
export interface ILaunchUpgradeWorkerUnderLockOptions {
|
|
1767
|
+
coordinator: UpgradeCoordinator;
|
|
1768
|
+
installation: IPnpmGlobalInstallation;
|
|
1769
|
+
/** The invocation's upgrade lock, held over the `starting` transaction of `payload.token`. */
|
|
1770
|
+
lock: UpgradeInstallationLock;
|
|
1771
|
+
payload: IUpgradeWorkerPayload;
|
|
1772
|
+
/** The running controller, which launches the worker against a one-time grant; absent, it is launched detached. */
|
|
1773
|
+
status?: IControllerStatus;
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
export interface ILaunchUpgradeWorkerDependencies {
|
|
1777
|
+
requestControllerLaunch?: typeof requestControllerUpgradeLaunch;
|
|
1778
|
+
launchDetachedWorker?: (optionsArg: ILaunchDetachedUpgradeWorkerOptions) => Promise<IDetachedUpgradeWorker>;
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
/**
|
|
1782
|
+
* Launches the worker of a fresh `agl upgrade` whose transaction is still `starting`: through the running controller
|
|
1783
|
+
* against a one-time launch grant, else detached. The candidate it returns goes on, with the lock, to
|
|
1784
|
+
* `handoffLaunchedUpgradeWorkerUnderLock`.
|
|
1785
|
+
*
|
|
1786
|
+
* A failure that may have left a worker running throws `UpgradeWorkerCandidateDrainageError` and leaves the lock and
|
|
1787
|
+
* the transaction as they are. A failure that proves no worker runs ends the transaction failed and releases the
|
|
1788
|
+
* lock, so the next `agl upgrade` proceeds (AGL-327). A controller launch proves it when its grant is revoked
|
|
1789
|
+
* unconsumed: the controller consumes the grant (an atomic rename) before it launches a worker, so this covers a
|
|
1790
|
+
* controller that refuses the request before it consumes the grant, as one still starting does, and a request that
|
|
1791
|
+
* never reached it.
|
|
1792
|
+
*/
|
|
1793
|
+
export const launchUpgradeWorkerUnderLock = async (
|
|
1794
|
+
optionsArg: ILaunchUpgradeWorkerUnderLockOptions,
|
|
1795
|
+
dependenciesArg: ILaunchUpgradeWorkerDependencies = {},
|
|
1796
|
+
): Promise<IUpgradeWorkerLaunchCandidate> => {
|
|
1797
|
+
const { coordinator, installation, payload, status } = optionsArg;
|
|
1798
|
+
let candidate: IUpgradeWorkerLaunchCandidate | undefined;
|
|
1799
|
+
let launchError: unknown;
|
|
1800
|
+
// Only a sent launch request lets the controller consume the grant.
|
|
1801
|
+
let requested = false;
|
|
1802
|
+
try {
|
|
1803
|
+
if (status) {
|
|
1804
|
+
if (!payload.expectedController) {
|
|
1805
|
+
throw new Error('The upgrade worker payload does not name the running controller.');
|
|
1806
|
+
}
|
|
1807
|
+
await coordinator.createLaunchGrant({
|
|
1808
|
+
token: payload.token,
|
|
1809
|
+
port: payload.port,
|
|
1810
|
+
packageName: controllerPackageName,
|
|
1811
|
+
packageVersion: commitinfo.version,
|
|
1812
|
+
cliPath: installation.cliPath,
|
|
1813
|
+
controller: payload.expectedController,
|
|
1814
|
+
});
|
|
1815
|
+
requested = true;
|
|
1816
|
+
const launched = await (dependenciesArg.requestControllerLaunch ?? requestControllerUpgradeLaunch)(
|
|
1817
|
+
payload.port,
|
|
1818
|
+
status,
|
|
1819
|
+
payload.token,
|
|
1820
|
+
);
|
|
1821
|
+
candidate = { pid: launched.workerPid, cliPath: installation.cliPath, logFilePath: launched.logFilePath };
|
|
1822
|
+
} else {
|
|
1823
|
+
const launchDetached = dependenciesArg.launchDetachedWorker ?? launchDetachedUpgradeWorker;
|
|
1824
|
+
candidate = await launchDetached({ installation, payload });
|
|
1825
|
+
}
|
|
1826
|
+
} catch (errorArg) {
|
|
1827
|
+
launchError = errorArg;
|
|
1828
|
+
}
|
|
1829
|
+
if (status) {
|
|
1830
|
+
let revoked = false;
|
|
1831
|
+
try {
|
|
1832
|
+
revoked = await coordinator.removeLaunchGrant(payload.token);
|
|
1833
|
+
} catch (removalErrorArg) {
|
|
1834
|
+
launchError = launchError === undefined
|
|
1835
|
+
? removalErrorArg
|
|
1836
|
+
: new AggregateError(
|
|
1837
|
+
[launchError, removalErrorArg],
|
|
1838
|
+
'The controller upgrade launch failed and its launch grant could not be revoked.',
|
|
1839
|
+
);
|
|
1840
|
+
}
|
|
1841
|
+
// A consumed grant may have launched a worker the CLI cannot see, so the transaction stays as it is.
|
|
1842
|
+
if (launchError !== undefined && requested && !revoked) {
|
|
1843
|
+
throw new UpgradeWorkerCandidateDrainageError(
|
|
1844
|
+
'The controller upgrade launch failed and candidate drainage cannot be proven by the CLI.',
|
|
1845
|
+
launchError,
|
|
1846
|
+
);
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
if (launchError === undefined) return candidate!;
|
|
1850
|
+
if (launchError instanceof UpgradeWorkerCandidateDrainageError) throw launchError;
|
|
1851
|
+
const errors: unknown[] = [launchError];
|
|
1852
|
+
try {
|
|
1853
|
+
const transaction = await coordinator.readTransaction(payload.token);
|
|
1854
|
+
if (transaction.worker?.logFilePath) {
|
|
1855
|
+
return {
|
|
1856
|
+
pid: transaction.worker.pid,
|
|
1857
|
+
processGroupId: transaction.worker.processGroupId,
|
|
1858
|
+
fingerprint: transaction.worker.fingerprint,
|
|
1859
|
+
cliPath: transaction.worker.cliPath,
|
|
1860
|
+
logFilePath: transaction.worker.logFilePath,
|
|
1861
|
+
};
|
|
1862
|
+
}
|
|
1863
|
+
await coordinator.finishTransaction(
|
|
1864
|
+
payload.token,
|
|
1865
|
+
false,
|
|
1866
|
+
'The upgrade worker could not be launched.',
|
|
1867
|
+
launchError instanceof Error ? launchError.message : String(launchError),
|
|
1868
|
+
);
|
|
1869
|
+
} catch (errorArg) {
|
|
1870
|
+
errors.push(errorArg);
|
|
1871
|
+
}
|
|
1872
|
+
try {
|
|
1873
|
+
await optionsArg.lock.release();
|
|
1874
|
+
} catch (errorArg) {
|
|
1875
|
+
errors.push(errorArg);
|
|
1876
|
+
}
|
|
1877
|
+
if (errors.length > 1) {
|
|
1878
|
+
throw new AggregateError(errors, 'The upgrade worker could not be launched and its cleanup was incomplete.');
|
|
1879
|
+
}
|
|
1880
|
+
throw launchError;
|
|
1881
|
+
};
|
|
1882
|
+
|
|
1765
1883
|
const launchDetachedRecoveryWorker = async (optionsArg: {
|
|
1766
1884
|
installation: IPnpmGlobalInstallation;
|
|
1767
1885
|
payload: IUpgradeWorkerPayload;
|
|
@@ -280,11 +280,14 @@ const visitStrings = (valueArg: unknown, pathArg: string, visitorArg: (textArg:
|
|
|
280
280
|
else if (isPlainObject(valueArg)) for (const [key, item] of Object.entries(valueArg)) visitStrings(item, `${pathArg}.${key}`, visitorArg);
|
|
281
281
|
};
|
|
282
282
|
|
|
283
|
-
/**
|
|
284
|
-
|
|
283
|
+
/**
|
|
284
|
+
* Admits a master document or throws naming the first offending field. `save` checks a document being saved rather than
|
|
285
|
+
* read: it refuses what this release reads but only a later one writes.
|
|
286
|
+
*/
|
|
287
|
+
export const validateMaster = (valueArg: unknown, optionsArg: { save?: boolean } = {}): IConfigurationMaster => {
|
|
285
288
|
if (!isPlainObject(valueArg)) invalid('master', 'must be an object.');
|
|
286
289
|
exactKeys(valueArg, 'master', ['instructions', 'harnessInstructions', 'drivers', 'skills', 'agents', 'commands', 'mcpServers', 'policy'], ['nativeSettings', 'inference', 'fleet']);
|
|
287
|
-
if (valueArg.fleet !== undefined) validateFleetConfiguration(valueArg.fleet);
|
|
290
|
+
if (valueArg.fleet !== undefined) validateFleetConfiguration(valueArg.fleet, optionsArg);
|
|
288
291
|
if (Buffer.byteLength(JSON.stringify(valueArg), 'utf8') > maxMasterBytes) invalid('master', 'exceeds 4 MiB.');
|
|
289
292
|
text(valueArg.instructions, 'instructions');
|
|
290
293
|
if (!isPlainObject(valueArg.harnessInstructions)) invalid('harnessInstructions', 'must map harnesses to text.');
|