witan-code 0.4.0__tar.gz → 0.7.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 (81) hide show
  1. witan_code-0.7.0/CHANGELOG.md +126 -0
  2. {witan_code-0.4.0 → witan_code-0.7.0}/PKG-INFO +29 -3
  3. {witan_code-0.4.0 → witan_code-0.7.0}/README.md +23 -0
  4. {witan_code-0.4.0 → witan_code-0.7.0}/pyproject.toml +21 -7
  5. witan_code-0.7.0/tests/test_config.py +64 -0
  6. witan_code-0.7.0/tests/test_config_targets.py +247 -0
  7. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_repo.py +36 -0
  8. witan_code-0.7.0/tests/test_tasks.py +104 -0
  9. witan_code-0.7.0/witan_code/config.py +214 -0
  10. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/elicit.py +7 -3
  11. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/graph.py +2 -1
  12. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/repo.py +11 -5
  13. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/schema/bridge-schema.pg +6 -2
  14. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/schema/code-schema.pg +6 -1
  15. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/server.py +41 -3
  16. witan_code-0.4.0/CHANGELOG.md +0 -60
  17. witan_code-0.4.0/witan_code/config.py +0 -65
  18. {witan_code-0.4.0 → witan_code-0.7.0}/.gitignore +0 -0
  19. {witan_code-0.4.0 → witan_code-0.7.0}/config/claude.json +0 -0
  20. {witan_code-0.4.0 → witan_code-0.7.0}/config/copilot.json +0 -0
  21. {witan_code-0.4.0 → witan_code-0.7.0}/config/pi.json +0 -0
  22. {witan_code-0.4.0 → witan_code-0.7.0}/docs/BRANCH_INDEXING.md +0 -0
  23. {witan_code-0.4.0 → witan_code-0.7.0}/docs/CLI_REFERENCE.md +0 -0
  24. {witan_code-0.4.0 → witan_code-0.7.0}/docs/EDGE_PRECISION_TIERS.md +0 -0
  25. {witan_code-0.4.0 → witan_code-0.7.0}/docs/PACKAGE_MAP.md +0 -0
  26. {witan_code-0.4.0 → witan_code-0.7.0}/docs/STAGE2_STITCHING.md +0 -0
  27. {witan_code-0.4.0 → witan_code-0.7.0}/docs/SYMBOL_FORMAT.md +0 -0
  28. {witan_code-0.4.0 → witan_code-0.7.0}/docs/SYMBOL_TABLE.md +0 -0
  29. {witan_code-0.4.0 → witan_code-0.7.0}/docs/USER_GUIDE.md +0 -0
  30. {witan_code-0.4.0 → witan_code-0.7.0}/install.sh +0 -0
  31. {witan_code-0.4.0 → witan_code-0.7.0}/tests/__init__.py +0 -0
  32. {witan_code-0.4.0 → witan_code-0.7.0}/tests/conftest.py +0 -0
  33. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_branches.py +0 -0
  34. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_bridge.py +0 -0
  35. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_cli_hooks.py +0 -0
  36. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_cli_output_format.py +0 -0
  37. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_context.py +0 -0
  38. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_crossrepo.py +0 -0
  39. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_edges.py +0 -0
  40. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_elicit.py +0 -0
  41. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_enrichment.py +0 -0
  42. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_graph.py +0 -0
  43. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_hooks.py +0 -0
  44. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_indexer.py +0 -0
  45. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_languages.py +0 -0
  46. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_maintenance.py +0 -0
  47. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_package_map.py +0 -0
  48. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_server.py +0 -0
  49. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_setup.py +0 -0
  50. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_stitch.py +0 -0
  51. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_symbol_table.py +0 -0
  52. {witan_code-0.4.0 → witan_code-0.7.0}/tests/test_visualize.py +0 -0
  53. {witan_code-0.4.0 → witan_code-0.7.0}/uv.lock +0 -0
  54. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/__init__.py +0 -0
  55. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/__main__.py +0 -0
  56. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/bridge.py +0 -0
  57. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/bridge_extractors.py +0 -0
  58. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/cli.py +0 -0
  59. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/context.py +0 -0
  60. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/edges.py +0 -0
  61. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/extensions/pi/codegraph.ts +0 -0
  62. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/hooks.py +0 -0
  63. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/indexer.py +0 -0
  64. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/maintenance.py +0 -0
  65. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/output.py +0 -0
  66. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/package_map.py +0 -0
  67. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries/bridge.gq +0 -0
  68. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries/code_mutations.gq +0 -0
  69. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries/code_read.gq +0 -0
  70. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries/delete.gq +0 -0
  71. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries_ts/bash.scm +0 -0
  72. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries_ts/hcl.scm +0 -0
  73. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries_ts/python.scm +0 -0
  74. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries_ts/sql.scm +0 -0
  75. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries_ts/typescript.scm +0 -0
  76. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/queries_ts/yaml.scm +0 -0
  77. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/setup.py +0 -0
  78. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/skills/witan-code/SKILL.md +0 -0
  79. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/stitch.py +0 -0
  80. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/store.py +0 -0
  81. {witan_code-0.4.0 → witan_code-0.7.0}/witan_code/visualize.py +0 -0
@@ -0,0 +1,126 @@
1
+ # Changelog
2
+
3
+ All notable changes to `witan-code` are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/) (pre-1.0:
7
+ a MINOR bump may include breaking changes).
8
+
9
+ ## [0.7.0] - 2026-07-30
10
+
11
+ ### Changed
12
+
13
+ - **Requires `witan-core>=0.5`** (was `>=0.2`) — the server now imports
14
+ `witan_core.caching` and `elicit.MRTRElicitationMiddleware`, neither of which
15
+ exists below that.
16
+ - **`code_reindex` is now async and no longer blocks the event loop.** It was a
17
+ plain `def`, so a multi-minute index stalled every other request on the server
18
+ for its whole run; the indexer now runs on a thread.
19
+ - **`tools/list` declares a cache directive** — `ttlMs=300000`,
20
+ `cacheScope=private`.
21
+ - **Elicitation works on the stateless protocol era again** — the index-now
22
+ confirms and the repo prompt had been silently returning their
23
+ non-interactive defaults on any MCP 2026-07-28 connection. See the
24
+ `witan-core` 0.5.0 entry.
25
+
26
+ ### Added
27
+
28
+ - **`code_reindex` accepts task-augmented execution** (`io.modelcontextprotocol/tasks`,
29
+ SEP-2663) via the new optional `witan-code[tasks]` extra: a client can take a
30
+ handle and poll rather than holding the tool call open for the whole rebuild.
31
+ Opt-in per call — a client that does not ask gets the same completed result as
32
+ before. Without the extra the tool stays synchronous; the extra is separate
33
+ because it pulls a Docket/Redis stack most installs never need, and because it
34
+ requires FastMCP 4 while this package still supports 3.4.x. Note that while
35
+ 4.0 is still a pre-release, `witan-code[tasks]` needs `--prerelease=allow`
36
+ under uv; the base package does not.
37
+
38
+ ## [0.6.0] - 2026-07-21
39
+
40
+ ### Fixed
41
+
42
+ - **`repo.detect(override=...)` and the `WITAN_REPO` env var now route
43
+ through `normalise`** (issue #142), same as an auto-detected git remote —
44
+ matching the fix in witan-council 0.6.0. Previously an explicitly-passed
45
+ `repo=` (or `WITAN_REPO`) was stored verbatim, bypassing
46
+ canonicalization. `graph_id` (the shared-cluster graph id) already
47
+ case-folded unconditionally and needs no change; a repo whose canonical
48
+ key changes case after this fix should be re-indexed (`witan-code
49
+ reindex`) so its per-repo store lands under the new key.
50
+ - Depends on `witan-core[cli]>=0.2,<1` (unchanged range; picks up 0.4.0's
51
+ repo-key case-fold in `normalise`, which this package's `repo.detect`
52
+ relies on).
53
+
54
+ ## [0.5.0] - 2026-07-20
55
+
56
+ ### Added
57
+
58
+ - `config.toml` + `[targets.<name>]` support (previously env-var only):
59
+ `WITAN_CONFIG` / `~/.config/witan/config.toml`, a global `code_dir`/
60
+ `author`, and named targets overriding `code_dir`/`author`, selected by
61
+ `WITAN_TARGET` env var, an explicit `load(target=...)`, or auto-detection
62
+ via `match_paths`/`match_repos`/`match_hosts`/`match_orgs` (same
63
+ precedence and file as witan — see `witan_core.target_config` and
64
+ `witan.config.load()`'s docstring). A target can carry witan's
65
+ `server`/`graph`/`token` alongside this server's `code_dir` under one
66
+ name; witan-code reads only the fields it knows. `Config` gained
67
+ `target_name`. `load()` now optionally takes a `target` argument (was
68
+ zero-argument only). New dependency: `pydantic>=2,<3`.
69
+
70
+ ### Changed
71
+
72
+ - Now depends on `witan-core[cli]>=0.2,<1` (unchanged range; picks up
73
+ 0.3.0, which adds the shared target-routing logic this release uses).
74
+
75
+ ## [0.4.0] - 2026-07-16
76
+
77
+ ### Added
78
+
79
+ - **`witan-code --output-format`**: `repos`, `symbols`, and `stitch` can now
80
+ render their table data as `json`, `toml`, or `yaml` in addition to the
81
+ default Rich table output. The same option is honored when mounted as
82
+ `witan --output-format … code …`.
83
+
84
+ ### Changed
85
+
86
+ - Adopted the shared `witan-core` package for the CLI scaffolding
87
+ (`make_app`/`report_install`/`resolve_author` + agent-name constants),
88
+ replacing the previously duplicated copies. No user-facing behavior change.
89
+ Now depends on `witan-core[cli]>=0.2,<1`.
90
+
91
+ ## [0.3.0] - 2026-07-10
92
+
93
+ ### Added
94
+
95
+ - **`witan-code --version`**: prints the installed package version,
96
+ appending a git short ref when installed editable (local workspace) or
97
+ directly from a VCS URL (`uvx --from git+...`), via the new
98
+ `agent_config_kit.resolve_version()` helper.
99
+
100
+ ### Changed
101
+
102
+ - `agent-config-kit` dependency bumped to `>=0.4,<1` (was `>=0.1,<1`) to
103
+ require the version that ships `resolve_version`, which backs the new
104
+ `--version` flag.
105
+
106
+ ## [0.2.1] - 2026-07-09
107
+
108
+ ### Fixed
109
+
110
+ - **Pinned `tree-sitter` to `0.25.x`**: `0.26.0` has a use-after-free in its
111
+ pyo3 `Node` binding that segfaults (exit 139) under the heavy
112
+ `.parent()`/`.children()` churn `_walk_defs` does on real-world-sized
113
+ files — deterministically on the 2nd-3rd parse in one process, so
114
+ `witan-code index` hard-crashed on any real repo. `0.25.0`-`0.25.2` are
115
+ crash-free with the same ABI-15 grammar wheels.
116
+
117
+ ## [0.2.0] - 2026-07-09
118
+
119
+ Initial PyPI release. `witan-code` is a tree-sitter-based code graph MCP
120
+ server (Layer 2): indexes a repo's symbols (functions, methods, classes,
121
+ modules) and their relationships, then exposes `code_search_symbol` /
122
+ `code_find_definition` / `code_find_references` / `code_callers` /
123
+ `code_impact` queries to the agent. Includes the Layer 2.5 cross-repo bridge,
124
+ linking SOA repos by shared `env_var`/`endpoint`/`package`/`service` contract
125
+ keys. Mounts standalone as `witan-code` or under `witan code …` when the
126
+ `witan` umbrella CLI is installed alongside it.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: witan-code
3
- Version: 0.4.0
3
+ Version: 0.7.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
@@ -12,7 +12,8 @@ Classifier: Programming Language :: Python :: 3
12
12
  Requires-Python: >=3.11
13
13
  Requires-Dist: agent-config-kit<1,>=0.4
14
14
  Requires-Dist: cyclopts<5,>=4
15
- Requires-Dist: fastmcp<4,>=3.4.2
15
+ Requires-Dist: fastmcp<5,>=3.4.2
16
+ Requires-Dist: pydantic<3,>=2
16
17
  Requires-Dist: pyyaml<7,>=6
17
18
  Requires-Dist: rich>=13
18
19
  Requires-Dist: tomli-w<2,>=1
@@ -24,7 +25,9 @@ Requires-Dist: tree-sitter-sql<0.4,>=0.3
24
25
  Requires-Dist: tree-sitter-typescript<0.24,>=0.23
25
26
  Requires-Dist: tree-sitter-yaml<0.8,>=0.7
26
27
  Requires-Dist: tree-sitter<0.26,>=0.25
27
- Requires-Dist: witan-core[cli]<1,>=0.2
28
+ Requires-Dist: witan-core[cli]<1,>=0.5
29
+ Provides-Extra: tasks
30
+ Requires-Dist: fastmcp-tasks<5,>=4.0.0b1; extra == 'tasks'
28
31
  Description-Content-Type: text/markdown
29
32
 
30
33
  # witan-code
@@ -334,12 +337,35 @@ symlink alternative:
334
337
  | `WITAN_AUTHOR` / `USER` | `unknown` | attribution string |
335
338
  | `WITAN_REPO` | — | override the detected repo slug |
336
339
  | `WITAN_CODE_OPTIMIZE_INTERVAL` | `86400` (daily) | throttle window (seconds) for `checkpoint`'s opportunistic store compaction; `0` disables it |
340
+ | `WITAN_CONFIG` | `~/.config/witan/config.toml` | config file path (see below) |
341
+ | `WITAN_TARGET` | — | force a named `[targets.<name>]` block instead of auto-detecting one |
337
342
 
338
343
  The store URI for a repo is `<dir>/<sanitized-slug>.omni`, where the slug's `/`
339
344
  and `:` are replaced with `_`. The shared cross-repo bridge lives alongside them
340
345
  at `<dir>/_bridge.omni` and is created lazily on the first index that yields any
341
346
  bindings.
342
347
 
348
+ ### config.toml
349
+
350
+ witan-code reads the same `config.toml` as witan (witan-council) — a global
351
+ `code_dir`/`author`, plus named `[targets.<name>]` blocks that override them
352
+ and are selected by `WITAN_TARGET`, `load(target=...)` (Python API only — no
353
+ CLI `--target` flag yet), or auto-detection against the current
354
+ repo/checkout (`match_paths` > `match_repos` > `match_hosts` > `match_orgs`
355
+ — see witan's README/`witan/config.py` docstring for the full precedence).
356
+ Because the file is shared, one target block can carry witan's
357
+ `server`/`graph`/`token` alongside `code_dir` under
358
+ the same name — each server reads only the fields it knows:
359
+
360
+ ```toml
361
+ [targets.work]
362
+ server = "http://witan.internal:8080" # witan (witan-council)
363
+ code_dir = "/mnt/work/witan-code" # witan-code
364
+ match_orgs = ["myorg"]
365
+ ```
366
+
367
+ Environment variables always win over `config.toml`.
368
+
343
369
  ## MCP tools
344
370
 
345
371
  Each tool resolves the per-repo store from the current working directory and
@@ -305,12 +305,35 @@ symlink alternative:
305
305
  | `WITAN_AUTHOR` / `USER` | `unknown` | attribution string |
306
306
  | `WITAN_REPO` | — | override the detected repo slug |
307
307
  | `WITAN_CODE_OPTIMIZE_INTERVAL` | `86400` (daily) | throttle window (seconds) for `checkpoint`'s opportunistic store compaction; `0` disables it |
308
+ | `WITAN_CONFIG` | `~/.config/witan/config.toml` | config file path (see below) |
309
+ | `WITAN_TARGET` | — | force a named `[targets.<name>]` block instead of auto-detecting one |
308
310
 
309
311
  The store URI for a repo is `<dir>/<sanitized-slug>.omni`, where the slug's `/`
310
312
  and `:` are replaced with `_`. The shared cross-repo bridge lives alongside them
311
313
  at `<dir>/_bridge.omni` and is created lazily on the first index that yields any
312
314
  bindings.
313
315
 
316
+ ### config.toml
317
+
318
+ witan-code reads the same `config.toml` as witan (witan-council) — a global
319
+ `code_dir`/`author`, plus named `[targets.<name>]` blocks that override them
320
+ and are selected by `WITAN_TARGET`, `load(target=...)` (Python API only — no
321
+ CLI `--target` flag yet), or auto-detection against the current
322
+ repo/checkout (`match_paths` > `match_repos` > `match_hosts` > `match_orgs`
323
+ — see witan's README/`witan/config.py` docstring for the full precedence).
324
+ Because the file is shared, one target block can carry witan's
325
+ `server`/`graph`/`token` alongside `code_dir` under
326
+ the same name — each server reads only the fields it knows:
327
+
328
+ ```toml
329
+ [targets.work]
330
+ server = "http://witan.internal:8080" # witan (witan-council)
331
+ code_dir = "/mnt/work/witan-code" # witan-code
332
+ match_orgs = ["myorg"]
333
+ ```
334
+
335
+ Environment variables always win over `config.toml`.
336
+
314
337
  ## MCP tools
315
338
 
316
339
  Each tool resolves the per-repo store from the current working directory and
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "witan-code"
3
- version = "0.4.0"
3
+ version = "0.7.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"
@@ -11,9 +11,12 @@ classifiers = [
11
11
  ]
12
12
  dependencies = [
13
13
  "agent-config-kit>=0.4,<1",
14
- "witan-core[cli]>=0.2,<1",
15
- "fastmcp>=3.4.2,<4",
14
+ "witan-core[cli]>=0.5,<1",
15
+ "fastmcp>=3.4.2,<5",
16
16
  "cyclopts>=4,<5",
17
+ # [targets.<name>] override models (config.py) — also transitively pulled
18
+ # in by fastmcp, declared explicitly since config.py imports it directly.
19
+ "pydantic>=2,<3",
17
20
  # Pinned to 0.25.x: tree-sitter 0.26.0 has a use-after-free in its pyo3 Node
18
21
  # binding that segfaults (exit 139) under the heavy .parent()/.children()
19
22
  # churn _walk_defs does on real-world-sized files — deterministically on the
@@ -40,6 +43,14 @@ dependencies = [
40
43
  "rich>=13",
41
44
  ]
42
45
 
46
+ [project.optional-dependencies]
47
+ # The io.modelcontextprotocol/tasks extension (SEP-2663), which lets a client
48
+ # poll a long `code_reindex` instead of holding the tool call open. Optional
49
+ # because it exists only for fastmcp 4.x while the pin above still spans 3.4.x,
50
+ # and because it pulls a Docket/Redis stack that the default in-process
51
+ # `memory://` backend does not need. server.py registers it when importable.
52
+ tasks = ["fastmcp-tasks>=4.0.0b1,<5"]
53
+
43
54
  [project.urls]
44
55
  Homepage = "https://github.com/mitodl/agent-kit/tree/main/mcp/servers/witan-code"
45
56
  Repository = "https://github.com/mitodl/agent-kit"
@@ -58,11 +69,14 @@ build-backend = "hatchling.build"
58
69
  # agent-config-kit version bumped alongside it — even before that version
59
70
  # has actually landed on PyPI. Published wheels only see the PyPI-range
60
71
  # dependency declared above.
61
- agent-config-kit = { path = "../../../packages/agent-config-kit", editable = true }
62
- witan-core = { path = "../../../packages/witan-core", editable = true }
72
+ agent-config-kit = { workspace = true }
73
+ witan-core = { workspace = true }
63
74
 
64
75
  [dependency-groups]
65
- test = ["pytest>=8"]
76
+ # fastmcp-tasks: the `tasks` extra. Without it CI would only ever exercise the
77
+ # extension-absent branch, and `code_reindex`'s task declaration would go
78
+ # untested — the failure mode there is the server refusing to start at all.
79
+ test = ["pytest>=8", "fastmcp-tasks>=4.0.0b1,<5"]
66
80
 
67
81
  [tool.pytest.ini_options]
68
82
  pythonpath = ["."]
@@ -72,7 +86,7 @@ testpaths = ["tests"]
72
86
  packages = ["witan_code"]
73
87
 
74
88
  [tool.bumpversion]
75
- current_version = "0.4.0"
89
+ current_version = "0.7.0"
76
90
  allow_dirty = true
77
91
 
78
92
  [[tool.bumpversion.files]]
@@ -0,0 +1,64 @@
1
+ """Canonical cluster graph-id derivation (config.graph_id).
2
+
3
+ This id is a cross-repo contract: witan-code addresses `--graph <id>` and
4
+ ol-infrastructure's cluster.yaml provisioning declares the same `<id>`. If this
5
+ algorithm drifts from the provisioning side, clients address graphs the cluster
6
+ never created — so pin the observable mapping here.
7
+ """
8
+
9
+ from witan_code import config
10
+
11
+
12
+ def test_graph_id_canonical_example():
13
+ # The example from the deployment decision; must be byte-for-byte stable.
14
+ assert (
15
+ config.graph_id("https://github.com/mitodl/ol-django")
16
+ == "code-github-com-mitodl-ol-django"
17
+ )
18
+
19
+
20
+ def test_graph_id_matches_constraint():
21
+ for repo in (
22
+ "https://github.com/mitodl/ol-django",
23
+ "http://example.com/x",
24
+ "git@github.com:mitodl/agent-kit.git",
25
+ "github.com/mitodl/ol-infrastructure",
26
+ "https://github.com/MITODL/Mixed_Case.Repo",
27
+ ):
28
+ assert config.GRAPH_ID_RE.match(config.graph_id(repo)), repo
29
+
30
+
31
+ def test_graph_id_no_underscores_and_lowercase():
32
+ gid = config.graph_id("https://github.com/MITODL/Mixed_Case.Repo")
33
+ assert "_" not in gid
34
+ assert gid == gid.lower()
35
+
36
+
37
+ def test_graph_id_strips_scheme():
38
+ with_scheme = config.graph_id("https://github.com/mitodl/x")
39
+ without = config.graph_id("github.com/mitodl/x")
40
+ assert with_scheme == without == "code-github-com-mitodl-x"
41
+
42
+
43
+ def test_graph_id_is_prefixed():
44
+ assert config.graph_id("github.com/a/b").startswith(config.CODE_GRAPH_PREFIX)
45
+
46
+
47
+ def test_graph_id_long_repo_truncates_with_hash():
48
+ long_repo = "https://github.com/mitodl/" + "a" * 200
49
+ gid = config.graph_id(long_repo)
50
+ assert len(gid) <= 64
51
+ assert config.GRAPH_ID_RE.match(gid)
52
+ # Distinct long repos disambiguate via the URI hash, never colliding.
53
+ other = config.graph_id("https://github.com/mitodl/" + "a" * 199 + "b")
54
+ assert gid != other
55
+
56
+
57
+ def test_graph_id_is_deterministic():
58
+ repo = "https://github.com/mitodl/" + "z" * 100
59
+ assert config.graph_id(repo) == config.graph_id(repo)
60
+
61
+
62
+ def test_bridge_graph_id_constant():
63
+ assert config.BRIDGE_GRAPH_ID == "code-bridge"
64
+ assert config.GRAPH_ID_RE.match(config.BRIDGE_GRAPH_ID)
@@ -0,0 +1,247 @@
1
+ """Tests for witan_code.config's [targets.<name>] routing (load(), _parse_targets).
2
+
3
+ The match-precedence logic itself (match_paths/match_repos/match_hosts/
4
+ match_orgs) lives in witan_core.target_config, with its own test suite at
5
+ packages/witan-core/tests/test_target_config.py; this file covers only
6
+ witan-code's own typed target model and load() integration.
7
+ """
8
+
9
+ import textwrap
10
+
11
+ import pytest
12
+
13
+ from witan_code.config import _parse_targets, load
14
+
15
+
16
+ # ── _parse_targets ────────────────────────────────────────────────────────────
17
+
18
+
19
+ def test_parse_targets_empty():
20
+ assert _parse_targets({}) == []
21
+
22
+
23
+ def test_parse_targets_basic():
24
+ raw = {
25
+ "targets": {"work": {"code_dir": "/mnt/work-code", "match_orgs": ["mitodl"]}}
26
+ }
27
+ result = _parse_targets(raw)
28
+ assert len(result) == 1
29
+ assert result[0].name == "work"
30
+ assert result[0].code_dir == "/mnt/work-code"
31
+ assert result[0].match_orgs == ["mitodl"]
32
+ assert result[0].match_paths == []
33
+
34
+
35
+ def test_parse_targets_targets_not_a_table():
36
+ with pytest.raises(ValueError, match="must be a table"):
37
+ _parse_targets({"targets": ["not", "a", "table"]})
38
+
39
+
40
+ def test_parse_targets_target_entry_not_a_table():
41
+ with pytest.raises(ValueError, match="must be a table"):
42
+ _parse_targets({"targets": {"work": "not-a-table"}})
43
+
44
+
45
+ def test_parse_targets_invalid_match_orgs_type():
46
+ raw = {"targets": {"work": {"match_orgs": 42}}}
47
+ with pytest.raises(ValueError, match="Expected a list or string"):
48
+ _parse_targets(raw)
49
+
50
+
51
+ # ── load() ────────────────────────────────────────────────────────────────────
52
+
53
+
54
+ @pytest.fixture
55
+ def toml_file(tmp_path):
56
+ """Write a config.toml and return its path."""
57
+
58
+ def _write(content: str) -> str:
59
+ p = tmp_path / "config.toml"
60
+ p.write_text(textwrap.dedent(content))
61
+ return str(p)
62
+
63
+ return _write
64
+
65
+
66
+ def test_load_defaults(monkeypatch, toml_file):
67
+ monkeypatch.setenv("WITAN_CONFIG", toml_file(""))
68
+ monkeypatch.delenv("WITAN_CODE_DIR", raising=False)
69
+ monkeypatch.delenv("WITAN_TARGET", raising=False)
70
+ monkeypatch.setenv("WITAN_REPO", "https://github.com/nobody/nothing")
71
+
72
+ cfg = load()
73
+ assert cfg.target_name is None
74
+ assert str(cfg.code_dir).endswith("/.local/share/witan/code")
75
+
76
+
77
+ def test_load_env_overrides_file(monkeypatch, toml_file):
78
+ monkeypatch.setenv("WITAN_CONFIG", toml_file('code_dir = "/from-file"'))
79
+ monkeypatch.setenv("WITAN_CODE_DIR", "/from-env")
80
+ monkeypatch.setenv("WITAN_REPO", "https://github.com/nobody/nothing")
81
+
82
+ cfg = load()
83
+ assert str(cfg.code_dir) == "/from-env"
84
+
85
+
86
+ def test_load_global_file_code_dir(monkeypatch, toml_file):
87
+ monkeypatch.setenv("WITAN_CONFIG", toml_file('code_dir = "/from-file"'))
88
+ monkeypatch.delenv("WITAN_CODE_DIR", raising=False)
89
+ monkeypatch.setenv("WITAN_REPO", "https://github.com/nobody/nothing")
90
+
91
+ cfg = load()
92
+ assert str(cfg.code_dir) == "/from-file"
93
+
94
+
95
+ def test_load_auto_detects_target_by_org(monkeypatch, toml_file):
96
+ monkeypatch.setenv(
97
+ "WITAN_CONFIG",
98
+ toml_file(
99
+ """
100
+ [targets.work]
101
+ code_dir = "/work-code"
102
+ match_orgs = ["mitodl"]
103
+ """
104
+ ),
105
+ )
106
+ monkeypatch.setenv("WITAN_REPO", "https://github.com/mitodl/agent-kit")
107
+ monkeypatch.delenv("WITAN_TARGET", raising=False)
108
+ monkeypatch.delenv("WITAN_CODE_DIR", raising=False)
109
+
110
+ cfg = load()
111
+ assert cfg.target_name == "work"
112
+ assert str(cfg.code_dir) == "/work-code"
113
+
114
+
115
+ def test_load_auto_detects_target_by_path(monkeypatch, toml_file, tmp_path):
116
+ checkout = tmp_path / "code" / "personal" / "dotfiles"
117
+ checkout.mkdir(parents=True)
118
+ monkeypatch.setenv(
119
+ "WITAN_CONFIG",
120
+ toml_file(
121
+ f"""
122
+ [targets.personal]
123
+ code_dir = "/personal-code"
124
+ match_paths = [{str(tmp_path / "code" / "personal")!r}]
125
+ """
126
+ ),
127
+ )
128
+ monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(checkout))
129
+ monkeypatch.setenv("WITAN_REPO", "")
130
+ monkeypatch.delenv("WITAN_TARGET", raising=False)
131
+ monkeypatch.delenv("WITAN_CODE_DIR", raising=False)
132
+
133
+ cfg = load()
134
+ assert cfg.target_name == "personal"
135
+ assert str(cfg.code_dir) == "/personal-code"
136
+
137
+
138
+ def test_load_explicit_target_arg(monkeypatch, toml_file):
139
+ monkeypatch.setenv(
140
+ "WITAN_CONFIG",
141
+ toml_file(
142
+ """
143
+ [targets.personal]
144
+ code_dir = "/personal-code"
145
+ match_orgs = ["alice"]
146
+ """
147
+ ),
148
+ )
149
+ monkeypatch.setenv("WITAN_REPO", "https://github.com/mitodl/agent-kit")
150
+ monkeypatch.delenv("WITAN_TARGET", raising=False)
151
+ monkeypatch.delenv("WITAN_CODE_DIR", raising=False)
152
+
153
+ cfg = load(target="personal")
154
+ assert cfg.target_name == "personal"
155
+ assert str(cfg.code_dir) == "/personal-code"
156
+
157
+
158
+ def test_load_witan_target_env(monkeypatch, toml_file):
159
+ monkeypatch.setenv(
160
+ "WITAN_CONFIG",
161
+ toml_file(
162
+ """
163
+ [targets.work]
164
+ code_dir = "/work-code"
165
+ match_orgs = ["mitodl"]
166
+ """
167
+ ),
168
+ )
169
+ monkeypatch.setenv("WITAN_TARGET", "work")
170
+ monkeypatch.delenv("WITAN_CODE_DIR", raising=False)
171
+
172
+ cfg = load()
173
+ assert cfg.target_name == "work"
174
+
175
+
176
+ def test_load_target_inherits_global_defaults(monkeypatch, toml_file):
177
+ """A target that omits author falls through to the global config value."""
178
+ monkeypatch.setenv(
179
+ "WITAN_CONFIG",
180
+ toml_file(
181
+ """
182
+ author = "Alice"
183
+
184
+ [targets.work]
185
+ code_dir = "/work-code"
186
+ match_orgs = ["mitodl"]
187
+ """
188
+ ),
189
+ )
190
+ monkeypatch.setenv("WITAN_REPO", "https://github.com/mitodl/agent-kit")
191
+ monkeypatch.delenv("WITAN_TARGET", raising=False)
192
+ monkeypatch.delenv("WITAN_AUTHOR", raising=False)
193
+ monkeypatch.delenv("WITAN_CODE_DIR", raising=False)
194
+
195
+ cfg = load()
196
+ assert cfg.target_name == "work"
197
+ assert cfg.author == "Alice"
198
+
199
+
200
+ def test_load_unknown_explicit_target_raises(monkeypatch, toml_file):
201
+ monkeypatch.setenv("WITAN_CONFIG", toml_file('[targets.work]\nmatch_orgs = ["x"]'))
202
+ monkeypatch.delenv("WITAN_TARGET", raising=False)
203
+
204
+ with pytest.raises(ValueError, match="Unknown target 'nope'"):
205
+ load(target="nope")
206
+
207
+
208
+ def test_load_missing_config_file_uses_defaults(monkeypatch, tmp_path):
209
+ monkeypatch.setenv("WITAN_CONFIG", str(tmp_path / "nonexistent.toml"))
210
+ monkeypatch.delenv("WITAN_TARGET", raising=False)
211
+ monkeypatch.setenv("WITAN_REPO", "https://github.com/nobody/nothing")
212
+
213
+ cfg = load()
214
+ assert cfg.target_name is None
215
+
216
+
217
+ def test_load_malformed_toml_raises(monkeypatch, tmp_path):
218
+ bad = tmp_path / "bad.toml"
219
+ bad.write_text("this is not [ valid toml !!!")
220
+ monkeypatch.setenv("WITAN_CONFIG", str(bad))
221
+
222
+ with pytest.raises(ValueError, match="Failed to parse config file"):
223
+ load()
224
+
225
+
226
+ def test_load_shares_config_file_with_witan(monkeypatch, toml_file):
227
+ """One [targets.<name>] block can carry both witan's server/graph and
228
+ witan-code's code_dir — this server only reads the fields it knows."""
229
+ monkeypatch.setenv(
230
+ "WITAN_CONFIG",
231
+ toml_file(
232
+ """
233
+ [targets.work]
234
+ server = "http://work:8080"
235
+ graph = "council-work"
236
+ code_dir = "/work-code"
237
+ match_orgs = ["mitodl"]
238
+ """
239
+ ),
240
+ )
241
+ monkeypatch.setenv("WITAN_REPO", "https://github.com/mitodl/agent-kit")
242
+ monkeypatch.delenv("WITAN_TARGET", raising=False)
243
+ monkeypatch.delenv("WITAN_CODE_DIR", raising=False)
244
+
245
+ cfg = load()
246
+ assert cfg.target_name == "work"
247
+ assert str(cfg.code_dir) == "/work-code"
@@ -18,6 +18,42 @@ def test_detect_env_override(monkeypatch):
18
18
  assert repo.detect() == "https://github.com/test/cg"
19
19
 
20
20
 
21
+ def test_detect_env_override_is_normalised(monkeypatch):
22
+ monkeypatch.setenv("WITAN_REPO", "https://GitHub.com/MITODL/OL-Django")
23
+ assert repo.detect() == "https://github.com/mitodl/ol-django"
24
+
25
+
26
+ @pytest.mark.skipif(shutil.which("git") is None, reason="git not on PATH")
27
+ def test_detect_empty_env_disables_autodetection(monkeypatch, tmp_path):
28
+ """An explicit empty WITAN_REPO must disable auto-detection outright,
29
+ matching witan-council's repo.detect — not fall through to git remote
30
+ lookup (the walrus-operator `if env_repo := ...` form treats "" as
31
+ falsy and used to do exactly that)."""
32
+ monkeypatch.setenv("WITAN_REPO", "")
33
+ subprocess.run(["git", "init", "-q", str(tmp_path)], check=True)
34
+ subprocess.run(
35
+ [
36
+ "git",
37
+ "-C",
38
+ str(tmp_path),
39
+ "remote",
40
+ "add",
41
+ "origin",
42
+ "git@github.com:mitodl/should-not-be-detected.git",
43
+ ],
44
+ check=True,
45
+ )
46
+ assert repo.detect(start=tmp_path) is None
47
+
48
+
49
+ def test_detect_override_arg_is_normalised(monkeypatch):
50
+ monkeypatch.delenv("WITAN_REPO", raising=False)
51
+ assert (
52
+ repo.detect(override="git@github.com:MITODL/OL-Django.git")
53
+ == "https://github.com/mitodl/ol-django"
54
+ )
55
+
56
+
21
57
  @pytest.mark.skipif(shutil.which("git") is None, reason="git not on PATH")
22
58
  def test_detect_tolerates_multivalued_fetch(tmp_path, monkeypatch):
23
59
  # git allows several `fetch =` lines under a remote; configparser rejects