@intentic/fileq 1.430.0 → 1.432.0

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 (69) hide show
  1. package/NOTICE +58 -1
  2. package/README.md +29 -1
  3. package/bin/fileq-rg-pre +6 -2
  4. package/dist/app.d.ts.map +1 -1
  5. package/dist/app.js +4 -1
  6. package/dist/app.js.map +1 -1
  7. package/dist/commands/check.command.d.ts.map +1 -1
  8. package/dist/commands/check.command.js +12 -5
  9. package/dist/commands/check.command.js.map +1 -1
  10. package/dist/commands/derive.command.d.ts.map +1 -1
  11. package/dist/commands/derive.command.js +3 -2
  12. package/dist/commands/derive.command.js.map +1 -1
  13. package/dist/commands/read.command.d.ts.map +1 -1
  14. package/dist/commands/read.command.js +42 -7
  15. package/dist/commands/read.command.js.map +1 -1
  16. package/dist/commands/render.command.d.ts.map +1 -1
  17. package/dist/commands/render.command.js +23 -10
  18. package/dist/commands/render.command.js.map +1 -1
  19. package/dist/lib/derivers/docx-math-symbols.d.ts +14 -0
  20. package/dist/lib/derivers/docx-math-symbols.d.ts.map +1 -0
  21. package/dist/lib/derivers/docx-math-symbols.js +160 -0
  22. package/dist/lib/derivers/docx-math-symbols.js.map +1 -0
  23. package/dist/lib/derivers/docx-math.d.ts +15 -0
  24. package/dist/lib/derivers/docx-math.d.ts.map +1 -0
  25. package/dist/lib/derivers/docx-math.js +252 -0
  26. package/dist/lib/derivers/docx-math.js.map +1 -0
  27. package/dist/lib/derivers/docx.d.ts.map +1 -1
  28. package/dist/lib/derivers/docx.js +3 -2
  29. package/dist/lib/derivers/docx.js.map +1 -1
  30. package/dist/lib/derivers/pdf-furniture.d.ts.map +1 -1
  31. package/dist/lib/derivers/pdf-furniture.js +3 -0
  32. package/dist/lib/derivers/pdf-furniture.js.map +1 -1
  33. package/dist/lib/derivers/pdf-tables.d.ts +9 -0
  34. package/dist/lib/derivers/pdf-tables.d.ts.map +1 -0
  35. package/dist/lib/derivers/pdf-tables.js +319 -0
  36. package/dist/lib/derivers/pdf-tables.js.map +1 -0
  37. package/dist/lib/derivers/pdf.d.ts.map +1 -1
  38. package/dist/lib/derivers/pdf.js +77 -38
  39. package/dist/lib/derivers/pdf.js.map +1 -1
  40. package/dist/lib/ocr-slots.d.ts +8 -0
  41. package/dist/lib/ocr-slots.d.ts.map +1 -0
  42. package/dist/lib/ocr-slots.js +35 -0
  43. package/dist/lib/ocr-slots.js.map +1 -0
  44. package/dist/skill.d.ts.map +1 -1
  45. package/dist/skill.js +7 -2
  46. package/dist/skill.js.map +1 -1
  47. package/package.json +7 -7
  48. package/src/app.ts +4 -1
  49. package/src/cli.integration.test.ts +30 -8
  50. package/src/commands/check.command.ts +20 -5
  51. package/src/commands/derive.command.ts +3 -2
  52. package/src/commands/read.command.ts +56 -8
  53. package/src/commands/render.command.ts +33 -11
  54. package/src/derivers.integration.test.ts +131 -11
  55. package/src/lib/derivers/docx-math-symbols.ts +197 -0
  56. package/src/lib/derivers/docx-math.test.ts +131 -0
  57. package/src/lib/derivers/docx-math.ts +355 -0
  58. package/src/lib/derivers/docx.ts +5 -3
  59. package/src/lib/derivers/image.ts +2 -2
  60. package/src/lib/derivers/pdf-furniture.test.ts +9 -0
  61. package/src/lib/derivers/pdf-furniture.ts +5 -0
  62. package/src/lib/derivers/pdf-tables.test.ts +292 -0
  63. package/src/lib/derivers/pdf-tables.ts +465 -0
  64. package/src/lib/derivers/pdf.ts +94 -40
  65. package/src/lib/ocr-slots.integration.test.ts +64 -0
  66. package/src/lib/ocr-slots.ts +61 -0
  67. package/src/lib/sidecar.ts +1 -1
  68. package/src/render.integration.test.ts +5 -4
  69. package/src/skill.ts +10 -5
@@ -0,0 +1,64 @@
1
+ /* The machine-wide OCR slots: flock locks in a directory every process shares. */
2
+ import { mkdtempSync, readFileSync, rmSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { requires } from "@intentic/testing/requires";
6
+ import { ocrSlotCount, runInOcrSlot } from "./ocr-slots.js";
7
+ import { onPath } from "./tools.js";
8
+
9
+ let dir: string;
10
+ beforeEach(() => {
11
+ dir = mkdtempSync(join(tmpdir(), "fileq-ocr-slots-"));
12
+ });
13
+ afterEach(() => {
14
+ rmSync(dir, { recursive: true, force: true });
15
+ });
16
+
17
+ describe("ocrSlotCount", () => {
18
+ test("one slot per eight cores and per 8 GiB, never none and never more than two", () => {
19
+ expect(ocrSlotCount(4, 64 * 1024 ** 3)).toBe(1);
20
+ expect(ocrSlotCount(16, 64 * 1024 ** 3)).toBe(2);
21
+ expect(ocrSlotCount(64, 256 * 1024 ** 3)).toBe(2);
22
+ expect(ocrSlotCount(16, 12 * 1024 ** 3)).toBe(1);
23
+ expect(ocrSlotCount(1, 1024 ** 3)).toBe(1);
24
+ });
25
+ });
26
+
27
+ describe("runInOcrSlot", () => {
28
+ const flock = requires(onPath("flock"), "flock (util-linux) on PATH");
29
+ // Each run notes when it starts and ends in one shared log, so overlapping runs interleave there.
30
+ const run = (name: string, slots: number) =>
31
+ runInOcrSlot("sh", ["-c", `echo "start ${name}" >> ${dir}/log; sleep 0.4; echo "end ${name}" >> ${dir}/log; echo done ${name}`], {
32
+ slots,
33
+ lockDir: dir,
34
+ timeoutMs: 30_000,
35
+ });
36
+ const log = (): string[] => readFileSync(join(dir, "log"), "utf8").trim().split("\n");
37
+
38
+ test.skipIf(!flock.runs)(flock.title("with one slot, a second run waits for the first to end"), async () => {
39
+ const [first, second] = await Promise.all([run("a", 1), run("b", 1)]);
40
+ expect(first.stdout.text).toBe("done a\n");
41
+ expect(second.stdout.text).toBe("done b\n");
42
+ const lines = log();
43
+ expect(lines).toHaveLength(4);
44
+ expect(lines[0]?.replace(/ .$/, "")).toBe("start");
45
+ expect(lines[1]).toBe(lines[0]?.replace("start", "end"));
46
+ });
47
+
48
+ test.skipIf(!flock.runs)(flock.title("with two slots, two runs go at once"), async () => {
49
+ await Promise.all([run("a", 2), run("b", 2)]);
50
+ expect(log().map((line) => line.split(" ")[0])).toEqual(["start", "start", "end", "end"]);
51
+ });
52
+
53
+ test.skipIf(!flock.runs)(flock.title("the command's own exit code and output come back"), async () => {
54
+ const result = await runInOcrSlot("sh", ["-c", "echo read; exit 1"], { slots: 1, lockDir: dir, timeoutMs: 30_000 });
55
+ expect(result.exitCode).toBe(1);
56
+ expect(result.stdout.text).toBe("read\n");
57
+ });
58
+
59
+ test.skipIf(!flock.runs)(flock.title("a lock directory flock cannot use costs the slot, not the run"), async () => {
60
+ const result = await runInOcrSlot("sh", ["-c", "echo read"], { slots: 1, lockDir: join(dir, "missing", "deeper"), timeoutMs: 30_000 });
61
+ expect(result.exitCode).toBe(0);
62
+ expect(result.stdout.text).toBe("read\n");
63
+ });
64
+ });
@@ -0,0 +1,61 @@
1
+ import { availableParallelism, tmpdir, totalmem } from "node:os";
2
+ import { join } from "node:path";
3
+ import { sleep } from "@intentic/base/async";
4
+ import { runBounded, type ProcessResult, type RunOptions } from "@intentic/base/process";
5
+ import { onPath } from "./tools.js";
6
+
7
+ // How many OCR readers run at once on this machine, across every process: each holds about 1.5 GiB of models and four
8
+ // threads, and fileq is started by many callers at once. ripgrep runs its preprocessor for twelve documents in
9
+ // parallel, so `rg` over a folder of unread scans started six readers (7 GiB) at once, and all six then ran past their
10
+ // time limit (measured 2026-11-23). The daemon's shadow keeper, its document routes and agents' own reads add more.
11
+ // The slots are flock(1) locks on files in /tmp, which every process here shares and the kernel releases however the
12
+ // holder ends. Without flock (macOS, a bare image) a run takes no slot, as before.
13
+
14
+ const CORES_PER_SLOT = 8;
15
+ const BYTES_PER_SLOT = 8 * 1024 ** 3;
16
+ const MAX_SLOTS = 2;
17
+ // flock's answer when the lock is held; the OCR command's own exits are 0, 1 and 2.
18
+ const BUSY = 75;
19
+ const POLL_FIRST_MS = 250;
20
+ const POLL_MAX_MS = 2_000;
21
+
22
+ /** One slot per eight cores and per 8 GiB, at least one and at most two. */
23
+ export const ocrSlotCount = (cores: number = availableParallelism(), memoryBytes: number = totalmem()): number =>
24
+ Math.max(1, Math.min(MAX_SLOTS, Math.floor(cores / CORES_PER_SLOT), Math.floor(memoryBytes / BYTES_PER_SLOT)));
25
+
26
+ export interface SlotOptions extends RunOptions {
27
+ readonly slots?: number;
28
+ /** Where the lock files live; /tmp unless a test says otherwise. */
29
+ readonly lockDir?: string;
30
+ }
31
+
32
+ const lockDirOf = (options: SlotOptions): string => options.lockDir ?? (process.platform === "win32" ? tmpdir() : "/tmp");
33
+
34
+ /**
35
+ * `command args` run while holding one of the machine's OCR slots, waiting while every slot is held. `flock --no-fork`
36
+ * execs the command in its own place, so the command holds the lock itself and the time limit counts from the moment
37
+ * it has one. The command must not exit 64 or above: those are flock's own answers.
38
+ */
39
+ export const runInOcrSlot = async (command: string, args: readonly string[], options: SlotOptions = {}): Promise<ProcessResult> => {
40
+ if (!onPath("flock")) {
41
+ return runBounded(command, args, options);
42
+ }
43
+ const slots = options.slots ?? ocrSlotCount();
44
+ const dir = lockDirOf(options);
45
+ for (let wait = POLL_FIRST_MS; ; wait = Math.min(wait * 2, POLL_MAX_MS)) {
46
+ for (let slot = 0; slot < slots; slot++) {
47
+ const lock = join(dir, `fileq-ocr-${String(slot)}.lock`);
48
+ const result = await runBounded("flock", ["--no-fork", "--nonblock", "--conflict-exit-code", String(BUSY), lock, command, ...args], options);
49
+ if (result.exitCode === BUSY) {
50
+ continue;
51
+ }
52
+ // A lock file flock could not open or lock (a read-only /tmp, another user's file) costs the slot, not the run.
53
+ if (result.exitCode !== null && result.exitCode >= 64 && result.stdout.text === "") {
54
+ return runBounded(command, args, options);
55
+ }
56
+ // An aborted run never starts, and comes back here as itself.
57
+ return result;
58
+ }
59
+ await sleep(wait, { signal: options.signal });
60
+ }
61
+ };
@@ -207,7 +207,7 @@ export const writeSidecar = async (workspaceRoot: string, input: WriteSidecarInp
207
207
  return written;
208
208
  };
209
209
 
210
- // The shadow tree ignores itself: outside the sandbox (fileq from npm, the Claude Code plugin) it lands inside somebody's
210
+ // The shadow tree ignores itself: outside the sandbox (fileq from npm) it lands inside somebody's
211
211
  // repository, where a `git add -A` would sweep derived text of every document into a commit. `wx` leaves an existing
212
212
  // file alone, so an owner's own edit to it wins; any other failure costs only the ignore, never the shadow.
213
213
  export const ignoreShadowsInGit = async (workspaceRoot: string): Promise<void> => {
@@ -89,7 +89,7 @@ describe("pdf", () => {
89
89
  writeFileSync(join(root, "short.pdf"), pagesPdf(2));
90
90
  const { out, exitCode } = await fileq("render", join(root, "short.pdf"), "--pages", "5-9");
91
91
  expect(exitCode).toBe(2);
92
- expect(out).toBe(`fileq: cannot render ${join(root, "short.pdf")}: the document has 2 pages; --pages names none of them\n`);
92
+ expect(out).toBe(`fileq: error: usage: cannot render ${join(root, "short.pdf")}: the document has 2 pages; --pages names none of them\n`);
93
93
  });
94
94
 
95
95
  test.skipIf(!raster.runs)(raster.title("a long document draws its first twenty pages and says how to get the rest"), async () => {
@@ -111,7 +111,8 @@ describe("tools", () => {
111
111
  process.env["PATH"] = "/nonexistent";
112
112
  const { out, exitCode } = await fileq("render", join(root, "plain.pdf"));
113
113
  expect(exitCode).toBe(2);
114
- expect(out).toContain("rendering pages needs pdftoppm (poppler-utils) or mutool (mupdf-tools) on PATH, and neither is here");
114
+ expect(out).toContain("fileq: error: missing_tool: cannot render");
115
+ expect(out).toContain("rendering pages needs pdftoppm (poppler-utils) or mutool (mupdf-tools) on PATH, and neither is here\nfileq: next: install one");
115
116
  });
116
117
 
117
118
  test("in an Intentic sandbox (its environment command on PATH) it prints the command that asks for the office pack", async () => {
@@ -124,7 +125,7 @@ describe("tools", () => {
124
125
  const { out, exitCode } = await fileq("render", join(root, "sandboxed.pdf"));
125
126
  expect(exitCode).toBe(2);
126
127
  expect(out).toContain(
127
- 'neither is here: ask the owner for the office image pack (LibreOffice and poppler) with `environment propose office --pack --why "to look at the document I made"`',
128
+ 'neither is here\nfileq: next: ask the owner for the office image pack (LibreOffice and poppler) with `environment propose office --pack --why "to look at the document I made"`',
128
129
  );
129
130
  });
130
131
 
@@ -144,7 +145,7 @@ describe("tools", () => {
144
145
  writeFileSync(join(root, "photo.png"), pngBytes());
145
146
  const { out, exitCode } = await fileq("render", join(root, "photo.png"));
146
147
  expect(exitCode).toBe(1);
147
- expect(out).toContain("an image is already pixels: Read it directly");
148
+ expect(out).toBe(`fileq: error: unsupported: cannot render ${join(root, "photo.png")}: an image is already pixels: Read it directly\n`);
148
149
  });
149
150
  });
150
151
 
package/src/skill.ts CHANGED
@@ -2,13 +2,13 @@ import { DERIVED_DIR } from "./lib/sidecar.js";
2
2
  import { AUTHORING_RULES } from "./skill-authoring.js";
3
3
 
4
4
  // fileq's own teaching: the SKILL.md every agent that has fileq on its PATH is given. It lives with the CLI it describes
5
- // so a new flag and its sentence land together, and so the two hosts that ship it cannot drift: the sandbox writes it
6
- // into the workspace's skills (the daemon's settings/skills.ts), the Claude Code plugin ships it as its fileq skill.
5
+ // so a new flag and its sentence land together: the sandbox writes it into the workspace's skills (the daemon's
6
+ // settings/skills.ts), and a standalone install of fileq reads the same text.
7
7
  // Only what the host itself provides differs: the sandbox image carries the `ocr` command (PaddleOCR, its models in the
8
8
  // privacy image pack) and wires fileq as git's textconv; a standalone install has neither until its owner adds them.
9
9
 
10
10
  export interface FileqSkillHost {
11
- // "sandbox": the Intentic sandbox image. "standalone": fileq on anyone's machine (the Claude Code plugin, npm).
11
+ // "sandbox": the Intentic sandbox image. "standalone": fileq on anyone's machine (npm).
12
12
  readonly host: "sandbox" | "standalone";
13
13
  }
14
14
 
@@ -84,7 +84,9 @@ cached by content, so a repeat read of unchanged content is instant.
84
84
  ## Read a file
85
85
  \`fileq <file>\` (or \`fileq read <file> --budget 8000\`)
86
86
  - Prints a capsule (format, token cost), the content up to the budget, and \`saved:\` — the markdown file
87
- carrying the whole thing. Over budget, the cut is announced with that exact path to Read.
87
+ carrying the whole thing. Over budget, the last line says where the rest starts: \`[cut at line 212 of 1480,
88
+ ~19,100 of 23,100 tokens left: continue with Read <file> from line 212, or raise --budget]\`. Read that file
89
+ from that line rather than from the top.
88
90
  - Formats: docx and odt (headings, lists, tables), xlsx (capped tables), pptx (slides + speaker notes),
89
91
  pdf (text layer; ${OCR_FORMAT[host]}, and the note says the words are
90
92
  recognised, not exact), epub (chapters in reading order), ipynb (cells, fenced code, capped outputs),
@@ -106,7 +108,10 @@ ${OCR_REFUSAL[host]}
106
108
  - Plain text (md, csv, txt, code) is not fileq's business: Read it directly — but see below for the big ones.
107
109
  - Web pages belong to \`webq\`; images for a vision model belong to the Read tool, which shows the pixels.
108
110
 
109
- ${SEARCH[host]}${host === "sandbox" ? TEXTCONV : ""}Exit codes: 0 content, 1 nothing derivable, 2 broken invocation or install.
111
+ ${SEARCH[host]}${host === "sandbox" ? TEXTCONV : ""}Exit codes: 0 content, 1 nothing derivable, 2 broken invocation or install. A failure is one line,
112
+ \`fileq: error: <code>: <message>\`, often followed by \`fileq: next: <step>\`: do the step. The codes are
113
+ \`usage\`, \`not_found\`, \`unsupported\`, \`too_large\`, \`unreadable\` (damaged, locked or password-protected),
114
+ \`missing_tool\`, \`broken_install\` and \`internal\`; the last two are fileq's fault, never an empty document.
110
115
 
111
116
  ## After you produce a document
112
117
  A deck, report, spreadsheet or PDF made for someone is not finished until you have checked it and looked at it.