witan-code 0.10.0__tar.gz → 0.11.0__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.10.0 → witan_code-0.11.0}/PKG-INFO +3 -2
  2. {witan_code-0.10.0 → witan_code-0.11.0}/pyproject.toml +16 -4
  3. {witan_code-0.10.0 → witan_code-0.11.0}/tests/conftest.py +19 -0
  4. witan_code-0.11.0/tests/test_batched_deletes.py +134 -0
  5. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_branches.py +30 -0
  6. witan_code-0.11.0/tests/test_github_app.py +310 -0
  7. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_indexer_scope.py +14 -4
  8. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_ingest.py +6 -2
  9. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_store.py +168 -15
  10. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/bridge.py +15 -4
  11. witan_code-0.11.0/witan_code/github_app.py +296 -0
  12. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/graph.py +8 -1
  13. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/indexer.py +26 -7
  14. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/remote/store.py +27 -0
  15. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/server.py +7 -4
  16. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/store.py +134 -77
  17. {witan_code-0.10.0 → witan_code-0.11.0}/.gitignore +0 -0
  18. {witan_code-0.10.0 → witan_code-0.11.0}/CHANGELOG.md +0 -0
  19. {witan_code-0.10.0 → witan_code-0.11.0}/README.md +0 -0
  20. {witan_code-0.10.0 → witan_code-0.11.0}/config/claude.json +0 -0
  21. {witan_code-0.10.0 → witan_code-0.11.0}/config/copilot.json +0 -0
  22. {witan_code-0.10.0 → witan_code-0.11.0}/config/pi.json +0 -0
  23. {witan_code-0.10.0 → witan_code-0.11.0}/docs/BRANCH_INDEXING.md +0 -0
  24. {witan_code-0.10.0 → witan_code-0.11.0}/docs/CLI_REFERENCE.md +0 -0
  25. {witan_code-0.10.0 → witan_code-0.11.0}/docs/EDGE_PRECISION_TIERS.md +0 -0
  26. {witan_code-0.10.0 → witan_code-0.11.0}/docs/PACKAGE_MAP.md +0 -0
  27. {witan_code-0.10.0 → witan_code-0.11.0}/docs/STAGE2_STITCHING.md +0 -0
  28. {witan_code-0.10.0 → witan_code-0.11.0}/docs/SYMBOL_FORMAT.md +0 -0
  29. {witan_code-0.10.0 → witan_code-0.11.0}/docs/SYMBOL_TABLE.md +0 -0
  30. {witan_code-0.10.0 → witan_code-0.11.0}/docs/USER_GUIDE.md +0 -0
  31. {witan_code-0.10.0 → witan_code-0.11.0}/install.sh +0 -0
  32. {witan_code-0.10.0 → witan_code-0.11.0}/tests/__init__.py +0 -0
  33. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_bridge.py +0 -0
  34. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_cli_dispatch.py +0 -0
  35. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_cli_hooks.py +0 -0
  36. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_cli_output_format.py +0 -0
  37. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_config.py +0 -0
  38. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_config_targets.py +0 -0
  39. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_context.py +0 -0
  40. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_crossrepo.py +0 -0
  41. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_edges.py +0 -0
  42. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_elicit.py +0 -0
  43. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_enrichment.py +0 -0
  44. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_graph.py +0 -0
  45. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_hooks.py +0 -0
  46. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_identity.py +0 -0
  47. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_indexer.py +0 -0
  48. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_languages.py +0 -0
  49. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_maintenance.py +0 -0
  50. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_package_map.py +0 -0
  51. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_reaper.py +0 -0
  52. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_remote_proxy.py +0 -0
  53. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_remote_store.py +0 -0
  54. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_repo.py +0 -0
  55. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_server.py +0 -0
  56. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_setup.py +0 -0
  57. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_stitch.py +0 -0
  58. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_symbol_table.py +0 -0
  59. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_tasks.py +0 -0
  60. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_views.py +0 -0
  61. {witan_code-0.10.0 → witan_code-0.11.0}/tests/test_visualize.py +0 -0
  62. {witan_code-0.10.0 → witan_code-0.11.0}/uv.lock +0 -0
  63. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/__init__.py +0 -0
  64. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/__main__.py +0 -0
  65. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/bridge_extractors.py +0 -0
  66. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/cli.py +0 -0
  67. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/config.py +0 -0
  68. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/context.py +0 -0
  69. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/edges.py +0 -0
  70. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/elicit.py +0 -0
  71. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/extensions/pi/codegraph.ts +0 -0
  72. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/hooks.py +0 -0
  73. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/identity.py +0 -0
  74. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/ingest.py +0 -0
  75. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/maintenance.py +0 -0
  76. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/output.py +0 -0
  77. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/package_map.py +0 -0
  78. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries/bridge.gq +0 -0
  79. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries/code_mutations.gq +0 -0
  80. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries/code_read.gq +0 -0
  81. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries/delete.gq +0 -0
  82. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries_ts/bash.scm +0 -0
  83. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries_ts/hcl.scm +0 -0
  84. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries_ts/python.scm +0 -0
  85. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries_ts/sql.scm +0 -0
  86. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries_ts/typescript.scm +0 -0
  87. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/queries_ts/yaml.scm +0 -0
  88. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/reaper.py +0 -0
  89. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/remote/__init__.py +0 -0
  90. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/remote/oidc.py +0 -0
  91. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/remote/proxy.py +0 -0
  92. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/repo.py +0 -0
  93. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/schema/bridge-schema.pg +0 -0
  94. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/schema/code-schema.pg +0 -0
  95. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/setup.py +0 -0
  96. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/skills/witan-code/SKILL.md +0 -0
  97. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/stitch.py +0 -0
  98. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/views.py +0 -0
  99. {witan_code-0.10.0 → witan_code-0.11.0}/witan_code/visualize.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: witan-code
3
- Version: 0.10.0
3
+ Version: 0.11.0
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
@@ -13,6 +13,7 @@ Requires-Python: >=3.11
13
13
  Requires-Dist: agent-config-kit<1,>=0.4
14
14
  Requires-Dist: cyclopts<5,>=4
15
15
  Requires-Dist: fastmcp<5,>=3.4.2
16
+ Requires-Dist: joserfc<2,>=1.0
16
17
  Requires-Dist: pydantic<3,>=2
17
18
  Requires-Dist: pyyaml<7,>=6
18
19
  Requires-Dist: rich>=13
@@ -25,7 +26,7 @@ Requires-Dist: tree-sitter-sql<0.4,>=0.3
25
26
  Requires-Dist: tree-sitter-typescript<0.24,>=0.23
26
27
  Requires-Dist: tree-sitter-yaml<0.8,>=0.7
27
28
  Requires-Dist: tree-sitter<0.26,>=0.25
28
- Requires-Dist: witan-core[cli,remote]<1,>=0.7
29
+ Requires-Dist: witan-core[cli,remote]<1,>=0.8
29
30
  Provides-Extra: tasks
30
31
  Requires-Dist: fastmcp-tasks<5,>=4.0.0b1; extra == 'tasks'
31
32
  Description-Content-Type: text/markdown
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "witan-code"
3
- version = "0.10.0"
3
+ version = "0.11.0"
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"
@@ -15,12 +15,21 @@ dependencies = [
15
15
  # proxy) behind `witan-code login` and the read commands' remote mode.
16
16
  # Costs nothing extra in practice — fastmcp is already required here and
17
17
  # itself depends on httpx2, the extra's only other dependency.
18
- "witan-core[cli,remote]>=0.7,<1",
18
+ # >=0.8: the batched reindex deletes call OmnigraphClient.change_many with
19
+ # chunk_size, neither of which 0.7 has. A looser floor lets an external
20
+ # install resolve a witan-core that fails on the first reindex.
21
+ "witan-core[cli,remote]>=0.8,<1",
19
22
  "fastmcp>=3.4.2,<5",
20
23
  "cyclopts>=4,<5",
21
24
  # [targets.<name>] override models (config.py) — also transitively pulled
22
25
  # in by fastmcp, declared explicitly since config.py imports it directly.
23
26
  "pydantic>=2,<3",
27
+ # RS256 signing for the GitHub App JWT the CI indexer exchanges for a
28
+ # clone credential (github_app.py). Reaches the environment anyway via
29
+ # fastmcp -> authlib -> joserfc, but that chain is nobody's contract: a
30
+ # fastmcp release that drops authlib would break private-repo cloning at
31
+ # runtime, in a CronJob, rather than at resolve time here.
32
+ "joserfc>=1.0,<2",
24
33
  # Pinned to 0.25.x: tree-sitter 0.26.0 has a use-after-free in its pyo3 Node
25
34
  # binding that segfaults (exit 139) under the heavy .parent()/.children()
26
35
  # churn _walk_defs does on real-world-sized files — deterministically on the
@@ -80,7 +89,10 @@ witan-core = { workspace = true }
80
89
  # fastmcp-tasks: the `tasks` extra. Without it CI would only ever exercise the
81
90
  # extension-absent branch, and `code_reindex`'s task declaration would go
82
91
  # untested — the failure mode there is the server refusing to start at all.
83
- test = ["pytest>=8", "fastmcp-tasks>=4.0.0b1,<5"]
92
+ # cryptography: test_github_app generates a throwaway RSA key so the JWT path
93
+ # is exercised for real rather than mocked. joserfc requires it at runtime, so
94
+ # this only makes an existing import explicit.
95
+ test = ["pytest>=8", "fastmcp-tasks>=4.0.0b1,<5", "cryptography"]
84
96
 
85
97
  [tool.pytest.ini_options]
86
98
  pythonpath = ["."]
@@ -90,7 +102,7 @@ testpaths = ["tests"]
90
102
  packages = ["witan_code"]
91
103
 
92
104
  [tool.bumpversion]
93
- current_version = "0.9.0"
105
+ current_version = "0.11.0"
94
106
  allow_dirty = true
95
107
 
96
108
  [[tool.bumpversion.files]]
@@ -56,6 +56,25 @@ def _fresh_identity():
56
56
  identity.reset_cache()
57
57
 
58
58
 
59
+ @pytest.fixture(autouse=True)
60
+ def _fresh_git_context():
61
+ """Forget the memoized git context between tests.
62
+
63
+ ``server._git_context`` caches ``detect()``/``store_branch()`` for
64
+ ``_GIT_CONTEXT_TTL`` (2s) to amortize git subprocesses across one agent
65
+ turn. Across tests that TTL is a *race*: a test that sets ``WITAN_REPO``
66
+ within 2s of an earlier one gets the earlier test's repo, and its own
67
+ ``monkeypatch.setenv`` is silently ignored. Whether that lands depends on
68
+ how fast the preceding tests ran, which is why it reproduced locally and
69
+ not in CI.
70
+ """
71
+ from witan_code import server
72
+
73
+ server._git_context.clear()
74
+ yield
75
+ server._git_context.clear()
76
+
77
+
59
78
  @pytest.fixture
60
79
  def sample_repo(tmp_path, monkeypatch):
61
80
  """A tiny source tree plus a configured, isolated code store."""
@@ -0,0 +1,134 @@
1
+ """A reindex must clear stale rows in ONE commit, not two per file.
2
+
3
+ The insert side has always been bulk (`client.load`). The DELETE side was not:
4
+ `_delete_file_data` issued two `mutate` calls per changed-or-purged file, so a
5
+ 200-file reindex cost 400 Lance versions — which made a reindex the largest
6
+ fragmentation source in the store the bulk load exists to protect.
7
+ """
8
+
9
+ import subprocess
10
+ from pathlib import Path
11
+
12
+ import pytest
13
+
14
+ from .conftest import requires_stack
15
+
16
+
17
+ @pytest.fixture
18
+ def git_repo(tmp_path, monkeypatch) -> Path:
19
+ """A real git checkout — purging is gated on a confirmed git root, so a
20
+ bare directory would (correctly) never purge and the test would pass
21
+ vacuously."""
22
+ monkeypatch.setenv("WITAN_REPO", "https://github.com/test/cgdel")
23
+ monkeypatch.setenv("WITAN_CODE_DIR", str(tmp_path / "code"))
24
+ src = tmp_path / "repo"
25
+ src.mkdir()
26
+ subprocess.run(["git", "init", "-q", "-b", "main"], cwd=src, check=True)
27
+ (src / "keep.py").write_text("def keep():\n return 1\n")
28
+ return src
29
+
30
+
31
+ @pytest.fixture
32
+ def mutates(monkeypatch):
33
+ """Count `omnigraph mutate` subprocess invocations while in scope."""
34
+ real_run = subprocess.run
35
+ calls: list[list[str]] = []
36
+
37
+ def counting_run(cmd, *args, **kwargs):
38
+ if isinstance(cmd, list) and "mutate" in cmd:
39
+ calls.append(cmd)
40
+ return real_run(cmd, *args, **kwargs)
41
+
42
+ monkeypatch.setattr(subprocess, "run", counting_run)
43
+
44
+ class Counter:
45
+ def reset(self):
46
+ calls.clear()
47
+
48
+ @property
49
+ def count(self):
50
+ return len(calls)
51
+
52
+ return Counter()
53
+
54
+
55
+ @requires_stack
56
+ def test_reindexing_many_changed_files_is_one_delete_commit(
57
+ sample_repo, mutates, tmp_path
58
+ ):
59
+ from witan_code import config as cfg_mod
60
+ from witan_code import indexer
61
+
62
+ cfg = cfg_mod.load()
63
+ for i in range(5):
64
+ (sample_repo / f"mod{i}.py").write_text(f"def f{i}():\n return {i}\n")
65
+ first = indexer.index_path(sample_repo, config=cfg)
66
+ assert first.indexed >= 6
67
+
68
+ # Change every file so all of them are REINDEXED (new files delete nothing).
69
+ for i in range(5):
70
+ (sample_repo / f"mod{i}.py").write_text(f"def f{i}():\n return {i} + 1\n")
71
+ mutates.reset()
72
+ second = indexer.index_path(sample_repo, config=cfg)
73
+ assert second.indexed == 5
74
+ # 5 files x (delete symbols + delete file) = 10 deletes, previously 10
75
+ # commits. Now one, and nothing else on this path mutates.
76
+ assert mutates.count == 1
77
+
78
+
79
+ @requires_stack
80
+ def test_purged_files_share_the_same_delete_commit(git_repo, mutates):
81
+ from witan_code import config as cfg_mod
82
+ from witan_code import indexer
83
+
84
+ cfg = cfg_mod.load()
85
+ for i in range(4):
86
+ (git_repo / f"gone{i}.py").write_text(f"def g{i}():\n return {i}\n")
87
+ indexer.index_path(git_repo, config=cfg)
88
+
89
+ for i in range(4):
90
+ (git_repo / f"gone{i}.py").unlink()
91
+ mutates.reset()
92
+ stats = indexer.index_path(git_repo, config=cfg)
93
+ # purged files feed the SAME delete_steps list as reindexed ones, so the
94
+ # whole purge is one commit rather than two per file
95
+ assert stats.purged == 4
96
+ assert mutates.count == 1
97
+
98
+
99
+ @requires_stack
100
+ def test_a_clean_reindex_writes_no_delete_commit(sample_repo, mutates):
101
+ from witan_code import config as cfg_mod
102
+ from witan_code import indexer
103
+
104
+ cfg = cfg_mod.load()
105
+ indexer.index_path(sample_repo, config=cfg)
106
+ mutates.reset()
107
+ stats = indexer.index_path(sample_repo, config=cfg)
108
+ # nothing changed: no deletes to issue, so no mutate at all
109
+ assert stats.indexed == 0
110
+ assert mutates.count == 0
111
+
112
+
113
+ @requires_stack
114
+ def test_deletes_are_chunked_so_the_composed_query_stays_bounded(
115
+ sample_repo, mutates, monkeypatch
116
+ ):
117
+ from witan_code import config as cfg_mod
118
+ from witan_code import indexer
119
+
120
+ # 2 delete statements per file; a cap of 4 means 2 files per commit.
121
+ monkeypatch.setattr(indexer, "_DELETE_BATCH_SIZE", 4)
122
+ cfg = cfg_mod.load()
123
+ for i in range(5):
124
+ (sample_repo / f"chunk{i}.py").write_text(f"def c{i}():\n return {i}\n")
125
+ indexer.index_path(sample_repo, config=cfg)
126
+
127
+ for i in range(5):
128
+ (sample_repo / f"chunk{i}.py").write_text(f"def c{i}():\n return {i} + 1\n")
129
+ mutates.reset()
130
+ stats = indexer.index_path(sample_repo, config=cfg)
131
+ assert stats.indexed == 5
132
+ # 5 changed files + svc.py is unchanged -> 10 delete statements at 4 per
133
+ # commit = 3 commits (4 + 4 + 2)
134
+ assert mutates.count == 3
@@ -9,6 +9,7 @@ from witan_code import repo as repo_module
9
9
  from .conftest import SAMPLE, requires_stack
10
10
 
11
11
  REPO = "https://github.com/test/cg"
12
+ LEAKED_REPO = "https://github.com/test/leaked-git-context"
12
13
 
13
14
 
14
15
  def _git(base, *args):
@@ -294,6 +295,35 @@ def test_cached_git_refreshes_after_ttl(monkeypatch):
294
295
  assert srv._cached_detect() == "b"
295
296
 
296
297
 
298
+ # These two run as a pair, in order: the first leaves residue the second must
299
+ # not see. Without the autouse ``_fresh_git_context`` fixture the second fails,
300
+ # because the 2s TTL has not expired between two adjacent tests. That is the
301
+ # exact mechanism that made test_crossrepo's fan-out collapse to one repo.
302
+ def test_git_context_survives_within_a_test(monkeypatch):
303
+ # Seed from a known-clean state rather than inheriting whatever the
304
+ # preceding test memoized: test_cached_git_refreshes_after_ttl leaves a
305
+ # fresh "b" behind, which within the 2s TTL would make _cached_detect()
306
+ # below return "b" and fail this test for the wrong reason. That matters
307
+ # precisely when the autouse fixture is toggled off to check that the pair
308
+ # still detects its removal — the residue the next test sees must
309
+ # provably be LEAKED_REPO, set here, and not an unrelated predecessor's.
310
+ from witan_code import server as srv
311
+
312
+ srv._git_context.clear()
313
+ monkeypatch.setattr(srv.repo_module, "detect", lambda: LEAKED_REPO)
314
+ assert srv._cached_detect() == LEAKED_REPO
315
+ assert srv._git_context["detect"][1] == LEAKED_REPO
316
+
317
+
318
+ def test_git_context_does_not_leak_into_the_next_test():
319
+ """The previous test's memoized repo must not be visible here."""
320
+ from witan_code import server as srv
321
+
322
+ assert srv._git_context == {}, (
323
+ "a previous test's git context leaked; _fresh_git_context must clear it"
324
+ )
325
+
326
+
297
327
  @requires_stack
298
328
  def test_bridge_client_follows_current_checkout_branch(tmp_path, monkeypatch):
299
329
  """code_interface_providers auto-detects the cwd's repo+branch: sees the
@@ -0,0 +1,310 @@
1
+ """Tests for witan_code.github_app — App JWT minting and the token exchange.
2
+
3
+ The token exchange runs against an ``httpx2.MockTransport`` (same approach as
4
+ ``packages/witan-core/tests/test_remote_oidc.py``); the JWT is signed with a
5
+ throwaway RSA key generated per session and verified by decoding it back, so
6
+ these cover the real crypto path without needing a real App.
7
+ """
8
+
9
+ import time
10
+
11
+ import httpx2
12
+ import pytest
13
+ from cryptography.hazmat.primitives import serialization
14
+ from cryptography.hazmat.primitives.asymmetric import rsa
15
+ from joserfc import jwt
16
+ from joserfc.jwk import RSAKey
17
+ from witan_code.github_app import (
18
+ API_URL_ENV_VAR,
19
+ APP_ID_ENV_VAR,
20
+ EXIT_ERROR,
21
+ EXIT_NOT_CONFIGURED,
22
+ EXIT_OK,
23
+ GITHUB_API_URL,
24
+ INSTALLATION_ID_ENV_VAR,
25
+ KEY_FILE_ENV_VAR,
26
+ AppCredentials,
27
+ GitHubAppError,
28
+ app_jwt,
29
+ from_env,
30
+ installation_token,
31
+ main,
32
+ )
33
+
34
+ _JWT_MAX_LIFETIME = 600 # GitHub's hard limit on an App JWT.
35
+
36
+
37
+ @pytest.fixture(scope="session")
38
+ def pem() -> str:
39
+ key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
40
+ return key.private_bytes(
41
+ encoding=serialization.Encoding.PEM,
42
+ format=serialization.PrivateFormat.PKCS8,
43
+ encryption_algorithm=serialization.NoEncryption(),
44
+ ).decode()
45
+
46
+
47
+ @pytest.fixture
48
+ def credentials(pem: str) -> AppCredentials:
49
+ return AppCredentials(app_id="123456", installation_id="789", private_key=pem)
50
+
51
+
52
+ def _client(handler) -> httpx2.Client:
53
+ return httpx2.Client(transport=httpx2.MockTransport(handler))
54
+
55
+
56
+ # ── app_jwt ───────────────────────────────────────────────────────────────────
57
+
58
+
59
+ def test_app_jwt_claims_are_what_github_requires(credentials, pem):
60
+ now = 1_700_000_000
61
+ decoded = jwt.decode(app_jwt(credentials, now=now), RSAKey.import_key(pem))
62
+
63
+ assert decoded.header["alg"] == "RS256"
64
+ assert decoded.claims["iss"] == "123456"
65
+ # Backdated against clock skew: GitHub rejects a JWT issued in the future.
66
+ assert decoded.claims["iat"] < now
67
+ # And inside GitHub's 10-minute ceiling, measured from the backdated iat
68
+ # rather than from now — that is the span GitHub actually validates.
69
+ assert decoded.claims["exp"] - decoded.claims["iat"] <= _JWT_MAX_LIFETIME
70
+
71
+
72
+ def test_app_jwt_defaults_to_the_current_time(credentials, pem):
73
+ before = int(time.time())
74
+ decoded = jwt.decode(app_jwt(credentials), RSAKey.import_key(pem))
75
+ assert decoded.claims["exp"] > before
76
+
77
+
78
+ def test_app_jwt_rejects_a_key_that_is_not_a_pem(credentials):
79
+ broken = AppCredentials(
80
+ app_id="1", installation_id="2", private_key="-----BEGIN NOPE-----"
81
+ )
82
+ with pytest.raises(GitHubAppError, match="not a usable RSA PEM"):
83
+ app_jwt(broken)
84
+
85
+
86
+ # ── installation_token ────────────────────────────────────────────────────────
87
+
88
+
89
+ def test_installation_token_posts_to_the_installation_and_returns_the_token(
90
+ credentials, pem
91
+ ):
92
+ seen = {}
93
+
94
+ def handler(request: httpx2.Request) -> httpx2.Response:
95
+ seen["url"] = str(request.url)
96
+ seen["auth"] = request.headers["Authorization"]
97
+ seen["accept"] = request.headers["Accept"]
98
+ seen["method"] = request.method
99
+ return httpx2.Response(201, json={"token": "ghs_secret", "expires_at": "x"})
100
+
101
+ token = installation_token(credentials, client=_client(handler))
102
+
103
+ assert token == "ghs_secret"
104
+ assert seen["method"] == "POST"
105
+ assert seen["url"] == ("https://api.github.com/app/installations/789/access_tokens")
106
+ assert seen["accept"] == "application/vnd.github+json"
107
+ # The bearer is the App JWT, and it must verify against the App's own key.
108
+ presented = seen["auth"].removeprefix("Bearer ")
109
+ assert jwt.decode(presented, RSAKey.import_key(pem)).claims["iss"] == "123456"
110
+
111
+
112
+ def test_installation_token_raises_on_a_refusal(credentials):
113
+ def handler(_: httpx2.Request) -> httpx2.Response:
114
+ return httpx2.Response(404, json={"message": "Not Found"})
115
+
116
+ with pytest.raises(GitHubAppError, match="HTTP 404"):
117
+ installation_token(credentials, client=_client(handler))
118
+
119
+
120
+ def test_installation_token_raises_when_the_body_has_no_token(credentials):
121
+ def handler(_: httpx2.Request) -> httpx2.Response:
122
+ return httpx2.Response(201, json={"expires_at": "x"})
123
+
124
+ with pytest.raises(GitHubAppError, match="no token"):
125
+ installation_token(credentials, client=_client(handler))
126
+
127
+
128
+ def test_installation_token_raises_on_an_empty_token(credentials):
129
+ """An empty string would otherwise be exported as a credential and 401."""
130
+
131
+ def handler(_: httpx2.Request) -> httpx2.Response:
132
+ return httpx2.Response(201, json={"token": ""})
133
+
134
+ with pytest.raises(GitHubAppError, match="empty token"):
135
+ installation_token(credentials, client=_client(handler))
136
+
137
+
138
+ def test_installation_token_raises_when_github_is_unreachable(credentials):
139
+ def handler(_: httpx2.Request) -> httpx2.Response:
140
+ raise httpx2.ConnectError("no route")
141
+
142
+ with pytest.raises(GitHubAppError, match="Could not reach"):
143
+ installation_token(credentials, client=_client(handler))
144
+
145
+
146
+ def test_installation_token_leaves_a_caller_supplied_client_open(credentials):
147
+ """A client the caller owns is theirs to close, even on the error path."""
148
+
149
+ def handler(_: httpx2.Request) -> httpx2.Response:
150
+ return httpx2.Response(201, json={"token": "ghs_secret"})
151
+
152
+ client = _client(handler)
153
+ installation_token(credentials, client=client)
154
+ assert not client.is_closed
155
+
156
+
157
+ def test_installation_token_closes_a_client_it_created(credentials, monkeypatch):
158
+ """Otherwise a long-lived caller accumulates connections per call."""
159
+ created: list[httpx2.Client] = []
160
+ real_client = httpx2.Client
161
+
162
+ def spy(*args, **kwargs):
163
+ client = real_client(
164
+ *args,
165
+ **kwargs,
166
+ transport=httpx2.MockTransport(
167
+ lambda _: httpx2.Response(201, json={"token": "ghs_secret"})
168
+ ),
169
+ )
170
+ created.append(client)
171
+ return client
172
+
173
+ monkeypatch.setattr(httpx2, "Client", spy)
174
+ installation_token(credentials)
175
+
176
+ assert len(created) == 1
177
+ assert created[0].is_closed
178
+
179
+
180
+ # ── from_env ──────────────────────────────────────────────────────────────────
181
+
182
+
183
+ def _env(tmp_path, pem, **overrides) -> dict[str, str]:
184
+ key_file = tmp_path / "app.pem"
185
+ key_file.write_text(pem)
186
+ env = {
187
+ APP_ID_ENV_VAR: "123456",
188
+ INSTALLATION_ID_ENV_VAR: "789",
189
+ KEY_FILE_ENV_VAR: str(key_file),
190
+ }
191
+ env.update(overrides)
192
+ return {k: v for k, v in env.items() if v is not None}
193
+
194
+
195
+ def test_from_env_none_when_nothing_is_configured():
196
+ assert from_env({}) is None
197
+
198
+
199
+ def test_from_env_reads_the_key_off_disk(tmp_path, pem):
200
+ creds = from_env(_env(tmp_path, pem))
201
+ assert creds is not None
202
+ assert creds.app_id == "123456"
203
+ assert creds.installation_id == "789"
204
+ assert creds.private_key == pem
205
+
206
+
207
+ @pytest.mark.parametrize(
208
+ "missing", [APP_ID_ENV_VAR, INSTALLATION_ID_ENV_VAR, KEY_FILE_ENV_VAR]
209
+ )
210
+ def test_from_env_rejects_a_partial_configuration(tmp_path, pem, missing):
211
+ """Half-configured must not read as unconfigured — see the module docstring."""
212
+ with pytest.raises(GitHubAppError, match="Incomplete GitHub App configuration"):
213
+ from_env(_env(tmp_path, pem, **{missing: None}))
214
+
215
+
216
+ def test_from_env_rejects_an_empty_key_file(tmp_path, pem):
217
+ """What an unfulfilled Vault-synced Secret looks like."""
218
+ env = _env(tmp_path, pem)
219
+ (tmp_path / "app.pem").write_text("")
220
+ with pytest.raises(GitHubAppError, match="is empty"):
221
+ from_env(env)
222
+
223
+
224
+ def test_from_env_rejects_a_missing_key_file(tmp_path, pem):
225
+ with pytest.raises(GitHubAppError, match="Cannot read"):
226
+ from_env(_env(tmp_path, pem, **{KEY_FILE_ENV_VAR: str(tmp_path / "nope.pem")}))
227
+
228
+
229
+ def test_from_env_rejects_a_non_utf8_key_file(tmp_path, pem):
230
+ """A truncated or binary mount, which a PEM never is — same clear error."""
231
+ env = _env(tmp_path, pem)
232
+ (tmp_path / "app.pem").write_bytes(b"\xff\xfe not a pem \x00")
233
+ with pytest.raises(GitHubAppError, match="Cannot read"):
234
+ from_env(env)
235
+
236
+
237
+ def test_credentials_repr_does_not_leak_the_key(credentials):
238
+ """These land in tracebacks; the PEM must not ride along."""
239
+ assert "BEGIN" not in repr(credentials)
240
+ assert "<redacted>" in repr(credentials)
241
+
242
+
243
+ # ── main / exit codes ─────────────────────────────────────────────────────────
244
+
245
+
246
+ def test_main_check_reports_not_configured(monkeypatch):
247
+ for name in (APP_ID_ENV_VAR, INSTALLATION_ID_ENV_VAR, KEY_FILE_ENV_VAR):
248
+ monkeypatch.delenv(name, raising=False)
249
+ assert main(["--check"]) == EXIT_NOT_CONFIGURED
250
+
251
+
252
+ def test_main_check_succeeds_without_minting(tmp_path, pem, monkeypatch):
253
+ for name, value in _env(tmp_path, pem).items():
254
+ monkeypatch.setenv(name, value)
255
+ # No transport is patched, so a token exchange here would fail outright —
256
+ # which is the assertion: --check must not make one.
257
+ assert main(["--check"]) == EXIT_OK
258
+
259
+
260
+ def test_main_check_errors_on_a_partial_configuration(tmp_path, pem, monkeypatch):
261
+ monkeypatch.delenv(INSTALLATION_ID_ENV_VAR, raising=False)
262
+ monkeypatch.setenv(APP_ID_ENV_VAR, "123456")
263
+ monkeypatch.setenv(KEY_FILE_ENV_VAR, str(tmp_path / "app.pem"))
264
+ (tmp_path / "app.pem").write_text(pem)
265
+ assert main(["--check"]) == EXIT_ERROR
266
+
267
+
268
+ def test_main_prints_the_token(tmp_path, pem, monkeypatch, capsys):
269
+ for name, value in _env(tmp_path, pem).items():
270
+ monkeypatch.setenv(name, value)
271
+ monkeypatch.delenv(API_URL_ENV_VAR, raising=False)
272
+ seen = {}
273
+
274
+ def fake(credentials, *, api_url):
275
+ seen["api_url"] = api_url
276
+ return "ghs_printed"
277
+
278
+ monkeypatch.setattr("witan_code.github_app.installation_token", fake)
279
+ assert main([]) == EXIT_OK
280
+ assert capsys.readouterr().out.strip() == "ghs_printed"
281
+ assert seen["api_url"] == GITHUB_API_URL
282
+
283
+
284
+ def test_main_honours_an_api_url_override(tmp_path, pem, monkeypatch, capsys):
285
+ """What lets the entrypoint's App path be exercised against a stub API."""
286
+ for name, value in _env(tmp_path, pem).items():
287
+ monkeypatch.setenv(name, value)
288
+ monkeypatch.setenv(API_URL_ENV_VAR, "http://127.0.0.1:9999")
289
+ monkeypatch.setattr(
290
+ "witan_code.github_app.installation_token",
291
+ lambda _creds, *, api_url: f"ghs_{api_url}",
292
+ )
293
+ assert main([]) == EXIT_OK
294
+ assert capsys.readouterr().out.strip() == "ghs_http://127.0.0.1:9999"
295
+
296
+
297
+ def test_main_reports_a_mint_failure(tmp_path, pem, monkeypatch, capsys):
298
+ for name, value in _env(tmp_path, pem).items():
299
+ monkeypatch.setenv(name, value)
300
+
301
+ def boom(_creds, *, api_url):
302
+ raise GitHubAppError("GitHub said no")
303
+
304
+ monkeypatch.setattr("witan_code.github_app.installation_token", boom)
305
+ assert main([]) == EXIT_ERROR
306
+ assert "GitHub said no" in capsys.readouterr().err
307
+
308
+
309
+ def test_main_rejects_an_unknown_argument():
310
+ assert main(["--nope"]) == EXIT_ERROR
@@ -392,9 +392,11 @@ def test_index_path_refuses_a_shared_default_branch_view(tmp_path, monkeypatch):
392
392
  def _cluster_cfg(monkeypatch, repo: str, **env: str):
393
393
  """Config addressing a (stubbed) cluster that has ``repo``'s graph declared.
394
394
 
395
- ``graphs list`` is the one thing a client genuinely cannot do without a
396
- server; everything downstream of it — the graph id, the ``--server/--graph``
397
- split, the write guard — is exercised for real.
395
+ Reaching the server is the one thing a client genuinely cannot do without
396
+ one, so only the probe's single round trip is stubbed — on the real client
397
+ class, so everything downstream of it (the graph id, the
398
+ ``--server/--graph`` split, the token, the write guard) is exercised for
399
+ real.
398
400
  """
399
401
  from witan_code import config as cfg_module
400
402
  from witan_code import store as store_mod
@@ -403,7 +405,15 @@ def _cluster_cfg(monkeypatch, repo: str, **env: str):
403
405
  for key, value in env.items():
404
406
  monkeypatch.setenv(key, value)
405
407
  declared = frozenset({cfg_module.graph_id(repo), cfg_module.BRIDGE_GRAPH_ID})
406
- monkeypatch.setattr(store_mod, "cluster_graphs", lambda *a, **kw: declared)
408
+
409
+ def _list_branches(self):
410
+ if self.graph_id not in declared:
411
+ # The server's own words for a graph it does not serve — the string
412
+ # `probe_cluster_graph` keys "not provisioned" off of.
413
+ raise RuntimeError(f"graph '{self.graph_id}' not found")
414
+ return ["main"]
415
+
416
+ monkeypatch.setattr(store_mod.OmnigraphClient, "list_branches", _list_branches)
407
417
  return cfg_module.load()
408
418
 
409
419
 
@@ -209,8 +209,12 @@ def test_a_graph_the_cluster_does_not_declare_fails_on_the_first_call(monkeypatc
209
209
  from witan_code import store as store_module
210
210
 
211
211
  monkeypatch.setenv("WITAN_CODE_SERVER", "https://omnigraph.example.org")
212
- monkeypatch.setattr(store_module, "cluster_graphs", lambda *_a, **_kw: frozenset())
213
- with pytest.raises(store_module.ClusterGraphMissing, match="not registered"):
212
+
213
+ def _not_served(self):
214
+ raise RuntimeError(f"graph '{self.graph_id}' not found")
215
+
216
+ monkeypatch.setattr(store_module.OmnigraphClient, "list_branches", _not_served)
217
+ with pytest.raises(store_module.ClusterGraphMissing, match="not served"):
214
218
  ingest.read(
215
219
  "https://github.com/test/never-provisioned",
216
220
  None,