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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/reports-action-not-found.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

fix(mcp): return outcome not_found from reports_action on unknown id (#1325)
13 changes: 7 additions & 6 deletions crates/nostream-mcp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,9 @@ it stands after the call"
so REQ and COUNT withhold them while `nip56.hideActionableReports` is on) and `allow` clears them \
(`actionable: false`). The argument is the report's own kind-1984 event id — exactly 64 hex \
characters, not the reported event's id — and every report row that event wrote is flipped. \
Answers `reports` (the rows as they stand, by `id`) and `changed` (how many this call flipped). \
Every new REQ reflects the change at once; already-connected subscriptions see it at the relay's \
next refresh, up to 30 s"
Answers `reports` (the rows as they stand, by `id`) and `changed` (how many this call flipped), \
or `outcome: \"not_found\"` when no report carries that id. Every new REQ reflects the change at \
once; already-connected subscriptions see it at the relay's next refresh, up to 30 s"
)]
async fn reports_action(
&self,
Expand Down Expand Up @@ -918,8 +918,9 @@ maintenance worker runs in another process and its passes are not visible here"
/// The report payload: the rows one report event wrote, sorted by `id` because neither
/// backend orders that read, and how many this call flipped (R13).
///
/// An id with no rows is a result rather than an error — the answer `get_event` gives an
/// unknown id — so a caller that mistyped one sees an empty list and not `Not Found`.
/// An id with no rows answers `outcome: "not_found"` (like `delete_event`) so an agent that
/// passed the reported event's id or a mistyped id does not mistake a miss for a completed
/// triage call.
async fn act_on_report(&self, arguments: &ReportsActionArgs) -> Result<Value, McpError> {
let id = hex_prefix("id", &arguments.id)?;
if id.len() != 64 {
Expand All @@ -931,7 +932,7 @@ maintenance worker runs in another process and its passes are not visible here"
.map_err(|error| McpError::Criterion(error.to_string()))?;
let reports = &self.state.stores().reports;
if reports.find_by_event_id(&event_id).await?.is_empty() {
return Ok(json!({ "reports": [], "changed": 0 }));
return Ok(json!({ "outcome": OUTCOME_NOT_FOUND, "reports": [], "changed": 0 }));
}
let changed = reports
.set_actionable(&event_id, !matches!(arguments.action, ReportAction::Allow))
Expand Down
2 changes: 1 addition & 1 deletion crates/nostream-mcp/tests/fixtures/tools_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
"name": "relay_status"
},
{
"description": "Act on one report event: `hide` marks its targets hidden (`actionable: true`, so REQ and COUNT withhold them while `nip56.hideActionableReports` is on) and `allow` clears them (`actionable: false`). The argument is the report's own kind-1984 event id — exactly 64 hex characters, not the reported event's id — and every report row that event wrote is flipped. Answers `reports` (the rows as they stand, by `id`) and `changed` (how many this call flipped). Every new REQ reflects the change at once; already-connected subscriptions see it at the relay's next refresh, up to 30 s",
"description": "Act on one report event: `hide` marks its targets hidden (`actionable: true`, so REQ and COUNT withhold them while `nip56.hideActionableReports` is on) and `allow` clears them (`actionable: false`). The argument is the report's own kind-1984 event id — exactly 64 hex characters, not the reported event's id — and every report row that event wrote is flipped. Answers `reports` (the rows as they stand, by `id`) and `changed` (how many this call flipped), or `outcome: \"not_found\"` when no report carries that id. Every new REQ reflects the change at once; already-connected subscriptions see it at the relay's next refresh, up to 30 s",
"inputSchema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"additionalProperties": false,
Expand Down
39 changes: 34 additions & 5 deletions crates/nostream-mcp/tests/write_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! | `hiding_a_report_withholds_its_target_again` | `hide` puts it back |
//! | `a_second_allow_changes_nothing` | a value that already holds is not written and not counted |
//! | `every_row_of_one_report_event_moves` | one report event writes one row per target, and one call moves them all |
//! | `an_unknown_report_is_an_empty_answer` | an id with no rows answers an empty list and zero |
//! | `an_unknown_report_answers_not_found` | an id with no rows answers `outcome: "not_found"`, distinct from a flip that changed nothing |
//! | `a_report_id_that_is_not_an_exact_id_is_refused` | a short id is refused by the handler, after its dispatch line |
//! | `an_unknown_action_never_reaches_the_handler` | the closed set is the arguments type, so nothing is written |
//! | `an_allow_leaves_a_target_another_report_still_hides` | the action is one row's flag: another actionable report keeps the target hidden |
Expand Down Expand Up @@ -521,20 +521,49 @@ async fn every_row_of_one_report_event_moves() {
}

#[tokio::test]
async fn an_unknown_report_is_an_empty_answer() {
async fn an_unknown_report_answers_not_found() {
let fixture = TestMcp::start().await;
let record = event(34, 1, 1_699_999_973);
fixture.seed(std::slice::from_ref(&record)).await;
let known_report_id = EventId::from_byte_array([65; 32]).to_string();
seed_report(&fixture, [65; 32], &record).await;
let client = fixture.client().await;

let acted = client
let unchanged = client
.call(
TOOL_REPORTS_ACTION,
json!({ "id": known_report_id, "action": "hide" }),
)
.await;
assert_eq!(unchanged.is_error, Some(false), "{}", text(&unchanged));
let unchanged_answer = payload(&unchanged);
assert_eq!(unchanged_answer["changed"], 0, "{unchanged_answer}");
assert_eq!(
unchanged_answer["reports"].as_array().unwrap().len(),
1,
"{unchanged_answer}"
);

let unknown = client
.call(
TOOL_REPORTS_ACTION,
json!({ "id": "ab".repeat(32), "action": "hide" }),
)
.await;
assert_eq!(acted.is_error, Some(false), "{}", text(&acted));
let answer = payload(&acted);
assert_eq!(unknown.is_error, Some(false), "{}", text(&unknown));
let answer = payload(&unknown);
assert_eq!(answer["outcome"], OUTCOME_NOT_FOUND, "{answer}");
assert_eq!(answer["reports"], json!([]), "{answer}");
assert_eq!(answer["changed"], 0, "{answer}");
assert_ne!(
answer, unchanged_answer,
"an unknown id must differ from a flip that changed nothing"
);
assert_ne!(
answer.get("outcome"),
unchanged_answer.get("outcome"),
"the unknown id carries outcome: not_found while a no-op flip does not"
);
}

#[tokio::test]
Expand Down
8 changes: 4 additions & 4 deletions crates/nostreamd/tests/mcp_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,8 @@ async fn the_store_backed_tools_read_the_opened_store() {
assert!(outcome.status.success(), "{}", outcome.stderr);
}

/// The two write tools reach the opened store: a delete of an id it does not hold is answered
/// `not_found` by the store, rather than by an unwired bundle's error.
/// The two write tools reach the opened store: a miss on `delete_event` or `reports_action` is
/// answered `not_found` by the store, rather than by an unwired bundle's error.
#[tokio::test]
async fn the_write_tools_reach_the_opened_store() {
let prepared = Prepared::new();
Expand All @@ -567,8 +567,8 @@ async fn the_write_tools_reach_the_opened_store() {
assert_eq!(acted["result"]["isError"], false, "{acted}");
assert_eq!(
acted["result"]["structuredContent"],
json!({ "reports": [], "changed": 0 }),
"an id no report names flips nothing and answers the empty rows: {acted}"
json!({ "outcome": "not_found", "reports": [], "changed": 0 }),
"an id no report names flips nothing and answers not_found: {acted}"
);
assert!(outcome.status.success(), "{}", outcome.stderr);
}
Expand Down
Loading