clikcode 1.0.3 → 1.0.5
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 -14
- package/dist/chunk-23TDEBGZ.js +71 -0
- package/dist/chunk-25T3GVS7.js +425 -0
- package/dist/chunk-2BBELGHY.js +11 -0
- package/dist/chunk-2EOL5PZL.js +208 -0
- package/dist/chunk-2GAB36IE.js +26 -0
- package/dist/chunk-2OLS7V66.js +2440 -0
- package/dist/chunk-2QFO2URB.js +179 -0
- package/dist/chunk-2QWPQRSU.js +254 -0
- package/dist/chunk-2YPAPAR3.js +184 -0
- package/dist/chunk-2ZAG5372.js +33 -0
- package/dist/chunk-32IKAIZC.js +12507 -0
- package/dist/chunk-34AZEGYW.js +95 -0
- package/dist/chunk-34JQYQCA.js +175 -0
- package/dist/chunk-34ZISPAO.js +67 -0
- package/dist/chunk-3AAAARE6.js +46 -0
- package/dist/chunk-3AJ6Q3OY.js +510 -0
- package/dist/chunk-3B2CHX7M.js +884 -0
- package/dist/chunk-3BUFORRX.js +59 -0
- package/dist/chunk-3QLHYYFK.js +30 -0
- package/dist/chunk-3RXBMYKZ.js +140 -0
- package/dist/chunk-3TFNIJWS.js +52 -0
- package/dist/chunk-3W3IPDFP.js +119 -0
- package/dist/chunk-3WMS7SEE.js +417 -0
- package/dist/chunk-435ZNSZ3.js +23 -0
- package/dist/chunk-4AQ5NINV.js +39 -0
- package/dist/chunk-4GVEJ5FK.js +168 -0
- package/dist/chunk-4GXAO3UY.js +262 -0
- package/dist/chunk-4L2ZZM4Q.js +332 -0
- package/dist/chunk-4LPDG57B.js +17 -0
- package/dist/chunk-4NN3XQQT.js +223 -0
- package/dist/chunk-4QZEZME7.js +54 -0
- package/dist/chunk-4R6CSC3S.js +38 -0
- package/dist/chunk-4RHXTL3W.js +514 -0
- package/dist/chunk-4SJMPTVQ.js +18 -0
- package/dist/chunk-4VY7G6TX.js +114 -0
- package/dist/chunk-5642PBF7.js +124 -0
- package/dist/chunk-5B6TVO4A.js +33 -0
- package/dist/chunk-5BJLDUPL.js +62 -0
- package/dist/chunk-5HMFFJWU.js +19 -0
- package/dist/chunk-5IKLK33T.js +69 -0
- package/dist/chunk-5NQ5FFMX.js +44 -0
- package/dist/chunk-5QNNW5L6.js +176 -0
- package/dist/chunk-5SM5UAWN.js +239 -0
- package/dist/chunk-5WYJTA6O.js +74 -0
- package/dist/chunk-5ZSR2Y5T.js +227 -0
- package/dist/chunk-5ZWUM5GH.js +39 -0
- package/dist/chunk-64E7D4HY.js +364 -0
- package/dist/chunk-6CK466BC.js +99 -0
- package/dist/chunk-6ES45HHO.js +138 -0
- package/dist/chunk-6HYEZFGL.js +277 -0
- package/dist/chunk-6Y7HLXGW.js +29 -0
- package/dist/chunk-6YKK4FYR.js +258 -0
- package/dist/chunk-74WNMLMS.js +117 -0
- package/dist/chunk-7AULZ7OD.js +55 -0
- package/dist/chunk-7DOKGSUN.js +101 -0
- package/dist/chunk-7IH5LTIZ.js +195 -0
- package/dist/chunk-7ISTS5NS.js +21 -0
- package/dist/chunk-7NMUTGOW.js +527 -0
- package/dist/chunk-7PNHUZEL.js +153 -0
- package/dist/chunk-7TSQLONI.js +88 -0
- package/dist/chunk-AE7TOLD5.js +30 -0
- package/dist/chunk-AEMHSXKA.js +130 -0
- package/dist/chunk-AKRYTS2D.js +211 -0
- package/dist/chunk-AMNSE36L.js +59 -0
- package/dist/chunk-AZVD74ZV.js +23 -0
- package/dist/chunk-B35DVOTK.js +1981 -0
- package/dist/chunk-B4HNJRAH.js +626 -0
- package/dist/chunk-B5WYPPHS.js +13 -0
- package/dist/chunk-BBOVDXQM.js +59 -0
- package/dist/chunk-BD3ECZLK.js +62 -0
- package/dist/chunk-BDGSOT74.js +4578 -0
- package/dist/chunk-BF5VEOPK.js +31 -0
- package/dist/chunk-BFFRW4XH.js +47 -0
- package/dist/chunk-BFPC6PEW.js +208 -0
- package/dist/chunk-BGEMY2JJ.js +376 -0
- package/dist/chunk-BISGNHKD.js +35 -0
- package/dist/chunk-BMRAEBRY.js +21 -0
- package/dist/chunk-BNTXAZXW.js +127 -0
- package/dist/chunk-BOQBXGSN.js +75 -0
- package/dist/chunk-BTJDSTQP.js +213 -0
- package/dist/chunk-BUOPJQP5.js +218 -0
- package/dist/chunk-BXFJS65Q.js +52 -0
- package/dist/chunk-BXPQFSX5.js +59 -0
- package/dist/chunk-C57LIWNY.js +1269 -0
- package/dist/chunk-C6YVCCWR.js +74 -0
- package/dist/chunk-CAADDF6M.js +167 -0
- package/dist/chunk-CBANJFBO.js +69 -0
- package/dist/chunk-CERF3RIB.js +597 -0
- package/dist/chunk-COAS5ETR.js +46 -0
- package/dist/chunk-CQ7J6UQ7.js +78 -0
- package/dist/chunk-CRUVYGO2.js +26 -0
- package/dist/chunk-CVFVFRGN.js +58 -0
- package/dist/chunk-D2UK66HM.js +37 -0
- package/dist/chunk-D2W7SA4R.js +18 -0
- package/dist/chunk-DA44ZENT.js +501 -0
- package/dist/chunk-DAJ76TOC.js +99 -0
- package/dist/chunk-DCZ6CIUM.js +147 -0
- package/dist/chunk-DF35KMXM.js +1314 -0
- package/dist/chunk-DGASQT26.js +151 -0
- package/dist/chunk-DJT57C5P.js +55 -0
- package/dist/chunk-DKDPZTD3.js +44 -0
- package/dist/chunk-DQYVQ2N7.js +77 -0
- package/dist/chunk-DRTP2RTY.js +266 -0
- package/dist/chunk-DW2BMUKY.js +61 -0
- package/dist/chunk-DW5T5ZVU.js +307 -0
- package/dist/chunk-DXKNDNFC.js +164 -0
- package/dist/chunk-DZEQ7UYR.js +291 -0
- package/dist/chunk-E4F4OUAC.js +463 -0
- package/dist/chunk-EE4B6A7W.js +69 -0
- package/dist/chunk-EISKIYWG.js +23 -0
- package/dist/chunk-EKAQK7UQ.js +235 -0
- package/dist/chunk-EOBTJV32.js +123 -0
- package/dist/chunk-ESF3DIZY.js +325 -0
- package/dist/chunk-EVOHOZ4U.js +45 -0
- package/dist/chunk-EXN4KHLB.js +110 -0
- package/dist/chunk-F252T6Y3.js +78 -0
- package/dist/chunk-F3DP4ZA2.js +40 -0
- package/dist/chunk-F6F47QAY.js +58 -0
- package/dist/chunk-FA3Y23ZW.js +57 -0
- package/dist/chunk-FBKXE4EJ.js +640 -0
- package/dist/chunk-FCGEB3H6.js +23 -0
- package/dist/chunk-FF7ZVYJF.js +3732 -0
- package/dist/chunk-FFGKN6L3.js +39 -0
- package/dist/chunk-FFGU4GCC.js +94 -0
- package/dist/chunk-FSFDAOVP.js +74 -0
- package/dist/chunk-FT5TX7OX.js +1573 -0
- package/dist/chunk-G3QP67Z7.js +232 -0
- package/dist/chunk-G5ORJKV2.js +179 -0
- package/dist/chunk-GFNC62PE.js +111 -0
- package/dist/chunk-GGK43Q3N.js +67 -0
- package/dist/chunk-GGYKY4GF.js +11537 -0
- package/dist/chunk-GGYNHGNJ.js +55 -0
- package/dist/chunk-GLY7RM3C.js +354 -0
- package/dist/chunk-GQBZU6EO.js +20 -0
- package/dist/chunk-GSXENUZL.js +9 -0
- package/dist/chunk-GTOAT5D3.js +40 -0
- package/dist/chunk-GWCMDPQU.js +1007 -0
- package/dist/chunk-GXYSMYHJ.js +152 -0
- package/dist/chunk-GYMAI5NY.js +14 -0
- package/dist/chunk-H2VZMKPE.js +30 -0
- package/dist/chunk-H4THUNBM.js +2091 -0
- package/dist/chunk-HGRVSNUZ.js +361 -0
- package/dist/chunk-HI4VY473.js +27 -0
- package/dist/chunk-HOJRSD3Q.js +447 -0
- package/dist/chunk-HRX25W35.js +82 -0
- package/dist/chunk-HSE4AQYV.js +55 -0
- package/dist/chunk-HU5SSUFX.js +383 -0
- package/dist/chunk-I3GDBJLH.js +196 -0
- package/dist/chunk-I75EDU75.js +684 -0
- package/dist/chunk-IBA7DMIO.js +20 -0
- package/dist/chunk-IC6N4NZA.js +231 -0
- package/dist/chunk-IF43HIGV.js +284 -0
- package/dist/chunk-IGZ6C2MC.js +439 -0
- package/dist/chunk-IMLTL2MY.js +192 -0
- package/dist/chunk-IMZL6SGR.js +33 -0
- package/dist/chunk-IS4USJPY.js +516 -0
- package/dist/chunk-ISFSPN2F.js +82 -0
- package/dist/chunk-IXE7GTFI.js +55 -0
- package/dist/chunk-IZ77QJQG.js +36 -0
- package/dist/chunk-J2PACWRQ.js +34 -0
- package/dist/chunk-J6EGFVQX.js +180 -0
- package/dist/chunk-JEKOD43D.js +22 -0
- package/dist/chunk-JGIWATSG.js +30 -0
- package/dist/chunk-JYBONKD4.js +43 -0
- package/dist/chunk-K2NLG2HK.js +454 -0
- package/dist/chunk-KCQ4TBLS.js +164 -0
- package/dist/chunk-KD5KIVMS.js +176 -0
- package/dist/chunk-KHW6TX6I.js +63 -0
- package/dist/chunk-KI5GZFCE.js +520 -0
- package/dist/chunk-KN34B7NO.js +105 -0
- package/dist/chunk-KYUHIFP3.js +136 -0
- package/dist/chunk-KZL5VS4B.js +300 -0
- package/dist/chunk-L3VW3QIH.js +48 -0
- package/dist/chunk-LA6MG4ET.js +2110 -0
- package/dist/chunk-LAVFOA3S.js +66 -0
- package/dist/chunk-LB4OA77N.js +13 -0
- package/dist/chunk-LBFRF2K4.js +196 -0
- package/dist/chunk-LM6EIA4B.js +93 -0
- package/dist/chunk-LPCNRGJ7.js +692 -0
- package/dist/chunk-LSLNL5T3.js +168 -0
- package/dist/chunk-LVRRWP4W.js +568 -0
- package/dist/chunk-LWCFBRHL.js +33 -0
- package/dist/chunk-LXFTMGZZ.js +166 -0
- package/dist/chunk-M33LVA4M.js +151 -0
- package/dist/chunk-M3FO5UMK.js +4944 -0
- package/dist/chunk-M5QNDBAY.js +66 -0
- package/dist/chunk-M6ND6KBE.js +1291 -0
- package/dist/chunk-M7H3WQTE.js +76 -0
- package/dist/chunk-MCBRVPA7.js +191 -0
- package/dist/chunk-MCSDWZET.js +91 -0
- package/dist/chunk-MDT5XDXL.js +289 -0
- package/dist/chunk-MJG3IQY4.js +91 -0
- package/dist/chunk-MOU3N2UL.js +44 -0
- package/dist/chunk-MP4KZOQC.js +3451 -0
- package/dist/chunk-MPAJ446F.js +66 -0
- package/dist/chunk-MVJZZ6AL.js +16 -0
- package/dist/chunk-MXS6S2PY.js +253 -0
- package/dist/chunk-N3SFDYM7.js +291 -0
- package/dist/chunk-NAOX3JUB.js +13 -0
- package/dist/chunk-NEKKIBCX.js +283 -0
- package/dist/chunk-NKDATMEM.js +133 -0
- package/dist/chunk-NR4FMC4K.js +395 -0
- package/dist/chunk-NSOHOJBR.js +221 -0
- package/dist/chunk-NTDB6QKF.js +29 -0
- package/dist/chunk-NWEYRBWK.js +25 -0
- package/dist/chunk-O2YSNYWD.js +23 -0
- package/dist/chunk-O3EW7LAY.js +54 -0
- package/dist/chunk-O7BNJIXU.js +366 -0
- package/dist/chunk-OARL2NXZ.js +116 -0
- package/dist/chunk-OHKZUEJW.js +351 -0
- package/dist/chunk-OKU45BXA.js +647 -0
- package/dist/chunk-OL4JBD3J.js +59 -0
- package/dist/chunk-OLUO5DDR.js +192 -0
- package/dist/chunk-OQ245JLA.js +196 -0
- package/dist/chunk-P72UZAQI.js +289 -0
- package/dist/chunk-PAL2ENB6.js +20 -0
- package/dist/chunk-PAXYQZH7.js +53 -0
- package/dist/chunk-PDPEM5VW.js +51 -0
- package/dist/chunk-PEWCDI2C.js +668 -0
- package/dist/chunk-PGZ54EDX.js +27 -0
- package/dist/chunk-PRWVLDZF.js +73 -0
- package/dist/chunk-PSQQ75VJ.js +52 -0
- package/dist/chunk-PUOVIFHW.js +118 -0
- package/dist/chunk-PWBF2X3V.js +107 -0
- package/dist/chunk-PWXII6HF.js +43 -0
- package/dist/chunk-Q35VLJRT.js +80 -0
- package/dist/chunk-QJP7EYGL.js +128 -0
- package/dist/chunk-QPQKX2MK.js +8836 -0
- package/dist/chunk-QQRLPY25.js +1053 -0
- package/dist/chunk-QUS4ER2W.js +407 -0
- package/dist/chunk-QYHI3YET.js +29 -0
- package/dist/chunk-QYRU2UQI.js +32 -0
- package/dist/chunk-R6XVVQGC.js +13 -0
- package/dist/chunk-R7QN6ONS.js +483 -0
- package/dist/chunk-RD23MPGC.js +221 -0
- package/dist/chunk-RDDWQBEM.js +185 -0
- package/dist/chunk-RDLLDYFO.js +104 -0
- package/dist/chunk-RE4TGORR.js +84 -0
- package/dist/chunk-RJLHVYQY.js +217 -0
- package/dist/chunk-RJLPTWHW.js +905 -0
- package/dist/chunk-RKUVLRZI.js +295 -0
- package/dist/chunk-ROGXPGYR.js +37 -0
- package/dist/chunk-RSZGEL5U.js +163 -0
- package/dist/chunk-RVUOIJXN.js +909 -0
- package/dist/chunk-RW7F4JRP.js +11 -0
- package/dist/chunk-S4UO3AO3.js +53 -0
- package/dist/chunk-S5JAAF6S.js +585 -0
- package/dist/chunk-SNU37TQI.js +56 -0
- package/dist/chunk-SQVUDO2O.js +31 -0
- package/dist/chunk-SYVHIYQT.js +66 -0
- package/dist/chunk-SZBQMTG2.js +85 -0
- package/dist/chunk-T5VCXA3J.js +25 -0
- package/dist/chunk-TK6NSP56.js +30 -0
- package/dist/chunk-TKWVMA6L.js +22 -0
- package/dist/chunk-TQDKJHWX.js +59 -0
- package/dist/chunk-TQDM7S5N.js +127 -0
- package/dist/chunk-TULAXS77.js +23 -0
- package/dist/chunk-TVT45XNH.js +4625 -0
- package/dist/chunk-TWCVV2JO.js +467 -0
- package/dist/chunk-U4DKAD6S.js +47 -0
- package/dist/chunk-UA5K2BTI.js +564 -0
- package/dist/chunk-UAMKCMOK.js +40 -0
- package/dist/chunk-UBMVADBF.js +51 -0
- package/dist/chunk-UFQ5A5PW.js +645 -0
- package/dist/chunk-UOD2SOLL.js +1301 -0
- package/dist/chunk-UTSB4LOW.js +156 -0
- package/dist/chunk-UVXNIUTN.js +412 -0
- package/dist/chunk-UXVPXYPE.js +90 -0
- package/dist/chunk-UYEEMIWR.js +374 -0
- package/dist/chunk-VBGM5ZBW.js +54 -0
- package/dist/chunk-VEWPCZPG.js +762 -0
- package/dist/chunk-VIRDLZ5E.js +108 -0
- package/dist/chunk-VMPO7WKN.js +52 -0
- package/dist/chunk-W2NVSDO5.js +5122 -0
- package/dist/chunk-W3YYPBEA.js +176 -0
- package/dist/chunk-W5AYOICN.js +280 -0
- package/dist/chunk-W5GZOIUF.js +27 -0
- package/dist/chunk-WHKNLZ3C.js +76 -0
- package/dist/chunk-WKBEPPQ5.js +1409 -0
- package/dist/chunk-WM3Z6QKQ.js +151 -0
- package/dist/chunk-WMI2TPMA.js +31 -0
- package/dist/chunk-WNX5VD7S.js +150 -0
- package/dist/chunk-WOVTYST3.js +60 -0
- package/dist/chunk-WPB5WEZJ.js +546 -0
- package/dist/chunk-WUO5EOAT.js +44 -0
- package/dist/chunk-WWLJMP6D.js +133 -0
- package/dist/chunk-WZMLH3XP.js +58 -0
- package/dist/chunk-WZYVLUVQ.js +79 -0
- package/dist/chunk-X2OCPVU5.js +135 -0
- package/dist/chunk-X3CL6DTR.js +95 -0
- package/dist/chunk-X6WF7JEV.js +354 -0
- package/dist/chunk-XAHGBPWT.js +44 -0
- package/dist/chunk-XCKQ5BM2.js +228 -0
- package/dist/chunk-XIBYREEY.js +106 -0
- package/dist/chunk-XKW5NDMR.js +378 -0
- package/dist/chunk-XRDALAO3.js +100 -0
- package/dist/chunk-XUP22EAS.js +41 -0
- package/dist/chunk-XWAC3MKW.js +243 -0
- package/dist/chunk-Y3R7JMXB.js +59 -0
- package/dist/chunk-Y54LTP4W.js +82 -0
- package/dist/chunk-Y7UECI4M.js +99 -0
- package/dist/chunk-YACGDFHC.js +47 -0
- package/dist/chunk-YHNIQPKY.js +71 -0
- package/dist/chunk-YMBR6HYP.js +111 -0
- package/dist/chunk-YMCH3L2S.js +26 -0
- package/dist/chunk-Z4ZU6T7V.js +99 -0
- package/dist/chunk-ZDPF67ZZ.js +173 -0
- package/dist/chunk-ZFC2Q67K.js +101 -0
- package/dist/chunk-ZGHNNSCT.js +114 -0
- package/dist/chunk-ZGX3ERAT.js +173 -0
- package/dist/chunk-ZI3YHEBY.js +37 -0
- package/dist/chunk-ZN7G6JLS.js +639 -0
- package/dist/chunk-ZNPFQMXT.js +47 -0
- package/dist/chunk-ZOU3LV4U.js +63 -0
- package/dist/chunk-ZT7B4H2V.js +117 -0
- package/dist/chunk-ZZCBDNZI.js +58 -0
- package/dist/cli.js +498 -112593
- package/dist/computer-tools.js +36642 -21258
- package/dist/harness-catalog.cjs +88 -43
- package/dist/index.js +1 -1
- package/dist/local-model-supervisor.cjs +555 -0
- package/package.json +3 -4
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { createRequire as __clikcodeCreateRequire } from 'node:module';
|
|
2
|
+
const require = __clikcodeCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
keyShaped
|
|
5
|
+
} from "./chunk-MCBRVPA7.js";
|
|
6
|
+
import {
|
|
7
|
+
deleteNamedSecret,
|
|
8
|
+
readNamedSecret,
|
|
9
|
+
readVaultRecords,
|
|
10
|
+
removeVaultRecord,
|
|
11
|
+
saveNamedSecret,
|
|
12
|
+
writeVaultRecord
|
|
13
|
+
} from "./chunk-I75EDU75.js";
|
|
14
|
+
import {
|
|
15
|
+
quiet
|
|
16
|
+
} from "./chunk-RDDWQBEM.js";
|
|
17
|
+
|
|
18
|
+
// src/agent/computer/profiles.ts
|
|
19
|
+
import { randomUUID } from "node:crypto";
|
|
20
|
+
var PROFILE_KEYS = ["given_name", "family_name", "email", "phone", "address_line1", "address_line2", "city", "state", "postal_code", "country", "organization", "job_title", "website", "birthday", "sso_email"];
|
|
21
|
+
var PASSWORD_SECRET = "password";
|
|
22
|
+
var FOLDER = "profile";
|
|
23
|
+
async function listProfiles(stateDir) {
|
|
24
|
+
return (await readVaultRecords(stateDir, FOLDER)).map(({ id, value }) => ({ id, label: value.label, fields: value.fields, ...value.custom ? { custom: value.custom } : {}, ...value.secrets ? { secrets: value.secrets } : {} })).sort((a, b) => a.label.localeCompare(b.label));
|
|
25
|
+
}
|
|
26
|
+
function defaultProfile(profiles) {
|
|
27
|
+
const standard = (profile) => PROFILE_KEYS.filter((key) => profile.fields[key]).length;
|
|
28
|
+
return [...profiles].sort((a, b) => Number(b.label.toLowerCase() === "personal") - Number(a.label.toLowerCase() === "personal") || standard(b) - standard(a) || a.label.localeCompare(b.label))[0];
|
|
29
|
+
}
|
|
30
|
+
async function profileEmail(stateDir) {
|
|
31
|
+
const listed = await listProfiles(stateDir);
|
|
32
|
+
const first = defaultProfile(listed);
|
|
33
|
+
return (first ? [first, ...listed.filter((profile) => profile !== first)] : listed).map((profile) => profile.fields.sso_email).find(Boolean);
|
|
34
|
+
}
|
|
35
|
+
async function saveProfile(stateDir, input) {
|
|
36
|
+
const fields = {};
|
|
37
|
+
for (const [key, value] of Object.entries(input.fields)) {
|
|
38
|
+
if (!PROFILE_KEYS.includes(key)) throw new Error(`Unknown profile field "${key}". Fields: ${PROFILE_KEYS.join(", ")}.`);
|
|
39
|
+
if (typeof value === "string" && value.trim()) fields[key] = (key === "phone" ? normalizePhone(value) : value.trim()).slice(0, 200);
|
|
40
|
+
}
|
|
41
|
+
const all = await listProfiles(stateDir);
|
|
42
|
+
const existing = all.find((profile) => profile.id === input.id) ?? (input.label ? all.find((profile) => profile.label.toLowerCase() === input.label.toLowerCase()) : !input.id ? defaultProfile(all) : void 0);
|
|
43
|
+
const custom = mergeCustom(existing?.custom, input.custom);
|
|
44
|
+
const saved = existing ? { ...existing, label: input.id ? input.label?.trim() || existing.label : existing.label, fields: { ...existing.fields, ...fields }, ...custom ? { custom } : {} } : { id: randomUUID(), label: input.label?.trim() || "Personal", fields, ...custom ? { custom } : {} };
|
|
45
|
+
await writeVaultRecord(stateDir, FOLDER, saved.id, { label: saved.label, fields: saved.fields, ...saved.custom ? { custom: saved.custom } : {}, ...saved.secrets ? { secrets: saved.secrets } : {} });
|
|
46
|
+
return saved;
|
|
47
|
+
}
|
|
48
|
+
async function removeProfile(stateDir, id) {
|
|
49
|
+
const profile = (await listProfiles(stateDir)).find((item) => item.id === id);
|
|
50
|
+
for (const key of Object.keys(profile?.secrets ?? {})) await deleteNamedSecret(stateDir, secretName(id, key)).catch(() => false);
|
|
51
|
+
return removeVaultRecord(stateDir, FOLDER, id);
|
|
52
|
+
}
|
|
53
|
+
var secretName = (id, key) => `profile/${id.slice(0, 8)}/${key}`;
|
|
54
|
+
async function saveProfileSecret(stateDir, id, key, value, spec = {}) {
|
|
55
|
+
assertCustomKey(key, true);
|
|
56
|
+
const profile = (await listProfiles(stateDir)).find((item) => item.id === id);
|
|
57
|
+
if (!profile) throw new Error("No such profile.");
|
|
58
|
+
if (!value) throw new Error(`The secret "${key}" is empty.`);
|
|
59
|
+
await saveNamedSecret(stateDir, secretName(id, key), value);
|
|
60
|
+
const label = (spec.label?.trim() || profile.secrets?.[key]?.label || key.replace(/_/g, " ")).slice(0, 80);
|
|
61
|
+
const match = spec.match ? spec.match.map((item) => item.trim().toLowerCase()).filter((item) => item.length >= 3 && item.length <= 60).slice(0, 8) : profile.secrets?.[key]?.match;
|
|
62
|
+
if (Object.keys(profile.secrets ?? {}).length >= MAX_SECRETS && !profile.secrets?.[key]) throw new Error(`A profile holds at most ${MAX_SECRETS} secrets.`);
|
|
63
|
+
const secrets = { ...profile.secrets, [key]: { label, ...match?.length ? { match } : {} } };
|
|
64
|
+
await writeVaultRecord(stateDir, FOLDER, id, { label: profile.label, fields: profile.fields, ...profile.custom ? { custom: profile.custom } : {}, secrets });
|
|
65
|
+
}
|
|
66
|
+
async function readProfileSecret(stateDir, profile, key) {
|
|
67
|
+
return profile.secrets?.[key] ? readNamedSecret(stateDir, secretName(profile.id, key)).catch(quiet("agent/computer/profiles")) : void 0;
|
|
68
|
+
}
|
|
69
|
+
var CUSTOM_KEY = /^[a-z][a-z0-9_]{0,39}$/;
|
|
70
|
+
function assertCustomKey(key, secret = false) {
|
|
71
|
+
if (!CUSTOM_KEY.test(key)) throw new Error(`A custom field name is lower-case letters, digits and underscores, starting with a letter (got "${key}").`);
|
|
72
|
+
if (!secret && PROFILE_KEYS.includes(key)) throw new Error(`"${key}" is a standard profile field: save it with fields, not custom.`);
|
|
73
|
+
}
|
|
74
|
+
var MAX_CUSTOM_FIELDS = 40;
|
|
75
|
+
var MAX_SECRETS = 20;
|
|
76
|
+
var MAX_CUSTOM_VALUE = 2e3;
|
|
77
|
+
function mergeCustom(existing, input) {
|
|
78
|
+
if (!input || !Object.keys(input).length) return existing;
|
|
79
|
+
const next = { ...existing };
|
|
80
|
+
for (const [key, given] of Object.entries(input)) {
|
|
81
|
+
assertCustomKey(key);
|
|
82
|
+
const value = typeof given.value === "string" ? given.value.trim() : "";
|
|
83
|
+
if (!value) {
|
|
84
|
+
delete next[key];
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (value.length > MAX_CUSTOM_VALUE) throw new Error(`The value of "${key}" is longer than ${MAX_CUSTOM_VALUE} characters.`);
|
|
88
|
+
if (value.split(/\s+/).some(keyShaped)) throw new Error(`The value of "${key}" looks like a key or token. A profile holds details, not secrets: capture it with capture_secret instead.`);
|
|
89
|
+
const label = (given.label?.trim() || next[key]?.label || key.replace(/_/g, " ")).slice(0, 80);
|
|
90
|
+
const match = given.match ? given.match.map((item) => item.trim().toLowerCase()).filter((item) => item.length >= 3 && item.length <= 60).slice(0, 8) : next[key]?.match;
|
|
91
|
+
next[key] = { label, value, ...match?.length ? { match } : {} };
|
|
92
|
+
}
|
|
93
|
+
if (Object.keys(next).length > MAX_CUSTOM_FIELDS) throw new Error(`A profile holds at most ${MAX_CUSTOM_FIELDS} custom fields.`);
|
|
94
|
+
return Object.keys(next).length ? next : void 0;
|
|
95
|
+
}
|
|
96
|
+
function describeProfile(profile) {
|
|
97
|
+
const standard = PROFILE_KEYS.filter((key) => profile.fields[key]).map((key) => `${key}=${profile.fields[key]}`);
|
|
98
|
+
const custom = Object.entries(profile.custom ?? {}).map(([key, field]) => `${key}=${field.value.length > 300 ? `${field.value.slice(0, 300)}\u2026` : field.value}`);
|
|
99
|
+
const secrets = Object.keys(profile.secrets ?? {}).map((key) => `${key}=(secret, hidden)`);
|
|
100
|
+
return `${profile.label}: ${[...standard, ...custom, ...secrets].join(", ") || "(empty)"}`;
|
|
101
|
+
}
|
|
102
|
+
var TOKENS = {
|
|
103
|
+
"given-name": "given_name",
|
|
104
|
+
"family-name": "family_name",
|
|
105
|
+
email: "email",
|
|
106
|
+
tel: "phone",
|
|
107
|
+
"tel-national": "phone",
|
|
108
|
+
"street-address": "address_line1",
|
|
109
|
+
"address-line1": "address_line1",
|
|
110
|
+
"address-line2": "address_line2",
|
|
111
|
+
"address-level2": "city",
|
|
112
|
+
"address-level1": "state",
|
|
113
|
+
"postal-code": "postal_code",
|
|
114
|
+
country: "country",
|
|
115
|
+
"country-name": "country",
|
|
116
|
+
organization: "organization",
|
|
117
|
+
"organization-title": "job_title",
|
|
118
|
+
url: "website",
|
|
119
|
+
bday: "birthday",
|
|
120
|
+
"bday-day": "birthday",
|
|
121
|
+
"bday-month": "birthday",
|
|
122
|
+
"bday-year": "birthday"
|
|
123
|
+
};
|
|
124
|
+
var plain = (text) => text.normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/ß/g, "ss").toLowerCase();
|
|
125
|
+
var WORDS = [
|
|
126
|
+
[/e-?mail|correo|courriel|\bmail\b/, "email"],
|
|
127
|
+
[/company|organi[sz]ation|organizacao|employer|business.?name|nombre.?de.?(?:la.?)?(?:empresa|compania)|\bempresa\b|razon.?social|ragione.?sociale|\bazienda\b|entreprise|societe|raison.?sociale|firma|unternehmen|\bbetrieb\b|nome.?da.?empresa/, "organization"],
|
|
128
|
+
[/zip|postal|post.?code|codigo.?postal|\bcep\b|code.?postal|postleitzahl|\bplz\b|\bcap\b|codice.?postale/, "postal_code"],
|
|
129
|
+
[/phone|mobile|\btel\b|\bcell|telefon|telefone|telephone|\bmovil\b|celular|handy|cellulare|\bfono\b/, "phone"],
|
|
130
|
+
[/\bcity\b|town|ciudad|cidade|\bville\b|stadt|\bort\b|citta|localidad|municipio|comune/, "city"],
|
|
131
|
+
[/\bstate\b|province|region(?!al)|provincia|\bestado\b|\betat\b|bundesland|\bregiao\b|\bregione\b|departamento/, "state"],
|
|
132
|
+
[/country|territory|\bpais\b|\bpays\b|\bland\b|\blaender\b|paese|nacion/, "country"],
|
|
133
|
+
[/first.?name|given.?name|forename|\bfname\b|\bnombres?\b|\bnome\b|prenom|vorname|\bnom\b(?!.*(?:de|da|empresa|entreprise|societe))/, "given_name"],
|
|
134
|
+
[/last.?name|family.?name|surname|\blname\b|apellidos?|sobrenome|cognome|nom.?de.?famille|nachname|familienname/, "family_name"],
|
|
135
|
+
[/address.?(?:line)?.?2|\bapt\b|suite|\bunit\b|\bapartment\b|apartamento|\bpiso\b|complement|zusatz|interno/, "address_line2"],
|
|
136
|
+
[/address|street|direccion|domicilio|endereco|adresse|strasse|\bcalle\b|\brua\b|indirizzo|\bvia\b|\brue\b/, "address_line1"],
|
|
137
|
+
[/job.?title|position|\brole\b|cargo|puesto|\bfonction\b|\bberuf\b|\bruolo\b/, "job_title"],
|
|
138
|
+
[/website|\burl\b|sitio.?web|pagina.?web|site.?web|webseite|sito/, "website"],
|
|
139
|
+
[/birth|\bdob\b|\bbday\b|\b(?:dd|mm|yyyy)\b|nacimiento|nascimento|naissance|geburt|nascita/, "birthday"]
|
|
140
|
+
];
|
|
141
|
+
var UNFILLABLE_KIND = /\b(?:registration|registro|company|business|tax|vat|fiscal|steuer|iva|identification|identificacion|trade|legal)\b/;
|
|
142
|
+
var UNFILLABLE_NUMBER = /\b(?:number|no|nr|num|numero|nummer|id|identifier)\b/;
|
|
143
|
+
var UNFILLABLE_ALONE = /steuernummer|umsatzsteuer|\b(?:vat|ein|cif|nif|cnpj|cpf|siret|siren|abn|duns|rfc|tin|registration|registro)\b/;
|
|
144
|
+
function isUnfillable(hint) {
|
|
145
|
+
const text = plain(`${hint.label ?? ""} ${hint.text}`).replace(/[_\-[\].]+/g, " ");
|
|
146
|
+
if (/postal|zip|post code|e mail|email|correo|phone|\btel\b|telefon|mobile|movil/.test(text)) return false;
|
|
147
|
+
return UNFILLABLE_ALONE.test(text) || UNFILLABLE_KIND.test(text) && UNFILLABLE_NUMBER.test(text);
|
|
148
|
+
}
|
|
149
|
+
function isSecretField(hint) {
|
|
150
|
+
const text = plain(`${hint.autocomplete} ${hint.text}`);
|
|
151
|
+
if (hint.type === "password") return true;
|
|
152
|
+
if (/password|passcode|one-time|verification|security.?code|\botp\b|\bpin\b|card|cvv|cvc|contrasena|senha|mot.?de.?passe|passwort|kennwort|codigo.?de.?(?:verificacion|seguridad)|verifizierungscode|code.?de.?verification/.test(text)) return !/postal|zip|post.?code|postleitzahl|\bcep\b/.test(text);
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
var words = (text) => ` ${text.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim()} `;
|
|
156
|
+
function customFieldFor(profile, hint) {
|
|
157
|
+
if (!profile?.custom || isSecretField(hint)) return void 0;
|
|
158
|
+
const asked = words(hint.text);
|
|
159
|
+
let best;
|
|
160
|
+
for (const [key, field] of Object.entries(profile.custom)) {
|
|
161
|
+
for (const phrase of field.match?.length ? field.match : [field.label, key.replace(/_/g, " ")]) {
|
|
162
|
+
const needle = words(phrase);
|
|
163
|
+
if (needle.trim().length >= 3 && asked.includes(needle) && (!best || needle.length > best.length)) best = { key, field, length: needle.length };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return best ? { key: best.key, field: best.field } : void 0;
|
|
167
|
+
}
|
|
168
|
+
function secretFieldFor(profile, hint) {
|
|
169
|
+
if (!profile?.secrets) return void 0;
|
|
170
|
+
if (hint.type === "password" && profile.secrets[PASSWORD_SECRET] && !/one-time|verification|security.?code|\botp\b|current-password/i.test(`${hint.autocomplete} ${hint.text}`)) return PASSWORD_SECRET;
|
|
171
|
+
const asked = words(hint.text);
|
|
172
|
+
let best;
|
|
173
|
+
for (const [key, field] of Object.entries(profile.secrets)) {
|
|
174
|
+
if (key === PASSWORD_SECRET) continue;
|
|
175
|
+
for (const phrase of field.match?.length ? field.match : [field.label, key.replace(/_/g, " ")]) {
|
|
176
|
+
const needle = words(phrase);
|
|
177
|
+
if (needle.trim().length >= 3 && asked.includes(needle) && (!best || needle.length > best.length)) best = { key, length: needle.length };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return best?.key;
|
|
181
|
+
}
|
|
182
|
+
function profileKeyFor(hint) {
|
|
183
|
+
if (isSecretField(hint) || isUnfillable(hint)) return void 0;
|
|
184
|
+
for (const token of hint.autocomplete.toLowerCase().split(/\s+/)) if (TOKENS[token]) return TOKENS[token];
|
|
185
|
+
for (const text of [hint.label, hint.text]) {
|
|
186
|
+
if (!text?.trim()) continue;
|
|
187
|
+
const flat = plain(text).replace(/[_\-[\].]+/g, " ");
|
|
188
|
+
for (const [pattern, key] of WORDS) if (pattern.test(flat)) return key;
|
|
189
|
+
}
|
|
190
|
+
return hint.type === "email" ? "email" : hint.type === "tel" ? "phone" : void 0;
|
|
191
|
+
}
|
|
192
|
+
function phoneFor(value, placeholder = "", currentValue = "") {
|
|
193
|
+
const digits = value.replace(/\D/g, "");
|
|
194
|
+
const national = value.trim().startsWith("+1") || digits.length === 11 && digits.startsWith("1") ? digits.slice(1) : digits;
|
|
195
|
+
if (currentValue.trim().startsWith("+")) return value.trim().startsWith("+") ? value.trim() : `+1${national}`;
|
|
196
|
+
if (national.length === 10 && /\(/.test(placeholder)) return `(${national.slice(0, 3)}) ${national.slice(3, 6)}-${national.slice(6)}`;
|
|
197
|
+
if (national.length === 10 && /\d{3}-\d{3}/.test(placeholder)) return `${national.slice(0, 3)}-${national.slice(3, 6)}-${national.slice(6)}`;
|
|
198
|
+
if (national.length === 10 && /^\d{10}$/.test(placeholder.trim())) return national;
|
|
199
|
+
return value.trim();
|
|
200
|
+
}
|
|
201
|
+
function normalizePhone(value) {
|
|
202
|
+
const digits = value.replace(/\D/g, "");
|
|
203
|
+
if (digits.length === 10) return `+1${digits}`;
|
|
204
|
+
if (digits.length === 11 && digits.startsWith("1")) return `+${digits}`;
|
|
205
|
+
return value.trim().startsWith("+") ? `+${digits}` : digits;
|
|
206
|
+
}
|
|
207
|
+
var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
|
208
|
+
function birthdayFor(value, words2 = "") {
|
|
209
|
+
const match = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.exec(value.trim());
|
|
210
|
+
if (!match) return value.trim();
|
|
211
|
+
const [, month, day, year] = match;
|
|
212
|
+
const hint = words2.toLowerCase();
|
|
213
|
+
if (/\b(yyyy|year)\b/.test(hint) && !/\b(mm|dd|month|day)\b/.test(hint)) return year;
|
|
214
|
+
if (/\b(dd|day)\b/.test(hint) && !/\b(mm|month|yyyy|year)\b/.test(hint)) return day;
|
|
215
|
+
if (/\b(mm|month)\b/.test(hint) && !/\b(dd|day|yyyy|year)\b/.test(hint)) return /\bmm\b/.test(hint) ? month : MONTHS[Number(month) - 1] ?? month;
|
|
216
|
+
const order = /\d{2}[-/. ]\d{2}[-/. ]\d{4}|dd[-/. ]mm|mm[-/. ]dd|yyyy[-/. ]mm/.exec(hint)?.[0] ?? "";
|
|
217
|
+
if (/^yyyy/.test(order)) return `${year}-${month}-${day}`;
|
|
218
|
+
if (/^dd/.test(order)) return `${day}/${month}/${year}`;
|
|
219
|
+
return `${month}/${day}/${year}`;
|
|
220
|
+
}
|
|
221
|
+
var GENERIC_LABEL_WORDS = /* @__PURE__ */ new Set(["admin", "personal", "work", "main", "default", "account", "profile", "home", "business", "company", "bot", "the", "and", "for"]);
|
|
222
|
+
function profileDeclaresHost(profile, host) {
|
|
223
|
+
if (!host) return false;
|
|
224
|
+
const bare = host.toLowerCase().replace(/^www\./, "");
|
|
225
|
+
const site = profile.fields.website ? plain(profile.fields.website).replace(/^[a-z]+:\/\//, "").replace(/^www\./, "").split(/[/?#]/)[0] : "";
|
|
226
|
+
if (site && (bare === site || bare.endsWith(`.${site}`))) return true;
|
|
227
|
+
for (const listed of (profile.custom?.sites?.value ?? "").split(/[,\s]+/)) {
|
|
228
|
+
const named = plain(listed).replace(/^[a-z]+:\/\//, "").replace(/^www\./, "").split(/[/?#]/)[0];
|
|
229
|
+
if (named && (bare === named || bare.endsWith(`.${named}`))) return true;
|
|
230
|
+
}
|
|
231
|
+
const hostWords = bare.split(".").slice(0, -1).join(".");
|
|
232
|
+
return plain(profile.label).split(/[^a-z0-9]+/).some((word) => word.length >= 3 && !GENERIC_LABEL_WORDS.has(word) && hostWords.split(/[^a-z0-9]+/).includes(word));
|
|
233
|
+
}
|
|
234
|
+
function bestProfileFor(profiles, _hints, label, host) {
|
|
235
|
+
if (profiles.length <= 1) return profiles[0];
|
|
236
|
+
const named = label ? profiles.find((candidate) => candidate.label.toLowerCase() === label.trim().toLowerCase()) : void 0;
|
|
237
|
+
if (named) return named;
|
|
238
|
+
const def = defaultProfile(profiles);
|
|
239
|
+
return profiles.find((candidate) => candidate !== def && profileDeclaresHost(candidate, host)) ?? def;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export {
|
|
243
|
+
PROFILE_KEYS,
|
|
244
|
+
PASSWORD_SECRET,
|
|
245
|
+
listProfiles,
|
|
246
|
+
defaultProfile,
|
|
247
|
+
profileEmail,
|
|
248
|
+
saveProfile,
|
|
249
|
+
removeProfile,
|
|
250
|
+
saveProfileSecret,
|
|
251
|
+
readProfileSecret,
|
|
252
|
+
assertCustomKey,
|
|
253
|
+
describeProfile,
|
|
254
|
+
isUnfillable,
|
|
255
|
+
customFieldFor,
|
|
256
|
+
secretFieldFor,
|
|
257
|
+
profileKeyFor,
|
|
258
|
+
phoneFor,
|
|
259
|
+
normalizePhone,
|
|
260
|
+
birthdayFor,
|
|
261
|
+
bestProfileFor
|
|
262
|
+
};
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { createRequire as __clikcodeCreateRequire } from 'node:module';
|
|
2
|
+
const require = __clikcodeCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
ensurePrivateDirectory
|
|
5
|
+
} from "./chunk-F6F47QAY.js";
|
|
6
|
+
import {
|
|
7
|
+
lifecycle,
|
|
8
|
+
quiet
|
|
9
|
+
} from "./chunk-RDDWQBEM.js";
|
|
10
|
+
import {
|
|
11
|
+
stateDirectory
|
|
12
|
+
} from "./chunk-MPAJ446F.js";
|
|
13
|
+
|
|
14
|
+
// src/session/store/locks.ts
|
|
15
|
+
import { randomBytes } from "node:crypto";
|
|
16
|
+
import { readlinkSync } from "node:fs";
|
|
17
|
+
import { link, open, readdir, readFile, rename, stat, unlink, utimes, writeFile } from "node:fs/promises";
|
|
18
|
+
import { hostname } from "node:os";
|
|
19
|
+
import { basename, dirname, join } from "node:path";
|
|
20
|
+
var StateLockTimeoutError = class extends Error {
|
|
21
|
+
constructor(lockPath, holder, heldMs) {
|
|
22
|
+
super(`ClikCode could not lock its local state: a task in this process has held it for ${Math.round(heldMs / 1e3)}s without finishing (${lockPath}${holder ? `, held by ${holder}` : ""}). Nothing was written.`);
|
|
23
|
+
this.name = "StateLockTimeoutError";
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
function pidIsAlive(pid) {
|
|
27
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
28
|
+
try {
|
|
29
|
+
process.kill(pid, 0);
|
|
30
|
+
return true;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
return error.code === "EPERM";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function writeOwnerFile(path, contents, writer = writeFile) {
|
|
36
|
+
try {
|
|
37
|
+
await writer(path, contents, { encoding: "utf8", mode: 384, flag: "wx" });
|
|
38
|
+
} catch (error) {
|
|
39
|
+
await unlink(path).catch(quiet("session/store/locks"));
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
var LOCK_TUNING = {
|
|
44
|
+
/** A lock whose file has not been refreshed for this long is stale. */
|
|
45
|
+
staleMs: 3e4,
|
|
46
|
+
/** A lock file with no readable owner is stale this soon. A lock is
|
|
47
|
+
* created with its owner already in it (createLock), so only an older
|
|
48
|
+
* build -- open, then write -- shows one empty, for the moment between
|
|
49
|
+
* the two steps; one that stays empty was left by a process killed
|
|
50
|
+
* there, and waiting staleMs on it stood a VS Code send 30 s. */
|
|
51
|
+
ownerlessStaleMs: 1e3,
|
|
52
|
+
/** A holder refreshes its lock file's mtime this often... */
|
|
53
|
+
heartbeatMs: 1e4,
|
|
54
|
+
/** ...for at most this long. A holder stuck past it stops refreshing, and
|
|
55
|
+
* is broken staleMs later rather than stalling every ClikCode forever. */
|
|
56
|
+
maxHoldMs: 5 * 6e4,
|
|
57
|
+
/** A waiter behind a live holder never gives up (a holder that is alive and
|
|
58
|
+
* heartbeating is only slow, and a stuck one is broken by maxHoldMs +
|
|
59
|
+
* staleMs); it records a 'lock.wait' event this often so the wait is
|
|
60
|
+
* visible. It never proceeds unlocked. */
|
|
61
|
+
waitNoticeMs: 1e4
|
|
62
|
+
};
|
|
63
|
+
var lockQueues = /* @__PURE__ */ new Map();
|
|
64
|
+
var ownPidNamespace;
|
|
65
|
+
function pidNamespace() {
|
|
66
|
+
if (ownPidNamespace === void 0) {
|
|
67
|
+
try {
|
|
68
|
+
ownPidNamespace = readlinkSync("/proc/self/ns/pid");
|
|
69
|
+
} catch {
|
|
70
|
+
ownPidNamespace = null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return ownPidNamespace ?? void 0;
|
|
74
|
+
}
|
|
75
|
+
function parseLockOwner(raw) {
|
|
76
|
+
try {
|
|
77
|
+
const parsed = JSON.parse(raw);
|
|
78
|
+
return typeof parsed.pid === "number" && typeof parsed.nonce === "string" && typeof parsed.host === "string" ? parsed : void 0;
|
|
79
|
+
} catch {
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async function lockLooksStale(lockPath, raw) {
|
|
84
|
+
const age = await stat(lockPath).then((info) => Date.now() - info.mtimeMs).catch(quiet("session/store/locks"));
|
|
85
|
+
if (age === void 0) return false;
|
|
86
|
+
if (age > LOCK_TUNING.staleMs) return true;
|
|
87
|
+
const owner = parseLockOwner(raw);
|
|
88
|
+
if (!owner) return age > LOCK_TUNING.ownerlessStaleMs;
|
|
89
|
+
if (owner.host !== hostname()) return false;
|
|
90
|
+
if (owner.pidns !== void 0 && owner.pidns !== pidNamespace()) return false;
|
|
91
|
+
return !pidIsAlive(owner.pid);
|
|
92
|
+
}
|
|
93
|
+
async function breakStaleLock(lockPath, observedRaw, stillStale) {
|
|
94
|
+
const breaking = `${lockPath}.breaking`;
|
|
95
|
+
try {
|
|
96
|
+
await (await open(breaking, "wx", 384)).close();
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (error.code !== "EEXIST") return;
|
|
99
|
+
const age = await stat(breaking).then((info) => Date.now() - info.mtimeMs).catch(quiet("session/store/locks"));
|
|
100
|
+
if (age !== void 0 && age > LOCK_TUNING.staleMs) await unlink(breaking).catch(quiet("session/store/locks"));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const judged = await stat(lockPath).catch(quiet("session/store/locks"));
|
|
105
|
+
const current = await readFile(lockPath, "utf8").catch(quiet("session/store/locks"));
|
|
106
|
+
if (!judged || current !== observedRaw) return;
|
|
107
|
+
if (stillStale && !await stillStale(current)) return;
|
|
108
|
+
const aside = `${lockPath}.${process.pid}.${randomBytes(6).toString("hex")}.stale`;
|
|
109
|
+
try {
|
|
110
|
+
await rename(lockPath, aside);
|
|
111
|
+
} catch {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const taken = await stat(aside).catch(quiet("session/store/locks"));
|
|
115
|
+
if (!taken || taken.ino !== judged.ino) await link(aside, lockPath).catch(quiet("session/store/locks"));
|
|
116
|
+
await unlink(aside).catch(quiet("session/store/locks"));
|
|
117
|
+
await sweepOrphanTemps(lockPath);
|
|
118
|
+
} finally {
|
|
119
|
+
await unlink(breaking).catch(quiet("session/store/locks"));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function sweepOrphanTemps(lockPath) {
|
|
123
|
+
const prefix = `${basename(lockPath)}.`;
|
|
124
|
+
const names = await readdir(dirname(lockPath)).catch(() => []);
|
|
125
|
+
await Promise.all(names.filter((name) => name.startsWith(prefix) && /\.\d+\.[0-9a-f]+\.(new|stale)$/.test(name)).map(async (name) => {
|
|
126
|
+
const path = join(dirname(lockPath), name);
|
|
127
|
+
const age = await stat(path).then((info) => Date.now() - info.mtimeMs).catch(quiet("session/store/locks"));
|
|
128
|
+
if (age !== void 0 && age > ORPHAN_TEMP_MS) await unlink(path).catch(quiet("session/store/locks"));
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
var ORPHAN_TEMP_MS = 6e4;
|
|
132
|
+
async function withFileLock(lockPath, run, options = {}) {
|
|
133
|
+
const previous = lockQueues.get(lockPath) ?? Promise.resolve();
|
|
134
|
+
const result = queueTurn(lockPath, previous).then(() => holdFileLock(lockPath, run, options.fair === true));
|
|
135
|
+
const settled = result.catch(quiet("session/store/locks"));
|
|
136
|
+
lockQueues.set(lockPath, settled);
|
|
137
|
+
void settled.then(() => {
|
|
138
|
+
if (lockQueues.get(lockPath) === settled) lockQueues.delete(lockPath);
|
|
139
|
+
});
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
var running = /* @__PURE__ */ new Map();
|
|
143
|
+
function queueTurn(lockPath, previous) {
|
|
144
|
+
return new Promise((resolve, reject) => {
|
|
145
|
+
const poll = setInterval(() => {
|
|
146
|
+
const since = running.get(lockPath);
|
|
147
|
+
const limit = LOCK_TUNING.maxHoldMs + LOCK_TUNING.staleMs;
|
|
148
|
+
if (since !== void 0 && Date.now() - since > limit) {
|
|
149
|
+
clearInterval(poll);
|
|
150
|
+
reject(new StateLockTimeoutError(lockPath, `this process (pid ${process.pid})`, Date.now() - since));
|
|
151
|
+
}
|
|
152
|
+
}, Math.max(10, Math.min(1e3, Math.floor((LOCK_TUNING.maxHoldMs + LOCK_TUNING.staleMs) / 4))));
|
|
153
|
+
poll.unref?.();
|
|
154
|
+
void previous.then(() => {
|
|
155
|
+
clearInterval(poll);
|
|
156
|
+
resolve();
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
async function fileLocksIdle() {
|
|
161
|
+
while (lockQueues.size) await Promise.all([...lockQueues.values()]);
|
|
162
|
+
}
|
|
163
|
+
function fileLocksHeld() {
|
|
164
|
+
return lockQueues.size > 0;
|
|
165
|
+
}
|
|
166
|
+
async function createLock(lockPath, mine) {
|
|
167
|
+
const temp = `${lockPath}.${process.pid}.${randomBytes(6).toString("hex")}.new`;
|
|
168
|
+
await writeOwnerFile(temp, mine);
|
|
169
|
+
try {
|
|
170
|
+
await link(temp, lockPath);
|
|
171
|
+
return true;
|
|
172
|
+
} catch (error) {
|
|
173
|
+
if (error.code === "EEXIST") return false;
|
|
174
|
+
throw error;
|
|
175
|
+
} finally {
|
|
176
|
+
await unlink(temp).catch(quiet("session/store/locks"));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
async function tryLease(lockPath) {
|
|
180
|
+
await ensurePrivateDirectory(dirname(lockPath));
|
|
181
|
+
const pidns = pidNamespace();
|
|
182
|
+
const owner = { pid: process.pid, host: hostname(), nonce: randomBytes(12).toString("hex"), at: (/* @__PURE__ */ new Date()).toISOString(), ...pidns ? { pidns } : {} };
|
|
183
|
+
const mine = JSON.stringify(owner);
|
|
184
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
185
|
+
if (await createLock(lockPath, mine)) return leaseOf(lockPath, mine);
|
|
186
|
+
const raw = await readFile(lockPath, "utf8").catch(quiet("session/store/locks"));
|
|
187
|
+
if (raw === void 0) continue;
|
|
188
|
+
if (!await lockLooksStale(lockPath, raw)) return void 0;
|
|
189
|
+
await breakStaleLock(lockPath, raw, (current) => lockLooksStale(lockPath, current));
|
|
190
|
+
}
|
|
191
|
+
return void 0;
|
|
192
|
+
}
|
|
193
|
+
function leaseOf(lockPath, mine) {
|
|
194
|
+
const held = async () => await readFile(lockPath, "utf8").catch(quiet("session/store/locks")) === mine;
|
|
195
|
+
const heartbeat = setInterval(() => {
|
|
196
|
+
void held().then((still) => {
|
|
197
|
+
if (!still) {
|
|
198
|
+
clearInterval(heartbeat);
|
|
199
|
+
return void 0;
|
|
200
|
+
}
|
|
201
|
+
const now = /* @__PURE__ */ new Date();
|
|
202
|
+
return utimes(lockPath, now, now);
|
|
203
|
+
}).catch(quiet("session/store/locks"));
|
|
204
|
+
}, LOCK_TUNING.heartbeatMs);
|
|
205
|
+
heartbeat.unref();
|
|
206
|
+
return {
|
|
207
|
+
held,
|
|
208
|
+
release: async () => {
|
|
209
|
+
clearInterval(heartbeat);
|
|
210
|
+
if (await held()) await unlink(lockPath).catch(quiet("session/store/locks"));
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
async function takeTicket(lockPath, mine) {
|
|
215
|
+
const queue = `${lockPath}.queue`;
|
|
216
|
+
await ensurePrivateDirectory(queue);
|
|
217
|
+
const ticket = join(queue, `${String(Date.now()).padStart(15, "0")}.${process.pid}.${randomBytes(6).toString("hex")}`);
|
|
218
|
+
await writeOwnerFile(ticket, mine);
|
|
219
|
+
return ticket;
|
|
220
|
+
}
|
|
221
|
+
async function firstInLine(ticket) {
|
|
222
|
+
const queue = dirname(ticket);
|
|
223
|
+
const own = basename(ticket);
|
|
224
|
+
const names = (await readdir(queue).catch(() => [])).filter((name) => name < own).sort();
|
|
225
|
+
for (const name of names) {
|
|
226
|
+
const path = join(queue, name);
|
|
227
|
+
const raw = await readFile(path, "utf8").catch(quiet("session/store/locks"));
|
|
228
|
+
if (raw === void 0) continue;
|
|
229
|
+
if (!await lockLooksStale(path, raw)) return false;
|
|
230
|
+
await unlink(path).catch(quiet("session/store/locks"));
|
|
231
|
+
}
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
async function holdFileLock(lockPath, run, fair) {
|
|
235
|
+
await ensurePrivateDirectory(dirname(lockPath));
|
|
236
|
+
const pidns = pidNamespace();
|
|
237
|
+
const owner = { pid: process.pid, host: hostname(), nonce: randomBytes(12).toString("hex"), at: (/* @__PURE__ */ new Date()).toISOString(), ...pidns ? { pidns } : {} };
|
|
238
|
+
const mine = JSON.stringify(owner);
|
|
239
|
+
const started = Date.now();
|
|
240
|
+
let holder;
|
|
241
|
+
let lastNotice = started;
|
|
242
|
+
let ticket = fair ? await takeTicket(lockPath, mine) : void 0;
|
|
243
|
+
let ticketTouched = Date.now();
|
|
244
|
+
try {
|
|
245
|
+
for (; ; ) {
|
|
246
|
+
const next = !ticket || await firstInLine(ticket);
|
|
247
|
+
if (next && await createLock(lockPath, mine)) break;
|
|
248
|
+
const raw = await readFile(lockPath, "utf8").catch(quiet("session/store/locks"));
|
|
249
|
+
if (raw !== void 0) {
|
|
250
|
+
const other = parseLockOwner(raw);
|
|
251
|
+
holder = other ? `pid ${other.pid} on ${other.host}` : void 0;
|
|
252
|
+
if (await lockLooksStale(lockPath, raw)) {
|
|
253
|
+
await breakStaleLock(lockPath, raw, (current) => lockLooksStale(lockPath, current));
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (Date.now() - lastNotice >= LOCK_TUNING.waitNoticeMs) {
|
|
258
|
+
lastNotice = Date.now();
|
|
259
|
+
lifecycle("lock.wait", { lock: basename(lockPath), ms: lastNotice - started, waiting: true, ...holder ? { holder } : {} });
|
|
260
|
+
}
|
|
261
|
+
if (ticket && Date.now() - ticketTouched > LOCK_TUNING.heartbeatMs) {
|
|
262
|
+
ticketTouched = Date.now();
|
|
263
|
+
const now = /* @__PURE__ */ new Date();
|
|
264
|
+
await utimes(ticket, now, now).catch(quiet("session/store/locks"));
|
|
265
|
+
}
|
|
266
|
+
await new Promise((resolve) => setTimeout(resolve, ticket && next ? 2 + Math.floor(Math.random() * 3) : 8 + Math.floor(Math.random() * 12)));
|
|
267
|
+
}
|
|
268
|
+
} finally {
|
|
269
|
+
if (ticket) await unlink(ticket).catch(quiet("session/store/locks"));
|
|
270
|
+
ticket = void 0;
|
|
271
|
+
}
|
|
272
|
+
const waitedMs = Date.now() - started;
|
|
273
|
+
if (waitedMs >= 1e3) lifecycle("lock.wait", { lock: basename(lockPath), ms: waitedMs, ...holder ? { holder } : {} });
|
|
274
|
+
const heldSince = Date.now();
|
|
275
|
+
const heartbeat = setInterval(() => {
|
|
276
|
+
if (Date.now() - heldSince > LOCK_TUNING.maxHoldMs) {
|
|
277
|
+
clearInterval(heartbeat);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
void readFile(lockPath, "utf8").then((current) => {
|
|
281
|
+
if (current !== mine) {
|
|
282
|
+
clearInterval(heartbeat);
|
|
283
|
+
return void 0;
|
|
284
|
+
}
|
|
285
|
+
const now = /* @__PURE__ */ new Date();
|
|
286
|
+
return utimes(lockPath, now, now);
|
|
287
|
+
}).catch(quiet("session/store/locks"));
|
|
288
|
+
}, LOCK_TUNING.heartbeatMs);
|
|
289
|
+
heartbeat.unref();
|
|
290
|
+
running.set(lockPath, Date.now());
|
|
291
|
+
try {
|
|
292
|
+
return await run();
|
|
293
|
+
} finally {
|
|
294
|
+
running.delete(lockPath);
|
|
295
|
+
clearInterval(heartbeat);
|
|
296
|
+
const current = await readFile(lockPath, "utf8").catch(quiet("session/store/locks"));
|
|
297
|
+
if (current === mine) await unlink(lockPath).catch(quiet("session/store/locks"));
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
function stateLockPath() {
|
|
301
|
+
return join(stateDirectory(), "harness-state.json.lock");
|
|
302
|
+
}
|
|
303
|
+
var STATE_LOCK_HELD = Object.freeze({});
|
|
304
|
+
function withStateLock(run) {
|
|
305
|
+
const site = new Error().stack;
|
|
306
|
+
return withFileLock(stateLockPath(), async () => {
|
|
307
|
+
const started = Date.now();
|
|
308
|
+
try {
|
|
309
|
+
return await run(STATE_LOCK_HELD);
|
|
310
|
+
} finally {
|
|
311
|
+
const ms = Date.now() - started;
|
|
312
|
+
if (ms >= LONG_HOLD_MS) lifecycle("lock.held", { lock: "harness-state.json.lock", ms, by: holdSite(site) });
|
|
313
|
+
}
|
|
314
|
+
}, { fair: true });
|
|
315
|
+
}
|
|
316
|
+
var LONG_HOLD_MS = 500;
|
|
317
|
+
function holdSite(stack) {
|
|
318
|
+
return stack?.split("\n").slice(1).map((line) => line.trim()).filter((line) => !/\bwith(State|File)Lock\b|[\\/]locks\.[jt]s/.test(line)).slice(0, 3).map((line) => line.replace(/^at /, "").replace(/ \(.*\)$/, "")).join(" < ") || void 0;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export {
|
|
322
|
+
pidIsAlive,
|
|
323
|
+
writeOwnerFile,
|
|
324
|
+
LOCK_TUNING,
|
|
325
|
+
lockLooksStale,
|
|
326
|
+
breakStaleLock,
|
|
327
|
+
withFileLock,
|
|
328
|
+
fileLocksIdle,
|
|
329
|
+
fileLocksHeld,
|
|
330
|
+
tryLease,
|
|
331
|
+
withStateLock
|
|
332
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createRequire as __clikcodeCreateRequire } from 'node:module';
|
|
2
|
+
const require = __clikcodeCreateRequire(import.meta.url);
|
|
3
|
+
|
|
4
|
+
// src/agent/cancellation.ts
|
|
5
|
+
var TURN_CANCELLED_CODE = "ERR_TURN_CANCELLED";
|
|
6
|
+
function turnCancelledError() {
|
|
7
|
+
return Object.assign(new Error("Stopped"), { code: TURN_CANCELLED_CODE });
|
|
8
|
+
}
|
|
9
|
+
function isTurnCancelled(error) {
|
|
10
|
+
const candidate = error;
|
|
11
|
+
return candidate?.code === TURN_CANCELLED_CODE || candidate?.name === "AbortError";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
turnCancelledError,
|
|
16
|
+
isTurnCancelled
|
|
17
|
+
};
|