Skip to content

Commit 586909e

Browse files
committed
perf(hooks,search,worker): eliminate hook stalls, deduplicate copilot hooks, optimize worker io and search retries
1 parent 2c8278e commit 586909e

23 files changed

Lines changed: 627 additions & 131 deletions

‎src/bridges/copilot_cli.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,14 @@ async function main() {
376376
let raw = ''
377377
if (sessionStartDeadline !== undefined) {
378378
try {
379-
raw = await readSessionStartInput(sessionStartDeadline - Date.now())
379+
raw = await readBoundedHookInput(sessionStartDeadline - Date.now(), SESSION_START_MAX_INPUT_BYTES)
380380
} catch (error) {
381381
failOpenSessionStart(error && error.message ? error.message : 'input could not be read')
382382
return
383383
}
384384
} else {
385385
try {
386-
raw = require('node:fs').readFileSync(0, 'utf8')
386+
raw = await readBoundedHookInput(SHIM_FALLBACK_TIMEOUT_MS, COPILOT_HOOK_MAX_INPUT_BYTES)
387387
} catch {
388388
process.stdout.write('{}')
389389
return

‎src/bridges/copilot_session_start.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ const SESSION_START_CLEANUP_RESERVE_MS = 1000
1515
// How long past the deadline the shim keeps its stdin listener for a payload already in the pipe: on Windows the pipe read completes on a worker thread, so the completion can land many event-loop turns after the listener attaches. Measured resume-to-data under 52 busy loops on 26 CPUs: p50 5.7 ms, max 204 ms.
1616
const SESSION_START_LATE_PAYLOAD_GRACE_MS = 1000
1717
const SESSION_START_MAX_INPUT_BYTES = 1024 * 1024
18+
const COPILOT_HOOK_MAX_INPUT_BYTES = 64 * 1024 * 1024
1819
1920
function failOpenSessionStart(reason) {
2021
process.stderr.write('[token-goat] SessionStart failed open: ' + reason + '\\n')
2122
process.stdout.write('{}')
2223
}
2324
24-
function readSessionStartInput(timeoutMs) {
25+
function readBoundedHookInput(timeoutMs, maxInputBytes) {
2526
return new Promise((resolve, reject) => {
2627
const chunks = []
2728
const closers = []
@@ -55,8 +56,8 @@ function readSessionStartInput(timeoutMs) {
5556
const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)
5657
const chunkStart = totalBytes
5758
totalBytes += bytes.length
58-
if (totalBytes > SESSION_START_MAX_INPUT_BYTES) {
59-
finish('input exceeded 1 MiB')
59+
if (totalBytes > maxInputBytes) {
60+
finish(maxInputBytes === SESSION_START_MAX_INPUT_BYTES ? 'input exceeded 1 MiB' : 'input exceeded ' + maxInputBytes + ' bytes')
6061
return
6162
}
6263
chunks.push(bytes)

‎src/db.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,10 @@ export const WAL_SIZE_LIMIT_PRAGMA = `journal_size_limit = ${WAL_SIZE_LIMIT_BYTE
504504
const HOOK_DB_WAIT_MS = 250
505505
let _hookWaits = false
506506

507-
/** Switch this process to the short waits of {@link HOOK_DB_WAIT_MS} for every connection it opens from now on. Called once by the one-shot `hook` command and by no other path: the worker, the indexer and every other CLI command keep the 15 s budget `initConnection` documents, because a patient wait is right for them. Connections opened before the call keep the budget they were opened with. `on = false` restores the patient budget, for tests. */
507+
/** Set the hook/patient busy timeout on all cached connections and select that budget for future connections; `on = false` restores the patient budget. */
508508
export function limitDbWaitsForHook(on = true): void {
509509
_hookWaits = on
510+
_connections.forEach((conn) => conn.pragma(`busy_timeout = ${connectionWaitBudgets().busyMs}`))
510511
}
511512

512513
/** The busy_timeout and the WAL-switch deadline, in milliseconds, that a connection opened now gets: the patient indexing budgets, or the hook's short ones after {@link limitDbWaitsForHook}. */

‎src/fingerprint.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import * as fs from 'node:fs'
55

66
/** Return the hex-encoded SHA-256 of `content`. A `string` is encoded as UTF-8 before hashing so the digest matches Python's `hashlib.sha256(text.encode("utf-8")).hexdigest()` for identical text. A `Buffer` is hashed verbatim, matching `hashlib.sha256(raw).hexdigest()` for identical bytes. */
77
export function fingerprintContent(content: string | Buffer): string {
8-
const hash = createHash('sha256')
9-
hash.update(typeof content === 'string' ? Buffer.from(content, 'utf-8') : content)
10-
return hash.digest('hex')
8+
return createHash('sha256').update(content).digest('hex')
119
}
1210

1311
/** Return the first 16 hex chars of the SHA-256 of `content`, used for short cache keys. Extracted from ≥11 call sites that all use `.slice(0, 16)`. */
@@ -31,16 +29,21 @@ export function statIndexedFile(filePath: string): fs.Stats | 'gone' | null {
3129
}
3230
}
3331

34-
/** Return the hex-encoded SHA-256 of the file at `filePath`, or `null` if the file cannot be read (missing, permission denied, is a directory, etc.). Reads the raw bytes and hashes them verbatim, so the digest matches a content fingerprint computed over the same bytes. Never throws: any read failure collapses to `null` so callers on the hot hook path can branch without a try/catch. */
35-
export function fingerprintFile(filePath: string): string | null {
36-
let data: Buffer
32+
/** Read the raw bytes at `filePath` once and return both them and their hex-encoded SHA-256, or `null` if the file cannot be read. Never throws: any read failure collapses to `null` so callers on the hot hook path can branch without a try/catch. */
33+
export function fingerprintFileWithBytes(filePath: string): { sha: string; bytes: Buffer } | null {
34+
let bytes: Buffer
3735
try {
38-
data = fs.readFileSync(filePath)
36+
bytes = fs.readFileSync(filePath)
3937
} catch {
4038
// Missing file, permission error, directory, or transient lock — the caller treats "no fingerprint" the same regardless of the cause.
4139
return null
4240
}
43-
return fingerprintContent(data)
41+
return { sha: fingerprintContent(bytes), bytes }
42+
}
43+
44+
/** Return the hex-encoded SHA-256 of the file at `filePath`, or `null` if the file cannot be read. Hashes the raw bytes verbatim, so the digest matches a content fingerprint computed over the same bytes. */
45+
export function fingerprintFile(filePath: string): string | null {
46+
return fingerprintFileWithBytes(filePath)?.sha ?? null
4447
}
4548

4649
/** Is there genuinely no file at `filePath`? `true` only for ENOENT. A file that is present but cannot be examined right now -- a Windows ACL denial, EACCES on a parent directory, a disconnected network share -- answers `false`, because "I cannot tell" is not the same as "it is gone", and every caller here does something destructive or user-visible with a `true`. This is what `fs.existsSync` cannot give: existsSync is a stat inside a bare catch, so it answers `false` for a permission or I/O error exactly as it does for a missing file. Measured on Windows against a directory with a deny ACE for the current user: `existsSync` returned false for a file that was still there, while `statSync(p, { throwIfNoEntry: false })` threw EPERM. `throwIfNoEntry: false` returns `undefined` for ENOENT and only ENOENT, and throws for everything else, which is precisely the distinction. */

‎src/hook_client.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fs from 'node:fs'
44
import * as net from 'node:net'
55
import * as os from 'node:os'
66

7-
import { envBool } from './env.js'
7+
import { envBool, envInt } from './env.js'
88
import {
99
configStamp,
1010
encodeFrame,
@@ -34,8 +34,12 @@ const HANDSHAKE_TIMEOUT_MS = 150
3434
const FIND_BUDGET_MS = 300
3535
/** How long a slot gets to answer the handshake before the next slot is asked beside it. An idle server answers in 1 to 3 ms, so a slot silent this long is in synchronous work, and the caller now waits on it and the next slot at once instead of spending its whole budget on one slot and then the next: two slots each held past the handshake allowance used to use up the budget before the third, possibly idle, was ever asked. */
3636
const HEDGE_MS = 40
37-
/** A dispatched hook request that has not answered by now is failed open. Matches the hook relay's own queue wait. A CLI request has no such limit (see attempt). */
37+
/** Legacy wait for dispatched requests outside Copilot CLI hooks. CLI requests have no response limit (see attempt). */
3838
const RESPONSE_TIMEOUT_MS = 120_000
39+
/** Default response wait for Copilot CLI hooks, well below their 60-second harness limit. */
40+
const COPILOT_HOOK_RESPONSE_TIMEOUT_MS = 12_000
41+
/** Leave 15 seconds before Copilot CLI's installed 60-second hook limit, even with an override. */
42+
const MAX_COPILOT_HOOK_RESPONSE_TIMEOUT_MS = 45_000
3943
/** How long `status` and `stop` keep asking a slot whose handshake timed out. */
4044
const CONTROL_WAIT_MS = 10_000
4145
const SPAWN_RETRY_MS = 30_000
@@ -46,6 +50,17 @@ type Outcome = { kind: 'served'; reply: ServerReply } | { kind: 'absent' } | { k
4650
/** What lets several attempts wait on different slots at once while at most one of them dispatches: `claim` is asked on a verified challenge, with the canceller the attempt registered, and answers whether this attempt may send its request; `onCancel` registers what to run once another attempt has claimed. */
4751
type Race = { claim: (self: () => void) => boolean; onCancel: (cancel: () => void) => void }
4852

53+
/** Response budget for a dispatched request, with a pre-limit default for Copilot CLI hooks. */
54+
export function responseTimeoutFor(request: ServerRequest): number {
55+
if (request.kind !== 'hook' || request.env['TOKEN_GOAT_HARNESS_OVERRIDE'] !== 'copilot_cli') return RESPONSE_TIMEOUT_MS
56+
return envInt(
57+
'TOKEN_GOAT_COPILOT_HOOK_CLIENT_TIMEOUT_MS',
58+
COPILOT_HOOK_RESPONSE_TIMEOUT_MS,
59+
1,
60+
MAX_COPILOT_HOOK_RESPONSE_TIMEOUT_MS,
61+
)
62+
}
63+
4964
/** One attempt against one slot. Resolves exactly once. With `race`, an attempt that loses the claim, or is cancelled before its challenge arrives, hangs up without dispatching and resolves `busy`. */
5065
function attempt(slot: number, key: Buffer, request: ServerRequest, handshakeMs: number, dir?: string, race?: Race): Promise<Outcome> {
5166
return new Promise((resolve) => {
@@ -92,7 +107,7 @@ function attempt(slot: number, key: Buffer, request: ServerRequest, handshakeMs:
92107
dispatched = true
93108
clearTimeout(timer)
94109
// A CLI command waits for its answer however long it runs. The server cannot abandon a command it has started (the work is synchronous and nothing interrupts it), so giving up here only made the caller run the same command a second time beside it, on the same index, never faster (a 342.8 s `symbol` miss on a large project was 120 s of waiting here and then the whole command again). A server that dies mid-command still closes the connection, which reads as lost below, and the caller still runs it itself.
95-
if (request.kind !== 'cli') timer = setTimeout(() => finish({ kind: 'lost' }), RESPONSE_TIMEOUT_MS)
110+
if (request.kind !== 'cli') timer = setTimeout(() => finish({ kind: 'lost' }), responseTimeoutFor(request))
96111
socket.write(frame)
97112
return
98113
}

‎src/reconcile.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ interface VerifiedUnchangedFile {
4646
readonly stamp: PreHashStamp
4747
}
4848

49-
/** Record that the bytes behind each file's `sha` carried its `stamp.mtimeSec`, in one transaction. Each file is judged, and its `indexed_at` written, from the clock read before that file's hash, never from a clock read at the end of a sweep: a file saved again after the hash but before such a clock would otherwise look older than the index time it was given and never be hashed again. When the mtime is at least one tick older than that clock, `mtime` and `indexed_at = clockSec` are written together, so the row satisfies the racy-clean rule from the next sweep on. A newer mtime is not stamped with an index time: the file may still be mid-write, so it stays racy and is hashed again until it ages past the tick (only a moved mtime is recorded, as the pre-read probe's equality gate needs), and an `indexed_at` later than that clock, which only a clock that stepped back leaves, is lowered to it: a stamp is never left later than the clock that judged it, or a same-tick edit would read as proven unchanged. Guarded on the sha, so a row rewritten since keeps its stamp. Best-effort: a database error leaves the rows as they were. */
50-
export function restampVerifiedUnchanged(dbPath: string, files: readonly VerifiedUnchangedFile[]): void {
49+
/** Record that the bytes behind each file's `sha` carried its `stamp.mtimeSec`, in one transaction. Each file is judged, and its `indexed_at` written, from the clock read before that file's hash, never from a clock read at the end of a sweep: a file saved again after the hash but before such a clock would otherwise look older than the index time it was given and never be hashed again. When the mtime is at least one tick older than that clock, `mtime` and `indexed_at = clockSec` are written together, so the row satisfies the racy-clean rule from the next sweep on. A newer mtime is not stamped with an index time: the file may still be mid-write, so it stays racy and is hashed again until it ages past the tick (only a moved mtime is recorded, as the pre-read probe's equality gate needs), and an `indexed_at` later than that clock, which only a clock that stepped back leaves, is lowered to it: a stamp is never left later than the clock that judged it, or a same-tick edit would read as proven unchanged. Guarded on the sha, so a row rewritten since keeps its stamp. Best-effort: a database error leaves the rows as they were and returns false. */
50+
export function restampVerifiedUnchanged(dbPath: string, files: readonly VerifiedUnchangedFile[]): boolean {
5151
try {
5252
const db = getDb(dbPath)
5353
const stampBoth = db.prepare('UPDATE files SET mtime = ?, indexed_at = ? WHERE path = ? AND sha = ?')
@@ -59,8 +59,10 @@ export function restampVerifiedUnchanged(dbPath: string, files: readonly Verifie
5959
else stampRacy.run(stamp.mtimeSec, stamp.clockSec, stamp.clockSec, indexedPath, sha, stamp.mtimeSec, stamp.clockSec)
6060
}
6161
})()
62+
return true
6263
} catch {
6364
// Best-effort: the next drain or sweep tries again.
65+
return false
6466
}
6567
}
6668

‎src/search/parallel_search.ts‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ import { snippetAround } from '../snippet_window.js';
1616
import { assessDenseRelevance, floorEmptiedPhrase, weakMatchPhrase } from '../semantic_relevance.js';
1717
import { ALL_CHANNELS, type ChannelHit, type FusedSearchResult, type SearchChannel, type SearchExecutionSummary, type SearchLowConfidence, type SearchOptions } from './types.js';
1818

19+
const MAX_TEXT_SCAN_BYTES = 64 * 1024 * 1024;
20+
const MAX_TEXT_CACHE_ESTIMATED_BYTES = 2 * MAX_TEXT_SCAN_BYTES;
21+
const TEXT_CACHE_ENTRY_OVERHEAD_BYTES = 64;
22+
23+
type TextSearchCache = Map<string, string>;
24+
25+
const textSearchCacheSizes = new WeakMap<TextSearchCache, number>();
26+
27+
interface SearchExecutionContext {
28+
readonly textCache?: TextSearchCache;
29+
}
30+
1931
/** A symbol hit's preview: its docstring, else the first 140 characters of its body from the declaration on, so a decorated symbol previews as `def area(...)` rather than `@property`. */
2032
export function symbolPreview(sym: { name: string; docstring?: string | null; body?: string | null }): string {
2133
if (sym.docstring) return sym.docstring;
@@ -28,6 +40,15 @@ function matchPreview(line: string, lowerQuery: string): string {
2840
return snippetAround(text, Math.max(0, text.toLowerCase().indexOf(lowerQuery)), 114);
2941
}
3042

43+
function cacheTextIfWithinBudget(cache: TextSearchCache, filePath: string, content: string): void {
44+
if (cache.has(filePath)) return;
45+
const estimatedBytes = 2 * (filePath.length + content.length) + TEXT_CACHE_ENTRY_OVERHEAD_BYTES;
46+
const cachedBytes = textSearchCacheSizes.get(cache) ?? 0;
47+
if (cachedBytes + estimatedBytes > MAX_TEXT_CACHE_ESTIMATED_BYTES) return;
48+
cache.set(filePath, content);
49+
textSearchCacheSizes.set(cache, cachedBytes + estimatedBytes);
50+
}
51+
3152
/** Searches symbols via Full-Text Search and symbol queries. */
3253
async function searchSymbolChannel(query: string, limit: number, rootDir?: string): Promise<{ hits: ChannelHit[]; degradedReason?: string }> {
3354
try {
@@ -73,7 +94,7 @@ async function searchHeadingChannel(query: string, limit: number, rootDir?: stri
7394
}
7495

7596
/** Searches textual file contents in parallel with resource bounding. */
76-
async function searchTextChannel(query: string, limit: number, rootDir?: string): Promise<{ hits: ChannelHit[]; degradedReason?: string }> {
97+
async function searchTextChannel(query: string, limit: number, rootDir?: string, textCache?: TextSearchCache): Promise<{ hits: ChannelHit[]; degradedReason?: string }> {
7798
await Promise.resolve();
7899
try {
79100
const fileEntries = getOwnProjectFileEntries(rootDir ?? process.cwd());
@@ -84,12 +105,11 @@ async function searchTextChannel(query: string, limit: number, rootDir?: string)
84105
let unreadableCount = 0;
85106
let capped = false;
86107
// A byte budget, not a file count: a count of 300 stopped this repo's scan at src/ while its 1,874 indexed files came to 22.8 MB, read in under 200 ms.
87-
const MAX_BYTES = 64 * 1024 * 1024;
88108
const PER_FILE_HITS = 3;
89109
const matched: Array<{ lines: Array<{ line: number; preview: string }>; count: number; filePath: string }> = [];
90110

91111
for (const file of fileEntries.values()) {
92-
if (totalBytesScanned >= MAX_BYTES) {
112+
if (totalBytesScanned >= MAX_TEXT_SCAN_BYTES) {
93113
capped = true;
94114
break;
95115
}
@@ -106,7 +126,14 @@ async function searchTextChannel(query: string, limit: number, rootDir?: string)
106126
totalBytesScanned += stat.size;
107127

108128
// Through the reader seam, not a plain utf-8 read: the matching line is printed as the preview, so a dotenv value has to come back masked the way `read` shows it, and a UTF-16 file has to be decoded before a query can match it.
109-
const content = readFileText(fullPath);
129+
const cachedContent = textCache?.get(fullPath)
130+
let content: string | null;
131+
if (cachedContent !== undefined) {
132+
content = cachedContent;
133+
} else {
134+
content = readFileText(fullPath);
135+
if (content !== null && textCache) cacheTextIfWithinBudget(textCache, fullPath, content);
136+
}
110137
if (content === null) {
111138
unreadableCount++;
112139
continue;
@@ -194,7 +221,7 @@ async function searchSemanticChannel(query: string, limit: number, rootDir?: str
194221
}
195222

196223
/** Executes multi-angle searches concurrently across all requested channels. */
197-
export async function executeParallelSearch(options: SearchOptions): Promise<SearchExecutionSummary> {
224+
export async function executeParallelSearch(options: SearchOptions, context: SearchExecutionContext = {}): Promise<SearchExecutionSummary> {
198225
const startTime = Date.now();
199226
const query = options.query.trim().slice(0, 500);
200227
const limit = options.limit ?? 15;
@@ -240,7 +267,7 @@ export async function executeParallelSearch(options: SearchOptions): Promise<Sea
240267
);
241268
} else if (ch === 'text') {
242269
channelPromises.push(
243-
searchTextChannel(query, limit, projectRoot)
270+
searchTextChannel(query, limit, projectRoot, context.textCache)
244271
.then((res) => ({
245272
channel: ch,
246273
hits: res.hits,

‎src/search/search_cli.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export interface NearRetryOutcome {
9696

9797
/** Runs the search and, on a miss, retries the near-word names {@link nearSymbolNames} ranks, stopping at the first that hits. */
9898
export async function searchWithNearRetry(options: SearchOptions): Promise<NearRetryOutcome> {
99-
const first = await executeParallelSearch(options);
99+
const searchContext = { textCache: new Map<string, string>() };
100+
const first = await executeParallelSearch(options, searchContext);
100101
if (first.totalHits > 0 || options.limit === 0) return { summary: first, answeredFrom: null, retries: 0 };
101102

102103
const near = nearSymbolNames(first.query, options.projectRoot ?? process.cwd());
@@ -107,7 +108,7 @@ export async function searchWithNearRetry(options: SearchOptions): Promise<NearR
107108
let retries = 0;
108109
for (const candidate of candidates) {
109110
retries += 1;
110-
const summary = await executeParallelSearch({ ...options, query: candidate, channels: retryChannels });
111+
const summary = await executeParallelSearch({ ...options, query: candidate, channels: retryChannels }, searchContext);
111112
if (summary.totalHits > 0) return { summary, answeredFrom: first.query, retries };
112113
}
113114
return { summary: first, answeredFrom: null, retries };

0 commit comments

Comments
 (0)