stitchkit 0.103.8 → 0.103.10

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 (79) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/agent-runtime/sandbox-process.d.ts +2 -2
  3. package/dist/agent-runtime/sandbox-process.d.ts.map +1 -1
  4. package/dist/agent-runtime/store-conformance-lifecycle.d.ts +10 -10
  5. package/dist/durability/contract.d.ts +3 -2
  6. package/dist/durability/contract.d.ts.map +1 -1
  7. package/dist/durability/effect.d.ts.map +1 -1
  8. package/dist/durability/engine.d.ts.map +1 -1
  9. package/dist/durability/json.d.ts.map +1 -1
  10. package/dist/entrypoints/agent-runtime/harness.js +2 -2
  11. package/dist/entrypoints/agent-runtime/sandbox.js +29 -81
  12. package/dist/entrypoints/agent-runtime.js +2 -2
  13. package/dist/entrypoints/application/diagnostic-journal.js +2 -1
  14. package/dist/entrypoints/cli.js +1 -1
  15. package/dist/entrypoints/files.d.ts +2 -0
  16. package/dist/entrypoints/files.d.ts.map +1 -1
  17. package/dist/entrypoints/files.js +14 -6
  18. package/dist/entrypoints/primitives.js +77 -57
  19. package/dist/entrypoints/process.d.ts +5 -0
  20. package/dist/entrypoints/process.d.ts.map +1 -0
  21. package/dist/entrypoints/process.js +20 -0
  22. package/dist/entrypoints/tools.js +12 -3
  23. package/dist/files/boundary.d.ts +19 -1
  24. package/dist/files/boundary.d.ts.map +1 -1
  25. package/dist/files/file-io.d.ts +5 -1
  26. package/dist/files/file-io.d.ts.map +1 -1
  27. package/dist/files/read.d.ts +13 -0
  28. package/dist/files/read.d.ts.map +1 -0
  29. package/dist/index-12m2v2a9.js +98 -0
  30. package/dist/index-6m6a9ty4.js +285 -0
  31. package/dist/index-8a9ksm91.js +95 -0
  32. package/dist/{index-90ppdx3q.js → index-9jb61j5a.js} +1 -1
  33. package/dist/{index-5vyd0fkb.js → index-ary5s7k0.js} +40 -61
  34. package/dist/index-c1jmtxa0.js +74 -0
  35. package/dist/index-ga597cvy.js +184 -0
  36. package/dist/index-mbnnw3f2.js +25 -0
  37. package/dist/index-nybn2err.js +213 -0
  38. package/dist/{index-v4a2fjtk.js → index-sybedc8b.js} +124 -55
  39. package/dist/internal/abort-race.d.ts +3 -0
  40. package/dist/internal/abort-race.d.ts.map +1 -0
  41. package/dist/internal/atomic-file.d.ts +20 -2
  42. package/dist/internal/atomic-file.d.ts.map +1 -1
  43. package/dist/internal/atomic-publication.d.ts +19 -0
  44. package/dist/internal/atomic-publication.d.ts.map +1 -0
  45. package/dist/internal/exclusive-lock.d.ts +2 -1
  46. package/dist/internal/exclusive-lock.d.ts.map +1 -1
  47. package/dist/internal/json-data.d.ts +8 -0
  48. package/dist/internal/json-data.d.ts.map +1 -0
  49. package/dist/internal/process-instance.d.ts +46 -9
  50. package/dist/internal/process-instance.d.ts.map +1 -1
  51. package/dist/internal/with-exclusive-lock.d.ts +4 -3
  52. package/dist/internal/with-exclusive-lock.d.ts.map +1 -1
  53. package/dist/primitives/canonical-json.d.ts +11 -0
  54. package/dist/primitives/canonical-json.d.ts.map +1 -0
  55. package/dist/primitives/index.d.ts +1 -0
  56. package/dist/primitives/index.d.ts.map +1 -1
  57. package/dist/process/command-owner.d.ts +13 -0
  58. package/dist/process/command-owner.d.ts.map +1 -0
  59. package/dist/process/command.d.ts +3 -0
  60. package/dist/process/command.d.ts.map +1 -0
  61. package/dist/process/contract.d.ts +45 -0
  62. package/dist/process/contract.d.ts.map +1 -0
  63. package/dist/process/group.d.ts +6 -0
  64. package/dist/process/group.d.ts.map +1 -0
  65. package/dist/process/leader-settlement.d.ts +4 -0
  66. package/dist/process/leader-settlement.d.ts.map +1 -0
  67. package/dist/tools/transfer/managed-file-error.d.ts.map +1 -1
  68. package/llms/files.txt +9 -1
  69. package/llms/primitives.txt +31 -1
  70. package/llms/process.txt +242 -0
  71. package/llms/stitchkit.txt +1 -0
  72. package/llms/tools.txt +18 -0
  73. package/llms-full.txt +292 -2
  74. package/llms.txt +6 -4
  75. package/native/darwin-arm64.node +0 -0
  76. package/native/darwin-x64.node +0 -0
  77. package/package.json +5 -1
  78. package/dist/index-7kxw1awn.js +0 -328
  79. package/dist/index-aan6hqb9.js +0 -65
@@ -1,328 +0,0 @@
1
- import {
2
- loadDarwinBinding
3
- } from "./index-d4pa2bgs.js";
4
- import {
5
- isRecord
6
- } from "./index-9jfdm5tg.js";
7
-
8
- // src/internal/exclusive-lock.ts
9
- import { constants } from "node:fs";
10
- import { lstat, open, unlink } from "node:fs/promises";
11
- import { hostname } from "node:os";
12
- import { z as z2 } from "zod";
13
-
14
- // src/internal/process-identity.ts
15
- import { execFile } from "node:child_process";
16
- import { readFile } from "node:fs/promises";
17
- import { platform } from "node:os";
18
- import { promisify } from "node:util";
19
- var detectedIdentity;
20
- async function firstReadableLine(paths) {
21
- for (const path of paths) {
22
- try {
23
- const text = (await readFile(path, "utf8")).trim();
24
- if (text.length > 0)
25
- return text;
26
- } catch {}
27
- }
28
- return null;
29
- }
30
- var run = promisify(execFile);
31
- function parsePlatformUuid(dump) {
32
- return /"IOPlatformUUID"\s*=\s*"([^"]+)"/.exec(dump)?.[1] ?? null;
33
- }
34
- async function readDarwinPlatformUuid(command = "ioreg") {
35
- try {
36
- const { stdout } = await run(command, ["-rd1", "-c", "IOPlatformExpertDevice"], {
37
- timeout: 2000,
38
- maxBuffer: 1 << 20,
39
- encoding: "utf8"
40
- });
41
- return parsePlatformUuid(stdout);
42
- } catch {
43
- return null;
44
- }
45
- }
46
- async function machineIdentity(declared) {
47
- if (declared !== undefined)
48
- return declared;
49
- if (detectedIdentity !== undefined)
50
- return detectedIdentity;
51
- detectedIdentity = platform() === "darwin" ? await readDarwinPlatformUuid() : await firstReadableLine(["/etc/machine-id", "/var/lib/dbus/machine-id"]);
52
- return detectedIdentity;
53
- }
54
- async function probeLiveness(pid, reach = (target) => process.kill(target, 0)) {
55
- try {
56
- reach(pid);
57
- } catch (error) {
58
- return isRecord(error) && error.code === "ESRCH" ? "gone" : "alive";
59
- }
60
- return await isZombieProcess(pid) ? "gone" : "alive";
61
- }
62
- async function isZombieProcess(pid, seam = {}) {
63
- const { procRoot = "/proc", psCommand = "ps" } = seam;
64
- try {
65
- const stat = await readFile(`${procRoot}/${pid}/stat`, "utf8");
66
- return stat.slice(stat.lastIndexOf(")") + 1).trimStart().startsWith("Z");
67
- } catch {}
68
- try {
69
- const { stdout } = await run(psCommand, ["-o", "state=", "-p", String(pid)], {
70
- timeout: 2000,
71
- maxBuffer: 1 << 16,
72
- encoding: "utf8"
73
- });
74
- return stdout.trim().startsWith("Z");
75
- } catch {
76
- return false;
77
- }
78
- }
79
-
80
- // src/internal/process-instance.ts
81
- import { readFile as readFile2, readlink } from "node:fs/promises";
82
- import { platform as platform2 } from "node:os";
83
- import { z } from "zod";
84
- var ProcessInstanceSchema = z.object({
85
- platform: z.enum(["linux", "darwin"]),
86
- bootId: z.string().min(1),
87
- namespace: z.string().min(1),
88
- startId: z.string().regex(/^\d+$/)
89
- }).strict();
90
- function linuxProcessStart(stat, pid) {
91
- if (!stat.startsWith(`${pid} (`))
92
- return null;
93
- const end = stat.lastIndexOf(")");
94
- if (end < 0)
95
- return null;
96
- const start = stat.slice(end + 1).trim().split(/\s+/)[19];
97
- return start !== undefined && /^\d+$/.test(start) ? start : null;
98
- }
99
- async function readProcessInstance(pid, procRoot = "/proc") {
100
- try {
101
- if (platform2() === "darwin") {
102
- const result = ProcessInstanceSchema.safeParse(loadDarwinBinding().processIdentity(pid));
103
- return result.success ? result.data : null;
104
- }
105
- if (platform2() !== "linux")
106
- return null;
107
- const [bootId, namespace, ownStatus, timeNamespace, ownStat, stat] = await Promise.all([
108
- readFile2(`${procRoot}/sys/kernel/random/boot_id`, "utf8"),
109
- readlink(`${procRoot}/self/ns/pid`),
110
- readFile2(`${procRoot}/self/status`, "utf8"),
111
- readlink(`${procRoot}/self/ns/time`).catch((error) => {
112
- if (error instanceof Error && "code" in error && error.code === "ENOENT")
113
- return "";
114
- throw error;
115
- }),
116
- readFile2(`${procRoot}/self/stat`, "utf8"),
117
- readFile2(`${procRoot}/${pid}/stat`, "utf8")
118
- ]);
119
- const namespacePids = ownStatus.match(/^NStgid:[ \t]*(.*)$/m)?.[1]?.trim().split(/\s+/);
120
- if (namespacePids?.length !== 1 || namespacePids[0] !== String(process.pid) || linuxProcessStart(ownStat, process.pid) === null)
121
- return null;
122
- const startId = linuxProcessStart(stat, pid);
123
- if (startId === null || !bootId.trim())
124
- return null;
125
- return {
126
- platform: "linux",
127
- bootId: bootId.trim(),
128
- namespace: `${namespace};${timeNamespace}`,
129
- startId
130
- };
131
- } catch {
132
- return null;
133
- }
134
- }
135
- async function probeProcessOwner(pid, recorded, probes = { read: readProcessInstance, liveness: probeLiveness }) {
136
- if (recorded === undefined)
137
- return { liveness: await probes.liveness(pid), identity: "legacy" };
138
- const unavailable = {
139
- liveness: "not-probed",
140
- identity: "unavailable"
141
- };
142
- if (recorded === null)
143
- return unavailable;
144
- const current = await probes.read(process.pid);
145
- if (!current || recorded.platform !== current.platform)
146
- return unavailable;
147
- if (recorded.bootId !== current.bootId)
148
- return { liveness: "gone", identity: "different-boot" };
149
- if (recorded.namespace !== current.namespace)
150
- return unavailable;
151
- const liveness = await probes.liveness(pid);
152
- if (liveness === "gone")
153
- return { liveness, identity: "pid-gone" };
154
- const observed = await probes.read(pid);
155
- if (!observed || observed.bootId !== current.bootId || observed.namespace !== current.namespace)
156
- return unavailable;
157
- if (recorded.startId !== observed.startId)
158
- return { liveness: "gone", identity: "reused-pid" };
159
- return { liveness: "alive", identity: "matched" };
160
- }
161
-
162
- // src/internal/exclusive-lock.ts
163
- var ExclusiveLockOwnerSchema = z2.object({
164
- pid: z2.number().int().positive().max(Number.MAX_SAFE_INTEGER),
165
- host: z2.string().min(1),
166
- acquiredAt: z2.string(),
167
- machine: z2.string().min(1).optional().catch(undefined),
168
- process: ProcessInstanceSchema.nullable().optional().catch(null)
169
- });
170
- function isCode(error, code) {
171
- return isRecord(error) && error.code === code;
172
- }
173
- function readOwner(text) {
174
- let parsed;
175
- try {
176
- parsed = JSON.parse(text);
177
- } catch {
178
- return;
179
- }
180
- const owner = ExclusiveLockOwnerSchema.safeParse(parsed);
181
- return owner.success ? owner.data : undefined;
182
- }
183
- function attribute(owner, identity) {
184
- if (identity !== null && owner.machine !== undefined) {
185
- return owner.machine === identity ? "this-machine" : "another-machine";
186
- }
187
- return owner.host === hostname() ? "this-machine" : "unattributable";
188
- }
189
- async function diagnose(owner, declaredIdentity) {
190
- if (!owner)
191
- return { attribution: "unattributable", liveness: "not-probed", owner: null };
192
- const attribution = attribute(owner, await machineIdentity(declaredIdentity));
193
- if (attribution !== "this-machine")
194
- return { attribution, liveness: "not-probed", owner };
195
- return { attribution, ...await probeProcessOwner(owner.pid, owner.process), owner };
196
- }
197
- async function readLockFile(path) {
198
- let handle;
199
- try {
200
- handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
201
- const info = await handle.stat();
202
- return {
203
- owner: readOwner(await handle.readFile("utf8")),
204
- ino: info.ino,
205
- mtimeMs: info.mtimeMs
206
- };
207
- } catch {
208
- return;
209
- } finally {
210
- await handle?.close().catch(() => {
211
- return;
212
- });
213
- }
214
- }
215
- async function unlinkIfSame(path, ino) {
216
- try {
217
- if ((await lstat(path)).ino !== ino)
218
- return;
219
- await unlink(path);
220
- } catch (error) {
221
- if (!isCode(error, "ENOENT"))
222
- throw error;
223
- }
224
- }
225
- async function createOwned(path, options, reclaimed) {
226
- const identity = await machineIdentity(options.machineIdentity);
227
- const instance = await readProcessInstance(process.pid);
228
- const handle = await open(path, "wx", options.mode);
229
- const owner = {
230
- pid: process.pid,
231
- process: instance,
232
- host: hostname(),
233
- acquiredAt: new Date().toISOString(),
234
- ...identity !== null && { machine: identity }
235
- };
236
- let ino;
237
- try {
238
- await handle.writeFile(`${JSON.stringify(owner)}
239
- `, "utf8");
240
- ino = (await handle.stat()).ino;
241
- } catch (error) {
242
- await handle.close().catch(() => {
243
- return;
244
- });
245
- await unlink(path).catch(() => {
246
- return;
247
- });
248
- throw error;
249
- }
250
- let released = false;
251
- return {
252
- path,
253
- owner,
254
- reclaimed,
255
- async release() {
256
- if (released)
257
- return;
258
- released = true;
259
- try {
260
- await handle.close();
261
- } finally {
262
- await unlinkIfSame(path, ino);
263
- }
264
- }
265
- };
266
- }
267
- function reclaimable(state, diagnosis, graceMs) {
268
- if (state.owner)
269
- return diagnosis.liveness === "gone";
270
- return graceMs !== undefined && Date.now() - state.mtimeMs >= graceMs;
271
- }
272
- async function reclaimUnderGuard(path, options) {
273
- const guardPath = `${path}.reclaim`;
274
- let guard;
275
- try {
276
- guard = await createOwned(guardPath, options, false);
277
- } catch (error) {
278
- if (!isCode(error, "EEXIST"))
279
- throw error;
280
- const stale = await readLockFile(guardPath);
281
- if (stale) {
282
- const verdict = await diagnose(stale.owner, options.machineIdentity);
283
- if (reclaimable(stale, verdict, options.ownerlessGraceMs ?? 5000)) {
284
- await unlinkIfSame(guardPath, stale.ino);
285
- }
286
- }
287
- return;
288
- }
289
- try {
290
- const state = await readLockFile(path);
291
- if (state) {
292
- const diagnosis = await diagnose(state.owner, options.machineIdentity);
293
- if (!reclaimable(state, diagnosis, options.ownerlessGraceMs))
294
- return "refused";
295
- await unlinkIfSame(path, state.ino);
296
- }
297
- return await createOwned(path, options, state !== undefined).catch((error) => {
298
- if (isCode(error, "EEXIST"))
299
- return "refused";
300
- throw error;
301
- });
302
- } finally {
303
- await guard.release().catch(() => {
304
- return;
305
- });
306
- }
307
- }
308
- async function attemptExclusiveLock(path, options) {
309
- try {
310
- return { held: await createOwned(path, options, false) };
311
- } catch (error) {
312
- if (!isCode(error, "EEXIST"))
313
- throw error;
314
- if (!options.reclaim)
315
- return { error };
316
- const state = await readLockFile(path);
317
- const diagnosis = await diagnose(state?.owner, options.machineIdentity);
318
- if (!state || !reclaimable(state, diagnosis, options.ownerlessGraceMs)) {
319
- return { error, diagnosis };
320
- }
321
- const reclaimed = await reclaimUnderGuard(path, options);
322
- if (reclaimed !== undefined && reclaimed !== "refused")
323
- return { held: reclaimed };
324
- return { error, diagnosis };
325
- }
326
- }
327
-
328
- export { attemptExclusiveLock };
@@ -1,65 +0,0 @@
1
- // src/internal/atomic-file.ts
2
- import { randomBytes } from "node:crypto";
3
- import {
4
- closeSync,
5
- fchmodSync,
6
- fsyncSync,
7
- openSync,
8
- renameSync,
9
- unlinkSync,
10
- writeSync
11
- } from "node:fs";
12
- import { open, rename, unlink } from "node:fs/promises";
13
- import { basename, dirname, join } from "node:path";
14
- var DEFAULT_MODE = 384;
15
- function stagingPath(target) {
16
- return join(dirname(target), `.${basename(target)}.${randomBytes(12).toString("hex")}.tmp`);
17
- }
18
- function bytesOf(data) {
19
- return typeof data === "string" ? new TextEncoder().encode(data) : data;
20
- }
21
- async function writeFileAtomic2(target, data, options = {}) {
22
- const mode = options.mode ?? DEFAULT_MODE;
23
- const staged = stagingPath(target);
24
- const handle = await open(staged, "wx", mode);
25
- try {
26
- try {
27
- await handle.writeFile(bytesOf(data));
28
- await handle.chmod(mode);
29
- await handle.sync();
30
- } finally {
31
- await handle.close();
32
- }
33
- await rename(staged, target);
34
- } catch (error) {
35
- await unlink(staged).catch(() => {
36
- return;
37
- });
38
- throw error;
39
- }
40
- }
41
- function writeFileAtomicSync2(target, data, options = {}) {
42
- const mode = options.mode ?? DEFAULT_MODE;
43
- const bytes = bytesOf(data);
44
- const staged = stagingPath(target);
45
- const handle = openSync(staged, "wx", mode);
46
- try {
47
- try {
48
- for (let offset = 0;offset < bytes.byteLength; ) {
49
- offset += writeSync(handle, bytes, offset, bytes.byteLength - offset);
50
- }
51
- fchmodSync(handle, mode);
52
- fsyncSync(handle);
53
- } finally {
54
- closeSync(handle);
55
- }
56
- renameSync(staged, target);
57
- } catch (error) {
58
- try {
59
- unlinkSync(staged);
60
- } catch {}
61
- throw error;
62
- }
63
- }
64
-
65
- export { writeFileAtomic2, writeFileAtomicSync2 };