Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(documents): normalize large delimited row sets without stack overflow

Afloate41c9d7134a2
diff --git a/apps/node-services/AGENTS.md b/apps/node-services/AGENTS.md
index 66751866..1640db74 100644
--- a/apps/node-services/AGENTS.md
+++ b/apps/node-services/AGENTS.md
@@ -105,6 +105,12 @@ not make an empty message indexable.
focused work; `NODE_SERVICES_ROLES=agent` (or another role) narrows it.
- Memory CLIs: `pnpm --filter @linger/node-services memory:<command>`.
+## Large text documents
+
+CSV and TSV normalization accepts large row counts within the existing source
+and Markdown byte limits. An output that exceeds its configured byte budget
+fails with `DOCUMENT_MARKDOWN_TOO_LARGE`, independently of the number of rows.
+
## Image
- The `Dockerfile` builds one image that runs every role: port 4319 for the
diff --git a/apps/node-services/src/document-normalizer/document-normalizer-core.ts b/apps/node-services/src/document-normalizer/document-normalizer-core.ts
index e602f9f3..10f3bf77 100644
--- a/apps/node-services/src/document-normalizer/document-normalizer-core.ts
+++ b/apps/node-services/src/document-normalizer/document-normalizer-core.ts
@@ -237,7 +237,7 @@ function delimitedToMarkdown(text: string, delimiter: string): string {
if (rows.length === 0) {
throw new DocumentNormalizationError("DOCUMENT_SOURCE_EMPTY");
}
- const width = Math.max(...rows.map((row) => row.length));
+ const width = rows.reduce((max, row) => Math.max(max, row.length), 0);
const padded = rows.map((row) =>
Array.from({ length: width }, (_, index) =>
(row[index] ?? "").replaceAll("\n", " ").replaceAll("|", "\\|"),
diff --git a/apps/node-services/tests/document-normalizer/document-normalizer.test.ts b/apps/node-services/tests/document-normalizer/document-normalizer.test.ts
index b1e09d23..49f584fc 100644
--- a/apps/node-services/tests/document-normalizer/document-normalizer.test.ts
+++ b/apps/node-services/tests/document-normalizer/document-normalizer.test.ts
@@ -14,6 +14,7 @@ import {
DocumentNormalizationError,
DOCX_MEDIA_TYPE,
GZIP_MEDIA_TYPE,
+ MAX_DOCUMENT_MARKDOWN_BYTES,
MAX_DOCUMENT_SOURCE_BYTES,
PDF_MEDIA_TYPE,
PNG_MEDIA_TYPE,
@@ -299,6 +300,39 @@ describe("document normalization", () => {
).resolves.toBe("# Notes\n");
});
+ it.each([
+ ["text/csv", ","],
+ ["text/tab-separated-values", "\t"],
+ ])(
+ "normalizes large delimited documents within the byte limits: %s",
+ async (mediaType, delimiter) => {
+ const rows = 200_000;
+ const source = encode(
+ `name${delimiter}count\n${`item${delimiter}1\n`.repeat(rows)}`,
+ );
+ const expected = `| name | count |\n| --- | --- |\n${"| item | 1 |\n".repeat(rows)}`;
+ expect(source.byteLength).toBeLessThan(MAX_DOCUMENT_SOURCE_BYTES);
+ expect(encode(expected).byteLength).toBeLessThan(
+ MAX_DOCUMENT_MARKDOWN_BYTES,
+ );
+ await expect(
+ normalizeDocumentContent({ mediaType, source }).then(decode),
+ ).resolves.toBe(expected);
+ },
+ );
+
+ it("keeps the Markdown size error for a large delimited document", async () => {
+ await expect(
+ normalizeDocumentContent(
+ {
+ mediaType: "text/csv",
+ source: encode(`name,count\n${"item,1\n".repeat(200_000)}`),
+ },
+ 1024,
+ ),
+ ).rejects.toMatchObject({ code: "DOCUMENT_MARKDOWN_TOO_LARGE" });
+ });
+
it("normalizes LF and one trailing newline and enforces output size", () => {
expect(decode(canonicalizeMarkdown("Cafe\u0301\r\nline\r\n\r\n"))).toBe(
"Caf\u00e9\nline\n",