Skip to content

feat(mcp): warn on answers served from a failed rebuild - #1728

Open
liuchong wants to merge 6 commits into
DeusData:mainfrom
liuchong:feat/index-stale-warning
Open

feat(mcp): warn on answers served from a failed rebuild#1728
liuchong wants to merge 6 commits into
DeusData:mainfrom
liuchong:feat/index-stale-warning

Conversation

@liuchong

@liuchong liuchong commented Aug 19, 2026

Copy link
Copy Markdown

Stacked on #1727. A fork PR must target a branch in this repository, so the diff below also contains the five commits under it. The new work here is the last commit, 3d51b655. Once #1727 merges, this diff shrinks to that one commit on its own.

This implements the review suggestion on #1348 to surface a stale index on the next MCP response rather than only in index_status.

Related to #1347.

Problem

A recorded attempt is only visible to whoever thinks to ask for it. An agent that calls search_graph after a background rebuild failed receives a confident answer from a graph that no longer matches the tree, and nothing in that response says so. Agents do not poll index_status before every query.

What this changes

While the recorded attempt is failed or cancelled, answers served from that project's graph carry a warning naming the origin, the recorded finish time, and the limit that ended the attempt — enough for the caller to decide whether to trust the answer and what to fix.

  • JSON payloads gain a stale_index_warning field, so a structured reader cannot miss it
  • Text and tree payloads gain a trailing content block
  • The answer itself keeps its position and content; content[0] is never displaced, because hook_augment and the smoke tests read it as the payload

Annotated: search_graph, query_graph, trace_path, trace_call_path, get_architecture, get_code_snippet, search_code. Not annotated: index_status and check_index_coverage already report freshness themselves, and index_repository is the remedy.

A queued or running rebuild is not yet a failure and stays silent; a completed rebuild clears the warning. Every failure inside the warning path is silent — a warning that cannot be produced must never damage an answer that is otherwise fine.

Cost is one small record read per answered query. It probes neither Git nor the working tree.

Testing

make -f Makefile.cbm test and make -f Makefile.cbm lint-ci on macOS. New coverage: the warning on both JSON and text payloads with the answer intact, the attributed cause text, silence for index_status, silence while an attempt is queued or running, and clearing after a completed rebuild.

Stack

  1. feat(index): add opt-in discovery resource limits #1723 — discovery limits
  2. feat(index): add worker resource watchdogs #1724 — worker watchdogs
  3. feat(index): enforce opt-in storage resource limits #1725 — storage limits
  4. feat(index): add opt-in index resource profiles #1726 — resource profiles
  5. feat(index): record and report the latest index attempt #1727 — attempt record and freshness
  6. this PR — stale-index warning on answers

@github-actions

Copy link
Copy Markdown

Thanks for opening this — it has been seen, and it is queued.

This note is automated, but it is not a brush-off: it exists so you know where your PR stands instead of having to guess from silence.

Current review status: working through a backlog. 0.9.1-rc.1 is out, so the release freeze that held reviews is over — but it left a large queue of open pull requests behind it, and we are reading through them oldest-first. The background is in discussion #1144.

What that means for this PR, concretely:

  • It will not be closed for inactivity. No stale bot touches pull requests here.
  • It may still sit a while before a human reads it. That is on us, not on you.
  • Older PRs are read first, so a recent one is not being skipped — it is behind a queue.

Things that will genuinely speed it up whenever review does happen:

  • Keep it rebased on main — the tree is moving quickly right now, and a conflicting branch cannot be reviewed as the diff you intended.
  • Get CI green, or say which failures you believe are pre-existing.
  • Keep the change to one claim. Bundled features and refactors get split before they get merged, which costs you a round trip.
  • Every commit needs a sign-off (git commit -s) — CI enforces DCO.

If this fixes a bug, a reproduction we can run is worth more than a description of the symptom.

Thanks for contributing, and sorry in advance for the wait.

Indexing accepts whatever a repository contains. A tree carrying a vendored
monorepo, a generated dump, or a runaway build directory is discovered in
full, and the first sign of trouble is a host under memory pressure with
nothing that attributes it to indexing.

Add two opt-in limits evaluated during discovery against accepted source
files only: index_max_files and index_max_source_mb. Both default to off, so
nothing changes until an operator sets one. Crossing a limit fails the whole
attempt with a structured resource_limit_exceeded result naming the resource,
the observed value and the limit; no partial graph is published, and an
existing serving index keeps answering.

Limits are read from the CLI-managed _config.db and are not MCP request
arguments. A supervised parent replaces any caller-supplied policy before
spawning its worker, and the worker rejects a missing or incomplete contract,
so the CLI, the daemon and the supervised worker all enforce the same
decision.

The two shell regressions that hand-roll the supervisor's worker argv carry
that contract as well. Without it the worker exits before either guard can
observe anything, and the guard would go quietly vacuous.

Signed-off-by: 刘冲 <mail@liuchong.dev>
Discovery limits bound what indexing accepts, not what it then costs. A
repository well inside those bounds can still exhaust the host through parser
memory, or simply never finish, and a supervised worker that hangs leaves the
parent waiting with nothing to report.

Add index_max_rss_mb and index_max_duration_seconds, enforced by the parent
against the worker process tree rather than the worker process alone, so a
runaway child cannot hide behind a small parent. Resident memory is sampled
through the platform interface on macOS, Linux and Windows. Crossing a limit
terminates the tree and yields one trusted, structured terminal result that
attributes the failure to the resource that caused it.

Both limits default to off. A measurement that cannot be taken fails the
attempt instead of passing it: a watchdog that quietly stops watching is
worse than no watchdog at all.

The shell fixture that stands in for the supervisor names the two new keys.
The worker accepts only a policy that spells out every key it knows, which is
what keeps a stale supervisor from starting a worker it cannot bound.

Signed-off-by: 刘冲 <mail@liuchong.dev>
An index that fits in memory and finishes in time can still fill the disk.
Publication needs room for the staging artifacts and the final database at
the same time, and running out of space during publication is the one
failure that can cost a working index.

Add index_cache_max_mb and index_min_free_disk_mb, measured before staging
and again before publication, together with internal ceilings on the final
database, the staging artifacts and the task temporary directory. Those three
have no public keys because they are only meaningful as part of one composed
decision. Crossing any of them fails the attempt before the old database is
touched, so atomic publication is unchanged and the previous index keeps
serving.

Staging cleanup is scoped by a private per-task token, so a worker removes
only the artifacts it created and never a concurrent run's. An old database
is treated as replaceable only after an integrity verdict distinguishes real
corruption from a transient busy error. A probe that cannot complete fails
closed. Both public keys default to off.

The shell fixture that stands in for the supervisor names them too, so the
worker still recognises the policy it is handed.

Signed-off-by: 刘冲 <mail@liuchong.dev>
@liuchong
liuchong force-pushed the feat/index-stale-warning branch 2 times, most recently from f22b6d0 to c47efae Compare August 19, 2026 18:37
Six independent keys are an honest interface and a poor default. An operator
who only wants indexing to stay within reason has to learn all six, choose a
value for each, and keep them consistent as the machine changes.

Add index_resource_profile, accepting off, balanced or strict. Balanced
guards the host: it lets an index use what the machine can spare and stops a
runaway worker before the host is exhausted. Strict holds indexing to the
daemon's own budget, so a repository that needs more fails fast and
attributed instead of finishing at the host's expense. Any individual key,
including an explicit off, replaces that dimension of the selected profile,
so a profile is a baseline rather than a lock.

The balanced worker ceiling is derived from detected host memory instead of
being tabled. Large-repository indexing peaks in the tens of gigabytes, so
any round number low enough to feel safe would reject repositories that index
successfully today. When host memory cannot be read, balanced falls back to a
fixed floor.

Profiles also bound directory count, entry count, traversal depth and
discovery time. These have no individual keys for the same reason as the
storage internals: they are only meaningful inside a composed decision. The
resolved profile travels through the same trusted worker contract as the
individual limits, so every entry point enforces the same decision.

That contract now also carries the resolved profile and the override mask, so
the shell fixture standing in for the supervisor sends both.

The coverage that proves a forged policy cannot fork a second job asserts
that an explicit request joins the auto-index job already running for the
same root, and that assertion found the join broken on Windows. A project
root reaches the daemon through the platform canonicalizer, which answers in
backslash form there, while every tool handler normalizes the separators of
the path it canonicalizes. One directory therefore had two spellings, and
they met in comparisons that are exact: whether an index request may join the
running job, and whether a watch is still live for that root. Both were
wrong on Windows, in opposite directions, so aligning either one alone moved
the failure rather than removing it. The session context now stores the
normalized spelling, and the daemon canonicalizes project roots through a
single function that normalizes on the way out.

Only the storage side is directly assertable off Windows, since separators
are folded on every platform; the watch-liveness side is held by the existing
watcher-ownership tests, which is where the second half of this surfaced.

Signed-off-by: 刘冲 <mail@liuchong.dev>
A failed index is currently a log line. A watcher-triggered rebuild that dies
on a resource limit leaves the previously published graph in service and
nothing durable behind it, so index_status reports a healthy generation and
the operator has no way to learn that it is no longer the tree.

Record one attempt per project under the cache directory, updated under a
file lock across queued, running and terminal states, carrying the origin,
timestamps, the effective profile source and the resource failure when there
is one. index_status returns it as last_index_attempt, and delete_project
removes a matching record when no database was published.

Freshness is reported alongside it and is deliberately conservative. A
generation is comparable only when matching clean Git snapshots were observed
before and after indexing, and it is fresh only while the worktree is still
clean at that HEAD. A different clean HEAD is stale. Non-Git roots, dirty
trees, a tree that changed during indexing, a failed probe and a corrupt
record are all unknown, because a dirty tree is evidence of neither freshness
nor staleness.

Daemon startup fails attempts whose worker no longer exists. Liveness is
judged by process identity rather than the pid alone, so a recycled pid
cannot keep a dead attempt alive, and an unavailable start time falls back to
plain liveness rather than declaring the worker gone. Projects with no record
keep the previous response shape.

Signed-off-by: 刘冲 <mail@liuchong.dev>
A recorded attempt is only visible to whoever thinks to ask for it. An agent
that calls search_graph after a background rebuild failed gets a confident
answer from a graph that no longer matches the tree, and nothing in that
response says so.

While the recorded attempt is failed or cancelled, attach a warning to the
answers served from that project's graph, naming the origin, the recorded
finish time and the limit that ended the attempt. A JSON payload gains a
stale_index_warning field so a structured reader cannot miss it; text and
tree payloads gain a trailing content block. The answer itself keeps its
position and its content.

index_status and check_index_coverage already report freshness themselves and
index_repository is the remedy, so none of them is annotated. A queued or
running rebuild is not yet a failure and stays silent, a completed rebuild
clears the warning, and every failure inside the warning path is silent: a
warning that cannot be produced must never damage an answer that is fine.

Signed-off-by: 刘冲 <mail@liuchong.dev>
@liuchong
liuchong force-pushed the feat/index-stale-warning branch from c47efae to b810789 Compare August 19, 2026 20:18
@liuchong liuchong closed this Aug 19, 2026
@liuchong liuchong reopened this Aug 19, 2026
@liuchong liuchong closed this Aug 20, 2026
@liuchong liuchong reopened this Aug 20, 2026
@liuchong
liuchong marked this pull request as ready for review August 20, 2026 03:38
@liuchong
liuchong requested a review from DeusData as a code owner August 20, 2026 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant