tenjin-cli 0.1.0-alpha.2 → 0.1.0-alpha.20
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/NOTICE.md +54 -7
- package/README.md +161 -257
- package/dist/{_esm-FEASBWRP.js → _esm-CE7KNONL.js} +9 -10
- package/dist/{ccip-NQXCEK2U.js → ccip-7RMRLAGV.js} +4 -4
- package/dist/{ccip-XR5NE5QK.js → ccip-NLCSCHKI.js} +5 -5
- package/dist/chunk-2E6GVIRP.js +888 -0
- package/dist/chunk-2E6GVIRP.js.map +1 -0
- package/dist/chunk-2G2WLVHB.js +102 -0
- package/dist/chunk-2G2WLVHB.js.map +1 -0
- package/dist/chunk-2QYORQBF.js +55 -0
- package/dist/chunk-2QYORQBF.js.map +1 -0
- package/dist/chunk-3BS55ANY.js +245 -0
- package/dist/chunk-3BS55ANY.js.map +1 -0
- package/dist/chunk-3KKKB2AQ.js +40 -0
- package/dist/chunk-3KKKB2AQ.js.map +1 -0
- package/dist/chunk-4AS52HUG.js +165 -0
- package/dist/chunk-4AS52HUG.js.map +1 -0
- package/dist/chunk-4M5FBWT5.js +368 -0
- package/dist/chunk-4M5FBWT5.js.map +1 -0
- package/dist/chunk-5FYKPQNX.js +1784 -0
- package/dist/chunk-5FYKPQNX.js.map +1 -0
- package/dist/{chunk-ZMHYXMO6.js → chunk-5IEP5EGE.js} +3 -3
- package/dist/chunk-5KD5HRAJ.js +241 -0
- package/dist/chunk-5KD5HRAJ.js.map +1 -0
- package/dist/chunk-72UHZRG6.js +231 -0
- package/dist/chunk-72UHZRG6.js.map +1 -0
- package/dist/chunk-7ACHDNJA.js +155 -0
- package/dist/chunk-7ACHDNJA.js.map +1 -0
- package/dist/chunk-7QKHZQKW.js +1680 -0
- package/dist/chunk-7QKHZQKW.js.map +1 -0
- package/dist/chunk-A36LQPHV.js +29 -0
- package/dist/chunk-A36LQPHV.js.map +1 -0
- package/dist/{chunk-UGX7YPX2.js → chunk-AWQPGIR6.js} +7 -9
- package/dist/{chunk-UGX7YPX2.js.map → chunk-AWQPGIR6.js.map} +1 -1
- package/dist/{chunk-5CLSD5YN.js → chunk-BPMWXTFX.js} +2 -2
- package/dist/{chunk-XJIW7USF.js → chunk-BTX2QE2Y.js} +115 -11
- package/dist/chunk-BTX2QE2Y.js.map +1 -0
- package/dist/{chunk-OYMYCW7J.js → chunk-CCDOV45D.js} +588 -97
- package/dist/chunk-CCDOV45D.js.map +1 -0
- package/dist/chunk-CXUYCDPX.js +127 -0
- package/dist/chunk-CXUYCDPX.js.map +1 -0
- package/dist/{chunk-YALGASEU.js → chunk-EGELTSPU.js} +2 -2
- package/dist/chunk-HH3PDYIG.js +194 -0
- package/dist/chunk-HH3PDYIG.js.map +1 -0
- package/dist/{chunk-CYOF7ROH.js → chunk-HPWO4O6S.js} +3 -3
- package/dist/chunk-JLEF5WHM.js +132 -0
- package/dist/chunk-JLEF5WHM.js.map +1 -0
- package/dist/{chunk-QVX5MWJZ.js → chunk-JRGFKVAZ.js} +2 -2
- package/dist/{chunk-HIVZG55D.js → chunk-KG3D7FIF.js} +2 -2
- package/dist/chunk-LHHMV4GU.js +27 -0
- package/dist/chunk-LHHMV4GU.js.map +1 -0
- package/dist/chunk-LI44XLMB.js +242 -0
- package/dist/chunk-LI44XLMB.js.map +1 -0
- package/dist/chunk-LN2HISWU.js +172 -0
- package/dist/chunk-LN2HISWU.js.map +1 -0
- package/dist/{chunk-YBMMV4Y4.js → chunk-MEFB5KHA.js} +13 -5
- package/dist/chunk-MEFB5KHA.js.map +1 -0
- package/dist/{chunk-NCE5NSN6.js → chunk-N35E6CM2.js} +140 -10
- package/dist/chunk-N35E6CM2.js.map +1 -0
- package/dist/chunk-NSG5TVZQ.js +447 -0
- package/dist/chunk-NSG5TVZQ.js.map +1 -0
- package/dist/chunk-O2JYBOUO.js +47 -0
- package/dist/chunk-O2JYBOUO.js.map +1 -0
- package/dist/{chunk-N62ROTMC.js → chunk-P42QX3QF.js} +14 -10
- package/dist/chunk-P42QX3QF.js.map +1 -0
- package/dist/chunk-PO4HAXAW.js +10528 -0
- package/dist/chunk-PO4HAXAW.js.map +1 -0
- package/dist/{chunk-ZABPQJ3M.js → chunk-POUWHWID.js} +2 -2
- package/dist/{chunk-S3IGX57D.js → chunk-RGVOODF4.js} +7703 -7475
- package/dist/chunk-RGVOODF4.js.map +1 -0
- package/dist/chunk-RPR2C2JO.js +112 -0
- package/dist/chunk-RPR2C2JO.js.map +1 -0
- package/dist/chunk-S2NRVRPF.js +171 -0
- package/dist/chunk-S2NRVRPF.js.map +1 -0
- package/dist/chunk-TGLKKLQG.js +11 -0
- package/dist/chunk-TGLKKLQG.js.map +1 -0
- package/dist/{chunk-RRN3YTK7.js → chunk-TTNESUPQ.js} +3 -3
- package/dist/chunk-TU7XH3JJ.js +97 -0
- package/dist/chunk-TU7XH3JJ.js.map +1 -0
- package/dist/{chunk-HSDCI7OV.js → chunk-XZKD7DIF.js} +3 -3
- package/dist/{chunk-Z6MFJV5Z.js → chunk-YEBNE5NW.js} +851 -500
- package/dist/chunk-YEBNE5NW.js.map +1 -0
- package/dist/chunk-YREKMJPP.js +131 -0
- package/dist/chunk-YREKMJPP.js.map +1 -0
- package/dist/{cli-4NNBEQYH.js → cli-XJQYUOHI.js} +355 -217
- package/dist/cli-XJQYUOHI.js.map +1 -0
- package/dist/config-KMJLFPSI.js +39 -0
- package/dist/dist-3LOO6M4U.js +2842 -0
- package/dist/dist-3LOO6M4U.js.map +1 -0
- package/dist/doctor-KMN55D7U.js +444 -0
- package/dist/doctor-KMN55D7U.js.map +1 -0
- package/dist/{ethersCompat-Df4a6QIZ-3YZIAXDK.js → ethersCompat-Df4a6QIZ-THAVUGIJ.js} +5 -5
- package/dist/fund-LT6ZPGEQ.js +298 -0
- package/dist/fund-LT6ZPGEQ.js.map +1 -0
- package/dist/hook-command-U5XPEHAJ.js +1052 -0
- package/dist/hook-command-U5XPEHAJ.js.map +1 -0
- package/dist/index.js +11 -3
- package/dist/index.js.map +1 -1
- package/dist/install-MMNXJ5LM.js +61 -0
- package/dist/install-identity-B7FVIWML.js +27 -0
- package/dist/install-wallet-XMVUBCHS.js +107 -0
- package/dist/install-wallet-XMVUBCHS.js.map +1 -0
- package/dist/mcp-TUPRIPSI.js +10000 -0
- package/dist/mcp-TUPRIPSI.js.map +1 -0
- package/dist/paths-LRMWNTJP.js +51 -0
- package/dist/pay-XAWW3NV3.js +44 -0
- package/dist/redact-6W23GGJC.js +15 -0
- package/dist/{secp256k1-N3ZHU7LB.js → secp256k1-ZZZT2CAX.js} +3 -3
- package/dist/secp256k1-ZZZT2CAX.js.map +1 -0
- package/dist/send-HESQYRKM.js +225 -0
- package/dist/send-HESQYRKM.js.map +1 -0
- package/dist/status-ANZPRZJJ.js +71 -0
- package/dist/status-ANZPRZJJ.js.map +1 -0
- package/dist/status-line-J3TUWVRQ.js +87 -0
- package/dist/status-line-J3TUWVRQ.js.map +1 -0
- package/dist/status-line-wiring-HSV7DXO5.js +29 -0
- package/dist/status-line-wiring-HSV7DXO5.js.map +1 -0
- package/dist/tenjin-daemon.mjs +20547 -0
- package/dist/tenjin-shim.mjs +251 -0
- package/dist/tenjin-vitest-reporter.mjs +49 -0
- package/dist/uninstall-GUU34YKP.js +165 -0
- package/dist/uninstall-GUU34YKP.js.map +1 -0
- package/dist/update-KO2NZRJ5.js +433 -0
- package/dist/update-KO2NZRJ5.js.map +1 -0
- package/dist/{viemAdapter-By13KInL-ZZE6UV4Y.js → viemAdapter-By13KInL-X2VY6CY3.js} +5 -5
- package/dist/viemAdapter-By13KInL-X2VY6CY3.js.map +1 -0
- package/dist/wallet-3URC5QPK.js +33 -0
- package/dist/wallet-3URC5QPK.js.map +1 -0
- package/dist/wallet-kdf-worker.mjs +980 -0
- package/docs/agent-permissions.md +139 -0
- package/docs/safety-model.md +70 -0
- package/package.json +14 -9
- package/LICENSE +0 -21
- package/dist/buy-U35DQ6XM.js +0 -1598
- package/dist/buy-U35DQ6XM.js.map +0 -1
- package/dist/candidate-RLYCQMF5.js +0 -127
- package/dist/candidate-RLYCQMF5.js.map +0 -1
- package/dist/chunk-27ONARHV.js +0 -171
- package/dist/chunk-27ONARHV.js.map +0 -1
- package/dist/chunk-3OV7E5G5.js +0 -18
- package/dist/chunk-3OV7E5G5.js.map +0 -1
- package/dist/chunk-7KUW4MJD.js +0 -59
- package/dist/chunk-7KUW4MJD.js.map +0 -1
- package/dist/chunk-7PHSZKQS.js +0 -333
- package/dist/chunk-7PHSZKQS.js.map +0 -1
- package/dist/chunk-IYLHQMT5.js +0 -211
- package/dist/chunk-IYLHQMT5.js.map +0 -1
- package/dist/chunk-JPHG2JW3.js +0 -96
- package/dist/chunk-JPHG2JW3.js.map +0 -1
- package/dist/chunk-MUBUQVC3.js +0 -67
- package/dist/chunk-MUBUQVC3.js.map +0 -1
- package/dist/chunk-N62ROTMC.js.map +0 -1
- package/dist/chunk-NCE5NSN6.js.map +0 -1
- package/dist/chunk-OYMYCW7J.js.map +0 -1
- package/dist/chunk-QNP7H47L.js +0 -152
- package/dist/chunk-QNP7H47L.js.map +0 -1
- package/dist/chunk-RY4XKNV3.js +0 -140
- package/dist/chunk-RY4XKNV3.js.map +0 -1
- package/dist/chunk-S3IGX57D.js.map +0 -1
- package/dist/chunk-TFGSOPWR.js +0 -261
- package/dist/chunk-TFGSOPWR.js.map +0 -1
- package/dist/chunk-TLCMMYLY.js +0 -126
- package/dist/chunk-TLCMMYLY.js.map +0 -1
- package/dist/chunk-UO22YNY4.js +0 -69
- package/dist/chunk-UO22YNY4.js.map +0 -1
- package/dist/chunk-UPIEAMGH.js +0 -160
- package/dist/chunk-UPIEAMGH.js.map +0 -1
- package/dist/chunk-XJIW7USF.js.map +0 -1
- package/dist/chunk-YBMMV4Y4.js.map +0 -1
- package/dist/chunk-YIYQ2ZNT.js +0 -286
- package/dist/chunk-YIYQ2ZNT.js.map +0 -1
- package/dist/chunk-YN5SHIXQ.js +0 -126
- package/dist/chunk-YN5SHIXQ.js.map +0 -1
- package/dist/chunk-Z52RA3NC.js +0 -137
- package/dist/chunk-Z52RA3NC.js.map +0 -1
- package/dist/chunk-Z6MFJV5Z.js.map +0 -1
- package/dist/cli-4NNBEQYH.js.map +0 -1
- package/dist/config-2VSXWWHK.js +0 -21
- package/dist/doctor-JJPNIBYH.js +0 -20
- package/dist/inspect-JGYJWFDP.js +0 -96
- package/dist/inspect-JGYJWFDP.js.map +0 -1
- package/dist/install-ZQRMSRGW.js +0 -378
- package/dist/install-ZQRMSRGW.js.map +0 -1
- package/dist/lookup-WYVTOMVY.js +0 -113
- package/dist/lookup-WYVTOMVY.js.map +0 -1
- package/dist/outcome-HKKT6QIA.js +0 -66
- package/dist/outcome-HKKT6QIA.js.map +0 -1
- package/dist/publish-D76B3MBK.js +0 -1480
- package/dist/publish-D76B3MBK.js.map +0 -1
- package/dist/usdc-JIAJW4SV.js +0 -22
- package/dist/wallet-6QKS6UHW.js +0 -179
- package/dist/wallet-6QKS6UHW.js.map +0 -1
- package/dist/wallet-PA54CRBL.js +0 -30
- package/skills/tenjin/SKILL.md +0 -199
- package/skills/tenjin-publish/SKILL.md +0 -96
- package/skills/tenjin-search/SKILL.md +0 -122
- /package/dist/{_esm-FEASBWRP.js.map → _esm-CE7KNONL.js.map} +0 -0
- /package/dist/{ccip-NQXCEK2U.js.map → ccip-7RMRLAGV.js.map} +0 -0
- /package/dist/{ccip-XR5NE5QK.js.map → ccip-NLCSCHKI.js.map} +0 -0
- /package/dist/{chunk-ZMHYXMO6.js.map → chunk-5IEP5EGE.js.map} +0 -0
- /package/dist/{chunk-5CLSD5YN.js.map → chunk-BPMWXTFX.js.map} +0 -0
- /package/dist/{chunk-YALGASEU.js.map → chunk-EGELTSPU.js.map} +0 -0
- /package/dist/{chunk-CYOF7ROH.js.map → chunk-HPWO4O6S.js.map} +0 -0
- /package/dist/{chunk-QVX5MWJZ.js.map → chunk-JRGFKVAZ.js.map} +0 -0
- /package/dist/{chunk-HIVZG55D.js.map → chunk-KG3D7FIF.js.map} +0 -0
- /package/dist/{chunk-ZABPQJ3M.js.map → chunk-POUWHWID.js.map} +0 -0
- /package/dist/{chunk-RRN3YTK7.js.map → chunk-TTNESUPQ.js.map} +0 -0
- /package/dist/{chunk-HSDCI7OV.js.map → chunk-XZKD7DIF.js.map} +0 -0
- /package/dist/{config-2VSXWWHK.js.map → config-KMJLFPSI.js.map} +0 -0
- /package/dist/{doctor-JJPNIBYH.js.map → ethersCompat-Df4a6QIZ-THAVUGIJ.js.map} +0 -0
- /package/dist/{ethersCompat-Df4a6QIZ-3YZIAXDK.js.map → install-MMNXJ5LM.js.map} +0 -0
- /package/dist/{secp256k1-N3ZHU7LB.js.map → install-identity-B7FVIWML.js.map} +0 -0
- /package/dist/{usdc-JIAJW4SV.js.map → paths-LRMWNTJP.js.map} +0 -0
- /package/dist/{viemAdapter-By13KInL-ZZE6UV4Y.js.map → pay-XAWW3NV3.js.map} +0 -0
- /package/dist/{wallet-PA54CRBL.js.map → redact-6W23GGJC.js.map} +0 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { createRequire as __tenjinCreateRequire } from 'node:module'; const require = __tenjinCreateRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
hasCode
|
|
4
|
+
} from "./chunk-2I4QTOLB.js";
|
|
5
|
+
import {
|
|
6
|
+
external_exports
|
|
7
|
+
} from "./chunk-CCDOV45D.js";
|
|
8
|
+
import {
|
|
9
|
+
spendLedgerPath
|
|
10
|
+
} from "./chunk-TU7XH3JJ.js";
|
|
11
|
+
|
|
12
|
+
// src/lib/spend-ledger.ts
|
|
13
|
+
import { readFile } from "node:fs/promises";
|
|
14
|
+
var DEFAULT_WINDOW_MS = 864e5;
|
|
15
|
+
var RESERVATION_TTL_MS = 6e5;
|
|
16
|
+
var ReservationSchema = external_exports.object({
|
|
17
|
+
id: external_exports.string(),
|
|
18
|
+
amountAtomic: external_exports.string().regex(/^\d+$/),
|
|
19
|
+
atMs: external_exports.number(),
|
|
20
|
+
/** Optional so a ledger written by an older build still parses. */
|
|
21
|
+
requestKey: external_exports.string().optional(),
|
|
22
|
+
mode: external_exports.enum(["automatic", "manual"]).optional()
|
|
23
|
+
});
|
|
24
|
+
var LedgerSchema = external_exports.object({
|
|
25
|
+
schemaVersion: external_exports.literal(2),
|
|
26
|
+
windowStartMs: external_exports.number(),
|
|
27
|
+
/**
|
|
28
|
+
* EXPOSURE: every authorization this window transmitted. A signed EIP-3009
|
|
29
|
+
* authorization is a bearer instrument, so total exposure stays recorded,
|
|
30
|
+
* whatever any counterparty later says it took.
|
|
31
|
+
*/
|
|
32
|
+
committedAtomic: external_exports.string().regex(/^\d+$/),
|
|
33
|
+
automaticCommittedAtomic: external_exports.string().regex(/^\d+$/).optional(),
|
|
34
|
+
/**
|
|
35
|
+
* SETTLED: what counterparties reported actually taking, which the router's
|
|
36
|
+
* waived outcomes made a different number from the line above (2026-09-23
|
|
37
|
+
* lookup contract). Optional so a ledger an older build wrote still parses;
|
|
38
|
+
* absent means "everything committed was settled", which is what that build
|
|
39
|
+
* assumed. Reporting only, never a budget input: under-counting exposure
|
|
40
|
+
* because a server said "no charge" is exactly the hole this avoids.
|
|
41
|
+
*/
|
|
42
|
+
settledAtomic: external_exports.string().regex(/^\d+$/).optional(),
|
|
43
|
+
reservations: external_exports.array(ReservationSchema)
|
|
44
|
+
});
|
|
45
|
+
function emptyLedger(nowMs) {
|
|
46
|
+
return {
|
|
47
|
+
schemaVersion: 2,
|
|
48
|
+
windowStartMs: nowMs,
|
|
49
|
+
committedAtomic: "0",
|
|
50
|
+
automaticCommittedAtomic: "0",
|
|
51
|
+
settledAtomic: "0",
|
|
52
|
+
reservations: []
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function spentOf(ledger) {
|
|
56
|
+
return ledger.reservations.reduce(
|
|
57
|
+
(sum, r) => sum + (r.mode === "manual" ? 0n : BigInt(r.amountAtomic)),
|
|
58
|
+
BigInt(ledger.automaticCommittedAtomic ?? ledger.committedAtomic)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
async function readLedger(path) {
|
|
62
|
+
let raw;
|
|
63
|
+
try {
|
|
64
|
+
raw = await readFile(path, "utf8");
|
|
65
|
+
} catch (err) {
|
|
66
|
+
if (hasCode(err, "ENOENT")) return { ledger: null };
|
|
67
|
+
return { ledger: null, corrupt: err instanceof Error ? err.message : String(err) };
|
|
68
|
+
}
|
|
69
|
+
let json;
|
|
70
|
+
try {
|
|
71
|
+
json = JSON.parse(raw);
|
|
72
|
+
} catch {
|
|
73
|
+
return { ledger: null, corrupt: "not valid JSON" };
|
|
74
|
+
}
|
|
75
|
+
const parsed = LedgerSchema.safeParse(json);
|
|
76
|
+
if (parsed.success) return { ledger: parsed.data };
|
|
77
|
+
const issue = parsed.error.issues[0];
|
|
78
|
+
const field = issue?.path.join(".");
|
|
79
|
+
const message = issue?.message ?? "schema mismatch";
|
|
80
|
+
return {
|
|
81
|
+
ledger: null,
|
|
82
|
+
corrupt: field !== void 0 && field.length > 0 ? `${field}: ${message}` : message
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
async function readSpendSummary(dir, opts = {}) {
|
|
86
|
+
const { ledger } = await readLedger(spendLedgerPath(dir));
|
|
87
|
+
if (ledger === null) return null;
|
|
88
|
+
const nowMs = (opts.now ?? Date.now)();
|
|
89
|
+
const windowMs = opts.windowMs ?? DEFAULT_WINDOW_MS;
|
|
90
|
+
if (nowMs - ledger.windowStartMs >= windowMs) return emptyLedger(nowMs);
|
|
91
|
+
return {
|
|
92
|
+
...ledger,
|
|
93
|
+
reservations: ledger.reservations.filter((r) => nowMs - r.atMs < RESERVATION_TTL_MS)
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/lib/policy.ts
|
|
98
|
+
function normCreator(value) {
|
|
99
|
+
return value.trim().toLowerCase();
|
|
100
|
+
}
|
|
101
|
+
function evaluateSpendPolicy(policy, req) {
|
|
102
|
+
if (req.maxPriceAtomic !== void 0 && req.amountAtomic > req.maxPriceAtomic) {
|
|
103
|
+
return {
|
|
104
|
+
decision: "deny",
|
|
105
|
+
reason: "price_cap_exceeded",
|
|
106
|
+
message: `Price ${req.amountAtomic} exceeds the --max-price cap ${req.maxPriceAtomic}.`
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (policy.allowlistCreators.length > 0) {
|
|
110
|
+
const allowed = policy.allowlistCreators.map(normCreator);
|
|
111
|
+
if (!allowed.includes(normCreator(req.creator))) {
|
|
112
|
+
return {
|
|
113
|
+
decision: "deny",
|
|
114
|
+
reason: "not_allowlisted",
|
|
115
|
+
message: `Creator "${req.creator}" is not in allowlistCreators.`
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (req.mode === "manual") {
|
|
120
|
+
return req.amountAtomic > 0n ? {
|
|
121
|
+
decision: "confirm",
|
|
122
|
+
reason: "confirm_always",
|
|
123
|
+
message: "Manual payment requires explicit consent."
|
|
124
|
+
} : { decision: "allow", reason: "within_policy", message: "No payment required." };
|
|
125
|
+
}
|
|
126
|
+
if (policy.sessionBudgetAtomic !== null) {
|
|
127
|
+
const projected = req.sessionSpentAtomic + req.amountAtomic;
|
|
128
|
+
if (projected > policy.sessionBudgetAtomic) {
|
|
129
|
+
return {
|
|
130
|
+
decision: "deny",
|
|
131
|
+
reason: "session_budget_exceeded",
|
|
132
|
+
message: `This spend would bring the session total to ${projected}, over sessionBudget ${policy.sessionBudgetAtomic}.`
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (req.amountAtomic > policy.maxAutoSpendAtomic) {
|
|
137
|
+
return {
|
|
138
|
+
decision: "deny",
|
|
139
|
+
reason: "above_auto_spend",
|
|
140
|
+
message: `Price ${req.amountAtomic} is above maxAutoSpend ${policy.maxAutoSpendAtomic}; automatic payment refused.`
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return { decision: "allow", reason: "within_policy", message: "within spend policy" };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export {
|
|
147
|
+
DEFAULT_WINDOW_MS,
|
|
148
|
+
RESERVATION_TTL_MS,
|
|
149
|
+
emptyLedger,
|
|
150
|
+
spentOf,
|
|
151
|
+
readLedger,
|
|
152
|
+
readSpendSummary,
|
|
153
|
+
evaluateSpendPolicy
|
|
154
|
+
};
|
|
155
|
+
//# sourceMappingURL=chunk-7ACHDNJA.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/lib/spend-ledger.ts","../src/lib/policy.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport { z } from 'zod';\nimport { hasCode } from './errno';\nimport { spendLedgerPath } from './paths';\n\n/**\n * The spend ledger's READ side: its shape, the two expiries, and a read-only\n * summary. It lives outside `lib/wallet` so the router hooks can measure an\n * offer against the same session total an authorization would, without the\n * wallet entering their chunk graph (`src/router/dist-chunks.test.ts`). The\n * write side, with its lock and reservations, stays in `lib/wallet/spend.ts`.\n */\n\nexport const DEFAULT_WINDOW_MS = 86_400_000; // 24h rolling day\nexport const RESERVATION_TTL_MS = 600_000; // 10 min: a dangling reservation self-expires\n\nconst ReservationSchema = z.object({\n id: z.string(),\n amountAtomic: z.string().regex(/^\\d+$/),\n atMs: z.number(),\n /** Optional so a ledger written by an older build still parses. */\n requestKey: z.string().optional(),\n mode: z.enum(['automatic', 'manual']).optional(),\n});\nexport type Reservation = z.infer<typeof ReservationSchema>;\n\nconst LedgerSchema = z.object({\n schemaVersion: z.literal(2),\n windowStartMs: z.number(),\n /**\n * EXPOSURE: every authorization this window transmitted. A signed EIP-3009\n * authorization is a bearer instrument, so total exposure stays recorded,\n * whatever any counterparty later says it took.\n */\n committedAtomic: z.string().regex(/^\\d+$/),\n automaticCommittedAtomic: z.string().regex(/^\\d+$/).optional(),\n /**\n * SETTLED: what counterparties reported actually taking, which the router's\n * waived outcomes made a different number from the line above (2026-09-23\n * lookup contract). Optional so a ledger an older build wrote still parses;\n * absent means \"everything committed was settled\", which is what that build\n * assumed. Reporting only, never a budget input: under-counting exposure\n * because a server said \"no charge\" is exactly the hole this avoids.\n */\n settledAtomic: z.string().regex(/^\\d+$/).optional(),\n reservations: z.array(ReservationSchema),\n});\nexport type Ledger = z.infer<typeof LedgerSchema>;\n\nexport function emptyLedger(nowMs: number): Ledger {\n return {\n schemaVersion: 2,\n windowStartMs: nowMs,\n committedAtomic: '0',\n automaticCommittedAtomic: '0',\n settledAtomic: '0',\n reservations: [],\n };\n}\n\n/** Automatic exposure plus automatic pending reservations: the budget's input.\n * Legacy records with no mode/counter conservatively count as automatic. */\nexport function spentOf(ledger: {\n committedAtomic: string;\n automaticCommittedAtomic?: string;\n reservations: { amountAtomic: string; mode?: 'automatic' | 'manual' }[];\n}): bigint {\n return ledger.reservations.reduce(\n (sum, r) => sum + (r.mode === 'manual' ? 0n : BigInt(r.amountAtomic)),\n BigInt(ledger.automaticCommittedAtomic ?? ledger.committedAtomic),\n );\n}\n\n/**\n * ABSENT and CORRUPT both reset the window, but they are not the same fact: the\n * first is a first run, the second is spend that existed and is now gone. Only\n * the second is worth telling anyone about, so the two stay distinguishable here\n * rather than collapsing into one null.\n */\ninterface LedgerRead {\n ledger: Ledger | null;\n /** Set only when the file EXISTS and could not be turned back into a ledger. */\n corrupt?: string;\n}\n\nexport async function readLedger(path: string): Promise<LedgerRead> {\n let raw: string;\n try {\n raw = await readFile(path, 'utf8');\n } catch (err) {\n // A missing file is the ordinary first run. Any other read failure means a\n // file is there and unusable, which is the corrupt case by another name.\n if (hasCode(err, 'ENOENT')) return { ledger: null };\n return { ledger: null, corrupt: err instanceof Error ? err.message : String(err) };\n }\n let json: unknown;\n try {\n json = JSON.parse(raw);\n } catch {\n return { ledger: null, corrupt: 'not valid JSON' };\n }\n const parsed = LedgerSchema.safeParse(json);\n if (parsed.success) return { ledger: parsed.data };\n const issue = parsed.error.issues[0];\n const field = issue?.path.join('.');\n const message = issue?.message ?? 'schema mismatch';\n // Field-qualified: \"expected number, received undefined\" names nothing on its\n // own. zod never echoes the received VALUE, so no spend figure rides along.\n return {\n ledger: null,\n corrupt: field !== undefined && field.length > 0 ? `${field}: ${message}` : message,\n };\n}\n\nexport interface SpendSummary {\n windowStartMs: number;\n committedAtomic: string;\n automaticCommittedAtomic?: string;\n reservations: {\n amountAtomic: string;\n atMs: number;\n requestKey?: string;\n mode?: 'automatic' | 'manual';\n }[];\n}\n\n/**\n * The ledger AS AN AUTHORIZATION WOULD SEE IT, for `tenjin status`. Read-only,\n * and it applies the same two expiries the authorizer applies before it\n * evaluates a spend: a rolling window that has run out reads as a fresh one,\n * and reservations past their TTL are gone. Summing the raw file instead\n * reported spend as current, and crashed reservations as open, until the next\n * payment happened to rewrite it.\n *\n * `null` for absent or unreadable, which is the same thing to a report.\n */\nexport async function readSpendSummary(\n dir: string,\n opts: { now?: () => number; windowMs?: number } = {},\n): Promise<SpendSummary | null> {\n const { ledger } = await readLedger(spendLedgerPath(dir));\n if (ledger === null) return null;\n const nowMs = (opts.now ?? Date.now)();\n const windowMs = opts.windowMs ?? DEFAULT_WINDOW_MS;\n if (nowMs - ledger.windowStartMs >= windowMs) return emptyLedger(nowMs);\n return {\n ...ledger,\n reservations: ledger.reservations.filter((r) => nowMs - r.atMs < RESERVATION_TTL_MS),\n };\n}\n","/**\n * Spend-policy evaluation, pure and provider-agnostic. The wallet provider layer\n * (lib/wallet/spend.ts) owns the ledger and calls this; a future hosted provider\n * (Privy, B5) reuses the same decision rules, so the knobs live in exactly one\n * place. Nothing here does IO, the caller supplies the already-spent total and\n * decides how to satisfy a `confirm` decision (a `--yes`, a TTY prompt, or a\n * non-interactive refusal).\n */\n\nexport type PaymentMode = 'automatic' | 'manual';\n\nexport interface SpendPolicy {\n /** Automatic per-call ceiling; zero blocks positive automatic payments. */\n maxAutoSpendAtomic: bigint;\n /** Rolling ceiling on automatic exposure. null = no ceiling; zero blocks positive automatic payments. */\n sessionBudgetAtomic: bigint | null;\n /** Creators (handle or 0x-address, lowercased) all payments are restricted to.\n * Empty = no restriction. A non-empty list is a hard gate: a non-member is\n * denied even with `--yes`. */\n allowlistCreators: string[];\n}\n\nexport interface SpendRequest {\n /** Omitted by legacy consumers: conservatively automatic. */\n mode?: PaymentMode;\n amountAtomic: bigint;\n /** Creator identity from the 402 preview / candidate, handle or 0x address. */\n creator: string;\n /** The caller's `--max-price` cap, if given. A hard ceiling, never bypassable. */\n maxPriceAtomic?: bigint;\n /** Cumulative spend already recorded in this rolling session window. */\n sessionSpentAtomic: bigint;\n}\n\n/**\n * `allow`: within policy, proceed silently. `confirm`: an approval is required ,\n * the caller satisfies it with `--yes`, an interactive prompt, or refuses. `deny`:\n * a hard gate blocked it (price cap, allowlist, session budget); NOT satisfiable\n * by `--yes` or a prompt.\n */\nexport type SpendDecision = 'allow' | 'confirm' | 'deny';\n\n/**\n * `duplicate_in_flight` is the one reason this pure evaluator never returns: it\n * is a fact about the ledger (an identical request already holds a reservation\n * in this turn), so the authorizer raises it. It lives in the union because\n * every caller renders a decision's reason from one vocabulary.\n */\nexport type PolicyReason =\n | 'within_policy'\n | 'duplicate_in_flight'\n | 'price_cap_exceeded'\n | 'not_allowlisted'\n | 'session_budget_exceeded'\n | 'above_auto_spend'\n | 'confirm_always';\n\nexport interface PolicyEvaluation {\n decision: SpendDecision;\n reason: PolicyReason;\n message: string;\n}\n\n/** Normalize a creator identity for allowlist comparison (case-insensitive; a\n * word-handle and a 0x address are both single tokens). */\nfunction normCreator(value: string): string {\n return value.trim().toLowerCase();\n}\n\n/** Hard price/creator checks apply in both modes. Manual payments require consent;\n * automatic payments must fit both configured ceilings without prompting. */\nexport function evaluateSpendPolicy(policy: SpendPolicy, req: SpendRequest): PolicyEvaluation {\n if (req.maxPriceAtomic !== undefined && req.amountAtomic > req.maxPriceAtomic) {\n return {\n decision: 'deny',\n reason: 'price_cap_exceeded',\n message: `Price ${req.amountAtomic} exceeds the --max-price cap ${req.maxPriceAtomic}.`,\n };\n }\n\n if (policy.allowlistCreators.length > 0) {\n const allowed = policy.allowlistCreators.map(normCreator);\n if (!allowed.includes(normCreator(req.creator))) {\n return {\n decision: 'deny',\n reason: 'not_allowlisted',\n message: `Creator \"${req.creator}\" is not in allowlistCreators.`,\n };\n }\n }\n\n if (req.mode === 'manual') {\n return req.amountAtomic > 0n\n ? {\n decision: 'confirm',\n reason: 'confirm_always',\n message: 'Manual payment requires explicit consent.',\n }\n : { decision: 'allow', reason: 'within_policy', message: 'No payment required.' };\n }\n\n if (policy.sessionBudgetAtomic !== null) {\n const projected = req.sessionSpentAtomic + req.amountAtomic;\n if (projected > policy.sessionBudgetAtomic) {\n return {\n decision: 'deny',\n reason: 'session_budget_exceeded',\n message: `This spend would bring the session total to ${projected}, over sessionBudget ${policy.sessionBudgetAtomic}.`,\n };\n }\n }\n\n if (req.amountAtomic > policy.maxAutoSpendAtomic) {\n return {\n decision: 'deny',\n reason: 'above_auto_spend',\n message: `Price ${req.amountAtomic} is above maxAutoSpend ${policy.maxAutoSpendAtomic}; automatic payment refused.`,\n };\n }\n\n return { decision: 'allow', reason: 'within_policy', message: 'within spend policy' };\n}\n"],"mappings":";;;;;;;;;;;;AAAA,SAAS,gBAAgB;AAalB,IAAM,oBAAoB;AAC1B,IAAM,qBAAqB;AAElC,IAAM,oBAAoB,iBAAE,OAAO;AAAA,EACjC,IAAI,iBAAE,OAAO;AAAA,EACb,cAAc,iBAAE,OAAO,EAAE,MAAM,OAAO;AAAA,EACtC,MAAM,iBAAE,OAAO;AAAA;AAAA,EAEf,YAAY,iBAAE,OAAO,EAAE,SAAS;AAAA,EAChC,MAAM,iBAAE,KAAK,CAAC,aAAa,QAAQ,CAAC,EAAE,SAAS;AACjD,CAAC;AAGD,IAAM,eAAe,iBAAE,OAAO;AAAA,EAC5B,eAAe,iBAAE,QAAQ,CAAC;AAAA,EAC1B,eAAe,iBAAE,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMxB,iBAAiB,iBAAE,OAAO,E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