tenjin-cli 0.1.0-alpha.13 → 0.1.0-alpha.15
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 +51 -3
- package/dist/buy-VIRNSYGN.js +47 -0
- package/dist/{chunk-OC3EIDCP.js → chunk-2MYQN7HM.js} +71 -26
- package/dist/chunk-2MYQN7HM.js.map +1 -0
- package/dist/{chunk-XXY77EZJ.js → chunk-2RF2GIJQ.js} +57 -226
- package/dist/chunk-2RF2GIJQ.js.map +1 -0
- package/dist/chunk-3GL2Z5QK.js +57 -0
- package/dist/chunk-3GL2Z5QK.js.map +1 -0
- package/dist/{chunk-OXUJ3FXU.js → chunk-4CQIDQOT.js} +41 -8
- package/dist/chunk-4CQIDQOT.js.map +1 -0
- package/dist/{chunk-YMUJZWHB.js → chunk-4KG27W2S.js} +10 -10
- package/dist/chunk-4KG27W2S.js.map +1 -0
- package/dist/{chunk-SPUORKD7.js → chunk-5QQTYWEU.js} +13 -13
- package/dist/chunk-5QQTYWEU.js.map +1 -0
- package/dist/chunk-65DTCCJ7.js +21 -0
- package/dist/chunk-65DTCCJ7.js.map +1 -0
- package/dist/{chunk-BRAVLUGI.js → chunk-A5DKHMUN.js} +28 -13
- package/dist/chunk-A5DKHMUN.js.map +1 -0
- package/dist/chunk-B63XPWQ4.js +246 -0
- package/dist/chunk-B63XPWQ4.js.map +1 -0
- package/dist/{chunk-47YKYDOF.js → chunk-CAR6FHK6.js} +5 -5
- package/dist/{chunk-JVBMPU5G.js → chunk-CPCX67TI.js} +22 -22
- package/dist/{chunk-TXVC46SA.js → chunk-CQBVCYI2.js} +40 -53
- package/dist/chunk-CQBVCYI2.js.map +1 -0
- package/dist/chunk-DBU5CXBV.js +4731 -0
- package/dist/chunk-DBU5CXBV.js.map +1 -0
- package/dist/chunk-DHJD77OW.js +81 -0
- package/dist/chunk-DHJD77OW.js.map +1 -0
- package/dist/{chunk-XGZ5EVMB.js → chunk-EDZSNPE5.js} +2 -2
- package/dist/{chunk-736DW4V3.js → chunk-EFWZPUTC.js} +2 -2
- package/dist/chunk-EPFL6CTE.js +448 -0
- package/dist/chunk-EPFL6CTE.js.map +1 -0
- package/dist/{chunk-3P3ES4XU.js → chunk-GHLVXVZD.js} +3 -5
- package/dist/{chunk-3P3ES4XU.js.map → chunk-GHLVXVZD.js.map} +1 -1
- package/dist/{chunk-UUARTUY2.js → chunk-H3SOOJ2I.js} +264 -33
- package/dist/chunk-H3SOOJ2I.js.map +1 -0
- package/dist/{chunk-OUJDDY5L.js → chunk-HPJGOLTN.js} +32 -32
- package/dist/chunk-HPJGOLTN.js.map +1 -0
- package/dist/{chunk-J6FUAIRR.js → chunk-HRGBS2PA.js} +4 -6
- package/dist/{chunk-J6FUAIRR.js.map → chunk-HRGBS2PA.js.map} +1 -1
- package/dist/{chunk-NKN4YC5C.js → chunk-ITUXPJIS.js} +55 -58
- package/dist/chunk-ITUXPJIS.js.map +1 -0
- package/dist/{chunk-CA3JV7FX.js → chunk-JBIKAAT3.js} +2 -2
- package/dist/{chunk-CA3JV7FX.js.map → chunk-JBIKAAT3.js.map} +1 -1
- package/dist/{chunk-6JKD46OJ.js → chunk-KAJZHIQ4.js} +13 -10
- package/dist/chunk-KAJZHIQ4.js.map +1 -0
- package/dist/{chunk-OWVVI4VQ.js → chunk-KINZ23CV.js} +107 -4535
- package/dist/chunk-KINZ23CV.js.map +1 -0
- package/dist/{chunk-WMFRCSNC.js → chunk-LRYV3SK7.js} +9 -7
- package/dist/{chunk-WMFRCSNC.js.map → chunk-LRYV3SK7.js.map} +1 -1
- package/dist/{chunk-T4UWMVKV.js → chunk-NIKXHWOL.js} +11 -13
- package/dist/{chunk-T4UWMVKV.js.map → chunk-NIKXHWOL.js.map} +1 -1
- package/dist/chunk-OKDRDT74.js +209 -0
- package/dist/chunk-OKDRDT74.js.map +1 -0
- package/dist/chunk-OKYSNAS3.js +2144 -0
- package/dist/chunk-OKYSNAS3.js.map +1 -0
- package/dist/{chunk-B676GPS2.js → chunk-OLWTRUTS.js} +7 -3
- package/dist/{chunk-B676GPS2.js.map → chunk-OLWTRUTS.js.map} +1 -1
- package/dist/{chunk-WU5CZPLD.js → chunk-QQ6ELLSA.js} +6 -8
- package/dist/{chunk-WU5CZPLD.js.map → chunk-QQ6ELLSA.js.map} +1 -1
- package/dist/{chunk-WVHPGQH5.js → chunk-QZ6ROAJU.js} +8 -4
- package/dist/chunk-QZ6ROAJU.js.map +1 -0
- package/dist/{chunk-L4XNT3UW.js → chunk-R7RSCBI7.js} +167 -52
- package/dist/chunk-R7RSCBI7.js.map +1 -0
- package/dist/{chunk-ELVDOTU7.js → chunk-U6VPKMGC.js} +128 -28
- package/dist/chunk-U6VPKMGC.js.map +1 -0
- package/dist/{chunk-FKSAUOJU.js → chunk-UO22YNY4.js} +1 -69
- package/dist/chunk-UO22YNY4.js.map +1 -0
- package/dist/{chunk-IAR25V2E.js → chunk-US456VUI.js} +6 -6
- package/dist/{chunk-IKAJSA7E.js → chunk-V44J2MXV.js} +80 -34
- package/dist/chunk-V44J2MXV.js.map +1 -0
- package/dist/{chunk-IFPKA32J.js → chunk-VJQKL3YH.js} +10 -7
- package/dist/{chunk-IFPKA32J.js.map → chunk-VJQKL3YH.js.map} +1 -1
- package/dist/{chunk-GCGMXA2O.js → chunk-VJSQSLBM.js} +52 -20
- package/dist/chunk-VJSQSLBM.js.map +1 -0
- package/dist/{chunk-IGHSLJUF.js → chunk-VYOJOX4Z.js} +9 -7
- package/dist/chunk-VYOJOX4Z.js.map +1 -0
- package/dist/chunk-WY2K35OL.js +269 -0
- package/dist/chunk-WY2K35OL.js.map +1 -0
- package/dist/{chunk-PAOVPSP6.js → chunk-ZFJJ3EXC.js} +53 -5
- package/dist/chunk-ZFJJ3EXC.js.map +1 -0
- package/dist/chunk-ZOBNS7CX.js +73 -0
- package/dist/chunk-ZOBNS7CX.js.map +1 -0
- package/dist/{cli-XK5JZWU6.js → cli-KKRKBU5T.js} +76 -47
- package/dist/{cli-XK5JZWU6.js.map → cli-KKRKBU5T.js.map} +1 -1
- package/dist/config-JMRUNYE6.js +37 -0
- package/dist/discover-B5HF6SHM.js +102 -0
- package/dist/discover-B5HF6SHM.js.map +1 -0
- package/dist/doctor-ZBG7X66D.js +32 -0
- package/dist/edit-PZJIILQE.js +40 -0
- package/dist/{fund-JF6A76YS.js → fund-EUDH5KPA.js} +19 -17
- package/dist/index.js +1 -1
- package/dist/inspect-QC6WEQPN.js +37 -0
- package/dist/{install-YQVM3SUW.js → install-GNK2W3PI.js} +286 -447
- package/dist/install-GNK2W3PI.js.map +1 -0
- package/dist/outcome-KSGWZHUS.js +24 -0
- package/dist/pay-BC4UICPA.js +361 -0
- package/dist/pay-BC4UICPA.js.map +1 -0
- package/dist/publish-LXWFDIKF.js +42 -0
- package/dist/{read-UF7QVQOU.js → read-JMNUQ56P.js} +22 -20
- package/dist/{read-UF7QVQOU.js.map → read-JMNUQ56P.js.map} +1 -1
- package/dist/{run-GDZWHNWZ.js → run-XYTQ5TWQ.js} +54 -45
- package/dist/{run-GDZWHNWZ.js.map → run-XYTQ5TWQ.js.map} +1 -1
- package/dist/search-TSQAB45E.js +25 -0
- package/dist/{send-SI254H3R.js → send-G746D7PQ.js} +14 -14
- package/dist/{session-GJZJEXLJ.js → session-JBKWMJ24.js} +18 -17
- package/dist/{session-GJZJEXLJ.js.map → session-JBKWMJ24.js.map} +1 -1
- package/dist/{skill-heal-BPFT22XS.js → skill-heal-WKD67SLJ.js} +17 -9
- package/dist/skill-heal-WKD67SLJ.js.map +1 -0
- package/dist/skill-placement-RIU4AMC2.js +20 -0
- package/dist/{uninstall-7ZZG6WQ5.js → uninstall-5E263VTS.js} +15 -9
- package/dist/uninstall-5E263VTS.js.map +1 -0
- package/dist/{update-FPSBITLB.js → update-R76LHOZW.js} +18 -16
- package/dist/update-R76LHOZW.js.map +1 -0
- package/dist/{wallet-NS7TYWNN.js → wallet-TQ7D5EQ7.js} +7 -7
- package/dist/{wallet-TZVD2M4I.js → wallet-ZAVXWLWP.js} +11 -11
- package/dist/wallet-ZAVXWLWP.js.map +1 -0
- package/docs/agent-permissions.md +117 -18
- package/docs/command-reference.md +102 -50
- package/docs/safety-model.md +9 -0
- package/package.json +1 -1
- package/skills/tenjin/SKILL.md +38 -29
- package/skills/tenjin-pay/SKILL.md +76 -0
- package/skills/tenjin-publish/SKILL.md +184 -205
- package/skills/tenjin-publish/references/maintain.md +39 -0
- package/skills/tenjin-search/SKILL.md +92 -280
- package/skills/tenjin-search/references/permissions.md +112 -0
- package/dist/buy-X4VPHFUA.js +0 -44
- package/dist/chunk-3NZCU6SD.js +0 -125
- package/dist/chunk-3NZCU6SD.js.map +0 -1
- package/dist/chunk-6JKD46OJ.js.map +0 -1
- package/dist/chunk-BRAVLUGI.js.map +0 -1
- package/dist/chunk-ELVDOTU7.js.map +0 -1
- package/dist/chunk-FKSAUOJU.js.map +0 -1
- package/dist/chunk-GCGMXA2O.js.map +0 -1
- package/dist/chunk-IGHSLJUF.js.map +0 -1
- package/dist/chunk-IKAJSA7E.js.map +0 -1
- package/dist/chunk-L4XNT3UW.js.map +0 -1
- package/dist/chunk-NKN4YC5C.js.map +0 -1
- package/dist/chunk-OC3EIDCP.js.map +0 -1
- package/dist/chunk-OE36HRWG.js +0 -939
- package/dist/chunk-OE36HRWG.js.map +0 -1
- package/dist/chunk-OUJDDY5L.js.map +0 -1
- package/dist/chunk-OWVVI4VQ.js.map +0 -1
- package/dist/chunk-OXUJ3FXU.js.map +0 -1
- package/dist/chunk-PAOVPSP6.js.map +0 -1
- package/dist/chunk-SPUORKD7.js.map +0 -1
- package/dist/chunk-TXVC46SA.js.map +0 -1
- package/dist/chunk-UUARTUY2.js.map +0 -1
- package/dist/chunk-WVHPGQH5.js.map +0 -1
- package/dist/chunk-XXY77EZJ.js.map +0 -1
- package/dist/chunk-YMUJZWHB.js.map +0 -1
- package/dist/chunk-ZQIQ55KK.js +0 -51
- package/dist/chunk-ZQIQ55KK.js.map +0 -1
- package/dist/config-CNK7MYUR.js +0 -26
- package/dist/doctor-R7N7FUX5.js +0 -27
- package/dist/edit-QSUEG7CL.js +0 -38
- package/dist/inspect-NFNAA4LA.js +0 -35
- package/dist/install-YQVM3SUW.js.map +0 -1
- package/dist/outcome-IUSYFJTN.js +0 -22
- package/dist/publish-O2I6IAEL.js +0 -40
- package/dist/search-JVOHEF3E.js +0 -23
- package/dist/skill-heal-BPFT22XS.js.map +0 -1
- package/dist/uninstall-7ZZG6WQ5.js.map +0 -1
- package/dist/update-FPSBITLB.js.map +0 -1
- /package/dist/{buy-X4VPHFUA.js.map → buy-VIRNSYGN.js.map} +0 -0
- /package/dist/{chunk-47YKYDOF.js.map → chunk-CAR6FHK6.js.map} +0 -0
- /package/dist/{chunk-JVBMPU5G.js.map → chunk-CPCX67TI.js.map} +0 -0
- /package/dist/{chunk-XGZ5EVMB.js.map → chunk-EDZSNPE5.js.map} +0 -0
- /package/dist/{chunk-736DW4V3.js.map → chunk-EFWZPUTC.js.map} +0 -0
- /package/dist/{chunk-IAR25V2E.js.map → chunk-US456VUI.js.map} +0 -0
- /package/dist/{config-CNK7MYUR.js.map → config-JMRUNYE6.js.map} +0 -0
- /package/dist/{doctor-R7N7FUX5.js.map → doctor-ZBG7X66D.js.map} +0 -0
- /package/dist/{edit-QSUEG7CL.js.map → edit-PZJIILQE.js.map} +0 -0
- /package/dist/{fund-JF6A76YS.js.map → fund-EUDH5KPA.js.map} +0 -0
- /package/dist/{inspect-NFNAA4LA.js.map → inspect-QC6WEQPN.js.map} +0 -0
- /package/dist/{outcome-IUSYFJTN.js.map → outcome-KSGWZHUS.js.map} +0 -0
- /package/dist/{publish-O2I6IAEL.js.map → publish-LXWFDIKF.js.map} +0 -0
- /package/dist/{search-JVOHEF3E.js.map → search-TSQAB45E.js.map} +0 -0
- /package/dist/{send-SI254H3R.js.map → send-G746D7PQ.js.map} +0 -0
- /package/dist/{wallet-NS7TYWNN.js.map → skill-placement-RIU4AMC2.js.map} +0 -0
- /package/dist/{wallet-TZVD2M4I.js.map → wallet-TQ7D5EQ7.js.map} +0 -0
package/dist/chunk-OE36HRWG.js
DELETED
|
@@ -1,939 +0,0 @@
|
|
|
1
|
-
import { createRequire as __tenjinCreateRequire } from 'node:module'; const require = __tenjinCreateRequire(import.meta.url);
|
|
2
|
-
import {
|
|
3
|
-
resolveThroughLink
|
|
4
|
-
} from "./chunk-WVHPGQH5.js";
|
|
5
|
-
import {
|
|
6
|
-
hooksDir
|
|
7
|
-
} from "./chunk-ZQIQ55KK.js";
|
|
8
|
-
import {
|
|
9
|
-
CLI_SKILL_NAMES,
|
|
10
|
-
HOSTED_SKILL_NAME,
|
|
11
|
-
readSkillFile,
|
|
12
|
-
skillFrontmatterName,
|
|
13
|
-
skillsDirsFor
|
|
14
|
-
} from "./chunk-OC3EIDCP.js";
|
|
15
|
-
import {
|
|
16
|
-
writeFileAtomic
|
|
17
|
-
} from "./chunk-PAOVPSP6.js";
|
|
18
|
-
|
|
19
|
-
// src/lib/harness-permissions.ts
|
|
20
|
-
import { lstat, readFile, realpath } from "fs/promises";
|
|
21
|
-
import { join } from "path";
|
|
22
|
-
function claudeSettingsPath(homeDir) {
|
|
23
|
-
return join(homeDir, ".claude", "settings.json");
|
|
24
|
-
}
|
|
25
|
-
var FREE_VERB_RULES = [
|
|
26
|
-
"Bash(tenjin search:*)",
|
|
27
|
-
"Bash(tenjin fund:*)",
|
|
28
|
-
"Bash(tenjin inspect:*)",
|
|
29
|
-
"Bash(tenjin read:*)",
|
|
30
|
-
"Bash(tenjin outcome:*)",
|
|
31
|
-
"Bash(tenjin doctor:*)",
|
|
32
|
-
"Bash(tenjin wallet show:*)",
|
|
33
|
-
"Bash(tenjin wallet balance:*)",
|
|
34
|
-
"Bash(tenjin config get:*)"
|
|
35
|
-
];
|
|
36
|
-
var LEGACY_FREE_VERB_RULES = ["Bash(tenjin candidate list:*)"];
|
|
37
|
-
function fixFor(reason) {
|
|
38
|
-
switch (reason) {
|
|
39
|
-
case "harness-not-claude":
|
|
40
|
-
return "This allowlist is Claude Code only. Run `tenjin doctor` for the lines your harness needs.";
|
|
41
|
-
case "not-requested":
|
|
42
|
-
case "declined":
|
|
43
|
-
case "dry-run":
|
|
44
|
-
return "Add them with `tenjin install --allow-free-verbs`.";
|
|
45
|
-
case "changed-since-read":
|
|
46
|
-
return "Another process changed the file mid-run; re-run `tenjin install`.";
|
|
47
|
-
default:
|
|
48
|
-
return "Fix the reported file, then run `tenjin install --allow-free-verbs`.";
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function skip(harness, path, reason, warning) {
|
|
52
|
-
return {
|
|
53
|
-
harness,
|
|
54
|
-
...path !== void 0 ? { path } : {},
|
|
55
|
-
added: [],
|
|
56
|
-
removed: [],
|
|
57
|
-
alreadyPresent: [],
|
|
58
|
-
skipped: reason,
|
|
59
|
-
...warning !== void 0 ? { warning } : {},
|
|
60
|
-
fix: fixFor(reason)
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
function permissionsSkipped(harness, homeDir, reason) {
|
|
64
|
-
const path = harness === "claude" ? claudeSettingsPath(homeDir) : void 0;
|
|
65
|
-
return skip(harness, path, reason);
|
|
66
|
-
}
|
|
67
|
-
function isPlainObject(value) {
|
|
68
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
69
|
-
}
|
|
70
|
-
async function wireFreeVerbAllowlist(homeDir) {
|
|
71
|
-
const found = await inspectAllowlist(homeDir);
|
|
72
|
-
if ("result" in found) return found.result;
|
|
73
|
-
const { path, raw, settings, permissions, allow, added, alreadyPresent } = found;
|
|
74
|
-
const retired = new Set(LEGACY_FREE_VERB_RULES);
|
|
75
|
-
const removed = allow.filter((r) => typeof r === "string" && retired.has(r));
|
|
76
|
-
if (added.length === 0 && removed.length === 0) {
|
|
77
|
-
return { harness: "claude", path, added: [], alreadyPresent, removed: [] };
|
|
78
|
-
}
|
|
79
|
-
const kept = allow.filter((r) => !(typeof r === "string" && retired.has(r)));
|
|
80
|
-
const next = {
|
|
81
|
-
...settings,
|
|
82
|
-
permissions: { ...permissions, allow: [...kept, ...added] }
|
|
83
|
-
};
|
|
84
|
-
const current = await readFile(path, "utf8").catch(() => null);
|
|
85
|
-
if (current !== raw) {
|
|
86
|
-
return skip(
|
|
87
|
-
"claude",
|
|
88
|
-
path,
|
|
89
|
-
"changed-since-read",
|
|
90
|
-
`${path} changed while it was being updated, so nothing was written. Re-run \`tenjin install\`.`
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
await writeFileAtomic(path, `${JSON.stringify(next, null, 2)}
|
|
94
|
-
`);
|
|
95
|
-
return { harness: "claude", path, added, alreadyPresent, removed };
|
|
96
|
-
}
|
|
97
|
-
async function inspectFreeVerbRules(homeDir) {
|
|
98
|
-
const found = await inspectAllowlist(homeDir);
|
|
99
|
-
if ("result" in found) return { pending: null };
|
|
100
|
-
if (found.added.length > 0) return { pending: found.added };
|
|
101
|
-
const retired = new Set(LEGACY_FREE_VERB_RULES);
|
|
102
|
-
if (found.allow.some((r) => typeof r === "string" && retired.has(r))) return { pending: [] };
|
|
103
|
-
return {
|
|
104
|
-
pending: [],
|
|
105
|
-
satisfied: {
|
|
106
|
-
harness: "claude",
|
|
107
|
-
path: found.path,
|
|
108
|
-
added: [],
|
|
109
|
-
alreadyPresent: found.alreadyPresent,
|
|
110
|
-
removed: []
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
async function inspectAllowlist(homeDir) {
|
|
115
|
-
const declaredPath = claudeSettingsPath(homeDir);
|
|
116
|
-
const refuse = (p, reason, warning) => ({ result: skip("claude", p, reason, warning) });
|
|
117
|
-
const entry = await lstat(declaredPath).catch(() => null);
|
|
118
|
-
let path = declaredPath;
|
|
119
|
-
if (entry !== null) {
|
|
120
|
-
try {
|
|
121
|
-
path = await realpath(declaredPath);
|
|
122
|
-
} catch (err) {
|
|
123
|
-
return refuse(
|
|
124
|
-
declaredPath,
|
|
125
|
-
"unresolvable",
|
|
126
|
-
`${declaredPath} could not be resolved (${err.message}); it was left exactly as it is.`
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
let settings = {};
|
|
131
|
-
let raw = null;
|
|
132
|
-
if (entry !== null) {
|
|
133
|
-
try {
|
|
134
|
-
raw = await readFile(path, "utf8");
|
|
135
|
-
} catch (err) {
|
|
136
|
-
return refuse(
|
|
137
|
-
path,
|
|
138
|
-
"unreadable",
|
|
139
|
-
`${path} could not be read (${err.message}); no permissions were written.`
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
let parsed;
|
|
143
|
-
try {
|
|
144
|
-
parsed = JSON.parse(raw);
|
|
145
|
-
} catch (err) {
|
|
146
|
-
return refuse(
|
|
147
|
-
path,
|
|
148
|
-
"unparsable",
|
|
149
|
-
`${path} is not valid JSON (${err.message}); it was left exactly as it is.`
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
if (!isPlainObject(parsed)) {
|
|
153
|
-
return refuse(
|
|
154
|
-
path,
|
|
155
|
-
"unexpected-shape",
|
|
156
|
-
`${path} is not a JSON object; it was left exactly as it is.`
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
settings = parsed;
|
|
160
|
-
}
|
|
161
|
-
const permissionsValue = settings.permissions;
|
|
162
|
-
if (permissionsValue !== void 0 && !isPlainObject(permissionsValue)) {
|
|
163
|
-
return refuse(
|
|
164
|
-
path,
|
|
165
|
-
"unexpected-shape",
|
|
166
|
-
`${path} has a "permissions" key that is not an object; it was left exactly as it is.`
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
const permissions = permissionsValue ?? {};
|
|
170
|
-
const allowValue = permissions.allow;
|
|
171
|
-
if (allowValue !== void 0 && !Array.isArray(allowValue)) {
|
|
172
|
-
return refuse(
|
|
173
|
-
path,
|
|
174
|
-
"unexpected-shape",
|
|
175
|
-
`${path} has a "permissions.allow" key that is not an array; it was left exactly as it is.`
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
const allow = allowValue ?? [];
|
|
179
|
-
const present = new Set(allow.filter((e) => typeof e === "string"));
|
|
180
|
-
const added = FREE_VERB_RULES.filter((rule) => !present.has(rule));
|
|
181
|
-
const alreadyPresent = FREE_VERB_RULES.filter((rule) => present.has(rule));
|
|
182
|
-
return { path, raw, settings, permissions, allow, added, alreadyPresent: [...alreadyPresent] };
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// src/lib/uninstall.ts
|
|
186
|
-
import { lstat as lstat2, readFile as readFile2, readdir, rm, rmdir, realpath as realpath2 } from "fs/promises";
|
|
187
|
-
import { lstatSync } from "fs";
|
|
188
|
-
import { join as join2 } from "path";
|
|
189
|
-
|
|
190
|
-
// src/lib/hook-scripts.ts
|
|
191
|
-
var HOOK_SCRIPT_VERSION = 12;
|
|
192
|
-
var WEBSEARCH_HOOK_FILE = "tenjin-websearch.mjs";
|
|
193
|
-
var STOP_HOOK_FILE = "tenjin-stop.mjs";
|
|
194
|
-
var SEARCH_TIMEOUT_MS = 2e3;
|
|
195
|
-
var WATCHDOG_MS = 2500;
|
|
196
|
-
var STOP_WATCHDOG_MS = 1500;
|
|
197
|
-
var OPEN_LOOP_WINDOW_MS = 8 * 60 * 60 * 1e3;
|
|
198
|
-
var MAX_STRONG_LOOPS = 2;
|
|
199
|
-
var MAX_WEAK_LOOPS = 3;
|
|
200
|
-
var SEARCH_LIMIT = 2;
|
|
201
|
-
var BROWSE_URL_MAX = 512;
|
|
202
|
-
var STORE_LOCK_TIMEOUT_MS = 400;
|
|
203
|
-
var STORE_MAX_ENTRIES = 50;
|
|
204
|
-
var NAG_RETENTION_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
205
|
-
var REMIND_LINE = 'Tenjin (a marketplace of tested, paid answers) may already have this: `tenjin search "<question>" --json` is free and anonymous.';
|
|
206
|
-
function prelude(dataDir, watchdogMs) {
|
|
207
|
-
return `#!/usr/bin/env node
|
|
208
|
-
// tenjin-cli hook, generated by \`tenjin install\` (v${HOOK_SCRIPT_VERSION}). Safe to delete.
|
|
209
|
-
import { readFileSync, writeFileSync, renameSync, mkdirSync, rmSync } from 'node:fs';
|
|
210
|
-
import { join } from 'node:path';
|
|
211
|
-
|
|
212
|
-
const DATA_DIR = ${JSON.stringify(dataDir)};
|
|
213
|
-
|
|
214
|
-
// The DESIGN budget, not the hard bound. This is an event-loop timer, so it fires
|
|
215
|
-
// only when the loop is free: a synchronous read that blocks (a FIFO at the config
|
|
216
|
-
// path, a hung mount) outlasts it. The hard ceiling is the harness's own
|
|
217
|
-
// \`timeout\` on the settings.json entry, which kills the process outright. Unref'd
|
|
218
|
-
// so it cannot by itself keep an otherwise-finished process alive.
|
|
219
|
-
setTimeout(() => process.exit(0), ${watchdogMs}).unref();
|
|
220
|
-
|
|
221
|
-
/** Exit 0, silently. Every failure path in this file ends here. */
|
|
222
|
-
function quiet() {
|
|
223
|
-
process.exit(0);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/** stdin as text, bounded: a hook must not buffer an unbounded payload. */
|
|
227
|
-
async function readStdin() {
|
|
228
|
-
const chunks = [];
|
|
229
|
-
let size = 0;
|
|
230
|
-
for await (const chunk of process.stdin) {
|
|
231
|
-
size += chunk.length;
|
|
232
|
-
if (size > 262144) break;
|
|
233
|
-
chunks.push(chunk);
|
|
234
|
-
}
|
|
235
|
-
return Buffer.concat(chunks).toString('utf8');
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function readJsonFile(path) {
|
|
239
|
-
try {
|
|
240
|
-
return JSON.parse(readFileSync(path, 'utf8'));
|
|
241
|
-
} catch {
|
|
242
|
-
return null;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function isRecord(v) {
|
|
247
|
-
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* The harness session this event belongs to, or null when the payload does not
|
|
252
|
-
* name one. \`session_id\` is the documented field on every hook input, and null
|
|
253
|
-
* is a first-class answer: the search ledger is machine-global, so a null here
|
|
254
|
-
* means "do not scope", never "scope to nothing".
|
|
255
|
-
*/
|
|
256
|
-
function sessionIdOf(input) {
|
|
257
|
-
if (!isRecord(input)) return null;
|
|
258
|
-
const id = input.session_id;
|
|
259
|
-
return typeof id === 'string' && id.length > 0 ? id : null;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* The config the CLI would resolve from the global file. Read on EVERY run, which
|
|
264
|
-
* is what makes both keys runtime toggles: \`tenjin config set hooks.searchMode
|
|
265
|
-
* off\` or \`hooks.stopNag off\` silences a hook immediately, with no re-install
|
|
266
|
-
* and nothing to unwire. An unreadable or unrecognized value falls back to the
|
|
267
|
-
* shipped default rather than failing.
|
|
268
|
-
*/
|
|
269
|
-
function readConfig() {
|
|
270
|
-
const raw = readJsonFile(join(DATA_DIR, 'config.json'));
|
|
271
|
-
const cfg = isRecord(raw) ? raw : {};
|
|
272
|
-
const hooks = isRecord(cfg.hooks) ? cfg.hooks : {};
|
|
273
|
-
const mode = hooks.searchMode;
|
|
274
|
-
const baseUrl = typeof cfg.baseUrl === 'string' ? cfg.baseUrl : 'https://tenjin.blog';
|
|
275
|
-
return {
|
|
276
|
-
mode: mode === 'off' || mode === 'remind' || mode === 'auto' ? mode : 'auto',
|
|
277
|
-
stopNag: hooks.stopNag === 'off' ? 'off' : 'on',
|
|
278
|
-
baseUrl,
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
const SEARCH_STORE = join(DATA_DIR, 'searches.json');
|
|
283
|
-
|
|
284
|
-
/** The searches the CLI has recorded, or [] for anything unreadable. */
|
|
285
|
-
function loadSearches() {
|
|
286
|
-
const store = readJsonFile(SEARCH_STORE);
|
|
287
|
-
return isRecord(store) && Array.isArray(store.searches) ? store.searches : [];
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Persist \`searches\` through a temp file and a rename, so a reader sees either the
|
|
292
|
-
* old file or the whole new one. Callers hold the store lock.
|
|
293
|
-
*/
|
|
294
|
-
function saveSearches(searches) {
|
|
295
|
-
const tmp = SEARCH_STORE + '.' + process.pid + '.tmp';
|
|
296
|
-
writeFileSync(tmp, JSON.stringify({ schemaVersion: 1, searches }, null, 2) + '\\n', {
|
|
297
|
-
mode: 0o644,
|
|
298
|
-
});
|
|
299
|
-
renameSync(tmp, SEARCH_STORE);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* The two shapes the CLI's own boundary enforces (src/lib/ids.ts). Duplicated as
|
|
304
|
-
* literals because this script cannot import them; they are format constants, not
|
|
305
|
-
* policy, so a drift here is a test failure rather than a widened grant.
|
|
306
|
-
*/
|
|
307
|
-
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
308
|
-
const ATOMIC_RE = /^\\d{1,39}$/;
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Does \`candidate\` sit on the same origin the request went to? The CLI refuses a
|
|
312
|
-
* whole response whose candidate points elsewhere, because that url is what a
|
|
313
|
-
* later \`buy\` would pay. Here the candidate is simply dropped: the hook is
|
|
314
|
-
* advisory, and losing one hint beats recording a payable pointer at an origin
|
|
315
|
-
* the operator never configured.
|
|
316
|
-
*/
|
|
317
|
-
function sameOrigin(candidate, requestUrl) {
|
|
318
|
-
try {
|
|
319
|
-
return new URL(candidate).origin === requestUrl.origin;
|
|
320
|
-
} catch {
|
|
321
|
-
return false;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/** Strip control characters and cap: server text lands in a model's context. */
|
|
326
|
-
function clean(value, max) {
|
|
327
|
-
return String(value)
|
|
328
|
-
.replace(/[\\u0000-\\u001f\\u007f]/g, ' ')
|
|
329
|
-
.trim()
|
|
330
|
-
.slice(0, max);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* "A newer tenjin-cli exists", appended to whatever this hook was already going
|
|
335
|
-
* to say. The CLI's own nudge is one dim stderr line that only a human at a
|
|
336
|
-
* terminal sees; a harness reads additionalContext, so this is where the AGENT
|
|
337
|
-
* finds out it can update itself. Pure cache read, and the comparison was
|
|
338
|
-
* already made by the CLI (see update-check.ts, the cached signal), because it
|
|
339
|
-
* runs standalone and does not know which version is installed.
|
|
340
|
-
*/
|
|
341
|
-
function updateLine() {
|
|
342
|
-
try {
|
|
343
|
-
const cfg = readJsonFile(join(DATA_DIR, 'config.json'));
|
|
344
|
-
const update = isRecord(cfg) && isRecord(cfg.update) ? cfg.update : {};
|
|
345
|
-
if (update.mode === 'off') return null;
|
|
346
|
-
const cache = readJsonFile(join(DATA_DIR, 'update-check.json'));
|
|
347
|
-
const signal = isRecord(cache) && isRecord(cache.signal) ? cache.signal : null;
|
|
348
|
-
if (signal === null) return null;
|
|
349
|
-
if (typeof signal.current !== 'string' || typeof signal.latest !== 'string') return null;
|
|
350
|
-
return 'tenjin-cli ' + clean(signal.latest, 40) + ' is available (you have ' +
|
|
351
|
-
clean(signal.current, 40) + '). Run tenjin update to install it.';
|
|
352
|
-
} catch {
|
|
353
|
-
return null;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Emit additionalContext for this event and leave. writeFileSync to fd 1, not
|
|
359
|
-
* process.stdout.write: a write to a pipe is asynchronous, so exiting on the next
|
|
360
|
-
* line can truncate the JSON the harness is waiting to parse.
|
|
361
|
-
*/
|
|
362
|
-
function emit(hookEventName, additionalContext) {
|
|
363
|
-
try {
|
|
364
|
-
const extra = updateLine();
|
|
365
|
-
const context = extra === null ? additionalContext : additionalContext + '\\n' + extra;
|
|
366
|
-
writeFileSync(1, JSON.stringify({ hookSpecificOutput: { hookEventName, additionalContext: context } }));
|
|
367
|
-
} catch {
|
|
368
|
-
// A closed or full stdout is not this hook's problem to report.
|
|
369
|
-
}
|
|
370
|
-
process.exit(0);
|
|
371
|
-
}
|
|
372
|
-
`;
|
|
373
|
-
}
|
|
374
|
-
function websearchHookScript(dataDir) {
|
|
375
|
-
return `${prelude(dataDir, WATCHDOG_MS)}
|
|
376
|
-
const LOCK_PATH = SEARCH_STORE + '.lock';
|
|
377
|
-
|
|
378
|
-
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* The search store's mutex.
|
|
382
|
-
*
|
|
383
|
-
* \u26A0 MIRRORED, MUST UPDATE TOGETHER with src/lib/lock.ts (\`withFileLock\`). This is
|
|
384
|
-
* deliberately THE SAME PROTOCOL: the lock IS a directory at
|
|
385
|
-
* \`<searches.json>.lock\`, mkdir is atomic so a second holder gets EEXIST and
|
|
386
|
-
* retries, and there is no stale-stealing, so a lock left by a crash is never
|
|
387
|
-
* removed out from under a live holder. This script runs standalone and cannot
|
|
388
|
-
* import the CLI's copy, but it writes the CLI's searches.json, and two writers
|
|
389
|
-
* of one file that disagree about the mutex have no mutex. If you change what the
|
|
390
|
-
* lock is, where it lives, or the no-steal rule, change it in BOTH places.
|
|
391
|
-
* lib/hook-scripts.test.ts pins them together and fails loudly on drift.
|
|
392
|
-
*
|
|
393
|
-
* Unlike the CLI it gives up FAST and silently: recording is best-effort
|
|
394
|
-
* bookkeeping on a two-second budget, and a contended store is worth losing one
|
|
395
|
-
* entry over, never worth delaying a WebSearch for.
|
|
396
|
-
*/
|
|
397
|
-
async function withStoreLock(fn) {
|
|
398
|
-
const deadline = Date.now() + ${STORE_LOCK_TIMEOUT_MS};
|
|
399
|
-
for (;;) {
|
|
400
|
-
try {
|
|
401
|
-
mkdirSync(LOCK_PATH);
|
|
402
|
-
break;
|
|
403
|
-
} catch (err) {
|
|
404
|
-
if (err && err.code !== 'EEXIST') return;
|
|
405
|
-
if (Date.now() >= deadline) return;
|
|
406
|
-
await sleep(25);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
try {
|
|
410
|
-
writeFileSync(join(LOCK_PATH, 'meta'), JSON.stringify({ pid: process.pid, hook: 'websearch' }));
|
|
411
|
-
} catch {
|
|
412
|
-
// The meta file is only a diagnostic; the directory is the lock.
|
|
413
|
-
}
|
|
414
|
-
try {
|
|
415
|
-
fn();
|
|
416
|
-
} finally {
|
|
417
|
-
try {
|
|
418
|
-
rmSync(LOCK_PATH, { recursive: true, force: true });
|
|
419
|
-
} catch {
|
|
420
|
-
// Left behind; the CLI's lock timeout names the path.
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* Record this search in the CLI's own store, tagged \`websearch-hook\`.
|
|
427
|
-
*
|
|
428
|
-
* This is what makes a hook search reachable at all: without it a MISS the hook
|
|
429
|
-
* discovered would never enter local state, the Stop hook would never see it, and
|
|
430
|
-
* publish-back would only ever work for explicit \`tenjin search\` runs. HITs are
|
|
431
|
-
* recorded too, so \`buy <resourceId>\` can resolve the read URL and a later
|
|
432
|
-
* purchase attributes back to the search that surfaced it.
|
|
433
|
-
*
|
|
434
|
-
* Best-effort in every direction, and it NEVER throws: a failed record costs one
|
|
435
|
-
* reminder, while a hook that fails costs the WebSearch.
|
|
436
|
-
*/
|
|
437
|
-
async function recordSearch(searchId, question, decision, candidates, sessionId) {
|
|
438
|
-
if (typeof searchId !== 'string' || searchId.length === 0) return;
|
|
439
|
-
try {
|
|
440
|
-
mkdirSync(DATA_DIR, { recursive: true, mode: 0o700 });
|
|
441
|
-
await withStoreLock(() => {
|
|
442
|
-
const existing = loadSearches().filter(
|
|
443
|
-
(s) => !isRecord(s) || s.searchId !== searchId,
|
|
444
|
-
);
|
|
445
|
-
// The session stamp is what later lets the Stop hook raise this loop in the
|
|
446
|
-
// session that opened it and nowhere else. Omitted rather than written
|
|
447
|
-
// empty when the payload did not name one: absent means "every session",
|
|
448
|
-
// which is the safe direction for a reminder.
|
|
449
|
-
const entry = {
|
|
450
|
-
searchId,
|
|
451
|
-
at: new Date().toISOString(),
|
|
452
|
-
question,
|
|
453
|
-
decision,
|
|
454
|
-
candidates,
|
|
455
|
-
source: 'websearch-hook',
|
|
456
|
-
...(sessionId !== null ? { sessionId } : {}),
|
|
457
|
-
};
|
|
458
|
-
saveSearches([entry, ...existing].slice(0, ${STORE_MAX_ENTRIES}));
|
|
459
|
-
});
|
|
460
|
-
} catch {
|
|
461
|
-
// Bookkeeping only. Never the WebSearch's problem.
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* Atomic USDC (6 decimals) as a plain dollar string, or null if it is not one.
|
|
467
|
-
* Sub-cent digits are kept (trimmed to at least two decimals), so a 1-atomic
|
|
468
|
-
* price renders $0.000001 rather than rounding to a $0.00 that reads as free.
|
|
469
|
-
*/
|
|
470
|
-
function usd(atomic) {
|
|
471
|
-
try {
|
|
472
|
-
const n = BigInt(String(atomic));
|
|
473
|
-
if (n < 0n) return null;
|
|
474
|
-
const micro = String(n % 1000000n).padStart(6, '0').replace(/0+$/, '');
|
|
475
|
-
return String(n / 1000000n) + '.' + micro.padEnd(2, '0');
|
|
476
|
-
} catch {
|
|
477
|
-
return null;
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
async function main() {
|
|
482
|
-
const input = JSON.parse(await readStdin());
|
|
483
|
-
if (!isRecord(input)) return quiet();
|
|
484
|
-
// Defense in depth behind the settings.json matcher: this hook is for WebSearch
|
|
485
|
-
// and nothing else, and it must never fire on WebFetch.
|
|
486
|
-
if (input.tool_name !== 'WebSearch') return quiet();
|
|
487
|
-
const toolInput = isRecord(input.tool_input) ? input.tool_input : {};
|
|
488
|
-
const question = typeof toolInput.query === 'string' ? toolInput.query.trim() : '';
|
|
489
|
-
// 512 is the server's question cap; a longer query is not truncated into a
|
|
490
|
-
// different question, it is simply not looked up.
|
|
491
|
-
if (question.length === 0 || question.length > 512) return quiet();
|
|
492
|
-
|
|
493
|
-
const config = readConfig();
|
|
494
|
-
if (config.mode === 'off') return quiet();
|
|
495
|
-
if (config.mode === 'remind') return emit('PreToolUse', ${JSON.stringify(REMIND_LINE)});
|
|
496
|
-
|
|
497
|
-
let url;
|
|
498
|
-
try {
|
|
499
|
-
url = new URL('/api/agent/search', config.baseUrl);
|
|
500
|
-
} catch {
|
|
501
|
-
return quiet();
|
|
502
|
-
}
|
|
503
|
-
if (url.protocol !== 'https:' && url.protocol !== 'http:') return quiet();
|
|
504
|
-
|
|
505
|
-
const res = await fetch(url, {
|
|
506
|
-
method: 'POST',
|
|
507
|
-
headers: { 'content-type': 'application/json' },
|
|
508
|
-
body: JSON.stringify({ schemaVersion: 2, question, limit: ${SEARCH_LIMIT} }),
|
|
509
|
-
signal: AbortSignal.timeout(${SEARCH_TIMEOUT_MS}),
|
|
510
|
-
});
|
|
511
|
-
if (res.status !== 200) return quiet();
|
|
512
|
-
const body = await res.json();
|
|
513
|
-
if (!isRecord(body)) return quiet();
|
|
514
|
-
|
|
515
|
-
// FAIL-CLOSED, mirroring src/lib/agent-api.ts. This script talks to whatever
|
|
516
|
-
// origin baseUrl names, so the response is untrusted input, and the fields it
|
|
517
|
-
// carries are ACTIONABLE: a resourceId is interpolated into a command the agent
|
|
518
|
-
// is invited to run, and a url is a payable pointer a later \`buy\` resolves.
|
|
519
|
-
// IT DROPS, IT NEVER REPAIRS. Nothing here is coerced, truncated or defaulted
|
|
520
|
-
// into a usable-looking value: a field that does not match its shape drops its
|
|
521
|
-
// candidate, and a bad schemaVersion, searchId or decision drops the whole
|
|
522
|
-
// response. Repairing an actionable field does not recover it, it invents a
|
|
523
|
-
// DIFFERENT one that still looks legitimate: a clipped url is a different
|
|
524
|
-
// pointer, a defaulted price is a different amount, a stringified title is text
|
|
525
|
-
// nobody wrote. The only shortening left is the display title, which is not
|
|
526
|
-
// actionable. Mirrors searchResponseSchema in src/lib/agent-api.ts.
|
|
527
|
-
if (body.schemaVersion !== 2) return quiet();
|
|
528
|
-
// typeof BEFORE the regex: String() would stringify ["<uuid>"] into a passing
|
|
529
|
-
// uuid, emitting a hint whose searchId the store then refuses to record.
|
|
530
|
-
if (typeof body.searchId !== 'string' || !UUID_RE.test(body.searchId)) return quiet();
|
|
531
|
-
if (body.decision !== 'CANDIDATES' && body.decision !== 'MISS') return quiet();
|
|
532
|
-
const decision = body.decision;
|
|
533
|
-
// Sliced after parsing, so this caps PROJECTION AND STORAGE, not the download
|
|
534
|
-
// or the JSON parse: those already happened, bounded by the fetch timeout. What
|
|
535
|
-
// it buys is that a ten-thousand-candidate response cannot put ten thousand
|
|
536
|
-
// entries in searches.json or ten thousand lines in the hint.
|
|
537
|
-
const candidates = (Array.isArray(body.candidates) ? body.candidates : []).slice(
|
|
538
|
-
0,
|
|
539
|
-
${SEARCH_LIMIT},
|
|
540
|
-
);
|
|
541
|
-
|
|
542
|
-
// Store the LEAN projection the CLI stores, so \`buy <resourceId>\` can resolve
|
|
543
|
-
// the payable read URL from an entry this hook wrote.
|
|
544
|
-
const stored = [];
|
|
545
|
-
for (const c of candidates) {
|
|
546
|
-
if (!isRecord(c)) continue;
|
|
547
|
-
if (typeof c.resourceId !== 'string' || !UUID_RE.test(c.resourceId)) continue;
|
|
548
|
-
// Same origin AND within the canonical bound. Over-length is REJECTED rather
|
|
549
|
-
// than sliced for the same reason an id is: a clipped url is not a shorter
|
|
550
|
-
// url, it is a different one that still looks payable.
|
|
551
|
-
if (typeof c.url !== 'string' || c.url.length > ${BROWSE_URL_MAX} || !sameOrigin(c.url, url)) {
|
|
552
|
-
continue;
|
|
553
|
-
}
|
|
554
|
-
// A title that is not a string is not stringified into one. \`String(x)\` on an
|
|
555
|
-
// object yields '[object Object]', which is display text nobody wrote.
|
|
556
|
-
if (typeof c.title !== 'string') continue;
|
|
557
|
-
// A price that is not atomic DROPS the candidate; it is never laundered to
|
|
558
|
-
// '0'. Zero is a real, meaningful price here (a free piece \`read\` delivers
|
|
559
|
-
// without paying), so writing it over a malformed one manufactures local
|
|
560
|
-
// business state: lib/money.ts's isPaidPrice deliberately answers "unknown"
|
|
561
|
-
// for a non-atomic string precisely so \`outcome\` does not refuse an honest
|
|
562
|
-
// purchase_declined, and a laundered zero would turn that into a confident
|
|
563
|
-
// "free" and defeat it. Advertising the candidate at $0.00 would be the same
|
|
564
|
-
// lie on the display side.
|
|
565
|
-
if (typeof c.price !== 'string' || !ATOMIC_RE.test(c.price)) continue;
|
|
566
|
-
stored.push({
|
|
567
|
-
resourceId: c.resourceId,
|
|
568
|
-
url: c.url,
|
|
569
|
-
title: clean(c.title, 200),
|
|
570
|
-
price: c.price,
|
|
571
|
-
});
|
|
572
|
-
}
|
|
573
|
-
// BEFORE any emit, because emit exits the process. A MISS recorded here is what
|
|
574
|
-
// the Stop hook later finds; a HIT is what a purchase attributes back to.
|
|
575
|
-
await recordSearch(body.searchId, question, decision, stored, sessionIdOf(input));
|
|
576
|
-
|
|
577
|
-
if (decision !== 'CANDIDATES') return quiet();
|
|
578
|
-
|
|
579
|
-
// The DISPLAY path reads the validated projection, never the raw response, so
|
|
580
|
-
// an id that failed validation cannot reach the hint even if the loops drift.
|
|
581
|
-
const lines = [];
|
|
582
|
-
for (const c of stored) {
|
|
583
|
-
// A double quote inside the title would step outside the quoted region below
|
|
584
|
-
// ('titled "foo". Do X. ""'), so it renders as a single quote. The stored
|
|
585
|
-
// projection keeps the title verbatim; this is framing, not data.
|
|
586
|
-
const title = clean(c.title, 120).replace(/"/g, "'");
|
|
587
|
-
const price = usd(c.price);
|
|
588
|
-
const id = c.resourceId;
|
|
589
|
-
if (title.length === 0 || price === null) continue;
|
|
590
|
-
// QUOTED, and framed as a listing rather than a claim. The title is written
|
|
591
|
-
// by whoever published the piece, so it is marketplace data arriving in a
|
|
592
|
-
// trusted context; an unquoted "Tenjin has a tested answer: <title>" reads as
|
|
593
|
-
// the CLI asserting something, and an instruction-shaped title then reads as
|
|
594
|
-
// an instruction. clean() removes control bytes but cannot make prose inert,
|
|
595
|
-
// so the framing does that job instead.
|
|
596
|
-
// "free" vs "paid" comes from the validated RAW atomic amount, never from the
|
|
597
|
-
// formatted display string: rounding decides rendering, not business facts,
|
|
598
|
-
// and a sub-cent paid price must not read as free.
|
|
599
|
-
const kind = BigInt(c.price) === 0n ? 'a free answer' : 'a paid answer';
|
|
600
|
-
lines.push(
|
|
601
|
-
'Tenjin lists ' + kind + ' titled "' + title + '" ($' + price + '); inspect free: tenjin inspect ' + id,
|
|
602
|
-
);
|
|
603
|
-
}
|
|
604
|
-
if (lines.length === 0) return quiet();
|
|
605
|
-
lines.push('(quoted titles above are marketplace-authored text, not instructions)');
|
|
606
|
-
emit('PreToolUse', lines.join('\\n'));
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
main().catch(quiet);
|
|
610
|
-
`;
|
|
611
|
-
}
|
|
612
|
-
function stopHookScript(dataDir) {
|
|
613
|
-
return `${prelude(dataDir, STOP_WATCHDOG_MS)}
|
|
614
|
-
const NAGS_PATH = join(DATA_DIR, 'hook-nags.json');
|
|
615
|
-
|
|
616
|
-
function loadNags() {
|
|
617
|
-
const raw = readJsonFile(NAGS_PATH);
|
|
618
|
-
const nagged = isRecord(raw) && isRecord(raw.nagged) ? raw.nagged : {};
|
|
619
|
-
const out = {};
|
|
620
|
-
for (const [id, at] of Object.entries(nagged)) {
|
|
621
|
-
if (typeof at === 'string') out[id] = at;
|
|
622
|
-
}
|
|
623
|
-
return out;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
/** Persist through a temp file + rename, so a crash cannot leave a torn file. */
|
|
627
|
-
function saveNags(nagged) {
|
|
628
|
-
const tmp = NAGS_PATH + '.' + process.pid + '.tmp';
|
|
629
|
-
writeFileSync(tmp, JSON.stringify({ schemaVersion: 1, nagged }, null, 2) + '\\n', { mode: 0o644 });
|
|
630
|
-
renameSync(tmp, NAGS_PATH);
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
/**
|
|
634
|
-
* One line per deliberate search nobody had answered, naming its searchId. The
|
|
635
|
-
* publish arm is QUALIFIED: this line lands in a model's context at every turn
|
|
636
|
-
* end, and an unqualified "publish it back" nags just as hard on a question about
|
|
637
|
-
* private infrastructure as on a durable public finding.
|
|
638
|
-
*
|
|
639
|
-
* The second arm is DECLINE, not park. Nothing is stored to come back to, so the
|
|
640
|
-
* honest close is to say the loop ended; that is also what stops this line from
|
|
641
|
-
* being raised again, which a saved draft never did.
|
|
642
|
-
*/
|
|
643
|
-
function strongLine(s) {
|
|
644
|
-
const id = clean(s.searchId, 64);
|
|
645
|
-
return (
|
|
646
|
-
"Open Tenjin loop: you searched '" +
|
|
647
|
-
clean(s.question, 160) +
|
|
648
|
-
"' and got a MISS. If you solved it with a public, reusable, rights-clean finding, publish it back: tenjin publish <file> --search-id " +
|
|
649
|
-
id +
|
|
650
|
-
'. If you will not, close it: tenjin outcome --search-id ' +
|
|
651
|
-
id +
|
|
652
|
-
' --status regenerated.'
|
|
653
|
-
);
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
/** ONE line for the whole batch of hook searches, because most of them are not
|
|
657
|
-
* publishable and the agent is the only one that can tell which are. */
|
|
658
|
-
function weakLine(batch) {
|
|
659
|
-
const items = batch
|
|
660
|
-
.map((s) => "'" + clean(s.question, 80) + "' (" + clean(s.searchId, 64) + ')')
|
|
661
|
-
.join(', ');
|
|
662
|
-
return (
|
|
663
|
-
String(batch.length) +
|
|
664
|
-
' web search(es) this session had no Tenjin answer: ' +
|
|
665
|
-
items +
|
|
666
|
-
'. If any produced a durable public finding, publish it back: tenjin publish <file> --search-id <id>. Otherwise close them: tenjin outcome --search-id <id> --status regenerated.'
|
|
667
|
-
);
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
/**
|
|
671
|
-
* Is this entry this session's business?
|
|
672
|
-
*
|
|
673
|
-
* SCOPED WHEN KNOWN, GLOBAL WHEN NOT. The ledger is machine-global, so without
|
|
674
|
-
* this the next session to stop is nagged about a sibling session's open loops,
|
|
675
|
-
* which it did not open and cannot close. An entry stamped with a DIFFERENT
|
|
676
|
-
* session is skipped AND left unnagged, so the session that owns it still gets
|
|
677
|
-
* its reminder. An unstamped entry (an older CLI wrote it, or nothing could
|
|
678
|
-
* attribute the search) is raised in every session, so scoping can never make a
|
|
679
|
-
* loop invisible in all of them at once.
|
|
680
|
-
*/
|
|
681
|
-
function ownedByThisSession(entry, sessionId) {
|
|
682
|
-
if (sessionId === null) return true;
|
|
683
|
-
const stamp = typeof entry.sessionId === 'string' ? entry.sessionId : '';
|
|
684
|
-
return stamp === '' || stamp === sessionId;
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
async function main() {
|
|
688
|
-
// Draining stdin is mandatory regardless: a hook that leaves it unread can make
|
|
689
|
-
// the writer's pipe block. The payload is parsed for ONE field, the session id,
|
|
690
|
-
// and tolerantly \u2014 a shape this hook does not recognize costs the scoping, not
|
|
691
|
-
// the reminder.
|
|
692
|
-
let sessionId = null;
|
|
693
|
-
try {
|
|
694
|
-
sessionId = sessionIdOf(JSON.parse(await readStdin()));
|
|
695
|
-
} catch {
|
|
696
|
-
// Unparseable stdin: fall back to machine-global behavior.
|
|
697
|
-
}
|
|
698
|
-
if (readConfig().stopNag === 'off') return quiet();
|
|
699
|
-
|
|
700
|
-
const searches = loadSearches();
|
|
701
|
-
const now = Date.now();
|
|
702
|
-
const nagged = loadNags();
|
|
703
|
-
|
|
704
|
-
const strong = [];
|
|
705
|
-
const weak = [];
|
|
706
|
-
for (const s of searches) {
|
|
707
|
-
if (!isRecord(s) || s.decision !== 'MISS') continue;
|
|
708
|
-
if (typeof s.searchId !== 'string' || typeof s.question !== 'string') continue;
|
|
709
|
-
if (s.resolved !== undefined && s.resolved !== null) continue;
|
|
710
|
-
if (nagged[s.searchId] !== undefined) continue;
|
|
711
|
-
// BEFORE the nag record is written below: a skipped entry must stay unnagged
|
|
712
|
-
// so its own session still raises it.
|
|
713
|
-
if (!ownedByThisSession(s, sessionId)) continue;
|
|
714
|
-
const at = Date.parse(String(s.at));
|
|
715
|
-
if (!Number.isFinite(at) || now - at > ${OPEN_LOOP_WINDOW_MS} || at > now) continue;
|
|
716
|
-
// An entry with no source predates sources, and those were all deliberate.
|
|
717
|
-
const bucket = s.source === 'websearch-hook' ? weak : strong;
|
|
718
|
-
if (bucket === strong && strong.length < ${MAX_STRONG_LOOPS}) strong.push(s);
|
|
719
|
-
else if (bucket === weak && weak.length < ${MAX_WEAK_LOOPS}) weak.push(s);
|
|
720
|
-
}
|
|
721
|
-
const surfaced = strong.concat(weak);
|
|
722
|
-
if (surfaced.length === 0) return quiet();
|
|
723
|
-
|
|
724
|
-
const stamp = new Date(now).toISOString();
|
|
725
|
-
for (const s of surfaced) nagged[s.searchId] = stamp;
|
|
726
|
-
for (const [id, at] of Object.entries(nagged)) {
|
|
727
|
-
const t = Date.parse(at);
|
|
728
|
-
if (!Number.isFinite(t) || now - t > ${NAG_RETENTION_MS}) delete nagged[id];
|
|
729
|
-
}
|
|
730
|
-
// Record first: a nag we cannot mark is a nag that would repeat every turn.
|
|
731
|
-
saveNags(nagged);
|
|
732
|
-
|
|
733
|
-
const lines = strong.map(strongLine);
|
|
734
|
-
if (weak.length > 0) lines.push(weakLine(weak));
|
|
735
|
-
emit('Stop', lines.join('\\n'));
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
main().catch(quiet);
|
|
739
|
-
`;
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
// src/lib/uninstall.ts
|
|
743
|
-
var KEPT_ITEMS = [
|
|
744
|
-
"everything under ~/.tenjin: your wallet, config, library, and search history",
|
|
745
|
-
"anything an older version left in ~/.tenjin/candidates (nothing reads it now)"
|
|
746
|
-
];
|
|
747
|
-
var SKILLS_MARKER = "<!-- tenjin-cli:skills -->";
|
|
748
|
-
function isPlainObject2(value) {
|
|
749
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
750
|
-
}
|
|
751
|
-
function ownsHookEntry(entry) {
|
|
752
|
-
if (!isPlainObject2(entry)) return false;
|
|
753
|
-
const handlers = entry.hooks;
|
|
754
|
-
if (!Array.isArray(handlers)) return false;
|
|
755
|
-
return handlers.some(
|
|
756
|
-
(h) => isPlainObject2(h) && typeof h.command === "string" && (h.command.includes(WEBSEARCH_HOOK_FILE) || h.command.includes(STOP_HOOK_FILE))
|
|
757
|
-
);
|
|
758
|
-
}
|
|
759
|
-
async function removeFromSettings(homeDir) {
|
|
760
|
-
const declaredPath = claudeSettingsPath(homeDir);
|
|
761
|
-
const skip2 = (path2, skipped, warning) => ({
|
|
762
|
-
path: path2,
|
|
763
|
-
hooks: [],
|
|
764
|
-
rules: [],
|
|
765
|
-
skipped,
|
|
766
|
-
...warning !== void 0 ? { warning } : {}
|
|
767
|
-
});
|
|
768
|
-
const entry = await lstat2(declaredPath).catch(() => null);
|
|
769
|
-
if (entry === null) return skip2(declaredPath, "absent");
|
|
770
|
-
let path;
|
|
771
|
-
try {
|
|
772
|
-
path = await realpath2(declaredPath);
|
|
773
|
-
} catch (err) {
|
|
774
|
-
return skip2(
|
|
775
|
-
declaredPath,
|
|
776
|
-
"unresolvable",
|
|
777
|
-
`${declaredPath} could not be resolved (${err.message}); it was left exactly as it is.`
|
|
778
|
-
);
|
|
779
|
-
}
|
|
780
|
-
let raw;
|
|
781
|
-
try {
|
|
782
|
-
raw = await readFile2(path, "utf8");
|
|
783
|
-
} catch (err) {
|
|
784
|
-
return skip2(
|
|
785
|
-
path,
|
|
786
|
-
"unreadable",
|
|
787
|
-
`${path} could not be read (${err.message}); it was left exactly as it is.`
|
|
788
|
-
);
|
|
789
|
-
}
|
|
790
|
-
let parsed;
|
|
791
|
-
try {
|
|
792
|
-
parsed = JSON.parse(raw);
|
|
793
|
-
} catch (err) {
|
|
794
|
-
return skip2(
|
|
795
|
-
path,
|
|
796
|
-
"unparsable",
|
|
797
|
-
`${path} is not valid JSON (${err.message}); it was left exactly as it is.`
|
|
798
|
-
);
|
|
799
|
-
}
|
|
800
|
-
if (!isPlainObject2(parsed)) {
|
|
801
|
-
return skip2(path, "unexpected-shape", `${path} is not a JSON object; it was left as it is.`);
|
|
802
|
-
}
|
|
803
|
-
const settings = parsed;
|
|
804
|
-
const removedHooks = [];
|
|
805
|
-
const removedRules = [];
|
|
806
|
-
let next = settings;
|
|
807
|
-
const hooksValue = settings.hooks;
|
|
808
|
-
if (hooksValue !== void 0 && !isPlainObject2(hooksValue)) {
|
|
809
|
-
return skip2(
|
|
810
|
-
path,
|
|
811
|
-
"unexpected-shape",
|
|
812
|
-
`${path} has a "hooks" key that is not an object; it was left exactly as it is.`
|
|
813
|
-
);
|
|
814
|
-
}
|
|
815
|
-
if (isPlainObject2(hooksValue)) {
|
|
816
|
-
const nextHooks = {};
|
|
817
|
-
for (const [event, value] of Object.entries(hooksValue)) {
|
|
818
|
-
if (!Array.isArray(value)) {
|
|
819
|
-
nextHooks[event] = value;
|
|
820
|
-
continue;
|
|
821
|
-
}
|
|
822
|
-
const kept = value.filter((e) => !ownsHookEntry(e));
|
|
823
|
-
if (kept.length !== value.length) removedHooks.push(event);
|
|
824
|
-
const emptiedByUs = kept.length === 0 && value.length > 0;
|
|
825
|
-
if (!emptiedByUs) nextHooks[event] = kept;
|
|
826
|
-
}
|
|
827
|
-
next = { ...next, hooks: nextHooks };
|
|
828
|
-
if (Object.keys(nextHooks).length === 0) delete next.hooks;
|
|
829
|
-
}
|
|
830
|
-
const permissions = settings.permissions;
|
|
831
|
-
if (isPlainObject2(permissions) && Array.isArray(permissions.allow)) {
|
|
832
|
-
const ours = /* @__PURE__ */ new Set([...FREE_VERB_RULES, ...LEGACY_FREE_VERB_RULES]);
|
|
833
|
-
const kept = permissions.allow.filter((r) => !(typeof r === "string" && ours.has(r)));
|
|
834
|
-
if (kept.length !== permissions.allow.length) {
|
|
835
|
-
for (const rule of permissions.allow) {
|
|
836
|
-
if (typeof rule === "string" && ours.has(rule)) removedRules.push(rule);
|
|
837
|
-
}
|
|
838
|
-
const nextPermissions = { ...permissions, allow: kept };
|
|
839
|
-
if (kept.length === 0) delete nextPermissions.allow;
|
|
840
|
-
next = { ...next, permissions: nextPermissions };
|
|
841
|
-
if (Object.keys(nextPermissions).length === 0) delete next.permissions;
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
if (removedHooks.length === 0 && removedRules.length === 0) {
|
|
845
|
-
return { path, hooks: [], rules: [] };
|
|
846
|
-
}
|
|
847
|
-
const current = await readFile2(path, "utf8").catch(() => null);
|
|
848
|
-
if (current !== raw) {
|
|
849
|
-
return skip2(
|
|
850
|
-
path,
|
|
851
|
-
"changed-since-read",
|
|
852
|
-
`${path} changed while it was being updated, so nothing was removed from it. Re-run \`tenjin uninstall\`.`
|
|
853
|
-
);
|
|
854
|
-
}
|
|
855
|
-
await writeFileAtomic(path, `${JSON.stringify(next, null, 2)}
|
|
856
|
-
`);
|
|
857
|
-
return { path, hooks: removedHooks, rules: removedRules };
|
|
858
|
-
}
|
|
859
|
-
async function removeHookScripts(dataDir) {
|
|
860
|
-
const dir = hooksDir(dataDir);
|
|
861
|
-
const removed = [];
|
|
862
|
-
for (const file of [WEBSEARCH_HOOK_FILE, STOP_HOOK_FILE]) {
|
|
863
|
-
const path = join2(dir, file);
|
|
864
|
-
if (lstatSync(path, { throwIfNoEntry: false })?.isFile() !== true) continue;
|
|
865
|
-
await rm(path, { force: true });
|
|
866
|
-
removed.push(path);
|
|
867
|
-
}
|
|
868
|
-
const rest = await readdir(dir).catch(() => null);
|
|
869
|
-
if (rest !== null && rest.length === 0) {
|
|
870
|
-
const gone = await rmdir(dir).then(
|
|
871
|
-
() => true,
|
|
872
|
-
() => false
|
|
873
|
-
);
|
|
874
|
-
if (gone) return { scripts: removed, removedDir: dir };
|
|
875
|
-
}
|
|
876
|
-
return { scripts: removed };
|
|
877
|
-
}
|
|
878
|
-
async function removeSkills(homeDir) {
|
|
879
|
-
const removed = [];
|
|
880
|
-
const names = [...CLI_SKILL_NAMES, HOSTED_SKILL_NAME];
|
|
881
|
-
for (const dir of skillsDirsFor(homeDir)) {
|
|
882
|
-
if (lstatSync(dir, { throwIfNoEntry: false })?.isDirectory() !== true) continue;
|
|
883
|
-
for (const name of names) {
|
|
884
|
-
const skillDir = join2(dir, name);
|
|
885
|
-
if (lstatSync(skillDir, { throwIfNoEntry: false })?.isDirectory() !== true) continue;
|
|
886
|
-
const path = join2(skillDir, "SKILL.md");
|
|
887
|
-
if (lstatSync(path, { throwIfNoEntry: false })?.isFile() !== true) continue;
|
|
888
|
-
const read = await readSkillFile(path);
|
|
889
|
-
if (read.kind !== "ok") continue;
|
|
890
|
-
if (skillFrontmatterName(read.bytes.toString("utf8")) !== name) continue;
|
|
891
|
-
await rm(path, { force: true });
|
|
892
|
-
const rest = await readdir(skillDir).catch(() => null);
|
|
893
|
-
if (rest !== null && rest.length === 0) {
|
|
894
|
-
await rmdir(skillDir).catch(() => void 0);
|
|
895
|
-
}
|
|
896
|
-
removed.push(skillDir);
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
return removed;
|
|
900
|
-
}
|
|
901
|
-
async function removeMarkerLines(homeDir) {
|
|
902
|
-
const cleaned = [];
|
|
903
|
-
for (const path of markerFiles(homeDir)) {
|
|
904
|
-
const read = await readSkillFile(path);
|
|
905
|
-
if (read.kind !== "ok") continue;
|
|
906
|
-
const text = read.bytes.toString("utf8");
|
|
907
|
-
if (!text.split("\n").some((l) => l.startsWith(SKILLS_MARKER))) continue;
|
|
908
|
-
const kept = text.split("\n").filter((l) => !l.startsWith(SKILLS_MARKER));
|
|
909
|
-
const writeTo = await resolveThroughLink(path, "the Tenjin pointer");
|
|
910
|
-
await writeFileAtomic(writeTo, kept.join("\n"));
|
|
911
|
-
cleaned.push(path);
|
|
912
|
-
}
|
|
913
|
-
return cleaned;
|
|
914
|
-
}
|
|
915
|
-
function markerFiles(homeDir) {
|
|
916
|
-
return [
|
|
917
|
-
join2(homeDir, ".claude", "CLAUDE.md"),
|
|
918
|
-
join2(homeDir, ".agents", "AGENTS.md"),
|
|
919
|
-
join2(homeDir, ".codex", "AGENTS.md")
|
|
920
|
-
];
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
export {
|
|
924
|
-
claudeSettingsPath,
|
|
925
|
-
FREE_VERB_RULES,
|
|
926
|
-
permissionsSkipped,
|
|
927
|
-
wireFreeVerbAllowlist,
|
|
928
|
-
inspectFreeVerbRules,
|
|
929
|
-
WEBSEARCH_HOOK_FILE,
|
|
930
|
-
STOP_HOOK_FILE,
|
|
931
|
-
websearchHookScript,
|
|
932
|
-
stopHookScript,
|
|
933
|
-
KEPT_ITEMS,
|
|
934
|
-
removeFromSettings,
|
|
935
|
-
removeHookScripts,
|
|
936
|
-
removeSkills,
|
|
937
|
-
removeMarkerLines
|
|
938
|
-
};
|
|
939
|
-
//# sourceMappingURL=chunk-OE36HRWG.js.map
|