@on-par/factory-core 0.9.25-next.1 → 0.9.25-next.3
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/dist/internal.d.ts +19 -54
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +17 -33
- package/dist/internal.js.map +1 -1
- package/dist/local-small/stepwise.d.ts +0 -38
- package/dist/local-small/stepwise.d.ts.map +1 -1
- package/dist/local-small/stepwise.js +1 -129
- package/dist/local-small/stepwise.js.map +1 -1
- package/dist/public-api-keys.d.ts.map +1 -1
- package/dist/public-api-keys.js +0 -1
- package/dist/public-api-keys.js.map +1 -1
- package/dist/queue/activity.d.ts +1 -2
- package/dist/queue/activity.d.ts.map +1 -1
- package/dist/queue/activity.js +2 -4
- package/dist/queue/activity.js.map +1 -1
- package/dist/queue/internal-exports.d.ts +3 -4
- package/dist/queue/internal-exports.d.ts.map +1 -1
- package/dist/queue/internal-exports.js +3 -3
- package/dist/queue/internal-exports.js.map +1 -1
- package/dist/queue/project-board-poller.d.ts +0 -25
- package/dist/queue/project-board-poller.d.ts.map +1 -1
- package/dist/queue/project-board-poller.js +2 -261
- package/dist/queue/project-board-poller.js.map +1 -1
- package/dist/queue/unpark.d.ts +0 -2
- package/dist/queue/unpark.d.ts.map +1 -1
- package/dist/queue/unpark.js +0 -2
- package/dist/queue/unpark.js.map +1 -1
- package/dist/usage/constants.d.ts.map +1 -1
- package/dist/usage/constants.js +1 -4
- package/dist/usage/constants.js.map +1 -1
- package/package.json +5 -5
- package/dist/daemon/engine-supervisor.d.ts +0 -52
- package/dist/daemon/engine-supervisor.d.ts.map +0 -1
- package/dist/daemon/engine-supervisor.js +0 -119
- package/dist/daemon/engine-supervisor.js.map +0 -1
- package/dist/daemon/run-repo.d.ts +0 -13
- package/dist/daemon/run-repo.d.ts.map +0 -1
- package/dist/daemon/run-repo.js +0 -15
- package/dist/daemon/run-repo.js.map +0 -1
- package/dist/queue/prerequisite-order.d.ts +0 -30
- package/dist/queue/prerequisite-order.d.ts.map +0 -1
- package/dist/queue/prerequisite-order.js +0 -92
- package/dist/queue/prerequisite-order.js.map +0 -1
- package/dist/usage/coordinator.d.ts +0 -47
- package/dist/usage/coordinator.d.ts.map +0 -1
- package/dist/usage/coordinator.js +0 -183
- package/dist/usage/coordinator.js.map +0 -1
- package/dist/usage/grant-ledger.d.ts +0 -45
- package/dist/usage/grant-ledger.d.ts.map +0 -1
- package/dist/usage/grant-ledger.js +0 -80
- package/dist/usage/grant-ledger.js.map +0 -1
- package/dist/usage/lane-scheduler.d.ts +0 -19
- package/dist/usage/lane-scheduler.d.ts.map +0 -1
- package/dist/usage/lane-scheduler.js +0 -31
- package/dist/usage/lane-scheduler.js.map +0 -1
- package/dist/usage/local-coordinator.d.ts +0 -12
- package/dist/usage/local-coordinator.d.ts.map +0 -1
- package/dist/usage/local-coordinator.js +0 -54
- package/dist/usage/local-coordinator.js.map +0 -1
- package/dist/usage/select-coordinator.d.ts +0 -8
- package/dist/usage/select-coordinator.d.ts.map +0 -1
- package/dist/usage/select-coordinator.js +0 -6
- package/dist/usage/select-coordinator.js.map +0 -1
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
// src/daemon/engine-supervisor.ts — Per-engine liveness supervision for factoryd
|
|
2
|
-
// (#1178, epic #764). One supervisor owns one in-process engine for one attached
|
|
3
|
-
// repo: staleness is measured from the engine event stream (events.ndjson
|
|
4
|
-
// activity, ADR-0002), a stale engine is stopped and relaunched in-process, and
|
|
5
|
-
// every restart publishes exactly one 'engine-restarted' event. Restart is a
|
|
6
|
-
// pure lifecycle action: unlike the legacy relaunch-if-dead.sh cron script, the
|
|
7
|
-
// restart path never snapshots or backs up queue state — the logEvent append is
|
|
8
|
-
// this module's ONLY filesystem write (see the ADR shipped with #1178).
|
|
9
|
-
import { stat } from 'node:fs/promises';
|
|
10
|
-
import { logEvent } from '../utils/index.js';
|
|
11
|
-
import { createDaemonLaneContext } from './lane-context.js';
|
|
12
|
-
import { dispatchableRepos } from './registry.js';
|
|
13
|
-
export const DEFAULT_STALE_THRESHOLD_MS = 15 * 60_000;
|
|
14
|
-
export const DEFAULT_SUPERVISOR_POLL_MS = 60_000;
|
|
15
|
-
async function defaultLastEventAt(context) {
|
|
16
|
-
try {
|
|
17
|
-
return (await stat(context.paths.events)).mtimeMs;
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
/** Supervises one in-process engine for one registry entry. Callers own
|
|
24
|
-
* start()/stop(); checkNow() is the deterministic seam the poll interval
|
|
25
|
-
* drives (mirrors project-board-poller). */
|
|
26
|
-
export function superviseEngine(entry, runEngine, opts = {}) {
|
|
27
|
-
const staleThresholdMs = opts.staleThresholdMs ?? DEFAULT_STALE_THRESHOLD_MS;
|
|
28
|
-
const pollMs = opts.pollMs ?? DEFAULT_SUPERVISOR_POLL_MS;
|
|
29
|
-
const now = opts.now ?? Date.now;
|
|
30
|
-
const lastEventAt = opts.lastEventAt ?? defaultLastEventAt;
|
|
31
|
-
const log = opts.log ?? ((line) => console.error(line));
|
|
32
|
-
const context = createDaemonLaneContext(entry);
|
|
33
|
-
let handle = null;
|
|
34
|
-
let startedAt = 0;
|
|
35
|
-
let restartCount = 0;
|
|
36
|
-
let timer = null;
|
|
37
|
-
let checking = false;
|
|
38
|
-
let stopped = false;
|
|
39
|
-
const launch = () => {
|
|
40
|
-
handle = runEngine(context);
|
|
41
|
-
startedAt = now();
|
|
42
|
-
// A crashing engine must never raise an unhandled rejection: the staleness
|
|
43
|
-
// poll is what notices a dead engine — events stop, the threshold passes,
|
|
44
|
-
// the restart happens (the cron script's liveness idea, moved in-process).
|
|
45
|
-
handle.done.catch(() => { });
|
|
46
|
-
};
|
|
47
|
-
const checkNow = async () => {
|
|
48
|
-
if (handle === null || stopped || checking)
|
|
49
|
-
return 'idle';
|
|
50
|
-
checking = true;
|
|
51
|
-
try {
|
|
52
|
-
const last = await lastEventAt(context);
|
|
53
|
-
// stop() may have run while we awaited — it already stopped the engine,
|
|
54
|
-
// and nothing may relaunch after it (the no-restart-after-stop contract).
|
|
55
|
-
if (stopped)
|
|
56
|
-
return 'idle';
|
|
57
|
-
// The start-time floor keeps a just-(re)started engine with no events yet
|
|
58
|
-
// from being instantly stale.
|
|
59
|
-
const effective = Math.max(last ?? 0, startedAt);
|
|
60
|
-
const age = now() - effective;
|
|
61
|
-
if (age <= staleThresholdMs)
|
|
62
|
-
return 'fresh';
|
|
63
|
-
const stale = handle;
|
|
64
|
-
try {
|
|
65
|
-
await stale.stop();
|
|
66
|
-
}
|
|
67
|
-
catch (err) {
|
|
68
|
-
log(`engine-supervisor[${entry.slug}]: stop of stale engine failed: ${String(err)}`);
|
|
69
|
-
}
|
|
70
|
-
if (stopped)
|
|
71
|
-
return 'idle';
|
|
72
|
-
launch();
|
|
73
|
-
restartCount += 1;
|
|
74
|
-
logEvent(context.paths.events, 'engine-restarted', entry.slug, `engine stale for ${age}ms (threshold ${staleThresholdMs}ms) — restarted in-process`, { lane: entry.slug, actor: 'factoryd' });
|
|
75
|
-
return 'restarted';
|
|
76
|
-
}
|
|
77
|
-
finally {
|
|
78
|
-
checking = false;
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
return {
|
|
82
|
-
context,
|
|
83
|
-
start() {
|
|
84
|
-
if (stopped || handle !== null)
|
|
85
|
-
return;
|
|
86
|
-
launch();
|
|
87
|
-
timer = setInterval(() => {
|
|
88
|
-
void checkNow().catch((err) => {
|
|
89
|
-
log(`engine-supervisor[${entry.slug}]: staleness check failed: ${String(err)}`);
|
|
90
|
-
});
|
|
91
|
-
}, pollMs);
|
|
92
|
-
timer.unref?.();
|
|
93
|
-
},
|
|
94
|
-
checkNow,
|
|
95
|
-
restarts() {
|
|
96
|
-
return restartCount;
|
|
97
|
-
},
|
|
98
|
-
async stop() {
|
|
99
|
-
stopped = true;
|
|
100
|
-
if (timer !== null) {
|
|
101
|
-
clearInterval(timer);
|
|
102
|
-
timer = null;
|
|
103
|
-
}
|
|
104
|
-
try {
|
|
105
|
-
await handle?.stop();
|
|
106
|
-
}
|
|
107
|
-
catch (err) {
|
|
108
|
-
log(`engine-supervisor[${entry.slug}]: stop failed: ${String(err)}`);
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
/** One supervisor per dispatchable (state === 'active') registry entry, keyed by
|
|
114
|
-
* slug. Paused/draining/detached entries are never supervised. Callers own
|
|
115
|
-
* start()/stop() on each. */
|
|
116
|
-
export function superviseActiveRepos(registry, runEngine, opts = {}) {
|
|
117
|
-
return new Map(dispatchableRepos(registry).map((entry) => [entry.slug, superviseEngine(entry, runEngine, opts)]));
|
|
118
|
-
}
|
|
119
|
-
//# sourceMappingURL=engine-supervisor.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"engine-supervisor.js","sourceRoot":"","sources":["../../src/daemon/engine-supervisor.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,iFAAiF;AACjF,0EAA0E;AAC1E,gFAAgF;AAChF,6EAA6E;AAC7E,gFAAgF;AAChF,gFAAgF;AAChF,wEAAwE;AAExE,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAExC,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,uBAAuB,EAA0B,MAAM,mBAAmB,CAAC;AACpF,OAAO,EAAE,iBAAiB,EAA+C,MAAM,eAAe,CAAC;AAe/F,MAAM,CAAC,MAAM,0BAA0B,GAAG,EAAE,GAAG,MAAM,CAAC;AACtD,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAgCjD,KAAK,UAAU,kBAAkB,CAAC,OAA0B;IAC1D,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;IACpD,CAAC;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BACvB,KAAK,QAAQ,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;oBAC5B,GAAG,CAAC,qBAAqB,KAAK,CAAC,IAAI,8BAA8B,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;gBAClF,CAAC,CAAC,CAAC;YACL,CAAC,EAAE,MAAM,CAAC,CAAC;YACX,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;QAClB,CAAC;QACD,QAAQ;QACR,QAAQ;YACN,OAAO,YAAY,CAAC;QACtB,CAAC;QACD,KAAK,CAAC,IAAI;YACR,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACnB,aAAa,CAAC,KAAK,CAAC,CAAC;gBACrB,KAAK,GAAG,IAAI,CAAC;YACf,CAAC;YACD,IAAI,CAAC;gBACH,MAAM,MAAM,EAAE,IAAI,EAAE,CAAC;YACvB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,GAAG,CAAC,qBAAqB,KAAK,CAAC,IAAI,mBAAmB,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YACvE,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;8BAE8B;AAC9B,MAAM,UAAU,oBAAoB,CAClC,QAAsB,EACtB,SAAuB,EACvB,IAAI,GAA4B,EAAE;IAElC,OAAO,IAAI,GAAG,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,eAAe,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACpH,CAAC"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { type DaemonLaneContext } from './lane-context.js';
|
|
2
|
-
import type { RepoRegistryListing } from './registry.js';
|
|
3
|
-
/** The orchestration entry the daemon hands a resolved lane context to. Injected as a
|
|
4
|
-
* port so core never imports the CLI's runLane (contracts <- core <- cli); the daemon
|
|
5
|
-
* caller wires the real orchestration. */
|
|
6
|
-
export type DaemonOrchestrator = (context: DaemonLaneContext) => Promise<void>;
|
|
7
|
-
/** Runs orchestration for one daemon-managed registry entry. The lane context resolves
|
|
8
|
-
* factory-state paths from the entry's external `stateRoot` (via createDaemonLaneContext
|
|
9
|
-
* / getFactoryPaths), falling back to the checkout's `.factory/` only when no stateRoot
|
|
10
|
-
* is registered, so the injected orchestrator never sees checkout-local paths for an
|
|
11
|
-
* externally-rooted repo. Returns the context it dispatched with. */
|
|
12
|
-
export declare function runDaemonRepo(entry: RepoRegistryListing, orchestrate: DaemonOrchestrator): Promise<DaemonLaneContext>;
|
|
13
|
-
//# sourceMappingURL=run-repo.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"run-repo.d.ts","sourceRoot":"","sources":["../../src/daemon/run-repo.ts"],"names":[],"mappings":"AAIA,OAAO,EAA2B,KAAK,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACpF,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEzD;;2CAE2C;AAC3C,MAAM,MAAM,kBAAkB,GAAG,CAAC,OAAO,EAAE,iBAAiB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAE/E;;;;sEAIsE;AACtE,wBAAsB,aAAa,CACjC,KAAK,EAAE,mBAAmB,EAC1B,WAAW,EAAE,kBAAkB,GAC9B,OAAO,CAAC,iBAAiB,CAAC,CAI5B"}
|
package/dist/daemon/run-repo.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// src/daemon/run-repo.ts — Dispatches orchestration for one daemon-managed
|
|
2
|
-
// registry entry, handing the injected runner a lane context whose paths are
|
|
3
|
-
// rooted at the entry's registered external stateRoot (#843, #1041).
|
|
4
|
-
import { createDaemonLaneContext } from './lane-context.js';
|
|
5
|
-
/** Runs orchestration for one daemon-managed registry entry. The lane context resolves
|
|
6
|
-
* factory-state paths from the entry's external `stateRoot` (via createDaemonLaneContext
|
|
7
|
-
* / getFactoryPaths), falling back to the checkout's `.factory/` only when no stateRoot
|
|
8
|
-
* is registered, so the injected orchestrator never sees checkout-local paths for an
|
|
9
|
-
* externally-rooted repo. Returns the context it dispatched with. */
|
|
10
|
-
export async function runDaemonRepo(entry, orchestrate) {
|
|
11
|
-
const context = createDaemonLaneContext(entry);
|
|
12
|
-
await orchestrate(context);
|
|
13
|
-
return context;
|
|
14
|
-
}
|
|
15
|
-
//# sourceMappingURL=run-repo.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"run-repo.js","sourceRoot":"","sources":["../../src/daemon/run-repo.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,6EAA6E;AAC7E,qEAAqE;AAErE,OAAO,EAAE,uBAAuB,EAA0B,MAAM,mBAAmB,CAAC;AAQpF;;;;sEAIsE;AACtE,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,KAA0B,EAC1B,WAA+B;IAE/B,MAAM,OAAO,GAAG,uBAAuB,CAAC,KAAK,CAAC,CAAC;IAC/C,MAAM,WAAW,CAAC,OAAO,CAAC,CAAC;IAC3B,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { BlockedByItem } from '../blockers/dependencies.js';
|
|
2
|
-
export type PrerequisiteOrderResult = {
|
|
3
|
-
ok: true;
|
|
4
|
-
order: number[];
|
|
5
|
-
} | {
|
|
6
|
-
ok: false;
|
|
7
|
-
cycle: number[];
|
|
8
|
-
};
|
|
9
|
-
export interface BatchPrerequisiteReaders {
|
|
10
|
-
repo: string;
|
|
11
|
-
readIssueBody(issue: number): Promise<string>;
|
|
12
|
-
listBlockedBy(issue: number): Promise<BlockedByItem[]>;
|
|
13
|
-
}
|
|
14
|
-
export type BatchPrerequisitesResult = {
|
|
15
|
-
ok: true;
|
|
16
|
-
prereqsOf: Map<number, number[]>;
|
|
17
|
-
} | {
|
|
18
|
-
ok: false;
|
|
19
|
-
issue: number;
|
|
20
|
-
error: string;
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* Stable topological sort: every in-batch prerequisite precedes the issues that
|
|
24
|
-
* need it; ties keep the given order. Out-of-batch prerequisites are ignored.
|
|
25
|
-
* A cycle yields the cycle (first issue repeated) instead of an order.
|
|
26
|
-
*/
|
|
27
|
-
export declare function orderByPrerequisites(issues: readonly number[], prereqsOf: ReadonlyMap<number, readonly number[]>): PrerequisiteOrderResult;
|
|
28
|
-
/** Build the prerequisite map for a batch from declared `Prerequisite: #N` lines and open same-repo native blocked_by items. */
|
|
29
|
-
export declare function collectBatchPrerequisites(issues: readonly number[], readers: BatchPrerequisiteReaders): Promise<BatchPrerequisitesResult>;
|
|
30
|
-
//# sourceMappingURL=prerequisite-order.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"prerequisite-order.d.ts","sourceRoot":"","sources":["../../src/queue/prerequisite-order.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAEjE,MAAM,MAAM,uBAAuB,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAErG,MAAM,WAAW,wBAAwB;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC9C,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC;CACxD;AAED,MAAM,MAAM,wBAAwB,GAClC;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AA6C/F;;;;GAIG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,SAAS,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,GAChD,uBAAuB,CAwBzB;AAED,gIAAgI;AAChI,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,OAAO,EAAE,wBAAwB,GAChC,OAAO,CAAC,wBAAwB,CAAC,CAoBnC"}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
// src/queue/prerequisite-order.ts — order an unpark batch prerequisite-first (#3330).
|
|
2
|
-
import { findDeclaredPrerequisites } from '../blockers/declared-prerequisites.js';
|
|
3
|
-
function errorMessage(error) {
|
|
4
|
-
return error instanceof Error ? error.message : String(error);
|
|
5
|
-
}
|
|
6
|
-
function findCycle(batch, remaining, deps) {
|
|
7
|
-
const visited = new Set();
|
|
8
|
-
const stack = [];
|
|
9
|
-
const onStack = new Set();
|
|
10
|
-
const visit = (node) => {
|
|
11
|
-
if (onStack.has(node)) {
|
|
12
|
-
return [...stack.slice(stack.indexOf(node)), node];
|
|
13
|
-
}
|
|
14
|
-
if (visited.has(node)) {
|
|
15
|
-
return undefined;
|
|
16
|
-
}
|
|
17
|
-
visited.add(node);
|
|
18
|
-
stack.push(node);
|
|
19
|
-
onStack.add(node);
|
|
20
|
-
for (const dep of deps.get(node) ?? []) {
|
|
21
|
-
if (remaining.has(dep)) {
|
|
22
|
-
const found = visit(dep);
|
|
23
|
-
if (found) {
|
|
24
|
-
return found;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
stack.pop();
|
|
29
|
-
onStack.delete(node);
|
|
30
|
-
return undefined;
|
|
31
|
-
};
|
|
32
|
-
for (const start of batch) {
|
|
33
|
-
if (remaining.has(start)) {
|
|
34
|
-
const found = visit(start);
|
|
35
|
-
if (found) {
|
|
36
|
-
return found;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return [];
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Stable topological sort: every in-batch prerequisite precedes the issues that
|
|
44
|
-
* need it; ties keep the given order. Out-of-batch prerequisites are ignored.
|
|
45
|
-
* A cycle yields the cycle (first issue repeated) instead of an order.
|
|
46
|
-
*/
|
|
47
|
-
export function orderByPrerequisites(issues, prereqsOf) {
|
|
48
|
-
const batch = [...new Set(issues)];
|
|
49
|
-
const inBatch = new Set(batch);
|
|
50
|
-
const deps = new Map(batch.map((n) => [n, [...new Set((prereqsOf.get(n) ?? []).filter((p) => inBatch.has(p)))]]));
|
|
51
|
-
const remaining = new Set(batch);
|
|
52
|
-
const emitted = new Set();
|
|
53
|
-
const order = [];
|
|
54
|
-
for (;;) {
|
|
55
|
-
const next = batch.find((n) => remaining.has(n) && (deps.get(n) ?? []).every((p) => emitted.has(p)));
|
|
56
|
-
if (next === undefined) {
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
order.push(next);
|
|
60
|
-
remaining.delete(next);
|
|
61
|
-
emitted.add(next);
|
|
62
|
-
}
|
|
63
|
-
if (remaining.size === 0) {
|
|
64
|
-
return { ok: true, order };
|
|
65
|
-
}
|
|
66
|
-
return { ok: false, cycle: findCycle(batch, remaining, deps) };
|
|
67
|
-
}
|
|
68
|
-
/** Build the prerequisite map for a batch from declared `Prerequisite: #N` lines and open same-repo native blocked_by items. */
|
|
69
|
-
export async function collectBatchPrerequisites(issues, readers) {
|
|
70
|
-
const prereqsOf = new Map();
|
|
71
|
-
const repo = readers.repo.toLowerCase();
|
|
72
|
-
for (const n of new Set(issues)) {
|
|
73
|
-
let body;
|
|
74
|
-
try {
|
|
75
|
-
body = await readers.readIssueBody(n);
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
return { ok: false, issue: n, error: `#${n}: could not read issue body: ${errorMessage(error)}` };
|
|
79
|
-
}
|
|
80
|
-
let items;
|
|
81
|
-
try {
|
|
82
|
-
items = await readers.listBlockedBy(n);
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
return { ok: false, issue: n, error: `#${n}: could not list blocked_by: ${errorMessage(error)}` };
|
|
86
|
-
}
|
|
87
|
-
const blocked = items.filter((i) => i.state === 'open' && i.repository.toLowerCase() === repo).map((i) => i.number);
|
|
88
|
-
prereqsOf.set(n, [...new Set([...findDeclaredPrerequisites(body), ...blocked])]);
|
|
89
|
-
}
|
|
90
|
-
return { ok: true, prereqsOf };
|
|
91
|
-
}
|
|
92
|
-
//# sourceMappingURL=prerequisite-order.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"prerequisite-order.js","sourceRoot":"","sources":["../../src/queue/prerequisite-order.ts"],"names":[],"mappings":"AAAA,sFAAsF;AAEtF,OAAO,EAAE,yBAAyB,EAAE,MAAM,uCAAuC,CAAC;AAclF,SAAS,YAAY,CAAC,KAAc;IAClC,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChE,CAAC;AAED,SAAS,SAAS,CAAC,KAAwB,EAAE,SAA8B,EAAE,IAA2B;IACtG,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAClC,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAElC,MAAM,KAAK,GAAG,CAAC,IAAY,EAAwB,EAAE;QACnD,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YACtB,OAAO,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAA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,CAAC,CAAC,CAAC;IACnF,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;AACjC,CAAC"}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { FactoryLogger } from '../logger/index.js';
|
|
2
|
-
import type { AcquireResult, GrantRequest } from './grant-ledger.js';
|
|
3
|
-
import { DEFAULT_USAGE_POLL_MS } from './constants.js';
|
|
4
|
-
import type { SubscriptionUsage, SubscriptionUsageDeps } from './subscription.js';
|
|
5
|
-
export { DEFAULT_USAGE_POLL_MS };
|
|
6
|
-
export interface UsageCoordinatorState {
|
|
7
|
-
version: 1;
|
|
8
|
-
snapshot: SubscriptionUsage | null;
|
|
9
|
-
fetchedAt: string | null;
|
|
10
|
-
}
|
|
11
|
-
export interface WriteUsageStateOptions {
|
|
12
|
-
writeFile?: (file: string, data: string) => Promise<void>;
|
|
13
|
-
rename?: (from: string, to: string) => Promise<void>;
|
|
14
|
-
}
|
|
15
|
-
export interface UsageCoordinatorOptions {
|
|
16
|
-
statePath: string;
|
|
17
|
-
logger: FactoryLogger;
|
|
18
|
-
pollMs?: number;
|
|
19
|
-
subscriptionDeps?: SubscriptionUsageDeps;
|
|
20
|
-
fetchSubscription?: () => Promise<SubscriptionUsage | null>;
|
|
21
|
-
now?: () => number;
|
|
22
|
-
writeState?: (file: string, state: UsageCoordinatorState) => Promise<void>;
|
|
23
|
-
grantsPath?: string;
|
|
24
|
-
admissionCeilingPct?: number;
|
|
25
|
-
grantReservationPct?: number;
|
|
26
|
-
grantTtlMs?: number;
|
|
27
|
-
withLock?: <T>(fn: () => Promise<T>) => Promise<T>;
|
|
28
|
-
randomId?: () => string;
|
|
29
|
-
}
|
|
30
|
-
export interface UsageCoordinator {
|
|
31
|
-
start(): Promise<void>;
|
|
32
|
-
stop(): void;
|
|
33
|
-
pollNow(): Promise<SubscriptionUsage | null>;
|
|
34
|
-
read(): SubscriptionUsage | null;
|
|
35
|
-
acquire(request: GrantRequest): Promise<AcquireResult>;
|
|
36
|
-
}
|
|
37
|
-
export declare function defaultUsageStatePath(home?: string): string;
|
|
38
|
-
/** Reads the persisted coordinator state. A missing file, unparsable JSON, or a
|
|
39
|
-
* non-object payload all yield empty state — this NEVER throws and NEVER
|
|
40
|
-
* creates the file. A malformed `snapshot` drops to `null`; a malformed
|
|
41
|
-
* `fetchedAt` drops to `null`. */
|
|
42
|
-
export declare function loadUsageState(file: string): Promise<UsageCoordinatorState>;
|
|
43
|
-
/** Atomic write: mkdir the parent, serialize to `${file}.tmp`, then rename it
|
|
44
|
-
* onto `file`. Mirrors `writeRegistry` in `daemon/registry.ts`. */
|
|
45
|
-
export declare function writeUsageState(file: string, state: UsageCoordinatorState, opts?: WriteUsageStateOptions): Promise<void>;
|
|
46
|
-
export declare function createUsageCoordinator(options: UsageCoordinatorOptions): UsageCoordinator;
|
|
47
|
-
//# sourceMappingURL=coordinator.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"coordinator.d.ts","sourceRoot":"","sources":["../../src/usage/coordinator.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAExD,OAAO,KAAK,EAAE,aAAa,EAAoB,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAWvF,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAGlF,OAAO,EAAE,qBAAqB,EAAE,CAAC;AAEjC,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,CAAC,CAAC;IACX,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAC;IACnC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED,MAAM,WAAW,sBAAsB;IACrC,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,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mCAGmC;AACnC,wBAAsB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAuBjF;AAED;oEACoE;AACpE,wBAAsB,eAAe,CACnC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,qBAAqB,EAC5B,IAAI,GAAE,sBAA2B,GAChC,OAAO,CAAC,IAAI,CAAC,CAOf;AAQD,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,uBAAuB,GAAG,gBAAgB,CAoHzF"}
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
// src/usage/coordinator.ts — Single-poller cached subscription-usage snapshot (#1029)
|
|
2
|
-
// and admission-control acquire() API backed by a shared grant ledger (#1030).
|
|
3
|
-
import { randomUUID } from 'node:crypto';
|
|
4
|
-
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
5
|
-
import { homedir } from 'node:os';
|
|
6
|
-
import { dirname, join } from 'node:path';
|
|
7
|
-
import { withFileLock } from '../utils/lock.js';
|
|
8
|
-
import { defaultGrantLedgerPath, DEFAULT_GRANT_TTL_MS, isCappedModel, loadGrantLedger, pruneGrants, USAGE_ADMISSION_CEILING_PCT, USAGE_GRANT_RESERVATION_PCT, writeGrantLedger, } from './grant-ledger.js';
|
|
9
|
-
import { DEFAULT_USAGE_POLL_MS } from './constants.js';
|
|
10
|
-
import { fetchSubscriptionUsage } from './subscription.js';
|
|
11
|
-
export { DEFAULT_USAGE_POLL_MS };
|
|
12
|
-
export function defaultUsageStatePath(home) {
|
|
13
|
-
return join(home ?? homedir(), '.factory', 'usage', 'coordinator.json');
|
|
14
|
-
}
|
|
15
|
-
function emptyUsageState() {
|
|
16
|
-
return { version: 1, snapshot: null, fetchedAt: null };
|
|
17
|
-
}
|
|
18
|
-
function isSnapshot(value) {
|
|
19
|
-
if (!value || typeof value !== 'object')
|
|
20
|
-
return false;
|
|
21
|
-
const s = value;
|
|
22
|
-
return (typeof s.fiveHourUtilization === 'number' && (s.fiveHourResetsAt === null || typeof s.fiveHourResetsAt === 'string'));
|
|
23
|
-
}
|
|
24
|
-
/** Reads the persisted coordinator state. A missing file, unparsable JSON, or a
|
|
25
|
-
* non-object payload all yield empty state — this NEVER throws and NEVER
|
|
26
|
-
* creates the file. A malformed `snapshot` drops to `null`; a malformed
|
|
27
|
-
* `fetchedAt` drops to `null`. */
|
|
28
|
-
export async function loadUsageState(file) {
|
|
29
|
-
let raw;
|
|
30
|
-
try {
|
|
31
|
-
raw = await readFile(file, 'utf-8');
|
|
32
|
-
}
|
|
33
|
-
catch {
|
|
34
|
-
return emptyUsageState();
|
|
35
|
-
}
|
|
36
|
-
let parsed;
|
|
37
|
-
try {
|
|
38
|
-
parsed = JSON.parse(raw);
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
return emptyUsageState();
|
|
42
|
-
}
|
|
43
|
-
if (!parsed || typeof parsed !== 'object')
|
|
44
|
-
return emptyUsageState();
|
|
45
|
-
const candidate = parsed;
|
|
46
|
-
const snapshot = isSnapshot(candidate.snapshot)
|
|
47
|
-
? {
|
|
48
|
-
fiveHourUtilization: candidate.snapshot.fiveHourUtilization,
|
|
49
|
-
fiveHourResetsAt: candidate.snapshot.fiveHourResetsAt,
|
|
50
|
-
}
|
|
51
|
-
: null;
|
|
52
|
-
const fetchedAt = typeof candidate.fetchedAt === 'string' ? candidate.fetchedAt : null;
|
|
53
|
-
return { version: 1, snapshot, fetchedAt };
|
|
54
|
-
}
|
|
55
|
-
/** Atomic write: mkdir the parent, serialize to `${file}.tmp`, then rename it
|
|
56
|
-
* onto `file`. Mirrors `writeRegistry` in `daemon/registry.ts`. */
|
|
57
|
-
export async function writeUsageState(file, state, opts = {}) {
|
|
58
|
-
const write = opts.writeFile ?? ((f, data) => writeFile(f, data));
|
|
59
|
-
const move = opts.rename ?? ((from, to) => rename(from, to));
|
|
60
|
-
await mkdir(dirname(file), { recursive: true });
|
|
61
|
-
const tmp = `${file}.tmp`;
|
|
62
|
-
await write(tmp, `${JSON.stringify(state, null, 2)}\n`);
|
|
63
|
-
await move(tmp, file);
|
|
64
|
-
}
|
|
65
|
-
function validatePollMs(options) {
|
|
66
|
-
const pollMs = options.pollMs ?? DEFAULT_USAGE_POLL_MS;
|
|
67
|
-
if (!Number.isFinite(pollMs) || pollMs <= 0)
|
|
68
|
-
throw new RangeError('Usage coordinator poll interval must be positive');
|
|
69
|
-
return pollMs;
|
|
70
|
-
}
|
|
71
|
-
export function createUsageCoordinator(options) {
|
|
72
|
-
const pollMs = validatePollMs(options);
|
|
73
|
-
const { statePath, logger } = options;
|
|
74
|
-
const fetchSubscription = options.fetchSubscription ?? (() => fetchSubscriptionUsage(options.subscriptionDeps ?? {}));
|
|
75
|
-
const now = options.now ?? options.subscriptionDeps?.now ?? Date.now;
|
|
76
|
-
const writeState = options.writeState ?? ((f, s) => writeUsageState(f, s));
|
|
77
|
-
const grantsPath = options.grantsPath ?? defaultGrantLedgerPath();
|
|
78
|
-
const admissionCeilingPct = options.admissionCeilingPct ?? USAGE_ADMISSION_CEILING_PCT;
|
|
79
|
-
const grantReservationPct = options.grantReservationPct ?? USAGE_GRANT_RESERVATION_PCT;
|
|
80
|
-
const grantTtlMs = options.grantTtlMs ?? DEFAULT_GRANT_TTL_MS;
|
|
81
|
-
const randomId = options.randomId ?? randomUUID;
|
|
82
|
-
const withLock = options.withLock ?? ((fn) => withFileLock(`${grantsPath}.lock`, fn));
|
|
83
|
-
let cachedSnapshot = null;
|
|
84
|
-
let interval;
|
|
85
|
-
let started = false;
|
|
86
|
-
let inFlight;
|
|
87
|
-
async function refresh() {
|
|
88
|
-
try {
|
|
89
|
-
const result = await fetchSubscription();
|
|
90
|
-
if (result !== null) {
|
|
91
|
-
cachedSnapshot = result;
|
|
92
|
-
await writeState(statePath, { version: 1, snapshot: result, fetchedAt: new Date(now()).toISOString() });
|
|
93
|
-
logger.info('usage_coordinator_poll_succeeded', 'Usage coordinator poll succeeded', {
|
|
94
|
-
actor: 'daemon/usage-coordinator',
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
logger.warn('usage_coordinator_poll_empty', 'Usage coordinator poll returned no snapshot', {
|
|
99
|
-
actor: 'daemon/usage-coordinator',
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
return cachedSnapshot;
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
106
|
-
logger.warn('usage_coordinator_poll_failed', `Usage coordinator poll failed: ${message}`, {
|
|
107
|
-
actor: 'daemon/usage-coordinator',
|
|
108
|
-
});
|
|
109
|
-
return cachedSnapshot;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
function pollNow() {
|
|
113
|
-
if (inFlight !== undefined)
|
|
114
|
-
return inFlight;
|
|
115
|
-
inFlight = refresh().finally(() => {
|
|
116
|
-
inFlight = undefined;
|
|
117
|
-
});
|
|
118
|
-
return inFlight;
|
|
119
|
-
}
|
|
120
|
-
async function start() {
|
|
121
|
-
started = true;
|
|
122
|
-
const persisted = await loadUsageState(statePath);
|
|
123
|
-
if (cachedSnapshot === null)
|
|
124
|
-
cachedSnapshot = persisted.snapshot;
|
|
125
|
-
await pollNow();
|
|
126
|
-
if (started && interval === undefined) {
|
|
127
|
-
interval = setInterval(() => {
|
|
128
|
-
void pollNow();
|
|
129
|
-
}, pollMs);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
function stop() {
|
|
133
|
-
started = false;
|
|
134
|
-
if (interval !== undefined)
|
|
135
|
-
clearInterval(interval);
|
|
136
|
-
interval = undefined;
|
|
137
|
-
}
|
|
138
|
-
function read() {
|
|
139
|
-
return cachedSnapshot === null ? null : { ...cachedSnapshot };
|
|
140
|
-
}
|
|
141
|
-
async function acquire(request) {
|
|
142
|
-
// Non-Claude routes (codex, opencode) have no observable subscription signal,
|
|
143
|
-
// so they pass through ungated — no lock, snapshot, or grant-ledger access,
|
|
144
|
-
// and never a fabricated retryAfter/utilization figure.
|
|
145
|
-
if (!isCappedModel(request.model))
|
|
146
|
-
return { granted: true };
|
|
147
|
-
return withLock(async () => {
|
|
148
|
-
const snapshot = cachedSnapshot;
|
|
149
|
-
const at = now();
|
|
150
|
-
const retryAfter = () => {
|
|
151
|
-
const resetsAt = snapshot?.fiveHourResetsAt;
|
|
152
|
-
if (!resetsAt)
|
|
153
|
-
return pollMs;
|
|
154
|
-
const t = Date.parse(resetsAt);
|
|
155
|
-
return Number.isNaN(t) ? pollMs : Math.max(0, t - at);
|
|
156
|
-
};
|
|
157
|
-
// No usage signal yet: deny conservatively rather than admit blind.
|
|
158
|
-
if (snapshot === null)
|
|
159
|
-
return { granted: false, retryAfter: pollMs };
|
|
160
|
-
const ledger = await loadGrantLedger(grantsPath);
|
|
161
|
-
const outstanding = pruneGrants(ledger.grants, at, grantTtlMs);
|
|
162
|
-
const reserved = outstanding.reduce((sum, g) => sum + g.reservationPct, 0);
|
|
163
|
-
const projected = snapshot.fiveHourUtilization + reserved + grantReservationPct;
|
|
164
|
-
if (projected > admissionCeilingPct) {
|
|
165
|
-
await writeGrantLedger(grantsPath, { version: 1, grants: outstanding });
|
|
166
|
-
return { granted: false, retryAfter: retryAfter() };
|
|
167
|
-
}
|
|
168
|
-
const entry = {
|
|
169
|
-
id: randomId(),
|
|
170
|
-
repo: request.repo,
|
|
171
|
-
lane: request.lane,
|
|
172
|
-
phase: request.phase,
|
|
173
|
-
model: request.model,
|
|
174
|
-
grantedAt: new Date(at).toISOString(),
|
|
175
|
-
reservationPct: grantReservationPct,
|
|
176
|
-
};
|
|
177
|
-
await writeGrantLedger(grantsPath, { version: 1, grants: [...outstanding, entry] });
|
|
178
|
-
return { granted: true };
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
return { start, stop, pollNow, read, acquire };
|
|
182
|
-
}
|
|
183
|
-
//# sourceMappingURL=coordinator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"coordinator.js","sourceRoot":"","s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AAmB,GAAG,QAAQ,GAAG,mBAAmB,CAAC;YAEhF,IAAI,SAAS,GAAG,mBAAmB,EAAE,CAAC;gBACpC,MAAM,gBAAgB,CAAC,UAAU,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC,CAAC;gBACxE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,EAAE,CAAC;YACtD,CAAC;YAED,MAAM,KAAK,GAAqB;gBAC9B,EAAE,EAAE,QAAQ,EAAE;gBACd,IAAI,EAAE,OAAO,CAAC,IAAI;gBAClB,IAAI,EAAE,OAAO,CAAC,IAAI;gBAClB,KAAK,EAAE,OAAO,CAAC,KAAK;gBACpB,KAAK,EAAE,OAAO,CAAC,KAAK;gBACpB,SAAS,EAAE,IAAI,IAAI,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE;gBACrC,cAAc,EAAE,mBAAmB;aACpC,CAAC;YACF,MAAM,gBAAgB,CAAC,UAAU,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,CAAC;YACpF,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AACjD,CAAC"}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { WriteUsageStateOptions } from './coordinator.js';
|
|
2
|
-
export interface GrantRequest {
|
|
3
|
-
repo: string;
|
|
4
|
-
lane: string;
|
|
5
|
-
phase: string;
|
|
6
|
-
model: string;
|
|
7
|
-
}
|
|
8
|
-
export interface GrantLedgerEntry {
|
|
9
|
-
id: string;
|
|
10
|
-
repo: string;
|
|
11
|
-
lane: string;
|
|
12
|
-
phase: string;
|
|
13
|
-
model: string;
|
|
14
|
-
grantedAt: string;
|
|
15
|
-
reservationPct: number;
|
|
16
|
-
}
|
|
17
|
-
export interface GrantLedger {
|
|
18
|
-
version: 1;
|
|
19
|
-
grants: GrantLedgerEntry[];
|
|
20
|
-
}
|
|
21
|
-
export type AcquireResult = {
|
|
22
|
-
granted: true;
|
|
23
|
-
} | {
|
|
24
|
-
granted: false;
|
|
25
|
-
retryAfter: number;
|
|
26
|
-
};
|
|
27
|
-
export declare const USAGE_ADMISSION_CEILING_PCT = 100;
|
|
28
|
-
export declare const USAGE_GRANT_RESERVATION_PCT = 20;
|
|
29
|
-
export declare const DEFAULT_GRANT_TTL_MS: number;
|
|
30
|
-
export declare function defaultGrantLedgerPath(home?: string): string;
|
|
31
|
-
/** Coarse routing predicate: everything Claude-branded is capped by the subscription
|
|
32
|
-
* window; every other model bypasses this gate. Non-Claude route gating is out of
|
|
33
|
-
* scope here. */
|
|
34
|
-
export declare function isCappedModel(model: string): boolean;
|
|
35
|
-
/** Reads the persisted grant ledger. A missing file, unparsable JSON, a non-object
|
|
36
|
-
* payload, or a non-array `grants` all yield an empty ledger — this NEVER throws
|
|
37
|
-
* and NEVER creates the file. Individual malformed entries are dropped. */
|
|
38
|
-
export declare function loadGrantLedger(file: string): Promise<GrantLedger>;
|
|
39
|
-
/** Atomic write: mkdir the parent, serialize to `${file}.tmp`, then rename it onto
|
|
40
|
-
* `file`. Mirrors `writeUsageState`. */
|
|
41
|
-
export declare function writeGrantLedger(file: string, ledger: GrantLedger, opts?: WriteUsageStateOptions): Promise<void>;
|
|
42
|
-
/** Pure: drops grants older than `ttlMs`, on the assumption their work has by then
|
|
43
|
-
* been reflected in the polled snapshot. */
|
|
44
|
-
export declare function pruneGrants(grants: GrantLedgerEntry[], now: number, ttlMs: number): GrantLedgerEntry[];
|
|
45
|
-
//# sourceMappingURL=grant-ledger.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"grant-ledger.d.ts","sourceRoot":"","sources":["../../src/usage/grant-led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