Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .changeset/39008-core-document-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'@objectstack/core': patch
'@objectstack/mcp': patch
---

feat(core): the bounded document text extractor is exported as `@objectstack/core/document-text`

Clause-②: yes (widening)
Metadata-protocol: none

The extractor MCP `read_file` reads PDF, Word, text and image files with now lives in `@objectstack/core` and is exported from a Node-only subpath, so a service that depends on the kernel package (an AI service handing a model a document's text instead of the document) uses it without depending on `@objectstack/mcp`:

```ts
import { extractFileContent, ReadFileExtractionBusyError } from '@objectstack/core/document-text';

const content = await extractFileContent(bytes, mimeType, fileName);
// { kind: 'text', text, truncated, format, note? } | { kind: 'image', data, mimeType } | { kind: 'none', reason }
```

It keeps every bound `read_file`'s extraction had (how many bytes to read at all stays the caller's cap: `read_file` keeps its 10 MiB `READ_FILE_MAX_BYTES`, and a text or image file is decoded on the calling thread at whatever size it is handed):

- at most 100,000 characters of text (`READ_FILE_TEXT_MAX_CHARS`), 200 PDF pages, a 32 MiB inflated Word body and a 5 MiB image;
- a CPU-time budget per PDF or Word extraction (`OS_MCP_READ_FILE_CPU_BUDGET_MS`, default 2000 ms), checked between pages and paragraphs, with a wall-clock ceiling of five budgets on a worker;
- a `worker_threads` pool and a process-wide cap on extractions in flight (`OS_MCP_READ_FILE_MAX_CONCURRENT`, default 4); one more is refused at once with `ReadFileExtractionBusyError` (`SERVICE_UNAVAILABLE`, 503, `retryAfterSeconds`), never queued.

Every caller in one process shares the bounds, the pool and the count, so the cap holds whoever asks, and through whichever copy of the package: an ESM `import` and a CJS `require` of the subpath (or two installed versions) share one state under a registered symbol, and `instanceof ReadFileExtractionBusyError` recognises a refusal from any copy. A host that bundles `@objectstack/core` into one file ships `dist/document-text-worker.js` (or `.cjs`) beside its bundle, or points the pool at wherever it put that file with `configureExtractionWorkerEntry` (the file is addressed by path; no `exports` entry names it). Without a worker, that copy extracts on the request thread under the same two bounds, said once at `warn`; another copy in the same process that finds its worker keeps reading on workers, and an idle worker serves any copy.

The subpath is kept off the main entry, so a host that never reads a document never loads `unpdf`, which is now a dependency of `@objectstack/core` instead of `@objectstack/mcp`. `read_file` reads through the shared extractor and answers as before; the two host variables keep their names and defaults. One fix on the way: the pool now copies the bytes it hands a worker into a fresh `Uint8Array` instead of slicing them, so a Node `Buffer` (whose `slice` is a view over shared memory) reads on a worker instead of failing to transfer, which `read_file` answered with its could-not-read sentence, and the caller's `Buffer` is left intact. One operator-visible change: the one-time `warn` when no worker can start now reads `[document-text] PDF and Word text (read_file and every other caller) is extracted on the request thread, not on a worker: …` (was `[MCP] read_file extracts PDF and Word text on the request thread, not on a worker: …`), since it now speaks for every caller; a log alert matching the old text needs the new one.
8 changes: 8 additions & 0 deletions content/docs/ai/connect-mcp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,14 @@ The defaults keep a self-hosted deployment reading files as before: a 9 MiB,
inside the budget. Both are read once when the server starts; a value that is
not a positive whole number stops the start with a message naming the variable.

The extractor behind `read_file` is the platform's shared one,
`@objectstack/core/document-text`, so an AI service in the same process reads
documents under the same two bounds: the cap counts every extraction in flight,
whoever asked for it. If no worker can start (a host that bundled the server
into one file), files are read on the request thread under the same two bounds,
said once in the log as `[document-text] PDF and Word text … is extracted on the
request thread, not on a worker`.

The tools are a fixed ~10-tool **spine** with the object name as a *parameter*
(`query_records(objectName, …)`), not one tool per object — so the list stays
usable on an app with hundreds of objects. Which objects and actions that spine
Expand Down
24 changes: 15 additions & 9 deletions docs/qa/platform-checklist/areas/ai.json
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@
"title": "read_file answers the text of a PDF and a Word document with its facts, stops a long extraction at the CPU-time budget with a note, refuses past the concurrent-reading cap with a retryable 503, and a malformed budget variable fails the boot naming it",
"since": "v18.4",
"status": "active",
"revision": 3,
"revision": 4,
"priority": "P1",
"surface": "api",
"personas": [
Expand Down Expand Up @@ -1295,7 +1295,7 @@
{
"clause": "a Word document answers its paragraphs as text",
"oracle": "network",
"verify": "facts carry content 'text' and the .docx mime type as format; the text block holds the document's paragraphs (packages/mcp/src/file-content.ts#extractFileContent)",
"verify": "facts carry content 'text' and the .docx mime type as format; the text block holds the document's paragraphs (packages/core/src/document-text-extract.ts#extractFileContent)",
"evidence": "both content blocks"
},
{
Expand All @@ -1307,13 +1307,13 @@
{
"clause": "an extraction over the CPU-time budget answers the text read so far, truncated, with the budget note",
"oracle": "network",
"verify": "facts carry content 'text', truncated true and a note opening 'Reading stopped at the CPU-time budget for one file (60 ms) after page', the budget the boot carries (packages/mcp/src/file-content.ts#pdfAnswer, #stoppedExtractionAnswer, #READ_FILE_CPU_BUDGET_MS_ENV). A budget spent before the first page's text answers the zero-page shape instead: content 'none', no truncated, and the budget sentence as the note (pdfAnswer's empty arm hands the budget note over as its reason). That shape is a fixture budget too low for the host, not this clause's verdict: record it and read again at a higher budget. Whether a stopped read must always carry truncated true is open for the maintainer (#32945); until it is ruled, the zero-page shape is no FAIL",
"verify": "facts carry content 'text', truncated true and a note opening 'Reading stopped at the CPU-time budget for one file (60 ms) after page', the budget the boot carries (packages/core/src/document-text-extract.ts#pdfAnswer, #stoppedExtractionAnswer, #READ_FILE_CPU_BUDGET_MS_ENV). A budget spent before the first page's text answers the zero-page shape instead: content 'none', no truncated, and the budget sentence as the note (pdfAnswer's empty arm hands the budget note over as its reason). That shape is a fixture budget too low for the host, not this clause's verdict: record it and read again at a higher budget. Whether a stopped read must always carry truncated true is open for the maintainer (#32945); until it is ruled, the zero-page shape is no FAIL",
"evidence": "the facts block"
},
{
"clause": "a malformed budget variable fails the boot naming the variable",
"oracle": "log",
"verify": "the boot fails with 'OS_MCP_READ_FILE_CPU_BUDGET_MS must be a positive integer; got \"abc\"' (packages/mcp/src/file-content.ts#configureReadFileExtraction, called from the MCP plugin's init)",
"verify": "the boot fails with 'OS_MCP_READ_FILE_CPU_BUDGET_MS must be a positive integer; got \"abc\"' (packages/core/src/document-text-extract.ts#configureReadFileExtraction, called from the MCP plugin's init)",
"evidence": "the exit code and the log line"
},
{
Expand All @@ -1336,10 +1336,10 @@
},
"source": [
"packages/mcp/src/file-content.ts#renderReadFile",
"packages/mcp/src/file-content.ts#extractFileContent",
"packages/mcp/src/file-content.ts#stoppedExtractionAnswer",
"packages/mcp/src/file-content.ts#configureReadFileExtraction",
"packages/mcp/src/file-content.ts#ReadFileExtractionBusyError",
"packages/core/src/document-text-extract.ts#extractFileContent",
"packages/core/src/document-text-extract.ts#stoppedExtractionAnswer",
"packages/core/src/document-text-extract.ts#configureReadFileExtraction",
"packages/core/src/document-text-extract.ts#ReadFileExtractionBusyError",
"packages/mcp/src/mcp-http-tools.ts#registerFileTools"
],
"history": [
Expand All @@ -1352,14 +1352,20 @@
{
"revision": 2,
"date": "2026-11-13",
"change": "assertion-defect correction of acceptance[3] (with fixtures.requires[2] and [3], steps[0] and steps[2]). At OS_MCP_READ_FILE_CPU_BUDGET_MS=1 the budget is spent before the first page, so pdfAnswer's empty arm answers content 'none' with the budget note and no truncated (packages/mcp/src/file-content.ts#pdfAnswer); at 60 the clause's shape appears. The fixture budget is raised to 60, the clause names the zero-page shape as a fixture signal to re-read at a higher budget, and leaves open whether a stopped read must always carry truncated true (if ruled so, that is a product defect in pdfAnswer's empty arm, not this item's). The receipt field is on showcase_invoice_line, not showcase_invoice (examples/app-showcase/src/data/objects/invoice.object.ts), so the fixture and steps name the invoice line. Found by the 18.4.0 acceptance run #32940 (F-4 to F-8 and its R4/R5 comments; wave anchor #32908). Re-read at main cf1b55c0. Card #32945",
"change": "assertion-defect correction of acceptance[3] (with fixtures.requires[2] and [3], steps[0] and steps[2]). At OS_MCP_READ_FILE_CPU_BUDGET_MS=1 the budget is spent before the first page, so pdfAnswer's empty arm answers content 'none' with the budget note and no truncated (packages/core/src/document-text-extract.ts#pdfAnswer, where the extractor moved in revision 4); at 60 the clause's shape appears. The fixture budget is raised to 60, the clause names the zero-page shape as a fixture signal to re-read at a higher budget, and leaves open whether a stopped read must always carry truncated true (if ruled so, that is a product defect in pdfAnswer's empty arm, not this item's). The receipt field is on showcase_invoice_line, not showcase_invoice (examples/app-showcase/src/data/objects/invoice.object.ts), so the fixture and steps name the invoice line. Found by the 18.4.0 acceptance run #32940 (F-4 to F-8 and its R4/R5 comments; wave anchor #32908). Re-read at main cf1b55c0. Card #32945",
"ref": "#32945"
},
{
"revision": 3,
"date": "2026-11-15",
"change": "acceptance[5].verify names `--reporter=tree`, the reporter that prints the one line per pin file this is scored on: in a runner's non-TTY shell vitest's default reporter prints the run's totals only (RUNNER.md's environment facts; QA run #33784's note 10, comment 6893243542). No clause or threshold changed. Card #33969",
"ref": "#34068"
},
{
"revision": 4,
"date": "2026-12-02",
"change": "anchor repoint, no clause, step, fixture or threshold changed: the extractor read_file reads through (its caps, the CPU budget, the worker pool, the cap on reads in flight and the busy refusal) moved from packages/mcp/src/file-content.ts to the shared packages/core/src/document-text-extract.ts (published through the curated entry packages/core/src/document-text.ts, export @objectstack/core/document-text), so acceptance[1], [3] and [4], source and revision 2's citation name it there; renderReadFile and READ_FILE_MAX_BYTES stay in the MCP file. The worker-pool pins moved beside the pool (packages/core/src/document-text.worker-pool.test.ts); the pins acceptance[5] scores stay in automated.ref. Card #39008",
"ref": "#39008"
}
]
},
Expand Down
11 changes: 11 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
"types": "./dist/logger.d.cts",
"default": "./dist/logger.cjs"
}
},
"./document-text": {
"import": {
"types": "./dist/document-text.d.ts",
"default": "./dist/document-text.js"
},
"require": {
"types": "./dist/document-text.d.cts",
"default": "./dist/document-text.cjs"
}
}
},
"scripts": {
Expand All @@ -60,6 +70,7 @@
"@objectstack/spec": "workspace:*",
"@objectstack/types": "workspace:*",
"undici": "^7.30.1",
"unpdf": "1.9.1",
"zod": "^4.6.6"
},
"keywords": [
Expand Down
Loading
Loading