@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,416 @@
|
|
|
1
|
+
/** Device-wide admission control. These limits are conservative estimates, not measured hardware maxima. */
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { execFileSync } from 'node:child_process';
|
|
4
|
+
import { closeSync, constants, fstatSync, fsyncSync, lstatSync, openSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { availableParallelism, freemem, homedir, totalmem } from 'node:os';
|
|
6
|
+
import { join, resolve } from 'node:path';
|
|
7
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
8
|
+
import { DirectoryLease } from './a2a/lease.js';
|
|
9
|
+
export class CapacityError extends Error {
|
|
10
|
+
code;
|
|
11
|
+
constructor(code) {
|
|
12
|
+
super(code === 'ACP_CAPACITY_MEMORY_LIMIT'
|
|
13
|
+
? 'Available device memory is below the ACP startup reserve; retry after memory is freed.'
|
|
14
|
+
: code === 'ACP_CAPACITY_SOFT_LIMIT'
|
|
15
|
+
? 'Automatic ACP capacity is full; retry after an active task or process finishes.'
|
|
16
|
+
: 'Device ACP capacity is full; retry after an active task or process finishes.');
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.name = 'CapacityError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const processOrigins = new Set(['prewarm', 'job', 'user-opened']);
|
|
22
|
+
const MiB = 1024 ** 2;
|
|
23
|
+
const startupWindowMs = 30_000;
|
|
24
|
+
const tokenPattern = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/;
|
|
25
|
+
const stateMessage = 'ACP capacity state is unavailable; check the local host capacity state before retrying.';
|
|
26
|
+
const pidIsValid = (value) => Number.isSafeInteger(value) && value > 0;
|
|
27
|
+
const validKind = (value) => value === 'process' || value === 'task';
|
|
28
|
+
const validSource = (value) => value === 'automatic' || value === 'interactive';
|
|
29
|
+
/** Allow roughly one automatic agent per two CPUs and 3 GiB, then keep at least one manual slot. */
|
|
30
|
+
export function estimateCapacityLimits() {
|
|
31
|
+
const cores = availableParallelism();
|
|
32
|
+
const memoryGiB = totalmem() / (1024 ** 3);
|
|
33
|
+
const soft = Math.max(1, Math.min(Math.floor(cores / 2), Math.floor(memoryGiB / 3)));
|
|
34
|
+
const hard = Math.max(soft + 1, Math.min(cores, Math.floor(memoryGiB / 2)));
|
|
35
|
+
return { soft, hard, basis: 'hardware-estimate' };
|
|
36
|
+
}
|
|
37
|
+
function limit(value, label) {
|
|
38
|
+
if (value === undefined)
|
|
39
|
+
return undefined;
|
|
40
|
+
const parsed = typeof value === 'string' && /^[1-9]\d*$/.test(value) ? Number(value) : value;
|
|
41
|
+
if (typeof parsed !== 'number' || !Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
42
|
+
throw new Error(`${label} must be a positive safe integer`);
|
|
43
|
+
}
|
|
44
|
+
return parsed;
|
|
45
|
+
}
|
|
46
|
+
function bytes(value, environment, label, allowZero = false) {
|
|
47
|
+
let parsed = value;
|
|
48
|
+
if (parsed === undefined && environment !== undefined) {
|
|
49
|
+
if (!/^(?:0|[1-9]\d*)(?:\.\d+)?$/.test(environment))
|
|
50
|
+
throw new Error(`${label} must be a valid memory size`);
|
|
51
|
+
parsed = Number(environment) * MiB;
|
|
52
|
+
}
|
|
53
|
+
if (parsed === undefined)
|
|
54
|
+
return undefined;
|
|
55
|
+
if (typeof parsed !== 'number' || !Number.isSafeInteger(parsed) || parsed < (allowZero ? 0 : 1)) {
|
|
56
|
+
throw new Error(`${label} must be a valid memory size`);
|
|
57
|
+
}
|
|
58
|
+
return parsed;
|
|
59
|
+
}
|
|
60
|
+
/** Darwin's raw free count excludes reclaimable inactive caches. Purgeable pages are not added twice. */
|
|
61
|
+
export function parseMacOSAvailableMemory(output, totalBytes) {
|
|
62
|
+
const pageSize = /page size of (\d+) bytes/.exec(output);
|
|
63
|
+
if (!pageSize || !Number.isSafeInteger(totalBytes) || totalBytes <= 0)
|
|
64
|
+
throw new Error(stateMessage);
|
|
65
|
+
const pageBytes = Number(pageSize[1]);
|
|
66
|
+
if (!Number.isSafeInteger(pageBytes) || pageBytes <= 0)
|
|
67
|
+
throw new Error(stateMessage);
|
|
68
|
+
let pages = 0;
|
|
69
|
+
for (const label of ['free', 'inactive', 'speculative']) {
|
|
70
|
+
const match = new RegExp(`^Pages ${label}:\\s+(\\d+)\\.?\\s*$`, 'm').exec(output);
|
|
71
|
+
if (!match || !Number.isSafeInteger(Number(match[1])))
|
|
72
|
+
throw new Error(stateMessage);
|
|
73
|
+
pages += Number(match[1]);
|
|
74
|
+
}
|
|
75
|
+
const available = pages * pageBytes;
|
|
76
|
+
if (!Number.isSafeInteger(available))
|
|
77
|
+
throw new Error(stateMessage);
|
|
78
|
+
return Math.min(totalBytes, available);
|
|
79
|
+
}
|
|
80
|
+
function currentMemory() {
|
|
81
|
+
const totalBytes = totalmem();
|
|
82
|
+
if (process.platform === 'darwin') {
|
|
83
|
+
try {
|
|
84
|
+
const output = execFileSync('/usr/bin/vm_stat', [], { encoding: 'utf8', timeout: 1000, maxBuffer: 65536, stdio: ['ignore', 'pipe', 'ignore'] });
|
|
85
|
+
return { totalBytes, availableBytes: parseMacOSAvailableMemory(output, totalBytes), source: 'macos-reclaimable-estimate' };
|
|
86
|
+
}
|
|
87
|
+
catch { /* Fall back conservatively when the host cannot read its reclaimable-page estimate. */ }
|
|
88
|
+
}
|
|
89
|
+
// availableMemory also respects the process/container allocation when the Node host exposes it.
|
|
90
|
+
const processAvailable = process.availableMemory?.();
|
|
91
|
+
return { totalBytes, availableBytes: Math.min(freemem(), processAvailable ?? Infinity), source: 'system-available' };
|
|
92
|
+
}
|
|
93
|
+
/** Only ESRCH proves death. Permission failures and PID reuse retain the reservation. */
|
|
94
|
+
function isDead(pid) {
|
|
95
|
+
try {
|
|
96
|
+
process.kill(pid, 0);
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
return error.code === 'ESRCH';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export function resolveCapacityDirectory(options = {}) {
|
|
104
|
+
const directory = options.directory ?? process.env.ACP_CAPACITY_DIRECTORY ?? join(homedir(), '.1agents', 'state', 'acp-capacity');
|
|
105
|
+
if (typeof directory !== 'string' || !directory.trim())
|
|
106
|
+
throw new Error('ACP capacity directory must be a nonempty path');
|
|
107
|
+
return resolve(directory);
|
|
108
|
+
}
|
|
109
|
+
export class CapacityGovernor {
|
|
110
|
+
directory;
|
|
111
|
+
limits;
|
|
112
|
+
memoryProvider;
|
|
113
|
+
memoryReserveBytes;
|
|
114
|
+
estimatedInstanceBytes;
|
|
115
|
+
constructor(options = {}) {
|
|
116
|
+
this.directory = resolveCapacityDirectory(options);
|
|
117
|
+
const estimate = estimateCapacityLimits();
|
|
118
|
+
const soft = limit(options.softLimit ?? process.env.ACP_CAPACITY_SOFT_LIMIT, 'ACP capacity soft limit');
|
|
119
|
+
const hard = limit(options.hardLimit ?? process.env.ACP_CAPACITY_HARD_LIMIT, 'ACP capacity hard limit');
|
|
120
|
+
this.limits = {
|
|
121
|
+
soft: soft ?? (hard === undefined ? estimate.soft : Math.min(estimate.soft, hard - 1)),
|
|
122
|
+
hard: hard ?? Math.max(estimate.hard, (soft ?? estimate.soft) + 1),
|
|
123
|
+
basis: soft === undefined && hard === undefined ? 'hardware-estimate' : 'configured',
|
|
124
|
+
};
|
|
125
|
+
if (this.limits.soft < 1 || this.limits.hard <= this.limits.soft || !Number.isSafeInteger(this.limits.hard)) {
|
|
126
|
+
throw new Error('ACP capacity hard limit must exceed the soft limit and reserve at least one interactive slot');
|
|
127
|
+
}
|
|
128
|
+
this.memoryProvider = options.memoryProvider ?? currentMemory;
|
|
129
|
+
if (typeof this.memoryProvider !== 'function')
|
|
130
|
+
throw new Error('ACP capacity memory provider must be a function');
|
|
131
|
+
this.memoryReserveBytes = bytes(options.memoryReserveBytes, process.env.ACP_CAPACITY_MEMORY_RESERVE_MB, 'ACP capacity memory reserve', true) ?? Math.max(512 * MiB, Math.min(4 * 1024 * MiB, Math.floor(totalmem() * 0.1)));
|
|
132
|
+
this.estimatedInstanceBytes = bytes(options.estimatedInstanceBytes, process.env.ACP_CAPACITY_INSTANCE_MEMORY_MB, 'ACP estimated instance memory') ?? 1024 * MiB;
|
|
133
|
+
}
|
|
134
|
+
async acquire(request) {
|
|
135
|
+
if (!request || !validKind(request.kind) || !validSource(request.source) || typeof request.id !== 'string' ||
|
|
136
|
+
!request.id.trim() || request.id.length > 4096 ||
|
|
137
|
+
(request.origin !== undefined && (request.kind !== 'process' || !processOrigins.has(request.origin)))) {
|
|
138
|
+
throw new Error('Invalid ACP capacity request');
|
|
139
|
+
}
|
|
140
|
+
const reservation = { version: 1, token: randomUUID(), ownerPid: process.pid, startupUntil: Date.now() + startupWindowMs,
|
|
141
|
+
kind: request.kind, source: request.source, id: request.id, ...(request.origin ? { origin: request.origin } : {}) };
|
|
142
|
+
await this.transaction(() => {
|
|
143
|
+
const records = this.readReservations();
|
|
144
|
+
this.ensurePolicy(records);
|
|
145
|
+
const state = this.count(request.kind, records);
|
|
146
|
+
if (state.used >= this.limits.hard)
|
|
147
|
+
throw new CapacityError('ACP_CAPACITY_HARD_LIMIT');
|
|
148
|
+
if (request.source === 'automatic' && state.used >= this.limits.soft)
|
|
149
|
+
throw new CapacityError('ACP_CAPACITY_SOFT_LIMIT');
|
|
150
|
+
if (state.memory.availableSlots < 1)
|
|
151
|
+
throw new CapacityError('ACP_CAPACITY_MEMORY_LIMIT');
|
|
152
|
+
this.writeReservation({ ...reservation, startupUntil: Date.now() + startupWindowMs });
|
|
153
|
+
});
|
|
154
|
+
// Serialize methods on an individual lease so release can never be followed by resurrection.
|
|
155
|
+
let released = false;
|
|
156
|
+
let pending = Promise.resolve();
|
|
157
|
+
const queue = (operation) => {
|
|
158
|
+
const result = pending.then(operation);
|
|
159
|
+
pending = result.catch(() => undefined);
|
|
160
|
+
return result;
|
|
161
|
+
};
|
|
162
|
+
return {
|
|
163
|
+
recheck: () => queue(async () => {
|
|
164
|
+
if (released)
|
|
165
|
+
throw new Error('ACP capacity lease has already been released');
|
|
166
|
+
await this.transaction(() => {
|
|
167
|
+
const records = this.readReservations();
|
|
168
|
+
this.ensurePolicy(records);
|
|
169
|
+
if (!records.some(item => item.token === reservation.token && item.ownerPid === process.pid))
|
|
170
|
+
throw new Error(stateMessage);
|
|
171
|
+
const state = this.count(request.kind, records, reservation.token);
|
|
172
|
+
if (state.memory.availableSlots < 1)
|
|
173
|
+
throw new CapacityError('ACP_CAPACITY_MEMORY_LIMIT');
|
|
174
|
+
const own = records.find(item => item.token === reservation.token);
|
|
175
|
+
this.writeReservation({ ...own, startupUntil: Date.now() + startupWindowMs });
|
|
176
|
+
});
|
|
177
|
+
}),
|
|
178
|
+
spawned: pid => queue(async () => {
|
|
179
|
+
if (!pidIsValid(pid) || request.kind !== 'process')
|
|
180
|
+
throw new Error('Invalid ACP child process registration');
|
|
181
|
+
if (released)
|
|
182
|
+
throw new Error('ACP capacity lease has already been released');
|
|
183
|
+
await this.transaction(() => {
|
|
184
|
+
const current = this.readReservation(this.reservationPath(reservation.token));
|
|
185
|
+
if (current.ownerPid !== process.pid || current.token !== reservation.token ||
|
|
186
|
+
(current.childPid !== undefined && current.childPid !== pid))
|
|
187
|
+
throw new Error(stateMessage);
|
|
188
|
+
this.writeReservation({ ...current, childPid: pid, startupUntil: Date.now() + startupWindowMs });
|
|
189
|
+
});
|
|
190
|
+
}),
|
|
191
|
+
release: () => queue(async () => {
|
|
192
|
+
if (released)
|
|
193
|
+
return;
|
|
194
|
+
await this.transaction(() => {
|
|
195
|
+
try {
|
|
196
|
+
unlinkSync(this.reservationPath(reservation.token));
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
if (error.code !== 'ENOENT')
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
released = true;
|
|
204
|
+
}),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
async snapshot(kind) {
|
|
208
|
+
if (!validKind(kind))
|
|
209
|
+
throw new Error('Invalid ACP capacity kind');
|
|
210
|
+
return this.transaction(() => {
|
|
211
|
+
const records = this.readReservations();
|
|
212
|
+
this.ensurePolicy(records);
|
|
213
|
+
return this.count(kind, records);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Read both ledgers and process-lease origins. Does not acquire, release, or relabel.
|
|
218
|
+
* A missing origin stays unlabeled. Dead owners stay on disk for the next admission pass.
|
|
219
|
+
*/
|
|
220
|
+
async inspect() {
|
|
221
|
+
return this.transaction(() => {
|
|
222
|
+
const records = this.readReservations(false);
|
|
223
|
+
const limit = this.recordedLimit() ?? this.limits.soft;
|
|
224
|
+
const sessions = { prewarm: [], job: [], 'user-opened': [], unlabeled: [] };
|
|
225
|
+
for (const record of records) {
|
|
226
|
+
if (record.kind !== 'process')
|
|
227
|
+
continue;
|
|
228
|
+
const row = { id: record.id, ownerPid: record.ownerPid, ...(record.childPid !== undefined ? { childPid: record.childPid } : {}) };
|
|
229
|
+
if (record.origin && processOrigins.has(record.origin))
|
|
230
|
+
sessions[record.origin].push(row);
|
|
231
|
+
else
|
|
232
|
+
sessions.unlabeled.push(row);
|
|
233
|
+
}
|
|
234
|
+
const used = (kind) => records.filter(record => record.kind === kind).length;
|
|
235
|
+
return { taskSlot: { limit, used: used('task') }, processSlot: { limit, used: used('process') }, sessions };
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
count(kind, reservations, excludeStartupToken) {
|
|
239
|
+
const matching = reservations.filter(item => item.kind === kind);
|
|
240
|
+
const automatic = matching.filter(item => item.source === 'automatic').length;
|
|
241
|
+
const used = matching.length;
|
|
242
|
+
const reading = this.memoryProvider();
|
|
243
|
+
if (!reading || !Number.isSafeInteger(reading.totalBytes) || reading.totalBytes <= 0 ||
|
|
244
|
+
!Number.isSafeInteger(reading.availableBytes) || reading.availableBytes < 0 || reading.availableBytes > reading.totalBytes) {
|
|
245
|
+
throw new Error(stateMessage);
|
|
246
|
+
}
|
|
247
|
+
// Pending processes retain their budget until spawn is registered. Stable leases stay counted
|
|
248
|
+
// for concurrency, but actual free memory carries their cost after the startup window expires.
|
|
249
|
+
// Only process starts hold startup memory. A task allocates nothing itself: its ACP agent is
|
|
250
|
+
// admitted as a process, so counting the task too would charge the same start twice.
|
|
251
|
+
const starting = reservations.filter(item => item.token !== excludeStartupToken && item.kind === 'process' &&
|
|
252
|
+
(item.childPid === undefined || item.startupUntil > Date.now()));
|
|
253
|
+
const startupReservedBytes = starting.length * this.estimatedInstanceBytes;
|
|
254
|
+
const availableForStartupBytes = Math.max(0, reading.availableBytes - this.memoryReserveBytes - startupReservedBytes);
|
|
255
|
+
const availableSlots = Math.floor(availableForStartupBytes / this.estimatedInstanceBytes);
|
|
256
|
+
return { limits: { ...this.limits }, used, automatic, interactive: used - automatic,
|
|
257
|
+
availableAutomatic: Math.max(0, Math.min(this.limits.soft - used, this.limits.hard - used, availableSlots)),
|
|
258
|
+
availableInteractive: Math.max(0, Math.min(this.limits.hard - used, availableSlots)),
|
|
259
|
+
memory: { ...reading, source: reading.source ?? 'host-provider', reserveBytes: this.memoryReserveBytes, estimatedInstanceBytes: this.estimatedInstanceBytes,
|
|
260
|
+
startupReservedBytes, availableForStartupBytes, availableSlots, startupWindowMs } };
|
|
261
|
+
}
|
|
262
|
+
recordedLimit() {
|
|
263
|
+
try {
|
|
264
|
+
const value = this.readJson(join(this.directory, 'policy.json'));
|
|
265
|
+
if (!value || value.version !== 1 || !Number.isSafeInteger(value.softLimit) || value.softLimit < 1)
|
|
266
|
+
throw new Error(stateMessage);
|
|
267
|
+
return value.softLimit;
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
if (error.code === 'ENOENT')
|
|
271
|
+
return undefined;
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
readReservations(reclaim = true) {
|
|
276
|
+
const records = [];
|
|
277
|
+
for (const name of readdirSync(this.directory)) {
|
|
278
|
+
if (name === '.lock' || name === 'policy.json')
|
|
279
|
+
continue;
|
|
280
|
+
if (name === 'queue') {
|
|
281
|
+
const stat = lstatSync(join(this.directory, name));
|
|
282
|
+
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
283
|
+
throw new Error(stateMessage);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
// A crash before an atomic rename leaves no admitted caller and therefore no spawned child.
|
|
287
|
+
if (/^pending-[a-f0-9-]{36}\.json$/.test(name)) {
|
|
288
|
+
unlinkSync(join(this.directory, name));
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (!/^lease-[a-f0-9-]{36}\.json$/.test(name))
|
|
292
|
+
throw new Error(stateMessage);
|
|
293
|
+
const path = join(this.directory, name);
|
|
294
|
+
const record = this.readReservation(path);
|
|
295
|
+
if (name !== `lease-${record.token}.json`)
|
|
296
|
+
throw new Error(stateMessage);
|
|
297
|
+
// An unregistered process may have spawned just before its owner crashed. Never guess it dead.
|
|
298
|
+
const reclaimable = isDead(record.ownerPid) &&
|
|
299
|
+
(record.kind === 'task' || (record.childPid !== undefined && isDead(record.childPid)));
|
|
300
|
+
if (reclaim && reclaimable)
|
|
301
|
+
unlinkSync(path);
|
|
302
|
+
else
|
|
303
|
+
records.push(record);
|
|
304
|
+
}
|
|
305
|
+
return records;
|
|
306
|
+
}
|
|
307
|
+
reservationPath(token) { return join(this.directory, `lease-${token}.json`); }
|
|
308
|
+
ensurePolicy(records) {
|
|
309
|
+
const policy = { version: 1, softLimit: this.limits.soft, hardLimit: this.limits.hard,
|
|
310
|
+
memoryReserveBytes: this.memoryReserveBytes, estimatedInstanceBytes: this.estimatedInstanceBytes };
|
|
311
|
+
const path = join(this.directory, 'policy.json');
|
|
312
|
+
let current;
|
|
313
|
+
try {
|
|
314
|
+
current = this.readJson(path);
|
|
315
|
+
if (!current || current.version !== 1 || !Number.isSafeInteger(current.softLimit) || current.softLimit < 1 ||
|
|
316
|
+
!Number.isSafeInteger(current.hardLimit) || current.hardLimit <= current.softLimit ||
|
|
317
|
+
!Number.isSafeInteger(current.memoryReserveBytes) || current.memoryReserveBytes < 0 ||
|
|
318
|
+
!Number.isSafeInteger(current.estimatedInstanceBytes) || current.estimatedInstanceBytes <= 0)
|
|
319
|
+
throw new Error(stateMessage);
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
if (error.code !== 'ENOENT')
|
|
323
|
+
throw error;
|
|
324
|
+
if (records.length)
|
|
325
|
+
throw new Error(stateMessage);
|
|
326
|
+
}
|
|
327
|
+
const matches = current && Object.entries(policy).every(([key, value]) => current[key] === value);
|
|
328
|
+
if (matches)
|
|
329
|
+
return;
|
|
330
|
+
// Hosts sharing a device cannot raise each other's active policy by choosing different options.
|
|
331
|
+
// Drain first; releases do not require a policy match, so active hosts can always finish.
|
|
332
|
+
if (records.length)
|
|
333
|
+
throw new Error(stateMessage);
|
|
334
|
+
this.writeJson(path, policy);
|
|
335
|
+
}
|
|
336
|
+
readJson(path) {
|
|
337
|
+
const fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
338
|
+
try {
|
|
339
|
+
const stat = fstatSync(fd);
|
|
340
|
+
if (!stat.isFile() || stat.size > 64 * 1024)
|
|
341
|
+
throw new Error(stateMessage);
|
|
342
|
+
return JSON.parse(readFileSync(fd, 'utf8'));
|
|
343
|
+
}
|
|
344
|
+
finally {
|
|
345
|
+
closeSync(fd);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
readReservation(path) {
|
|
349
|
+
const value = this.readJson(path);
|
|
350
|
+
if (!value || value.version !== 1 || typeof value.token !== 'string' || !tokenPattern.test(value.token) ||
|
|
351
|
+
!pidIsValid(value.ownerPid) || !validKind(value.kind) || !validSource(value.source) ||
|
|
352
|
+
typeof value.id !== 'string' || !value.id.trim() || value.id.length > 4096 ||
|
|
353
|
+
!Number.isSafeInteger(value.startupUntil) || value.startupUntil < 0 ||
|
|
354
|
+
(value.childPid !== undefined && (!pidIsValid(value.childPid) || value.kind !== 'process')) ||
|
|
355
|
+
(value.origin !== undefined && (value.kind !== 'process' || !processOrigins.has(value.origin))))
|
|
356
|
+
throw new Error(stateMessage);
|
|
357
|
+
return value;
|
|
358
|
+
}
|
|
359
|
+
writeReservation(record) {
|
|
360
|
+
this.writeJson(this.reservationPath(record.token), record);
|
|
361
|
+
}
|
|
362
|
+
writeJson(path, value) {
|
|
363
|
+
const temporary = join(this.directory, `pending-${randomUUID()}.json`);
|
|
364
|
+
const fd = openSync(temporary, 'wx', 0o600);
|
|
365
|
+
try {
|
|
366
|
+
writeFileSync(fd, JSON.stringify(value));
|
|
367
|
+
fsyncSync(fd);
|
|
368
|
+
}
|
|
369
|
+
finally {
|
|
370
|
+
closeSync(fd);
|
|
371
|
+
}
|
|
372
|
+
try {
|
|
373
|
+
renameSync(temporary, path);
|
|
374
|
+
}
|
|
375
|
+
finally {
|
|
376
|
+
try {
|
|
377
|
+
unlinkSync(temporary);
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
if (error.code !== 'ENOENT')
|
|
381
|
+
throw error;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
async transaction(operation) {
|
|
386
|
+
let lease;
|
|
387
|
+
const deadline = performance.now() + 5000;
|
|
388
|
+
do {
|
|
389
|
+
try {
|
|
390
|
+
lease = new DirectoryLease(this.directory);
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
if (performance.now() >= deadline)
|
|
395
|
+
throw new Error(stateMessage);
|
|
396
|
+
}
|
|
397
|
+
await delay(10 + Math.floor(Math.random() * 20));
|
|
398
|
+
} while (true);
|
|
399
|
+
try {
|
|
400
|
+
return operation();
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
if (error instanceof CapacityError)
|
|
404
|
+
throw error;
|
|
405
|
+
throw new Error(stateMessage);
|
|
406
|
+
}
|
|
407
|
+
finally {
|
|
408
|
+
try {
|
|
409
|
+
lease.close();
|
|
410
|
+
}
|
|
411
|
+
catch {
|
|
412
|
+
throw new Error(stateMessage);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
package/dist/src/dsh.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Host services are requested only when DSH activates this package as a plugin. */
|
|
2
2
|
export const name = 'oneagents-acp';
|
|
3
|
-
export const inject = ['llm', 'agents', 'approval', 'userQuestions', 'commands', 'sessionProjections', 'webServer', 'sessionController', 'agentPresets', 'sessions', 'sessionPersistence', 'workspaceRegistry'];
|
|
3
|
+
export const inject = ['llm', 'agents', 'approval', 'userQuestions', 'commands', 'sessionProjections', 'webServer', 'sessionController', 'agentPresets', 'sessions', 'sessionPersistence', 'workspaceRegistry', 'tools', 'connection'];
|
|
4
4
|
/** Keep the service API usable without loading plugin code or DSH declarations.
|
|
5
5
|
* The fully typed plugin entry is available from /dsh. */
|
|
6
6
|
export async function apply(ctx, input = {}) {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare const AGENT_FILE_MAX_BYTES: number;
|
|
2
|
+
export declare const agentNamePattern: RegExp;
|
|
3
|
+
export interface AgentDefinition {
|
|
4
|
+
name: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
/** Real path of the Markdown file. */
|
|
7
|
+
file: string;
|
|
8
|
+
/** The Markdown body after the frontmatter: the role's instructions. */
|
|
9
|
+
body: string;
|
|
10
|
+
sha256: string;
|
|
11
|
+
}
|
|
12
|
+
/** Reads the top-level `key: value` lines of a leading `---` block. Nested YAML is not needed for name and description. */
|
|
13
|
+
export declare function parseAgentMarkdown(text: string): {
|
|
14
|
+
fields: Record<string, string>;
|
|
15
|
+
body: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Read one agent file that must stay inside `root` after symlinks resolve.
|
|
19
|
+
* The frontmatter `name` must be a valid agent name; the body must not be empty.
|
|
20
|
+
*/
|
|
21
|
+
export declare function readAgentFile(file: string, root: string): Promise<AgentDefinition>;
|
|
22
|
+
/** List `<root>/agents/*.md` in name order. Names must be unique within the directory. */
|
|
23
|
+
export declare function listAgentFiles(root: string): Promise<AgentDefinition[]>;
|
|
24
|
+
/** The prompt prefix a host prepends to a path agent's first message. */
|
|
25
|
+
export declare function agentPromptPrefix(agent: AgentDefinition): string;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/** Plugin-style agent definitions, the format Claude Code and Grok load natively: `<root>/agents/*.md`, Markdown with a frontmatter header. */
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { constants } from 'node:fs';
|
|
4
|
+
import { lstat, open, readdir, realpath } from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
export const AGENT_FILE_MAX_BYTES = 64 * 1024;
|
|
7
|
+
const MAX_AGENT_FILES = 100;
|
|
8
|
+
export const agentNamePattern = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
9
|
+
const within = (root, value) => {
|
|
10
|
+
const relative = path.relative(root, value);
|
|
11
|
+
return relative !== '' && relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
12
|
+
};
|
|
13
|
+
/** Reads the top-level `key: value` lines of a leading `---` block. Nested YAML is not needed for name and description. */
|
|
14
|
+
export function parseAgentMarkdown(text) {
|
|
15
|
+
const normalized = text.replace(/^/, '').replace(/\r\n/g, '\n');
|
|
16
|
+
const match = /^---\n([\s\S]*?)\n---(?:\n|$)/.exec(normalized);
|
|
17
|
+
if (!match)
|
|
18
|
+
return { fields: {}, body: normalized.trim() };
|
|
19
|
+
const fields = {};
|
|
20
|
+
for (const line of match[1].split('\n')) {
|
|
21
|
+
const pair = /^([A-Za-z][A-Za-z0-9_-]*):\s*(.*)$/.exec(line);
|
|
22
|
+
if (!pair)
|
|
23
|
+
continue;
|
|
24
|
+
let value = pair[2].trim();
|
|
25
|
+
if (value.length >= 2 && ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))))
|
|
26
|
+
value = value.slice(1, -1);
|
|
27
|
+
fields[pair[1]] = value;
|
|
28
|
+
}
|
|
29
|
+
return { fields, body: normalized.slice(match[0].length).trim() };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Read one agent file that must stay inside `root` after symlinks resolve.
|
|
33
|
+
* The frontmatter `name` must be a valid agent name; the body must not be empty.
|
|
34
|
+
*/
|
|
35
|
+
export async function readAgentFile(file, root) {
|
|
36
|
+
const actualRoot = await realpath(root), actual = await realpath(file);
|
|
37
|
+
if (!within(actualRoot, actual) || path.extname(actual) !== '.md')
|
|
38
|
+
throw new Error('Agent file must be a .md file inside its directory');
|
|
39
|
+
const handle = await open(actual, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
40
|
+
let bytes;
|
|
41
|
+
try {
|
|
42
|
+
const info = await handle.stat();
|
|
43
|
+
if (!info.isFile() || info.size > AGENT_FILE_MAX_BYTES)
|
|
44
|
+
throw new Error(`Agent file must be a regular file of at most ${AGENT_FILE_MAX_BYTES} bytes`);
|
|
45
|
+
bytes = await handle.readFile();
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
await handle.close();
|
|
49
|
+
}
|
|
50
|
+
const { fields, body } = parseAgentMarkdown(bytes.toString('utf8'));
|
|
51
|
+
const name = fields.name ?? path.basename(actual, '.md');
|
|
52
|
+
if (!agentNamePattern.test(name))
|
|
53
|
+
throw new Error(`Invalid agent name in ${path.basename(actual)}`);
|
|
54
|
+
if (!body)
|
|
55
|
+
throw new Error(`Agent file ${path.basename(actual)} has no instructions`);
|
|
56
|
+
return { name, ...(fields.description ? { description: fields.description } : {}), file: actual, body,
|
|
57
|
+
sha256: createHash('sha256').update(bytes).digest('hex') };
|
|
58
|
+
}
|
|
59
|
+
/** List `<root>/agents/*.md` in name order. Names must be unique within the directory. */
|
|
60
|
+
export async function listAgentFiles(root) {
|
|
61
|
+
if (!path.isAbsolute(root))
|
|
62
|
+
throw new Error('Agent path must be absolute');
|
|
63
|
+
const actualRoot = await realpath(root);
|
|
64
|
+
if (!(await lstat(actualRoot)).isDirectory())
|
|
65
|
+
throw new Error('Agent path must be a directory');
|
|
66
|
+
const directory = path.join(actualRoot, 'agents');
|
|
67
|
+
let entries;
|
|
68
|
+
try {
|
|
69
|
+
entries = await readdir(directory, { withFileTypes: true });
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (error.code === 'ENOENT')
|
|
73
|
+
throw new Error('No agents/ directory under this path');
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
const files = entries.filter(entry => entry.name.endsWith('.md') && (entry.isFile() || entry.isSymbolicLink())).map(entry => entry.name).sort();
|
|
77
|
+
if (files.length > MAX_AGENT_FILES)
|
|
78
|
+
throw new Error(`At most ${MAX_AGENT_FILES} agent files are supported`);
|
|
79
|
+
const agents = [];
|
|
80
|
+
for (const name of files)
|
|
81
|
+
agents.push(await readAgentFile(path.join(directory, name), actualRoot));
|
|
82
|
+
const names = agents.map(agent => agent.name);
|
|
83
|
+
if (new Set(names).size !== names.length)
|
|
84
|
+
throw new Error('Agent names must be unique within agents/');
|
|
85
|
+
return agents;
|
|
86
|
+
}
|
|
87
|
+
/** The prompt prefix a host prepends to a path agent's first message. */
|
|
88
|
+
export function agentPromptPrefix(agent) {
|
|
89
|
+
return `宿主已按路径加载智能体 ${agent.name}(${agent.file})。以下是它的角色说明:\n\n${agent.body}\n\n上游任务:\n`;
|
|
90
|
+
}
|