Skip to content

code.map invalidates cached maps when the indexed head advances - #1825

Merged
zlareb1 merged 6 commits into
ClickHouse:mainfrom
yorkhellen:fix/code-map-artifact
Dec 1, 2026
Merged

zlareb1 merged 6 commits into
ClickHouse:mainfrom
yorkhellen:fix/code-map-artifact

Conversation

@yorkhellen

@yorkhellen yorkhellen commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #1823

Changelog category

  • Bug Fix (user-visible misbehavior in a release)

Changelog entry

code.map rebuilds cached maps after the indexed head advances and returns the head stored with the map. Rendering retries when it detects a completed index advance.

Why

The text cache keys only on org, namespace, module prefix and token budget. Reading git_head after retrieving that text pairs an older map with a newer head.

Store the rendered text and observed head together, and include the head in the cache key. On a miss, compare the head before and after rendering; discard the result if they differ. When all configured attempts fail, return a retryable overload error without caching the result.

Head validation fixes the stale-cache failure without adding a storage generation or changing the indexing pipeline. Immutable generations would be needed for full snapshot consistency.

Scope and risk

Changes the map cache used by code.map and orientation-mode code.context_pack. Cache hits keep the head stored with their text even if the index advances just after lookup; the next request sees the new head.

git_head is an indexing watermark. Progressive index writes, summaries and graph statistics can change before or independently of that watermark. This change does not make those reads an atomic snapshot or certify that all map content belongs to one Git commit.

The cache keeps its 900-second TTL and 200-entry bound. Retaining entries for different heads can increase eviction churn. A miss adds a head validation query. code_map_render_attempts bounds rendering attempts, defaults to 3 and requires a value of at least 1. Larger values can increase latency during repeated index advances.

Compatibility, rollout and rollback

  • Wire: no operations, request fields or response fields added or removed. commit_sha comes from the cached record. code.map and orientation-mode code.context_pack can return the existing retryable HTTP 503 overload error when the head changes during every render attempt.
  • Operator steps: no migration, re-index or backfill. The default needs no configuration. Set LOOM_CODE_MAP_RENDER_ATTEMPTS or code_map_render_attempts in the configuration file to change the limit; restart the server to apply it.
  • Rollback: revert the code and remove the new configuration key if set; the process-local cache is discarded on restart. No stored rows are added.
  • Configuration reference, JSON Schema, YAML example and default snapshot include the setting. Public usage documentation will be refreshed after the next release, if needed.

Tests

  • make format, make lint, make typecheck.
  • Focused unit tests for map rendering, orientation, transport, settings, documentation and tunable constants: 897 passed, 1 failed.
  • The HTTP regression tests/integration/test_code_map_cache.py passed against a private migrated ClickHouse and source server before the settings follow-up. It was not rerun for this follow-up.

The HTTP regression fails on the original implementation because the second response still contains the A summary while reporting head B. It passes with this fix and checks a repeated B request.

Unit regressions cover an empty initial head, warm-cache invalidation, an advance just after cache lookup, advances during rendering, retry exhaustion without caching, recovery and orientation-mode cache sharing. The settings follow-up covers limits of 1, 3 and 5 attempts, environment loading, the default and rejection of zero and negative values.

The token-budget test is adapted to the helper's record return type and head read; its tiny-budget and unlimited-budget assertions are preserved. The task-search fallback test also returns the map record from its stub, preserving its orientation and context assertions.

The remaining failure is test_no_new_tunable_constant, which reports PLACES_TOKEN_BUDGET, TOP_SOURCE_MAX_LINES and _QUERY_WORDS_MAX in server/transport/code_lean.py. The same test fails on isolated upstream main at a5a018489 with those three constants; this PR does not change that file. _MAP_RENDER_ATTEMPTS is removed and no longer appears in the failure.

The full unit suite and remaining integration checks are running in CI. Dependency review failed before completing its scan: Invalid dependency snapshot warning header. A separate API request reports that no dependency snapshot exists for this head; its decoded warning is plain text, while the unchanged workflow expects JSON. This is not a completed vulnerability scan.

AI assistance in the code

  • AI-generated: GPT wrote most of the diff. Agent checks include the caller paths, failure interleavings, local gates and a real HTTP regression. Maintainer review is pending.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Local format, lint and type checks pass. Focused tests pass apart from the reproduced upstream tunable-constants failure; full-suite results remain for CI.
  • Generated settings files and the default snapshot are included; no migration, plugin or frontend changes are required.
  • Operator steps and wire behavior are described above.
  • Public text omits deployment identities and credentials.
  • Commits are signed off.
  • I signed the Contributor License Agreement if CLA assistant requested a signature.

The handler read the current commit after retrieving cached map text, so
an index advance could attach a new commit to an older map.

Cache the map text and commit together, key entries by commit, and retry
when the index head changes during rendering. After three unstable
attempts, return a retryable error without caching the result.

Cover warm-cache advances, render-time changes, retry exhaustion and
orientation reuse. The HTTP regression checks the map and commit after a
completed index advance.

Signed-off-by: zhangxiaoyu.york <zhangxiaoyu.york@bytedance.com>
Signed-off-by: zhangxiaoyu.york <zhangxiaoyu.york@bytedance.com>
The map helper returns text with its observed head. Preserve the token-budget assertions after merging the new tests from main.

Signed-off-by: zhangxiaoyu.york <zhangxiaoyu.york@bytedance.com>
The orientation handler reads artifact.text. Keep the empty-search fallback test aligned with that helper contract.

Signed-off-by: zhangxiaoyu.york <zhangxiaoyu.york@bytedance.com>
@vercel

vercel Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Dec 1, 2026 12:46am UTC
loom-telemetry Ready Ready Preview Dec 1, 2026 12:46am UTC

Request Review

@zlareb1 zlareb1 self-assigned this Nov 30, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The focused map, orientation and transport checks passed. The tunable-constants gate fails on the new retry count.

Comment thread server/code/app/context.py Resolved
Read the render bound from settings with a default of three and a minimum
of one. Cover configured bounds, environment loading and invalid values,
and regenerate the configuration contracts.

Signed-off-by: zhangxiaoyu.york <zhangxiaoyu.york@bytedance.com>
Signed-off-by: zhangxiaoyu.york <zhangxiaoyu.york@bytedance.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map and orientation paths read code_map_render_attempts from the settings catalog. The 97 focused map, orientation, transport and settings tests pass on the current merge tree.

@zlareb1

zlareb1 commented Dec 1, 2026

Copy link
Copy Markdown
Member

The tunable-constants gate reports the same three search limits when run locally on this merge tree and on clean main. The gate, map and settings-defaults tests pass together with the existing repair. Main job · Repair #1871

@zlareb1

zlareb1 commented Dec 1, 2026

Copy link
Copy Markdown
Member

The dependency review job stops because GitHub returns a missing head snapshot warning. The same failure reproduces on clean main. I replayed the API responses with the workflow fix; it completes the vulnerability review after verifying the snapshot inputs are unchanged.

@zlareb1
zlareb1 merged commit 79a0ee5 into ClickHouse:main Dec 1, 2026
78 of 80 checks passed
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.

code.map returns a cached map with a newer commit_sha

2 participants