Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Record a refused REST answer in the contract

Guido Boykedbfb621b818e
diff --git a/spec/contracts/mcp_contract_recorder.rb b/spec/contracts/mcp_contract_recorder.rb
index b1a0474..e878264 100644
--- a/spec/contracts/mcp_contract_recorder.rb
+++ b/spec/contracts/mcp_contract_recorder.rb
@@ -465,9 +465,7 @@ module McpContract
'GET /api/v1/tickets/:id/attachments' => attachment_calls,
# REST R0 (cli_rest_plan D6, D7), appended last for the same reason: one
# answer per error code, then a write with an Idempotency-Key, its replay
- # and the key reused with another body. `refused` has no case: no
- # endpoint reaches an actor refusal deterministically (pinned with a
- # stubbed actor in spec/requests/api/v1/error_codes_spec.rb).
+ # and the key reused with another body.
'errors' => rest_error_calls,
'idempotency' => rest_idempotency_calls
}
@@ -483,10 +481,35 @@ module McpContract
'not_found' => rest_get(:read, "/api/v1/tickets/#{id(:missing)}"),
'invalid_input' => rest_send(:post, :write, login_posts, params: {}),
'invalid_input_validation' => rest_send(:post, :write, "/api/v1/projects/#{id(:website)}/tickets",
- params: { ticket: { title: '' } })
+ params: { ticket: { title: '' } }),
+ # Provoked: no endpoint reaches an actor refusal deterministically today
+ # (tickets have no lock_version). The answer is the real one on the wire,
+ # status, `error`, `code` and `reason` as the controller renders them.
+ 'refused' => refusing_ticket_writes do
+ rest_send(:patch, :write, "/api/v1/tickets/#{id(:login_ticket)}", params: { ticket: { status: 'new' } })
+ end
}
end
+ # While the block runs, Actors::SaveTicket answers the failure BaseActor
+ # builds for a lost optimistic lock, in the request's own locale.
+ module RefuseTicketWrites
+ def call(...)
+ return super unless Thread.current[:mcp_contract_refuse_ticket_writes]
+
+ Actors::BaseActor::Result.new(success: false, errors: [I18n.t('services.actors.stale_object')],
+ code: :stale_object)
+ end
+ end
+
+ def refusing_ticket_writes
+ Actors::SaveTicket.singleton_class.prepend(RefuseTicketWrites) unless Actors::SaveTicket.singleton_class < RefuseTicketWrites
+ Thread.current[:mcp_contract_refuse_ticket_writes] = true
+ yield
+ ensure
+ Thread.current[:mcp_contract_refuse_ticket_writes] = nil
+ end
+
def rest_idempotency_calls
# Spelled out, not a loop: each request from its own line, as everywhere
# here (Prosopite reads one call site asked three times as an N+1).
diff --git a/spec/fixtures/mcp_contract.json b/spec/fixtures/mcp_contract.json
index da00613..c46a8d5 100644
--- a/spec/fixtures/mcp_contract.json
+++ b/spec/fixtures/mcp_contract.json
@@ -1,9 +1,9 @@
{
"header": {
"regenerate": "RAILS_ENV=test bin/rails mcp:contract",
- "core_commit": "de411024f1483e7c7601855a7c9b46954dc9b7ec",
+ "core_commit": "5efde7ed1c730d059eb559e15fab95ef35ccf129",
"core_dirty": true,
- "contract_sha256": "605a4d53ca32608b71308f622a65f119e1ddbdaa8029849c802fa6a11754e39f"
+ "contract_sha256": "3fcc755d77e87d2762e975e0d111d8d5065f461277229f1a83d7bac370a672fb"
},
"contract": {
"mcp": {
@@ -3480,6 +3480,29 @@
]
}
}
+ },
+ "refused": {
+ "token": "write",
+ "request": {
+ "method": "PATCH",
+ "path": "/api/v1/tickets/00000000-0000-4000-8000-000000000041",
+ "params": {
+ "ticket": {
+ "status": "new"
+ }
+ }
+ },
+ "response": {
+ "status": 409,
+ "body": {
+ "error": "stale_object",
+ "code": "refused",
+ "reason": "stale_object",
+ "errors": [
+ "Der Datensatz wurde zwischenzeitlich von jemand anderem geändert. Bitte neu laden und erneut speichern."
+ ]
+ }
+ }
}
},
"idempotency": {