mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-11-16 09:22:00 +00:00
Merge branch 'main' into codex/file-lock-reclaim-guard
This commit is contained in:
commit
466072f3c5
146 changed files with 720473 additions and 145 deletions
|
|
@ -68,6 +68,7 @@ GRAMMARS: dict[str, tuple[str, str, str]] = {
|
|||
"tree-sitter-typescript": ("tree-sitter/tree-sitter-typescript", "master", "typescript/src/parser.c"),
|
||||
# Vendored parsers — kept here so the upstream coords for drift
|
||||
# detection are co-located with every other grammar's coords.
|
||||
"tree-sitter-objc": ("tree-sitter-grammars/tree-sitter-objc", "master", "src/parser.c"),
|
||||
"tree-sitter-proto": ("coder3101/tree-sitter-proto", "main", "src/parser.c"),
|
||||
"tree-sitter-zig": ("tree-sitter-grammars/tree-sitter-zig", "master", "src/parser.c"),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ which is deliberately dependency-free so it runs on any vanilla runner. Run with
|
|||
(pytest also discovers ``unittest.TestCase`` classes, so a future pytest CI job
|
||||
picks these up unchanged.)
|
||||
|
||||
These tests lock in the #858 fix: the 6 vendored grammars
|
||||
(c/swift/kotlin/dart/proto/zig) are classified from the shared manifest
|
||||
These tests lock in the #858 fix: the 7 vendored grammars
|
||||
(c/swift/kotlin/dart/objc/proto/zig) are classified from the shared manifest
|
||||
(.github/vendored-grammars.json), their ABI is read from gitnexus/vendor/<name>,
|
||||
and the report never renders a bare ``?`` placeholder. All network is mocked.
|
||||
"""
|
||||
|
|
@ -192,7 +192,7 @@ class AssertCurrent(TestCase):
|
|||
def test_assert_current_is_network_free_and_passes(self):
|
||||
report, code = self._run_assert_current() # raises if any urlopen fires
|
||||
self.assertEqual(code, 0)
|
||||
# All 6 vendored grammars are introspected from the repo (ABI 14), not skipped.
|
||||
# All 7 vendored grammars are introspected from the repo (ABI 14), not skipped.
|
||||
for name in readiness.VENDORED_NAMES:
|
||||
self.assertIn(f"{name}: vendored ABI", report)
|
||||
|
||||
|
|
@ -365,15 +365,16 @@ class ReportRendering(TestCase):
|
|||
# Counts are derived from _render_report()'s mock corpus (all npm peer
|
||||
# deps mocked permissive): of the 10 npm-installed grammars, 9 render
|
||||
# Ready and 1 — tree-sitter-cpp — is the intentional pin (#1242), so it is
|
||||
# not counted ready. The 3 blockers are that same pinned tree-sitter-cpp
|
||||
# plus two held vendored grammars: ABI-held tree-sitter-c (#1242/#858) and
|
||||
# not counted ready. The 4 blockers are that same pinned tree-sitter-cpp
|
||||
# plus three held vendored grammars: ABI-held tree-sitter-c (#1242/#858),
|
||||
# tree-sitter-kotlin (pinned to an unreleased fwcd main commit for `fun
|
||||
# interface` support — ABI 14 is in range, but a hold counts as a blocker
|
||||
# until it is lifted). If a grammar is added/removed or a pin/hold changes,
|
||||
# until it is lifted), and tree-sitter-objc. If a grammar is added/removed
|
||||
# or a pin/hold changes,
|
||||
# update _render_report()'s mock AND these expected counts together; a
|
||||
# mismatch here means the report prose drifted, not the regex.
|
||||
self.assertEqual(ready.groups(), ("9", "10"))
|
||||
self.assertEqual(blockers.group(1), "3")
|
||||
self.assertEqual(blockers.group(1), "4")
|
||||
|
||||
def _matrix_row(self, name: str) -> str:
|
||||
for line in self.report.splitlines():
|
||||
|
|
|
|||
2
.github/scripts/update-vendored-grammars.mjs
vendored
2
.github/scripts/update-vendored-grammars.mjs
vendored
|
|
@ -21,7 +21,7 @@
|
|||
* node update-vendored-grammars.mjs # detect only → JSON report on stdout
|
||||
* node update-vendored-grammars.mjs --apply X # re-vendor grammar X in place
|
||||
*
|
||||
* tree-sitter-c is MONITORED but report-only (`hold`): it is ABI-pinned at 0.21.4
|
||||
* tree-sitter-c and tree-sitter-objc are MONITORED but report-only (`hold`): c is ABI-pinned at 0.21.4
|
||||
* (#1242/#858) and must not auto-bump without a tree-sitter runtime upgrade, so an
|
||||
* available c update is detected + reported but never auto-applied — even if it is
|
||||
* ABI-13/14. A maintainer re-vendors it deliberately.
|
||||
|
|
|
|||
5
.github/vendored-grammars.json
vendored
5
.github/vendored-grammars.json
vendored
|
|
@ -6,6 +6,11 @@
|
|||
"upstream": { "npm": "tree-sitter-c" },
|
||||
"hold": "ABI-pinned at 0.21.4 (#1242/#858) — needs a tree-sitter runtime upgrade before bumping"
|
||||
},
|
||||
"objc": {
|
||||
"name": "tree-sitter-objc",
|
||||
"upstream": { "npm": "tree-sitter-objc" },
|
||||
"hold": "Pinned at 3.0.2 for the Objective-C provider MVP; carries darwin/linux arm64+x64 prebuilds compatible with the current tree-sitter runtime (linux-arm64 built from vendored source because the upstream npm artifact is mislabeled)"
|
||||
},
|
||||
"swift": {
|
||||
"name": "tree-sitter-swift",
|
||||
"upstream": { "npm": "tree-sitter-swift" }
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ name: Build tree-sitter prebuilds
|
|||
#
|
||||
# Grammars covered here (the at-risk set — everything else already ships 6
|
||||
# upstream prebuilds AND stays dependency-review-tracked, so it is left alone).
|
||||
# All five are vendored under gitnexus/vendor/; `kind` (below) only picks where
|
||||
# All seven are vendored under gitnexus/vendor/; `kind` (below) only picks where
|
||||
# the build job fetches the C source to compile:
|
||||
# - tree-sitter-c (vendored prebuild-only; built from the published npm
|
||||
# package — closes upstream's 4/6 ARM gap #2116 for a
|
||||
|
|
@ -17,6 +17,8 @@ name: Build tree-sitter prebuilds
|
|||
# - tree-sitter-kotlin (vendored source; built from gitnexus/vendor/ — pinned to
|
||||
# an unreleased main commit for `fun interface` support
|
||||
# (#169) that no npm release carries yet)
|
||||
# - tree-sitter-objc (vendored source; built from gitnexus/vendor/ — pinned
|
||||
# for the Objective-C provider MVP)
|
||||
# - tree-sitter-swift (vendored source; built from gitnexus/vendor/ — its
|
||||
# prebuilds were originally upstream-shipped, now
|
||||
# GitNexus-cross-built like the rest for uniformity)
|
||||
|
|
@ -24,13 +26,13 @@ name: Build tree-sitter prebuilds
|
|||
# off npm optionalDependency so `npm i -g gitnexus`
|
||||
# no longer warns on peerOptional tree-sitter@^0.22.1.
|
||||
# Upstream linux-arm64 prebuild is a mispackaged
|
||||
# x86-64 binary; this workflow rebuilds all six.)
|
||||
# x86-64 binary; this workflow rebuilds all seven.)
|
||||
#
|
||||
# Output: gitnexus/vendor/<grammar>/prebuilds/<platform-arch>/<grammar>.node for
|
||||
# all 6 targets ({linux,darwin,win32}-{x64,arm64}). tree-sitter grammars are
|
||||
# N-API, so one ABI-stable .node per platform-arch works across all Node majors.
|
||||
#
|
||||
# COST DISCIPLINE — this is a HEAVY native matrix (up to 3 grammars x 6 runners,
|
||||
# COST DISCIPLINE — this is a HEAVY native matrix (up to 7 grammars x 6 runners,
|
||||
# incl. macOS + arm64). It is DELIBERATELY NOT wired into normal PR/push CI. It
|
||||
# runs only:
|
||||
# 1. on manual dispatch (workflow_dispatch); or
|
||||
|
|
@ -61,7 +63,7 @@ on:
|
|||
workflow_dispatch:
|
||||
inputs:
|
||||
grammars:
|
||||
description: 'Comma-separated grammar shortnames to build (c,dart,proto,kotlin,swift,zig), or "all".'
|
||||
description: 'Comma-separated grammar shortnames to build (c,dart,proto,kotlin,objc,swift,zig), or "all".'
|
||||
required: false
|
||||
type: string
|
||||
default: 'all'
|
||||
|
|
@ -93,7 +95,7 @@ on:
|
|||
- '!gitnexus/vendor/tree-sitter-*/prebuilds/**'
|
||||
# Self-test: re-run the guard if a future grammar pin is reintroduced in
|
||||
# the main package.json (optionalDependencies fallback). No-op otherwise —
|
||||
# all six grammars are now fully vendored (kotlin and zig included).
|
||||
# all seven grammars are now fully vendored (including kotlin, objc, and zig).
|
||||
- 'gitnexus/package.json'
|
||||
# Self-test: re-run the guard (normally a no-op) when the recipe changes.
|
||||
- '.github/workflows/build-tree-sitter-prebuilds.yml'
|
||||
|
|
@ -163,6 +165,9 @@ jobs:
|
|||
// unreleased main commit for `fun interface` support (#169) that no
|
||||
// npm release carries yet — so it must build from the vendored source.
|
||||
kotlin: { name: 'tree-sitter-kotlin', kind: 'vendored' },
|
||||
// Objective-C is vendored WITH its source and its native bindings
|
||||
// must be recut together with the pinned grammar snapshot.
|
||||
objc: { name: 'tree-sitter-objc', kind: 'vendored' },
|
||||
// swift is vendored WITH its source (parser.c/scanner.c/binding.gyp),
|
||||
// so it builds from gitnexus/vendor/ like dart/proto. Its prebuilds
|
||||
// were originally upstream-shipped; rebuilding them here unifies it.
|
||||
|
|
@ -505,6 +510,7 @@ jobs:
|
|||
dart: "void main() { print(\"hi\"); }",
|
||||
proto: "syntax = \"proto3\";\nmessage M { int32 id = 1; }",
|
||||
kotlin: "fun main() { println(\"hi\") }",
|
||||
objc: "@interface GNValidationProbe : NSObject\n@end",
|
||||
swift: "func greet() { print(\"hi\") }",
|
||||
zig: "pub fn main() void {}",
|
||||
};
|
||||
|
|
|
|||
21
.github/workflows/ci-tests.yml
vendored
21
.github/workflows/ci-tests.yml
vendored
|
|
@ -764,6 +764,16 @@ jobs:
|
|||
run: node --import tsx bench/zig-cross-file-resolution/measure.mjs --check
|
||||
working-directory: gitnexus
|
||||
|
||||
- name: Objective-C workspace resolution guards (#3179)
|
||||
if: ${{ !cancelled() }}
|
||||
# Build-free: fingerprints spread (typed self/super/sibling) and
|
||||
# protocol-candidate evidence, and gates linear file-count scaling
|
||||
# of emitPostResolutionEdges. Import lookup is the shared
|
||||
# import-target `objc` arm; this is the C#/Zig analog for the
|
||||
# workspace message-send pass.
|
||||
run: node --import tsx bench/objective-c-resolution/measure.mjs --check
|
||||
working-directory: gitnexus
|
||||
|
||||
- name: CFG construction time / disk / memory guards (#2081 M1)
|
||||
if: ${{ !cancelled() }}
|
||||
# Build-free: asserts collectFunctionCfgs output is unchanged
|
||||
|
|
@ -803,11 +813,13 @@ jobs:
|
|||
# guards they hold never run in the main coverage job.
|
||||
env:
|
||||
GITNEXUS_BENCH: '1'
|
||||
GITNEXUS_WORKER_READY_TIMEOUT_MS: '60000'
|
||||
run: >-
|
||||
npx vitest run --no-file-parallelism
|
||||
test/integration/cobol-pipeline-benchmark.test.ts
|
||||
test/integration/csharp-pipeline-benchmark.test.ts
|
||||
test/integration/csharp-razor-view-components-benchmark.test.ts
|
||||
test/integration/objective-c-pipeline-benchmark.test.ts
|
||||
test/integration/cpp-adl-benchmark.test.ts
|
||||
test/integration/data-route-table-benchmark.test.ts
|
||||
test/integration/instance-ownership-pipeline-benchmark.test.ts
|
||||
|
|
@ -846,6 +858,11 @@ jobs:
|
|||
timeout-minutes: 20
|
||||
env:
|
||||
GITNEXUS_REQUIRE_BWRAP_CANARY: '1'
|
||||
# This job installs bubblewrap, the pinned runtime and a built GitNexus,
|
||||
# so the offline sweep runs here with nothing provisioning-stubbed: real
|
||||
# containment, real mounts, real graph. A missing piece fails the job
|
||||
# rather than silently falling back to the stubbed path.
|
||||
GITNEXUS_REQUIRE_FULL_SWEEP: '1'
|
||||
GITNEXUS_REQUIRE_CLAUDE_CANARY: '1'
|
||||
steps:
|
||||
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
|
||||
|
|
@ -904,7 +921,9 @@ jobs:
|
|||
tests/test_process_control.py
|
||||
tests/test_proposer_sandbox.py
|
||||
tests/test_workflow_bench_sessions.py
|
||||
tests/test_ce_plugin_runtime.py -q
|
||||
tests/test_ce_plugin_runtime.py
|
||||
tests/test_offline_sweep_integration.py
|
||||
tests/test_mock_provider.py -q
|
||||
working-directory: eval
|
||||
|
||||
# Native Windows Job Object canary. POSIX-only tests skip by platform, while
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ name: Tree-sitter Upgrade Readiness
|
|||
# 1. Peer-dep compatibility — can each NPM-installed grammar install cleanly
|
||||
# with tree-sitter@0.25.0 without --legacy-peer-deps?
|
||||
# 2. Vendored grammars — each grammar in .github/vendored-grammars.json
|
||||
# (c/swift/kotlin/dart/proto) is classified by its vendored ABI, read
|
||||
# (c/swift/kotlin/dart/proto/objc) is classified by its vendored ABI, read
|
||||
# straight from gitnexus/vendor/<name>/src/parser.c (NOT node_modules,
|
||||
# which is never populated for vendored grammars — that mismatch is why
|
||||
# the report used to render bare "?" placeholders, #858).
|
||||
|
|
|
|||
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -72,6 +72,8 @@ eval/.hypothesis/
|
|||
|
||||
# Local docs — planning output (gitnexus-plan / gitnexus-work) stays local, not tracked
|
||||
docs/*
|
||||
!docs/fork/
|
||||
!docs/fork/**
|
||||
|
||||
gitnexus/test/fixtures/mini-repo/*.md
|
||||
gitnexus/test/fixtures/mini-repo/.claude
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
<!-- version: 1.14.0 -->
|
||||
<!-- Last updated: 2026-07-16 -->
|
||||
<!-- version: 1.15.0 -->
|
||||
<!-- Last updated: 2026-09-07 -->
|
||||
|
||||
Last reviewed: 2026-07-16
|
||||
Last reviewed: 2026-09-07
|
||||
|
||||
**Project:** GitNexus · **Environment:** dev · **Maintainer:** repository maintainers (see GitHub)
|
||||
|
||||
|
|
@ -39,6 +39,7 @@ Commands and gotchas live under **Repo reference** below and in **[CONTRIBUTING.
|
|||
## Reference docs
|
||||
|
||||
- **[ARCHITECTURE.md](ARCHITECTURE.md)**, **[CONTRIBUTING.md](CONTRIBUTING.md)**, **[GUARDRAILS.md](GUARDRAILS.md)**
|
||||
- **Objective-C provider work:** read **[docs/languages/objective-c-provider.md](docs/languages/objective-c-provider.md)** before changing Objective-C parsing or resolution.
|
||||
- **Call & inheritance resolution (RFC #909 Ring 3):** See ARCHITECTURE.md § Scope-Resolution Pipeline. All languages resolve calls and inheritance through the scope-resolution pipeline (`Registry.lookup`, `preEmitInheritanceEdges`, `emitHeritageEdges`, `buildMro` → `MethodDispatchIndex`). **Shared code in `gitnexus/src/core/ingestion/` must not name languages** — plug language behavior in via `LanguageProvider` / `ScopeResolver` hooks. A language plugs in by implementing `ScopeResolver` (`scope-resolution/contract/scope-resolver.ts`) and registering it in `SCOPE_RESOLVERS`. (The legacy call-resolution DAG + `@heritage` capture path were removed in RING4-1 #942.)
|
||||
- **Cursor:** `.cursor/index.mdc` (always-on); `.cursor/rules/*.mdc` (glob-scoped). Legacy `.cursorrules` deprecated.
|
||||
- **GitNexus:** standard skills in `.claude/skills/gitnexus-*/`; MCP rules in `gitnexus:start` block below.
|
||||
|
|
@ -90,6 +91,7 @@ mirror. `gitnexus/test/unit/shipped-skills-sync.test.ts` guards the copies. Toke
|
|||
|
||||
| Date | Version | Change |
|
||||
|------|---------|--------|
|
||||
| 2026-09-07 | 1.15.0 | Added the Objective-C provider guide as the required reference before changing Objective-C parsing or resolution. |
|
||||
| 2026-07-20 | 1.14.0 | `gitnexus-review` gains a coordinated swarm: six `ci-personas/` lanes the CI review agent dispatches as subagents (via the `Agent` tool), with a bounded critic gate and sidechain-excluded evidence. |
|
||||
| 2026-07-16 | 1.13.0 | `gitnexus-plan` asks plan depth up front (quick/standard/deep) in interactive runs; `gitnexus-lfg` gate slimmed to proceed/stop (Deepen stays as the route-back mechanism). |
|
||||
| 2026-07-16 | 1.12.0 | Renamed `gitnexus-pr-review` to `gitnexus-review`; added PR URL/number, branch/range, and local-change targets plus install migration (setup warns on a legacy `gitnexus-pr-review` dir and leaves it in place; uninstall removes it). |
|
||||
|
|
|
|||
|
|
@ -377,7 +377,7 @@ CI auto-discovers the set via `tsx`. No workflow edit required.
|
|||
|
||||
## Language-agnostic graph feeding
|
||||
|
||||
16 languages → single unified graph. Four abstraction layers:
|
||||
18 languages → single unified graph. Four abstraction layers:
|
||||
|
||||
```
|
||||
Unified Graph Schema (44 node types, 21 relationship types)
|
||||
|
|
@ -405,7 +405,7 @@ Each language implements `LanguageProvider` (`language-provider.ts`). Key fields
|
|||
| `descriptionExtractor` | Optional hook returning a symbol's doc-comment text as its `description`; feeds the embedding metadata header so doc-only terms are semantically searchable (issue #2270). Most languages register `createLeadingDocDescriptionExtractor` (shared, language-neutral; per-language comment/wrapper config passed at the call site) |
|
||||
| `definitionPropertiesExtractor` | Optional language-owned hook for structured, clone-safe definition metadata. Shared ingestion persists these properties opaquely; the owning provider supplies the extraction semantics. |
|
||||
|
||||
16 providers in `languages/index.ts` via `satisfies Record<SupportedLanguages, LanguageProvider>` — missing a language is a compile error.
|
||||
18 providers in `languages/index.ts` via `satisfies Record<SupportedLanguages, LanguageProvider>` — missing a language is a compile error.
|
||||
|
||||
### Unified capture tags
|
||||
|
||||
|
|
@ -542,4 +542,5 @@ Node IDs use arity suffix (`#<paramCount>`): `Method:file:Class.method#1` vs `#2
|
|||
- [RUNBOOK.md](RUNBOOK.md) — operational commands and recovery
|
||||
- [GUARDRAILS.md](GUARDRAILS.md) — safety boundaries for humans and agents
|
||||
- [TESTING.md](TESTING.md) — how to run tests
|
||||
- [docs/languages/objective-c-provider.md](docs/languages/objective-c-provider.md) — Objective-C provider behavior and limits
|
||||
- `AGENTS.md` / `CLAUDE.md` — agent workflows and tool usage
|
||||
|
|
|
|||
|
|
@ -661,6 +661,7 @@ GitNexus builds a complete knowledge graph of your codebase through a multi-phas
|
|||
| Swift | — | — | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| C | — | — | ✓ | — | ✓ | ✓ | — | ✓ | ✓ |
|
||||
| C++ | — | — | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ |
|
||||
| Objective-C | ✓ | — | ✓ | ✓ | ✓ | — | — | — | — |
|
||||
| Dart | ✓ | — | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ |
|
||||
| Zig | ✓ | — | ✓ | — | ✓ | ✓ | ✓ | — | ✓ |
|
||||
|
||||
|
|
|
|||
109
docs/languages/objective-c-provider.md
Normal file
109
docs/languages/objective-c-provider.md
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
# Objective-C Language Provider
|
||||
|
||||
Status: implemented
|
||||
|
||||
The deterministic provider is covered by focused unit and integration tests. The parser-loader ABI
|
||||
smoke runs in the published multi-OS test matrix, and the native prebuild workflow owns
|
||||
Objective-C together with all six vendored grammar targets. This status describes the implemented
|
||||
MVP; it does not promise full Objective-C runtime dispatch.
|
||||
|
||||
## Goal
|
||||
|
||||
Add deterministic, symbol-level Objective-C analysis to GitNexus. The first release must support high-confidence code navigation and direct static dependency analysis for `.m`, `.mm`, and Objective-C `.h` files. It must not imply that Objective-C runtime dispatch is fully resolved.
|
||||
|
||||
The provider belongs in the existing language-provider and scope-resolution extension points. Shared ingestion code must remain language-agnostic.
|
||||
|
||||
## Compatibility contract
|
||||
|
||||
- Existing language detection and parsing must remain unchanged.
|
||||
- A `.h` file must be classified from its content or surrounding context; it cannot be unconditionally claimed by Objective-C because C and C++ also use that extension.
|
||||
- If Objective-C grammar loading fails, the error must clearly name the missing provider/grammar and cannot corrupt a previously valid index.
|
||||
- Provider and grammar versions must be stored in index metadata. A version change that can alter node identity or edges requires a full rebuild.
|
||||
- No LLM participates in parsing, name resolution, or edge creation. Analysis is Tree-sitter plus deterministic static resolution.
|
||||
|
||||
## MVP model
|
||||
|
||||
The provider must extract and connect:
|
||||
|
||||
- Classes, superclasses, protocols, categories, class extensions, properties, ivars, C functions, imports, declarations, and implementations.
|
||||
- Instance and class methods, preserving their complete multi-part selector.
|
||||
- Inheritance, protocol conformance, import, declaration/implementation, host-class/category, and statically resolved call relationships.
|
||||
|
||||
Stable identity must include enough ownership to distinguish same-named methods. Recommended forms are:
|
||||
|
||||
```text
|
||||
objc:class:<ClassName>
|
||||
objc:protocol:<ProtocolName>
|
||||
objc:category:<HostClass>:<CategoryName>
|
||||
objc:method:<Owner>:-:<selector>
|
||||
objc:method:<Owner>:+:<selector>
|
||||
objc:function:<qualified-or-file-scoped-name>
|
||||
```
|
||||
|
||||
For example, `-loadData:completion:` and `+loadData:completion:` are different symbols. A category method remains linked to both its category and host class; querying the host class must expose distributed implementations.
|
||||
|
||||
## Resolution policy
|
||||
|
||||
Resolution must be conservative. A missing or dynamic target is evidence of uncertainty, not proof that no target exists.
|
||||
|
||||
| Receiver case | Required result |
|
||||
| --------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
|
||||
| Explicit class name, `self`, or `super` | Resolve when the owner is statically known. |
|
||||
| Local, parameter, property, or ivar with known static type | Resolve to matching owner and selector. |
|
||||
| Protocol-typed receiver | Link the protocol method and identify possible implementations as candidates. |
|
||||
| Multiple host-class/category implementations of one selector | Record all static candidates as evidence; do not emit a certain call edge because runtime image-load order is unknown. |
|
||||
| `id`, `Class`, macros, reflection, `performSelector:`, `NSInvocation`, runtime injection, or unknown type | Store selector/location with `resolution=unresolved`; do not emit a certain call edge. |
|
||||
|
||||
The provider should first collect file-local declarations, imports, and types, then resolve across the repository. It must use structured Tree-sitter captures or AST traversal, not regular expressions over source text. Multi-part selectors, block arguments, nullability annotations, generics, macros, and multiline declarations make a regex-only extractor unsafe.
|
||||
|
||||
## Imports and incremental correctness
|
||||
|
||||
- Resolve quoted project imports against the current directory, configured include roots, and indexed headers. Model framework imports as external-module evidence without downloading SDK source.
|
||||
- Merge `@interface`, `@implementation`, categories, and extensions across files.
|
||||
- A changed header, protocol, class declaration, or category invalidates importing and affected implementation/call-resolution state. Incremental output after such a change must match a full rebuild.
|
||||
- Index metadata must record provider version, grammar version, include/exclude configuration, and parsing options used for resolution.
|
||||
|
||||
## Implementation sequence
|
||||
|
||||
1. Add and package a pinned Objective-C Tree-sitter grammar; verify macOS arm64 and the production Linux runner can load it.
|
||||
2. Add language detection for `.m`, `.mm`, and content-classified `.h` files.
|
||||
3. Implement AST extraction and stable IDs for declarations and definitions.
|
||||
4. Implement repository-level merge, imports, inheritance, protocol, and category relationships.
|
||||
5. Add conservative message-send resolution and explicit unresolved evidence.
|
||||
6. Integrate invalidation, metadata comparison, MCP/CLI output, and fixtures.
|
||||
|
||||
## Fixtures and acceptance
|
||||
|
||||
Create a minimal Objective-C fixture containing a class, protocol, category, extension, superclass, properties, ivars, C function, imports, multi-part selector, block parameter, `self`, `super`, protocol receiver, and `id` receiver. Use `symodulebridge` as a real integration fixture after the minimal suite is stable.
|
||||
|
||||
The acceptance bar is:
|
||||
|
||||
- `query "SYModuleCaller"` yields class/method semantic nodes, not only file nodes.
|
||||
- `context "SYModuleCaller" --file <path>` yields declaration, implementation, imports, and known references.
|
||||
- Known statically typed message sends create call edges; dynamic sends are marked unresolved.
|
||||
- Same selector on multiple classes, a category override, and `+` versus `-` methods remain distinct.
|
||||
- A `.m`, `.h`, protocol, or category edit produces results equivalent to a clean rebuild.
|
||||
- Generated documentation, dependency directories, and build output are excluded through explicit indexing configuration.
|
||||
|
||||
## Non-goals
|
||||
|
||||
The MVP does not promise exact runtime type inference for `id` or `instancetype`, reflection, swizzling, arbitrary category replacement, dynamic selector construction, or complete impact analysis across every runtime dispatch path. Tool results must surface confidence and unresolved evidence rather than presenting guesses as certain graph facts.
|
||||
|
||||
## Current implementation coverage
|
||||
|
||||
Implemented capabilities:
|
||||
|
||||
- Vendored `tree-sitter-objc` grammar, registered through the existing Tree-sitter loader.
|
||||
- `.m` and `.mm` language mapping plus content-based `.h` classification so plain C/C++ headers are not unconditionally claimed.
|
||||
- LanguageProvider extraction for classes, protocols, categories, extensions, methods, properties, ivars, C functions, imports, unresolved message evidence, stable Objective-C qualified names, and provider/grammar metadata.
|
||||
- Length-preserving preprocessing of bare, file-scope all-caps macro markers before Tree-sitter parsing. This recovers declarations after wrappers such as `RCT_EXTERN_C_BEGIN` / `RCT_EXTERN_C_END` without expanding macros or adding framework-specific rules.
|
||||
- ScopeResolver edges for imports, inheritance, protocol conformance, category host membership, implementation evidence, and conservative static message sends.
|
||||
- Persisted query/context support for Objective-C class and method nodes, including implementation evidence via `DECLARES`.
|
||||
- Regression tests for grammar loading, `.h` classification, stable identities, conservative calls, metadata feature mismatch, persisted query/context behavior, and incremental-vs-force parity for Objective-C fixture edits.
|
||||
|
||||
Known limits of this MVP:
|
||||
|
||||
- The first version does not perform full Objective-C runtime dispatch, swizzling, dynamic selector construction, macro expansion, or `id` flow inference. Bare file-scope marker macros are elided only to preserve parser recovery; their expansion semantics are not interpreted.
|
||||
- Protocol receiver handling records the protocol method and candidate implementation evidence, but candidate implementations are not emitted as certain call edges.
|
||||
- When a host class and one or more named categories define the same selector, the provider records candidate evidence rather than choosing a runtime winner or emitting multiple certain call edges.
|
||||
- Objective-C++ `.mm` files are parsed with the Objective-C grammar path for this MVP; deep C++ semantic extraction inside Objective-C++ bodies remains outside this provider.
|
||||
175
eval/tests/fixtures/fake_claude.py
vendored
Executable file
175
eval/tests/fixtures/fake_claude.py
vendored
Executable file
|
|
@ -0,0 +1,175 @@
|
|||
#!/usr/bin/env python3
|
||||
"""A stand-in for the Claude Code CLI: real HTTP, real tool execution, real stream-json.
|
||||
|
||||
Not a mock of the harness's own code. It does what the CLI does at the two
|
||||
boundaries the harness depends on - it calls ANTHROPIC_BASE_URL for a turn, it
|
||||
EXECUTES the tool blocks that come back, and it prints the stream-json event
|
||||
sequence the parent parses. Only Write really executes - it is what produces the
|
||||
review artifact, so the artifact path has to be genuine end to end. Skill is
|
||||
MODELLED: it validates the request and returns a synthetic result, because the
|
||||
parent's evidence gate keys on the request/result pair rather than on a skill
|
||||
having loaded, and a fixture cannot load a real one. Bash is stubbed outright:
|
||||
arbitrary shell from a scripted reply buys no fidelity for the paths this
|
||||
exercises and plenty of ways to damage the host. Everything between
|
||||
those boundaries (the sandbox,
|
||||
the artifact capture, the scoring, the row) stays real, which is the whole
|
||||
point: those are the layers that shipped bugs no unit test could see.
|
||||
|
||||
Reads the prompt from stdin, as the real CLI does under "-p --input-format text".
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
|
||||
def _turn(base_url: str, prompt: str) -> dict:
|
||||
request = urllib.request.Request(
|
||||
base_url.rstrip("/") + "/v1/messages",
|
||||
data=json.dumps({"model": os.environ.get("ANTHROPIC_MODEL", "mock"), "max_tokens": 1024,
|
||||
"messages": [{"role": "user", "content": prompt}]}).encode(),
|
||||
headers={"Content-Type": "application/json",
|
||||
"x-api-key": os.environ.get("ANTHROPIC_API_KEY", ""),
|
||||
"anthropic-version": "2023-06-01"},
|
||||
)
|
||||
with urllib.request.urlopen(request, timeout=30) as response:
|
||||
return json.load(response)
|
||||
|
||||
|
||||
def _run_tool(name: str, params: dict) -> str:
|
||||
"""Write executes for real - it is what produces the review artifact.
|
||||
|
||||
Skill and Bash do not: see the module docstring for which is modelled and
|
||||
which is stubbed, and why neither can be genuine here.
|
||||
"""
|
||||
|
||||
if name == "Write":
|
||||
target = pathlib.Path(params["file_path"])
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
# Atomic, exactly as the real Write tool does it: temp file beside the
|
||||
# target, then rename. This is the operation the read-only workspace
|
||||
# boundary has to permit for the artifact directory and refuse for the
|
||||
# workspace, so a stand-in that wrote in place would prove nothing.
|
||||
staging = target.with_name(target.name + ".tmp.fake")
|
||||
staging.write_text(params.get("content", ""))
|
||||
os.replace(staging, target)
|
||||
return f"wrote {target}"
|
||||
if name == "Skill":
|
||||
# Modelled explicitly rather than falling through to a generic success.
|
||||
# The parent's evidence gate keys on a Skill request with a non-error
|
||||
# result, so leaving this unimplemented let an unexecuted skill satisfy
|
||||
# the gate - the gate would have been measuring the fixture, not a skill.
|
||||
skill = params.get("skill") or params.get("command") or params.get("name")
|
||||
if not skill:
|
||||
raise NotImplementedError("Skill request carried no skill name")
|
||||
return f"loaded skill {skill}"
|
||||
if name == "Bash":
|
||||
return "(bash suppressed in the stand-in)"
|
||||
# An unsupported tool is a FAILED tool run, not a quiet success. Returning a
|
||||
# plain string here made the parent's evidence gate read an unexecuted Skill
|
||||
# request as a successful invocation.
|
||||
raise NotImplementedError(f"unsupported tool {name}")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
# stdin, because that is where the real CLI takes it under
|
||||
# "-p --input-format text": the parent pipes prompt bytes in. Scanning argv
|
||||
# for a non-flag token picks up a flag's VALUE instead ("text"), which is
|
||||
# exactly what the prompt-fidelity test caught.
|
||||
prompt = sys.stdin.read()
|
||||
base_url = os.environ.get("ANTHROPIC_BASE_URL")
|
||||
if not base_url:
|
||||
print(json.dumps({"type": "result", "subtype": "error", "is_error": True,
|
||||
"session_id": "fake-session", "num_turns": 0}), flush=True)
|
||||
return 1
|
||||
|
||||
emit = lambda event: print(json.dumps(event), flush=True) # noqa: E731
|
||||
emit({"type": "system", "subtype": "init", "session_id": "fake-session"})
|
||||
|
||||
try:
|
||||
message = _turn(base_url, prompt)
|
||||
except (OSError, ValueError) as exc:
|
||||
# A provider failure is a failed SESSION, not a crashed process: dying
|
||||
# here leaves no terminal result event, so the parent reports a generic
|
||||
# stream error instead of the upstream failure it actually saw.
|
||||
emit({"type": "result", "subtype": "error", "is_error": True,
|
||||
"session_id": "fake-session", "num_turns": 0,
|
||||
"error": f"provider request failed: {type(exc).__name__}: {exc}"})
|
||||
return 1
|
||||
blocks = message.get("content", [])
|
||||
emit({"type": "assistant", "message": {"role": "assistant", "content": blocks}})
|
||||
|
||||
tool_results = []
|
||||
for block in blocks:
|
||||
if block.get("type") == "tool_use":
|
||||
# A refused write is a tool ERROR the session reports and carries
|
||||
# on from, not a crash. Letting it kill the process would lose the
|
||||
# result event and misreport a working boundary as a broken run.
|
||||
failed = False
|
||||
try:
|
||||
output = _run_tool(block["name"], block.get("input", {}))
|
||||
except (OSError, NotImplementedError) as exc:
|
||||
output, failed = f"error: {type(exc).__name__}: {exc}", True
|
||||
# is_error is load-bearing: the parent treats an ABSENT is_error as
|
||||
# success, so a refused or unsupported tool would otherwise be
|
||||
# scored as a completed one.
|
||||
tool_results.append({
|
||||
"type": "tool_result", "tool_use_id": block["id"],
|
||||
"content": output, "is_error": failed,
|
||||
})
|
||||
if tool_results:
|
||||
emit({"type": "user", "message": {"role": "user", "content": tool_results}})
|
||||
|
||||
# Unknown is not zero. A reply carrying no usage used to become four
|
||||
# zero-valued fields plus a fabricated cost, which the harness then treats
|
||||
# as a real measurement - the exact confusion the accounting this fixture
|
||||
# feeds exists to prevent.
|
||||
usage = message.get("usage")
|
||||
# Every field that gets forwarded is validated, not just the required two.
|
||||
# The parent's well_formed check tests only that the four keys are PRESENT,
|
||||
# so an unvalidated cache value rides into a success result and is recorded
|
||||
# as a real measurement. A field good enough to report is good enough to
|
||||
# check.
|
||||
countable = lambda v: isinstance(v, int) and not isinstance(v, bool) and v >= 0 # noqa: E731
|
||||
if not isinstance(usage, dict) or not all(
|
||||
countable(usage.get(f)) for f in ("input_tokens", "output_tokens")
|
||||
) or not all(
|
||||
countable(usage[f])
|
||||
for f in ("cache_read_input_tokens", "cache_creation_input_tokens")
|
||||
if f in usage
|
||||
):
|
||||
emit({"type": "result", "subtype": "error", "is_error": True,
|
||||
"session_id": "fake-session", "num_turns": 1,
|
||||
"error": "provider reply carried no usable usage; refusing to report a measured run"})
|
||||
return 1
|
||||
emit({
|
||||
"type": "result",
|
||||
"subtype": "success",
|
||||
"is_error": False,
|
||||
"session_id": "fake-session",
|
||||
"num_turns": 1,
|
||||
"duration_ms": 1200,
|
||||
# A measured zero is not the same as unmeasured; the parent rejects a
|
||||
# collapsed cost, so report a real one.
|
||||
"total_cost_usd": 0.42,
|
||||
# Forward exactly the fields the provider reported. Defaulting the
|
||||
# absent ones to 0 fabricated a complete measurement out of an
|
||||
# incomplete reply - and worse, it made the parent's own completeness
|
||||
# check (runner_sessions.USAGE_FIELDS / well_formed) unfirable from any
|
||||
# offline test, because the stand-in always satisfied it.
|
||||
"usage": {
|
||||
field: usage[field]
|
||||
for field in ("input_tokens", "output_tokens",
|
||||
"cache_read_input_tokens", "cache_creation_input_tokens")
|
||||
if field in usage
|
||||
},
|
||||
})
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
280
eval/tests/test_mock_provider.py
Normal file
280
eval/tests/test_mock_provider.py
Normal file
|
|
@ -0,0 +1,280 @@
|
|||
"""The mock has to be right about the wire, or every test built on it lies."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from workflow_bench.mock_provider import MockProvider, Reply
|
||||
from workflow_bench.provider_usage import (
|
||||
ANTHROPIC,
|
||||
LITELLM_NORMALIZED,
|
||||
OPENAI_RESPONSES,
|
||||
normalize_usage,
|
||||
)
|
||||
|
||||
|
||||
def _post(url: str, payload: dict) -> tuple[int, bytes]:
|
||||
request = urllib.request.Request(
|
||||
url, data=json.dumps(payload).encode(), headers={"Content-Type": "application/json"}
|
||||
)
|
||||
with urllib.request.urlopen(request, timeout=10) as response:
|
||||
return response.status, response.read()
|
||||
|
||||
|
||||
def test_anthropic_messages_returns_a_usable_message() -> None:
|
||||
with MockProvider([Reply(text="reviewed")]) as provider:
|
||||
_status, raw = _post(provider.base_url + "/v1/messages", {"model": "m", "messages": []})
|
||||
body = json.loads(raw)
|
||||
assert body["role"] == "assistant"
|
||||
assert body["content"][0]["text"] == "reviewed"
|
||||
assert body["stop_reason"] == "end_turn"
|
||||
|
||||
|
||||
def test_a_scripted_tool_call_is_carried_as_a_tool_use_block() -> None:
|
||||
"""Tool blocks are how a mocked run produces real artifacts.
|
||||
|
||||
The CLI executes what it is asked to run, so a Write block makes it write
|
||||
that file for real inside the sandbox - which is how an artifact-producing
|
||||
cell can be exercised with no model involved.
|
||||
"""
|
||||
|
||||
write = {"name": "Write", "input": {"file_path": "/review-output/review-output.json", "content": "{}"}}
|
||||
with MockProvider([Reply(text="writing", tools=[write])]) as provider:
|
||||
_status, raw = _post(provider.base_url + "/v1/messages", {"model": "m", "messages": []})
|
||||
body = json.loads(raw)
|
||||
block = body["content"][1]
|
||||
assert block["type"] == "tool_use" and block["name"] == "Write"
|
||||
assert block["input"]["file_path"] == "/review-output/review-output.json"
|
||||
assert body["stop_reason"] == "tool_use", "a turn ending in a tool call must say so"
|
||||
|
||||
|
||||
def test_streaming_emits_the_event_sequence_a_consumer_expects() -> None:
|
||||
with MockProvider([Reply(text="hi")]) as provider:
|
||||
request = urllib.request.Request(
|
||||
provider.base_url + "/v1/messages",
|
||||
data=json.dumps({"model": "m", "messages": [], "stream": True}).encode(),
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
with urllib.request.urlopen(request, timeout=10) as response:
|
||||
assert response.headers["Content-Type"] == "text/event-stream"
|
||||
body = response.read().decode()
|
||||
|
||||
events = [line[len("event: ") :] for line in body.splitlines() if line.startswith("event: ")]
|
||||
assert events[0] == "message_start"
|
||||
assert events[-1] == "message_stop"
|
||||
assert "content_block_delta" in events
|
||||
# message_delta carries the final usage, which is where output tokens land.
|
||||
assert events[-2] == "message_delta"
|
||||
|
||||
|
||||
def test_each_protocol_reports_usage_in_its_own_arithmetic() -> None:
|
||||
"""The whole point: the two providers count the same numbers differently.
|
||||
|
||||
Anthropic's cache fields ADD to input_tokens; OpenAI's are SUBSETS of it.
|
||||
Scripting one Reply and serving it both ways is what makes that asymmetry
|
||||
testable without a paid request.
|
||||
"""
|
||||
|
||||
reply = Reply(input_tokens=2_000, output_tokens=300, cache_read_input_tokens=7_000, cache_creation_input_tokens=1_000)
|
||||
|
||||
with MockProvider([reply, reply]) as provider:
|
||||
_s, anthropic_raw = _post(provider.base_url + "/v1/messages", {"model": "m", "messages": []})
|
||||
_s, openai_raw = _post(provider.base_url + "/v1/responses", {"model": "m", "input": []})
|
||||
|
||||
anthropic = normalize_usage(ANTHROPIC, json.loads(anthropic_raw)["usage"])
|
||||
openai = normalize_usage(OPENAI_RESPONSES, json.loads(openai_raw)["usage"])
|
||||
|
||||
assert anthropic.total_input_tokens == 10_000
|
||||
assert openai.total_input_tokens == 10_000, "same billed work, stated as the whole"
|
||||
assert anthropic.ordinary_input_tokens == 2_000
|
||||
assert openai.ordinary_input_tokens == 2_000, "recovered by subtraction, not addition"
|
||||
assert openai.cache_read_input_tokens == 7_000
|
||||
|
||||
|
||||
def test_a_scripted_failure_is_returned_as_one() -> None:
|
||||
"""Billed failures are part of what the accounting must survive."""
|
||||
|
||||
with MockProvider([Reply(status_code=529, error_body={"error": {"type": "overloaded_error"}})]) as provider:
|
||||
try:
|
||||
_post(provider.base_url + "/v1/messages", {"model": "m", "messages": []})
|
||||
raise AssertionError("the scripted failure was not returned")
|
||||
except urllib.error.HTTPError as exc:
|
||||
assert exc.code == 529
|
||||
|
||||
|
||||
def test_requests_are_recorded_for_assertions() -> None:
|
||||
with MockProvider() as provider:
|
||||
_post(provider.base_url + "/v1/messages", {"model": "claude-sonnet-4-5", "messages": [{"role": "user"}]})
|
||||
assert len(provider.requests) == 1
|
||||
assert provider.requests[0].body["model"] == "claude-sonnet-4-5"
|
||||
assert provider.requests[0].path.endswith("/v1/messages")
|
||||
|
||||
|
||||
def test_an_unscripted_turn_gets_the_default_rather_than_stalling() -> None:
|
||||
"""A real run makes more calls than a test wants to enumerate."""
|
||||
|
||||
with MockProvider([Reply(text="first")], default=Reply(text="fallback")) as provider:
|
||||
_s, one = _post(provider.base_url + "/v1/messages", {"model": "m", "messages": []})
|
||||
_s, two = _post(provider.base_url + "/v1/messages", {"model": "m", "messages": []})
|
||||
assert json.loads(one)["content"][0]["text"] == "first"
|
||||
assert json.loads(two)["content"][0]["text"] == "fallback"
|
||||
|
||||
|
||||
def test_a_request_through_the_real_gateway_records_native_usage(tmp_path, monkeypatch) -> None:
|
||||
"""The whole stack minus the model: proxy, translation, callback, log.
|
||||
|
||||
This is the path that shipped three separate defects invisible to unit
|
||||
tests - the usage variable never reaching the proxy subprocess, the
|
||||
callback failing to import when loaded by path, and failures never
|
||||
recorded. All three live between the gateway and the provider, which is
|
||||
exactly the span this exercises.
|
||||
"""
|
||||
|
||||
|
||||
import yaml
|
||||
|
||||
from workflow_bench import model_gateway
|
||||
from workflow_bench.model_gateway import OpenAIGateway
|
||||
from workflow_bench.provider_usage import USAGE_LOG_ENV_VAR
|
||||
|
||||
if shutil.which("litellm") is None:
|
||||
import pytest
|
||||
|
||||
pytest.skip("litellm console script absent; the proxy cannot start here")
|
||||
|
||||
usage_log = tmp_path / "provider_usage.jsonl"
|
||||
monkeypatch.setenv(USAGE_LOG_ENV_VAR, str(usage_log))
|
||||
|
||||
reply = Reply(input_tokens=2_000, output_tokens=300, cache_read_input_tokens=7_000, cache_creation_input_tokens=1_000)
|
||||
with MockProvider(default=reply) as provider:
|
||||
original = model_gateway.write_openai_litellm_config
|
||||
|
||||
def config(path, names):
|
||||
original(path, names)
|
||||
document = yaml.safe_load(path.read_text())
|
||||
for entry in document["model_list"]:
|
||||
entry["litellm_params"]["api_base"] = f"{provider.base_url}/v1"
|
||||
path.write_text(yaml.safe_dump(document))
|
||||
return path
|
||||
|
||||
monkeypatch.setattr(model_gateway, "write_openai_litellm_config", config)
|
||||
with OpenAIGateway(
|
||||
openai_api_key="mock-key", model_names=["gpt-4.1"], work_dir=tmp_path / "gw", ready_timeout_s=60
|
||||
) as gateway:
|
||||
request = urllib.request.Request(
|
||||
gateway.base_url + "/v1/messages",
|
||||
data=json.dumps({"model": "gpt-4.1", "max_tokens": 32, "messages": [{"role": "user", "content": "ping"}]}).encode(),
|
||||
headers={"Content-Type": "application/json", "x-api-key": gateway.auth_token, "anthropic-version": "2023-06-01"},
|
||||
)
|
||||
with urllib.request.urlopen(request, timeout=60):
|
||||
pass
|
||||
|
||||
assert usage_log.exists(), "the callback never wrote - the env did not reach the proxy"
|
||||
events = [json.loads(line) for line in usage_log.read_text().splitlines()]
|
||||
assert events, "the proxy started but recorded nothing"
|
||||
event = events[-1]
|
||||
native = event["native_usage"]
|
||||
# LiteLLM hands a callback its OWN normalised object, not the upstream body:
|
||||
# an OpenAI Responses reply arrives as prompt_tokens / prompt_tokens_details.
|
||||
# Asserting the wire shape here is what proved the shipped adapter read keys
|
||||
# that are never present.
|
||||
assert native["prompt_tokens_details"]["cached_tokens"] == 7_000
|
||||
assert native["prompt_tokens_details"]["cache_write_tokens"] == 1_000
|
||||
assert event["provider"] == LITELLM_NORMALIZED
|
||||
assert event["call_type"] == "anthropic_messages", "the observed call type, not a Responses one"
|
||||
|
||||
usage = normalize_usage(event["provider"], native)
|
||||
assert usage.total_input_tokens == 10_000
|
||||
assert usage.cache_read_input_tokens == 7_000
|
||||
assert usage.cache_write_input_tokens == 1_000
|
||||
assert usage.ordinary_input_tokens == 2_000
|
||||
assert usage.complete, "a run that cannot interpret its own usage measured nothing"
|
||||
|
||||
|
||||
def test_probe_what_identity_the_real_cli_actually_sends(tmp_path: Path) -> None:
|
||||
"""An experiment, not an assertion: which fields could correlate a request to a cell?
|
||||
|
||||
Per-cell usage attribution is unbuilt because one proxy serves the whole
|
||||
sweep, so anything read from the proxy environment is identical for every
|
||||
request. Attribution needs something that travels WITH the request, and
|
||||
what the Claude Code CLI actually sends is not documented anywhere I can
|
||||
check - guessing it is how the last three accounting bugs happened.
|
||||
|
||||
So this drives the REAL pinned CLI against the mock and prints the
|
||||
identity-bearing fields that arrive. It asserts only that a request was
|
||||
made; the value is the recorded evidence, which the job log preserves.
|
||||
"""
|
||||
|
||||
claude = os.environ.get("CLAUDE_CANARY_BIN")
|
||||
if not claude or not Path(claude).exists():
|
||||
pytest.skip("no pinned Claude CLI here; the containment job supplies CLAUDE_CANARY_BIN")
|
||||
|
||||
with MockProvider(default=Reply(text="ok")) as provider:
|
||||
subprocess.run(
|
||||
[claude, "-p", "--input-format", "text", "--output-format", "stream-json", "--verbose"],
|
||||
input=b"say ok",
|
||||
capture_output=True,
|
||||
timeout=120,
|
||||
env={
|
||||
**os.environ,
|
||||
"ANTHROPIC_BASE_URL": provider.base_url,
|
||||
"ANTHROPIC_API_KEY": "offline-probe",
|
||||
"HOME": str(tmp_path),
|
||||
},
|
||||
)
|
||||
|
||||
assert provider.requests, "the real CLI never reached the mock provider"
|
||||
request = provider.requests[0]
|
||||
interesting = {
|
||||
"header:" + name: value
|
||||
for name, value in request.headers.items()
|
||||
if any(k in name.lower() for k in ("session", "user", "trace", "request-id", "conversation", "metadata"))
|
||||
}
|
||||
interesting.update(
|
||||
{f"body:{key}": request.body[key] for key in ("metadata", "user", "session_id") if key in request.body}
|
||||
)
|
||||
print("\nIDENTITY FIELDS THE REAL CLI SENDS:")
|
||||
print(" body keys:", sorted(request.body))
|
||||
print(" candidate correlators:", interesting or "NONE — per-cell attribution needs another mechanism")
|
||||
|
||||
|
||||
def test_scripted_tools_survive_the_responses_protocol_too() -> None:
|
||||
"""The gateway uses Responses BECAUSE it carries tool use.
|
||||
|
||||
Emitting only output_text there meant a scripted Write or Skill crossed the
|
||||
gateway with the tool dropped, so a mock claiming to serve both protocols
|
||||
was wrong about the one the gateway actually runs.
|
||||
"""
|
||||
|
||||
write = {"name": "Write", "input": {"file_path": "/review-output/review-output.json", "content": "{}"}}
|
||||
with MockProvider([Reply(text="writing", tools=[write])]) as provider:
|
||||
_status, raw = _post(provider.base_url + "/v1/responses", {"model": "m", "input": []})
|
||||
|
||||
output = json.loads(raw)["output"]
|
||||
calls = [item for item in output if item["type"] == "function_call"]
|
||||
assert len(calls) == 1, "the scripted tool must cross the Responses path"
|
||||
assert calls[0]["name"] == "Write"
|
||||
assert json.loads(calls[0]["arguments"])["file_path"] == "/review-output/review-output.json"
|
||||
|
||||
|
||||
def test_an_omitted_cache_field_stays_omitted_on_the_responses_wire_too() -> None:
|
||||
"""Absence must survive both protocols, not just the Anthropic one.
|
||||
|
||||
`_int_or_none` reads an absent detail key as unknown and a present 0 as a
|
||||
measured zero, so serializing 0 for a scripted None would claim a
|
||||
measurement the reply never made.
|
||||
"""
|
||||
|
||||
with MockProvider([Reply(input_tokens=2_000, cache_read_input_tokens=None)]) as provider:
|
||||
_status, raw = _post(provider.base_url + "/v1/responses", {"model": "m", "input": []})
|
||||
|
||||
details = json.loads(raw)["usage"]["input_tokens_details"]
|
||||
assert "cached_tokens" not in details, "an omitted field must not serialize as a measured zero"
|
||||
assert details["cache_write_tokens"] == 0, "a scripted 0 is still a real measurement"
|
||||
195
eval/tests/test_offline_session_integration.py
Normal file
195
eval/tests/test_offline_session_integration.py
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
"""A session end to end with only the model faked.
|
||||
|
||||
The layers between the CLI and the row are where this harness has actually
|
||||
shipped bugs - the artifact that could not be written, the usage that was never
|
||||
recorded, the evidence that was scored from the wrong directory. Every one of
|
||||
them sat below the level its tests exercised. These run the real session path
|
||||
against a scripted provider, so the only thing not real is what the model says.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from workflow_bench.mock_provider import MockProvider, Reply
|
||||
from workflow_bench.proposer_sandbox import (
|
||||
host_workspace_write_boundary,
|
||||
prepare_review_workspace,
|
||||
prepare_sandbox,
|
||||
)
|
||||
from workflow_bench.review_scoring import REVIEW_OUTPUT, parse_review_output
|
||||
from workflow_bench.runner_sessions import run_claude
|
||||
|
||||
FAKE_CLI = Path(__file__).parent / "fixtures" / "fake_claude.py"
|
||||
REVIEW_JSON = '{"schema_version": 1, "verdict": "approve", "findings": []}'
|
||||
|
||||
|
||||
def _session(clone: Path, provider: MockProvider, **overrides):
|
||||
return run_claude(
|
||||
"review the change",
|
||||
clone,
|
||||
claude_bin=str(FAKE_CLI),
|
||||
timeout=60,
|
||||
env={
|
||||
"ANTHROPIC_BASE_URL": provider.base_url,
|
||||
"ANTHROPIC_API_KEY": "offline",
|
||||
"PATH": "/usr/bin:/bin",
|
||||
},
|
||||
**overrides,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def clone(tmp_path: Path) -> Path:
|
||||
workspace = tmp_path / "clone"
|
||||
workspace.mkdir()
|
||||
(workspace / "source.ts").write_text("export const answer = 42;\n")
|
||||
return workspace
|
||||
|
||||
|
||||
def test_a_session_records_the_usage_the_provider_reported(clone: Path) -> None:
|
||||
"""Token counts must survive the CLI boundary, not be invented after it."""
|
||||
|
||||
reply = Reply(input_tokens=2_000, output_tokens=300, cache_read_input_tokens=7_000, cache_creation_input_tokens=1_000)
|
||||
with MockProvider(default=reply) as provider:
|
||||
record = _session(clone, provider)
|
||||
|
||||
assert record["ok"] is True, record.get("error_detail")
|
||||
assert record["input_tokens"] == 2_000
|
||||
assert record["cache_read_input_tokens"] == 7_000
|
||||
assert record["cache_creation_input_tokens"] == 1_000
|
||||
assert record["output_tokens"] == 300
|
||||
# A measured zero would be indistinguishable from an unmeasured one.
|
||||
assert record["cost_usd"] == 0.42
|
||||
assert record["num_turns"] == 1
|
||||
|
||||
|
||||
def test_a_scripted_write_produces_a_review_artifact_the_scorer_accepts(clone: Path) -> None:
|
||||
"""The full artifact path: model asks, CLI writes atomically, scorer reads.
|
||||
|
||||
This is the operation that shipped empty for a whole run. Nothing here
|
||||
fakes the write, the directory, or the parse - only the decision to write.
|
||||
"""
|
||||
|
||||
with prepare_sandbox(
|
||||
clone=clone, claude_bin=Path(sys.executable), backend="host-unsafe", preflight=False
|
||||
) as sandbox:
|
||||
artifact = prepare_review_workspace(sandbox, REVIEW_OUTPUT)
|
||||
write = {"name": "Write", "input": {"file_path": str(artifact), "content": REVIEW_JSON}}
|
||||
with MockProvider(default=Reply(text="reviewing", tools=[write])) as provider:
|
||||
# Take the command configuration from the sandbox the way run_arm
|
||||
# does, rather than calling run_claude bare. On host-unsafe the
|
||||
# prefix is [] by construction, so this pins the WIRING, not the
|
||||
# isolation - a bwrap run would carry a real prefix through here.
|
||||
record = _session(
|
||||
clone,
|
||||
provider,
|
||||
command_prefix=sandbox.command_prefix_for(),
|
||||
require_pid_namespace=sandbox.require_pid_namespace,
|
||||
)
|
||||
assert record["ok"] is True, record.get("error_detail")
|
||||
# Read inside the scope: prepare_sandbox removes the private root on exit.
|
||||
verdict, findings = parse_review_output(artifact)
|
||||
|
||||
assert verdict == "approve"
|
||||
assert findings == ()
|
||||
|
||||
|
||||
def test_the_provider_saw_the_prompt_the_harness_meant_to_send(clone: Path) -> None:
|
||||
"""A run that measures the wrong prompt measures nothing."""
|
||||
|
||||
with MockProvider() as provider:
|
||||
_session(clone, provider)
|
||||
|
||||
assert provider.requests, "the session never reached the provider"
|
||||
sent = provider.requests[0].body["messages"][0]["content"]
|
||||
assert "review the change" in sent
|
||||
|
||||
|
||||
def test_a_provider_failure_surfaces_as_a_failed_session_not_a_silent_pass(clone: Path) -> None:
|
||||
"""An upstream 529 must not be recorded as a usable measurement."""
|
||||
|
||||
failing = Reply(status_code=529, error_body={"error": {"type": "overloaded_error"}})
|
||||
with MockProvider(default=failing) as provider:
|
||||
record = _session(clone, provider)
|
||||
|
||||
assert record["ok"] is False
|
||||
assert record["error_kind"] is not None
|
||||
|
||||
|
||||
def test_the_write_boundary_refuses_the_workspace_and_permits_the_artifact(clone: Path, tmp_path: Path) -> None:
|
||||
"""The contract the empty-artifact run violated, on the backend available here.
|
||||
|
||||
A review must not change the workspace, and must still be able to write its
|
||||
artifact ATOMICALLY - temp file beside the target, then rename - which is
|
||||
what needs a writable parent DIRECTORY rather than a writable file. Both
|
||||
halves are asserted through the real session, with the real boundary
|
||||
applied, and the model scripted to attempt each one.
|
||||
|
||||
Scope: this is the host-unsafe boundary, which its own docstring calls
|
||||
best-effort because a session that can chmod can undo it. The kernel-enforced
|
||||
version is bubblewrap's --ro-bind, which needs namespaces this machine cannot
|
||||
create; that half stays with the real-sandbox canary in CI.
|
||||
"""
|
||||
|
||||
artifacts = tmp_path / "artifacts"
|
||||
artifacts.mkdir()
|
||||
target = artifacts / REVIEW_OUTPUT
|
||||
protected = clone / "source.ts"
|
||||
before = protected.read_text()
|
||||
|
||||
write_artifact = {"name": "Write", "input": {"file_path": str(target), "content": REVIEW_JSON}}
|
||||
tamper = {"name": "Write", "input": {"file_path": str(protected), "content": "tampered"}}
|
||||
|
||||
# No writable= entry: the boundary only governs paths INSIDE the workspace
|
||||
# (it refuses one that escapes), and the artifact directory deliberately
|
||||
# lives outside it - that relocation is the fix for the empty-artifact run.
|
||||
with host_workspace_write_boundary(clone):
|
||||
with MockProvider(default=Reply(text="writing", tools=[write_artifact, tamper])) as provider:
|
||||
record = _session(clone, provider)
|
||||
|
||||
assert record["ok"] is True, record.get("error_detail")
|
||||
# The artifact landed, written the way the agent's Write tool does it.
|
||||
verdict, _findings = parse_review_output(target)
|
||||
assert verdict == "approve"
|
||||
assert not list(artifacts.glob("*.tmp.*")), "the rename landed rather than a copy"
|
||||
# The workspace did not move.
|
||||
assert protected.read_text() == before, "the read-only workspace was modified"
|
||||
|
||||
|
||||
def test_a_reply_missing_cache_usage_is_refused_not_zero_filled(clone: Path) -> None:
|
||||
"""An omitted cache field must not arrive as a measured zero.
|
||||
|
||||
The parent already demands all four USAGE_FIELDS before it calls a session
|
||||
measured (runner_sessions.well_formed). The stand-in used to default the
|
||||
absent ones to 0, which both fabricated a complete measurement AND made
|
||||
that parent guard unfirable from any offline test - it was always
|
||||
satisfied. Scripting the absence is what proves the guard still fires.
|
||||
"""
|
||||
|
||||
partial = Reply(input_tokens=2_000, output_tokens=300, cache_read_input_tokens=None)
|
||||
with MockProvider(default=partial) as provider:
|
||||
record = _session(clone, provider)
|
||||
|
||||
assert record["ok"] is False, "an incomplete usage report is not a usable measurement"
|
||||
assert record["error_kind"] == "session-error"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bad", [-5, True, "1200"], ids=["negative", "boolean", "string"])
|
||||
def test_a_nonsense_cache_value_is_refused_rather_than_forwarded(clone: Path, bad: object) -> None:
|
||||
"""A field good enough to report is good enough to validate.
|
||||
|
||||
The parent's well_formed check tests only that the four keys are PRESENT,
|
||||
so an unvalidated cache value would ride into a success result and be
|
||||
recorded as a real measurement.
|
||||
"""
|
||||
|
||||
reply = Reply(input_tokens=2_000, output_tokens=300)
|
||||
object.__setattr__(reply, "cache_read_input_tokens", bad)
|
||||
with MockProvider(default=reply) as provider:
|
||||
record = _session(clone, provider)
|
||||
|
||||
assert record["ok"] is False, f"{bad!r} must not be recorded as a measured cache value"
|
||||
348
eval/tests/test_offline_sweep_integration.py
Normal file
348
eval/tests/test_offline_sweep_integration.py
Normal file
|
|
@ -0,0 +1,348 @@
|
|||
"""A whole sweep, offline: real runner, real sessions, scripted model.
|
||||
|
||||
The layers between a model turn and a promotion decision had never been
|
||||
exercised together. Unit tests covered each in isolation and the paid runs that
|
||||
would have covered the composition kept dying, so the contracts BETWEEN them
|
||||
went unverified - and that is where this harness has repeatedly shipped bugs.
|
||||
|
||||
This drives runner.main() the way the workflow does. Everything is real: task
|
||||
selection, hidden-oracle capture, the sandbox, the CLI subprocess, artifact
|
||||
capture, review scoring against the oracle, aggregation, the health guard, and
|
||||
the promotion gate. Only the model is scripted, through MockProvider.
|
||||
|
||||
Two provisioning steps are stubbed because this environment cannot supply them,
|
||||
and neither is harness logic: the pinned gitnexus runtime mounts (no
|
||||
node_modules in a worktree) and the sanitized graph build (needs the gitnexus
|
||||
CLI at a mounted path). Containment is host-unsafe here; bubblewrap stays with
|
||||
the real-sandbox canary in the containment job.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import os
|
||||
import shutil
|
||||
|
||||
import pytest
|
||||
|
||||
from workflow_bench import oracle_assets, runner
|
||||
from workflow_bench.mock_provider import MockProvider, Reply
|
||||
|
||||
FAKE_CLI = Path(__file__).parent / "fixtures" / "fake_claude.py"
|
||||
ARMS = ("ce_review", "review", "candidate_review")
|
||||
|
||||
# When set, the sweep runs with NOTHING provisioning-stubbed: real bubblewrap
|
||||
# containment, the real pinned runtime mounts, and the real sanitized graph
|
||||
# build. The named CI job installs all three, so a missing one there is a
|
||||
# regression rather than an unsupported machine - it FAILS instead of quietly
|
||||
# degrading to the stubbed path, which is the whole point of the gate.
|
||||
FULL_SWEEP_ENV = "GITNEXUS_REQUIRE_FULL_SWEEP"
|
||||
FULL_SWEEP = os.environ.get(FULL_SWEEP_ENV) == "1"
|
||||
|
||||
# The runner refuses --unsafe-no-bwrap whenever CI is set, because that mode runs
|
||||
# sessions with bypassPermissions behind a boundary its own docstring calls "not
|
||||
# a security boundary". Deleting CI to get past that refusal would run an
|
||||
# uncontained agent sweep on the runner holding the checkout and credentials, so
|
||||
# the stubbed path is skipped under CI instead. The containment job sets
|
||||
# GITNEXUS_REQUIRE_FULL_SWEEP=1 and takes the real bubblewrap path, so CI keeps
|
||||
# its coverage; only the uncontained convenience run is given up.
|
||||
pytestmark = pytest.mark.skipif(
|
||||
not FULL_SWEEP and bool(os.environ.get("CI")),
|
||||
reason="an uncontained sweep must not run in CI; the containment job runs it with GITNEXUS_REQUIRE_FULL_SWEEP=1",
|
||||
)
|
||||
|
||||
# The review output and the hidden labels are DELIBERATELY different shapes -
|
||||
# the labels carry line_start/line_end and no recommendation. Only a real run
|
||||
# surfaces that; it is why these are written out rather than shared.
|
||||
FINDING = {
|
||||
"id": "f1", "severity": "high", "category": "correctness", "path": "src/sum.js",
|
||||
"line": 1, "end_line": 1, "blocking": True, "scenario": "review-defect",
|
||||
"evidence": "export const total = (a, b) => a - b;", "recommendation": "use a + b",
|
||||
}
|
||||
LABEL = {"id": "f1", "severity": "high", "category": "correctness",
|
||||
"path": "src/sum.js", "line_start": 1, "line_end": 1}
|
||||
SECOND_LABEL = {"id": "f2", "severity": "high", "category": "correctness",
|
||||
"path": "src/scale.js", "line_start": 1, "line_end": 1}
|
||||
SECOND_FINDING = {
|
||||
"id": "f2", "severity": "high", "category": "correctness", "path": "src/scale.js",
|
||||
"line": 1, "end_line": 1, "blocking": True, "scenario": "review-defect",
|
||||
"evidence": "export const twice = (n) => n + 2;", "recommendation": "use n * 2",
|
||||
}
|
||||
|
||||
|
||||
def _git(repo: Path, *args: str) -> str:
|
||||
return subprocess.run(["git", "-C", str(repo), *args], check=True,
|
||||
capture_output=True, text=True).stdout.strip()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bench(tmp_path: Path):
|
||||
"""A self-contained corpus: one repo, one task, one hidden label."""
|
||||
|
||||
repo = tmp_path / "repo"
|
||||
(repo / "src").mkdir(parents=True)
|
||||
(repo / "src" / "sum.js").write_text("export const total = (a, b) => a - b;\n")
|
||||
(repo / "src" / "scale.js").write_text("export const twice = (n) => n + 2;\n")
|
||||
_git(repo, "init", "-q", ".")
|
||||
_git(repo, "config", "user.email", "t@t")
|
||||
_git(repo, "config", "user.name", "t")
|
||||
_git(repo, "add", "-A")
|
||||
_git(repo, "commit", "-q", "-m", "fixture")
|
||||
sha = _git(repo, "rev-parse", "HEAD")
|
||||
|
||||
oracles = tmp_path / "oracles"
|
||||
oracles.mkdir()
|
||||
(oracles / "review-fixture-defect.labels.json").write_text(
|
||||
json.dumps({"schema_version": 1, "findings": [LABEL]})
|
||||
)
|
||||
(oracles / "review-fixture-second.labels.json").write_text(
|
||||
json.dumps({"schema_version": 1, "findings": [SECOND_LABEL]})
|
||||
)
|
||||
|
||||
tasks = tmp_path / "tasks.yaml"
|
||||
tasks.write_text(
|
||||
"tasks:\n"
|
||||
" - id: review-fixture-defect\n"
|
||||
" class: review-defect\n"
|
||||
f" repo: {repo}\n"
|
||||
f" ref: {sha}\n"
|
||||
" prompt: Review this change and report actionable defects.\n"
|
||||
' verify: test -s "$GITNEXUS_BENCH_REVIEW_OUTPUT"\n'
|
||||
" oracle:\n"
|
||||
' command: test -s "$GITNEXUS_BENCH_REVIEW_OUTPUT"\n'
|
||||
" files: [{ source: review-fixture-defect.labels.json, target: review-labels.json }]\n"
|
||||
# A SECOND task, because the thing a cross-task scheduler changes is
|
||||
# invisible with one: waves are per-task, so a single task cannot show
|
||||
# ordering, packing, or a breaker that spans a task boundary.
|
||||
" - id: review-fixture-second\n"
|
||||
" class: review-defect\n"
|
||||
f" repo: {repo}\n"
|
||||
f" ref: {sha}\n"
|
||||
" prompt: Review the scaling helper and report actionable defects.\n"
|
||||
' verify: test -s "$GITNEXUS_BENCH_REVIEW_OUTPUT"\n'
|
||||
" oracle:\n"
|
||||
' command: test -s "$GITNEXUS_BENCH_REVIEW_OUTPUT"\n'
|
||||
" files: [{ source: review-fixture-second.labels.json, target: review-labels.json }]\n"
|
||||
)
|
||||
|
||||
plugin = tmp_path / "ce-plugin"
|
||||
(plugin / ".claude-plugin").mkdir(parents=True)
|
||||
(plugin / ".claude-plugin" / "plugin.json").write_text(
|
||||
json.dumps({"name": "compound-engineering", "version": "0.0.0-fixture"})
|
||||
)
|
||||
for skill in ("ce-plan", "ce-work", "ce-code-review"):
|
||||
directory = plugin / "skills" / skill
|
||||
directory.mkdir(parents=True)
|
||||
(directory / "SKILL.md").write_text(f"---\nname: {skill}\ndescription: fixture\n---\nFixture.\n")
|
||||
|
||||
overlay = tmp_path / "overlay" / ".claude" / "skills" / "gitnexus-review"
|
||||
overlay.mkdir(parents=True)
|
||||
(overlay / "SKILL.md").write_text("---\nname: gitnexus-review\ndescription: fixture\n---\nCandidate.\n")
|
||||
|
||||
return SimpleNamespace(tasks=tasks, oracles=oracles, plugin=plugin,
|
||||
overlay=tmp_path / "overlay", out=tmp_path / "out")
|
||||
|
||||
|
||||
def _stub_provisioning(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Replace what this machine cannot supply - and nothing else.
|
||||
|
||||
Under FULL_SWEEP nothing is replaced: the runtime mounts and the graph are
|
||||
built for real, so the sweep exercises containment and provisioning too.
|
||||
"""
|
||||
|
||||
if FULL_SWEEP:
|
||||
if shutil.which("bwrap") is None:
|
||||
pytest.fail(f"{FULL_SWEEP_ENV}=1 but bubblewrap is absent")
|
||||
return
|
||||
|
||||
monkeypatch.setattr(runner, "trusted_gitnexus_runtime_mounts", lambda: ())
|
||||
|
||||
def materialize(worktree, *, sanitized_head=None, **_kwargs):
|
||||
# The one-clone registry guard reads this before any session runs.
|
||||
meta = Path(worktree) / ".gitnexus"
|
||||
meta.mkdir(parents=True, exist_ok=True)
|
||||
(meta / "meta.json").write_text(
|
||||
json.dumps({"indexedAt": "2026-09-08T00:00:00Z", "lastCommit": sanitized_head or "0" * 40})
|
||||
)
|
||||
|
||||
def fake_graph(**kwargs):
|
||||
kwargs["env"].graph_snapshots[kwargs["graph_key"]] = SimpleNamespace(
|
||||
digest="fixture-graph", manifest_digest="fixture-graph-manifest",
|
||||
dependency_content_digest=None, dependency_manifest_digest=None,
|
||||
materialize=materialize,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(runner, "ensure_task_graph", fake_graph)
|
||||
|
||||
|
||||
def _sweep(bench, monkeypatch: pytest.MonkeyPatch, findings: list[dict], verdict: str, *, invoke_skill: bool = True):
|
||||
"""Run the real CLI against a model scripted to return `findings`."""
|
||||
|
||||
_stub_provisioning(monkeypatch)
|
||||
monkeypatch.setattr(
|
||||
oracle_assets, "ORACLE_ROOT", bench.oracles, raising=False
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
runner, "capture_task_oracles",
|
||||
lambda tasks, root=bench.oracles: oracle_assets.capture_task_oracles(tasks, root=root),
|
||||
)
|
||||
def review_for(body: str) -> str:
|
||||
# Per task: the second task's defect is in another file, so replying
|
||||
# with the first task's finding would score it wrong. A cross-task
|
||||
# scheduler makes which task a request belongs to load-bearing.
|
||||
chosen = findings
|
||||
if findings and "scaling helper" in body:
|
||||
chosen = [SECOND_FINDING if f is FINDING else f for f in findings]
|
||||
return json.dumps({"schema_version": 1, "verdict": verdict, "findings": chosen})
|
||||
|
||||
class Scripted(MockProvider):
|
||||
def next_reply(self) -> Reply:
|
||||
body = json.dumps(self.requests[-1].body if self.requests else {})
|
||||
target = re.search(r"(/[^\s\"']*review-output\.json)", body)
|
||||
skill = re.search(r"\b(gitnexus-review|ce-code-review)\b", body)
|
||||
return Reply(
|
||||
text="reviewing",
|
||||
tools=[
|
||||
# The evidence gate needs a Skill request with a non-error
|
||||
# result: a review that never invoked its skill measured the
|
||||
# model, not the skill.
|
||||
*([{"name": "Skill", "input": {"skill": skill.group(1) if skill else "gitnexus-review"}}]
|
||||
if invoke_skill else []),
|
||||
{"name": "Write", "input": {
|
||||
"file_path": target.group(1) if target else str(bench.out / "unmatched-review-output.json"),
|
||||
"content": review_for(body)}},
|
||||
],
|
||||
input_tokens=2_000, output_tokens=300,
|
||||
cache_read_input_tokens=7_000, cache_creation_input_tokens=1_000,
|
||||
)
|
||||
|
||||
with Scripted() as provider:
|
||||
monkeypatch.setattr(sys, "argv", [
|
||||
"runner", "--tasks", str(bench.tasks), "--arms", *ARMS,
|
||||
"--runs", "1", "--workers", "1", "--out", str(bench.out),
|
||||
"--base-url", provider.base_url, "--anthropic-api-key", "offline",
|
||||
"--claude-bin", str(FAKE_CLI),
|
||||
*([] if FULL_SWEEP else ["--unsafe-no-bwrap"]),
|
||||
"--model", "mock-model",
|
||||
"--ce-plugin-dir", str(bench.plugin), "--ce-plugin-version", "0.0.0-fixture",
|
||||
"--candidate-overlay", str(bench.overlay),
|
||||
])
|
||||
|
||||
try:
|
||||
code = runner.main()
|
||||
except SystemExit as exc:
|
||||
code = exc.code
|
||||
|
||||
rows = [json.loads(line) for line in (bench.out / "results.jsonl").read_text().splitlines()]
|
||||
return code, rows, provider
|
||||
|
||||
|
||||
def _row(rows: list[dict], arm: str, task: str = "review-fixture-defect") -> dict:
|
||||
return next(r for r in rows if r["arm"] == arm and r["task"] == task)
|
||||
|
||||
|
||||
def test_a_correct_review_scores_and_the_sweep_exits_clean(bench, monkeypatch) -> None:
|
||||
"""The whole path, green: every arm measured, scored, and accounted for."""
|
||||
|
||||
code, rows, provider = _sweep(bench, monkeypatch, [FINDING], "request_changes")
|
||||
|
||||
assert code in (None, 0), f"sweep did not succeed: {code}"
|
||||
tasks = {"review-fixture-defect", "review-fixture-second"}
|
||||
assert len(rows) == len(ARMS) * len(tasks)
|
||||
assert len(provider.requests) == len(ARMS) * len(tasks), "each cell must reach the provider once"
|
||||
assert {r["task"] for r in rows} == tasks, "both tasks must have run"
|
||||
|
||||
row = _row(rows, "review")
|
||||
assert row["ok"] is True and row["resolved"] is True
|
||||
assert row["skill_invoked"] is True
|
||||
assert (row["review_true_positives"], row["review_false_positives"], row["review_false_negatives"]) == (1, 0, 0)
|
||||
assert row["review_f1"] == 1.0
|
||||
# The provider's own numbers survived the CLI, the parser and the row.
|
||||
assert row["cache_read_input_tokens"] == 7_000
|
||||
assert row["input_tokens"] == 2_000
|
||||
|
||||
# Each task scored against ITS OWN oracle. This is what a cross-task
|
||||
# scheduler puts at risk: interleaving cells from different tasks means a
|
||||
# mis-routed context or artifact scores one task against another's labels,
|
||||
# and both would still look "green" per row.
|
||||
second = _row(rows, "review", task="review-fixture-second")
|
||||
assert second["resolved"] is True and second["review_f1"] == 1.0
|
||||
assert second["review_artifact"] == "review-fixture-second-review-run0.review.json"
|
||||
|
||||
for name in ("results.jsonl", "report.md", "promotion.json"):
|
||||
assert (bench.out / name).is_file(), f"{name} was not written"
|
||||
assert (bench.out / "review-fixture-defect-review-run0.review.json").is_file()
|
||||
|
||||
|
||||
def test_one_run_cannot_promote_a_candidate(bench, monkeypatch) -> None:
|
||||
"""The gate refuses on insufficient paired runs, and says so."""
|
||||
|
||||
_sweep(bench, monkeypatch, [FINDING], "request_changes")
|
||||
promotion = json.loads((bench.out / "promotion.json").read_text())
|
||||
|
||||
assert promotion["run_status"] == "complete"
|
||||
decision = next(d for d in promotion["decisions"] if d["candidate_arm"] == "candidate_review")
|
||||
assert decision["decision"] == "insufficient_evidence"
|
||||
assert any("valid paired runs" in reason for reason in decision["reasons"])
|
||||
|
||||
|
||||
def test_a_finding_in_the_wrong_place_scores_zero_but_stays_valid_evidence(bench, monkeypatch) -> None:
|
||||
"""Being wrong is a quality result, not a broken measurement.
|
||||
|
||||
The negative control that makes the passing case mean something: same
|
||||
harness, same well-formed artifact, only the answer changed.
|
||||
"""
|
||||
|
||||
wrong = {**FINDING, "path": "src/WRONG.js", "line": 99, "end_line": 99}
|
||||
_code, rows, _provider = _sweep(bench, monkeypatch, [wrong], "request_changes")
|
||||
|
||||
row = _row(rows, "review")
|
||||
assert (row["review_true_positives"], row["review_false_positives"], row["review_false_negatives"]) == (0, 1, 1)
|
||||
assert row["review_f1"] == 0.0
|
||||
assert row["resolved"] is False
|
||||
assert row["error_kind"] == "oracle-failed", "a wrong answer is not a session or evidence failure"
|
||||
assert row["review_evidence_valid"] is True, "the artifact was well formed; only the answer was wrong"
|
||||
|
||||
|
||||
def test_approving_defective_code_is_a_miss_with_no_false_positive(bench, monkeypatch) -> None:
|
||||
"""The other half of the control: silence scores differently from a wrong guess."""
|
||||
|
||||
_code, rows, _provider = _sweep(bench, monkeypatch, [], "approve")
|
||||
|
||||
row = _row(rows, "review")
|
||||
assert (row["review_true_positives"], row["review_false_positives"], row["review_false_negatives"]) == (0, 0, 1)
|
||||
assert row["review_precision"] is None, "precision is undefined with no predictions, not zero"
|
||||
assert row["review_verdict_correct"] is False, "approving defective code is the wrong verdict"
|
||||
assert row["review_evidence_valid"] is True
|
||||
|
||||
|
||||
def test_a_review_that_never_invoked_its_skill_is_not_a_measurement(bench, monkeypatch) -> None:
|
||||
"""The gate that separates measuring a SKILL from measuring a model.
|
||||
|
||||
Added because a mutation exposed it: forcing skill_was_invoked_events to
|
||||
return True left every other test here passing, so nothing pinned the gate.
|
||||
The artifact is written and correct in this run - only the skill request is
|
||||
missing - so a pass would mean the arm scored a review it never performed.
|
||||
"""
|
||||
|
||||
code, rows, _provider = _sweep(bench, monkeypatch, [FINDING], "request_changes", invoke_skill=False)
|
||||
|
||||
row = _row(rows, "review")
|
||||
assert row["skill_invoked"] is False
|
||||
assert row["error_kind"] == "skill-not-invoked"
|
||||
assert code not in (None, 0), "the sweep must not report success on unusable evidence"
|
||||
|
||||
# The row still carries its own score - the artifact was well formed - and
|
||||
# aggregate() DOES count it in the arm's quality median (the KNOWN GAP noted
|
||||
# above aggregate(); test_workflow_bench pins the resulting 0.5). Filtering
|
||||
# it out of the median alone inverted a promotion, because valid_runs and
|
||||
# excluded_runs kept counting it. It counts for cost either way: the session
|
||||
# ran and was billed.
|
||||
assert row["review_weighted_f1"] == 1.0
|
||||
assert row["review_evidence_valid"] is True
|
||||
|
|
@ -24,7 +24,7 @@ from workflow_bench.model_gateway import (
|
|||
)
|
||||
from workflow_bench.provider_usage import (
|
||||
ANTHROPIC,
|
||||
OPENAI_RESPONSES,
|
||||
LITELLM_NORMALIZED,
|
||||
USAGE_ENV_VARS,
|
||||
normalize_usage,
|
||||
)
|
||||
|
|
@ -49,11 +49,16 @@ def _openai_response(usage: dict) -> SimpleNamespace:
|
|||
)
|
||||
|
||||
|
||||
# The shape a callback actually receives: LiteLLM normalises usage into its own
|
||||
# Chat-Completions-style object before any logger sees it, so an OpenAI reply
|
||||
# arrives as prompt_tokens / prompt_tokens_details. Confirmed against a real
|
||||
# proxy in tests/test_mock_provider.py; a fixture in the wire shape would test
|
||||
# an object this code path never gets.
|
||||
NATIVE = {
|
||||
"input_tokens": 48_000,
|
||||
"input_tokens_details": {"cached_tokens": 44_000, "cache_write_tokens": 1_000},
|
||||
"output_tokens": 900,
|
||||
"output_tokens_details": {"reasoning_tokens": 640},
|
||||
"prompt_tokens": 48_000,
|
||||
"prompt_tokens_details": {"cached_tokens": 44_000, "cache_write_tokens": 1_000},
|
||||
"completion_tokens": 900,
|
||||
"completion_tokens_details": {"reasoning_tokens": 640},
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -79,9 +84,9 @@ def test_native_openai_usage_survives_the_anthropic_translation(logged) -> None:
|
|||
event = logged(NATIVE)
|
||||
native = event["native_usage"]
|
||||
# Verbatim: the fields an Anthropic-shaped response cannot carry.
|
||||
assert native["input_tokens_details"]["cached_tokens"] == 44_000
|
||||
assert native["input_tokens_details"]["cache_write_tokens"] == 1_000
|
||||
assert native["output_tokens_details"]["reasoning_tokens"] == 640
|
||||
assert native["prompt_tokens_details"]["cached_tokens"] == 44_000
|
||||
assert native["prompt_tokens_details"]["cache_write_tokens"] == 1_000
|
||||
assert native["completion_tokens_details"]["reasoning_tokens"] == 640
|
||||
assert event["response_id"] == "resp_68f2c1"
|
||||
|
||||
|
||||
|
|
@ -100,7 +105,10 @@ def test_the_captured_event_normalizes_with_openai_arithmetic(logged) -> None:
|
|||
event = logged(NATIVE)
|
||||
# The provider the LOG recorded, not one the test supplies - passing
|
||||
# OPENAI_RESPONSES by hand here is what hid the adapter-key mismatch.
|
||||
assert event["provider"] == OPENAI_RESPONSES
|
||||
# LITELLM_NORMALIZED, not OPENAI_RESPONSES: a proxy callback never sees the
|
||||
# upstream body. Measured against a real gateway - the Responses adapter
|
||||
# found none of its keys there and reported every field unknown.
|
||||
assert event["provider"] == LITELLM_NORMALIZED
|
||||
assert event["provider_label"] == "openai"
|
||||
usage = normalize_usage(event["provider"], event["native_usage"])
|
||||
assert usage.total_input_tokens == 48_000
|
||||
|
|
@ -112,7 +120,7 @@ def test_the_captured_event_normalizes_with_openai_arithmetic(logged) -> None:
|
|||
def test_usage_without_details_normalizes_to_unknown_rather_than_zero(logged) -> None:
|
||||
"""The mutation the accounting must not survive: dropped details, silent zeros."""
|
||||
|
||||
stripped = {k: v for k, v in NATIVE.items() if k != "input_tokens_details"}
|
||||
stripped = {k: v for k, v in NATIVE.items() if k != "prompt_tokens_details"}
|
||||
event = logged(stripped)
|
||||
usage = normalize_usage(event["provider"], event["native_usage"])
|
||||
assert usage.cache_read_input_tokens is None
|
||||
|
|
@ -238,10 +246,16 @@ def test_an_unresolvable_provider_is_refused_rather_than_guessed() -> None:
|
|||
|
||||
from workflow_bench.provider_usage import canonical_provider
|
||||
|
||||
assert canonical_provider("openai", "responses") == OPENAI_RESPONSES
|
||||
assert canonical_provider("openai", "completion") is None
|
||||
assert canonical_provider("openai", None) is None
|
||||
# Every openai call reaching this callback has already been normalised by
|
||||
# LiteLLM, whatever endpoint it used - the observed call_type for a Claude
|
||||
# Code request through the gateway is "anthropic_messages". The adapter has
|
||||
# to match the object in hand, not the protocol on the wire.
|
||||
assert canonical_provider("openai", "responses") == LITELLM_NORMALIZED
|
||||
assert canonical_provider("openai", "anthropic_messages") == LITELLM_NORMALIZED
|
||||
assert canonical_provider("anthropic", "completion") == ANTHROPIC
|
||||
# An unrecognised provider is still refused rather than guessed.
|
||||
assert canonical_provider("some-new-provider", "responses") is None
|
||||
assert canonical_provider(None, None) is None
|
||||
|
||||
|
||||
def test_request_identity_cannot_come_from_the_proxy_environment() -> None:
|
||||
|
|
|
|||
|
|
@ -195,6 +195,11 @@ def test_eval_ci_uses_locked_uv_and_blocking_native_containment_jobs():
|
|||
assert containment["env"] == {
|
||||
"GITNEXUS_REQUIRE_BWRAP_CANARY": "1",
|
||||
"GITNEXUS_REQUIRE_CLAUDE_CANARY": "1",
|
||||
# This job is the only place with bubblewrap, the pinned runtime and a
|
||||
# built GitNexus together, so it is where the offline sweep runs with
|
||||
# nothing provisioning-stubbed. Pinned here so the gate cannot be
|
||||
# dropped and leave the sweep silently running the stubbed path.
|
||||
"GITNEXUS_REQUIRE_FULL_SWEEP": "1",
|
||||
}
|
||||
assert containment["timeout-minutes"] == 20
|
||||
assert containment_node_setup["with"] == {
|
||||
|
|
@ -239,6 +244,12 @@ def test_eval_ci_uses_locked_uv_and_blocking_native_containment_jobs():
|
|||
"tests/test_proposer_sandbox.py",
|
||||
"tests/test_workflow_bench_sessions.py",
|
||||
"tests/test_ce_plugin_runtime.py",
|
||||
# The offline sweep, run here with nothing stubbed: this job is the only
|
||||
# one carrying bubblewrap, the pinned runtime and a built GitNexus.
|
||||
"tests/test_offline_sweep_integration.py",
|
||||
# Carries the real-CLI identity probe, which needs CLAUDE_CANARY_BIN -
|
||||
# set only on this job. Omitted from this list it skipped everywhere.
|
||||
"tests/test_mock_provider.py",
|
||||
"-q",
|
||||
]
|
||||
bwrap_canary_marker = re.compile(
|
||||
|
|
@ -1052,3 +1063,33 @@ def test_a_raising_packed_cell_still_persists_its_settled_siblings():
|
|||
)
|
||||
|
||||
assert (1, "review") in folded, "the sibling that completed was never recorded"
|
||||
|
||||
|
||||
def test_an_uninvoked_skill_still_counts_toward_the_arm_median():
|
||||
"""Pins a KNOWN GAP, not a desired behaviour.
|
||||
|
||||
A cell whose skill never ran still moves the arm's quality median, even
|
||||
though an arm exists to measure a SKILL. The narrow fix - filtering those
|
||||
rows out of the quality metrics - is worse than the gap: valid_runs and
|
||||
excluded_runs keep counting them, so the promotion gate sees N clean runs
|
||||
while the median came from fewer. Since the dropped rows are systematically
|
||||
an arm's worst, that biases toward promoting, and it was measured flipping
|
||||
keep_incumbent to promote.
|
||||
|
||||
Closing it honestly needs a scored-run count and a paired-equality check in
|
||||
the promotion gate. Pinned here so the half-fix cannot be reapplied without
|
||||
someone reading why it was reverted.
|
||||
"""
|
||||
|
||||
good = record(review_weighted_f1=1.0, cost_usd=2.0)
|
||||
uninvoked = record(
|
||||
review_weighted_f1=0.0, cost_usd=4.0, error_kind="skill-not-invoked", skill_invoked=False
|
||||
)
|
||||
agg = aggregate([good, uninvoked])
|
||||
|
||||
assert agg["review_weighted_f1"] == 0.5, "the uninvoked row is counted - the known gap"
|
||||
assert agg["cost_usd"] == 3.0
|
||||
# The invariant that makes the half-fix unsafe: the median and the run count
|
||||
# the gate reads must cover the same rows.
|
||||
assert agg["valid_runs"] == 2
|
||||
|
||||
|
|
|
|||
|
|
@ -42,8 +42,8 @@ def canonical_provider(label, call_type): # noqa: ANN001, ANN201
|
|||
above for why this is a copy rather than an import.
|
||||
"""
|
||||
|
||||
if label == "openai" and call_type and "responses" in call_type:
|
||||
return "openai-responses"
|
||||
if label == "openai":
|
||||
return "litellm-normalized"
|
||||
if label == "anthropic":
|
||||
return "anthropic"
|
||||
return None
|
||||
|
|
@ -104,11 +104,10 @@ class ProviderUsageLogger(CustomLogger):
|
|||
"requested_model": kwargs.get("model"),
|
||||
"actual_model": getattr(response_obj, "model", None),
|
||||
# Two fields, because they answer different questions. The raw
|
||||
# label is what LiteLLM said; "provider" is the adapter key,
|
||||
# which needs the call type too - LiteLLM reports "openai" for
|
||||
# both Chat Completions and Responses and those report usage
|
||||
# differently. Unresolvable stays None so normalize_usage
|
||||
# refuses rather than guessing token semantics.
|
||||
# label is what LiteLLM said; "provider" is the adapter key for
|
||||
# the object actually in hand, which is always LiteLLM's own
|
||||
# normalised shape here. An unrecognised label stays None so
|
||||
# normalize_usage refuses rather than guessing token semantics.
|
||||
"provider_label": provider_label,
|
||||
"provider": canonical_provider(provider_label, call_type),
|
||||
"response_id": getattr(response_obj, "id", None),
|
||||
|
|
|
|||
284
eval/workflow_bench/mock_provider.py
Normal file
284
eval/workflow_bench/mock_provider.py
Normal file
|
|
@ -0,0 +1,284 @@
|
|||
"""A scriptable stand-in for Anthropic and OpenAI, for running the harness offline.
|
||||
|
||||
Every defect this benchmark shipped in the last round was invisible to its own
|
||||
tests for the same reason: the tests exercised a layer BELOW where the code
|
||||
runs. The usage log was never written because the proxy is a subprocess with a
|
||||
constructed environment. The callback could not be imported because LiteLLM
|
||||
loads it by path. Failures went unrecorded because only the async hook was
|
||||
overridden. Each was caught by CI or review, never by a unit test, because the
|
||||
unit test called the function directly instead of driving the path that calls
|
||||
it.
|
||||
|
||||
This closes that gap without spending money. It speaks the two wire protocols
|
||||
the harness actually depends on, so a run can go through the real sandbox, the
|
||||
real Claude Code CLI, the real gateway and the real usage callback, and only
|
||||
the model is fake:
|
||||
|
||||
POST /v1/messages Anthropic Messages, streaming and non-streaming
|
||||
POST /v1/responses OpenAI Responses, which the gateway translates into
|
||||
|
||||
Point the runner at it with ``--base-url http://127.0.0.1:<port>``, which is
|
||||
the same supported path the free-model proxy documentation already uses, or
|
||||
give it to LiteLLM as ``api_base`` to exercise the gateway.
|
||||
|
||||
Scripted, not simulated: replies are supplied by the caller, so a test decides
|
||||
what the model "says", which tools it asks for, and exactly what usage it
|
||||
reports. That last part is what makes provider-native accounting testable at
|
||||
all - real cache hits are not reproducible on demand, but a declared
|
||||
``cache_read`` of 44_000 is.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
from collections import deque
|
||||
from dataclasses import dataclass, field
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass
|
||||
class Reply:
|
||||
"""One scripted model turn.
|
||||
|
||||
``tools`` drives real tool execution: Claude Code runs what it is asked to
|
||||
run, so a reply carrying a Write block makes the CLI write that file inside
|
||||
the sandbox for real. That is how an artifact-producing cell can be
|
||||
exercised without a model deciding anything.
|
||||
"""
|
||||
|
||||
text: str = "ok"
|
||||
tools: list[dict[str, Any]] = field(default_factory=list)
|
||||
stop_reason: str = "end_turn"
|
||||
# Anthropic accounting: input_tokens is the UNCACHED remainder and the
|
||||
# cache fields add to it. Defaults are deliberately non-zero so a test that
|
||||
# forgets to script usage still cannot mistake silence for a measurement.
|
||||
input_tokens: int = 11
|
||||
output_tokens: int = 7
|
||||
# None means the field is OMITTED from the reply, which is not the same as
|
||||
# reporting 0. A consumer that cannot tell those apart is the bug this
|
||||
# harness exists to catch, so the mock has to be able to script absence.
|
||||
cache_read_input_tokens: int | None = 0
|
||||
cache_creation_input_tokens: int | None = 0
|
||||
status_code: int = 200
|
||||
error_body: dict[str, Any] | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class Request:
|
||||
"""What the harness actually sent, kept so a test can assert on it."""
|
||||
|
||||
path: str
|
||||
headers: dict[str, str]
|
||||
body: dict[str, Any]
|
||||
|
||||
|
||||
class _Handler(BaseHTTPRequestHandler):
|
||||
provider: MockProvider
|
||||
|
||||
def log_message(self, *_args: Any) -> None: # noqa: A003 - silence the default stderr spam
|
||||
return
|
||||
|
||||
def do_POST(self) -> None: # noqa: N802 - BaseHTTPRequestHandler's interface
|
||||
length = int(self.headers.get("Content-Length") or 0)
|
||||
raw = self.rfile.read(length) if length else b"{}"
|
||||
try:
|
||||
body = json.loads(raw or b"{}")
|
||||
except json.JSONDecodeError:
|
||||
body = {"_unparsed": raw.decode("utf-8", "replace")}
|
||||
self.provider.record(Request(self.path, dict(self.headers), body))
|
||||
reply = self.provider.next_reply()
|
||||
|
||||
if reply.status_code != 200:
|
||||
self._send_json(reply.status_code, reply.error_body or {"error": {"message": "scripted failure"}})
|
||||
return
|
||||
if self.path.rstrip("/").endswith("/responses"):
|
||||
self._send_json(200, _openai_response(reply))
|
||||
return
|
||||
if body.get("stream"):
|
||||
self._send_anthropic_stream(reply)
|
||||
return
|
||||
self._send_json(200, _anthropic_message(reply))
|
||||
|
||||
def _send_json(self, status: int, payload: dict[str, Any]) -> None:
|
||||
encoded = json.dumps(payload).encode()
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(encoded)))
|
||||
self.end_headers()
|
||||
self.wfile.write(encoded)
|
||||
|
||||
def _send_anthropic_stream(self, reply: Reply) -> None:
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/event-stream")
|
||||
self.send_header("Cache-Control", "no-cache")
|
||||
self.end_headers()
|
||||
for event, data in _anthropic_stream_events(reply):
|
||||
self.wfile.write(f"event: {event}\ndata: {json.dumps(data)}\n\n".encode())
|
||||
self.wfile.flush()
|
||||
|
||||
|
||||
def _content_blocks(reply: Reply) -> list[dict[str, Any]]:
|
||||
blocks: list[dict[str, Any]] = [{"type": "text", "text": reply.text}]
|
||||
for index, tool in enumerate(reply.tools):
|
||||
blocks.append(
|
||||
{
|
||||
"type": "tool_use",
|
||||
"id": f"toolu_mock_{index}",
|
||||
"name": tool["name"],
|
||||
"input": tool.get("input", {}),
|
||||
}
|
||||
)
|
||||
return blocks
|
||||
|
||||
|
||||
def _anthropic_usage(reply: Reply) -> dict[str, int]:
|
||||
usage = {
|
||||
"input_tokens": reply.input_tokens,
|
||||
"output_tokens": reply.output_tokens,
|
||||
"cache_read_input_tokens": reply.cache_read_input_tokens,
|
||||
"cache_creation_input_tokens": reply.cache_creation_input_tokens,
|
||||
}
|
||||
return {field: value for field, value in usage.items() if value is not None}
|
||||
|
||||
|
||||
def _anthropic_message(reply: Reply) -> dict[str, Any]:
|
||||
return {
|
||||
"id": "msg_mock",
|
||||
"type": "message",
|
||||
"role": "assistant",
|
||||
"model": "mock-model",
|
||||
"content": _content_blocks(reply),
|
||||
"stop_reason": "tool_use" if reply.tools else reply.stop_reason,
|
||||
"stop_sequence": None,
|
||||
"usage": _anthropic_usage(reply),
|
||||
}
|
||||
|
||||
|
||||
def _anthropic_stream_events(reply: Reply) -> list[tuple[str, dict[str, Any]]]:
|
||||
"""The SSE sequence a Messages consumer expects, in order."""
|
||||
|
||||
message = _anthropic_message(reply)
|
||||
events: list[tuple[str, dict[str, Any]]] = [
|
||||
("message_start", {"type": "message_start", "message": {**message, "content": [], "usage": _anthropic_usage(reply)}})
|
||||
]
|
||||
for index, block in enumerate(message["content"]):
|
||||
if block["type"] == "text":
|
||||
events.append(("content_block_start", {"type": "content_block_start", "index": index, "content_block": {"type": "text", "text": ""}}))
|
||||
events.append(("content_block_delta", {"type": "content_block_delta", "index": index, "delta": {"type": "text_delta", "text": block["text"]}}))
|
||||
else:
|
||||
events.append(("content_block_start", {"type": "content_block_start", "index": index, "content_block": {"type": "tool_use", "id": block["id"], "name": block["name"], "input": {}}}))
|
||||
events.append(("content_block_delta", {"type": "content_block_delta", "index": index, "delta": {"type": "input_json_delta", "partial_json": json.dumps(block["input"])}}))
|
||||
events.append(("content_block_stop", {"type": "content_block_stop", "index": index}))
|
||||
events.append(("message_delta", {"type": "message_delta", "delta": {"stop_reason": message["stop_reason"], "stop_sequence": None}, "usage": {"output_tokens": reply.output_tokens}}))
|
||||
events.append(("message_stop", {"type": "message_stop"}))
|
||||
return events
|
||||
|
||||
|
||||
def _openai_response(reply: Reply) -> dict[str, Any]:
|
||||
"""OpenAI Responses shape: input_tokens is the WHOLE, cache fields subsets."""
|
||||
|
||||
# An omitted cache field contributes nothing to the Responses total; that
|
||||
# is arithmetic, not a claim the value was measured as zero.
|
||||
cache_read = reply.cache_read_input_tokens or 0
|
||||
cache_write = reply.cache_creation_input_tokens or 0
|
||||
total_input = reply.input_tokens + cache_read + cache_write
|
||||
return {
|
||||
"id": "resp_mock",
|
||||
"object": "response",
|
||||
"created_at": int(time.time()),
|
||||
"status": "completed",
|
||||
"model": "mock-model",
|
||||
"error": None,
|
||||
"output": [
|
||||
{
|
||||
"id": "msg_mock",
|
||||
"type": "message",
|
||||
"role": "assistant",
|
||||
"status": "completed",
|
||||
"content": [{"type": "output_text", "text": reply.text, "annotations": []}],
|
||||
},
|
||||
# Tool calls belong here too. Responses is the protocol the gateway
|
||||
# is configured for BECAUSE it carries tool use, so emitting only
|
||||
# output_text meant a reply scripted with a Write or Skill crossed
|
||||
# the gateway with the tool silently dropped - the mock would have
|
||||
# been wrong about the wire on the one path that matters most.
|
||||
*(
|
||||
{
|
||||
"id": f"fc_mock_{index}",
|
||||
"type": "function_call",
|
||||
"status": "completed",
|
||||
"call_id": f"call_mock_{index}",
|
||||
"name": tool["name"],
|
||||
"arguments": json.dumps(tool.get("input", {})),
|
||||
}
|
||||
for index, tool in enumerate(reply.tools)
|
||||
),
|
||||
],
|
||||
"usage": {
|
||||
"input_tokens": total_input,
|
||||
"output_tokens": reply.output_tokens,
|
||||
"total_tokens": total_input + reply.output_tokens,
|
||||
# Omitted stays omitted here too. Collapsing None to 0 is right for
|
||||
# the total above (an unreported field adds nothing) but wrong on
|
||||
# the wire: _int_or_none reads an absent key as unknown and a
|
||||
# present 0 as a measured zero, so serializing 0 would claim a
|
||||
# measurement the reply never made - the same confusion the
|
||||
# Anthropic path already refuses.
|
||||
"input_tokens_details": {
|
||||
**({"cached_tokens": cache_read} if reply.cache_read_input_tokens is not None else {}),
|
||||
**({"cache_write_tokens": cache_write} if reply.cache_creation_input_tokens is not None else {}),
|
||||
},
|
||||
"output_tokens_details": {"reasoning_tokens": 0},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class MockProvider:
|
||||
"""Loopback-only provider stand-in. Use as a context manager."""
|
||||
|
||||
def __init__(self, replies: list[Reply] | None = None, *, default: Reply | None = None) -> None:
|
||||
self._replies: deque[Reply] = deque(replies or [])
|
||||
# A run makes more requests than a test wants to script; the default
|
||||
# keeps it going rather than failing on the first unscripted turn.
|
||||
self._default = default or Reply()
|
||||
self._requests: list[Request] = []
|
||||
self._lock = threading.Lock()
|
||||
self._server: ThreadingHTTPServer | None = None
|
||||
|
||||
def __enter__(self) -> MockProvider:
|
||||
handler = type("_BoundHandler", (_Handler,), {"provider": self})
|
||||
# Loopback only: this answers with no authentication at all.
|
||||
self._server = ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=self._server.serve_forever, daemon=True).start()
|
||||
return self
|
||||
|
||||
def __exit__(self, *_exc: object) -> bool:
|
||||
if self._server is not None:
|
||||
self._server.shutdown()
|
||||
self._server.server_close()
|
||||
return False
|
||||
|
||||
@property
|
||||
def port(self) -> int:
|
||||
assert self._server is not None, "provider is not running"
|
||||
return self._server.server_port
|
||||
|
||||
@property
|
||||
def base_url(self) -> str:
|
||||
return f"http://127.0.0.1:{self.port}"
|
||||
|
||||
def record(self, request: Request) -> None:
|
||||
with self._lock:
|
||||
self._requests.append(request)
|
||||
|
||||
def next_reply(self) -> Reply:
|
||||
with self._lock:
|
||||
return self._replies.popleft() if self._replies else self._default
|
||||
|
||||
@property
|
||||
def requests(self) -> list[Request]:
|
||||
with self._lock:
|
||||
return list(self._requests)
|
||||
|
|
@ -50,6 +50,14 @@ USAGE_ENV_VARS = (USAGE_LOG_ENV_VAR, SWEEP_ID_ENV_VAR)
|
|||
|
||||
ANTHROPIC = "anthropic"
|
||||
OPENAI_RESPONSES = "openai-responses"
|
||||
# What the gateway's callback actually receives. LiteLLM does not hand a logger
|
||||
# the upstream body: it normalises usage into its own Chat-Completions-shaped
|
||||
# object first, so an OpenAI Responses reply arrives as prompt_tokens /
|
||||
# prompt_tokens_details even though the wire carried input_tokens /
|
||||
# input_tokens_details. Measured against a real proxy, not assumed - the
|
||||
# Responses adapter below found none of its keys and reported every field
|
||||
# unknown. The arithmetic is still OpenAI's (the whole, with subsets).
|
||||
LITELLM_NORMALIZED = "litellm-normalized"
|
||||
|
||||
|
||||
class UsageSemanticsError(ValueError):
|
||||
|
|
@ -131,6 +139,35 @@ def _normalize_openai_responses(usage: Mapping[str, Any]) -> NormalizedUsage:
|
|||
)
|
||||
|
||||
|
||||
def _normalize_litellm(usage: Mapping[str, Any]) -> NormalizedUsage:
|
||||
"""prompt_tokens is the WHOLE; the details are subsets of it."""
|
||||
|
||||
total = _int_or_none(usage, "prompt_tokens")
|
||||
details = usage.get("prompt_tokens_details")
|
||||
cache_read = _int_or_none(details, "cached_tokens")
|
||||
cache_write = _int_or_none(details, "cache_write_tokens")
|
||||
if cache_write is None:
|
||||
cache_write = _int_or_none(details, "cache_creation_tokens")
|
||||
output_details = usage.get("completion_tokens_details")
|
||||
|
||||
ordinary: int | None = None
|
||||
if total is not None and cache_read is not None and cache_write is not None:
|
||||
ordinary = total - cache_read - cache_write
|
||||
if ordinary < 0:
|
||||
raise UsageSemanticsError(
|
||||
f"LiteLLM cached ({cache_read}) + cache_write ({cache_write}) "
|
||||
f"exceed prompt_tokens ({total})"
|
||||
)
|
||||
return NormalizedUsage(
|
||||
ordinary_input_tokens=ordinary,
|
||||
cache_read_input_tokens=cache_read,
|
||||
cache_write_input_tokens=cache_write,
|
||||
total_input_tokens=total,
|
||||
output_tokens=_int_or_none(usage, "completion_tokens"),
|
||||
reasoning_output_tokens=_int_or_none(output_details, "reasoning_tokens"),
|
||||
)
|
||||
|
||||
|
||||
def _normalize_anthropic(usage: Mapping[str, Any]) -> NormalizedUsage:
|
||||
"""input_tokens is the uncached REMAINDER; the cache fields add to it."""
|
||||
|
||||
|
|
@ -154,16 +191,24 @@ def _normalize_anthropic(usage: Mapping[str, Any]) -> NormalizedUsage:
|
|||
def canonical_provider(label: str | None, call_type: str | None) -> str | None:
|
||||
"""Map LiteLLM's provider label onto an adapter key, or None if unsure.
|
||||
|
||||
LiteLLM reports ``custom_llm_provider`` as "openai" for both Chat
|
||||
Completions and Responses, and those two report usage differently, so the
|
||||
label alone cannot pick an adapter. The call type is what distinguishes
|
||||
them. Returning None when it does not is deliberate: normalize_usage
|
||||
refuses an unknown provider rather than guessing token semantics, which is
|
||||
the whole point of keeping the native object authoritative.
|
||||
Every "openai" label maps to LITELLM_NORMALIZED regardless of call type,
|
||||
because anything reaching a proxy callback has already been normalised by
|
||||
LiteLLM into its own object - measured against a real gateway, where the
|
||||
observed call type is "anthropic_messages" and the upstream Responses shape
|
||||
never arrives. OPENAI_RESPONSES stays in the adapter table for a RAW
|
||||
upstream body, which only direct callers and the wire-shape tests pass.
|
||||
|
||||
An unrecognised label still returns None, so normalize_usage refuses rather
|
||||
than guessing token semantics.
|
||||
"""
|
||||
|
||||
if label == "openai" and call_type and "responses" in call_type:
|
||||
return OPENAI_RESPONSES
|
||||
if label == "openai":
|
||||
# Anything reaching a proxy callback has already been normalised by
|
||||
# LiteLLM, whichever endpoint the caller used - the observed call_type
|
||||
# for a Claude Code request through this gateway is "anthropic_messages",
|
||||
# not a Responses one. The adapter has to match the object in hand, not
|
||||
# the protocol on the wire.
|
||||
return LITELLM_NORMALIZED
|
||||
if label in _ADAPTERS:
|
||||
return label
|
||||
return None
|
||||
|
|
@ -172,6 +217,7 @@ def canonical_provider(label: str | None, call_type: str | None) -> str | None:
|
|||
_ADAPTERS = {
|
||||
ANTHROPIC: _normalize_anthropic,
|
||||
OPENAI_RESPONSES: _normalize_openai_responses,
|
||||
LITELLM_NORMALIZED: _normalize_litellm,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1591,6 +1591,21 @@ def aggregate(records: list[dict[str, Any]]) -> dict[str, Any]:
|
|||
"review_category_accuracy",
|
||||
"review_grounded_evidence",
|
||||
)
|
||||
# NOTE: a skill-not-invoked row still contributes to these medians. That is
|
||||
# a real measurement gap - an arm exists to measure a SKILL, and a cell
|
||||
# where the skill never ran did not measure it - but the narrow fix is
|
||||
# WORSE than the gap, so it is deliberately not applied here.
|
||||
#
|
||||
# Filtering those rows out of the quality metrics alone leaves valid_runs
|
||||
# and excluded_runs counting them, so the promotion gate sees N clean runs
|
||||
# while the median was taken over fewer. Because the dropped rows are
|
||||
# systematically an arm's worst, that biases toward PROMOTING: measured on
|
||||
# one real run at 0.9 plus two uninvoked rows at 0.0, the gate flipped from
|
||||
# keep_incumbent to promote. The three verdict fields below compound it -
|
||||
# they are all() reducers, so one uninvoked cell flips a whole arm.
|
||||
# Closing this honestly needs a scored-run count and a paired-equality
|
||||
# check in the gate itself: a promotion-semantics change, not an
|
||||
# aggregation fix.
|
||||
if any("review_weighted_f1" in record for record in valid):
|
||||
for metric in review_metrics:
|
||||
values = [record[metric] for record in valid if record.get(metric) is not None]
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ export type NodeLabel =
|
|||
| 'Folder'
|
||||
| 'File'
|
||||
| 'Class'
|
||||
| 'Protocol'
|
||||
| 'Category'
|
||||
| 'Function'
|
||||
| 'Method'
|
||||
| 'Variable'
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ const EXTENSION_MAP: Record<SupportedLanguages, readonly string[]> = {
|
|||
[SupportedLanguages.Python]: ['.py'],
|
||||
[SupportedLanguages.Java]: ['.java'],
|
||||
[SupportedLanguages.C]: ['.c'],
|
||||
[SupportedLanguages.ObjectiveC]: ['.m', '.mm'],
|
||||
[SupportedLanguages.CPlusPlus]: [
|
||||
'.cpp',
|
||||
'.cc',
|
||||
|
|
@ -111,6 +112,7 @@ const SYNTAX_MAP: Record<SupportedLanguages, string> = {
|
|||
[SupportedLanguages.Python]: 'python',
|
||||
[SupportedLanguages.Java]: 'java',
|
||||
[SupportedLanguages.C]: 'c',
|
||||
[SupportedLanguages.ObjectiveC]: 'objectivec',
|
||||
[SupportedLanguages.CPlusPlus]: 'cpp',
|
||||
[SupportedLanguages.CSharp]: 'csharp',
|
||||
[SupportedLanguages.Go]: 'go',
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ export enum SupportedLanguages {
|
|||
Java = 'java',
|
||||
C = 'c',
|
||||
CPlusPlus = 'cpp',
|
||||
ObjectiveC = 'objective-c',
|
||||
CSharp = 'csharp',
|
||||
Go = 'go',
|
||||
Ruby = 'ruby',
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ export const NODE_TABLES = [
|
|||
'Folder',
|
||||
'Function',
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'Method',
|
||||
'CodeElement',
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@
|
|||
* Initial classification (locked in Ring 1 #910):
|
||||
* - production: javascript, typescript, python, java, c, cpp, csharp, go,
|
||||
* ruby, rust, php, kotlin, swift, dart
|
||||
* - experimental: vue (embedded-language / SFC complexity),
|
||||
* - experimental: objective-c (fork provider MVP),
|
||||
* vue (embedded-language / SFC complexity),
|
||||
* cobol (regex-provider path)
|
||||
* - quarantined: (none)
|
||||
*
|
||||
|
|
@ -34,6 +35,7 @@ export const LanguageClassifications: Readonly<Record<SupportedLanguages, Langua
|
|||
[SupportedLanguages.Java]: 'production',
|
||||
[SupportedLanguages.C]: 'production',
|
||||
[SupportedLanguages.CPlusPlus]: 'production',
|
||||
[SupportedLanguages.ObjectiveC]: 'experimental',
|
||||
[SupportedLanguages.CSharp]: 'production',
|
||||
[SupportedLanguages.Go]: 'production',
|
||||
[SupportedLanguages.Ruby]: 'production',
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@
|
|||
*
|
||||
* Thin wrapper over `lookupCore`, specialized for class kinds:
|
||||
*
|
||||
* - `acceptedKinds` = Class / Interface / Enum / Struct / Union /
|
||||
* Trait / TypeAlias / Typedef / Record / Delegate / Annotation /
|
||||
* Template / Namespace.
|
||||
* - `acceptedKinds` = Class / Protocol / Category / Interface / Enum /
|
||||
* Struct / Union / Trait / TypeAlias / Typedef / Record / Delegate /
|
||||
* Annotation / Template / Namespace.
|
||||
* - `useReceiverTypeBinding` is **false** — classes are resolved by
|
||||
* name through the lexical chain + global qualified fallback, not
|
||||
* via a receiver type.
|
||||
|
|
|
|||
|
|
@ -136,6 +136,8 @@ export interface RegistryContext {
|
|||
|
||||
export const CLASS_KINDS: readonly NodeLabel[] = Object.freeze([
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'Enum',
|
||||
'Struct',
|
||||
|
|
|
|||
|
|
@ -82,6 +82,8 @@ const STRICT_ORIGINS: ReadonlySet<BindingRef['origin']> = new Set<BindingRef['or
|
|||
*/
|
||||
const TYPE_KINDS: ReadonlySet<NodeLabel> = new Set<NodeLabel>([
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'Enum',
|
||||
'Struct',
|
||||
|
|
|
|||
|
|
@ -186,6 +186,10 @@ const getNodeTypeIcon = (label: NodeLabel) => {
|
|||
return FileCode;
|
||||
case 'Class':
|
||||
return Box;
|
||||
case 'Protocol':
|
||||
return Hash;
|
||||
case 'Category':
|
||||
return Box;
|
||||
case 'Function':
|
||||
return Braces;
|
||||
case 'Method':
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ export const NODE_COLORS: Record<NodeLabel, string> = {
|
|||
Folder: '#6366f1', // Indigo
|
||||
File: '#3b82f6', // Blue
|
||||
Class: '#f59e0b', // Amber - stands out
|
||||
Protocol: '#ec4899', // Pink - like Interface
|
||||
Category: '#14b8a6', // Teal - like Method
|
||||
Function: '#10b981', // Emerald
|
||||
Method: '#14b8a6', // Teal
|
||||
Variable: '#64748b', // Slate - muted (less important)
|
||||
|
|
@ -51,6 +53,8 @@ export const NODE_SIZES: Record<NodeLabel, number> = {
|
|||
Folder: 10, // Structural - clearly bigger than files
|
||||
File: 6, // Common element - smaller than folders
|
||||
Class: 8, // Important code structure
|
||||
Protocol: 7, // Like Interface
|
||||
Category: 3, // Like Method
|
||||
Function: 4, // Common code element - small
|
||||
Method: 3, // Smaller than function
|
||||
Variable: 2, // Tiny - leaf node
|
||||
|
|
@ -115,6 +119,8 @@ export const DEFAULT_VISIBLE_LABELS: NodeLabel[] = [
|
|||
'Folder',
|
||||
'File',
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Function',
|
||||
'Method',
|
||||
'Property', // Kotlin/Java fields (HAS_PROPERTY + DEFINES File→Property)
|
||||
|
|
@ -129,6 +135,8 @@ export const FILTERABLE_LABELS: NodeLabel[] = [
|
|||
'Folder',
|
||||
'File',
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'Enum',
|
||||
'Type',
|
||||
|
|
|
|||
|
|
@ -65,6 +65,8 @@ describe('FILTERABLE_LABELS', () => {
|
|||
expect(FILTERABLE_LABELS).toContain('Type');
|
||||
expect(FILTERABLE_LABELS).toContain('Decorator');
|
||||
expect(FILTERABLE_LABELS).toContain('Variable');
|
||||
expect(FILTERABLE_LABELS).toContain('Protocol');
|
||||
expect(FILTERABLE_LABELS).toContain('Category');
|
||||
});
|
||||
|
||||
it('every filterable label has a defined color in NODE_COLORS', () => {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ const LEGEND_LABELS: NodeLabel[] = [
|
|||
'Folder',
|
||||
'File',
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'Enum',
|
||||
'Type',
|
||||
|
|
@ -20,6 +22,8 @@ const ICON_MAP: Record<string, string> = {
|
|||
Folder: 'Folder',
|
||||
File: 'FileCode',
|
||||
Class: 'Box',
|
||||
Protocol: 'Hash',
|
||||
Category: 'Box',
|
||||
Function: 'Braces',
|
||||
Method: 'Braces',
|
||||
Interface: 'Hash',
|
||||
|
|
@ -62,6 +66,8 @@ describe('color legend', () => {
|
|||
expect(LEGEND_LABELS).toContain('Type');
|
||||
expect(LEGEND_LABELS).toContain('Decorator');
|
||||
expect(LEGEND_LABELS).toContain('Variable');
|
||||
expect(LEGEND_LABELS).toContain('Protocol');
|
||||
expect(LEGEND_LABELS).toContain('Category');
|
||||
});
|
||||
|
||||
it('every legend label has a color defined', () => {
|
||||
|
|
@ -76,6 +82,8 @@ describe('color legend', () => {
|
|||
'Folder',
|
||||
'File',
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'Enum',
|
||||
'Type',
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"fingerprint": "011485e5180c005be9dfec7ac8dc4e3bb0fcfc4a680a16dcae2cf2d8af5ef7e2",
|
||||
"fingerprint": "d3c3a53ead47663816344369dc0e501f31d60c462039de190ab6f5361037bd79",
|
||||
"_rebaselined_objective_c_node_tables": "Objective-C support adds Protocol and Category node tables to csv-generator.ts's MULTI_LANG_TYPES, so this deterministic 2,400-entity synthetic emit now creates 38 CSV files rather than 36. The two additions, category.csv and protocol.csv, are both expected 55-byte header-only files because the synthetic graph contains no Objective-C nodes. Re-emitting the graph and recomputing the fingerprint after excluding exactly those two files yields the exact prior baseline 011485e5180c005be9dfec7ac8dc4e3bb0fcfc4a680a16dcae2cf2d8af5ef7e2, proving all 36 pre-existing files retain their bytes, routing, and within-file order. Prior 011485e5180c005be9dfec7ac8dc4e3bb0fcfc4a680a16dcae2cf2d8af5ef7e2 -> d3c3a53ead47663816344369dc0e501f31d60c462039de190ab6f5361037bd79. The timing guard remains within budget: local scaling_ratio 0.965 against 1.8 and elapsed_ms_large 69.24ms against 1000ms.",
|
||||
"_rebaselined_3161_static_gated_column": "Every relationship row gained a trailing `staticGated` BOOLEAN column (RELATION_SCHEMA in src/core/lbug/schema.ts, REL_CSV_HEADER + buildRelRow in csv-generator.ts, the fallback CREATE in lbug-adapter.ts), written as `0` for every edge that does not carry the flag and `1` for a Zig call site inside a comptime-false branch (PR #3161). Unlike the earlier header-only rebaselines this one touches ROWS, so the evidence is the inverse operation rather than a file-set diff: re-emitting this bench's 2,400-entity graph on the branch produces the same 36 CSV files; exactly 3 of them differ from a copy with the new column stripped (`rel_File_Class.csv` 156228 -> 151416 bytes, `rel_File_Function.csv` 334008 -> 324396, `rel_Function_Function.csv` 185028 -> 180216: one header field plus `,0` per row, 4,800 / 9,600 / 4,800 rows, 2 bytes each), the other 33 files are byte-identical, and the per-file fingerprint over the stripped copies is EXACTLY the prior baseline 72096279092d4f118de7e179333705c19c9aff2664f77d71a7da48cd9f73fb5a. So no row moved between pair files and no row reordered; the only bytes that changed are the appended column. Prior 72096279092d4f118de7e179333705c19c9aff2664f77d71a7da48cd9f73fb5a -> 011485e5180c005be9dfec7ac8dc4e3bb0fcfc4a680a16dcae2cf2d8af5ef7e2. Timing gates passed while the guard was red: scaling_ratio 0.82 against the 1.8 budget, elapsed_ms_large 185.54ms against the 1000ms backstop, so no throughput claim is being rebaselined away.",
|
||||
"scaling_budget": 1.8,
|
||||
"max_ms_large": 1000,
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -490,6 +490,7 @@ import { makeVueResolveImportTarget } from '../../src/core/ingestion/languages/v
|
|||
import { typescriptScopeResolver } from '../../src/core/ingestion/languages/typescript/scope-resolver.ts';
|
||||
import { cScopeResolver } from '../../src/core/ingestion/languages/c/scope-resolver.ts';
|
||||
import { cppScopeResolver } from '../../src/core/ingestion/languages/cpp/scope-resolver.ts';
|
||||
import { objectiveCScopeResolver } from '../../src/core/ingestion/languages/objective-c/scope-resolver.ts';
|
||||
// `SCOPE_RESOLVERS` is NOT imported here — see the inventory arm at the bottom,
|
||||
// which loads it dynamically. Statically it costs 6-10 s of module load
|
||||
// depending on the box (measured both ways there), because reaching the
|
||||
|
|
@ -598,6 +599,7 @@ const HEAP_BUDGETED = [
|
|||
'dart',
|
||||
'go',
|
||||
'cpp',
|
||||
'objc',
|
||||
];
|
||||
// javascript, typescript and vue were budgeted here until #2953 and are now
|
||||
// BOUNDED, which is a demotion in gate strength and a promotion in what the
|
||||
|
|
@ -756,6 +758,7 @@ const EXTENSION = {
|
|||
vue: '.vue',
|
||||
c: '.c',
|
||||
cpp: '.cpp',
|
||||
objc: '.m',
|
||||
zig: '.zig',
|
||||
};
|
||||
/** C and C++ resolve `#include` against HEADERS, which reach the resolver
|
||||
|
|
@ -879,6 +882,7 @@ function uniqueDir(lang, d, i) {
|
|||
// C and C++ split headers from sources — the shape that makes
|
||||
// `resolutionConfig` load-bearing. Odd `i` is the header.
|
||||
if (lang === 'c' || lang === 'cpp') return i % 2 === 1 ? `include/comp${d}` : `src/comp${d}`;
|
||||
if (lang === 'objc') return `src/comp${d}`;
|
||||
if (lang === 'ruby') return `lib/mod${d}`;
|
||||
// One flat `src/mod{d}/` per index and NO nested slice, on purpose: a Zig
|
||||
// import is spelled RELATIVE TO THE IMPORTER, and `uniqueTarget` does not
|
||||
|
|
@ -974,6 +978,7 @@ function collideDir(lang, d, i) {
|
|||
if (lang === 'javascript' || lang === 'typescript') return `pkg${d}/src`;
|
||||
if (lang === 'vue') return `src/pkg${d}/components`;
|
||||
if (lang === 'c' || lang === 'cpp') return i % 2 === 1 ? `svc${d}/include` : `svc${d}/src`;
|
||||
if (lang === 'objc') return `svc${d}/src`;
|
||||
if (lang === 'ruby') return `svc${d}/lib/models`;
|
||||
// Rust's reasoning, verbatim: the resolver walks path components and probes
|
||||
// `.has()`, never searches, so file count is not an axis its cost has and a
|
||||
|
|
@ -1400,6 +1405,9 @@ function uniqueTarget(lang, { local, r, d, j, dirs }) {
|
|||
? ['stdio.h', 'stdlib.h', 'string.h'][(r >>> 4) % 3]
|
||||
: `vendor${(r >>> 4) % 97}/missing${h}`;
|
||||
}
|
||||
if (lang === 'objc') {
|
||||
return local ? `comp${j % dirs}/file${j}.m` : `vendor${(r >>> 4) % 97}/missing.m`;
|
||||
}
|
||||
if (lang === 'ruby') {
|
||||
return local
|
||||
? `mod${d}/file${j}`
|
||||
|
|
@ -1630,6 +1638,9 @@ function collideTarget(lang, { local, r, d, j, dirs }) {
|
|||
? ['stdio.h', 'stdlib.h', 'string.h'][(r >>> 4) % 3]
|
||||
: `vendor${(r >>> 4) % 97}/mod0${h}`;
|
||||
}
|
||||
if (lang === 'objc') {
|
||||
return local ? `src/file${j}.m` : `vendor${(r >>> 4) % 97}/missing.m`;
|
||||
}
|
||||
if (lang === 'ruby') {
|
||||
// `models/mod{n}.rb` in every package. Ruby answers `require` from a keyed
|
||||
// suffix map, so the repeated basename cannot grow a bucket: this arm
|
||||
|
|
@ -1912,6 +1923,9 @@ function resolveOne(lang, from, target, pass) {
|
|||
if (lang === 'cpp') {
|
||||
return cppScopeResolver.resolveImportTarget(target, from, allFilePaths, pass.config);
|
||||
}
|
||||
if (lang === 'objc') {
|
||||
return objectiveCScopeResolver.resolveImportTarget(target, from, allFilePaths, pass.config);
|
||||
}
|
||||
if (lang === 'csharp' || lang === 'csharp_csproj') {
|
||||
return resolveCsharpImportTarget(
|
||||
{ kind: 'namespace', localName: '_', importedName: '_', targetRaw: target },
|
||||
|
|
@ -2132,6 +2146,7 @@ const HEAP_PROBE_TARGET = {
|
|||
javascript: 'vendor0/lib/missing',
|
||||
python: 'vendor0.deep.missing',
|
||||
c: 'vendor0/missing.h',
|
||||
objc: 'vendor0/missing.m',
|
||||
// The entries below cover the BOUNDED tier — see `HEAP_BOUNDED`, which
|
||||
// derives to cobol, swift and rust; the rest were promoted. Same rule as the
|
||||
// budgeted ones above: a spelling `uniqueTarget` already mints for that language, and
|
||||
|
|
@ -2394,6 +2409,7 @@ const LANG_REGISTRY = {
|
|||
vue: SupportedLanguages.Vue,
|
||||
c: SupportedLanguages.C,
|
||||
cpp: SupportedLanguages.CPlusPlus,
|
||||
objc: SupportedLanguages.ObjectiveC,
|
||||
zig: SupportedLanguages.Zig,
|
||||
};
|
||||
const LANGS = Object.keys(LANG_REGISTRY);
|
||||
|
|
|
|||
41
gitnexus/bench/objective-c-resolution/baseline.json
Normal file
41
gitnexus/bench/objective-c-resolution/baseline.json
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"_comment": "Correctness counts are exact. Both arms are linear file-count gates. protocol implementer USES live on one shared node per (protocol, selector): N objc-protocol-candidate edges, not N². Measured after the shared-set hoist: spread 0.62→2.37 ms (linear_factor 0.956); protocol 0.69→2.14 ms (linear_factor 0.775) with candidate uses 40→160.",
|
||||
"spread": {
|
||||
"small": {
|
||||
"classes": 40,
|
||||
"calls": 119,
|
||||
"extends": 39,
|
||||
"implements": 0,
|
||||
"protocol_candidate_uses": 0,
|
||||
"ms_budget": 80
|
||||
},
|
||||
"large": {
|
||||
"classes": 160,
|
||||
"calls": 479,
|
||||
"extends": 159,
|
||||
"implements": 0,
|
||||
"protocol_candidate_uses": 0,
|
||||
"ms_budget": 320
|
||||
},
|
||||
"linear_scaling_slack": 1.375
|
||||
},
|
||||
"protocol": {
|
||||
"small": {
|
||||
"classes": 40,
|
||||
"calls": 40,
|
||||
"extends": 0,
|
||||
"implements": 40,
|
||||
"protocol_candidate_uses": 40,
|
||||
"ms_budget": 80
|
||||
},
|
||||
"large": {
|
||||
"classes": 160,
|
||||
"calls": 160,
|
||||
"extends": 0,
|
||||
"implements": 160,
|
||||
"protocol_candidate_uses": 160,
|
||||
"ms_budget": 80
|
||||
},
|
||||
"linear_scaling_slack": 1.375
|
||||
}
|
||||
}
|
||||
386
gitnexus/bench/objective-c-resolution/measure.mjs
Normal file
386
gitnexus/bench/objective-c-resolution/measure.mjs
Normal file
|
|
@ -0,0 +1,386 @@
|
|||
#!/usr/bin/env node
|
||||
/**
|
||||
* Build-free scaling and correctness guard for Objective-C workspace
|
||||
* resolution (#3179).
|
||||
*
|
||||
* Import lookup already lives in bench/import-target (the `objc` arm). This
|
||||
* bench isolates `emitPostResolutionEdges`: heritage, category membership,
|
||||
* and static message-send dispatch. Two shapes:
|
||||
*
|
||||
* spread — typed self / super / sibling receivers. Cost must stay
|
||||
* linear in class count (the C# "namespaces spread" analog).
|
||||
* protocol — every class conforms to one protocol and sends one
|
||||
* protocol-typed message. Implementer USES live on one
|
||||
* shared node per (protocol, selector); messages only add
|
||||
* the source→shared hop. Both arms must stay linear.
|
||||
*
|
||||
* Usage:
|
||||
* node --import tsx bench/objective-c-resolution/measure.mjs
|
||||
* node --import tsx bench/objective-c-resolution/measure.mjs --check
|
||||
*/
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { performance } from 'node:perf_hooks';
|
||||
import { createKnowledgeGraph } from '../../src/core/graph/graph.ts';
|
||||
import {
|
||||
OBJECTIVE_C_GRAMMAR_PACKAGE,
|
||||
OBJECTIVE_C_GRAMMAR_VERSION,
|
||||
OBJECTIVE_C_PROVIDER_VERSION,
|
||||
objcClassQualifiedName,
|
||||
objcMethodQualifiedName,
|
||||
objcProtocolQualifiedName,
|
||||
} from '../../src/core/ingestion/languages/objective-c/facts.ts';
|
||||
import { objectiveCScopeResolver } from '../../src/core/ingestion/languages/objective-c/scope-resolver.ts';
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url));
|
||||
const SMALL_CLASSES = 40;
|
||||
const LARGE_CLASSES = 160;
|
||||
const REPS = 7;
|
||||
|
||||
const graphNodeId = (label, qualifiedName) => `${label}:${qualifiedName}`;
|
||||
|
||||
function emptyFacts(filePath) {
|
||||
return {
|
||||
providerVersion: OBJECTIVE_C_PROVIDER_VERSION,
|
||||
grammarPackage: OBJECTIVE_C_GRAMMAR_PACKAGE,
|
||||
grammarVersion: OBJECTIVE_C_GRAMMAR_VERSION,
|
||||
filePath,
|
||||
containers: [],
|
||||
methods: [],
|
||||
members: [],
|
||||
functions: [],
|
||||
imports: [],
|
||||
messages: [],
|
||||
unresolvedMessages: [],
|
||||
};
|
||||
}
|
||||
|
||||
function classContainer(filePath, name, extras = {}) {
|
||||
const qualifiedName = objcClassQualifiedName(name);
|
||||
return {
|
||||
kind: 'class',
|
||||
declarationRole: 'implementation',
|
||||
name,
|
||||
qualifiedName,
|
||||
nodeId: graphNodeId('Class', qualifiedName),
|
||||
label: 'Class',
|
||||
filePath,
|
||||
startLine: 1,
|
||||
endLine: 20,
|
||||
protocols: extras.protocols ?? [],
|
||||
...(extras.superclass !== undefined ? { superclass: extras.superclass } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function protocolContainer(filePath, name) {
|
||||
const qualifiedName = objcProtocolQualifiedName(name);
|
||||
return {
|
||||
kind: 'protocol',
|
||||
declarationRole: 'interface',
|
||||
name,
|
||||
qualifiedName,
|
||||
nodeId: graphNodeId('Protocol', qualifiedName),
|
||||
label: 'Protocol',
|
||||
filePath,
|
||||
startLine: 1,
|
||||
endLine: 4,
|
||||
protocols: [],
|
||||
};
|
||||
}
|
||||
|
||||
function methodFact(filePath, owner, selector, methodKind, startLine) {
|
||||
const qualifiedName = objcMethodQualifiedName(owner.qualifiedName, methodKind, selector);
|
||||
return {
|
||||
name: selector,
|
||||
selector,
|
||||
methodKind,
|
||||
ownerQualifiedName: owner.qualifiedName,
|
||||
ownerName: owner.name,
|
||||
ownerKind: owner.kind,
|
||||
qualifiedName,
|
||||
nodeId: graphNodeId('Method', qualifiedName),
|
||||
filePath,
|
||||
startLine,
|
||||
endLine: startLine,
|
||||
declarationRole: 'implementation',
|
||||
parameterTypes: [],
|
||||
parameterNames: [],
|
||||
};
|
||||
}
|
||||
|
||||
function messageFact(method, selector, receiverKind, receiverText, extras = {}) {
|
||||
return {
|
||||
selector,
|
||||
receiverText,
|
||||
receiverKind,
|
||||
sourceMethodQualifiedName: method.qualifiedName,
|
||||
sourceMethodId: method.nodeId,
|
||||
sourceOwnerQualifiedName: method.ownerQualifiedName,
|
||||
sourceOwnerName: method.ownerName,
|
||||
sourceMethodKind: method.methodKind,
|
||||
filePath: method.filePath,
|
||||
startLine: extras.startLine ?? method.startLine + 1,
|
||||
startCol: extras.startCol ?? 2,
|
||||
...(extras.receiverType !== undefined ? { receiverType: extras.receiverType } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function parsedFile(filePath, facts) {
|
||||
return Object.freeze({
|
||||
filePath,
|
||||
moduleScope: 0,
|
||||
scopes: Object.freeze([]),
|
||||
parsedImports: Object.freeze([]),
|
||||
localDefs: Object.freeze([]),
|
||||
referenceSites: Object.freeze([]),
|
||||
captureSideChannel: Object.freeze({ kind: 'objective-c', facts }),
|
||||
});
|
||||
}
|
||||
|
||||
function seedGraph(graph, factsList) {
|
||||
for (const facts of factsList) {
|
||||
graph.addNode({
|
||||
id: graphNodeId('File', facts.filePath),
|
||||
label: 'File',
|
||||
properties: {
|
||||
name: facts.filePath,
|
||||
qualifiedName: facts.filePath,
|
||||
filePath: facts.filePath,
|
||||
startLine: 1,
|
||||
endLine: 1,
|
||||
language: 'objective-c',
|
||||
isExported: false,
|
||||
},
|
||||
});
|
||||
for (const container of facts.containers) {
|
||||
graph.addNode({
|
||||
id: container.nodeId,
|
||||
label: container.label,
|
||||
properties: {
|
||||
name: container.name,
|
||||
qualifiedName: container.qualifiedName,
|
||||
filePath: facts.filePath,
|
||||
startLine: container.startLine,
|
||||
endLine: container.endLine,
|
||||
language: 'objective-c',
|
||||
isExported: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
for (const method of facts.methods) {
|
||||
graph.addNode({
|
||||
id: method.nodeId,
|
||||
label: 'Method',
|
||||
properties: {
|
||||
name: method.selector,
|
||||
qualifiedName: method.qualifiedName,
|
||||
filePath: facts.filePath,
|
||||
startLine: method.startLine,
|
||||
endLine: method.endLine,
|
||||
language: 'objective-c',
|
||||
isExported: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function summarize(graph) {
|
||||
let calls = 0;
|
||||
let extendsEdges = 0;
|
||||
let implementsEdges = 0;
|
||||
let protocolCandidateUses = 0;
|
||||
for (const rel of graph.iterRelationships()) {
|
||||
if (rel.type === 'CALLS') calls++;
|
||||
else if (rel.type === 'EXTENDS') extendsEdges++;
|
||||
else if (rel.type === 'IMPLEMENTS') implementsEdges++;
|
||||
else if (rel.type === 'USES' && String(rel.reason).startsWith('objc-protocol-candidate:')) {
|
||||
protocolCandidateUses++;
|
||||
}
|
||||
}
|
||||
return {
|
||||
calls,
|
||||
extends: extendsEdges,
|
||||
implements: implementsEdges,
|
||||
protocol_candidate_uses: protocolCandidateUses,
|
||||
};
|
||||
}
|
||||
|
||||
function spreadCorpus(classCount) {
|
||||
const factsList = [];
|
||||
const parsedFiles = [];
|
||||
for (let i = 0; i < classCount; i++) {
|
||||
const filePath = `src/Class${i}.m`;
|
||||
const name = `Class${i}`;
|
||||
const sibling = `Class${(i + 1) % classCount}`;
|
||||
const owner = classContainer(filePath, name, i === 0 ? {} : { superclass: 'Class0' });
|
||||
const run = methodFact(filePath, owner, 'run', '-', 4);
|
||||
const tick = methodFact(filePath, owner, 'tick', '-', 8);
|
||||
const messages = [
|
||||
messageFact(tick, 'run', 'self', 'self', { startLine: 9, startCol: 2 }),
|
||||
messageFact(tick, 'run', 'local', 'sib', {
|
||||
startLine: 10,
|
||||
startCol: 2,
|
||||
receiverType: { kind: 'class', name: sibling, raw: `${sibling} *` },
|
||||
}),
|
||||
];
|
||||
if (i > 0) {
|
||||
messages.push(messageFact(tick, 'run', 'super', 'super', { startLine: 11, startCol: 2 }));
|
||||
}
|
||||
const facts = {
|
||||
...emptyFacts(filePath),
|
||||
containers: [owner],
|
||||
methods: [run, tick],
|
||||
messages,
|
||||
};
|
||||
factsList.push(facts);
|
||||
parsedFiles.push(parsedFile(filePath, facts));
|
||||
}
|
||||
return { factsList, parsedFiles };
|
||||
}
|
||||
|
||||
function protocolCorpus(classCount) {
|
||||
const factsList = [];
|
||||
const parsedFiles = [];
|
||||
const protocolPath = 'src/Runnable.h';
|
||||
const protocol = protocolContainer(protocolPath, 'Runnable');
|
||||
const protocolRun = methodFact(protocolPath, protocol, 'run', '-', 2);
|
||||
const protocolFacts = {
|
||||
...emptyFacts(protocolPath),
|
||||
containers: [protocol],
|
||||
methods: [protocolRun],
|
||||
};
|
||||
factsList.push(protocolFacts);
|
||||
parsedFiles.push(parsedFile(protocolPath, protocolFacts));
|
||||
|
||||
for (let i = 0; i < classCount; i++) {
|
||||
const filePath = `src/Class${i}.m`;
|
||||
const owner = classContainer(filePath, `Class${i}`, { protocols: ['Runnable'] });
|
||||
const run = methodFact(filePath, owner, 'run', '-', 4);
|
||||
const tick = methodFact(filePath, owner, 'tick:', '-', 8);
|
||||
const facts = {
|
||||
...emptyFacts(filePath),
|
||||
containers: [owner],
|
||||
methods: [run, tick],
|
||||
messages: [
|
||||
messageFact(tick, 'run', 'local', 'runner', {
|
||||
startLine: 9,
|
||||
startCol: 2,
|
||||
receiverType: { kind: 'protocol', name: 'Runnable', raw: 'id<Runnable>' },
|
||||
}),
|
||||
],
|
||||
};
|
||||
factsList.push(facts);
|
||||
parsedFiles.push(parsedFile(filePath, facts));
|
||||
}
|
||||
return { factsList, parsedFiles };
|
||||
}
|
||||
|
||||
function run(shape, classCount) {
|
||||
const corpus = shape === 'spread' ? spreadCorpus(classCount) : protocolCorpus(classCount);
|
||||
const graph = createKnowledgeGraph();
|
||||
seedGraph(graph, corpus.factsList);
|
||||
objectiveCScopeResolver.emitPostResolutionEdges(graph, corpus.parsedFiles);
|
||||
return summarize(graph);
|
||||
}
|
||||
|
||||
function measure(shape, classCount) {
|
||||
run(shape, classCount);
|
||||
let bestMs = Infinity;
|
||||
let counts = { calls: 0, extends: 0, implements: 0, protocol_candidate_uses: 0 };
|
||||
for (let i = 0; i < REPS; i++) {
|
||||
const start = performance.now();
|
||||
counts = run(shape, classCount);
|
||||
bestMs = Math.min(bestMs, performance.now() - start);
|
||||
}
|
||||
return {
|
||||
classes: classCount,
|
||||
...counts,
|
||||
min_ms: Number(bestMs.toFixed(2)),
|
||||
};
|
||||
}
|
||||
|
||||
function scalingReport(small, large) {
|
||||
const workloadRatio = large.classes / small.classes;
|
||||
const scalingRatio = Number((large.min_ms / Math.max(small.min_ms, 0.01)).toFixed(3));
|
||||
return {
|
||||
small,
|
||||
large,
|
||||
workload_ratio: workloadRatio,
|
||||
scaling_ratio: scalingRatio,
|
||||
linear_factor: Number((scalingRatio / workloadRatio).toFixed(3)),
|
||||
};
|
||||
}
|
||||
|
||||
const report = {
|
||||
spread: scalingReport(measure('spread', SMALL_CLASSES), measure('spread', LARGE_CLASSES)),
|
||||
protocol: scalingReport(measure('protocol', SMALL_CLASSES), measure('protocol', LARGE_CLASSES)),
|
||||
};
|
||||
|
||||
if (!process.argv.includes('--check')) {
|
||||
console.log(JSON.stringify(report, null, 2));
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const baseline = JSON.parse(readFileSync(join(HERE, 'baseline.json'), 'utf8'));
|
||||
const failures = [];
|
||||
const requirePositiveNumber = (path, value) => {
|
||||
if (typeof value !== 'number' || !Number.isFinite(value) || !(value > 0)) {
|
||||
failures.push(`${path}: expected a finite positive number, got ${JSON.stringify(value)}`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
for (const shape of ['spread', 'protocol']) {
|
||||
const measured = report[shape];
|
||||
const expected = baseline[shape];
|
||||
for (const arm of ['small', 'large']) {
|
||||
for (const key of ['classes', 'calls', 'extends', 'implements', 'protocol_candidate_uses']) {
|
||||
if (measured[arm][key] !== expected[arm][key]) {
|
||||
failures.push(
|
||||
`${shape}.${arm}.${key}: expected ${expected[arm][key]}, got ${measured[arm][key]}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (
|
||||
requirePositiveNumber(`${shape}.${arm}.ms_budget`, expected[arm].ms_budget) &&
|
||||
measured[arm].min_ms > expected[arm].ms_budget
|
||||
) {
|
||||
failures.push(
|
||||
`${shape}.${arm}.min_ms ${measured[arm].min_ms} exceeds budget ${expected[arm].ms_budget}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
requirePositiveNumber('spread.linear_scaling_slack', baseline.spread.linear_scaling_slack) &&
|
||||
report.spread.linear_factor > baseline.spread.linear_scaling_slack
|
||||
) {
|
||||
failures.push(
|
||||
`spread.linear_factor ${report.spread.linear_factor} exceeds slack ` +
|
||||
`${baseline.spread.linear_scaling_slack} (runtime ${report.spread.scaling_ratio}x ` +
|
||||
`for ${report.spread.workload_ratio}x work)`,
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
requirePositiveNumber('protocol.linear_scaling_slack', baseline.protocol.linear_scaling_slack) &&
|
||||
report.protocol.linear_factor > baseline.protocol.linear_scaling_slack
|
||||
) {
|
||||
failures.push(
|
||||
`protocol.linear_factor ${report.protocol.linear_factor} exceeds slack ` +
|
||||
`${baseline.protocol.linear_scaling_slack} (runtime ${report.protocol.scaling_ratio}x ` +
|
||||
`for ${report.protocol.workload_ratio}x work)`,
|
||||
);
|
||||
}
|
||||
|
||||
console.log(JSON.stringify(report, null, 2));
|
||||
if (failures.length > 0) {
|
||||
console.error('[objective-c-resolution --check] FAIL');
|
||||
for (const failure of failures) console.error(` - ${failure}`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('[objective-c-resolution --check] PASS');
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
* One registry-driven script replaces the former per-grammar
|
||||
* build-tree-sitter-<name>.cjs files (they were ~95% identical).
|
||||
*
|
||||
* The grammars (tree-sitter-c/dart/proto/swift/kotlin/zig) are loaded from
|
||||
* The grammars (tree-sitter-c/objc/dart/proto/swift/kotlin/zig) are loaded from
|
||||
* `vendor/<name>/` by absolute path at runtime (see
|
||||
* src/core/tree-sitter/vendored-grammars.ts) and are NEVER copied into
|
||||
* node_modules — an undeclared package under node_modules is "extraneous" to
|
||||
|
|
@ -25,8 +25,8 @@
|
|||
* or exit non-zero — a failure for any single grammar must not break the install.
|
||||
*
|
||||
* Opt-out: GITNEXUS_SKIP_OPTIONAL_GRAMMARS=1 (strict '1') skips the OPTIONAL
|
||||
* grammars only. tree-sitter-c is REQUIRED (it backstops upstream's 4/6 ARM
|
||||
* prebuild gap, #2116) and is always built.
|
||||
* grammars only. tree-sitter-c and tree-sitter-objc are REQUIRED (C backstops upstream's 4/6 ARM
|
||||
* prebuild gap, #2116) and are always built.
|
||||
*
|
||||
* Usage:
|
||||
* node build-tree-sitter-grammars.cjs # all grammars (postinstall)
|
||||
|
|
@ -40,6 +40,7 @@ const { execSync } = require('child_process');
|
|||
// grammars ignore the opt-out gate. Insertion order == build order (c first).
|
||||
const GRAMMARS = {
|
||||
c: { required: true, display: 'C', ext: '.c' },
|
||||
objc: { required: true, display: 'Objective-C', ext: '.m/.mm/.h' },
|
||||
dart: { required: false, display: 'Dart', ext: '.dart' },
|
||||
proto: { required: false, display: 'Proto', ext: '.proto' },
|
||||
swift: { required: false, display: 'Swift', ext: '.swift' },
|
||||
|
|
|
|||
|
|
@ -291,13 +291,15 @@ export const doctorCommand = async () => {
|
|||
console.log(` ${padDisplayEnd('', 18)}${remedy}`);
|
||||
}
|
||||
}
|
||||
// Semantic mode follows the probe, not the platform: without a loadable
|
||||
// VECTOR extension the index can be neither built nor queried, so search is
|
||||
// really on exact scan no matter what the platform would allow.
|
||||
// Doctor has no repository target, so this probe can report only whether the
|
||||
// runtime can load VECTOR. It cannot claim that a repository has built the
|
||||
// named HNSW index. Keep the capability and repository state distinct.
|
||||
console.log(
|
||||
` ${label('doctor.labels.semanticMode', 18)}${
|
||||
vectorProbe.loaded ? capabilities.semanticMode : 'exact-scan'
|
||||
}`,
|
||||
` ${label('doctor.labels.semanticMode', 18)}${t(
|
||||
vectorProbe.loaded
|
||||
? 'doctor.vectorCapability.indexUnverified'
|
||||
: 'doctor.vectorCapability.exactScanOnly',
|
||||
)}`,
|
||||
);
|
||||
// Surface the optional-extension install policy so offline users can see
|
||||
// whether analyze/query will reach the network (extension.ladybugdb.com).
|
||||
|
|
|
|||
|
|
@ -123,8 +123,10 @@ export const en = {
|
|||
'doctor.labels.onnx': 'ONNX:',
|
||||
'doctor.labels.graphStore': 'Graph store:',
|
||||
'doctor.labels.fullTextSearch': 'Full-text search:',
|
||||
'doctor.labels.vectorIndex': 'VECTOR index:',
|
||||
'doctor.labels.semanticMode': 'Semantic mode:',
|
||||
'doctor.labels.vectorIndex': 'VECTOR extension:',
|
||||
'doctor.labels.semanticMode': 'Semantic support:',
|
||||
'doctor.vectorCapability.indexUnverified': 'vector-index capable (repository index not checked)',
|
||||
'doctor.vectorCapability.exactScanOnly': 'exact-scan only (VECTOR extension unavailable)',
|
||||
'doctor.labels.exactScanLimit': 'Exact scan limit:',
|
||||
'doctor.labels.note': 'Note:',
|
||||
'doctor.labels.backend': 'Backend:',
|
||||
|
|
|
|||
|
|
@ -119,8 +119,10 @@ export const zhCN = {
|
|||
'doctor.labels.onnx': 'ONNX:',
|
||||
'doctor.labels.graphStore': '图存储:',
|
||||
'doctor.labels.fullTextSearch': '全文搜索:',
|
||||
'doctor.labels.vectorIndex': '向量索引:',
|
||||
'doctor.labels.semanticMode': '语义模式:',
|
||||
'doctor.labels.vectorIndex': 'VECTOR 扩展:',
|
||||
'doctor.labels.semanticMode': '语义支持:',
|
||||
'doctor.vectorCapability.indexUnverified': '支持向量索引(未检查仓库索引)',
|
||||
'doctor.vectorCapability.exactScanOnly': '仅精确扫描(VECTOR 扩展不可用)',
|
||||
'doctor.labels.exactScanLimit': '精确扫描上限:',
|
||||
'doctor.labels.note': '说明:',
|
||||
'doctor.labels.backend': '后端:',
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import {
|
|||
JAVA_RECORD_COMPONENT_ACCESSORS_FEATURE,
|
||||
SPRING_CONFIG_BINDINGS_FEATURE,
|
||||
} from './ingestion/languages/java/analysis-features.js';
|
||||
import { OBJECTIVE_C_PROVIDER_FEATURE } from './ingestion/languages/objective-c/analysis-features.js';
|
||||
|
||||
/** Production registry of independently versioned analysis capabilities. */
|
||||
export const ANALYSIS_FEATURES = [
|
||||
|
|
@ -23,4 +24,5 @@ export const ANALYSIS_FEATURES = [
|
|||
SPRING_CONFIG_BINDINGS_FEATURE,
|
||||
JAVA_ENUM_INTERFACE_HERITAGE_FEATURE,
|
||||
JAVA_RECORD_COMPONENT_ACCESSORS_FEATURE,
|
||||
OBJECTIVE_C_PROVIDER_FEATURE,
|
||||
] as const;
|
||||
|
|
|
|||
|
|
@ -4,14 +4,13 @@
|
|||
* used by both chunker.ts and structural-extractor.ts.
|
||||
*/
|
||||
|
||||
import { getLanguageFromFilename } from 'gitnexus-shared';
|
||||
import {
|
||||
createParserForLanguage,
|
||||
isLanguageAvailable,
|
||||
resolveLanguageKey,
|
||||
} from '../tree-sitter/parser-loader.js';
|
||||
import { parseSourceSafe } from '../tree-sitter/safe-parse.js';
|
||||
import { getProvider } from '../ingestion/languages/index.js';
|
||||
import { getLanguageForFileContent, getProvider } from '../ingestion/languages/index.js';
|
||||
|
||||
const parserCache = new Map<string, any>();
|
||||
|
||||
|
|
@ -20,7 +19,10 @@ const parserCache = new Map<string, any>();
|
|||
* Returns null if language is unavailable or parsing fails.
|
||||
*/
|
||||
export const ensureAndParse = async (content: string, filePath: string): Promise<any | null> => {
|
||||
const language = getLanguageFromFilename(filePath);
|
||||
// Same classifier as ingest. Filename-only maps `.h` → C++, so Objective-C
|
||||
// headers (and method snippets from those headers) would parse with the
|
||||
// wrong grammar and miss class_interface / protocol_declaration / methods.
|
||||
const language = getLanguageForFileContent(filePath, content);
|
||||
if (!language) return null;
|
||||
if (!isLanguageAvailable(language)) return null;
|
||||
|
||||
|
|
@ -100,6 +102,9 @@ export const findDeclarationNode = (root: any): any | null => {
|
|||
'struct_item',
|
||||
'interface_declaration',
|
||||
'interface_definition',
|
||||
'protocol_declaration', // Objective-C protocol
|
||||
'class_interface', // Objective-C class, category, or extension
|
||||
'class_implementation', // Objective-C implementation
|
||||
'enum_declaration',
|
||||
'enum_item',
|
||||
'type_declaration', // Go: type X struct
|
||||
|
|
|
|||
|
|
@ -145,9 +145,35 @@ const DECLARATION_BODY_NODE_TYPES = new Set([
|
|||
'class_body',
|
||||
'object_type',
|
||||
'declaration_list',
|
||||
'implementation_definition',
|
||||
'interface_body',
|
||||
]);
|
||||
|
||||
const DIRECT_MEMBER_DECLARATION_TYPES = new Set([
|
||||
'protocol_declaration',
|
||||
'class_interface',
|
||||
// tree-sitter-objc exposes each implementation_definition directly under
|
||||
// class_implementation, rather than grouping them in a shared body node.
|
||||
'class_implementation',
|
||||
]);
|
||||
|
||||
const DIRECT_MEMBER_HEADER_NODE_TYPES = new Set([
|
||||
'identifier',
|
||||
'parameterized_arguments',
|
||||
'protocol_reference_list',
|
||||
// tree-sitter-objc `_type_params` hides itself and exposes this named child
|
||||
// for `(T)` generics and category-shaped argument lists.
|
||||
'generic_arguments',
|
||||
// `_class_interface_header` / `_class_implementation_header` expose these
|
||||
// named children before members (`NS_ROOT_CLASS @interface …`).
|
||||
'attribute_declaration',
|
||||
'attribute_specifier',
|
||||
'availability_attribute_specifier',
|
||||
'ms_declspec_modifier',
|
||||
'storage_class_specifier',
|
||||
'type_qualifier',
|
||||
]);
|
||||
|
||||
const FIELD_LIKE_MEMBER_TYPES = new Set([
|
||||
'field_definition',
|
||||
'public_field_definition',
|
||||
|
|
@ -157,8 +183,18 @@ const FIELD_LIKE_MEMBER_TYPES = new Set([
|
|||
'lexical_declaration',
|
||||
'pair',
|
||||
'enum_assignment',
|
||||
// tree-sitter-objc wraps each ivar as `instance_variable`, not field_definition.
|
||||
'instance_variable',
|
||||
]);
|
||||
|
||||
const DECLARATION_MEMBER_WRAPPER_TYPES = new Set([
|
||||
'qualified_protocol_interface_declaration',
|
||||
'instance_variables',
|
||||
]);
|
||||
|
||||
/** Named prefixes the ObjC grammar allows immediately before each ivar. */
|
||||
const IVAR_ATTRIBUTE_PREFIX_TYPES = new Set(['attribute_specifier', 'attribute_declaration']);
|
||||
|
||||
const declarationChunk = async (
|
||||
content: string,
|
||||
filePath: string,
|
||||
|
|
@ -337,6 +373,8 @@ const getDeclarationBodyNode = (node: any): any | null => {
|
|||
const bodyNode = node.childForFieldName?.('body');
|
||||
if (bodyNode) return bodyNode;
|
||||
|
||||
if (DIRECT_MEMBER_DECLARATION_TYPES.has(node.type)) return node;
|
||||
|
||||
for (let i = 0; i < node.namedChildCount; i++) {
|
||||
const child = node.namedChild(i);
|
||||
if (!child) continue;
|
||||
|
|
@ -352,15 +390,43 @@ const collectDeclarationUnits = (
|
|||
): Array<{ startIndex: number; endIndex: number }> => {
|
||||
const members: Array<{ startIndex: number; endIndex: number; groupable: boolean }> = [];
|
||||
|
||||
for (let i = 0; i < bodyNode.namedChildCount; i++) {
|
||||
const child = bodyNode.namedChild(i);
|
||||
if (!child) continue;
|
||||
members.push({
|
||||
startIndex: child.startIndex,
|
||||
endIndex: child.endIndex,
|
||||
groupable: groupFields && FIELD_LIKE_MEMBER_TYPES.has(child.type),
|
||||
});
|
||||
}
|
||||
const collectMembers = (
|
||||
node: any,
|
||||
skipHeaderChildren: boolean,
|
||||
includeNodePrefixOnFirstMember = false,
|
||||
): void => {
|
||||
const firstMemberIndex = members.length;
|
||||
let ivarAttributePrefixStart: number | undefined;
|
||||
for (let i = 0; i < node.namedChildCount; i++) {
|
||||
const child = node.namedChild(i);
|
||||
if (!child) continue;
|
||||
if (DECLARATION_MEMBER_WRAPPER_TYPES.has(child.type)) {
|
||||
collectMembers(child, false, true);
|
||||
continue;
|
||||
}
|
||||
if (skipHeaderChildren && DIRECT_MEMBER_HEADER_NODE_TYPES.has(child.type)) continue;
|
||||
if (node.type === 'instance_variables' && IVAR_ATTRIBUTE_PREFIX_TYPES.has(child.type)) {
|
||||
ivarAttributePrefixStart ??= child.startIndex;
|
||||
continue;
|
||||
}
|
||||
members.push({
|
||||
startIndex: ivarAttributePrefixStart ?? child.startIndex,
|
||||
endIndex: child.endIndex,
|
||||
groupable: groupFields && FIELD_LIKE_MEMBER_TYPES.has(child.type),
|
||||
});
|
||||
ivarAttributePrefixStart = undefined;
|
||||
}
|
||||
|
||||
const firstMember = members[firstMemberIndex];
|
||||
if (includeNodePrefixOnFirstMember && firstMember) {
|
||||
firstMember.startIndex = node.startIndex;
|
||||
if (node.type === 'instance_variables') {
|
||||
members[members.length - 1].endIndex = node.endIndex;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
collectMembers(bodyNode, DIRECT_MEMBER_DECLARATION_TYPES.has(bodyNode.type));
|
||||
|
||||
if (members.length === 0) return [];
|
||||
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ const ensureVectorExtensionAvailable = async (): Promise<boolean> => {
|
|||
* invalidate existing vectors, such as metadata/header shape changes,
|
||||
* structural container context changes, or preceding-context formatting rules.
|
||||
*/
|
||||
export const EMBEDDING_TEXT_VERSION = 'v4';
|
||||
export const EMBEDDING_TEXT_VERSION = 'v5';
|
||||
|
||||
/**
|
||||
* Compute a stable content fingerprint for an embeddable node.
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
* to extract method and field names for embedding text generation.
|
||||
*/
|
||||
|
||||
import { getProviderForFile } from '../ingestion/languages/index.js';
|
||||
import { getProviderForFileContent } from '../ingestion/languages/index.js';
|
||||
import type { MethodExtractorContext, ExtractedMethods } from '../ingestion/method-types.js';
|
||||
import type { FieldExtractorContext, ExtractedFields } from '../ingestion/field-types.js';
|
||||
import type { LanguageProvider } from '../ingestion/language-provider.js';
|
||||
|
|
@ -32,7 +32,7 @@ export const extractStructuralNames = async (
|
|||
content: string,
|
||||
filePath: string,
|
||||
): Promise<StructuralNames> => {
|
||||
const provider = getProviderForFile(filePath);
|
||||
const provider = getProviderForFileContent(filePath, content);
|
||||
if (!provider) return { methodNames: [], fieldNames: [] };
|
||||
|
||||
const tree = await ensureAndParse(content, filePath);
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ export const LABEL_FUNCTION = 'Function' as const;
|
|||
export const LABEL_METHOD = 'Method' as const;
|
||||
export const LABEL_CONSTRUCTOR = 'Constructor' as const;
|
||||
export const LABEL_CLASS = 'Class' as const;
|
||||
export const LABEL_PROTOCOL = 'Protocol' as const;
|
||||
export const LABEL_CATEGORY = 'Category' as const;
|
||||
export const LABEL_INTERFACE = 'Interface' as const;
|
||||
export const LABEL_STRUCT = 'Struct' as const;
|
||||
export const LABEL_ENUM = 'Enum' as const;
|
||||
|
|
@ -53,6 +55,8 @@ export const CHUNKABLE_LABELS = [
|
|||
LABEL_METHOD,
|
||||
LABEL_CONSTRUCTOR,
|
||||
LABEL_CLASS,
|
||||
LABEL_PROTOCOL,
|
||||
LABEL_CATEGORY,
|
||||
LABEL_INTERFACE,
|
||||
LABEL_STRUCT,
|
||||
LABEL_ENUM,
|
||||
|
|
@ -165,6 +169,20 @@ export const CHUNKING_RULES: Readonly<Partial<Record<ChunkableLabel, ChunkingRul
|
|||
groupFields: false,
|
||||
structuralTextMode: STRUCTURAL_TEXT_MODE_DECLARATION,
|
||||
},
|
||||
[LABEL_PROTOCOL]: {
|
||||
mode: CHUNK_MODE_AST_DECLARATION,
|
||||
includePrefix: true,
|
||||
includeSuffix: false,
|
||||
groupFields: false,
|
||||
structuralTextMode: STRUCTURAL_TEXT_MODE_NONE,
|
||||
},
|
||||
[LABEL_CATEGORY]: {
|
||||
mode: CHUNK_MODE_AST_DECLARATION,
|
||||
includePrefix: true,
|
||||
includeSuffix: false,
|
||||
groupFields: true,
|
||||
structuralTextMode: STRUCTURAL_TEXT_MODE_NONE,
|
||||
},
|
||||
[LABEL_STRUCT]: {
|
||||
mode: CHUNK_MODE_AST_DECLARATION,
|
||||
includePrefix: true,
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ export interface ManifestExtractResult {
|
|||
// #2325 integration test can run the EXACT production query against a real
|
||||
// LadybugDB — a hand-copied query string in the test would silently drift
|
||||
// from this allowlist. Uses the `labels(n) IN [...]` allowlist form rather
|
||||
// than a `MATCH (n:A|B)` disjunction: this 21-label list contains the
|
||||
// than a `MATCH (n:A|B)` disjunction: this 23-label list contains the
|
||||
// reserved-keyword labels `Macro` and `Union`, and LadybugDB's parser rejects
|
||||
// a disjunction that names a reserved keyword (#2325) — which the resolver's
|
||||
// try/catch then swallowed. `labels(n) IN` has no such collision.
|
||||
|
|
@ -25,7 +25,7 @@ export interface ManifestExtractResult {
|
|||
// two would widen which nodes resolve as contract symbols and must update the
|
||||
// #2325 test, so they are intentionally kept separate for now.
|
||||
export const CUSTOM_CONTRACT_RESOLVE_QUERY = `MATCH (n)
|
||||
WHERE labels(n) IN ['Function','Method','Class','Interface','Struct','Enum','Trait','Constructor','TypeAlias','Impl','Macro','Union','Typedef','Property','Record','Delegate','Annotation','Template','Const','Static','CodeElement']
|
||||
WHERE labels(n) IN ['Function','Method','Class','Protocol','Category','Interface','Struct','Enum','Trait','Constructor','TypeAlias','Impl','Macro','Union','Typedef','Property','Record','Delegate','Annotation','Template','Const','Static','CodeElement']
|
||||
AND n.name = $symbolName
|
||||
RETURN n.id AS uid, n.name AS name, n.filePath AS filePath
|
||||
ORDER BY n.filePath ASC, n.id ASC
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import type { SupportedLanguages } from 'gitnexus-shared';
|
||||
import { mapConcurrent } from '../../lib/utils.js';
|
||||
import { READ_CONCURRENCY } from './filesystem-walker.js';
|
||||
import { getLanguageForFileContent } from './languages/index.js';
|
||||
|
||||
/**
|
||||
* Classify files whose language cannot be determined from their extension.
|
||||
*
|
||||
* Source text is retained only for the duration of each individual read and
|
||||
* classification. The returned map deliberately contains language results,
|
||||
* not source text, so downstream phases can reuse the decision without
|
||||
* holding every candidate header in memory or rereading it to classify again.
|
||||
*/
|
||||
export async function classifyContentLanguages(
|
||||
repoPath: string,
|
||||
relativePaths: readonly string[],
|
||||
): Promise<ReadonlyMap<string, SupportedLanguages | null>> {
|
||||
const classifications = new Map<string, SupportedLanguages | null>();
|
||||
const results = await mapConcurrent(
|
||||
relativePaths,
|
||||
async (relativePath) => {
|
||||
const sourceText = await fs.readFile(path.join(repoPath, relativePath), 'utf-8');
|
||||
return {
|
||||
path: relativePath,
|
||||
language: getLanguageForFileContent(relativePath, sourceText),
|
||||
};
|
||||
},
|
||||
{ concurrency: READ_CONCURRENCY },
|
||||
);
|
||||
|
||||
// An unreadable file yields `undefined` from mapConcurrent. Leave it absent
|
||||
// so the caller retains the existing filename-based fallback behavior.
|
||||
for (const result of results) {
|
||||
if (result !== undefined) classifications.set(result.path, result.language);
|
||||
}
|
||||
|
||||
return classifications;
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ export interface FilePath {
|
|||
path: string;
|
||||
}
|
||||
|
||||
const READ_CONCURRENCY = 32;
|
||||
export const READ_CONCURRENCY = 32;
|
||||
const ANALYZE_PROGRESS_ACTIVE_ENV = 'GITNEXUS_ANALYZE_PROGRESS_ACTIVE';
|
||||
|
||||
const DECLARATION_COMPANION_SUFFIXES = [
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ import type { VariableExtractor } from './variable-types.js';
|
|||
import type { ImportResolverFn } from './import-resolvers/types.js';
|
||||
import type { SyntaxNode } from './utils/ast-helpers.js';
|
||||
import type { CfgVisitor } from './cfg/types.js';
|
||||
import type { GraphNode, NodeLabel } from 'gitnexus-shared';
|
||||
import type { GraphNode, NodeLabel, ParameterTypeClass, RelationshipType } from 'gitnexus-shared';
|
||||
import type { ExtractedRoute } from './route-extractors/laravel.js';
|
||||
import type { SharedSpringType } from './route-extractors/spring-shared.js';
|
||||
import type {
|
||||
|
|
@ -120,6 +120,68 @@ export function mergeCanonicalDefinitionProperties<
|
|||
return { ...providerProperties, ...canonicalProperties } as Record<string, unknown> & TCanonical;
|
||||
}
|
||||
|
||||
export interface ProviderSemanticNode {
|
||||
readonly id: string;
|
||||
readonly label: NodeLabel;
|
||||
readonly properties: {
|
||||
readonly name: string;
|
||||
readonly filePath: string;
|
||||
readonly startLine: number;
|
||||
readonly endLine: number;
|
||||
readonly language: SupportedLanguages;
|
||||
readonly isExported: boolean;
|
||||
readonly qualifiedName?: string;
|
||||
readonly parameterCount?: number;
|
||||
readonly requiredParameterCount?: number;
|
||||
readonly parameterTypes?: readonly string[];
|
||||
readonly parameterTypeClasses?: readonly ParameterTypeClass[];
|
||||
readonly returnType?: string;
|
||||
readonly declaredType?: string;
|
||||
readonly visibility?: string;
|
||||
readonly isStatic?: boolean;
|
||||
readonly isReadonly?: boolean;
|
||||
readonly [key: string]: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
export type ProviderSemanticRelationshipType = Extract<
|
||||
RelationshipType,
|
||||
'DECLARES' | 'DEFINES' | 'HAS_METHOD' | 'HAS_PROPERTY'
|
||||
>;
|
||||
|
||||
export interface ProviderSemanticRelationship {
|
||||
readonly id: string;
|
||||
readonly sourceId: string;
|
||||
readonly targetId: string;
|
||||
readonly type: ProviderSemanticRelationshipType;
|
||||
readonly confidence: number;
|
||||
readonly reason: string;
|
||||
}
|
||||
|
||||
export interface ProviderSemanticSymbol {
|
||||
readonly filePath: string;
|
||||
readonly name: string;
|
||||
readonly nodeId: string;
|
||||
readonly type: NodeLabel;
|
||||
readonly qualifiedName?: string;
|
||||
readonly parameterCount?: number;
|
||||
readonly requiredParameterCount?: number;
|
||||
readonly parameterTypes?: readonly string[];
|
||||
readonly parameterTypeClasses?: readonly ParameterTypeClass[];
|
||||
readonly returnType?: string;
|
||||
readonly declaredType?: string;
|
||||
readonly ownerId?: string;
|
||||
readonly visibility?: string;
|
||||
readonly isStatic?: boolean;
|
||||
readonly isReadonly?: boolean;
|
||||
}
|
||||
|
||||
export interface ProviderSemanticGraph {
|
||||
readonly nodes: readonly ProviderSemanticNode[];
|
||||
readonly relationships: readonly ProviderSemanticRelationship[];
|
||||
readonly symbols: readonly ProviderSemanticSymbol[];
|
||||
}
|
||||
|
||||
// ── Strategy tag types ─────────────────────────────────────────────────────
|
||||
// NOTE: `MroStrategy` is defined in `gitnexus-shared` and re-exported above
|
||||
// so `core/ingestion/model/resolve.ts` can consume it without importing from
|
||||
|
|
@ -230,6 +292,29 @@ interface LanguageProviderConfig {
|
|||
*/
|
||||
readonly runtimeSymbolStrategy?: RuntimeSymbolStrategy;
|
||||
|
||||
/**
|
||||
* Optional content-based language classifier. The filename detector remains
|
||||
* the default source of truth; this hook lets a provider claim ambiguous
|
||||
* files only when the source text carries language-specific evidence.
|
||||
*
|
||||
* Used for extensions shared by several languages, where mapping the suffix
|
||||
* globally would steal files from an existing provider. Implementations must
|
||||
* be deterministic and conservative: false negatives are acceptable, false
|
||||
* positives change which parser and resolver consumes the file.
|
||||
*
|
||||
* Default: undefined (provider never overrides filename detection).
|
||||
*/
|
||||
readonly classifyFileContent?: (filePath: string, sourceText: string) => boolean;
|
||||
|
||||
/**
|
||||
* Cheap path-only prefilter for `classifyFileContent`. When supplied, callers
|
||||
* can avoid loading source text for files this provider would never claim.
|
||||
*
|
||||
* Default: undefined (only callers that already have content invoke
|
||||
* `classifyFileContent`).
|
||||
*/
|
||||
readonly shouldClassifyFileContent?: (filePath: string) => boolean;
|
||||
|
||||
// ── Core (required) ───────────────────────────────────────────────
|
||||
/** Type extraction: declarations, initializers, for-loop bindings */
|
||||
readonly typeConfig: LanguageTypeConfig;
|
||||
|
|
@ -643,6 +728,23 @@ interface LanguageProviderConfig {
|
|||
repo: RepoConstants,
|
||||
) => string | null;
|
||||
|
||||
/**
|
||||
* Optional provider-owned semantic graph extraction for languages whose
|
||||
* stable symbol identities cannot be represented by the generic query
|
||||
* pipeline's `(label, filePath, qualifiedName)` rule.
|
||||
*
|
||||
* Runs in the parse worker after tree-sitter has parsed the file and after
|
||||
* `extractParsedFile` has produced the scope-resolution artifact. The hook is
|
||||
* deterministic and AST-based: it receives the already-parsed tree and must
|
||||
* return plain graph nodes/relationships/symbol-table rows. Existing
|
||||
* providers leave it undefined, preserving the generic capture path exactly.
|
||||
*/
|
||||
readonly extractSemanticGraph?: (
|
||||
tree: Parser.Tree,
|
||||
filePath: string,
|
||||
sourceText: string,
|
||||
) => ProviderSemanticGraph;
|
||||
|
||||
// ── Noise filtering ────────────────────────────────────────────────
|
||||
/** Built-in/stdlib names that should be filtered from the call graph for this language.
|
||||
* Default: undefined (no language-specific filtering). */
|
||||
|
|
|
|||
|
|
@ -8,7 +8,11 @@
|
|||
* 4. Run `tsc --noEmit` to verify
|
||||
*/
|
||||
|
||||
import { SupportedLanguages, isBladeTemplateFilename } from 'gitnexus-shared';
|
||||
import {
|
||||
SupportedLanguages,
|
||||
getLanguageFromFilename,
|
||||
isBladeTemplateFilename,
|
||||
} from 'gitnexus-shared';
|
||||
import type { LanguageProvider } from '../language-provider.js';
|
||||
|
||||
import { typescriptProvider, javascriptProvider } from './typescript.js';
|
||||
|
|
@ -19,6 +23,7 @@ import { goProvider } from './go.js';
|
|||
import { rustProvider } from './rust.js';
|
||||
import { csharpProvider } from './csharp.js';
|
||||
import { cProvider, cppProvider } from './c-cpp.js';
|
||||
import { objectiveCProvider } from './objective-c.js';
|
||||
import { phpProvider } from './php.js';
|
||||
import { rubyProvider } from './ruby.js';
|
||||
import { swiftProvider } from './swift.js';
|
||||
|
|
@ -38,6 +43,7 @@ export const providers = {
|
|||
[SupportedLanguages.CSharp]: csharpProvider,
|
||||
[SupportedLanguages.C]: cProvider,
|
||||
[SupportedLanguages.CPlusPlus]: cppProvider,
|
||||
[SupportedLanguages.ObjectiveC]: objectiveCProvider,
|
||||
[SupportedLanguages.PHP]: phpProvider,
|
||||
[SupportedLanguages.Ruby]: rubyProvider,
|
||||
[SupportedLanguages.Swift]: swiftProvider,
|
||||
|
|
@ -70,3 +76,39 @@ export function getProviderForFile(filePath: string): LanguageProvider | null {
|
|||
const basename = filePath.slice(filePath.lastIndexOf('/') + 1);
|
||||
return extensionMap.get(ext) ?? extensionMap.get(basename) ?? null;
|
||||
}
|
||||
|
||||
/** Return the provider whose content classifier confidently claims this file. */
|
||||
export function getProviderForFileContent(
|
||||
filePath: string,
|
||||
content: string,
|
||||
): LanguageProvider | null {
|
||||
if (isBladeTemplateFilename(filePath)) return null;
|
||||
|
||||
for (const provider of Object.values(providers)) {
|
||||
if (provider.classifyFileContent?.(filePath, content) === true) return provider;
|
||||
}
|
||||
const byExtension = getProviderForFile(filePath);
|
||||
if (byExtension) return byExtension;
|
||||
// Filename-only languages (Rakefile, Gemfile, …) live in
|
||||
// getLanguageFromFilename, not provider.extensions.
|
||||
const filenameLang = getLanguageFromFilename(filePath);
|
||||
return filenameLang === null ? null : providers[filenameLang];
|
||||
}
|
||||
|
||||
/** True when at least one provider wants source text before language bucketing. */
|
||||
export function needsContentLanguageClassification(filePath: string): boolean {
|
||||
if (isBladeTemplateFilename(filePath)) return false;
|
||||
return Object.values(providers).some(
|
||||
(provider) =>
|
||||
provider.classifyFileContent !== undefined &&
|
||||
provider.shouldClassifyFileContent?.(filePath) === true,
|
||||
);
|
||||
}
|
||||
|
||||
/** Return the effective language for a file, optionally using source content. */
|
||||
export function getLanguageForFileContent(
|
||||
filePath: string,
|
||||
content: string,
|
||||
): SupportedLanguages | null {
|
||||
return getProviderForFileContent(filePath, content)?.id ?? null;
|
||||
}
|
||||
|
|
|
|||
343
gitnexus/src/core/ingestion/languages/objective-c.ts
Normal file
343
gitnexus/src/core/ingestion/languages/objective-c.ts
Normal file
|
|
@ -0,0 +1,343 @@
|
|||
import path from 'path';
|
||||
import {
|
||||
SupportedLanguages,
|
||||
type CaptureMatch,
|
||||
type ParsedImport,
|
||||
type ParsedTypeBinding,
|
||||
} from 'gitnexus-shared';
|
||||
import Parser from 'tree-sitter';
|
||||
import { defineLanguage } from '../language-provider.js';
|
||||
import type { ImportResolverFn } from '../import-resolvers/types.js';
|
||||
import { getLanguageGrammar } from '../../tree-sitter/parser-loader.js';
|
||||
import { parseSourceSafe } from '../../tree-sitter/safe-parse.js';
|
||||
import { assertCloneable } from '../workers/clone-safety.js';
|
||||
import { preprocessObjectiveCMacroMarkers } from './objective-c/macro-marker-preprocess.js';
|
||||
import {
|
||||
buildObjectiveCSemanticGraph,
|
||||
buildObjectiveCScopeCaptures,
|
||||
collectObjectiveCCaptureSideChannel,
|
||||
collectObjectiveCFacts,
|
||||
parseObjCType,
|
||||
setObjectiveCFileFacts,
|
||||
} from './objective-c/facts.js';
|
||||
|
||||
const OBJECTIVE_C_SCOPE_QUERY = `((translation_unit) @objc.root)`;
|
||||
|
||||
const EMPTY_TYPE_CONFIG = {
|
||||
declarationNodeTypes: new Set<string>(),
|
||||
extractDeclaration: () => null,
|
||||
extractParameter: () => null,
|
||||
};
|
||||
|
||||
const noImportResolution: ImportResolverFn = () => null;
|
||||
|
||||
function normalizedExt(filePath: string): string {
|
||||
return path.extname(filePath).toLowerCase();
|
||||
}
|
||||
|
||||
function isObjectiveCSourcePath(filePath: string): boolean {
|
||||
const ext = normalizedExt(filePath);
|
||||
return ext === '.m' || ext === '.mm';
|
||||
}
|
||||
|
||||
function isHeaderPath(filePath: string): boolean {
|
||||
return normalizedExt(filePath) === '.h';
|
||||
}
|
||||
|
||||
const OBJECTIVE_C_HEADER_NODE_TYPES = new Set([
|
||||
'class_declaration',
|
||||
'class_interface',
|
||||
'class_implementation',
|
||||
'compatibility_alias_declaration',
|
||||
'module_import',
|
||||
'protocol_declaration',
|
||||
// Embedding chunks a Method node's snippet, not the enclosing @interface.
|
||||
// tree-sitter-objc only emits these for +/- declarators, not C++ functions.
|
||||
'method_declaration',
|
||||
'method_definition',
|
||||
]);
|
||||
|
||||
const OBJECTIVE_C_HEADER_DIRECTIVES = [
|
||||
'@class',
|
||||
'@compatibility_alias',
|
||||
'@import',
|
||||
'@implementation',
|
||||
'@interface',
|
||||
'@protocol',
|
||||
];
|
||||
|
||||
function hasObjectiveCHeaderSyntax(sourceText: string): boolean {
|
||||
try {
|
||||
const tree = parseObjectiveCSource(sourceText);
|
||||
const stack: Parser.SyntaxNode[] = [tree.rootNode];
|
||||
while (stack.length > 0) {
|
||||
const node = stack.pop();
|
||||
if (node === undefined) continue;
|
||||
if (OBJECTIVE_C_HEADER_NODE_TYPES.has(node.type)) return true;
|
||||
for (let i = node.namedChildCount - 1; i >= 0; i--) {
|
||||
const child = node.namedChild(i);
|
||||
if (child !== null) stack.push(child);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Keep unambiguous Objective-C headers on the normal unavailable-parser path.
|
||||
return hasObjectiveCHeaderDirective(sourceText) || hasObjectiveCMethodDeclarator(sourceText);
|
||||
}
|
||||
// Bare method snippets (`- (void)run;`) are not `method_declaration` at
|
||||
// translation-unit scope; the grammar only emits that under @interface.
|
||||
return hasObjectiveCMethodDeclarator(sourceText);
|
||||
}
|
||||
|
||||
/**
|
||||
* Line-leading `+/- (Type)selector` used when the tree has no @interface and
|
||||
* when the grammar cannot load. `-(x);` does not match: `)` is not followed
|
||||
* by a selector identifier.
|
||||
*/
|
||||
function hasObjectiveCMethodDeclarator(sourceText: string): boolean {
|
||||
return /(?:^|[\r\n])[ \t]*[+-][ \t]*\([^;\n]+\)[ \t]*[A-Za-z_]/.test(
|
||||
sourceTextWithoutComments(sourceText),
|
||||
);
|
||||
}
|
||||
|
||||
/** Replace comments with spaces so line-leading method probes see only code. */
|
||||
function sourceTextWithoutComments(sourceText: string): string {
|
||||
const chars = sourceText.split('');
|
||||
let index = 0;
|
||||
let state: 'code' | 'line-comment' | 'block-comment' | 'single-quote' | 'double-quote' = 'code';
|
||||
|
||||
while (index < chars.length) {
|
||||
const current = chars[index];
|
||||
const next = chars[index + 1];
|
||||
|
||||
if (state === 'line-comment') {
|
||||
if (current === '\n' || current === '\r') state = 'code';
|
||||
else chars[index] = ' ';
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
if (state === 'block-comment') {
|
||||
if (current === '*' && next === '/') {
|
||||
chars[index] = ' ';
|
||||
chars[index + 1] = ' ';
|
||||
state = 'code';
|
||||
index += 2;
|
||||
} else {
|
||||
if (current !== '\n' && current !== '\r') chars[index] = ' ';
|
||||
index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (state === 'single-quote' || state === 'double-quote') {
|
||||
if (current === '\\') {
|
||||
index += 2;
|
||||
} else if (
|
||||
(state === 'single-quote' && current === "'") ||
|
||||
(state === 'double-quote' && current === '"')
|
||||
) {
|
||||
state = 'code';
|
||||
index++;
|
||||
} else {
|
||||
index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current === '/' && next === '/') {
|
||||
chars[index] = ' ';
|
||||
chars[index + 1] = ' ';
|
||||
state = 'line-comment';
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (current === '/' && next === '*') {
|
||||
chars[index] = ' ';
|
||||
chars[index + 1] = ' ';
|
||||
state = 'block-comment';
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (current === "'") {
|
||||
state = 'single-quote';
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
if (current === '"') {
|
||||
state = 'double-quote';
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
return chars.join('');
|
||||
}
|
||||
|
||||
function hasObjectiveCHeaderDirective(sourceText: string): boolean {
|
||||
let index = 0;
|
||||
let state: 'code' | 'line-comment' | 'block-comment' | 'single-quote' | 'double-quote' = 'code';
|
||||
|
||||
while (index < sourceText.length) {
|
||||
const current = sourceText[index];
|
||||
const next = sourceText[index + 1];
|
||||
|
||||
if (state === 'line-comment') {
|
||||
if (current === '\n' || current === '\r') state = 'code';
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
if (state === 'block-comment') {
|
||||
if (current === '*' && next === '/') {
|
||||
state = 'code';
|
||||
index += 2;
|
||||
} else {
|
||||
index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (state === 'single-quote' || state === 'double-quote') {
|
||||
if (current === '\\') {
|
||||
index += 2;
|
||||
} else if (
|
||||
(state === 'single-quote' && current === "'") ||
|
||||
(state === 'double-quote' && current === '"')
|
||||
) {
|
||||
state = 'code';
|
||||
index++;
|
||||
} else {
|
||||
index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current === '/' && next === '/') {
|
||||
state = 'line-comment';
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (current === '/' && next === '*') {
|
||||
state = 'block-comment';
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (current === "'") {
|
||||
state = 'single-quote';
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
if (current === '"') {
|
||||
state = 'double-quote';
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
if (current === '@') {
|
||||
const directive = OBJECTIVE_C_HEADER_DIRECTIVES.find((candidate) =>
|
||||
sourceText.startsWith(candidate, index),
|
||||
);
|
||||
if (directive !== undefined && !isIdentifierCharacter(sourceText[index + directive.length])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function isIdentifierCharacter(character: string | undefined): boolean {
|
||||
return character !== undefined && /[A-Za-z0-9_]/.test(character);
|
||||
}
|
||||
|
||||
export function classifyObjectiveCFileContent(filePath: string, sourceText: string): boolean {
|
||||
if (isObjectiveCSourcePath(filePath)) return true;
|
||||
if (!isHeaderPath(filePath)) return false;
|
||||
return hasObjectiveCHeaderSyntax(sourceText);
|
||||
}
|
||||
|
||||
function parseObjectiveCSource(sourceText: string): Parser.Tree {
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(getLanguageGrammar(SupportedLanguages.ObjectiveC));
|
||||
return parseSourceSafe(
|
||||
parser,
|
||||
preprocessObjectiveCMacroMarkers(sourceText),
|
||||
undefined,
|
||||
undefined,
|
||||
'Objective-C source',
|
||||
);
|
||||
}
|
||||
|
||||
function treeFromCachedOrSource(cachedTree: unknown, sourceText: string): Parser.Tree {
|
||||
if (cachedTree !== undefined && looksLikeTree(cachedTree)) return cachedTree;
|
||||
return parseObjectiveCSource(sourceText);
|
||||
}
|
||||
|
||||
function looksLikeTree(value: unknown): value is Parser.Tree {
|
||||
return (
|
||||
value !== null &&
|
||||
typeof value === 'object' &&
|
||||
'rootNode' in value &&
|
||||
(value as { rootNode?: unknown }).rootNode !== undefined
|
||||
);
|
||||
}
|
||||
|
||||
function interpretObjectiveCImport(captures: CaptureMatch): ParsedImport | null {
|
||||
const source = captures['@import.source'];
|
||||
if (source === undefined || source.text.trim().length === 0) return null;
|
||||
const targetRaw = source.text.trim();
|
||||
const kind = captures['@import.kind']?.text.trim();
|
||||
const isSystemHeader = targetRaw.startsWith('<') && targetRaw.endsWith('>');
|
||||
return {
|
||||
kind: 'side-effect',
|
||||
// Scope resolution needs to distinguish a quoted header path from a bare
|
||||
// @import module name and an angle-bracket system header. The latter stays
|
||||
// wrapped so the Objective-C resolver can fail closed instead of resolving
|
||||
// a framework header to a same-named local file.
|
||||
targetRaw:
|
||||
isSystemHeader || kind === 'module' || targetRaw.startsWith('./')
|
||||
? targetRaw
|
||||
: `./${targetRaw}`,
|
||||
};
|
||||
}
|
||||
|
||||
function interpretObjectiveCTypeBinding(captures: CaptureMatch): ParsedTypeBinding | null {
|
||||
const name = captures['@type-binding.name'];
|
||||
const type = captures['@type-binding.type'];
|
||||
if (name === undefined || type === undefined) return null;
|
||||
const parsed = parseObjCType(type.text);
|
||||
return {
|
||||
boundName: name.text,
|
||||
rawTypeName: parsed?.name ?? parsed?.raw ?? type.text,
|
||||
declaredSpelling: type.text,
|
||||
source: 'annotation',
|
||||
};
|
||||
}
|
||||
|
||||
export const objectiveCProvider = defineLanguage({
|
||||
id: SupportedLanguages.ObjectiveC,
|
||||
extensions: ['.m', '.mm'],
|
||||
treeSitterQueries: OBJECTIVE_C_SCOPE_QUERY,
|
||||
typeConfig: EMPTY_TYPE_CONFIG,
|
||||
exportChecker: () => true,
|
||||
importResolver: noImportResolution,
|
||||
classifyFileContent: classifyObjectiveCFileContent,
|
||||
shouldClassifyFileContent: isHeaderPath,
|
||||
preprocessSource: preprocessObjectiveCMacroMarkers,
|
||||
importsExecuteWhereWritten: false,
|
||||
|
||||
emitScopeCaptures: (sourceText, filePath, cachedTree): readonly CaptureMatch[] => {
|
||||
const tree = treeFromCachedOrSource(cachedTree, sourceText);
|
||||
const facts = collectObjectiveCFacts(tree, filePath);
|
||||
setObjectiveCFileFacts(facts);
|
||||
return buildObjectiveCScopeCaptures(facts, tree.rootNode);
|
||||
},
|
||||
|
||||
collectCaptureSideChannel: (filePath) =>
|
||||
assertCloneable(collectObjectiveCCaptureSideChannel(filePath)),
|
||||
|
||||
interpretImport: interpretObjectiveCImport,
|
||||
interpretTypeBinding: interpretObjectiveCTypeBinding,
|
||||
|
||||
extractSemanticGraph: (tree, filePath) => {
|
||||
const facts = collectObjectiveCFacts(tree, filePath);
|
||||
setObjectiveCFileFacts(facts);
|
||||
return buildObjectiveCSemanticGraph(facts);
|
||||
},
|
||||
});
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import type { AnalysisFeatureDescriptor } from '../../../analysis-features.js';
|
||||
import {
|
||||
OBJECTIVE_C_GRAMMAR_PACKAGE,
|
||||
OBJECTIVE_C_GRAMMAR_VERSION,
|
||||
OBJECTIVE_C_PROVIDER_VERSION,
|
||||
} from './facts.js';
|
||||
|
||||
function isObjectiveCProviderCandidatePath(filePath: string): boolean {
|
||||
const normalized = filePath.replaceAll('\\', '/').toLowerCase();
|
||||
return normalized.endsWith('.m') || normalized.endsWith('.mm') || normalized.endsWith('.h');
|
||||
}
|
||||
|
||||
/**
|
||||
* Durable metadata stamp for Objective-C semantic indexing. The feature id
|
||||
* carries provider and grammar versions verbatim so a semantic identity/edge
|
||||
* change records the exact producer in index metadata and forces a full rebuild.
|
||||
*
|
||||
* `.h` is included only as a path-level rebuild predicate; content classification
|
||||
* still decides whether a header is actually parsed as Objective-C.
|
||||
*/
|
||||
export const OBJECTIVE_C_PROVIDER_FEATURE: AnalysisFeatureDescriptor = {
|
||||
id:
|
||||
`objective-c.provider-${OBJECTIVE_C_PROVIDER_VERSION}.` +
|
||||
`${OBJECTIVE_C_GRAMMAR_PACKAGE}-${OBJECTIVE_C_GRAMMAR_VERSION}`,
|
||||
version: 1,
|
||||
appliesTo: (filePaths) => filePaths.some(isObjectiveCProviderCandidatePath),
|
||||
};
|
||||
|
|
@ -0,0 +1,138 @@
|
|||
/**
|
||||
* Header / implementation visibility for `populateNamespaceSiblings`.
|
||||
*
|
||||
* Objective-C has no Java package or C# namespace. The compilation unit is
|
||||
* the workspace analog: `Foo.h` + `Foo.m` in the same directory, and files
|
||||
* that declare or implement the same class / protocol, see each other's
|
||||
* top-level defs without a second `#import` of a sibling they already share.
|
||||
* Bindings go through the append-only `bindingAugmentations` channel
|
||||
* (`origin: 'namespace'`), matching Swift / Go.
|
||||
*/
|
||||
|
||||
import type { BindingRef, ParsedFile, ScopeId, SymbolDefinition } from 'gitnexus-shared';
|
||||
import type { ScopeResolutionIndexes } from '../../model/scope-resolution-indexes.js';
|
||||
import {
|
||||
isInternalObjectiveCFunctionDef,
|
||||
objectiveCFactsFromParsedFiles,
|
||||
type ObjCContainerFact,
|
||||
} from './facts.js';
|
||||
|
||||
export function populateObjectiveCCompilationUnitSiblings(
|
||||
parsedFiles: readonly ParsedFile[],
|
||||
indexes: ScopeResolutionIndexes,
|
||||
): void {
|
||||
const groups = new Map<string, ParsedFile[]>();
|
||||
const byPath = new Map<string, ParsedFile>();
|
||||
for (const parsed of parsedFiles) {
|
||||
byPath.set(parsed.filePath, parsed);
|
||||
const stemKey = compilationUnitStemKey(parsed.filePath);
|
||||
if (stemKey !== undefined) appendGroup(groups, stemKey, parsed);
|
||||
}
|
||||
|
||||
const internalFunctionIds = new Set<string>();
|
||||
for (const fact of objectiveCFactsFromParsedFiles(parsedFiles)) {
|
||||
const parsed = byPath.get(fact.filePath);
|
||||
if (parsed !== undefined) {
|
||||
for (const container of fact.containers) {
|
||||
appendGroup(groups, `type:${typeVisibilityKey(container)}`, parsed);
|
||||
}
|
||||
}
|
||||
for (const fn of fact.functions) {
|
||||
if (fn.linkage === 'internal') internalFunctionIds.add(fn.nodeId);
|
||||
}
|
||||
}
|
||||
|
||||
const augmentations = indexes.bindingAugmentations as Map<ScopeId, Map<string, BindingRef[]>>;
|
||||
for (const group of groups.values()) {
|
||||
if (group.length < 2) continue;
|
||||
const unique = uniqueFiles(group);
|
||||
if (unique.length < 2) continue;
|
||||
for (const receiver of unique) {
|
||||
const receiverModule = indexes.moduleScopes.byFilePath.get(receiver.filePath);
|
||||
if (receiverModule === undefined) continue;
|
||||
for (const source of unique) {
|
||||
if (source.filePath === receiver.filePath) continue;
|
||||
for (const def of source.localDefs as SymbolDefinition[]) {
|
||||
if (internalFunctionIds.has(def.nodeId) || isInternalObjectiveCFunctionDef(def)) {
|
||||
continue;
|
||||
}
|
||||
const name = siblingBindingName(def);
|
||||
if (name === '') continue;
|
||||
const bucket = getAugmentationBucket(augmentations, receiverModule, name);
|
||||
if (bucket.some((binding) => binding.def.nodeId === def.nodeId)) continue;
|
||||
bucket.push({ def, origin: 'namespace' });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function compilationUnitStemKey(filePath: string): string | undefined {
|
||||
const normalized = filePath.replaceAll('\\', '/');
|
||||
const slash = normalized.lastIndexOf('/');
|
||||
const base = slash === -1 ? normalized : normalized.slice(slash + 1);
|
||||
const ext = extensionOf(base);
|
||||
if (ext !== '.h' && ext !== '.m' && ext !== '.mm') return undefined;
|
||||
const dir = slash === -1 ? '' : normalized.slice(0, slash);
|
||||
return `stem:${dir}/${base.slice(0, -ext.length)}`;
|
||||
}
|
||||
|
||||
/** Class/category/extension files share one group; a same-named protocol does not. */
|
||||
function typeVisibilityKey(container: ObjCContainerFact): string {
|
||||
if (container.kind === 'category' || container.kind === 'extension') {
|
||||
return `class:${container.hostClass ?? container.name}`;
|
||||
}
|
||||
return `${container.kind}:${container.name}`;
|
||||
}
|
||||
|
||||
function siblingBindingName(def: SymbolDefinition): string {
|
||||
const qualified = def.qualifiedName ?? '';
|
||||
if (qualified.startsWith('objc:')) {
|
||||
const colon = qualified.lastIndexOf(':');
|
||||
return colon === -1 ? qualified : qualified.slice(colon + 1);
|
||||
}
|
||||
return qualified.split('.').pop() ?? qualified;
|
||||
}
|
||||
|
||||
function extensionOf(fileName: string): string {
|
||||
const dot = fileName.lastIndexOf('.');
|
||||
return dot === -1 ? '' : fileName.slice(dot).toLowerCase();
|
||||
}
|
||||
|
||||
function appendGroup(groups: Map<string, ParsedFile[]>, key: string, parsed: ParsedFile): void {
|
||||
const bucket = groups.get(key);
|
||||
if (bucket === undefined) {
|
||||
groups.set(key, [parsed]);
|
||||
return;
|
||||
}
|
||||
bucket.push(parsed);
|
||||
}
|
||||
|
||||
function uniqueFiles(group: readonly ParsedFile[]): ParsedFile[] {
|
||||
const seen = new Set<string>();
|
||||
const out: ParsedFile[] = [];
|
||||
for (const parsed of group) {
|
||||
if (seen.has(parsed.filePath)) continue;
|
||||
seen.add(parsed.filePath);
|
||||
out.push(parsed);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function getAugmentationBucket(
|
||||
augmentations: Map<ScopeId, Map<string, BindingRef[]>>,
|
||||
scopeId: ScopeId,
|
||||
name: string,
|
||||
): BindingRef[] {
|
||||
let scopeBindings = augmentations.get(scopeId);
|
||||
if (scopeBindings === undefined) {
|
||||
scopeBindings = new Map<string, BindingRef[]>();
|
||||
augmentations.set(scopeId, scopeBindings);
|
||||
}
|
||||
let bucket = scopeBindings.get(name);
|
||||
if (bucket === undefined) {
|
||||
bucket = [];
|
||||
scopeBindings.set(name, bucket);
|
||||
}
|
||||
return bucket;
|
||||
}
|
||||
1653
gitnexus/src/core/ingestion/languages/objective-c/facts.ts
Normal file
1653
gitnexus/src/core/ingestion/languages/objective-c/facts.ts
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,161 @@
|
|||
/**
|
||||
* Resolve an Objective-C `#import` / `#include` / `@import` to a workspace file.
|
||||
*
|
||||
* Quoted imports keep the suffix-index first-path tie-break used before the
|
||||
* workspace config existed. Angle-bracket imports never suffix-match the
|
||||
* source tree — they only hit in-repo frameworks and HEADER_SEARCH_PATHS —
|
||||
* so `<Foundation/Foundation.h>` stays unresolved even when a decoy
|
||||
* `Foundation.h` exists. `@import` names resolve only through a scanned
|
||||
* module map.
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import { perFileSet } from '../../import-resolvers/per-file-set.js';
|
||||
import {
|
||||
coerceObjectiveCResolutionConfig,
|
||||
normalizeRepoPath,
|
||||
type ObjectiveCResolutionConfig,
|
||||
} from './resolution-config.js';
|
||||
|
||||
interface ObjectiveCImportIndex {
|
||||
readonly filePathSet: ReadonlySet<string>;
|
||||
readonly suffixMatches: ReadonlyMap<
|
||||
string,
|
||||
{ readonly filePath: string; readonly order: number }
|
||||
>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-pass memo of `allFilePaths ∪ scanned headers`. Identity is load-bearing
|
||||
* for `getObjectiveCImportIndex` (same rule as C/C++ `augmentedFilePathsFor`).
|
||||
* This memo stays private to Objective-C — sharing it with C would hand each
|
||||
* language the other's suffix index.
|
||||
*/
|
||||
const augmentedFilePathsFor = perFileSet((allFilePaths: ReadonlySet<string>) =>
|
||||
perFileSet((headerPaths: ReadonlySet<string>): ReadonlySet<string> => {
|
||||
const set = new Set(allFilePaths);
|
||||
for (const header of headerPaths) set.add(header);
|
||||
return set;
|
||||
}),
|
||||
);
|
||||
|
||||
const getObjectiveCImportIndex = perFileSet(
|
||||
(allFilePaths: ReadonlySet<string>): ObjectiveCImportIndex => {
|
||||
const filePaths = [...allFilePaths];
|
||||
const suffixMatches = new Map<string, { readonly filePath: string; readonly order: number }>();
|
||||
for (const [order, filePath] of filePaths.entries()) {
|
||||
const segments = normalizeRepoPath(filePath).split('/');
|
||||
for (let start = 1; start < segments.length; start++) {
|
||||
const suffix = segments.slice(start).join('/');
|
||||
if (!suffixMatches.has(suffix)) suffixMatches.set(suffix, { filePath, order });
|
||||
}
|
||||
}
|
||||
return { filePathSet: new Set(filePaths), suffixMatches };
|
||||
},
|
||||
);
|
||||
|
||||
export function resolveObjectiveCImportTarget(
|
||||
targetRaw: string,
|
||||
fromFile: string,
|
||||
allFilePaths: ReadonlySet<string>,
|
||||
resolutionConfig?: unknown,
|
||||
): string | null {
|
||||
const target = targetRaw.trim();
|
||||
if (target.length === 0) return null;
|
||||
|
||||
const config = coerceObjectiveCResolutionConfig(resolutionConfig);
|
||||
const searchPaths =
|
||||
config !== undefined && config.headers.size > 0
|
||||
? augmentedFilePathsFor(allFilePaths)(config.headers)
|
||||
: allFilePaths;
|
||||
|
||||
if (target.startsWith('<') && target.endsWith('>')) {
|
||||
return resolveAngleImport(target.slice(1, -1), searchPaths, config);
|
||||
}
|
||||
|
||||
const looksLikeFileImport =
|
||||
target.startsWith('.') || target.includes('/') || path.posix.extname(target).length > 0;
|
||||
if (!looksLikeFileImport) {
|
||||
return resolveModuleImport(target, searchPaths, config);
|
||||
}
|
||||
|
||||
return findImportCandidate(target, fromFile, getObjectiveCImportIndex(searchPaths), config);
|
||||
}
|
||||
|
||||
function resolveAngleImport(
|
||||
inner: string,
|
||||
searchPaths: ReadonlySet<string>,
|
||||
config: ObjectiveCResolutionConfig | undefined,
|
||||
): string | null {
|
||||
const normalized = normalizeRepoPath(inner);
|
||||
if (normalized.length === 0 || config === undefined) return null;
|
||||
|
||||
const slash = normalized.indexOf('/');
|
||||
if (slash > 0) {
|
||||
const framework = normalized.slice(0, slash);
|
||||
const rest = normalized.slice(slash + 1);
|
||||
const headersDir = config.frameworks.get(framework);
|
||||
if (headersDir !== undefined) {
|
||||
const framed = normalizeRepoPath(`${headersDir}/${rest}`);
|
||||
if (searchPaths.has(framed)) return framed;
|
||||
}
|
||||
}
|
||||
|
||||
for (const searchPath of config.headerSearchPaths) {
|
||||
const candidate = normalizeRepoPath(`${searchPath}/${normalized}`);
|
||||
if (searchPaths.has(candidate)) return candidate;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolveModuleImport(
|
||||
moduleName: string,
|
||||
searchPaths: ReadonlySet<string>,
|
||||
config: ObjectiveCResolutionConfig | undefined,
|
||||
): string | null {
|
||||
if (config === undefined) return null;
|
||||
const exact = config.modules.get(moduleName);
|
||||
if (exact !== undefined && searchPaths.has(exact)) return exact;
|
||||
const root = moduleName.split('.')[0];
|
||||
if (root === undefined || root === moduleName) return null;
|
||||
const fallback = config.modules.get(root);
|
||||
return fallback !== undefined && searchPaths.has(fallback) ? fallback : null;
|
||||
}
|
||||
|
||||
function findImportCandidate(
|
||||
targetRaw: string,
|
||||
fromFile: string,
|
||||
importIndex: ObjectiveCImportIndex,
|
||||
config: ObjectiveCResolutionConfig | undefined,
|
||||
): string | null {
|
||||
const normalizedTarget = normalizeRepoPath(targetRaw);
|
||||
const fromDir = normalizeRepoPath(path.posix.dirname(normalizeRepoPath(fromFile)));
|
||||
const spelledCandidates = new Set<string>([
|
||||
normalizeRepoPath(path.posix.join(fromDir, normalizedTarget)),
|
||||
normalizedTarget,
|
||||
]);
|
||||
if (config !== undefined) {
|
||||
for (const searchPath of [...config.userHeaderSearchPaths, ...config.headerSearchPaths]) {
|
||||
spelledCandidates.add(normalizeRepoPath(`${searchPath}/${normalizedTarget}`));
|
||||
}
|
||||
}
|
||||
const ext = path.posix.extname(normalizedTarget);
|
||||
if (ext.length === 0) {
|
||||
for (const base of [...spelledCandidates]) {
|
||||
spelledCandidates.add(`${base}.h`);
|
||||
spelledCandidates.add(`${base}.m`);
|
||||
spelledCandidates.add(`${base}.mm`);
|
||||
}
|
||||
}
|
||||
for (const candidate of spelledCandidates) {
|
||||
if (importIndex.filePathSet.has(candidate)) return candidate;
|
||||
}
|
||||
let suffixMatch: { readonly filePath: string; readonly order: number } | undefined;
|
||||
for (const candidate of spelledCandidates) {
|
||||
const match = importIndex.suffixMatches.get(candidate);
|
||||
if (match !== undefined && (suffixMatch === undefined || match.order < suffixMatch.order)) {
|
||||
suffixMatch = match;
|
||||
}
|
||||
}
|
||||
return suffixMatch?.filePath ?? null;
|
||||
}
|
||||
|
|
@ -0,0 +1,283 @@
|
|||
/**
|
||||
* Normalize bare file-scope Objective-C macro markers before parsing.
|
||||
*
|
||||
* Headers commonly use macro pairs such as `RCT_EXTERN_C_BEGIN` and
|
||||
* `RCT_EXTERN_C_END` around C declarations. tree-sitter-objc does not expand
|
||||
* those macros; a bare invocation can put the parser into error recovery and
|
||||
* hide every Objective-C declaration that follows it. These markers do not
|
||||
* contribute syntax on their own, so we replace only the narrow, generic form
|
||||
* with spaces before parsing.
|
||||
*
|
||||
* This is deliberately not macro expansion or a framework-specific allowlist:
|
||||
* a candidate must be a whole, all-caps identifier at file scope. Function-like
|
||||
* macros, directives, statements, strings, and comments remain untouched.
|
||||
* Replacement preserves UTF-16 length and line endings exactly. Candidates are
|
||||
* ASCII-only, so their byte offsets are preserved as well.
|
||||
*/
|
||||
|
||||
interface ScanState {
|
||||
inBlockComment: boolean;
|
||||
inLineCommentContinuation: boolean;
|
||||
inCodeLineContinuation: boolean;
|
||||
inPreprocessorDirective: boolean;
|
||||
quote: '"' | "'" | undefined;
|
||||
braceDepth: number;
|
||||
parenDepth: number;
|
||||
objcDeclDepth: number;
|
||||
}
|
||||
|
||||
function isPreprocessorWhitespace(code: number): boolean {
|
||||
// C preprocessing whitespace includes space, horizontal tab, vertical tab,
|
||||
// and form feed. Newlines are split before this scanner sees a line.
|
||||
return code === 0x20 || code === 0x09 || code === 0x0b || code === 0x0c;
|
||||
}
|
||||
|
||||
function isBareMarkerIdentifier(line: string): boolean {
|
||||
let index = 0;
|
||||
while (index < line.length && isPreprocessorWhitespace(line.charCodeAt(index))) index++;
|
||||
|
||||
const identifierStart = index;
|
||||
const first = line.charCodeAt(index);
|
||||
if (!((first >= 0x41 && first <= 0x5a) || first === 0x5f)) return false;
|
||||
|
||||
let hasUppercaseLetter = false;
|
||||
while (index < line.length) {
|
||||
const code = line.charCodeAt(index);
|
||||
if (code >= 0x41 && code <= 0x5a) {
|
||||
hasUppercaseLetter = true;
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
if ((code >= 0x30 && code <= 0x39) || code === 0x5f) {
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (index === identifierStart || !hasUppercaseLetter) return false;
|
||||
|
||||
while (index < line.length && isPreprocessorWhitespace(line.charCodeAt(index))) index++;
|
||||
return index === line.length;
|
||||
}
|
||||
|
||||
function isIdentifierContinue(line: string, index: number): boolean {
|
||||
if (index >= line.length) return false;
|
||||
const code = line.charCodeAt(index);
|
||||
return (
|
||||
(code >= 0x41 && code <= 0x5a) ||
|
||||
(code >= 0x61 && code <= 0x7a) ||
|
||||
(code >= 0x30 && code <= 0x39) ||
|
||||
code === 0x5f
|
||||
);
|
||||
}
|
||||
|
||||
function leadingObjCDeclKeyword(line: string, inBlockComment = false): 'begin' | 'end' | null {
|
||||
let index = 0;
|
||||
if (inBlockComment) {
|
||||
const blockCommentEnd = line.indexOf('*/');
|
||||
if (blockCommentEnd < 0) return null;
|
||||
index = blockCommentEnd + 2;
|
||||
}
|
||||
while (index < line.length) {
|
||||
while (index < line.length && isPreprocessorWhitespace(line.charCodeAt(index))) index++;
|
||||
if (line.startsWith('//', index)) return null;
|
||||
if (line.startsWith('/*', index)) {
|
||||
const blockCommentEnd = line.indexOf('*/', index + 2);
|
||||
if (blockCommentEnd < 0) return null;
|
||||
index = blockCommentEnd + 2;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (line.startsWith('@end', index) && !isIdentifierContinue(line, index + 4)) return 'end';
|
||||
for (const keyword of ['@interface', '@protocol', '@implementation'] as const) {
|
||||
if (line.startsWith(keyword, index) && !isIdentifierContinue(line, index + keyword.length)) {
|
||||
return 'begin';
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function hasEscapedLineEnding(line: string): boolean {
|
||||
let trailingBackslashes = 0;
|
||||
for (let index = line.length - 1; index >= 0 && line.charCodeAt(index) === 0x5c; index--) {
|
||||
trailingBackslashes++;
|
||||
}
|
||||
return trailingBackslashes % 2 === 1;
|
||||
}
|
||||
|
||||
function startsPreprocessorDirective(line: string, state: ScanState): boolean {
|
||||
let index = 0;
|
||||
let inBlockComment = state.inBlockComment;
|
||||
|
||||
while (index < line.length) {
|
||||
if (inBlockComment) {
|
||||
const blockCommentEnd = line.indexOf('*/', index);
|
||||
if (blockCommentEnd < 0) return false;
|
||||
inBlockComment = false;
|
||||
index = blockCommentEnd + 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
while (index < line.length && isPreprocessorWhitespace(line.charCodeAt(index))) index++;
|
||||
if (line.startsWith('/*', index)) {
|
||||
inBlockComment = true;
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith('//', index)) return false;
|
||||
if (line.charCodeAt(index) !== 0x23) return false;
|
||||
|
||||
state.inBlockComment = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function scanBalancedDelimiters(code: number, state: ScanState): void {
|
||||
if (code === 0x28) state.parenDepth++;
|
||||
else if (code === 0x29) state.parenDepth = Math.max(0, state.parenDepth - 1);
|
||||
else if (code === 0x7b) state.braceDepth++;
|
||||
else if (code === 0x7d) state.braceDepth = Math.max(0, state.braceDepth - 1);
|
||||
}
|
||||
|
||||
function scanLineBody(
|
||||
line: string,
|
||||
state: ScanState,
|
||||
startIndex: number,
|
||||
trackDelimiters: boolean,
|
||||
): void {
|
||||
for (let index = startIndex; index < line.length; index++) {
|
||||
const code = line.charCodeAt(index);
|
||||
const next = line.charCodeAt(index + 1);
|
||||
|
||||
if (state.inBlockComment) {
|
||||
if (code === 0x2a && next === 0x2f) {
|
||||
state.inBlockComment = false;
|
||||
index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state.quote !== undefined) {
|
||||
if (code === 0x5c) {
|
||||
index++;
|
||||
} else if (line[index] === state.quote) {
|
||||
state.quote = undefined;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (code === 0x2f && next === 0x2f) {
|
||||
state.inLineCommentContinuation = hasEscapedLineEnding(line);
|
||||
return;
|
||||
}
|
||||
if (code === 0x2f && next === 0x2a) {
|
||||
state.inBlockComment = true;
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
if (code === 0x22 || code === 0x27) {
|
||||
state.quote = line[index] as '"' | "'";
|
||||
continue;
|
||||
}
|
||||
// Directive replacement text is tokens, not C scopes. `{` in
|
||||
// `#define WRAP {` must not pin braceDepth for later file-scope markers.
|
||||
if (trackDelimiters) scanBalancedDelimiters(code, state);
|
||||
}
|
||||
}
|
||||
|
||||
function scanDirectiveLine(line: string, state: ScanState): void {
|
||||
const continued = hasEscapedLineEnding(line);
|
||||
scanLineBody(line, state, 0, false);
|
||||
state.inPreprocessorDirective = continued;
|
||||
// A quote opened in replacement text does not survive past the directive.
|
||||
if (!continued) state.quote = undefined;
|
||||
}
|
||||
|
||||
function scanLine(line: string, state: ScanState): void {
|
||||
if (state.inLineCommentContinuation) {
|
||||
state.inLineCommentContinuation = hasEscapedLineEnding(line);
|
||||
// A `// ... \` splice keeps the next physical line in the comment, but the
|
||||
// directive ends unless that line is itself backslash-continued.
|
||||
if (state.inPreprocessorDirective) {
|
||||
state.inPreprocessorDirective = state.inLineCommentContinuation;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (state.inPreprocessorDirective) {
|
||||
scanDirectiveLine(line, state);
|
||||
return;
|
||||
}
|
||||
if (startsPreprocessorDirective(line, state)) {
|
||||
// Directives can open a block comment (`#define X /*`) that continues
|
||||
// onto the next physical line. Scan comment/quote state only.
|
||||
scanDirectiveLine(line, state);
|
||||
return;
|
||||
}
|
||||
|
||||
if (state.inCodeLineContinuation) {
|
||||
scanLineBody(line, state, 0, true);
|
||||
if (state.quote !== undefined && !hasEscapedLineEnding(line)) state.quote = undefined;
|
||||
state.inCodeLineContinuation =
|
||||
!state.inLineCommentContinuation && !state.inBlockComment && hasEscapedLineEnding(line);
|
||||
return;
|
||||
}
|
||||
|
||||
const keyword = leadingObjCDeclKeyword(line, state.inBlockComment);
|
||||
if (keyword === 'begin') state.objcDeclDepth++;
|
||||
else if (keyword === 'end') state.objcDeclDepth = Math.max(0, state.objcDeclDepth - 1);
|
||||
|
||||
scanLineBody(line, state, 0, true);
|
||||
|
||||
if (state.quote !== undefined && !hasEscapedLineEnding(line)) state.quote = undefined;
|
||||
// Line-comment `\` continuation is not a C splice; it must not suppress
|
||||
// the next physical line's file-scope marker test.
|
||||
state.inCodeLineContinuation =
|
||||
!state.inLineCommentContinuation && !state.inBlockComment && hasEscapedLineEnding(line);
|
||||
}
|
||||
|
||||
/**
|
||||
* Elide bare, file-scope macro markers while preserving source positions.
|
||||
*
|
||||
* `_filePath` is accepted for the LanguageProvider hook signature. The
|
||||
* transform is based only on source syntax and deliberately has no framework
|
||||
* or repository-specific configuration.
|
||||
*/
|
||||
export function preprocessObjectiveCMacroMarkers(source: string, _filePath?: string): string {
|
||||
const state: ScanState = {
|
||||
inBlockComment: false,
|
||||
inLineCommentContinuation: false,
|
||||
inCodeLineContinuation: false,
|
||||
inPreprocessorDirective: false,
|
||||
quote: undefined,
|
||||
braceDepth: 0,
|
||||
parenDepth: 0,
|
||||
objcDeclDepth: 0,
|
||||
};
|
||||
const segments = source.split(/(\r\n|\n|\r)/);
|
||||
let changed = false;
|
||||
|
||||
for (let index = 0; index < segments.length; index += 2) {
|
||||
const line = segments[index];
|
||||
if (
|
||||
!state.inBlockComment &&
|
||||
!state.inLineCommentContinuation &&
|
||||
!state.inCodeLineContinuation &&
|
||||
!state.inPreprocessorDirective &&
|
||||
state.quote === undefined &&
|
||||
state.braceDepth === 0 &&
|
||||
state.parenDepth === 0 &&
|
||||
state.objcDeclDepth === 0 &&
|
||||
isBareMarkerIdentifier(line)
|
||||
) {
|
||||
segments[index] = ' '.repeat(line.length);
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
scanLine(line, state);
|
||||
}
|
||||
|
||||
return changed ? segments.join('') : source;
|
||||
}
|
||||
|
|
@ -0,0 +1,323 @@
|
|||
/**
|
||||
* Per-workspace Objective-C import config — the analog of TypeScript's
|
||||
* tsconfig, C#'s csproj scan, and C/C++'s header scan.
|
||||
*
|
||||
* Loaded once per analyze pass via `objectiveCScopeResolver.loadResolutionConfig`
|
||||
* and threaded into `resolveObjectiveCImportTarget`. `.h` files are usually
|
||||
* classified as C++ unless they contain Objective-C syntax, so the ObjC
|
||||
* resolver's `allFilePaths` can miss headers that `#import` / `#include`
|
||||
* still name. This scan is what makes those paths visible.
|
||||
*/
|
||||
|
||||
import { readdirSync, readFileSync, type Dirent } from 'fs';
|
||||
import { join, relative } from 'path';
|
||||
import { clearObjectiveCFileFacts } from './facts.js';
|
||||
|
||||
const HEADER_EXTENSIONS = new Set(['.h']);
|
||||
const MODULE_MAP_NAMES = new Set(['module.modulemap', 'module.map']);
|
||||
|
||||
export interface ObjectiveCResolutionConfig {
|
||||
/** Repo-relative `.h` paths discovered on disk (C/C++ header-scan analog). */
|
||||
readonly headers: ReadonlySet<string>;
|
||||
/**
|
||||
* xcconfig `HEADER_SEARCH_PATHS`. Used for both quoted and angle-bracket
|
||||
* imports — clang's system/project include path.
|
||||
*/
|
||||
readonly headerSearchPaths: readonly string[];
|
||||
/**
|
||||
* xcconfig `USER_HEADER_SEARCH_PATHS` plus in-repo `Headers` / `include`
|
||||
* roots. Quoted `#import "…"` only. Angle-bracket imports must not use
|
||||
* these, or `<Foundation/Foundation.h>` would bind a local decoy.
|
||||
*/
|
||||
readonly userHeaderSearchPaths: readonly string[];
|
||||
/** Framework name → repo-relative `*.framework/Headers` directory. */
|
||||
readonly frameworks: ReadonlyMap<string, string>;
|
||||
/** `@import` module name → umbrella / first header path. */
|
||||
readonly modules: ReadonlyMap<string, string>;
|
||||
}
|
||||
|
||||
export function emptyObjectiveCResolutionConfig(): ObjectiveCResolutionConfig {
|
||||
return {
|
||||
headers: new Set(),
|
||||
headerSearchPaths: [],
|
||||
userHeaderSearchPaths: [],
|
||||
frameworks: new Map(),
|
||||
modules: new Map(),
|
||||
};
|
||||
}
|
||||
|
||||
export function coerceObjectiveCResolutionConfig(
|
||||
value: unknown,
|
||||
): ObjectiveCResolutionConfig | undefined {
|
||||
if (value == null) return undefined;
|
||||
if (value instanceof Set) {
|
||||
return {
|
||||
headers: value as ReadonlySet<string>,
|
||||
headerSearchPaths: [],
|
||||
userHeaderSearchPaths: [],
|
||||
frameworks: new Map(),
|
||||
modules: new Map(),
|
||||
};
|
||||
}
|
||||
if (typeof value !== 'object') return undefined;
|
||||
const record = value as Partial<ObjectiveCResolutionConfig>;
|
||||
if (record.headers === undefined) return undefined;
|
||||
return {
|
||||
headers: record.headers,
|
||||
headerSearchPaths: record.headerSearchPaths ?? [],
|
||||
userHeaderSearchPaths: record.userHeaderSearchPaths ?? [],
|
||||
frameworks: record.frameworks ?? new Map(),
|
||||
modules: record.modules ?? new Map(),
|
||||
};
|
||||
}
|
||||
|
||||
export function loadObjectiveCResolutionConfig(repoPath: string): ObjectiveCResolutionConfig {
|
||||
// Worker capture facts are process-local and outlive a single analysis in
|
||||
// server mode. This hook runs once before each Objective-C workspace pass,
|
||||
// before ParsedFile side channels are restored for the current files.
|
||||
clearObjectiveCFileFacts();
|
||||
return scanObjectiveCWorkspace(repoPath);
|
||||
}
|
||||
|
||||
export function scanObjectiveCWorkspace(repoPath: string): ObjectiveCResolutionConfig {
|
||||
const headers = new Set<string>();
|
||||
const frameworks = new Map<string, string>();
|
||||
const moduleMapPaths: string[] = [];
|
||||
const xcconfigPaths: string[] = [];
|
||||
const implicitSearchRoots = new Set<string>();
|
||||
|
||||
walk(repoPath, repoPath, {
|
||||
headers,
|
||||
frameworks,
|
||||
moduleMapPaths,
|
||||
xcconfigPaths,
|
||||
implicitSearchRoots,
|
||||
});
|
||||
|
||||
const xcconfig = { headerSearchPaths: [] as string[], userHeaderSearchPaths: [] as string[] };
|
||||
for (const filePath of xcconfigPaths) {
|
||||
const parsed = parseXcconfigSearchPaths(readText(join(repoPath, filePath)));
|
||||
xcconfig.headerSearchPaths.push(...parsed.headerSearchPaths);
|
||||
xcconfig.userHeaderSearchPaths.push(...parsed.userHeaderSearchPaths);
|
||||
}
|
||||
const headerSearchPaths = uniquePaths(xcconfig.headerSearchPaths);
|
||||
const userHeaderSearchPaths = uniquePaths([
|
||||
...implicitSearchRoots,
|
||||
...xcconfig.userHeaderSearchPaths,
|
||||
]);
|
||||
|
||||
const modules = new Map<string, string>();
|
||||
for (const mapPath of moduleMapPaths) {
|
||||
for (const [name, header] of parseModuleMap(
|
||||
readText(join(repoPath, mapPath)),
|
||||
dirnamePosix(mapPath),
|
||||
)) {
|
||||
if (!modules.has(name)) modules.set(name, header);
|
||||
}
|
||||
}
|
||||
|
||||
return { headers, headerSearchPaths, userHeaderSearchPaths, frameworks, modules };
|
||||
}
|
||||
|
||||
interface ScanSink {
|
||||
readonly headers: Set<string>;
|
||||
readonly frameworks: Map<string, string>;
|
||||
readonly moduleMapPaths: string[];
|
||||
readonly xcconfigPaths: string[];
|
||||
readonly implicitSearchRoots: Set<string>;
|
||||
}
|
||||
|
||||
function walk(dir: string, root: string, sink: ScanSink): void {
|
||||
let entries: Dirent[];
|
||||
try {
|
||||
entries = readdirSync(dir, { withFileTypes: true, encoding: 'utf8' });
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
for (const entry of entries) {
|
||||
const name = entry.name;
|
||||
const full = join(dir, name);
|
||||
if (entry.isDirectory()) {
|
||||
if (shouldSkipDirectory(name)) continue;
|
||||
const relativeDir = normalizeRepoPath(relative(root, full));
|
||||
if (name.endsWith('.framework')) {
|
||||
const frameworkName = name.slice(0, -'.framework'.length);
|
||||
if (frameworkName.length > 0 && !sink.frameworks.has(frameworkName)) {
|
||||
sink.frameworks.set(frameworkName, `${relativeDir}/Headers`);
|
||||
}
|
||||
}
|
||||
if (name === 'Headers' || name === 'include') {
|
||||
sink.implicitSearchRoots.add(relativeDir);
|
||||
}
|
||||
walk(full, root, sink);
|
||||
} else if (entry.isFile()) {
|
||||
const relativePath = normalizeRepoPath(relative(root, full));
|
||||
const ext = name.slice(name.lastIndexOf('.'));
|
||||
if (HEADER_EXTENSIONS.has(ext)) sink.headers.add(relativePath);
|
||||
if (MODULE_MAP_NAMES.has(name)) sink.moduleMapPaths.push(relativePath);
|
||||
if (ext === '.xcconfig') sink.xcconfigPaths.push(relativePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function shouldSkipDirectory(name: string): boolean {
|
||||
return (
|
||||
name === 'node_modules' ||
|
||||
name === '.git' ||
|
||||
name === 'vendor' ||
|
||||
name === 'dist' ||
|
||||
name === 'build' ||
|
||||
name === 'out' ||
|
||||
name === 'target' ||
|
||||
name === '_build' ||
|
||||
name === '.next' ||
|
||||
name === 'DerivedData' ||
|
||||
name === 'xcuserdata' ||
|
||||
name === '.build' ||
|
||||
name === 'Pods' ||
|
||||
name === 'Carthage' ||
|
||||
name.startsWith('cmake-build')
|
||||
);
|
||||
}
|
||||
|
||||
export function parseXcconfigSearchPaths(source: string): {
|
||||
readonly headerSearchPaths: string[];
|
||||
readonly userHeaderSearchPaths: string[];
|
||||
} {
|
||||
const headerSearchPaths: string[] = [];
|
||||
const userHeaderSearchPaths: string[] = [];
|
||||
const joined = source.replace(/\\\r?\n/g, ' ');
|
||||
for (const rawLine of joined.split(/\r?\n/)) {
|
||||
const line = rawLine.replace(/\/\/.*$/, '').trim();
|
||||
if (line.length === 0) continue;
|
||||
const match = /^(HEADER_SEARCH_PATHS|USER_HEADER_SEARCH_PATHS)\s*=\s*(.*)$/.exec(line);
|
||||
if (match === null) continue;
|
||||
const sink =
|
||||
match[1] === 'USER_HEADER_SEARCH_PATHS' ? userHeaderSearchPaths : headerSearchPaths;
|
||||
for (const token of tokenizeXcconfigValue(match[2] ?? '')) {
|
||||
const resolved = expandXcconfigPath(token);
|
||||
if (resolved !== undefined) sink.push(resolved);
|
||||
}
|
||||
}
|
||||
return { headerSearchPaths, userHeaderSearchPaths };
|
||||
}
|
||||
|
||||
function tokenizeXcconfigValue(value: string): string[] {
|
||||
const tokens: string[] = [];
|
||||
let current = '';
|
||||
let quoted = false;
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
const ch = value[i];
|
||||
if (ch === '"') {
|
||||
quoted = !quoted;
|
||||
continue;
|
||||
}
|
||||
if (!quoted && /\s/.test(ch ?? '')) {
|
||||
if (current.length > 0) {
|
||||
tokens.push(current);
|
||||
current = '';
|
||||
}
|
||||
continue;
|
||||
}
|
||||
current += ch;
|
||||
}
|
||||
if (current.length > 0) tokens.push(current);
|
||||
return tokens;
|
||||
}
|
||||
|
||||
function expandXcconfigPath(token: string): string | undefined {
|
||||
if (token === '$(inherited)' || token === '$inherited') return undefined;
|
||||
if (/\$\((SDKROOT|DEVELOPER_DIR|PLATFORM_DIR|TOOLCHAIN_DIR)\)/.test(token)) return undefined;
|
||||
const expanded = token
|
||||
.replace(/\$\((SRCROOT|PROJECT_DIR)\)\/?/g, '')
|
||||
.replace(/\/\*\*?$/, '')
|
||||
.replace(/\\/g, '/')
|
||||
.replace(/^\.\//, '');
|
||||
if (expanded.startsWith('$') || expanded.startsWith('/')) return undefined;
|
||||
return collapseRepoPath(expanded);
|
||||
}
|
||||
|
||||
export function parseModuleMap(
|
||||
source: string,
|
||||
mapDir: string,
|
||||
): ReadonlyArray<readonly [string, string]> {
|
||||
const modules: Array<readonly [string, string]> = [];
|
||||
const blocks = /(?:framework\s+)?module\s+([A-Za-z_][\w.]*)\s*\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}/g;
|
||||
for (const match of source.matchAll(blocks)) {
|
||||
const name = match[1];
|
||||
const body = match[2] ?? '';
|
||||
if (name === undefined || name === '*') continue;
|
||||
const header =
|
||||
/umbrella\s+header\s+"([^"]+)"/.exec(body)?.[1] ??
|
||||
/umbrella\s+"([^"]+)"/.exec(body)?.[1] ??
|
||||
/header\s+"([^"]+)"/.exec(body)?.[1];
|
||||
if (header === undefined) continue;
|
||||
modules.push([name, collapseRepoPath(resolveModuleMapHeader(mapDir, header))]);
|
||||
}
|
||||
return modules;
|
||||
}
|
||||
|
||||
function readText(filePath: string): string {
|
||||
try {
|
||||
return readFileSync(filePath, 'utf8');
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function dirnamePosix(filePath: string): string {
|
||||
const normalized = normalizeRepoPath(filePath);
|
||||
const slash = normalized.lastIndexOf('/');
|
||||
return slash === -1 ? '' : normalized.slice(0, slash);
|
||||
}
|
||||
|
||||
function uniquePaths(paths: readonly string[]): string[] {
|
||||
const seen = new Set<string>();
|
||||
const out: string[] = [];
|
||||
for (const path of paths) {
|
||||
const normalized = normalizeRepoPath(path);
|
||||
if (normalized.length === 0 || seen.has(normalized)) continue;
|
||||
seen.add(normalized);
|
||||
out.push(normalized);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function normalizeRepoPath(value: string): string {
|
||||
return value.replaceAll('\\', '/').replace(/^\.\//, '');
|
||||
}
|
||||
|
||||
/** Collapse `.` / `..` in scanned config paths. Import targets stay raw. */
|
||||
export function collapseRepoPath(value: string): string {
|
||||
const parts = normalizeRepoPath(value).split('/');
|
||||
const out: string[] = [];
|
||||
for (const part of parts) {
|
||||
if (part === '' || part === '.') continue;
|
||||
if (part === '..') {
|
||||
out.pop();
|
||||
continue;
|
||||
}
|
||||
out.push(part);
|
||||
}
|
||||
return out.join('/');
|
||||
}
|
||||
|
||||
function resolveModuleMapHeader(mapDir: string, header: string): string {
|
||||
const direct = collapseRepoPath(joinPosix(mapDir, header));
|
||||
const frameworkRoot = frameworkRootFromModulesDir(mapDir);
|
||||
if (frameworkRoot === undefined) return direct;
|
||||
const base = header.slice(header.replaceAll('\\', '/').lastIndexOf('/') + 1);
|
||||
return collapseRepoPath(joinPosix(`${frameworkRoot}/Headers`, base));
|
||||
}
|
||||
|
||||
function frameworkRootFromModulesDir(mapDir: string): string | undefined {
|
||||
if (mapDir === 'Modules') return undefined;
|
||||
if (!mapDir.endsWith('/Modules')) return undefined;
|
||||
const parent = mapDir.slice(0, -'/Modules'.length);
|
||||
return parent.endsWith('.framework') ? parent : undefined;
|
||||
}
|
||||
|
||||
function joinPosix(left: string, right: string): string {
|
||||
if (left.length === 0) return right;
|
||||
return `${left}/${right}`;
|
||||
}
|
||||
|
|
@ -0,0 +1,827 @@
|
|||
import {
|
||||
SupportedLanguages,
|
||||
type ParsedFile,
|
||||
type SymbolDefinition,
|
||||
type Callsite,
|
||||
} from 'gitnexus-shared';
|
||||
import type { RelationshipType } from 'gitnexus-shared';
|
||||
import type { KnowledgeGraph } from '../../../graph/types.js';
|
||||
import type { ScopeResolver } from '../../scope-resolution/contract/scope-resolver.js';
|
||||
import { generateId } from '../../../../lib/utils.js';
|
||||
import { populateClassOwnedMembers } from '../../scope-resolution/scope/walkers.js';
|
||||
import { objectiveCProvider } from '../objective-c.js';
|
||||
import { populateObjectiveCCompilationUnitSiblings } from './compilation-unit-siblings.js';
|
||||
import { resolveObjectiveCImportTarget } from './import-target.js';
|
||||
import { loadObjectiveCResolutionConfig } from './resolution-config.js';
|
||||
import {
|
||||
applyObjectiveCCaptureSideChannel,
|
||||
isInternalObjectiveCFunctionDef,
|
||||
objcClassQualifiedName,
|
||||
objcProtocolQualifiedName,
|
||||
objcUnresolvedMessageQualifiedName,
|
||||
objectiveCFactsFromParsedFiles,
|
||||
type ObjCContainerFact,
|
||||
type ObjCFileFacts,
|
||||
type ObjCMemberFact,
|
||||
type ObjCMessageFact,
|
||||
type ObjCMethodFact,
|
||||
type ObjCTypeInfo,
|
||||
parseObjCType,
|
||||
} from './facts.js';
|
||||
|
||||
interface ObjCWorkspaceFacts {
|
||||
readonly containersByQualifiedName: ReadonlyMap<string, ObjCContainerFact>;
|
||||
readonly classByName: ReadonlyMap<string, ObjCContainerFact>;
|
||||
readonly protocolsByName: ReadonlyMap<string, ObjCContainerFact>;
|
||||
readonly methodsByDispatchOwner: ReadonlyMap<string, readonly ObjCMethodFact[]>;
|
||||
readonly methodsByExactOwner: ReadonlyMap<string, readonly ObjCMethodFact[]>;
|
||||
readonly memberTypesByOwner: ReadonlyMap<string, ReadonlyMap<string, ObjCTypeInfo>>;
|
||||
readonly classProtocols: ReadonlyMap<string, ReadonlySet<string>>;
|
||||
readonly protocolParents: ReadonlyMap<string, ReadonlySet<string>>;
|
||||
readonly superclassByClass: ReadonlyMap<string, string>;
|
||||
/** Class names that conform to each protocol, including inherited protocols. */
|
||||
readonly classesByProtocol: ReadonlyMap<string, readonly string[]>;
|
||||
/**
|
||||
* Per-(protocol, selector) implementer methods. Filled on first query so
|
||||
* N messages to the same protocol do not rescan the class set.
|
||||
*/
|
||||
readonly protocolImplementationBySelector: Map<string, readonly ObjCMethodFact[]>;
|
||||
/**
|
||||
* Shared implementer-evidence nodes already written. The implementer
|
||||
* USES fan-out is emitted once; later messages only add the
|
||||
* source→shared hop (C# workspaceTypeBindings analog).
|
||||
*/
|
||||
readonly emittedProtocolCandidateSets: Set<string>;
|
||||
}
|
||||
|
||||
export const objectiveCScopeResolver: ScopeResolver = {
|
||||
language: SupportedLanguages.ObjectiveC,
|
||||
languageProvider: objectiveCProvider,
|
||||
importEdgeReason: 'objective-c-scope: import',
|
||||
|
||||
loadResolutionConfig: (repoPath) => loadObjectiveCResolutionConfig(repoPath),
|
||||
|
||||
resolveImportTarget: (targetRaw, fromFile, allFilePaths, resolutionConfig) =>
|
||||
resolveObjectiveCImportTarget(targetRaw, fromFile, allFilePaths, resolutionConfig),
|
||||
|
||||
mergeBindings: (existing, incoming) => [...existing, ...incoming],
|
||||
|
||||
arityCompatibility: (callsite: Callsite, def: SymbolDefinition) => {
|
||||
if (callsite.arity === undefined || def.parameterCount === undefined) return 'unknown';
|
||||
return callsite.arity === def.parameterCount ? 'compatible' : 'incompatible';
|
||||
},
|
||||
|
||||
buildMro: () => new Map(),
|
||||
|
||||
applyCaptureSideChannel: applyObjectiveCCaptureSideChannel,
|
||||
populateOwners: (parsed: ParsedFile) => populateClassOwnedMembers(parsed),
|
||||
populateNamespaceSiblings: populateObjectiveCCompilationUnitSiblings,
|
||||
isFileLocalDef: (def: SymbolDefinition) => isInternalObjectiveCFunctionDef(def),
|
||||
isSuperReceiver: (receiverText) => receiverText.trim() === 'super',
|
||||
|
||||
fieldFallbackOnMethodLookup: false,
|
||||
propagatesReturnTypesAcrossImports: false,
|
||||
collapseMemberCallsByCallerTarget: true,
|
||||
|
||||
emitPostResolutionEdges(graph, parsedFiles) {
|
||||
const facts = objectiveCFactsFromParsedFiles(parsedFiles);
|
||||
if (facts.length === 0) return;
|
||||
const workspace = buildObjectiveCWorkspaceFacts(facts);
|
||||
|
||||
for (const fact of facts) {
|
||||
emitObjectiveCHeritageEdges(graph, fact, workspace);
|
||||
emitObjectiveCCategoryEdges(graph, fact, workspace);
|
||||
emitObjectiveCImplementationEvidence(graph, fact);
|
||||
emitObjectiveCUnresolvedMessageEvidence(graph, fact);
|
||||
emitObjectiveCMessageEdges(graph, fact, workspace);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
function graphNodeId(label: string, qualifiedName: string): string {
|
||||
return generateId(label, qualifiedName);
|
||||
}
|
||||
|
||||
function relationshipId(
|
||||
type: RelationshipType,
|
||||
sourceId: string,
|
||||
targetId: string,
|
||||
reason: string,
|
||||
): string {
|
||||
return generateId(type, `${sourceId}->${targetId}:${reason}`);
|
||||
}
|
||||
|
||||
function addRelationship(
|
||||
graph: KnowledgeGraph,
|
||||
type: RelationshipType,
|
||||
sourceId: string,
|
||||
targetId: string,
|
||||
reason: string,
|
||||
confidence = 0.9,
|
||||
): void {
|
||||
graph.addRelationship({
|
||||
id: relationshipId(type, sourceId, targetId, reason),
|
||||
sourceId,
|
||||
targetId,
|
||||
type,
|
||||
confidence,
|
||||
reason,
|
||||
});
|
||||
}
|
||||
|
||||
function labelForContainer(container: ObjCContainerFact): 'Class' | 'Protocol' | 'Category' {
|
||||
return container.label;
|
||||
}
|
||||
|
||||
function buildObjectiveCWorkspaceFacts(facts: readonly ObjCFileFacts[]): ObjCWorkspaceFacts {
|
||||
const containersByQualifiedName = new Map<string, ObjCContainerFact>();
|
||||
const classByName = new Map<string, ObjCContainerFact>();
|
||||
const protocolsByName = new Map<string, ObjCContainerFact>();
|
||||
const methodsByExactOwner = new Map<string, ObjCMethodFact[]>();
|
||||
const methodsByDispatchOwner = new Map<string, ObjCMethodFact[]>();
|
||||
const memberTypesByOwner = new Map<string, Map<string, ObjCTypeInfo>>();
|
||||
const classProtocols = new Map<string, Set<string>>();
|
||||
const protocolParents = new Map<string, Set<string>>();
|
||||
const superclassByClass = new Map<string, string>();
|
||||
|
||||
for (const fileFact of facts) {
|
||||
for (const container of fileFact.containers) {
|
||||
const existing = containersByQualifiedName.get(container.qualifiedName);
|
||||
containersByQualifiedName.set(
|
||||
container.qualifiedName,
|
||||
mergeContainerFacts(existing, container),
|
||||
);
|
||||
if (container.kind === 'class') {
|
||||
classByName.set(container.name, container);
|
||||
if (container.superclass !== undefined)
|
||||
superclassByClass.set(container.name, container.superclass);
|
||||
if (container.protocols.length > 0) {
|
||||
let protocols = classProtocols.get(container.name);
|
||||
if (protocols === undefined) {
|
||||
protocols = new Set();
|
||||
classProtocols.set(container.name, protocols);
|
||||
}
|
||||
for (const protocol of container.protocols) protocols.add(protocol);
|
||||
}
|
||||
} else if (container.kind === 'protocol') {
|
||||
protocolsByName.set(container.name, container);
|
||||
if (container.protocols.length > 0) {
|
||||
let parents = protocolParents.get(container.name);
|
||||
if (parents === undefined) {
|
||||
parents = new Set();
|
||||
protocolParents.set(container.name, parents);
|
||||
}
|
||||
for (const protocol of container.protocols) parents.add(protocol);
|
||||
}
|
||||
} else if (container.hostClass !== undefined) {
|
||||
if (container.protocols.length > 0) {
|
||||
let protocols = classProtocols.get(container.hostClass);
|
||||
if (protocols === undefined) {
|
||||
protocols = new Set();
|
||||
classProtocols.set(container.hostClass, protocols);
|
||||
}
|
||||
for (const protocol of container.protocols) protocols.add(protocol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const method of fileFact.methods) {
|
||||
appendMap(methodsByExactOwner, method.ownerQualifiedName, method);
|
||||
appendMap(methodsByDispatchOwner, method.ownerQualifiedName, method);
|
||||
if (method.hostClass !== undefined) {
|
||||
appendMap(methodsByDispatchOwner, objcClassQualifiedName(method.hostClass), method);
|
||||
}
|
||||
}
|
||||
|
||||
for (const member of fileFact.members) {
|
||||
addMemberType(memberTypesByOwner, member.ownerQualifiedName, member);
|
||||
if (member.hostClass !== undefined) {
|
||||
addMemberType(memberTypesByOwner, objcClassQualifiedName(member.hostClass), member);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const hierarchyWorkspace = { protocolParents };
|
||||
const classNames = new Set([...classByName.keys(), ...classProtocols.keys()]);
|
||||
const classesByProtocol = new Map<string, string[]>();
|
||||
for (const className of classNames) {
|
||||
const seenClasses = new Set<string>();
|
||||
let currentClass: string | undefined = className;
|
||||
while (currentClass !== undefined && !seenClasses.has(currentClass)) {
|
||||
seenClasses.add(currentClass);
|
||||
const directProtocols = classProtocols.get(currentClass);
|
||||
if (directProtocols !== undefined) {
|
||||
for (const directProtocol of directProtocols) {
|
||||
for (const protocolName of protocolHierarchy(hierarchyWorkspace, directProtocol)) {
|
||||
let implementers = classesByProtocol.get(protocolName);
|
||||
if (implementers === undefined) {
|
||||
implementers = [];
|
||||
classesByProtocol.set(protocolName, implementers);
|
||||
}
|
||||
implementers.push(className);
|
||||
}
|
||||
}
|
||||
}
|
||||
currentClass = superclassByClass.get(currentClass);
|
||||
}
|
||||
}
|
||||
const classesByProtocolFrozen = new Map<string, readonly string[]>();
|
||||
for (const [protocolName, implementers] of classesByProtocol) {
|
||||
classesByProtocolFrozen.set(
|
||||
protocolName,
|
||||
Object.freeze([...new Set(implementers)].sort((left, right) => left.localeCompare(right))),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
containersByQualifiedName,
|
||||
classByName,
|
||||
protocolsByName,
|
||||
methodsByDispatchOwner,
|
||||
methodsByExactOwner,
|
||||
memberTypesByOwner,
|
||||
classProtocols,
|
||||
protocolParents,
|
||||
superclassByClass,
|
||||
classesByProtocol: classesByProtocolFrozen,
|
||||
protocolImplementationBySelector: new Map(),
|
||||
emittedProtocolCandidateSets: new Set(),
|
||||
};
|
||||
}
|
||||
|
||||
function addMemberType(
|
||||
memberTypesByOwner: Map<string, Map<string, ObjCTypeInfo>>,
|
||||
ownerQualifiedName: string,
|
||||
member: ObjCMemberFact,
|
||||
): void {
|
||||
const type = parseObjCType(member.declaredType);
|
||||
if (type === undefined) return;
|
||||
let types = memberTypesByOwner.get(ownerQualifiedName);
|
||||
if (types === undefined) {
|
||||
types = new Map();
|
||||
memberTypesByOwner.set(ownerQualifiedName, types);
|
||||
}
|
||||
types.set(member.name, type);
|
||||
}
|
||||
|
||||
function mergeContainerFacts(
|
||||
existing: ObjCContainerFact | undefined,
|
||||
incoming: ObjCContainerFact,
|
||||
): ObjCContainerFact {
|
||||
if (existing === undefined) return incoming;
|
||||
const protocols = Array.from(new Set([...existing.protocols, ...incoming.protocols])).sort();
|
||||
return {
|
||||
...existing,
|
||||
declarationRole:
|
||||
existing.declarationRole === 'implementation' || incoming.declarationRole === 'implementation'
|
||||
? 'implementation'
|
||||
: 'interface',
|
||||
startLine: Math.min(existing.startLine, incoming.startLine),
|
||||
endLine: Math.max(existing.endLine, incoming.endLine),
|
||||
...(existing.superclass !== undefined || incoming.superclass !== undefined
|
||||
? { superclass: existing.superclass ?? incoming.superclass }
|
||||
: {}),
|
||||
protocols,
|
||||
};
|
||||
}
|
||||
|
||||
function appendMap<K, V>(map: Map<K, V[]>, key: K, value: V): void {
|
||||
const existing = map.get(key);
|
||||
if (existing === undefined) map.set(key, [value]);
|
||||
else existing.push(value);
|
||||
}
|
||||
|
||||
function emitObjectiveCHeritageEdges(
|
||||
graph: KnowledgeGraph,
|
||||
facts: ObjCFileFacts,
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
): void {
|
||||
for (const container of facts.containers) {
|
||||
const sourceId = graphNodeId(labelForContainer(container), container.qualifiedName);
|
||||
if (container.kind === 'class' && container.superclass !== undefined) {
|
||||
const superclass = workspace.classByName.get(container.superclass);
|
||||
if (superclass !== undefined) {
|
||||
addRelationship(
|
||||
graph,
|
||||
'EXTENDS',
|
||||
sourceId,
|
||||
graphNodeId('Class', superclass.qualifiedName),
|
||||
'objc: superclass',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const protocolSourceId =
|
||||
container.hostClass !== undefined
|
||||
? graphNodeId('Class', objcClassQualifiedName(container.hostClass))
|
||||
: sourceId;
|
||||
for (const protocolName of container.protocols) {
|
||||
const protocol = workspace.protocolsByName.get(protocolName);
|
||||
if (protocol === undefined) continue;
|
||||
addRelationship(
|
||||
graph,
|
||||
'IMPLEMENTS',
|
||||
protocolSourceId,
|
||||
graphNodeId('Protocol', protocol.qualifiedName),
|
||||
'objc: protocol conformance',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function emitObjectiveCCategoryEdges(
|
||||
graph: KnowledgeGraph,
|
||||
facts: ObjCFileFacts,
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
): void {
|
||||
for (const container of facts.containers) {
|
||||
if (container.hostClass === undefined) continue;
|
||||
if (!workspace.classByName.has(container.hostClass)) continue;
|
||||
addRelationship(
|
||||
graph,
|
||||
'MEMBER_OF',
|
||||
graphNodeId('Category', container.qualifiedName),
|
||||
graphNodeId('Class', objcClassQualifiedName(container.hostClass)),
|
||||
'objc: category host class',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function emitObjectiveCUnresolvedMessageEvidence(
|
||||
graph: KnowledgeGraph,
|
||||
facts: ObjCFileFacts,
|
||||
): void {
|
||||
for (const unresolved of facts.unresolvedMessages) {
|
||||
const evidenceId = graphNodeId(
|
||||
'CodeElement',
|
||||
objcUnresolvedMessageQualifiedName(
|
||||
facts.filePath,
|
||||
unresolved.startLine,
|
||||
unresolved.startCol,
|
||||
unresolved.selector,
|
||||
),
|
||||
);
|
||||
addRelationship(
|
||||
graph,
|
||||
'USES',
|
||||
unresolved.sourceMethodId,
|
||||
evidenceId,
|
||||
`objc-message: unresolved: ${unresolved.reason}`,
|
||||
0.5,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function emitObjectiveCImplementationEvidence(graph: KnowledgeGraph, facts: ObjCFileFacts): void {
|
||||
for (const container of facts.containers) {
|
||||
if (container.declarationRole !== 'implementation') continue;
|
||||
const targetId = graphNodeId(labelForContainer(container), container.qualifiedName);
|
||||
emitImplementationEvidence(
|
||||
graph,
|
||||
facts.filePath,
|
||||
targetId,
|
||||
`@implementation ${container.name}`,
|
||||
`objc:implementation:${container.qualifiedName}:${facts.filePath}:${container.startLine}`,
|
||||
container.startLine,
|
||||
container.endLine,
|
||||
{
|
||||
objectiveCKind: 'implementation-evidence',
|
||||
implementationKind: container.kind,
|
||||
targetQualifiedName: container.qualifiedName,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
for (const method of facts.methods) {
|
||||
if (method.declarationRole !== 'implementation') continue;
|
||||
emitImplementationEvidence(
|
||||
graph,
|
||||
facts.filePath,
|
||||
method.nodeId,
|
||||
`${method.methodKind}[${method.ownerName} ${method.selector}] implementation`,
|
||||
`objc:method-implementation:${method.qualifiedName}:${facts.filePath}:${method.startLine}`,
|
||||
method.startLine,
|
||||
method.endLine,
|
||||
{
|
||||
objectiveCKind: 'implementation-evidence',
|
||||
implementationKind: 'method',
|
||||
targetQualifiedName: method.qualifiedName,
|
||||
selector: method.selector,
|
||||
methodKind: method.methodKind,
|
||||
objectiveCOwner: method.ownerQualifiedName,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function emitImplementationEvidence(
|
||||
graph: KnowledgeGraph,
|
||||
filePath: string,
|
||||
targetId: string,
|
||||
name: string,
|
||||
qualifiedName: string,
|
||||
startLine: number,
|
||||
endLine: number,
|
||||
extras: Record<string, unknown>,
|
||||
): void {
|
||||
const nodeId = graphNodeId('CodeElement', qualifiedName);
|
||||
graph.addNode({
|
||||
id: nodeId,
|
||||
label: 'CodeElement',
|
||||
properties: {
|
||||
name,
|
||||
qualifiedName,
|
||||
filePath,
|
||||
startLine,
|
||||
endLine,
|
||||
language: SupportedLanguages.ObjectiveC,
|
||||
isExported: false,
|
||||
...extras,
|
||||
},
|
||||
});
|
||||
addRelationship(
|
||||
graph,
|
||||
'DEFINES',
|
||||
graphNodeId('File', filePath),
|
||||
nodeId,
|
||||
'objc: implementation evidence',
|
||||
1,
|
||||
);
|
||||
addRelationship(graph, 'DECLARES', nodeId, targetId, 'objc: implementation of merged symbol', 1);
|
||||
}
|
||||
|
||||
function emitObjectiveCMessageEdges(
|
||||
graph: KnowledgeGraph,
|
||||
facts: ObjCFileFacts,
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
): void {
|
||||
for (const message of facts.messages) {
|
||||
const targets = resolveMessageTargets(message, workspace);
|
||||
if (targets.kind === 'none') continue;
|
||||
if (targets.kind === 'category-collision') {
|
||||
emitCategoryDispatchEvidence(graph, facts, message, targets.candidates);
|
||||
continue;
|
||||
}
|
||||
if (targets.kind === 'protocol') {
|
||||
emitProtocolMessageEvidence(
|
||||
graph,
|
||||
facts,
|
||||
message,
|
||||
targets.protocolName,
|
||||
targets.candidates,
|
||||
workspace,
|
||||
);
|
||||
}
|
||||
for (const target of targets.methods) {
|
||||
if (graph.getNode(target.nodeId) === undefined) continue;
|
||||
addRelationship(
|
||||
graph,
|
||||
'CALLS',
|
||||
message.sourceMethodId,
|
||||
target.nodeId,
|
||||
targets.kind === 'protocol'
|
||||
? 'objc-message: protocol receiver'
|
||||
: `objc-message: ${message.receiverKind} receiver`,
|
||||
targets.kind === 'protocol' ? 0.8 : 0.9,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type MessageTargets =
|
||||
| { readonly kind: 'none'; readonly methods: readonly ObjCMethodFact[] }
|
||||
| { readonly kind: 'direct'; readonly methods: readonly ObjCMethodFact[] }
|
||||
| { readonly kind: 'category-collision'; readonly candidates: readonly ObjCMethodFact[] }
|
||||
| {
|
||||
readonly kind: 'protocol';
|
||||
readonly protocolName: string;
|
||||
readonly methods: readonly ObjCMethodFact[];
|
||||
readonly candidates: readonly ObjCMethodFact[];
|
||||
};
|
||||
|
||||
function messageTargetsFromDispatchLookup(lookup: DispatchMethodLookup): MessageTargets {
|
||||
return lookup.categoryCollision
|
||||
? { kind: 'category-collision', candidates: lookup.methods }
|
||||
: { kind: 'direct', methods: lookup.methods };
|
||||
}
|
||||
|
||||
function resolveMessageTargets(
|
||||
message: ObjCMessageFact,
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
): MessageTargets {
|
||||
if (message.receiverKind === 'dynamic') {
|
||||
return { kind: 'none', methods: [] };
|
||||
}
|
||||
|
||||
if (message.receiverKind === 'class') {
|
||||
const className = message.receiverType?.name ?? message.receiverText;
|
||||
return messageTargetsFromDispatchLookup(
|
||||
findDispatchMethods(workspace, className, '+', message.selector),
|
||||
);
|
||||
}
|
||||
|
||||
if (message.receiverKind === 'self') {
|
||||
const owner = workspace.containersByQualifiedName.get(message.sourceOwnerQualifiedName);
|
||||
const className = owner?.hostClass ?? owner?.name ?? message.sourceOwnerName;
|
||||
if (owner?.kind === 'protocol') {
|
||||
return {
|
||||
kind: 'direct',
|
||||
methods: findProtocolMethods(
|
||||
workspace,
|
||||
owner.name,
|
||||
message.sourceMethodKind,
|
||||
message.selector,
|
||||
),
|
||||
};
|
||||
}
|
||||
return messageTargetsFromDispatchLookup(
|
||||
findDispatchMethods(workspace, className, message.sourceMethodKind, message.selector),
|
||||
);
|
||||
}
|
||||
|
||||
if (message.receiverKind === 'super') {
|
||||
const owner = workspace.containersByQualifiedName.get(message.sourceOwnerQualifiedName);
|
||||
const className = owner?.hostClass ?? owner?.name ?? message.sourceOwnerName;
|
||||
const superclass = workspace.superclassByClass.get(className);
|
||||
return superclass === undefined
|
||||
? { kind: 'none', methods: [] }
|
||||
: messageTargetsFromDispatchLookup(
|
||||
findDispatchMethods(workspace, superclass, message.sourceMethodKind, message.selector),
|
||||
);
|
||||
}
|
||||
|
||||
const receiverType = message.receiverType ?? resolveMemberReceiverType(message, workspace);
|
||||
if (receiverType?.kind === 'dynamic' || receiverType?.kind === 'class-object') {
|
||||
return { kind: 'none', methods: [] };
|
||||
}
|
||||
if (message.receiverKind === 'unknown' && receiverType === undefined) {
|
||||
return { kind: 'none', methods: [] };
|
||||
}
|
||||
if (receiverType?.kind === 'class' && receiverType.name !== undefined) {
|
||||
return messageTargetsFromDispatchLookup(
|
||||
findDispatchMethods(workspace, receiverType.name, '-', message.selector),
|
||||
);
|
||||
}
|
||||
|
||||
if (receiverType?.kind === 'protocol' && receiverType.name !== undefined) {
|
||||
const methods = findProtocolMethods(workspace, receiverType.name, '-', message.selector);
|
||||
const candidates = findProtocolImplementationCandidates(
|
||||
workspace,
|
||||
receiverType.name,
|
||||
message.selector,
|
||||
);
|
||||
return {
|
||||
kind: 'protocol',
|
||||
protocolName: receiverType.name,
|
||||
methods,
|
||||
candidates,
|
||||
};
|
||||
}
|
||||
|
||||
return { kind: 'none', methods: [] };
|
||||
}
|
||||
|
||||
function resolveMemberReceiverType(
|
||||
message: ObjCMessageFact,
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
): ObjCTypeInfo | undefined {
|
||||
if (message.receiverMemberName === undefined) return undefined;
|
||||
const owner = workspace.containersByQualifiedName.get(message.sourceOwnerQualifiedName);
|
||||
let className = owner?.hostClass ?? owner?.name ?? message.sourceOwnerName;
|
||||
const seen = new Set<string>();
|
||||
while (className !== undefined && !seen.has(className)) {
|
||||
seen.add(className);
|
||||
const type = workspace.memberTypesByOwner
|
||||
.get(objcClassQualifiedName(className))
|
||||
?.get(message.receiverMemberName);
|
||||
if (type !== undefined) return type;
|
||||
className = workspace.superclassByClass.get(className);
|
||||
}
|
||||
return workspace.memberTypesByOwner
|
||||
.get(message.sourceOwnerQualifiedName)
|
||||
?.get(message.receiverMemberName);
|
||||
}
|
||||
|
||||
function findDispatchMethods(
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
className: string,
|
||||
methodKind: '-' | '+',
|
||||
selector: string,
|
||||
): DispatchMethodLookup {
|
||||
const seen = new Set<string>();
|
||||
let currentClass: string | undefined = className;
|
||||
while (currentClass !== undefined && !seen.has(currentClass)) {
|
||||
seen.add(currentClass);
|
||||
const ownerQn = objcClassQualifiedName(currentClass);
|
||||
const methods = uniqueMethods(
|
||||
(workspace.methodsByDispatchOwner.get(ownerQn) ?? []).filter(
|
||||
(method) => method.methodKind === methodKind && method.selector === selector,
|
||||
),
|
||||
);
|
||||
if (methods.length > 0) {
|
||||
return {
|
||||
methods,
|
||||
// Named categories can replace a host-class implementation at runtime,
|
||||
// but image-load order is not statically knowable. Preserve all targets
|
||||
// as evidence instead of claiming that each is a certain CALLS edge.
|
||||
categoryCollision:
|
||||
methods.length > 1 && methods.some((method) => method.ownerKind === 'category'),
|
||||
};
|
||||
}
|
||||
currentClass = workspace.superclassByClass.get(currentClass);
|
||||
}
|
||||
return { methods: [], categoryCollision: false };
|
||||
}
|
||||
|
||||
interface DispatchMethodLookup {
|
||||
readonly methods: readonly ObjCMethodFact[];
|
||||
readonly categoryCollision: boolean;
|
||||
}
|
||||
|
||||
function findExactOwnerMethods(
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
ownerQualifiedName: string,
|
||||
methodKind: '-' | '+',
|
||||
selector: string,
|
||||
): readonly ObjCMethodFact[] {
|
||||
return (workspace.methodsByExactOwner.get(ownerQualifiedName) ?? []).filter(
|
||||
(method) => method.methodKind === methodKind && method.selector === selector,
|
||||
);
|
||||
}
|
||||
|
||||
function findProtocolMethods(
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
protocolName: string,
|
||||
methodKind: '-' | '+',
|
||||
selector: string,
|
||||
): readonly ObjCMethodFact[] {
|
||||
for (const name of protocolHierarchy(workspace, protocolName)) {
|
||||
const methods = findExactOwnerMethods(
|
||||
workspace,
|
||||
objcProtocolQualifiedName(name),
|
||||
methodKind,
|
||||
selector,
|
||||
);
|
||||
if (methods.length > 0) return uniqueMethods(methods);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function protocolHierarchy(
|
||||
workspace: Pick<ObjCWorkspaceFacts, 'protocolParents'>,
|
||||
protocolName: string,
|
||||
): readonly string[] {
|
||||
const seen = new Set<string>();
|
||||
const pending = [protocolName];
|
||||
const hierarchy: string[] = [];
|
||||
while (pending.length > 0) {
|
||||
const current = pending.shift();
|
||||
if (current === undefined || seen.has(current)) continue;
|
||||
seen.add(current);
|
||||
hierarchy.push(current);
|
||||
const parents = workspace.protocolParents.get(current);
|
||||
if (parents === undefined) continue;
|
||||
pending.push(...[...parents].sort());
|
||||
}
|
||||
return hierarchy;
|
||||
}
|
||||
|
||||
function uniqueMethods(methods: readonly ObjCMethodFact[]): readonly ObjCMethodFact[] {
|
||||
return [...new Map(methods.map((method) => [method.nodeId, method])).values()].sort(
|
||||
(left, right) => left.qualifiedName.localeCompare(right.qualifiedName),
|
||||
);
|
||||
}
|
||||
|
||||
function findProtocolImplementationCandidates(
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
protocolName: string,
|
||||
selector: string,
|
||||
): readonly ObjCMethodFact[] {
|
||||
const cacheKey = `${protocolName}\0${selector}`;
|
||||
const cached = workspace.protocolImplementationBySelector.get(cacheKey);
|
||||
if (cached !== undefined) return cached;
|
||||
|
||||
const out: ObjCMethodFact[] = [];
|
||||
for (const className of workspace.classesByProtocol.get(protocolName) ?? []) {
|
||||
out.push(...findDispatchMethods(workspace, className, '-', selector).methods);
|
||||
}
|
||||
const result = uniqueMethods(out);
|
||||
workspace.protocolImplementationBySelector.set(cacheKey, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
function emitCategoryDispatchEvidence(
|
||||
graph: KnowledgeGraph,
|
||||
facts: ObjCFileFacts,
|
||||
message: ObjCMessageFact,
|
||||
candidates: readonly ObjCMethodFact[],
|
||||
): void {
|
||||
if (candidates.length === 0) return;
|
||||
const qualifiedName =
|
||||
`objc:category-dispatch-candidates:${facts.filePath}:` +
|
||||
`${message.startLine}:${message.startCol}:${message.selector}`;
|
||||
const nodeId = graphNodeId('CodeElement', qualifiedName);
|
||||
graph.addNode({
|
||||
id: nodeId,
|
||||
label: 'CodeElement',
|
||||
properties: {
|
||||
name: `[${message.receiverText} ${message.selector}] category dispatch candidates`,
|
||||
qualifiedName,
|
||||
filePath: facts.filePath,
|
||||
startLine: message.startLine,
|
||||
endLine: message.startLine,
|
||||
language: SupportedLanguages.ObjectiveC,
|
||||
isExported: false,
|
||||
objectiveCKind: 'category-dispatch-candidates',
|
||||
},
|
||||
});
|
||||
addRelationship(
|
||||
graph,
|
||||
'DEFINES',
|
||||
graphNodeId('File', facts.filePath),
|
||||
nodeId,
|
||||
`objc: category dispatch candidate evidence: ${message.selector}`,
|
||||
1,
|
||||
);
|
||||
addRelationship(
|
||||
graph,
|
||||
'USES',
|
||||
message.sourceMethodId,
|
||||
nodeId,
|
||||
`objc-message: category dispatch candidates: ${message.selector}`,
|
||||
0.7,
|
||||
);
|
||||
for (const candidate of candidates) {
|
||||
if (graph.getNode(candidate.nodeId) === undefined) continue;
|
||||
addRelationship(
|
||||
graph,
|
||||
'USES',
|
||||
nodeId,
|
||||
candidate.nodeId,
|
||||
`objc-category-dispatch-candidate: ${message.selector}`,
|
||||
0.5,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function emitProtocolMessageEvidence(
|
||||
graph: KnowledgeGraph,
|
||||
facts: ObjCFileFacts,
|
||||
message: ObjCMessageFact,
|
||||
protocolName: string,
|
||||
candidates: readonly ObjCMethodFact[],
|
||||
workspace: ObjCWorkspaceFacts,
|
||||
): void {
|
||||
if (candidates.length === 0) return;
|
||||
const setKey = `${protocolName}\0${message.selector}`;
|
||||
const qualifiedName = `objc:protocol-candidates:${protocolName}:${message.selector}`;
|
||||
const nodeId = graphNodeId('CodeElement', qualifiedName);
|
||||
const protocol = workspace.protocolsByName.get(protocolName);
|
||||
const evidenceFilePath = protocol?.filePath ?? facts.filePath;
|
||||
|
||||
if (!workspace.emittedProtocolCandidateSets.has(setKey)) {
|
||||
workspace.emittedProtocolCandidateSets.add(setKey);
|
||||
graph.addNode({
|
||||
id: nodeId,
|
||||
label: 'CodeElement',
|
||||
properties: {
|
||||
name: `[id<${protocolName}> ${message.selector}] protocol implementers`,
|
||||
qualifiedName,
|
||||
filePath: evidenceFilePath,
|
||||
startLine: protocol?.startLine ?? message.startLine,
|
||||
endLine: protocol?.endLine ?? message.startLine,
|
||||
language: SupportedLanguages.ObjectiveC,
|
||||
isExported: false,
|
||||
objectiveCKind: 'protocol-implementers',
|
||||
},
|
||||
});
|
||||
addRelationship(
|
||||
graph,
|
||||
'DEFINES',
|
||||
graphNodeId('File', evidenceFilePath),
|
||||
nodeId,
|
||||
`objc: protocol receiver candidate evidence: ${protocolName} ${message.selector}`,
|
||||
1,
|
||||
);
|
||||
for (const candidate of candidates) {
|
||||
if (graph.getNode(candidate.nodeId) === undefined) continue;
|
||||
addRelationship(
|
||||
graph,
|
||||
'USES',
|
||||
nodeId,
|
||||
candidate.nodeId,
|
||||
`objc-protocol-candidate: ${protocolName} ${message.selector}`,
|
||||
0.5,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
addRelationship(
|
||||
graph,
|
||||
'USES',
|
||||
message.sourceMethodId,
|
||||
nodeId,
|
||||
`objc-message: protocol receiver candidates: ${protocolName} ${message.selector}`,
|
||||
0.7,
|
||||
);
|
||||
}
|
||||
|
|
@ -385,7 +385,7 @@ const tsScopeOwnsReceivers = (match: CaptureMatch): ReadonlySet<string> | undefi
|
|||
|
||||
export const typescriptProvider = defineLanguage({
|
||||
id: SupportedLanguages.TypeScript,
|
||||
extensions: ['.ts', '.tsx'],
|
||||
extensions: ['.ts', '.tsx', '.mts', '.cts'],
|
||||
entryPointPatterns: [/^use[A-Z]/],
|
||||
astFrameworkPatterns: [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -15,11 +15,11 @@
|
|||
* └── symbols (SymbolTable) ← owns fileIndex + callableByName,
|
||||
* calls dispatch() in add()
|
||||
*
|
||||
* ## Behavior groups (5 hooks, 13 table entries)
|
||||
* ## Behavior groups (4 hooks, 12 table entries)
|
||||
*
|
||||
* | Group | NodeLabel values | Hook | Skip callable? |
|
||||
* |---------------|---------------------------------------------------|--------------|----------------|
|
||||
* | class-like | Class, Struct, Interface, Enum, Record, Trait | classLikeHook | no |
|
||||
* | class-like | Class, Protocol, Category, Struct, Interface, Enum, Record, Trait | classLikeHook | no |
|
||||
* | method-like | Method, Constructor | methodHook | no |
|
||||
* | property | Property | propertyHook | YES |
|
||||
* | impl-block | Impl | implHook | no |
|
||||
|
|
@ -95,7 +95,7 @@ export interface RegistrationTableDeps {
|
|||
* registry (if any) receives the symbol write during `SymbolTable.add()`:
|
||||
*
|
||||
* - `dispatch` — owner-scoped registry write via the dispatch table
|
||||
* (Class/Struct/Interface/Enum/Record/Trait → types.registerClass,
|
||||
* (Class/Protocol/Category/Struct/Interface/Enum/Record/Trait → types.registerClass,
|
||||
* Method/Constructor → methods.register,
|
||||
* Property → fields.register,
|
||||
* Impl → types.registerImpl)
|
||||
|
|
@ -143,6 +143,8 @@ export type LabelBehavior = 'dispatch' | 'callable-only' | 'inert';
|
|||
const LABEL_BEHAVIOR = {
|
||||
// dispatch — owner-scoped registry writes
|
||||
Class: 'dispatch',
|
||||
Protocol: 'dispatch',
|
||||
Category: 'dispatch',
|
||||
Struct: 'dispatch',
|
||||
Interface: 'dispatch',
|
||||
Enum: 'dispatch',
|
||||
|
|
@ -271,8 +273,8 @@ export const createRegistrationTable = (
|
|||
): Map<NodeLabel, RegistrationHook> => {
|
||||
const { types, methods, fields } = deps;
|
||||
|
||||
// Hook 1: class-like — Class, Struct, Interface, Enum, Record, Trait.
|
||||
// Shared reference — six table entries point at this one closure.
|
||||
// Hook 1: class-like — Class, Protocol, Category, Struct, Interface, Enum, Record, Trait.
|
||||
// Shared reference — eight table entries point at this one closure.
|
||||
const classLikeHook: RegistrationHook = (name, def) => {
|
||||
const qualifiedKey = def.qualifiedName ?? name;
|
||||
types.registerClass(name, qualifiedKey, def);
|
||||
|
|
@ -311,11 +313,13 @@ export const createRegistrationTable = (
|
|||
// classified as 'dispatch'. This is the compile-time twin of the
|
||||
// runtime taxonomy — no drift possible.
|
||||
const dispatchByLabel = {
|
||||
// class-like — six labels share the single `classLikeHook` closure,
|
||||
// class-like — eight labels share the single `classLikeHook` closure,
|
||||
// kept in lockstep with `CLASS_TYPES_TUPLE` via the
|
||||
// `Record<ClassLikeLabel, 'dispatch'>` cross-invariant on
|
||||
// `LABEL_BEHAVIOR`.
|
||||
Class: classLikeHook,
|
||||
Protocol: classLikeHook,
|
||||
Category: classLikeHook,
|
||||
Struct: classLikeHook,
|
||||
Interface: classLikeHook,
|
||||
Enum: classLikeHook,
|
||||
|
|
|
|||
|
|
@ -56,6 +56,8 @@ import type { NodeLabel, ParameterTypeClass, SymbolDefinition } from 'gitnexus-s
|
|||
*/
|
||||
export const CLASS_TYPES_TUPLE = [
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Struct',
|
||||
'Interface',
|
||||
'Enum',
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ const EMPTY: readonly SymbolDefinition[] = Object.freeze([]);
|
|||
|
||||
export interface TypeRegistry {
|
||||
/**
|
||||
* Look up class-like definitions (Class, Struct, Interface, Enum, Record, Trait)
|
||||
* Look up class-like definitions (Class, Protocol, Category, Struct, Interface, Enum, Record, Trait)
|
||||
* by simple name. Returns all matching definitions across files
|
||||
* (e.g. partial classes). Returned array is a view into the live
|
||||
* internal index — do not mutate.
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import type { NodeLabel } from 'gitnexus-shared';
|
||||
import { KnowledgeGraph } from '../graph/types.js';
|
||||
import type { SymbolTableWriter } from './model/index.js';
|
||||
import { getLanguageFromFilename } from 'gitnexus-shared';
|
||||
import { getLanguageForFileContent } from './languages/index.js';
|
||||
|
||||
import { accumulateExportedTypesFromParsedNode, type ExportedTypeMap } from './call-processor.js';
|
||||
|
||||
|
|
@ -260,7 +260,7 @@ export const dispatchChunkParseRound = async (
|
|||
const dispatchGroups: DispatchGroup<ParseWorkerInput>[] = groups.map((group) => {
|
||||
const items: ParseWorkerInput[] = [];
|
||||
for (const file of group.items) {
|
||||
const lang = getLanguageFromFilename(file.path);
|
||||
const lang = getLanguageForFileContent(file.path, file.content);
|
||||
if (lang) items.push({ path: file.path, content: file.content });
|
||||
}
|
||||
return { items, chunkHash: group.chunkHash };
|
||||
|
|
|
|||
|
|
@ -61,7 +61,13 @@ import {
|
|||
createParserForLanguage,
|
||||
} from '../../tree-sitter/parser-loader.js';
|
||||
import { parseSourceSafe } from '../../tree-sitter/safe-parse.js';
|
||||
import { getProvider, getProviderForFile, providers } from '../languages/index.js';
|
||||
import {
|
||||
getProvider,
|
||||
getProviderForFile,
|
||||
needsContentLanguageClassification,
|
||||
providers,
|
||||
} from '../languages/index.js';
|
||||
import { classifyContentLanguages } from '../content-language-classification.js';
|
||||
import { SCOPE_RESOLVERS } from '../scope-resolution/pipeline/registry.js';
|
||||
import { DATA_ROUTE_TABLE_SOURCE } from '../route-extractors/data-route-table.js';
|
||||
import type Parser from 'tree-sitter';
|
||||
|
|
@ -497,6 +503,14 @@ export async function runChunkedParseAndResolve(
|
|||
* cache analyze run can skip the dominant `extractParsedFile` cost
|
||||
* (otherwise ~58s on a 1000-file repo). */
|
||||
parsedFiles: import('gitnexus-shared').ParsedFile[];
|
||||
/**
|
||||
* Content-derived language decisions for extension-ambiguous files.
|
||||
*
|
||||
* The source text used to classify these paths is released before parsing
|
||||
* begins. Scope resolution consumes this snapshot instead of reading the
|
||||
* same files again just to repeat classification.
|
||||
*/
|
||||
contentLanguageByPath: ReadonlyMap<string, SupportedLanguages | null>;
|
||||
/** Repo-wide harvested constants, already prepared per provider. See
|
||||
* `ParseOutput.moduleConstants` for why this leaves the parse phase. */
|
||||
moduleConstants: ReadonlyMap<string, ModuleConstants>;
|
||||
|
|
@ -507,15 +521,27 @@ export async function runChunkedParseAndResolve(
|
|||
const model = createSemanticModel();
|
||||
const symbolTable = model.symbols;
|
||||
|
||||
const contentClassifiedPaths = scannedFiles
|
||||
.map((file) => file.path)
|
||||
.filter(needsContentLanguageClassification);
|
||||
const contentLanguageByPath =
|
||||
contentClassifiedPaths.length > 0
|
||||
? await classifyContentLanguages(repoPath, contentClassifiedPaths)
|
||||
: new Map<string, SupportedLanguages | null>();
|
||||
const languageForScannedFile = (file: (typeof scannedFiles)[number]) => {
|
||||
return contentLanguageByPath.has(file.path)
|
||||
? (contentLanguageByPath.get(file.path) ?? null)
|
||||
: getLanguageFromFilename(file.path);
|
||||
};
|
||||
const parseableScanned = scannedFiles.filter((f) => {
|
||||
const lang = getLanguageFromFilename(f.path);
|
||||
const lang = languageForScannedFile(f);
|
||||
return lang && isLanguageAvailable(lang);
|
||||
});
|
||||
|
||||
// Warn about files skipped due to unavailable parsers
|
||||
const skippedByLang = new Map<string, number>();
|
||||
for (const f of scannedFiles) {
|
||||
const lang = getLanguageFromFilename(f.path);
|
||||
const lang = languageForScannedFile(f);
|
||||
const provider = lang === null ? undefined : getProvider(lang);
|
||||
if (lang && provider?.parseStrategy !== 'standalone' && !isLanguageAvailable(lang)) {
|
||||
skippedByLang.set(lang, (skippedByLang.get(lang) || 0) + 1);
|
||||
|
|
@ -1879,6 +1905,7 @@ export async function runChunkedParseAndResolve(
|
|||
// cache: when the file's ParsedFile is here, scope-resolution skips its own
|
||||
// `extractParsedFile` call.
|
||||
parsedFiles: allParsedFiles,
|
||||
contentLanguageByPath,
|
||||
// Repo-wide, file-path-keyed constants, already through each provider's
|
||||
// `prepareRouteConstants` hook. Empty when no provider harvests constants
|
||||
// for the languages in this repo.
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ import type { PipelinePhase, PipelineContext, PhaseResult } from './types.js';
|
|||
import { getPhaseOutput } from './types.js';
|
||||
import type { StructureOutput } from './structure.js';
|
||||
import type { BindingAccumulator } from '../binding-accumulator.js';
|
||||
import type { ParsedFile } from 'gitnexus-shared';
|
||||
import type { ParsedFile, SupportedLanguages } from 'gitnexus-shared';
|
||||
import type {
|
||||
ExtractedFetchCall,
|
||||
ExtractedRoute,
|
||||
|
|
@ -83,6 +83,12 @@ export interface ParseOutput {
|
|||
* costing ~58s on a 1000-file repo).
|
||||
*/
|
||||
readonly parsedFiles: readonly ParsedFile[];
|
||||
/**
|
||||
* Content-derived language decisions for files such as `.h` whose extension
|
||||
* is ambiguous. Scope resolution reuses this result rather than rereading
|
||||
* source solely to classify it.
|
||||
*/
|
||||
readonly contentLanguageByPath: ReadonlyMap<string, SupportedLanguages | null>;
|
||||
/**
|
||||
* Repo-wide string constants harvested by the providers that declare
|
||||
* `extractModuleConstants`, keyed by file path and already through each
|
||||
|
|
|
|||
|
|
@ -847,6 +847,10 @@ function normalizeNodeLabel(kindStr: string): SymbolDefinition['type'] | undefin
|
|||
switch (kindStr.toLowerCase()) {
|
||||
case 'class':
|
||||
return 'Class';
|
||||
case 'protocol':
|
||||
return 'Protocol';
|
||||
case 'category':
|
||||
return 'Category';
|
||||
case 'interface':
|
||||
return 'Interface';
|
||||
case 'enum':
|
||||
|
|
|
|||
|
|
@ -87,6 +87,8 @@ export const CALLER_ANCHOR_LABELS: ReadonlySet<NodeLabel> = new Set<NodeLabel>([
|
|||
'Constructor',
|
||||
'Module',
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'Struct',
|
||||
'Enum',
|
||||
|
|
|
|||
|
|
@ -280,6 +280,8 @@ export const LINKABLE_LABELS: ReadonlySet<NodeLabel> = new Set<NodeLabel>([
|
|||
// targets and need the same def→graph bridge.
|
||||
'Module',
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'Struct',
|
||||
'Enum',
|
||||
|
|
|
|||
|
|
@ -178,7 +178,7 @@ export const scopeResolutionPhase: PipelinePhase<ScopeResolutionOutput> = {
|
|||
logHeapProbe('scopeResolution-enter');
|
||||
const { scannedFiles } = getPhaseOutput<StructureOutput>(deps, 'structure');
|
||||
const parseOutput = getPhaseOutput<ParseOutput>(deps, 'parse');
|
||||
const { model, parsedFiles: workerParsedFiles } = parseOutput;
|
||||
const { model, parsedFiles: workerParsedFiles, contentLanguageByPath } = parseOutput;
|
||||
const scopeExtractionFailures = new Set(parseOutput.scopeExtractionFailures);
|
||||
// SemanticModel populated during `parse`: scope-resolution consumes
|
||||
// TypeRegistry / MethodRegistry / SymbolTable lookups instead of
|
||||
|
|
@ -258,7 +258,9 @@ export const scopeResolutionPhase: PipelinePhase<ScopeResolutionOutput> = {
|
|||
(typeof scannedFiles)[number][]
|
||||
>();
|
||||
for (const f of scannedFiles) {
|
||||
const fileLang = getLanguageFromFilename(f.path);
|
||||
const fileLang = contentLanguageByPath.has(f.path)
|
||||
? (contentLanguageByPath.get(f.path) ?? null)
|
||||
: getLanguageFromFilename(f.path);
|
||||
if (fileLang === null) continue;
|
||||
// Tree-sitter providers require an available grammar. Standalone regex
|
||||
// providers deliberately have none and re-extract on the main thread.
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import { swiftScopeResolver } from '../../languages/swift/scope-resolver.js';
|
|||
import { dartScopeResolver } from '../../languages/dart/scope-resolver.js';
|
||||
import { vueScopeResolver } from '../../languages/vue/scope-resolver.js';
|
||||
import { zigScopeResolver } from '../../languages/zig/scope-resolver.js';
|
||||
import { objectiveCScopeResolver } from '../../languages/objective-c/scope-resolver.js';
|
||||
|
||||
/** Map of `SupportedLanguages` → `ScopeResolver`. The scope-resolution phase
|
||||
* iterates this map directly — every registered resolver runs. This is the
|
||||
|
|
@ -53,4 +54,5 @@ export const SCOPE_RESOLVERS: ReadonlyMap<SupportedLanguages, ScopeResolver> = n
|
|||
[SupportedLanguages.Dart, dartScopeResolver],
|
||||
[SupportedLanguages.Vue, vueScopeResolver],
|
||||
[SupportedLanguages.Zig, zigScopeResolver],
|
||||
[SupportedLanguages.ObjectiveC, objectiveCScopeResolver],
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -181,6 +181,8 @@ export function namesAtScope(scopeId: ScopeId, scopes: ScopeResolutionIndexes):
|
|||
export function isClassLike(t: string): boolean {
|
||||
return (
|
||||
t === 'Class' ||
|
||||
t === 'Protocol' ||
|
||||
t === 'Category' ||
|
||||
t === 'Interface' ||
|
||||
t === 'Struct' ||
|
||||
t === 'Record' ||
|
||||
|
|
@ -431,9 +433,10 @@ export function moduleScopeIdOf(
|
|||
/**
|
||||
* Look up a class-like binding by name in the given scope's chain.
|
||||
*
|
||||
* "Class-like" covers `Class | Interface | Struct | Record | Enum |
|
||||
* Trait` via the shared `isClassLike` predicate — every kind that
|
||||
* collapses to `@scope.class` in the scope-extractor query contract.
|
||||
* "Class-like" covers `Class | Interface | Struct | Record | Enum | Trait |
|
||||
* Protocol | Category` via the shared `isClassLike` predicate. Objective-C
|
||||
* protocol and category definitions are graph-side containers rather than
|
||||
* `@scope.class` captures.
|
||||
*
|
||||
* Walks the scope chain upward and consults TWO sources at each step:
|
||||
* 1. `scope.bindings` — populated during scope-extraction Pass 2 with
|
||||
|
|
|
|||
|
|
@ -2610,12 +2610,15 @@ export const ZIG_QUERIES = `
|
|||
|
||||
import { SupportedLanguages } from 'gitnexus-shared';
|
||||
|
||||
const OBJECTIVE_C_QUERIES = `((translation_unit) @objc.root)`;
|
||||
|
||||
export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {
|
||||
[SupportedLanguages.TypeScript]: TYPESCRIPT_QUERIES,
|
||||
[SupportedLanguages.JavaScript]: JAVASCRIPT_QUERIES,
|
||||
[SupportedLanguages.Python]: PYTHON_QUERIES,
|
||||
[SupportedLanguages.Java]: JAVA_QUERIES,
|
||||
[SupportedLanguages.C]: C_QUERIES,
|
||||
[SupportedLanguages.ObjectiveC]: OBJECTIVE_C_QUERIES,
|
||||
[SupportedLanguages.Go]: GO_QUERIES,
|
||||
[SupportedLanguages.CPlusPlus]: CPP_QUERIES,
|
||||
[SupportedLanguages.CSharp]: CSHARP_QUERIES,
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@ export const SYMBOL_NODE_LABELS: ReadonlySet<NodeLabel> = new Set<NodeLabel>([
|
|||
'Function',
|
||||
'Method',
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'CodeElement',
|
||||
'Struct',
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import PHP from 'tree-sitter-php';
|
|||
import Ruby from 'tree-sitter-ruby';
|
||||
import { requireVendoredGrammar } from '../../tree-sitter/vendored-grammars.js';
|
||||
import { SupportedLanguages } from 'gitnexus-shared';
|
||||
import { getProvider } from '../languages/index.js';
|
||||
import { getLanguageForFileContent, getProvider } from '../languages/index.js';
|
||||
import {
|
||||
getTreeSitterBufferSize,
|
||||
getTreeSitterContentByteLength,
|
||||
|
|
@ -87,6 +87,11 @@ let Zig: TreeSitterLanguage | null = null;
|
|||
try {
|
||||
Zig = requireVendoredGrammar('tree-sitter-zig') as TreeSitterLanguage;
|
||||
} catch {}
|
||||
|
||||
let ObjectiveC: TreeSitterLanguage | null = null;
|
||||
try {
|
||||
ObjectiveC = requireVendoredGrammar('tree-sitter-objc') as TreeSitterLanguage;
|
||||
} catch {}
|
||||
import { getLanguageFromFilename } from 'gitnexus-shared';
|
||||
import {
|
||||
buildDefinitionPreScan,
|
||||
|
|
@ -251,7 +256,7 @@ interface ParsedRelationship {
|
|||
id: string;
|
||||
sourceId: string;
|
||||
targetId: string;
|
||||
type: 'DEFINES' | 'HAS_METHOD' | 'HAS_PROPERTY';
|
||||
type: 'DEFINES' | 'DECLARES' | 'HAS_METHOD' | 'HAS_PROPERTY';
|
||||
confidence: number;
|
||||
reason: string;
|
||||
}
|
||||
|
|
@ -557,6 +562,7 @@ const languageMap: Record<string, TreeSitterLanguage> = {
|
|||
[SupportedLanguages.Java]: Java,
|
||||
...(C ? { [SupportedLanguages.C]: C } : {}),
|
||||
[SupportedLanguages.CPlusPlus]: CPP,
|
||||
...(ObjectiveC ? { [SupportedLanguages.ObjectiveC]: ObjectiveC } : {}),
|
||||
[SupportedLanguages.CSharp]: CSharp,
|
||||
[SupportedLanguages.Go]: Go,
|
||||
[SupportedLanguages.Rust]: Rust,
|
||||
|
|
@ -1269,7 +1275,7 @@ const processBatch = (
|
|||
// Group by language to minimize setLanguage calls
|
||||
const byLanguage = new Map<SupportedLanguages, ParseWorkerInput[]>();
|
||||
for (const file of files) {
|
||||
const lang = getLanguageFromFilename(file.path);
|
||||
const lang = getLanguageForFileContent(file.path, file.content);
|
||||
if (!lang) continue;
|
||||
let list = byLanguage.get(lang);
|
||||
if (!list) {
|
||||
|
|
@ -1739,6 +1745,52 @@ const processFileGroup = (
|
|||
result.parsedFiles.push(withChannels);
|
||||
}
|
||||
|
||||
const semanticGraph = provider.extractSemanticGraph?.(tree, file.path, parseContent);
|
||||
if (semanticGraph !== undefined) {
|
||||
for (const node of semanticGraph.nodes) {
|
||||
result.nodes.push({
|
||||
id: node.id,
|
||||
label: node.label,
|
||||
properties: { ...node.properties },
|
||||
});
|
||||
}
|
||||
for (const relationship of semanticGraph.relationships) {
|
||||
if (
|
||||
relationship.type === 'DECLARES' ||
|
||||
relationship.type === 'DEFINES' ||
|
||||
relationship.type === 'HAS_METHOD' ||
|
||||
relationship.type === 'HAS_PROPERTY'
|
||||
) {
|
||||
result.relationships.push({ ...relationship, type: relationship.type });
|
||||
}
|
||||
}
|
||||
for (const symbol of semanticGraph.symbols) {
|
||||
result.symbols.push({
|
||||
filePath: symbol.filePath,
|
||||
name: symbol.name,
|
||||
nodeId: symbol.nodeId,
|
||||
type: symbol.type,
|
||||
...(symbol.qualifiedName !== undefined ? { qualifiedName: symbol.qualifiedName } : {}),
|
||||
...(symbol.parameterCount !== undefined ? { parameterCount: symbol.parameterCount } : {}),
|
||||
...(symbol.requiredParameterCount !== undefined
|
||||
? { requiredParameterCount: symbol.requiredParameterCount }
|
||||
: {}),
|
||||
...(symbol.parameterTypes !== undefined
|
||||
? { parameterTypes: [...symbol.parameterTypes] }
|
||||
: {}),
|
||||
...(symbol.parameterTypeClasses !== undefined
|
||||
? { parameterTypeClasses: [...symbol.parameterTypeClasses] }
|
||||
: {}),
|
||||
...(symbol.returnType !== undefined ? { returnType: symbol.returnType } : {}),
|
||||
...(symbol.declaredType !== undefined ? { declaredType: symbol.declaredType } : {}),
|
||||
...(symbol.ownerId !== undefined ? { ownerId: symbol.ownerId } : {}),
|
||||
...(symbol.visibility !== undefined ? { visibility: symbol.visibility } : {}),
|
||||
...(symbol.isStatic !== undefined ? { isStatic: symbol.isStatic } : {}),
|
||||
...(symbol.isReadonly !== undefined ? { isReadonly: symbol.isReadonly } : {}),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Build per-file type environment + constructor bindings in a single AST walk.
|
||||
// The legacy heritage pre-pass that seeded a file-local parentMap for
|
||||
// buildTypeEnv was removed in RING4-1 (#942) along with the rest of the
|
||||
|
|
|
|||
|
|
@ -568,6 +568,8 @@ export const streamAllCSVsToDisk = async (
|
|||
const multiLangHeader = 'id,name,filePath,startLine,endLine,content,description';
|
||||
const constHeader = `${multiLangHeader},convexEndpointFactory`;
|
||||
const MULTI_LANG_TYPES = [
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Struct',
|
||||
'Enum',
|
||||
'Macro',
|
||||
|
|
|
|||
|
|
@ -69,6 +69,30 @@ CREATE NODE TABLE Class (
|
|||
PRIMARY KEY (id)
|
||||
)`;
|
||||
|
||||
export const PROTOCOL_SCHEMA = `
|
||||
CREATE NODE TABLE Protocol (
|
||||
id STRING,
|
||||
name STRING,
|
||||
filePath STRING,
|
||||
startLine INT64,
|
||||
endLine INT64,
|
||||
content STRING,
|
||||
description STRING,
|
||||
PRIMARY KEY (id)
|
||||
)`;
|
||||
|
||||
export const CATEGORY_SCHEMA = `
|
||||
CREATE NODE TABLE Category (
|
||||
id STRING,
|
||||
name STRING,
|
||||
filePath STRING,
|
||||
startLine INT64,
|
||||
endLine INT64,
|
||||
content STRING,
|
||||
description STRING,
|
||||
PRIMARY KEY (id)
|
||||
)`;
|
||||
|
||||
export const INTERFACE_SCHEMA = `
|
||||
CREATE NODE TABLE Interface (
|
||||
id STRING,
|
||||
|
|
@ -581,6 +605,9 @@ export const STRUCTURAL_PAIR_DDL = ` FROM File TO Folder,
|
|||
FROM \`Module\` TO \`Namespace\`,
|
||||
FROM \`Namespace\` TO Function,
|
||||
FROM CodeElement TO CodeElement,
|
||||
FROM CodeElement TO Class,
|
||||
FROM CodeElement TO Category,
|
||||
FROM CodeElement TO Method,
|
||||
FROM CodeElement TO \`Module\`,
|
||||
FROM CodeElement TO \`Property\`,
|
||||
FROM Section TO Section,
|
||||
|
|
@ -714,6 +741,8 @@ export const NODE_SCHEMA_QUERIES = [
|
|||
FOLDER_SCHEMA,
|
||||
FUNCTION_SCHEMA,
|
||||
CLASS_SCHEMA,
|
||||
PROTOCOL_SCHEMA,
|
||||
CATEGORY_SCHEMA,
|
||||
INTERFACE_SCHEMA,
|
||||
METHOD_SCHEMA,
|
||||
CODE_ELEMENT_SCHEMA,
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ export const FTS_INDEXES: readonly FTSIndexDefinition[] = [
|
|||
// Original 5 (minus File) gain `description`.
|
||||
{ table: 'Function', indexName: 'function_fts', properties: FTS_PROPERTIES },
|
||||
{ table: 'Class', indexName: 'class_fts', properties: FTS_PROPERTIES },
|
||||
{ table: 'Protocol', indexName: 'protocol_fts', properties: FTS_PROPERTIES },
|
||||
{ table: 'Category', indexName: 'category_fts', properties: FTS_PROPERTIES },
|
||||
{ table: 'Method', indexName: 'method_fts', properties: FTS_PROPERTIES },
|
||||
{ table: 'Interface', indexName: 'interface_fts', properties: FTS_PROPERTIES },
|
||||
// Remaining EMBEDDABLE_LABELS symbol tables — all CODE_ELEMENT_BASE-shaped
|
||||
|
|
|
|||
|
|
@ -96,6 +96,17 @@ const SOURCES: Record<string, GrammarSource> = {
|
|||
unavailableNote:
|
||||
'C++ parsing requires `tree-sitter-cpp`. Check the install and native binding.',
|
||||
},
|
||||
[SupportedLanguages.ObjectiveC]: {
|
||||
load: () => requireVendoredGrammar('tree-sitter-objc'),
|
||||
optional: true,
|
||||
severity: 'error',
|
||||
unavailableNote:
|
||||
'Objective-C parsing disabled: vendored `tree-sitter-objc` (under ' +
|
||||
'`gitnexus/vendor/tree-sitter-objc`) could not be loaded. GitNexus ships ' +
|
||||
'prebuilt binaries for supported macOS/Linux runner architectures; this usually ' +
|
||||
'indicates a corrupted install or native ABI mismatch with the bundled ' +
|
||||
'tree-sitter@0.21.1 runtime.',
|
||||
},
|
||||
[SupportedLanguages.Go]: {
|
||||
load: () => _require('tree-sitter-go'),
|
||||
unavailableNote: 'Go parsing requires `tree-sitter-go`. Check the install and native binding.',
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ export const VENDORED_GRAMMAR_PACKAGES: ReadonlySet<string> = new Set([
|
|||
'tree-sitter-proto',
|
||||
'tree-sitter-swift',
|
||||
'tree-sitter-kotlin',
|
||||
'tree-sitter-objc',
|
||||
'tree-sitter-zig',
|
||||
]);
|
||||
|
||||
|
|
@ -43,7 +44,7 @@ export const vendoredGrammarDir = (packageName: string): string =>
|
|||
/**
|
||||
* Load a vendored tree-sitter grammar by its absolute path under `vendor/`.
|
||||
*
|
||||
* GitNexus vendors six grammars (c/dart/proto/swift/kotlin/zig) inside its own
|
||||
* GitNexus vendors seven grammars (c/dart/proto/swift/kotlin/objc/zig) inside its own
|
||||
* package under `vendor/`, each shipping committed per-platform prebuilds. They
|
||||
* are deliberately NOT npm dependencies and must NEVER be copied into
|
||||
* `node_modules`: an undeclared package under `node_modules` is "extraneous" to
|
||||
|
|
|
|||
|
|
@ -328,6 +328,8 @@ export const VALID_NODE_LABELS = new Set([
|
|||
'Folder',
|
||||
'Function',
|
||||
'Class',
|
||||
'Protocol',
|
||||
'Category',
|
||||
'Interface',
|
||||
'Method',
|
||||
'CodeElement',
|
||||
|
|
@ -4249,7 +4251,8 @@ export class LocalBackend {
|
|||
repo.lbugPath,
|
||||
`
|
||||
MATCH (caller)-[r:CodeRelation]->(n {id: $symId})
|
||||
WHERE r.type IN ['CALLS', 'IMPORTS', 'EXTENDS', 'IMPLEMENTS', 'USES', 'HAS_METHOD', 'HAS_PROPERTY', 'METHOD_OVERRIDES', 'OVERRIDES', 'METHOD_IMPLEMENTS', 'ACCESSES']
|
||||
WHERE r.type IN ['CALLS', 'IMPORTS', 'EXTENDS', 'IMPLEMENTS', 'MEMBER_OF', 'USES', 'DECLARES', 'HAS_METHOD', 'HAS_PROPERTY', 'METHOD_OVERRIDES', 'OVERRIDES', 'METHOD_IMPLEMENTS', 'ACCESSES']
|
||||
AND (r.type <> 'MEMBER_OF' OR labels(n) <> 'Community')
|
||||
RETURN r.type AS relType, caller.id AS uid, caller.name AS name, caller.filePath AS filePath, labels(caller)[0] AS kind
|
||||
ORDER BY uid, relType
|
||||
LIMIT 30
|
||||
|
|
@ -4404,7 +4407,8 @@ export class LocalBackend {
|
|||
repo.lbugPath,
|
||||
`
|
||||
MATCH (n {id: $symId})-[r:CodeRelation]->(target)
|
||||
WHERE r.type IN ['CALLS', 'IMPORTS', 'EXTENDS', 'IMPLEMENTS', 'USES', 'HAS_METHOD', 'HAS_PROPERTY', 'METHOD_OVERRIDES', 'OVERRIDES', 'METHOD_IMPLEMENTS', 'ACCESSES']
|
||||
WHERE r.type IN ['CALLS', 'IMPORTS', 'EXTENDS', 'IMPLEMENTS', 'MEMBER_OF', 'USES', 'DECLARES', 'HAS_METHOD', 'HAS_PROPERTY', 'METHOD_OVERRIDES', 'OVERRIDES', 'METHOD_IMPLEMENTS', 'ACCESSES']
|
||||
AND (r.type <> 'MEMBER_OF' OR labels(target) <> 'Community')
|
||||
RETURN r.type AS relType, target.id AS uid, target.name AS name, target.filePath AS filePath, labels(target)[0] AS kind
|
||||
ORDER BY uid, relType
|
||||
LIMIT 30
|
||||
|
|
|
|||
|
|
@ -735,7 +735,21 @@ import { copyV8CacheIfPresent, tryLoadV8Cache, writeV8CacheFile } from './v8-sid
|
|||
// v93: Zig call captures inside a comptime-false branch carry
|
||||
// `@reference.static-gated` (feat/zig-static-gated-edges); the site gains
|
||||
// `staticGated` and the CALLS edge a BOOLEAN column.
|
||||
const SCHEMA_BUMP = 93;
|
||||
// v94: Objective-C now elides bare, file-scope macro markers before parsing.
|
||||
// A warm v93 cache can retain error-recovered trees and provider facts that
|
||||
// omit Objective-C declarations following markers such as RCT_EXTERN_C_END.
|
||||
// v95: Objective-C header classification no longer treats framework `#import`
|
||||
// alone as Objective-C syntax. A warm v94 cache can replay Objective-C worker
|
||||
// output for a C++ header during `--force`, even though the current classifier
|
||||
// routes that same header through the C++ provider.
|
||||
// v96: Objective-C macro-marker preprocessing recognizes all C preprocessing
|
||||
// whitespace before a directive or bare marker. A warm v95 cache can retain
|
||||
// error-recovered facts for sources that begin those lines with form feed or
|
||||
// vertical tab.
|
||||
// v97: Objective-C macro-marker preprocessing recognizes comment-prefixed
|
||||
// directives and rejects invalid numeric marker prefixes. A warm v96 cache can
|
||||
// replay error-recovered facts from the previous normalization behavior.
|
||||
const SCHEMA_BUMP = 97;
|
||||
const GITNEXUS_PKG_VERSION = (() => {
|
||||
try {
|
||||
// package.json sits at gitnexus/package.json — two levels up from
|
||||
|
|
|
|||
9
gitnexus/test/fixtures/objective-c/SYModuleBridge.mm
vendored
Normal file
9
gitnexus/test/fixtures/objective-c/SYModuleBridge.mm
vendored
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#import "SYModuleCaller.h"
|
||||
|
||||
@interface SYModuleBridge : NSObject
|
||||
- (void)bridgeValue:(NSInteger)value;
|
||||
@end
|
||||
|
||||
@implementation SYModuleBridge
|
||||
- (void)bridgeValue:(NSInteger)value {}
|
||||
@end
|
||||
32
gitnexus/test/fixtures/objective-c/SYModuleCaller.h
vendored
Normal file
32
gitnexus/test/fixtures/objective-c/SYModuleCaller.h
vendored
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
#define RCT_EXTERN_C_BEGIN
|
||||
#define RCT_EXTERN_C_END
|
||||
|
||||
RCT_EXTERN_C_BEGIN
|
||||
typedef struct SYModuleMethodInfo {
|
||||
const char *const name;
|
||||
} SYModuleMethodInfo;
|
||||
RCT_EXTERN_C_END
|
||||
|
||||
int SYModuleSupportAdd(int a, int b);
|
||||
|
||||
@protocol SYModuleRunnable <NSObject>
|
||||
- (void)runTask:(NSString *)name completion:(void (^)(BOOL ok))completion;
|
||||
@end
|
||||
|
||||
@interface SYBaseCaller : NSObject
|
||||
- (void)loadData:(NSString *)name completion:(void (^)(BOOL ok))completion;
|
||||
@end
|
||||
|
||||
@interface SYModuleCaller : SYBaseCaller <SYModuleRunnable> {
|
||||
SYBaseCaller *_base;
|
||||
}
|
||||
@property (nonatomic, strong) SYBaseCaller *helper;
|
||||
+ (instancetype)sharedCaller;
|
||||
- (void)runTask:(NSString *)name completion:(void (^)(BOOL ok))completion;
|
||||
@end
|
||||
|
||||
@interface SYModuleCaller (Tracing)
|
||||
- (void)traceEvent:(NSString *)name;
|
||||
@end
|
||||
32
gitnexus/test/fixtures/objective-c/SYModuleCaller.m
vendored
Normal file
32
gitnexus/test/fixtures/objective-c/SYModuleCaller.m
vendored
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#import "SYModuleCaller.h"
|
||||
#include "SYModuleSupport.h"
|
||||
@import Foundation;
|
||||
|
||||
#define SY_OBJC_RECEIVER(x) x
|
||||
|
||||
@interface SYModuleCaller ()
|
||||
@property (nonatomic, strong) SYBaseCaller *privateHelper;
|
||||
@end
|
||||
|
||||
@implementation SYModuleCaller
|
||||
+ (instancetype)sharedCaller { return [SYModuleCaller new]; }
|
||||
- (void)runTask:(NSString *)name completion:(void (^)(BOOL ok))completion {
|
||||
SYBaseCaller *typed = self.helper;
|
||||
id dynamic = typed;
|
||||
[self traceEvent:name];
|
||||
[SY_OBJC_RECEIVER(self) traceEvent:name];
|
||||
[super loadData:name completion:completion];
|
||||
[self loadData:name completion:completion];
|
||||
[typed loadData:name completion:completion];
|
||||
[dynamic loadData:name completion:completion];
|
||||
}
|
||||
- (void)runProtocol:(id<SYModuleRunnable>)runner {
|
||||
[runner runTask:@"x" completion:^(BOOL ok) {}];
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation SYModuleCaller (Tracing)
|
||||
- (void)traceEvent:(NSString *)name {}
|
||||
@end
|
||||
|
||||
static int SYModuleCompute(int value) { return value + 1; }
|
||||
4
gitnexus/test/fixtures/objective-c/SYModuleSupport.h
vendored
Normal file
4
gitnexus/test/fixtures/objective-c/SYModuleSupport.h
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#ifndef SY_MODULE_SUPPORT_H
|
||||
#define SY_MODULE_SUPPORT_H
|
||||
int SYModuleSupportAdd(int a, int b);
|
||||
#endif
|
||||
|
|
@ -109,7 +109,7 @@ interface NodeTypeEntry {
|
|||
|
||||
/** Resolve the on-disk directory of an installed package, or null if absent. */
|
||||
function resolvePackageDir(pkg: string): string | null {
|
||||
// Vendored grammars (c/dart/proto/swift/kotlin/zig) are NOT in node_modules — they
|
||||
// Vendored grammars (c/dart/proto/swift/kotlin/objc/zig) are NOT in node_modules — they
|
||||
// load from vendor/ by absolute path (vendored-grammars.ts / #2111), so resolve
|
||||
// their node-types.json from there rather than via _require.resolve.
|
||||
if (VENDORED_GRAMMAR_PACKAGES.has(pkg)) {
|
||||
|
|
|
|||
|
|
@ -151,6 +151,50 @@ describe('streamAllCSVsToDisk', () => {
|
|||
expect(await readAllRelRows(result.relsByPair)).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('persists Protocol and Category nodes and their structural relationships', async () => {
|
||||
const graph = buildTestGraph(
|
||||
[
|
||||
{
|
||||
id: 'Protocol:objc:protocol:Runnable',
|
||||
label: 'Protocol',
|
||||
name: 'Runnable',
|
||||
filePath: 'src/Runnable.h',
|
||||
},
|
||||
{
|
||||
id: 'Class:objc:class:Worker',
|
||||
label: 'Class',
|
||||
name: 'Worker',
|
||||
filePath: 'src/Worker.h',
|
||||
},
|
||||
{
|
||||
id: 'Category:objc:category:Worker:Tracing',
|
||||
label: 'Category',
|
||||
name: 'Tracing',
|
||||
filePath: 'src/Worker+Tracing.m',
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
sourceId: 'Class:objc:class:Worker',
|
||||
targetId: 'Protocol:objc:protocol:Runnable',
|
||||
type: 'IMPLEMENTS',
|
||||
},
|
||||
{
|
||||
sourceId: 'Category:objc:category:Worker:Tracing',
|
||||
targetId: 'Class:objc:class:Worker',
|
||||
type: 'MEMBER_OF',
|
||||
},
|
||||
],
|
||||
);
|
||||
|
||||
const result = await streamAllCSVsToDisk(graph, repoDir, csvDir);
|
||||
|
||||
expect(result.nodeFiles.get('Protocol')?.rows).toBe(1);
|
||||
expect(result.nodeFiles.get('Category')?.rows).toBe(1);
|
||||
expect(result.relsByPair.get('Class|Protocol')?.rows).toBe(1);
|
||||
expect(result.relsByPair.get('Category|Class')?.rows).toBe(1);
|
||||
});
|
||||
|
||||
it('CSV content is properly escaped', async () => {
|
||||
const graph = buildTestGraph([
|
||||
{
|
||||
|
|
|
|||
247
gitnexus/test/integration/objective-c-pipeline-benchmark.test.ts
Normal file
247
gitnexus/test/integration/objective-c-pipeline-benchmark.test.ts
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
/**
|
||||
* Objective-C ingestion pipeline benchmark.
|
||||
*
|
||||
* Generates synthetic .h/.m pairs at increasing scales and measures
|
||||
* wall-clock time and peak heap through the full pipeline — header
|
||||
* classification, parsing, provider-owned semantic facts, import
|
||||
* suffix resolution, and static message-send edges.
|
||||
*
|
||||
* Mirrors test/integration/csharp-pipeline-benchmark.test.ts and
|
||||
* test/integration/php-pipeline-benchmark.test.ts. Two shapes:
|
||||
* 1. "spread" — each class imports a fixed sibling (constant fan-out;
|
||||
* see cpp-pipeline-benchmark.test.ts). This is the linear file-count
|
||||
* gate.
|
||||
* 2. "protocol" — every class conforms to one shared protocol and
|
||||
* sends one protocol-typed message. Implementer USES are shared
|
||||
* per (protocol, selector); the dedicated
|
||||
* bench/objective-c-resolution protocol arm pins that evidence
|
||||
* shape at O(implementers).
|
||||
*
|
||||
* Run: GITNEXUS_BENCH=1 npx vitest run test/integration/objective-c-pipeline-benchmark.test.ts
|
||||
*/
|
||||
import { beforeAll, describe, it, expect, vi } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { runPipelineFromRepo } from '../../src/core/ingestion/pipeline.js';
|
||||
|
||||
const BENCH_ENABLED = process.env.GITNEXUS_BENCH === '1';
|
||||
|
||||
beforeAll(() => vi.stubEnv('GITNEXUS_WORKER_READY_TIMEOUT_MS', '60000'));
|
||||
|
||||
interface BenchResult {
|
||||
classCount: number;
|
||||
fileCount: number;
|
||||
elapsedMs: number;
|
||||
peakHeapMB: number;
|
||||
nodeCount: number;
|
||||
edgeCount: number;
|
||||
}
|
||||
|
||||
type FixtureShape = 'spread' | 'protocol';
|
||||
|
||||
function generateObjectiveCFixture(
|
||||
classCount: number,
|
||||
shape: FixtureShape,
|
||||
): { dir: string; fileCount: number } {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), `objc-bench-${shape}-${classCount}-`));
|
||||
|
||||
if (shape === 'protocol') {
|
||||
fs.writeFileSync(
|
||||
path.join(dir, 'Runnable.h'),
|
||||
[
|
||||
'#import <Foundation/Foundation.h>',
|
||||
'',
|
||||
'@protocol Runnable',
|
||||
'- (void)run;',
|
||||
'@end',
|
||||
'',
|
||||
].join('\n'),
|
||||
);
|
||||
}
|
||||
|
||||
for (let f = 0; f < classCount; f++) {
|
||||
const className = `Class${f}`;
|
||||
const sibling = `Class${(f + 1) % classCount}`;
|
||||
const header =
|
||||
shape === 'protocol'
|
||||
? [
|
||||
'#import <Foundation/Foundation.h>',
|
||||
'#import "Runnable.h"',
|
||||
'',
|
||||
`@interface ${className} : NSObject <Runnable>`,
|
||||
'- (void)run;',
|
||||
'- (void)tick:(id<Runnable>)runner;',
|
||||
'@end',
|
||||
'',
|
||||
].join('\n')
|
||||
: [
|
||||
'#import <Foundation/Foundation.h>',
|
||||
'',
|
||||
`@interface ${className} : NSObject`,
|
||||
'- (void)process;',
|
||||
'@end',
|
||||
'',
|
||||
].join('\n');
|
||||
|
||||
const impl =
|
||||
shape === 'protocol'
|
||||
? [
|
||||
`#import "${className}.h"`,
|
||||
'',
|
||||
`@implementation ${className}`,
|
||||
'- (void)run {}',
|
||||
'- (void)tick:(id<Runnable>)runner',
|
||||
'{',
|
||||
' [self run];',
|
||||
' [runner run];',
|
||||
'}',
|
||||
'@end',
|
||||
'',
|
||||
].join('\n')
|
||||
: [
|
||||
`#import "${className}.h"`,
|
||||
`#import "${sibling}.h"`,
|
||||
'',
|
||||
`@implementation ${className}`,
|
||||
'- (void)process',
|
||||
'{',
|
||||
' [self process];',
|
||||
` ${sibling} *sib = [${sibling} new];`,
|
||||
' [sib process];',
|
||||
'}',
|
||||
'@end',
|
||||
'',
|
||||
].join('\n');
|
||||
|
||||
fs.writeFileSync(path.join(dir, `${className}.h`), header);
|
||||
fs.writeFileSync(path.join(dir, `${className}.m`), impl);
|
||||
}
|
||||
|
||||
const fileCount = classCount * 2 + (shape === 'protocol' ? 1 : 0);
|
||||
return { dir, fileCount };
|
||||
}
|
||||
|
||||
async function runBenchmark(
|
||||
classCount: number,
|
||||
shape: FixtureShape,
|
||||
budgetMs: number,
|
||||
): Promise<BenchResult> {
|
||||
const { dir, fileCount } = generateObjectiveCFixture(classCount, shape);
|
||||
|
||||
let peakHeapMB = 0;
|
||||
const heapSampler = setInterval(() => {
|
||||
const heap = process.memoryUsage().heapUsed / 1024 / 1024;
|
||||
if (heap > peakHeapMB) peakHeapMB = heap;
|
||||
}, 50);
|
||||
|
||||
let budgetTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
try {
|
||||
const start = Date.now();
|
||||
const result = await Promise.race([
|
||||
runPipelineFromRepo(dir, () => {}, { skipGraphPhases: true }),
|
||||
new Promise<never>((_, reject) => {
|
||||
budgetTimer = setTimeout(
|
||||
() =>
|
||||
reject(
|
||||
new Error(`Pipeline exceeded ${budgetMs}ms at ${classCount} classes (${shape})`),
|
||||
),
|
||||
budgetMs,
|
||||
);
|
||||
}),
|
||||
]);
|
||||
const elapsedMs = Date.now() - start;
|
||||
|
||||
return {
|
||||
classCount,
|
||||
fileCount,
|
||||
elapsedMs,
|
||||
peakHeapMB: Math.round(peakHeapMB),
|
||||
nodeCount: result.graph.nodeCount,
|
||||
edgeCount: result.graph.relationshipCount,
|
||||
};
|
||||
} finally {
|
||||
clearInterval(heapSampler);
|
||||
clearTimeout(budgetTimer);
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function printResults(label: string, results: BenchResult[]) {
|
||||
console.log(`\n${label}`);
|
||||
console.log('┌──────────┬──────────┬───────────┬──────────┬───────┬───────┐');
|
||||
console.log('│ Classes │ Files │ Time (ms) │ Heap MB │ Nodes │ Edges │');
|
||||
console.log('├──────────┼──────────┼───────────┼──────────┼───────┼───────┤');
|
||||
for (const r of results) {
|
||||
console.log(
|
||||
`│ ${String(r.classCount).padStart(8)} │ ${String(r.fileCount).padStart(8)} │ ${String(r.elapsedMs).padStart(9)} │ ${String(r.peakHeapMB).padStart(8)} │ ${String(r.nodeCount).padStart(5)} │ ${String(r.edgeCount).padStart(5)} │`,
|
||||
);
|
||||
}
|
||||
console.log('└──────────┴──────────┴───────────┴──────────┴───────┴───────┘');
|
||||
|
||||
if (results.length >= 2) {
|
||||
console.log('\nScaling ratios (time_ratio / class_ratio):');
|
||||
for (let i = 1; i < results.length; i++) {
|
||||
const classRatio = results[i].classCount / results[i - 1].classCount;
|
||||
const timeRatio = results[i].elapsedMs / results[i - 1].elapsedMs;
|
||||
const scaling = timeRatio / classRatio;
|
||||
console.log(
|
||||
` ${results[i - 1].classCount} → ${results[i].classCount}: ${scaling.toFixed(2)}x (${scaling < 1.5 ? 'linear' : scaling < 3 ? 'superlinear' : 'WARNING: quadratic'})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe.skipIf(!BENCH_ENABLED)('Objective-C pipeline benchmark', () => {
|
||||
it('scales with file count — typed sibling receivers', async () => {
|
||||
const scales = [40, 80, 160];
|
||||
const results: BenchResult[] = [];
|
||||
|
||||
for (const classCount of scales) {
|
||||
const result = await runBenchmark(classCount, 'spread', 180_000);
|
||||
results.push(result);
|
||||
console.log(
|
||||
` ${classCount} classes: ${result.elapsedMs}ms, ${result.peakHeapMB}MB heap, ${result.nodeCount} nodes, ${result.edgeCount} edges`,
|
||||
);
|
||||
}
|
||||
|
||||
printResults('Objective-C Pipeline — Spread', results);
|
||||
|
||||
expect(results[results.length - 1].edgeCount).toBeGreaterThan(0);
|
||||
|
||||
for (let i = 1; i < results.length; i++) {
|
||||
const classRatio = results[i].classCount / results[i - 1].classCount;
|
||||
const timeRatio = results[i].elapsedMs / results[i - 1].elapsedMs;
|
||||
expect(timeRatio / classRatio).toBeLessThan(3);
|
||||
// Wall-clock at these scales is worker-startup dominated; node/edge
|
||||
// cardinality is the load-bearing linearity check (cpp-pipeline analog).
|
||||
expect(results[i].nodeCount / results[i - 1].nodeCount).toBeCloseTo(classRatio, 1);
|
||||
expect(results[i].edgeCount / results[i - 1].edgeCount).toBeCloseTo(classRatio, 1);
|
||||
}
|
||||
}, 600_000);
|
||||
|
||||
it('scales with file count — shared protocol receivers', async () => {
|
||||
const scales = [40, 80, 160];
|
||||
const results: BenchResult[] = [];
|
||||
|
||||
for (const classCount of scales) {
|
||||
const result = await runBenchmark(classCount, 'protocol', 180_000);
|
||||
results.push(result);
|
||||
console.log(
|
||||
` ${classCount} classes: ${result.elapsedMs}ms, ${result.peakHeapMB}MB heap, ${result.nodeCount} nodes, ${result.edgeCount} edges`,
|
||||
);
|
||||
}
|
||||
|
||||
printResults('Objective-C Pipeline — Shared Protocol', results);
|
||||
|
||||
expect(results[results.length - 1].edgeCount).toBeGreaterThan(0);
|
||||
|
||||
for (let i = 1; i < results.length; i++) {
|
||||
const classRatio = results[i].classCount / results[i - 1].classCount;
|
||||
const timeRatio = results[i].elapsedMs / results[i - 1].elapsedMs;
|
||||
expect(timeRatio / classRatio).toBeLessThan(3);
|
||||
expect(results[i].nodeCount / results[i - 1].nodeCount).toBeCloseTo(classRatio, 1);
|
||||
expect(results[i].edgeCount / results[i - 1].edgeCount).toBeCloseTo(classRatio, 1);
|
||||
}
|
||||
}, 600_000);
|
||||
});
|
||||
887
gitnexus/test/integration/objective-c-provider.test.ts
Normal file
887
gitnexus/test/integration/objective-c-provider.test.ts
Normal file
|
|
@ -0,0 +1,887 @@
|
|||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
import { execSync } from 'child_process';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import type { GraphNode, RelationshipType } from 'gitnexus-shared';
|
||||
import { runPipelineFromRepo } from '../../src/core/ingestion/pipeline.js';
|
||||
import { runFullAnalysis } from '../../src/core/run-analyze.js';
|
||||
import { LocalBackend } from '../../src/mcp/local/local-backend.js';
|
||||
import { classifyObjectiveCFileContent } from '../../src/core/ingestion/languages/objective-c.js';
|
||||
import type { PipelineResult } from '../../src/types/pipeline.js';
|
||||
|
||||
const FIXTURE_DIR = path.resolve(
|
||||
path.dirname(fileURLToPath(import.meta.url)),
|
||||
'../fixtures/objective-c',
|
||||
);
|
||||
|
||||
function readFixture(name: string): string {
|
||||
return fs.readFileSync(path.join(FIXTURE_DIR, name), 'utf8');
|
||||
}
|
||||
|
||||
const HEADER = readFixture('SYModuleCaller.h');
|
||||
const IMPL = readFixture('SYModuleCaller.m');
|
||||
const MM_IMPL = readFixture('SYModuleBridge.mm');
|
||||
const PLAIN_C_HEADER = readFixture('SYModuleSupport.h');
|
||||
|
||||
const HEADER_V2 = HEADER.replace(
|
||||
'- (void)runTask:(NSString *)name completion:(void (^)(BOOL ok))completion;\n@end',
|
||||
'- (void)runTask:(NSString *)name completion:(void (^)(BOOL ok))completion;\n- (void)cancelTask;\n@end',
|
||||
).replace(
|
||||
'- (void)traceEvent:(NSString *)name;\n@end',
|
||||
'- (void)traceEvent:(NSString *)name;\n- (void)traceDetail:(NSString *)name level:(NSInteger)level;\n@end',
|
||||
);
|
||||
|
||||
const IMPL_V2 = IMPL.replace(
|
||||
'[self traceEvent:name];',
|
||||
'[self traceEvent:name];\n [self traceDetail:name level:1];',
|
||||
).replace(
|
||||
'@implementation SYModuleCaller (Tracing)\n- (void)traceEvent:(NSString *)name {}\n@end',
|
||||
'@implementation SYModuleCaller (Tracing)\n- (void)traceEvent:(NSString *)name {}\n- (void)traceDetail:(NSString *)name level:(NSInteger)level {}\n@end',
|
||||
);
|
||||
|
||||
function git(repoRoot: string, command: string): void {
|
||||
execSync(command, { cwd: repoRoot, stdio: 'pipe' });
|
||||
}
|
||||
|
||||
function gitCommitAll(repoRoot: string, message: string): void {
|
||||
git(repoRoot, 'git -c user.name=test -c user.email=t@t -c commit.gpgsign=false add -A');
|
||||
git(
|
||||
repoRoot,
|
||||
`git -c user.name=test -c user.email=t@t -c commit.gpgsign=false commit -q -m "${message}"`,
|
||||
);
|
||||
}
|
||||
|
||||
function writeObjectiveCRepo(repoRoot: string, header = HEADER, impl = IMPL): void {
|
||||
fs.writeFileSync(path.join(repoRoot, 'SYModuleCaller.h'), header);
|
||||
fs.writeFileSync(path.join(repoRoot, 'SYModuleCaller.m'), impl);
|
||||
fs.writeFileSync(path.join(repoRoot, 'SYModuleBridge.mm'), MM_IMPL);
|
||||
fs.writeFileSync(path.join(repoRoot, 'SYModuleSupport.h'), PLAIN_C_HEADER);
|
||||
}
|
||||
|
||||
function normalizeRows(rows: unknown): unknown[] {
|
||||
if (!Array.isArray(rows)) return [];
|
||||
return rows
|
||||
.map((row) => {
|
||||
const record = row as Record<string, unknown>;
|
||||
return {
|
||||
uid: record.uid ?? record.id,
|
||||
name: record.name,
|
||||
filePath: record.filePath,
|
||||
kind: record.kind,
|
||||
};
|
||||
})
|
||||
.sort((left, right) =>
|
||||
`${left.uid ?? ''}:${left.name ?? ''}:${left.filePath ?? ''}`.localeCompare(
|
||||
`${right.uid ?? ''}:${right.name ?? ''}:${right.filePath ?? ''}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeBuckets(value: unknown): Record<string, unknown[]> {
|
||||
const record = (value ?? {}) as Record<string, unknown>;
|
||||
return Object.fromEntries(
|
||||
Object.keys(record)
|
||||
.sort()
|
||||
.map((key) => [key, normalizeRows(record[key])]),
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeContext(value: unknown): Record<string, unknown> {
|
||||
const record = value as Record<string, unknown>;
|
||||
const symbol = (record.symbol ?? {}) as Record<string, unknown>;
|
||||
return {
|
||||
status: record.status,
|
||||
symbol: {
|
||||
uid: symbol.uid,
|
||||
name: symbol.name,
|
||||
kind: symbol.kind,
|
||||
filePath: symbol.filePath,
|
||||
},
|
||||
incoming: normalizeBuckets(record.incoming),
|
||||
outgoing: normalizeBuckets(record.outgoing),
|
||||
};
|
||||
}
|
||||
|
||||
async function readPersistedObjectiveCSurface(repoRoot: string): Promise<Record<string, unknown>> {
|
||||
const backend = new LocalBackend();
|
||||
try {
|
||||
const classContext = await backend.callTool('context', {
|
||||
name: 'SYModuleCaller',
|
||||
file_path: 'SYModuleCaller.h',
|
||||
repo: repoRoot,
|
||||
});
|
||||
const runTaskContext = await backend.callTool('context', {
|
||||
uid: 'Method:objc:method:objc:class:SYModuleCaller:-:runTask:completion:',
|
||||
repo: repoRoot,
|
||||
});
|
||||
const runProtocolContext = await backend.callTool('context', {
|
||||
uid: 'Method:objc:method:objc:class:SYModuleCaller:-:runProtocol:',
|
||||
repo: repoRoot,
|
||||
});
|
||||
const candidateEvidenceId = (
|
||||
(
|
||||
(runProtocolContext as Record<string, unknown>).outgoing as
|
||||
| Record<string, unknown[]>
|
||||
| undefined
|
||||
)?.uses ?? []
|
||||
)
|
||||
.map((entry) => (entry as Record<string, unknown>).uid)
|
||||
.find(
|
||||
(uid): uid is string =>
|
||||
typeof uid === 'string' && uid.startsWith('CodeElement:objc:protocol-candidates:'),
|
||||
);
|
||||
if (candidateEvidenceId === undefined) {
|
||||
throw new Error('Persisted protocol candidate evidence was not reachable from context');
|
||||
}
|
||||
const candidateEvidenceContext = await backend.callTool('context', {
|
||||
uid: candidateEvidenceId,
|
||||
repo: repoRoot,
|
||||
});
|
||||
const categoryContext = await backend.callTool('context', {
|
||||
uid: 'Category:objc:category:SYModuleCaller:Tracing',
|
||||
repo: repoRoot,
|
||||
});
|
||||
const queryResult = (await backend.callTool('query', {
|
||||
search_query: 'SYModuleCaller',
|
||||
repo: repoRoot,
|
||||
limit: 5,
|
||||
include_content: false,
|
||||
})) as Record<string, unknown>;
|
||||
const protocolAndCategoryResult = await backend.callTool('cypher', {
|
||||
query:
|
||||
"MATCH (n) WHERE labels(n) IN ['Protocol', 'Category'] " +
|
||||
'RETURN n.id AS id, labels(n)[0] AS kind ORDER BY kind, id',
|
||||
repo: repoRoot,
|
||||
});
|
||||
const categoryHostResult = await backend.callTool('cypher', {
|
||||
query:
|
||||
'MATCH (category:Category)-[r:CodeRelation]->(host:Class) ' +
|
||||
"WHERE r.type = 'MEMBER_OF' " +
|
||||
'RETURN category.id AS category, host.id AS host',
|
||||
repo: repoRoot,
|
||||
});
|
||||
const protocolCandidateResult = await backend.callTool('cypher', {
|
||||
query:
|
||||
'MATCH (source:Method)-[sourceRel:CodeRelation]->(e:CodeElement)-[candidateRel:CodeRelation]->(candidate:Method) ' +
|
||||
"WHERE sourceRel.type = 'USES' AND candidateRel.type = 'USES' " +
|
||||
"AND e.id STARTS WITH 'CodeElement:objc:protocol-candidates:' " +
|
||||
'RETURN source.id AS sourceId, candidate.id AS candidateId, candidateRel.reason AS reason ' +
|
||||
'ORDER BY sourceId, candidateId',
|
||||
repo: repoRoot,
|
||||
});
|
||||
const unresolvedReasonResult = await backend.callTool('cypher', {
|
||||
query:
|
||||
'MATCH (source:Method)-[r:CodeRelation]->(e:CodeElement) ' +
|
||||
"WHERE r.type = 'USES' AND e.id STARTS WITH 'CodeElement:objc:unresolved:' " +
|
||||
'RETURN source.id AS sourceId, e.name AS evidence, r.reason AS reason ' +
|
||||
'ORDER BY sourceId, evidence',
|
||||
repo: repoRoot,
|
||||
});
|
||||
return {
|
||||
classContext: normalizeContext(classContext),
|
||||
runTaskContext: normalizeContext(runTaskContext),
|
||||
runProtocolContext: normalizeContext(runProtocolContext),
|
||||
candidateEvidenceContext: normalizeContext(candidateEvidenceContext),
|
||||
categoryContext: normalizeContext(categoryContext),
|
||||
queryDefinitions: normalizeRows(queryResult.definitions),
|
||||
protocolAndCategoryResult,
|
||||
categoryHostResult,
|
||||
protocolCandidateResult,
|
||||
unresolvedReasonResult,
|
||||
};
|
||||
} finally {
|
||||
await backend.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
async function analyzeObjectiveCRepo(
|
||||
repoRoot: string,
|
||||
options: { force?: boolean } = {},
|
||||
): Promise<string[]> {
|
||||
const logs: string[] = [];
|
||||
await runFullAnalysis(
|
||||
repoRoot,
|
||||
{
|
||||
force: options.force,
|
||||
skipAgentsMd: true,
|
||||
skipSkills: true,
|
||||
workerPoolSize: 1,
|
||||
},
|
||||
{
|
||||
onProgress: () => undefined,
|
||||
onLog: (message) => logs.push(message),
|
||||
},
|
||||
);
|
||||
return logs;
|
||||
}
|
||||
|
||||
describe('Objective-C provider integration', () => {
|
||||
let repoRoot: string;
|
||||
let result: PipelineResult;
|
||||
|
||||
beforeAll(async () => {
|
||||
repoRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-objc-provider-'));
|
||||
fs.writeFileSync(path.join(repoRoot, 'SYModuleCaller.h'), HEADER);
|
||||
fs.writeFileSync(path.join(repoRoot, 'SYModuleCaller.m'), IMPL);
|
||||
fs.writeFileSync(path.join(repoRoot, 'SYModuleBridge.mm'), MM_IMPL);
|
||||
fs.writeFileSync(path.join(repoRoot, 'SYModuleSupport.h'), PLAIN_C_HEADER);
|
||||
result = await runPipelineFromRepo(repoRoot, () => undefined, {
|
||||
workerPoolSize: 1,
|
||||
});
|
||||
}, 60000);
|
||||
|
||||
afterAll(() => {
|
||||
fs.rmSync(repoRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function nodeByQualifiedName(qualifiedName: string): GraphNode | undefined {
|
||||
return result.graph.nodes.find((node) => node.properties.qualifiedName === qualifiedName);
|
||||
}
|
||||
|
||||
function expectNode(qualifiedName: string, label: GraphNode['label']): GraphNode {
|
||||
const node = nodeByQualifiedName(qualifiedName);
|
||||
expect(node, qualifiedName).toBeDefined();
|
||||
expect(node?.label).toBe(label);
|
||||
if (node === undefined) throw new Error(`Missing expected node ${qualifiedName}`);
|
||||
return node;
|
||||
}
|
||||
|
||||
function hasRelationship(
|
||||
type: RelationshipType,
|
||||
sourceId: string,
|
||||
targetId: string,
|
||||
reason?: string | RegExp,
|
||||
): boolean {
|
||||
return result.graph.relationships.some((rel) => {
|
||||
if (rel.type !== type || rel.sourceId !== sourceId || rel.targetId !== targetId) return false;
|
||||
if (reason === undefined) return true;
|
||||
return typeof reason === 'string' ? rel.reason === reason : reason.test(rel.reason);
|
||||
});
|
||||
}
|
||||
|
||||
it('classifies a macro-wrapped Objective-C header by declarations after the marker', () => {
|
||||
expect(
|
||||
classifyObjectiveCFileContent(
|
||||
'RCTBridgeModule.h',
|
||||
[
|
||||
'RCT_EXTERN_C_BEGIN',
|
||||
'typedef struct RCTMethodInfo {',
|
||||
' const char *const jsName;',
|
||||
'} RCTMethodInfo;',
|
||||
'RCT_EXTERN_C_END',
|
||||
'@protocol RCTBridgeModule <NSObject>',
|
||||
'- (void)run;',
|
||||
'@end',
|
||||
'',
|
||||
].join('\n'),
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('indexes Objective-C semantic nodes beyond File nodes', () => {
|
||||
expectNode('objc:protocol:SYModuleRunnable', 'Protocol');
|
||||
expectNode('objc:class:SYBaseCaller', 'Class');
|
||||
expectNode('objc:class:SYModuleCaller', 'Class');
|
||||
expectNode('objc:class:SYModuleBridge', 'Class');
|
||||
expectNode('objc:category:SYModuleCaller:Tracing', 'Category');
|
||||
expectNode('objc:method:objc:class:SYModuleCaller:-:runTask:completion:', 'Method');
|
||||
expectNode('objc:method:objc:class:SYModuleBridge:-:bridgeValue:', 'Method');
|
||||
expectNode('objc:method:objc:class:SYModuleCaller:+:sharedCaller', 'Method');
|
||||
expectNode('objc:method:objc:category:SYModuleCaller:Tracing:-:traceEvent:', 'Method');
|
||||
expectNode('objc:property:objc:class:SYModuleCaller:helper', 'Property');
|
||||
expectNode('objc:ivar:objc:class:SYModuleCaller:_base', 'Variable');
|
||||
expectNode('objc:function:SYModuleSupportAdd', 'Function');
|
||||
expectNode('objc:function:static:SYModuleCaller.m:SYModuleCompute', 'Function');
|
||||
});
|
||||
|
||||
it('emits imports, inheritance, protocol, and category host relationships', () => {
|
||||
const caller = expectNode('objc:class:SYModuleCaller', 'Class');
|
||||
const base = expectNode('objc:class:SYBaseCaller', 'Class');
|
||||
const protocol = expectNode('objc:protocol:SYModuleRunnable', 'Protocol');
|
||||
const category = expectNode('objc:category:SYModuleCaller:Tracing', 'Category');
|
||||
|
||||
expect(hasRelationship('EXTENDS', caller.id, base.id)).toBe(true);
|
||||
expect(hasRelationship('IMPLEMENTS', caller.id, protocol.id)).toBe(true);
|
||||
expect(hasRelationship('MEMBER_OF', category.id, caller.id)).toBe(true);
|
||||
|
||||
const importNodes = result.graph.nodes.filter((node) => node.label === 'Import');
|
||||
expect(importNodes.map((node) => node.properties.targetRaw)).toEqual(
|
||||
expect.arrayContaining(['SYModuleCaller.h', 'SYModuleSupport.h', 'Foundation']),
|
||||
);
|
||||
|
||||
const mFile = result.graph.nodes.find(
|
||||
(node) => node.label === 'File' && node.properties.filePath === 'SYModuleCaller.m',
|
||||
);
|
||||
const hFile = result.graph.nodes.find(
|
||||
(node) => node.label === 'File' && node.properties.filePath === 'SYModuleCaller.h',
|
||||
);
|
||||
const supportFile = result.graph.nodes.find(
|
||||
(node) => node.label === 'File' && node.properties.filePath === 'SYModuleSupport.h',
|
||||
);
|
||||
expect(mFile).toBeDefined();
|
||||
expect(hFile).toBeDefined();
|
||||
expect(supportFile).toBeDefined();
|
||||
if (mFile === undefined || hFile === undefined || supportFile === undefined) {
|
||||
throw new Error('Missing Objective-C fixture file nodes');
|
||||
}
|
||||
expect(hasRelationship('IMPORTS', mFile.id, hFile.id)).toBe(true);
|
||||
expect(hasRelationship('IMPORTS', mFile.id, supportFile.id)).toBe(true);
|
||||
});
|
||||
|
||||
it('records implementation evidence for merged declarations', () => {
|
||||
const caller = expectNode('objc:class:SYModuleCaller', 'Class');
|
||||
const runTask = expectNode(
|
||||
'objc:method:objc:class:SYModuleCaller:-:runTask:completion:',
|
||||
'Method',
|
||||
);
|
||||
|
||||
const implementationEvidence = result.graph.nodes.filter(
|
||||
(node) =>
|
||||
node.label === 'CodeElement' &&
|
||||
node.properties.objectiveCKind === 'implementation-evidence' &&
|
||||
node.properties.filePath === 'SYModuleCaller.m',
|
||||
);
|
||||
expect(implementationEvidence.map((node) => node.properties.targetQualifiedName)).toEqual(
|
||||
expect.arrayContaining([
|
||||
'objc:class:SYModuleCaller',
|
||||
'objc:method:objc:class:SYModuleCaller:-:runTask:completion:',
|
||||
]),
|
||||
);
|
||||
expect(
|
||||
implementationEvidence.some((node) =>
|
||||
hasRelationship('DECLARES', node.id, caller.id, 'objc: implementation of merged symbol'),
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
implementationEvidence.some((node) =>
|
||||
hasRelationship('DECLARES', node.id, runTask.id, 'objc: implementation of merged symbol'),
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('emits conservative Objective-C message-send call edges and unresolved evidence', () => {
|
||||
const runTask = expectNode(
|
||||
'objc:method:objc:class:SYModuleCaller:-:runTask:completion:',
|
||||
'Method',
|
||||
);
|
||||
const loadData = expectNode(
|
||||
'objc:method:objc:class:SYBaseCaller:-:loadData:completion:',
|
||||
'Method',
|
||||
);
|
||||
const traceEvent = expectNode(
|
||||
'objc:method:objc:category:SYModuleCaller:Tracing:-:traceEvent:',
|
||||
'Method',
|
||||
);
|
||||
const runProtocol = expectNode(
|
||||
'objc:method:objc:class:SYModuleCaller:-:runProtocol:',
|
||||
'Method',
|
||||
);
|
||||
const protocolRun = expectNode(
|
||||
'objc:method:objc:protocol:SYModuleRunnable:-:runTask:completion:',
|
||||
'Method',
|
||||
);
|
||||
|
||||
expect(
|
||||
hasRelationship('CALLS', runTask.id, loadData.id, /objc-message: (super|local) receiver/),
|
||||
).toBe(true);
|
||||
expect(hasRelationship('CALLS', runTask.id, loadData.id, 'objc-message: self receiver')).toBe(
|
||||
true,
|
||||
);
|
||||
expect(hasRelationship('CALLS', runTask.id, traceEvent.id, 'objc-message: self receiver')).toBe(
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
hasRelationship('CALLS', runProtocol.id, protocolRun.id, 'objc-message: protocol receiver'),
|
||||
).toBe(true);
|
||||
|
||||
const unresolved = result.graph.nodes.find(
|
||||
(node) =>
|
||||
node.label === 'CodeElement' &&
|
||||
node.properties.objectiveCKind === 'unresolved-message' &&
|
||||
node.properties.receiver === 'dynamic',
|
||||
);
|
||||
expect(unresolved).toBeDefined();
|
||||
if (unresolved === undefined) throw new Error('Missing unresolved dynamic message evidence');
|
||||
expect(
|
||||
hasRelationship('CALLS', runTask.id, unresolved.id),
|
||||
'dynamic id receiver must not become a certain CALLS edge',
|
||||
).toBe(false);
|
||||
expect(
|
||||
hasRelationship(
|
||||
'USES',
|
||||
runTask.id,
|
||||
unresolved.id,
|
||||
'objc-message: unresolved: id receiver is dynamic',
|
||||
),
|
||||
).toBe(true);
|
||||
expect(unresolved.properties.name).toContain('unresolved: id receiver is dynamic');
|
||||
|
||||
const macroUnresolved = result.graph.nodes.find(
|
||||
(node) =>
|
||||
node.label === 'CodeElement' &&
|
||||
node.properties.objectiveCKind === 'unresolved-message' &&
|
||||
node.properties.receiver === 'SY_OBJC_RECEIVER(self)',
|
||||
);
|
||||
expect(macroUnresolved?.properties.name).toContain(
|
||||
'unresolved: macro receiver SY_OBJC_RECEIVER is dynamic',
|
||||
);
|
||||
|
||||
const candidates = result.graph.nodes.find(
|
||||
(node) =>
|
||||
node.label === 'CodeElement' &&
|
||||
String(node.properties.qualifiedName).startsWith('objc:protocol-candidates:'),
|
||||
);
|
||||
expect(candidates).toBeDefined();
|
||||
if (candidates === undefined) throw new Error('Missing protocol candidate evidence');
|
||||
expect(
|
||||
hasRelationship(
|
||||
'USES',
|
||||
runProtocol.id,
|
||||
candidates.id,
|
||||
'objc-message: protocol receiver candidates: SYModuleRunnable runTask:completion:',
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
hasRelationship(
|
||||
'USES',
|
||||
candidates.id,
|
||||
runTask.id,
|
||||
'objc-protocol-candidate: SYModuleRunnable runTask:completion:',
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
hasRelationship('CALLS', runProtocol.id, runTask.id),
|
||||
'candidate implementations must not become certain CALLS edges',
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('resolves header member types across files and keeps static C helpers distinct', async () => {
|
||||
const crossFileRepo = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-objc-cross-file-'));
|
||||
try {
|
||||
fs.writeFileSync(
|
||||
path.join(crossFileRepo, 'Helper.h'),
|
||||
'@interface Helper\n- (void)ping;\n@end\n',
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(crossFileRepo, 'Worker.h'),
|
||||
'#import "Helper.h"\n@interface Worker { Helper *_helper; }\n@property Helper *helper;\n- (void)run;\n@end\n',
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(crossFileRepo, 'Worker.m'),
|
||||
'#import "Worker.h"\n@implementation Worker\n- (void)run { [self.helper ping]; [_helper ping]; }\n@end\n',
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(crossFileRepo, 'First.m'),
|
||||
'static int helper(void) { return 1; }\n',
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(crossFileRepo, 'Second.m'),
|
||||
'static int helper(void) { return 2; }\n',
|
||||
);
|
||||
|
||||
const crossFileResult = await runPipelineFromRepo(crossFileRepo, () => undefined, {
|
||||
workerPoolSize: 1,
|
||||
});
|
||||
const run = crossFileResult.graph.nodes.find(
|
||||
(node) =>
|
||||
node.label === 'Method' &&
|
||||
node.properties.qualifiedName === 'objc:method:objc:class:Worker:-:run',
|
||||
);
|
||||
const ping = crossFileResult.graph.nodes.find(
|
||||
(node) =>
|
||||
node.label === 'Method' &&
|
||||
node.properties.qualifiedName === 'objc:method:objc:class:Helper:-:ping',
|
||||
);
|
||||
expect(run).toBeDefined();
|
||||
expect(ping).toBeDefined();
|
||||
if (run === undefined || ping === undefined) {
|
||||
throw new Error('Missing cross-file Objective-C method nodes');
|
||||
}
|
||||
|
||||
const memberCalls = crossFileResult.graph.relationships.filter(
|
||||
(relationship) =>
|
||||
relationship.type === 'CALLS' &&
|
||||
relationship.sourceId === run.id &&
|
||||
relationship.targetId === ping.id,
|
||||
);
|
||||
expect(memberCalls).toHaveLength(2);
|
||||
|
||||
const staticHelpers = crossFileResult.graph.nodes.filter(
|
||||
(node) => node.label === 'Function' && node.properties.name === 'helper',
|
||||
);
|
||||
expect(staticHelpers.map((node) => node.properties.filePath).sort()).toEqual([
|
||||
'First.m',
|
||||
'Second.m',
|
||||
]);
|
||||
expect(new Set(staticHelpers.map((node) => node.properties.qualifiedName)).size).toBe(2);
|
||||
} finally {
|
||||
fs.rmSync(crossFileRepo, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('resolves inherited protocol members and candidates without looping on protocol cycles', async () => {
|
||||
const protocolRepo = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-objc-protocols-'));
|
||||
try {
|
||||
fs.writeFileSync(
|
||||
path.join(protocolRepo, 'Protocols.h'),
|
||||
'@protocol Parent\n' +
|
||||
'- (void)ping;\n' +
|
||||
'@end\n' +
|
||||
'@protocol Child <Parent>\n' +
|
||||
'@end\n' +
|
||||
'@protocol CycleA <CycleB>\n' +
|
||||
'- (void)cycleA;\n' +
|
||||
'@end\n' +
|
||||
'@protocol CycleB <CycleA>\n' +
|
||||
'- (void)cycleB;\n' +
|
||||
'@end\n' +
|
||||
'@interface BaseWorker <Child>\n' +
|
||||
'@end\n' +
|
||||
'@interface ConcreteWorker : BaseWorker\n' +
|
||||
'- (void)ping;\n' +
|
||||
'@end\n' +
|
||||
'@interface Caller\n' +
|
||||
'- (void)run:(id<Child>)worker;\n' +
|
||||
'- (void)runCycle:(id<CycleA>)worker;\n' +
|
||||
'@end\n',
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(protocolRepo, 'Protocols.m'),
|
||||
'#import "Protocols.h"\n' +
|
||||
'@implementation ConcreteWorker\n' +
|
||||
'- (void)ping {}\n' +
|
||||
'@end\n' +
|
||||
'@implementation Caller\n' +
|
||||
'- (void)run:(id<Child>)worker { [worker ping]; }\n' +
|
||||
'- (void)runCycle:(id<CycleA>)worker { [worker cycleB]; }\n' +
|
||||
'@end\n',
|
||||
);
|
||||
|
||||
const protocolResult = await runPipelineFromRepo(protocolRepo, () => undefined, {
|
||||
workerPoolSize: 1,
|
||||
});
|
||||
const node = (qualifiedName: string): GraphNode => {
|
||||
const found = protocolResult.graph.nodes.find(
|
||||
(item) => item.properties.qualifiedName === qualifiedName,
|
||||
);
|
||||
if (found === undefined) throw new Error(`Missing protocol fixture node ${qualifiedName}`);
|
||||
return found;
|
||||
};
|
||||
const has = (type: RelationshipType, sourceId: string, targetId: string): boolean =>
|
||||
protocolResult.graph.relationships.some(
|
||||
(relationship) =>
|
||||
relationship.type === type &&
|
||||
relationship.sourceId === sourceId &&
|
||||
relationship.targetId === targetId,
|
||||
);
|
||||
|
||||
const caller = node('objc:method:objc:class:Caller:-:run:');
|
||||
const parentMethod = node('objc:method:objc:protocol:Parent:-:ping');
|
||||
const workerMethod = node('objc:method:objc:class:ConcreteWorker:-:ping');
|
||||
const cycleCaller = node('objc:method:objc:class:Caller:-:runCycle:');
|
||||
const cycleMethod = node('objc:method:objc:protocol:CycleB:-:cycleB');
|
||||
const candidateEvidence = protocolResult.graph.nodes.find(
|
||||
(item) =>
|
||||
item.label === 'CodeElement' &&
|
||||
String(item.properties.qualifiedName).startsWith('objc:protocol-candidates:'),
|
||||
);
|
||||
|
||||
expect(has('CALLS', caller.id, parentMethod.id)).toBe(true);
|
||||
expect(has('CALLS', caller.id, workerMethod.id)).toBe(false);
|
||||
expect(candidateEvidence).toBeDefined();
|
||||
if (candidateEvidence === undefined)
|
||||
throw new Error('Missing inherited protocol candidate evidence');
|
||||
expect(has('USES', candidateEvidence.id, workerMethod.id)).toBe(true);
|
||||
expect(has('CALLS', cycleCaller.id, cycleMethod.id)).toBe(true);
|
||||
} finally {
|
||||
fs.rmSync(protocolRepo, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('emits category membership only when the host class exists locally', async () => {
|
||||
const categoryRepo = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-objc-category-host-'));
|
||||
try {
|
||||
fs.writeFileSync(
|
||||
path.join(categoryRepo, 'Categories.m'),
|
||||
'@interface LocalHost\n@end\n' +
|
||||
'@interface LocalHost (Tracing)\n@end\n' +
|
||||
'@interface UIView (Tracing)\n@end\n',
|
||||
);
|
||||
|
||||
const categoryResult = await runPipelineFromRepo(categoryRepo, () => undefined, {
|
||||
workerPoolSize: 1,
|
||||
});
|
||||
const localHost = categoryResult.graph.nodes.find(
|
||||
(node) => node.properties.qualifiedName === 'objc:class:LocalHost',
|
||||
);
|
||||
const localCategory = categoryResult.graph.nodes.find(
|
||||
(node) => node.properties.qualifiedName === 'objc:category:LocalHost:Tracing',
|
||||
);
|
||||
const sdkCategory = categoryResult.graph.nodes.find(
|
||||
(node) => node.properties.qualifiedName === 'objc:category:UIView:Tracing',
|
||||
);
|
||||
expect(localHost).toBeDefined();
|
||||
expect(localCategory).toBeDefined();
|
||||
expect(sdkCategory).toBeDefined();
|
||||
if (localHost === undefined || localCategory === undefined || sdkCategory === undefined) {
|
||||
throw new Error('Missing category host fixture nodes');
|
||||
}
|
||||
|
||||
expect(
|
||||
categoryResult.graph.relationships.some(
|
||||
(relationship) =>
|
||||
relationship.type === 'MEMBER_OF' &&
|
||||
relationship.sourceId === localCategory.id &&
|
||||
relationship.targetId === localHost.id,
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
categoryResult.graph.relationships.some(
|
||||
(relationship) =>
|
||||
relationship.type === 'MEMBER_OF' && relationship.sourceId === sdkCategory.id,
|
||||
),
|
||||
).toBe(false);
|
||||
} finally {
|
||||
fs.rmSync(categoryRepo, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('keeps category selector collisions as candidate evidence instead of certain calls', async () => {
|
||||
const collisionRepo = fs.mkdtempSync(
|
||||
path.join(os.tmpdir(), 'gitnexus-objc-category-collision-'),
|
||||
);
|
||||
try {
|
||||
fs.writeFileSync(
|
||||
path.join(collisionRepo, 'Collision.m'),
|
||||
`@interface CollisionWorker
|
||||
- (void)run;
|
||||
- (void)refresh;
|
||||
@end
|
||||
|
||||
@interface CollisionWorker (Tracing)
|
||||
- (void)refresh;
|
||||
- (void)categoryOnly;
|
||||
@end
|
||||
|
||||
@interface CollisionWorker (Metrics)
|
||||
- (void)refresh;
|
||||
@end
|
||||
|
||||
@implementation CollisionWorker
|
||||
- (void)run {
|
||||
[self refresh];
|
||||
CollisionWorker *worker = self;
|
||||
[worker refresh];
|
||||
[self categoryOnly];
|
||||
}
|
||||
- (void)refresh {}
|
||||
@end
|
||||
|
||||
@implementation CollisionWorker (Tracing)
|
||||
- (void)refresh {}
|
||||
- (void)categoryOnly {}
|
||||
@end
|
||||
|
||||
@implementation CollisionWorker (Metrics)
|
||||
- (void)refresh {}
|
||||
@end
|
||||
`,
|
||||
);
|
||||
|
||||
const collisionResult = await runPipelineFromRepo(collisionRepo, () => undefined, {
|
||||
workerPoolSize: 1,
|
||||
});
|
||||
const node = (qualifiedName: string): GraphNode => {
|
||||
const found = collisionResult.graph.nodes.find(
|
||||
(item) => item.properties.qualifiedName === qualifiedName,
|
||||
);
|
||||
if (found === undefined)
|
||||
throw new Error(`Missing category collision node ${qualifiedName}`);
|
||||
return found;
|
||||
};
|
||||
const caller = node('objc:method:objc:class:CollisionWorker:-:run');
|
||||
const collisionTargets = [
|
||||
node('objc:method:objc:class:CollisionWorker:-:refresh'),
|
||||
node('objc:method:objc:category:CollisionWorker:Tracing:-:refresh'),
|
||||
node('objc:method:objc:category:CollisionWorker:Metrics:-:refresh'),
|
||||
];
|
||||
const categoryOnly = node('objc:method:objc:category:CollisionWorker:Tracing:-:categoryOnly');
|
||||
const collisionTargetIds = new Set(collisionTargets.map((target) => target.id));
|
||||
const certainCollisionCalls = collisionResult.graph.relationships.filter(
|
||||
(relationship) =>
|
||||
relationship.type === 'CALLS' &&
|
||||
relationship.sourceId === caller.id &&
|
||||
collisionTargetIds.has(relationship.targetId),
|
||||
);
|
||||
|
||||
expect(certainCollisionCalls).toHaveLength(0);
|
||||
|
||||
const evidenceNodes = collisionResult.graph.nodes.filter(
|
||||
(item) =>
|
||||
item.label === 'CodeElement' &&
|
||||
String(item.properties.qualifiedName).startsWith('objc:category-dispatch-candidates:'),
|
||||
);
|
||||
expect(evidenceNodes).toHaveLength(2);
|
||||
|
||||
for (const evidence of evidenceNodes) {
|
||||
expect(
|
||||
collisionResult.graph.relationships.some(
|
||||
(relationship) =>
|
||||
relationship.type === 'USES' &&
|
||||
relationship.sourceId === caller.id &&
|
||||
relationship.targetId === evidence.id &&
|
||||
relationship.confidence === 0.7,
|
||||
),
|
||||
).toBe(true);
|
||||
for (const target of collisionTargets) {
|
||||
expect(
|
||||
collisionResult.graph.relationships.some(
|
||||
(relationship) =>
|
||||
relationship.type === 'USES' &&
|
||||
relationship.sourceId === evidence.id &&
|
||||
relationship.targetId === target.id &&
|
||||
relationship.confidence === 0.5,
|
||||
),
|
||||
).toBe(true);
|
||||
}
|
||||
}
|
||||
|
||||
const categoryOnlyCalls = collisionResult.graph.relationships.filter(
|
||||
(relationship) =>
|
||||
relationship.type === 'CALLS' &&
|
||||
relationship.sourceId === caller.id &&
|
||||
relationship.targetId === categoryOnly.id,
|
||||
);
|
||||
expect(categoryOnlyCalls).toHaveLength(1);
|
||||
expect(categoryOnlyCalls[0]?.confidence).toBe(0.9);
|
||||
} finally {
|
||||
fs.rmSync(collisionRepo, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('Objective-C provider persisted index behavior', () => {
|
||||
it('surfaces query/context semantics and keeps incremental results aligned with force rebuild', async () => {
|
||||
const repoRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-objc-provider-index-'));
|
||||
try {
|
||||
writeObjectiveCRepo(repoRoot);
|
||||
git(repoRoot, 'git init');
|
||||
gitCommitAll(repoRoot, 'initial Objective-C fixture');
|
||||
|
||||
await analyzeObjectiveCRepo(repoRoot);
|
||||
const initialSurface = await readPersistedObjectiveCSurface(repoRoot);
|
||||
const reopenedInitialSurface = await readPersistedObjectiveCSurface(repoRoot);
|
||||
expect(reopenedInitialSurface).toEqual(initialSurface);
|
||||
expect(initialSurface).toMatchObject({
|
||||
classContext: {
|
||||
status: 'found',
|
||||
symbol: {
|
||||
uid: 'Class:objc:class:SYModuleCaller',
|
||||
kind: 'Class',
|
||||
filePath: 'SYModuleCaller.h',
|
||||
},
|
||||
incoming: {
|
||||
declares: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
uid: expect.stringContaining(
|
||||
'CodeElement:objc:implementation:objc:class:SYModuleCaller:SYModuleCaller.m:',
|
||||
),
|
||||
filePath: 'SYModuleCaller.m',
|
||||
}),
|
||||
]),
|
||||
imports: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
uid: 'File:SYModuleCaller.m',
|
||||
filePath: 'SYModuleCaller.m',
|
||||
}),
|
||||
]),
|
||||
member_of: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
uid: 'Category:objc:category:SYModuleCaller:Tracing',
|
||||
}),
|
||||
]),
|
||||
},
|
||||
},
|
||||
categoryContext: {
|
||||
outgoing: {
|
||||
member_of: expect.arrayContaining([
|
||||
expect.objectContaining({ uid: 'Class:objc:class:SYModuleCaller' }),
|
||||
]),
|
||||
},
|
||||
},
|
||||
runProtocolContext: {
|
||||
outgoing: {
|
||||
uses: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
uid: expect.stringContaining('CodeElement:objc:protocol-candidates:'),
|
||||
}),
|
||||
]),
|
||||
},
|
||||
},
|
||||
candidateEvidenceContext: {
|
||||
outgoing: {
|
||||
uses: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
uid: 'Method:objc:method:objc:class:SYModuleCaller:-:runTask:completion:',
|
||||
}),
|
||||
]),
|
||||
},
|
||||
},
|
||||
queryDefinitions: expect.arrayContaining([
|
||||
expect.objectContaining({ uid: 'Class:objc:class:SYModuleCaller' }),
|
||||
expect.objectContaining({
|
||||
uid: expect.stringMatching(/^Method:objc:method:objc:class:SYModuleCaller:/),
|
||||
}),
|
||||
]),
|
||||
protocolAndCategoryResult: expect.objectContaining({
|
||||
markdown: expect.stringContaining('Protocol:objc:protocol:SYModuleRunnable'),
|
||||
}),
|
||||
categoryHostResult: expect.objectContaining({
|
||||
markdown: expect.stringContaining('Category:objc:category:SYModuleCaller:Tracing'),
|
||||
}),
|
||||
protocolCandidateResult: expect.objectContaining({
|
||||
markdown: expect.stringContaining(
|
||||
'objc-protocol-candidate: SYModuleRunnable runTask:completion:',
|
||||
),
|
||||
}),
|
||||
unresolvedReasonResult: expect.objectContaining({
|
||||
markdown: expect.stringContaining('objc-message: unresolved: id receiver is dynamic'),
|
||||
}),
|
||||
});
|
||||
|
||||
writeObjectiveCRepo(repoRoot, HEADER_V2, IMPL_V2);
|
||||
gitCommitAll(repoRoot, 'change Objective-C declarations and implementations');
|
||||
const incrementalLogs = await analyzeObjectiveCRepo(repoRoot);
|
||||
expect(incrementalLogs).toContainEqual(expect.stringContaining('Incremental: changed='));
|
||||
const incrementalSurface = await readPersistedObjectiveCSurface(repoRoot);
|
||||
|
||||
await analyzeObjectiveCRepo(repoRoot, { force: true });
|
||||
const forceSurface = await readPersistedObjectiveCSurface(repoRoot);
|
||||
expect(incrementalSurface).toEqual(forceSurface);
|
||||
expect(forceSurface).toMatchObject({
|
||||
classContext: {
|
||||
outgoing: {
|
||||
has_method: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
uid: 'Method:objc:method:objc:category:SYModuleCaller:Tracing:-:traceDetail:level:',
|
||||
}),
|
||||
]),
|
||||
},
|
||||
},
|
||||
runTaskContext: {
|
||||
outgoing: {
|
||||
calls: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
uid: 'Method:objc:method:objc:category:SYModuleCaller:Tracing:-:traceDetail:level:',
|
||||
}),
|
||||
]),
|
||||
},
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
fs.rmSync(repoRoot, { recursive: true, force: true });
|
||||
}
|
||||
}, 180000);
|
||||
});
|
||||
|
|
@ -16,6 +16,8 @@ import {
|
|||
pruneAndSaveDurableParsedFileStore,
|
||||
} from '../../../src/storage/parsedfile-store.js';
|
||||
|
||||
type CallableFlowCoverage = 'matrix' | 'dedicated' | 'not-applicable';
|
||||
|
||||
const CALLABLE_FLOW_PROVIDER_COVERAGE = {
|
||||
[SupportedLanguages.JavaScript]: 'matrix',
|
||||
[SupportedLanguages.TypeScript]: 'dedicated',
|
||||
|
|
@ -34,7 +36,10 @@ const CALLABLE_FLOW_PROVIDER_COVERAGE = {
|
|||
[SupportedLanguages.Vue]: 'matrix',
|
||||
[SupportedLanguages.Cobol]: 'matrix',
|
||||
[SupportedLanguages.Zig]: 'matrix',
|
||||
} as const satisfies Record<SupportedLanguages, 'matrix' | 'dedicated'>;
|
||||
// Objective-C message sends are resolved by its ScopeResolver. The provider
|
||||
// does not emit callable-value-flow captures in this MVP.
|
||||
[SupportedLanguages.ObjectiveC]: 'not-applicable',
|
||||
} as const satisfies Record<SupportedLanguages, CallableFlowCoverage>;
|
||||
|
||||
const PROVIDER_FLOW_CASES = [
|
||||
{
|
||||
|
|
@ -339,6 +344,13 @@ describe('callable value flow', () => {
|
|||
);
|
||||
});
|
||||
|
||||
it('does not overstate Objective-C callable-value-flow coverage', () => {
|
||||
expect(CALLABLE_FLOW_PROVIDER_COVERAGE[SupportedLanguages.ObjectiveC]).toBe('not-applicable');
|
||||
expect(
|
||||
PROVIDER_FLOW_CASES.some(({ language }) => language === SupportedLanguages.ObjectiveC),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it.each(PROVIDER_FLOW_CASES)(
|
||||
'resolves assign → copy → argument → invoke for $language',
|
||||
async ({ extension, source, caller, target }) => {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,12 @@ import {
|
|||
type AnalysisFeatureDescriptor,
|
||||
} from '../../src/core/analysis-features.js';
|
||||
import { ANALYSIS_FEATURES } from '../../src/core/analysis-feature-registry.js';
|
||||
import { OBJECTIVE_C_PROVIDER_FEATURE } from '../../src/core/ingestion/languages/objective-c/analysis-features.js';
|
||||
import {
|
||||
OBJECTIVE_C_GRAMMAR_PACKAGE,
|
||||
OBJECTIVE_C_GRAMMAR_VERSION,
|
||||
OBJECTIVE_C_PROVIDER_VERSION,
|
||||
} from '../../src/core/ingestion/languages/objective-c/facts.js';
|
||||
|
||||
describe('analysis feature versions', () => {
|
||||
it('separates the global Class schema capability from JVM-only Bean evidence', () => {
|
||||
|
|
@ -88,6 +94,32 @@ describe('analysis feature versions', () => {
|
|||
]);
|
||||
});
|
||||
|
||||
it('stamps Objective-C provider and grammar versions for semantic rebuilds', () => {
|
||||
const expectedId =
|
||||
`objective-c.provider-${OBJECTIVE_C_PROVIDER_VERSION}.` +
|
||||
`${OBJECTIVE_C_GRAMMAR_PACKAGE}-${OBJECTIVE_C_GRAMMAR_VERSION}`;
|
||||
expect(OBJECTIVE_C_PROVIDER_FEATURE.id).toBe(expectedId);
|
||||
|
||||
const objcFeatures = resolveAnalysisFeatureVersions(ANALYSIS_FEATURES, [
|
||||
'Sources/SYModuleCaller.m',
|
||||
'Sources/SYModuleCaller.mm',
|
||||
'Headers/SYModuleCaller.h',
|
||||
]);
|
||||
expect(objcFeatures).toMatchObject({
|
||||
[OBJECTIVE_C_PROVIDER_FEATURE.id]: OBJECTIVE_C_PROVIDER_FEATURE.version,
|
||||
});
|
||||
|
||||
expect(
|
||||
resolveAnalysisFeatureVersions(ANALYSIS_FEATURES, ['include/plain.hpp']),
|
||||
).not.toHaveProperty(OBJECTIVE_C_PROVIDER_FEATURE.id);
|
||||
expect(
|
||||
findAnalysisFeatureMismatches(
|
||||
{ [OBJECTIVE_C_PROVIDER_FEATURE.id]: OBJECTIVE_C_PROVIDER_FEATURE.version - 1 },
|
||||
{ [OBJECTIVE_C_PROVIDER_FEATURE.id]: OBJECTIVE_C_PROVIDER_FEATURE.version },
|
||||
),
|
||||
).toEqual([`version:${OBJECTIVE_C_PROVIDER_FEATURE.id}`]);
|
||||
});
|
||||
|
||||
it('rejects invalid or duplicate descriptors', () => {
|
||||
const invalid: AnalysisFeatureDescriptor = {
|
||||
id: 'invalid',
|
||||
|
|
|
|||
|
|
@ -102,4 +102,25 @@ describe('ensureAndParse', () => {
|
|||
expect(parseSourceSafeSpy).toHaveBeenCalled();
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
it('parses Objective-C .h declarations and method snippets with the objc grammar', async () => {
|
||||
const objcParse = vi.fn().mockReturnValue({ lang: 'objc' });
|
||||
const cppParse = vi.fn().mockReturnValue({ lang: 'cpp' });
|
||||
createParserForLanguage.mockImplementation(async (language: string) => {
|
||||
if (language === 'objective-c') return { parse: objcParse };
|
||||
if (language === 'cpp') return { parse: cppParse };
|
||||
throw new Error(`unexpected language ${language}`);
|
||||
});
|
||||
|
||||
const { ensureAndParse } = await import('../../src/core/embeddings/ast-utils.js');
|
||||
|
||||
await ensureAndParse('@interface Worker\n- (void)run;\n@end\n', 'Worker.h');
|
||||
await ensureAndParse('- (void)run;\n', 'Worker.h');
|
||||
await ensureAndParse('class Widget { int value; };\n', 'widget.h');
|
||||
|
||||
expect(createParserForLanguage).toHaveBeenCalledWith('objective-c', 'Worker.h');
|
||||
expect(createParserForLanguage).toHaveBeenCalledWith('cpp', 'widget.h');
|
||||
expect(objcParse).toHaveBeenCalledTimes(2);
|
||||
expect(cppParse).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -4,22 +4,24 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { characterChunk } from '../../src/core/embeddings/character-chunk.js';
|
||||
|
||||
const { createParserForLanguage } = vi.hoisted(() => ({
|
||||
const { createParserForLanguage, resolveLanguageKey } = vi.hoisted(() => ({
|
||||
createParserForLanguage: vi.fn(),
|
||||
resolveLanguageKey: vi.fn((language: string, filePath?: string) =>
|
||||
language === 'typescript' && filePath?.endsWith('.tsx') ? 'typescript:tsx' : language,
|
||||
),
|
||||
}));
|
||||
|
||||
const { getLanguageFromFilename } = vi.hoisted(() => ({
|
||||
getLanguageFromFilename: vi.fn((filePath: string) =>
|
||||
filePath.endsWith('.rs') ? 'rust' : 'typescript',
|
||||
),
|
||||
getLanguageFromFilename: vi.fn((filePath: string) => {
|
||||
if (filePath.endsWith('.m') || filePath.endsWith('.mm')) return 'objective-c';
|
||||
return filePath.endsWith('.rs') ? 'rust' : 'typescript';
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('../../src/core/tree-sitter/parser-loader.js', () => ({
|
||||
createParserForLanguage,
|
||||
isLanguageAvailable: vi.fn().mockReturnValue(true),
|
||||
resolveLanguageKey: vi.fn((language: string, filePath?: string) =>
|
||||
language === 'typescript' && filePath?.endsWith('.tsx') ? 'typescript:tsx' : language,
|
||||
),
|
||||
resolveLanguageKey,
|
||||
}));
|
||||
|
||||
// Partial mock: `ast-utils` now resolves the LanguageProvider registry to apply
|
||||
|
|
@ -96,7 +98,7 @@ const makeDeclarationTree = (
|
|||
memberTexts: string[],
|
||||
) => {
|
||||
let searchFrom = 0;
|
||||
const memberNodes = memberTexts.map((text, index) => {
|
||||
const memberNodes = memberTexts.map((text) => {
|
||||
const startIndex = content.indexOf(text, searchFrom);
|
||||
if (startIndex < 0) {
|
||||
throw new Error(`Unable to locate member text: ${text}`);
|
||||
|
|
@ -120,6 +122,28 @@ const makeDeclarationTree = (
|
|||
};
|
||||
};
|
||||
|
||||
const makeObjectiveCDeclarationTree = (
|
||||
nodeType: 'protocol_declaration' | 'class_interface',
|
||||
content: string,
|
||||
memberTexts: string[],
|
||||
) => {
|
||||
const headerName = nodeType === 'protocol_declaration' ? 'Worker' : 'Worker (Tracing)';
|
||||
const headerNode = makeFakeNode(
|
||||
'identifier',
|
||||
content.indexOf(headerName),
|
||||
content.indexOf(headerName) + headerName.length,
|
||||
);
|
||||
let searchFrom = 0;
|
||||
const memberNodes = memberTexts.map((text) => {
|
||||
const startIndex = content.indexOf(text, searchFrom);
|
||||
if (startIndex < 0) throw new Error(`Unable to locate member text: ${text}`);
|
||||
searchFrom = startIndex + text.length;
|
||||
return makeFakeNode('method_declaration', startIndex, startIndex + text.length);
|
||||
});
|
||||
const declNode = makeFakeNode(nodeType, 0, content.length, [headerNode, ...memberNodes]);
|
||||
return { rootNode: makeFakeNode('program', 0, content.length, [declNode]) };
|
||||
};
|
||||
|
||||
describe('characterChunk', () => {
|
||||
it('returns single chunk when content fits', () => {
|
||||
const result = characterChunk('short content', 1, 5, 1200, 120);
|
||||
|
|
@ -177,9 +201,14 @@ describe('characterChunk', () => {
|
|||
describe('chunkNode', () => {
|
||||
beforeEach(() => {
|
||||
createParserForLanguage.mockReset();
|
||||
getLanguageFromFilename.mockImplementation((filePath: string) =>
|
||||
filePath.endsWith('.rs') ? 'rust' : 'typescript',
|
||||
resolveLanguageKey.mockReset();
|
||||
resolveLanguageKey.mockImplementation(
|
||||
(language: string, filePath?: string) => `${language}:${filePath ?? ''}`,
|
||||
);
|
||||
getLanguageFromFilename.mockImplementation((filePath: string) => {
|
||||
if (filePath.endsWith('.m') || filePath.endsWith('.mm')) return 'objective-c';
|
||||
return filePath.endsWith('.rs') ? 'rust' : 'typescript';
|
||||
});
|
||||
});
|
||||
|
||||
it('returns single chunk for short content', async () => {
|
||||
|
|
@ -279,6 +308,442 @@ describe('chunkNode', () => {
|
|||
expect(result[0].startLine).toBe(40);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
label: 'Protocol',
|
||||
nodeType: 'protocol_declaration' as const,
|
||||
filePath: 'Worker.m',
|
||||
content: [
|
||||
'@protocol Worker',
|
||||
'- (void)startWithConfiguration:(id)configuration;',
|
||||
'- (void)stopWithCompletion:(id)completion;',
|
||||
'- (void)reloadWithOptions:(id)options;',
|
||||
'@end',
|
||||
].join('\n'),
|
||||
},
|
||||
{
|
||||
label: 'Category',
|
||||
nodeType: 'class_interface' as const,
|
||||
filePath: 'Worker.mm',
|
||||
content: [
|
||||
'@interface Worker (Tracing)',
|
||||
'- (void)startWithConfiguration:(id)configuration;',
|
||||
'- (void)stopWithCompletion:(id)completion;',
|
||||
'- (void)reloadWithOptions:(id)options;',
|
||||
'@end',
|
||||
].join('\n'),
|
||||
},
|
||||
])(
|
||||
'chunks Objective-C $label declarations at member boundaries',
|
||||
async ({ label, nodeType, filePath, content }) => {
|
||||
const members = [
|
||||
'- (void)startWithConfiguration:(id)configuration;',
|
||||
'- (void)stopWithCompletion:(id)completion;',
|
||||
'- (void)reloadWithOptions:(id)options;',
|
||||
];
|
||||
createParserForLanguage.mockResolvedValue({
|
||||
parse: vi.fn().mockReturnValue(makeObjectiveCDeclarationTree(nodeType, content, members)),
|
||||
});
|
||||
|
||||
const result = await chunkNode(label, content, filePath, 1, 5, 90, 0);
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0].text).toContain(members[0]);
|
||||
expect(result.slice(1).every((chunk) => chunk.text.startsWith('- (void)'))).toBe(true);
|
||||
expect(createParserForLanguage).toHaveBeenCalledWith('objective-c', filePath);
|
||||
},
|
||||
);
|
||||
|
||||
it('skips Objective-C parameterized interface arguments as declaration members', async () => {
|
||||
const content = ['@interface Worker <Runnable>', '- (void)run;', '@end'].join('\n');
|
||||
const nameStart = content.indexOf('Worker');
|
||||
const argumentsStart = content.indexOf('<Runnable>');
|
||||
const methodStart = content.indexOf('- (void)run;');
|
||||
const declaration = makeFakeNode('class_interface', 0, content.length, [
|
||||
makeFakeNode('identifier', nameStart, nameStart + 'Worker'.length),
|
||||
makeFakeNode('parameterized_arguments', argumentsStart, argumentsStart + '<Runnable>'.length),
|
||||
makeFakeNode('method_declaration', methodStart, methodStart + '- (void)run;'.length),
|
||||
]);
|
||||
createParserForLanguage.mockResolvedValue({
|
||||
parse: vi.fn().mockReturnValue({
|
||||
rootNode: makeFakeNode('program', 0, content.length, [declaration]),
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await chunkNode('Class', content, 'ParameterizedWorker.m', 1, 3, 40, 0);
|
||||
|
||||
expect(createParserForLanguage).toHaveBeenCalledWith('objective-c', 'ParameterizedWorker.m');
|
||||
expect(result[0].text).toContain('- (void)run');
|
||||
});
|
||||
|
||||
it('skips Objective-C generic argument lists as declaration members', async () => {
|
||||
const content = ['@interface Worker(Tracing)', '- (void)run;', '@end'].join('\n');
|
||||
const nameStart = content.indexOf('Worker');
|
||||
const argumentsStart = content.indexOf('(Tracing)');
|
||||
const methodStart = content.indexOf('- (void)run;');
|
||||
const declaration = makeFakeNode('class_interface', 0, content.length, [
|
||||
makeFakeNode('identifier', nameStart, nameStart + 'Worker'.length),
|
||||
makeFakeNode('generic_arguments', argumentsStart, argumentsStart + '(Tracing)'.length),
|
||||
makeFakeNode('method_declaration', methodStart, methodStart + '- (void)run;'.length),
|
||||
]);
|
||||
createParserForLanguage.mockResolvedValue({
|
||||
parse: vi.fn().mockReturnValue({
|
||||
rootNode: makeFakeNode('program', 0, content.length, [declaration]),
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await chunkNode('Class', content, 'GenericCategoryWorker.m', 1, 3, 40, 0);
|
||||
|
||||
expect(createParserForLanguage).toHaveBeenCalledWith('objective-c', 'GenericCategoryWorker.m');
|
||||
expect(result[0].text).toContain('- (void)run');
|
||||
expect(result.some((chunk) => chunk.text === '(Tracing)')).toBe(false);
|
||||
});
|
||||
|
||||
it('chunks an Objective-C implementation at method boundaries', async () => {
|
||||
const content = [
|
||||
'@implementation Worker',
|
||||
'- (void)first {}',
|
||||
'- (void)second {}',
|
||||
'@end',
|
||||
].join('\n');
|
||||
const firstMethod = '- (void)first {}';
|
||||
const secondMethod = '- (void)second {}';
|
||||
const firstImplementation = makeFakeNode(
|
||||
'implementation_definition',
|
||||
content.indexOf(firstMethod),
|
||||
content.indexOf(firstMethod) + firstMethod.length,
|
||||
[
|
||||
makeFakeNode(
|
||||
'method_definition',
|
||||
content.indexOf(firstMethod),
|
||||
content.indexOf(firstMethod) + firstMethod.length,
|
||||
),
|
||||
],
|
||||
);
|
||||
const secondImplementation = makeFakeNode(
|
||||
'implementation_definition',
|
||||
content.indexOf(secondMethod),
|
||||
content.indexOf(secondMethod) + secondMethod.length,
|
||||
[
|
||||
makeFakeNode(
|
||||
'method_definition',
|
||||
content.indexOf(secondMethod),
|
||||
content.indexOf(secondMethod) + secondMethod.length,
|
||||
),
|
||||
],
|
||||
);
|
||||
const declaration = makeFakeNode('class_implementation', 0, content.indexOf('@end') + 4, [
|
||||
makeFakeNode(
|
||||
'identifier',
|
||||
content.indexOf('Worker'),
|
||||
content.indexOf('Worker') + 'Worker'.length,
|
||||
),
|
||||
firstImplementation,
|
||||
secondImplementation,
|
||||
]);
|
||||
createParserForLanguage.mockResolvedValue({
|
||||
parse: vi.fn().mockReturnValue({
|
||||
rootNode: makeFakeNode('program', 0, content.length, [declaration]),
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await chunkNode('Class', content, 'ImplementationWorker.m', 1, 4, 48, 0);
|
||||
|
||||
expect(createParserForLanguage).toHaveBeenCalledWith('objective-c', 'ImplementationWorker.m');
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0].text).toContain(firstMethod);
|
||||
expect(result[1].text).toContain(secondMethod);
|
||||
expect(result[1].text).not.toContain(firstMethod);
|
||||
});
|
||||
|
||||
it('parses distinct Objective-C sources correctly through one cached parser', async () => {
|
||||
const protocolMethods = [
|
||||
'- (void)startWithConfiguration:(id)configuration;',
|
||||
'- (void)stopWithCompletion:(id)completion;',
|
||||
'- (void)reloadWithOptions:(id)options;',
|
||||
];
|
||||
const categoryMethods = [
|
||||
'- (void)traceStartWithConfiguration:(id)configuration;',
|
||||
'- (void)traceStopWithCompletion:(id)completion;',
|
||||
'- (void)traceReloadWithOptions:(id)options;',
|
||||
];
|
||||
const protocolContent = ['@protocol Worker', ...protocolMethods, '@end'].join('\n');
|
||||
const categoryContent = ['@interface Worker (Tracing)', ...categoryMethods, '@end'].join('\n');
|
||||
const parser = {
|
||||
parse: vi.fn((source: string) => {
|
||||
if (source === protocolContent) {
|
||||
return makeObjectiveCDeclarationTree(
|
||||
'protocol_declaration',
|
||||
protocolContent,
|
||||
protocolMethods,
|
||||
);
|
||||
}
|
||||
if (source === categoryContent) {
|
||||
return makeObjectiveCDeclarationTree('class_interface', categoryContent, categoryMethods);
|
||||
}
|
||||
throw new Error(`Unexpected Objective-C source: ${source}`);
|
||||
}),
|
||||
};
|
||||
createParserForLanguage.mockResolvedValue(parser);
|
||||
|
||||
const protocol = await chunkNode(
|
||||
'Protocol',
|
||||
protocolContent,
|
||||
'CachedObjectiveC.m',
|
||||
1,
|
||||
5,
|
||||
90,
|
||||
0,
|
||||
);
|
||||
const category = await chunkNode(
|
||||
'Category',
|
||||
categoryContent,
|
||||
'CachedObjectiveC.m',
|
||||
1,
|
||||
5,
|
||||
90,
|
||||
0,
|
||||
);
|
||||
|
||||
expect(createParserForLanguage).toHaveBeenCalledTimes(1);
|
||||
expect(protocol.map((chunk) => chunk.text).join('\n')).toContain(protocolMethods[2]);
|
||||
expect(category.map((chunk) => chunk.text).join('\n')).toContain(categoryMethods[2]);
|
||||
expect(parser.parse.mock.calls.map(([source]) => source)).toEqual([
|
||||
protocolContent,
|
||||
categoryContent,
|
||||
]);
|
||||
});
|
||||
|
||||
it('expands Objective-C protocol optional and required sections', async () => {
|
||||
const content = [
|
||||
'@protocol P',
|
||||
'@optional',
|
||||
'- (void)first;',
|
||||
'- (void)second;',
|
||||
'@required',
|
||||
'- (void)third;',
|
||||
'- (void)fourth;',
|
||||
'@end',
|
||||
].join('\n');
|
||||
const members = ['- (void)first;', '- (void)second;', '- (void)third;', '- (void)fourth;'];
|
||||
let searchFrom = 0;
|
||||
const methodNodes = members.map((text) => {
|
||||
const startIndex = content.indexOf(text, searchFrom);
|
||||
searchFrom = startIndex + text.length;
|
||||
return makeFakeNode('method_declaration', startIndex, startIndex + text.length);
|
||||
});
|
||||
const optionalStart = content.indexOf('@optional');
|
||||
const requiredStart = content.indexOf('@required');
|
||||
const optional = makeFakeNode(
|
||||
'qualified_protocol_interface_declaration',
|
||||
optionalStart,
|
||||
methodNodes[1].endIndex,
|
||||
methodNodes.slice(0, 2),
|
||||
);
|
||||
const required = makeFakeNode(
|
||||
'qualified_protocol_interface_declaration',
|
||||
requiredStart,
|
||||
methodNodes[3].endIndex,
|
||||
methodNodes.slice(2),
|
||||
);
|
||||
const header = makeFakeNode('identifier', content.indexOf('P'), content.indexOf('P') + 1);
|
||||
const declaration = makeFakeNode('protocol_declaration', 0, content.length, [
|
||||
header,
|
||||
optional,
|
||||
required,
|
||||
]);
|
||||
createParserForLanguage.mockResolvedValue({
|
||||
parse: vi.fn().mockReturnValue({
|
||||
rootNode: makeFakeNode('program', 0, content.length, [declaration]),
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await chunkNode('Protocol', content, 'ProtocolSections.m', 1, 8, 36, 0);
|
||||
const combined = result.map((chunk) => chunk.text).join('\n');
|
||||
const requiredChunk = result.find((chunk) => chunk.text.includes(members[2]));
|
||||
|
||||
expect(result.length).toBeGreaterThan(1);
|
||||
for (const member of members) expect(combined).toContain(member);
|
||||
expect(
|
||||
result.some((chunk) => chunk.text.includes(members[0]) && chunk.text.includes(members[1])),
|
||||
).toBe(false);
|
||||
expect(requiredChunk?.text).toContain('@required');
|
||||
expect(requiredChunk?.text).not.toContain(members[1]);
|
||||
expect(createParserForLanguage).toHaveBeenCalledWith('objective-c', 'ProtocolSections.m');
|
||||
});
|
||||
|
||||
it('expands Objective-C instance variables before chunking a class declaration', async () => {
|
||||
const content = [
|
||||
'@interface Worker {',
|
||||
' id _first;',
|
||||
' id _second;',
|
||||
'}',
|
||||
'- (void)run;',
|
||||
'@end',
|
||||
].join('\n');
|
||||
const firstIvar = 'id _first;';
|
||||
const secondIvar = 'id _second;';
|
||||
const method = '- (void)run;';
|
||||
const firstIvarStart = content.indexOf(firstIvar);
|
||||
const secondIvarStart = content.indexOf(secondIvar);
|
||||
const methodStart = content.indexOf(method);
|
||||
const instanceVariables = makeFakeNode(
|
||||
'instance_variables',
|
||||
content.indexOf('{'),
|
||||
content.indexOf('}') + 1,
|
||||
[
|
||||
makeFakeNode('instance_variable', firstIvarStart, firstIvarStart + firstIvar.length),
|
||||
makeFakeNode('instance_variable', secondIvarStart, secondIvarStart + secondIvar.length),
|
||||
],
|
||||
);
|
||||
const declaration = makeFakeNode('class_interface', 0, content.length, [
|
||||
makeFakeNode(
|
||||
'identifier',
|
||||
content.indexOf('Worker'),
|
||||
content.indexOf('Worker') + 'Worker'.length,
|
||||
),
|
||||
instanceVariables,
|
||||
makeFakeNode('method_declaration', methodStart, methodStart + method.length),
|
||||
]);
|
||||
createParserForLanguage.mockResolvedValue({
|
||||
parse: vi.fn().mockReturnValue({
|
||||
rootNode: makeFakeNode('program', 0, content.length, [declaration]),
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await chunkNode('Class', content, 'Worker.m', 1, 6, 48, 0);
|
||||
const combined = result.map((chunk) => chunk.text).join('\n');
|
||||
|
||||
expect(result.length).toBeGreaterThan(1);
|
||||
expect(combined).toContain(firstIvar);
|
||||
expect(combined).toContain(secondIvar);
|
||||
expect(combined).toContain(method);
|
||||
expect(combined).toContain('}');
|
||||
expect(
|
||||
result.some((chunk) => chunk.text.includes(firstIvar) && chunk.text.includes(secondIvar)),
|
||||
).toBe(true);
|
||||
expect(
|
||||
result.some((chunk) => chunk.text.includes(secondIvar) && chunk.text.includes('}')),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('keeps ivar attributes attached to the following instance variable', async () => {
|
||||
const extraMethods = ['- (void)alpha;', '- (void)bravo;', '- (void)charlie;'];
|
||||
const content = [
|
||||
'@interface Worker {',
|
||||
' __attribute__((unused)) id _first;',
|
||||
'}',
|
||||
...extraMethods,
|
||||
'@end',
|
||||
].join('\n');
|
||||
const attribute = '__attribute__((unused))';
|
||||
const firstIvar = 'id _first;';
|
||||
const attrStart = content.indexOf(attribute);
|
||||
const firstIvarStart = content.indexOf(firstIvar);
|
||||
const instanceVariables = makeFakeNode(
|
||||
'instance_variables',
|
||||
content.indexOf('{'),
|
||||
content.indexOf('}') + 1,
|
||||
[
|
||||
makeFakeNode('attribute_specifier', attrStart, attrStart + attribute.length),
|
||||
makeFakeNode('instance_variable', firstIvarStart, firstIvarStart + firstIvar.length),
|
||||
],
|
||||
);
|
||||
const methodNodes = extraMethods.map((method) => {
|
||||
const methodStart = content.indexOf(method);
|
||||
return makeFakeNode('method_declaration', methodStart, methodStart + method.length);
|
||||
});
|
||||
const declaration = makeFakeNode('class_interface', 0, content.length, [
|
||||
makeFakeNode(
|
||||
'identifier',
|
||||
content.indexOf('Worker'),
|
||||
content.indexOf('Worker') + 'Worker'.length,
|
||||
),
|
||||
instanceVariables,
|
||||
...methodNodes,
|
||||
]);
|
||||
createParserForLanguage.mockResolvedValue({
|
||||
parse: vi.fn().mockReturnValue({
|
||||
rootNode: makeFakeNode('program', 0, content.length, [declaration]),
|
||||
}),
|
||||
});
|
||||
|
||||
// First-chunk prefix starts at `@interface`, so size must cover that
|
||||
// prefix plus the ivar unit. Smaller sizes fall into characterChunk and
|
||||
// can split the attribute token itself.
|
||||
const chunkSize = content.indexOf('}') + 2;
|
||||
const result = await chunkNode(
|
||||
'Class',
|
||||
content,
|
||||
'WorkerAttr.m',
|
||||
1,
|
||||
extraMethods.length + 4,
|
||||
chunkSize,
|
||||
0,
|
||||
);
|
||||
|
||||
expect(content.length).toBeGreaterThan(chunkSize);
|
||||
expect(
|
||||
result.some((chunk) => chunk.text.includes(attribute) && !chunk.text.includes(firstIvar)),
|
||||
).toBe(false);
|
||||
expect(
|
||||
result.some((chunk) => chunk.text.includes(attribute) && chunk.text.includes(firstIvar)),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('keeps Objective-C declaration modifiers in the class prefix', async () => {
|
||||
const content = ['NS_ROOT_CLASS @interface Worker', '- (void)run;', '@end'].join('\n');
|
||||
const modifier = 'NS_ROOT_CLASS';
|
||||
const method = '- (void)run;';
|
||||
const methodStart = content.indexOf(method);
|
||||
const declaration = makeFakeNode('class_interface', 0, content.length, [
|
||||
makeFakeNode('storage_class_specifier', 0, modifier.length),
|
||||
makeFakeNode(
|
||||
'identifier',
|
||||
content.indexOf('Worker'),
|
||||
content.indexOf('Worker') + 'Worker'.length,
|
||||
),
|
||||
makeFakeNode('method_declaration', methodStart, methodStart + method.length),
|
||||
]);
|
||||
createParserForLanguage.mockResolvedValue({
|
||||
parse: vi.fn().mockReturnValue({
|
||||
rootNode: makeFakeNode('program', 0, content.length, [declaration]),
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await chunkNode('Class', content, 'RootClass.m', 1, 3, 80, 0);
|
||||
|
||||
expect(result[0].text).toContain(method);
|
||||
expect(result.some((chunk) => chunk.text.trim() === modifier)).toBe(false);
|
||||
});
|
||||
|
||||
it('keeps Objective-C protocol inheritance in the declaration prefix', async () => {
|
||||
const content = ['@protocol Worker <Runnable, Observable>', '- (void)run;', '@end'].join('\n');
|
||||
const protocolNameStart = content.indexOf('Worker');
|
||||
const inheritanceStart = content.indexOf('<Runnable, Observable>');
|
||||
const methodStart = content.indexOf('- (void)run;');
|
||||
const declaration = makeFakeNode('protocol_declaration', 0, content.length, [
|
||||
makeFakeNode('identifier', protocolNameStart, protocolNameStart + 'Worker'.length),
|
||||
makeFakeNode(
|
||||
'protocol_reference_list',
|
||||
inheritanceStart,
|
||||
inheritanceStart + '<Runnable, Observable>'.length,
|
||||
),
|
||||
makeFakeNode('method_declaration', methodStart, methodStart + '- (void)run;'.length),
|
||||
]);
|
||||
createParserForLanguage.mockResolvedValue({
|
||||
parse: vi.fn().mockReturnValue({
|
||||
rootNode: makeFakeNode('program', 0, content.length, [declaration]),
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await chunkNode('Protocol', content, 'ProtocolInheritance.m', 1, 3, 50, 0);
|
||||
|
||||
expect(createParserForLanguage).toHaveBeenCalledWith('objective-c', 'ProtocolInheritance.m');
|
||||
expect(result[0].text).toContain('- (void)');
|
||||
expect(result[0].text).not.toBe('@protocol Worker <Runnable, Observable>');
|
||||
});
|
||||
|
||||
it('splits a function into multiple AST-aware chunks using snippet offsets', async () => {
|
||||
const content = [
|
||||
'function example() {',
|
||||
|
|
|
|||
70
gitnexus/test/unit/content-language-classification.test.ts
Normal file
70
gitnexus/test/unit/content-language-classification.test.ts
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import fs from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { SupportedLanguages } from 'gitnexus-shared';
|
||||
import { classifyContentLanguages } from '../../src/core/ingestion/content-language-classification.js';
|
||||
import { getLanguageForFileContent } from '../../src/core/ingestion/languages/index.js';
|
||||
|
||||
describe('content language classification', () => {
|
||||
let repoDir = '';
|
||||
|
||||
afterEach(async () => {
|
||||
if (repoDir) await fs.rm(repoDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('retains only reusable language decisions and skips unreadable files', async () => {
|
||||
repoDir = await fs.mkdtemp(path.join(os.tmpdir(), 'content-language-classification-'));
|
||||
await fs.writeFile(
|
||||
path.join(repoDir, 'ObjectiveC.h'),
|
||||
'@interface ObjectiveC : NSObject\n@end\n',
|
||||
);
|
||||
await fs.writeFile(
|
||||
path.join(repoDir, 'CoreFoundation.h'),
|
||||
'#import <CoreFoundation/CoreFoundation.h>\nclass NativeHeader {};\n',
|
||||
);
|
||||
|
||||
const classifications = await classifyContentLanguages(repoDir, [
|
||||
'ObjectiveC.h',
|
||||
'CoreFoundation.h',
|
||||
'missing.h',
|
||||
]);
|
||||
|
||||
expect([...classifications.entries()]).toEqual([
|
||||
['ObjectiveC.h', SupportedLanguages.ObjectiveC],
|
||||
['CoreFoundation.h', SupportedLanguages.CPlusPlus],
|
||||
]);
|
||||
expect(classifications.has('missing.h')).toBe(false);
|
||||
expect(classifications.get('ObjectiveC.h')).not.toContain('@interface');
|
||||
});
|
||||
|
||||
it('keeps TypeScript module extensions in the provider fallback map', () => {
|
||||
expect(getLanguageForFileContent('service.mts', 'export class EsmService {}')).toBe(
|
||||
SupportedLanguages.TypeScript,
|
||||
);
|
||||
expect(getLanguageForFileContent('service.cts', 'export class CjsService {}')).toBe(
|
||||
SupportedLanguages.TypeScript,
|
||||
);
|
||||
});
|
||||
|
||||
it('parses Objective-C header snippets with the Objective-C language', () => {
|
||||
expect(
|
||||
getLanguageForFileContent('Worker.h', '@protocol Worker <NSObject>\n- (void)run;\n@end\n'),
|
||||
).toBe(SupportedLanguages.ObjectiveC);
|
||||
expect(getLanguageForFileContent('Worker.h', '- (void)run;\n')).toBe(
|
||||
SupportedLanguages.ObjectiveC,
|
||||
);
|
||||
expect(getLanguageForFileContent('widget.h', 'class Widget { int value; };\n')).toBe(
|
||||
SupportedLanguages.CPlusPlus,
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps extensionless Ruby filenames when content classification misses', () => {
|
||||
expect(getLanguageForFileContent('Rakefile', 'task :default do\nend\n')).toBe(
|
||||
SupportedLanguages.Ruby,
|
||||
);
|
||||
expect(getLanguageForFileContent('Gemfile', 'source "https://rubygems.org"\n')).toBe(
|
||||
SupportedLanguages.Ruby,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -8,8 +8,20 @@ import {
|
|||
pageSizeDoctorLines,
|
||||
poolSizeDoctorLine,
|
||||
} from '../../src/cli/doctor.js';
|
||||
import { setCliLanguage, type SupportedCliLanguage } from '../../src/cli/i18n/index.js';
|
||||
import type { NativeCheckResult } from '../../src/core/lbug/native-check.js';
|
||||
|
||||
const nativeProbeState = vi.hoisted(() => ({ vectorLoaded: true }));
|
||||
|
||||
vi.mock('../../src/core/lbug/native-check.js', () => ({
|
||||
checkLbugNative: () => ({ ok: true, binaryPath: '/synthetic/lbugjs.node' }),
|
||||
probeFtsExtensionLoad: async () => ({ loaded: true }),
|
||||
probeVectorExtensionLoad: async () =>
|
||||
nativeProbeState.vectorLoaded
|
||||
? { loaded: true }
|
||||
: { loaded: false, reason: 'synthetic VECTOR load failure' },
|
||||
}));
|
||||
|
||||
describe('doctor output formatting', () => {
|
||||
it('keeps ASCII padding equivalent to String.padEnd', () => {
|
||||
expect(displayWidth('OS:')).toBe(3);
|
||||
|
|
@ -29,6 +41,67 @@ describe('doctor output formatting', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('doctor VECTOR capability claims', () => {
|
||||
const ENV_KEYS = [
|
||||
'GITNEXUS_EMBEDDING_URL',
|
||||
'GITNEXUS_EMBEDDING_MODEL',
|
||||
'GITNEXUS_EMBEDDING_DIMS',
|
||||
] as const;
|
||||
const savedEnv = Object.fromEntries(ENV_KEYS.map((key) => [key, process.env[key]]));
|
||||
|
||||
const renderDoctor = async (
|
||||
vectorLoaded: boolean,
|
||||
language: SupportedCliLanguage = 'en',
|
||||
): Promise<string> => {
|
||||
nativeProbeState.vectorLoaded = vectorLoaded;
|
||||
setCliLanguage(language);
|
||||
process.env.GITNEXUS_EMBEDDING_URL = 'http://127.0.0.1:9/v1';
|
||||
process.env.GITNEXUS_EMBEDDING_MODEL = 'synthetic-doctor';
|
||||
process.env.GITNEXUS_EMBEDDING_DIMS = '384';
|
||||
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
||||
|
||||
await doctorCommand();
|
||||
|
||||
return log.mock.calls.map((args) => args.map(String).join(' ')).join('\n');
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
nativeProbeState.vectorLoaded = true;
|
||||
setCliLanguage(null);
|
||||
vi.restoreAllMocks();
|
||||
for (const key of ENV_KEYS) {
|
||||
if (savedEnv[key] === undefined) delete process.env[key];
|
||||
else process.env[key] = savedEnv[key];
|
||||
}
|
||||
});
|
||||
|
||||
it('reports a loaded extension without claiming a repository index exists', async () => {
|
||||
const output = await renderDoctor(true);
|
||||
|
||||
expect(output).toContain('VECTOR extension: available');
|
||||
expect(output).toContain(
|
||||
'Semantic support: vector-index capable (repository index not checked)',
|
||||
);
|
||||
expect(output).not.toContain('VECTOR index:');
|
||||
expect(output).not.toContain('Semantic mode:');
|
||||
});
|
||||
|
||||
it('reports exact-scan capability when the extension cannot load', async () => {
|
||||
const output = await renderDoctor(false);
|
||||
|
||||
expect(output).toContain('VECTOR extension: unavailable');
|
||||
expect(output).toContain('Semantic support: exact-scan only (VECTOR extension unavailable)');
|
||||
});
|
||||
|
||||
it('keeps the corrected capability claims localized', async () => {
|
||||
const output = await renderDoctor(true, 'zh-CN');
|
||||
|
||||
expect(output).toContain('VECTOR 扩展:');
|
||||
expect(output).toContain('语义支持:');
|
||||
expect(output).toContain('支持向量索引(未检查仓库索引)');
|
||||
});
|
||||
});
|
||||
|
||||
describe('doctor embedding-runtime support status', () => {
|
||||
it('flags local embeddings as unavailable on macOS Intel (darwin/x64)', () => {
|
||||
const { status, detail } = localEmbeddingDoctorStatus({
|
||||
|
|
|
|||
|
|
@ -7,12 +7,33 @@ import {
|
|||
} from '../../src/core/embeddings/embedding-pipeline.js';
|
||||
import { generateEmbeddingText } from '../../src/core/embeddings/text-generator.js';
|
||||
import type { EmbeddableNode, EmbeddingProgress } from '../../src/core/embeddings/types.js';
|
||||
import { DEFAULT_EMBEDDING_CONFIG, EMBEDDABLE_LABELS } from '../../src/core/embeddings/types.js';
|
||||
import {
|
||||
DEFAULT_EMBEDDING_CONFIG,
|
||||
EMBEDDABLE_LABELS,
|
||||
LABEL_CATEGORY,
|
||||
LABEL_PROTOCOL,
|
||||
LABELS_WITH_EXPORTED,
|
||||
STRUCTURAL_LABELS,
|
||||
} from '../../src/core/embeddings/types.js';
|
||||
import { STALE_HASH_SENTINEL } from '../../src/core/lbug/schema.js';
|
||||
|
||||
const CLASS_CHUNK_SIZE = 90;
|
||||
const CLASS_OVERLAP = 10;
|
||||
|
||||
describe('embedding schema column contracts', () => {
|
||||
it('does not query Objective-C protocol/category tables for an isExported column', () => {
|
||||
expect(LABELS_WITH_EXPORTED.has(LABEL_PROTOCOL)).toBe(false);
|
||||
expect(LABELS_WITH_EXPORTED.has(LABEL_CATEGORY)).toBe(false);
|
||||
});
|
||||
|
||||
it('keeps Objective-C protocol/category declaration chunking without unsupported structural extraction', () => {
|
||||
expect(STRUCTURAL_LABELS.has(LABEL_PROTOCOL)).toBe(false);
|
||||
expect(STRUCTURAL_LABELS.has(LABEL_CATEGORY)).toBe(false);
|
||||
expect(EMBEDDABLE_LABELS).toContain(LABEL_PROTOCOL);
|
||||
expect(EMBEDDABLE_LABELS).toContain(LABEL_CATEGORY);
|
||||
});
|
||||
});
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// resolveEmbeddingInstallPolicy (offline-first, #1153)
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
|
@ -134,7 +155,7 @@ describe('contentHashForNode', () => {
|
|||
});
|
||||
|
||||
it('exports a text template version marker', () => {
|
||||
expect(EMBEDDING_TEXT_VERSION).toBe('v4');
|
||||
expect(EMBEDDING_TEXT_VERSION).toBe('v5');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue