@cortexkit/opencode-magic-context 0.49.52 → 0.49.54
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/dist/auto-search-worker.js +3 -3
- package/dist/chunk-embedding-write-worker.js +3 -4
- package/dist/{context-bkkd7br4.js → context-vx5gw5h2.js} +117 -80
- package/dist/{embedding-backfill-3wdjzrvs.js → embedding-backfill-2dvf3rcn.js} +4 -4
- package/dist/embedding-worker.js +2 -2
- package/dist/features/magic-context/compartment-embedding.d.ts.map +1 -1
- package/dist/features/magic-context/message-fts-session-filter.d.ts +1 -1
- package/dist/features/magic-context/message-fts-session-filter.d.ts.map +1 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts +8 -0
- package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
- package/dist/features/magic-context/search.d.ts.map +1 -1
- package/dist/features/magic-context/storage-host-instruction-retention.d.ts +10 -3
- package/dist/features/magic-context/storage-host-instruction-retention.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-persisted.d.ts +19 -0
- package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta.d.ts +1 -1
- package/dist/features/magic-context/storage-meta.d.ts.map +1 -1
- package/dist/features/magic-context/storage.d.ts +1 -1
- package/dist/features/magic-context/storage.d.ts.map +1 -1
- package/dist/features/magic-context/transform-decision-log.d.ts +1 -1
- package/dist/features/magic-context/transform-decision-log.d.ts.map +1 -1
- package/dist/hooks/magic-context/active-anthropic-turn.d.ts +6 -0
- package/dist/hooks/magic-context/active-anthropic-turn.d.ts.map +1 -1
- package/dist/hooks/magic-context/compaction-marker-manager.d.ts +2 -1
- package/dist/hooks/magic-context/compaction-marker-manager.d.ts.map +1 -1
- package/dist/hooks/magic-context/emergency-drop.d.ts +75 -2
- package/dist/hooks/magic-context/emergency-drop.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-handler.d.ts.map +1 -1
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts +14 -0
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
- package/dist/hooks/magic-context/heuristic-cleanup.d.ts +12 -0
- package/dist/hooks/magic-context/heuristic-cleanup.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook.d.ts.map +1 -1
- package/dist/hooks/magic-context/host-served-rows.d.ts +12 -2
- package/dist/hooks/magic-context/host-served-rows.d.ts.map +1 -1
- package/dist/hooks/magic-context/inject-compartments.d.ts +10 -0
- package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
- package/dist/hooks/magic-context/latest-assistant-turn.d.ts +43 -0
- package/dist/hooks/magic-context/latest-assistant-turn.d.ts.map +1 -1
- package/dist/hooks/magic-context/latest-thinking-recovery.d.ts +102 -26
- package/dist/hooks/magic-context/latest-thinking-recovery.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-recovery-prefix.d.ts +18 -2
- package/dist/hooks/magic-context/lkg-recovery-prefix.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-slot.d.ts.map +1 -1
- package/dist/hooks/magic-context/pass-outcome.d.ts.map +1 -1
- package/dist/hooks/magic-context/pending-marker-rearm.d.ts +20 -0
- package/dist/hooks/magic-context/pending-marker-rearm.d.ts.map +1 -0
- package/dist/hooks/magic-context/priced-pass-sizing.d.ts +7 -0
- package/dist/hooks/magic-context/priced-pass-sizing.d.ts.map +1 -1
- package/dist/hooks/magic-context/routine-cleanup-decision.d.ts +11 -1
- package/dist/hooks/magic-context/routine-cleanup-decision.d.ts.map +1 -1
- package/dist/hooks/magic-context/rust-mode-transform.d.ts.map +1 -1
- package/dist/hooks/magic-context/served-check.d.ts +127 -0
- package/dist/hooks/magic-context/served-check.d.ts.map +1 -0
- package/dist/hooks/magic-context/signed-thinking-hold.d.ts +32 -4
- package/dist/hooks/magic-context/signed-thinking-hold.d.ts.map +1 -1
- package/dist/hooks/magic-context/tool-drop-target.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +35 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/index-29ff546v.js +2747 -0
- package/dist/{index-8d7gkgjs.js → index-2vsd5064.js} +3 -3
- package/dist/{index-kaqs6h31.js → index-52x68mj4.js} +1 -1
- package/dist/{index-k2efzh9r.js → index-567xsm72.js} +7 -5
- package/dist/{index-q9m97m5g.js → index-5gqszjq9.js} +4 -4
- package/dist/{index-a4zmmhxs.js → index-5ptp2de1.js} +6 -8
- package/dist/{index-jjy5a5w0.js → index-6nqz8hsz.js} +16 -5
- package/dist/{index-a6aqpfp6.js → index-9mggb1nv.js} +403 -1
- package/dist/{index-kanfby7e.js → index-9n6mdfqy.js} +452 -442
- package/dist/{index-fxgaaptn.js → index-a3n2dczz.js} +8 -7
- package/dist/{index-g3j49m95.js → index-b6rdv8re.js} +1 -1
- package/dist/{index-qbda6a8k.js → index-by14cr73.js} +30 -19
- package/dist/{index-25tm250q.js → index-dhdv88e5.js} +1 -1
- package/dist/{index-4xpff2m1.js → index-eyr425dx.js} +820 -179
- package/dist/{index-x4h461yf.js → index-h62j8z2r.js} +1 -1
- package/dist/{index-xqhejxmd.js → index-pdh4tdcq.js} +1 -1
- package/dist/{index-9gs34125.js → index-sk6p6esd.js} +3777 -2544
- package/dist/{index-pb8ysnq2.js → index-sv5tn72j.js} +1 -1
- package/dist/{index-bamnjp2t.js → index-sx1sppd6.js} +11 -7
- package/dist/{index-x7jcqpy6.js → index-tx8nfw85.js} +539 -29
- package/dist/{index-42n4fhxy.js → index-xj5bap1r.js} +61 -54
- package/dist/{index-rbs27k1d.js → index-xstbytjn.js} +1 -1
- package/dist/{index-0jc25yjf.js → index-xvtjwhm6.js} +3 -3
- package/dist/{index-qtpkkzan.js → index-yamsmkmh.js} +4 -4
- package/dist/index.js +87 -68
- package/dist/{message-index-bounded-71b75npz.js → message-index-bounded-xav3h8qe.js} +5 -6
- package/dist/migration-worker.js +3 -4
- package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
- package/dist/plugin/messages-transform.d.ts +6 -0
- package/dist/plugin/messages-transform.d.ts.map +1 -1
- package/dist/{project-embedding-registry-5dwh5zfn.js → project-embedding-registry-f2azrq24.js} +5 -3
- package/dist/raw-ordinal-warmup-worker.js +4 -4
- package/dist/{read-session-chunk-bfv831yx.js → read-session-chunk-70aam2c1.js} +4 -5
- package/dist/{recomp-orchestrator-8gj1b7pz.js → recomp-orchestrator-wbm4kpq9.js} +15 -16
- package/dist/{render-b3zcz9x4.js → render-73vrt30h.js} +10 -11
- package/dist/{search-archive-dp9afcv1.js → search-archive-ew4c3bsw.js} +7 -8
- package/dist/{search-y3w590tn.js → search-jfwb4ms3.js} +14 -15
- package/dist/shared/test-open-files.d.ts +17 -0
- package/dist/shared/test-open-files.d.ts.map +1 -0
- package/dist/{storage-tags-kdb4qsyq.js → storage-tags-arnkn5t1.js} +4 -2
- package/dist/tools/ctx-expand/search-archive.d.ts.map +1 -1
- package/dist/{transform-postprocess-phase-8awze16a.js → transform-postprocess-phase-mt4dcbhd.js} +16 -17
- package/dist/v2/auto-search-worker.js +3 -3
- package/dist/v2/chunk-embedding-write-worker.js +3 -4
- package/dist/v2/{context-99v7x7fm.js → context-13er940g.js} +1147 -467
- package/dist/v2/{embedding-backfill-x1zpc04f.js → embedding-backfill-48qbnh7p.js} +4 -4
- package/dist/v2/embedding-worker.js +2 -2
- package/dist/v2/fold/instruction-supersession.d.ts.map +1 -1
- package/dist/v2/hooks/context.d.ts.map +1 -1
- package/dist/v2/{message-index-bounded-2pev910h.js → message-index-bounded-sw8hyqw1.js} +5 -6
- package/dist/v2/migration-worker.js +3 -4
- package/dist/v2/{project-embedding-registry-v2fxrs8b.js → project-embedding-registry-kwahtjgg.js} +5 -3
- package/dist/v2/raw-ordinal-warmup-worker.js +4 -4
- package/dist/v2/{read-session-chunk-a454a4d2.js → read-session-chunk-nqkn0yb4.js} +4 -5
- package/dist/v2/{recomp-orchestrator-03fskpsa.js → recomp-orchestrator-cdkfynz4.js} +15 -16
- package/dist/v2/{render-cryx4xsj.js → render-a7j6fqbd.js} +10 -11
- package/dist/v2/{search-2qj4w7rw.js → search-8dc9ex0a.js} +13 -14
- package/dist/v2/{search-archive-xw2rsq81.js → search-archive-3a36887a.js} +7 -8
- package/dist/v2/{server-kreheyw5.js → server-0ayyh3mx.js} +5 -7
- package/dist/v2/{server-t8n27rbt.js → server-4x0xq8a1.js} +1 -1
- package/dist/v2/{server-kaqs6h31.js → server-52x68mj4.js} +1 -1
- package/dist/v2/{server-cpstyzvy.js → server-578pc4tf.js} +3 -3
- package/dist/v2/{server-sb55ddgn.js → server-5p0bjk3y.js} +1 -1
- package/dist/v2/{server-58h6rrnx.js → server-6k0rnwbd.js} +1 -1
- package/dist/v2/{server-y5cma2j1.js → server-79tbtk5w.js} +8 -7
- package/dist/v2/{server-b3h8vqa6.js → server-7rmf6a57.js} +538 -28
- package/dist/v2/{server-xjzx27w2.js → server-7waad75d.js} +29 -18
- package/dist/v2/{server-wq9y9a03.js → server-96g41nad.js} +61 -54
- package/dist/v2/{server-txdsx71h.js → server-avfc9ezb.js} +4 -4
- package/dist/v2/{server-ww7xbxxf.js → server-b0357as4.js} +357 -1
- package/dist/v2/{server-qsvm6w79.js → server-beydmd68.js} +3763 -2484
- package/dist/v2/server-gge95a03.js +2687 -0
- package/dist/v2/{server-kfw4m268.js → server-jwydbwcx.js} +1 -1
- package/dist/v2/{server-brsx45r6.js → server-kwtnrhv3.js} +1 -1
- package/dist/v2/{server-4h4acntc.js → server-rpmkz5ta.js} +11 -7
- package/dist/v2/{server-0ms13pc8.js → server-rz6g5xne.js} +16 -5
- package/dist/v2/{server-dkfs58nq.js → server-v817sf79.js} +1 -1
- package/dist/v2/{server-vjadwb3j.js → server-ycvqg2s3.js} +3 -3
- package/dist/v2/server.js +4 -4
- package/dist/v2/{storage-tags-rzv7y0n3.js → storage-tags-19da4c0m.js} +4 -2
- package/dist/v2/{v2-archive-reader-3575hw8s.js → v2-archive-reader-jm3d7ccj.js} +1 -1
- package/dist/v2/{wrapup-orchestrator-shzv6t4v.js → wrapup-orchestrator-xnqq81c4.js} +18 -18
- package/dist/{v2-archive-reader-75fdm0te.js → v2-archive-reader-9bbfh131.js} +1 -1
- package/dist/{wrapup-orchestrator-e0z7nvnx.js → wrapup-orchestrator-svhee0fn.js} +16 -17
- package/package.json +1 -1
- package/src/shared/escalation-bands.test.ts +16 -1
- package/src/shared/sqlite-placeholders.test.ts +103 -0
- package/src/shared/storage-file-mutex-review.test.ts +5 -7
- package/src/shared/test-open-files.test.ts +194 -0
- package/src/shared/test-open-files.ts +71 -0
- package/dist/index-bkg4hdyh.js +0 -3865
- package/dist/index-d9vfe7xq.js +0 -16
- package/dist/index-hsv31e69.js +0 -405
- package/dist/v2/server-d9vfe7xq.js +0 -16
- package/dist/v2/server-kgq2e2hh.js +0 -361
- package/dist/v2/server-rcchadw0.js +0 -3865
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { expect, spyOn, test } from "bun:test";
|
|
2
|
+
import * as childProcess from "node:child_process";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import { closeSync, openSync, realpathSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { listOpenFiles, parseLsofPaths } from "./test-open-files";
|
|
8
|
+
import { cleanupTestTempDir, createTestTempDirFromPath } from "./test-temp-dir";
|
|
9
|
+
|
|
10
|
+
test.skipIf(process.platform !== "linux" && process.platform !== "darwin")(
|
|
11
|
+
"open-file inventory sees a file only while its descriptor is open",
|
|
12
|
+
() => {
|
|
13
|
+
const root = createTestTempDirFromPath(join(tmpdir(), "mc-open-files-"));
|
|
14
|
+
const path = join(realpathSync(root), "file with spaces.db");
|
|
15
|
+
let descriptor: number | undefined;
|
|
16
|
+
try {
|
|
17
|
+
descriptor = openSync(path, "w");
|
|
18
|
+
const open = listOpenFiles();
|
|
19
|
+
if (!open.available) throw new Error(open.reason);
|
|
20
|
+
expect(open.paths).toContain(path);
|
|
21
|
+
if (process.platform === "linux")
|
|
22
|
+
expect(open.entries).toContainEqual({ descriptor, path });
|
|
23
|
+
closeSync(descriptor);
|
|
24
|
+
descriptor = undefined;
|
|
25
|
+
const closed = listOpenFiles();
|
|
26
|
+
if (!closed.available) throw new Error(closed.reason);
|
|
27
|
+
expect(closed.paths).not.toContain(path);
|
|
28
|
+
} finally {
|
|
29
|
+
if (descriptor !== undefined) closeSync(descriptor);
|
|
30
|
+
cleanupTestTempDir(root);
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
test.skipIf(process.platform !== "linux")("Linux inventory never spawns lsof", () => {
|
|
36
|
+
const execute = spyOn(childProcess, "execFileSync").mockImplementation(() => {
|
|
37
|
+
throw new Error("subprocess forbidden during inventory");
|
|
38
|
+
});
|
|
39
|
+
const root = createTestTempDirFromPath(join(tmpdir(), "mc-open-files-path-"));
|
|
40
|
+
const path = join(realpathSync(root), "marker");
|
|
41
|
+
const descriptor = openSync(path, "w");
|
|
42
|
+
try {
|
|
43
|
+
const result = listOpenFiles();
|
|
44
|
+
if (!result.available) throw new Error(result.reason);
|
|
45
|
+
expect(result.paths).toContain(path);
|
|
46
|
+
expect(execute).not.toHaveBeenCalled();
|
|
47
|
+
} finally {
|
|
48
|
+
execute.mockRestore();
|
|
49
|
+
closeSync(descriptor);
|
|
50
|
+
cleanupTestTempDir(root);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test.skipIf(process.platform !== "linux" && process.platform !== "darwin")(
|
|
55
|
+
"inventory can observe a child process's open file",
|
|
56
|
+
async () => {
|
|
57
|
+
const root = createTestTempDirFromPath(join(tmpdir(), "mc-open-files-child-"));
|
|
58
|
+
const path = join(realpathSync(root), "child marker");
|
|
59
|
+
const child = Bun.spawn(
|
|
60
|
+
[
|
|
61
|
+
process.execPath,
|
|
62
|
+
"--eval",
|
|
63
|
+
`
|
|
64
|
+
import { openSync } from 'node:fs';
|
|
65
|
+
const descriptor = openSync(${JSON.stringify(path)}, 'w');
|
|
66
|
+
console.log('ready');
|
|
67
|
+
setInterval(() => {}, 1000);
|
|
68
|
+
`,
|
|
69
|
+
],
|
|
70
|
+
{ stdout: "pipe", stderr: "pipe", windowsHide: true },
|
|
71
|
+
);
|
|
72
|
+
try {
|
|
73
|
+
const reader = child.stdout.getReader();
|
|
74
|
+
const ready = await reader.read();
|
|
75
|
+
reader.releaseLock();
|
|
76
|
+
expect(new TextDecoder().decode(ready.value)).toContain("ready");
|
|
77
|
+
const inventory = listOpenFiles(child.pid);
|
|
78
|
+
if (!inventory.available) throw new Error(inventory.reason);
|
|
79
|
+
expect(inventory.paths).toContain(path);
|
|
80
|
+
} finally {
|
|
81
|
+
child.kill();
|
|
82
|
+
await child.exited;
|
|
83
|
+
cleanupTestTempDir(root);
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
test.skipIf(process.platform !== "linux")("Linux inventory skips vanished descriptors", () => {
|
|
89
|
+
const readlink = fs.readlinkSync;
|
|
90
|
+
let raced = false;
|
|
91
|
+
const link = spyOn(fs, "readlinkSync").mockImplementation(((path: fs.PathLike) => {
|
|
92
|
+
if (!raced && String(path).startsWith("/proc/self/fd/")) {
|
|
93
|
+
raced = true;
|
|
94
|
+
throw Object.assign(new Error("closed during inventory"), { code: "ENOENT" });
|
|
95
|
+
}
|
|
96
|
+
return readlink(path);
|
|
97
|
+
}) as typeof fs.readlinkSync);
|
|
98
|
+
try {
|
|
99
|
+
expect(listOpenFiles().available).toBe(true);
|
|
100
|
+
expect(raced).toBe(true);
|
|
101
|
+
} finally {
|
|
102
|
+
link.mockRestore();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test.skipIf(process.platform !== "linux")(
|
|
107
|
+
"Linux inventory reports listing failures as unavailable",
|
|
108
|
+
() => {
|
|
109
|
+
const listing = spyOn(fs, "readdirSync").mockImplementation(() => {
|
|
110
|
+
throw new Error("inventory denied");
|
|
111
|
+
});
|
|
112
|
+
try {
|
|
113
|
+
expect(listOpenFiles()).toEqual({ available: false, reason: "inventory denied" });
|
|
114
|
+
} finally {
|
|
115
|
+
listing.mockRestore();
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
test.skipIf(process.platform !== "linux")(
|
|
121
|
+
"child exit during inventory is unavailable, not empty",
|
|
122
|
+
() => {
|
|
123
|
+
const read = fs.readFileSync;
|
|
124
|
+
let reads = 0;
|
|
125
|
+
const stat = spyOn(fs, "readFileSync").mockImplementation(((
|
|
126
|
+
path: fs.PathOrFileDescriptor,
|
|
127
|
+
) => {
|
|
128
|
+
if (String(path) !== "/proc/123456/stat") return read(path);
|
|
129
|
+
return `123456 (test process) ${reads++ === 0 ? "S" : "Z"} ${"0 ".repeat(18)}100`;
|
|
130
|
+
}) as typeof fs.readFileSync);
|
|
131
|
+
const listing = spyOn(fs, "readdirSync").mockReturnValue([]);
|
|
132
|
+
try {
|
|
133
|
+
expect(listOpenFiles(123456)).toEqual({
|
|
134
|
+
available: false,
|
|
135
|
+
reason: "Process 123456 has exited",
|
|
136
|
+
});
|
|
137
|
+
} finally {
|
|
138
|
+
stat.mockRestore();
|
|
139
|
+
listing.mockRestore();
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
test("macOS name parser ignores warnings and preserves paths with spaces", () => {
|
|
145
|
+
expect(
|
|
146
|
+
parseLsofPaths(
|
|
147
|
+
[
|
|
148
|
+
"lsof: WARNING: can't stat() overlay file system /container/one",
|
|
149
|
+
" Output information may be incomplete.",
|
|
150
|
+
"p123",
|
|
151
|
+
"n/private/var/test directory/context.db",
|
|
152
|
+
"n/private/var/test directory/context.db-wal",
|
|
153
|
+
"lsof: WARNING: can't stat() overlay file system /container/two",
|
|
154
|
+
"",
|
|
155
|
+
].join("\n"),
|
|
156
|
+
),
|
|
157
|
+
).toEqual([
|
|
158
|
+
"/private/var/test directory/context.db",
|
|
159
|
+
"/private/var/test directory/context.db-wal",
|
|
160
|
+
]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("macOS name parser keeps paths beyond the old 1 MiB subprocess limit", () => {
|
|
164
|
+
const paths = Array.from(
|
|
165
|
+
{ length: 24_000 },
|
|
166
|
+
(_, index) => `/private/var/test directory/fixture-${index}.db`,
|
|
167
|
+
);
|
|
168
|
+
const output = `p123\n${paths.map((path) => `n${path}\n`).join("")}`;
|
|
169
|
+
expect(Buffer.byteLength(output)).toBeGreaterThan(1024 * 1024);
|
|
170
|
+
expect(parseLsofPaths(output)).toEqual(paths);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("macOS inventory requests name fields with a 64 MiB buffer", () => {
|
|
174
|
+
const platform = Object.getOwnPropertyDescriptor(process, "platform")!;
|
|
175
|
+
const execute = spyOn(childProcess, "execFileSync").mockReturnValue("p123\nn/test file.db\n");
|
|
176
|
+
try {
|
|
177
|
+
// Exercise command construction on every host without needing a macOS subprocess.
|
|
178
|
+
Object.defineProperty(process, "platform", { value: "darwin" });
|
|
179
|
+
expect(listOpenFiles(123)).toEqual({
|
|
180
|
+
available: true,
|
|
181
|
+
paths: ["/test file.db"],
|
|
182
|
+
entries: [{ path: "/test file.db" }],
|
|
183
|
+
});
|
|
184
|
+
expect(execute).toHaveBeenCalledWith("lsof", ["-p", "123", "-Fn"], {
|
|
185
|
+
encoding: "utf8",
|
|
186
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
187
|
+
timeout: 10_000,
|
|
188
|
+
windowsHide: true,
|
|
189
|
+
});
|
|
190
|
+
} finally {
|
|
191
|
+
Object.defineProperty(process, "platform", platform);
|
|
192
|
+
execute.mockRestore();
|
|
193
|
+
}
|
|
194
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { readdirSync, readFileSync, readlinkSync } from "node:fs";
|
|
3
|
+
|
|
4
|
+
export interface OpenFileEntry {
|
|
5
|
+
path: string;
|
|
6
|
+
descriptor?: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type OpenFilesResult =
|
|
10
|
+
| { available: true; paths: string[]; entries: OpenFileEntry[] }
|
|
11
|
+
| { available: false; reason: string };
|
|
12
|
+
|
|
13
|
+
/** Parse lsof's name fields without interpreting its padded human-readable table. */
|
|
14
|
+
export function parseLsofPaths(output: string): string[] {
|
|
15
|
+
return output
|
|
16
|
+
.split("\n")
|
|
17
|
+
.filter((line) => line.startsWith("n"))
|
|
18
|
+
.map((line) => line.slice(1));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function processIdentity(pid: number): string {
|
|
22
|
+
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
23
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
24
|
+
if (["Z", "X", "x"].includes(fields[0] ?? "")) throw new Error(`Process ${pid} has exited`);
|
|
25
|
+
const startTime = fields[19];
|
|
26
|
+
if (!startTime) throw new Error(`Cannot read identity for process ${pid}`);
|
|
27
|
+
return startTime; // Start time distinguishes a reused PID from the requested process.
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Test-only inventory; callers decide whether an unavailable check can be skipped. */
|
|
31
|
+
export function listOpenFiles(pid = process.pid): OpenFilesResult {
|
|
32
|
+
try {
|
|
33
|
+
if (process.platform === "linux") {
|
|
34
|
+
// /proc avoids subprocess output limits and lsof's overlay-mount warnings.
|
|
35
|
+
const directory = pid === process.pid ? "/proc/self/fd" : `/proc/${pid}/fd`;
|
|
36
|
+
const identity = pid === process.pid ? undefined : processIdentity(pid);
|
|
37
|
+
const entries: OpenFileEntry[] = [];
|
|
38
|
+
for (const descriptor of readdirSync(directory)) {
|
|
39
|
+
try {
|
|
40
|
+
entries.push({
|
|
41
|
+
descriptor: Number(descriptor),
|
|
42
|
+
path: readlinkSync(`${directory}/${descriptor}`),
|
|
43
|
+
});
|
|
44
|
+
} catch (error) {
|
|
45
|
+
// The listing's own directory handle, or any concurrent close, can vanish.
|
|
46
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (identity !== undefined && processIdentity(pid) !== identity)
|
|
50
|
+
throw new Error(`Process ${pid} changed during the inventory`);
|
|
51
|
+
return { available: true, paths: entries.map((entry) => entry.path), entries };
|
|
52
|
+
}
|
|
53
|
+
if (process.platform === "darwin") {
|
|
54
|
+
const paths = parseLsofPaths(
|
|
55
|
+
execFileSync("lsof", ["-p", String(pid), "-Fn"], {
|
|
56
|
+
encoding: "utf8",
|
|
57
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
58
|
+
timeout: 10_000,
|
|
59
|
+
windowsHide: true,
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
return { available: true, paths, entries: paths.map((path) => ({ path })) };
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
available: false,
|
|
66
|
+
reason: `Open-file inventory is unavailable on ${process.platform}`,
|
|
67
|
+
};
|
|
68
|
+
} catch (error) {
|
|
69
|
+
return { available: false, reason: error instanceof Error ? error.message : String(error) };
|
|
70
|
+
}
|
|
71
|
+
}
|