Extend the contract gate to composio_execute, MCP registry, and workflows - #4861
Extend the contract gate to composio_execute, MCP registry, and workflows#4861yh928 wants to merge 5 commits into
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds ChangesContract gate
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Model
participant ContractGateMiddleware
participant ContractResolver
participant LateBoundTool
Model->>ContractGateMiddleware: issue gated tool call
ContractGateMiddleware->>ContractResolver: fetch full contract
ContractResolver-->>ContractGateMiddleware: return contract or lookup result
ContractGateMiddleware-->>Model: return contract in tool error
Model->>ContractGateMiddleware: retry call with marker present
ContractGateMiddleware->>LateBoundTool: execute tool
LateBoundTool-->>Model: return tool result
Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6236939f1f
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/openhuman/tinyagents/contract_gate.rs (1)
174-184: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick winAvoid bulk allocation of tool message texts.
Collecting
m.text()for all tool messages into an intermediateVec<String>forces a peak memory allocation proportional to the total size of all tool messages in the transcript (which can reach megabytes).You can avoid this O(N) memory spike by streaming the iterator directly into
collect_present_keysusingAsRef<str>, evaluating and dropping each message's text one at a time.♻️ Proposed refactor to stream texts without an intermediate `Vec`
Update
refresh_presentto remove the.collect():- fn refresh_present(&self, messages: &[TaMessage]) { - let tool_texts: Vec<String> = messages - .iter() - .filter(|m| matches!(m, TaMessage::Tool(_))) - .map(|m| m.text()) - .collect(); - let keys = collect_present_keys(tool_texts.iter().map(String::as_str)); - if let Ok(mut set) = self.present.lock() { - *set = keys; - } - } + fn refresh_present(&self, messages: &[TaMessage]) { + let keys = collect_present_keys( + messages + .iter() + .filter(|m| matches!(m, TaMessage::Tool(_))) + .map(|m| m.text()), + ); + if let Ok(mut set) = self.present.lock() { + *set = keys; + } + }Then generalize the helper's signature further down the file so it accepts the owned
Stringiterator:-fn collect_present_keys<'a>(texts: impl Iterator<Item = &'a str>) -> HashSet<String> { +fn collect_present_keys(texts: impl Iterator<Item = impl AsRef<str>>) -> HashSet<String> { let mut set = HashSet::new(); for text in texts { - if let Some(rest) = text.strip_prefix(MARKER_OPEN) { + if let Some(rest) = text.as_ref().strip_prefix(MARKER_OPEN) { if let Some(j) = rest.find(']') { set.insert(rest[..j].to_string()); } } } set }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/openhuman/tinyagents/contract_gate.rs` around lines 174 - 184, Update refresh_present to pass the filtered tool-message text iterator directly to collect_present_keys, removing the intermediate Vec<String> allocation. Generalize collect_present_keys to accept an iterator of items implementing AsRef<str>, while preserving key collection behavior and allowing each generated text value to be dropped after processing.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/openhuman/tinyagents/contract_gate.rs`:
- Around line 478-480: Add a verbose, grep-friendly diagnostic log in the
ContractResult::Unavailable arm of the fetch_contract match before delegating to
next.run(ctx, state, call). Include enough context to identify the target and
transient contract-fetch failure, while preserving the existing execution flow.
---
Nitpick comments:
In `@src/openhuman/tinyagents/contract_gate.rs`:
- Around line 174-184: Update refresh_present to pass the filtered tool-message
text iterator directly to collect_present_keys, removing the intermediate
Vec<String> allocation. Generalize collect_present_keys to accept an iterator of
items implementing AsRef<str>, while preserving key collection behavior and
allowing each generated text value to be dropped after processing.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: a6d9c2ae-35b5-4406-854f-586e018ae5f7
📒 Files selected for processing (3)
src/openhuman/tinyagents/contract_gate.rssrc/openhuman/tinyagents/contract_gate_tests.rssrc/openhuman/tinyagents/mod.rs
6236939 to
62d3b81
Compare
62d3b81 to
fad71b4
Compare
fad71b4 to
ef89c16
Compare
YellowSnnowmann
left a comment
There was a problem hiding this comment.
Solid, well-engineered change — the design directly addresses #4853, and deriving contract-presence from a transcript marker (rather than mutable turn state) is genuinely nice: it's correct-by-construction across summarization, microcompact, hard-trim, and durable sub-agent resume, which is strictly better than the "reset seen-state on compaction" the issue asked for. The anti-spoof prefix check and the "all same-batch calls gated" reasoning are right and are tested.
I'm requesting changes on verification gaps, not on the design. Four things I'd want resolved before approving:
1. The core behavior is untested. All 22 tests are pure logic. The network resolvers and — more importantly — the real harness wiring (before_model refreshes presence, then wrap_tool gates, in the actual middleware order from assemble_turn_harness) have no integration coverage. The gated_call_delivers_the_contract_then_the_retry_passes test manually simulates the rescan; it doesn't prove the middleware ordering actually produces that sequence. The top acceptance criterion — "repro gone" — has no automated proof. Please add at least one integration test exercising deliver → retry through assemble_turn_harness.
2. The inventory-count assertions may not run. The updated 16 / 5 counts in tests.rs sit under a comment stating they're "NOT compiled by cargo check --lib … verify/adjust the exact numbers when the test suite actually compiles." Please confirm that block actually executes in CI (link a passing run), or the counts are unverified.
3. UPPER_SNAKE false-positive is a hard-failure mode. looks_like_composio_slug gates on name shape alone. Any non-Composio tool ever named in UPPER_SNAKE_CASE would resolve to Composio(name) → toolkit_from_slug returns None → NotFound → the tool never executes and the model gets a misleading "not a valid Composio action slug" message. This relies on an unenforced lowercase-tool convention. Please add a guard (e.g. check the name against the actual registered Composio action set) or at minimum a comment documenting the invariant.
4. Contracts are delivered as tool errors — confirm they don't trip an error breaker. The NotFound path's own comment references "the repeated-tool-failure breaker." Since Found deliveries are also TaToolResult::error, a turn touching several gated targets emits multiple errors before any retry lands. Please confirm the Found deliveries don't count toward that breaker (or exempt them).
None of these are design objections — CI green + CodeRabbit clean is necessary but doesn't cover any of the above. Happy to re-review once addressed.
🤖 Council Merge Gate — APPROVE (merge blocked by branch protection)The model council review gate returned APPROVE (session However, Action needed (human): resolve the failing required checks / obtain the required review, then merge. A maintainer with admin rights may also merge explicitly. The PR is left OPEN. Posted automatically by the |
🤖 Council Merge Gate — APPROVE (merge blocked by branch protection)The model council review gate returned APPROVE (session However, Action needed (human): resolve the failing required checks / obtain the required review, then merge. A maintainer with admin rights may also merge explicitly. The PR is left OPEN. Posted automatically by the |
Review notes on #4861 (contract gate for #4853) — non-blocking commentReviewed the diff and read Does it address the root cause?Mechanically, yes. The gate short-circuits the first call per late-bound target each turn and hands back the full contract as a tool error, so the retry runs with the real schema in context — the same lever the issue's own diagnostic ("tell it to read the contract first") confirmed works. Verified:
Gaps / risks worth weighing
Test qualityGood — 22 pure-logic tests with real assertions: target extraction for every kind (+ missing-arg pass-through), the core deliver→retry transition, same-batch gating, marker presence across resume/compaction/anti-spoof, the Composio/MCP resolvers (found + not-found + valid-list + empty-server), list capping, and contract rendering. One caveat: the async Bottom lineSolid engineering and a faithful implementation of the issue's chosen design; merge-worthy on mechanism and quality. It does not, on its own, prove the repro is eliminated — that hinges on a live Gmail run and on Composio's real schema carrying the quoting guidance. Suggest a live smoke of the Gmail search repro before/at merge, and noting the deferred |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 69d383f403
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| match crate::openhuman::skills::registry::get_workflow(&config.workspace_dir, id) { | ||
| Some(def) => ContractResult::Found(render_workflow_contract(id, &def)), |
There was a problem hiding this comment.
Honor workflow profile allowlists before returning contracts
When an active agent profile scopes workflows, list_workflows, describe_workflow, and RunWorkflowTool all enforce skill_allowlist, but this new lookup goes straight to the global registry. A model that guesses a disallowed workflow_id will receive that workflow's input contract before the real run_workflow tool gets a chance to reject it, leaking metadata that the profile intentionally hides; thread the active allowlist into the gate or let the underlying tool return its scoped error.
Useful? React with 👍 / 👎.
| Ok(MiddlewareToolOutcome::Result(TaToolResult::error( | ||
| call.id, call.name, body, | ||
| ))) |
There was a problem hiding this comment.
Preserve contract bodies through tool-output rewriting
When a fetched contract is large enough to hit the existing after-tool output pipeline, returning it as an ordinary composio_execute/mcp_registry_tool_call/run_workflow error lets payload summarization, TokenJuice compaction, or the 16 KiB result cap rewrite or truncate the schema. Because the gate marker is part of that same body, the next turn can either treat a partial contract as present and execute with an incomplete schema, or lose the marker and repeatedly re-deliver; contract-gate results need an exemption or a marker/body format that survives those stages.
Useful? React with 👍 / 👎.
Codex flagged (tinyhumansai#4861 review) that a delivered contract, returned as an ordinary tool error, flows through the same output pipeline as any tool result — payload summarization, TokenJuice compaction, the 16 KiB result cap. Any of those can rewrite or truncate the body, and because the `[contract-gate:]` presence marker rides in that same body, the next turn could treat a partial contract as "present" and execute with an incomplete schema (or lose the marker and re-deliver forever). Rather than exempt deliveries from that pipeline, embed a fingerprint: the marker now carries an XXH3-64 digest of the exact bytes that follow it — `[contract-gate:<digest>:<key>[,<key>...]]`. On rescan `before_model` re-hashes those bytes and credits the key(s) ONLY when the digest still matches. A summarized / capped / rewritten body fails the check, so the gate re-delivers the full contract (fail-safe) instead of trusting a mutated one. XXH3 is fast and stable across processes (a resumed sub-agent's byte-identical history still verifies); non-cryptographic — it guards accidental mutation, not a forged collision. - new `xxhash-rust` dep (xxh3 feature) - `payload_digest` / `build_marker` assemble+verify the digest; the marker-to-body concatenation is owned by `deliver_body` and `prefix_with_present_marker` so the hashed bytes always equal the post-marker bytes - `present_marker(keys) -> Option<String>` becomes `prefix_with_present_marker(keys, body) -> String`; the three discovery pre-crediting call sites updated (mcp helper now returns keys, not a marker) - `collect_present_keys` parses `<digest>:<keys>` and drops the keys unless the recomputed digest matches - tests rebuilt on the real delivery path, plus new coverage: a truncated body and a rewritten body both fail the digest gate, and a digest-less marker is ignored Addresses the "preserve contract bodies through tool-output rewriting" review point on tinyhumansai#4861. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012BLvp9tiJ2bFUrcY821FY1
…contract leak) Codex flagged (tinyhumansai#4861 review) that the gate's `fetch_workflow` resolved a workflow contract straight from the global registry, while the real `run_workflow` / `describe_workflow` tools enforce the active profile's `skill_allowlist`. A model guessing a scoped-out `workflow_id` would receive that workflow's input contract from the gate — leaking metadata the profile hides. Thread the allowlist to the gate the same identity-from-the-tool way `composio_actions` already flows: `RunWorkflowTool` self-reports its `skill_allowlist` via a new `Tool::workflow_gate_allowlist` trait method, and `assemble_turn_harness` collects it into `ContractGateMiddleware`. `decide` then passes a scoped-out workflow straight through (no contract rendered) so the real tool owns the canonical "not available to the active agent profile" rejection — the gate never reveals such a workflow exists. Checked the other two gated surfaces: Composio (`composio_integrations`) and MCP-registry (`allowed_mcp_servers`) have NO equivalent leak, because their executing tools do not enforce that per-profile scope in the first place (the gate mirrors the absent enforcement, exposing nothing the model couldn't already reach by calling the tool). Only workflows enforce an allowlist the gate bypassed. - new default-None `Tool::workflow_gate_allowlist`; `RunWorkflowTool` overrides it - gate stores `workflow_allowlist`, checks it in `decide` via `workflow_allowed` - tests: scoped-out workflow not gated; allowed workflow still gated; a None allowlist gates every workflow Addresses the "honor workflow profile allowlists before returning contracts" review point on tinyhumansai#4861. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012BLvp9tiJ2bFUrcY821FY1
Reviewer tinyhumansai#1 on tinyhumansai#4861 asked for a test through the real `assemble_turn_harness` middleware ordering (not just unit-level `decide()`), exercising the whole deliver→retry so the transcript-derived presence check is validated end to end. - cfg(test)-only contract-source seam (`test_support::InjectedContracts`): `fetch_contract` first consults an injected map keyed by `GateTarget::key()`, so an integration test resolves a deterministic contract without reaching a live Composio / workflow / MCP source. The middleware is built inside `assemble_turn_harness`, so the test never holds it — the seam is global, serial-locked, and RAII-cleared. String-valued (delivered as `ContractResult::Found`) so the private `ContractResult` keeps its visibility. The whole module is `#[cfg(test)]` and absent from production builds. - integration test in `tinyagents::tests`: a mock provider issues the same gated `composio_execute` call twice (gated first attempt, then retry) with a recording stub tool, driven through `run_turn_via_tinyagents_shared` → `assemble_turn_harness`. The single assertion `executed == 1` proves BOTH halves: the gate blocked the first attempt (else 2) AND the delivered contract round-tripped through `before_model` so the retry passed (else 0, looping). Also asserts the delivery precedes the execution in the transcript. `openhuman::tinyagents` suite green (152, incl. the `adapter_inventory` middleware-count assertions — the seam adds no middleware); fmt clean. Addresses reviewer integration-test point tinyhumansai#1 on tinyhumansai#4861. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SRSNnqQsokuGmkbpLoLCGy
Codex flagged (tinyhumansai#4861 review) that a delivered contract, returned as an ordinary tool error, flows through the same output pipeline as any tool result — payload summarization, TokenJuice compaction, the 16 KiB result cap. Any of those can rewrite or truncate the body, and because the `[contract-gate:]` presence marker rides in that same body, the next turn could treat a partial contract as "present" and execute with an incomplete schema (or lose the marker and re-deliver forever). Rather than exempt deliveries from that pipeline, embed a fingerprint: the marker now carries an XXH3-64 digest of the exact bytes that follow it — `[contract-gate:<digest>:<key>[,<key>...]]`. On rescan `before_model` re-hashes those bytes and credits the key(s) ONLY when the digest still matches. A summarized / capped / rewritten body fails the check, so the gate re-delivers the full contract (fail-safe) instead of trusting a mutated one. XXH3 is fast and stable across processes (a resumed sub-agent's byte-identical history still verifies); non-cryptographic — it guards accidental mutation, not a forged collision. - new `xxhash-rust` dep (xxh3 feature) - `payload_digest` / `build_marker` assemble+verify the digest; the marker-to-body concatenation is owned by `deliver_body` and `prefix_with_present_marker` so the hashed bytes always equal the post-marker bytes - `present_marker(keys) -> Option<String>` becomes `prefix_with_present_marker(keys, body) -> String`; the three discovery pre-crediting call sites updated (mcp helper now returns keys, not a marker) - `collect_present_keys` parses `<digest>:<keys>` and drops the keys unless the recomputed digest matches - tests rebuilt on the real delivery path, plus new coverage: a truncated body and a rewritten body both fail the digest gate, and a digest-less marker is ignored Addresses the "preserve contract bodies through tool-output rewriting" review point on tinyhumansai#4861. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012BLvp9tiJ2bFUrcY821FY1
98515dd to
51aec00
Compare
…contract leak) Codex flagged (tinyhumansai#4861 review) that the gate's `fetch_workflow` resolved a workflow contract straight from the global registry, while the real `run_workflow` / `describe_workflow` tools enforce the active profile's `skill_allowlist`. A model guessing a scoped-out `workflow_id` would receive that workflow's input contract from the gate — leaking metadata the profile hides. Thread the allowlist to the gate the same identity-from-the-tool way `composio_actions` already flows: `RunWorkflowTool` self-reports its `skill_allowlist` via a new `Tool::workflow_gate_allowlist` trait method, and `assemble_turn_harness` collects it into `ContractGateMiddleware`. `decide` then passes a scoped-out workflow straight through (no contract rendered) so the real tool owns the canonical "not available to the active agent profile" rejection — the gate never reveals such a workflow exists. Checked the other two gated surfaces: Composio (`composio_integrations`) and MCP-registry (`allowed_mcp_servers`) have NO equivalent leak, because their executing tools do not enforce that per-profile scope in the first place (the gate mirrors the absent enforcement, exposing nothing the model couldn't already reach by calling the tool). Only workflows enforce an allowlist the gate bypassed. - new default-None `Tool::workflow_gate_allowlist`; `RunWorkflowTool` overrides it - gate stores `workflow_allowlist`, checks it in `decide` via `workflow_allowed` - tests: scoped-out workflow not gated; allowed workflow still gated; a None allowlist gates every workflow Addresses the "honor workflow profile allowlists before returning contracts" review point on tinyhumansai#4861. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012BLvp9tiJ2bFUrcY821FY1
Reviewer tinyhumansai#1 on tinyhumansai#4861 asked for a test through the real `assemble_turn_harness` middleware ordering (not just unit-level `decide()`), exercising the whole deliver→retry so the transcript-derived presence check is validated end to end. - cfg(test)-only contract-source seam (`test_support::InjectedContracts`): `fetch_contract` first consults an injected map keyed by `GateTarget::key()`, so an integration test resolves a deterministic contract without reaching a live Composio / workflow / MCP source. The middleware is built inside `assemble_turn_harness`, so the test never holds it — the seam is global, serial-locked, and RAII-cleared. String-valued (delivered as `ContractResult::Found`) so the private `ContractResult` keeps its visibility. The whole module is `#[cfg(test)]` and absent from production builds. - integration test in `tinyagents::tests`: a mock provider issues the same gated `composio_execute` call twice (gated first attempt, then retry) with a recording stub tool, driven through `run_turn_via_tinyagents_shared` → `assemble_turn_harness`. The single assertion `executed == 1` proves BOTH halves: the gate blocked the first attempt (else 2) AND the delivered contract round-tripped through `before_model` so the retry passed (else 0, looping). Also asserts the delivery precedes the execution in the transcript. `openhuman::tinyagents` suite green (152, incl. the `adapter_inventory` middleware-count assertions — the seam adds no middleware); fmt clean. Addresses reviewer integration-test point tinyhumansai#1 on tinyhumansai#4861. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SRSNnqQsokuGmkbpLoLCGy
3f65295 to
09d6d31
Compare
|
| Filename | Overview |
|---|---|
| src/openhuman/tools/contract_gate.rs | Core of the PR: relocates and generalizes the contract gate to cover Composio, MCP registry, and workflow surfaces with namespaced keys, transcript-hash presence verification, validate-then-pass, and per-turn auto-proceed safety net. Logic is sound; see style note on colon-containing MCP key ambiguity. |
| src/openhuman/tools/contract_gate_tests.rs | Comprehensive test suite: 9 pre-existing Composio tests preserved, plus new coverage for MCP resolution, workflow surfacing/validate-then-pass/no-config, key namespacing, per-kind retry hints, injected-arg scoping, and transcript presence mechanics. |
| src/openhuman/mcp_registry/tools.rs | Wires the gate into McpRegistryToolCallTool and adds pre-crediting in McpRegistryListToolsTool. McpRegistryConnectTool does not pre-credit its tool-schema response (validate-then-pass mitigates UX impact but the optimization is missing). |
| src/openhuman/agent/tools/run_workflow.rs | Adds the contract gate to RunWorkflowTool, correctly positioned after the profile-allowlist check so scoped-out workflows are never gated. Config is threaded in so the gate can resolve the workflow's inputs block. |
| src/openhuman/composio/tools.rs | Adds the gate to ComposioExecuteTool using live_config (post-reload), and correctly skips pre-crediting in the prefer_markdown path of composio_list_tools since schemas are dropped there. |
| src/openhuman/skills/tools.rs | WorkflowDescribeTool now calls prefix_with_present_marker with the workflow key, correctly pre-crediting the contract so a describe_workflow → run_workflow sequence skips redundant re-delivery. |
| src/openhuman/composio/action_tool.rs | Import path updated from composio::contract_gate to tools::contract_gate. No logic change — pure path update. |
| .github/workflows/ci-lite.yml | Adds tools::contract_gate:: to the gates-off smoke lane test filter and acknowledges contract_gate_tests.rs in the feature-gated module sentinel. Both changes correctly track the new module location. |
| Cargo.toml | Adds xxhash-rust v0.8.18 as a new dependency for XXH3-64 payload hashing in the transcript presence system. |
| Cargo.lock | Lock file updated to pin xxhash-rust v0.8.18; no other dependency changes. |
Sequence Diagram
sequenceDiagram
participant M as Model
participant T as Tool (composio_execute / mcp_registry_tool_call / run_workflow)
participant G as ContractGate (consult)
participant P as is_present (RUN_PRESENCE)
participant C as lookup_contract
participant V as args_satisfy_contract
M->>T: first call (args may be guessed)
T->>G: consult(gate, config, target, args)
G->>P: is_present(key)?
P-->>G: false (transcript not yet credited)
G->>G: gate_consult(key) → FirstTime
G->>C: lookup_contract(config, target)
C-->>G: Some(contract)
G->>V: args_satisfy_contract(args, contract)?
alt args conform (validate-then-pass)
V-->>G: true
G-->>T: Proceed
T-->>M: tool result
else args don't conform
V-->>G: false
G->>G: deliver_body + record_delivered (hash in DELIVERED)
G-->>T: Surface(contract_text)
T-->>M: error(contract_text)
M->>T: retry with corrected args
T->>G: consult(gate, config, target, corrected_args)
G->>P: is_present(key)?
note over P: refresh_present() scanned transcript,<br/>found matching hash → present
P-->>G: true
G-->>T: Proceed
T-->>M: tool result
end
Reviews (5): Last reviewed commit: "contract gate: add a FAILING test for th..." | Re-trigger Greptile
| async fn fresh_gates_eventually_auto_proceed() { | ||
| // Regression for #5119: when the main agent re-delegates to a fresh | ||
| // integrations_agent sub-agent, each spawn creates a new ComposioActionTool | ||
| // with a fresh ContractGate. Without a process-wide safety net, every fresh | ||
| // gate surfaces the contract again and the action never executes, causing an | ||
| // infinite loop (51x surfacing in the reported log). | ||
| // | ||
| // The safety net tracks how many *fresh gate instances* have been consulted | ||
| // per slug. After the threshold (3+) of fresh instances have all seen the | ||
| // slug as "first time" and surfaced the contract, the next instance | ||
| // auto-proceeds — the model has clearly seen the schema and doesn't need it | ||
| // surfaced again. | ||
| let toolkit = "autopkit"; | ||
| let slug = "AUTOPKIT_FETCH_EMAILS"; | ||
| seed_live_catalog_cache(toolkit, vec![full_contract(slug, toolkit)]); | ||
|
|
||
| let config = Config::default(); | ||
|
|
||
| // Gates 1-3 each surface the contract (fresh instance, first-time consult). | ||
| for i in 1..=3 { | ||
| let gate = ContractGate::new(); | ||
| let decision = consult_composio(&gate, &config, slug, &guessing_args()).await; | ||
| assert!( | ||
| matches!(decision, GateDecision::Surface(_)), | ||
| "fresh gate {i} must surface the contract (count {i})" | ||
| ); | ||
| } | ||
|
|
||
| // Gate 4: auto-proceeds because 3+ fresh instances have already surfaced | ||
| // this contract without any of them executing. | ||
| let gate4 = ContractGate::new(); | ||
| let decision = consult_composio(&gate4, &config, slug, &guessing_args()).await; | ||
| assert!( | ||
| matches!(decision, GateDecision::Proceed), | ||
| "gate 4 must auto-proceed after 3+ fresh instances surfaced the same slug" | ||
| ); | ||
| } |
There was a problem hiding this comment.
fresh_gates_eventually_auto_proceed is non-idempotent within a single test binary
The test relies on GLOBAL_FIRST_CONSULT_COUNT starting at zero for AUTOPKIT_FETCH_EMAILS. If the test is re-run in the same process (e.g., via nextest's retry flag), the second run starts with a count of 3, gate 1 immediately auto-proceeds, and the assertion "fresh gate 1 must surface the contract" fails. This is a direct consequence of the process-lifetime counter issue but worth noting explicitly for test reliability.
The per-action gate (tinyhumansai#4853 / tinyhumansai#4995) lives in `composio/contract_gate.rs`, but its own module doc already flags the next step: generalising it to the `composio_execute` dispatcher, the MCP bridges, and the Workflow dispatcher. Those domains cannot reach into `composio::` without inverting the layering, so move the module to `openhuman/tools/contract_gate.rs` — the aggregation layer that already depends on every domain — before extending it. Pure relocation: no behaviour change, no logic edited. Only the module declaration, the `ComposioActionTool` import path, and two doc links move. Issue tinyhumansai#4853
The gate merged in tinyhumansai#4995 (and hardened in tinyhumansai#5119/tinyhumansai#5132/tinyhumansai#5154) fronts exactly one late-bound surface: the per-action `ComposioActionTool`. Three more share its shape — the model is handed an envelope whose real contract lives behind a separate discovery tool it usually has not called: composio_execute {tool, arguments} -> live toolkit catalog mcp_registry_tool_call {server_id, tool_name, args} -> server input_schema run_workflow {workflow_id, inputs} -> declared [[inputs]] On each of those the model composes the call before the schema is in context and guesses — the same failure tinyhumansai#4853 opened on, one layer up. Extend the existing gate rather than adding a second mechanism. `GateTarget` names what to resolve a contract for; a neutral `GatedContract` normalises the three sources so the merged `args_satisfy_contract` validator and one formatter serve them all. Every property the gate already had applies unchanged to the new kinds: surface-once per instance, validate-then-pass (tinyhumansai#5119) so a well-formed first call executes directly, and the process-wide auto-proceed safety net (tinyhumansai#5119) so re-delegation cannot loop. Kind-specific details: - Keys are namespaced per kind, so a workflow and an MCP tool sharing a name never credit each other's gate. Composio slugs stay case-folded; MCP pairs and workflow ids address exact identities and are not. - The `connection_id` exemption is Composio's alone — nothing injects extra keys onto an MCP or workflow call, so an unknown key there is a real guess. - Each surfaced contract names the call to re-issue. "Call the action again" tells a model nothing after a blocked `run_workflow`. - A workflow's `[[inputs]]` block is synthesised into a JSON Schema so the shared validator treats it like any other target. - `run_workflow` consults the gate AFTER its profile-allowlist check, so a scoped-out workflow's contract is never rendered. - Every lookup sits behind its domain's Cargo feature; a compiled-out domain yields no contract and the gate proceeds. The kind-agnostic half (target identity, validation, rendering) compiles in every configuration, and its tests run in the gates-off smoke lane. Verified: contract_gate 16/16 (3 of them also under --no-default-features), composio 367, mcp_registry 150, tools 807, skills 187, run_workflow 11; default clippy --all-features clean; gates-off check + scoped gates-off test run clean; fmt clean. Issue tinyhumansai#4853
…paction The gate tracked "already surfaced" as tool state. That answer is only useful while the contract is still in front of the model, and it often is not: summarisation, microcompact tool-body blanking, the hard trim, and result-size caps can each drop or rewrite a delivered contract mid-turn. The gate would keep counting it as seen, and the model would call the tool against a schema it can no longer read — the exact failure the gate exists to prevent. Derive presence from the transcript instead. Each delivery leads with a `[contract-gate:<slug list>]` marker and the hash of its payload is recorded; before every model call the tool messages are rescanned, each marker's payload re-hashed, and its slugs credited only on an exact match. Correct across every context-management path by construction: a contract that survives byte-for-byte (including in a resumed sub-agent's history) still hashes equal and is not re-delivered; one summarised, blanked, truncated, or whitespace-collapsed no longer does, so it is re-delivered once. Recognition is a fixed-prefix compare at byte 0, not a substring search — one `strip_prefix` per tool message, and a model echoing the marker mid-text cannot spoof presence. Only tool-role messages are scanned. The marker carries slugs only, so it stays short and whitespace-free while the integrity check rides on the payload hash. **A hash miss skips that message; it never rejects the target.** The scan accumulates across the whole transcript, so a lookalike — a tool echoing the syntax, a stale copy since rewritten — contributes nothing and a genuine copy elsewhere still credits. Short-circuiting on the first miss would let one lookalike make a contract permanently un-creditable and re-deliver it on every call, forever. Two supporting pieces this needs to hold: - **Deliveries reach the transcript verbatim.** `HandoffMiddleware` (sub-agents) collapses whitespace runs to one space, which flattens the delivered schema's indentation, and `ToolOutputMiddleware` summarises/tabulates/truncates. Either makes the re-hash miss. Both now skip a delivery. The check is the hash, not the bare marker, so a coincidental lookalike is still cleaned normally. - **Discovery pre-crediting.** `describe_workflow`, `mcp_registry_list_tools`, and `composio_list_tools` (full-JSON rendering only — never the thin `prefer_markdown` listing, which would credit a contract the model never saw) lead their output with one marker packing every target they fully described, so a model that already read the real schema pays no re-delivery. Also addresses the review finding on the auto-proceed counter: it was a process-lifetime global, so once a slug crossed the threshold in any turn, every later turn auto-proceeded for it permanently — and in a multi-tenant process one user's count suppressed the gate for everyone. It is now scoped to the top-level turn (sub-agent runs deliberately inherit rather than re-scope, since a fresh count per spawn is the very loop the net detects). That also makes the auto-proceed test idempotent within one test binary. Verified: contract_gate 20/20 (7 of them also under --no-default-features), tinyagents 126, mcp_registry 150, skills 187, run_workflow 11; clippy --all-features clean; gates-off check + scoped gates-off run clean; fmt clean; feature-gate-smoke allowlist unchanged. Issue tinyhumansai#4853
A presence check that answers "absent" costs a re-delivery, so the mechanism is only safe if it is guaranteed to eventually answer "present". Two properties give that, and both were load-bearing but only one was written down. 1. A hash miss skips that message; it never rejects the target. Already documented and covered. 2. **Delivery overwrites the recorded hash.** This was only an implicit consequence of `HashMap::insert`. Every delivery records the hash of the exact bytes it emits, so the newest delivery is always creditable: the next rescan finds it intact, credits the slug, and the retry runs. Termination does not depend on any earlier copy surviving. Keeping the first hash instead would be the bug — a second delivery whose contract text differs (the provider republished the schema, or a discovery listing recorded a different rendering first) would hash to a value never recorded, could never be credited, and the gate would re-deliver it on every call forever. The cost of overwriting is that a superseded copy still in the transcript stops matching, which is harmless: the fresh delivery beside it credits instead. Documents both on `record_delivered` and together in the module doc, and pins the overwrite with `a_re_delivery_is_immediately_creditable_even_when_the_ contract_changed`. No behaviour change. contract_gate 21/21 (8 gates-off); clippy + fmt clean. Issue tinyhumansai#4853
This test is red on purpose. It marks a gap the rest of this PR does not
close, so a green CI cannot imply the feature works on every path.
The gate credits a delivered contract by finding its `[contract-gate:…]`
marker at byte 0 of a tool message and re-hashing the payload after it.
`to_provider_messages` breaks both halves: it wraps each result in
`<tool_result id="…">` and prefixes the batch with a `[Tool results]`
banner, emitted as a *user* message:
"[Tool results]\n<tool_result id=\"call-1\">\n[contract-gate:…]\n\n…"
The session and sub-agent paths build their history through that renderer
(`session/turn/core.rs:1199`, `subagent_runner/ops/graph.rs:388`), so
presence is never credited there. The gate itself still behaves — the
per-instance seen-set and the auto-proceed net bound it, so there is no
loop and no regression against upstream — but the compaction safety added
in `fc41b064` is inert on those paths: a contract summarised out of
context is not re-delivered, because it was never counted as present.
The fix is a `trusted_verbatim` flag carried on the result, with a flagged
delivery emitted as its own unframed turn. The flag has to survive
`tinyagents::harness::message::ToolMessage`, which today carries only
`tool_call_id` + `content`, so it needs an upstream crate change first.
That work is in flight; this test turns green when the flag lands.
Verified failing for the stated reason, not by accident — the assertion
prints the actual rendered framing.
Issue tinyhumansai#4853
8ef5eb1 to
aa16823
Compare
Summary
The contract gate merged in #4995 (hardened in #5119 / #5132 / #5154) fronts exactly one late-bound surface: the per-action
ComposioActionTool. Three more surfaces have the same shape — the model is handed an envelope whose real contract lives behind a separate discovery tool it usually has not called:composio_execute{tool, arguments}—argumentsis a bare objectmcp_registry_tool_call{server_id, tool_name, arguments}input_schemarun_workflow{workflow_id, inputs}[[inputs]]On each, the model composes the call before the schema is in context and guesses — the same failure #4853 was opened on, one layer up.
This PR extends the existing gate to those three, rather than introducing a second mechanism.
What changed
Commit 1 — pure relocation.
composio/contract_gate.rs→tools/contract_gate.rs.mcp_registry/andagent/tools/cannot reach intocomposio::without inverting the layering, and the module's own doc already flagged generalisation as the next step. No logic edited; renders as a rename.Commit 2 — the extension.
GateTargetnames what to resolve a contract for; a neutralGatedContractnormalises the three sources so the mergedargs_satisfy_contractvalidator and a single formatter serve all of them.Every property the gate already had carries over unchanged to the new kinds:
Kind-specific details worth review attention:
connection_idexemption is Composio's alone — nothing injects extra keys onto an MCP or workflow call, so an unknown key there is a real guess.run_workflow.[[inputs]]block is synthesised into a JSON Schema so the shared validator treats it like any other target.run_workflowconsults the gate after its profile-allowlist check, so a scoped-out workflow's contract is never rendered.History note
This PR previously proposed a separate
tinyagents::contract_gatemiddleware with transcript-derived presence, and removed #4995's tool-layer gate to avoid double-gating. That is fully withdrawn. #5132's validate-then-pass and #5154's auto-proceed net solve the loop and redundancy concerns that motivated it, so the merged design is now the one being extended — and none of #4995 / #5119 / #5132 / #5154 is reverted.Test plan
openhuman::tools::contract_gate— 16/16 (9 pre-existing Composio tests preserved; new coverage for MCP resolution, workflow surfacing/validate-then-pass/no-config, key namespacing, per-kind retry hints, injected-arg scoping)--no-default-features --features tokenjuice-treesitter(gates-off lane)cargo clippy --lib --all-featuresclean;cargo fmt --checkcleancargo checkcleanLocal validation
The full husky pre-push hook runs clean in this environment (
HOOK_EXIT=0):format:check->lint->compile->rust:clippy(root-D warningsandapp/src-tauri) ->lint:commands-tokens.An earlier revision of this description said the hook could not run here and that the
push used
--no-verify. That is no longer true and the claim is withdrawn: the missingpieces were environment-side (uninitialised Tauri submodules, absent
ripgrep, and theLinux GTK/WebKit dev packages CONTRIBUTING.md lists as prerequisites), and they have
since been installed.
Closes #5038