1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! The HTTP approval rail's wire contract, in ONE place.
//!
//! greentic-designer `docs/superpowers/specs/2026-09-29-approval-rail-http-design.md`
//! §3 (and its §13 amendments). greentic-designer-admin implements the other
//! side of every type here; change nothing without changing that spec.
//!
//! Three doors, siblings of `/api/v1/ingest/worker-usage`, all authenticated
//! by the unit's `gtm_` worker-usage token (`Authorization: Bearer gtm_…`),
//! which must carry the `approvals` purpose. The admin takes tenant, env and
//! unit from the TOKEN, never from a body:
//!
//! | door | method | body / query | answers |
//! |---|---|---|---|
//! | `/api/v1/ingest/approval-request` | `POST` | [`ApprovalRequestBody`] | `201 {id}` new, `200 {id}` idempotent repeat, `409 approval_conflict`, `429 approval_pending_cap`, `400`/`413`, `404` (admin predates the door) |
//! | `/api/v1/ingest/approval-decision` | `GET` | `?correlation_id=<urlencoded>` | `204` pending, `200` [`ApprovalDecisionBody`], `410 approval_withdrawn` / `approval_expired`, `404` for every miss |
//! | `/api/v1/ingest/approval-withdraw` | `POST` | [`ApprovalWithdrawBody`] | `204` for a scoped hit or miss |
//!
//! Every request also carries [`APPROVAL_RAIL_HEADER`]` = `[`APPROVAL_RAIL_VERSION`]
//! and a `User-Agent` naming the same transport; the admin stamps
//! `worker_usage_tokens.approvals_seen_at` from it (spec §4.7).
//!
//! **No `decision_token` crosses this wire, in either direction.** The worker
//! mints, holds and verifies it; a decision fetched from the admin is resumed
//! with the worker's OWN held token injected (spec §3.2, §4.4, §10).
use ;
use ;
use Value;
/// Header identifying the transport on every request to the three doors.
pub const APPROVAL_RAIL_HEADER: &str = "X-Greentic-Approval-Rail";
/// Value of [`APPROVAL_RAIL_HEADER`]: the HTTP approval rail, version 1.
pub const APPROVAL_RAIL_VERSION: &str = "http/1";
/// `User-Agent` sent on every request to the three doors.
pub const APPROVAL_RAIL_USER_AGENT: &str = concat!;
/// Query parameter of the decision door.
pub const DECISION_QUERY_PARAM: &str = "correlation_id";
/// Admin error code for "this token already has 50 pending approvals" (`429`).
/// Unlike any other `429` it is NOT retried: another attempt cannot succeed
/// until a human resolves something.
pub const PENDING_CAP_CODE: &str = "approval_pending_cap";
/// `POST /api/v1/ingest/approval-request`.
///
/// ```json
/// {
/// "tenant_slug": "acme",
/// "correlation_id": "<bare hint>::pack=<p>::flow=<f>[::thread=…][::reply=…]::n=<32 hex>",
/// "request": { "target": "...", "operation": "...", "mode": "await",
/// "input": { ... }, "deadline_ms": 604800000 }
/// }
/// ```
///
/// Idempotent by construction: the admin upserts on `(tenant, correlation_id)`
/// and the id carries a per-dispatch nonce, so a retried send is a repeat.
/// `200` answer of `POST /api/v1/ingest/approval-request` (`201` or `200`).
/// Informational only: the runner keys everything by `correlation_id`.
/// A human's decision.
/// `200` answer of `GET /api/v1/ingest/approval-decision`, once resolved:
///
/// ```json
/// { "decision": "approved" | "denied", "resolved_by": "...", "note": "..." }
/// ```
///
/// Carries NO `decision_token` (the admin holds none for a poll row). An
/// unknown field is ignored so the admin can add one without breaking a
/// deployed runner.
/// Why the runner withdraws a pending approval.
/// `POST /api/v1/ingest/approval-withdraw`: marks the row `expired` only while
/// it is still `pending`. `204` whether or not it matched.
///
/// ```json
/// { "tenant_slug": "acme", "correlation_id": "…", "reason": "timeout" | "cancelled" | "poll_cap" }
/// ```
/// Build the request door's body. `mode` and `deadline_ms` are the EFFECTIVE
/// ones (the inbox applies a default deadline, spec §4.3).
pub