Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/codegraph-cli/tests/http_serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ fn serve_http_global_without_projectpath_returns_actionable_error() {
resp.contains("No indexed project resolved"),
"global mode without projectPath must return the actionable resolve error: {resp}"
);
assert!(
resp.contains("offer to run `codegraph init .`")
&& resp.contains("do not run it without the user's consent"),
"the error offers init and asks for the user's consent (#269): {resp}"
);
}

/// Spawn `serve --http --path <indexed>` with `CODEGRAPH_DEBUG` set to `debug`
Expand Down
38 changes: 37 additions & 1 deletion crates/codegraph-mcp/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
//! can never drift from what the binary actually parses. Upstream hardcodes a
//! "30+" prose count; a copied number would silently go stale the next time a
//! grammar lands.
//!
//! The Limitations bullet for a project without an index is this repository's
//! own wording as well (sunerpy/codegraph-rust#269): the agent tells the user,
//! offers `codegraph init .`, and runs it only with the user's consent. Upstream
//! (`461a5d87`) instead tells the agent to stop calling codegraph tools for that
//! project and never to run `codegraph init` itself.

use codegraph_core::types::Language;
use std::sync::OnceLock;
Expand Down Expand Up @@ -107,7 +113,7 @@ typically one to a few calls; a grep/read exploration is dozens.

## Limitations

- If a tool reports the project isn't initialized, `.codegraph/` doesn't exist yet — offer to run `codegraph init .` to build the index.
- If a tool reports the project isn't initialized (no `.codegraph/` directory), tell the user the project has no CodeGraph index yet, offer to run `codegraph init .` to build it, and do not run it without the user's consent.
- Index lags file writes by ~1 second. A burst of many changes (a branch switch, a mass rename) or a sign that the OS dropped file events makes the watcher re-scan the whole project on its own, and responses can start with "⚠️ CodeGraph auto-sync is RECOVERING" until that catch-up commits. Renames never need a rebuild: `codegraph sync` reconciles the whole project when no watcher is running.
- Cross-file resolution is best-effort name matching; ambiguous calls may return multiple candidates.
- Explore matches names and indexed code words lexically, not by meaning; an empty result reports word matches and may suggest indexed candidate names to retry with `codegraph_explore`.
Expand Down Expand Up @@ -196,4 +202,34 @@ mod tests {
"single trailing newline"
);
}

/// sunerpy/codegraph-rust#269: when a tool reports a project without an
/// index, the agent tells the user and offers `codegraph init .`, and does
/// not run it without the user's consent.
#[test]
fn instructions_offer_init_only_with_the_users_consent() {
let text = server_instructions();
let bullet = text
.lines()
.find(|line| line.contains("isn't initialized"))
.expect("the Limitations section covers a project without an index");
assert!(
bullet.contains("tell the user the project has no CodeGraph index yet"),
"{bullet}"
);
assert!(
bullet.contains("offer to run `codegraph init .`"),
"{bullet}"
);
assert!(
bullet.contains("do not run it without the user's consent"),
"{bullet}"
);
assert!(
!text
.replace("offer to run `codegraph init .`", "")
.contains("codegraph init"),
"every mention of `codegraph init` must be an offer"
);
}
}
5 changes: 5 additions & 0 deletions crates/codegraph-mcp/src/rmcp_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,11 @@ mod handler_tests {
text.contains("No indexed project found for projectPath"),
"bad-projectPath message, got: {text}"
);
assert!(
text.contains("offer to run `codegraph init .`")
&& text.contains("do not run it without the user's consent"),
"the error offers init and asks for the user's consent (#269), got: {text}"
);
let _ = client.cancel().await;
let _ = task.await;
});
Expand Down
65 changes: 63 additions & 2 deletions crates/codegraph-mcp/src/roots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ pub(crate) fn format_subproject_candidates(base: Option<&Path>, roots: &[PathBuf
display.join(", ")
}

/// What a tool response says about a project that may have no index yet:
/// creating one is the user's decision, so the agent offers `codegraph init .`
/// and never runs it on its own (sunerpy/codegraph-rust#269).
const ASK_BEFORE_INIT: &str = "If the project has no CodeGraph index yet, tell the user so, offer to run `codegraph init .` in the project directory, and do not run it without the user's consent.";

pub(crate) fn not_indexed_message(
raw_project: Option<&str>,
search_from: Option<&Path>,
Expand All @@ -491,11 +496,13 @@ pub(crate) fn not_indexed_message(
) -> String {
if let Some(raw) = raw_project {
return format!(
"No indexed project found for projectPath {raw:?}. Pass an absolute path to an indexed project, or run `codegraph init` there."
"No indexed project found for projectPath {raw:?}. Pass an absolute path to an indexed project. {ASK_BEFORE_INIT}"
);
}
let mut message = if candidates.is_empty() {
"No indexed project resolved. Pass a `projectPath` argument, run `codegraph init` in the project, or start the server with `--path <project>`.".to_string()
format!(
"No indexed project resolved. Pass a `projectPath` argument, or start the server with `--path <project>`. {ASK_BEFORE_INIT}"
)
} else {
let listed = format_subproject_candidates(search_from, candidates);
format!(
Expand Down Expand Up @@ -1645,6 +1652,60 @@ mod tests {
);
}

/// sunerpy/codegraph-rust#269: creating an index is the user's decision.
/// A message that mentions `codegraph init` only offers it, and tells the
/// agent to say the project has no index and to wait for the user's
/// consent; the rest of each message stays as it was.
#[test]
fn not_indexed_messages_offer_init_only_with_the_users_consent() {
let explicit = not_indexed_message(Some("/no/such/project"), None, &[], false);
let unresolved = not_indexed_message(None, None, &[], false);
let truncated = not_indexed_message(None, None, &[], true);
for message in [&explicit, &unresolved, &truncated] {
assert!(
message.contains("has no CodeGraph index yet, tell the user"),
"{message}"
);
assert!(
message.contains("offer to run `codegraph init .`"),
"{message}"
);
assert!(
message.contains("do not run it without the user's consent"),
"{message}"
);
assert!(
!message
.replace("offer to run `codegraph init .`", "")
.contains("codegraph init"),
"every mention of `codegraph init` must be an offer: {message}"
);
}
assert!(
explicit.starts_with("No indexed project found for projectPath \"/no/such/project\"."),
"{explicit}"
);
assert!(
explicit.contains("Pass an absolute path to an indexed project"),
"{explicit}"
);
assert!(
unresolved.starts_with("No indexed project resolved."),
"{unresolved}"
);
assert!(
unresolved.contains("`projectPath`") && unresolved.contains("`--path <project>`"),
"{unresolved}"
);
assert!(truncated.contains("stopped after"), "{truncated}");

// With indexed sub-projects to choose from, the message names them and
// does not bring up `codegraph init` at all.
let base = Path::new("/workspace");
let listed = not_indexed_message(None, Some(base), &[base.join("service-a")], false);
assert!(!listed.contains("codegraph init"), "{listed}");
}

#[test]
fn subproject_scan_caps_candidates_at_sixty_four() {
let workspace = unindexed_dir("subscan-cap");
Expand Down
10 changes: 10 additions & 0 deletions crates/codegraph-mcp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,11 @@ mod tests {
text.contains("No indexed project resolved"),
"unresolved (raw=None) message, got: {text}"
);
assert!(
text.contains("offer to run `codegraph init .`")
&& text.contains("do not run it without the user's consent"),
"the guidance offers init and asks for the user's consent (#269), got: {text}"
);
}

#[test]
Expand All @@ -1352,6 +1357,11 @@ mod tests {
text.contains("No indexed project found for projectPath"),
"raw-project message, got: {text}"
);
assert!(
text.contains("offer to run `codegraph init .`")
&& text.contains("do not run it without the user's consent"),
"the error offers init and asks for the user's consent (#269), got: {text}"
);
}

#[test]
Expand Down
10 changes: 10 additions & 0 deletions crates/codegraph-mcp/tests/rmcp_roots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ fn rmcp_does_not_adopt_unindexed_client_root() {
text.contains("No indexed project"),
"unindexed client root must NOT be adopted; expected no-project guidance, got: {text}"
);
assert!(
text.contains("offer to run `codegraph init .`")
&& text.contains("do not run it without the user's consent"),
"the guidance offers init and asks for the user's consent (#269), got: {text}"
);

let _ = client.cancel().await;
let _ = server_task.await;
Expand Down Expand Up @@ -299,6 +304,11 @@ fn rmcp_http_no_roots_never_adopts() {
text.contains("No indexed project"),
"http no_roots mode must NOT adopt the client's indexed root: {text}"
);
assert!(
text.contains("offer to run `codegraph init .`")
&& text.contains("do not run it without the user's consent"),
"the HTTP guidance offers init and asks for the user's consent (#269), got: {text}"
);

let _ = client.cancel().await;
let _ = server_task.await;
Expand Down
8 changes: 8 additions & 0 deletions docs/equivalence.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ The golden changes of the v0.56.0 follow-up program, in commit order.
| `feat(resolve): Erlang behaviours, macros, escripts and application resource files` | erlang | `edges.json`, `colby.db` | a macro use links its `-define` (upstream #1168): `m.erl`'s `g/0` gains `references` to the constant `m::X` for `?X` (16:20, `exact-match` 0.9); the corpus has no `-behaviour`, variable-module call, escript or application resource file, so the `implements` rule, the `erlang-behaviour` pass and the resource-tuple references move nothing (`bench --graph-diff` of the committed and regenerated goldens: 1 added row, 0 removed; every other corpus is identical under `--check`) | `scripts/regen-goldens.sh --write erlang` |
| `feat(explore): gather the subgraph the way upstream walks it` | mcp | `codegraph_explore.json` | explore's search takes upstream's high-value kinds only (`findRelevantContext`'s default node kinds), so on the mini fixture the import `./math` and the property `value` no longer seed or join the gathered subgraph: the text's `Found 7 symbols across 2 files.` becomes `Found 5 symbols across 2 files.` and the `src/math.ts` header loses `value(property)`; no extraction golden moves | re-captured from the Rust output (OD-4) |

#### v0.56.1 regeneration log

The golden changes of the v0.56.1 maintenance patch, in commit order.

| Commit | Corpus | Artifacts | Intent | Command |
| ------------------------------------------------------------------------ | ------ | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `fix(mcp): ask the user before initializing a project that has no index` | mcp | `initialize.json` | when a tool reports the project isn't initialized, the server instructions now have the agent tell the user the project has no CodeGraph index yet, offer to run `codegraph init .`, and not run it without the user's consent; before, they only said to offer it (sunerpy/codegraph-rust#269); the fixture's `instructions` string changes in that one bullet, the rest of the fixture is unchanged, and no graph artifact moves | edited with `crates/codegraph-mcp/src/instructions.rs`; `cargo test -p codegraph-mcp --locked --test golden_mcp --test rmcp_parity` requires the instructions byte-identical |

The canonicalizer strips inherently unstable timestamp columns
(`nodes.updated_at`, `files.modified_at`, `files.indexed_at`), parses JSON text
columns before re-serializing them with deterministic key order, asserts all
Expand Down
7 changes: 7 additions & 0 deletions docs/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,13 @@ probe runs again on relevant requests, while the downward scan is throttled to
once per five seconds so a project initialized after server startup can be
adopted without walking the workspace on every call.

The no-default text and the explicit-path error never tell the agent to run
`codegraph init` on its own. Where one mentions it, it says that if the project
has no CodeGraph index yet, the agent should tell the user so, offer to run
`codegraph init .` in the project directory, and not run it without the user's
consent. The server instructions in the `initialize` response give the same
rule.

For an explicit path that **does** resolve to an existing index, the first call
is also the live-service boundary: it waits for catch-up and retains that
project's shared daemon/watcher as described above. Query routing therefore does
Expand Down
Loading
Loading