khala-cli 0.4.8 → 0.4.9

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.
Files changed (48) hide show
  1. package/README.md +4 -4
  2. package/dist/THIRD_PARTY_NOTICES.txt +781 -0
  3. package/dist/{chunk-APMWDF6B.mjs → chunk-3KDOTR54.mjs} +4 -2
  4. package/dist/{chunk-VUSV2ADL.mjs → chunk-3R6HFSLL.mjs} +147 -36
  5. package/dist/{chunk-2VCAW4GH.mjs → chunk-47XNLAJG.mjs} +5 -1
  6. package/dist/chunk-6BO3JSUP.mjs +33 -0
  7. package/dist/chunk-7R3EIDGR.mjs +58 -0
  8. package/dist/{chunk-5LIE5GUU.mjs → chunk-ANSR4IAW.mjs} +5 -1
  9. package/dist/chunk-BEO7WIIM.mjs +153 -0
  10. package/dist/chunk-BLCRVRIK.mjs +1000 -0
  11. package/dist/{chunk-54XFVSEC.mjs → chunk-BV45NK7K.mjs} +5 -1
  12. package/dist/{chunk-Z4JF2M5C.mjs → chunk-DJM23SIU.mjs} +8 -7
  13. package/dist/{chunk-2DHIAFEV.mjs → chunk-FO5GXCST.mjs} +24 -10
  14. package/dist/{chunk-CJTCU755.mjs → chunk-FZEH2IXZ.mjs} +95 -15
  15. package/dist/{chunk-RNDUFTBF.mjs → chunk-GSZADJMW.mjs} +233 -77
  16. package/dist/chunk-ITFD3JZS.mjs +1137 -0
  17. package/dist/chunk-K7V7X6WU.mjs +207 -0
  18. package/dist/chunk-KMLXOPBQ.mjs +20 -0
  19. package/dist/chunk-LBT47HLZ.mjs +19 -0
  20. package/dist/{chunk-IQJQ4RF5.mjs → chunk-NGSYOVQ4.mjs} +12 -1
  21. package/dist/chunk-NIX5F4LT.mjs +863 -0
  22. package/dist/chunk-NPSQNCOY.mjs +590 -0
  23. package/dist/chunk-P3YIN7G7.mjs +26 -0
  24. package/dist/chunk-PAJ4JJPV.mjs +9827 -0
  25. package/dist/{chunk-VAI7IUI4.mjs → chunk-PDZ5Z5ZE.mjs} +9 -6
  26. package/dist/chunk-QKWEZXR3.mjs +52 -0
  27. package/dist/chunk-SSO5RFOX.mjs +7 -0
  28. package/dist/{chunk-ZJ4W7P33.mjs → chunk-T7IL7V7Q.mjs} +11 -9
  29. package/dist/{chunk-VKQ3KUYO.mjs → chunk-TANCKTDK.mjs} +9 -4
  30. package/dist/{chunk-FV32AQTC.mjs → chunk-TEFOQXBK.mjs} +10 -6
  31. package/dist/{chunk-W6R4UJUQ.mjs → chunk-UK5GJO4Y.mjs} +15 -9
  32. package/dist/{chunk-BKLW2SPG.mjs → chunk-V4F3SYKW.mjs} +19 -10
  33. package/dist/{chunk-H3K4GA2P.mjs → chunk-WDOHRV3D.mjs} +31 -19
  34. package/dist/chunk-XBQDIKB5.mjs +18 -0
  35. package/dist/chunk-XUCWSYVS.mjs +148 -0
  36. package/dist/khala.mjs +20 -13
  37. package/dist/web/assets/local-CFzRHZpB.js +9 -0
  38. package/dist/web/assets/{local-OwDq47O4.css → local-ciypUFoK.css} +1 -1
  39. package/dist/web/index.html +2 -2
  40. package/package.json +5 -3
  41. package/dist/chunk-5BQTYU66.mjs +0 -886
  42. package/dist/chunk-5GLTKWJ5.mjs +0 -1
  43. package/dist/chunk-5PUQT3YS.mjs +0 -569
  44. package/dist/chunk-7HBRFY3Z.mjs +0 -8
  45. package/dist/chunk-CBPLCCRZ.mjs +0 -191
  46. package/dist/chunk-FH34B3I3.mjs +0 -235
  47. package/dist/chunk-NKX3SLS7.mjs +0 -227
  48. package/dist/web/assets/local-BQl2KrE1.js +0 -9
@@ -0,0 +1,207 @@
1
+ import { createRequire as __khalaCreateRequire } from 'node:module'; const require = __khalaCreateRequire(import.meta.url);
2
+ import {
3
+ init_define_KHALA_BUNDLE
4
+ } from "./chunk-7R3EIDGR.mjs";
5
+
6
+ // src/wake/shared/nonce.ts
7
+ init_define_KHALA_BUNDLE();
8
+ import * as fs2 from "node:fs/promises";
9
+ import path2 from "node:path";
10
+ import { randomBytes as randomBytes3 } from "node:crypto";
11
+
12
+ // src/wake/shared/rules.ts
13
+ init_define_KHALA_BUNDLE();
14
+ import { randomBytes } from "node:crypto";
15
+ var CODEX_IDLE_WAKE_NOTICE = "Khala: channel messages are waiting. Continue.";
16
+ function newNonce() {
17
+ return randomBytes(4).toString("hex");
18
+ }
19
+ function wakeLine(nonce) {
20
+ if (!/^[a-f0-9]{8}$/.test(nonce)) throw new Error("invalid_wake_nonce");
21
+ return `${CODEX_IDLE_WAKE_NOTICE} (k-${nonce})`;
22
+ }
23
+
24
+ // src/wake/shared/lock.ts
25
+ init_define_KHALA_BUNDLE();
26
+ import * as fs from "node:fs/promises";
27
+ import path from "node:path";
28
+ import { randomBytes as randomBytes2 } from "node:crypto";
29
+ async function clearAbandonedLock(lock) {
30
+ let entries;
31
+ try {
32
+ entries = await fs.readdir(lock);
33
+ } catch (error) {
34
+ if (error.code === "ENOENT") return;
35
+ throw error;
36
+ }
37
+ for (const entry of entries) {
38
+ const owner = /^owner-(\d+)-[a-f0-9]{16}$/.exec(entry);
39
+ if (!owner) continue;
40
+ try {
41
+ process.kill(Number(owner[1]), 0);
42
+ } catch (error) {
43
+ if (error.code !== "ESRCH") continue;
44
+ await fs.rm(path.join(lock, entry), { force: true });
45
+ }
46
+ }
47
+ try {
48
+ await fs.rmdir(lock);
49
+ } catch (error) {
50
+ if (!["ENOENT", "ENOTEMPTY", "EEXIST"].includes(error.code ?? "")) throw error;
51
+ }
52
+ }
53
+ async function withWakeLock(dir, name, work) {
54
+ await fs.mkdir(dir, { recursive: true, mode: 448 });
55
+ const lock = path.join(dir, name);
56
+ const owner = `owner-${process.pid}-${randomBytes2(8).toString("hex")}`;
57
+ const candidate = path.join(dir, `.${owner}`);
58
+ await fs.mkdir(candidate, { mode: 448 });
59
+ await fs.writeFile(path.join(candidate, owner), "", { mode: 384 });
60
+ let acquired = false;
61
+ try {
62
+ for (let tries = 0; tries < 200; tries++) {
63
+ try {
64
+ await fs.rename(candidate, lock);
65
+ acquired = true;
66
+ break;
67
+ } catch (error) {
68
+ if (!["EEXIST", "ENOTEMPTY", "EPERM", "EACCES"].includes(error.code ?? "")) throw error;
69
+ }
70
+ await clearAbandonedLock(lock);
71
+ await new Promise((resolve) => setTimeout(resolve, 10));
72
+ }
73
+ } finally {
74
+ if (!acquired) await fs.rm(candidate, { recursive: true, force: true });
75
+ }
76
+ if (!acquired) throw new Error("wake_state_locked");
77
+ try {
78
+ return await work();
79
+ } finally {
80
+ await fs.rm(path.join(lock, owner), { force: true });
81
+ try {
82
+ await fs.rmdir(lock);
83
+ } catch (error) {
84
+ if (!["ENOENT", "ENOTEMPTY", "EEXIST"].includes(error.code ?? "")) throw error;
85
+ }
86
+ }
87
+ }
88
+
89
+ // src/wake/shared/nonce.ts
90
+ async function readJson(file, fallback) {
91
+ try {
92
+ return JSON.parse(await fs2.readFile(file, "utf8"));
93
+ } catch (error) {
94
+ if (error.code === "ENOENT") return fallback;
95
+ throw error;
96
+ }
97
+ }
98
+ async function atomic(file, value) {
99
+ const temp = `${file}.${randomBytes3(8).toString("hex")}.tmp`;
100
+ try {
101
+ await fs2.writeFile(temp, JSON.stringify(value), { mode: 384 });
102
+ await fs2.rename(temp, file);
103
+ } finally {
104
+ await fs2.rm(temp, { force: true });
105
+ }
106
+ }
107
+ async function locked(dir, work) {
108
+ return withWakeLock(dir, "wake.lock", async () => {
109
+ const data = await readJson(path2.join(dir, "wake-journal.json"), { attempts: [], state: {} });
110
+ const before = JSON.stringify(data);
111
+ const result = work(data);
112
+ if (JSON.stringify(data) === before) return result;
113
+ await atomic(path2.join(dir, "wake-journal.json"), data);
114
+ await atomic(path2.join(dir, "wake-state.json"), data.state);
115
+ return result;
116
+ });
117
+ }
118
+ async function readWakeState(dir) {
119
+ return (await readJson(path2.join(dir, "wake-journal.json"), { attempts: [], state: {} })).state;
120
+ }
121
+ async function recordAttempt(dir, attempt) {
122
+ wakeLine(attempt.nonce);
123
+ if (!attempt.driver || ["__proto__", "constructor", "prototype"].includes(attempt.driver) || !Number.isFinite(attempt.at) || !Number.isFinite(attempt.deadline) || attempt.deadline <= attempt.at) throw new Error("invalid_wake_attempt");
124
+ await locked(dir, (data) => {
125
+ if (data.attempts.some((item) => item.nonce === attempt.nonce)) throw new Error("duplicate_wake_nonce");
126
+ data.attempts.push(attempt);
127
+ });
128
+ }
129
+ async function settleAttempts(dir, input) {
130
+ const snapshot = await readJson(path2.join(dir, "wake-journal.json"), { attempts: [], state: {} });
131
+ if (!snapshot.attempts.length) return [];
132
+ return locked(dir, (data) => {
133
+ const results = [];
134
+ const remaining = [];
135
+ const activityAt = typeof input.activity?.updatedAt === "number" ? input.activity.updatedAt : Date.parse(input.activity?.updatedAt ?? "");
136
+ for (const attempt of data.attempts) {
137
+ let status;
138
+ const baselineAt = typeof attempt.activityUpdatedAt === "number" ? attempt.activityUpdatedAt : attempt.activityUpdatedAt === void 0 ? attempt.at : Date.parse(attempt.activityUpdatedAt);
139
+ const verified = input.promptText?.includes(`(k-${attempt.nonce})`);
140
+ const activityChanged = input.activity && (input.activity.state !== "idle" || activityAt > baselineAt);
141
+ if (verified && input.verifiedAt !== void 0 && input.verifiedAt >= attempt.at && input.verifiedAt <= attempt.deadline) status = "success";
142
+ else if (activityChanged && !(attempt.startsActivity && input.activity?.state === "busy" && input.promptText === void 0)) status = "void";
143
+ else if (verified && input.verifiedAt === void 0 && input.now <= attempt.deadline) status = "success";
144
+ else if (input.now >= attempt.deadline && input.activity?.state === "idle") status = "failure";
145
+ else if (input.promptText !== void 0) status = "void";
146
+ if (!status) {
147
+ remaining.push(attempt);
148
+ continue;
149
+ }
150
+ results.push({ nonce: attempt.nonce, driver: attempt.driver, status });
151
+ if (status === "void") continue;
152
+ const prior = data.state[attempt.driver] ?? { failures: 0 };
153
+ if (status === "success") data.state[attempt.driver] = { failures: 0 };
154
+ else {
155
+ const failures = prior.failures + 1;
156
+ data.state[attempt.driver] = failures >= 2 ? { ...prior, failures, disabled: true, reason: "nonce_timeout", at: new Date(input.now).toISOString() } : { failures };
157
+ }
158
+ }
159
+ data.attempts = remaining;
160
+ return results;
161
+ });
162
+ }
163
+ async function cancelAttempt(dir, nonce) {
164
+ await locked(dir, (data) => {
165
+ data.attempts = data.attempts.filter((attempt) => attempt.nonce !== nonce);
166
+ });
167
+ }
168
+ async function failAttempt(dir, nonce, now) {
169
+ await locked(dir, (data) => {
170
+ const attempt = data.attempts.find((item) => item.nonce === nonce);
171
+ if (!attempt) return;
172
+ data.attempts = data.attempts.filter((item) => item.nonce !== nonce);
173
+ const prior = data.state[attempt.driver] ?? { failures: 0 };
174
+ const failures = prior.failures + 1;
175
+ data.state[attempt.driver] = failures >= 2 ? { ...prior, failures, disabled: true, reason: "nonce_timeout", at: new Date(now).toISOString() } : { failures };
176
+ });
177
+ }
178
+ async function resetWakeDriver(dir, driver) {
179
+ await locked(dir, (data) => {
180
+ delete data.state[driver];
181
+ data.attempts = data.attempts.filter((attempt) => attempt.driver !== driver);
182
+ });
183
+ }
184
+ async function takeWakeDisableNotices(dir) {
185
+ return locked(dir, (data) => {
186
+ const drivers = [];
187
+ for (const [driver, state] of Object.entries(data.state)) {
188
+ if (!state.disabled || state.noticeShown) continue;
189
+ state.noticeShown = true;
190
+ drivers.push(driver);
191
+ }
192
+ return drivers;
193
+ });
194
+ }
195
+
196
+ export {
197
+ newNonce,
198
+ wakeLine,
199
+ withWakeLock,
200
+ readWakeState,
201
+ recordAttempt,
202
+ settleAttempts,
203
+ cancelAttempt,
204
+ failAttempt,
205
+ resetWakeDriver,
206
+ takeWakeDisableNotices
207
+ };
@@ -0,0 +1,20 @@
1
+ import { createRequire as __khalaCreateRequire } from 'node:module'; const require = __khalaCreateRequire(import.meta.url);
2
+ import {
3
+ cancelAttempt,
4
+ failAttempt,
5
+ readWakeState,
6
+ recordAttempt,
7
+ resetWakeDriver,
8
+ settleAttempts,
9
+ takeWakeDisableNotices
10
+ } from "./chunk-K7V7X6WU.mjs";
11
+ import "./chunk-7R3EIDGR.mjs";
12
+ export {
13
+ cancelAttempt,
14
+ failAttempt,
15
+ readWakeState,
16
+ recordAttempt,
17
+ resetWakeDriver,
18
+ settleAttempts,
19
+ takeWakeDisableNotices
20
+ };
@@ -0,0 +1,19 @@
1
+ import { createRequire as __khalaCreateRequire } from 'node:module'; const require = __khalaCreateRequire(import.meta.url);
2
+ import {
3
+ resolveHarness,
4
+ resolveSession,
5
+ resolveSessionId
6
+ } from "./chunk-6BO3JSUP.mjs";
7
+ import "./chunk-ITFD3JZS.mjs";
8
+ import "./chunk-QKWEZXR3.mjs";
9
+ import "./chunk-NIX5F4LT.mjs";
10
+ import "./chunk-NGSYOVQ4.mjs";
11
+ import "./chunk-TANCKTDK.mjs";
12
+ import "./chunk-NPSQNCOY.mjs";
13
+ import "./chunk-K7V7X6WU.mjs";
14
+ import "./chunk-7R3EIDGR.mjs";
15
+ export {
16
+ resolveHarness,
17
+ resolveSession,
18
+ resolveSessionId
19
+ };
@@ -5,9 +5,19 @@ import {
5
5
  object,
6
6
  readMatrixUserId,
7
7
  version
8
- } from "./chunk-NKX3SLS7.mjs";
8
+ } from "./chunk-NPSQNCOY.mjs";
9
+ import {
10
+ init_define_KHALA_BUNDLE
11
+ } from "./chunk-7R3EIDGR.mjs";
12
+
13
+ // ../contracts/src/m1/listening-mode.ts
14
+ init_define_KHALA_BUNDLE();
15
+
16
+ // ../contracts/src/delivery/listening-mode.ts
17
+ init_define_KHALA_BUNDLE();
9
18
 
10
19
  // ../contracts/src/delivery/decode.ts
20
+ init_define_KHALA_BUNDLE();
11
21
  var MAX_IDENTIFIER_BYTES = 512;
12
22
  var DecodeFailure = class extends Error {
13
23
  code;
@@ -67,6 +77,7 @@ function literal2(input, field, allowed) {
67
77
  }
68
78
 
69
79
  // ../contracts/src/delivery/ids.ts
80
+ init_define_KHALA_BUNDLE();
70
81
  function readId(input, field) {
71
82
  return identifier(input, field);
72
83
  }