Files
gstack/lib/code-intelligence/suggest.ts
T
Garry Tanandcapy-ai[bot] 36b349d660 Commands that read cut-off output now fail instead of judging part of it (GST-3086) (#4492)
Commands that read large git or gh output, including the credential
pre-push hook, the pre-merge secret and literal scans and the
sealed-output guard, now fail when Bun cuts that output off, instead of
judging the part they got.

Why: Bun can report exit status 0 together with ENOBUFS and partial
stdout, so a push whose diff passed 64 MiB could get a credential past
the pre-push hook.

Nothing to do: the failure names `SPAWN_OUTPUT_INCOMPLETE` and links its
troubleshooting entry.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-11-18 17:00:18 +00:00

58 lines
2.2 KiB
TypeScript

/**
* suggest — should the session-start indexing offer be made for this repo?
*
* Portions copyright (c) 2026 Sina Matian, time-attack/gstack (GStack 2), MIT.
*
* The offer fires at most once per machine: never when a provider is already
* selected, never after an explicit decline (`select none`), and never for
* small repos where grep is already fast. Detection is cheap and local
* (`git ls-files` count); a non-repo directory never triggers the offer.
*/
import { spawnSync } from "../spawn-sync";
import { resolve } from "path";
import { readSelection } from "./selection";
// Single tracked-file-count knob for "large"; add a LOC signal if it misfires.
/** Tracked-file count at which indexing starts paying for itself. */
export const LARGE_REPO_FILE_THRESHOLD = 1000;
export type SuggestReason =
| "provider-selected"
| "declined"
| "not-a-repo"
| "small-repo"
| "large-repo";
export interface Suggestion {
offer: boolean;
reason: SuggestReason;
fileCount: number | null;
threshold: number;
}
/** Count of git-tracked files, or null when the path is not a git repo. */
export function trackedFileCount(repoPath: string): number | null {
const result = spawnSync("git", ["-C", resolve(repoPath), "ls-files"], {
encoding: "utf-8",
maxBuffer: 64 * 1024 * 1024,
});
if (result.status !== 0 || typeof result.stdout !== "string") return null;
const out = result.stdout.trim();
return out ? out.split("\n").length : 0;
}
export function shouldOfferIndexing(
repoPath: string,
opts: { env?: NodeJS.ProcessEnv; threshold?: number } = {},
): Suggestion {
const threshold = opts.threshold ?? LARGE_REPO_FILE_THRESHOLD;
const selection = readSelection(opts.env);
if (selection.provider) return { offer: false, reason: "provider-selected", fileCount: null, threshold };
if (selection.declined) return { offer: false, reason: "declined", fileCount: null, threshold };
const fileCount = trackedFileCount(repoPath);
if (fileCount === null) return { offer: false, reason: "not-a-repo", fileCount, threshold };
if (fileCount < threshold) return { offer: false, reason: "small-repo", fileCount, threshold };
return { offer: true, reason: "large-repo", fileCount, threshold };
}