@1agents/acp-service 0.8.3 → 0.9.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/README.md +25 -13
- package/dist/bin/a2a.js +16 -4
- package/dist/bin/acp-service.js +30 -0
- package/dist/bin/process.d.ts +4 -0
- package/dist/bin/process.js +57 -0
- package/dist/bin/roles.d.ts +1 -0
- package/dist/bin/roles.js +24 -0
- package/dist/bin/rsi.d.ts +3 -0
- package/dist/bin/rsi.js +114 -0
- package/dist/bin/ticket.d.ts +13 -0
- package/dist/bin/ticket.js +97 -0
- package/dist/bin/trail.d.ts +4 -0
- package/dist/bin/trail.js +62 -0
- package/dist/src/a2a/artifact-content.d.ts +9 -0
- package/dist/src/a2a/artifact-content.js +45 -0
- package/dist/src/a2a/desktop-notify.d.ts +1 -0
- package/dist/src/a2a/desktop-notify.js +12 -0
- package/dist/src/a2a/execution-binding.d.ts +9 -0
- package/dist/src/a2a/execution-binding.js +19 -0
- package/dist/src/a2a/executor.d.ts +22 -1
- package/dist/src/a2a/executor.js +308 -22
- package/dist/src/a2a/gateway.d.ts +613 -1
- package/dist/src/a2a/gateway.js +1337 -88
- package/dist/src/a2a/index.d.ts +14 -1
- package/dist/src/a2a/index.js +8 -0
- package/dist/src/a2a/job-directory.d.ts +6 -0
- package/dist/src/a2a/job-directory.js +19 -0
- package/dist/src/a2a/preflight.js +16 -1
- package/dist/src/a2a/resubmit.d.ts +46 -0
- package/dist/src/a2a/resubmit.js +82 -0
- package/dist/src/a2a/result-metadata.d.ts +6 -0
- package/dist/src/a2a/result-metadata.js +25 -6
- package/dist/src/a2a/server.d.ts +10 -0
- package/dist/src/a2a/server.js +127 -2
- package/dist/src/a2a/session-lifecycle.d.ts +40 -0
- package/dist/src/a2a/session-lifecycle.js +54 -0
- package/dist/src/a2a/store.d.ts +4 -0
- package/dist/src/a2a/store.js +76 -1
- package/dist/src/a2a/types.d.ts +77 -1
- package/dist/src/a2a/types.js +11 -1
- package/dist/src/a2a/workbench-ui.d.ts +28 -0
- package/dist/src/a2a/workbench-ui.js +182 -0
- package/dist/src/acp-connection.js +119 -14
- package/dist/src/bridge.d.ts +1 -0
- package/dist/src/bridge.js +312 -37
- package/dist/src/capacity-lifecycle.d.ts +5 -0
- package/dist/src/capacity-lifecycle.js +57 -0
- package/dist/src/capacity-queue.d.ts +58 -0
- package/dist/src/capacity-queue.js +388 -0
- package/dist/src/capacity.d.ts +101 -0
- package/dist/src/capacity.js +416 -0
- package/dist/src/dsh.js +1 -1
- package/dist/src/execution/agent-files.d.ts +25 -0
- package/dist/src/execution/agent-files.js +90 -0
- package/dist/src/execution/agent-registry.d.ts +164 -0
- package/dist/src/execution/agent-registry.js +275 -0
- package/dist/src/execution/human-review.d.ts +105 -0
- package/dist/src/execution/human-review.js +89 -0
- package/dist/src/execution/ledger.d.ts +57 -0
- package/dist/src/execution/ledger.js +122 -13
- package/dist/src/execution/ppt-delivery.d.ts +36 -3
- package/dist/src/execution/ppt-delivery.js +243 -53
- package/dist/src/execution/process-request.d.ts +151 -0
- package/dist/src/execution/process-request.js +177 -0
- package/dist/src/execution/project-discovery.d.ts +56 -0
- package/dist/src/execution/project-discovery.js +429 -0
- package/dist/src/execution/report-review.d.ts +60 -0
- package/dist/src/execution/report-review.js +173 -0
- package/dist/src/execution/targets.d.ts +20 -0
- package/dist/src/execution/targets.js +23 -3
- package/dist/src/execution/ticket-executor.d.ts +25 -0
- package/dist/src/execution/ticket-executor.js +27 -0
- package/dist/src/execution/ticket.d.ts +326 -0
- package/dist/src/execution/ticket.js +301 -0
- package/dist/src/execution/workbench-chat.d.ts +25 -0
- package/dist/src/execution/workbench-chat.js +54 -0
- package/dist/src/execution/workbench.d.ts +162 -0
- package/dist/src/execution/workbench.js +356 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +3 -0
- package/dist/src/roles/index.d.ts +1 -0
- package/dist/src/roles/index.js +1 -0
- package/dist/src/roles/skills.d.ts +71 -0
- package/dist/src/roles/skills.js +427 -0
- package/dist/src/rsi/gate.d.ts +25 -0
- package/dist/src/rsi/gate.js +59 -0
- package/dist/src/rsi/outline-run.d.ts +107 -0
- package/dist/src/rsi/outline-run.js +443 -0
- package/dist/workbench-ui/app.css +453 -0
- package/dist/workbench-ui/app.js +13 -0
- package/dist/workbench-ui/chunk-23JHGYT3.js +1 -0
- package/dist/workbench-ui/chunk-3H6STGPV.js +1 -0
- package/dist/workbench-ui/chunk-3SNBBMGH.js +1 -0
- package/dist/workbench-ui/chunk-3XJZ6DIE.js +1 -0
- package/dist/workbench-ui/chunk-44KDLLKJ.js +1 -0
- package/dist/workbench-ui/chunk-46H7FU5Y.js +7 -0
- package/dist/workbench-ui/chunk-4N44QPL7.js +1 -0
- package/dist/workbench-ui/chunk-4YN4BS7I.js +1 -0
- package/dist/workbench-ui/chunk-53EG4IVQ.js +96 -0
- package/dist/workbench-ui/chunk-5FIPUNR4.js +1 -0
- package/dist/workbench-ui/chunk-6DWWYA6G.js +2 -0
- package/dist/workbench-ui/chunk-6FU3T633.js +99 -0
- package/dist/workbench-ui/chunk-6XKIHRA4.js +1 -0
- package/dist/workbench-ui/chunk-7GIX43Y5.js +1 -0
- package/dist/workbench-ui/chunk-7Q4WBGX6.js +4 -0
- package/dist/workbench-ui/chunk-A3BQM7HR.js +1 -0
- package/dist/workbench-ui/chunk-AFCFDDOP.js +24 -0
- package/dist/workbench-ui/chunk-AG67EUYD.js +89 -0
- package/dist/workbench-ui/chunk-AGJHMMTN.js +1 -0
- package/dist/workbench-ui/chunk-AO6JUBV6.js +41 -0
- package/dist/workbench-ui/chunk-AUSLTP43.js +72 -0
- package/dist/workbench-ui/chunk-AVZL46TT.js +1 -0
- package/dist/workbench-ui/chunk-AZRIGIV2.js +168 -0
- package/dist/workbench-ui/chunk-B2LIVH37.js +1 -0
- package/dist/workbench-ui/chunk-B7YX5TR5.js +1 -0
- package/dist/workbench-ui/chunk-BCB4X4XX.js +1 -0
- package/dist/workbench-ui/chunk-BCRQUL2A.js +88 -0
- package/dist/workbench-ui/chunk-BI6UHHQP.js +1 -0
- package/dist/workbench-ui/chunk-BLSLQ4AQ.js +120 -0
- package/dist/workbench-ui/chunk-C3QSRS6E.js +1 -0
- package/dist/workbench-ui/chunk-CK7M2PJ3.js +62 -0
- package/dist/workbench-ui/chunk-CM6LZ7TU.js +1 -0
- package/dist/workbench-ui/chunk-CP63H27F.js +36 -0
- package/dist/workbench-ui/chunk-CPUMMIS2.js +70 -0
- package/dist/workbench-ui/chunk-CSEIBI5U.js +2 -0
- package/dist/workbench-ui/chunk-CUS65DKZ.js +161 -0
- package/dist/workbench-ui/chunk-CVQY7TNC.js +1 -0
- package/dist/workbench-ui/chunk-D6S3XBIX.js +106 -0
- package/dist/workbench-ui/chunk-E253Y562.js +1 -0
- package/dist/workbench-ui/chunk-EADDJUDC.js +1 -0
- package/dist/workbench-ui/chunk-EBGJ3YXG.js +206 -0
- package/dist/workbench-ui/chunk-EKPVZ3WY.js +8 -0
- package/dist/workbench-ui/chunk-EQDCEAFY.js +1 -0
- package/dist/workbench-ui/chunk-ES3SSDJY.js +40 -0
- package/dist/workbench-ui/chunk-ESFD2H7N.js +231 -0
- package/dist/workbench-ui/chunk-FVOHPMS6.js +1 -0
- package/dist/workbench-ui/chunk-FXMQAGVT.js +1 -0
- package/dist/workbench-ui/chunk-GGTADRXD.js +24 -0
- package/dist/workbench-ui/chunk-GHIVPGNQ.js +1 -0
- package/dist/workbench-ui/chunk-GLU2YQV2.js +1 -0
- package/dist/workbench-ui/chunk-GOCKBYSM.js +1 -0
- package/dist/workbench-ui/chunk-H2KMMTJB.js +1 -0
- package/dist/workbench-ui/chunk-HGVLDNPP.js +39 -0
- package/dist/workbench-ui/chunk-HSAVC5GY.js +1 -0
- package/dist/workbench-ui/chunk-HSEAD2JH.js +1 -0
- package/dist/workbench-ui/chunk-HSIHOBTY.js +1 -0
- package/dist/workbench-ui/chunk-IZXOKUGG.js +67 -0
- package/dist/workbench-ui/chunk-JFT2LC2T.js +1 -0
- package/dist/workbench-ui/chunk-JHL3ZDYK.js +3 -0
- package/dist/workbench-ui/chunk-JMORLCZG.js +257 -0
- package/dist/workbench-ui/chunk-JR4SIX7A.js +1 -0
- package/dist/workbench-ui/chunk-K23HAGEH.js +30 -0
- package/dist/workbench-ui/chunk-K43JDCBH.js +1 -0
- package/dist/workbench-ui/chunk-KINKFJY7.js +1 -0
- package/dist/workbench-ui/chunk-KKQSA4LI.js +2 -0
- package/dist/workbench-ui/chunk-KOUZX3UX.js +1 -0
- package/dist/workbench-ui/chunk-KR4DRHW2.js +78 -0
- package/dist/workbench-ui/chunk-KT4DJNOR.js +1 -0
- package/dist/workbench-ui/chunk-KTPNKRRZ.js +292 -0
- package/dist/workbench-ui/chunk-LFHIPGSU.js +7 -0
- package/dist/workbench-ui/chunk-LWDL4HZB.js +125 -0
- package/dist/workbench-ui/chunk-MVYC3CY4.js +1 -0
- package/dist/workbench-ui/chunk-NWUPALCY.js +129 -0
- package/dist/workbench-ui/chunk-NY7C662Z.js +1 -0
- package/dist/workbench-ui/chunk-O76WTHWP.js +1 -0
- package/dist/workbench-ui/chunk-OTF7YHRE.js +162 -0
- package/dist/workbench-ui/chunk-P6TKKRKP.js +1 -0
- package/dist/workbench-ui/chunk-PFHOOWV3.js +1 -0
- package/dist/workbench-ui/chunk-PUQLANIM.js +1 -0
- package/dist/workbench-ui/chunk-PZCGBYVO.js +1 -0
- package/dist/workbench-ui/chunk-QOHUZGP4.js +1 -0
- package/dist/workbench-ui/chunk-QSEG3OFF.js +1 -0
- package/dist/workbench-ui/chunk-QYPOXZIA.js +84 -0
- package/dist/workbench-ui/chunk-R6SOF3JC.js +1 -0
- package/dist/workbench-ui/chunk-RCLOYU5F.js +1 -0
- package/dist/workbench-ui/chunk-RSRUP64S.js +1 -0
- package/dist/workbench-ui/chunk-RVAIT6JW.js +1 -0
- package/dist/workbench-ui/chunk-S2CCN3II.js +1 -0
- package/dist/workbench-ui/chunk-S2Q52W46.js +1 -0
- package/dist/workbench-ui/chunk-S2W5THJ5.js +1 -0
- package/dist/workbench-ui/chunk-S6ZQZNOX.js +1 -0
- package/dist/workbench-ui/chunk-S74PLMMS.js +1 -0
- package/dist/workbench-ui/chunk-S7C4HKEY.js +1 -0
- package/dist/workbench-ui/chunk-SDTB3Q65.js +1 -0
- package/dist/workbench-ui/chunk-SLPRGYUV.js +32 -0
- package/dist/workbench-ui/chunk-SQE76S5B.js +1 -0
- package/dist/workbench-ui/chunk-SSDZPVPW.js +1 -0
- package/dist/workbench-ui/chunk-SVI7DGIE.js +1 -0
- package/dist/workbench-ui/chunk-T6QKLZ4H.js +62 -0
- package/dist/workbench-ui/chunk-TJNRPTFL.js +1 -0
- package/dist/workbench-ui/chunk-TP6U4EDH.js +2 -0
- package/dist/workbench-ui/chunk-TYAAT3GQ.js +1 -0
- package/dist/workbench-ui/chunk-U6OCY4ZH.js +1 -0
- package/dist/workbench-ui/chunk-UAN3NLRF.js +1 -0
- package/dist/workbench-ui/chunk-UMLU3FIX.js +1 -0
- package/dist/workbench-ui/chunk-URIY2OTM.js +1 -0
- package/dist/workbench-ui/chunk-V2NUKAA7.js +38 -0
- package/dist/workbench-ui/chunk-V4QLL4TE.js +139 -0
- package/dist/workbench-ui/chunk-VASHXUK4.js +2 -0
- package/dist/workbench-ui/chunk-VJLIWNIU.js +1 -0
- package/dist/workbench-ui/chunk-VQNQFBID.js +1 -0
- package/dist/workbench-ui/chunk-VSIWU7XA.js +3 -0
- package/dist/workbench-ui/chunk-VSJASPUZ.js +321 -0
- package/dist/workbench-ui/chunk-VXPNUNUX.js +3 -0
- package/dist/workbench-ui/chunk-WBKLDCV6.js +1 -0
- package/dist/workbench-ui/chunk-WOKJM3FY.js +1 -0
- package/dist/workbench-ui/chunk-X3U3NA3X.js +34 -0
- package/dist/workbench-ui/chunk-XBYU7M4J.js +1 -0
- package/dist/workbench-ui/chunk-XBZAO4ID.js +10 -0
- package/dist/workbench-ui/chunk-XLLPZ2WO.js +1 -0
- package/dist/workbench-ui/chunk-YFVL4NJL.js +1 -0
- package/dist/workbench-ui/chunk-YRFQENM4.js +1 -0
- package/dist/workbench-ui/chunk-YUADQVB2.js +1 -0
- package/dist/workbench-ui/chunk-Z3TCAKM5.js +1 -0
- package/dist/workbench-ui/chunk-Z6QR6FRE.js +10 -0
- package/dist/workbench-ui/chunk-ZJPSORPP.js +3 -0
- package/dist/workbench-ui/chunk-ZLGCSTR2.js +15 -0
- package/dist/workbench-ui/chunk-ZLR4SG5X.js +1 -0
- package/dist/workbench-ui/chunk-ZND3TTGF.js +1 -0
- package/dist/workbench-ui/chunk-ZOP3ZYH7.js +2 -0
- package/dist/workbench-ui/index.html +14 -0
- package/package.json +23 -8
- package/skills/acp-service/SKILL.md +24 -8
- package/skills/acp-service/references/a2a.md +24 -8
- package/skills/acp-service/references/async-caller.md +48 -0
- package/skills/acp-service/references/dsh.md +2 -0
- package/skills/acp-service/references/roles-and-tasks.md +120 -0
- package/skills/acp-service/scripts/a2a-caller.py +242 -0
- package/skills/rsi-coach/SKILL.md +134 -0
- package/vendor/dsh/README.md +11 -5
- package/vendor/dsh/a2a-executor.d.ts +60 -3
- package/vendor/dsh/a2a-executor.js +208 -18
- package/vendor/dsh/a2a.d.ts +12 -2
- package/vendor/dsh/a2a.js +9 -2
- package/vendor/dsh/adapter.d.ts +61 -2
- package/vendor/dsh/adapter.js +354 -23
- package/vendor/dsh/client.js +67 -11
- package/vendor/dsh/index.js +57 -4
- package/vendor/dsh/native-approvals.d.ts +4 -0
- package/vendor/dsh/native-approvals.js +44 -0
- package/vendor/dsh/preset-route.d.ts +49 -0
- package/vendor/dsh/preset-route.js +51 -0
- package/vendor/dsh/preset.js +10 -3
- package/vendor/dsh/process-admission.d.ts +25 -0
- package/vendor/dsh/process-admission.js +87 -0
- package/vendor/dsh/process-requests.d.ts +57 -0
- package/vendor/dsh/process-requests.js +49 -0
- package/vendor/dsh/review-mcp.d.ts +1 -0
- package/vendor/dsh/review-mcp.js +56 -0
- package/vendor/dsh/review.d.ts +30 -0
- package/vendor/dsh/review.js +149 -0
- package/vendor/dsh/session-settings.d.ts +34 -0
- package/vendor/dsh/session-settings.js +114 -0
- package/vendor/dsh/ticket-spec.d.ts +30 -0
- package/vendor/dsh/ticket-spec.js +10 -0
- package/vendor/dsh/ticket-tools.d.ts +21 -0
- package/vendor/dsh/ticket-tools.js +43 -0
- package/vendor/dsh/workbench.d.ts +18 -0
- package/vendor/dsh/workbench.js +91 -0
- package/vendor/runtime/README.md +6 -4
- package/vendor/runtime/dist/{cli-DeOpE2Xz.js → cli-DXmYwv3U.js} +4 -4
- package/vendor/runtime/dist/{cli-DeOpE2Xz.js.map → cli-DXmYwv3U.js.map} +1 -1
- package/vendor/runtime/dist/cli.js +7 -7
- package/vendor/runtime/dist/client-VQsqcIOJ.d.ts.map +1 -1
- package/vendor/runtime/dist/docs-site/CLI.html +2 -2
- package/vendor/runtime/dist/docs-site/VISION.html +2 -2
- package/vendor/runtime/dist/docs-site/agents.html +2 -2
- package/vendor/runtime/dist/docs-site/compare.html +2 -2
- package/vendor/runtime/dist/docs-site/config.html +2 -2
- package/vendor/runtime/dist/docs-site/custom-agents.html +2 -2
- package/vendor/runtime/dist/docs-site/exit-codes.html +2 -2
- package/vendor/runtime/dist/docs-site/flows.html +2 -2
- package/vendor/runtime/dist/docs-site/index.html +1 -1
- package/vendor/runtime/dist/docs-site/install.html +2 -2
- package/vendor/runtime/dist/docs-site/llms.txt +1 -1
- package/vendor/runtime/dist/docs-site/output-formats.html +2 -2
- package/vendor/runtime/dist/docs-site/permissions.html +2 -2
- package/vendor/runtime/dist/docs-site/prompting.html +2 -2
- package/vendor/runtime/dist/docs-site/quickstart.html +2 -2
- package/vendor/runtime/dist/docs-site/releasing.html +2 -2
- package/vendor/runtime/dist/docs-site/runtime-environment.html +2 -2
- package/vendor/runtime/dist/docs-site/runtime-process-lifecycle.html +3 -2
- package/vendor/runtime/dist/docs-site/safe-errors.html +2 -2
- package/vendor/runtime/dist/docs-site/session-control.html +2 -2
- package/vendor/runtime/dist/docs-site/session-watch.html +2 -2
- package/vendor/runtime/dist/docs-site/sessions.html +2 -2
- package/vendor/runtime/dist/docs-site/shared-sessions.html +2 -2
- package/vendor/runtime/dist/{flows-oaBGn31w.js → flows-DJ17ajbw.js} +3 -3
- package/vendor/runtime/dist/{flows-oaBGn31w.js.map → flows-DJ17ajbw.js.map} +1 -1
- package/vendor/runtime/dist/flows.js +1 -1
- package/vendor/runtime/dist/{invocation-options-CUDEE8vW.js → invocation-options-PBfCgrdi.js} +3 -3
- package/vendor/runtime/dist/{invocation-options-CUDEE8vW.js.map → invocation-options-PBfCgrdi.js.map} +1 -1
- package/vendor/runtime/dist/{output-CFv3jcOQ.js → output-B3MDn7kv.js} +2 -2
- package/vendor/runtime/dist/{output-CFv3jcOQ.js.map → output-B3MDn7kv.js.map} +1 -1
- package/vendor/runtime/dist/{queue-owner-runtime-DvMJiGbP.js → queue-owner-runtime-DXCVngwU.js} +15 -8
- package/vendor/runtime/dist/queue-owner-runtime-DXCVngwU.js.map +1 -0
- package/vendor/runtime/dist/runtime.js +1 -1
- package/vendor/runtime/dist/{session-watch-Di-wIdCH.js → session-watch-DURyCw31.js} +2 -2
- package/vendor/runtime/dist/{session-watch-Di-wIdCH.js.map → session-watch-DURyCw31.js.map} +1 -1
- package/vendor/runtime/package.json +6 -4
- package/vendor/runtime/skills/acpx/SKILL.md +2 -0
- package/vendor/runtime/dist/queue-owner-runtime-DvMJiGbP.js.map +0 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** The runtime owns child termination; capacity remains reserved until its exit is observed. */
|
|
2
|
+
export function createCapacityLifecycle(governor, source = 'automatic') {
|
|
3
|
+
const reservations = new Map();
|
|
4
|
+
const release = (launchId) => {
|
|
5
|
+
const entry = reservations.get(launchId);
|
|
6
|
+
if (!entry)
|
|
7
|
+
return Promise.resolve();
|
|
8
|
+
if (entry.releasing)
|
|
9
|
+
return entry.releasing;
|
|
10
|
+
const operation = (async () => {
|
|
11
|
+
try {
|
|
12
|
+
const lease = await entry.lease;
|
|
13
|
+
await lease.release();
|
|
14
|
+
if (reservations.get(launchId) === entry)
|
|
15
|
+
reservations.delete(launchId);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// Retain the entry and durable reservation so a later observation can retry.
|
|
19
|
+
console.warn('ACP capacity reservation cleanup failed; the reservation remains recorded.');
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
entry.releasing = undefined;
|
|
23
|
+
}
|
|
24
|
+
})();
|
|
25
|
+
entry.releasing = operation;
|
|
26
|
+
return operation;
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
async onBeforeSpawn(launch) {
|
|
30
|
+
if (reservations.has(launch.launchId))
|
|
31
|
+
throw new Error('ACP process launch already has a capacity reservation');
|
|
32
|
+
// Install the entry before awaiting admission so concurrent duplicate callbacks cannot leak a slot.
|
|
33
|
+
const entry = {
|
|
34
|
+
lease: Promise.resolve().then(() => governor.acquire({ kind: 'process', source, id: launch.launchId })),
|
|
35
|
+
};
|
|
36
|
+
reservations.set(launch.launchId, entry);
|
|
37
|
+
try {
|
|
38
|
+
await entry.lease;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (reservations.get(launch.launchId) === entry)
|
|
42
|
+
reservations.delete(launch.launchId);
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
async onSpawned(process) {
|
|
47
|
+
const entry = reservations.get(process.launchId);
|
|
48
|
+
if (!entry)
|
|
49
|
+
throw new Error('ACP process launch has no capacity reservation');
|
|
50
|
+
const lease = await entry.lease;
|
|
51
|
+
// Propagate registration failures: the runtime then stops the child and reports its exit.
|
|
52
|
+
await lease.spawned(process.pid);
|
|
53
|
+
},
|
|
54
|
+
onSpawnFailed: failure => release(failure.launchId),
|
|
55
|
+
onExit: exit => release(exit.launchId),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type CapacityLease, type CapacityOptions, type CapacityRequest } from './capacity.js';
|
|
2
|
+
export interface CapacityQueueWaitOptions {
|
|
3
|
+
signal?: AbortSignal;
|
|
4
|
+
/** Called once only when admission actually has to wait. */
|
|
5
|
+
onPending?: () => void;
|
|
6
|
+
}
|
|
7
|
+
/** Non-blocking FIFO result. The queue entry stays until admission or cancel. */
|
|
8
|
+
export interface CapacityPendingAdmission {
|
|
9
|
+
pending: true;
|
|
10
|
+
position: number;
|
|
11
|
+
waitedMs: number;
|
|
12
|
+
}
|
|
13
|
+
export interface CapacityQueueTimingOptions {
|
|
14
|
+
pollIntervalMs?: number;
|
|
15
|
+
}
|
|
16
|
+
export declare class CapacityQueue {
|
|
17
|
+
private readonly directory;
|
|
18
|
+
private readonly governor;
|
|
19
|
+
private readonly pollIntervalMs;
|
|
20
|
+
constructor(options?: CapacityOptions, timing?: CapacityQueueTimingOptions);
|
|
21
|
+
acquire(request: CapacityRequest, { signal, onPending }?: CapacityQueueWaitOptions): Promise<CapacityLease>;
|
|
22
|
+
/**
|
|
23
|
+
* Enqueue if needed and return immediately.
|
|
24
|
+
* A free head slot is acquired in this same call. A full slot keeps the entry and reports position.
|
|
25
|
+
* Retries with the same id reuse that entry. Callers without a queue id keep using acquire().
|
|
26
|
+
*/
|
|
27
|
+
tryAdmit(request: CapacityRequest, { signal }?: {
|
|
28
|
+
signal?: AbortSignal;
|
|
29
|
+
}): Promise<CapacityLease | CapacityPendingAdmission>;
|
|
30
|
+
/** Age of the oldest still-owned queue entry. Missing queues and dead owners are not rewritten. */
|
|
31
|
+
headWaitMs(kind: CapacityRequest['kind']): Promise<number>;
|
|
32
|
+
/** Current admission counts. Uses this queue's governor, including its configured limit. */
|
|
33
|
+
slotView(): Promise<{
|
|
34
|
+
taskSlot: {
|
|
35
|
+
limit: number;
|
|
36
|
+
used: number;
|
|
37
|
+
};
|
|
38
|
+
processSlot: {
|
|
39
|
+
limit: number;
|
|
40
|
+
used: number;
|
|
41
|
+
headWaitMs: number;
|
|
42
|
+
};
|
|
43
|
+
}>;
|
|
44
|
+
/** True while a process lease still names this child pid. */
|
|
45
|
+
holdsChild(pid: number): Promise<boolean>;
|
|
46
|
+
/** Drop this process's queued entry. A lease already returned to the caller is not released here. */
|
|
47
|
+
cancel(request: {
|
|
48
|
+
id: string;
|
|
49
|
+
kind: CapacityRequest['kind'];
|
|
50
|
+
}): Promise<boolean>;
|
|
51
|
+
private entryPath;
|
|
52
|
+
private readState;
|
|
53
|
+
private readJson;
|
|
54
|
+
private readEntry;
|
|
55
|
+
private removeEntry;
|
|
56
|
+
private writeJson;
|
|
57
|
+
private transaction;
|
|
58
|
+
}
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
/** Durable device-wide FIFO admission for automatically invoked ACP work. */
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { closeSync, constants, existsSync, fstatSync, fsyncSync, openSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
6
|
+
import { DirectoryLease } from './a2a/lease.js';
|
|
7
|
+
import { CapacityError, CapacityGovernor, resolveCapacityDirectory } from './capacity.js';
|
|
8
|
+
const tokenPattern = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/;
|
|
9
|
+
const stateMessage = 'ACP capacity queue state is unavailable; check the local host queue state before retrying.';
|
|
10
|
+
const processOrigins = new Set(['prewarm', 'job', 'user-opened']);
|
|
11
|
+
const validRequest = (value) => !!value &&
|
|
12
|
+
(value.kind === 'task' || value.kind === 'process') &&
|
|
13
|
+
(value.source === 'automatic' || value.source === 'interactive') &&
|
|
14
|
+
typeof value.id === 'string' && !!value.id.trim() && value.id.length <= 4096 &&
|
|
15
|
+
(value.origin === undefined || (value.kind === 'process' && processOrigins.has(value.origin)));
|
|
16
|
+
const isPositiveInteger = (value) => Number.isSafeInteger(value) && value > 0;
|
|
17
|
+
/** Permission failures do not prove that a queued owner has exited. */
|
|
18
|
+
function isDead(pid) {
|
|
19
|
+
try {
|
|
20
|
+
process.kill(pid, 0);
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
return error.code === 'ESRCH';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class CapacityQueue {
|
|
28
|
+
directory;
|
|
29
|
+
governor;
|
|
30
|
+
pollIntervalMs;
|
|
31
|
+
constructor(options = {}, timing = {}) {
|
|
32
|
+
this.directory = join(resolveCapacityDirectory(options), 'queue');
|
|
33
|
+
this.governor = new CapacityGovernor(options);
|
|
34
|
+
this.pollIntervalMs = timing.pollIntervalMs ?? 750;
|
|
35
|
+
if (!isPositiveInteger(this.pollIntervalMs))
|
|
36
|
+
throw new Error('ACP capacity queue poll interval must be a positive safe integer');
|
|
37
|
+
}
|
|
38
|
+
async acquire(request, { signal, onPending } = {}) {
|
|
39
|
+
if (!validRequest(request))
|
|
40
|
+
throw new Error('Invalid ACP capacity request');
|
|
41
|
+
signal?.throwIfAborted();
|
|
42
|
+
// Manual work keeps the governor's reserved interactive room even while automatic work waits.
|
|
43
|
+
if (request.source === 'interactive') {
|
|
44
|
+
const lease = await this.governor.acquire(request);
|
|
45
|
+
if (signal?.aborted) {
|
|
46
|
+
await lease.release();
|
|
47
|
+
signal.throwIfAborted();
|
|
48
|
+
}
|
|
49
|
+
return lease;
|
|
50
|
+
}
|
|
51
|
+
const entry = { ...request, version: 1, token: randomUUID(), ownerPid: process.pid,
|
|
52
|
+
enqueuedAt: Date.now(), sequence: 0 };
|
|
53
|
+
let queued = false;
|
|
54
|
+
let acquired;
|
|
55
|
+
let returned = false;
|
|
56
|
+
let reportedPending = false;
|
|
57
|
+
try {
|
|
58
|
+
await this.transaction(() => {
|
|
59
|
+
signal?.throwIfAborted();
|
|
60
|
+
const state = this.readState();
|
|
61
|
+
if (state.lastSequence >= Number.MAX_SAFE_INTEGER)
|
|
62
|
+
throw new Error(stateMessage);
|
|
63
|
+
entry.sequence = state.lastSequence + 1;
|
|
64
|
+
// Advance the durable counter before publishing the entry; a crash may leave a harmless gap.
|
|
65
|
+
this.writeJson(join(this.directory, 'counter.json'), { version: 1, lastSequence: entry.sequence });
|
|
66
|
+
queued = true;
|
|
67
|
+
this.writeJson(this.entryPath(entry.token), entry);
|
|
68
|
+
}, signal);
|
|
69
|
+
while (true) {
|
|
70
|
+
signal?.throwIfAborted();
|
|
71
|
+
await this.transaction(async () => {
|
|
72
|
+
signal?.throwIfAborted();
|
|
73
|
+
const state = this.readState();
|
|
74
|
+
const own = state.entries.find(item => item.token === entry.token);
|
|
75
|
+
if (!own || own.ownerPid !== process.pid || own.sequence !== entry.sequence)
|
|
76
|
+
throw new Error(stateMessage);
|
|
77
|
+
// Task and child-process capacity have separate FIFOs, avoiding a waiting parent blocking its child.
|
|
78
|
+
const head = state.entries.find(item => item.kind === request.kind);
|
|
79
|
+
if (head?.token !== entry.token)
|
|
80
|
+
return;
|
|
81
|
+
try {
|
|
82
|
+
acquired = await this.governor.acquire(request);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (error instanceof CapacityError)
|
|
86
|
+
return;
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
signal?.throwIfAborted();
|
|
90
|
+
this.removeEntry(entry);
|
|
91
|
+
queued = false;
|
|
92
|
+
}, signal);
|
|
93
|
+
if (acquired) {
|
|
94
|
+
signal?.throwIfAborted();
|
|
95
|
+
returned = true;
|
|
96
|
+
return acquired;
|
|
97
|
+
}
|
|
98
|
+
if (!reportedPending) {
|
|
99
|
+
reportedPending = true;
|
|
100
|
+
onPending?.();
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
await delay(this.pollIntervalMs, undefined, { signal });
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
signal?.throwIfAborted();
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
// An abort or storage failure between admission and delivery must not leak a capacity slot.
|
|
113
|
+
try {
|
|
114
|
+
if (acquired && !returned)
|
|
115
|
+
await acquired.release();
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
if (queued)
|
|
119
|
+
await this.transaction(() => this.removeEntry(entry));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Enqueue if needed and return immediately.
|
|
125
|
+
* A free head slot is acquired in this same call. A full slot keeps the entry and reports position.
|
|
126
|
+
* Retries with the same id reuse that entry. Callers without a queue id keep using acquire().
|
|
127
|
+
*/
|
|
128
|
+
async tryAdmit(request, { signal } = {}) {
|
|
129
|
+
if (!validRequest(request))
|
|
130
|
+
throw new Error('Invalid ACP capacity request');
|
|
131
|
+
signal?.throwIfAborted();
|
|
132
|
+
if (request.source === 'interactive') {
|
|
133
|
+
const lease = await this.governor.acquire(request);
|
|
134
|
+
if (signal?.aborted) {
|
|
135
|
+
await lease.release();
|
|
136
|
+
signal.throwIfAborted();
|
|
137
|
+
}
|
|
138
|
+
return lease;
|
|
139
|
+
}
|
|
140
|
+
let acquired;
|
|
141
|
+
let pending;
|
|
142
|
+
let created = false;
|
|
143
|
+
let entry;
|
|
144
|
+
try {
|
|
145
|
+
await this.transaction(async () => {
|
|
146
|
+
signal?.throwIfAborted();
|
|
147
|
+
const state = this.readState();
|
|
148
|
+
const existing = state.entries.find(item => item.id === request.id && item.kind === request.kind && item.ownerPid === process.pid);
|
|
149
|
+
if (existing)
|
|
150
|
+
entry = existing;
|
|
151
|
+
else {
|
|
152
|
+
if (state.lastSequence >= Number.MAX_SAFE_INTEGER)
|
|
153
|
+
throw new Error(stateMessage);
|
|
154
|
+
entry = { ...request, version: 1, token: randomUUID(), ownerPid: process.pid,
|
|
155
|
+
enqueuedAt: Date.now(), sequence: state.lastSequence + 1 };
|
|
156
|
+
this.writeJson(join(this.directory, 'counter.json'), { version: 1, lastSequence: entry.sequence });
|
|
157
|
+
created = true;
|
|
158
|
+
this.writeJson(this.entryPath(entry.token), entry);
|
|
159
|
+
}
|
|
160
|
+
const current = this.readState();
|
|
161
|
+
const own = current.entries.find(item => item.token === entry.token);
|
|
162
|
+
if (!own || own.ownerPid !== process.pid)
|
|
163
|
+
throw new Error(stateMessage);
|
|
164
|
+
const same = current.entries.filter(item => item.kind === request.kind);
|
|
165
|
+
const position = same.findIndex(item => item.token === own.token) + 1;
|
|
166
|
+
const waitedMs = Math.max(0, Date.now() - own.enqueuedAt);
|
|
167
|
+
if (position < 1)
|
|
168
|
+
throw new Error(stateMessage);
|
|
169
|
+
const head = same[0];
|
|
170
|
+
if (head?.token !== own.token) {
|
|
171
|
+
pending = { pending: true, position, waitedMs };
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
acquired = await this.governor.acquire(request);
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
if (error instanceof CapacityError) {
|
|
179
|
+
pending = { pending: true, position, waitedMs };
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
this.removeEntry(own);
|
|
185
|
+
created = false;
|
|
186
|
+
}, signal);
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
if (created && entry)
|
|
190
|
+
await this.transaction(() => this.removeEntry(entry)).catch(() => undefined);
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
if (signal?.aborted) {
|
|
194
|
+
if (acquired)
|
|
195
|
+
await acquired.release();
|
|
196
|
+
else if (entry)
|
|
197
|
+
await this.transaction(() => this.removeEntry(entry)).catch(() => undefined);
|
|
198
|
+
signal.throwIfAborted();
|
|
199
|
+
}
|
|
200
|
+
if (acquired)
|
|
201
|
+
return acquired;
|
|
202
|
+
if (!pending)
|
|
203
|
+
throw new Error(stateMessage);
|
|
204
|
+
return pending;
|
|
205
|
+
}
|
|
206
|
+
/** Age of the oldest still-owned queue entry. Missing queues and dead owners are not rewritten. */
|
|
207
|
+
async headWaitMs(kind) {
|
|
208
|
+
if (kind !== 'task' && kind !== 'process')
|
|
209
|
+
throw new Error('Invalid ACP capacity request');
|
|
210
|
+
if (!existsSync(this.directory))
|
|
211
|
+
return 0;
|
|
212
|
+
return this.transaction(() => {
|
|
213
|
+
let head;
|
|
214
|
+
for (const name of readdirSync(this.directory)) {
|
|
215
|
+
if (name === '.lock' || name === 'counter.json' || /^pending-[a-f0-9-]{36}\.json$/.test(name))
|
|
216
|
+
continue;
|
|
217
|
+
if (!/^entry-[a-f0-9-]{36}\.json$/.test(name))
|
|
218
|
+
throw new Error(stateMessage);
|
|
219
|
+
const entry = this.readEntry(join(this.directory, name));
|
|
220
|
+
if (name !== `entry-${entry.token}.json` || entry.kind !== kind || isDead(entry.ownerPid))
|
|
221
|
+
continue;
|
|
222
|
+
if (!head || entry.sequence < head.sequence)
|
|
223
|
+
head = entry;
|
|
224
|
+
}
|
|
225
|
+
return head ? Math.max(0, Date.now() - head.enqueuedAt) : 0;
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
/** Current admission counts. Uses this queue's governor, including its configured limit. */
|
|
229
|
+
async slotView() {
|
|
230
|
+
const task = await this.governor.snapshot('task');
|
|
231
|
+
const process = await this.governor.snapshot('process');
|
|
232
|
+
return {
|
|
233
|
+
taskSlot: { limit: task.limits.soft, used: task.used },
|
|
234
|
+
processSlot: { limit: process.limits.soft, used: process.used, headWaitMs: await this.headWaitMs('process') },
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/** True while a process lease still names this child pid. */
|
|
238
|
+
async holdsChild(pid) {
|
|
239
|
+
const { sessions } = await this.governor.inspect();
|
|
240
|
+
return Object.values(sessions).some(rows => rows.some(row => row.childPid === pid));
|
|
241
|
+
}
|
|
242
|
+
/** Drop this process's queued entry. A lease already returned to the caller is not released here. */
|
|
243
|
+
async cancel(request) {
|
|
244
|
+
if (typeof request.id !== 'string' || !request.id.trim() || request.id.length > 4096)
|
|
245
|
+
throw new Error('Invalid ACP capacity request');
|
|
246
|
+
if (request.kind !== 'task' && request.kind !== 'process')
|
|
247
|
+
throw new Error('Invalid ACP capacity request');
|
|
248
|
+
let removed = false;
|
|
249
|
+
await this.transaction(() => {
|
|
250
|
+
const own = this.readState().entries.find(item => item.id === request.id && item.kind === request.kind && item.ownerPid === process.pid);
|
|
251
|
+
if (!own)
|
|
252
|
+
return;
|
|
253
|
+
this.removeEntry(own);
|
|
254
|
+
removed = true;
|
|
255
|
+
});
|
|
256
|
+
return removed;
|
|
257
|
+
}
|
|
258
|
+
entryPath(token) { return join(this.directory, `entry-${token}.json`); }
|
|
259
|
+
readState() {
|
|
260
|
+
const entries = [];
|
|
261
|
+
let lastSequence;
|
|
262
|
+
for (const name of readdirSync(this.directory)) {
|
|
263
|
+
if (name === '.lock')
|
|
264
|
+
continue;
|
|
265
|
+
// Only a completed atomic rename can publish a queue record.
|
|
266
|
+
if (/^pending-[a-f0-9-]{36}\.json$/.test(name)) {
|
|
267
|
+
unlinkSync(join(this.directory, name));
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (name === 'counter.json') {
|
|
271
|
+
const value = this.readJson(join(this.directory, name));
|
|
272
|
+
if (!value || value.version !== 1 || !Number.isSafeInteger(value.lastSequence) || value.lastSequence < 0)
|
|
273
|
+
throw new Error(stateMessage);
|
|
274
|
+
lastSequence = value.lastSequence;
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (!/^entry-[a-f0-9-]{36}\.json$/.test(name))
|
|
278
|
+
throw new Error(stateMessage);
|
|
279
|
+
const entry = this.readEntry(join(this.directory, name));
|
|
280
|
+
if (name !== `entry-${entry.token}.json`)
|
|
281
|
+
throw new Error(stateMessage);
|
|
282
|
+
if (isDead(entry.ownerPid))
|
|
283
|
+
unlinkSync(this.entryPath(entry.token));
|
|
284
|
+
else
|
|
285
|
+
entries.push(entry);
|
|
286
|
+
}
|
|
287
|
+
// Missing/regressed/duplicated sequence state cannot safely establish FIFO order.
|
|
288
|
+
const sequences = new Set(entries.map(item => item.sequence));
|
|
289
|
+
if (sequences.size !== entries.length || entries.some(item => item.sequence > (lastSequence ?? -1)))
|
|
290
|
+
throw new Error(stateMessage);
|
|
291
|
+
entries.sort((a, b) => a.sequence - b.sequence);
|
|
292
|
+
return { entries, lastSequence: lastSequence ?? 0 };
|
|
293
|
+
}
|
|
294
|
+
readJson(path) {
|
|
295
|
+
const fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
296
|
+
try {
|
|
297
|
+
const stat = fstatSync(fd);
|
|
298
|
+
if (!stat.isFile() || stat.size > 64 * 1024)
|
|
299
|
+
throw new Error(stateMessage);
|
|
300
|
+
return JSON.parse(readFileSync(fd, 'utf8'));
|
|
301
|
+
}
|
|
302
|
+
finally {
|
|
303
|
+
closeSync(fd);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
readEntry(path) {
|
|
307
|
+
const value = this.readJson(path);
|
|
308
|
+
if (!validRequest(value) || value.version !== 1 || typeof value.token !== 'string' || !tokenPattern.test(value.token) ||
|
|
309
|
+
!isPositiveInteger(value.ownerPid) || !isPositiveInteger(value.sequence) ||
|
|
310
|
+
!Number.isSafeInteger(value.enqueuedAt) || value.enqueuedAt < 0 || value.source !== 'automatic')
|
|
311
|
+
throw new Error(stateMessage);
|
|
312
|
+
return value;
|
|
313
|
+
}
|
|
314
|
+
removeEntry(entry) {
|
|
315
|
+
const path = this.entryPath(entry.token);
|
|
316
|
+
try {
|
|
317
|
+
const current = this.readEntry(path);
|
|
318
|
+
if (current.token !== entry.token || current.ownerPid !== process.pid || current.sequence !== entry.sequence)
|
|
319
|
+
throw new Error(stateMessage);
|
|
320
|
+
unlinkSync(path);
|
|
321
|
+
}
|
|
322
|
+
catch (error) {
|
|
323
|
+
if (error.code !== 'ENOENT')
|
|
324
|
+
throw error;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
writeJson(path, value) {
|
|
328
|
+
const temporary = join(this.directory, `pending-${randomUUID()}.json`);
|
|
329
|
+
const fd = openSync(temporary, 'wx', 0o600);
|
|
330
|
+
try {
|
|
331
|
+
writeFileSync(fd, JSON.stringify(value));
|
|
332
|
+
fsyncSync(fd);
|
|
333
|
+
}
|
|
334
|
+
finally {
|
|
335
|
+
closeSync(fd);
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
renameSync(temporary, path);
|
|
339
|
+
}
|
|
340
|
+
finally {
|
|
341
|
+
try {
|
|
342
|
+
unlinkSync(temporary);
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
if (error.code !== 'ENOENT')
|
|
346
|
+
throw error;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
async transaction(operation, signal) {
|
|
351
|
+
let lease;
|
|
352
|
+
const deadline = performance.now() + 5000;
|
|
353
|
+
do {
|
|
354
|
+
signal?.throwIfAborted();
|
|
355
|
+
try {
|
|
356
|
+
lease = new DirectoryLease(this.directory);
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
catch {
|
|
360
|
+
if (performance.now() >= deadline)
|
|
361
|
+
throw new Error(stateMessage);
|
|
362
|
+
}
|
|
363
|
+
try {
|
|
364
|
+
await delay(10 + Math.floor(Math.random() * 20), undefined, { signal });
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
signal?.throwIfAborted();
|
|
368
|
+
throw error;
|
|
369
|
+
}
|
|
370
|
+
} while (true);
|
|
371
|
+
try {
|
|
372
|
+
return await operation();
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
if (signal?.aborted && error === signal.reason)
|
|
376
|
+
throw error;
|
|
377
|
+
throw new Error(stateMessage);
|
|
378
|
+
}
|
|
379
|
+
finally {
|
|
380
|
+
try {
|
|
381
|
+
lease.close();
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
throw new Error(stateMessage);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export type CapacityKind = 'process' | 'task';
|
|
2
|
+
export type CapacitySource = 'automatic' | 'interactive';
|
|
3
|
+
/** Process leases only. Missing on a record means the lease stays unlabeled. */
|
|
4
|
+
export type ProcessLeaseOrigin = 'prewarm' | 'job' | 'user-opened';
|
|
5
|
+
export interface CapacityMemoryReading {
|
|
6
|
+
totalBytes: number;
|
|
7
|
+
availableBytes: number;
|
|
8
|
+
source?: 'system-available' | 'macos-reclaimable-estimate' | 'host-provider';
|
|
9
|
+
}
|
|
10
|
+
export interface CapacityOptions {
|
|
11
|
+
directory?: string;
|
|
12
|
+
softLimit?: number;
|
|
13
|
+
hardLimit?: number;
|
|
14
|
+
memoryReserveBytes?: number;
|
|
15
|
+
estimatedInstanceBytes?: number;
|
|
16
|
+
/** Host-owned memory reader, useful for deterministic admission tests. Never populate from a task payload. */
|
|
17
|
+
memoryProvider?: () => CapacityMemoryReading;
|
|
18
|
+
}
|
|
19
|
+
export interface CapacityRequest {
|
|
20
|
+
kind: CapacityKind;
|
|
21
|
+
source: CapacitySource;
|
|
22
|
+
id: string;
|
|
23
|
+
origin?: ProcessLeaseOrigin;
|
|
24
|
+
}
|
|
25
|
+
export interface CapacityLimits {
|
|
26
|
+
soft: number;
|
|
27
|
+
hard: number;
|
|
28
|
+
basis: 'hardware-estimate' | 'configured';
|
|
29
|
+
}
|
|
30
|
+
export interface CapacitySnapshot {
|
|
31
|
+
limits: CapacityLimits;
|
|
32
|
+
used: number;
|
|
33
|
+
automatic: number;
|
|
34
|
+
interactive: number;
|
|
35
|
+
availableAutomatic: number;
|
|
36
|
+
availableInteractive: number;
|
|
37
|
+
memory: CapacityMemoryReading & {
|
|
38
|
+
reserveBytes: number;
|
|
39
|
+
estimatedInstanceBytes: number;
|
|
40
|
+
startupReservedBytes: number;
|
|
41
|
+
availableForStartupBytes: number;
|
|
42
|
+
availableSlots: number;
|
|
43
|
+
startupWindowMs: number;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export interface CapacityLease {
|
|
47
|
+
spawned(pid: number): Promise<void>;
|
|
48
|
+
recheck(): Promise<void>;
|
|
49
|
+
release(): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
export interface CapacityProcessSession {
|
|
52
|
+
id: string;
|
|
53
|
+
ownerPid: number;
|
|
54
|
+
childPid?: number;
|
|
55
|
+
}
|
|
56
|
+
export interface CapacityInspection {
|
|
57
|
+
taskSlot: {
|
|
58
|
+
limit: number;
|
|
59
|
+
used: number;
|
|
60
|
+
};
|
|
61
|
+
processSlot: {
|
|
62
|
+
limit: number;
|
|
63
|
+
used: number;
|
|
64
|
+
};
|
|
65
|
+
sessions: Record<ProcessLeaseOrigin | 'unlabeled', CapacityProcessSession[]>;
|
|
66
|
+
}
|
|
67
|
+
export type CapacityErrorCode = 'ACP_CAPACITY_SOFT_LIMIT' | 'ACP_CAPACITY_HARD_LIMIT' | 'ACP_CAPACITY_MEMORY_LIMIT';
|
|
68
|
+
export declare class CapacityError extends Error {
|
|
69
|
+
readonly code: CapacityErrorCode;
|
|
70
|
+
constructor(code: CapacityErrorCode);
|
|
71
|
+
}
|
|
72
|
+
/** Allow roughly one automatic agent per two CPUs and 3 GiB, then keep at least one manual slot. */
|
|
73
|
+
export declare function estimateCapacityLimits(): CapacityLimits;
|
|
74
|
+
/** Darwin's raw free count excludes reclaimable inactive caches. Purgeable pages are not added twice. */
|
|
75
|
+
export declare function parseMacOSAvailableMemory(output: string, totalBytes: number): number;
|
|
76
|
+
export declare function resolveCapacityDirectory(options?: CapacityOptions): string;
|
|
77
|
+
export declare class CapacityGovernor {
|
|
78
|
+
private readonly directory;
|
|
79
|
+
private readonly limits;
|
|
80
|
+
private readonly memoryProvider;
|
|
81
|
+
private readonly memoryReserveBytes;
|
|
82
|
+
private readonly estimatedInstanceBytes;
|
|
83
|
+
constructor(options?: CapacityOptions);
|
|
84
|
+
acquire(request: CapacityRequest): Promise<CapacityLease>;
|
|
85
|
+
snapshot(kind: CapacityKind): Promise<CapacitySnapshot>;
|
|
86
|
+
/**
|
|
87
|
+
* Read both ledgers and process-lease origins. Does not acquire, release, or relabel.
|
|
88
|
+
* A missing origin stays unlabeled. Dead owners stay on disk for the next admission pass.
|
|
89
|
+
*/
|
|
90
|
+
inspect(): Promise<CapacityInspection>;
|
|
91
|
+
private count;
|
|
92
|
+
private recordedLimit;
|
|
93
|
+
private readReservations;
|
|
94
|
+
private reservationPath;
|
|
95
|
+
private ensurePolicy;
|
|
96
|
+
private readJson;
|
|
97
|
+
private readReservation;
|
|
98
|
+
private writeReservation;
|
|
99
|
+
private writeJson;
|
|
100
|
+
private transaction;
|
|
101
|
+
}
|