@bitkyc08/opencodex 2.98.0-preview.20261107 → 2.99.0-preview.20261108
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/bin/ocx.mjs +6 -19
- package/gui/dist/assets/{App-6TFtZsUF.css → App-C1hGwaN1.css} +1 -1
- package/gui/dist/assets/App-CWz0areF.js +59 -0
- package/gui/dist/assets/Tray-BosDoskm.js +1 -0
- package/gui/dist/assets/{brand-mark-VPhzz-WE.js → brand-mark-DpiBmFGI.js} +1 -1
- package/gui/dist/assets/{index-BWKBeK77.js → index-CAxjGWkm.js} +13 -13
- package/gui/dist/assets/index-bnUWypyz.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-BQYzrze9.js → segment-thumb-features-JcszQ18T.js} +1 -1
- package/gui/dist/classic/assets/App-BOEsFmiN.js +58 -0
- package/gui/dist/classic/assets/Tray-5v86r9Az.js +1 -0
- package/gui/dist/classic/assets/{index-Dp2j8bSc.js → index-ClZ_zZ77.js} +13 -13
- package/gui/dist/classic/assets/{quota-summary-CvamtHjB.js → quota-summary-xwvDtvu7.js} +1 -1
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/package.json +1 -1
- package/src/adapters/coding-agent/protocol.ts +8 -3
- package/src/adapters/coding-agent/turn.ts +6 -2
- package/src/adapters/command-code.ts +8 -5
- package/src/adapters/devin.ts +11 -1
- package/src/adapters/kiro/stream.ts +5 -4
- package/src/adapters/openai-chat/destination-policy.ts +25 -0
- package/src/adapters/openai-chat/native-content.ts +32 -0
- package/src/adapters/openai-chat/passthrough.ts +14 -1
- package/src/adapters/openai-chat/reasoning-wire.ts +42 -1
- package/src/adapters/openai-chat.ts +2 -2
- package/src/adapters/qoder/adapter.ts +9 -0
- package/src/bridge/response-json.ts +13 -1
- package/src/bridge/sse.ts +112 -22
- package/src/cli/account-affinity-reset.ts +42 -0
- package/src/cli/account.ts +5 -0
- package/src/cli/capabilities-accounts.ts +8 -0
- package/src/cli/capabilities-agents-routing.ts +5 -4
- package/src/cli/message-command.ts +1 -1
- package/src/cli/message-mcp-command.ts +8 -5
- package/src/client/orphan-token-recovery.ts +1 -1
- package/src/codex/autostart-health.ts +4 -2
- package/src/generated/compatibility-version.json +128 -76
- package/src/lib/desktop-cli-handoff.mjs +5 -8
- package/src/lib/failure-detail.ts +5 -2
- package/src/lib/launcher-signal-forwarding.d.mts +8 -0
- package/src/lib/launcher-signal-forwarding.mjs +36 -0
- package/src/lib/privacy.ts +6 -0
- package/src/lib/request-execution-budget.ts +188 -33
- package/src/lib/upstream-validation-detail.ts +47 -0
- package/src/lib/workflow-budget.ts +35 -16
- package/src/messaging/mcp-queue-rpc.ts +7 -3
- package/src/messaging/mcp-queue-server.ts +6 -0
- package/src/messaging/mcp-reply-cursor.ts +34 -0
- package/src/messaging/mcp-reply-tools.ts +93 -0
- package/src/oauth/anthropic-routing.ts +46 -3
- package/src/oauth/anthropic-session-affinity-store.ts +23 -10
- package/src/protocols/codecs/ollama.ts +22 -12
- package/src/providers/registry/entries-extended.ts +14 -0
- package/src/remote/hub-usage.ts +7 -0
- package/src/responses/parser.ts +27 -0
- package/src/responses/reasoning-envelope.ts +4 -0
- package/src/responses/schema.ts +1 -0
- package/src/server/chat-completions.ts +3 -0
- package/src/server/chat-native-sse.ts +14 -10
- package/src/server/chat-native.ts +24 -11
- package/src/server/index/websocket-handler.ts +12 -1
- package/src/server/management/oauth-account-routes.ts +65 -13
- package/src/server/management/route-registry.ts +1 -0
- package/src/server/messages-native.ts +21 -5
- package/src/server/request-log-attempt-recovery.ts +17 -0
- package/src/server/request-log-failure-detail.ts +18 -1
- package/src/server/request-log.ts +5 -18
- package/src/server/responses/account-auto-pause-recovery.ts +29 -1
- package/src/server/responses/adapter-continuation.ts +12 -5
- package/src/server/responses/adapter-dispatch.ts +50 -51
- package/src/server/responses/antigravity-model-denial-recovery.ts +4 -2
- package/src/server/responses/codex-ws-exchange.ts +13 -4
- package/src/server/responses/combo-required-tool-contract.ts +22 -3
- package/src/server/responses/core-codex-account.ts +92 -86
- package/src/server/responses/core-combo-failure.ts +7 -0
- package/src/server/responses/core-combo.ts +11 -4
- package/src/server/responses/core-lifetime.ts +3 -0
- package/src/server/responses/deferred-send-reporter.ts +18 -1
- package/src/server/responses/fetch-helpers.ts +9 -3
- package/src/server/responses/native-continuation-refusal.ts +8 -0
- package/src/server/responses/native-continuation-send.ts +34 -0
- package/src/server/responses/passthrough-delivery.ts +6 -2
- package/src/server/responses/passthrough-dispatch.ts +48 -33
- package/src/server/responses/passthrough-execution.ts +1 -0
- package/src/server/responses/policy-fallback.ts +32 -5
- package/src/server/responses/policy-request-scope.ts +26 -0
- package/src/server/responses/request-prepare.ts +11 -2
- package/src/server/responses/request-send-budget.ts +96 -14
- package/src/server/responses/request-spend-target.ts +19 -0
- package/src/server/responses/request-spend.ts +80 -28
- package/src/server/responses/request-transport.ts +106 -53
- package/src/server/responses/run-turn-execution.ts +3 -0
- package/src/server/responses/sidecar-execution.ts +2 -1
- package/src/server/responses/ws-upstream.ts +2 -1
- package/src/server/workflow-refusal.ts +38 -4
- package/src/server/ws-bridge.ts +10 -3
- package/src/service/desktop-startup.ts +76 -13
- package/src/types/request.ts +2 -1
- package/src/usage/app-breakdown.ts +23 -0
- package/src/usage/summary.ts +34 -1
- package/src/web-search/anthropic-executor.ts +25 -9
- package/src/web-search/loop.ts +3 -1
- package/src/web-search/passthrough-bridge.ts +3 -0
- package/src/web-search/run-turn-loop.ts +3 -1
- package/gui/dist/assets/App-B8_sOxoz.js +0 -59
- package/gui/dist/assets/Tray-DpWEp6ee.js +0 -1
- package/gui/dist/assets/index-5REODpG3.css +0 -2
- package/gui/dist/classic/assets/App-Bz65x1J6.js +0 -58
- package/gui/dist/classic/assets/Tray-kk7mdO6I.js +0 -1
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { createInferenceSendBudget } from "../inference/context";
|
|
2
|
+
import { responsesSpendTarget } from "./request-spend-target";
|
|
2
3
|
import type { ResponsesRequestContext } from "./core-options";
|
|
3
|
-
import { claimDispatchSpendProof, createPhysicalSendReporter, createRequestExecutionBudget, isRequestExecutionBudget, reportDispatchSends } from "../../lib/request-execution-budget";
|
|
4
|
+
import { claimDispatchSpendProof, createPhysicalSendReporter, createRequestExecutionBudget, isRequestExecutionBudget, hasWorkflowSendObserver, reportDispatchSends } from "../../lib/request-execution-budget";
|
|
4
5
|
import {
|
|
5
6
|
chargeWorkflowSends,
|
|
7
|
+
reserveWorkflowSend,
|
|
6
8
|
workflowSendCeilingReached,
|
|
7
9
|
workflowSpendCeilingReached,
|
|
8
10
|
} from "../../lib/workflow-budget";
|
|
@@ -11,12 +13,14 @@ import { poolContinuityRefusalReason, workflowRefusalResponse } from "../workflo
|
|
|
11
13
|
import type { AttemptRecoveryKind, AttemptRecoveryWithheld } from "../../usage/log";
|
|
12
14
|
import { noteAttemptRecoveryWithheld, noteAttemptSend } from "../request-log";
|
|
13
15
|
import { TRANSIENT_RETRY_MAX_ATTEMPTS, SendBudgetExhaustedError } from "../../lib/upstream-retry";
|
|
16
|
+
import { estimateTokens } from "../../lib/token-estimate";
|
|
14
17
|
import type {
|
|
15
18
|
DispatchDecision,
|
|
16
19
|
DispatchIntent,
|
|
17
20
|
RequestExecutionBudget,
|
|
18
21
|
SendClass,
|
|
19
22
|
SingleUseDispatchPermit,
|
|
23
|
+
AuxiliaryModelSend,
|
|
20
24
|
} from "../../lib/request-execution-budget";
|
|
21
25
|
|
|
22
26
|
/**
|
|
@@ -59,6 +63,7 @@ export function createResponsesSendBudget(
|
|
|
59
63
|
// sends once per child seven hundred times, so every send charged to the request is charged
|
|
60
64
|
// to the root as well (#4546).
|
|
61
65
|
const workflowRootId = req.headers.get("x-codex-parent-thread-id")?.trim() || undefined;
|
|
66
|
+
const physicalWorkflowAccounting = hasWorkflowSendObserver(sendBudget);
|
|
62
67
|
// A child that owns its Combo booking settles that exact permit through the reporter, even
|
|
63
68
|
// when its caller handed ownership over only as `comboInitialSend`.
|
|
64
69
|
const inheritedPermit = options.compactionRecoveryPermit ?? options.comboDispatchPermit
|
|
@@ -74,22 +79,71 @@ export function createResponsesSendBudget(
|
|
|
74
79
|
targetSendsUsed += charged;
|
|
75
80
|
if (charged > 0) initialPermit = undefined;
|
|
76
81
|
};
|
|
77
|
-
const recordTransientSends = (used: number, permit?: SingleUseDispatchPermit): void => {
|
|
82
|
+
const recordTransientSends = (used: number, permit?: SingleUseDispatchPermit, workflowAlreadyCounted = false): void => {
|
|
78
83
|
const charged = Math.max(0, used);
|
|
79
84
|
noteTargetSends(charged);
|
|
80
|
-
reportDispatchSends(sendBudget, charged, permit);
|
|
85
|
+
reportDispatchSends(sendBudget, charged, permit, workflowAlreadyCounted);
|
|
81
86
|
options.onCompactionRecoverySendsReported?.(charged);
|
|
82
|
-
chargeWorkflowSends(workflowRootId, charged);
|
|
87
|
+
if (!physicalWorkflowAccounting && !workflowAlreadyCounted) chargeWorkflowSends(workflowRootId, charged);
|
|
83
88
|
};
|
|
84
89
|
const noteTransientSends = (used: number): void => recordTransientSends(used, pendingHopPermit ?? inheritedPermit);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
90
|
+
const physicalSendReporter = (permit?: SingleUseDispatchPermit, explicitContinuation = false,
|
|
91
|
+
continuationEstimate?: { inputTokens: number; outputCeilingTokens: number }) => {
|
|
92
|
+
let unreportedWorkflowStarts = 0;
|
|
93
|
+
const targetKey = explicitContinuation ? `native-continuation:${crypto.randomUUID()}` : undefined;
|
|
94
|
+
const reporter = createPhysicalSendReporter(sendBudget, () => ({ ...responsesSpendTarget(logCtx),
|
|
95
|
+
...(targetKey ? { targetKey } : {}),
|
|
96
|
+
...(continuationEstimate ? { estimate: continuationEstimate, explicitEstimate: true } : {}) }), permit, charged => {
|
|
89
97
|
noteTargetSends(Math.max(0, charged));
|
|
90
98
|
options.onCompactionRecoverySendsReported?.(charged);
|
|
91
|
-
|
|
99
|
+
const prepaid = Math.min(unreportedWorkflowStarts, Math.max(0, charged));
|
|
100
|
+
unreportedWorkflowStarts -= prepaid;
|
|
101
|
+
if (!physicalWorkflowAccounting) chargeWorkflowSends(workflowRootId, charged - prepaid);
|
|
102
|
+
}, { explicitContinuation });
|
|
103
|
+
if (workflowRootId && !physicalWorkflowAccounting) {
|
|
104
|
+
const beforeSend = reporter.beforeSend, close = reporter.close;
|
|
105
|
+
let closed = false;
|
|
106
|
+
reporter.beforeSend = () => {
|
|
107
|
+
const policy = currentWorkflowBudgetPolicy();
|
|
108
|
+
// Check before a legacy permit's admission can spend it. No await separates this
|
|
109
|
+
// read from the atomic claim, and the reporter owns the later accounting receipt.
|
|
110
|
+
if (closed || workflowSendCeilingReached(workflowRootId, policy) || beforeSend?.() === false
|
|
111
|
+
|| !reserveWorkflowSend(workflowRootId, policy)) return false;
|
|
112
|
+
unreportedWorkflowStarts++;
|
|
113
|
+
return true;
|
|
114
|
+
};
|
|
115
|
+
reporter.close = () => { closed = true; close?.(); };
|
|
116
|
+
}
|
|
117
|
+
return reporter;
|
|
118
|
+
};
|
|
119
|
+
// Capture at helper creation, before another leg can replace the pending handoff.
|
|
120
|
+
const transientSendReporter = (permit = pendingHopPermit ?? inheritedPermit) => physicalSendReporter(permit);
|
|
121
|
+
// A helper owns a fresh booking, never a routed leg's permit or target-local retry counter.
|
|
122
|
+
const dispatchSidecarSend: AuxiliaryModelSend = async (target, dispatch) => {
|
|
123
|
+
if (!isRequestExecutionBudget(sendBudget)) {
|
|
124
|
+
if (!reserveWorkflowSend(workflowRootId, currentWorkflowBudgetPolicy())) throw new SendBudgetExhaustedError();
|
|
125
|
+
sendBudget.used++;
|
|
126
|
+
return dispatch();
|
|
127
|
+
}
|
|
128
|
+
const decision = sendBudget.reserveDispatch({
|
|
129
|
+
sendClass: "auxiliary", targetKey: target.targetKey,
|
|
130
|
+
spendScopes: { poolId: target.poolId, identityId: target.identityId ?? "" },
|
|
131
|
+
spendEstimate: target.estimate,
|
|
92
132
|
});
|
|
133
|
+
if (!decision.allowed) throw new SendBudgetExhaustedError();
|
|
134
|
+
const permit = decision.permit;
|
|
135
|
+
const run = () => {
|
|
136
|
+
const policy = currentWorkflowBudgetPolicy();
|
|
137
|
+
// No await separates this precheck, synchronous permit admission and the root claim.
|
|
138
|
+
// Refuse before use() so the finally path can return an unused reservation.
|
|
139
|
+
if (!physicalWorkflowAccounting && workflowSendCeilingReached(workflowRootId, policy)) throw new SendBudgetExhaustedError();
|
|
140
|
+
if (!permit.use()) throw new SendBudgetExhaustedError();
|
|
141
|
+
if (!physicalWorkflowAccounting && !reserveWorkflowSend(workflowRootId, policy)) throw new SendBudgetExhaustedError();
|
|
142
|
+
return dispatch();
|
|
143
|
+
};
|
|
144
|
+
try { return await (permit.execute ? permit.execute(run) : run()); }
|
|
145
|
+
finally { permit.release(); }
|
|
146
|
+
};
|
|
93
147
|
// Refused before any dispatch, and deliberately not by evicting the root's ledger entry:
|
|
94
148
|
// dropping the record to make room would hand the fan-out a fresh allowance, which is the
|
|
95
149
|
// laundering this ceiling exists to stop. The client is told the task needs a new grant
|
|
@@ -316,27 +370,51 @@ export function createResponsesSendBudget(
|
|
|
316
370
|
workflowRootId,
|
|
317
371
|
noteTransientSends,
|
|
318
372
|
transientSendReporter,
|
|
373
|
+
/**
|
|
374
|
+
* A new native continuation cannot consume an initial or recovery booking. Its
|
|
375
|
+
* reservation is priced on the exact successor frame about to leave — the
|
|
376
|
+
* production dispatch path always supplies it. The merged outgoing body's
|
|
377
|
+
* max_output_tokens replaces the output reservation when present, including null.
|
|
378
|
+
* When the wire omits it, retain the caller's local output reservation: the
|
|
379
|
+
* canonical backend strips unsupported output caps. Input is always remeasured.
|
|
380
|
+
*/
|
|
381
|
+
continuationSendReporter: (frame: { body: Record<string, unknown>; text: string }) =>
|
|
382
|
+
physicalSendReporter(undefined, true, {
|
|
383
|
+
inputTokens: Math.max(1, estimateTokens(frame.text, logCtx.model)),
|
|
384
|
+
outputCeilingTokens: typeof frame.body.max_output_tokens === "number"
|
|
385
|
+
&& Number.isFinite(frame.body.max_output_tokens) && frame.body.max_output_tokens > 0
|
|
386
|
+
? Math.trunc(frame.body.max_output_tokens)
|
|
387
|
+
: Object.hasOwn(frame.body, "max_output_tokens") ? 0 : logCtx.spendOutputCeilingTokens ?? 0,
|
|
388
|
+
}),
|
|
319
389
|
remainingTransientSendBudget,
|
|
320
390
|
/** Only this child's prepaid send, not every pending external booking, is spendable here. */
|
|
321
391
|
initialSendAllowance: (cap: number): number => Math.min(cap,
|
|
322
392
|
remainingTransientSendBudget(cap) + (initialPermit ? 1 : 0)),
|
|
323
393
|
/** At final executor admission, settle only the owning booking or reserve this send. */
|
|
324
394
|
noteInitialDispatch: (prepaid = initialPermit): void => {
|
|
395
|
+
const policy = currentWorkflowBudgetPolicy();
|
|
396
|
+
if (!physicalWorkflowAccounting && workflowSendCeilingReached(workflowRootId, policy)) {
|
|
397
|
+
prepaid?.release();
|
|
398
|
+
throw new SendBudgetExhaustedError();
|
|
399
|
+
}
|
|
325
400
|
if (prepaid) {
|
|
326
|
-
if (!prepaid.assumeCharge()) throw new SendBudgetExhaustedError();
|
|
401
|
+
if (!prepaid.assumeCharge()) { prepaid.release(); throw new SendBudgetExhaustedError(); }
|
|
327
402
|
if (prepaid === initialPermit) initialPermit = undefined;
|
|
328
403
|
} else if (adapterSendBudget && (options.comboInitialSend || options.compactionRecoveryPermit)) {
|
|
329
404
|
const decision = adapterSendBudget.reserveDispatch({ sendClass: "transient",
|
|
330
405
|
targetKey: adapterSendBudget.lastTargetKey ?? `${logCtx.provider}/${logCtx.model}` });
|
|
331
|
-
if (!decision.allowed
|
|
406
|
+
if (!decision.allowed) throw new SendBudgetExhaustedError();
|
|
407
|
+
if (!decision.permit.use()) { decision.permit.release(); throw new SendBudgetExhaustedError(); }
|
|
332
408
|
} else {
|
|
333
409
|
// Legacy direct callers can prebook without handing over a permit; keep their reporter.
|
|
334
|
-
|
|
410
|
+
if (adapterSendBudget?.claimWorkflowSend && !adapterSendBudget.claimWorkflowSend()) throw new SendBudgetExhaustedError();
|
|
411
|
+
if (!physicalWorkflowAccounting && !reserveWorkflowSend(workflowRootId, policy)) throw new SendBudgetExhaustedError();
|
|
412
|
+
recordTransientSends(1, pendingHopPermit ?? inheritedPermit, true);
|
|
335
413
|
return;
|
|
336
414
|
}
|
|
415
|
+
if (!physicalWorkflowAccounting && !reserveWorkflowSend(workflowRootId, policy)) throw new SendBudgetExhaustedError();
|
|
337
416
|
targetSendsUsed += 1;
|
|
338
417
|
options.onCompactionRecoverySendsReported?.(1);
|
|
339
|
-
chargeWorkflowSends(workflowRootId, 1);
|
|
340
418
|
},
|
|
341
419
|
/** Physical sends reported by this owner; unrelated shared reservations cannot consume it. */
|
|
342
420
|
get targetSendsUsed(): number { return targetSendsUsed; },
|
|
@@ -348,6 +426,7 @@ export function createResponsesSendBudget(
|
|
|
348
426
|
*/
|
|
349
427
|
get sendsUsed(): number { return sendBudget.used; },
|
|
350
428
|
adapterSendBudget,
|
|
429
|
+
dispatchSidecarSend,
|
|
351
430
|
adapterDispatchBudget,
|
|
352
431
|
refundableAdapterDispatchBudget,
|
|
353
432
|
noteAdapterPhysicalSend,
|
|
@@ -391,11 +470,14 @@ function adapterDispatchBudgetView(
|
|
|
391
470
|
return {
|
|
392
471
|
get physicalStarted() { return budget.physicalStarted; },
|
|
393
472
|
get physicalLimit() { return budget.physicalLimit; },
|
|
473
|
+
get workflowCountObserved() { return budget.workflowCountObserved; },
|
|
394
474
|
get spendEnforced() { return budget.spendEnforced; },
|
|
395
475
|
get spendPolicyStarted() { return budget.spendPolicyStarted; },
|
|
396
476
|
get spendAdmissionPolicy() { return budget.spendAdmissionPolicy; },
|
|
397
477
|
startRequest: target => budget.startRequest?.(target),
|
|
398
|
-
claimPhysicalSend: ()
|
|
478
|
+
claimPhysicalSend: auxiliary => auxiliary ? budget.claimPhysicalSend?.(true) : budget.claimPhysicalSend?.(),
|
|
479
|
+
claimContinuationPhysicalSend: () => budget.claimContinuationPhysicalSend?.(),
|
|
480
|
+
claimWorkflowSend: auxiliary => (auxiliary ? budget.claimWorkflowSend?.(true) : budget.claimWorkflowSend?.()) ?? true,
|
|
399
481
|
beginSpendProducer: () => budget.beginSpendProducer?.(),
|
|
400
482
|
get used(): number { return budget.used; },
|
|
401
483
|
set used(next: number) { budget.used = next; },
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SpendScopes } from "../../lib/spend-reservation-ledger";
|
|
2
|
+
import type { RequestLogContext } from "../request-log";
|
|
3
|
+
|
|
4
|
+
type PhysicalSpendTarget = SpendScopes & { targetKey?: string };
|
|
5
|
+
const targets = new WeakMap<RequestLogContext, () => PhysicalSpendTarget>();
|
|
6
|
+
|
|
7
|
+
/** A prepared candidate can own admission without overwriting the last physical send's log. */
|
|
8
|
+
export function bindResponsesSpendTarget(context: RequestLogContext, target?: () => PhysicalSpendTarget): void {
|
|
9
|
+
if (target) targets.set(context, target);
|
|
10
|
+
else targets.delete(context);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Resolve late, after queued negotiation; ordinary callers retain their original pool fallback. */
|
|
14
|
+
export function responsesSpendTarget(context: RequestLogContext, fallbackPool?: string): PhysicalSpendTarget {
|
|
15
|
+
return targets.get(context)?.() ?? {
|
|
16
|
+
poolId: context.spendPoolId ?? fallbackPool ?? context.provider,
|
|
17
|
+
identityId: context.accountLogLabel,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -3,7 +3,8 @@ import type { RequestSendObserver } from "../../lib/request-execution-budget";
|
|
|
3
3
|
import { sharedSpendLedger, sharedSpendPolicy, type SpendReservationLedger, type SpendScopes, type SpendSeed, type SpendReservationProof, type SpendAdmissionPolicy } from "../../lib/spend-reservation-ledger";
|
|
4
4
|
import { SpendLedgerOwnerError } from "../../lib/spend-ledger-owner";
|
|
5
5
|
import { markLocalRequestLogRefusal, type RequestLogContext } from "../request-log";
|
|
6
|
-
import { recordWorkflowRefusalEvent, workflowDenialSummary, type WorkflowDenial } from "../../lib/workflow-budget";
|
|
6
|
+
import { chargeWorkflowSends, recordWorkflowRefusalEvent, reserveWorkflowSend, workflowDenialSummary, type WorkflowDenial, type WorkflowSpendDenialDetail } from "../../lib/workflow-budget";
|
|
7
|
+
import { currentWorkflowBudgetPolicy } from "../../lib/workflow-budget-config";
|
|
7
8
|
|
|
8
9
|
/** The terminal usage a request reported, in the only two fields the ledger books. */
|
|
9
10
|
export interface TerminalSpendUsage {
|
|
@@ -16,7 +17,19 @@ export interface RequestSpendSettlement {
|
|
|
16
17
|
settle(usage: TerminalSpendUsage | undefined): void;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
export interface SpendTargetIdentity extends SpendScopes {
|
|
20
|
+
export interface SpendTargetIdentity extends SpendScopes {
|
|
21
|
+
readonly targetKey?: string;
|
|
22
|
+
/** Helpers preserve other bookings and do not own the routed model's terminal usage. */
|
|
23
|
+
readonly auxiliary?: boolean;
|
|
24
|
+
readonly estimate?: { inputTokens: number; outputCeilingTokens: number };
|
|
25
|
+
/**
|
|
26
|
+
* The target carries its own measured estimate (explicit native continuations). Unlike an
|
|
27
|
+
* auxiliary target it still owns the routed model's terminal usage; unlike an anchor
|
|
28
|
+
* rebind it never retires an unrelated pending booking and is never adopted by an
|
|
29
|
+
* unkeyed same-scope lookup.
|
|
30
|
+
*/
|
|
31
|
+
readonly explicitEstimate?: boolean;
|
|
32
|
+
}
|
|
20
33
|
export interface RequestSpendReporter {
|
|
21
34
|
start(seed: SpendSeed, ordinal: number): void;
|
|
22
35
|
rebindTarget(target: SpendTargetIdentity, ordinal: number): boolean;
|
|
@@ -27,6 +40,8 @@ export interface RequestSpendReporter {
|
|
|
27
40
|
export interface RequestSpendTracker extends RequestSendObserver, RequestSpendSettlement {
|
|
28
41
|
/** Dispatches this request lost to a ledger ceiling. Zero on every ordinary request. */
|
|
29
42
|
readonly refusals: number;
|
|
43
|
+
/** Latest local admission decision, paired with the monotonic refusal count. */
|
|
44
|
+
readonly lastRefusal?: { readonly reason: WorkflowDenial; readonly spend?: WorkflowSpendDenialDetail };
|
|
30
45
|
readonly enforced: boolean;
|
|
31
46
|
ensureSeed(target: SpendTargetIdentity, prepaidProof?: SpendReservationProof): SpendSeed | undefined;
|
|
32
47
|
beginReporter(): RequestSpendReporter;
|
|
@@ -73,13 +88,15 @@ export function createRequestSpendTracker(
|
|
|
73
88
|
const live: string[] = [];
|
|
74
89
|
const pendingDispatch = new Set<string>();
|
|
75
90
|
let refusals = 0;
|
|
91
|
+
let lastRefusal: RequestSpendTracker["lastRefusal"];
|
|
76
92
|
let resolved = false;
|
|
77
93
|
let terminalProcessed = false;
|
|
78
94
|
let seededAccounting = false;
|
|
79
95
|
let finalRequested = false;
|
|
80
96
|
let finalUsage: TerminalSpendUsage | undefined;
|
|
81
97
|
let reporters = 0;
|
|
82
|
-
const seeds = new Map<string, { seed: SpendSeed; started: boolean; reported: boolean }>();
|
|
98
|
+
const seeds = new Map<string, { seed: SpendSeed; started: boolean; reported: boolean; auxiliary: boolean; explicitEstimate: boolean; estimate?: { inputTokens: number; outputCeilingTokens: number } }>();
|
|
99
|
+
const auxiliarySendIds = new Set<string>();
|
|
83
100
|
// Abandoned capabilities retain cleanup ownership, never admission lookup.
|
|
84
101
|
const retiredSeeds = new Set<SpendSeed>();
|
|
85
102
|
const resolvedSendIds = new Set<string>();
|
|
@@ -108,7 +125,7 @@ export function createRequestSpendTracker(
|
|
|
108
125
|
retryPhysicalReports();
|
|
109
126
|
// All starts are booked before producers close. Ordinal, never callback order, owns usage.
|
|
110
127
|
const entries = [...physical.entries()].sort((a, b) => a[0] - b[0]);
|
|
111
|
-
const terminal = entries.
|
|
128
|
+
const terminal = entries.findLast(([, entry]) => entry.sendId && !auxiliarySendIds.has(entry.sendId))?.[1].sendId;
|
|
112
129
|
const ids: string[] = [];
|
|
113
130
|
for (const [, entry] of entries) {
|
|
114
131
|
if (!entry.sendId) return false;
|
|
@@ -180,13 +197,13 @@ export function createRequestSpendTracker(
|
|
|
180
197
|
if (!pendingDispatch.has(sendId)) ledger().markDispatched(sendId);
|
|
181
198
|
}
|
|
182
199
|
};
|
|
183
|
-
const refusal = (denial: import("../../lib/spend-reservation-ledger").SpendDenial): void => {
|
|
200
|
+
const refusal = (denial: import("../../lib/spend-reservation-ledger").SpendDenial, terminal = true): void => {
|
|
184
201
|
refusals += 1;
|
|
185
202
|
// Preserve the PR's explicit unresolved-history refusal and its distinct operator code.
|
|
186
203
|
if (denial.reason === "pool-history-unresolved") {
|
|
204
|
+
lastRefusal = { reason: "workflow-pool-history-unresolved" };
|
|
187
205
|
const summary = workflowDenialSummary("workflow-pool-history-unresolved");
|
|
188
|
-
markLocalRequestLogRefusal(logCtx, summary.code);
|
|
189
|
-
logCtx.errorCode = summary.code;
|
|
206
|
+
if (terminal) { markLocalRequestLogRefusal(logCtx, summary.code); logCtx.errorCode = summary.code; }
|
|
190
207
|
recordWorkflowRefusalEvent(rootId, "workflow-pool-history-unresolved", Date.now());
|
|
191
208
|
return;
|
|
192
209
|
}
|
|
@@ -205,14 +222,31 @@ export function createRequestSpendTracker(
|
|
|
205
222
|
...(denial.includesUnboundPoolHistory ? { includesUnboundPoolHistory: true } : {}),
|
|
206
223
|
}
|
|
207
224
|
: undefined;
|
|
225
|
+
lastRefusal = { reason, ...(detail ? { spend: detail } : {}) };
|
|
208
226
|
const summary = workflowDenialSummary(reason, detail);
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
227
|
+
if (terminal) {
|
|
228
|
+
markLocalRequestLogRefusal(logCtx, summary.code);
|
|
229
|
+
logCtx.errorCode = summary.code;
|
|
230
|
+
if (detail?.includesUnboundPoolHistory) logCtx.spendRefusalDetail = detail;
|
|
231
|
+
}
|
|
212
232
|
recordWorkflowRefusalEvent(rootId, reason, Date.now(), detail);
|
|
213
233
|
return;
|
|
214
234
|
};
|
|
235
|
+
/** Record a refused root claim here, including its counter, log and event; callers must not repeat it. */
|
|
236
|
+
const claimWorkflowSend = (terminal = true): boolean => {
|
|
237
|
+
if (reserveWorkflowSend(rootId, currentWorkflowBudgetPolicy())) return true;
|
|
238
|
+
refusals++;
|
|
239
|
+
lastRefusal = { reason: "workflow-sends-exhausted" };
|
|
240
|
+
const summary = workflowDenialSummary("workflow-sends-exhausted");
|
|
241
|
+
if (terminal) { markLocalRequestLogRefusal(logCtx, summary.code); logCtx.errorCode = summary.code; }
|
|
242
|
+
recordWorkflowRefusalEvent(rootId, "workflow-sends-exhausted");
|
|
243
|
+
return false;
|
|
244
|
+
};
|
|
215
245
|
const tracker: RequestSpendTracker = {
|
|
246
|
+
recordWorkflowSend() { chargeWorkflowSends(rootId, 1); },
|
|
247
|
+
beforePhysicalSend(auxiliary = false) {
|
|
248
|
+
return claimWorkflowSend(!auxiliary);
|
|
249
|
+
},
|
|
216
250
|
startRequest(target) {
|
|
217
251
|
if (admissionPolicy) return;
|
|
218
252
|
const policy = ledgerRef?.policy ?? sharedSpendPolicy();
|
|
@@ -242,29 +276,39 @@ export function createRequestSpendTracker(
|
|
|
242
276
|
}
|
|
243
277
|
}
|
|
244
278
|
if (target.targetKey === undefined) {
|
|
245
|
-
const same = [...seeds.entries()].find(([candidate]) => {
|
|
279
|
+
const same = [...seeds.entries()].find(([candidate, candidateState]) => {
|
|
246
280
|
const identity = JSON.parse(candidate) as Array<string | undefined>;
|
|
247
|
-
return
|
|
281
|
+
return candidateState.explicitEstimate !== true
|
|
282
|
+
&& identity[0] === (scopes.rootId ?? null) && identity[1] === (scopes.identityId ?? null) && identity[2] === (scopes.poolId ?? null);
|
|
248
283
|
});
|
|
249
284
|
if (same) return same[1].seed;
|
|
250
285
|
}
|
|
251
|
-
// Rebind an unused provisional anchor before selection changes. Dispatched anchors stay
|
|
252
|
-
|
|
286
|
+
// Rebind an unused provisional anchor before selection changes. Dispatched anchors stay
|
|
287
|
+
// live, and an explicit-estimate successor neither retires nor is retired by another leg:
|
|
288
|
+
// both directions would destroy a booking its own reporter still owns.
|
|
289
|
+
for (const [oldKey, state] of seeds) if (!state.started && !state.explicitEstimate && !target.auxiliary && target.explicitEstimate !== true) {
|
|
253
290
|
if (!retireSeed(oldKey, state.seed)) return undefined;
|
|
254
291
|
}
|
|
255
292
|
// Exact prepaid proofs originate from this tracker and are found above; foreign receipts
|
|
256
293
|
// cannot enroll arbitrary scopes. Normal reservation remains the only initial admission.
|
|
257
294
|
if (prepaidProof && prepaidProof.ledger !== ledger()) return undefined;
|
|
258
|
-
const
|
|
295
|
+
const estimate = target.auxiliary || target.explicitEstimate ? target.estimate : undefined;
|
|
296
|
+
const decision = ledger().reserveSeed({ sendId: randomUUID(), scopes, ...(estimate ?? estimates()), admissionPolicy }, tracker);
|
|
259
297
|
if (!decision.reserved) {
|
|
260
298
|
// Use the established denial mapping without making another reservation.
|
|
261
|
-
refusal(decision.denial);
|
|
299
|
+
refusal(decision.denial, !target.auxiliary);
|
|
262
300
|
return undefined;
|
|
263
301
|
}
|
|
264
302
|
seededAccounting = true;
|
|
265
|
-
seeds.set(key, { seed: decision.seed, started: false, reported: false
|
|
303
|
+
seeds.set(key, { seed: decision.seed, started: false, reported: false, auxiliary: target.auxiliary === true,
|
|
304
|
+
explicitEstimate: target.explicitEstimate === true, estimate });
|
|
266
305
|
return decision.seed;
|
|
267
306
|
},
|
|
307
|
+
releaseSeed(seed) {
|
|
308
|
+
const entry = [...seeds.entries()].find(([, state]) => state.seed === seed);
|
|
309
|
+
// A started send or another tracker's seed retains its owner and settlement duty.
|
|
310
|
+
if (entry && !entry[1].started) retireSeed(entry[0], seed);
|
|
311
|
+
},
|
|
268
312
|
beginReporter() {
|
|
269
313
|
if (resolved || finalRequested && reporters === 0) throw new Error("Spend reporter registered after final closure");
|
|
270
314
|
deferCleanup();
|
|
@@ -328,7 +372,8 @@ export function createRequestSpendTracker(
|
|
|
328
372
|
entry.reportPending = true;
|
|
329
373
|
const firstOrdinal = [...physical.entries()].filter(([, send]) => send.seed === seed).map(([order]) => order).sort((a, b) => a - b)[0];
|
|
330
374
|
const sendId = entry.sendId ??= ordinal === firstOrdinal ? seed.sendId : randomUUID();
|
|
331
|
-
|
|
375
|
+
if (state.auxiliary) auxiliarySendIds.add(sendId);
|
|
376
|
+
const estimate = entry.estimate ??= state.estimate ?? estimates();
|
|
332
377
|
if (sendId !== seed.sendId && !ledger().reserveReportedFromSeed(seed, { sendId, ...estimate })) {
|
|
333
378
|
throw new Error("Already-sent spend could not be persisted");
|
|
334
379
|
}
|
|
@@ -341,22 +386,27 @@ export function createRequestSpendTracker(
|
|
|
341
386
|
finalizeSeeds();
|
|
342
387
|
},
|
|
343
388
|
charge(options?: Parameters<RequestSendObserver["charge"]>[0]): boolean {
|
|
344
|
-
tracker.startRequest?.();
|
|
389
|
+
tracker.startRequest?.(options?.scopes);
|
|
345
390
|
// A send that has already left is RECORDED, never refused: the tokens are spent, and a
|
|
346
391
|
// booking the ledger drops is a booking the ceiling can never see. This is the reporting
|
|
347
392
|
// transports' path -- the passthrough ladder reports through `onSendsConsumed` after the
|
|
348
393
|
// fetch -- so without it a root ceiling on the canonical Codex path would sit one send
|
|
349
394
|
// short of its limit forever and refuse nothing.
|
|
350
395
|
const alreadySent = options?.alreadySent === true;
|
|
396
|
+
if (!options?.deferDispatch && !options?.workflowAlreadyCounted) {
|
|
397
|
+
if (alreadySent) chargeWorkflowSends(rootId, 1);
|
|
398
|
+
else if (!claimWorkflowSend(!options?.auxiliary)) return false;
|
|
399
|
+
}
|
|
351
400
|
if (!alreadySent && tracker.enforced) {
|
|
352
|
-
const seed = tracker.ensureSeed({ targetKey: options?.targetKey
|
|
401
|
+
const seed = tracker.ensureSeed({ ...options?.scopes, targetKey: options?.targetKey,
|
|
402
|
+
...(options?.auxiliary ? { auxiliary: true, estimate: options.estimate } : {}) });
|
|
353
403
|
if (!seed) return false;
|
|
354
404
|
options?.onReserved?.({ ledger: ledger(), sendId: seed.sendId });
|
|
355
405
|
return true;
|
|
356
406
|
}
|
|
357
407
|
const sendId = randomUUID();
|
|
358
408
|
const bookedLedger = ledger();
|
|
359
|
-
const scopes: SpendScopes = {
|
|
409
|
+
const scopes: SpendScopes = options?.scopes ? selectedScopes(options.scopes) : {
|
|
360
410
|
...(rootId !== undefined ? { rootId } : {}),
|
|
361
411
|
// Already the privacy-safe label the request log uses, and the ledger aliases it
|
|
362
412
|
// again on the way to disk. A raw credential never reaches either.
|
|
@@ -373,8 +423,7 @@ export function createRequestSpendTracker(
|
|
|
373
423
|
sendId,
|
|
374
424
|
scopes,
|
|
375
425
|
admissionPolicy,
|
|
376
|
-
|
|
377
|
-
outputCeilingTokens: logCtx.spendOutputCeilingTokens ?? 0,
|
|
426
|
+
...(options?.auxiliary && options.estimate ? options.estimate : estimates()),
|
|
378
427
|
...(alreadySent ? { alreadySent: true } : {}),
|
|
379
428
|
});
|
|
380
429
|
if (!decision.reserved) {
|
|
@@ -382,11 +431,12 @@ export function createRequestSpendTracker(
|
|
|
382
431
|
// capacity is full. Unconfigured/nonapplicable requests remain observe-only, and a
|
|
383
432
|
// physical send reported after dispatch cannot be refused retroactively.
|
|
384
433
|
if (alreadySent || !enforced) return true;
|
|
385
|
-
refusal(decision.denial);
|
|
434
|
+
refusal(decision.denial, !options?.auxiliary);
|
|
386
435
|
return false;
|
|
387
436
|
}
|
|
388
437
|
if (!alreadySent) options?.onReserved?.({ ledger: bookedLedger, sendId });
|
|
389
438
|
live.push(sendId);
|
|
439
|
+
if (options?.auxiliary) auxiliarySendIds.add(sendId);
|
|
390
440
|
if (options?.deferDispatch) pendingDispatch.add(sendId);
|
|
391
441
|
else confirmOlderSends();
|
|
392
442
|
// It has already left, so the reservation cannot be handed back for free: from here only
|
|
@@ -425,18 +475,19 @@ export function createRequestSpendTracker(
|
|
|
425
475
|
if (seededAccounting) { tracker.requestFinalSettlement(usage); return; }
|
|
426
476
|
try {
|
|
427
477
|
if (!terminalProcessed && live.length > 0) {
|
|
428
|
-
const
|
|
478
|
+
const terminalIndex = live.findLastIndex(sendId => !auxiliarySendIds.has(sendId));
|
|
479
|
+
const terminal = live[terminalIndex];
|
|
429
480
|
const reported = typeof usage?.inputTokens === "number" || typeof usage?.outputTokens === "number";
|
|
430
|
-
if (reported) {
|
|
481
|
+
if (terminal && reported) {
|
|
431
482
|
ledger().settle(terminal, {
|
|
432
483
|
inputTokens: usage?.inputTokens ?? 0,
|
|
433
484
|
outputTokens: usage?.outputTokens ?? 0,
|
|
434
485
|
});
|
|
435
|
-
} else {
|
|
486
|
+
} else if (terminal) {
|
|
436
487
|
// The response never reported usage. It may still have been billed.
|
|
437
488
|
ledger().markLost(terminal);
|
|
438
489
|
}
|
|
439
|
-
live.
|
|
490
|
+
if (terminalIndex >= 0) live.splice(terminalIndex, 1);
|
|
440
491
|
terminalProcessed = true;
|
|
441
492
|
}
|
|
442
493
|
while (live.length > 0) {
|
|
@@ -454,6 +505,7 @@ export function createRequestSpendTracker(
|
|
|
454
505
|
}
|
|
455
506
|
},
|
|
456
507
|
get refusals(): number { return refusals; },
|
|
508
|
+
get lastRefusal() { return lastRefusal; },
|
|
457
509
|
};
|
|
458
510
|
return tracker;
|
|
459
511
|
}
|