-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add SKIP_DIRS, file-count guard, lower max size to 2MB #128 #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
db9c2f8
feat: add SKIP_DIRS, file-count guard, lower max size to 2MB #128
four-bytes-robby ce4c572
feat: add slow-file warning logging (30s threshold, app.log) #128
four-bytes-robby cae2dd2
fix: record timeout errors in IngestResult.errors
four-bytes-robby 54ca790
fix: update test to match 2MB cap
four-bytes-robby c5d956a
fix: update test comment to reflect actual 3MB size
four-bytes-robby afe261b
fix: stale timeout comment, type annotation, double-counting
four-bytes-robby 4161dc3
fix: cancel processFile on timeout with abort flag
four-bytes-robby 9d56d07
fix: revert counter increments on abort
four-bytes-robby f9e3318
fix: remove unused import, verify SAVEPOINT cleanup
four-bytes-robby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| // 1. Resolve path | ||
| // 2. Walk files (recursive if dir, single if file) | ||
| // 3. Detect language from extension | ||
| // 4. 10MB file size cap (skip oversized files, never read content → avoid OOM) | ||
| // 4. 2MB file size cap (skip oversized files, never read content → avoid OOM) | ||
| // 5. Content-hash check against files table → skip unchanged | ||
| // 6. Upsert files table (FK-preserving: ON CONFLICT DO UPDATE, keeps rowid) | ||
| // 7. Insert documents table (dedup via BEFORE INSERT trigger) | ||
|
|
@@ -21,7 +21,7 @@ import type { Database } from "bun:sqlite"; | |
| import { generateId, hashBuffer, hashContent, checkpointDatabase } from "../schema"; | ||
| import { log } from "../logger"; | ||
| import { ingestMutex } from "./mutex"; | ||
| import { resolveFiles, detectLanguage, isBinaryContent, type WalkResult } from "./loader"; | ||
| import { resolveFiles, isBinaryContent, type WalkResult, type WalkedFile } from "./loader"; | ||
| import { chunkContent, type Chunk } from "./chunker"; | ||
| import { extractSymbols } from "./symbolExtractor"; | ||
| import { embedChunks } from "./embed"; | ||
|
|
@@ -58,11 +58,14 @@ export interface IngestOptions { | |
| // Constants | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /** Maximum file size for ingestion (10 MB). Files larger than this are skipped. */ | ||
| const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB | ||
| /** Maximum file size for ingestion (2 MB). Files larger than this are skipped. */ | ||
| const MAX_FILE_SIZE = 2 * 1024 * 1024; // 2 MB | ||
|
|
||
| /** Per-file processing timeout (10 seconds). */ | ||
| const FILE_TIMEOUT_MS = 10_000; // 10 seconds per file | ||
| /** Per-file processing timeout (30 seconds). */ | ||
| const FILE_TIMEOUT_MS = 30_000; // 30 seconds per file | ||
|
|
||
| /** Files exceeding this duration get logged to app.log as a warning. */ | ||
| const SLOW_FILE_WARN_MS = 30_000; // 30 seconds | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Progress event helpers (gated on BRAIN_DEBUG=true) | ||
|
|
@@ -147,10 +150,24 @@ export async function ingestPath( | |
| return result; | ||
| } | ||
|
|
||
| // ── File-count guard: warn at 500, hard-abort at 5000 ────────────── | ||
| const FILE_COUNT_WARN = 500; | ||
| const FILE_COUNT_ABORT = 5000; | ||
| if (walkedFiles.length > FILE_COUNT_ABORT) { | ||
| result.errors.push(`Aborted: ${walkedFiles.length} files found — exceeds hard limit of ${FILE_COUNT_ABORT}. Use a more specific path.`); | ||
| result.durationMs = Date.now() - startTime; | ||
| return result; | ||
| } | ||
| if (walkedFiles.length > FILE_COUNT_WARN) { | ||
| log("warn", "ingest", `Large ingest: ${walkedFiles.length} files — may take a while`); | ||
| } | ||
|
|
||
| // Yield so event loop can handle HTTP requests + tool calls before ingest starts | ||
| await new Promise(r => setTimeout(r, 0)); | ||
|
|
||
| async function processFile(walked: WalkResult, i: number): Promise<void> { | ||
| const abortFlags = new Map<number, boolean>(); | ||
|
|
||
| async function processFile(walked: WalkedFile, i: number, aborted: Map<number, boolean>): Promise<void> { | ||
| const filePath = walked.path; | ||
| const language = walked.language; | ||
| const fileStart = Date.now(); | ||
|
|
@@ -161,7 +178,7 @@ export async function ingestPath( | |
| file: filePath, | ||
| }); | ||
|
|
||
| // ── 4. 10MB file size cap (before reading content) ─────────────── | ||
| // ── 4. 2MB file size cap (before reading content) ─────────────── | ||
| let fileStats; | ||
| try { | ||
| fileStats = await stat(filePath); | ||
|
|
@@ -172,7 +189,7 @@ export async function ingestPath( | |
|
|
||
| if (fileStats.size > MAX_FILE_SIZE) { | ||
| result.errors.push( | ||
| `Skipped ${filePath}: file size ${fileStats.size} exceeds 10MB cap`, | ||
| `Skipped ${filePath}: file size ${fileStats.size} exceeds 2MB cap`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
@@ -248,6 +265,10 @@ export async function ingestPath( | |
| const fileName = filePath.split("/").pop() ?? filePath; | ||
| const filetype = filePath.split(".").pop() ?? "unknown"; | ||
|
|
||
| // Snapshot result counters so we can revert this file's increments on abort | ||
| const docCountBefore = result.documentsCreated; | ||
| const chunkCountBefore = result.chunksCreated; | ||
|
|
||
| try { | ||
| db.run( | ||
| `INSERT OR IGNORE INTO documents (id, title, content, content_hash, type, path, language, filetype, project_hash) | ||
|
|
@@ -375,6 +396,15 @@ export async function ingestPath( | |
| } | ||
| } | ||
|
|
||
| // ── Abort guard: if timeout fired, ROLLBACK (don't commit) and skip counters ── | ||
| if (aborted.get(i)) { | ||
| db.exec(`ROLLBACK TO SAVEPOINT ${spFile}`); | ||
| db.exec(`RELEASE SAVEPOINT ${spFile}`); | ||
| result.chunksCreated -= result.chunksCreated - chunkCountBefore; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Abort counter rollback is not concurrency-safe and can undercount successful files. Timed-out file completion can reset global totals to stale snapshots after later files already updated them. Prompt for AI agents |
||
| result.documentsCreated -= result.documentsCreated - docCountBefore; | ||
| return; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // ── RELEASE per-file SAVEPOINT ───────────────────────────── | ||
| db.exec(`RELEASE SAVEPOINT ${spFile}`); | ||
|
|
||
|
|
@@ -386,20 +416,27 @@ export async function ingestPath( | |
| if (process.env.BRAIN_DEBUG === "true") { | ||
| log("debug", "ingest", `processed ${result.filesProcessed}/${walkedFiles.length}: ${filePath} (${Date.now() - fileStart}ms)`); | ||
| } | ||
|
|
||
| // Slow-file warning: log to app.log for diagnostics | ||
| const elapsed = Date.now() - fileStart; | ||
| if (elapsed > SLOW_FILE_WARN_MS) { | ||
| log("warn", `ingest.slow_file.${filePath}`, `${filePath} took ${elapsed}ms`, { path: filePath, durationMs: elapsed, size }); | ||
| } | ||
| } | ||
|
|
||
| for (const [i, walked] of walkedFiles.entries()) { | ||
| try { | ||
| await Promise.race([ | ||
| processFile(walked, i), | ||
| processFile(walked, i, abortFlags), | ||
| new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), FILE_TIMEOUT_MS)) | ||
| ]); | ||
| } catch (err) { | ||
| if (err instanceof Error && err.message === 'timeout') { | ||
| abortFlags.set(i, true); | ||
| emitProgressEvent("ingest.file_timeout", { file: walked.path }); | ||
| result.errors.push(`Timeout processing ${walked.path}: exceeded ${FILE_TIMEOUT_MS}ms`); | ||
| result.filesProcessed++; | ||
| continue; | ||
| log("warn", "ingest.timeout", `Timeout processing ${walked.path}: exceeded ${FILE_TIMEOUT_MS}ms`, { path: walked.path, timeoutMs: FILE_TIMEOUT_MS }); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| result.errors.push(`Timeout after ${FILE_TIMEOUT_MS}ms: ${walked.path}`); | ||
| continue; // Don't increment filesProcessed — processFile handles it if it completes | ||
| } | ||
| throw err; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.