Skip to content

refactor: deduplicate tool code and remove dead stubs - #98

Merged
ilblackdragon merged 2 commits into
mainfrom
refactor/deduplicate-tool-code
Feb 15, 2026
Merged

ilblackdragon merged 2 commits into
mainfrom
refactor/deduplicate-tool-code

Conversation

@ilblackdragon

Copy link
Copy Markdown
Member

Summary

  • Delete 4 dead stub tools (marketplace, restaurant, ecommerce, taskrabbit) that were exported but never registered in ToolRegistry, removing ~625 lines of dead code
  • Add require_str/require_param helpers to src/tools/tool.rs and refactor ~30 call sites across 10 tool files from 4-6 line inline parameter extractions to single-line calls
  • Consolidate worker HTTP client with get_json/post_json private helpers in src/worker/api.rs, deduplicating bearer-auth + status-check + JSON-parse boilerplate across 4 methods

Net: -720 lines (157 added, 877 removed) across 19 files.

Test plan

  • cargo fmt clean
  • cargo clippy --all --benches --tests --examples --all-features zero warnings
  • cargo test all 836 tests pass
  • cargo check --no-default-features --features libsql compiles clean
  • No .unwrap()/.expect() in production code
  • No super:: imports in changed files

🤖 Generated with Claude Code

…tools

Delete 4 never-registered stub tools (marketplace, restaurant, ecommerce,
taskrabbit) removing ~625 lines of dead code. Add require_str/require_param
helpers to tool.rs and refactor ~30 call sites across 10 tool files from
4-6 line inline extractions to single-line calls. Consolidate worker HTTP
client with get_json/post_json helpers, reducing boilerplate in 4 methods.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 15, 2026 01:23
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @ilblackdragon, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves code quality and maintainability by eliminating unused tool stubs and introducing standardized helper functions for common operations. The changes streamline parameter handling in tools and centralize HTTP client logic, making the codebase cleaner, more robust, and easier to extend. The overall impact is a substantial reduction in code lines and improved consistency across the project.

Highlights

  • Dead Code Removal: Deleted four unused stub tool files (marketplace, restaurant, ecommerce, taskrabbit) and their associated references, resulting in a significant reduction of approximately 625 lines of code.
  • Parameter Extraction Helpers: Introduced require_str and require_param helper functions in src/tools/tool.rs to simplify and standardize the extraction of required parameters from JSON objects, replacing verbose inline checks across numerous tool implementations.
  • HTTP Client Consolidation: Refactored the worker HTTP client in src/worker/api.rs by adding private get_json and post_json helpers, which encapsulate common logic for bearer authentication, status checking, and JSON parsing, thereby deduplicating boilerplate code.
Changelog
  • src/estimation/cost.rs
    • Removed cost entries for the marketplace, ecommerce, taskrabbit, and restaurant tools.
    • Updated a test assertion to reflect the removal of the marketplace tool cost.
  • src/estimation/time.rs
    • Removed duration entries for the marketplace, ecommerce, taskrabbit, and restaurant tools.
  • src/tools/builtin/echo.rs
    • Updated parameter extraction for 'message' to use the new require_str helper.
  • src/tools/builtin/ecommerce.rs
    • Removed the entire ecommerce tool file.
  • src/tools/builtin/extension_tools.rs
    • Updated parameter extraction for 'name' in ToolInstallTool, ToolAuthTool, ToolActivateTool, and ToolRemoveTool to use the require_str helper.
  • src/tools/builtin/file.rs
    • Updated parameter extraction for 'path', 'content', 'old_string', and 'new_string' in ReadFileTool, WriteFileTool, and ApplyPatchTool to use the require_str helper.
  • src/tools/builtin/http.rs
    • Updated parameter extraction for 'method' and 'url' in HttpTool to use the require_str helper.
  • src/tools/builtin/job.rs
    • Updated parameter extraction for 'title', 'description', and 'job_id' in CreateJobTool, JobStatusTool, and CancelJobTool to use the require_str helper.
  • src/tools/builtin/json.rs
    • Updated parameter extraction for 'operation' to use require_str and for 'data' to use require_param.
  • src/tools/builtin/marketplace.rs
    • Removed the entire marketplace tool file.
  • src/tools/builtin/memory.rs
    • Updated parameter extraction for 'query', 'content', and 'path' in MemorySearchTool, MemoryWriteTool, and MemoryReadTool to use the require_str helper.
  • src/tools/builtin/mod.rs
    • Removed module imports and pub use statements for ecommerce, marketplace, restaurant, and taskrabbit tools.
  • src/tools/builtin/restaurant.rs
    • Removed the entire restaurant tool file.
  • src/tools/builtin/routine.rs
    • Updated parameter extraction for 'name', 'trigger_type', and 'prompt' in RoutineCreateTool, RoutineUpdateTool, RoutineDeleteTool, and RoutineHistoryTool to use the require_str helper.
  • src/tools/builtin/shell.rs
    • Updated parameter extraction for 'command' in ShellTool to use the require_str helper.
  • src/tools/builtin/taskrabbit.rs
    • Removed the entire taskrabbit tool file.
  • src/tools/builtin/time.rs
    • Updated parameter extraction for 'operation', 'timestamp', and 'timestamp2' in TimeTool to use the require_str helper.
  • src/tools/tool.rs
    • Added require_str function to extract a required string parameter from a JSON object.
    • Added require_param function to extract a required parameter of any type from a JSON object.
    • Updated the EchoTool test implementation to use the new require_str helper.
    • Added unit tests for require_str and require_param functions covering present, missing, and wrong type scenarios.
  • src/worker/api.rs
    • Introduced a private get_json helper method to handle common GET request logic, including bearer authentication, status checks, and JSON deserialization.
    • Introduced a private post_json helper method to handle common POST request logic, including bearer authentication, JSON serialization, status checks, and JSON deserialization.
    • Refactored get_job to utilize the new get_json helper.
    • Refactored llm_complete to utilize the new post_json helper.
    • Refactored llm_complete_with_tools to utilize the new post_json helper.
    • Refactored report_complete to utilize the new post_json helper.
Activity
  • The pull request was created by ilblackdragon.
  • The pull request description details the summary of changes, including dead code removal, new helper functions, and HTTP client consolidation.
  • A test plan is provided, confirming cargo fmt clean, cargo clippy zero warnings, cargo test passing, cargo check compiling clean, and no unwrap()/expect() in production code.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly improves the codebase through excellent refactoring, including the removal of over 600 lines of dead code from unused tool stubs and the introduction of require_str/require_param helpers for simplified parameter handling. It also consolidates HTTP logic in the worker API with get_json/post_json helpers. However, a critical security issue was identified in the ShellTool's safety mechanisms: its blocklist and auto-approval prevention logic rely on easily bypassable string containment checks, which could lead to unintended execution of destructive commands. Implementing more robust command validation or whitespace normalization is recommended. Furthermore, a potential runtime failure may have been introduced by the refactoring of report_complete.

Comment thread src/worker/api.rs
Comment on lines +340 to +342
let _: serde_json::Value = self
.post_json("complete", report, "report complete")
.await?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The refactoring to use post_json here introduces a requirement that the /complete endpoint returns a valid JSON body. The previous implementation did not have this requirement and would have succeeded with an empty response body (e.g., from a 204 No Content or 200 OK with no body).

If the orchestrator's /complete endpoint can return a successful status with an empty body, this change will cause the worker to fail when reporting completion, as resp.json() will fail to parse an empty body. This could be a critical issue in the worker's lifecycle.

To address this, you could create a separate helper for POST requests that don't expect a JSON response body, or adjust this function to handle non-JSON success responses.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The orchestrator's report_complete handler was returning bare StatusCode::OK with no body, which would cause post_json's .json() call to fail. Fixed in 68faf85 by changing the handler to return Json({"status": "ok"}), consistent with the other worker endpoints.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the codebase to reduce duplication and remove dead code. It introduces helper functions for common parameter extraction patterns, consolidates HTTP client logic with reusable methods, and deletes 4 unregistered tool stubs that were never integrated into the system.

Changes:

  • Added require_str and require_param helper functions to eliminate repetitive parameter extraction boilerplate across 10 tool files
  • Introduced get_json and post_json private helpers in WorkerHttpClient to deduplicate bearer auth, status checking, and JSON parsing logic
  • Removed 4 dead tool stubs (marketplace, restaurant, ecommerce, taskrabbit) that were exported but never registered in ToolRegistry

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/worker/api.rs Refactored HTTP client with get_json and post_json helpers to consolidate bearer auth, status checks, and JSON parsing across 4 methods
src/tools/tool.rs Added require_str and require_param helper functions with comprehensive tests for extracting required parameters from JSON objects
src/tools/builtin/time.rs Replaced 3 inline parameter extractions with require_str calls
src/tools/builtin/taskrabbit.rs Deleted 157-line dead stub tool that was never registered
src/tools/builtin/shell.rs Replaced inline parameter extraction with require_str call
src/tools/builtin/routine.rs Replaced 6 inline parameter extractions with require_str calls across 4 tool implementations
src/tools/builtin/restaurant.rs Deleted 172-line dead stub tool that was never registered
src/tools/builtin/mod.rs Removed module declarations and exports for 4 deleted tools
src/tools/builtin/memory.rs Replaced 4 inline parameter extractions with require_str calls
src/tools/builtin/marketplace.rs Deleted 160-line dead stub tool that was never registered
src/tools/builtin/json.rs Replaced 2 inline parameter extractions with require_str and require_param calls
src/tools/builtin/job.rs Replaced 4 inline parameter extractions with require_str calls
src/tools/builtin/http.rs Replaced 2 inline parameter extractions with require_str calls
src/tools/builtin/file.rs Replaced 5 inline parameter extractions with require_str calls
src/tools/builtin/extension_tools.rs Replaced 4 inline parameter extractions with require_str calls
src/tools/builtin/ecommerce.rs Deleted 136-line dead stub tool that was never registered
src/tools/builtin/echo.rs Replaced inline parameter extraction with require_str call
src/estimation/time.rs Removed duration estimates for 4 deleted tools
src/estimation/cost.rs Removed cost estimates for 4 deleted tools and updated tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/worker/api.rs
Comment on lines +340 to +342
let _: serde_json::Value = self
.post_json("complete", report, "report complete")
.await?;

Copilot AI Feb 15, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The report_complete method now uses post_json, which returns WorkerError::LlmProxyFailed for connection and status errors. However, the original implementation used WorkerError::ConnectionFailed for connection errors and WorkerError::OrchestratorRejected for non-success status codes. This is a semantic error because report_complete is not an LLM proxy operation - it's a completion report to the orchestrator.

Consider creating a separate helper method for non-LLM POST requests that uses appropriate error types, or pass an error type parameter to post_json to allow customization.

Suggested change
let _: serde_json::Value = self
.post_json("complete", report, "report complete")
.await?;
let resp = self
.client
.post(self.url("complete"))
.bearer_auth(&self.token)
.json(report)
.send()
.await
.map_err(|e| WorkerError::ConnectionFailed {
url: self.orchestrator_url.clone(),
reason: e.to_string(),
})?;
if !resp.status().is_success() {
return Err(WorkerError::OrchestratorRejected {
job_id: self.job_id,
reason: format!("complete endpoint returned {}", resp.status()),
});
}
// Consume and validate the JSON response from the orchestrator, if any.
let _: serde_json::Value =
resp.json().await.map_err(|e| WorkerError::OrchestratorRejected {
job_id: self.job_id,
reason: format!("failed to parse complete response: {}", e),
})?;

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point about the empty-body issue and the error type mismatch. Fixed in 68faf85 by changing the orchestrator's report_complete handler to return Json({"status": "ok"}) instead of bare StatusCode::OK. This makes all worker POST endpoints consistently return JSON, so post_json works uniformly. The error variant difference (LlmProxyFailed vs OrchestratorRejected) is cosmetic in practice since the context string already identifies the operation.

The report_complete handler returned bare StatusCode::OK (no body),
which broke the post_json helper that expects a JSON response.
Return {"status": "ok"} for consistency with other worker endpoints.

Addresses review feedback on PR #98.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ilblackdragon
ilblackdragon merged commit ca8d5c6 into main Feb 15, 2026
11 checks passed
@ilblackdragon
ilblackdragon deleted the refactor/deduplicate-tool-code branch February 15, 2026 05:39
@github-actions github-actions Bot mentioned this pull request Feb 15, 2026
jaswinder6991 pushed a commit to jaswinder6991/ironclaw that referenced this pull request Feb 26, 2026
* refactor: deduplicate tool parameter extraction and remove dead stub tools

Delete 4 never-registered stub tools (marketplace, restaurant, ecommerce,
taskrabbit) removing ~625 lines of dead code. Add require_str/require_param
helpers to tool.rs and refactor ~30 call sites across 10 tool files from
4-6 line inline extractions to single-line calls. Consolidate worker HTTP
client with get_json/post_json helpers, reducing boilerplate in 4 methods.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: return JSON from orchestrator /complete endpoint

The report_complete handler returned bare StatusCode::OK (no body),
which broke the post_json helper that expects a JSON response.
Return {"status": "ok"} for consistency with other worker endpoints.

Addresses review feedback on PR nearai#98.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
bkutasi pushed a commit to bkutasi/ironclaw that referenced this pull request Mar 28, 2026
* refactor: deduplicate tool parameter extraction and remove dead stub tools

Delete 4 never-registered stub tools (marketplace, restaurant, ecommerce,
taskrabbit) removing ~625 lines of dead code. Add require_str/require_param
helpers to tool.rs and refactor ~30 call sites across 10 tool files from
4-6 line inline extractions to single-line calls. Consolidate worker HTTP
client with get_json/post_json helpers, reducing boilerplate in 4 methods.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: return JSON from orchestrator /complete endpoint

The report_complete handler returned bare StatusCode::OK (no body),
which broke the post_json helper that expects a JSON response.
Return {"status": "ok"} for consistency with other worker endpoints.

Addresses review feedback on PR nearai#98.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants