Compare commits

...
27 Commits
Author SHA1 Message Date
Riz Ashraf 36b86e5b92 chore: remove unused redb::ReadableTable import in db.rs 2026-10-09 09:43:02 +01:00
Riz Ashraf 0c2fc96ff8 chore: housekeeping 2026-10-09 09:33:54 +01:00
Riz Ashraf 4ea8f861cc refactor(nvim-core): map requests directly to mega-tools and remove legacy routing 2026-10-09 06:43:05 +01:00
Riz Ashraf 7fed3a2e77 docs(nvim-core): prune obsolete tool names from configuration and docs 2026-10-09 06:16:29 +01:00
Riz Ashraf bce9b82b66 feat(nvim-core): consolidate nvim tools into 5 mega-tools 2026-10-09 06:08:08 +01:00
Riz Ashraf 03f8786d24 chore: resolve clippy warnings for pre-push audit 2026-10-09 01:06:30 +01:00
Riz Ashraf bd20482cea fix(embedding): resolve large_enum_variant clippy error 2026-10-09 00:56:47 +01:00
Riz Ashraf 1ed75ca156 feat(nvim): add nvim_lsp, nvim_ast, buffer undo/redo 2026-10-09 00:51:05 +01:00
Riz Ashraf 3765e6b05d chore: fix clippy warnings and run rustfmt 2026-10-09 00:39:50 +01:00
Riz Ashraf fecd8bf33f fix(tests): replace serde_json::from_str with serde_yaml::from_str for tool output assertions 2026-10-09 00:30:05 +01:00
Riz Ashraf b4431a4dcd feat(nvim): update nvim_buffer edit to handle multiple block edits 2026-10-09 00:18:01 +01:00
Riz Ashraf d6acd885dd fix(nvim): eliminate \r corruption in buffer mutations
- Pre-process and strip carriage returns natively in Rust before RPC serialization.

- Pass replacement strings to Neovim as native MsgPack arrays to bypass Lua vim.split overhead.

- Directly feed table arrays into vim.api.nvim_buf_set_lines for exact buffer matches.
2026-10-08 23:58:49 +01:00
Riz Ashraf cb0c55775a feat(mcp): switch to YAML serialization for tool outputs
- Replaced serde_json with serde_yaml in all handler endpoints.

- This drastically reduces token consumption for large list/graph responses by removing JSON braces and quotes.
2026-10-08 23:46:32 +01:00
Riz Ashraf 410b0c42ca feat(ollama): enforce JSON logits & add nvim_buffer edit action
- Added format parameter to Ollama generate for JSON logit enforcement.

- Implemented safe edit action in nvim_buffer MCP tool to replace raw Lua.
2026-10-08 23:41:57 +01:00
Riz Ashraf 003b3cb2bf docs: strictly enforce agnostic Neovim rules and ban invisible shell edits 2026-10-08 23:29:44 +01:00
Riz Ashraf 5485b40d14 feat(nvim-core): make nvim_execute_lua read-only to enforce specialized tool usage 2026-10-08 23:19:47 +01:00
Riz Ashraf 9d9e959744 chore: fix formatting and clippy lints 2026-10-08 23:01:38 +01:00
Riz Ashraf 8952bd5399 feat(ollama): keep models in VRAM for 1h to prevent cold starts 2026-10-08 22:57:02 +01:00
Riz Ashraf 4005f566cf Expand Ollama context windows (num_ctx) and output limits 2026-10-08 22:53:24 +01:00
Riz Ashraf 7eae2fa0aa Update ocrs models to S3 endpoints and add download error logging 2026-10-08 22:38:38 +01:00
Riz Ashraf 5c5aead1a5 Replace Ollama/Windows OCR with ocrs, add clipboard image deduplication 2026-10-08 21:48:12 +01:00
Reazul Ashraf 548f0a06ea Fix type inference for non-windows target 2026-10-08 13:43:43 +01:00
Riz Ashraf 8c6bf05269 feat: implement native UDP ping/pong health check pattern 2026-10-08 12:46:55 +01:00
Riz Ashraf 0da84c804c chore: test UDP rendezvous 2026-10-08 11:36:42 +01:00
Riz Ashraf 8e3d356167 chore: test UDP rendezvous 2026-10-08 11:35:13 +01:00
Riz Ashraf 8bc8c97504 feat: use UDP rendezvous for git pre-push hook
Replaces the Python-based pre-push hook and long-polling HTTP logic with a native shell script using udp-send --wait to block on an ephemeral UDP port in the kernel. mcp-memory-server acts as the broker and replies to subscribers.
2026-10-08 11:33:13 +01:00
Riz Ashraf f4dcf3acbf feat: event-driven parity, Windows shell detection, dirty build timestamps, and gatekeeper integration 2026-10-08 10:52:19 +01:00
82 changed files with 5545 additions and 2832 deletions

No files matched your search

+1
View File
@@ -9,3 +9,4 @@ llvm-cov.zip
temp.json
*.txt
summary.md
__pycache__/
-65
View File
@@ -1,65 +0,0 @@
# Integrating MCP Memory: A Strategy Guide for LLMs and Agents
This guide documents the approach and rationale for integrating the `mcp-memory` server with agentic LLMs (like Antigravity). Because `mcp-memory` is a central hub for context, tasks, and environment state, it is critical that LLMs interact with it efficiently without exhausting their primary context window or causing workflow ambiguity.
## 1. The Core Philosophy: "The Central Brain"
The `mcp-memory` server is the persistence layer for the AI development lifecycle. It holds:
* **The Knowledge Graph:** Code changes, bug fixes, architecture decisions, and tech debt.
* **Project State:** Milestones, tasks, acceptance criteria, and PR checklists.
* **Environment State:** Handoff memos, standup reports, and environment fingerprints.
* **Live UI Integrations:** Neovim buffer manipulation and user-action webhooks.
**Rationale:** The LLM's context window is ephemeral and expensive. By pushing state to the `mcp-memory` server (via a local database and Tantivy index), the LLM can selectively retrieve only what it needs, when it needs it.
## 2. Global Rules vs. Subagents
To maximize efficiency, we split interactions into two categories: **Synchronous Rules** (executed by the primary conversational agent) and **Asynchronous Subagents** (delegated background tasks).
### A. Synchronous Rules (The Primary Loop)
The main LLM interacting with the user should be constrained by global system rules to ensure basic context synchronization. These actions must happen synchronously so the main agent never loses the plot.
* **Context Initialization (`tasks` list, `get_preflight_context`):** Executed when a session starts. This gives the LLM immediate awareness of the current workflow.
* **Context Switching (`manage_checkpoint`):** Executed when moving between branches or large features. This prevents context bleed between disparate tasks.
* **End-of-Day Handoff (`add_session_summary`, `generate_standup_report`):** Triggered when the user logs off, seamlessly serializing the mental state of the LLM for tomorrow.
### B. Subagent Orchestration (The Background Team)
Heavy or verbose interactions with the MCP server are delegated to specialized background subagents. This keeps the primary chat fast and focused on the code, while the "team" handles project management.
#### 1. `MemoryLibrarian` (The Graph Curator)
* **Role:** Analyzes git diffs and chat history to structure the Knowledge Graph.
* **Tools:** `log_code_change`, `log_error_fix`, `create_entities`, `log_tech_debt`.
* **Rationale:** Parsing diffs and determining entity relationships is token-heavy. Delegating this prevents the main agent from wasting reasoning cycles on database normalization.
#### 2. `ScrumMaster` (The Project Manager)
* **Role:** Manages the task lifecycle and acceptance criteria.
* **Tools:** `add_task`, `update_task_status`, `add_milestone`, `verify_acceptance_criteria`.
* **Rationale:** The main agent shouldn't have to repeatedly query "are we done yet?" The `ScrumMaster` runs alongside the session, validating criteria in the background and updating the board autonomously.
#### 3. `DevOpsSRE` (The Environment Manager)
* **Role:** Monitors dependencies and manages session transitions.
* **Tools:** `update_env_fingerprint`, `leave_handoff_memo`.
* **Rationale:** Prevents "it works on my machine" failures by passively updating fingerprints when build files (e.g., `Cargo.toml`) change.
## 3. Graceful Degradation & Server Resilience
The `mcp-memory` server is a distinct background process (typically port 3000). The LLM ecosystem must handle server downtime gracefully:
1. **Event Webhooks:** If the server goes down, waiting webhook tasks (e.g., waiting for a user to save a file in Neovim) will drop. These *do not* self-heal. The LLM must recognize the dropped connection and prompt the user to retry the action.
2. **Persistent Storage:** Data (tasks, graph, ledger) is persisted to `mcp_store.redb`. When the server comes back online, no data is lost. The LLM can immediately resume querying.
3. **Subagent Fast-Failing:** If the `MemoryLibrarian` attempts to log a change while the server is offline, it will instantly fail. It is designed to abandon the background task and notify the primary agent. To recover, the primary agent can manually re-invoke the Librarian once the connection is restored, instructing it to analyze recent commits to backfill the graph.
## Conclusion
By treating `mcp-memory` as the durable brain, and enforcing a strict division of labor between the primary agent loop and background subagents, we achieve a highly autonomous, highly resilient AI pair-programming environment that scales across long-running projects and multiple terminal sessions.
## 4. MCP Feature Differentiation (Cognitive Boundaries)
The MCP protocol exposes three primary primitives. To prevent LLM confusion and API hallucination, the LLM must strictly adhere to the following interaction boundaries:
### A. Tools (For Stateful Mutation)
* **When to use:** Use tools *exclusively* for mutating state (e.g., dd_task, log_code_change) or for highly targeted semantic searches (e.g., search_nodes, query_graph_path).
* **LLM Awareness:** The LLM must not use tools to repeatedly poll for state changes. Tools represent active, expensive computing steps.
### B. Resources (For Passive Awareness)
* **When to use:** Use URIs (e.g., memory://tasks/active, memory://session/delta) to read holistic project state.
* **LLM Awareness:** The client integration should map these URIs to the LLM's context window. Instead of the LLM invoking a list_active_tasks tool (which costs a round-trip), the LLM should simply read the memory://tasks/active resource content if it needs to know what to do next. Resources are for passive, zero-cost reading.
### C. Prompts (For Macro-Workflows)
* **When to use:** Use server-defined prompts to execute complex, multi-step routines that require bundled context.
* **LLM Awareness:** Instead of the user or main agent trying to manually figure out the correct sequence of tools to end a session, the LLM should trigger the handoff_routine prompt. The server will respond with a strictly formatted message array that perfectly primes the LLM on exactly what to do next. Prompts act as "macro-instructions" to prevent the LLM from wandering off-script during complex transitions.
-77
View File
@@ -1,77 +0,0 @@
# Architecture Design: MCP Resources & Prompts
## 1. Current Architecture (Tools)
Currently, the `mcp-memory` server handles MCP tools using an elegant trait-based approach in `router.rs`:
```rust
#[async_trait]
pub trait McpTool: Send + Sync {
fn name(&self) -> &'static str;
fn schema(&self) -> Value;
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> Result<String, String>;
}
```
Tools are registered into a `HashMap<String, Box<dyn McpTool>>` within the `MemoryHandler`. This prevents the main JSON-RPC match block from becoming a monolithic switch statement.
## 2. The Problem
Currently, the `resources/list`, `resources/read`, `prompts/list`, and `prompts/get` endpoints are hardcoded directly inside the `MemoryHandler::handle_request` match block in `router.rs`.
As we expand our usage of Resources (to expose the database state dynamically) and Prompts (to bundle complex workflows), continuing to hardcode them in `router.rs` will result in massive code duplication and tearup.
## 3. The Proposed Solution (Trait Extensibility)
We will replicate the success of the `McpTool` trait by introducing `McpResource` and `McpPrompt` traits.
### A. MCP Resources
**Trait Definition (`router.rs` or `resources.rs`):**
```rust
#[async_trait]
pub trait McpResource: Send + Sync {
/// The exact URI the client requests (e.g. "memory://tasks/active")
fn uri(&self) -> &'static str;
/// Human-readable name for the client UI
fn name(&self) -> &'static str;
/// Description for the client UI
fn description(&self) -> Option<&'static str> { None }
/// Mime type of the content (usually "application/json" or "text/markdown")
fn mime_type(&self) -> Option<&'static str> { Some("application/json") }
/// Retrieve the resource content
async fn read(&self, state: Arc<MemoryState>) -> Result<String, String>;
}
```
**Implementation:**
* Add `pub resources: std::collections::HashMap<String, Box<dyn McpResource>>` to `MemoryHandler`.
* In `handle_request("resources/list")`, iterate over `self.resources.values()` and build the JSON payload.
* In `handle_request("resources/read")`, lookup the requested URI in `self.resources` and call `.read(state).await`.
* Move the existing `memory://graph/entities` logic into its own handler struct.
### B. MCP Prompts
**Trait Definition (`router.rs` or `prompts.rs`):**
```rust
#[async_trait]
pub trait McpPrompt: Send + Sync {
/// The unique name of the prompt (e.g. "analyze_tech_debt")
fn name(&self) -> &'static str;
/// Description for the client UI
fn description(&self) -> Option<&'static str> { None }
/// Schema or array defining arguments (can default to empty)
fn arguments(&self) -> serde_json::Value { serde_json::json!([]) }
/// Execute the prompt and return the `messages` array payload
async fn get(&self, args: Value, state: Arc<MemoryState>) -> Result<serde_json::Value, String>;
}
```
**Implementation:**
* Add `pub prompts: std::collections::HashMap<String, Box<dyn McpPrompt>>` to `MemoryHandler`.
* In `handle_request("prompts/list")`, map over `self.prompts.values()`.
* In `handle_request("prompts/get")`, call `.get(args, state).await`.
## 4. Execution Plan
1. **Refactor `router.rs` (No functional changes yet):** Define the `McpResource` and `McpPrompt` traits. Update the `MemoryHandler` struct to hold these HashMaps. Migrate the existing hardcoded stubs (`memory://graph/entities` and `analyze_tech_debt`) into structs implementing these traits.
2. **Expand Resources (Phase 1):** Add new handlers for `memory://tasks/active`, `memory://session/delta`, etc.
3. **Expand Prompts (Phase 2):** Add new handlers for `handoff_routine`, etc.
This design guarantees we do not needlessly tear up code—we merely extend the existing robust `McpTool` pattern to the rest of the protocol.
Generated
+167
View File
@@ -1370,6 +1370,16 @@ version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef25905e51abafe4dcea6c15fec58c57b601cdbd0ee53d22ea1d3016c587d39b"
[[package]]
name = "flatbuffers"
version = "24.12.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f1baf0dbf96932ec9a3038d57900329c015b0bfb7b63d904f3bc27e2b02a096"
dependencies = [
"bitflags 1.3.2",
"rustc_version",
]
[[package]]
name = "flate2"
version = "1.1.10"
@@ -2636,12 +2646,15 @@ dependencies = [
"ignore",
"image",
"notify",
"ocrs",
"redb 4.3.0",
"reqwest",
"rmcp",
"rten",
"schemars",
"serde",
"serde_json",
"serde_yaml",
"tantivy",
"tempfile",
"thiserror 2.0.20",
@@ -2661,6 +2674,7 @@ dependencies = [
"tree-sitter-rust",
"tree-sitter-typescript",
"uuid",
"windows",
"windows-sys 0.59.0",
"winres",
]
@@ -2670,6 +2684,7 @@ name = "mcp-memory-stub"
version = "1.0.0"
dependencies = [
"async-channel",
"chrono",
"clap",
"dirs 7.0.0",
"futures-util",
@@ -2988,6 +3003,7 @@ name = "nvim-core"
version = "1.0.0"
dependencies = [
"bytes",
"chrono",
"dashmap",
"dirs 7.0.0",
"mcp-stdio",
@@ -3104,6 +3120,21 @@ dependencies = [
"objc2-core-foundation",
]
[[package]]
name = "ocrs"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e4192a7dbc7570398ef97ab2606799703b6ea11da53f637d705d44546614435"
dependencies = [
"anyhow",
"rayon",
"rten",
"rten-imageproc",
"rten-tensor",
"thiserror 2.0.20",
"wasm-bindgen",
]
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -3869,6 +3900,123 @@ dependencies = [
"rmp",
]
[[package]]
name = "rten"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf7741649977427aa13009537ac9a8fe1a503b0aa58784069ce8f117e47eb6ab"
dependencies = [
"flatbuffers",
"num_cpus",
"rayon",
"rten-base",
"rten-gemm",
"rten-model-file",
"rten-onnx",
"rten-parallel",
"rten-shape-inference",
"rten-simd",
"rten-tensor",
"rten-vecmath",
"rustc-hash",
"smallvec",
"typeid",
"wasm-bindgen",
]
[[package]]
name = "rten-base"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413dc975c7a3d0e6da0b146cd0387e3d389cceab0fc48328bfa716c7f2ae1b1e"
[[package]]
name = "rten-gemm"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8efc07c654d4b3f8d81e2efd8b13aea7475568395289e5bb35393411aa6a451e"
dependencies = [
"rayon",
"rten-base",
"rten-parallel",
"rten-simd",
"rten-tensor",
]
[[package]]
name = "rten-imageproc"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18726eae9614e23a400560eb3a5e3fdb06526914c31aadaf767f14afde7680dd"
dependencies = [
"rten-tensor",
]
[[package]]
name = "rten-model-file"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04ea9e630b377f9249ff3c290466fef282319f86d13d560964209104cd3af542"
dependencies = [
"flatbuffers",
"rten-base",
]
[[package]]
name = "rten-onnx"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96944374568990b8d1bbc5bae5957585866f9dd8256130460405730948bfd42e"
[[package]]
name = "rten-parallel"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba9075b130f81df7be82496ea3a9bbf20fe9e75f106a22b0ece8700850f8f664"
dependencies = [
"rayon",
"rten-base",
]
[[package]]
name = "rten-shape-inference"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89289c26b6a5248fccf4d67627596848a03ed31888e6ce4ba6f666c689258122"
dependencies = [
"rten-tensor",
"smallvec",
]
[[package]]
name = "rten-simd"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f0c01294eb782adca256aa130b48a04006d03763073a68f45a9c33b982ea556"
[[package]]
name = "rten-tensor"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf72556ba231d7af50238d26dd264856a2149c6369694eb79b22644c8280c81b"
dependencies = [
"rayon",
"rten-base",
"rten-parallel",
"smallvec",
"typeid",
]
[[package]]
name = "rten-vecmath"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1007f05160f9ea2ce4e9c67324e5bafb27ae1e5302d0c6b1ea4db0d35fed78ee"
dependencies = [
"rten-base",
"rten-simd",
]
[[package]]
name = "rust-stemmers"
version = "1.2.0"
@@ -4195,6 +4343,19 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.9.34+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "sha1"
version = "0.10.7"
@@ -5291,6 +5452,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]]
name = "untrusted"
version = "0.9.0"
-131
View File
@@ -1,131 +0,0 @@
# Effective Discourse: LLM Prompting Guide for MCP Memory
To get the most out of the Antigravity MCP Memory server and its advanced developer tools, use specific phrases that clearly state your intent. This guides the LLM to use the most efficient tools, reducing token consumption, speeding up time-to-resolve (T2R), and avoiding brute-force file reading.
---
## 1. Global Semantic Code Search & Navigation
Instead of having the LLM use brute-force text searches or `grep` to find abstract logic, instruct it to use local vector embeddings.
* **Don't say:** "Grep the codebase for database connection strings."
* **Do say:** "Perform a semantic code search for how database connections are established."
* **Tool Triggered:** `semantic_code_search`
---
## 2. Subgraph Expansion & Neighborhood Exploration
When inspecting complex system interactions or module dependencies around a target component.
* **Don't say:** "Tell me everything connected to the DatabaseTable node."
* **Do say:** "Get the subgraph expansion around 'DatabaseTable' up to 2 hops."
* **Tool Triggered:** `get_subgraph`
---
## 3. Automated Error Fix Suggestions
When encountering build failures, runtime crashes, or stack traces.
* **Don't say:** "Here is a stack trace, let's debug from scratch: [paste stack trace]"
* **Do say:** "Suggest an error fix for this stack trace before we start debugging."
* **Tool Triggered:** `suggest_error_fix`
---
## 4. Memory State Checkpointing & Safety Rollbacks
Before initiating risky refactors or running experimental multi-step subagents.
* **Don't say:** "Hope this refactor doesn't mess up our task board or memory graph."
* **Do say:** "Checkpoint the memory state under 'pre-refactor' before we begin."
* **Tool Triggered:** `checkpoint_state` / `restore_state`
---
## 5. Codebase Exploration & Token Efficiency
When entering a new file, avoid having the LLM read the entire contents blindly.
* **Don't say:** "Read server.rs and tell me what it does." *(Consumes massive tokens)*
* **Do say:** "Extract the AST skeleton of server.rs to understand its structure first."
* **Tool Triggered:** `read_file_skeleton`
---
## 6. Debugging & Log Parsing
Stop copy-pasting giant walls of logs into the chat interface.
* **Don't say:** "Here is the error: [paste 500 lines of logs]"
* **Do say:** "The daemon crashed. Fetch the recent logs from daemon.log." or "Watch the process logs for server.log."
* **Tool Triggered:** `process_logs` (action: "get", action: "watch")
---
## 7. Git & Context Handoff
When you've been working independently and need to loop the LLM back in on your current state.
* **Don't say:** "I changed some files, here are the diffs..."
* **Do say:** "Get the active git worktree context to review my uncommitted changes before we continue."
* **Tool Triggered:** `get_active_worktree_context`
---
## 8. Inter-Agent Signal Bus & Coordination
When multiple subagents collaborate or background tasks complete.
* **Don't say:** "Let me manually tell the ScrumMaster that testing finished."
* **Do say:** "Broadcast an agent signal that unit tests passed and ping the PrePushAuditor."
* **Tool Triggered:** `agent_signals` (action: "broadcast", action: "query")
---
## 9. Structural AST Editing
When asking the LLM to modify complex files, prevent indentation bugs and regex failures by guiding it to use tree-sitter.
* **Don't say:** "Search for `fn process()` and replace it with this string."
* **Do say:** "Use the AST node replacer to swap out the `process` function in `server.rs`."
* **Tool Triggered:** `replace_ast_node`
---
## 10. Bird's-Eye Repository Exploration
When the LLM is first analyzing a repository, don't let it run `ls -R` and guess.
* **Don't say:** "List the files in the directory and guess where the database code is."
* **Do say:** "Read the directory architecture to get a summary of what each file is responsible for."
* **Tool Triggered:** `read_directory_architecture`
---
## 11. Graph, Memory & Casing Standards
Actively instruct the LLM to maintain its memory constraints and use canonical casing.
* **Do say:** "Log this architectural decision in the knowledge graph using PascalCase for entity types."
* **Do say:** "Leave a handoff memo with the test database credentials and session action items."
* **Do say:** "Create a milestone for the 'Rich Clipboard' feature and break it down into active tasks."
* **Tools Triggered:** `create_entities`, `decisions` (log), `handoff_memos` (leave), `milestones` (add), `tasks` (add)
---
## 12. Self-Healing Graph Maintenance
* **Don't say:** "Search for duplicates and orphaned entities in the graph manually."
* **Do say:** "Sweep graph health to identify orphaned entities and duplicate candidates."
* **Tool Triggered:** `sweep_graph_health`
---
## 13. Causal Lineage & Provenance
* **Don't say:** "Search all tasks, ADRs, and commits to explain why this file was changed."
* **Do say:** "Query lineage for `server/src/state.rs` to construct a causal timeline."
* **Tool Triggered:** `query_lineage`
---
## 14. Actionable Task Resolution
* **Don't say:** "List all tasks and figure out which ones are blocked."
* **Do say:** "Get the next actionable tasks to find unblocked work ready for execution."
* **Tool Triggered:** `get_next_actionable_tasks`
---
## 15. Diagnostic Hypotheses & Reasoning Traces
* **Don't say:** "Let's test three guesses and remember what we tried in chat."
* **Do say:** "Log a hypothesis for this memory leak with tested evidence."
* **Tool Triggered:** `hypotheses` (action: "log", action: "query")
---
## 16. State Checkpoints & Rollbacks
* **Don't say:** "Save a backup snapshot before we do this refactor."
* **Do say:** "Create a checkpoint named 'pre-refactor' before editing the database layer."
* **Tool Triggered:** `manage_checkpoint`
---
By phrasing requests around *actions* rather than *information retrieval*, the LLM is primed to leverage the rich MCP toolset built into the Antigravity Memory Server.
@@ -1,4 +0,0 @@
# Role & Competency Constraints
- **Role**: Junior Developer.
- **Discretion**: NO autonomous discretion allowed. The agent must operate strictly under the direct guidance of the user and must not make autonomous decisions or execute sweeping, unchecked actions.
+6
View File
@@ -42,6 +42,12 @@ Before making expensive active tool calls, read available MCP resources:
- **Hypotheses & Root Cause Analysis**: When diagnosing complex bugs or race conditions, call `hypotheses` (action: "log" / "query") to record test evidence and maintain reasoning trails across sessions.
- **Inter-Agent Coordination**: Autonomous subagents should call `agent_signals` (action: "broadcast" / "query") to publish events and discover peer agent status.
- **Process & Daemon Logs**: Query or tail daemon logs with `process_logs` (action: "get", "watch", "clear") instead of dumping log files into context.
- **Clipboard & Multimodal Screenshots**:
- **User Image & Visual Queries**: When the user says "look at image in clipboard", "see screenshot", "check the clipboard", or refers to an error screenshot, ALWAYS call `clipboard(action: "image")`.
- **Decoupled Screenshot History**: Even if the user got sidetracked and copied text/URLs afterwards, `clipboard(action: "image")` reliably retrieves the last captured screenshot from the event-driven cache with exact metadata (`file_path`, `file_path_wsl`, `age`, `dimensions`, `ocr_text`).
- **WSL Ubuntu Compatibility**: For WSL sessions, use the returned `file_path_wsl` (e.g. `/mnt/c/...`) directly with `view_file`.
- **Text-Only Models & Fast Context**: Use the returned deterministic `ocr_text` to immediately extract error traces, URLs, and code snippets without waiting for multimodal vision tokens.
- **Copying Formatted Tables / Data**: `clipboard(action: "read")` automatically strips Win32 `CF_HTML` headers and converts HTML tables directly into clean GFM Markdown.
- **End-of-Session Handoff**: When finishing work or logging off, trigger the `handoff_routine` prompt or call `generate_standup_report` and `handoff_memos` (action: "leave").
## 3. Delegation
+1 -1
View File
@@ -1,6 +1,6 @@
# Neovim UX Protocol & Live Editing
Whenever you need to actively interact with the user's Neovim UI or dynamically inject code edits into their live buffers, use the nvim_execute_lua tool (the "God Mode" escape hatch).
Whenever you need to actively interact with the user's Neovim UI or dynamically inject code edits into their live buffers, use the nvim_exec tool (with action 'lua') (the "God Mode" escape hatch).
## 1. Showing UI Feedback (Agent Notifications)
The user has a global Lua table _G.gemini loaded in their Neovim environment. You can use it to pop up a floating notification window when you are starting a background task.
+7 -9
View File
@@ -21,7 +21,7 @@ The Antigravity CLI (`agy`) acts as the MCP Client and automatically manages the
When `agy` starts up, it reads `mcp_config.json`. If it finds `"win-nvim": { "command": "C:\\Users\\reazul.ashraf\\.local\\bin\\mcp-memory-nvim.exe" }`, it will spawn that binary as a background subprocess using standard `stdio`.
3. **Communication:**
- The LLM requests to use a consolidated tool (e.g., `nvim_view` with action `goto_line`, or `nvim_execute_lua`).
- The LLM requests to use a consolidated tool (e.g., `nvim_workspace` with action `focus`, or `nvim_exec`).
- The `agy` CLI sends a JSON-RPC request to the `mcp-memory-nvim` subprocess via its `stdin`.
- The Rust MCP Server receives the request, connects to the Neovim active socket/pipe (`~/.gemini/active_nvim.txt` or `\\.\pipe\nvim.*`), sends the Msgpack-RPC command, and writes the JSON-RPC response back to `stdout`.
- The `agy` CLI reads the response from `stdout` and returns it to the LLM context.
@@ -29,12 +29,10 @@ The Antigravity CLI (`agy`) acts as the MCP Client and automatically manages the
## Capabilities & Requirements
To use this architecture, Neovim must run the `gemini-integration.lua` script to broadcast its active socket to `~/.gemini/active_nvim.txt`.
The MCP server provides 7 cohesive domain tools:
1. **`nvim_buffer`** (actions: `get_active`, `read`, `open`, `create_scratch`, `save`, `reload`, `close`, `list`, `search`)
2. **`nvim_window`** (actions: `list`, `get_active`, `focus`, `split`, `close`)
3. **`nvim_view`** (actions: `goto_line`, `get_cursor`, `get_viewport`, `get_selection`)
4. **`nvim_diagnostics`** (actions: `get`, `set`, `set_quickfix`)
5. **`nvim_visual`** (actions: `preview`, `extmark`, `highlight`, `clear_highlight`)
6. **`nvim_execute_lua`** (direct Lua execution escape hatch)
7. **`nvim_system`** (actions: `get_info`, `get_messages`, `send_to_terminal`)
The MCP server provides 5 cohesive mega-tools:
1. **`nvim_buffer`** (actions: `read`, `replace`, `save`, `undo`, `redo`, `create_scratch`)
2. **`nvim_workspace`** (actions: `list_buffers`, `list_windows`, `focus`, `split`, `cwd`)
3. **`nvim_intelligence`** (actions: `hover`, `definition`, `references`, `outline`, `query`, `diagnostics`, `rename`, `code_action`)
4. **`nvim_ui`** (actions: `highlight`, `ghost_text`, `clear`)
5. **`nvim_exec`** (actions: `lua`, `vimscript`, `terminal`)
+25 -24
View File
@@ -3,32 +3,33 @@ name: nvim_editing
description: Route code edits to Neovim buffers instead of disk/console when Neovim is running.
trigger: always_on
---
# Neovim Code Editing
# Neovim Code Editing (Agnostic & Universal)
## Never Spawn Headless Instances (CRITICAL)
**NEVER** run `nvim <filename>` via bash/terminal commands (`run_command`) to "open" or "show" a file to the user. Because agent terminal commands run in the background, this spawns an invisible ghost process.
These rules apply universally to ALL Neovim environments (Windows, WSL, or any remote server). Do not treat them as OS-specific.
If the user asks to "open", "show", or "see" a file in Neovim, you MUST use the `win-nvim` (Neovim) MCP Server to communicate with their *active* UI (e.g., using `nvim_execute_lua` with `vim.cmd('edit <file>')`).
## 1. No Invisible Shell Edits (CRITICAL)
**NEVER** use `sed`, `awk`, `python -c`, or `echo` to modify file contents from the terminal.
File edits must use EXACTLY one of two paths:
- **Active UI (Neovim MCP responsive):** Route edits through specialized MCP tools to provide live visual feedback.
- **No UI (Neovim MCP unresponsive):** Fall back to the built-in `replace_file_content` tool to generate clean chat diffs.
When making code edits or displaying search results/outputs:
1. Verify if the `win-nvim` (Neovim) MCP server is responsive.
2. If Neovim is running, apply code changes directly into the relevant Neovim buffers using nvim_execute_lua (e.g., using vim.api.nvim_buf_set_lines or vim.cmd).
3. DO NOT output large diffs or file contents to the chat console. Prefer live buffer manipulation over replace_file_content if the file is open in Neovim.
4. **Fallback Constraint (CRITICAL):** If Neovim is NOT running, you MUST use the `replace_file_content` tool to edit files. This ensures the user is presented with a visual diff block in the chat. **NEVER** use `sed`, `awk`, or `echo` to blindly modify file contents from the terminal.
## 2. Strict Tool Adherence (No Raw Lua RCE)
You must strictly use the specialized, sandboxed Neovim MCP tools:
- `nvim_buffer`: For reading, writing, saving, and creating scratch buffers.
- `nvim_workspace`: For creating splits and focusing panes.
- `nvim_ui`: For highlighting diffs, adding ghost text, and showing previews.
**DO NOT** use `nvim_exec` (action `lua`) to mutate editor state. It is restricted to **READ-ONLY** queries.
## Visual Cues & Auto-Save Policy (MANDATORY)
When manipulating buffers via Lua, you MUST provide visual feedback:
1. **Notifications:** Always trigger a notification explaining what you did using `require("notify")("Antigravity edited [File]: [Summary]", "info", { title = "Antigravity" })` (since `rcarriga/nvim-notify` and `noice.nvim` are installed).
2. **Buffer Focus:** Always switch the active Neovim window to the buffer you just edited (e.g., `vim.cmd('b ' .. bufnr)`).
3. **Cursor Jump:** Move the cursor to the line you edited (`vim.api.nvim_win_set_cursor(0, {line, 0})`).
4. **Auto-Save by Default:** You MUST save the buffer (`vim.cmd('write')`) after making edits so that external tools (tests, compilers, linters, git) can see the changes immediately and unattended tasks are not stalled.
5. **Interactive Review Mode (Exception):** The ONLY time you should disable auto-save (leaving the buffer modified and unsaved) is when you are actively halting execution to seek manual user confirmation for a highly sensitive change. This allows the user to visually review the `gitsigns` diff in the gutter before manually saving (`:w`).
## 3. Headless Quarantine
Headless mode (`nvim --headless`) is strictly banned for interactive edits.
**NEVER** run `nvim <filename>` via terminal commands to edit or "open" a file for the user (it spawns a zombie ghost process).
Headless instances are allowed ONLY for non-interactive background batch processing (e.g., project-wide formatting or linting).
## Interactive Review & Automatic Wakeup (MANDATORY)
When you modify a buffer and disable auto-save to allow the user to visually review the changes, you MUST NOT just wait idly for them to chat back.
You MUST spawn a background task to listen for their :w save event:
1. Use the ␍un_command tool to execute:
curl -s "http://127.0.0.1:3000/events/wait?topic=nvim:save:$((Get-Item $File).FullName.Replace('\', '/'))&session_id=$SessionId"
2. Set WaitMsBeforeAsync to a low value (e.g., 500ms) so it goes to the background immediately.
3. Stop calling tools and end your turn.
When the user saves the file, the curl command will complete and the system will automatically wake you up to proceed.
## 4. UI Presentation & Chat Console Minimization
Never output large plans, context blocks, or architectural discussions to the chat console if Neovim is running. You MUST use the `nvim_buffer` and `nvim_workspace` tools to open a vertical split (e.g., `Antigravity_Plan.md` scratch buffer) and present the markdown natively. Reserve the chat console strictly for brief confirmations.
## 5. Visual Cues & Auto-Save
When manipulating buffers via MCP:
1. **Focus & Jump:** Always switch the active Neovim window to the edited buffer and jump the cursor to the changed line.
2. **Auto-Save:** Auto-save the buffer by default so linters/compilers see it immediately.
3. **Interactive Review:** If pausing for human review (leaving unsaved), you MUST spawn a background `curl` task listening to `http://127.0.0.1:3000/events/wait?topic=nvim:save...` to wake you up when the user saves (`:w`).
+1 -1
View File
@@ -3,6 +3,6 @@
When interacting with the user's Neovim editor (e.g., opening a file, moving the cursor, reading the active buffer, setting diagnostics), you MUST ALWAYS use the MCP tools provided by the `win-nvim` (Neovim) MCP server.
- You are strictly forbidden from using bash scripts, `nvim --server`, or other raw terminal/shell hacks to remote-control Neovim.
- You must rely entirely on the consolidated MCP tool registry (`nvim_buffer`, `nvim_window`, `nvim_view`, `nvim_diagnostics`, `nvim_visual`, `nvim_execute_lua`, `nvim_system`).
- You must rely entirely on the consolidated MCP tool registry (`nvim_buffer`, `nvim_workspace`, `nvim_intelligence`, `nvim_ui`, `nvim_exec`).
- If the tool is eagerly loaded, use it natively as an agent tool. If lazy-loaded, invoke it via the `call_mcp_tool` mechanism.
-5
View File
@@ -1,5 +0,0 @@
# Rule 1: Network Connectivity Testing
- **Mandatory Tooling**: When testing ANY network connectivity, you MUST use the exact same Rust crates used in the production code.
- **Methodology**: You must create standalone Rust applications (e.g., small test binaries or examples in the repository) that utilize the exact same networking plumbing as the main application.
- **Forbidden Tools**: You CANNOT use bash (e.g., curl, netcat), Python, Java, Perl, wscat, or any other external scripting/CLI tools to validate network connectivity. Testing with these tools creates false positives and bypasses the actual networking crates causing issues.
-5
View File
@@ -1,5 +0,0 @@
# Rule 2: Empirical Evidence & Logging
- **Mandatory Logging**: If an issue occurs and there are no logs (or insufficient logs) to diagnose it, you must STOP immediately and add logging.
- **Replication**: After adding logging, you must repeat the test to replicate the exact issue so that the logs capture the failure.
- **No Blind Changes**: NO business logic can be altered blindly or based on guesses. You must have empirical evidence (derived from the logs) proving the root cause before attempting any changes to the logic.
-7
View File
@@ -1,7 +0,0 @@
# Rule 3: Resiliency & Cross-OS Load Testing
- **Resiliency Requirement**: A fix must be resilient and work under load. It is not enough for it to work just once.
- **Mandatory 5x Matrix Testing**: Any request/response cycle must be explicitly tested five (5) times for each of the following boundaries:
1. Windows to Windows (`win - win`)
2. WSL to Windows (`wsl - win`)
- **Documentation**: The results of these load tests MUST be recorded permanently (e.g., in an artifact or log) to prove resilience and to avoid repeating needless re-tests.
@@ -1,5 +0,0 @@
# Rule 4: Component Isolation & Skeletal Troubleshooting
- **Targeted Testing**: If a bug is found, DO NOT rely on testing the full stack. You must isolate and test the offending class or method directly.
- **Skeletal Reproducers**: If the issue spans across boundaries (e.g., between server, stub, or nvim), you must create skeletal (minimal reproducible) versions of those components.
- **Purpose**: Troubleshooting must be done on these skeletal versions to isolate the broken feature or aspect without the noise, side-effects, or overhead of the full application stack.
@@ -1,5 +0,0 @@
# Rule 5: Client-Centric Testing Personas
- **Avoid Server Bias**: Unit tests and integration tests must NOT be exclusively server-centric.
- **Client Personas**: Tests must explicitly adopt the persona, perspective, and constraints of the client components (e.g., the `stub` or `nvim`).
- **Validation**: Testing must validate the interaction from the client's side, ensuring that the client component correctly constructs the request, handles the connection lifecycle, and properly parses the response, rather than just verifying that the server successfully processed an isolated payload.
-13
View File
@@ -1,13 +0,0 @@
---
name: strict-no-verify
description: Strictly forbids the use of --no-verify or -n when interacting with git to ensure quality gates are run.
always_on: true
---
# STRICT GIT HOOK ENFORCEMENT
- **NEVER** use `--no-verify` or `-n` with `git commit` or `git push`.
- Bypassing git hooks is considered a critical violation of trust.
- You must allow the local git hooks (e.g., `pre-push`) to validate the code.
- If a hook fails (e.g., `ruff` check, `pytest`), you MUST fix the underlying issues iteratively until the hook passes natively. Do not assume a single fix attempt works without re-verifying.
- BEFORE running `git push`, you MUST use the `ask_question` tool to pop up an interactive modal to request explicit user permission. Wait for the user's approval before executing the push.
-4
View File
@@ -1,4 +0,0 @@
Write-Host "Building Windows Server..." -ForegroundColor Cyan
Building Windows Server...
cargo build --release -p mcp-memory-server
Compiling tantivy v0.26.2
-20
View File
@@ -1,20 +0,0 @@
use std::process::Command;
fn main() {
let git_hash = Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.unwrap_or_else(|| "unknown".to_string());
let git_date = Command::new("git")
.args(&["log", "-1", "--format=%cd", "--date=format:%Y.%m.%d"])
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.unwrap_or_else(|| "unknown".to_string());
let version = format!("{} ({} {})", env!("CARGO_PKG_VERSION"), git_date.trim(), git_hash.trim());
println!("cargo:rustc-env=APP_VERSION={}", version);
}
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 82 KiB

-199
View File
@@ -1,199 +0,0 @@
# MCP Memory Server Architecture & Workflow Design
## 1. Core Architecture
The `mcp-memory` system runs as a **single, continuously running background daemon natively on Windows**. It manages the state of the Antigravity knowledge graph and serves as a universal backend for both Windows and WSL environments.
### Why this design?
* **Cross-OS I/O Optimization:** Prevents the WSL agent from performing slow, heavy filesystem writes against the mounted Windows `C:\` drive.
* **Concurrency & Locking:** A single daemon holds the lock on the `mcp_memory` JSON files, preventing data corruption and eliminating complex delta-reconciliation between parallel WSL and Windows processes.
* **Dual Transport System:** Utilizes both `stdio` and HTTP transports concurrently. The local Windows `agy` instance connects natively via standard `stdio`, while the Axum HTTP server provides synchronous, non-blocking access for WSL clients and external scripts.
## 2. Server Transport & Endpoints (Axum)
The daemon uses the `axum` and `rust-mcp-axum` crates, binding to `0.0.0.0:3000` to serve the network.
### Standard MCP Endpoints
* `GET /ws`: The WebSocket endpoint for efficient, low-latency, full-duplex JSON-RPC communication (preferred for proxies and the UI dashboard).
* `GET /sse`: The Server-Sent Events (SSE) endpoint. Antigravity clients connect here to keep a one-way pipe open for receiving pushed responses.
* `POST /messages`: The JSON-RPC endpoint. Clients use this to send tool calls and resource reads up to the server when connected via SSE.
### Dashboard REST API
The server hosts a rich Single Page Application (SPA) natively on the root route, backed by a suite of REST endpoints:
* `GET /`: The Brain Monitor live HTML dashboard SPA.
* `GET /api/graph`: Returns the full node/edge topology for the interactive physics-simulated canvas.
* `GET /api/tasks` & `POST /api/tasks/{id}/complete`: Drives the actionable Kanban board.
* `GET /api/ledger` & `GET /api/search`: Powers the code change ledger and global fuzzy search UI.
* `GET /api/stats`: Returns a JSON snapshot of current entity, relation, task, and tech debt counts.
### Git & IDE Integration Endpoints
* `UDP /nvim/telemetry`: A one-way connectionless datagram listener that Neovim instances hit on `FocusGained` or `BufEnter` to globally broadcast the user's active file to the UI and Agent.
* `GET /gate/verify`: A lightweight, deterministic endpoint used by external scripts (like a `git push` wrapper) to verify if an action is authorized based on the current state.
## 3. Client Connections
The Antigravity configurations (`mcp_config.json`) utilize the dual-transport system:
* **Windows `agy`:** Spawns and connects to the local daemon natively via `stdio` subprocess execution.
* **WSL (Linux) `agy`:** Connects to the running Windows HTTP daemon via the host network proxy, e.g., `http://127.0.0.1:3000/sse`.
## 4. Git Integration (Global Wrapper)
Instead of relying on localized per-repository Git hooks (like `.git/hooks/pre-push`), the system leverages a **global bash/PowerShell alias wrapper** for the `git` command. This intercepts `git` commands universally across the OS.
### Workflow Example (Push Safety Gate)
1. The user types `git push`.
2. The global wrapper intercepts the command.
3. It makes a synchronous HTTP request to the local daemon: `curl -s http://localhost:3000/gate/verify`.
4. If the endpoint returns `200 OK` (indicating the `PrePushAuditor` subagent has verified that unit tests pass and history is squashed), the push proceeds.
5. If not `200 OK`, the wrapper blocks the push and alerts the user to fix tests or run `gsquash`.
### Benefits of the Wrapper Approach
* **Universal Enforcement:** The push safety gate is protected across all repositories automatically, without copying hook scripts.
* **No File Locks:** Uses safe, lightweight, parallelizable HTTP requests rather than executing the Rust binary directly.
* **Action Logging:** The wrapper can be seamlessly extended to log actions (like `checkout` or `commit`) directly into the knowledge graph in real-time.
## 5. Storage Architecture & Persistence (Redb LSM-Tree)
The daemon has completely eliminated raw JSON file sprawl and fragmented delta-file reconciliation. It now utilizes a pure-Rust, embedded Key-Value engine (`redb`) that implements a robust Log-Structured Merge-Tree (LSM-tree) architecture.
### Key Principles:
* **Embedded Database Engine:** All structured components (Tasks, Snippets, Tech Debt, Checklists, etc.) are stored as binary-encoded values inside a unified `redb` database file (`store.redb`).
* **ACID Compliance & File Locks:** The Windows daemon holds an exclusive read-write lock on the database file, guaranteeing zero data corruption, race conditions, or lock contention during concurrent access.
* **Atomic Write-Guard Scope:** Store modification methods (`Store::modify` and `Store::modify_async`) retain the write lock through both the in-memory mutation and JSON serialization phases, eliminating lock-release TOCTOU race conditions.
* **Store Quarantine Mode:** If deserialization fails during `Store::load_from_db`, the store flags `is_corrupted = true` and refuses to overwrite database keys with default values on subsequent writes.
* **Asynchronous Checkpointing:** The core Knowledge Graph (Entities, Relations, Observations) still utilizes a Write-Ahead Logging (WAL) pattern (`wal.jsonl`) and a master snapshot (`master.json`) to allow safe, lock-free memory mutations which are reconciled in the background.
## 6. Domain Models & Component Stores
The system leverages a modular, thread-safe generic `Store<T>` abstraction that automatically transparently serializes and deserializes native Rust structs directly into the underlying `redb` tables.
Currently implemented persistent stores include:
* **Audit Ledger & Tasks:** Tracks agent actions and active background tasks.
* **Context & Handoffs:** Session Summaries, Handoff Memos, Hypotheses, and Agent Signals.
* **Engineering Tracking:** ADRs (Architecture Decision Records), Snippets, Error Fixes, Tech Debt, and PR Checklists.
* **Environment State:** Pinned Files, Env Fingerprints, Milestones, and Environments.
* **Safety Gates:** Authorized execution gates (Push Safety).
## 7. Full-Text & Semantic Search Engine (Tantivy + FastEmbed)
To support blazing-fast, intelligent semantic retrieval across the sprawling knowledge graph, the daemon embeds **Tantivy** (a full-text search engine inspired by Apache Lucene) alongside **FastEmbed** (a local ONNX runtime for vector embeddings).
* **The `MemoryIndex`:** Whenever the graph or auxiliary stores mutate, a background thread dynamically rebuilds the Tantivy index (`tantivy_index/` dir) and computes semantic vectors.
* **Pre-cached Vector Embeddings:** `SearchService::semantic_search` reuses pre-cached snippet embedding vectors (`snippet.embedding`), bypassing redundant ONNX neural network inference calls during query execution.
* **Global Omni-Search:** This architecture powers the `omni_search` tool, allowing subagents to instantly fuzzy-search and semantically rank documents across Entities, Tasks, Snippets, Error Fixes, and ADRs simultaneously in milliseconds, without loading massive JSON arrays into RAM.
## 8. Webhook Telemetry & Passive Ingestion
The server features a suite of webhook listeners that passively ingest development activity to build context without requiring human copy-pasting:
* **Terminal Ingestion (`/terminal/telemetry`):** Shell hooks (PowerShell/Nushell) silently POST command execution history and exit codes, allowing agents to read recent stack traces via the `memory://terminal/recent` resource.
* **IDE Telemetry (`/nvim/telemetry`):** A connectionless UDP datagram listener receives focus events from Neovim, broadcasting the user's active file to the UI and Agent instantly.
## 9. The Gate System (Push Safety Verification)
The binary supports a flexible safety gate authorization system, accessible both via the HTTP API and CLI subcommands.
* **API / CLI set**: Records an authorization status (`authorized`, `blocked`, or `pending`) for a specific target and namespace. Can be invoked via `POST /gate/set` (JSON) or `mcp-memory-stub gate set`.
* **API / CLI verify**: Evaluates a pending action against the gate store. It returns standard HTTP status codes (`200 OK`, `403 Forbidden`, `404 Not Found`) via `GET /gate/verify?action=...` or POSIX exit codes (0, 1, 2) via `mcp-memory-stub gate verify`. Both support a `consume` parameter/flag to immediately revoke the authorization after a successful check.
## 9. Operational Configuration & Paths
The physical storage location of the knowledge graph and all persistent stores is strictly controlled by the MCP_MEMORY_STORE_DIR environment variable.
* **Default Path:** If not set, the daemon defaults to ~/.gemini/mcp_memory.
* **Port Binding:** The Axum HTTP server strictly binds to .0.0.0:3000.
## 10. Exposed MCP Capabilities (Tools)
The server implements the Model Context Protocol (MCP) by exposing a vast suite of tools via the JSON-RPC interface, categorized broadly into:
* **Graph Management:** create_entities, create_relations, merge_entities,
read_graph, etc.
* **Task & Context Tracking:** tasks, milestones, handoff_memos, hypotheses, agent_signals, process_logs, add_session_summary, etc.
* **Engineering & DevOps:** log_code_change, log_error_fix, tech_debt, decisions, etc.
* **Environment & Workspaces:** environment, manage_checkpoint, manage_subagent_namespace, snippets, etc.
## 11. Concurrency & Thread Safety
With the introduction of the Dual Transport System, the daemon must safely handle simultaneous read/write requests from both Stdio (Windows agy) and HTTP (WSL agy) clients.
* **Shared State:** The entire server operates on a cloned Arc<MemoryState>.
* **Locking Mechanism:** The unified Knowledge Graph and modular Store<T> components are protected by RwLock primitives.
* **Safe Mutation:** Store modifications utilize closure-based modify(|store| { ... }) methods to ensure locks are safely acquired, mutations applied, and file writes executed sequentially without deadlocking the asynchronous Tokio runtimes.
## 12. Lifecycle & Startup Management (Server/Stub Architecture)
The mcp-memory daemon employs a strict Server/Client paradigm to maintain separation of concerns. There are no dual roles or morphing executables.
### The Windows Background Daemon (`mcp-memory-server.exe`)
The server executable is responsible exclusively for running the Axum HTTP and WebSockets daemon and managing the Knowledge Graph. It does not contain any proxy logic. If port 3000 is already in use by an existing server instance, it gracefully exits instead of attempting to run.
* **Non-Blocking Startup & Indexing:** Upon launch, the server uses a `tokio::spawn` task to rebuild the Tantivy index in the background. This ensures the Axum HTTP server binds immediately to port 3000, allowing instantaneous liveness checks via the `/ping` endpoint without waiting for the synchronous I/O operations of the index rebuild to complete.
* **Robust Background Detachment:** To ensure the server survives the death of the parent shell that spawns it (such as a deployment script), it uses Windows `WScript.Shell` (via `start_server.ps1`) to spawn the binary with a strictly hidden window style (`SW_HIDE`). This completely detaches it from the parent process tree, preventing `CTRL_CLOSE_EVENT` signals from killing the background daemon when the deploying terminal closes.
### The Client Proxy (`mcp-memory-stub.exe`)
All Antigravity sessions (Windows and WSL) use the lightweight `mcp-memory-stub` as their proxy. The stub connects to the server via WebSockets and acts as the bridge for standard `stdio` JSON-RPC traffic.
* **Startup via Interop/Spawn:** Upon launch, the stub attempts to connect to the Windows host on port 3000. If the server is offline, the stub automatically executes a spawn command (e.g., executing `Start-Process` natively, or via WSL interop) to silently wake up the Windows Leader before commencing the proxy loop.
* **MPSC Queue Resilience:** The stub utilizes an asynchronous multi-producer, single-consumer (MPSC) channel queue. If the Windows Leader daemon restarts or momentarily drops, the proxy buffers incoming JSON-RPC tool calls and infinitely retries them until the connection is restored. This guarantees **zero message loss** and **zero thread leaks** without crashing the active `agy` session.
* **Zero I/O Penalty (WSL):** This ensures the Linux binary never directly touches the Windows NTFS files, reserving all heavy disk operations for the native Windows host.
## 13. Cargo Workspace & Binary Artifacts
To optimize for different environments, the codebase is structured as a Cargo Workspace containing two distinct crates:
### 1. mcp-memory-server (The "Full-Fat" Daemon)
* **Path:** server/
* **Size/Complexity:** Heavy (contains Axum, MCP SDK, JSON parsing, Tokio runtime).
* **Role:** This is the primary background daemon. It binds to .0.0.0:3000, holds file locks, and manages the graph.
* **Windows Behavior:** It is designed to run in the background as a standalone service.
### 2. mcp-memory-stub (The Ultra-Lightweight Proxy)
* **Path:** stub/
* **Size/Complexity:** Extremely light (only relies on
eqwest and okio).
* **Role:** A dedicated, OS-agnostic proxy binary used strictly for routing stdio JSON-RPC traffic over HTTP to a remote Leader. Windows gy clients point directly to this binary to bypass loading the heavy Server daemon into memory.
* **WSL Behavior:** Compiled as a Linux native binary (x86_64-unknown-linux-musl). When executed by WSL agy, it acts as a transparent proxy to http://127.0.0.1:3000. It can also execute wake_cmd (e.g., WSL interop) to silently wake the Windows host if the Leader is offline.
## 14. Neovim Integration Architecture & "God Mode"
To enable seamless pair-programming inside Neovim, the daemon integrates with Neovim using two complementary systems: a Webhook Telemetry pipeline and dedicated MCP binaries.
### Telemetry Pipeline (Last Focused Wins)
A lightweight Lua script (`gemini-integration.lua`) is loaded into Neovim, which fires an asynchronous UDP datagram to `127.0.0.1:3002` (fire-and-forget via native libuv) whenever the user focuses a buffer or moves the cursor. The server then writes this data (including `session_id`, `file`, `line`, and `col`) to both the Windows and WSL `active_nvim.txt` files and broadcasts it over WebSockets.
### Native MCP Binaries (win-nvim & linux-nvim)
The project compiles two standalone, highly-performant binaries that implement the MCP JSON-RPC protocol over Stdio and bridge it directly to Neovim's Msgpack-RPC Named Pipes/Sockets. These binaries avoid hardcoding infinite tools by utilizing a "God Mode" escape hatch.
Exposed Neovim Tools:
* **
vim_get_active_buffer &
vim_get_cursor**: Read file state.
* **
vim_goto_line &
vim_set_diagnostics**: Manipulate IDE state.
* **
vim_get_visual_selection**: Read exact highlight coordinates (handles mode dynamically).
* **
vim_list_buffers**: Discover unsaved work and context.
* **
vim_get_diagnostics**: Read live LSP errors dynamically instead of requiring a compiler.
* **
vim_execute_lua ("God Mode")**: The ultimate fallback tool. Evaluates raw Lua scripts inside the active Neovim instance and returns JSON. This prevents the need to continuously recompile the Rust server whenever a new Neovim capability is required.
## 15. Build & Deployment Strategy
Because the background server operates as an always-on Windows daemon, standard recompilation and file-copying strategies will fail due to active Windows OS file locks.
### Randomized Lock Bypassing
The \uild.ps1\ deployment pipeline intercepts locked \.exe\ files by appending a unique, timestamped/randomized suffix (e.g., \mcp-memory-server.exe.12345.old\) when forcing a \Move-Item\. This guarantees that rapid sequential deployments (where a previous \.old\ file might still be locked by a zombie process) never silently fail or collide.
### Dynamic Versioning
To trace binary provenances during rapid deployment cycles, all binaries embed dynamic versioning directly at compile time (via \uild.rs\ and \uild_template.rs\). The injected \APP_VERSION\ environment variable combines the static Cargo \ ersion\ with the live \git\ short hash and UTC timestamp, allowing the CLI \--version\ commands and the HTTP \/api/version\ endpoints to guarantee exactly which iteration of the code is actively executing.
## 16. Testing Architecture (Native Rust E2E)
Historically, the project relied on a complex Python testing suite (\pytest\ + \mcp_client.py\) to validate the server over HTTP/SSE. This has been fully deprecated in favor of **Native Rust End-to-End Testing**.
* **Unit Tests:** Handlers and business logic are tested directly inside \server/src/handlers.rs\ using native \ okio::test\ constructs.
* **E2E Tests:** Integration and full-system tests run via \stub/tests/e2e.rs\ and \win-nvim/tests/integration_test.rs\, ensuring type safety, faster execution, and eliminating Python environment dependencies.
## 17. Automated Git Context Binding (git2)
Instead of forcing the LLM client to manually run `git rev-parse HEAD` and pass `git_branch` / `git_commit` arguments for every single code change, the server integrates the native **`git2`** C bindings.
When engineering endpoints (`log_code_change`, `log_error_fix`, `log_tech_debt`) are invoked, the server asynchronously discovers the surrounding Git repository, peels the HEAD reference, and automatically injects the current commit hash, commit message, and branch name directly into the stored entities and audit logs. This guarantees airtight VCS traceability without wasting LLM tokens or relying on the agent's memory.
## 18. Upcoming Enhancements
### A. Graph Summarization & Decay
To prevent context window bloat, the Redb engine will enforce TTLs on ephemeral nodes (`StickyNotes`, minor `Tasks`). Furthermore, an `archive_routine` prompt will allow subagents to automatically compress older `SessionSummaries` into dense milestone retrospectives.
### B. Proactive Subagent Triggers
The daemon will eventually gain the ability to autonomously spawn background subagents (like the `BugDiagnostician`) when specific events are detected in the webhook telemetry, rather than relying strictly on the human user to initiate the agent.
## 11. Advanced Clipboard Capabilities
The memory server bypasses typical cross-OS Linux/Windows clipboard restrictions by proxying operations from the WSL stub back to the Windows Axum host.
It supports reading and writing rich formats natively to the Windows Host OS using the clipboard-win and rboard crates:
* **Plain & Rich Text:** CF_UNICODETEXT and CF_HTML are supported for rich copying/pasting.
* **File Drops (CF_HDROP):** The server can parse file lists copied from Windows Explorer, and can inversely synthesize file drops into the clipboard from absolute paths.
* **Images (CF_BITMAP):** The server natively rasterizes clipboard bitmaps to JPEG on read, and can write raw RgbaImage buffers back to the clipboard on write.
* **Developer Tooling:** read_file_skeleton (AST), get_active_worktree_context (Git), get_recent_logs, toggle_clipboard_watch_mode.
## 19. High-Performance Concurrency & Resilience Guarantees
* **Async Channel Backpressure (`push_async`)**: `Store::modify_async` uses `DbWriteQueue::push_async` with `tx.send(task).await` backpressure to guarantee database write persistence under heavy async write loads without dropping write transactions.
* **Atomic Search Index Swaps**: `MemoryState::rebuild_index` constructs and populates a new `MemoryIndex` instance in isolation before performing an atomic pointer swap (`*self.search_index.write().await = new_idx`), eliminating transient empty search result windows.
* **SIMD-Friendly Single-Pass Cosine Similarity**: `cosine_similarity` calculates dot product and Euclidean norm squares in a single linear pass over float vectors, enabling SIMD compiler auto-vectorization.
* **Safe Stream Decoding on Log Tails**: Log tail operations (`get_recent_logs`) read raw bytes and decode using lossy UTF-8 conversion (`String::from_utf8_lossy`) to ensure resilience when seeking across multi-byte UTF-8 boundaries.
* **Serde Parameter & Enum Tolerance**: All action enums (`StickyNoteAction`, `SnippetSearchMode`, `Relation`) support case-insensitive variants and field aliases (`source`/`from`, `target`/`to`, `relationType`/`relation_type`) to ensure seamless execution when LLMs pass varied string formatting.
+85 -16
View File
@@ -1,24 +1,16 @@
-- gemini-integration.lua
-- Registers the active Neovim instance for the Antigravity MCP Server
local last_active_task = nil
local last_active_gate = nil
vim.api.nvim_create_autocmd({"VimEnter", "FocusGained", "BufEnter", "BufWritePost"}, {
callback = function(ev)
-- Only register if Neovim has an active UI attached (ignore headless/embedded instances)
if #vim.api.nvim_list_uis() > 0 then
local server_name = vim.v.servername
if server_name then
-- 1. Legacy Disk Write (Fallback)
local home = os.getenv("HOME") or os.getenv("USERPROFILE")
if home then
os.execute("mkdir -p " .. home .. "/.gemini")
local path = home .. "/.gemini/active_nvim.txt"
local f = io.open(path, "w")
if f then
f:write(server_name)
f:close()
end
end
-- 2. V2 Telemetry Push (WebSockets & Cross-OS Sync via Rust Server)
-- 2. V2 Telemetry Push (HTTP with Knowledge Projection + UDP Fast Mirror)
local file = vim.api.nvim_buf_get_name(0)
local cursor = vim.api.nvim_win_get_cursor(0)
local row, col = cursor[1], cursor[2]
@@ -26,6 +18,7 @@ vim.api.nvim_create_autocmd({"VimEnter", "FocusGained", "BufEnter", "BufWritePos
'{"session_id": "%s", "event": "%s", "file": "%s", "line": %d, "col": %d}',
server_name:gsub("\\", "\\\\"), ev.event, file:gsub("\\", "\\\\"), row, col
)
local current_buf = ev.buf
vim.fn.jobstart({
"curl", "-s", "-X", "POST", "http://127.0.0.1:3000/nvim/telemetry",
@@ -38,11 +31,16 @@ vim.api.nvim_create_autocmd({"VimEnter", "FocusGained", "BufEnter", "BufWritePos
local raw = table.concat(data, "")
if raw ~= "" then
local ok, resp = pcall(vim.fn.json_decode, raw)
if ok and resp and (resp.tech_debts or resp.adrs) then
if ok and resp then
vim.schedule(function()
if _G.gemini and _G.gemini.project_knowledge then
if (resp.tech_debts or resp.adrs) and _G.gemini and _G.gemini.project_knowledge then
_G.gemini.project_knowledge(resp.tech_debts, resp.adrs, current_buf)
end
if _G.gemini and _G.gemini.update_statusline then
last_active_task = resp.active_task
last_active_gate = resp.active_gate
_G.gemini.update_statusline(resp.active_task, resp.active_gate)
end
end)
end
end
@@ -50,7 +48,6 @@ vim.api.nvim_create_autocmd({"VimEnter", "FocusGained", "BufEnter", "BufWritePos
end,
})
-- 3. UDP Mirroring (Native fast sync)
local uv = vim.uv or vim.loop
local client = uv.new_udp()
if client then
@@ -64,6 +61,78 @@ vim.api.nvim_create_autocmd({"VimEnter", "FocusGained", "BufEnter", "BufWritePos
desc = "Register active Neovim instance for Gemini CLI",
})
-- 3. Push-driven Server-Sent Events (SSE) subscriber (Zero Polling)
local sse_job_id = nil
local function start_sse_subscriber()
if sse_job_id then
pcall(vim.fn.jobstop, sse_job_id)
sse_job_id = nil
end
sse_job_id = vim.fn.jobstart({
"curl", "-s", "-N", "http://127.0.0.1:3000/api/events/stream"
}, {
on_stdout = function(_, data)
if not data then return end
for _, line in ipairs(data) do
if line:match("^data:%s*") then
local raw_json = line:gsub("^data:%s*", "")
local ok, ev = pcall(vim.fn.json_decode, raw_json)
if ok and ev and ev.payload then
local topic = ev.topic
local payload = ev.payload
local changed = false
if topic == "task:event" then
if payload.active_task ~= nil then
last_active_task = payload.active_task
changed = true
elseif payload.status == "in_progress" or payload.status == "active" then
last_active_task = payload.title or payload.task_id
changed = true
elseif payload.status == "completed" or payload.status == "expired" or payload.status == "failed" then
if last_active_task == (payload.title or payload.task_id) then
last_active_task = nil
changed = true
end
end
elseif topic == "gate:event" then
if payload.active_gate ~= nil then
last_active_gate = payload.active_gate
changed = true
elseif payload.status then
last_active_gate = payload.status
changed = true
end
end
if changed and _G.gemini and _G.gemini.update_statusline then
vim.schedule(function()
_G.gemini.update_statusline(last_active_task, last_active_gate)
end)
end
end
end
end
end,
on_exit = function()
sse_job_id = nil
-- Reconnect after server restart without busy looping
vim.defer_fn(function()
if #vim.api.nvim_list_uis() > 0 then
start_sse_subscriber()
end
end, 5000)
end,
})
end
-- Initialize SSE push listener when UI is active
if #vim.api.nvim_list_uis() > 0 then
start_sse_subscriber()
end
-- Load UI & Keymaps
local is_win = vim.fn.has("win32") == 1
local ui_path = is_win and "C:/Users/reazul.ashraf/workspace/rust/mcp-memory/gemini-ui.lua" or "/mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory/gemini-ui.lua"
+44
View File
@@ -348,4 +348,48 @@ function M.send_to_terminal(command)
end
end
-- ============================================================================
-- Antigravity Statusline Integration (Push-driven / Zero-polling)
-- ============================================================================
local statusline_cache = {
text = ""
}
function M.update_statusline(active_task, active_gate)
local parts = {}
if active_task and active_task ~= vim.NIL and active_task ~= "" then
local title = tostring(active_task)
if #title > 24 then
title = title:sub(1, 22) .. ".."
end
table.insert(parts, "📋 " .. title)
end
if active_gate and active_gate ~= vim.NIL and active_gate ~= "" then
local status = tostring(active_gate):lower()
if status == "authorized" then
table.insert(parts, "🟢 PUSH:OK")
elseif status == "blocked" then
table.insert(parts, "🔴 PUSH:BLOCKED")
else
table.insert(parts, "🟡 PUSH:WAIT")
end
end
if #parts > 0 then
statusline_cache.text = " [AGY: " .. table.concat(parts, " | ") .. "] "
else
statusline_cache.text = ""
end
vim.schedule(function()
pcall(vim.cmd, "redrawstatus")
end)
end
function M.statusline()
return statusline_cache.text
end
return M
+1 -1
View File
@@ -59,7 +59,7 @@ The server registers 5 high-signal workflow prompts to initiate standardized age
## 5. Consolidated Smart Tools Architecture (11 Primary Tools)
The server consolidates granular single-purpose tools into domain-named smart tools. Always prefer the consolidated tools over legacy aliases:
The server consolidates granular single-purpose tools into domain-named smart tools.
* **`tasks`**: Complete task lifecycle management.
- `action: "add"`: Create a new task (requires `title`, optional `description`, `git_branch`, `repo_name`, `priority: "low" | "medium" | "high" | "urgent"`, `assigned_agent`, `verification_command`, `parent_id`, `dependencies`).
+6 -6
View File
@@ -28,7 +28,7 @@ deploy-win: deploy-server deploy-stub-win deploy-nvim-win deploy-rules-win
# Stop the background server gracefully and verify port release
stop port="3000":
@Write-Host "Shutting down MCP server gracefully on port {{port}}..." -ForegroundColor Cyan
-& { $token = Get-Content "$env:USERPROFILE\.gemini\mcp_memory\admin.token" -ErrorAction SilentlyContinue; if ($token) { $hdr = @{ Authorization = "Bearer $($token.Trim())" }; $res = Invoke-RestMethod -Uri "http://127.0.0.1:{{port}}/shutdown" -Method Post -Headers $hdr -ErrorAction SilentlyContinue } else { $res = Invoke-RestMethod -Uri "http://127.0.0.1:{{port}}/shutdown" -Method Post -ErrorAction SilentlyContinue }; if ($res) { Write-Host "✅ Shutdown acknowledged with version details:" -ForegroundColor Green; $res | ConvertTo-Json -Depth 5 }; $stopped = $false; for ($i = 1; $i -le 10; $i++) { $conn = Get-NetTCPConnection -LocalPort {{port}} -State Listen -ErrorAction SilentlyContinue; $proc = Get-Process mcp-memory-server -ErrorAction SilentlyContinue; if (-not $conn -and -not $proc) { $stopped = $true; Write-Host '✅ Server process and port {{port}} fully terminated.' -ForegroundColor Green; break }; Start-Sleep -Seconds 1 }; if (-not $stopped) { Write-Host '⚠️ Server process did not exit within 10s. Force killing process...' -ForegroundColor Yellow; Get-Process mcp-memory-server -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue; Get-NetTCPConnection -LocalPort {{port}} -State Listen -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess | ForEach-Object { Stop-Process -Id $_ -Force -ErrorAction SilentlyContinue }; Start-Sleep -Seconds 2 } }
-& { $token = Get-Content "$env:USERPROFILE\.gemini\mcp_memory\admin.token" -ErrorAction SilentlyContinue; if ($token) { $hdr = @{ Authorization = "Bearer $($token.Trim())" }; $res = Invoke-RestMethod -Uri "http://127.0.0.1:{{port}}/shutdown" -Method Post -Headers $hdr -ErrorAction SilentlyContinue } else { $res = Invoke-RestMethod -Uri "http://127.0.0.1:{{port}}/shutdown" -Method Post -ErrorAction SilentlyContinue }; if ($res) { Write-Host "✅ Shutdown acknowledged with version details:" -ForegroundColor Green; $res | ConvertTo-Json -Depth 5 }; $procs = @(Get-Process mcp-memory-server -ErrorAction SilentlyContinue); if ($procs.Count -gt 0) { foreach ($p in $procs) { $exited = $p.WaitForExit(10000); if (-not $exited) { Write-Host "⚠️ Process $($p.Id) did not exit within 10s. Force killing..." -ForegroundColor Yellow; $p | Stop-Process -Force -ErrorAction SilentlyContinue } } }; $conns = Get-NetTCPConnection -LocalPort {{port}} -State Listen -ErrorAction SilentlyContinue; if ($conns) { $conns | Select-Object -ExpandProperty OwningProcess | ForEach-Object { Stop-Process -Id $_ -Force -ErrorAction SilentlyContinue } } }
@Write-Host "✅ Shutdown complete. Port {{port}} and process are free." -ForegroundColor Green
# Start the server in the background and verify /health
@@ -125,12 +125,12 @@ deploy-wsl: deploy-stub deploy-nvim deploy-rules-wsl
# Deploy WSL-native stub binary (skips if identical checksum)
deploy-stub:
@Write-Host "Deploying WSL Stub natively..." -ForegroundColor Cyan
wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && src="target/release/mcp-memory-stub" && dest="/home/riz/.local/bin/mcp-memory-stub" && if [ -f "$dest" ] && cmp -s "$src" "$dest"; then echo -e "\033[33mSkipping $dest (identical checksum).\033[0m"; else cp "$src" "$dest" && echo -e "\033[32mCopied updated $src to $dest\033[0m"; fi'
wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && src="target/release/mcp-memory-stub" && dest="/home/riz/.local/bin/mcp-memory-stub" && if [ -f "$dest" ] && cmp -s "$src" "$dest"; then echo -e "\033[33mSkipping $dest (identical checksum).\033[0m"; else cp --remove-destination "$src" "$dest" && echo -e "\033[32mCopied updated $src to $dest\033[0m"; fi'
# Deploy WSL-native Neovim plugin binary (skips if identical checksum)
deploy-nvim:
@Write-Host "Deploying WSL Nvim natively..." -ForegroundColor Cyan
wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && src="target/release/mcp-memory-nvim" && dest="/home/riz/.local/bin/mcp-memory-nvim" && if [ -f "$dest" ] && cmp -s "$src" "$dest"; then echo -e "\033[33mSkipping $dest (identical checksum).\033[0m"; else cp "$src" "$dest" && echo -e "\033[32mCopied updated $src to $dest\033[0m"; fi'
wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && src="target/release/mcp-memory-nvim" && dest="/home/riz/.local/bin/mcp-memory-nvim" && if [ -f "$dest" ] && cmp -s "$src" "$dest"; then echo -e "\033[33mSkipping $dest (identical checksum).\033[0m"; else cp --remove-destination "$src" "$dest" && echo -e "\033[32mCopied updated $src to $dest\033[0m"; fi'
# Deploy global Agent rules to WSL config directory (skips identical files)
deploy-rules-wsl:
@@ -191,8 +191,8 @@ all-nvim-wsl: build-nvim deploy-nvim
# Complete lifecycle: stop, build, deploy, start, and verify WSL server
all-server-wsl port="3000": (stop port) build-server deploy-server (start port) (verify port)
# Install git post-commit hook for automatic ADR/Task/TechDebt state reconciliation
# Install git hooks (post-commit reconciliation and pre-push gatekeeper)
install-git-hooks:
@Write-Host "Installing git post-commit reconciliation hook..." -ForegroundColor Cyan
-& { $hookContent = "#!/bin/sh`npython3 scripts/git-reconcile.py 2>/dev/null || python scripts/git-reconcile.py 2>/dev/null || true`n"; $hookPath = ".git\hooks\post-commit"; [System.IO.File]::WriteAllText($hookPath, $hookContent); Write-Host "✅ Git post-commit hook installed to $hookPath" -ForegroundColor Green }
@Write-Host "Installing git post-commit and pre-push hooks..." -ForegroundColor Cyan
-& { $postCommitContent = "#!/bin/sh`npython3 scripts/git-reconcile.py 2>/dev/null || python scripts/git-reconcile.py 2>/dev/null || true`n"; [System.IO.File]::WriteAllText(".git\hooks\post-commit", $postCommitContent); $prePushContent = "#!/bin/sh`nif command -v python3 >/dev/null 2>&1; then`n exec python3 scripts/git-pre-push.py `"`$@`"`nelse`n exec python scripts/git-pre-push.py `"`$@`"`nfi`n"; [System.IO.File]::WriteAllText(".git\hooks\pre-push", $prePushContent); Write-Host "✅ Git post-commit and pre-push hooks installed." -ForegroundColor Green }
-74
View File
@@ -1,74 +0,0 @@
rebuild_index: found 229 entities, 24 tasks
spawn_blocking started in rebuild_index
add_task_sync called for task: 8da665f7-107c-41c8-b203-d39a9ff1f9a5
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 01a788f0-7adb-456c-bf37-02bbd7e1d2f8
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 5e8d9b26-ae31-4136-a4c4-6ead74887bb5
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: ed184dd6-55ce-4d37-a06a-b34733795e03
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 3cea859d-c1ce-4f6c-bdc5-6eb65dbd8e4f
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 62446699-39b3-496b-9af8-99b331fe6c08
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 4b0e2179-1e70-4b64-9c2d-ff2fe2d05fdf
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 16dd35e4-eadd-4597-a5b6-9844852cfb76
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: c85f352b-f15b-4f90-a6ae-18fe914212b2
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: d1e907a2-89a2-4814-8722-22c754cc943c
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: c382a511-a5d6-4114-ad2c-f09fdd36d66a
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 42ca3765-5c69-4cdd-8324-5a174c34d4ae
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 05792eaa-4bc5-45e5-b9a0-d9027b3b3974
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 15c9fd58-8d8f-4c2f-80f4-15a46ba54588
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 9b55e54c-ac1b-4219-acf3-68f7cb34a1fd
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: a04923ea-6abf-41ba-9c45-f7d9cdac3440
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 4233293e-6d93-4c46-b9aa-f9e712af0e86
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 93451cd9-25c5-4323-8dce-79bba124242d
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 949f1d59-e1e2-421c-9682-43a79f41679b
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: cd680df7-2e1a-4486-bb9b-3881476c762e
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 561daa2b-ddb5-44ae-8fb8-8155405759fb
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: e36b0eba-aa79-493f-b89b-3da46a44fa11
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: a3a717b3-438a-42a7-83ec-2f2eb5d775b4
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 6a1b05d5-0ffd-4b0c-bb65-f45e0cb5540a
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
+1 -1
View File
@@ -23,7 +23,7 @@ pub async fn read_mcp_message<R: tokio::io::AsyncRead + Unpin>(
break;
}
if trimmed_line.as_bytes().len() >= 15
if trimmed_line.len() >= 15
&& trimmed_line.as_bytes()[..15].eq_ignore_ascii_case(b"content-length:")
{
length = trimmed_line[15..].trim().parse().unwrap_or(0);
-53
View File
@@ -1,53 +0,0 @@
# MCP Tools Review & Enhancement Strategy
## Part 1: Current Arsenal Review
Our current MCP ecosystem is highly advanced, utilizing a **Dual-Transport Leader/Stub Architecture** (Windows Host + WSL Proxy) to completely eliminate cross-OS I/O latency.
### 1. Context & Token Optimization
* `read_file_skeleton`: Highly effective. Uses tree-sitter to extract ASTs (Rust, Python, TS). **Score: A+ (Massive token savings)**
* `get_active_worktree_context`: Native git2 integration. Bypasses shell parsing for clean JSON diffs. **Score: A**
* `process_logs`: Direct file seeking and daemon log management (`watch`, `get`, `clear`). Prevents LLMs from reading multi-megabyte log files. **Score: A**
### 2. Neovim IDE Integration (nvim-core)
* `nvim_buffer`, `nvim_window`, `nvim_view`, `nvim_diagnostics`, `nvim_visual`, `nvim_execute_lua`, `nvim_system`.
* **Review:** Exceptional human QoL. The agent interacts with the code where the human's eyes actually are. Ghost text and diagnostic extmarks provide an IDE-like experience usually reserved for closed-source tools like Cursor. **Score: S-Tier**
### 3. Clipboard & Workflow
* `clipboard` (`read`, `write`).
* **Review:** Native cross-OS clipboard-win and arboard implementation with on-demand image grab. Bridges the gap between manual human research and the agent's context. **Score: A**
### 4. Graph & Memory Management
* `create_entities`, `hypotheses`, `agent_signals`, `handoff_routine`.
* **Review:** Solid foundation for state persistence across branches and days. **Score: A**
---
## Part 2: Proposed Enhancements (Focus: T2R, Token Cost, QoL)
To push the system to the absolute bleeding edge of autonomous coding, I propose the following 5 new tools/enhancements.
### 1. `replace_ast_node` (Robust Structural Editing)
* **The Problem:** Standard text replacement uses exact string matching and line numbers. Line numbers change when humans edit simultaneously, and string matching fails on whitespace/indentation.
* **The Solution:** An MCP tool that takes `(file_path, node_type, node_name, new_content)`. It uses tree-sitter to find the exact boundary of `fn execute(...)` and replaces just that AST node.
* **Impact:** Zero LLM syntax/indentation errors. 100% robust edits. Drastically lowers Time-to-Resolve (T2R) by eliminating failed edit loops.
### 2. `semantic_code_search` (Local Vector Embeddings)
* **The Problem:** Text search relies on exact regex. If the LLM guesses the wrong variable name, it wastes tokens searching and reading the wrong files.
* **The Solution:** Using Tantivy and BERT embeddings in our backend. We index the AST blocks of the codebase in the background. The LLM can query *"Where is the auth token validated?"* and get the exact 3 relevant functions instantly.
* **Impact:** Massive token cost reduction (no blind file reading). Instant T2R for codebase exploration.
### 3. `nvim_system` terminal execution (Interactive Execution QoL)
* **The Problem:** When the agent runs a background terminal command (`cargo build`, `npm run dev`), the output is hidden from the human, and interactive prompts cause the background task to hang indefinitely.
* **The Solution:** Dispatch to Neovim terminal splits where the human can watch the tests run natively, interact with prompts, see ANSI colors, and interact seamlessly.
* **Impact:** Massive Human QoL.
### 4. `read_directory_architecture` (Bird's-Eye View)
* **The Problem:** Single file inspection works for one file. When entering a new repository, the LLM usually runs `ls -R` and then has to guess what files do based on their names.
* **The Solution:** A tool that scans a directory structure and returns a clean hierarchical tree alongside summaries of what each directory and key file is responsible for.
* **Impact:** Immediate holistic context. Eliminates the "exploration phase" token tax.
### 5. `query_database_schema` (Introspection)
* **The Problem:** Working with databases usually involves the LLM writing clunky scripts to view table definitions, which often fail due to missing env vars or wrong dialects.
* **The Solution:** A direct MCP tool that parses the local `.env`, connects to the database (PostgreSQL), and returns a clean Markdown representation of the schema (Tables, Columns, Types, Foreign Keys).
* **Impact:** Prevents hallucinations about database structure. Fixes DB-related bugs significantly faster (T2R).
+3
View File
@@ -17,3 +17,6 @@ mcp-stdio = { version = "1.0.0", path = "../mcp-stdio" }
bytes = "1.12.1"
dashmap = "6.2.1"
[build-dependencies]
chrono = "0.4.45"
+20 -3
View File
@@ -1,22 +1,39 @@
use std::process::Command;
fn main() {
let git_hash = Command::new("git")
let mut git_hash = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.unwrap_or_else(|| "unknown".to_string());
git_hash = git_hash.trim().to_string();
let is_dirty = Command::new("git")
.args(["status", "--porcelain"])
.output()
.is_ok_and(|out| !out.stdout.is_empty());
if is_dirty {
let dirty_ts = chrono::Local::now().format("%y.%m.%d.%H%M%S").to_string();
git_hash.push_str(&format!("-dirty-{dirty_ts}"));
}
let git_date = Command::new("git")
.args(["log", "-1", "--format=%cd", "--date=format:%y.%m.%d.%H%M%S"])
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.unwrap_or_else(|| "unknown".to_string());
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| chrono::Local::now().format("%y.%m.%d.%H%M%S").to_string());
let version = format!("{} ({})", git_date.trim(), git_hash.trim());
let version = format!("v{git_date} ({git_hash})");
println!("cargo:rustc-env=APP_VERSION={version}");
println!("cargo:rerun-if-changed=../.git/HEAD");
println!("cargo:rerun-if-changed=../.git/index");
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=build.rs");
}
+12 -39
View File
@@ -2,48 +2,22 @@
When connected to this Neovim MCP server (`win-nvim`), you have powerful tools to interact directly with the active Neovim editor.
## The Consolidated Tool Arsenal (v2)
The server consolidates granular Neovim operations into 7 smart domain tools:
- **`nvim_buffer`**: Buffer and file management.
- `action: "open_file"`: Open file in buffer (args: `file`, `line`, `col`).
- `action: "open"`: Open buffer (args: `bufnr`).
- `action: "close"`: Close buffer (args: `bufnr`, `force`).
- `action: "reload"`: Reload buffer from disk (args: `bufnr`).
- `action: "save"`: Save buffer to disk (args: `bufnr`).
- `action: "list"`: List all loaded buffers.
- **`nvim_window`**: Window split and focus management.
- `action: "split"`: Split window (args: `direction: "horizontal" | "vertical"`, `file`).
- `action: "close"`: Close window (args: `winnr`).
- `action: "list"`: List open windows.
- `action: "get_active"`: Get active window details.
- `action: "set_active"`: Set active window focus (args: `winnr`).
- **`nvim_view`**: Editor viewport and navigation.
- `action: "get_active_buffer"`: Get active buffer details.
- `action: "get_cursor"`: Get current cursor line/col.
- `action: "goto_line"`: Jump cursor to line (args: `line`, `col`).
- `action: "get_viewport"`: Get visible line range in viewport.
- `action: "get_messages"`: Get Neovim command-line messages.
- **`nvim_diagnostics`**: LSP diagnostics querying and publishing.
- `action: "get"`: Get diagnostics (args: `bufnr`, `severity`).
- `action: "set"`: Set buffer diagnostics (args: `bufnr`, `diagnostics`).
- **`nvim_visual`**: Visual highlighting, extmarks, and quickfix.
- `action: "get_selection"`: Get current visual selection text and range.
- `action: "highlight_lines"`: Highlight line ranges (args: `bufnr`, `hl_group`, `start_line`, `end_line`).
- `action: "set_extmark"`: Place virtual text or sign extmarks (args: `bufnr`, `ns_id`, `line`, `col`, `opts`).
- `action: "set_quickfix"`: Populate quickfix list (args: `items`, `title`).
- **`nvim_execute_lua`**: God Mode arbitrary Lua evaluation.
- Arguments: `code: String`.
- **`nvim_system`**: System diagnostics and connection heartbeat.
- `action: "ping"`: Heartbeat test.
- `action: "status"`: Server and socket bridge health status.
## The Consolidated Tool Arsenal (v3)
The server consolidates granular Neovim operations into 5 smart mega-tools:
- **`nvim_buffer`**: Buffer and file management. Actions: `read`, `replace`, `save`, `undo`, `redo`, `create_scratch`.
- **`nvim_workspace`**: Window split and focus management. Actions: `list_buffers`, `list_windows`, `focus`, `split`, `cwd`.
- **`nvim_intelligence`**: Code intelligence and LSP. Actions: `hover`, `definition`, `references`, `outline`, `query`, `diagnostics`, `rename`, `code_action`.
- **`nvim_ui`**: Visual highlighting, diff previews, and ghost text. Actions: `highlight`, `ghost_text`, `clear`.
- **`nvim_exec`**: Escape hatch for raw evaluation. Actions: `lua`, `vimscript`, `terminal`.
## 1. Using Consolidated Domain Tools First
Always prefer the specific consolidated tools (like `nvim_buffer`, `nvim_window`, `nvim_visual`, etc.) over writing raw Lua scripts. These tools are strongly typed, tested, and safe.
Always prefer the specific consolidated tools (like `nvim_buffer`, `nvim_workspace`, `nvim_ui`, etc.) over writing raw Lua scripts. These tools are strongly typed, tested, and safe.
## 2. Lua God Mode (`nvim_execute_lua`)
If you need to access *any* Neovim API that does not have a dedicated tool (e.g., complex buffer edits, changing options, custom LSP interactions), you MUST use `nvim_execute_lua` as your escape hatch.
## 2. Lua God Mode (`nvim_exec` with action `lua`)
If you need to access *any* Neovim API that does not have a dedicated tool (e.g., changing options, setting autocmds), you MUST use `nvim_exec` with action `lua` as your escape hatch.
**CRITICAL**: `nvim_exec` is restricted to **READ-ONLY** queries. Do NOT use it to mutate editor state.
### CRITICAL RULES for `nvim_execute_lua`:
### CRITICAL RULES for `nvim_exec` (`lua`):
1. **Never Block:** Never use interactive prompt functions or interactive confirmation flags in substitutions (e.g., `%s/old/new/gc`). This will cause the headless MCP bridge to deadlock forever.
2. **Visual Feedback:** Always trigger a notification using `require("notify")("Antigravity: [Action]", "info", { title = "Antigravity" })`.
3. **Auto-Save:** If you modify a file buffer, always save it using `vim.cmd('write')` within the same Lua script so external tools can see the changes, unless you explicitly want to pause for manual human review.
@@ -57,4 +31,3 @@ While the headless background instance is great for autonomous, routine tasks, i
## 4. Tool Schema Discovery
Do **NOT** grep or search the Rust source code to find tool schemas or arguments. All lazy-loaded MCP tool schemas are automatically cached as JSON files on your disk. To understand a tool's arguments, directly read `~/.gemini/antigravity-cli/mcp/win-nvim/<tool_name>.json` (or linux-nvim).
+597 -768
View File
File diff suppressed because it is too large. Load diff
-1
View File
@@ -1 +0,0 @@
{"code": "local buf = vim.fn.bufnr('server/src/search.rs')\nif buf == -1 then\n vim.cmd('e server/src/search.rs')\n buf = vim.api.nvim_get_current_buf()\nend\n\nvim.api.nvim_buf_set_lines(buf, 216, 222, false, {\n ' &self,',\n ' entities: &[Entity],',\n ' tasks: &[Task],',\n ' snippets: &[Snippet],',\n ' adrs: &[Adr],'\n})\n\n-- We need to change the loop variables inside the task from values to clones if they are passed as slices\n-- Actually we can just clone the slice data before moving it into spawn_blocking\nvim.api.nvim_buf_set_lines(buf, 222, 223, false, {\n ' ) -> tokio::task::JoinHandle<tantivy::Result<()>> {',\n ' let entities = entities.to_vec();',\n ' let tasks = tasks.to_vec();',\n ' let snippets = snippets.to_vec();',\n ' let adrs = adrs.to_vec();'\n})\n\nvim.cmd('write')\nrequire('notify')('Updated search index_batch signature', 'info', { title = 'Antigravity' })\nreturn 'ok'\n"}
+204
View File
@@ -0,0 +1,204 @@
#!/usr/bin/env python3
"""
Pre-Push Gatekeeper for Git and MCP Memory.
Enforces workspace quality gates:
1. Allows tag pushes and branch deletions without verification.
2. Bypasses tests for docs/config-only modifications (.md, .txt, .png, justfile, etc.).
3. Queries mcp-memory gate API (/gate/verify?action=push&target=<branch>).
4. If no pre-authorized gate is found, runs project tests locally to verify 100% pass before allowing push.
"""
import os
import sys
import json
import subprocess
import urllib.request
import urllib.error
DOC_EXTENSIONS = {
".md", ".txt", ".png", ".jpg", ".jpeg", ".pdf", ".svg",
".drawio", ".gif", ".ico", ".csv"
}
DOC_FILENAMES = {
"justfile", "jenkinsfile", "dockerfile", "license",
"changelog", ".gitignore", ".gitattributes"
}
def is_doc_or_config(filepath: str) -> bool:
base = os.path.basename(filepath).lower()
if base in DOC_FILENAMES:
return True
ext = os.path.splitext(base)[1].lower()
return ext in DOC_EXTENSIONS
def run_git(cmd):
try:
res = subprocess.run(["git"] + cmd, capture_output=True, text=True, check=True)
return res.stdout.strip()
except Exception:
return ""
def check_mcp_gate(port: str, branch: str):
"""
Returns (status_code, reason)
status_code: 200 (Authorized), 403 (Blocked), 404 (Not found), 0 (Server unreachable)
"""
url = f"http://127.0.0.1:{port}/gate/verify?action=push&target={branch}"
try:
req = urllib.request.Request(url, method="GET")
with urllib.request.urlopen(req, timeout=1.5) as resp:
return resp.status, "Authorized"
except urllib.error.HTTPError as e:
body = ""
try:
body = e.read().decode("utf-8")
except Exception:
pass
return e.code, body
except Exception:
return 0, "Server unreachable"
def set_mcp_gate(port: str, branch: str, authorize: bool, reason: str):
url = f"http://127.0.0.1:{port}/gate/set"
payload = {
"action": "push",
"target": branch,
"authorize": authorize,
"reason": reason
}
try:
data = json.dumps(payload).encode("utf-8")
req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"}, method="POST")
with urllib.request.urlopen(req, timeout=1.5):
pass
except Exception:
pass
def post_mcp_event(port: str, topic: str, payload: dict):
url = f"http://127.0.0.1:{port}/api/events/post"
try:
data = json.dumps({"topic": topic, "payload": payload}).encode("utf-8")
req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"}, method="POST")
with urllib.request.urlopen(req, timeout=1.0):
pass
except Exception:
pass
def wait_for_mcp_decision(port: str, topic: str, timeout_secs: int = 2):
"""Waits for an operator or auditor decision on the event bus."""
url = f"http://127.0.0.1:{port}/events/wait?topic={topic}&timeout={timeout_secs}"
try:
req = urllib.request.Request(url, method="GET")
with urllib.request.urlopen(req, timeout=timeout_secs + 0.5) as resp:
data = json.loads(resp.read().decode("utf-8"))
payload = data.get("payload", {})
status = str(payload.get("status", "")).lower()
reason = payload.get("reason", "Decision received via event bus")
if status in ("authorized", "approved", "ok", "true"):
return True, reason
elif status in ("blocked", "denied", "rejected", "false"):
return False, reason
except Exception:
pass
return None, ""
def run_local_tests() -> bool:
# 1. Rust workspace
if os.path.exists("Cargo.toml"):
print("\033[36m[Pre-Push Gatekeeper]\033[0m Running cargo test...")
cmd = ["cargo", "test", "--workspace"]
res = subprocess.run(cmd)
return res.returncode == 0
# 2. Python workspace (uv / pytest)
elif os.path.exists("pyproject.toml") or os.path.exists("setup.py"):
print("\033[36m[Pre-Push Gatekeeper]\033[0m Running python tests...")
cmd = ["uv", "run", "pytest"] if os.system("uv --version >nul 2>&1") == 0 else ["pytest"]
res = subprocess.run(cmd)
return res.returncode == 0
return True
def main():
# Read stdin passed by git pre-push: <local ref> <local sha> <remote ref> <remote sha>
lines = sys.stdin.read().splitlines()
if not lines:
sys.exit(0)
port = os.environ.get("MCP_PORT", "3000")
for line in lines:
parts = line.strip().split()
if len(parts) < 4:
continue
local_ref, local_sha, remote_ref, remote_sha = parts[:4]
# 1. Skip deletions
if local_sha == "0000000000000000000000000000000000000000" or local_ref == "(delete)":
continue
# 2. Skip tags
if local_ref.startswith("refs/tags/"):
print(f"\033[33m[Pre-Push Gatekeeper]\033[0m Tag push detected ({local_ref}). Quality gate bypassed.")
continue
# Extract branch name
branch = local_ref.replace("refs/heads/", "")
# 3. Check for documentation / config-only changes
zero_sha = "0000000000000000000000000000000000000000"
diff_range = local_sha if remote_sha == zero_sha else f"{remote_sha}..{local_sha}"
changed_files_raw = run_git(["diff", "--name-only", diff_range])
changed_files = [f.strip() for f in changed_files_raw.splitlines() if f.strip()]
if changed_files and all(is_doc_or_config(f) for f in changed_files):
print(f"\033[33m[Pre-Push Gatekeeper]\033[0m All modified files are docs/config-only. Quality gate bypassed.")
continue
# 4. Query MCP Memory gate status
status, reason = check_mcp_gate(port, branch)
if status == 200:
print(f"\033[32m[Pre-Push Gatekeeper]\033[0m \u2714 Push AUTHORIZED by MCP Memory gate for branch '{branch}'.")
continue
elif status == 403:
print(f"\033[31m[Pre-Push Gatekeeper]\033[0m \u274c Push BLOCKED by MCP Memory gate for branch '{branch}'!")
if reason:
print(f" Reason: {reason}")
sys.exit(1)
# 5. No gate found (404 or server offline) -> Request interactive approval or verify unit tests
if status != 0:
post_mcp_event(port, "gate:approval_requested", {
"branch": branch,
"local_sha": local_sha,
"remote_sha": remote_sha,
"changed_files_count": len(changed_files)
})
# Fast check: Did dashboard / operator / auditor approve or reject via event bus?
decision, dec_reason = wait_for_mcp_decision(port, f"gate:decision:{branch}", timeout_secs=1)
if decision is True:
print(f"\033[32m[Pre-Push Gatekeeper]\033[0m \u2714 Push INTERACTIVELY AUTHORIZED via event bus: {dec_reason}")
set_mcp_gate(port, branch, True, dec_reason)
post_mcp_event(port, "gate:event", {"status": "authorized", "active_gate": "authorized", "branch": branch})
continue
elif decision is False:
print(f"\033[31m[Pre-Push Gatekeeper]\033[0m \u274c Push BLOCKED by operator: {dec_reason}")
set_mcp_gate(port, branch, False, dec_reason)
post_mcp_event(port, "gate:event", {"status": "blocked", "active_gate": "blocked", "branch": branch})
sys.exit(1)
print(f"\033[33m[Pre-Push Gatekeeper]\033[0m No pre-authorized gate record for '{branch}'. Verifying unit tests...")
if run_local_tests():
print(f"\033[32m[Pre-Push Gatekeeper]\033[0m \u2714 Unit tests passed 100%. Push authorized.")
if status != 0:
set_mcp_gate(port, branch, True, "Unit tests passed locally via pre-push gatekeeper")
post_mcp_event(port, "gate:event", {"status": "authorized", "active_gate": "authorized", "branch": branch})
else:
print(f"\033[31m[Pre-Push Gatekeeper]\033[0m \u274c Push BLOCKED! Unit tests failed locally. Fix failing tests before pushing.")
if status != 0:
set_mcp_gate(port, branch, False, "Unit tests failed during pre-push gatekeeper check")
post_mcp_event(port, "gate:event", {"status": "blocked", "active_gate": "blocked", "branch": branch})
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()
+45
View File
@@ -0,0 +1,45 @@
#!/bin/bash
# Native shell pre-push hook using UDP Rendezvous
PORT=${MCP_UDP_PORT1:-3001}
while read local_ref local_sha remote_ref remote_sha; do
if [ "$local_sha" = "0000000000000000000000000000000000000000" ] || [ "$local_ref" = "(delete)" ]; then
continue
fi
if [[ "$local_ref" == refs/tags/* ]]; then
continue
fi
BRANCH=${local_ref#refs/heads/}
echo -e "\033[36m[Pre-Push Gatekeeper]\033[0m Checking gate for branch '$BRANCH'..."
# Check if gate is already authorized or blocked
if mcp-memory-server gate verify --action push --target "$BRANCH" --consume 2>/dev/null; then
echo -e "\033[32m[Pre-Push Gatekeeper]\033[0m Push AUTHORIZED by MCP Memory gate for branch '$BRANCH'."
continue
else
EXIT_CODE=$?
if [ $EXIT_CODE -eq 1 ]; then
echo -e "\033[31m[Pre-Push Gatekeeper]\033[0m Push BLOCKED by MCP Memory gate for branch '$BRANCH'."
exit 1
fi
# If exit code is 2 (Not Found), we wait via UDP Rendezvous
echo -e "\033[33m[Pre-Push Gatekeeper]\033[0m No pre-authorized gate for '$BRANCH'. Waiting for approval via UDP rendezvous..."
# Payload for UDP
PAYLOAD="{\"type\":\"gate_wait\",\"action\":\"push\",\"target\":\"$BRANCH\"}"
if udp-send --wait "$PORT" "$PAYLOAD"; then
echo -e "\033[32m[Pre-Push Gatekeeper]\033[0m Push AUTHORIZED via UDP rendezvous for branch '$BRANCH'."
continue
else
echo -e "\033[31m[Pre-Push Gatekeeper]\033[0m Push DENIED or timed out via UDP rendezvous for branch '$BRANCH'."
exit 1
fi
fi
done
exit 0
-74
View File
@@ -1,74 +0,0 @@
rebuild_index: found 228 entities, 24 tasks
spawn_blocking started in rebuild_index
add_task_sync called for task: 8da665f7-107c-41c8-b203-d39a9ff1f9a5
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 01a788f0-7adb-456c-bf37-02bbd7e1d2f8
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 5e8d9b26-ae31-4136-a4c4-6ead74887bb5
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: ed184dd6-55ce-4d37-a06a-b34733795e03
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 3cea859d-c1ce-4f6c-bdc5-6eb65dbd8e4f
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 62446699-39b3-496b-9af8-99b331fe6c08
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 4b0e2179-1e70-4b64-9c2d-ff2fe2d05fdf
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 16dd35e4-eadd-4597-a5b6-9844852cfb76
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: c85f352b-f15b-4f90-a6ae-18fe914212b2
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: d1e907a2-89a2-4814-8722-22c754cc943c
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: c382a511-a5d6-4114-ad2c-f09fdd36d66a
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 42ca3765-5c69-4cdd-8324-5a174c34d4ae
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 05792eaa-4bc5-45e5-b9a0-d9027b3b3974
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 15c9fd58-8d8f-4c2f-80f4-15a46ba54588
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 9b55e54c-ac1b-4219-acf3-68f7cb34a1fd
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: a04923ea-6abf-41ba-9c45-f7d9cdac3440
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 4233293e-6d93-4c46-b9aa-f9e712af0e86
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 93451cd9-25c5-4323-8dce-79bba124242d
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 949f1d59-e1e2-421c-9682-43a79f41679b
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: cd680df7-2e1a-4486-bb9b-3881476c762e
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 561daa2b-ddb5-44ae-8fb8-8155405759fb
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: e36b0eba-aa79-493f-b89b-3da46a44fa11
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: a3a717b3-438a-42a7-83ec-2f2eb5d775b4
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
add_task_sync called for task: 6a1b05d5-0ffd-4b0c-bb65-f45e0cb5540a
Writer add_document returned id/result
Needs_commit set to true in add_task_sync
+18 -1
View File
@@ -48,6 +48,10 @@ ignore = "0.4.33"
notify = "7"
blake3 = "1.8"
tokio-stream = { version = "0.1", features = ["sync"] }
chrono = { version = "0.4.45", features = ["serde"] }
ocrs = "0.13.1"
rten = "0.26.0"
serde_yaml = "0.9.34"
[build-dependencies]
chrono = "0.4.45"
@@ -58,4 +62,17 @@ tempfile = "3.27.0"
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.59.0", features = ["Win32_System_DataExchange", "Win32_System_Memory"] }
windows-sys = { version = "0.59.0", features = [
"Win32_System_DataExchange",
"Win32_System_Memory",
"Win32_UI_WindowsAndMessaging",
"Win32_Foundation",
"Win32_System_Com",
] }
windows = { version = "0.62.2", features = [
"Foundation",
"Foundation_Collections",
"Media_Ocr",
"Graphics_Imaging",
"Storage_Streams",
] }
+14 -3
View File
@@ -16,15 +16,26 @@ fn main() {
.is_ok_and(|out| !out.stdout.is_empty());
if is_dirty {
git_hash.push_str("-dirty");
let dirty_ts = chrono::Local::now().format("%y.%m.%d.%H%M%S").to_string();
git_hash.push_str(&format!("-dirty-{dirty_ts}"));
}
let build_date = chrono::Local::now().format("%y.%m.%d.%H%M%S").to_string();
let git_date = Command::new("git")
.args(["log", "-1", "--format=%cd", "--date=format:%y.%m.%d.%H%M%S"])
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| chrono::Local::now().format("%y.%m.%d.%H%M%S").to_string());
let version = format!("{build_date} ({git_hash})");
let version = format!("v{git_date} ({git_hash})");
println!("cargo:rustc-env=APP_VERSION={version}");
println!("cargo:rerun-if-changed=../.git/HEAD");
println!("cargo:rerun-if-changed=../.git/index");
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=build.rs");
if std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "windows" {
let mut res = winres::WindowsResource::new();
+69 -19
View File
@@ -14,33 +14,83 @@ pub async fn wait_for_event_handler(
) -> impl IntoResponse {
let topic = params.get("topic").cloned();
let session_id = params.get("session_id").cloned();
let timeout_duration = params
.get("timeout_ms")
.and_then(|ms| ms.parse::<u64>().ok())
.map(tokio::time::Duration::from_millis)
.or_else(|| {
params
.get("timeout")
.and_then(|secs| secs.parse::<u64>().ok())
.map(tokio::time::Duration::from_secs)
});
let mut rx = state.handler.state.event_bus_tx.subscribe();
loop {
match rx.recv().await {
Ok(event) => {
let topic_matches = topic.as_ref().is_none_or(|t| t == &event.topic);
let session_matches = session_id
.as_ref()
.is_none_or(|s| Some(s) == event.session_id.as_ref());
let wait_future = async {
loop {
match rx.recv().await {
Ok(event) => {
let topic_matches = topic.as_ref().is_none_or(|t| t == &event.topic);
let session_matches = session_id
.as_ref()
.is_none_or(|s| Some(s) == event.session_id.as_ref());
if topic_matches && session_matches {
return axum::Json(event);
if topic_matches && session_matches {
return Ok(event);
}
}
Err(tokio::sync::broadcast::error::RecvError::Lagged(skipped)) => {
tracing::warn!(
"Event bus receiver lagged by {} messages; continuing wait.",
skipped
);
continue;
}
Err(tokio::sync::broadcast::error::RecvError::Closed) => {
return Err("Event bus closed");
}
}
Err(tokio::sync::broadcast::error::RecvError::Lagged(skipped)) => {
tracing::warn!("Event bus receiver lagged by {} messages; continuing wait.", skipped);
continue;
}
Err(tokio::sync::broadcast::error::RecvError::Closed) => {
return axum::Json(GenericEvent {
}
};
match timeout_duration {
Some(dur) => match tokio::time::timeout(dur, wait_future).await {
Ok(Ok(event)) => (axum::http::StatusCode::OK, axum::Json(event)).into_response(),
Ok(Err(err)) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(GenericEvent {
topic: "error".to_string(),
session_id: None,
payload: serde_json::json!({"error": "Event bus closed"}),
});
}
}
payload: serde_json::json!({ "error": err }),
}),
)
.into_response(),
Err(_) => (
axum::http::StatusCode::REQUEST_TIMEOUT,
axum::Json(GenericEvent {
topic: "timeout".to_string(),
session_id: None,
payload: serde_json::json!({
"error": "timed out waiting for event",
"topic": topic
}),
}),
)
.into_response(),
},
None => match wait_future.await {
Ok(event) => (axum::http::StatusCode::OK, axum::Json(event)).into_response(),
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(GenericEvent {
topic: "error".to_string(),
session_id: None,
payload: serde_json::json!({ "error": err }),
}),
)
.into_response(),
},
}
}
+22 -1
View File
@@ -104,9 +104,30 @@ pub async fn gate_set_handler(
};
app_state.handler.state.env.gates.modify(|gates| {
gates.retain(|g| !(g.action == record.action && g.target == record.target));
gates.push(record);
gates.push(record.clone());
});
let summary = format!("Push gate {} set to {}", record.target, record.status);
app_state
.handler
.state
.record_activity("gate", &summary, record.reason.as_deref());
let _ = app_state
.handler
.state
.event_bus_tx
.send(crate::state::GenericEvent {
topic: "gate:event".to_string(),
session_id: None,
payload: serde_json::json!({
"action": record.action,
"target": record.target,
"status": record.status,
"reason": record.reason,
"active_gate": record.status,
}),
});
Ok((axum::http::StatusCode::OK, "Gate state updated."))
}
-293
View File
@@ -1,293 +0,0 @@
if let Some(idx) = gates.iter().position(|g| {
g.action == q.action
&& g.target == q.target
&& g.namespace == q.namespace
&& g.params == q.params
}) {
found = Some(gates[idx].clone());
if q.consume {
to_remove = Some(idx);
}
}
if let Some(idx) = to_remove {
gates.remove(idx);
}
});
match found {
Some(record) => {
if record.status == "authorized" {
(axum::http::StatusCode::OK, "Authorized").into_response()
} else {
let msg = if let Some(r) = record.reason {
format!("Action blocked. Reason: {}", r)
} else {
"Action blocked.".to_string()
};
(axum::http::StatusCode::FORBIDDEN, msg).into_response()
}
}
None => (
axum::http::StatusCode::NOT_FOUND,
"Action not yet authorized (no gate record found).",
)
.into_response(),
}
}
async fn gate_set_handler(
State(app_state): State<Arc<AppState>>,
Json(body): Json<GateSetReq>,
) -> axum::response::Response {
let status = if body.block.unwrap_or(false) {
"blocked".to_string()
} else if body.authorize.unwrap_or(false) {
"authorized".to_string()
} else {
"pending".to_string()
};
let record = GateRecord {
id: uuid::Uuid::new_v4().to_string(),
action: body.action.clone(),
target: body.target.clone(),
namespace: body.namespace.clone(),
params: body.params.clone(),
status,
reason: body.reason.clone(),
timestamp: crate::handlers::utils::now_secs(),
};
app_state.handler.state.gates.modify(|gates| {
gates.retain(|g| !(g.action == record.action && g.target == record.target));
gates.push(record);
});
(axum::http::StatusCode::OK, "Gate state updated.").into_response()
}
async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::error::Error>> {
state.rebuild_index().await;
tokio::spawn(index_committer_worker(Arc::clone(&state)));
let app_state = Arc::new(AppState {
handler: Arc::new(MemoryHandler::new(Arc::clone(&state))),
clients: RwLock::new(HashMap::new()),
next_id: AtomicUsize::new(1),
});
let app_state_clone = Arc::clone(&app_state);
let mut rx = state.activity_tx.subscribe();
tokio::spawn(async move {
while let Ok(msg) = rx.recv().await {
let senders: Vec<_> = app_state_clone
.clients
.read()
.unwrap_or_else(|e| e.into_inner())
.values()
.cloned()
.collect();
for client_tx in senders {
let _ = client_tx.try_send(msg.clone());
}
}
});
let app = Router::new()
.route(
"/api/version",
get(|| async move {
axum::Json(serde_json::json!({
"version": env!("APP_VERSION"),
"git_hash": option_env!("GIT_HASH").unwrap_or("unknown")
}))
}),
)
.route("/ws", get(ws_handler))
.route("/health", get(health_handler))
.route("/nvim/telemetry", post(nvim_telemetry_handler))
.route("/gate/verify", get(gate_verify_handler))
.route("/gate/set", post(gate_set_handler))
.route(
"/shutdown",
post(
|headers: axum::http::HeaderMap, State(state): State<Arc<AppState>>| async move {
let token_path = state.handler.state.base_dir.join("admin.token");
let expected_token = tokio::fs::read_to_string(&token_path)
.await
.unwrap_or_default()
.trim()
.to_string();
let auth_header = headers
.get(axum::http::header::AUTHORIZATION)
.and_then(|h| h.to_str().ok())
.unwrap_or_default();
if expected_token.is_empty() || auth_header != format!("Bearer {}", expected_token) {
return (axum::http::StatusCode::UNAUTHORIZED, "Unauthorized").into_response();
}
std::thread::spawn(|| {
tracing::info!(
"Received shutdown request via /shutdown endpoint. Exiting process cleanly."
);
std::thread::sleep(std::time::Duration::from_millis(100));
std::process::exit(0);
});
(axum::http::StatusCode::OK, "Shutting down...").into_response()
},
),
)
.route(
"/",
get(|| async move { axum::response::Html(include_str!("dashboard.html")) }),
)
.route(
"/api/graph",
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let graph_json = state_clone.read_graph(|g| serde_json::to_string(g).unwrap_or_else(|_| "{}".to_string()));
([(axum::http::header::CONTENT_TYPE, "application/json")], graph_json)
}
}),
)
.route(
"/api/tasks/{id}/complete",
post({
let state_clone = app_state.handler.state.clone();
move |axum::extract::Path(id): axum::extract::Path<String>| async move {
state_clone.tasks.modify(|tasks| {
for t in tasks.iter_mut() {
if t.id == id {
t.status = "completed".to_string();
break;
}
}
});
axum::Json(serde_json::json!({"status": "success"}))
}
}),
)
.route(
"/api/tasks",
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let tasks_json = state_clone.tasks.read_with(|t| serde_json::to_string(t).unwrap_or_else(|_| "[]".to_string()));
([(axum::http::header::CONTENT_TYPE, "application/json")], tasks_json)
}
}),
)
.route(
"/api/sticky",
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let sticky_json = state_clone.sticky.read_with(|s| serde_json::to_string(s).unwrap_or_else(|_| "[]".to_string()));
([(axum::http::header::CONTENT_TYPE, "application/json")], sticky_json)
}
}),
)
.route(
"/api/search",
get({
let state_clone = app_state.handler.state.clone();
move |axum::extract::Query(params): axum::extract::Query<
std::collections::HashMap<String, String>,
>| async move {
if let Some(q) = params.get("q")
&& let Ok(idx) = state_clone.search_index.read()
&& let Ok(results) = idx.search(q, None) {
let mut formatted_results = Vec::new();
for (id, doc_type, title, body, score) in results {
formatted_results.push(serde_json::json!({
"id": id,
"type_name": doc_type,
"title": title,
"content": body,
"score": score
}));
}
return axum::Json(
serde_json::json!({ "results": formatted_results }),
);
}
axum::Json(serde_json::json!({ "results": [] }))
}
}),
)
.route(
"/api/activity",
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let activities_json = state_clone.recent_activities.read_with(|a| serde_json::to_string(a).unwrap_or_else(|_| "[]".to_string()));
([(axum::http::header::CONTENT_TYPE, "application/json")], activities_json)
}
}),
)
.route(
"/api/stats",
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let (entities, relations) = state_clone.read_graph(|g| (g.entities.len(), g.relations.len()));
let tasks = state_clone.tasks.read_with(|items| items.len());
let snippets = state_clone.snippets.read_with(|items| items.len());
let tech_debts = state_clone.tech_debts.read_with(|items| items.len());
let adrs = state_clone.adrs.read_with(|items| items.len());
let ledger = state_clone.ledger.read_with(|items| items.len());
let error_fixes = state_clone.error_fixes.read_with(|items| items.len());
let session_summaries = state_clone.session_summaries.read_with(|items| items.len());
let handoff_memos = state_clone.handoff_memos.read_with(|items| items.len());
let env_fingerprints = state_clone.env_fingerprints.read_with(|items| items.len());
let env_requirements = state_clone.env_requirements.read_with(|items| items.len());
let milestones = state_clone.milestones.read_with(|items| items.len());
let environments = state_clone.environments.read_with(|items| items.len());
let gates = state_clone.gates.read_with(|items| items.len());
axum::Json(serde_json::json!({
"entities": entities,
"relations": relations,
"tasks": tasks,
"snippets": snippets,
"tech_debts": tech_debts,
"adrs": adrs,
"ledger": ledger,
"error_fixes": error_fixes,
"session_summaries": session_summaries,
"handoff_memos": handoff_memos,
"env_fingerprints": env_fingerprints,
"env_requirements": env_requirements,
"milestones": milestones,
"environments": environments,
"gates": gates
}))
}
}),
)
.with_state(app_state);
tracing::info!("MCP Memory Server running on http://127.0.0.1:3000/sse");
let addr = std::env::var("MCP_PORT").unwrap_or_else(|_| "3000".to_string());
let addr: std::net::SocketAddr = format!("127.0.0.1:{}", addr)
.parse()
.expect("Invalid bind address");
let listener = match tokio::net::TcpListener::bind(&addr).await {
Ok(l) => l,
Err(e) => {
let log_path = dirs::home_dir()
.unwrap_or_default()
.join(".gemini/mcp_memory/daemon_error.log");
let _ =
tokio::fs::write(&log_path, format!("Failed to bind to {}: {}\n", addr, e)).await;
return Ok(());
}
};
if let Err(e) = axum::serve(listener, app.into_make_service()).await {
let log_path = dirs::home_dir()
.unwrap_or_default()
.join(".gemini/mcp_memory/daemon_error.log");
let _ = tokio::fs::write(&log_path, format!("Server crashed: {}\n", e)).await;
}
+257 -18
View File
@@ -6,7 +6,7 @@ use axum::{
Router,
extract::State,
response::IntoResponse,
routing::{get, post},
routing::{delete, get, post},
};
use std::sync::Arc;
use tokio_stream::StreamExt;
@@ -123,20 +123,8 @@ pub fn create_router(app_state: Arc<AppState>) -> Router {
}
tracing::info!(
"Received shutdown request via /shutdown endpoint. Creating session checkpoint and initiating graceful shutdown."
"Received shutdown request via /shutdown endpoint. Initiating graceful shutdown."
);
let mem_state = state.handler.state.clone();
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let memo = crate::models::HandoffMemo {
id: format!("chk_shutdown_{}", now),
author: "ShutdownHook".to_string(),
content: format!("Automatic session checkpoint triggered on graceful server shutdown at epoch {}", now),
expires_at: None,
namespace: "global".to_string(),
timestamp: now,
..Default::default()
};
mem_state.telemetry.handoff_memos.modify(|m| m.push(memo));
if let Some(tx) = state.shutdown_tx.lock().unwrap().take() {
let _ = tx.send(());
@@ -154,8 +142,7 @@ pub fn create_router(app_state: Arc<AppState>) -> Router {
"app_version": env!("APP_VERSION"),
"cargo_pkg_version": env!("CARGO_PKG_VERSION"),
"target_os": std::env::consts::OS,
"target_arch": std::env::consts::ARCH,
"shutdown_checkpoint": format!("chk_shutdown_{}", now)
"target_arch": std::env::consts::ARCH
}
})).into_response()
},
@@ -309,6 +296,30 @@ pub fn create_router(app_state: Arc<AppState>) -> Router {
}
}),
)
.route(
"/api/tech_debts/{id}/resolve",
post({
let state_clone = app_state.handler.state.clone();
move |axum::extract::Path(id): axum::extract::Path<String>| async move {
let mut found = false;
state_clone.code.tech_debts.modify(|debts| {
for d in debts.iter_mut() {
if d.id == id {
d.is_resolved = true;
found = true;
break;
}
}
});
if found {
state_clone.record_activity("tech_debt", &format!("Resolved tech debt: {}", id), Some("resolved"));
(axum::http::StatusCode::OK, axum::Json(serde_json::json!({ "status": "success", "message": format!("Tech debt {} resolved", id) })))
} else {
(axum::http::StatusCode::NOT_FOUND, axum::Json(serde_json::json!({ "status": "error", "message": "Tech debt not found" })))
}
}
}),
)
.route(
"/api/adrs",
get({
@@ -398,6 +409,139 @@ pub fn create_router(app_state: Arc<AppState>) -> Router {
}
}),
)
.route(
"/api/env_fingerprints",
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let json = state_clone.env.env_fingerprints.read_with(|items| {
let values: Vec<_> = items.values().cloned().collect();
serde_json::to_string(&values).unwrap_or_else(|_| "[]".to_string())
});
([(axum::http::header::CONTENT_TYPE, "application/json")], json)
}
}),
)
.route(
"/api/env_requirements",
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let json = state_clone.env.env_requirements.read_with(|items| {
serde_json::to_string(items).unwrap_or_else(|_| "[]".to_string())
});
([(axum::http::header::CONTENT_TYPE, "application/json")], json)
}
}),
)
.route(
"/api/environments",
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let json = state_clone.env.environments.read_with(|items| {
serde_json::to_string(items).unwrap_or_else(|_| "[]".to_string())
});
([(axum::http::header::CONTENT_TYPE, "application/json")], json)
}
}),
)
.route(
"/api/gates",
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let json = state_clone.env.gates.read_with(|items| {
serde_json::to_string(items).unwrap_or_else(|_| "[]".to_string())
});
([(axum::http::header::CONTENT_TYPE, "application/json")], json)
}
}),
)
.route(
"/api/gates/{id}/authorize",
post({
let state_clone = app_state.handler.state.clone();
move |axum::extract::Path(id): axum::extract::Path<String>| async move {
let mut found = false;
state_clone.env.gates.modify(|gates| {
for g in gates.iter_mut() {
if g.id == id {
g.status = "authorized".to_string();
g.reason = Some("Manually authorized via dashboard".to_string());
g.timestamp = crate::handlers::utils::now_secs();
found = true;
break;
}
}
});
if found {
state_clone.record_activity("gate", &format!("Authorized push gate: {}", id), Some("authorized"));
let _ = state_clone.event_bus_tx.send(crate::state::GenericEvent {
topic: "gate:event".to_string(),
session_id: None,
payload: serde_json::json!({ "id": id, "status": "authorized", "active_gate": "authorized" }),
});
(axum::http::StatusCode::OK, axum::Json(serde_json::json!({ "status": "success" })))
} else {
(axum::http::StatusCode::NOT_FOUND, axum::Json(serde_json::json!({ "status": "error", "message": "Gate not found" })))
}
}
}),
)
.route(
"/api/gates/{id}/block",
post({
let state_clone = app_state.handler.state.clone();
move |axum::extract::Path(id): axum::extract::Path<String>| async move {
let mut found = false;
state_clone.env.gates.modify(|gates| {
for g in gates.iter_mut() {
if g.id == id {
g.status = "blocked".to_string();
g.reason = Some("Manually blocked via dashboard".to_string());
g.timestamp = crate::handlers::utils::now_secs();
found = true;
break;
}
}
});
if found {
state_clone.record_activity("gate", &format!("Blocked push gate: {}", id), Some("blocked"));
let _ = state_clone.event_bus_tx.send(crate::state::GenericEvent {
topic: "gate:event".to_string(),
session_id: None,
payload: serde_json::json!({ "id": id, "status": "blocked", "active_gate": "blocked" }),
});
(axum::http::StatusCode::OK, axum::Json(serde_json::json!({ "status": "success" })))
} else {
(axum::http::StatusCode::NOT_FOUND, axum::Json(serde_json::json!({ "status": "error", "message": "Gate not found" })))
}
}
}),
)
.route(
"/api/gates/{id}",
delete({
let state_clone = app_state.handler.state.clone();
move |axum::extract::Path(id): axum::extract::Path<String>| async move {
let mut found = false;
state_clone.env.gates.modify(|gates| {
let initial_len = gates.len();
gates.retain(|g| g.id != id);
found = gates.len() < initial_len;
});
if found {
state_clone.record_activity("gate", &format!("Deleted push gate: {}", id), Some("deleted"));
let active_gate = state_clone.env.gates.read_with(|gates| gates.iter().find(|g| g.action == "push" || g.gate_type.as_deref() == Some("quality_gate")).map(|g| g.status.clone()));
let _ = state_clone.event_bus_tx.send(crate::state::GenericEvent { topic: "gate:event".to_string(), session_id: None, payload: serde_json::json!({ "id": id, "status": "deleted", "active_gate": active_gate }) });
(axum::http::StatusCode::OK, axum::Json(serde_json::json!({ "status": "success" })))
} else {
(axum::http::StatusCode::NOT_FOUND, axum::Json(serde_json::json!({ "status": "error", "message": "Gate not found" })))
}
}
}),
)
.route(
"/api/stats",
get({
@@ -967,7 +1111,8 @@ mod tests {
let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let summaries: Vec<crate::models::SessionSummary> = serde_json::from_slice(&body_bytes).unwrap();
let summaries: Vec<crate::models::SessionSummary> =
serde_json::from_slice(&body_bytes).unwrap();
assert_eq!(summaries.len(), 1);
assert_eq!(summaries[0].summary, "Session 2 summary");
}
@@ -1002,9 +1147,103 @@ mod tests {
let response = app.oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let adr = app_state.handler.state.code.adrs.read_with(|adrs| adrs[0].clone());
let adr = app_state
.handler
.state
.code
.adrs
.read_with(|adrs| adrs[0].clone());
assert_eq!(adr.status, "implemented");
assert_eq!(adr.git_commit.as_deref(), Some("git-hash-999"));
assert!(adr.resolved_at.is_some());
}
#[tokio::test]
async fn test_environment_endpoints() {
let (app, app_state, _dir) = setup_app().await;
app_state.handler.state.env.environments.modify(|envs| {
envs.push(crate::models::EnvironmentDetail {
namespace: "default".to_string(),
name: "staging-k8s".to_string(),
url: "https://staging.internal.net".to_string(),
description: "Staging cluster".to_string(),
requires_vpn: true,
updated_at: 12345,
..Default::default()
});
});
let req = Request::builder()
.uri("/api/environments")
.body(Body::empty())
.unwrap();
let resp = app.oneshot(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let envs: Vec<crate::models::EnvironmentDetail> = serde_json::from_slice(&body).unwrap();
assert_eq!(envs.len(), 1);
assert_eq!(envs[0].name, "staging-k8s");
}
#[tokio::test]
async fn test_interactive_endpoints() {
let (app, app_state, _dir) = setup_app().await;
// 1. Tech Debt resolve
app_state.handler.state.code.tech_debts.modify(|debts| {
debts.push(crate::models::TechDebt {
id: "TD-001".to_string(),
description: "Fix memory leak".to_string(),
is_resolved: false,
..Default::default()
});
});
let req = Request::builder()
.method("POST")
.uri("/api/tech_debts/TD-001/resolve")
.body(Body::empty())
.unwrap();
let resp = app.oneshot(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let debt = app_state
.handler
.state
.code
.tech_debts
.read_with(|d| d[0].clone());
assert!(debt.is_resolved);
// 2. Gate authorize
app_state.handler.state.env.gates.modify(|gates| {
gates.push(crate::models::GateRecord {
id: "gate-123".to_string(),
action: "push".to_string(),
target: "master".to_string(),
status: "blocked".to_string(),
..Default::default()
});
});
let app2 = create_router(app_state.clone());
let req2 = Request::builder()
.method("POST")
.uri("/api/gates/gate-123/authorize")
.body(Body::empty())
.unwrap();
let resp2 = app2.oneshot(req2).await.unwrap();
assert_eq!(resp2.status(), StatusCode::OK);
let gate = app_state
.handler
.state
.env
.gates
.read_with(|g| g[0].clone());
assert_eq!(gate.status, "authorized");
}
}
+71 -5
View File
@@ -44,8 +44,7 @@ pub fn find_projected_knowledge(
|| normalized_file.ends_with(&format!("/{}", ndfp.trim_start_matches('/')))
|| ndfp.ends_with(&format!("/{}", normalized_file.trim_start_matches('/')))
|| (!file_name.is_empty()
&& (ndfp == file_name
|| ndfp.ends_with(&format!("/{}", file_name))))
&& (ndfp == file_name || ndfp.ends_with(&format!("/{}", file_name))))
{
return true;
}
@@ -73,8 +72,7 @@ pub fn find_projected_knowledge(
all_adrs
.iter()
.filter(|a| {
a.status.eq_ignore_ascii_case("accepted")
|| a.status.eq_ignore_ascii_case("active")
a.status.eq_ignore_ascii_case("accepted") || a.status.eq_ignore_ascii_case("active")
})
.filter(|a| {
for comp in &a.affected_components {
@@ -187,6 +185,52 @@ pub async fn nvim_telemetry_handler(
let _ = state.handler.state.event_bus_tx.send(event);
}
if payload.event.starts_with("agent_") || payload.event.starts_with("diff_") {
let payload_val = serde_json::json!(&payload);
let _ = state
.handler
.state
.event_bus_tx
.send(crate::state::GenericEvent {
topic: format!("nvim:ui:{}", payload.event),
session_id: Some(payload.session_id.clone()),
payload: payload_val.clone(),
});
if let Some(ref req_id) = payload.request_id {
let _ = state
.handler
.state
.event_bus_tx
.send(crate::state::GenericEvent {
topic: format!("nvim:ui:{}:{}", payload.event, req_id),
session_id: Some(payload.session_id.clone()),
payload: payload_val.clone(),
});
}
if let Some(ref diff_id) = payload.diff_id {
let _ = state
.handler
.state
.event_bus_tx
.send(crate::state::GenericEvent {
topic: format!("nvim:ui:{}:{}", payload.event, diff_id),
session_id: Some(payload.session_id.clone()),
payload: payload_val.clone(),
});
let _ = state
.handler
.state
.event_bus_tx
.send(crate::state::GenericEvent {
topic: format!("nvim:ui:diff_decision:{}", diff_id),
session_id: Some(payload.session_id.clone()),
payload: payload_val.clone(),
});
}
}
if let Some(ref f) = payload.file {
state.handler.state.record_activity(
"editor",
@@ -195,10 +239,32 @@ pub async fn nvim_telemetry_handler(
);
}
// 4. Extract active task & push gate for zero-polling statusline
let active_task = state.handler.state.project.tasks.read_with(|tasks| {
tasks
.iter()
.find(|t| t.is_active())
.or_else(|| {
tasks
.iter()
.find(|t| t.status == "pending" && t.parent_id.is_none())
})
.map(|t| t.title.clone())
});
let active_gate = state.handler.state.env.gates.read_with(|gates| {
gates
.iter()
.find(|g| g.action == "push" || g.gate_type.as_deref() == Some("quality_gate"))
.map(|g| g.status.clone())
});
axum::Json(serde_json::json!({
"status": "ok",
"tech_debts": tech_debts,
"adrs": adrs
"adrs": adrs,
"active_task": active_task,
"active_gate": active_gate
}))
}
+67 -27
View File
@@ -29,6 +29,7 @@ pub struct SessionCleanup {
pub state: Arc<AppState>,
pub send_task: tokio::task::JoinHandle<()>,
pub recv_task: tokio::task::JoinHandle<()>,
pub event_task: tokio::task::JoinHandle<()>,
}
impl Drop for SessionCleanup {
@@ -41,6 +42,7 @@ impl Drop for SessionCleanup {
.remove(&self.session_id);
self.send_task.abort();
self.recv_task.abort();
self.event_task.abort();
}
}
@@ -64,19 +66,19 @@ pub async fn handle_socket(socket: WebSocket, state: Arc<AppState>, _client_type
activities
.iter()
.filter_map(|act_val| {
if act_val["category"] == "TASK_EVENT" {
if let Some(details_str) = act_val["details"].as_str() {
if let Ok(event_val) = serde_json::from_str::<serde_json::Value>(details_str) {
return Some(
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/task/completed",
"params": event_val
})
.to_string(),
);
}
}
if act_val["category"] == "TASK_EVENT"
&& let Some(details_str) = act_val["details"].as_str()
&& let Ok(event_val) =
serde_json::from_str::<serde_json::Value>(details_str)
{
return Some(
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/task/completed",
"params": event_val
})
.to_string(),
);
}
None
})
@@ -147,11 +149,42 @@ pub async fn handle_socket(socket: WebSocket, state: Arc<AppState>, _client_type
}
});
let event_tx = tx.clone();
let mut event_bus_rx = state.handler.state.event_bus_tx.subscribe();
let event_task = tokio::spawn(async move {
while let Ok(event) = event_bus_rx.recv().await {
if event.topic == "resource:updated" {
if let Some(uri) = event.payload.get("uri").and_then(|u| u.as_str()) {
let notif = serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/resources/updated",
"params": {
"uri": uri
}
});
if event_tx.send(notif.to_string()).await.is_err() {
break;
}
}
} else if event.topic == "task:event" {
let notif = serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/task/updated",
"params": event.payload
});
if event_tx.send(notif.to_string()).await.is_err() {
break;
}
}
}
});
let mut cleanup = SessionCleanup {
session_id: session_id.clone(),
state: Arc::clone(&state),
send_task,
recv_task,
event_task,
};
tokio::select! {
@@ -194,6 +227,7 @@ mod tests {
let send_task = tokio::spawn(async {});
let recv_task = tokio::spawn(async {});
let event_task = tokio::spawn(async {});
{
let _cleanup = SessionCleanup {
@@ -201,6 +235,7 @@ mod tests {
state: app_state.clone(),
send_task,
recv_task,
event_task,
};
} // Drop happens here
@@ -227,7 +262,10 @@ mod tests {
mem_state.broadcast_task_event(task_event.clone());
// Verify recent activities recorded the event
let recorded = mem_state.telemetry.recent_activities.read_with(|act| act.clone());
let recorded = mem_state
.telemetry
.recent_activities
.read_with(|act| act.clone());
assert!(!recorded.is_empty());
assert_eq!(recorded[0]["category"], "TASK_EVENT");
@@ -257,18 +295,17 @@ mod tests {
.iter()
.filter_map(|act_val| {
if act_val["category"] == "TASK_EVENT" {
if let Some(details_str) = act_val["details"].as_str() {
if let Ok(event_val) = serde_json::from_str::<serde_json::Value>(details_str) {
return Some(
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/task/completed",
"params": event_val
})
.to_string(),
);
}
}
let details_str = act_val["details"].as_str()?;
let event_val =
serde_json::from_str::<serde_json::Value>(details_str).ok()?;
return Some(
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/task/completed",
"params": event_val
})
.to_string(),
);
}
None
})
@@ -280,7 +317,10 @@ mod tests {
let _ = tx.try_send(notif);
}
let replayed_msg = rx.recv().await.expect("Expected replayed task event notification");
let replayed_msg = rx
.recv()
.await
.expect("Expected replayed task event notification");
let parsed: serde_json::Value = serde_json::from_str(&replayed_msg).unwrap();
assert_eq!(parsed["method"], "notifications/task/completed");
assert_eq!(parsed["params"]["task_id"], "task-999");
+21 -19
View File
@@ -1,8 +1,8 @@
use mcp_memory_server::router::MemoryHandler;
use mcp_memory_server::state::MemoryState;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use mcp_memory_server::router::MemoryHandler;
use mcp_memory_server::state::MemoryState;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -13,7 +13,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let handler = MemoryHandler::new(state);
let win_dir = PathBuf::from(r"C:\Users\reazul.ashraf\.gemini\antigravity-cli\mcp\mcp-memory");
let wsl_dir = PathBuf::from(r"\\wsl.localhost\Ubuntu\home\riz\.gemini\antigravity-cli\mcp\mcp-memory");
let wsl_dir =
PathBuf::from(r"\\wsl.localhost\Ubuntu\home\riz\.gemini\antigravity-cli\mcp\mcp-memory");
fs::create_dir_all(&win_dir)?;
if !wsl_dir.exists() {
@@ -41,13 +42,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Ok(entries) = fs::read_dir(&win_dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.extension().map_or(false, |ext| ext == "json") {
if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
if !active_tools.contains(stem) {
println!(" [STALE REMOVED Win] {}", path.display());
let _ = fs::remove_file(&path);
}
}
if path.extension().is_some_and(|ext| ext == "json")
&& let Some(stem) = path.file_stem().and_then(|s| s.to_str())
&& !active_tools.contains(stem)
{
println!(" [STALE REMOVED Win] {}", path.display());
let _ = fs::remove_file(&path);
}
}
}
@@ -56,23 +56,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Ok(entries) = fs::read_dir(&wsl_dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.extension().map_or(false, |ext| ext == "json") {
if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
if !active_tools.contains(stem) {
println!(" [STALE REMOVED WSL] {}", path.display());
let _ = fs::remove_file(&path);
}
}
if path.extension().is_some_and(|ext| ext == "json")
&& let Some(stem) = path.file_stem().and_then(|s| s.to_str())
&& !active_tools.contains(stem)
{
println!(" [STALE REMOVED WSL] {}", path.display());
let _ = fs::remove_file(&path);
}
}
}
// Sync instructions.md
let instructions_src = PathBuf::from(r"C:\Users\reazul.ashraf\workspace\rust\mcp-memory\instructions.md");
let instructions_src =
PathBuf::from(r"C:\Users\reazul.ashraf\workspace\rust\mcp-memory\instructions.md");
if instructions_src.exists() {
let _ = fs::copy(&instructions_src, win_dir.join("instructions.md"));
let _ = fs::copy(&instructions_src, wsl_dir.join("instructions.md"));
let server_instructions = PathBuf::from(r"C:\Users\reazul.ashraf\workspace\rust\mcp-memory\server\src\instructions.md");
let server_instructions = PathBuf::from(
r"C:\Users\reazul.ashraf\workspace\rust\mcp-memory\server\src\instructions.md",
);
let _ = fs::copy(&instructions_src, server_instructions);
println!(" [OK] Synchronized instructions.md to Win, WSL, and server/src/instructions.md");
}
+5
View File
@@ -0,0 +1,5 @@
fn main() {
let mut img = image::DynamicImage::new_rgb8(10, 10);
img.invert();
let _img2 = img.adjust_contrast(20.0);
}
+43
View File
@@ -0,0 +1,43 @@
use serde_json::Value;
use std::env;
use std::fs;
use std::path::PathBuf;
use tracing::info;
pub fn load_mcp_config_env() {
let config_path = dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(".gemini/config/mcp_config.json");
if !config_path.exists() {
return;
}
let config_str = match fs::read_to_string(&config_path) {
Ok(s) => s,
Err(_) => return,
};
let parsed: Value = match serde_json::from_str(&config_str) {
Ok(v) => v,
Err(_) => return,
};
if let Some(env_obj) = parsed
.get("mcpServers")
.and_then(|v| v.get("mcp-memory"))
.and_then(|v| v.get("env"))
.and_then(|v| v.as_object())
{
for (key, val) in env_obj {
if let Some(val_str) = val.as_str()
&& env::var(key).is_err()
{
unsafe {
env::set_var(key, val_str);
}
info!("Loaded env var {} from mcp_config.json", key);
}
}
}
}
+172
View File
@@ -642,6 +642,141 @@
flex-direction: column;
gap: 12px;
}
/* Command Palette Modal */
#cmd-palette-backdrop {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.65);
backdrop-filter: blur(4px);
z-index: 10000;
justify-content: center;
align-items: flex-start;
padding-top: 12vh;
box-sizing: border-box;
}
#cmd-palette-modal {
background: var(--card-bg);
border: 1px solid var(--border-color);
border-radius: 12px;
width: 90%;
max-width: 680px;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.45);
display: flex;
flex-direction: column;
overflow: hidden;
animation: paletteFadeIn 0.15s ease-out;
}
@keyframes paletteFadeIn {
from { opacity: 0; transform: translateY(-10px) scale(0.98); }
to { opacity: 1; transform: translateY(0) scale(1); }
}
.palette-header {
display: flex;
align-items: center;
padding: 14px 18px;
border-bottom: 1px solid var(--border-color);
background: var(--canvas-bg);
gap: 12px;
}
.palette-search-icon {
font-size: 1.1em;
color: var(--text-secondary);
}
.palette-input {
flex: 1;
background: transparent;
border: none;
outline: none;
font-size: 1.05em;
color: var(--text-primary);
font-family: inherit;
}
.palette-kbd-badge {
font-size: 0.72em;
padding: 2px 6px;
border-radius: 4px;
background: var(--card-bg);
border: 1px solid var(--border-color);
color: var(--text-secondary);
font-family: monospace;
}
.palette-results {
max-height: 380px;
overflow-y: auto;
padding: 8px 0;
margin: 0;
list-style: none;
}
.palette-item {
padding: 10px 18px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
cursor: pointer;
transition: background 0.1s;
}
.palette-item:hover, .palette-item.selected {
background: rgba(52, 152, 219, 0.12);
}
.palette-item-left {
display: flex;
align-items: center;
gap: 10px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.palette-item-icon {
font-size: 1em;
flex-shrink: 0;
}
.palette-item-text {
display: flex;
flex-direction: column;
gap: 2px;
overflow: hidden;
}
.palette-item-title {
font-weight: 600;
font-size: 0.92em;
color: var(--text-primary);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.palette-item-sub {
font-size: 0.78em;
color: var(--text-secondary);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.palette-item-badge {
font-size: 0.72em;
padding: 2px 6px;
border-radius: 4px;
border: 1px solid var(--border-color);
background: var(--canvas-bg);
color: var(--text-secondary);
flex-shrink: 0;
text-transform: uppercase;
}
.palette-footer {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 18px;
background: var(--canvas-bg);
border-top: 1px solid var(--border-color);
font-size: 0.75em;
color: var(--text-secondary);
}
</style>
</head>
<body>
@@ -651,12 +786,35 @@
<span id="app-version" style="font-size:0.8em; color:var(--text-secondary); margin-left:12px; font-family:monospace;"></span>
</div>
<div class="header-actions">
<button class="theme-toggle" onclick="openCommandPalette()" title="Command Palette (Ctrl+K)" style="background:var(--card-bg); border:1px solid var(--border-color); color:var(--text-primary); border-radius:6px; padding:5px 10px; font-size:13px; display:flex; align-items:center; gap:6px; cursor:pointer;">
<span>🔍</span>
<span>Command Palette</span>
<span class="enterprise-kbd">Ctrl+K</span>
</button>
<button class="theme-toggle" onclick="testClipboard()" title="Test Clipboard Vision Tool" style="background:#3498db; color:white; border-radius:4px; padding:4px 8px; font-size:14px; display:flex; align-items:center; gap:5px;">📋 Inspect Clipboard</button>
<button class="theme-toggle" onclick="toggleTheme()" title="Toggle Dark Mode">🌓</button>
<div style="font-weight: 500;"><span class="status-dot"></span>System Online</div>
</div>
</div>
<!-- Command Palette Modal -->
<div id="cmd-palette-backdrop" onclick="onPaletteBackdropClick(event)">
<div id="cmd-palette-modal">
<div class="palette-header">
<span class="palette-search-icon">🔍</span>
<input id="palette-input" class="palette-input" type="text" placeholder="Type a command or search tasks, debts, ADRs, environments..." autocomplete="off" oninput="onPaletteSearch(this.value)" onkeydown="onPaletteKeyDown(event)">
<span class="palette-kbd-badge">ESC</span>
</div>
<div id="palette-results" class="palette-results">
<!-- Injected dynamically -->
</div>
<div class="palette-footer">
<div><span>↑↓</span> to navigate &nbsp;•&nbsp; <span>↵</span> to jump &nbsp;•&nbsp; <span>ESC</span> to close</div>
<div id="palette-count" style="font-family: monospace;">0 items</div>
</div>
</div>
</div>
<!-- Clipboard Modal -->
<div id="clipboard-modal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.8); z-index:9999; justify-content:center; align-items:center; flex-direction:column;">
<div style="background:var(--card-bg); padding:20px; border-radius:8px; max-width:80%; max-height:80%; display:flex; flex-direction:column; gap:15px; border:1px solid var(--border-color); box-shadow: 0 10px 30px rgba(0,0,0,0.5);">
@@ -681,6 +839,7 @@
<button class="tab-button" onclick="switchTab('memos-tab', this)">Handoffs</button>
<button class="tab-button" onclick="switchTab('snippets-tab', this)">Snippets</button>
<button class="tab-button" onclick="switchTab('terminal-tab', this)">Terminal</button>
<button class="tab-button" onclick="switchTab('env-tab', this)">Environment</button>
</div>
<div id="graph-tab" class="tab-content">
@@ -785,6 +944,19 @@
</div>
</div>
<div id="env-tab" class="tab-content">
<div class="panel" style="flex:1; display:flex; flex-direction:column; overflow-y:auto;">
<h2>Target Environments & Infrastructure</h2>
<div id="environments-container" style="display:flex; flex-direction:column; gap:15px; margin-bottom: 30px;"></div>
<h2>Tool & System Fingerprints</h2>
<div id="fingerprints-container" style="display:flex; flex-direction:column; gap:15px; margin-bottom: 30px;"></div>
<h2>Environment Requirements & Secrets</h2>
<div id="requirements-container" style="display:flex; flex-direction:column; gap:15px; margin-bottom: 30px;"></div>
<h2>Quality & Authorization Gates</h2>
<div id="gates-container" style="display:flex; flex-direction:column; gap:15px;"></div>
</div>
</div>
<div id="activity-tab" class="tab-content active">
<div class="panel activity-panel" style="flex:1; display:flex; flex-direction:column;">
<div id="activity-feed" style="flex:1;"></div>
+507 -13
View File
@@ -42,6 +42,9 @@ function switchTab(tabId, btn) {
case "ledger-tab":
loadLedger();
break;
case "env-tab":
loadEnvironment();
break;
}
}
function toggleTheme() {
@@ -291,10 +294,20 @@ async function loadGraph() {
}
async function completeTask(id) {
try {
await fetch(`/api/tasks/${id}/complete`, { method: "POST" });
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("tasks/complete", { id });
} else {
await fetch(`/api/tasks/${encodeURIComponent(id)}/complete`, { method: "POST" });
}
loadTasks();
} catch (e) {
console.error("Failed to complete task", e);
console.error("Failed to complete task via WS, falling back to HTTP", e);
try {
await fetch(`/api/tasks/${encodeURIComponent(id)}/complete`, { method: "POST" });
loadTasks();
} catch (err) {
console.error("Failed to complete task via HTTP fallback", err);
}
}
}
function buildTaskTreeHTML(tasks, parentId, depth = 0) {
@@ -683,6 +696,34 @@ async function loadActivityHistory() {
}
var wsConnected = false;
var activeSSE = null;
var activeWS = null;
var wsRpcId = 1;
var pendingWsRequests = new Map;
function sendWsRpc(method, params = {}, timeoutMs = 5000) {
return new Promise((resolve, reject) => {
if (!activeWS || activeWS.readyState !== WebSocket.OPEN) {
return reject(new Error("WebSocket not open"));
}
const id = ++wsRpcId;
const timer = setTimeout(() => {
pendingWsRequests.delete(id);
reject(new Error(`WebSocket RPC timeout for ${method}`));
}, timeoutMs);
pendingWsRequests.set(id, { resolve, reject, timer });
try {
activeWS.send(JSON.stringify({
jsonrpc: "2.0",
id,
method,
params
}));
} catch (e) {
clearTimeout(timer);
pendingWsRequests.delete(id);
reject(e);
}
});
}
var tabRefreshDebounceTimers = {};
function requestDomainRefresh(domain) {
const tabMap = {
@@ -693,7 +734,9 @@ function requestDomainRefresh(domain) {
snippets: "snippets-tab",
terminal: "terminal-tab",
ledger: "ledger-tab",
memos: "memos-tab"
memos: "memos-tab",
env: "env-tab",
gates: "env-tab"
};
const targetTab = tabMap[domain];
if (!targetTab || targetTab !== currentTabId) {
@@ -729,6 +772,10 @@ function requestDomainRefresh(domain) {
case "memos":
loadMemos();
break;
case "env":
case "gates":
loadEnvironment();
break;
}
}, 250);
}
@@ -750,6 +797,9 @@ function handleIncomingActivity(payload) {
requestDomainRefresh("graph");
} else if (method === "notifications/task/completed" || category.startsWith("TASK")) {
requestDomainRefresh("task");
} else if (category === "GATE" || category === "QUALITY_GATE" || category === "ENVIRONMENT" || category === "ENV") {
requestDomainRefresh("gates");
requestDomainRefresh("env");
} else if (category === "TECH_DEBT" || category === "ERROR_FIX" || category === "CHECKPOINT") {
requestDomainRefresh("techdebt");
} else if (category === "SNIPPET") {
@@ -790,6 +840,7 @@ function setupWS() {
}
ws.onopen = function() {
wsConnected = true;
activeWS = ws;
if (activeSSE) {
activeSSE.close();
activeSSE = null;
@@ -798,7 +849,18 @@ function setupWS() {
ws.onmessage = function(event) {
try {
const data = JSON.parse(event.data);
if (data.type === "activity" || data.method === "notifications/activity" || data.method === "notifications/task/completed" || data.method === "notifications/resources/updated") {
if (data.id !== undefined && pendingWsRequests.has(data.id)) {
const handler = pendingWsRequests.get(data.id);
pendingWsRequests.delete(data.id);
clearTimeout(handler.timer);
if (data.error) {
handler.reject(new Error(data.error.message || JSON.stringify(data.error)));
} else {
handler.resolve(data.result);
}
return;
}
if (data.type === "activity" || data.type === "gate" || data.type === "task" || data.type === "tech_debt" || data.type === "nvim_telemetry" || data.method === "notifications/activity" || data.method === "notifications/task/completed" || data.method === "notifications/resources/updated") {
const payload = data.params || data.data || data;
handleIncomingActivity(payload);
}
@@ -808,12 +870,24 @@ function setupWS() {
};
ws.onclose = function() {
wsConnected = false;
activeWS = null;
pendingWsRequests.forEach((handler) => {
clearTimeout(handler.timer);
handler.reject(new Error("WebSocket disconnected"));
});
pendingWsRequests.clear();
console.log("WebSocket closed, falling back to SSE and retrying WS in 5s...");
setupSSE();
setTimeout(setupWS, 5000);
};
ws.onerror = function() {
wsConnected = false;
activeWS = null;
pendingWsRequests.forEach((handler) => {
clearTimeout(handler.timer);
handler.reject(new Error("WebSocket error"));
});
pendingWsRequests.clear();
setupSSE();
};
}
@@ -1328,7 +1402,10 @@ function loadTechDebt() {
<span style="background:${sevBg}; color:${sevColor}; border:1px solid ${sevBorder}; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; text-transform:uppercase;">${escapeHtml(item.severity || "medium")}</span>
${item.effort_estimate ? `<span style="background:var(--canvas-bg); font-size:0.75em; padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); color:var(--text-secondary);">⏳ ${escapeHtml(item.effort_estimate)}</span>` : ""}
</div>
<div>${statusBadge}</div>
<div style="display:flex; align-items:center; gap:8px;">
<div>${statusBadge}</div>
${!item.is_resolved ? `<button class="complete-btn" style="position:static;" onclick="resolveTechDebt('${escapeHtml(item.id)}')" title="Resolve Tech Debt">✓</button>` : ""}
</div>
</div>
${fileLoc ? `<div style="margin-top:6px; font-family:monospace; font-size:0.85em; color:var(--accent-color);">\uD83D\uDCC4 ${escapeHtml(fileLoc)}</div>` : ""}
<div style="margin-top:6px; font-size:0.9em; line-height:1.4;"><strong>Description:</strong> ${escapeHtml(item.description || "")}</div>
@@ -1704,6 +1781,121 @@ function loadSnippets() {
]
});
}
function loadEnvironment() {
loadGenericList("/api/environments", "environments-container", (item) => {
const isVpn = item.requires_vpn;
const envType = (item.env_type || "dev").toLowerCase();
let typeColor = "#3498db";
if (envType === "prod")
typeColor = "#e74c3c";
else if (envType === "staging")
typeColor = "#f39c12";
else if (envType === "qa")
typeColor = "#9b59b6";
return `
<div style="display:flex; justify-content:space-between; align-items:center; gap:8px;">
<div>
<strong style="font-size:1.05em; color:var(--text-primary);">${escapeHtml(item.name || "Environment")}</strong>
<span style="background:${typeColor}; color:#fff; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; margin-left:6px; text-transform:uppercase;">${escapeHtml(envType)}</span>
${isVpn ? '<span style="background:#e67e22; color:#fff; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; margin-left:6px;">\uD83D\uDD12 VPN Required</span>' : ""}
</div>
<span style="font-size:0.75em; color:var(--text-secondary); background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">${escapeHtml(item.namespace || "default")}</span>
</div>
${item.url ? `<div style="margin-top:6px; font-family:monospace; font-size:0.85em;"><a href="${escapeHtml(item.url)}" target="_blank" rel="noopener noreferrer" style="color:var(--accent-color); text-decoration:none;">\uD83C\uDF10 ${escapeHtml(item.url)}</a></div>` : ""}
${item.description ? `<div style="margin-top:4px; font-size:0.9em; color:var(--text-primary);">${escapeHtml(item.description)}</div>` : ""}
<div style="margin-top:8px; font-size:0.8em; display:flex; gap:8px; flex-wrap:wrap; align-items:center;">
${item.ssh_host ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); font-family:monospace;">SSH: ${escapeHtml(item.ssh_host)}</span>` : ""}
${item.healthcheck_endpoint ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); font-family:monospace;">Health: ${escapeHtml(item.healthcheck_endpoint)}</span>` : ""}
${item.repo_name ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">\uD83D\uDCE6 ${escapeHtml(item.repo_name)}</span>` : ""}
</div>
`;
}, {
itemNoun: "Environments",
cardBorderColor: "#3498db",
defaultSort: "desc",
timestampExtractor: (item) => item.updated_at,
searchFields: (item) => [item.name, item.url, item.description, item.env_type, item.ssh_host, item.namespace]
});
loadGenericList("/api/env_fingerprints", "fingerprints-container", (item) => {
const toolVersions = item.tool_versions || {};
const tools = Object.entries(toolVersions).map(([k, v]) => `<span style="background:var(--canvas-bg); padding:2px 8px; border-radius:4px; font-size:0.8em; font-family:monospace; border:1px solid var(--border-color);"><strong>${escapeHtml(k)}:</strong> ${escapeHtml(String(v))}</span>`).join(" ");
return `
<div style="display:flex; justify-content:space-between; align-items:center; gap:8px;">
<div>
<strong style="font-size:1.05em; color:var(--text-primary);">\uD83D\uDCBB ${escapeHtml(item.os || "Unknown OS")} (${escapeHtml(item.shell || "shell")})</strong>
</div>
<span style="font-size:0.75em; color:var(--text-secondary); background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">${escapeHtml(item.namespace || "default")}</span>
</div>
${tools ? `<div style="margin-top:10px; display:flex; gap:6px; flex-wrap:wrap; align-items:center;">${tools}</div>` : '<div style="margin-top:6px; font-size:0.85em; color:var(--text-secondary);">No tool versions registered</div>'}
<div style="margin-top:8px; font-size:0.8em; color:var(--text-secondary);">
${item.repo_name ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); margin-right:8px;">\uD83D\uDCE6 ${escapeHtml(item.repo_name)}</span>` : ""}
</div>
`;
}, {
itemNoun: "Fingerprints",
cardBorderColor: "#1abc9c",
defaultSort: "desc",
timestampExtractor: (item) => item.updated_at,
searchFields: (item) => [item.namespace, item.os, item.shell, item.repo_name, JSON.stringify(item.tool_versions || {})]
});
loadGenericList("/api/env_requirements", "requirements-container", (item) => {
const isSecret = item.is_secret;
return `
<div style="display:flex; justify-content:space-between; align-items:center; gap:8px;">
<div>
<span style="font-family:monospace; font-size:1.05em; font-weight:bold; color:var(--text-primary);">${escapeHtml(item.key)}</span>
${isSecret ? '<span style="background:#e74c3c; color:#fff; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; margin-left:6px;">\uD83D\uDD12 Secret</span>' : '<span style="background:var(--canvas-bg); color:var(--text-secondary); border:1px solid var(--border-color); font-size:0.75em; padding:2px 6px; border-radius:4px; margin-left:6px;">Public</span>'}
</div>
<span style="font-size:0.75em; color:var(--text-secondary); background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">${escapeHtml(item.namespace || "default")}</span>
</div>
${item.description ? `<div style="margin-top:6px; font-size:0.9em; color:var(--text-primary);">${escapeHtml(item.description)}</div>` : ""}
<div style="margin-top:8px; font-size:0.8em; display:flex; gap:8px; flex-wrap:wrap; align-items:center;">
${item.default_value ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); font-family:monospace;">Default: ${escapeHtml(item.default_value)}</span>` : ""}
${item.validation_regex ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); font-family:monospace;">Regex: ${escapeHtml(item.validation_regex)}</span>` : ""}
${item.repo_name ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">\uD83D\uDCE6 ${escapeHtml(item.repo_name)}</span>` : ""}
</div>
`;
}, {
itemNoun: "Requirements",
cardBorderColor: "#e67e22",
defaultSort: "desc",
searchFields: (item) => [item.key, item.description, item.default_value, item.namespace, item.repo_name]
});
loadGenericList("/api/gates", "gates-container", (item) => {
const status = (item.status || "pending").toLowerCase();
let statusColor = "var(--accent-color)";
if (status === "authorized")
statusColor = "var(--success-color)";
else if (status === "blocked")
statusColor = "var(--error-color)";
return `
<div style="display:flex; justify-content:space-between; align-items:center; gap:8px;">
<div>
<strong style="font-size:1.05em; color:var(--text-primary);">${escapeHtml(item.action)} &rarr; ${escapeHtml(item.target)}</strong>
<span style="background:${statusColor}; color:#fff; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; margin-left:6px; text-transform:uppercase;">${escapeHtml(status)}</span>
</div>
<div style="display:flex; align-items:center; gap:6px;">
<span style="font-size:0.75em; color:var(--text-secondary); background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">${escapeHtml(item.namespace || "global")}</span>
${status !== "authorized" ? `<button class="tab-button" style="padding:2px 8px; font-size:0.75em; background:rgba(46,204,113,0.15); color:var(--success-color); border:1px solid var(--success-color); cursor:pointer;" onclick="authorizeGate('${escapeHtml(item.id)}')" title="Authorize Gate">✓ Authorize</button>` : ""}
${status !== "blocked" ? `<button class="tab-button" style="padding:2px 8px; font-size:0.75em; background:rgba(231,76,60,0.15); color:var(--error-color); border:1px solid var(--error-color); cursor:pointer;" onclick="blockGate('${escapeHtml(item.id)}')" title="Block Gate">\uD83D\uDEAB Block</button>` : ""}
<button class="tab-button" style="padding:2px 6px; font-size:0.75em; background:var(--canvas-bg); color:var(--text-secondary); border:1px solid var(--border-color); cursor:pointer;" onclick="deleteGate('${escapeHtml(item.id)}')" title="Delete Gate">✕</button>
</div>
</div>
${item.reason ? `<div style="margin-top:6px; font-size:0.9em; color:var(--text-primary);">${escapeHtml(item.reason)}</div>` : ""}
<div style="margin-top:8px; font-size:0.8em; display:flex; gap:8px; flex-wrap:wrap; align-items:center;">
${item.gate_type ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">Type: ${escapeHtml(item.gate_type)}</span>` : ""}
${item.enforcer ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">Enforcer: ${escapeHtml(item.enforcer)}</span>` : ""}
${item.repo_name ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">\uD83D\uDCE6 ${escapeHtml(item.repo_name)}</span>` : ""}
</div>
`;
}, {
itemNoun: "Gates",
cardBorderColor: "#e74c3c",
defaultSort: "desc",
timestampExtractor: (item) => item.timestamp,
searchFields: (item) => [item.action, item.target, item.status, item.reason, item.enforcer]
});
}
function loadAllExtras() {
loadTerminal();
loadTechDebt();
@@ -1772,21 +1964,308 @@ function setupSSE() {
console.error("SSE initialization error", e);
}
}
async function resolveTechDebt(id) {
try {
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("tech_debts/resolve", { id });
} else {
const res = await fetch(`/api/tech_debts/${encodeURIComponent(id)}/resolve`, { method: "POST" });
if (!res.ok)
console.error("Failed to resolve tech debt", await res.text());
}
loadTechDebt();
} catch (err) {
console.error("Error resolving tech debt via WS, trying HTTP fallback", err);
try {
const res = await fetch(`/api/tech_debts/${encodeURIComponent(id)}/resolve`, { method: "POST" });
if (!res.ok)
console.error("Failed to resolve tech debt", await res.text());
loadTechDebt();
} catch (fallbackErr) {
console.error("Error resolving tech debt", fallbackErr);
}
}
}
async function authorizeGate(id) {
try {
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("gates/authorize", { id });
} else {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}/authorize`, { method: "POST" });
if (!res.ok)
console.error("Failed to authorize gate", await res.text());
}
loadEnvironment();
} catch (err) {
console.error("Error authorizing gate via WS, trying HTTP fallback", err);
try {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}/authorize`, { method: "POST" });
if (!res.ok)
console.error("Failed to authorize gate", await res.text());
loadEnvironment();
} catch (fallbackErr) {
console.error("Error authorizing gate", fallbackErr);
}
}
}
async function blockGate(id) {
try {
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("gates/block", { id });
} else {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}/block`, { method: "POST" });
if (!res.ok)
console.error("Failed to block gate", await res.text());
}
loadEnvironment();
} catch (err) {
console.error("Error blocking gate via WS, trying HTTP fallback", err);
try {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}/block`, { method: "POST" });
if (!res.ok)
console.error("Failed to block gate", await res.text());
loadEnvironment();
} catch (fallbackErr) {
console.error("Error blocking gate", fallbackErr);
}
}
}
async function deleteGate(id) {
try {
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("gates/delete", { id });
} else {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}`, { method: "DELETE" });
if (!res.ok)
console.error("Failed to delete gate", await res.text());
}
loadEnvironment();
} catch (err) {
console.error("Error deleting gate via WS, trying HTTP fallback", err);
try {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}`, { method: "DELETE" });
if (!res.ok)
console.error("Failed to delete gate", await res.text());
loadEnvironment();
} catch (fallbackErr) {
console.error("Error deleting gate", fallbackErr);
}
}
}
var paletteItems = [];
var filteredPaletteItems = [];
var paletteSelectedIndex = 0;
var paletteDataLoaded = false;
async function preloadPaletteData() {
try {
const [tasksRes, debtsRes, adrsRes, envsRes, gatesRes] = await Promise.all([
fetch("/api/tasks", { cache: "no-store" }).then((r) => r.json()).catch(() => []),
fetch("/api/tech_debts", { cache: "no-store" }).then((r) => r.json()).catch(() => []),
fetch("/api/adrs", { cache: "no-store" }).then((r) => r.json()).catch(() => []),
fetch("/api/environments", { cache: "no-store" }).then((r) => r.json()).catch(() => []),
fetch("/api/gates", { cache: "no-store" }).then((r) => r.json()).catch(() => [])
]);
const items = [
{ id: "nav-activity", category: "nav", icon: "⚡", title: "Live Activity Feed", sub: "Inspect real-time agent signals & SSE", badge: "VIEW", tabId: "activity-tab" },
{ id: "nav-ledger", category: "nav", icon: "\uD83D\uDCD6", title: "Code Ledger", sub: "View logged code changes & commits", badge: "VIEW", tabId: "ledger-tab" },
{ id: "nav-graph", category: "nav", icon: "\uD83D\uDD78️", title: "Knowledge Graph Explorer", sub: "Explore nodes, relationships & dependencies", badge: "VIEW", tabId: "graph-tab" },
{ id: "nav-search", category: "nav", icon: "\uD83D\uDD0D", title: "Omni-Search", sub: "Full-text cross-entity search", badge: "VIEW", tabId: "search-tab" },
{ id: "nav-tasks", category: "nav", icon: "\uD83D\uDCCB", title: "Task Board", sub: "HTN / DAG task hierarchies", badge: "VIEW", tabId: "task-tab" },
{ id: "nav-techdebt", category: "nav", icon: "⚡", title: "Tech Debt & Error Fixes", sub: "Known tech debt and error fixes", badge: "VIEW", tabId: "techdebt-tab" },
{ id: "nav-adrs", category: "nav", icon: "\uD83D\uDCDC", title: "Architectural Decision Records", sub: "ADRs and design choices", badge: "VIEW", tabId: "adrs-tab" },
{ id: "nav-memos", category: "nav", icon: "\uD83D\uDCDD", title: "Handoff Memos", sub: "Agent handoffs & session summaries", badge: "VIEW", tabId: "memos-tab" },
{ id: "nav-snippets", category: "nav", icon: "\uD83D\uDCCE", title: "Code Snippets", sub: "Indexed snippets & patterns", badge: "VIEW", tabId: "snippets-tab" },
{ id: "nav-terminal", category: "nav", icon: "\uD83D\uDCBB", title: "Terminal History", sub: "Command execution log & exit codes", badge: "VIEW", tabId: "terminal-tab" },
{ id: "nav-env", category: "nav", icon: "\uD83C\uDF10", title: "Environment & Quality Gates", sub: "Deploy targets, fingerprints, requirements & gates", badge: "VIEW", tabId: "env-tab" }
];
if (Array.isArray(tasksRes)) {
tasksRes.forEach((t) => {
items.push({
id: t.id,
category: "task",
icon: t.status === "completed" ? "✓" : "\uD83D\uDCCB",
title: t.title || t.id,
sub: t.description || `Task (${t.status || "pending"})`,
badge: t.status || "TASK",
tabId: "task-tab"
});
});
}
if (Array.isArray(debtsRes)) {
debtsRes.forEach((d) => {
items.push({
id: d.id,
category: "techdebt",
icon: "⚡",
title: `${d.id}: ${d.description || ""}`.trim(),
sub: d.file_path ? `${d.file_path} - Severity: ${d.severity || "medium"}` : `Severity: ${d.severity || "medium"}`,
badge: d.is_resolved ? "RESOLVED" : d.severity || "DEBT",
tabId: "techdebt-tab"
});
});
}
if (Array.isArray(adrsRes)) {
adrsRes.forEach((a) => {
items.push({
id: a.id,
category: "adr",
icon: "\uD83D\uDCDC",
title: `${a.id}: ${a.title || ""}`.trim(),
sub: a.decision || `Status: ${a.status || "accepted"}`,
badge: a.status || "ADR",
tabId: "adrs-tab"
});
});
}
if (Array.isArray(envsRes)) {
envsRes.forEach((e) => {
items.push({
id: e.name || e.url,
category: "env",
icon: "\uD83C\uDF10",
title: e.name || "Target Environment",
sub: `${e.url || ""} ${e.description ? "• " + e.description : ""}`.trim(),
badge: e.env_type || "ENV",
tabId: "env-tab"
});
});
}
if (Array.isArray(gatesRes)) {
gatesRes.forEach((g) => {
items.push({
id: g.id,
category: "gate",
icon: g.status === "authorized" ? "\uD83D\uDFE2" : g.status === "blocked" ? "\uD83D\uDD34" : "\uD83D\uDFE1",
title: `${g.action} → ${g.target}`,
sub: g.reason || `Enforcer: ${g.enforcer || "gate"}`,
badge: g.status || "GATE",
tabId: "env-tab"
});
});
}
paletteItems = items;
paletteDataLoaded = true;
} catch (err) {
console.error("Failed to preload palette data", err);
}
}
function openCommandPalette() {
const backdrop = document.getElementById("cmd-palette-backdrop");
const input = document.getElementById("palette-input");
if (!backdrop || !input)
return;
backdrop.style.display = "flex";
input.value = "";
paletteSelectedIndex = 0;
preloadPaletteData().then(() => {
onPaletteSearch("");
input.focus();
});
}
function closeCommandPalette() {
const backdrop = document.getElementById("cmd-palette-backdrop");
if (backdrop)
backdrop.style.display = "none";
}
function onPaletteBackdropClick(e) {
if (e.target === document.getElementById("cmd-palette-backdrop")) {
closeCommandPalette();
}
}
function onPaletteSearch(query) {
const q = query.toLowerCase().trim();
if (!q) {
filteredPaletteItems = paletteItems.slice(0, 30);
} else {
filteredPaletteItems = paletteItems.filter((item) => item.title.toLowerCase().includes(q) || item.sub.toLowerCase().includes(q) || item.badge.toLowerCase().includes(q) || item.category.toLowerCase().includes(q)).slice(0, 40);
}
paletteSelectedIndex = 0;
renderPaletteResults();
}
function renderPaletteResults() {
const container = document.getElementById("palette-results");
const countEl = document.getElementById("palette-count");
if (!container)
return;
if (countEl)
countEl.innerText = `${filteredPaletteItems.length} results`;
if (filteredPaletteItems.length === 0) {
container.innerHTML = '<li class="enterprise-empty-state" style="margin: 10px 18px;">No matching items found.</li>';
return;
}
container.innerHTML = filteredPaletteItems.map((item, idx) => {
const isSel = idx === paletteSelectedIndex;
return `
<li class="palette-item ${isSel ? "selected" : ""}" data-index="${idx}" onclick="selectPaletteItem(${idx})">
<div class="palette-item-left">
<span class="palette-item-icon">${escapeHtml(item.icon)}</span>
<div class="palette-item-text">
<span class="palette-item-title">${escapeHtml(item.title)}</span>
<span class="palette-item-sub">${escapeHtml(item.sub)}</span>
</div>
</div>
<span class="palette-item-badge">${escapeHtml(item.badge)}</span>
</li>
`;
}).join("");
const selectedEl = container.querySelector(`.palette-item[data-index="${paletteSelectedIndex}"]`);
if (selectedEl) {
selectedEl.scrollIntoView({ block: "nearest" });
}
}
function selectPaletteItem(idx) {
const item = filteredPaletteItems[idx];
if (!item)
return;
closeCommandPalette();
const tabBtn = document.querySelector(`button[onclick*="${item.tabId}"]`);
if (tabBtn) {
switchTab(item.tabId, tabBtn);
}
}
function onPaletteKeyDown(e) {
if (e.key === "ArrowDown") {
e.preventDefault();
if (filteredPaletteItems.length > 0) {
paletteSelectedIndex = (paletteSelectedIndex + 1) % filteredPaletteItems.length;
renderPaletteResults();
}
} else if (e.key === "ArrowUp") {
e.preventDefault();
if (filteredPaletteItems.length > 0) {
paletteSelectedIndex = (paletteSelectedIndex - 1 + filteredPaletteItems.length) % filteredPaletteItems.length;
renderPaletteResults();
}
} else if (e.key === "Enter") {
e.preventDefault();
selectPaletteItem(paletteSelectedIndex);
} else if (e.key === "Escape") {
e.preventDefault();
closeCommandPalette();
}
}
document.addEventListener("keydown", function(e) {
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "k") {
e.preventDefault();
const searchTabBtn = document.querySelector('button[onclick*="search-tab"]');
if (searchTabBtn) {
switchTab("search-tab", searchTabBtn);
}
const searchInput = document.getElementById("search-box");
if (searchInput) {
searchInput.focus();
searchInput.select();
const backdrop = document.getElementById("cmd-palette-backdrop");
if (backdrop && backdrop.style.display === "flex") {
closeCommandPalette();
} else {
openCommandPalette();
}
return;
}
if (e.key === "Escape") {
const backdrop = document.getElementById("cmd-palette-backdrop");
if (backdrop && backdrop.style.display === "flex") {
closeCommandPalette();
return;
}
}
if (e.key === "/" && document.activeElement?.tagName !== "INPUT" && document.activeElement?.tagName !== "TEXTAREA") {
const backdrop = document.getElementById("cmd-palette-backdrop");
if (backdrop && backdrop.style.display === "flex")
return;
const activeSearch = document.querySelector(".tab-content.active .enterprise-search-input, .tab-content.active #task-filter-input");
if (activeSearch) {
e.preventDefault();
@@ -1821,6 +2300,9 @@ function refreshActiveTab() {
case "ledger-tab":
loadLedger();
break;
case "env-tab":
loadEnvironment();
break;
case "activity-tab":
loadActivityHistory();
break;
@@ -1851,6 +2333,7 @@ Object.assign(window, {
requestDomainRefresh,
handleIncomingActivity,
setupWS,
sendWsRpc,
loadGenericList,
onListSearchInput,
onListClearSearch,
@@ -1866,6 +2349,17 @@ Object.assign(window, {
loadMemos,
loadSnippets,
loadAllExtras,
loadEnvironment,
resolveTechDebt,
authorizeGate,
blockGate,
deleteGate,
openCommandPalette,
closeCommandPalette,
onPaletteBackdropClick,
onPaletteSearch,
onPaletteKeyDown,
selectPaletteItem,
testClipboard,
loadVersion,
setupSSE,
+579 -15
View File
@@ -150,6 +150,9 @@ function switchTab(tabId: string, btn?: HTMLElement | null): void {
case "ledger-tab":
loadLedger();
break;
case "env-tab":
loadEnvironment();
break;
}
}
@@ -464,10 +467,20 @@ async function loadGraph(): Promise<void> {
// --- Task Tree (HTN/DAG) ---
async function completeTask(id: string): Promise<void> {
try {
await fetch(`/api/tasks/${id}/complete`, { method: "POST" });
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("tasks/complete", { id });
} else {
await fetch(`/api/tasks/${encodeURIComponent(id)}/complete`, { method: "POST" });
}
loadTasks();
} catch (e) {
console.error("Failed to complete task", e);
console.error("Failed to complete task via WS, falling back to HTTP", e);
try {
await fetch(`/api/tasks/${encodeURIComponent(id)}/complete`, { method: "POST" });
loadTasks();
} catch (err) {
console.error("Failed to complete task via HTTP fallback", err);
}
}
}
@@ -927,6 +940,36 @@ async function loadActivityHistory(): Promise<void> {
let wsConnected = false;
let activeSSE: EventSource | null = null;
let activeWS: WebSocket | null = null;
let wsRpcId = 1;
const pendingWsRequests = new Map<number | string, { resolve: (val: any) => void; reject: (err: any) => void; timer: any }>();
function sendWsRpc(method: string, params: any = {}, timeoutMs = 5000): Promise<any> {
return new Promise((resolve, reject) => {
if (!activeWS || activeWS.readyState !== WebSocket.OPEN) {
return reject(new Error("WebSocket not open"));
}
const id = ++wsRpcId;
const timer = setTimeout(() => {
pendingWsRequests.delete(id);
reject(new Error(`WebSocket RPC timeout for ${method}`));
}, timeoutMs);
pendingWsRequests.set(id, { resolve, reject, timer });
try {
activeWS.send(JSON.stringify({
jsonrpc: "2.0",
id,
method,
params,
}));
} catch (e) {
clearTimeout(timer);
pendingWsRequests.delete(id);
reject(e);
}
});
}
const tabRefreshDebounceTimers: Record<string, any> = {};
function requestDomainRefresh(domain: string): void {
@@ -939,6 +982,8 @@ function requestDomainRefresh(domain: string): void {
terminal: "terminal-tab",
ledger: "ledger-tab",
memos: "memos-tab",
env: "env-tab",
gates: "env-tab",
};
const targetTab = tabMap[domain];
@@ -977,6 +1022,10 @@ function requestDomainRefresh(domain: string): void {
case "memos":
loadMemos();
break;
case "env":
case "gates":
loadEnvironment();
break;
}
}, 250);
}
@@ -1007,6 +1056,14 @@ function handleIncomingActivity(payload: any): void {
category.startsWith("TASK")
) {
requestDomainRefresh("task");
} else if (
category === "GATE" ||
category === "QUALITY_GATE" ||
category === "ENVIRONMENT" ||
category === "ENV"
) {
requestDomainRefresh("gates");
requestDomainRefresh("env");
} else if (
category === "TECH_DEBT" ||
category === "ERROR_FIX" ||
@@ -1056,6 +1113,7 @@ function setupWS(): void {
ws.onopen = function () {
wsConnected = true;
activeWS = ws;
if (activeSSE) {
activeSSE.close();
activeSSE = null;
@@ -1065,8 +1123,23 @@ function setupWS(): void {
ws.onmessage = function (event: MessageEvent) {
try {
const data = JSON.parse(event.data);
if (data.id !== undefined && pendingWsRequests.has(data.id)) {
const handler = pendingWsRequests.get(data.id)!;
pendingWsRequests.delete(data.id);
clearTimeout(handler.timer);
if (data.error) {
handler.reject(new Error(data.error.message || JSON.stringify(data.error)));
} else {
handler.resolve(data.result);
}
return;
}
if (
data.type === "activity" ||
data.type === "gate" ||
data.type === "task" ||
data.type === "tech_debt" ||
data.type === "nvim_telemetry" ||
data.method === "notifications/activity" ||
data.method === "notifications/task/completed" ||
data.method === "notifications/resources/updated"
@@ -1081,6 +1154,12 @@ function setupWS(): void {
ws.onclose = function () {
wsConnected = false;
activeWS = null;
pendingWsRequests.forEach((handler) => {
clearTimeout(handler.timer);
handler.reject(new Error("WebSocket disconnected"));
});
pendingWsRequests.clear();
console.log(
"WebSocket closed, falling back to SSE and retrying WS in 5s...",
);
@@ -1090,6 +1169,12 @@ function setupWS(): void {
ws.onerror = function () {
wsConnected = false;
activeWS = null;
pendingWsRequests.forEach((handler) => {
clearTimeout(handler.timer);
handler.reject(new Error("WebSocket error"));
});
pendingWsRequests.clear();
setupSSE();
};
}
@@ -1777,7 +1862,10 @@ function loadTechDebt(): void {
<span style="background:${sevBg}; color:${sevColor}; border:1px solid ${sevBorder}; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; text-transform:uppercase;">${escapeHtml(item.severity || "medium")}</span>
${item.effort_estimate ? `<span style="background:var(--canvas-bg); font-size:0.75em; padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); color:var(--text-secondary);">⏳ ${escapeHtml(item.effort_estimate)}</span>` : ""}
</div>
<div>${statusBadge}</div>
<div style="display:flex; align-items:center; gap:8px;">
<div>${statusBadge}</div>
${!item.is_resolved ? `<button class="complete-btn" style="position:static;" onclick="resolveTechDebt('${escapeHtml(item.id)}')" title="Resolve Tech Debt">✓</button>` : ""}
</div>
</div>
${fileLoc ? `<div style="margin-top:6px; font-family:monospace; font-size:0.85em; color:var(--accent-color);">📄 ${escapeHtml(fileLoc)}</div>` : ""}
<div style="margin-top:6px; font-size:0.9em; line-height:1.4;"><strong>Description:</strong> ${escapeHtml(item.description || "")}</div>
@@ -2270,6 +2358,149 @@ function loadSnippets(): void {
},
);
}
function loadEnvironment(): void {
// 1. Target Environments & Infrastructure
loadGenericList(
"/api/environments",
"environments-container",
(item) => {
const isVpn = item.requires_vpn;
const envType = (item.env_type || "dev").toLowerCase();
let typeColor = "#3498db";
if (envType === "prod") typeColor = "#e74c3c";
else if (envType === "staging") typeColor = "#f39c12";
else if (envType === "qa") typeColor = "#9b59b6";
return `
<div style="display:flex; justify-content:space-between; align-items:center; gap:8px;">
<div>
<strong style="font-size:1.05em; color:var(--text-primary);">${escapeHtml(item.name || "Environment")}</strong>
<span style="background:${typeColor}; color:#fff; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; margin-left:6px; text-transform:uppercase;">${escapeHtml(envType)}</span>
${isVpn ? '<span style="background:#e67e22; color:#fff; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; margin-left:6px;">🔒 VPN Required</span>' : ""}
</div>
<span style="font-size:0.75em; color:var(--text-secondary); background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">${escapeHtml(item.namespace || "default")}</span>
</div>
${item.url ? `<div style="margin-top:6px; font-family:monospace; font-size:0.85em;"><a href="${escapeHtml(item.url)}" target="_blank" rel="noopener noreferrer" style="color:var(--accent-color); text-decoration:none;">🌐 ${escapeHtml(item.url)}</a></div>` : ""}
${item.description ? `<div style="margin-top:4px; font-size:0.9em; color:var(--text-primary);">${escapeHtml(item.description)}</div>` : ""}
<div style="margin-top:8px; font-size:0.8em; display:flex; gap:8px; flex-wrap:wrap; align-items:center;">
${item.ssh_host ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); font-family:monospace;">SSH: ${escapeHtml(item.ssh_host)}</span>` : ""}
${item.healthcheck_endpoint ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); font-family:monospace;">Health: ${escapeHtml(item.healthcheck_endpoint)}</span>` : ""}
${item.repo_name ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">📦 ${escapeHtml(item.repo_name)}</span>` : ""}
</div>
`;
},
{
itemNoun: "Environments",
cardBorderColor: "#3498db",
defaultSort: "desc",
timestampExtractor: (item: any) => item.updated_at,
searchFields: (item: any) => [item.name, item.url, item.description, item.env_type, item.ssh_host, item.namespace],
}
);
// 2. Tool Fingerprints
loadGenericList(
"/api/env_fingerprints",
"fingerprints-container",
(item) => {
const toolVersions = item.tool_versions || {};
const tools = Object.entries(toolVersions).map(
([k, v]) => `<span style="background:var(--canvas-bg); padding:2px 8px; border-radius:4px; font-size:0.8em; font-family:monospace; border:1px solid var(--border-color);"><strong>${escapeHtml(k)}:</strong> ${escapeHtml(String(v))}</span>`
).join(" ");
return `
<div style="display:flex; justify-content:space-between; align-items:center; gap:8px;">
<div>
<strong style="font-size:1.05em; color:var(--text-primary);">💻 ${escapeHtml(item.os || "Unknown OS")} (${escapeHtml(item.shell || "shell")})</strong>
</div>
<span style="font-size:0.75em; color:var(--text-secondary); background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">${escapeHtml(item.namespace || "default")}</span>
</div>
${tools ? `<div style="margin-top:10px; display:flex; gap:6px; flex-wrap:wrap; align-items:center;">${tools}</div>` : '<div style="margin-top:6px; font-size:0.85em; color:var(--text-secondary);">No tool versions registered</div>'}
<div style="margin-top:8px; font-size:0.8em; color:var(--text-secondary);">
${item.repo_name ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); margin-right:8px;">📦 ${escapeHtml(item.repo_name)}</span>` : ""}
</div>
`;
},
{
itemNoun: "Fingerprints",
cardBorderColor: "#1abc9c",
defaultSort: "desc",
timestampExtractor: (item: any) => item.updated_at,
searchFields: (item: any) => [item.namespace, item.os, item.shell, item.repo_name, JSON.stringify(item.tool_versions || {})],
}
);
// 3. Environment Requirements
loadGenericList(
"/api/env_requirements",
"requirements-container",
(item) => {
const isSecret = item.is_secret;
return `
<div style="display:flex; justify-content:space-between; align-items:center; gap:8px;">
<div>
<span style="font-family:monospace; font-size:1.05em; font-weight:bold; color:var(--text-primary);">${escapeHtml(item.key)}</span>
${isSecret ? '<span style="background:#e74c3c; color:#fff; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; margin-left:6px;">🔒 Secret</span>' : '<span style="background:var(--canvas-bg); color:var(--text-secondary); border:1px solid var(--border-color); font-size:0.75em; padding:2px 6px; border-radius:4px; margin-left:6px;">Public</span>'}
</div>
<span style="font-size:0.75em; color:var(--text-secondary); background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">${escapeHtml(item.namespace || "default")}</span>
</div>
${item.description ? `<div style="margin-top:6px; font-size:0.9em; color:var(--text-primary);">${escapeHtml(item.description)}</div>` : ""}
<div style="margin-top:8px; font-size:0.8em; display:flex; gap:8px; flex-wrap:wrap; align-items:center;">
${item.default_value ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); font-family:monospace;">Default: ${escapeHtml(item.default_value)}</span>` : ""}
${item.validation_regex ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color); font-family:monospace;">Regex: ${escapeHtml(item.validation_regex)}</span>` : ""}
${item.repo_name ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">📦 ${escapeHtml(item.repo_name)}</span>` : ""}
</div>
`;
},
{
itemNoun: "Requirements",
cardBorderColor: "#e67e22",
defaultSort: "desc",
searchFields: (item: any) => [item.key, item.description, item.default_value, item.namespace, item.repo_name],
}
);
// 4. Quality & Authorization Gates
loadGenericList(
"/api/gates",
"gates-container",
(item) => {
const status = (item.status || "pending").toLowerCase();
let statusColor = "var(--accent-color)";
if (status === "authorized") statusColor = "var(--success-color)";
else if (status === "blocked") statusColor = "var(--error-color)";
return `
<div style="display:flex; justify-content:space-between; align-items:center; gap:8px;">
<div>
<strong style="font-size:1.05em; color:var(--text-primary);">${escapeHtml(item.action)} &rarr; ${escapeHtml(item.target)}</strong>
<span style="background:${statusColor}; color:#fff; font-size:0.75em; font-weight:bold; padding:2px 6px; border-radius:4px; margin-left:6px; text-transform:uppercase;">${escapeHtml(status)}</span>
</div>
<div style="display:flex; align-items:center; gap:6px;">
<span style="font-size:0.75em; color:var(--text-secondary); background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">${escapeHtml(item.namespace || "global")}</span>
${status !== "authorized" ? `<button class="tab-button" style="padding:2px 8px; font-size:0.75em; background:rgba(46,204,113,0.15); color:var(--success-color); border:1px solid var(--success-color); cursor:pointer;" onclick="authorizeGate('${escapeHtml(item.id)}')" title="Authorize Gate">✓ Authorize</button>` : ""}
${status !== "blocked" ? `<button class="tab-button" style="padding:2px 8px; font-size:0.75em; background:rgba(231,76,60,0.15); color:var(--error-color); border:1px solid var(--error-color); cursor:pointer;" onclick="blockGate('${escapeHtml(item.id)}')" title="Block Gate">🚫 Block</button>` : ""}
<button class="tab-button" style="padding:2px 6px; font-size:0.75em; background:var(--canvas-bg); color:var(--text-secondary); border:1px solid var(--border-color); cursor:pointer;" onclick="deleteGate('${escapeHtml(item.id)}')" title="Delete Gate">✕</button>
</div>
</div>
${item.reason ? `<div style="margin-top:6px; font-size:0.9em; color:var(--text-primary);">${escapeHtml(item.reason)}</div>` : ""}
<div style="margin-top:8px; font-size:0.8em; display:flex; gap:8px; flex-wrap:wrap; align-items:center;">
${item.gate_type ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">Type: ${escapeHtml(item.gate_type)}</span>` : ""}
${item.enforcer ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">Enforcer: ${escapeHtml(item.enforcer)}</span>` : ""}
${item.repo_name ? `<span style="background:var(--canvas-bg); padding:2px 6px; border-radius:4px; border:1px solid var(--border-color);">📦 ${escapeHtml(item.repo_name)}</span>` : ""}
</div>
`;
},
{
itemNoun: "Gates",
cardBorderColor: "#e74c3c",
defaultSort: "desc",
timestampExtractor: (item: any) => item.timestamp,
searchFields: (item: any) => [item.action, item.target, item.status, item.reason, item.enforcer],
}
);
}
function loadAllExtras(): void {
loadTerminal();
loadTechDebt();
@@ -2343,31 +2574,349 @@ function setupSSE(): void {
}
}
// --- Interactive Dashboard Controls ---
async function resolveTechDebt(id: string): Promise<void> {
try {
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("tech_debts/resolve", { id });
} else {
const res = await fetch(`/api/tech_debts/${encodeURIComponent(id)}/resolve`, { method: "POST" });
if (!res.ok) console.error("Failed to resolve tech debt", await res.text());
}
loadTechDebt();
} catch (err) {
console.error("Error resolving tech debt via WS, trying HTTP fallback", err);
try {
const res = await fetch(`/api/tech_debts/${encodeURIComponent(id)}/resolve`, { method: "POST" });
if (!res.ok) console.error("Failed to resolve tech debt", await res.text());
loadTechDebt();
} catch (fallbackErr) {
console.error("Error resolving tech debt", fallbackErr);
}
}
}
async function authorizeGate(id: string): Promise<void> {
try {
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("gates/authorize", { id });
} else {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}/authorize`, { method: "POST" });
if (!res.ok) console.error("Failed to authorize gate", await res.text());
}
loadEnvironment();
} catch (err) {
console.error("Error authorizing gate via WS, trying HTTP fallback", err);
try {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}/authorize`, { method: "POST" });
if (!res.ok) console.error("Failed to authorize gate", await res.text());
loadEnvironment();
} catch (fallbackErr) {
console.error("Error authorizing gate", fallbackErr);
}
}
}
async function blockGate(id: string): Promise<void> {
try {
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("gates/block", { id });
} else {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}/block`, { method: "POST" });
if (!res.ok) console.error("Failed to block gate", await res.text());
}
loadEnvironment();
} catch (err) {
console.error("Error blocking gate via WS, trying HTTP fallback", err);
try {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}/block`, { method: "POST" });
if (!res.ok) console.error("Failed to block gate", await res.text());
loadEnvironment();
} catch (fallbackErr) {
console.error("Error blocking gate", fallbackErr);
}
}
}
async function deleteGate(id: string): Promise<void> {
try {
if (activeWS && activeWS.readyState === WebSocket.OPEN) {
await sendWsRpc("gates/delete", { id });
} else {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}`, { method: "DELETE" });
if (!res.ok) console.error("Failed to delete gate", await res.text());
}
loadEnvironment();
} catch (err) {
console.error("Error deleting gate via WS, trying HTTP fallback", err);
try {
const res = await fetch(`/api/gates/${encodeURIComponent(id)}`, { method: "DELETE" });
if (!res.ok) console.error("Failed to delete gate", await res.text());
loadEnvironment();
} catch (fallbackErr) {
console.error("Error deleting gate", fallbackErr);
}
}
}
// --- Command Palette (Ctrl+K) ---
interface PaletteItem {
id: string;
category: "task" | "techdebt" | "adr" | "env" | "gate" | "nav";
icon: string;
title: string;
sub: string;
badge: string;
tabId: string;
}
let paletteItems: PaletteItem[] = [];
let filteredPaletteItems: PaletteItem[] = [];
let paletteSelectedIndex = 0;
let paletteDataLoaded = false;
async function preloadPaletteData(): Promise<void> {
try {
const [tasksRes, debtsRes, adrsRes, envsRes, gatesRes] = await Promise.all([
fetch("/api/tasks", { cache: "no-store" }).then((r) => r.json()).catch(() => []),
fetch("/api/tech_debts", { cache: "no-store" }).then((r) => r.json()).catch(() => []),
fetch("/api/adrs", { cache: "no-store" }).then((r) => r.json()).catch(() => []),
fetch("/api/environments", { cache: "no-store" }).then((r) => r.json()).catch(() => []),
fetch("/api/gates", { cache: "no-store" }).then((r) => r.json()).catch(() => []),
]);
const items: PaletteItem[] = [
{ id: "nav-activity", category: "nav", icon: "⚡", title: "Live Activity Feed", sub: "Inspect real-time agent signals & SSE", badge: "VIEW", tabId: "activity-tab" },
{ id: "nav-ledger", category: "nav", icon: "📖", title: "Code Ledger", sub: "View logged code changes & commits", badge: "VIEW", tabId: "ledger-tab" },
{ id: "nav-graph", category: "nav", icon: "🕸️", title: "Knowledge Graph Explorer", sub: "Explore nodes, relationships & dependencies", badge: "VIEW", tabId: "graph-tab" },
{ id: "nav-search", category: "nav", icon: "🔍", title: "Omni-Search", sub: "Full-text cross-entity search", badge: "VIEW", tabId: "search-tab" },
{ id: "nav-tasks", category: "nav", icon: "📋", title: "Task Board", sub: "HTN / DAG task hierarchies", badge: "VIEW", tabId: "task-tab" },
{ id: "nav-techdebt", category: "nav", icon: "⚡", title: "Tech Debt & Error Fixes", sub: "Known tech debt and error fixes", badge: "VIEW", tabId: "techdebt-tab" },
{ id: "nav-adrs", category: "nav", icon: "📜", title: "Architectural Decision Records", sub: "ADRs and design choices", badge: "VIEW", tabId: "adrs-tab" },
{ id: "nav-memos", category: "nav", icon: "📝", title: "Handoff Memos", sub: "Agent handoffs & session summaries", badge: "VIEW", tabId: "memos-tab" },
{ id: "nav-snippets", category: "nav", icon: "📎", title: "Code Snippets", sub: "Indexed snippets & patterns", badge: "VIEW", tabId: "snippets-tab" },
{ id: "nav-terminal", category: "nav", icon: "💻", title: "Terminal History", sub: "Command execution log & exit codes", badge: "VIEW", tabId: "terminal-tab" },
{ id: "nav-env", category: "nav", icon: "🌐", title: "Environment & Quality Gates", sub: "Deploy targets, fingerprints, requirements & gates", badge: "VIEW", tabId: "env-tab" },
];
if (Array.isArray(tasksRes)) {
tasksRes.forEach((t: any) => {
items.push({
id: t.id,
category: "task",
icon: t.status === "completed" ? "✓" : "📋",
title: t.title || t.id,
sub: t.description || `Task (${t.status || "pending"})`,
badge: t.status || "TASK",
tabId: "task-tab",
});
});
}
if (Array.isArray(debtsRes)) {
debtsRes.forEach((d: any) => {
items.push({
id: d.id,
category: "techdebt",
icon: "⚡",
title: `${d.id}: ${d.description || ""}`.trim(),
sub: d.file_path ? `${d.file_path} - Severity: ${d.severity || "medium"}` : `Severity: ${d.severity || "medium"}`,
badge: d.is_resolved ? "RESOLVED" : (d.severity || "DEBT"),
tabId: "techdebt-tab",
});
});
}
if (Array.isArray(adrsRes)) {
adrsRes.forEach((a: any) => {
items.push({
id: a.id,
category: "adr",
icon: "📜",
title: `${a.id}: ${a.title || ""}`.trim(),
sub: a.decision || `Status: ${a.status || "accepted"}`,
badge: a.status || "ADR",
tabId: "adrs-tab",
});
});
}
if (Array.isArray(envsRes)) {
envsRes.forEach((e: any) => {
items.push({
id: e.name || e.url,
category: "env",
icon: "🌐",
title: e.name || "Target Environment",
sub: `${e.url || ""} ${e.description ? "• " + e.description : ""}`.trim(),
badge: e.env_type || "ENV",
tabId: "env-tab",
});
});
}
if (Array.isArray(gatesRes)) {
gatesRes.forEach((g: any) => {
items.push({
id: g.id,
category: "gate",
icon: g.status === "authorized" ? "🟢" : (g.status === "blocked" ? "🔴" : "🟡"),
title: `${g.action} → ${g.target}`,
sub: g.reason || `Enforcer: ${g.enforcer || "gate"}`,
badge: g.status || "GATE",
tabId: "env-tab",
});
});
}
paletteItems = items;
paletteDataLoaded = true;
} catch (err) {
console.error("Failed to preload palette data", err);
}
}
function openCommandPalette(): void {
const backdrop = document.getElementById("cmd-palette-backdrop");
const input = document.getElementById("palette-input") as HTMLInputElement | null;
if (!backdrop || !input) return;
backdrop.style.display = "flex";
input.value = "";
paletteSelectedIndex = 0;
preloadPaletteData().then(() => {
onPaletteSearch("");
input.focus();
});
}
function closeCommandPalette(): void {
const backdrop = document.getElementById("cmd-palette-backdrop");
if (backdrop) backdrop.style.display = "none";
}
function onPaletteBackdropClick(e: MouseEvent): void {
if (e.target === document.getElementById("cmd-palette-backdrop")) {
closeCommandPalette();
}
}
function onPaletteSearch(query: string): void {
const q = query.toLowerCase().trim();
if (!q) {
filteredPaletteItems = paletteItems.slice(0, 30);
} else {
filteredPaletteItems = paletteItems
.filter((item) =>
item.title.toLowerCase().includes(q) ||
item.sub.toLowerCase().includes(q) ||
item.badge.toLowerCase().includes(q) ||
item.category.toLowerCase().includes(q)
)
.slice(0, 40);
}
paletteSelectedIndex = 0;
renderPaletteResults();
}
function renderPaletteResults(): void {
const container = document.getElementById("palette-results");
const countEl = document.getElementById("palette-count");
if (!container) return;
if (countEl) countEl.innerText = `${filteredPaletteItems.length} results`;
if (filteredPaletteItems.length === 0) {
container.innerHTML = '<li class="enterprise-empty-state" style="margin: 10px 18px;">No matching items found.</li>';
return;
}
container.innerHTML = filteredPaletteItems
.map((item, idx) => {
const isSel = idx === paletteSelectedIndex;
return `
<li class="palette-item ${isSel ? "selected" : ""}" data-index="${idx}" onclick="selectPaletteItem(${idx})">
<div class="palette-item-left">
<span class="palette-item-icon">${escapeHtml(item.icon)}</span>
<div class="palette-item-text">
<span class="palette-item-title">${escapeHtml(item.title)}</span>
<span class="palette-item-sub">${escapeHtml(item.sub)}</span>
</div>
</div>
<span class="palette-item-badge">${escapeHtml(item.badge)}</span>
</li>
`;
})
.join("");
const selectedEl = container.querySelector(`.palette-item[data-index="${paletteSelectedIndex}"]`) as HTMLElement | null;
if (selectedEl) {
selectedEl.scrollIntoView({ block: "nearest" });
}
}
function selectPaletteItem(idx: number): void {
const item = filteredPaletteItems[idx];
if (!item) return;
closeCommandPalette();
const tabBtn = document.querySelector(`button[onclick*="${item.tabId}"]`) as HTMLElement | null;
if (tabBtn) {
switchTab(item.tabId, tabBtn);
}
}
function onPaletteKeyDown(e: KeyboardEvent): void {
if (e.key === "ArrowDown") {
e.preventDefault();
if (filteredPaletteItems.length > 0) {
paletteSelectedIndex = (paletteSelectedIndex + 1) % filteredPaletteItems.length;
renderPaletteResults();
}
} else if (e.key === "ArrowUp") {
e.preventDefault();
if (filteredPaletteItems.length > 0) {
paletteSelectedIndex = (paletteSelectedIndex - 1 + filteredPaletteItems.length) % filteredPaletteItems.length;
renderPaletteResults();
}
} else if (e.key === "Enter") {
e.preventDefault();
selectPaletteItem(paletteSelectedIndex);
} else if (e.key === "Escape") {
e.preventDefault();
closeCommandPalette();
}
}
document.addEventListener("keydown", function (e: KeyboardEvent) {
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "k") {
e.preventDefault();
const searchTabBtn = document.querySelector(
'button[onclick*="search-tab"]',
) as HTMLElement | null;
if (searchTabBtn) {
switchTab("search-tab", searchTabBtn);
}
const searchInput = document.getElementById(
"search-box",
) as HTMLInputElement | null;
if (searchInput) {
searchInput.focus();
searchInput.select();
const backdrop = document.getElementById("cmd-palette-backdrop");
if (backdrop && backdrop.style.display === "flex") {
closeCommandPalette();
} else {
openCommandPalette();
}
return;
}
if (e.key === "Escape") {
const backdrop = document.getElementById("cmd-palette-backdrop");
if (backdrop && backdrop.style.display === "flex") {
closeCommandPalette();
return;
}
}
// Pressing '/' focuses the current active list search input if not already typing
if (
e.key === "/" &&
document.activeElement?.tagName !== "INPUT" &&
document.activeElement?.tagName !== "TEXTAREA"
) {
const backdrop = document.getElementById("cmd-palette-backdrop");
if (backdrop && backdrop.style.display === "flex") return;
const activeSearch = document.querySelector(
".tab-content.active .enterprise-search-input, .tab-content.active #task-filter-input",
) as HTMLInputElement | null;
@@ -2405,6 +2954,9 @@ function refreshActiveTab(): void {
case "ledger-tab":
loadLedger();
break;
case "env-tab":
loadEnvironment();
break;
case "activity-tab":
loadActivityHistory();
break;
@@ -2437,6 +2989,7 @@ Object.assign(window, {
requestDomainRefresh,
handleIncomingActivity,
setupWS,
sendWsRpc,
loadGenericList,
onListSearchInput,
onListClearSearch,
@@ -2452,6 +3005,17 @@ Object.assign(window, {
loadMemos,
loadSnippets,
loadAllExtras,
loadEnvironment,
resolveTechDebt,
authorizeGate,
blockGate,
deleteGate,
openCommandPalette,
closeCommandPalette,
onPaletteBackdropClick,
onPaletteSearch,
onPaletteKeyDown,
selectPaletteItem,
testClipboard,
loadVersion,
setupSSE,
+5 -60
View File
@@ -1,5 +1,5 @@
use crate::store::STORE_TABLE;
use redb::{Database, ReadableTable};
use redb::Database;
use std::path::Path;
use std::sync::Arc;
@@ -117,77 +117,22 @@ pub fn init_redb(base: &Path) -> Arc<Database> {
}
};
// Ensure the table exists and migrate legacy JSON files
// Ensure the table exists
match db.begin_write() {
Ok(write_txn) => {
let mut opened_ok = false;
if let Ok(mut table) = write_txn.open_table(STORE_TABLE) {
if let Ok(_) = write_txn.open_table(STORE_TABLE) {
opened_ok = true;
if !is_in_memory {
let stores = vec![
("knowledge_graph_master", "knowledge_graph_master.json"),
("audit_ledger", "audit_ledger.json"),
("tasks", "tasks.json"),
("snippets", "snippets.json"),
("adrs", "adrs.json"),
("error_fixes", "error_fixes.json"),
("session_summaries", "session_summaries.json"),
("handoff_memos", "handoff_memos.json"),
("env_fingerprints", "env_fingerprints.json"),
("env_requirements", "env_requirements.json"),
("milestones", "milestones.json"),
("environments", "environments.json"),
("tech_debts", "tech_debts.json"),
("gates", "gates.json"),
("state_snapshots", "state_snapshots.json"),
("hypotheses", "hypotheses.json"),
("agent_signals", "agent_signals.json"),
];
for (key, file_name) in stores.iter() {
let is_missing = match table.get(*key) {
Ok(res) => res.is_none(),
Err(e) => {
tracing::warn!("Failed to read key '{}' from redb: {}", key, e);
false
}
};
if is_missing {
let json_path = base.join(file_name);
if json_path.exists()
&& let Ok(data) = std::fs::read(&json_path)
&& serde_json::from_slice::<serde_json::Value>(&data).is_ok()
{
if let Err(e) = table.insert(*key, data.as_slice()) {
tracing::error!(
"Failed to insert migrated key '{}': {}",
key,
e
);
} else {
let migrated_path = json_path.with_extension("json.migrated");
if let Err(_) = std::fs::rename(&json_path, &migrated_path) {
if migrated_path.exists() {
let _ = std::fs::remove_file(&migrated_path);
let _ = std::fs::rename(&json_path, &migrated_path);
}
}
}
}
}
}
}
}
if opened_ok {
if let Err(e) = write_txn.commit() {
tracing::error!("Failed to commit database migration transaction: {}", e);
tracing::error!("Failed to commit database initialization transaction: {}", e);
}
}
}
Err(e) => {
tracing::error!(
"Failed to begin write transaction for redb migration: {}",
"Failed to begin write transaction for redb initialization: {}",
e
);
}
+5 -8
View File
@@ -193,14 +193,14 @@ fn fallback_embed(text: &str) -> Vec<f32> {
}
enum EmbeddingModel {
Candle(CandleEmbeddingModel),
Candle(Box<CandleEmbeddingModel>),
Fallback,
}
impl EmbeddingModel {
fn new() -> Self {
match CandleEmbeddingModel::new() {
Ok(model) => EmbeddingModel::Candle(model),
Ok(model) => EmbeddingModel::Candle(Box::new(model)),
Err(e) => {
tracing::warn!(
"Failed to initialize Candle BERT model ({e}); falling back to deterministic offline embeddings."
@@ -259,13 +259,10 @@ pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
let mut norm_a_sq = 0.0f32;
let mut norm_b_sq = 0.0f32;
let chunks_a = a.chunks_exact(8);
let chunks_b = b.chunks_exact(8);
let (chunks_a, remainder_a) = a.as_chunks::<8>();
let (chunks_b, remainder_b) = b.as_chunks::<8>();
let remainder_a = chunks_a.remainder();
let remainder_b = chunks_b.remainder();
for (ca, cb) in chunks_a.zip(chunks_b) {
for (ca, cb) in chunks_a.iter().zip(chunks_b.iter()) {
for i in 0..8 {
let x = ca[i];
let y = cb[i];
+20 -3
View File
@@ -17,7 +17,11 @@ pub enum AppError {
EntityNotFound { name: String, namespace: String },
#[error("Invalid relation between '{from}' and '{to}': {reason}")]
InvalidRelation { from: String, to: String, reason: String },
InvalidRelation {
from: String,
to: String,
reason: String,
},
#[error("Database error for key '{key}': {message}")]
DatabaseError { key: String, message: String },
@@ -36,6 +40,9 @@ pub enum AppError {
#[error("I/O Error: {0}")]
IoError(#[from] std::io::Error),
#[error("YAML Serialization Error: {0}")]
YamlError(#[from] serde_yaml::Error),
}
impl From<AppError> for String {
@@ -50,11 +57,17 @@ impl IntoResponse for AppError {
AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg.clone()),
AppError::EntityNotFound { name, namespace } => (
StatusCode::NOT_FOUND,
format!("Entity '{}' was not found in namespace '{}'", name, namespace),
format!(
"Entity '{}' was not found in namespace '{}'",
name, namespace
),
),
AppError::InvalidRelation { from, to, reason } => (
StatusCode::BAD_REQUEST,
format!("Invalid relation between '{}' and '{}': {}", from, to, reason),
format!(
"Invalid relation between '{}' and '{}': {}",
from, to, reason
),
),
AppError::DatabaseError { key, message } => (
StatusCode::INTERNAL_SERVER_ERROR,
@@ -71,6 +84,10 @@ impl IntoResponse for AppError {
StatusCode::INTERNAL_SERVER_ERROR,
format!("I/O Error: {}", e),
),
AppError::YamlError(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("YAML Serialization Error: {}", e),
),
};
let body = Json(json!({
+11 -11
View File
@@ -119,10 +119,10 @@ impl McpTool for ReadFileSkeletonHandler {
header.push('\n');
}
}
if header.is_empty() {
if let Some(first_line) = node_text.lines().next() {
header = first_line.to_string();
}
if header.is_empty()
&& let Some(first_line) = node_text.lines().next()
{
header = first_line.to_string();
}
out.push_str(&indent);
@@ -425,10 +425,10 @@ fn scan_workspace_for_symbol(
if scanned_files > 500 {
break;
}
if let Ok(meta) = std::fs::metadata(path) {
if meta.len() > 1024 * 1024 {
continue;
}
if let Ok(meta) = std::fs::metadata(path)
&& meta.len() > 1024 * 1024
{
continue;
}
if let Ok(content) = std::fs::read_to_string(path) {
for (line_num, line) in content.lines().enumerate() {
@@ -506,7 +506,7 @@ impl McpTool for FindSymbolReferencesHandler {
matches.extend(disk_matches);
}
Ok(serde_json::to_string_pretty(&matches)?)
Ok(serde_yaml::to_string(&matches)?)
}
}
@@ -562,7 +562,7 @@ impl McpTool for GetCallersHandler {
callers.extend(disk_callers);
}
Ok(serde_json::to_string_pretty(&callers)?)
Ok(serde_yaml::to_string(&callers)?)
}
}
@@ -651,7 +651,7 @@ impl McpTool for AnalyzeImpactHandler {
}
});
Ok(serde_json::to_string_pretty(&result)?)
Ok(serde_yaml::to_string(&result)?)
}
}
+42 -18
View File
@@ -22,17 +22,40 @@ impl McpTool for EnvironmentHandler {
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> crate::error::Result<String> {
let req: EnvironmentTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
let ns = req.namespace.unwrap_or_else(|| crate::models::default_namespace());
let ns = req
.namespace
.unwrap_or_else(crate::models::default_namespace);
match req.action {
EnvAction::UpdateFingerprint => {
let tool_versions = req.tool_versions.unwrap_or_default();
let detected_os = req.os.unwrap_or_else(|| std::env::consts::OS.to_string());
let detected_shell = req
.shell
.or_else(|| std::env::var("SHELL").ok())
.or_else(|| std::env::var("STARSHIP_SHELL").ok())
.or_else(|| {
if std::env::var("PSModulePath").is_ok() {
Some("pwsh".to_string())
} else {
None
}
})
.or_else(|| {
if std::env::var("COMSPEC").is_ok() {
Some("cmd".to_string())
} else {
None
}
})
.unwrap_or_else(|| "unknown".to_string());
state.env.env_fingerprints.modify(|fps| {
fps.insert(
ns.clone(),
crate::models::EnvFingerprint {
namespace: ns.clone(),
os: std::env::consts::OS.to_string(),
shell: std::env::var("SHELL").unwrap_or_else(|_| "unknown".to_string()),
os: detected_os,
shell: detected_shell,
tool_versions,
updated_at: crate::handlers::utils::now_secs(),
..Default::default()
@@ -47,7 +70,7 @@ impl McpTool for EnvironmentHandler {
.env_fingerprints
.read_with(|fps| fps.get(&ns).cloned());
if let Some(fp) = data {
let data = Ok::<String, crate::error::AppError>(serde_json::to_string(&fp)?)?;
let data = Ok::<String, crate::error::AppError>(serde_yaml::to_string(&fp)?)?;
Ok(data)
} else {
Ok("{}".to_string())
@@ -94,11 +117,8 @@ impl McpTool for EnvironmentHandler {
}
EnvAction::GetDetails => {
let data = state.env.environments.read_with(|envs| {
let filtered: Vec<_> = envs
.iter()
.filter(|e| e.namespace == ns)
.collect();
Ok::<String, crate::error::AppError>(serde_json::to_string(&filtered)?)
let filtered: Vec<_> = envs.iter().filter(|e| e.namespace == ns).collect();
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&filtered)?)
})?;
Ok(data)
}
@@ -106,7 +126,6 @@ impl McpTool for EnvironmentHandler {
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -135,7 +154,10 @@ mod tests {
assert_eq!(res, "Env fingerprint updated");
let res2 = handler
.execute(json!({"action": "read_fingerprint", "namespace": "global"}), state.clone())
.execute(
json!({"action": "read_fingerprint", "namespace": "global"}),
state.clone(),
)
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
.unwrap();
@@ -163,7 +185,10 @@ mod tests {
let handler = EnvironmentHandler;
let res = handler
.execute(json!({"action": "get_details", "namespace": "global"}), state.clone())
.execute(
json!({"action": "get_details", "namespace": "global"}),
state.clone(),
)
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
.unwrap();
@@ -207,7 +232,10 @@ mod tests {
assert_eq!(res2, "Environment registered");
let res3 = handler
.execute(json!({"action": "get_details", "namespace": "global"}), state.clone())
.execute(
json!({"action": "get_details", "namespace": "global"}),
state.clone(),
)
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
.unwrap();
@@ -229,12 +257,8 @@ mod tests {
}
});
let res = handler
.execute(args, state.clone())
.await;
let res = handler.execute(args, state.clone()).await;
assert!(res.is_ok());
}
}
+23 -24
View File
@@ -87,7 +87,7 @@ impl McpTool for GetActiveWorktreeContextHandler {
.await
.map_err(|e| crate::error::AppError::Internal(format!("Task panic: {}", e)))??;
Ok::<String, crate::error::AppError>(serde_json::to_string_pretty(&result)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&result)?)
}
}
@@ -139,25 +139,25 @@ impl McpTool for QueryGitDiffsHandler {
let git_matches = tokio::task::spawn_blocking(move || {
let mut results = Vec::new();
let cwd = env::current_dir().unwrap_or_default();
if let Ok(repo) = git2::Repository::discover(&cwd) {
if let Ok(mut revwalk) = repo.revwalk() {
let _ = revwalk.push_head();
let mut count = 0;
for oid in revwalk.flatten() {
if count >= remaining {
break;
}
if let Ok(commit) = repo.find_commit(oid) {
let summary = commit.summary().unwrap_or("");
if summary.to_lowercase().contains(&q) {
count += 1;
results.push(json!({
"commit_id": oid.to_string(),
"author": commit.author().name().unwrap_or("unknown"),
"message": summary,
"timestamp": commit.time().seconds(),
}));
}
if let Ok(repo) = git2::Repository::discover(&cwd)
&& let Ok(mut revwalk) = repo.revwalk()
{
let _ = revwalk.push_head();
let mut count = 0;
for oid in revwalk.flatten() {
if count >= remaining {
break;
}
if let Ok(commit) = repo.find_commit(oid) {
let summary = commit.summary().unwrap_or("");
if summary.to_lowercase().contains(&q) {
count += 1;
results.push(json!({
"commit_id": oid.to_string(),
"author": commit.author().name().unwrap_or("unknown"),
"message": summary,
"timestamp": commit.time().seconds(),
}));
}
}
}
@@ -169,7 +169,7 @@ impl McpTool for QueryGitDiffsHandler {
matches.extend(git_matches);
}
Ok(serde_json::to_string_pretty(&matches)?)
Ok(serde_yaml::to_string(&matches)?)
}
}
@@ -192,7 +192,7 @@ mod tests {
.map_err(|e| format!("Failed to get worktree context: {}", e))
.unwrap();
let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
let parsed: serde_json::Value = serde_yaml::from_str(&result).unwrap();
assert!(parsed.get("branch").is_some());
assert!(parsed.get("modified_files").is_some());
assert!(parsed.get("diff").is_some());
@@ -211,8 +211,7 @@ mod tests {
.map_err(|e| format!("Failed to get worktree context: {}", e))
.unwrap();
let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
let parsed: serde_json::Value = serde_yaml::from_str(&result).unwrap();
assert!(parsed.get("branch").is_some() || parsed.is_object());
}
}
+177 -89
View File
@@ -62,15 +62,15 @@ impl<'a> GraphQueryBuilder<'a> {
break;
}
nodes_at_current_depth -= 1;
if current_depth < self.max_depth {
if let Some(neighbors) = adj.get(current) {
for &(neighbor, rel_type, is_inverse) in neighbors {
if !visited.contains(neighbor) {
visited.insert(neighbor);
parents.insert(neighbor, (current, rel_type, is_inverse));
queue.push_back(neighbor);
nodes_at_next_depth += 1;
}
if current_depth < self.max_depth
&& let Some(neighbors) = adj.get(current)
{
for &(neighbor, rel_type, is_inverse) in neighbors {
if !visited.contains(neighbor) {
visited.insert(neighbor);
parents.insert(neighbor, (current, rel_type, is_inverse));
queue.push_back(neighbor);
nodes_at_next_depth += 1;
}
}
}
@@ -138,7 +138,9 @@ impl McpTool for QueryGraphPathHandler {
})
})
.await
.map_err(|e| crate::error::AppError::Internal(format!("Graph traversal task failed: {}", e)))?
.map_err(|e| {
crate::error::AppError::Internal(format!("Graph traversal task failed: {}", e))
})?
}
}
@@ -169,12 +171,19 @@ impl McpTool for CreateEntitiesHandler {
}
}
});
let names: Vec<String> = inserted.iter().map(|e| format!("{} ({})", e.name, e.entity_type)).collect();
let names: Vec<String> = inserted
.iter()
.map(|e| format!("{} ({})", e.name, e.entity_type))
.collect();
if !inserted.is_empty() {
let idx = state.get_search_index().await;
let _ = idx.index_entities_batch(&inserted).await;
}
Ok(format!("Successfully created {} entity/entities: {}", names.len(), names.join(", ")))
Ok(format!(
"Successfully created {} entity/entities: {}",
names.len(),
names.join(", ")
))
}
}
@@ -236,7 +245,9 @@ impl McpTool for CreateRelationsHandler {
crate::models::Entity {
name: node_name.clone(),
entity_type: "Entity".to_string(),
observations: vec!["Auto-created stub entity for relation endpoint".to_string()],
observations: vec![
"Auto-created stub entity for relation endpoint".to_string(),
],
namespace: crate::models::default_namespace(),
git_branch: None,
..Default::default()
@@ -247,16 +258,28 @@ impl McpTool for CreateRelationsHandler {
}
for mut relation in req.relations {
if !relation.from.is_empty() && !relation.to.is_empty() {
relation.relation_type = crate::models::normalize_relation_type(&relation.relation_type);
added_relations.push(format!("{} -[{}]-> {}", relation.from, relation.relation_type, relation.to));
relation.relation_type =
crate::models::normalize_relation_type(&relation.relation_type);
added_relations.push(format!(
"{} -[{}]-> {}",
relation.from, relation.relation_type, relation.to
));
g.relations.push(relation);
}
}
});
let mut msg = format!("Successfully created {} relation(s):\n{}", added_relations.len(), added_relations.join("\n"));
let mut msg = format!(
"Successfully created {} relation(s):\n{}",
added_relations.len(),
added_relations.join("\n")
);
if !auto_created.is_empty() {
msg.push_str(&format!("\nNote: Auto-created {} missing stub entity/entities: {}", auto_created.len(), auto_created.join(", ")));
msg.push_str(&format!(
"\nNote: Auto-created {} missing stub entity/entities: {}",
auto_created.len(),
auto_created.join(", ")
));
}
Ok(msg)
}
@@ -462,7 +485,7 @@ impl McpTool for ReadGraphHandler {
let filtered_entities: Vec<(&String, &crate::models::Entity)> = full
.entities
.iter()
.filter(|(_, v)| req.namespace.as_ref().map_or(true, |ns| &v.namespace == ns))
.filter(|(_, v)| req.namespace.as_ref().is_none_or(|ns| &v.namespace == ns))
.collect();
let total_count = filtered_entities.len();
@@ -481,7 +504,7 @@ impl McpTool for ReadGraphHandler {
.relations
.iter()
.filter(|r| {
let ns_match = req.namespace.as_ref().map_or(true, |ns| &r.namespace == ns);
let ns_match = req.namespace.as_ref().is_none_or(|ns| &r.namespace == ns);
ns_match && (paged_entities.contains_key(&r.from) || paged_entities.contains_key(&r.to))
})
.collect();
@@ -499,7 +522,7 @@ impl McpTool for ReadGraphHandler {
let rel_vec: Vec<&crate::models::Relation> = full
.relations
.iter()
.filter(|r| req.namespace.as_ref().map_or(true, |ns| &r.namespace == ns))
.filter(|r| req.namespace.as_ref().is_none_or(|ns| &r.namespace == ns))
.collect();
(ent_map, rel_vec, None)
};
@@ -520,7 +543,7 @@ impl McpTool for ReadGraphHandler {
})
};
let mut result_json = serde_json::to_string(&serialized)?;
let mut result_json = serde_yaml::to_string(&serialized)?;
if let Some(max_tok) = max_tokens {
let max_chars = max_tok * 4;
@@ -549,21 +572,19 @@ impl McpTool for ReadGraphHandler {
}
});
if let Some(meta) = &meta_json {
if let Some(obj) = truncated_val.as_object_mut() {
if let Some(meta) = &meta_json
&& let Some(obj) = truncated_val.as_object_mut() {
obj.insert("total_count".to_string(), meta["total_count"].clone());
obj.insert("offset".to_string(), meta["offset"].clone());
obj.insert("limit".to_string(), meta["limit"].clone());
obj.insert("has_more".to_string(), serde_json::Value::Bool(true));
}
}
if let Ok(trunc_str) = serde_json::to_string(&truncated_val) {
if trunc_str.len() <= max_chars || keep_count == 1 {
if let Ok(trunc_str) = serde_yaml::to_string(&truncated_val)
&& (trunc_str.len() <= max_chars || keep_count == 1) {
result_json = trunc_str;
break;
}
}
keep_count = keep_count.saturating_sub(keep_count / 3 + 1);
}
}
@@ -618,7 +639,7 @@ impl McpTool for SearchNodesHandler {
}
}
}
Ok::<String, crate::error::AppError>(serde_json::to_string(&matched_entities)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&matched_entities)?)
})?;
Ok(data)
}
@@ -659,7 +680,7 @@ impl McpTool for OpenNodesHandler {
result.entities.insert(name, e);
}
}
Ok::<String, crate::error::AppError>(serde_json::to_string(&result)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&result)?)
})?;
Ok(data)
}
@@ -804,7 +825,7 @@ impl McpTool for CondenseEntityHandler {
req.entity_name,
unique_obs.join("\n- ")
);
if let Ok(summary) = state.ollama.generate(&prompt, None, None).await {
if let Ok(summary) = state.ollama.generate(&prompt, None, None, None).await {
let lines: Vec<String> = summary
.lines()
.map(|l| l.trim().trim_start_matches('-').trim().to_string())
@@ -820,7 +841,10 @@ impl McpTool for CondenseEntityHandler {
e.observations = unique_obs;
}
});
return Ok(format!("Entity '{}' observations condensed with deduplication/AI summary.", req.entity_name));
return Ok(format!(
"Entity '{}' observations condensed with deduplication/AI summary.",
req.entity_name
));
}
state.modify_graph(|master| {
@@ -931,7 +955,7 @@ impl McpTool for FindOrphansHandler {
.cloned()
.collect::<Vec<String>>()
});
let data = Ok::<String, crate::error::AppError>(serde_json::to_string(&orphans)?)?;
let data = Ok::<String, crate::error::AppError>(serde_yaml::to_string(&orphans)?)?;
Ok(data)
}
}
@@ -953,10 +977,9 @@ impl McpTool for GetSubgraphHandler {
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> crate::error::Result<String> {
let req: GetSubgraphTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
let root = req
.root_entity
.or(req.root_node)
.ok_or_else(|| crate::error::AppError::Internal("root_entity or root_node is required".to_string()))?;
let root = req.root_entity.ok_or_else(|| {
crate::error::AppError::Internal("root_entity is required".to_string())
})?;
let depth = req.depth.unwrap_or(2);
let format = req.format.unwrap_or(SubgraphFormat::Json);
@@ -974,12 +997,12 @@ impl McpTool for GetSubgraphHandler {
visited.insert(root.clone());
while let Some((curr, d)) = queue.pop_front() {
if d < depth {
if let Some(neighbors) = adj.get(curr) {
for &next in neighbors {
if visited.insert(next.to_string()) {
queue.push_back((next, d + 1));
}
if d < depth
&& let Some(neighbors) = adj.get(curr)
{
for &next in neighbors {
if visited.insert(next.to_string()) {
queue.push_back((next, d + 1));
}
}
}
@@ -1011,19 +1034,22 @@ impl McpTool for GetSubgraphHandler {
markdown.push_str("\n## Relations\n");
for r in &matched_relations {
markdown.push_str(&format!("- `{}` --[{}]--> `{}`\n", r.from, r.relation_type, r.to));
markdown.push_str(&format!(
"- `{}` --[{}]--> `{}`\n",
r.from, r.relation_type, r.to
));
}
return Ok(markdown);
}
let result = serde_json::json!({
"root_node": root,
"root_entity": root,
"depth": depth,
"entities": matched_entities,
"relations": matched_relations,
});
Ok(serde_json::to_string_pretty(&result)?)
Ok(serde_yaml::to_string(&result)?)
}
}
@@ -1038,7 +1064,10 @@ impl McpTool for SweepGraphHealthHandler {
}
fn schema(&self) -> Value {
crate::mcp::tool_def::<SweepGraphHealthTool>("sweep_graph_health", "Audit and sweep graph health")
crate::mcp::tool_def::<SweepGraphHealthTool>(
"sweep_graph_health",
"Audit and sweep graph health",
)
}
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> crate::error::Result<String> {
@@ -1119,7 +1148,7 @@ impl McpTool for SweepGraphHealthHandler {
"health_score": if orphans.is_empty() && duplicates.is_empty() && stale_entities.is_empty() { "100%" } else { "Needs Maintenance" }
});
Ok(serde_json::to_string_pretty(&report)?)
Ok(serde_yaml::to_string(&report)?)
}
}
@@ -1132,26 +1161,33 @@ impl McpTool for ResolveStaleSymbolsHandler {
}
fn schema(&self) -> Value {
crate::mcp::tool_def::<ResolveStaleSymbolsTool>("resolve_stale_symbols", "Inspect Knowledge Graph entities and tech debt symbol/line references against files on disk, healing stale pointers.")
crate::mcp::tool_def::<ResolveStaleSymbolsTool>(
"resolve_stale_symbols",
"Inspect Knowledge Graph entities and tech debt symbol/line references against files on disk, healing stale pointers.",
)
}
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> crate::error::Result<String> {
let req: ResolveStaleSymbolsTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
let req: ResolveStaleSymbolsTool =
serde_json::from_value(args).map_err(|e| e.to_string())?;
let auto_heal = req.auto_heal.unwrap_or(true);
let mut checked = 0;
let mut stale_entities = Vec::new();
state.modify_graph(|g| {
for (name, _entity) in g.entities.iter_mut() {
if let Some(target) = &req.target {
if name != target && !name.contains(target) {
continue;
}
for name in g.entities.keys() {
if let Some(target) = &req.target
&& name != target
&& !name.contains(target)
{
continue;
}
checked += 1;
let path = std::path::Path::new(name);
if (name.contains('/') || name.contains('\\') || name.contains('.')) && !path.exists() {
if (name.contains('/') || name.contains('\\') || name.contains('.'))
&& !path.exists()
{
stale_entities.push(name.clone());
}
}
@@ -1171,7 +1207,7 @@ impl McpTool for ResolveStaleSymbolsHandler {
"status": "success"
});
Ok(serde_json::to_string_pretty(&report)?)
Ok(serde_yaml::to_string(&report)?)
}
}
@@ -1184,7 +1220,10 @@ impl McpTool for SummarizeSubgraphHandler {
}
fn schema(&self) -> Value {
crate::mcp::tool_def::<SummarizeSubgraphTool>("summarize_subgraph", "Generate compact, LLM-optimized Markdown topology of a graph component capped within a token budget.")
crate::mcp::tool_def::<SummarizeSubgraphTool>(
"summarize_subgraph",
"Generate compact, LLM-optimized Markdown topology of a graph component capped within a token budget.",
)
}
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> crate::error::Result<String> {
@@ -1212,8 +1251,18 @@ impl McpTool for SummarizeSubgraphHandler {
}
}
let ent: Vec<_> = g.entities.values().filter(|e| visited.contains(&e.name)).cloned().collect();
let rel: Vec<_> = g.relations.iter().filter(|r| visited.contains(&r.from) && visited.contains(&r.to)).cloned().collect();
let ent: Vec<_> = g
.entities
.values()
.filter(|e| visited.contains(&e.name))
.cloned()
.collect();
let rel: Vec<_> = g
.relations
.iter()
.filter(|r| visited.contains(&r.from) && visited.contains(&r.to))
.cloned()
.collect();
(ent, rel)
});
@@ -1228,7 +1277,10 @@ impl McpTool for SummarizeSubgraphHandler {
markdown.push_str("\n## Relations\n");
for r in &sub_relations {
markdown.push_str(&format!("- `{}` --[{}]--> `{}`\n", r.from, r.relation_type, r.to));
markdown.push_str(&format!(
"- `{}` --[{}]--> `{}`\n",
r.from, r.relation_type, r.to
));
}
let max_tokens = req.max_tokens.unwrap_or(1000);
@@ -1249,7 +1301,6 @@ mod tests {
use crate::handlers::meta::AgentSignalsHandler;
use serde_json::json;
#[tokio::test]
async fn test_create_and_read_entities() {
let state = Arc::new(MemoryState::new_in_memory());
@@ -1324,10 +1375,7 @@ mod tests {
{"source": "A", "target": "B", "relationType": "knows"}
]
});
let alias_res = handler
.execute(alias_args, state.clone())
.await
.unwrap();
let alias_res = handler.execute(alias_args, state.clone()).await.unwrap();
assert!(alias_res.contains("Successfully created 1 relation(s)"));
// Test semantic LLM schema feedback on missing fields
@@ -1499,7 +1547,10 @@ mod tests {
{"name": "Beta", "entity_type": "Node", "observations": ["Obs3"], "namespace": "global"}
]
});
create_handler.execute(args_ent, state.clone()).await.unwrap();
create_handler
.execute(args_ent, state.clone())
.await
.unwrap();
let rel_handler = CreateRelationsHandler;
let args_rel = json!({
@@ -1510,57 +1561,94 @@ mod tests {
rel_handler.execute(args_rel, state.clone()).await.unwrap();
let path_handler = QueryGraphPathHandler;
let path_res = path_handler.execute(json!({"start_node": "Alpha", "end_node": "Beta"}), state.clone()).await.unwrap();
let path_res = path_handler
.execute(
json!({"start_node": "Alpha", "end_node": "Beta"}),
state.clone(),
)
.await
.unwrap();
assert!(path_res.contains("Alpha"));
let sub_handler = GetSubgraphHandler;
let sub_res = sub_handler.execute(json!({"root_entity": "Alpha"}), state.clone()).await.unwrap();
let sub_res = sub_handler
.execute(json!({"root_entity": "Alpha"}), state.clone())
.await
.unwrap();
assert!(sub_res.contains("Alpha"));
let sum_handler = SummarizeSubgraphHandler;
let sum_res = sum_handler.execute(json!({"root_entity": "Alpha"}), state.clone()).await.unwrap();
let sum_res = sum_handler
.execute(json!({"root_entity": "Alpha"}), state.clone())
.await
.unwrap();
assert!(!sum_res.is_empty());
let sweep_handler = SweepGraphHealthHandler;
let sweep_res = sweep_handler.execute(json!({}), state.clone()).await.unwrap();
let sweep_res = sweep_handler
.execute(json!({}), state.clone())
.await
.unwrap();
assert!(!sweep_res.is_empty());
let del_obs_handler = DeleteObservationsHandler;
let del_obs_res = del_obs_handler.execute(json!({
"deletions": [{"entity_name": "Alpha", "observations": ["Obs1"]}]
}), state.clone()).await.unwrap();
let del_obs_res = del_obs_handler
.execute(
json!({
"deletions": [{"entity_name": "Alpha", "observations": ["Obs1"]}]
}),
state.clone(),
)
.await
.unwrap();
assert_eq!(del_obs_res, "Observations deleted");
let del_rel_handler = DeleteRelationsHandler;
let del_rel_res = del_rel_handler.execute(json!({
"relations": [{"from": "Alpha", "to": "Beta", "relation_type": "links_to"}]
}), state.clone()).await.unwrap();
let del_rel_res = del_rel_handler
.execute(
json!({
"relations": [{"from": "Alpha", "to": "Beta", "relation_type": "links_to"}]
}),
state.clone(),
)
.await
.unwrap();
assert_eq!(del_rel_res, "Relations deleted");
let bcast_handler = AgentSignalsHandler;
let bcast_res = bcast_handler.execute(json!({
"action": "broadcast",
"sender": "agent1",
"signal_type": "task_completed",
"payload": "fix_bug"
}), state.clone()).await.unwrap();
let bcast_res = bcast_handler
.execute(
json!({
"action": "broadcast",
"sender": "agent1",
"signal_type": "task_completed",
"payload": "fix_bug"
}),
state.clone(),
)
.await
.unwrap();
assert!(bcast_res.contains("Broadcasted signal"));
let qsignal_handler = AgentSignalsHandler;
let qsignal_res = qsignal_handler.execute(json!({
"action": "query",
"sender": "agent1"
}), state.clone()).await.unwrap();
let qsignal_res = qsignal_handler
.execute(
json!({
"action": "query",
"sender": "agent1"
}),
state.clone(),
)
.await
.unwrap();
assert!(qsignal_res.contains("task_completed"));
let read_paged_handler = ReadGraphHandler;
let paged_res = read_paged_handler
.execute(json!({"limit": 1, "offset": 0}), state.clone())
.await
.unwrap();
let parsed_paged: serde_json::Value = serde_json::from_str(&paged_res).unwrap();
let parsed_paged: serde_json::Value = serde_yaml::from_str(&paged_res).unwrap();
assert_eq!(parsed_paged["total_count"], 2);
assert_eq!(parsed_paged["has_more"], true);
}
}
+24 -22
View File
@@ -38,32 +38,34 @@ impl McpTool for ProcessLogsHandler {
}
ProcessLogAction::Get => {
let max_lines = tool_args.max_lines.unwrap_or(100);
let result = tokio::task::spawn_blocking(move || -> crate::error::Result<String> {
let mut file = File::open(&safe_path).map_err(|e| {
crate::error::AppError::Internal(format!("Failed to open file: {}", e))
})?;
let len = file.metadata().map_err(|e| e.to_string())?.len();
let result =
tokio::task::spawn_blocking(move || -> crate::error::Result<String> {
let mut file = File::open(&safe_path).map_err(|e| {
crate::error::AppError::Internal(format!("Failed to open file: {}", e))
})?;
let len = file.metadata().map_err(|e| e.to_string())?.len();
let read_size = std::cmp::min(16 * 1024, len);
file.seek(SeekFrom::End(-(read_size as i64)))
.map_err(|e| e.to_string())?;
let read_size = std::cmp::min(16 * 1024, len);
file.seek(SeekFrom::End(-(read_size as i64)))
.map_err(|e| e.to_string())?;
let mut vec_buf = Vec::new();
file.read_to_end(&mut vec_buf)
.map_err(|e| e.to_string())?;
let mut vec_buf = Vec::new();
file.read_to_end(&mut vec_buf).map_err(|e| e.to_string())?;
let buffer = String::from_utf8_lossy(&vec_buf).to_string();
let lines: Vec<&str> = buffer.lines().collect();
let recent_lines = if lines.len() > max_lines {
lines[lines.len() - max_lines..].join("\n")
} else {
buffer
};
let buffer = String::from_utf8_lossy(&vec_buf).to_string();
let lines: Vec<&str> = buffer.lines().collect();
let recent_lines = if lines.len() > max_lines {
lines[lines.len() - max_lines..].join("\n")
} else {
buffer
};
Ok(recent_lines)
})
.await
.map_err(|e| crate::error::AppError::Internal(format!("Task panic: {}", e)))??;
Ok(recent_lines)
})
.await
.map_err(|e| {
crate::error::AppError::Internal(format!("Task panic: {}", e))
})??;
Ok(result)
}
+186 -141
View File
@@ -33,7 +33,7 @@ impl McpTool for LogErrorFixHandler {
);
if let Ok(summary) = state
.ollama
.generate(&prompt, Some(&state.ollama.reasoning_model), None)
.generate(&prompt, Some(&state.ollama.reasoning_model), None, None)
.await
{
let clean = summary.trim();
@@ -110,10 +110,10 @@ impl McpTool for SearchErrorFixesHandler {
} else if st_lower.contains(&sol_lower) || sol_lower.contains(&st_lower) {
score += 0.5;
}
if let Some(emb) = &f.embedding {
if !query_emb.is_empty() {
score += crate::embedding::cosine_similarity(&query_emb, emb);
}
if let Some(emb) = &f.embedding
&& !query_emb.is_empty()
{
score += crate::embedding::cosine_similarity(&query_emb, emb);
}
(f, score)
})
@@ -143,7 +143,7 @@ impl McpTool for SearchErrorFixesHandler {
})
.collect();
Ok::<String, crate::error::AppError>(serde_json::to_string_pretty(&suggestions)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&suggestions)?)
})?;
return Ok(data);
}
@@ -170,7 +170,7 @@ impl McpTool for SearchErrorFixesHandler {
}
})
.collect();
Ok::<String, crate::error::AppError>(serde_json::to_string_pretty(&filtered)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&filtered)?)
})?;
Ok(data)
}
@@ -198,10 +198,10 @@ impl McpTool for LogCodeChangeHandler {
if let Some(range) = &req.line_range {
description = format!("{} [Line Range: {}]", description, range);
}
if let Some(symbols) = &req.symbol_references {
if !symbols.is_empty() {
description = format!("{} [Symbols: {}]", description, symbols.join(", "));
}
if let Some(symbols) = &req.symbol_references
&& !symbols.is_empty()
{
description = format!("{} [Symbols: {}]", description, symbols.join(", "));
}
if state.ollama.is_available().await {
@@ -209,7 +209,7 @@ impl McpTool for LogCodeChangeHandler {
"Summarize in 1 concise sentence the architectural impact of changing file '{}': {}",
req.file_path, description
);
if let Ok(summary) = state.ollama.generate(&prompt, None, None).await {
if let Ok(summary) = state.ollama.generate(&prompt, None, None, None).await {
let clean = summary.trim();
if !clean.is_empty() {
description = format!("{} (AI Summary: {})", description, clean);
@@ -246,13 +246,12 @@ impl McpTool for LogCodeChangeHandler {
let detected_vcs = if let Some(vcs) = req.vcs_type.clone() {
Some(vcs)
} else if let Some(ref rev) = effective_rev {
if rev.starts_with('r') && rev[1..].chars().all(|c| c.is_ascii_digit()) {
Some("svn".to_string())
} else if req
.repo_url
.as_deref()
.map(|u| u.contains("/svn/"))
.unwrap_or(false)
if (rev.starts_with('r') && rev[1..].chars().all(|c| c.is_ascii_digit()))
|| req
.repo_url
.as_deref()
.map(|u| u.contains("/svn/"))
.unwrap_or(false)
{
Some("svn".to_string())
} else {
@@ -323,13 +322,14 @@ impl McpTool for QueryRecentChangesHandler {
}
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> crate::error::Result<String> {
let req: QueryRecentChangesTool = serde_json::from_value(args).unwrap_or(QueryRecentChangesTool {
namespace: None,
repo_name: None,
vcs_type: None,
limit: None,
offset: None,
});
let req: QueryRecentChangesTool =
serde_json::from_value(args).unwrap_or(QueryRecentChangesTool {
namespace: None,
repo_name: None,
vcs_type: None,
limit: None,
offset: None,
});
let limit = req.limit.unwrap_or(50);
let offset = req.offset.unwrap_or(0);
let data = state.code.ledger.read_with(|l| {
@@ -337,24 +337,23 @@ impl McpTool for QueryRecentChangesHandler {
.iter()
.rev()
.filter(|c| {
if let Some(ns) = &req.namespace {
if !c.namespace.eq_ignore_ascii_case(ns) {
return false;
}
if let Some(ns) = &req.namespace
&& !c.namespace.eq_ignore_ascii_case(ns)
{
return false;
}
if let Some(repo) = &req.repo_name {
if c.repo_name
if let Some(repo) = &req.repo_name
&& c.repo_name
.as_ref()
.map(|rn| !rn.eq_ignore_ascii_case(repo))
.unwrap_or(true)
{
return false;
}
{
return false;
}
if let Some(vcs) = &req.vcs_type {
if !c.effective_vcs().eq_ignore_ascii_case(vcs) {
return false;
}
if let Some(vcs) = &req.vcs_type
&& !c.effective_vcs().eq_ignore_ascii_case(vcs)
{
return false;
}
true
})
@@ -362,7 +361,7 @@ impl McpTool for QueryRecentChangesHandler {
.take(limit)
.cloned()
.collect();
Ok::<String, crate::error::AppError>(serde_json::to_string(&filtered)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&filtered)?)
})?;
Ok(data)
}
@@ -386,7 +385,7 @@ impl McpTool for DecisionsHandler {
let req: DecisionsTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
let ns = req
.namespace
.unwrap_or_else(|| crate::models::default_namespace());
.unwrap_or_else(crate::models::default_namespace);
match req.action {
DecisionAction::Log => {
let title = req.title.ok_or_else(|| {
@@ -464,13 +463,14 @@ impl McpTool for DecisionsHandler {
state.code.adrs.modify(|adrs| {
let target_pos = adrs.iter().position(|a| a.id.eq_ignore_ascii_case(&id));
if let Some(pos) = target_pos {
if let Some(superseded_id) = &req.supersedes {
if let Some(s_pos) = adrs.iter().position(|a| a.id.eq_ignore_ascii_case(superseded_id)) {
if s_pos != pos {
adrs[s_pos].status = "superseded".to_string();
adrs_to_index.push(adrs[s_pos].clone());
}
}
if let Some(superseded_id) = &req.supersedes
&& let Some(s_pos) = adrs
.iter()
.position(|a| a.id.eq_ignore_ascii_case(superseded_id))
&& s_pos != pos
{
adrs[s_pos].status = "superseded".to_string();
adrs_to_index.push(adrs[s_pos].clone());
}
let a = &mut adrs[pos];
@@ -488,7 +488,9 @@ impl McpTool for DecisionsHandler {
}
if let Some(s) = req.status {
let s_lower = s.to_ascii_lowercase();
if (s_lower == "implemented" || s_lower == "resolved") && a.resolved_at.is_none() {
if (s_lower == "implemented" || s_lower == "resolved")
&& a.resolved_at.is_none()
{
a.resolved_at = Some(crate::handlers::utils::now_secs());
} else if s_lower != "implemented" && s_lower != "resolved" {
a.resolved_at = None;
@@ -535,11 +537,15 @@ impl McpTool for DecisionsHandler {
&format!("Updated {}: {}", adr.id, adr.title),
Some(&adr.status),
);
Ok(format!("Updated decision {}: {} (status: {})", adr.id, adr.title, adr.status))
} else {
Err(crate::error::AppError::Internal(
format!("Decision with id '{}' not found", id),
Ok(format!(
"Updated decision {}: {} (status: {})",
adr.id, adr.title, adr.status
))
} else {
Err(crate::error::AppError::Internal(format!(
"Decision with id '{}' not found",
id
)))
}
}
DecisionAction::Query => {
@@ -554,9 +560,17 @@ impl McpTool for DecisionsHandler {
}
if let Some(q) = &req.query {
crate::handlers::utils::contains_ignore_ascii_case(&a.title, q)
|| crate::handlers::utils::contains_ignore_ascii_case(&a.context, q)
|| crate::handlers::utils::contains_ignore_ascii_case(&a.decision, q)
|| crate::handlers::utils::contains_ignore_ascii_case(&a.consequence, q)
|| crate::handlers::utils::contains_ignore_ascii_case(
&a.context, q,
)
|| crate::handlers::utils::contains_ignore_ascii_case(
&a.decision,
q,
)
|| crate::handlers::utils::contains_ignore_ascii_case(
&a.consequence,
q,
)
} else {
true
}
@@ -565,7 +579,7 @@ impl McpTool for DecisionsHandler {
.collect();
if include_body {
Ok::<String, crate::error::AppError>(serde_json::to_string(&filtered)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&filtered)?)
} else {
let compact: Vec<_> = filtered
.iter()
@@ -582,7 +596,7 @@ impl McpTool for DecisionsHandler {
})
})
.collect();
Ok::<String, crate::error::AppError>(serde_json::to_string(&compact)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&compact)?)
}
})?;
Ok(data)
@@ -632,17 +646,14 @@ impl McpTool for TechDebtHandler {
let req: TechDebtTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
let ns = req
.namespace
.unwrap_or_else(|| crate::models::default_namespace());
.unwrap_or_else(crate::models::default_namespace);
match req.action {
TechDebtAction::Log => {
let desc = req.description.or(req.title).ok_or_else(|| {
crate::error::AppError::Internal("Missing required parameter 'description' for action 'log'. Next step: Provide tech debt 'description' in request and retry.".to_string())
})?;
let ideal = req.ideal_solution.unwrap_or_default();
let text_to_embed = format!(
"Description: {}\nIdeal Solution: {}",
desc, ideal
);
let text_to_embed = format!("Description: {}\nIdeal Solution: {}", desc, ideal);
let embedding = crate::embedding::generate_embedding_async(text_to_embed)
.await
.ok();
@@ -665,18 +676,21 @@ impl McpTool for TechDebtHandler {
effort_estimate: req.effort_estimate,
});
if debts.len() > 300 {
let severity_rank = |sev: Option<&str>| match sev.unwrap_or("").to_lowercase().as_str() {
"critical" => 4,
"high" => 3,
"medium" => 2,
"low" => 1,
_ => 1,
};
if let Some((idx_to_remove, _)) = debts.iter().enumerate().min_by_key(|(_, d)| {
let status_score = if d.is_resolved { 0 } else { 10 };
let sev_score = severity_rank(d.severity.as_deref());
(status_score + sev_score, d.created_at)
}) {
let severity_rank =
|sev: Option<&str>| match sev.unwrap_or("").to_lowercase().as_str() {
"critical" => 4,
"high" => 3,
"medium" => 2,
"low" => 1,
_ => 1,
};
if let Some((idx_to_remove, _)) =
debts.iter().enumerate().min_by_key(|(_, d)| {
let status_score = if d.is_resolved { 0 } else { 10 };
let sev_score = severity_rank(d.severity.as_deref());
(status_score + sev_score, d.created_at)
})
{
debts.remove(idx_to_remove);
}
}
@@ -712,9 +726,7 @@ impl McpTool for TechDebtHandler {
let data = state.code.tech_debts.read_with(|debts| {
let filtered: Vec<_> = debts
.iter()
.filter(|d| {
d.namespace == ns && (inc || !d.is_resolved)
})
.filter(|d| d.namespace == ns && (inc || !d.is_resolved))
.map(|d| match level {
"compact" => serde_json::json!({
"id": d.id,
@@ -730,7 +742,7 @@ impl McpTool for TechDebtHandler {
}),
})
.collect();
let mut json_str = serde_json::to_string(&filtered)?;
let mut json_str = serde_yaml::to_string(&filtered)?;
if let Some(max_t) = req.max_tokens {
let char_limit = max_t * 4;
if json_str.len() > char_limit {
@@ -811,7 +823,8 @@ impl McpTool for OmniSearchHandler {
let mut ranked_items: Vec<_> = rrf_scores.into_values().collect();
ranked_items.sort_by(|a, b| b.0.total_cmp(&a.0));
let matches: Vec<crate::search::SearchResult> = ranked_items.into_iter().map(|(_, item)| item).collect();
let matches: Vec<crate::search::SearchResult> =
ranked_items.into_iter().map(|(_, item)| item).collect();
let kg_json = state.read_graph(|full| {
let mut kg_results = serde_json::Map::new();
@@ -1263,24 +1276,23 @@ impl McpTool for ManageCheckpointHandler {
)));
}
if let Ok(graph_content) = std::fs::read_to_string(target_dir.join("graph.json")) {
if let Ok(graph) = serde_json::from_str(&graph_content) {
state.graph.modify(|g| *g = graph);
}
if let Ok(graph_content) = std::fs::read_to_string(target_dir.join("graph.json"))
&& let Ok(graph) = serde_json::from_str(&graph_content)
{
state.graph.modify(|g| *g = graph);
}
if let Ok(tasks_content) = std::fs::read_to_string(target_dir.join("tasks.json")) {
if let Ok(tasks) = serde_json::from_str(&tasks_content) {
state.project.tasks.modify(|t| *t = tasks);
}
if let Ok(tasks_content) = std::fs::read_to_string(target_dir.join("tasks.json"))
&& let Ok(tasks) = serde_json::from_str(&tasks_content)
{
state.project.tasks.modify(|t| *t = tasks);
}
if let Ok(debts_content) =
std::fs::read_to_string(target_dir.join("tech_debts.json"))
&& let Ok(debts) = serde_json::from_str(&debts_content)
{
if let Ok(debts) = serde_json::from_str(&debts_content) {
state.code.tech_debts.modify(|d| *d = debts);
}
state.code.tech_debts.modify(|d| *d = debts);
}
Ok(format!("Checkpoint '{}' restored successfully.", name))
@@ -1290,10 +1302,10 @@ impl McpTool for ManageCheckpointHandler {
let checkpoints_dir = state.base_dir.join("checkpoints");
if let Ok(entries) = std::fs::read_dir(&checkpoints_dir) {
for entry in entries.flatten() {
if entry.path().is_dir() {
if let Some(n) = entry.file_name().to_str() {
list.push(serde_json::json!({"type": "checkpoint", "name": n}));
}
if entry.path().is_dir()
&& let Some(n) = entry.file_name().to_str()
{
list.push(serde_json::json!({"type": "checkpoint", "name": n}));
}
}
}
@@ -1301,7 +1313,7 @@ impl McpTool for ManageCheckpointHandler {
for s in snaps {
list.push(serde_json::json!({"type": "snapshot", "id": s.id, "description": s.description, "namespace": s.namespace}));
}
Ok(serde_json::to_string_pretty(&list)?)
Ok(serde_yaml::to_string(&list)?)
}
CheckpointAction::Delete => {
let name = req.name_or_id.ok_or_else(|| {
@@ -1423,7 +1435,7 @@ impl McpTool for QueryLineageHandler {
"timeline": timeline
});
Ok(serde_json::to_string_pretty(&res)?)
Ok(serde_yaml::to_string(&res)?)
}
}
@@ -1461,12 +1473,11 @@ impl McpTool for GetNextActionableTasksHandler {
continue;
}
if let Some(branch) = &req.git_branch {
if let Some(tb) = &task.git_branch {
if tb != branch {
continue;
}
}
if let Some(branch) = &req.git_branch
&& let Some(tb) = &task.git_branch
&& tb != branch
{
continue;
}
let unblocked = task.dependencies.is_empty()
@@ -1484,7 +1495,7 @@ impl McpTool for GetNextActionableTasksHandler {
"tasks": actionable
});
Ok(serde_json::to_string_pretty(&res)?)
Ok(serde_yaml::to_string(&res)?)
}
}
@@ -1509,7 +1520,9 @@ impl McpTool for HypothesesHandler {
match req.action {
HypothesisAction::Log => {
let hyp_text = req.hypothesis.ok_or_else(|| {
crate::error::AppError::Internal("Missing required 'hypothesis' for action 'log'".to_string())
crate::error::AppError::Internal(
"Missing required 'hypothesis' for action 'log'".to_string(),
)
})?;
let hyp_id = format!(
"HYP-{}",
@@ -1536,23 +1549,23 @@ impl McpTool for HypothesesHandler {
let filtered: Vec<_> = hypotheses
.into_iter()
.filter(|h| {
if let Some(tid) = &req.task_id {
if h.task_id.as_ref() != Some(tid) {
return false;
}
if let Some(tid) = &req.task_id
&& h.task_id.as_ref() != Some(tid)
{
return false;
}
if let Some(q) = &req.query {
let lq = q.to_lowercase();
return h.hypothesis.to_lowercase().contains(&lq)
|| h.evidence
.as_ref()
.map_or(false, |e| e.to_lowercase().contains(&lq));
.is_some_and(|e| e.to_lowercase().contains(&lq));
}
true
})
.collect();
Ok(serde_json::to_string_pretty(&filtered)?)
Ok(serde_yaml::to_string(&filtered)?)
}
}
}
@@ -1631,7 +1644,7 @@ impl McpTool for GetPreflightContextHandler {
"recent_activities": recent_activities
});
Ok(serde_json::to_string_pretty(&preflight)?)
Ok(serde_yaml::to_string(&preflight)?)
}
}
@@ -1656,13 +1669,19 @@ impl McpTool for AgentSignalsHandler {
match req.action {
AgentSignalAction::Broadcast => {
let sender = req.sender.ok_or_else(|| {
crate::error::AppError::Internal("Missing required 'sender' for action 'broadcast'".to_string())
crate::error::AppError::Internal(
"Missing required 'sender' for action 'broadcast'".to_string(),
)
})?;
let signal_type = req.signal_type.ok_or_else(|| {
crate::error::AppError::Internal("Missing required 'signal_type' for action 'broadcast'".to_string())
crate::error::AppError::Internal(
"Missing required 'signal_type' for action 'broadcast'".to_string(),
)
})?;
let payload = req.payload.ok_or_else(|| {
crate::error::AppError::Internal("Missing required 'payload' for action 'broadcast'".to_string())
crate::error::AppError::Internal(
"Missing required 'payload' for action 'broadcast'".to_string(),
)
})?;
let timestamp = std::time::SystemTime::now()
@@ -1715,29 +1734,29 @@ impl McpTool for AgentSignalsHandler {
signals
.iter()
.filter(|s| {
if let Some(ttl) = s.ttl_seconds {
if now > s.timestamp + ttl {
return false;
}
if let Some(ttl) = s.ttl_seconds
&& now > s.timestamp + ttl
{
return false;
}
if let Some(sender) = &req.sender {
if s.sender.to_lowercase() != sender.to_lowercase() {
return false;
}
if let Some(sender) = &req.sender
&& s.sender.to_lowercase() != sender.to_lowercase()
{
return false;
}
if let Some(st) = &req.signal_type {
if s.signal_type.to_lowercase() != st.to_lowercase() {
return false;
}
if let Some(st) = &req.signal_type
&& s.signal_type.to_lowercase() != st.to_lowercase()
{
return false;
}
true
})
.cloned()
.take(req.limit.unwrap_or(20))
.cloned()
.collect::<Vec<_>>()
});
Ok(serde_json::to_string_pretty(&filtered)?)
Ok(serde_yaml::to_string(&filtered)?)
}
}
}
@@ -1980,8 +1999,8 @@ mod tests {
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
.unwrap();
let uuid_start = debt_list.find("id\":\"").unwrap() + 5;
let uuid = &debt_list[uuid_start..uuid_start + 36];
let debt_list_val: serde_json::Value = serde_yaml::from_str(&debt_list).unwrap();
let uuid = debt_list_val[0]["id"].as_str().unwrap();
let resolve_debt = TechDebtHandler;
resolve_debt
@@ -2110,7 +2129,10 @@ mod tests {
assert!(q_dec_res.contains("Fast async API routing"));
let q_by_consequence = handler_dec
.execute(serde_json::json!({"action": "query", "query": "Fast async"}), state.clone())
.execute(
serde_json::json!({"action": "query", "query": "Fast async"}),
state.clone(),
)
.await
.unwrap();
assert!(q_by_consequence.contains("Use Axum"));
@@ -2133,7 +2155,10 @@ mod tests {
assert!(update_res.contains("implemented"));
let q_after_update = handler_dec
.execute(serde_json::json!({"action": "query", "include_body": false}), state.clone())
.execute(
serde_json::json!({"action": "query", "include_body": false}),
state.clone(),
)
.await
.unwrap();
assert!(q_after_update.contains("implemented"));
@@ -2143,7 +2168,10 @@ mod tests {
assert!(q_after_update.contains("resolved_at"));
let del_dec_res = handler_dec
.execute(serde_json::json!({"action": "delete", "id": "ADR-0001"}), state.clone())
.execute(
serde_json::json!({"action": "delete", "id": "ADR-0001"}),
state.clone(),
)
.await;
assert!(del_dec_res.is_ok());
@@ -2163,14 +2191,20 @@ mod tests {
assert!(td_res.contains("Tech debt logged"));
let list_td_res = handler_td
.execute(serde_json::json!({"action": "list", "include_resolved": true}), state.clone())
.execute(
serde_json::json!({"action": "list", "include_resolved": true}),
state.clone(),
)
.await
.unwrap();
assert!(list_td_res.contains("Replace unwraps"));
let debt_id = state.code.tech_debts.read_with(|debts| debts[0].id.clone());
let res_td_res = handler_td
.execute(serde_json::json!({"action": "resolve", "id": debt_id}), state.clone())
.execute(
serde_json::json!({"action": "resolve", "id": debt_id}),
state.clone(),
)
.await;
assert!(res_td_res.is_ok());
@@ -2394,7 +2428,10 @@ mod tests {
)
.await
.unwrap();
assert!(rest_state_res.contains("restored successfully") || rest_state_res.contains("restored memory state from snapshot"));
assert!(
rest_state_res.contains("restored successfully")
|| rest_state_res.contains("restored memory state from snapshot")
);
let mg_list = mg_chk
.execute(serde_json::json!({"action": "list"}), state.clone())
@@ -2607,7 +2644,10 @@ mod tests {
// DeleteDecision non-existent
let del_dec_err = handler_dec
.execute(serde_json::json!({"action": "delete", "id": "ADR-9999"}), state.clone())
.execute(
serde_json::json!({"action": "delete", "id": "ADR-9999"}),
state.clone(),
)
.await;
assert!(del_dec_err.is_err());
@@ -2882,9 +2922,14 @@ mod tests {
state.code.tech_debts.read_with(|debts| {
assert_eq!(debts.len(), 300);
assert!(debts.iter().any(|d| d.id == "critical-debt"), "Critical unresolved debt must be retained");
assert!(!debts.iter().any(|d| d.id == "resolved-debt"), "Resolved debt should have been evicted first");
assert!(
debts.iter().any(|d| d.id == "critical-debt"),
"Critical unresolved debt must be retained"
);
assert!(
!debts.iter().any(|d| d.id == "resolved-debt"),
"Resolved debt should have been evicted first"
);
});
}
}
-1
View File
@@ -10,4 +10,3 @@ pub mod tasks;
pub mod utils;
pub mod vision;
pub mod workspaces;
+31 -15
View File
@@ -29,7 +29,9 @@ impl McpTool for HandoffMemosHandler {
let content = req.content.ok_or_else(|| {
crate::error::AppError::Internal("Missing required parameter 'content' for action 'leave'. Next step: Provide non-empty 'content' string in request and retry.".to_string())
})?;
let ns = req.namespace.unwrap_or_else(|| crate::models::default_namespace());
let ns = req
.namespace
.unwrap_or_else(crate::models::default_namespace);
state.telemetry.handoff_memos.modify(|memos| {
memos.push(crate::models::HandoffMemo {
id: uuid::Uuid::new_v4().to_string(),
@@ -58,7 +60,7 @@ impl McpTool for HandoffMemosHandler {
}
})
.collect();
Ok::<String, crate::error::AppError>(serde_json::to_string(&filtered)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&filtered)?)
})?;
Ok(data)
}
@@ -109,11 +111,13 @@ impl McpTool for AddSessionSummaryHandler {
});
if summaries.len() > 200 {
let now = crate::handlers::utils::now_secs();
if let Some((idx_to_remove, _)) = summaries.iter().enumerate().min_by_key(|(_, s)| {
let is_expired = s.expires_at.map(|exp| exp < now).unwrap_or(false);
let status_score = if is_expired { 0 } else { 1 };
(status_score, s.timestamp)
}) {
if let Some((idx_to_remove, _)) =
summaries.iter().enumerate().min_by_key(|(_, s)| {
let is_expired = s.expires_at.map(|exp| exp < now).unwrap_or(false);
let status_score = if is_expired { 0 } else { 1 };
(status_score, s.timestamp)
})
{
summaries.remove(idx_to_remove);
}
}
@@ -143,13 +147,25 @@ impl McpTool for GenerateStandupReportHandler {
let cutoff = crate::handlers::utils::now_secs().saturating_sub(req.hours_lookback * 3600);
let filtered_tasks = state.project.tasks.read_with(|items| {
items.iter().filter(|t| t.updated_at >= cutoff).cloned().collect::<Vec<_>>()
items
.iter()
.filter(|t| t.updated_at >= cutoff)
.cloned()
.collect::<Vec<_>>()
});
let filtered_changes = state.code.ledger.read_with(|changes| {
changes.iter().filter(|c| c.timestamp >= cutoff).cloned().collect::<Vec<_>>()
changes
.iter()
.filter(|c| c.timestamp >= cutoff)
.cloned()
.collect::<Vec<_>>()
});
let filtered_summaries = state.telemetry.session_summaries.read_with(|summaries| {
summaries.iter().filter(|s| s.namespace == req.namespace && s.timestamp >= cutoff).cloned().collect::<Vec<_>>()
summaries
.iter()
.filter(|s| s.namespace == req.namespace && s.timestamp >= cutoff)
.cloned()
.collect::<Vec<_>>()
});
let report = serde_json::json!({
@@ -178,8 +194,7 @@ impl McpTool for PromoteToEntityHandler {
}
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> crate::error::Result<String> {
let req: PromoteToEntityTool =
serde_json::from_value(args).map_err(|e| e.to_string())?;
let req: PromoteToEntityTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
let entity_name = req.entity_name.clone();
let obs = req.content.clone();
@@ -236,7 +251,10 @@ mod tests {
assert_eq!(res, "Handoff memo left.");
let res2 = handoff_handler
.execute(json!({"action": "read", "namespace": "global"}), state.clone())
.execute(
json!({"action": "read", "namespace": "global"}),
state.clone(),
)
.await
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
.unwrap();
@@ -266,5 +284,3 @@ mod tests {
assert!(!res4.is_empty());
}
}
+33 -20
View File
@@ -39,7 +39,9 @@ pub fn extract_adrs(text: &str) -> Vec<String> {
if i + 3 <= len && lower_chars[i..i + 3] == ['a', 'd', 'r'] {
let mut cursor = i + 3;
// Skip optional '-', ':', or whitespace
while cursor < len && (chars[cursor] == '-' || chars[cursor] == ':' || chars[cursor] == ' ') {
while cursor < len
&& (chars[cursor] == '-' || chars[cursor] == ':' || chars[cursor] == ' ')
{
cursor += 1;
}
// Collect consecutive digits
@@ -80,11 +82,17 @@ pub fn extract_tech_debts(text: &str) -> Vec<String> {
while i < len {
if i + 2 <= len && lower_chars[i..i + 2] == ['t', 'd'] {
let mut cursor = i + 2;
while cursor < len && (chars[cursor] == '-' || chars[cursor] == ':' || chars[cursor] == ' ') {
while cursor < len
&& (chars[cursor] == '-' || chars[cursor] == ':' || chars[cursor] == ' ')
{
cursor += 1;
}
let start = cursor;
while cursor < len && (chars[cursor].is_ascii_alphanumeric() || chars[cursor] == '_' || chars[cursor] == '-') {
while cursor < len
&& (chars[cursor].is_ascii_alphanumeric()
|| chars[cursor] == '_'
|| chars[cursor] == '-')
{
cursor += 1;
}
if cursor > start {
@@ -117,11 +125,17 @@ pub fn extract_tasks(text: &str) -> Vec<String> {
while i < len {
if i + 4 <= len && lower_chars[i..i + 4] == ['t', 'a', 's', 'k'] {
let mut cursor = i + 4;
while cursor < len && (chars[cursor] == '-' || chars[cursor] == ':' || chars[cursor] == ' ') {
while cursor < len
&& (chars[cursor] == '-' || chars[cursor] == ':' || chars[cursor] == ' ')
{
cursor += 1;
}
let start = cursor;
while cursor < len && (chars[cursor].is_ascii_alphanumeric() || chars[cursor] == '_' || chars[cursor] == '-') {
while cursor < len
&& (chars[cursor].is_ascii_alphanumeric()
|| chars[cursor] == '_'
|| chars[cursor] == '-')
{
cursor += 1;
}
if cursor > start {
@@ -162,8 +176,7 @@ pub fn cascade_unblock_tasks(state: &MemoryState) -> Vec<String> {
} else {
let all_deps_completed = t.dependencies.iter().all(|dep_id| {
tasks_snapshot.iter().any(|other| {
(other.id == *dep_id || other.title == *dep_id)
&& !other.is_active()
(other.id == *dep_id || other.title == *dep_id) && !other.is_active()
})
});
if all_deps_completed {
@@ -226,18 +239,16 @@ pub fn recalculate_milestones(state: &MemoryState) -> Vec<String> {
}
updated.push(m.id.clone());
}
} else if completed > 0
} else if (completed > 0
|| tasks_snapshot.iter().any(|t| {
m.task_ids.iter().any(|tid| tid == &t.id || tid == &t.title)
&& (t.status == "in_progress" || t.status == "active")
})
}))
&& !m.status.eq_ignore_ascii_case("in_progress")
&& !m.status.eq_ignore_ascii_case("completed")
{
if !m.status.eq_ignore_ascii_case("in_progress")
&& !m.status.eq_ignore_ascii_case("completed")
{
m.status = "in_progress".to_string();
updated.push(m.id.clone());
}
m.status = "in_progress".to_string();
updated.push(m.id.clone());
}
}
});
@@ -275,10 +286,10 @@ pub async fn reconcile_commit_or_code_change(
if adr.id.eq_ignore_ascii_case(cand) {
return true;
}
if let (Some(n1), Some(n2)) = (adr_numeric_id(&adr.id), adr_numeric_id(cand)) {
if n1 == n2 {
return true;
}
if let (Some(n1), Some(n2)) = (adr_numeric_id(&adr.id), adr_numeric_id(cand))
&& n1 == n2
{
return true;
}
false
});
@@ -324,7 +335,9 @@ pub async fn reconcile_commit_or_code_change(
if d.is_resolved {
continue;
}
let matches_id = td_candidates.iter().any(|cand| d.id.eq_ignore_ascii_case(cand));
let matches_id = td_candidates
.iter()
.any(|cand| d.id.eq_ignore_ascii_case(cand));
let matches_file = file_path
.map(|fp| d.file_path.as_deref() == Some(fp))
.unwrap_or(false)
+15 -15
View File
@@ -50,7 +50,6 @@ impl McpTool for TasksHandler {
priority: req.priority,
assigned_agent: req.assigned_agent,
verification_command: req.verification_command,
..Default::default()
};
let idx = state.get_search_index().await;
drop(idx.index_task(&task));
@@ -297,12 +296,12 @@ impl McpTool for TasksHandler {
_ => serde_json::json!({ "id": t.id, "title": t.title, "status": t.status, "description": t.description, "git_branch": t.git_branch }),
})
.collect();
let mut json_str = serde_json::to_string(&filtered)?;
let mut json_str = serde_yaml::to_string(&filtered)?;
if let Some(max_t) = req.max_tokens {
let char_limit = max_t * 4;
while json_str.len() > char_limit && !filtered.is_empty() {
filtered.pop();
json_str = serde_json::to_string(&filtered)?;
json_str = serde_yaml::to_string(&filtered)?;
}
}
Ok::<String, crate::error::AppError>(json_str)
@@ -366,14 +365,14 @@ impl McpTool for TasksHandler {
let mut updated_task: Option<Task> = None;
state.project.tasks.modify(|tasks| {
if let Some(task) = tasks.iter_mut().find(|t| t.id == id || t.title == id) {
if let Some(ac) = task.acceptance_criteria.iter_mut().next() {
if let Some(ac) = task.acceptance_criteria.first_mut() {
if ac.is_met {
already_met = true;
} else {
ac.is_met = true;
success = true;
task.updated_at = crate::handlers::utils::now_secs();
updated_task = Some(task.clone());
task.updated_at = crate::handlers::utils::now_secs();
updated_task = Some(task.clone());
}
} else {
task.acceptance_criteria
@@ -444,7 +443,7 @@ impl McpTool for MilestonesHandler {
})?;
let ns = req
.namespace
.unwrap_or_else(|| crate::models::default_namespace());
.unwrap_or_else(crate::models::default_namespace);
state.project.milestones.modify(|ms| {
ms.push(crate::models::Milestone {
id: uuid::Uuid::new_v4().to_string(),
@@ -477,16 +476,17 @@ impl McpTool for MilestonesHandler {
for m in ms.iter_mut() {
if m.id == id {
m.status = status.clone();
if status.eq_ignore_ascii_case("completed") || status.eq_ignore_ascii_case("done") {
if m.completed_at.is_none() {
m.completed_at = Some(crate::handlers::utils::now_secs());
}
if (status.eq_ignore_ascii_case("completed")
|| status.eq_ignore_ascii_case("done"))
&& m.completed_at.is_none()
{
m.completed_at = Some(crate::handlers::utils::now_secs());
}
if let Some(task_ids) = &req.task_ids {
m.task_ids = task_ids.clone();
if let Some(deliverables) = &req.deliverables {
m.deliverables = deliverables.clone();
}
if let Some(deliverables) = &req.deliverables {
m.deliverables = deliverables.clone();
}
}
found = true;
break;
@@ -514,7 +514,7 @@ impl McpTool for MilestonesHandler {
}
})
.collect();
Ok::<String, crate::error::AppError>(serde_json::to_string(&filtered)?)
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&filtered)?)
})?;
Ok(data)
}
File diff suppressed because it is too large. Load diff
+25 -17
View File
@@ -6,14 +6,6 @@ use async_trait::async_trait;
use serde_json::Value;
use std::sync::Arc;
pub struct SnippetsHandler;
#[async_trait]
@@ -57,7 +49,7 @@ impl McpTool for SnippetsHandler {
embedding,
namespace: req
.namespace
.unwrap_or_else(|| crate::models::default_namespace()),
.unwrap_or_else(crate::models::default_namespace),
repo_name: req.repo_name,
origin_file: req.origin_file,
line_range: req.line_range,
@@ -89,8 +81,9 @@ impl McpTool for SnippetsHandler {
let mut scored: Vec<_> = snippets
.into_iter()
.filter_map(|s| {
let text = format!("{} {} {} {}", s.name, s.description, s.language, s.code)
.to_lowercase();
let text =
format!("{} {} {} {}", s.name, s.description, s.language, s.code)
.to_lowercase();
let mut score = 0.0;
for tok in &q_tokens {
if text.contains(tok) {
@@ -129,7 +122,7 @@ impl McpTool for SnippetsHandler {
.total_cmp(&a["score"].as_f64().unwrap_or(0.0))
});
scored.truncate(10);
Ok(serde_json::to_string_pretty(&scored)?)
Ok(serde_yaml::to_string(&scored)?)
} else {
let data = state.code.snippets.read_with(|snippets| {
let results: Vec<_> = snippets
@@ -143,9 +136,9 @@ impl McpTool for SnippetsHandler {
|| s.tags.iter().any(|t| contains_ignore_ascii_case(t, &query));
let matches_tags = tags_filter.is_empty()
|| tags_filter
.iter()
.any(|tf| s.tags.iter().any(|st| contains_ignore_ascii_case(st, tf)));
|| tags_filter.iter().any(|tf| {
s.tags.iter().any(|st| contains_ignore_ascii_case(st, tf))
});
matches_text && matches_tags
})
@@ -160,7 +153,7 @@ impl McpTool for SnippetsHandler {
})
})
.collect::<Vec<_>>();
serde_json::to_string_pretty(&results)
serde_yaml::to_string(&results)
})?;
Ok(data)
}
@@ -352,13 +345,28 @@ impl McpTool for SemanticCodeSearchHandler {
}
let mut out = format!("Semantic Search Results for '{}':\n", tool_args.query);
for res in results {
for res in &results {
out.push_str(&format!(
"- [{:.2}] {}: {}\n",
res.score, res.title, res.body
));
}
if state.ollama.is_available().await {
let prompt = format!(
"You are an expert software architect. Synthesize a concise GraphRAG summary of the following graph nodes relevant to the query: '{}'.\n\nData:\n{}",
tool_args.query, out
);
if let Ok(summary) = state
.ollama
.generate(&prompt, None, Some("Respond clearly and concisely."), None)
.await
{
out.push_str("\n\n--- Local GraphRAG Summary ---\n");
out.push_str(&summary);
}
}
Ok(out)
}
}
+9 -2
View File
@@ -131,6 +131,13 @@ pub async fn start_background_indexer(state: Arc<MemoryState>) {
snippets.drain(0..overflow);
}
});
let threshold: usize = std::env::var("MCP_MEMORY_CONDENSE_THRESHOLD")
.unwrap_or_else(|_| "100".to_string())
.parse()
.unwrap_or(100);
if state.code.snippets.read_with(|s| s.len()) > threshold {
state.condense_notify.notify_one();
}
for snippet in &snippets_to_index {
let _ = idx.index_snippet(snippet).await;
@@ -156,7 +163,7 @@ fn extract_chunks_with_parent(
node: Node,
code: &str,
chunks: &mut Vec<(String, String, String)>,
ext: &str,
_ext: &str,
parent_scope: Option<&str>,
depth: usize,
) {
@@ -222,7 +229,7 @@ fn extract_chunks_with_parent(
} else {
let mut cursor = node.walk();
for child in node.named_children(&mut cursor) {
extract_chunks_with_parent(child, code, chunks, ext, current_scope, depth + 1);
extract_chunks_with_parent(child, code, chunks, _ext, current_scope, depth + 1);
}
}
}
+1 -1
View File
@@ -59,7 +59,7 @@ The server registers 5 high-signal workflow prompts to initiate standardized age
## 5. Consolidated Smart Tools Architecture (11 Primary Tools)
The server consolidates granular single-purpose tools into domain-named smart tools. Always prefer the consolidated tools over legacy aliases:
The server consolidates granular single-purpose tools into domain-named smart tools.
* **`tasks`**: Complete task lifecycle management.
- `action: "add"`: Create a new task (requires `title`, optional `description`, `git_branch`, `repo_name`, `priority: "low" | "medium" | "high" | "urgent"`, `assigned_agent`, `verification_command`, `parent_id`, `dependencies`).
+278 -63
View File
@@ -4,6 +4,7 @@
)]
pub mod api;
pub mod config;
pub mod db;
pub mod embedding;
pub mod error;
@@ -96,7 +97,62 @@ pub struct AppState {
pub async fn ttl_sweeper_worker(state: Arc<MemoryState>) {
loop {
state.ttl_notify.notified().await;
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let mut next_expiry: Option<u64> = None;
state.project.tasks.read_with(|tasks| {
for t in tasks.iter() {
if let Some(exp) = t.expires_at
&& t.is_active()
{
next_expiry = Some(next_expiry.map_or(exp, |curr| curr.min(exp)));
}
}
});
state.telemetry.handoff_memos.read_with(|memos| {
for m in memos.iter() {
if let Some(exp) = m.expires_at {
next_expiry = Some(next_expiry.map_or(exp, |curr| curr.min(exp)));
}
}
});
state.telemetry.session_summaries.read_with(|summaries| {
for s in summaries.iter() {
if let Some(exp) = s.expires_at {
next_expiry = Some(next_expiry.map_or(exp, |curr| curr.min(exp)));
}
}
});
state.env.gates.read_with(|gates| {
for g in gates.iter() {
if let Some(exp) = g.expires_at {
next_expiry = Some(next_expiry.map_or(exp, |curr| curr.min(exp)));
}
}
});
let sleep_duration = match next_expiry {
Some(exp) if exp > now => {
let diff = exp - now;
std::time::Duration::from_secs(diff.clamp(1, 60))
}
Some(_) => std::time::Duration::from_millis(50),
None => std::time::Duration::from_secs(60),
};
tokio::select! {
_ = state.shutdown_notify.notified() => break,
_ = state.ttl_notify.notified() => {},
_ = tokio::time::sleep(sleep_duration) => {},
}
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
@@ -105,12 +161,13 @@ pub async fn ttl_sweeper_worker(state: Arc<MemoryState>) {
let mut expired_tasks = Vec::new();
state.project.tasks.modify(|tasks| {
for t in tasks.iter_mut() {
if let Some(exp) = t.expires_at {
if exp <= now && t.is_active() {
t.status = "expired".to_string();
t.updated_at = now;
expired_tasks.push(t.id.clone());
}
if let Some(exp) = t.expires_at
&& exp <= now
&& t.is_active()
{
t.status = "expired".to_string();
t.updated_at = now;
expired_tasks.push(t.id.clone());
}
}
});
@@ -139,12 +196,18 @@ pub async fn ttl_sweeper_worker(state: Arc<MemoryState>) {
state.telemetry.session_summaries.modify(|summaries| {
summaries.retain(|s| s.expires_at.is_none_or(|exp| exp > now));
});
state.env.gates.modify(|gates| {
gates.retain(|g| g.expires_at.is_none_or(|exp| exp > now));
});
}
}
pub async fn index_committer_worker(state: Arc<MemoryState>) {
loop {
state.index_commit_notify.notified().await;
tokio::select! {
_ = state.shutdown_notify.notified() => break,
_ = state.index_commit_notify.notified() => {},
}
let idx = state.search_index.read().await.clone();
let _ = idx.commit().await;
}
@@ -152,7 +215,10 @@ pub async fn index_committer_worker(state: Arc<MemoryState>) {
pub async fn condense_graph_worker(state: Arc<MemoryState>) {
loop {
state.condense_notify.notified().await;
tokio::select! {
_ = state.shutdown_notify.notified() => break,
_ = state.condense_notify.notified() => {},
}
let threshold: usize = std::env::var("MCP_MEMORY_CONDENSE_THRESHOLD")
.unwrap_or_else(|_| "100".to_string())
@@ -185,27 +251,108 @@ pub async fn condense_graph_worker(state: Arc<MemoryState>) {
}
});
if let Some((content, names)) = snippet_condensation {
if !content.is_empty() {
let name = format!("Snippet History {}", now);
state.modify_graph(|graph| {
graph.entities.insert(
name.clone(),
crate::models::Entity {
name: name.clone(),
entity_type: "Historical Summary".to_string(),
observations: vec![content],
namespace: crate::models::default_namespace(),
git_branch: None,
..Default::default()
},
if let Some((content, names)) = snippet_condensation
&& !content.is_empty()
{
let name = format!("Snippet History {}", now);
state.modify_graph(|graph| {
graph.entities.insert(
name.clone(),
crate::models::Entity {
name: name.clone(),
entity_type: "Historical Summary".to_string(),
observations: vec![content],
namespace: crate::models::default_namespace(),
git_branch: None,
..Default::default()
},
);
});
let name_set: std::collections::HashSet<String> = names.into_iter().collect();
state.code.snippets.modify(|snippets| {
snippets.retain(|s| !name_set.contains(&s.name));
});
tracing::info!("Condensed snippets into Historical Summary.");
}
}
}
pub async fn memory_consolidation_worker(state: Arc<MemoryState>) {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(300));
loop {
tokio::select! {
_ = state.shutdown_notify.notified() => break,
_ = interval.tick() => {},
}
let entities: Vec<_> = state.graph.read_with(|g| {
g.entities
.values()
.map(|e| (e.name.clone(), e.entity_type.clone()))
.collect()
});
if entities.len() < 2 {
continue;
}
let mut entity_summaries = String::new();
for (name, e_type) in entities.iter().take(50) {
entity_summaries.push_str(&format!("- [{}] {}\n", e_type, name));
}
let prompt = format!(
"Analyze the following list of entities and identify exactly TWO that represent the exact same concept or item but have slightly different names (e.g. 'auth_service' and 'AuthService'). Return ONLY a valid JSON array containing exactly two strings: the two names to merge. If no obvious duplicates exist, return an empty array []. Do not output any markdown formatting or extra text.\n\nEntities:\n{}",
entity_summaries
);
if let Ok(response) = state
.ollama
.generate(
&prompt,
None,
Some("You are a helpful JSON-only data deduplication assistant. Output only JSON."),
Some("json"),
)
.await
{
let cleaned = response
.trim()
.trim_start_matches("```json")
.trim_start_matches("```")
.trim_end_matches("```")
.trim();
if let Ok(duplicates) = serde_json::from_str::<Vec<String>>(cleaned)
&& duplicates.len() == 2
{
let e1_name = &duplicates[0];
let e2_name = &duplicates[1];
if e1_name != e2_name {
tracing::info!(
"Memory Consolidation Daemon: Merging '{}' into '{}'",
e2_name,
e1_name
);
});
let name_set: std::collections::HashSet<String> = names.into_iter().collect();
state.code.snippets.modify(|snippets| {
snippets.retain(|s| !name_set.contains(&s.name));
});
tracing::info!("Condensed snippets into Historical Summary.");
state.modify_graph(|g| {
if let Some(mut e2) = g.entities.remove(e2_name) {
if let Some(e1) = g.entities.get_mut(e1_name) {
e1.observations.append(&mut e2.observations);
} else {
g.entities.insert(e2_name.clone(), e2);
}
}
for rel in g.relations.iter_mut() {
if rel.from == *e2_name {
rel.from = e1_name.clone();
}
if rel.to == *e2_name {
rel.to = e1_name.clone();
}
}
});
}
}
}
}
@@ -223,7 +370,9 @@ pub async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::erro
tokio::spawn(index_committer_worker(Arc::clone(&state)));
tokio::spawn(ttl_sweeper_worker(Arc::clone(&state)));
tokio::spawn(condense_graph_worker(Arc::clone(&state)));
tokio::spawn(memory_consolidation_worker(Arc::clone(&state)));
crate::watcher::spawn_watcher(Arc::clone(&state));
crate::handlers::vision::spawn_clipboard_listener(Arc::clone(&state));
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
let app_state = Arc::new(AppState {
handler: Arc::new(MemoryHandler::new(Arc::clone(&state))),
@@ -270,32 +419,64 @@ pub async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::erro
tokio::spawn(async move {
let port1 = std::env::var("MCP_UDP_PORT1").unwrap_or_else(|_| "3001".to_string());
if let Ok(socket) = tokio::net::UdpSocket::bind(format!("127.0.0.1:{}", port1)).await {
let socket = Arc::new(socket);
let socket_rx = socket.clone();
let mut subscribers: HashMap<(String, String), std::net::SocketAddr> = HashMap::new();
let mut buf = vec![0u8; 65536];
let mut event_rx = udp_state.handler.state.event_bus_tx.subscribe();
loop {
if let Ok((len, _addr)) = socket.recv_from(&mut buf).await
&& let Ok(payload) =
serde_json::from_slice::<crate::models::TerminalHistory>(&buf[..len])
{
udp_state
.handler
.state
.record_terminal_history(payload.clone());
tokio::select! {
recv_res = socket_rx.recv_from(&mut buf) => {
if let Ok((len, addr)) = recv_res {
if let Ok(payload) = serde_json::from_slice::<crate::models::TerminalHistory>(&buf[..len]) {
udp_state
.handler
.state
.record_terminal_history(payload.clone());
let ws_msg = serde_json::json!({
"type": "terminal_telemetry",
"data": payload
});
let msg_str = ws_msg.to_string();
let ws_msg = serde_json::json!({
"type": "terminal_telemetry",
"data": payload
});
let msg_str = ws_msg.to_string();
let senders: Vec<_> = udp_state
.clients
.read()
.unwrap_or_else(|e| e.into_inner())
.values()
.cloned()
.collect();
for tx in senders {
let _ = tx.try_send(msg_str.clone());
let senders: Vec<_> = udp_state
.clients
.read()
.unwrap_or_else(|e| e.into_inner())
.values()
.cloned()
.collect();
for tx in senders {
let _ = tx.try_send(msg_str.clone());
}
} else if let Ok(json_payload) = serde_json::from_slice::<serde_json::Value>(&buf[..len]) {
if json_payload.get("type").and_then(|t| t.as_str()) == Some("ping") {
let _ = socket.send_to(b"pong", addr).await;
} else if json_payload.get("type").and_then(|t| t.as_str()) == Some("gate_wait")
&& let (Some(action), Some(target)) = (
json_payload.get("action").and_then(|a| a.as_str()),
json_payload.get("target").and_then(|t| t.as_str())
) {
subscribers.insert((action.to_string(), target.to_string()), addr);
}
}
}
}
Ok(event) = event_rx.recv() => {
if event.topic == "gate:event"
&& let (Some(action), Some(target), Some(status)) = (
event.payload.get("action").and_then(|a| a.as_str()),
event.payload.get("target").and_then(|t| t.as_str()),
event.payload.get("status").and_then(|s| s.as_str()),
)
&& (status == "authorized" || status == "blocked")
&& let Some(addr) = subscribers.remove(&(action.to_string(), target.to_string())) {
let response = if status == "authorized" { b"APPROVED" } else { b"REJECTED" };
let _ = socket.send_to(response, addr).await;
}
}
}
}
@@ -372,14 +553,46 @@ pub async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::erro
let _ = nvim_udp_state.handler.state.event_bus_tx.send(event);
}
if payload.event.starts_with("agent_") {
let topic = format!("nvim:ui:{}", payload.event);
let event = crate::state::GenericEvent {
topic,
session_id: Some(payload.session_id.clone()),
payload: serde_json::json!(&payload),
};
let _ = nvim_udp_state.handler.state.event_bus_tx.send(event);
if payload.event.starts_with("agent_") || payload.event.starts_with("diff_") {
let payload_val = serde_json::json!(&payload);
// 1. General event topic (e.g. nvim:ui:agent_prompt_response, nvim:ui:agent_diff_accepted)
let _ = nvim_udp_state.handler.state.event_bus_tx.send(
crate::state::GenericEvent {
topic: format!("nvim:ui:{}", payload.event),
session_id: Some(payload.session_id.clone()),
payload: payload_val.clone(),
},
);
// 2. Correlated request_id topic (e.g. nvim:ui:agent_prompt_response:REQ_ID)
if let Some(ref req_id) = payload.request_id {
let _ = nvim_udp_state.handler.state.event_bus_tx.send(
crate::state::GenericEvent {
topic: format!("nvim:ui:{}:{}", payload.event, req_id),
session_id: Some(payload.session_id.clone()),
payload: payload_val.clone(),
},
);
}
// 3. Correlated diff_id topics
if let Some(ref diff_id) = payload.diff_id {
let _ = nvim_udp_state.handler.state.event_bus_tx.send(
crate::state::GenericEvent {
topic: format!("nvim:ui:{}:{}", payload.event, diff_id),
session_id: Some(payload.session_id.clone()),
payload: payload_val.clone(),
},
);
// General diff decision topic
let _ = nvim_udp_state.handler.state.event_bus_tx.send(
crate::state::GenericEvent {
topic: format!("nvim:ui:diff_decision:{}", diff_id),
session_id: Some(payload.session_id.clone()),
payload: payload_val.clone(),
},
);
}
}
}
}
@@ -451,6 +664,7 @@ pub fn init_logging(app_name: &str) -> Option<tracing_appender::non_blocking::Wo
}
pub fn run_cli() -> Result<(), Box<dyn std::error::Error>> {
crate::config::load_mcp_config_env();
let _guard = init_logging("mcp-memory-server");
let cli = Cli::parse();
@@ -594,7 +808,7 @@ mod tests {
#[test]
fn test_cli_parsing_default() {
let cli = Cli::try_parse_from(&["mcp-memory-server"]).unwrap();
let cli = Cli::try_parse_from(["mcp-memory-server"]).unwrap();
assert!(cli.command.is_none());
assert!(!cli.exit);
assert!(!cli.restart);
@@ -602,7 +816,7 @@ mod tests {
#[test]
fn test_cli_parsing_exit_and_target() {
let cli = Cli::try_parse_from(&[
let cli = Cli::try_parse_from([
"mcp-memory-server",
"--exit",
"--target",
@@ -615,7 +829,7 @@ mod tests {
#[test]
fn test_cli_parsing_gate_set() {
let cli = Cli::try_parse_from(&[
let cli = Cli::try_parse_from([
"mcp-memory-server",
"gate",
"set",
@@ -651,7 +865,7 @@ mod tests {
#[test]
fn test_cli_parsing_gate_verify() {
let cli = Cli::try_parse_from(&[
let cli = Cli::try_parse_from([
"mcp-memory-server",
"gate",
"verify",
@@ -716,6 +930,7 @@ mod tests {
}
#[tokio::test]
#[allow(clippy::await_holding_lock)]
async fn test_run_server_graceful_shutdown() {
let _lock = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let temp_dir = tempfile::tempdir().unwrap();
+16 -24
View File
@@ -4,19 +4,15 @@ use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
pub enum ChangeKind {
Added,
#[default]
Modified,
Deleted,
Renamed,
}
impl Default for ChangeKind {
fn default() -> Self {
ChangeKind::Modified
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CodeChange {
pub timestamp: u64,
@@ -57,18 +53,18 @@ impl CodeChange {
if let Some(vcs) = &self.vcs_type {
return vcs.as_str();
}
if let Some(rev) = self.revision.as_deref().or(self.git_commit.as_deref()) {
if rev.starts_with('r') && rev[1..].chars().all(|c| c.is_ascii_digit()) {
return "svn";
}
if let Some(rev) = self.revision.as_deref().or(self.git_commit.as_deref())
&& rev.starts_with('r')
&& rev[1..].chars().all(|c| c.is_ascii_digit())
{
return "svn";
}
if let Some(br) = self.branch.as_deref().or(self.git_branch.as_deref()) {
if br.eq_ignore_ascii_case("trunk")
if let Some(br) = self.branch.as_deref().or(self.git_branch.as_deref())
&& (br.eq_ignore_ascii_case("trunk")
|| br.starts_with("branches/")
|| br.starts_with("tags/")
{
return "svn";
}
|| br.starts_with("tags/"))
{
return "svn";
}
"git"
}
@@ -266,10 +262,10 @@ impl KnowledgeGraph {
let mut adj: HashMap<&'a str, Vec<(&'a str, &'a str, bool)>> =
HashMap::with_capacity(self.relations.len() * 2);
for rel in &self.relations {
if let Some(rf) = relation_filter {
if rel.relation_type != rf {
continue;
}
if let Some(rf) = relation_filter
&& rel.relation_type != rf
{
continue;
}
adj.entry(rel.from.as_str()).or_default().push((
rel.to.as_str(),
@@ -398,7 +394,6 @@ fn default_adr_status() -> String {
"accepted".to_string()
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ErrorFix {
pub signature: String,
@@ -420,7 +415,6 @@ pub struct ErrorFix {
pub toolchain: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SessionSummary {
pub timestamp: u64,
@@ -527,7 +521,6 @@ pub struct EnvironmentDetail {
pub ssh_host: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TechDebt {
pub id: String,
@@ -554,7 +547,6 @@ pub struct TechDebt {
pub effort_estimate: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GateRecord {
pub id: String,
+29 -3
View File
@@ -23,6 +23,12 @@ struct GenerateRequest<'a> {
stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
images: Option<Vec<&'a str>>,
#[serde(skip_serializing_if = "Option::is_none")]
options: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
keep_alive: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
format: Option<&'a str>,
}
#[derive(Deserialize)]
@@ -34,6 +40,8 @@ struct GenerateResponse {
struct EmbeddingRequest<'a> {
model: &'a str,
prompt: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
keep_alive: Option<&'a str>,
}
#[derive(Deserialize)]
@@ -50,12 +58,17 @@ impl OllamaClient {
let reasoning_model =
env::var("OLLAMA_REASONING_MODEL").unwrap_or_else(|_| "deepseek-r1:1.5b".to_string());
let vision_model =
env::var("OLLAMA_VISION_MODEL").unwrap_or_else(|_| "qwen3-vl:2b".to_string());
env::var("OLLAMA_VISION_MODEL").unwrap_or_else(|_| "moondream".to_string());
let embed_model = env::var("OLLAMA_EMBED_MODEL")
.unwrap_or_else(|_| "nomic-embed-text:latest".to_string());
let timeout_sec = env::var("OLLAMA_TIMEOUT_SEC")
.unwrap_or_else(|_| "60".to_string())
.parse::<u64>()
.unwrap_or(60);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(60))
.timeout(Duration::from_secs(timeout_sec))
.build()
.unwrap_or_default();
@@ -102,6 +115,7 @@ impl OllamaClient {
prompt: &str,
model_override: Option<&str>,
system: Option<&str>,
format: Option<&str>,
) -> Result<String, AppError> {
let model = model_override.unwrap_or(&self.coder_model);
let url = format!("{}/api/generate", self.base_url.trim_end_matches('/'));
@@ -112,6 +126,12 @@ impl OllamaClient {
system,
stream: false,
images: None,
options: Some(serde_json::json!({
"num_ctx": 32768, // Massive context window win
"num_predict": 4096 // Give reasoning models plenty of output room
})),
keep_alive: Some("1h"),
format,
};
let res = self
@@ -151,6 +171,12 @@ impl OllamaClient {
),
stream: false,
images: Some(vec![image_base64]),
options: Some(serde_json::json!({
"num_ctx": 8192,
"num_predict": 1024
})),
keep_alive: Some("1h"),
format: None,
};
let res = self
@@ -181,6 +207,7 @@ impl OllamaClient {
let body = EmbeddingRequest {
model: &self.embed_model,
prompt: text,
keep_alive: Some("1h"),
};
let res = self
@@ -241,4 +268,3 @@ async fn test_ollama_client_invalid_api_key() {
client.base_url = "http://invalid-api-key:11434".to_string();
assert!(!client.is_available().await);
}
+237 -16
View File
@@ -639,12 +639,19 @@ impl MemoryHandler {
if let Some(resource) = self.resources.get(uri) {
match resource.read(Arc::clone(&self.state)).await {
Ok(text) => {
let mut content_obj = serde_json::json!({
"uri": uri,
"mimeType": resource.mime_type().unwrap_or("application/json"),
"text": &text
});
if text.len() > 4000 {
content_obj["cacheControl"] =
serde_json::json!({"type": "ephemeral"});
}
let payload = serde_json::json!({
"contents": [{
"uri": uri,
"mimeType": resource.mime_type().unwrap_or("application/json"),
"text": text
}]
"contents": [content_obj]
});
Some(crate::mcp::success(id, payload))
}
@@ -756,8 +763,15 @@ impl MemoryHandler {
match result {
Ok(text) => {
let mut content_obj = serde_json::json!({"type": "text", "text": &text});
// Heuristic: If response is large (e.g. > 4000 chars), inject cache_control for Prompt Caching
if text.len() > 4000 {
content_obj["cacheControl"] = serde_json::json!({"type": "ephemeral"});
}
let payload = serde_json::json!({
"content": [{"type": "text", "text": text}],
"content": [content_obj],
"isError": false
});
Some(crate::mcp::success(id_clone, payload))
@@ -772,6 +786,195 @@ impl MemoryHandler {
}
}
}
"tasks/complete" => {
let params = req.get("params").unwrap_or(&serde_json::Value::Null);
let task_id = params.get("id").and_then(|v| v.as_str()).unwrap_or("");
if task_id.is_empty() {
return Some(crate::mcp::error(id, -32602, "Missing task id parameter"));
}
self.state.project.tasks.modify(|tasks| {
for t in tasks.iter_mut() {
if t.id == task_id {
t.status = "completed".to_string();
t.updated_at = crate::handlers::utils::now_secs();
break;
}
}
});
let unblocked = crate::handlers::reconciliation::cascade_unblock_tasks(&self.state);
let milestones =
crate::handlers::reconciliation::recalculate_milestones(&self.state);
self.state.record_activity(
"task",
&format!("Completed task via WebSocket RPC: {}", task_id),
Some("completed"),
);
let _ = self.state.event_bus_tx.send(crate::state::GenericEvent {
topic: "task:event".to_string(),
session_id: None,
payload: serde_json::json!({
"task_id": task_id,
"status": "completed",
"unblocked_tasks": unblocked,
"updated_milestones": milestones,
}),
});
Some(crate::mcp::success(
id,
serde_json::json!({
"status": "success",
"unblocked_tasks": unblocked,
"updated_milestones": milestones
}),
))
}
"tech_debts/resolve" => {
let params = req.get("params").unwrap_or(&serde_json::Value::Null);
let debt_id = params.get("id").and_then(|v| v.as_str()).unwrap_or("");
if debt_id.is_empty() {
return Some(crate::mcp::error(id, -32602, "Missing debt id parameter"));
}
let mut found = false;
self.state.code.tech_debts.modify(|debts| {
for d in debts.iter_mut() {
if d.id == debt_id {
d.is_resolved = true;
found = true;
break;
}
}
});
if found {
self.state.record_activity(
"tech_debt",
&format!("Resolved tech debt via WebSocket RPC: {}", debt_id),
Some("resolved"),
);
let _ = self.state.event_bus_tx.send(crate::state::GenericEvent {
topic: "tech_debt:event".to_string(),
session_id: None,
payload: serde_json::json!({
"id": debt_id,
"status": "resolved"
}),
});
Some(crate::mcp::success(
id,
serde_json::json!({ "status": "success", "message": format!("Tech debt {} resolved", debt_id) }),
))
} else {
Some(crate::mcp::error(id, -32602, "Tech debt not found"))
}
}
"gates/authorize" => {
let params = req.get("params").unwrap_or(&serde_json::Value::Null);
let gate_id = params.get("id").and_then(|v| v.as_str()).unwrap_or("");
let mut found = false;
self.state.env.gates.modify(|gates| {
for g in gates.iter_mut() {
if g.id == gate_id {
g.status = "authorized".to_string();
g.reason = Some("Manually authorized via WebSocket RPC".to_string());
g.timestamp = crate::handlers::utils::now_secs();
found = true;
break;
}
}
});
if found {
self.state.record_activity(
"gate",
&format!("Authorized push gate via WebSocket: {}", gate_id),
Some("authorized"),
);
let _ = self.state.event_bus_tx.send(crate::state::GenericEvent {
topic: "gate:event".to_string(),
session_id: None,
payload: serde_json::json!({ "id": gate_id, "status": "authorized", "active_gate": "authorized" }),
});
Some(crate::mcp::success(
id,
serde_json::json!({ "status": "success" }),
))
} else {
Some(crate::mcp::error(id, -32602, "Gate not found"))
}
}
"gates/block" => {
let params = req.get("params").unwrap_or(&serde_json::Value::Null);
let gate_id = params.get("id").and_then(|v| v.as_str()).unwrap_or("");
let mut found = false;
self.state.env.gates.modify(|gates| {
for g in gates.iter_mut() {
if g.id == gate_id {
g.status = "blocked".to_string();
g.reason = Some("Manually blocked via WebSocket RPC".to_string());
g.timestamp = crate::handlers::utils::now_secs();
found = true;
break;
}
}
});
if found {
self.state.record_activity(
"gate",
&format!("Blocked push gate via WebSocket: {}", gate_id),
Some("blocked"),
);
let _ = self.state.event_bus_tx.send(crate::state::GenericEvent {
topic: "gate:event".to_string(),
session_id: None,
payload: serde_json::json!({ "id": gate_id, "status": "blocked", "active_gate": "blocked" }),
});
Some(crate::mcp::success(
id,
serde_json::json!({ "status": "success" }),
))
} else {
Some(crate::mcp::error(id, -32602, "Gate not found"))
}
}
"gates/delete" => {
let params = req.get("params").unwrap_or(&serde_json::Value::Null);
let gate_id = params.get("id").and_then(|v| v.as_str()).unwrap_or("");
let mut removed = false;
self.state.env.gates.modify(|gates| {
if let Some(pos) = gates.iter().position(|g| g.id == gate_id) {
gates.remove(pos);
removed = true;
}
});
if removed {
self.state.record_activity(
"gate",
&format!("Deleted push gate via WebSocket: {}", gate_id),
Some("deleted"),
);
let _ = self.state.event_bus_tx.send(crate::state::GenericEvent {
topic: "gate:event".to_string(),
session_id: None,
payload: serde_json::json!({ "id": gate_id, "status": "deleted" }),
});
Some(crate::mcp::success(
id,
serde_json::json!({ "status": "success" }),
))
} else {
Some(crate::mcp::error(id, -32602, "Gate not found"))
}
}
"resources/subscribe" => {
let params = req.get("params").unwrap_or(&serde_json::Value::Null);
let uri = params.get("uri").and_then(|u| u.as_str()).unwrap_or("");
tracing::info!("Client subscribed to MCP resource: {}", uri);
Some(crate::mcp::success(id, serde_json::json!({})))
}
"resources/unsubscribe" => {
let params = req.get("params").unwrap_or(&serde_json::Value::Null);
let uri = params.get("uri").and_then(|u| u.as_str()).unwrap_or("");
tracing::info!("Client unsubscribed from MCP resource: {}", uri);
Some(crate::mcp::success(id, serde_json::json!({})))
}
m if m.starts_with("notifications/") => None,
"ping" => Some(crate::mcp::success(id, serde_json::json!({}))),
_ => {
@@ -850,7 +1053,10 @@ pub fn format_tool_activity_description(name: &str, args: &serde_json::Value) ->
("Create Entities", names)
}
"hypotheses" => {
let act = args.get("action").and_then(|v| v.as_str()).unwrap_or("manage");
let act = args
.get("action")
.and_then(|v| v.as_str())
.unwrap_or("manage");
let hyp = args
.get("hypothesis")
.or_else(|| args.get("query"))
@@ -858,24 +1064,39 @@ pub fn format_tool_activity_description(name: &str, args: &serde_json::Value) ->
.unwrap_or("");
(
"Hypotheses",
format!("{}: {}", act, hyp).trim_end_matches(": ").to_string(),
format!("{}: {}", act, hyp)
.trim_end_matches(": ")
.to_string(),
)
}
"agent_signals" => {
let act = args.get("action").and_then(|v| v.as_str()).unwrap_or("signal");
let act = args
.get("action")
.and_then(|v| v.as_str())
.unwrap_or("signal");
let sender = args.get("sender").and_then(|v| v.as_str()).unwrap_or("");
let st = args.get("signal_type").and_then(|v| v.as_str()).unwrap_or("");
let st = args
.get("signal_type")
.and_then(|v| v.as_str())
.unwrap_or("");
(
"Agent Signals",
format!("{}: {} [{}]", act, sender, st).trim_end_matches(": ").to_string(),
format!("{}: {} [{}]", act, sender, st)
.trim_end_matches(": ")
.to_string(),
)
}
"process_logs" => {
let act = args.get("action").and_then(|v| v.as_str()).unwrap_or("logs");
let act = args
.get("action")
.and_then(|v| v.as_str())
.unwrap_or("logs");
let file = args.get("file_path").and_then(|v| v.as_str()).unwrap_or("");
(
"Process Logs",
format!("{}: {}", act, file).trim_end_matches(": ").to_string(),
format!("{}: {}", act, file)
.trim_end_matches(": ")
.to_string(),
)
}
_ => return name.to_string(),
@@ -900,8 +1121,8 @@ async fn execute_tool_span(
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
use serde_json::json;
use tempfile::tempdir;
#[test]
fn test_format_tool_activity_description() {
@@ -1177,7 +1398,7 @@ mod tests {
let handler = MemoryHandler::new(state);
// 1. Verify invalid LLM requests fail gracefully with isError: true or structured response
for (tool_name, _tool) in &handler.tools {
for tool_name in handler.tools.keys() {
let invalid_req = json!({
"jsonrpc": "2.0",
"id": 200,
@@ -1207,7 +1428,7 @@ mod tests {
("decisions", json!({"action": "query"})),
("tech_debt", json!({"action": "list"})),
("snippets", json!({"action": "search", "query": "test"})),
("clipboard", json!({"action": "read"})),
("clipboard", json!({"action": "history"})),
("environment", json!({"action": "read_fingerprint"})),
("omni_search", json!({"query": "test"})),
("get_project_health", json!({})),
+102 -29
View File
@@ -16,6 +16,26 @@ pub struct SearchResult {
pub type SearchResultTuple = (String, String, String, String, f32);
fn calculate_time_decay(timestamp_sec: Option<u64>) -> f32 {
let Some(ts) = timestamp_sec else { return 1.0 };
if ts == 0 {
return 1.0;
} // Avoid decaying default unset timestamps
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
if ts > now {
return 1.0;
}
let age_days = (now - ts) as f32 / 86400.0;
// 1% decay per day
(-0.01 * age_days).exp()
}
#[derive(Clone)]
pub struct MemoryIndex {
pub index: Index,
@@ -55,7 +75,8 @@ impl MemoryIndex {
.reload_policy(ReloadPolicy::OnCommitWithDelay)
.try_into()?;
let query_parser = tantivy::query::QueryParser::for_index(&index, vec![title_field, body_field]);
let query_parser =
tantivy::query::QueryParser::for_index(&index, vec![title_field, body_field]);
Ok(Self {
index,
@@ -87,7 +108,8 @@ impl MemoryIndex {
.reload_policy(ReloadPolicy::OnCommitWithDelay)
.try_into()?;
let query_parser = tantivy::query::QueryParser::for_index(&index, vec![title_field, body_field]);
let query_parser =
tantivy::query::QueryParser::for_index(&index, vec![title_field, body_field]);
Ok(Self {
index,
@@ -117,7 +139,10 @@ impl MemoryIndex {
self.index_entities_batch(std::slice::from_ref(e))
}
pub fn index_entities_batch(&self, entities: &[Entity]) -> tokio::task::JoinHandle<tantivy::Result<()>> {
pub fn index_entities_batch(
&self,
entities: &[Entity],
) -> tokio::task::JoinHandle<tantivy::Result<()>> {
let writer = Arc::clone(&self.writer);
let id_field = self.id_field;
let needs_commit = Arc::clone(&self.needs_commit);
@@ -154,13 +179,16 @@ impl MemoryIndex {
let id_val = t.id.clone();
let needs_commit = Arc::clone(&self.needs_commit);
let criteria_body = t.acceptance_criteria.iter().fold(String::new(), |mut acc, c| {
if !acc.is_empty() {
acc.push('\n');
}
acc.push_str(&c.description);
acc
});
let criteria_body = t
.acceptance_criteria
.iter()
.fold(String::new(), |mut acc, c| {
if !acc.is_empty() {
acc.push('\n');
}
acc.push_str(&c.description);
acc
});
let doc = doc!(
self.id_field => t.id.as_str(),
@@ -179,6 +207,34 @@ impl MemoryIndex {
})
}
pub fn index_clipboard_image(
&self,
id: &str,
file_path: &str,
ocr_text: &str,
) -> tokio::task::JoinHandle<tantivy::Result<()>> {
let writer = Arc::clone(&self.writer);
let id_field = self.id_field;
let id_val = id.to_string();
let needs_commit = Arc::clone(&self.needs_commit);
let doc = doc!(
self.id_field => id,
self.title_field => format!("Clipboard Screenshot: {}", file_path),
self.body_field => ocr_text,
self.type_field => "clipboard_image",
self.namespace_field => "global"
);
tokio::task::spawn_blocking(move || {
let writer = writer.lock().unwrap_or_else(|e| e.into_inner());
writer.delete_term(tantivy::Term::from_field_text(id_field, &id_val));
writer.add_document(doc)?;
needs_commit.store(true, std::sync::atomic::Ordering::SeqCst);
Ok(())
})
}
pub fn delete_document(&self, id: &str) -> tokio::task::JoinHandle<tantivy::Result<()>> {
let writer = Arc::clone(&self.writer);
let id_field = self.id_field;
@@ -203,7 +259,10 @@ impl MemoryIndex {
if let Err(first_err) = writer.commit() {
std::thread::sleep(std::time::Duration::from_millis(100));
writer.commit().map_err(|second_err| {
tantivy::TantivyError::SystemError(format!("First commit error: {}; Second commit error: {}", first_err, second_err))
tantivy::TantivyError::SystemError(format!(
"First commit error: {}; Second commit error: {}",
first_err, second_err
))
})?;
}
let _ = reader.reload();
@@ -218,9 +277,11 @@ impl MemoryIndex {
})
}
pub fn commit_if_needed(&self) -> tantivy::Result<()> {
if self.needs_commit.swap(false, std::sync::atomic::Ordering::SeqCst) {
if self
.needs_commit
.swap(false, std::sync::atomic::Ordering::SeqCst)
{
let mut writer = self.writer.lock().unwrap_or_else(|e| e.into_inner());
writer.commit()?;
let _ = self.reader.reload();
@@ -240,8 +301,8 @@ impl MemoryIndex {
let sanitized: String = query
.chars()
.map(|c| match c {
'+' | '-' | '&' | '|' | '!' | '(' | ')' | '{' | '}' | '[' | ']' | '^'
| '"' | '~' | '*' | '?' | ':' | '\\' | '/' => ' ',
'+' | '-' | '&' | '|' | '!' | '(' | ')' | '{' | '}' | '[' | ']' | '^' | '"'
| '~' | '*' | '?' | ':' | '\\' | '/' => ' ',
_ => c,
})
.collect();
@@ -256,10 +317,11 @@ impl MemoryIndex {
let target_limit = if is_wildcard_fallback { 10 } else { 50 };
let final_query: Box<dyn tantivy::query::Query> = if let Some(ns) = namespace {
let ns_term_query: Box<dyn tantivy::query::Query> = Box::new(tantivy::query::TermQuery::new(
tantivy::Term::from_field_text(self.namespace_field, ns),
tantivy::schema::IndexRecordOption::Basic,
));
let ns_term_query: Box<dyn tantivy::query::Query> =
Box::new(tantivy::query::TermQuery::new(
tantivy::Term::from_field_text(self.namespace_field, ns),
tantivy::schema::IndexRecordOption::Basic,
));
let ns_filter: Box<dyn tantivy::query::Query> = if ns != "global" {
let global_term_query = Box::new(tantivy::query::TermQuery::new(
tantivy::Term::from_field_text(self.namespace_field, "global"),
@@ -463,7 +525,8 @@ impl SearchService {
self.state.code.snippets.read_with(|snips| {
for snippet in snips.iter() {
if let Some(ref emb) = snippet.embedding {
let sim = cosine_similarity(&query_emb, emb);
let base_sim = cosine_similarity(&query_emb, emb);
let sim = base_sim * calculate_time_decay(Some(snippet.updated_at));
results.push(SearchResult {
id: snippet.name.clone(),
doc_type: "snippet".to_string(),
@@ -480,6 +543,7 @@ impl SearchService {
snippet.name.clone(),
"snippet".to_string(),
snippet.description.clone(),
Some(snippet.updated_at),
));
}
}
@@ -487,15 +551,16 @@ impl SearchService {
self.state.read_graph(|graph| {
for entity in graph.entities.values() {
if let Some(ns) = filter_namespace {
if entity.namespace != ns {
continue;
}
if let Some(ns) = filter_namespace
&& entity.namespace != ns
{
continue;
}
let obs = entity.observations.join("; ");
let desc = format!("{}: {}", entity.entity_type, obs);
if let Some(ref emb) = entity.embedding {
let sim = cosine_similarity(&query_emb, emb);
let base_sim = cosine_similarity(&query_emb, emb);
let sim = base_sim * calculate_time_decay(entity.updated_at);
results.push(SearchResult {
id: entity.name.clone(),
doc_type: "entity".to_string(),
@@ -505,7 +570,12 @@ impl SearchService {
});
} else if uncached_texts.len() < 50 {
uncached_texts.push(format!("{} {} {}", entity.name, entity.entity_type, obs));
uncached_meta.push((entity.name.clone(), "entity".to_string(), desc));
uncached_meta.push((
entity.name.clone(),
"entity".to_string(),
desc,
entity.updated_at,
));
}
}
});
@@ -513,7 +583,8 @@ impl SearchService {
self.state.code.error_fixes.read_with(|fixes| {
for fix in fixes.iter() {
if let Some(ref emb) = fix.embedding {
let sim = cosine_similarity(&query_emb, emb);
let base_sim = cosine_similarity(&query_emb, emb);
let sim = base_sim * calculate_time_decay(Some(fix.timestamp));
results.push(SearchResult {
id: fix.signature.clone(),
doc_type: "error_fix".to_string(),
@@ -527,6 +598,7 @@ impl SearchService {
fix.signature.clone(),
"error_fix".to_string(),
fix.solution.clone(),
Some(fix.timestamp),
));
}
}
@@ -535,8 +607,9 @@ impl SearchService {
if !uncached_texts.is_empty()
&& let Ok(embeddings) = generate_embeddings_async(uncached_texts).await
{
for (emb, (title, doc_type, body)) in embeddings.into_iter().zip(uncached_meta) {
let sim = cosine_similarity(&query_emb, &emb);
for (emb, (title, doc_type, body, ts)) in embeddings.into_iter().zip(uncached_meta) {
let base_sim = cosine_similarity(&query_emb, &emb);
let sim = base_sim * calculate_time_decay(ts);
results.push(SearchResult {
id: title.clone(),
doc_type,
+113 -7
View File
@@ -1,5 +1,6 @@
use crate::models::*;
use crate::search::MemoryIndex;
pub use crate::search::{SearchResult as UnifiedSearchResult, SearchService};
use crate::store::Store;
use std::collections::HashMap;
use std::path::PathBuf;
@@ -42,6 +43,78 @@ pub struct TelemetryStores {
pub agent_signals: Store<Vec<AgentSignal>>,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct CachedClipboardImage {
pub file_path: String,
pub file_path_wsl: String,
pub captured_at_epoch_ms: u64,
pub age: String,
pub width: u32,
pub height: u32,
pub size_bytes: usize,
pub ocr_text: Option<String>,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct CachedClipboardText {
pub text: String,
pub captured_at_epoch_ms: u64,
pub age: String,
pub char_count: usize,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ClipboardHistoryItem {
Image(CachedClipboardImage),
Text(CachedClipboardText),
}
#[derive(Default)]
pub struct ClipboardCacheState {
pub last_image: Option<CachedClipboardImage>,
pub last_text: Option<CachedClipboardText>,
pub history: std::collections::VecDeque<ClipboardHistoryItem>,
}
impl ClipboardCacheState {
pub fn push_image(&mut self, image: CachedClipboardImage) {
self.last_image = Some(image.clone());
self.history.push_front(ClipboardHistoryItem::Image(image));
if self.history.len() > 20 {
self.history.pop_back();
}
}
pub fn push_text(&mut self, text: CachedClipboardText) {
if let Some(ref prev) = self.last_text
&& prev.text == text.text
{
return;
}
self.last_text = Some(text.clone());
self.history.push_front(ClipboardHistoryItem::Text(text));
if self.history.len() > 20 {
self.history.pop_back();
}
}
}
pub fn format_age(epoch_ms: u64) -> String {
let now_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
let diff_secs = now_ms.saturating_sub(epoch_ms) / 1000;
if diff_secs < 60 {
format!("{}s ago", diff_secs)
} else if diff_secs < 3600 {
format!("{}m ago", diff_secs / 60)
} else {
format!("{}h ago", diff_secs / 3600)
}
}
pub struct MemoryState {
pub base_dir: PathBuf,
pub index_commit_notify: Arc<tokio::sync::Notify>,
@@ -59,6 +132,7 @@ pub struct MemoryState {
pub activity_tx: tokio::sync::broadcast::Sender<String>,
pub event_bus_tx: tokio::sync::broadcast::Sender<GenericEvent>,
pub ollama: Arc<crate::ollama::OllamaClient>,
pub clipboard_cache: Arc<tokio::sync::RwLock<ClipboardCacheState>>,
}
impl MemoryState {
@@ -69,10 +143,8 @@ impl MemoryState {
pub fn new(base_dir_str: &str) -> Self {
let is_in_memory = base_dir_str == ":memory:";
let base = std::path::PathBuf::from(base_dir_str);
if !is_in_memory {
if let Err(e) = std::fs::create_dir_all(&base) {
tracing::error!("Failed to create store directory at {:?}: {}", base, e);
}
if !is_in_memory && let Err(e) = std::fs::create_dir_all(&base) {
tracing::error!("Failed to create store directory at {:?}: {}", base, e);
}
let db = crate::db::init_redb(&base);
@@ -133,6 +205,7 @@ impl MemoryState {
activity_tx: tokio::sync::broadcast::channel(100).0,
event_bus_tx: tokio::sync::broadcast::channel(1000).0,
clipboard_cache: Arc::new(tokio::sync::RwLock::new(ClipboardCacheState::default())),
};
// Normalize pre-existing graph entity and relation types
@@ -162,6 +235,19 @@ impl MemoryState {
pub fn modify_graph<F: FnOnce(&mut KnowledgeGraph)>(&self, update_fn: F) {
self.graph.modify(update_fn);
let threshold: usize = std::env::var("MCP_MEMORY_CONDENSE_THRESHOLD")
.unwrap_or_else(|_| "100".to_string())
.parse()
.unwrap_or(100);
let count = self.graph.read_with(|g| g.entities.len());
if count >= threshold {
self.condense_notify.notify_one();
}
let _ = self.event_bus_tx.send(GenericEvent {
topic: "resource:updated".to_string(),
session_id: None,
payload: serde_json::json!({ "uri": "memory://graph" }),
});
}
pub async fn get_search_index(&self) -> MemoryIndex {
@@ -283,7 +369,24 @@ impl MemoryState {
}
pub fn broadcast_task_event(&self, event: TaskEvent) {
let payload_val = serde_json::to_value(&event).unwrap_or_default();
let mut payload_val = serde_json::to_value(&event).unwrap_or_default();
if let Some(obj) = payload_val.as_object_mut() {
let active_task = self.project.tasks.read_with(|tasks| {
tasks
.iter()
.find(|t| t.is_active())
.or_else(|| {
tasks
.iter()
.find(|t| t.status == "pending" && t.parent_id.is_none())
})
.map(|t| t.title.clone())
});
obj.insert(
"active_task".to_string(),
serde_json::to_value(active_task).unwrap_or_default(),
);
}
let summary_str = format!("Task {} -> {}", event.task_id, event.status);
let details_str = payload_val.to_string();
@@ -315,6 +418,11 @@ impl MemoryState {
payload: payload_val,
};
let _ = self.event_bus_tx.send(generic_ev);
let _ = self.event_bus_tx.send(GenericEvent {
topic: "resource:updated".to_string(),
session_id: None,
payload: serde_json::json!({ "uri": "memory://tasks/active" }),
});
let ws_notification = serde_json::json!({
"jsonrpc": "2.0",
@@ -468,5 +576,3 @@ mod tests {
assert!(activities_updated[0].timestamp >= activities_updated[1].timestamp);
}
}
pub use crate::search::{SearchResult as UnifiedSearchResult, SearchService};
+17 -17
View File
@@ -33,10 +33,11 @@ static QUEUE_REGISTRY: std::sync::Mutex<Option<(Arc<Database>, DbWriteQueue)>> =
fn get_or_create_queue(db: Arc<Database>) -> DbWriteQueue {
let mut reg = QUEUE_REGISTRY.lock().unwrap_or_else(|e| e.into_inner());
if let Some((ref existing_db, ref queue)) = *reg {
if Arc::ptr_eq(existing_db, &db) && !queue.tx.is_closed() {
return queue.clone();
}
if let Some((ref existing_db, ref queue)) = *reg
&& Arc::ptr_eq(existing_db, &db)
&& !queue.tx.is_closed()
{
return queue.clone();
}
let new_queue = DbWriteQueue::new(db.clone());
*reg = Some((db, new_queue.clone()));
@@ -334,19 +335,17 @@ impl<T: DeserializeOwned + Default + Serialize + Send + Sync + 'static> Store<T>
let mut known = std::collections::HashSet::new();
if let Ok(range) = table.range(prefix.as_str()..) {
for entry in range {
if let Ok((k, v)) = entry {
let k_str = k.value();
if !k_str.starts_with(&prefix) {
break;
}
found_granular = true;
known.insert(k_str.to_string());
if let Ok(val) = serde_json::from_slice::<serde_json::Value>(v.value()) {
let sub_key = &k_str[prefix.len()..];
items_array.push(val.clone());
items_map.insert(sub_key.to_string(), val);
}
for (k, v) in range.flatten() {
let k_str = k.value();
if !k_str.starts_with(&prefix) {
break;
}
found_granular = true;
known.insert(k_str.to_string());
if let Ok(val) = serde_json::from_slice::<serde_json::Value>(v.value()) {
let sub_key = &k_str[prefix.len()..];
items_array.push(val.clone());
items_map.insert(sub_key.to_string(), val);
}
}
}
@@ -461,6 +460,7 @@ impl<T: DeserializeOwned + Default + Serialize + Send + Sync + 'static> Store<T>
}
}
#[allow(clippy::type_complexity)]
fn prepare_batch(
&self,
new_snapshot: &T,
+36 -9
View File
@@ -172,8 +172,6 @@ pub enum SubgraphFormat {
pub struct GetSubgraphTool {
/// The root entity name to start the subgraph search from.
pub root_entity: Option<String>,
/// Legacy alias for root_entity.
pub root_node: Option<String>,
/// Maximum search depth (hops). Defaults to 2.
pub depth: Option<u32>,
/// Output format: 'json' (raw entities and relations) or 'markdown_tree' (compact topology tree). Defaults to 'json'.
@@ -612,6 +610,7 @@ pub enum HandoffMemoAction {
/// - 'leave': Post a handoff memo. Required: content. Optional: namespace.
/// - 'read': Inspect active memos. Optional: namespace.
/// - 'clear': Clear memos. Optional: namespace, ids.
///
/// Next steps on error: Check parameter requirements for 'leave' or 'clear' actions.
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct HandoffMemosTool {
@@ -655,6 +654,7 @@ pub enum TaskAction {
/// - 'list': List active tasks. Optional: git_branch, summary_level ('compact'|'detailed'|'full'), max_tokens.
/// - 'set_criteria': Set acceptance criteria. Required: id, criteria (array of strings).
/// - 'verify': Verify criteria met. Required: id.
///
/// Next steps on error: Check required parameters or call list to verify task IDs.
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct TasksTool {
@@ -719,6 +719,7 @@ pub enum MilestoneAction {
/// - 'add': Create milestone. Required: title. Optional: namespace.
/// - 'update': Update milestone status. Required: id, status.
/// - 'list': List milestones. Optional: namespace.
///
/// Next steps on error: Provide required title for 'add' or id/status for 'update'.
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct MilestonesTool {
@@ -765,6 +766,7 @@ pub enum SnippetAction {
/// - 'search': Search snippets. Optional: query, tags, hybrid (boolean for BM25+vector search).
/// - 'delete': Delete snippet. Required: id (or query) as snippet name.
/// - 'tag': Tag snippet. Required: id (or query), tags (array of strings).
///
/// Next steps on error: Ensure snippet name/query or id is provided.
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct SnippetsTool {
@@ -815,6 +817,7 @@ pub enum DecisionAction {
/// - 'query': Search ADRs. Optional: query, namespace.
/// - 'update': Update ADR status or implementation info. Required: id. Optional: status, git_commit, git_branch, task_id, context, decision, consequences, title.
/// - 'delete': Remove ADR. Required: id.
///
/// Next steps on error: Provide title for 'log' or id for 'update'/'delete'.
#[derive(Deserialize, Serialize, JsonSchema, Debug, Clone)]
pub struct DecisionsTool {
@@ -875,6 +878,7 @@ pub enum TechDebtAction {
/// - 'log': Record technical debt. Required: description. Optional: ideal_solution, git_commit, git_branch, symbol_references, line_range, namespace.
/// - 'resolve': Mark tech debt resolved. Required: id.
/// - 'list': List tech debt items. Optional: include_resolved, namespace.
///
/// Next steps on error: Provide description for 'log' or valid ID for 'resolve'.
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct TechDebtTool {
@@ -963,6 +967,7 @@ pub enum EnvAction {
/// - 'log_requirement': Log environment variable requirement. Required: key. Optional: description, is_secret, namespace.
/// - 'register': Register remote target environment. Required: name. Optional: url, description, requires_vpn, namespace.
/// - 'get_details': Get full environment details. Optional: namespace.
///
/// Next steps on error: Provide key for 'log_requirement' or name for 'register'.
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct EnvironmentTool {
@@ -984,24 +989,46 @@ pub struct EnvironmentTool {
pub requires_vpn: Option<bool>,
/// Optional namespace.
pub namespace: Option<String>,
/// Optional operating system name (e.g. 'windows', 'linux', 'darwin').
pub os: Option<String>,
/// Optional shell name (e.g. 'pwsh', 'bash', 'zsh', 'cmd').
pub shell: Option<String>,
}
#[derive(Debug, Deserialize, Serialize, JsonSchema, PartialEq)]
#[derive(Debug, Clone, Copy, Deserialize, Serialize, JsonSchema, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ClipboardAction {
#[serde(alias = "read", alias = "READ", alias = "Read")]
Read,
#[serde(
alias = "image",
alias = "IMAGE",
alias = "Image",
alias = "screenshot",
alias = "SCREENSHOT"
)]
Image,
#[serde(alias = "text", alias = "TEXT", alias = "Text")]
Text,
#[serde(alias = "history", alias = "HISTORY", alias = "History")]
History,
#[serde(alias = "clear", alias = "CLEAR", alias = "Clear")]
Clear,
#[serde(alias = "write", alias = "WRITE", alias = "Write")]
Write,
}
/// OS Clipboard management (read, write).
/// Smart OS Clipboard management with overwrite-immune screenshot caching and OCR.
/// Actions:
/// - 'read': Read current clipboard contents.
/// - 'image': Get latest screenshot image path (Windows + WSL) and extracted verbatim OCR text.
/// Returns cached screenshot even if text was copied afterwards!
/// - 'text': Get latest clipboard text (or normalized Markdown if HTML was copied).
/// - 'history': View recent clipboard history ring buffer (images and text with timestamps).
/// - 'clear': Clear OS clipboard and memory cache.
/// - 'write': Write content to OS clipboard. Optional: text, html, files, image_path.
/// Next steps on error: Provide text/html/files/image_path when calling 'write'.
///
/// Triggers: Call 'image' immediately when user says "look at image in clipboard", "see screenshot",
/// "look at clipboard", "what I copied", or shares terminal error captures.
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct ClipboardTool {
/// Action to perform: 'read' or 'write'.
/// Action to perform: 'image', 'text', 'history', 'clear', 'read', or 'write'.
pub action: ClipboardAction,
/// Plain text content (for 'write').
pub text: Option<String>,
+111 -70
View File
@@ -79,7 +79,11 @@ pub fn spawn_watcher(state: Arc<MemoryState>) {
fn should_review(path: &Path) -> bool {
let path_str = path.to_string_lossy();
if path_str.contains(".git") || path_str.contains("target") || path_str.contains(".gemini") || path_str.contains("node_modules") {
if path_str.contains(".git")
|| path_str.contains("target")
|| path_str.contains(".gemini")
|| path_str.contains("node_modules")
{
return false;
}
@@ -89,85 +93,123 @@ fn should_review(path: &Path) -> bool {
}
async fn trigger_autonomous_review(path: &Path, state: Arc<MemoryState>) {
info!("Triggering autonomous review & incremental AST index for {:?}", path);
state.broadcast_activity("AUTONOMOUS", &format!("Modified: {:?}", path.file_name().unwrap_or_default()));
info!(
"Triggering autonomous review & incremental AST index for {:?}",
path
);
state.broadcast_activity(
"AUTONOMOUS",
&format!("Modified: {:?}", path.file_name().unwrap_or_default()),
);
// ADR-0109: Incremental Background AST Indexing & Differential Graph Updates
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
if matches!(ext, "rs" | "ts" | "js" | "py" | "go" | "java" | "c" | "cpp") {
if let Ok(content) = std::fs::read_to_string(path) {
let language = match ext {
"rs" => Some(tree_sitter_rust::LANGUAGE),
"ts" | "js" => Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT),
"py" => Some(tree_sitter_python::LANGUAGE),
"java" => Some(tree_sitter_java::LANGUAGE),
"c" => Some(tree_sitter_c::LANGUAGE),
"cpp" => Some(tree_sitter_cpp::LANGUAGE),
"go" => Some(tree_sitter_go::LANGUAGE),
_ => None,
};
if matches!(ext, "rs" | "ts" | "js" | "py" | "go" | "java" | "c" | "cpp")
&& let Ok(content) = std::fs::read_to_string(path)
{
let language = match ext {
"rs" => Some(tree_sitter_rust::LANGUAGE),
"ts" | "js" => Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT),
"py" => Some(tree_sitter_python::LANGUAGE),
"java" => Some(tree_sitter_java::LANGUAGE),
"c" => Some(tree_sitter_c::LANGUAGE),
"cpp" => Some(tree_sitter_cpp::LANGUAGE),
"go" => Some(tree_sitter_go::LANGUAGE),
_ => None,
};
if let Some(lang) = language {
let mut parser = tree_sitter::Parser::new();
if parser.set_language(&lang.into()).is_ok() {
if let Some(tree) = parser.parse(&content, None) {
let mut chunks = Vec::new();
crate::indexer::extract_chunks(tree.root_node(), &content, &mut chunks, ext);
let file_str = path.to_string_lossy().to_string();
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
if let Some(lang) = language {
let mut parser = tree_sitter::Parser::new();
if parser.set_language(&lang.into()).is_ok()
&& let Some(tree) = parser.parse(&content, None)
{
let mut chunks = Vec::new();
crate::indexer::extract_chunks(tree.root_node(), &content, &mut chunks, ext);
let file_str = path.to_string_lossy().to_string();
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
state.modify_graph(|g| {
// Ensure File entity exists
g.entities.entry(file_str.clone()).or_insert_with(|| {
crate::models::Entity {
name: file_str.clone(),
entity_type: "File".to_string(),
namespace: "global".to_string(),
file_path: Some(file_str.clone()),
created_at: Some(now),
updated_at: Some(now),
..Default::default()
}
});
for (chunk_name, chunk_code, chunk_desc) in chunks {
let symbol_name = format!("{}::{}", file_str, chunk_name);
let symbol_type = if chunk_desc.contains("struct") {
"DataStructure".to_string()
} else {
"McpTool".to_string()
};
g.entities.insert(symbol_name.clone(), crate::models::Entity {
name: symbol_name.clone(),
entity_type: symbol_type,
observations: vec![format!("AST definition: {} chars", chunk_code.len())],
namespace: "global".to_string(),
file_path: Some(file_str.clone()),
created_at: Some(now),
updated_at: Some(now),
..Default::default()
});
let rel = crate::models::Relation {
from: file_str.clone(),
to: symbol_name,
relation_type: "declares".to_string(),
namespace: "global".to_string(),
..Default::default()
};
if !g.relations.contains(&rel) {
g.relations.push(rel);
}
}
let chunks_count = chunks.len();
let file_str_clone = file_str.clone();
state.modify_graph(|g| {
// Ensure File entity exists
g.entities
.entry(file_str.clone())
.or_insert_with(|| crate::models::Entity {
name: file_str.clone(),
entity_type: "File".to_string(),
namespace: "global".to_string(),
file_path: Some(file_str.clone()),
created_at: Some(now),
updated_at: Some(now),
..Default::default()
});
for (chunk_name, chunk_code, chunk_desc) in chunks {
let symbol_name = format!("{}::{}", file_str, chunk_name);
let symbol_type = if chunk_desc.contains("struct") {
"DataStructure".to_string()
} else {
"McpTool".to_string()
};
g.entities.insert(
symbol_name.clone(),
crate::models::Entity {
name: symbol_name.clone(),
entity_type: symbol_type,
observations: vec![format!(
"AST definition: {} chars",
chunk_code.len()
)],
namespace: "global".to_string(),
file_path: Some(file_str.clone()),
created_at: Some(now),
updated_at: Some(now),
..Default::default()
},
);
let rel = crate::models::Relation {
from: file_str.clone(),
to: symbol_name,
relation_type: "declares".to_string(),
namespace: "global".to_string(),
..Default::default()
};
if !g.relations.contains(&rel) {
g.relations.push(rel);
}
}
}
});
let _ = state.event_bus_tx.send(crate::state::GenericEvent {
topic: "ast:symbol_updated".to_string(),
session_id: None,
payload: serde_json::json!({
"file": file_str_clone,
"extension": ext,
"symbols_count": chunks_count,
}),
});
let _ = state.event_bus_tx.send(crate::state::GenericEvent {
topic: "resource:updated".to_string(),
session_id: None,
payload: serde_json::json!({
"uri": "memory://graph"
}),
});
}
}
}
info!("Autonomous review & incremental AST index complete for {:?}", path);
info!(
"Autonomous review & incremental AST index complete for {:?}",
path
);
}
#[cfg(test)]
mod tests {
@@ -199,4 +241,3 @@ mod tests {
spawn_watcher(state);
}
}
+6
View File
@@ -0,0 +1,6 @@
#[test]
fn test_img() {
let mut img = image::DynamicImage::new_rgb8(10, 10);
img.invert();
let _img2 = img.adjust_contrast(20.0);
}
+25 -12
View File
@@ -93,18 +93,18 @@ fn test_dashboard_route_parity() {
"server/src/api/setup.rs"
};
let dashboard_content = std::fs::read_to_string(dashboard_path)
.expect("Failed to read dashboard.ts");
let setup_content = std::fs::read_to_string(setup_path)
.expect("Failed to read setup.rs");
let dashboard_content =
std::fs::read_to_string(dashboard_path).expect("Failed to read dashboard.ts");
let setup_content = std::fs::read_to_string(setup_path).expect("Failed to read setup.rs");
// 1. Extract routes from setup.rs
let mut registered_routes = HashSet::new();
for part in setup_content.split(".route(") {
let trimmed = part.trim_start();
if trimmed.starts_with('"') {
if let Some(end_quote) = trimmed[1..].find('"') {
let route = &trimmed[1..=end_quote];
#[allow(clippy::collapsible_if)]
if let Some(stripped) = trimmed.strip_prefix('"') {
if let Some(end_quote) = stripped.find('"') {
let route = &stripped[..end_quote];
registered_routes.insert(route.to_string());
}
}
@@ -120,8 +120,10 @@ fn test_dashboard_route_parity() {
let rest = &dashboard_content[start..];
let quote_char = dashboard_content[..start].chars().last();
let mut endpoint = String::new();
#[allow(clippy::collapsible_if)]
if let Some(q) = quote_char {
if q == '`' || q == '"' || q == '\'' {
let is_quote = q == '`' || q == '"' || q == '\'';
if is_quote {
if let Some(close_idx) = rest.find(q) {
let full_call = &rest[..close_idx];
let clean = if let Some(q_idx) = full_call.find('?') {
@@ -129,13 +131,21 @@ fn test_dashboard_route_parity() {
} else {
full_call
};
let normalized = clean.replace("${id}", "{id}");
endpoint = normalized.to_string();
let mut normalized = clean.to_string();
while let Some(start_var) = normalized.find("${") {
if let Some(end_var) = normalized[start_var..].find('}') {
normalized.replace_range(start_var..=start_var + end_var, "{id}");
} else {
break;
}
}
endpoint = normalized;
}
}
}
if endpoint.is_empty() {
let end_idx = rest.find(['"', '`', '\'', ' ', '\n', '?'])
let end_idx = rest
.find(['"', '`', '\'', ' ', '\n', '?'])
.unwrap_or(rest.len());
endpoint = rest[..end_idx].to_string();
}
@@ -146,7 +156,10 @@ fn test_dashboard_route_parity() {
cursor = start + 5;
}
assert!(!frontend_endpoints.is_empty(), "No frontend endpoints found in dashboard.ts");
assert!(
!frontend_endpoints.is_empty(),
"No frontend endpoints found in dashboard.ts"
);
for endpoint in &frontend_endpoints {
assert!(
+3
View File
@@ -17,3 +17,6 @@ serde_json = "1.0.151"
mcp-stdio = { version = "1.0.0", path = "../mcp-stdio" }
regex = "1.13.1"
async-channel = "2.5.0"
[build-dependencies]
chrono = "0.4.45"
+20 -3
View File
@@ -1,22 +1,39 @@
use std::process::Command;
fn main() {
let git_hash = Command::new("git")
let mut git_hash = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.unwrap_or_else(|| "unknown".to_string());
git_hash = git_hash.trim().to_string();
let is_dirty = Command::new("git")
.args(["status", "--porcelain"])
.output()
.is_ok_and(|out| !out.stdout.is_empty());
if is_dirty {
let dirty_ts = chrono::Local::now().format("%y.%m.%d.%H%M%S").to_string();
git_hash.push_str(&format!("-dirty-{dirty_ts}"));
}
let git_date = Command::new("git")
.args(["log", "-1", "--format=%cd", "--date=format:%y.%m.%d.%H%M%S"])
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.unwrap_or_else(|| "unknown".to_string());
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| chrono::Local::now().format("%y.%m.%d.%H%M%S").to_string());
let version = format!("{} ({})", git_date.trim(), git_hash.trim());
let version = format!("v{git_date} ({git_hash})");
println!("cargo:rustc-env=APP_VERSION={version}");
println!("cargo:rerun-if-changed=../.git/HEAD");
println!("cargo:rerun-if-changed=../.git/index");
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=build.rs");
}
+24 -8
View File
@@ -48,28 +48,45 @@ mod tests {
#[test]
fn test_extract_log_prefix_response() {
let json_str = r#"{"jsonrpc":"2.0","id":123,"result":{}}"#;
assert_eq!(extract_log_prefix(json_str, true), "Response id=123 [Error: false]");
assert_eq!(
extract_log_prefix(json_str, true),
"Response id=123 [Error: false]"
);
let err_json = r#"{"jsonrpc":"2.0","id":456,"error":{"code":-32600,"message":"Invalid Request"}}"#;
assert_eq!(extract_log_prefix(err_json, true), "Response id=456 [Error: true]");
let err_json =
r#"{"jsonrpc":"2.0","id":456,"error":{"code":-32600,"message":"Invalid Request"}}"#;
assert_eq!(
extract_log_prefix(err_json, true),
"Response id=456 [Error: true]"
);
let is_err_json = r#"{"jsonrpc":"2.0","id":789,"result":{"isError":true}}"#;
assert_eq!(extract_log_prefix(is_err_json, true), "Response id=789 [Error: true]");
assert_eq!(
extract_log_prefix(is_err_json, true),
"Response id=789 [Error: true]"
);
}
#[test]
fn test_extract_log_prefix_tool_call() {
let json_str = r#"{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"search"}}"#;
let json_str =
r#"{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"search"}}"#;
assert_eq!(extract_log_prefix(json_str, false), "ToolCall[search] id=1");
let unknown_tool = r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{}}"#;
assert_eq!(extract_log_prefix(unknown_tool, false), "ToolCall[unknown] id=2");
assert_eq!(
extract_log_prefix(unknown_tool, false),
"ToolCall[unknown] id=2"
);
}
#[test]
fn test_extract_log_prefix_request() {
let json_str = r#"{"jsonrpc":"2.0","id":10,"method":"initialize"}"#;
assert_eq!(extract_log_prefix(json_str, false), "Request[initialize] id=10");
assert_eq!(
extract_log_prefix(json_str, false),
"Request[initialize] id=10"
);
}
#[test]
@@ -84,4 +101,3 @@ mod tests {
assert_eq!(extract_log_prefix(json_str, false), "Message");
}
}
+2 -2
View File
@@ -170,11 +170,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
}
if retry_count >= 20 {
return Err("Failed to connect to the MCP memory server after multiple attempts. Is it running?".into());
}
Ok(())
})
}