Skip to content

Studio: support source code files in workspace folders and project knowledge bases - #10395

Merged
danielhanchen merged 14 commits into
unslothai:mainfrom
harshaygadekar:fix/studio-source-files-indexing
Oct 5, 2026
Merged

danielhanchen merged 14 commits into
unslothai:mainfrom
harshaygadekar:fix/studio-source-files-indexing

Conversation

@harshaygadekar

@harshaygadekar harshaygadekar commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #10300 (continuation of #8843).

Earlier, PR #9495 added support for dropping source code files (.py, .js, .cs, .php, etc.) into the chat box. However, the backend RAG pipeline and the Project Knowledge Base still had a hardcoded list restricted to 7 document extensions (.pdf, .txt, .md, .markdown, .docx, .html, .htm).

Because of this:

  1. Adding code files to Project Sources was rejected with an HTTP 400 error (unsupported file type).
  2. Linking a local workspace folder skipped all source code files during synchronization, leaving project folders empty.
  3. The Project drag-and-drop zone and file picker did not accept source code files.

This pull request opens up source code and configuration files for reading, parsing, indexing, and knowledge-base search across Studio.


What changes in this PR?

  1. RAG Extensions Expansion:

    • Added standard source and configuration file extensions (.py, .js, .ts, .cs, .php, .c, .cpp, .rs, .go, .json, .yaml, .toml, etc.) to the backend's allowed extensions list in core/rag/config.py.
    • Kept the original UPLOAD_EXTS intact so existing desktop/Tauri contract tests remain completely untouched.
  2. Universal Plain-Text Parsing:

    • Updated parsers.py to route all text and source code extensions to standard UTF-8 text decoding (utf-8-sig) with BOM stripping.
    • Added a null-byte check (\x00) to safely reject corrupt or binary files masquerading as code without crashing the ingestion worker.
  3. Workspace Folder Sync Safety:

    • Added an ignore list (_IGNORE_SCAN_DIRS) in folder_sync.py so scanning a project folder skips dependency and build folders (node_modules, .git, .venv, __pycache__, dist, build).
  4. Frontend Project Dropzone & File Pickers:

    • Updated the Project Sources dropzone and file input to accept source code files alongside standard documents.
    • Defined source extensions directly inside rag.ts as a self-contained leaf constant to prevent circular imports between the chat and rag features.
  5. Safe Document Previews:

    • Updated routes/rag.py to serve source code previews as text/plain; charset=utf-8 so browsers can display them safely without script execution risks.

Verification & Tests

  • Backend Unit Tests:
    Added studio/backend/tests/test_rag_source_files_indexing.py with 7 tests covering:

    • Parsing source files (.php, .cs, .js, .py, .json, .yaml)
    • BOM stripping on Windows-encoded files
    • Null-byte binary file rejection
    • Skipping node_modules and .git while scanning project files
    • Document upload endpoint extension validation
    • Desktop native drop cryptographic grant validation
    • Result: 7 / 7 passed (100%).
  • Frontend Tests & Checks:

    • tests/chat-barrel-module-scope-reads.test.ts: 16 / 16 passed (no module-scope cycle regressions).
    • tests/project-source-save.test.ts: 10 / 10 passed.
    • tests/rag-source-drop-filter.test.ts: 9 / 9 passed.
    • Full frontend test suite (node --test): 6,920+ tests passed cleanly.
    • Type checking (npm run typecheck): 0 errors.
    • Production build (npm run build): completed with exit code 0.
  • End-to-End Live Testing:

    • Uploaded 7 sample source files (service.ts, Program.cs, main.py, index.php, deploy.yaml, config.json, app.js) to a Project Knowledge Base.
    • Confirmed all 7 files were parsed, chunked, and embedded into SQLite-vec with "completed" chips.
    • Asked queries in Chat regarding the code; verified the assistant retrieved the code snippets and correctly grounded its answers with interactive citation chips ([1], [2], [3]).

Before:
Screenshot 2026-09-06 220317

Snapshots after the fix:
Screenshot 2026-09-06 221912
Screenshot 2026-09-06 221926
Screenshot 2026-09-06 222009
Screenshot 2026-09-06 222252
Screenshot 2026-09-06 222603

Update (maintainer rework, 915fffe)

Merged latest main (resolves the conflict with RAG_UPLOAD_EXTS from #11499) and slimmed the change:

  • Backend: one SOURCE_TEXT_EXTS set in core/rag/config.py is folded into UPLOAD_EXTS, so routes, ingestion, folder sync and the RAG_UPLOAD_EXTS narrowing all keep using the same name. A test pins it to the chat's TEXT_ATTACHMENT_EXTENSIONS, so the two lists cannot drift.
  • The NUL-byte check applies only to source extensions, so .txt / .md ingestion is unchanged.
  • Folder sync skips VCS, node_modules, cache dirs and virtualenvs (by pyvenv.cfg, so a Terraform env/ folder is still indexed), plus .env* and lock files.
  • Frontend: the project and knowledge base pickers and drop zones reuse TEXT_ATTACHMENT_EXTENSIONS (via source-drop-policy.ts, outside the chat barrel cycle); extensionless names the backend would reject are no longer accepted.

Before / after on the project Sources tab, uploading notes.md and index.php through the picker (Program.cs via the API: 400 before, 200 after):

Project sources before and after

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Expands Studio’s RAG/Project Knowledge Base ingestion and UI to accept and index common source-code/config files, aligning drop/upload behavior with backend parsing and folder sync.

Changes:

  • Broadened backend allowed extensions, text parsing, and folder sync scanning/ignores for source/config files.
  • Updated frontend accept filters and supported-name checks for Project Sources uploads/drops.
  • Added backend + frontend tests covering source-file acceptance, parsing safety checks, and folder scanning behavior.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
studio/frontend/tests/rag-source-drop-filter.test.ts Adds regression test ensuring drops accept common code/config extensions.
studio/frontend/src/features/rag/types/rag.ts Introduces a large source extension list, new accept string, and updated isSupportedSourceName.
studio/frontend/src/features/rag/components/use-source-drop.ts Updates unsupported-file toast copy to mention source code.
studio/frontend/src/features/rag/components/project-sources-panel.tsx Switches file picker/drop accept filters to source-capable accept list.
studio/frontend/src/features/rag/components/project-source-dropzone.tsx Switches accept filter + updates toast copy for unsupported drops.
studio/backend/tests/test_rag_source_files_indexing.py Adds unit tests for source parsing, BOM stripping, binary rejection, ignore dirs, and extension validation.
studio/backend/routes/rag.py Expands upload validation extensions; serves text previews as text/plain fallback for text extensions.
studio/backend/core/rag/parsers.py Routes all TEXT_EXTS through UTF-8-SIG decode; rejects null-byte “binary” masquerading as text.
studio/backend/core/rag/ingestion.py Validates ingestion extensions against expanded allowlist.
studio/backend/core/rag/folder_sync.py Skips common dependency/build dirs; scans files using expanded allowlist.
studio/backend/core/rag/config.py Introduces richer extension taxonomy (BASE_TEXT_EXTS, SOURCE_TEXT_EXTS, TEXT_EXTS, ALL_UPLOAD_EXTS).
Suppressed comments (1)

studio/frontend/src/features/rag/types/rag.ts:3

  • Frontend now accepts many extensions that the backend allowlist does not appear to include (e.g. .ipynb, .csproj, .jsonc, .json5, .geojson, etc.). This will cause confusing UX where the picker/drop accepts a file but the backend rejects it (HTTP 400 / ingestion ValueError). Align the frontend list to the backend’s ALL_UPLOAD_EXTS contract (or expand backend support to match), ideally from a single shared source-of-truth or an endpoint-provided allowlist.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread studio/backend/core/rag/config.py Resolved
Comment thread studio/backend/core/rag/config.py Resolved
Comment thread studio/backend/routes/rag.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member

The direction is right, but the frontend list in studio/frontend/src/features/rag/types/rag.ts accepts extensions the backend ALL_UPLOAD_EXTS does not (.ipynb, .csproj, .jsonc, .csv, .svg), so the picker will take files the upload route answers 400 on; could you drive both from one list? Also _IGNORE_SCAN_DIRS skips bin, build, dist, obj and target, which would drop already-indexed documents for anyone whose linked folder uses those names, so I would like to understand that before reviewing.

@harshaygadekar

Copy link
Copy Markdown
Contributor Author

The direction is right, but the frontend list in studio/frontend/src/features/rag/types/rag.ts accepts extensions the backend ALL_UPLOAD_EXTS does not (.ipynb, .csproj, .jsonc, .csv, .svg), so the picker will take files the upload route answers 400 on; could you drive both from one list? Also _IGNORE_SCAN_DIRS skips bin, build, dist, obj and target, which would drop already-indexed documents for anyone whose linked folder uses those names, so I would like to understand that before reviewing.

hey, @danielhanchen! I've pushed a commit (latest) addressing both points:

  1. extension parity & shared List:
    Yeah, regarding driving both from a single list: python wheels are distributed via PyPI and cannot import or access frontend TypeScript source files at runtime, and on the frontend, the file picker needs the accept attribute synchronously on initial render before any backend API responses arrive. adding to that, inside the frontend, importing extensions from @/features/chat into @/features/rag violates the barrel import boundary (chat already imports rag, so re-importing created a circular cycle caught by chat-barrel-module-scope-reads.test.ts).

    so, to solve this cleanly, I followed the exact pattern the repo already uses for document extensions (where RAG_UPLOAD_ACCEPT in TypeScript, UPLOAD_EXTS in Python, and ATTACHMENT_EXTS in Rust are defined natively in their respective languages):

    • Expanded SOURCE_TEXT_EXTS and ALL_UPLOAD_EXTS in studio/backend/core/rag/config.py with all 284 extensions (including .ipynb, .csproj, .jsonc, .csv, .svg, etc.), so the backend accepts every single type the frontend allows.
    • Also, added an automated parity unit test in studio/frontend/tests/native-drop-paths.test.ts that statically checks RAG_SOURCE_UPLOAD_ACCEPT against config.py to assert that every frontend-accepted extension is covered by backend ALL_UPLOAD_EXTS, preventing any future drift in CI.
  2. linked folder scanning (_IGNORE_SCAN_DIRS):
    good catch on this. like I originally added bin, build, dist, target, etc., thinking of build output artifacts, without realizing it would drop files for users who link folders or repositories with those exact names.
    I've removed those folder names from _IGNORE_SCAN_DIRS. So, only standard version control and environment folders (.git, node_modules, venv, etc.) remain ignored. any actual compiled binaries inside a build folder are already filtered out safely by the allowed file extensions anyway.

  3. UI/fronten toast:
    Cleaned up the unsupported file drop toast message in use-source-drop.ts so it shows clean text instead of raw extension lists.

finally. all unit tests (pytest, typecheck, and node --test) are passing clean!

@danielhanchen

Copy link
Copy Markdown
Member

Checked the updated branch: ALL_UPLOAD_EXTS now covers the frontend list and the build/dist names are gone from _IGNORE_SCAN_DIRS, thanks for both. One thing before I review: folder_sync now accepts around 290 extensions, so an already linked folder will re-scan and index files it never did before, including .env and lock files; could you confirm what that does to an existing linked repo on the next sync?

@harshaygadekar

Copy link
Copy Markdown
Contributor Author

Checked the updated branch: ALL_UPLOAD_EXTS now covers the frontend list and the build/dist names are gone from _IGNORE_SCAN_DIRS, thanks for both. One thing before I review: folder_sync now accepts around 290 extensions, so an already linked folder will re-scan and index files it never did before, including .env and lock files; could you confirm what that does to an existing linked repo on the next sync?

On the next sync cycle, existing indexed files won't be touched or re-embedded unless they were modified. any newly recognized source files will be picked up as new and ingested in the background.

and about .env and lock files, lock files just add hash noise to retrieval, and .env files shouldn't be indexed automatically for security reasons.

So, I went ahead and added an ignore filter in folder_sync to skip lock files (*.lock, package-lock.json, pnpm-lock.yaml, etc.) and *.env* files during folder scans. they can still be attached manually if a user explicitly drops one in.

resolved the conflict!

@danielhanchen danielhanchen self-assigned this Oct 5, 2026
Resolve conflicts with RAG_UPLOAD_EXTS (unslothai#11499) and rework the change:
- backend: one SOURCE_TEXT_EXTS set folded into UPLOAD_EXTS, so every caller and
  the RAG_UPLOAD_EXTS narrowing keep working; parity test against the chat's
  TEXT_ATTACHMENT_EXTENSIONS instead of a second hand-kept list
- NUL rejection only for source extensions, so .txt behaviour is unchanged
- folder sync: skip venvs by pyvenv.cfg instead of the name 'env' (Terraform env/ dirs)
- frontend: reuse TEXT_ATTACHMENT_EXTENSIONS from source-drop-policy (no barrel cycle),
  knowledge base picker accepts the same types as its drop zone, drop the
  extensionless names the backend would 400 on
@danielhanchen

Copy link
Copy Markdown
Member

Review record for #10395, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#431.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 948992cc2 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/core/rag/config.py (on the mirror)

@@ -41,13 +41,41 @@
 
 # Types parsers.parse handles; frontend and Rust parity tests read this literal.
 SUPPORTED_UPLOAD_EXTS = {".pdf", ".txt", ".md", ".markdown", ".docx", ".html", ".htm"}
+# Source and config files, decoded like .txt; the chat's TEXT_ATTACHMENT_EXTENSIONS minus the above (parity test).
+SOURCE_TEXT_EXTS = frozenset(
+    """
+    .text .log .mdx .rst .adoc .asciidoc .org .textile .wiki .tex .latex .sty .cls .bib .rmd .qmd
+    .srt .vtt .sbv .ass .ssa .sub .lrc
+    .csv .tsv .psv .json .jsonl .ndjson .jsonc .json5 .geojson .har .avsc .xml .yaml .yml .toml .ini
+    .cfg .conf .cnf .env .properties .plist .edn .ron .cue .lock .mod .sum .reg .desktop .service
+    .po .pot .strings .resx .xliff .xlf .ics .vcf .eml .mbox .m3u8 .pls
+    .css .scss .sass .less .styl .svg .vue .svelte .astro .pug .jade .haml .slim .ejs .erb .hbs
+    .handlebars .mustache .njk .jinja .jinja2 .j2 .twig .liquid .cshtml .razor .aspx .jsp .tpl .qml
+    .js .jsx .mjs .cjs .ts .tsx .mts .cts .py .pyi .pyx .pxd .ipynb
+    .java .kt .kts .scala .groovy .gradle .sbt .clj .cljs .cljc
+    .c .h .cc .cpp .hpp .cxx .hxx .hh .ipp .inl .cu .cuh .rs .go .zig .odin .nim .nims .nimble .cr .d
+    .v .sv .svh .vhd .vhdl .asm .s
+    .cs .vb .vbs .fs .fsi .fsx .csproj .vbproj .fsproj .sln .props .targets
+    .m .mm .swift .applescript .metal
+    .rb .rake .gemspec .podspec .php .pl .pm .r .jl .lua .tcl .dart .hx .hs .lhs .ml .mli .ex .exs
+    .erl .hrl .rkt .scm .ss .lisp .lsp .cl .el .pas .pp .ada .adb .ads .cob .cbl .f .for .f90 .f95
+    .f03 .sas .awk .sed .m4 .sol .move .cairo .mojo .gd .sqf
+    .sh .bash .zsh .fish .ksh .csh .tcsh .nu .ps1 .psm1 .psd1 .bat .cmd
+    .sql .psql .plsql .hql .cql .graphql .gql .proto .thrift .capnp .prisma
+    .tf .tfvars .tfstate .hcl .nix .dhall .bicep .dockerfile .containerfile .makefile .mk .mak .cmake

P1 Badge Exclude Terraform state from linked-folder indexing

When a linked Terraform workspace contains terraform.tfstate or another *.tfstate file, adding this extension to UPLOAD_EXTS makes _scan ingest it automatically; _is_ignored_scan_file only excludes environment and lock files. Terraform state commonly contains plaintext sensitive resource attributes and outputs, so retrieval can subsequently paste credentials or secrets into model prompts. Keep .tfstate out of linked-folder scans, even if explicit uploads remain supported.

Useful? React with 👍 / 👎.

Accepted on the mirror

Reply from danielhanchen:

Fixed in dea626f: folder sync now skips *.tfstate alongside .env and lockfiles, with a scan test.

Round 2: reviewed mirror c609d6d36 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/core/rag/config.py (on the mirror)

@@ -41,13 +41,41 @@
 
 # Types parsers.parse handles; frontend and Rust parity tests read this literal.
 SUPPORTED_UPLOAD_EXTS = {".pdf", ".txt", ".md", ".markdown", ".docx", ".html", ".htm"}
+# Source and config files, decoded like .txt; the chat's TEXT_ATTACHMENT_EXTENSIONS minus the above (parity test).
+SOURCE_TEXT_EXTS = frozenset(
+    """
+    .text .log .mdx .rst .adoc .asciidoc .org .textile .wiki .tex .latex .sty .cls .bib .rmd .qmd
+    .srt .vtt .sbv .ass .ssa .sub .lrc
+    .csv .tsv .psv .json .jsonl .ndjson .jsonc .json5 .geojson .har .avsc .xml .yaml .yml .toml .ini
+    .cfg .conf .cnf .env .properties .plist .edn .ron .cue .lock .mod .sum .reg .desktop .service
+    .po .pot .strings .resx .xliff .xlf .ics .vcf .eml .mbox .m3u8 .pls
+    .css .scss .sass .less .styl .svg .vue .svelte .astro .pug .jade .haml .slim .ejs .erb .hbs
+    .handlebars .mustache .njk .jinja .jinja2 .j2 .twig .liquid .cshtml .razor .aspx .jsp .tpl .qml
+    .js .jsx .mjs .cjs .ts .tsx .mts .cts .py .pyi .pyx .pxd .ipynb
+    .java .kt .kts .scala .groovy .gradle .sbt .clj .cljs .cljc
+    .c .h .cc .cpp .hpp .cxx .hxx .hh .ipp .inl .cu .cuh .rs .go .zig .odin .nim .nims .nimble .cr .d
+    .v .sv .svh .vhd .vhdl .asm .s
+    .cs .vb .vbs .fs .fsi .fsx .csproj .vbproj .fsproj .sln .props .targets
+    .m .mm .swift .applescript .metal
+    .rb .rake .gemspec .podspec .php .pl .pm .r .jl .lua .tcl .dart .hx .hs .lhs .ml .mli .ex .exs
+    .erl .hrl .rkt .scm .ss .lisp .lsp .cl .el .pas .pp .ada .adb .ads .cob .cbl .f .for .f90 .f95
+    .f03 .sas .awk .sed .m4 .sol .move .cairo .mojo .gd .sqf
+    .sh .bash .zsh .fish .ksh .csh .tcsh .nu .ps1 .psm1 .psd1 .bat .cmd
+    .sql .psql .plsql .hql .cql .graphql .gql .proto .thrift .capnp .prisma
+    .tf .tfvars .tfstate .hcl .nix .dhall .bicep .dockerfile .containerfile .makefile .mk .mak .cmake

P1 Badge Exclude Terraform variable files from folder scans

When a linked Terraform workspace stores credentials in *.tfvars, this new allowlist causes _scan to ingest them automatically. Although the current revision added a *.tfstate exclusion in _is_ignored_scan_file, that predicate does not exclude .tfvars, so sensitive variable values become searchable and may be copied into model prompts. Exclude *.tfvars from linked-folder scans while retaining explicit uploads if desired.

Useful? React with 👍 / 👎.

Rejected on the mirror

Reply from danielhanchen:

Not changing: .tfvars is normally committed variable config that users want searchable; unlike .tfstate it does not embed provider secrets by construction, and any config format can hold a secret.

chatgpt-codex-connector[bot] on studio/frontend/src/features/rag/components/source-drop-policy.ts (on the mirror)

@@ -1,7 +1,23 @@
 // SPDX-License-Identifier: AGPL-3.0-only
 // Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
 
-import { isSupportedSourceName } from "../types/rag.ts";
+import { TEXT_ATTACHMENT_EXTENSIONS } from "../../chat/text-attachment-accept.ts";
+import { RAG_UPLOAD_ACCEPT } from "../types/rag.ts";
+
+// Projects and knowledge bases also index source files; backend SOURCE_TEXT_EXTS mirrors this list.
+export const RAG_SOURCE_UPLOAD_ACCEPT = [
+  ...new Set([...RAG_UPLOAD_ACCEPT.split(","), ...TEXT_ATTACHMENT_EXTENSIONS]),
+].join(",");
+
+const ACCEPTED_UPLOAD_EXTS = new Set(RAG_SOURCE_UPLOAD_ACCEPT.split(","));
+
+// `accept` only filters the picker, so a drop can carry anything, including an
+// extension-less folder entry the backend would reject.
+export function isSupportedSourceName(name: string): boolean {
+  const dot = name.lastIndexOf(".");
+  if (dot <= 0) return false;

P2 Badge Accept standard extensionless build files

When adding a conventional Dockerfile, Containerfile, or Makefile, lastIndexOf(".") returns -1, so the new project drop path rejects it; linked-folder scanning and backend uploads likewise see an empty suffix and skip or reject it. Chat already handles these names through TEXT_ATTACHMENT_BASENAMES, but this policy reuses only TEXT_ATTACHMENT_EXTENSIONS, meaning the newly added .dockerfile, .containerfile, and .makefile tokens support only nonstandard names such as app.dockerfile. Carry the basename allowlist through both frontend validation and backend ingestion.

Useful? React with 👍 / 👎.

Rejected on the mirror

Reply from danielhanchen:

Intentional: the backend accepts by extension, so an extensionless Dockerfile would 400 after upload. The drop zone now names it as unsupported up front instead; accepting such names end to end is a separate feature.

Round 3: reviewed mirror c609d6d36 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: c609d6d365

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror c609d6d36 (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Workspace files with extensions (.cs/.php/.js/…) are still inaccessible for reading, writing, and indexing — continuation of topic #8843.

3 participants