fix(impact): smaller agent output, token-budget trimming, old-index walks

Impact results are read by agents that keep every tool output in context and
re-read it each turn. On anko (a DeepSWE task repo), the impact calls agents
made returned 225 KB in total; the same calls now return 57 KB (-75%), and
the largest hub symbols drop from 58-72 KB to 13-15 KB.

- CLI: JSON is indented only for a terminal. Piped and agent output is
  compact (-33% on hub symbols).
- MCP and CLI agent boundary (impact-agent-output.ts): each affected symbol
  lists at most 3 processes plus processCount; affected_processes already
  lists every flow once. A default page of 30 symbols per depth applies when
  no limit, offset or summaryOnly is given; byDepthCounts stays complete and
  GITNEXUS_IMPACT_DEFAULT_LIMIT=0 restores the unpaged result. Backend
  results for group fan-out, the web UI and tests are unchanged.
- maxTokens on impact drops whole symbols from the deepest levels first, so
  the JSON stays valid instead of being cut mid-record, and reports what was
  shown in pagination.trimmedForBudget. The CLI gains --max-tokens.
- An index built before edges carried staticGated failed every frontier
  query with a binder error, so impact reported no dependants. The walk now
  retries with the column read as NULL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
abhigyantrumio 2026-10-31 05:31:56 +05:30
parent bd9586e008
commit 8a444eabd3
12 changed files with 438 additions and 21 deletions

View file

@ -145,6 +145,7 @@ const OPTION_DESCRIPTION_KEYS = {
'impact|-l, --limit <n>': 'help.option.impact.limit',
'impact|--offset <n>': 'help.option.impact.offset',
'impact|--summary-only': 'help.option.impact.summaryOnly',
'impact|--max-tokens <n>': 'help.option.impact.maxTokens',
'cypher|-r, --repo <name>': 'help.option.repo.target',
'cypher|--branch <name>': 'help.option.branch',
'cypher|-l, --limit <n>': 'help.option.cypher.limit',

View file

@ -386,9 +386,11 @@ export const en = {
'help.option.impact.depth': 'Max relationship depth (default: 3)',
'help.option.impact.includeTests': 'Include test files in results',
'help.option.impact.limit':
'Max symbols per depth level (default: 100); explicit --limit also caps affected processes/modules/routes',
'Max symbols per depth level (default: 30, GITNEXUS_IMPACT_DEFAULT_LIMIT=0 for all); explicit --limit also caps affected processes/modules/routes',
'help.option.impact.offset': 'Skip N symbols per depth level for pagination',
'help.option.impact.summaryOnly': 'Return counts and risk only, omit symbol list',
'help.option.impact.maxTokens':
'Fit the result into about N tokens by dropping the deepest symbols first (counts stay complete)',
'help.option.trace.fromUid': 'Source symbol UID (zero-ambiguity lookup)',
'help.option.trace.fromFile': 'Source file path to disambiguate common names',
'help.option.trace.toUid': 'Target symbol UID (zero-ambiguity lookup)',

View file

@ -351,9 +351,11 @@ export const zhCN = {
'help.option.impact.depth': '最大关系遍历深度(默认:3)',
'help.option.impact.includeTests': '在结果中包含测试文件',
'help.option.impact.limit':
'每层深度最大符号数(默认:100);显式指定 --limit 时,也限制受影响流程/模块/路由数',
'每层深度最大符号数(默认:30,设 GITNEXUS_IMPACT_DEFAULT_LIMIT=0 返回全部);显式指定 --limit 时,也限制受影响流程/模块/路由数',
'help.option.impact.offset': '每层深度跳过 N 个符号(分页用)',
'help.option.impact.summaryOnly': '仅返回计数和风险等级,省略符号列表',
'help.option.impact.maxTokens':
'将结果控制在约 N 个 token 内,优先舍弃最深层的符号(计数保持完整)',
'help.option.trace.fromUid': '源符号 UID(零歧义查找)',
'help.option.trace.fromFile': '源文件路径,用于消除常见名称歧义',
'help.option.trace.toUid': '目标符号 UID(零歧义查找)',

View file

@ -514,10 +514,14 @@ program
.option('--include-tests', 'Include test files in results')
.option(
'-l, --limit <n>',
'Max symbols per depth level (default: 100); explicit --limit also caps affected processes/modules/routes',
'Max symbols per depth level (default: 30, GITNEXUS_IMPACT_DEFAULT_LIMIT=0 for all); explicit --limit also caps affected processes/modules/routes',
)
.option('--offset <n>', 'Skip N symbols per depth level for pagination')
.option('--summary-only', 'Return counts and risk only, omit symbol list')
.option(
'--max-tokens <n>',
'Fit the result into about N tokens by dropping the deepest symbols first (counts stay complete)',
)
.action(createLbugLazyAction(() => import('./tool.js'), 'impactCommand'));
program

View file

@ -19,6 +19,7 @@ import { writeSync } from 'node:fs';
import { LocalBackend, VALID_NODE_LABELS } from '../mcp/local/local-backend.js';
import { cliErrorKey, cliWarnKey } from './cli-message.js';
import { formatDetectChangesResult } from './detect-changes-format.js';
import { shapeImpactForAgent, withImpactAgentDefaults } from '../mcp/impact-agent-output.js';
let _backend: LocalBackend | null = null;
@ -53,7 +54,9 @@ function output<T>(data: T, render?: (data: T) => string): void {
? render(data)
: typeof data === 'string'
? data
: JSON.stringify(data, null, 2);
: // Indent for a terminal only: agents and pipes re-read this output, and
// indentation made impact results ~45% larger with no added information.
JSON.stringify(data, null, process.stdout.isTTY ? 2 : undefined);
try {
writeSync(1, text + '\n');
} catch (err: any) {
@ -234,6 +237,7 @@ export async function impactCommand(
limit?: string;
offset?: string;
summaryOnly?: boolean;
maxTokens?: string;
},
): Promise<void> {
// A `--`-prefixed uid means Commander swallowed a following flag as the uid
@ -266,7 +270,12 @@ export async function impactCommand(
// rather than on a silently-dropped flag. A non-numeric `--line` parses to
// NaN, which the backend rejects as a non-positive integer (loud, not silent).
const parsedLine = options?.line !== undefined ? parseInt(options.line, 10) : undefined;
const result = await backend.callTool('impact', {
const maxTokens = parseLimit(options?.maxTokens);
if (options?.maxTokens !== undefined && maxTokens === undefined) {
cliErrorKey('tool.usage.impact');
process.exit(1);
}
const impactArgs = withImpactAgentDefaults({
target: target || undefined,
target_uid: options?.uid,
file_path: options?.file,
@ -285,6 +294,7 @@ export async function impactCommand(
offset: parsedOffset,
summaryOnly: options?.summaryOnly ?? undefined,
});
const result = shapeImpactForAgent(await backend.callTool('impact', impactArgs), maxTokens);
// Client-side cap of the affected-list payload to --limit (parity with the
// other tool commands). The backend already paginates byDepth per level to
// the same limit, so byDepth needs no client-side re-slice.

View file

@ -0,0 +1,165 @@
/**
* Agent-facing shaping of `impact` results.
*
* The MCP server and the `gitnexus impact` CLI hand impact results to an LLM,
* which keeps every tool output in its context and re-reads it on each later
* turn. Measured on DeepSWE trials (Claude Code, gpt-6.1-sol, 44 impact results):
* the per-symbol `processes` lists were 56% of `byDepth`, and pretty-printed CLI
* JSON was 45% larger than compact JSON. Neither adds information the model
* lacks: `affected_processes` already lists every flow once.
*
* These helpers run only at those two agent boundaries. The backend result is
* unchanged for internal callers (group fan-out, the web UI, tests).
*/
/** Symbols per depth level when an agent sets neither limit, offset nor summaryOnly. */
export const IMPACT_AGENT_DEFAULT_LIMIT = 30;
/** Processes listed per affected symbol; `processCount` carries the full count. */
export const IMPACT_ITEM_PROCESS_CAP = 3;
/** Bytes per estimated token, the same conservative estimate as output-budget.ts. */
const TOKEN_ESTIMATE_BYTES = 4;
/**
* The default per-depth page for agent calls. `GITNEXUS_IMPACT_DEFAULT_LIMIT=0`
* restores the unpaged result.
*/
export function impactAgentDefaultLimit(env: NodeJS.ProcessEnv = process.env): number | undefined {
const raw = env.GITNEXUS_IMPACT_DEFAULT_LIMIT;
if (raw === undefined || raw.trim() === '') return IMPACT_AGENT_DEFAULT_LIMIT;
const value = Number(raw.trim());
if (!Number.isSafeInteger(value) || value < 0) {
throw new Error('GITNEXUS_IMPACT_DEFAULT_LIMIT must be a non-negative integer.');
}
return value === 0 ? undefined : value;
}
/**
* Adds the default page to impact arguments that chose no pagination. Group
* targets (`@name`) and PDG mode ignore pagination, so they pass through.
*/
export function withImpactAgentDefaults<T extends Record<string, unknown> | undefined>(
args: T,
env: NodeJS.ProcessEnv = process.env,
): T {
if (!args) return args;
if (args.limit !== undefined || args.offset !== undefined || args.summaryOnly !== undefined) {
return args;
}
if (typeof args.repo === 'string' && args.repo.startsWith('@')) return args;
if (args.mode === 'pdg') return args;
const limit = impactAgentDefaultLimit(env);
return limit === undefined ? args : { ...args, limit };
}
type ImpactItem = Record<string, unknown> & { processes?: unknown };
function isRecord(value: unknown): value is Record<string, any> {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
/**
* Keeps the first {@link IMPACT_ITEM_PROCESS_CAP} processes on each affected
* symbol and records the full count as `processCount`.
*/
export function compactImpactItemProcesses<T>(result: T, cap = IMPACT_ITEM_PROCESS_CAP): T {
const input: unknown = result;
if (!isRecord(input) || !isRecord(input.byDepth)) return result;
let trimmed = false;
const byDepth: Record<string, unknown> = {};
for (const [depth, items] of Object.entries(input.byDepth)) {
if (!Array.isArray(items)) {
byDepth[depth] = items;
continue;
}
byDepth[depth] = items.map((item: ImpactItem) => {
if (!isRecord(item) || !Array.isArray(item.processes) || item.processes.length <= cap) {
return item;
}
trimmed = true;
return {
...item,
processes: item.processes.slice(0, cap),
processCount: item.processes.length,
};
});
}
return trimmed ? ({ ...input, byDepth } as T) : result;
}
function byteLength(value: unknown): number {
return Buffer.byteLength(JSON.stringify(value), 'utf8');
}
/** `byDepth` with only the first `keep` symbols, taken in depth order. */
function keepFirst(byDepth: Record<string, unknown[]>, keep: number): Record<string, unknown[]> {
const kept: Record<string, unknown[]> = {};
let left = keep;
for (const depth of Object.keys(byDepth).sort((a, b) => Number(a) - Number(b))) {
kept[depth] = byDepth[depth].slice(0, Math.max(0, left));
left -= kept[depth].length;
}
return kept;
}
/**
* Fits an impact result into `maxTokens` by dropping whole symbols from the
* deepest levels first, so the JSON stays valid and the direct dependants
* survive longest. Counts (`byDepthCounts`, `impactedCount`, `summary`) still
* describe the full result, and `pagination.trimmedForBudget` says what was
* cut. A result that cannot fit even without symbols is returned unchanged;
* the byte-level cap after serialization still applies to it.
*
* `reservedBytes` is text appended after serialization (next-step hints).
*/
export function fitImpactToTokenBudget<T>(result: T, maxTokens: number, reservedBytes = 0): T {
const input: unknown = result;
if (!isRecord(input) || !isRecord(input.byDepth)) return result;
const budget = maxTokens * TOKEN_ESTIMATE_BYTES - reservedBytes;
if (byteLength(result) <= budget) return result;
const byDepth = input.byDepth as Record<string, unknown[]>;
const total = Object.values(byDepth).reduce(
(sum, items) => sum + (Array.isArray(items) ? items.length : 0),
0,
);
const build = (keep: number) => {
const kept = keepFirst(byDepth, keep);
const shownByDepth = Object.fromEntries(
Object.entries(kept).map(([depth, items]) => [depth, items.length]),
);
return {
...input,
pagination: {
...(isRecord(input.pagination) ? input.pagination : {}),
truncated: true,
trimmedForBudget: { maxTokens, shownByDepth },
},
byDepth: kept,
};
};
// Largest prefix that fits: one serialization per probe, log2(total) probes.
let low = 0;
let high = total;
if (byteLength(build(0)) > budget) return result;
while (low < high) {
const mid = Math.ceil((low + high) / 2);
if (byteLength(build(mid)) <= budget) low = mid;
else high = mid - 1;
}
return build(low) as T;
}
/** The agent-boundary pipeline: compact process lists, then fit the token budget. */
export function shapeImpactForAgent<T>(
result: T,
maxTokens: number | undefined,
reservedBytes = 0,
): T {
const compact = compactImpactItemProcesses(result);
return maxTokens === undefined
? compact
: fitImpactToTokenBudget(compact, maxTokens, reservedBytes);
}

View file

@ -223,6 +223,16 @@ const CANDIDATE_WINDOW = 20;
*/
export const IMPACT_FRONTIER_ROW_CAP = 100_000;
/** The binder error from an index whose CodeRelation table predates `staticGated`. */
export function isMissingStaticGatedError(error: unknown): boolean {
return error instanceof Error && /Cannot find property staticGated for r\b/i.test(error.message);
}
/** The impact frontier query with `staticGated` read as NULL, for older indexes. */
export function withoutStaticGatedColumn(query: string): string {
return query.replace('r.staticGated AS staticGated', 'NULL AS staticGated');
}
/**
* Concurrent BFS walks when `query()` attaches per-process `chain`. Each walk
* is already node-capped inside `_computeContextChain`; this bounds how many
@ -1562,9 +1572,7 @@ const isMissingRouteOptionalPropertyError = (error: unknown): boolean => {
* form; any guard failure returns `{ error }`.
*/
type ApiImpactResult =
| ApiImpactRoute
| { routes: ApiImpactRoute[]; total: number }
| { error: string };
ApiImpactRoute | { routes: ApiImpactRoute[]; total: number } | { error: string };
/**
* One repository entry as returned by {@link LocalBackend.listRepos} and in each
@ -8520,6 +8528,7 @@ export class LocalBackend {
const objectCallableFrontier: string[] = [];
let traversalComplete = true;
let traversalCappedAtDepth: number | undefined;
let staticGatedAvailable = true;
// Fetch one sentinel row beyond the cap so generated object bindings
// degrade visibly instead of allocating an unbounded seed frontier.
const OBJECT_CALLABLE_MEMBER_CAP = 5000;
@ -8752,11 +8761,31 @@ export class LocalBackend {
`MATCH (n)-[r:CodeRelation]->(callee) WHERE n.id IN $frontierIds AND r.type IN $relTypes${confidenceFilter} RETURN n.id AS sourceId, callee.id AS id, callee.name AS name, labels(callee)[0] AS type, callee.filePath AS filePath, r.type AS relType, r.confidence AS confidence, r.staticGated AS staticGated LIMIT ${IMPACT_FRONTIER_ROW_CAP + 1}`;
try {
const fetched = await executeParameterized(repo.lbugPath, query, {
const queryParams = {
frontierIds: frontier,
relTypes: relationTypes,
...(safeMinConfidence > 0 ? { minConfidence: safeMinConfidence } : {}),
});
};
let fetched: any[];
try {
fetched = await executeParameterized(
repo.lbugPath,
staticGatedAvailable ? query : withoutStaticGatedColumn(query),
queryParams,
);
} catch (e) {
// An index built before edges carried `staticGated` has no such
// property, so the binder rejects every level and the walk would
// report no dependants at all. A missing column only means "not
// proven dead"; retry without it.
if (!staticGatedAvailable || !isMissingStaticGatedError(e)) throw e;
staticGatedAvailable = false;
fetched = await executeParameterized(
repo.lbugPath,
withoutStaticGatedColumn(query),
queryParams,
);
}
const levelCapped = fetched.length > IMPACT_FRONTIER_ROW_CAP;
const related = levelCapped ? fetched.slice(0, IMPACT_FRONTIER_ROW_CAP) : fetched;
if (levelCapped) {
@ -9714,8 +9743,7 @@ export class LocalBackend {
'individual indexed repository instead.',
target: groupRejectTarget,
direction: (params.direction === 'downstream' ? 'downstream' : 'upstream') as
| 'upstream'
| 'downstream',
'upstream' | 'downstream',
});
return pdgErr;
}

View file

@ -41,7 +41,13 @@ import {
McpRepositoryPolicy,
mcpRepositoryPolicyConfigured,
} from './repository-policy.js';
import { applyMcpMaxTokens, resolveMcpMaxTokens, withoutMcpBudgetArg } from './output-budget.js';
import {
applyMcpMaxTokens,
MCP_TRUNCATION_MARKER,
resolveMcpMaxTokens,
withoutMcpBudgetArg,
} from './output-budget.js';
import { shapeImpactForAgent, withImpactAgentDefaults } from './impact-agent-output.js';
import { assertKnownMcpToolArguments, schemaSourceToolName } from './tool-arguments.js';
import {
applyMcpResponseFilter,
@ -276,15 +282,31 @@ export function createMCPServer(
}
const responseFilter = resolveMcpResponseFilter(schemaSource, typedArgs);
maxTokens = resolveMcpMaxTokens(name, typedArgs);
const backendArgs = withoutMcpResponseFilterArgs(
const isImpact = schemaSource === 'impact';
const strippedArgs = withoutMcpResponseFilterArgs(
withoutMcpBudgetArg(typedArgs),
responseFilter,
);
const result = applyMcpResponseFilter(
const backendArgs = isImpact ? withImpactAgentDefaults(strippedArgs) : strippedArgs;
const rawResult = applyMcpResponseFilter(
await scopedBackend.callTool(name, backendArgs),
responseFilter,
);
const hint = getNextStepHint(name, args as Record<string, any> | undefined, readOnly, result);
const hint = getNextStepHint(
name,
args as Record<string, any> | undefined,
readOnly,
rawResult,
);
// Trim whole impact records to the budget so the JSON stays valid; the
// byte cap below remains the backstop for everything else.
const result = isImpact
? shapeImpactForAgent(
rawResult,
maxTokens,
Buffer.byteLength(hint, 'utf8') + Buffer.byteLength(MCP_TRUNCATION_MARKER, 'utf8'),
)
: rawResult;
const resultText =
name === 'query' && result !== null && typeof result === 'object' && !Array.isArray(result)
? JSON.stringify({ ...result, next: hint })

View file

@ -671,8 +671,8 @@ ${HOT_READ_STALENESS_NOTE}`,
limit: {
type: 'integer',
description:
'Max symbols returned in byDepth per depth level (default: 100). Single-repo only; ignored in group mode (@groupName). Use small values for hub symbols to avoid output truncation.',
default: 100,
'Max symbols per depth level in byDepth (default: 30; byDepthCounts stays full, page with offset). Symbols list ≤3 processes plus processCount. Single-repo only.',
default: 30,
minimum: 1,
maximum: 10000,
},

View file

@ -0,0 +1,143 @@
import { describe, expect, it } from 'vitest';
import {
compactImpactItemProcesses,
fitImpactToTokenBudget,
IMPACT_AGENT_DEFAULT_LIMIT,
impactAgentDefaultLimit,
shapeImpactForAgent,
withImpactAgentDefaults,
} from '../../src/mcp/impact-agent-output.js';
import {
isMissingStaticGatedError,
withoutStaticGatedColumn,
} from '../../src/mcp/local/local-backend.js';
const symbol = (id: string, processes = 0) => ({
id,
name: id,
filePath: `src/${id}.ts`,
relationType: 'CALLS',
confidence: 1,
processes: Array.from({ length: processes }, (_, i) => ({ id: `proc_${i}`, label: `Flow ${i}` })),
});
const result = (byDepth: Record<string, unknown[]>) => ({
target: { id: 'Function:src/t.ts:t', name: 't' },
impactedCount: Object.values(byDepth).reduce((n, items) => n + items.length, 0),
byDepthCounts: Object.fromEntries(Object.entries(byDepth).map(([d, items]) => [d, items.length])),
byDepth,
});
describe('impact agent defaults', () => {
it('adds the default page only when the caller chose no pagination', () => {
expect(withImpactAgentDefaults({ target: 't' }, {})).toEqual({
target: 't',
limit: IMPACT_AGENT_DEFAULT_LIMIT,
});
expect(withImpactAgentDefaults({ target: 't', limit: 5 }, {})).toEqual({
target: 't',
limit: 5,
});
expect(withImpactAgentDefaults({ target: 't', offset: 30 }, {})).toEqual({
target: 't',
offset: 30,
});
expect(withImpactAgentDefaults({ target: 't', summaryOnly: true }, {})).toEqual({
target: 't',
summaryOnly: true,
});
});
it('leaves group and PDG requests alone, which ignore pagination', () => {
expect(withImpactAgentDefaults({ target: 't', repo: '@group' }, {})).toEqual({
target: 't',
repo: '@group',
});
expect(withImpactAgentDefaults({ target: 't', mode: 'pdg' }, {})).toEqual({
target: 't',
mode: 'pdg',
});
});
it('reads GITNEXUS_IMPACT_DEFAULT_LIMIT, where 0 restores the unpaged result', () => {
expect(impactAgentDefaultLimit({ GITNEXUS_IMPACT_DEFAULT_LIMIT: '50' })).toBe(50);
expect(impactAgentDefaultLimit({ GITNEXUS_IMPACT_DEFAULT_LIMIT: '0' })).toBeUndefined();
expect(
withImpactAgentDefaults({ target: 't' }, { GITNEXUS_IMPACT_DEFAULT_LIMIT: '0' }),
).toEqual({
target: 't',
});
expect(() => impactAgentDefaultLimit({ GITNEXUS_IMPACT_DEFAULT_LIMIT: '-1' })).toThrow();
});
});
describe('compactImpactItemProcesses', () => {
it('keeps three processes per symbol and records the full count', () => {
const out = compactImpactItemProcesses(result({ '1': [symbol('a', 5), symbol('b', 2)] }));
const [a, b] = out.byDepth['1'] as any[];
expect(a.processes).toHaveLength(3);
expect(a.processCount).toBe(5);
expect(b.processes).toHaveLength(2);
expect(b).not.toHaveProperty('processCount');
});
it('returns results without a symbol list unchanged', () => {
const ambiguous = { status: 'ambiguous', candidates: [{ uid: 'x' }] };
expect(compactImpactItemProcesses(ambiguous)).toBe(ambiguous);
});
});
describe('fitImpactToTokenBudget', () => {
it('drops whole symbols from the deepest level first and keeps the JSON valid', () => {
const full = result({
'1': Array.from({ length: 3 }, (_, i) => symbol(`direct${i}`)),
'2': Array.from({ length: 40 }, (_, i) => symbol(`indirect${i}`)),
});
const maxTokens = 600;
const fitted = fitImpactToTokenBudget(full, maxTokens) as any;
expect(Buffer.byteLength(JSON.stringify(fitted), 'utf8')).toBeLessThanOrEqual(maxTokens * 4);
expect(fitted.byDepth['1']).toHaveLength(3);
expect(fitted.byDepth['2'].length).toBeLessThan(40);
expect(fitted.byDepthCounts).toEqual({ '1': 3, '2': 40 });
expect(fitted.pagination).toMatchObject({
truncated: true,
trimmedForBudget: { maxTokens, shownByDepth: { '1': 3, '2': fitted.byDepth['2'].length } },
});
});
it('keeps a result that already fits, and one that cannot fit without symbols', () => {
const small = result({ '1': [symbol('a')] });
expect(fitImpactToTokenBudget(small, 10_000)).toBe(small);
expect(fitImpactToTokenBudget(small, 1)).toBe(small);
});
it('reserves room for text appended after serialization', () => {
const full = result({ '1': Array.from({ length: 30 }, (_, i) => symbol(`s${i}`)) });
const withoutReserve = fitImpactToTokenBudget(full, 500) as any;
const withReserve = fitImpactToTokenBudget(full, 500, 400) as any;
expect(withReserve.byDepth['1'].length).toBeLessThan(withoutReserve.byDepth['1'].length);
});
it('shapeImpactForAgent compacts processes even without a budget', () => {
const out = shapeImpactForAgent(result({ '1': [symbol('a', 9)] }), undefined) as any;
expect(out.byDepth['1'][0].processCount).toBe(9);
});
});
describe('impact walk on indexes without staticGated', () => {
it('recognises the binder error and reads the column as NULL', () => {
expect(
isMissingStaticGatedError(
new Error('Prepare failed: Binder exception: Cannot find property staticGated for r.'),
),
).toBe(true);
expect(isMissingStaticGatedError(new Error('Cannot find property other for r.'))).toBe(false);
expect(isMissingStaticGatedError('Cannot find property staticGated for r.')).toBe(false);
expect(
withoutStaticGatedColumn(
'RETURN r.confidence AS confidence, r.staticGated AS staticGated LIMIT 5',
),
).toBe('RETURN r.confidence AS confidence, NULL AS staticGated LIMIT 5');
});
});

View file

@ -26,6 +26,7 @@ import {
SHUTDOWN_EXIT_CODES,
} from '../../src/mcp/server.js';
import { GITNEXUS_TOOLS } from '../../src/mcp/tools.js';
import { MCP_TRUNCATION_MARKER } from '../../src/mcp/output-budget.js';
import { createMcpRepositoryPolicy } from '../../src/mcp/repository-policy.js';
// ─── Mock backend ──────────────────────────────────────────────────
@ -528,6 +529,43 @@ describe('MCP output budgets', () => {
expect(backend.callTool).not.toHaveBeenCalled();
});
it('fits an over-budget impact result by dropping whole symbols, keeping valid JSON', async () => {
const item = (id: string) => ({
id,
name: id,
filePath: `src/${id}.ts`,
relationType: 'CALLS',
processes: Array.from({ length: 6 }, (_, i) => ({ id: `proc_${i}`, label: `Flow ${i}` })),
});
const impact = {
target: { id: 'Function:src/auth.ts:auth', name: 'auth' },
impactedCount: 62,
byDepthCounts: { 1: 2, 2: 60 },
byDepth: {
1: [item('direct0'), item('direct1')],
2: Array.from({ length: 60 }, (_, i) => item(`indirect${i}`)),
},
};
const backend = createMockBackend({ callTool: vi.fn().mockResolvedValue(impact) });
const { text, isError } = await callToolThroughServer(backend, 'impact', {
target: 'auth',
direction: 'upstream',
maxTokens: 800,
});
expect(isError).toBe(false);
expect(Buffer.byteLength(text, 'utf8')).toBeLessThanOrEqual(800 * 4);
expect(text.endsWith(MCP_TRUNCATION_MARKER)).toBe(false);
// The result is one compact JSON line; the next-step hint follows it.
const body = JSON.parse(text.split('\n')[0]);
expect(body.byDepth[1]).toHaveLength(2);
expect(body.byDepth[2].length).toBeLessThan(60);
expect(body.byDepthCounts).toEqual({ 1: 2, 2: 60 });
expect(body.byDepth[1][0].processes).toHaveLength(3);
expect(body.byDepth[1][0].processCount).toBe(6);
expect(body.pagination.trimmedForBudget.maxTokens).toBe(800);
});
it('accepts CLI-style depth as a known impact alias (#3261)', async () => {
// The CLI flag is --depth; since #3261 MCP advertises it alongside maxDepth
// as a compatibility alias. Before #3261, `depth` was silently dropped.
@ -543,6 +581,8 @@ describe('MCP output budgets', () => {
target: 'auth',
direction: 'downstream',
depth: 2,
// The agent boundary adds the default per-depth page (impact-agent-output.ts).
limit: 30,
});
});

View file

@ -90,7 +90,7 @@ describe('direct CLI tool commands', () => {
await checkCommand({ cycles: true, json: true });
expect(writeSyncMock).toHaveBeenCalledWith(1, expect.stringContaining('"status": "clean"'));
expect(writeSyncMock).toHaveBeenCalledWith(1, expect.stringContaining('"status":"clean"'));
expect(process.exitCode).toBeUndefined();
});
@ -156,7 +156,7 @@ describe('direct CLI tool commands', () => {
await cypherCommand('MATCH (n) RETURN count(n) AS count');
expect(writeSyncMock).toHaveBeenCalledWith(1, expect.stringContaining('"row_count": 1'));
expect(writeSyncMock).toHaveBeenCalledWith(1, expect.stringContaining('"row_count":1'));
expect(process.exitCode).toBeUndefined();
});
@ -177,7 +177,7 @@ describe('direct CLI tool commands', () => {
await queryCommand('auth flow');
expect(writeSyncMock).toHaveBeenCalledWith(1, expect.stringContaining('"definitions": []'));
expect(writeSyncMock).toHaveBeenCalledWith(1, expect.stringContaining('"definitions":[]'));
expect(disconnectMock).toHaveBeenCalledOnce();
});