witan-code 0.12.3__tar.gz → 0.13.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. {witan_code-0.12.3 → witan_code-0.13.1}/CHANGELOG.md +51 -0
  2. {witan_code-0.12.3 → witan_code-0.13.1}/PKG-INFO +3 -3
  3. {witan_code-0.12.3 → witan_code-0.13.1}/pyproject.toml +41 -4
  4. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_remote_proxy.py +23 -0
  5. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_remote_store.py +66 -0
  6. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/bridge.py +21 -5
  7. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/cli.py +13 -3
  8. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/indexer.py +35 -4
  9. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/remote/oidc.py +17 -0
  10. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/remote/proxy.py +27 -2
  11. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/remote/store.py +63 -0
  12. {witan_code-0.12.3 → witan_code-0.13.1}/.gitignore +0 -0
  13. {witan_code-0.12.3 → witan_code-0.13.1}/README.md +0 -0
  14. {witan_code-0.12.3 → witan_code-0.13.1}/config/claude.json +0 -0
  15. {witan_code-0.12.3 → witan_code-0.13.1}/config/copilot.json +0 -0
  16. {witan_code-0.12.3 → witan_code-0.13.1}/config/pi.json +0 -0
  17. {witan_code-0.12.3 → witan_code-0.13.1}/docs/BRANCH_INDEXING.md +0 -0
  18. {witan_code-0.12.3 → witan_code-0.13.1}/docs/CLI_REFERENCE.md +0 -0
  19. {witan_code-0.12.3 → witan_code-0.13.1}/docs/EDGE_PRECISION_TIERS.md +0 -0
  20. {witan_code-0.12.3 → witan_code-0.13.1}/docs/PACKAGE_MAP.md +0 -0
  21. {witan_code-0.12.3 → witan_code-0.13.1}/docs/STAGE2_STITCHING.md +0 -0
  22. {witan_code-0.12.3 → witan_code-0.13.1}/docs/SYMBOL_FORMAT.md +0 -0
  23. {witan_code-0.12.3 → witan_code-0.13.1}/docs/SYMBOL_TABLE.md +0 -0
  24. {witan_code-0.12.3 → witan_code-0.13.1}/docs/USER_GUIDE.md +0 -0
  25. {witan_code-0.12.3 → witan_code-0.13.1}/install.sh +0 -0
  26. {witan_code-0.12.3 → witan_code-0.13.1}/tests/__init__.py +0 -0
  27. {witan_code-0.12.3 → witan_code-0.13.1}/tests/conftest.py +0 -0
  28. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_batched_deletes.py +0 -0
  29. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_branches.py +0 -0
  30. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_bridge.py +0 -0
  31. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_chunked_load.py +0 -0
  32. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_cli_dispatch.py +0 -0
  33. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_cli_hooks.py +0 -0
  34. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_cli_output_format.py +0 -0
  35. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_config.py +0 -0
  36. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_config_targets.py +0 -0
  37. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_context.py +0 -0
  38. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_crossrepo.py +0 -0
  39. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_edges.py +0 -0
  40. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_elicit.py +0 -0
  41. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_enrichment.py +0 -0
  42. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_github_app.py +0 -0
  43. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_graph.py +0 -0
  44. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_hooks.py +0 -0
  45. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_identity.py +0 -0
  46. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_indexer.py +0 -0
  47. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_indexer_scope.py +0 -0
  48. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_ingest.py +0 -0
  49. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_languages.py +0 -0
  50. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_maintenance.py +0 -0
  51. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_package_map.py +0 -0
  52. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_reaper.py +0 -0
  53. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_repo.py +0 -0
  54. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_server.py +0 -0
  55. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_setup.py +0 -0
  56. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_stitch.py +0 -0
  57. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_store.py +0 -0
  58. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_symbol_table.py +0 -0
  59. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_tasks.py +0 -0
  60. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_views.py +0 -0
  61. {witan_code-0.12.3 → witan_code-0.13.1}/tests/test_visualize.py +0 -0
  62. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/__init__.py +0 -0
  63. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/__main__.py +0 -0
  64. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/bridge_extractors.py +0 -0
  65. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/config.py +0 -0
  66. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/context.py +0 -0
  67. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/edges.py +0 -0
  68. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/elicit.py +0 -0
  69. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/extensions/pi/codegraph.ts +0 -0
  70. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/github_app.py +0 -0
  71. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/graph.py +0 -0
  72. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/hooks.py +0 -0
  73. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/identity.py +0 -0
  74. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/ingest.py +0 -0
  75. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/maintenance.py +0 -0
  76. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/output.py +0 -0
  77. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/package_map.py +0 -0
  78. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries/bridge.gq +0 -0
  79. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries/code_mutations.gq +0 -0
  80. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries/code_read.gq +0 -0
  81. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries/delete.gq +0 -0
  82. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries_ts/bash.scm +0 -0
  83. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries_ts/hcl.scm +0 -0
  84. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries_ts/python.scm +0 -0
  85. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries_ts/sql.scm +0 -0
  86. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries_ts/typescript.scm +0 -0
  87. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/queries_ts/yaml.scm +0 -0
  88. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/reaper.py +0 -0
  89. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/remote/__init__.py +0 -0
  90. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/repo.py +0 -0
  91. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/schema/bridge-schema.pg +0 -0
  92. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/schema/code-schema.pg +0 -0
  93. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/server.py +0 -0
  94. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/setup.py +0 -0
  95. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/skills/witan-code/SKILL.md +0 -0
  96. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/stitch.py +0 -0
  97. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/store.py +0 -0
  98. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/views.py +0 -0
  99. {witan_code-0.12.3 → witan_code-0.13.1}/witan_code/visualize.py +0 -0
@@ -6,6 +6,57 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/) (pre-1.0:
7
7
  a MINOR bump may include breaking changes).
8
8
 
9
+ ## [0.13.1] - 2026-08-17
10
+
11
+ ### Fixed
12
+
13
+ - **Raised the `witan-core` floor to `>=0.22`, so an index write that loses a
14
+ race retries instead of failing outright.** 0.22 classifies a write-authority
15
+ conflict (HTTP 409, and the CLI's equivalent wording) as retryable rather
16
+ than fatal.
17
+
18
+ That matters here for a different reason than it does in `witan-council`, and
19
+ the difference is worth stating: `RemoteStoreClient.change` never sets
20
+ `surface_conflict`, so the compare-and-swap half of that fix is irrelevant to
21
+ the code graph. The ordinary-writer half is not. This package's **branched**
22
+ clients deliberately fall back to the CLI subprocess — `_transport()` returns
23
+ `None` whenever `_extra_args` is non-empty, because omnigraph's HTTP API has
24
+ no request-side branch selector — so every write from a branch view is
25
+ classified by `_classify_cli_error`, which treated the conflict as fatal.
26
+
27
+ On witan-core 0.21, a branch-view write that merely raced another writer on
28
+ the shared graph fails; on 0.22 it retries and commits.
29
+
30
+ A **behavioural** floor, and a deliberate exception to this package's stated
31
+ "floors track what this package actually imports" rule — the same rule under
32
+ which the 0.19/0.20 trace-context releases were explicitly *not* pinned. The
33
+ difference is that those changed nothing on this package's write path.
34
+
35
+ ## [0.13.0] - 2026-08-13
36
+
37
+ ### Fixed
38
+
39
+ - **An index write cut off by a gateway is no longer retried.**
40
+ `RemoteStoreClient` reconnects and retries on a transport fault, which is
41
+ right for a dropped connection and wrong for a 502/504: measured against the
42
+ deployed service, most writes cut that way had already committed — one run of
43
+ 4-, 8- and 16-writer bursts committed all 28 of its writes, and a later
44
+ standalone 16-writer burst committed 14 of 16 — so the blind retry writes
45
+ those rows a
46
+ second time. `_refuse_if_indeterminate` now stops it and says the outcome is
47
+ unknown, matching `_refuse_if_too_large` beside it
48
+ ([#225](https://github.com/mitodl/agent-kit/pull/225)).
49
+ ★ This transport is separate from the CLI proxy, and had to be fixed
50
+ separately. `RemoteServerProxy._writes()` returns `False` because nothing
51
+ *that proxy* dispatches writes — `code_reindex` is refused as local-only —
52
+ which is easy to misread as "witan-code never writes remotely". Index writes
53
+ go out over `code_store_mutate` / `code_store_mutate_many` /
54
+ `code_store_load`, on this client, and it does its own classification.
55
+ - The CI indexer's per-repo delete no longer times out. The batch was sized
56
+ against the argv length limit while the real constraint is time: a
57
+ 500-statement delete chunk takes ~181s against a 120s client timeout
58
+ ([#223](https://github.com/mitodl/agent-kit/pull/223)).
59
+
9
60
  ## [0.12.3] - 2026-08-07
10
61
 
11
62
  ### Fixed
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: witan-code
3
- Version: 0.12.3
3
+ Version: 0.13.1
4
4
  Summary: witan-code — tree-sitter code graph + cross-repo bridge (mounts under `witan code`)
5
5
  Project-URL: Homepage, https://github.com/mitodl/agent-kit/tree/main/mcp/servers/witan-code
6
6
  Project-URL: Repository, https://github.com/mitodl/agent-kit
@@ -26,7 +26,7 @@ Requires-Dist: tree-sitter-sql<0.4,>=0.3
26
26
  Requires-Dist: tree-sitter-typescript<0.24,>=0.23
27
27
  Requires-Dist: tree-sitter-yaml<0.8,>=0.7
28
28
  Requires-Dist: tree-sitter<0.26,>=0.25
29
- Requires-Dist: witan-core[cli,observability,remote]<1,>=0.14
29
+ Requires-Dist: witan-core[cli,observability,remote]<1,>=0.22
30
30
  Provides-Extra: tasks
31
31
  Requires-Dist: fastmcp-tasks<5,>=4.0.0b1; extra == 'tasks'
32
32
  Description-Content-Type: text/markdown
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "witan-code"
3
- version = "0.12.3"
3
+ version = "0.13.1"
4
4
  description = "witan-code — tree-sitter code graph + cross-repo bridge (mounts under `witan code`)"
5
5
  readme = "README.md"
6
6
  license = "BSD-3-Clause"
@@ -22,13 +22,50 @@ dependencies = [
22
22
  # and it has now been tripped THREE times. If you add a witan_core symbol in
23
23
  # the same change as its caller, bump this line in that change: the
24
24
  # workspace resolves witan-core by path, so nothing here can catch it.
25
- # (>=0.14 was required for remote.proxy.RemoteToolFailed, re-exported at
25
+ # (NOT raised for witan-core 0.19/0.20's trace-context work, deliberately.
26
+ # Nothing here imports `trace_context_middleware`, and standalone
27
+ # `witan-code serve` is stdio-only — the ASGI middleware is installed by
28
+ # `witan serve`, whose own package declares the floor it needs. Raising this
29
+ # one would buy no behaviour and would misstate the minimum. Floors here
30
+ # track what this package actually imports, not what was released alongside
31
+ # it;
32
+ # >=0.17 for remote.proxy.RemoteWriteIndeterminate AND gateway_failure,
33
+ # both imported at module scope by remote/store.py — so an older witan-core
34
+ # fails at IMPORT, exactly like the cases above; it does not degrade into
35
+ # retrying an indeterminate write, because the module holding that retry is
36
+ # the one that fails to load. Worth pinning precisely because store.py is
37
+ # the transport index writes actually go out on: the floor is what keeps a
38
+ # published install from being dead on arrival rather than subtly wrong;
39
+ # >=0.14 was required for remote.proxy.RemoteToolFailed, re-exported at
26
40
  # module scope by witan_code/remote/proxy.py and named by cli.py's cli();
27
41
  # >=0.11 for chunking.MCP_LOAD_MAX_BYTES, evaluated as a default argument;
28
42
  # >=0.10 for witan_core.chunking + load_batch, both moved up from this
29
43
  # package; >=0.9 for observability.middleware; >=0.8 for
30
44
  # OmnigraphClient.change_many with chunk_size.)
31
- "witan-core[cli,remote,observability]>=0.14,<1",
45
+ #
46
+ # ★ >=0.22 IS A BEHAVIOURAL FLOOR, the first here, and it is a deliberate
47
+ # exception to the "floors here track what this package actually imports"
48
+ # rule stated above — so read this before treating that rule as absolute.
49
+ # Nothing new is imported; an older witan-core loads fine and is wrong
50
+ # quietly.
51
+ #
52
+ # 0.22 classifies a write-authority conflict (HTTP 409, and the CLI's
53
+ # equivalent prose) as RETRYABLE rather than FATAL. That matters HERE, and
54
+ # not for the CAS reason it matters in witan-council: `change` above never
55
+ # sets `surface_conflict`, so the compare-and-swap half is irrelevant to the
56
+ # code graph. The ordinary-writer half is not. This package's BRANCHED
57
+ # clients deliberately fall back to the CLI subprocess — `_transport()`
58
+ # returns None whenever `_extra_args` is non-empty, because omnigraph's HTTP
59
+ # API has no request-side branch selector — so every write from a branch
60
+ # view is classified by `_classify_cli_error`, where the conflict was fatal.
61
+ # On 0.21 an index write that merely lost a race to a concurrent writer on
62
+ # the shared graph fails outright; on 0.22 it retries and commits.
63
+ #
64
+ # Maintainer decision 2026-08-17. Note this is the case the earlier
65
+ # parenthetical explicitly declined for 0.19/0.20 — the difference is that
66
+ # trace-context work changed nothing on this package's write path, and this
67
+ # does.
68
+ "witan-core[cli,remote,observability]>=0.22,<1",
32
69
  "fastmcp>=3.4.2,<5",
33
70
  "cyclopts>=4,<5",
34
71
  # [targets.<name>] override models (config.py) — also transitively pulled
@@ -112,7 +149,7 @@ testpaths = ["tests"]
112
149
  packages = ["witan_code"]
113
150
 
114
151
  [tool.bumpversion]
115
- current_version = "0.11.1"
152
+ current_version = "0.13.1"
116
153
  allow_dirty = true
117
154
 
118
155
  [[tool.bumpversion.files]]
@@ -242,3 +242,26 @@ def test_cli_prints_an_unreachable_remote_instead_of_a_traceback(monkeypatch, ca
242
242
  cli_module.cli()
243
243
  assert exit_code.value.code == 1
244
244
  assert "witan-code is down at X" in capsys.readouterr().out
245
+
246
+
247
+ def test_cli_prints_a_rejected_credential_instead_of_a_traceback(monkeypatch, capsys):
248
+ """★ witan-code's handler has to list this too.
249
+
250
+ The remote path can now raise `RemoteCredentialRejected` — on a write, or on
251
+ a read whose refreshed retry is refused again. The `witan` CLI was updated
252
+ and this one was not, so it would have escaped as the traceback the whole
253
+ handler exists to prevent.
254
+ """
255
+ from types import SimpleNamespace
256
+
257
+ from witan_code import cli as cli_module
258
+ from witan_code.remote.proxy import RemoteCredentialRejected
259
+
260
+ def _rejected():
261
+ raise RemoteCredentialRejected("witan-code: it rejected the credential")
262
+
263
+ monkeypatch.setattr(cli_module, "app", SimpleNamespace(meta=_rejected))
264
+ with pytest.raises(SystemExit) as exit_code:
265
+ cli_module.cli()
266
+ assert exit_code.value.code == 1
267
+ assert "rejected the credential" in capsys.readouterr().out
@@ -23,6 +23,7 @@ from witan_code import config as cfg_module
23
23
  from witan_code import store as store_module
24
24
  from witan_code.remote.store import (
25
25
  RemotePayloadTooLarge,
26
+ RemoteWriteIndeterminate,
26
27
  RemoteStoreClient,
27
28
  RemoteStoreUnsupported,
28
29
  StoreSession,
@@ -789,3 +790,68 @@ def test_a_reconnect_re_asks_the_surface():
789
790
  client.change_many(_steps(1))
790
791
  session.close()
791
792
  assert [entry for entry, _ in log].count("list_tools") == 2
793
+
794
+
795
+ def _gateway(status: int):
796
+ import httpx2
797
+
798
+ request = httpx2.Request("POST", "http://x/mcp")
799
+ response = httpx2.Response(status, request=request, text="<html>502 Bad Gateway")
800
+ return httpx2.HTTPStatusError(str(status), request=request, response=response)
801
+
802
+
803
+ @pytest.mark.parametrize("status", [502, 504])
804
+ def test_a_write_cut_off_by_a_gateway_is_never_re_invoked(status):
805
+ """★ THE RETRY IS THE BUG, and here it duplicates data rather than wasting a
806
+ round trip.
807
+
808
+ A 502/504 is not a ``ToolError``, so it lands in the generic arm — which
809
+ reconnects and sends the write AGAIN. Measured against CI, the backend
810
+ usually finished the write before the deadline cut the response (28 of 28
811
+ committed in one burst, 14 of 16 in another), so that second invocation
812
+ writes the rows a second time. The outcome is indeterminate, not failed, and
813
+ the only safe response is to stop and say so.
814
+ """
815
+ log: list = []
816
+ exc = _gateway(status)
817
+ clients = iter([_RefusesForSize(log, exc), _RefusesForSize(log, exc)])
818
+ session = StoreSession("http://x/mcp", lambda: "jwt", lambda _t: next(clients))
819
+ with pytest.raises(RemoteWriteIndeterminate) as caught:
820
+ RemoteStoreClient(REPO, session).load([{"id": "n1", "type": "Symbol"}])
821
+ # Exactly one attempt: the reconnect-and-retry must not have happened.
822
+ assert [entry[0] for entry in log] == ["connect", "attempt"]
823
+ message = str(caught.value)
824
+ assert "INDETERMINATE" in message
825
+ assert "NOT retried" in message
826
+ assert f"HTTP {status}" in message
827
+ assert "`code_store_load`" in message
828
+
829
+
830
+ def test_a_read_cut_off_by_a_gateway_still_reconnects():
831
+ """The other half: repeating a read changes nothing, so a dropped session is
832
+ exactly what the reconnect is for. Refusing it would be a strict loss."""
833
+ log: list = []
834
+
835
+ class _FailsOnceWithGateway(_FakeClient):
836
+ def __init__(self, log, fail):
837
+ super().__init__(log)
838
+ self._fail = fail
839
+
840
+ async def call_tool(self, name, arguments):
841
+ if self._fail:
842
+ self._log.append(("attempt", name))
843
+ raise _gateway(502)
844
+ return await super().call_tool(name, arguments)
845
+
846
+ clients = iter(
847
+ [_FailsOnceWithGateway(log, True), _FailsOnceWithGateway(log, False)]
848
+ )
849
+ session = StoreSession("http://x/mcp", lambda: "jwt", lambda _t: next(clients))
850
+ assert RemoteStoreClient(REPO, session).list_branches() == ["main"]
851
+ assert [entry[0] for entry in log] == [
852
+ "connect",
853
+ "attempt",
854
+ "disconnect",
855
+ "connect",
856
+ "code_store_views",
857
+ ]
@@ -22,9 +22,22 @@ from .bridge_extractors import (
22
22
  from .graph import OmnigraphClient, check_writable
23
23
  from .store import bridge_store, ensure_bridge_store
24
24
 
25
- # Delete statements per commit when purging a repo's bindings. The composed
26
- # query is a single argv element, so a full-repo purge has to be capped.
27
- _PURGE_BATCH_SIZE = 500
25
+ # Delete statements per commit when purging a repo's bindings. A full-repo purge
26
+ # has to be capped rather than built whole.
27
+ #
28
+ # Same reasoning, and the same measurement, as `_DELETE_BATCH_SIZE` in
29
+ # `indexer.py` — read the comment there. Short version: the argv justification is
30
+ # stale (the deployed path is the HTTP transport, no argv), a matching delete
31
+ # costs ~362ms, and 500 of them is ~181s against a 120s client timeout. One
32
+ # statement per file here rather than two, so this is 128 files per commit.
33
+ #
34
+ # Lowered together with the indexer's cap deliberately. This purge has not been
35
+ # SEEN to time out, but it is the identical shape against the same engine, and a
36
+ # repo with enough changed files would trip it the same way. Fixing only the one
37
+ # that happened to fail first would leave the other waiting for a busy day.
38
+ # (The 362ms figure was measured on `Symbol` deletes; `InterfaceBinding` deletes
39
+ # were not timed separately, so treat this cap as the same order, not as exact.)
40
+ _PURGE_BATCH_SIZE = 128
28
41
 
29
42
 
30
43
  logger = get_logger("witan.code.bridge")
@@ -139,8 +152,11 @@ def write_bindings(
139
152
  }
140
153
  # One commit for the whole purge, not one per file: this runs over every
141
154
  # touched file on each index, and a commit apiece fragments the bridge store
142
- # the same way the per-file deletes did the per-repo one. Chunked because the
143
- # composed query rides in argv and a full-repo purge is unbounded.
155
+ # the same way the per-file deletes did the per-repo one. Chunked because a
156
+ # full-repo purge is unbounded and a matching delete costs ~362ms, so an
157
+ # uncapped batch runs past the client's 120s timeout — see
158
+ # `_PURGE_BATCH_SIZE` for the measurements and for why the argv reasoning
159
+ # this comment used to give no longer applies.
144
160
  client.change_many(
145
161
  [
146
162
  ("bridge.gq", "delete_bindings_in_file", {"repo_file": f"{repo}|{rel}"})
@@ -62,10 +62,14 @@ def _srv():
62
62
  print(exc)
63
63
  raise SystemExit(1) from None
64
64
  if remote is not None:
65
- from .remote.oidc import default_token_provider
65
+ from .remote.oidc import default_token_provider, default_token_refresher
66
66
  from .remote.proxy import RemoteServerProxy
67
67
 
68
- _server = RemoteServerProxy(remote, default_token_provider(remote))
68
+ _server = RemoteServerProxy(
69
+ remote,
70
+ default_token_provider(remote),
71
+ default_token_refresher(remote),
72
+ )
69
73
  else:
70
74
  from . import server as server_module
71
75
 
@@ -1012,7 +1016,12 @@ def cli() -> None:
1012
1016
  from rich.console import Console
1013
1017
 
1014
1018
  from .remote.oidc import RemoteAuthError
1015
- from .remote.proxy import RemoteToolFailed, RemoteToolUnavailable, RemoteUnreachable
1019
+ from .remote.proxy import (
1020
+ RemoteCredentialRejected,
1021
+ RemoteToolFailed,
1022
+ RemoteToolUnavailable,
1023
+ RemoteUnreachable,
1024
+ )
1016
1025
  from .remote.store import RemotePayloadTooLarge
1017
1026
 
1018
1027
  try:
@@ -1027,6 +1036,7 @@ def cli() -> None:
1027
1036
  # have.
1028
1037
  except (
1029
1038
  RemoteAuthError,
1039
+ RemoteCredentialRejected,
1030
1040
  RemotePayloadTooLarge,
1031
1041
  RemoteToolFailed,
1032
1042
  RemoteToolUnavailable,
@@ -39,10 +39,41 @@ logger = get_logger("witan.code.index")
39
39
  _QUERIES_TS_DIR = Path(__file__).parent / "queries_ts"
40
40
 
41
41
  # Delete statements per commit when clearing reindexed/purged files. A full-repo
42
- # reindex can touch thousands of files, and the composed query rides in argv, so
43
- # the batch has to be capped rather than built whole. Two statements per file, so
44
- # this is 250 files per commit.
45
- _DELETE_BATCH_SIZE = 500
42
+ # reindex can touch thousands of files, so the batch has to be capped rather than
43
+ # built whole. Two statements per file, so this is 64 files per commit.
44
+ #
45
+ # ★ THE BINDING CONSTRAINT IS TIME, NOT ARGV. The original cap was 500, justified
46
+ # by the composed query riding in a single argv element. That reasoning does not
47
+ # apply to the deployed indexer at all: it uses the pooled HTTP transport, where
48
+ # the query travels in a JSON body and argv is never involved. Meanwhile a
49
+ # MATCHING delete costs ~362ms server-side, so 500 statements is ~181 SECONDS —
50
+ # past `witan_core.omnigraph_http.DEFAULT_TIMEOUT_SECONDS` (120s), and the chunk
51
+ # fails with a bare "timed out".
52
+ #
53
+ # That is not hypothetical: it is why `witan-ci-index` failed every run from
54
+ # 2026-08-07, in CI and Production alike, always on ol-infrastructure —
55
+ # `indexer.py` change_many → chunk of 500 → timeout → "indexed 13, failed 1".
56
+ #
57
+ # Measured 2026-08-12 against the CI data tier, inserting then deleting real rows
58
+ # in `code-github-com-mitodl-lehrer`:
59
+ # 50 matching deletes 19.23s (385 ms/stmt)
60
+ # 100 matching deletes 36.29s (363 ms/stmt)
61
+ # 250 matching deletes 90.47s (362 ms/stmt)
62
+ # Linear, and — measured on the SMALLEST graph (1,567 rows) — essentially
63
+ # independent of table size, so this is not a big-repo problem. Any repo whose
64
+ # reindex touches ~165 files in one run would hit it; ol-infrastructure is simply
65
+ # the one busy enough to do so every four hours.
66
+ #
67
+ # 128 statements is ~46s, a 2.5x margin under the timeout. It does NOT make a
68
+ # reindex faster — the cost is linear per statement either way — it makes it
69
+ # COMPLETE, which for a four-hourly job is the property that matters.
70
+ #
71
+ # ★ Raising DEFAULT_TIMEOUT_SECONDS instead would be the wrong lever: that
72
+ # budget exists to catch a genuinely wedged server, and widening it to fit a
73
+ # known-slow operation blunts the one signal that says the server is stuck.
74
+ # The real defect is that a single keyed delete costs 362ms at all — tracked as
75
+ # tk-upstream-omnigraph-a-single-row-insert-costs-a-f-eeeae3.
76
+ _DELETE_BATCH_SIZE = 128
46
77
 
47
78
  # Stand-in written into CodeFile.content_hash for the duration of a load, so a
48
79
  # run that fails part-way leaves nothing the incremental skip check will match.
@@ -40,6 +40,7 @@ __all__ = [
40
40
  "cache_path",
41
41
  "decode_claims",
42
42
  "default_token_provider",
43
+ "default_token_refresher",
43
44
  "discover_endpoints",
44
45
  "get_valid_token",
45
46
  "login",
@@ -78,3 +79,19 @@ def logout(cfg: RemoteConfig) -> bool:
78
79
  def default_token_provider(cfg: RemoteConfig) -> Callable[[], str]:
79
80
  """A zero-arg callable the proxy calls per request to get a fresh token."""
80
81
  return lambda: get_valid_token(cfg)
82
+
83
+
84
+ def default_token_refresher(cfg: RemoteConfig) -> Callable[[str], str]:
85
+ """Force-mint a token, given the one the deployment rejected.
86
+
87
+ Handed to the proxy alongside the provider so it can recover from a
88
+ credential the deployment rejected while this client still believed it was
89
+ good — see ``RemoteMCPProxy.__init__``. Separate from the provider because
90
+ the provider is allowed to answer from cache, which on a rejected token is
91
+ exactly the wrong answer.
92
+
93
+ Takes the rejected token rather than re-reading it: under concurrent 401s
94
+ the cache may already hold somebody else's fresh one, and refreshing that
95
+ would spend a rotating refresh token for nothing.
96
+ """
97
+ return lambda rejected: _auth(cfg).force_refresh(rejected)
@@ -15,6 +15,7 @@ from typing import Callable
15
15
 
16
16
  from witan_core.remote.config import RemoteConfig
17
17
  from witan_core.remote.proxy import (
18
+ RemoteCredentialRejected,
18
19
  RemoteMCPProxy,
19
20
  RemoteToolFailed,
20
21
  RemoteToolUnavailable,
@@ -22,6 +23,7 @@ from witan_core.remote.proxy import (
22
23
  )
23
24
 
24
25
  __all__ = [
26
+ "RemoteCredentialRejected",
25
27
  "RemoteServerProxy",
26
28
  "RemoteToolFailed",
27
29
  "RemoteToolUnavailable",
@@ -39,13 +41,36 @@ _LOCAL_ONLY = frozenset({"code_reindex"})
39
41
  class RemoteServerProxy(RemoteMCPProxy):
40
42
  """Mirrors the ``witan_code.server`` tool surface, dispatching over MCP."""
41
43
 
42
- def __init__(self, cfg: RemoteConfig, token_provider: Callable[[], str]) -> None:
43
- super().__init__(cfg.url, token_provider)
44
+ def __init__(
45
+ self,
46
+ cfg: RemoteConfig,
47
+ token_provider: Callable[[], str],
48
+ token_refresher: Callable[[], str] | None = None,
49
+ ) -> None:
50
+ super().__init__(cfg.url, token_provider, token_refresher)
44
51
  self._url_source = cfg.url_source
45
52
 
46
53
  def _is_admin_tool(self, name: str) -> bool:
47
54
  return name in _LOCAL_ONLY
48
55
 
56
+ def _writes(self, name: str) -> bool:
57
+ # Nothing THIS PROXY dispatches remotely writes. `code_reindex` is the
58
+ # only tool on this surface that does, and `_LOCAL_ONLY` refuses it
59
+ # before it can ever reach a transport — so a gateway cut-off here is
60
+ # always a read that returned nothing, never a write whose fate is
61
+ # unknown. Stated as a rule rather than a list because it follows from
62
+ # `_LOCAL_ONLY` above: anything that starts writing has to be added
63
+ # there or here, and both are three lines apart.
64
+ #
65
+ # ★ NOT the same thing as "witan-code never writes remotely". Index
66
+ # writes go to the deployed store over a DIFFERENT transport —
67
+ # `remote.store.RemoteStoreClient`, dispatching `code_store_mutate`,
68
+ # `code_store_mutate_many` and `code_store_load` — which does its own
69
+ # gateway classification, and has to, because it reconnects and retries
70
+ # (`_refuse_if_indeterminate`). Reading this `False` as covering that
71
+ # path is the mistake this note exists to stop.
72
+ return False
73
+
49
74
  def _unreachable_hint(self) -> str:
50
75
  # Two ways to name the wrong setting here, and this avoids both.
51
76
  # `remote_url` is what routes a client to this proxy — NOT
@@ -36,6 +36,8 @@ from witan_core.observability import get_logger
36
36
  from witan_core.remote.proxy import (
37
37
  RemotePayloadTooLarge,
38
38
  RemoteToolFailed,
39
+ RemoteWriteIndeterminate,
40
+ gateway_failure,
39
41
  payload_too_large,
40
42
  )
41
43
 
@@ -125,6 +127,54 @@ def _load_refusal(exc: BaseException, *, batch: int, mode: str, max_bytes: int)
125
127
  # asked rather than assumed — see `RemoteStoreClient.change_many`.
126
128
  _MUTATE_MANY_TOOL = "code_store_mutate_many"
127
129
 
130
+ # The store tools that change nothing. Everything else on this transport is
131
+ # treated as a write.
132
+ #
133
+ # ★ THE READS ARE LISTED, NOT THE WRITES, so an unclassified tool defaults to
134
+ # "assume it wrote" — the same asymmetry `RemoteMCPProxy._writes` is built on.
135
+ # Calling a read a write costs one over-careful sentence; calling a write a read
136
+ # tells somebody their index landed when it may not have, or invites the retry
137
+ # that writes it twice.
138
+ #
139
+ # `code_store_open` is deliberately absent: opening can create the store, so it
140
+ # is not a read.
141
+ _READ_ONLY_STORE_TOOLS = frozenset({"code_store_read", "code_store_views"})
142
+
143
+
144
+ def _refuse_if_indeterminate(exc: BaseException, tool: str, url: str) -> None:
145
+ """Re-raise a WRITE cut off by a gateway as indeterminate. Otherwise no-op.
146
+
147
+ ★ THIS TRANSPORT'S RECONNECT-AND-RETRY IS WHY THIS HAS TO EXIST. A 502/504
148
+ is not a ``ToolError``, so it lands in the generic arm of
149
+ :meth:`StoreSession.call`, which reconnects and re-invokes — and for a write
150
+ cut off mid-flight, re-invoking is the duplicate this whole change exists to
151
+ prevent. Measured against CI: a burst's 502s mostly HAD committed (28 of 28
152
+ in one run, 14 of 16 in another), so the blind retry writes those rows a
153
+ second time.
154
+
155
+ Exactly the shape of ``_refuse_if_too_large`` above, and called from the
156
+ same two places for the same reason: retrying cannot help, and the retry
157
+ actively hurts.
158
+
159
+ Reads fall through untouched — repeating one changes nothing, and the
160
+ reconnect is the right response to a dropped session.
161
+ """
162
+ if tool in _READ_ONLY_STORE_TOOLS:
163
+ return
164
+ cut_off = gateway_failure(exc)
165
+ if cut_off is None:
166
+ return
167
+ status = cut_off.response.status_code
168
+ msg = (
169
+ f"The deployed store at {url} answered HTTP {status} for `{tool}`: the "
170
+ f"request reached it and was cut off before a reply came back. `{tool}` "
171
+ "writes, so ITS OUTCOME IS INDETERMINATE — it may or may not have been "
172
+ "applied, and nothing in this response says which. This was NOT "
173
+ "retried, because retrying blind writes it twice if it did land. "
174
+ "Re-read the store before deciding; a reindex is safe to re-run."
175
+ )
176
+ raise RemoteWriteIndeterminate(msg) from exc
177
+
128
178
 
129
179
  class _LoopThread:
130
180
  """A private event loop on a daemon thread, driven from synchronous code."""
@@ -189,6 +239,11 @@ class StoreSession:
189
239
  raise RemoteToolFailed(str(exc)) from exc
190
240
  except Exception as exc: # noqa: BLE001 — transport-shaped; retry once
191
241
  _refuse_if_too_large(exc, tool, self.url)
242
+ # BEFORE the reconnect, and that order is the whole point: a
243
+ # gateway cut-off on a write must not be re-invoked, because the
244
+ # backend usually finished it and re-invoking writes the row
245
+ # twice. Only unclassified transport faults reach the retry.
246
+ _refuse_if_indeterminate(exc, tool, self.url)
192
247
  # Info: one reconnect is routine (idle session dropped), but a
193
248
  # deployment that reconnects on every call is pathological and
194
249
  # invisible without this.
@@ -204,6 +259,14 @@ class StoreSession:
204
259
  # arm exists to prevent.
205
260
  _refuse_if_too_large(refused, tool, self.url)
206
261
  raise RemoteToolFailed(str(refused)) from refused
262
+ except Exception as retried: # noqa: BLE001 — same two rules
263
+ # The reconnect landed and the SECOND call was cut off. The
264
+ # write is just as indeterminate as it would have been the
265
+ # first time, so it gets the same sentence rather than
266
+ # escaping as a traceback.
267
+ _refuse_if_too_large(retried, tool, self.url)
268
+ _refuse_if_indeterminate(retried, tool, self.url)
269
+ raise
207
270
 
208
271
  def has_tool(self, tool: str) -> bool:
209
272
  """Whether the deployment serves ``tool``.
File without changes
File without changes
File without changes
File without changes