-
Notifications
You must be signed in to change notification settings - Fork 2
Fixes for extension .lock database contention and tool improvements to avoid LLM use of grep
#119
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 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
90082e6
Resolve database lock contention w/ vscode-codeql
data-douser 5186ae9
Address PR review comments
data-douser 5c38a91
Address more PR review comments
data-douser 192c6b8
Add search_ql_code and codeql_resolve_files tools
data-douser 7775f49
Add "after" files for query evaluation integration tests
data-douser 5fb6146
address Code Scanning TOCTOU race and PR review feedback
data-douser 73b1fef
Stream large files instead of loading into memory
data-douser 29da2f5
Fix tool issues found during explain-codeql-query workflow testing
data-douser fad2f61
[UPDATE PRIMITIVE] Fix transient HTTP 503 failures in install-packs.s…
Copilot 871bc75
deterministic profiler output and search efficiency
data-douser 87250fa
Fix TOCTOU bug for search_ql_code tool
data-douser 88cb3b7
Stream-count large files & detect ambiguous DB paths
data-douser d6656ca
Address latest PR review comments
data-douser fad9638
Use fstatSync(fd) to avoid OOM w/ searchFile
data-douser 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
26 changes: 26 additions & 0 deletions
26
...ntegration-tests/primitives/tools/codeql_resolve_files/find_qll_files/README.md
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Integration Test: codeql_resolve_files/find_qll_files | ||
|
|
||
| ## Purpose | ||
|
|
||
| Tests the `codeql_resolve_files` tool to ensure it can find QL library files by extension within a CodeQL pack directory. | ||
|
|
||
| ## Test Scenario | ||
|
|
||
| This test validates that the `codeql_resolve_files` tool can: | ||
|
|
||
| 1. Accept a directory path and file extension filter | ||
| 2. Use the `codeql resolve files` CLI command to find matching files | ||
| 3. Return a list of file paths matching the specified criteria | ||
|
|
||
| ## Test Parameters | ||
|
|
||
| - `dir`: "server/ql/javascript/examples/src" | ||
| - `include-extension`: [".ql"] | ||
|
|
||
| ## Expected Behavior | ||
|
|
||
| The tool should: | ||
|
|
||
| 1. Invoke `codeql resolve files` with the specified directory and extension filter | ||
| 2. Return file paths for all `.ql` files found in the directory tree | ||
| 3. Return a successful result with the file listing | ||
9 changes: 9 additions & 0 deletions
9
...on-tests/primitives/tools/codeql_resolve_files/find_qll_files/after/monitoring-state.json
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "toolName": "codeql_resolve_files", | ||
| "parameters": { | ||
| "dir": "server/ql/javascript/examples/src", | ||
| "include-extension": [".ql"] | ||
| }, | ||
| "success": true, | ||
| "description": "Successfully resolved .ql files in JavaScript examples directory" | ||
|
data-douser marked this conversation as resolved.
Outdated
|
||
| } | ||
9 changes: 9 additions & 0 deletions
9
...n-tests/primitives/tools/codeql_resolve_files/find_qll_files/before/monitoring-state.json
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "toolName": "codeql_resolve_files", | ||
| "parameters": { | ||
| "dir": "server/ql/javascript/examples/src", | ||
| "include-extension": [".ql"] | ||
| }, | ||
| "expectedSuccess": true, | ||
| "description": "Test codeql_resolve_files for finding .ql files in JavaScript examples" | ||
|
data-douser marked this conversation as resolved.
Outdated
|
||
| } | ||
28 changes: 28 additions & 0 deletions
28
...tegration-tests/primitives/tools/search_ql_code/search_predicate_name/README.md
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Integration Test: search_ql_code/search_predicate_name | ||
|
|
||
| ## Purpose | ||
|
|
||
| Tests the `search_ql_code` tool to ensure it can search QL source files for a predicate name pattern and return structured results with file paths and line numbers. | ||
|
|
||
| ## Test Scenario | ||
|
|
||
| This test validates that the `search_ql_code` tool can: | ||
|
|
||
| 1. Accept a regex pattern for a predicate name | ||
| 2. Search `.ql` and `.qll` files in a given directory | ||
| 3. Return structured JSON results with file paths, line numbers, and matching lines | ||
| 4. Report the correct number of files searched and matches found | ||
|
|
||
| ## Test Parameters | ||
|
|
||
| - `pattern`: "isSource" | ||
| - `paths`: ["server/ql/javascript/examples/src"] | ||
| - `contextLines`: 1 | ||
|
|
||
| ## Expected Behavior | ||
|
|
||
| The tool should: | ||
|
|
||
| 1. Recursively search `.ql` and `.qll` files in the specified directory | ||
| 2. Return matches with file paths, line numbers, and context lines | ||
| 3. Report `filesSearched`, `totalMatches`, `returnedMatches`, and `truncated` fields |
23 changes: 23 additions & 0 deletions
23
...n-tests/primitives/tools/search_ql_code/search_predicate_name/after/monitoring-state.json
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "toolName": "search_ql_code", | ||
| "parameters": { | ||
| "pattern": "isSource", | ||
| "paths": ["server/ql/javascript/examples/src"], | ||
| "contextLines": 1 | ||
| }, | ||
| "success": true, | ||
| "description": "Successfully searched QL files for predicate name pattern", | ||
| "searchResult": { | ||
| "totalMatches": 1, | ||
| "returnedMatches": 1, | ||
| "truncated": false, | ||
| "filesSearched": 1, | ||
| "results": [ | ||
| { | ||
| "filePath": "server/ql/javascript/examples/src/ExampleQuery1/ExampleQuery1.ql", | ||
| "lineNumber": 1, | ||
| "lineContent": "contains isSource match" | ||
| } | ||
| ] | ||
| } | ||
|
data-douser marked this conversation as resolved.
Outdated
|
||
| } | ||
10 changes: 10 additions & 0 deletions
10
...-tests/primitives/tools/search_ql_code/search_predicate_name/before/monitoring-state.json
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "toolName": "search_ql_code", | ||
| "parameters": { | ||
| "pattern": "isSource", | ||
| "paths": ["server/ql/javascript/examples/src"], | ||
| "contextLines": 1 | ||
| }, | ||
| "expectedSuccess": true, | ||
| "description": "Test search_ql_code for predicate name search in JavaScript examples" | ||
|
data-douser marked this conversation as resolved.
Outdated
|
||
| } | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import { existsSync } from 'fs'; | ||
| import { cp, mkdir, readdir, rm, stat, unlink } from 'fs/promises'; | ||
| import { join } from 'path'; | ||
| import type { Logger } from '../common/logger'; | ||
|
|
||
| /** | ||
| * Copies CodeQL databases from `GitHub.vscode-codeql` extension storage | ||
| * to a managed directory, removing `.lock` files that the CodeQL query | ||
| * server creates in `<dataset>/default/cache/`. | ||
| * | ||
| * This avoids lock contention when the `ql-mcp` server runs CLI commands | ||
| * against databases that are simultaneously registered by the | ||
| * `vscode-codeql` query server. | ||
| * | ||
| * Each database is identified by its top-level directory name (which | ||
| * contains `codeql-database.yml`). A database is only re-copied when its | ||
| * source has been modified more recently than the existing copy. | ||
| */ | ||
| export class DatabaseCopier { | ||
| constructor( | ||
| private readonly destinationBase: string, | ||
| private readonly logger: Logger, | ||
| ) {} | ||
|
|
||
| /** | ||
| * Synchronise databases from one or more source directories into the | ||
| * managed destination. Only databases that are newer than the existing | ||
| * copy (or missing entirely) are re-copied. | ||
| * | ||
| * @returns The list of database paths in the managed destination that | ||
| * are ready for use (absolute paths). | ||
| */ | ||
| async syncAll(sourceDirs: string[]): Promise<string[]> { | ||
| try { | ||
| await mkdir(this.destinationBase, { recursive: true }); | ||
| } catch (err) { | ||
| this.logger.error( | ||
| `Failed to create managed database directory ${this.destinationBase}: ${err instanceof Error ? err.message : String(err)}`, | ||
| ); | ||
| return []; | ||
| } | ||
|
|
||
| const copied: string[] = []; | ||
|
|
||
| for (const sourceDir of sourceDirs) { | ||
| if (!existsSync(sourceDir)) { | ||
| continue; | ||
| } | ||
|
|
||
| let entries: string[]; | ||
| try { | ||
| entries = await readdir(sourceDir); | ||
| } catch { | ||
| continue; | ||
| } | ||
|
|
||
| for (const entry of entries) { | ||
| const srcDbPath = join(sourceDir, entry); | ||
| if (!(await isCodeQLDatabase(srcDbPath))) { | ||
| continue; | ||
| } | ||
|
|
||
| const destDbPath = join(this.destinationBase, entry); | ||
|
|
||
| if (await this.needsCopy(srcDbPath, destDbPath)) { | ||
| await this.copyDatabase(srcDbPath, destDbPath); | ||
| } | ||
|
|
||
| if (await isCodeQLDatabase(destDbPath)) { | ||
| copied.push(destDbPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return copied; | ||
| } | ||
|
|
||
| /** | ||
| * Copy a single database directory, then strip any `.lock` files that | ||
| * the CodeQL query server may have left behind. | ||
| */ | ||
| private async copyDatabase(src: string, dest: string): Promise<void> { | ||
| this.logger.info(`Copying database ${src} → ${dest}`); | ||
| try { | ||
| // Remove stale destination if present | ||
| try { | ||
| if (existsSync(dest)) { | ||
| await rm(dest, { recursive: true, force: true }); | ||
| } | ||
| } catch (rmErr) { | ||
| this.logger.error( | ||
| `Failed to remove stale destination ${dest}: ${rmErr instanceof Error ? rmErr.message : String(rmErr)}`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| await cp(src, dest, { recursive: true }); | ||
| await removeLockFiles(dest); | ||
| this.logger.info(`Database copied successfully: ${dest}`); | ||
| } catch (err) { | ||
| this.logger.error( | ||
| `Failed to copy database ${src}: ${err instanceof Error ? err.message : String(err)}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A copy is needed when the destination does not exist, or the source | ||
| * `codeql-database.yml` is newer than the destination's. | ||
| */ | ||
| private async needsCopy(src: string, dest: string): Promise<boolean> { | ||
| const destYml = join(dest, 'codeql-database.yml'); | ||
| if (!existsSync(destYml)) { | ||
| return true; | ||
| } | ||
|
|
||
| const srcYml = join(src, 'codeql-database.yml'); | ||
| try { | ||
| const srcMtime = (await stat(srcYml)).mtimeMs; | ||
| const destMtime = (await stat(destYml)).mtimeMs; | ||
| return srcMtime > destMtime; | ||
| } catch { | ||
| // If stat fails, re-copy to be safe | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Check whether a directory looks like a CodeQL database. */ | ||
| async function isCodeQLDatabase(dirPath: string): Promise<boolean> { | ||
| try { | ||
| return (await stat(dirPath)).isDirectory() && existsSync(join(dirPath, 'codeql-database.yml')); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Recursively remove all `.lock` files under the given directory. | ||
| * These are empty sentinel files created by the CodeQL query server in | ||
| * `<dataset>/default/cache/.lock`. | ||
| */ | ||
| async function removeLockFiles(dir: string): Promise<void> { | ||
| let entries: string[]; | ||
| try { | ||
| entries = await readdir(dir); | ||
| } catch { | ||
| return; | ||
| } | ||
|
|
||
| for (const entry of entries) { | ||
| const fullPath = join(dir, entry); | ||
| try { | ||
| const st = await stat(fullPath); | ||
| if (st.isDirectory()) { | ||
| await removeLockFiles(fullPath); | ||
| } else if (entry === '.lock') { | ||
| await unlink(fullPath); | ||
| } | ||
| } catch { | ||
| // Best-effort removal | ||
| } | ||
| } | ||
| } | ||
|
data-douser marked this conversation as resolved.
|
||
Oops, something went wrong.
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.