From 8ce2e1d3c4246e4177338de85e73c83fbe0db7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 18 Sep 2026 13:23:15 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(insight):=20size=20the=20rep?= =?UTF-8?q?ort=20deadline=20under=20the=20request=20envelope?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two verifier flags, one cause: the report door waits on its dispatch inside one HTTP request, and the request-timeout middleware (30 s) drops the handler future wherever it stands — mid-write included — answering its own 503 whose "the write may or may not have applied" prose is wrong for a door that knows nothing was stored, while a drop inside the store window stranded a `.report-.tmp`. `AI_INSIGHT_REPORT_TIMEOUT_SECS` drops 300 → 25, so the service loses the race on purpose and the door's own error path fires first: the honest "nothing was stored" 503, and no temp is ever opened. The constant's comment now states the relationship and that raising it needs the ceiling raised with it; the row reads before the dispatch share the same envelope, which is why the margin is not spent elsewhere. Pinned as a compile-time assertion beside the report contract tests: a raised deadline without a raised ceiling fails the build, proven by flipping the value to 30 (E0080, the message) and reverting. Local-Gate: c92220e9d9b886dc45b86b88c2ff7ecc6a54ce7a (1497 passed, 443s) --- src/ai/insight.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++-- src/constant.rs | 23 +++++++++----- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/src/ai/insight.rs b/src/ai/insight.rs index 08c840a..cee1d1a 100644 --- a/src/ai/insight.rs +++ b/src/ai/insight.rs @@ -296,8 +296,10 @@ pub async fn refresh( /// Ask for one school-level report document, awaited. The request carries /// every row the document needs, and the answer carries the rendered HTML -/// back; the deadline is [`AI_INSIGHT_REPORT_TIMEOUT_SECS`]. The caller -/// stores the document — this function only moves it. +/// back; the deadline is [`AI_INSIGHT_REPORT_TIMEOUT_SECS`] — short on +/// purpose, so the caller's own error path wins the race against the request +/// envelope ([`crate::constant::REQUEST_TIMEOUT_SECS`]). The caller stores the +/// document — this function only moves it. pub async fn report( bridge: &AiBridge, school: &Slug, @@ -592,4 +594,74 @@ mod tests { assert!(answer.signals.is_empty()); assert!(answer.coverage.is_none()); } + + /// The report payload's own names, pinned on the encoded JSON for the same + /// reason as the request test above: `hezarfen_zeka` reads `kind`, + /// `run_day`, `school` and the four row lists by name. + #[test] + fn the_report_request_names_are_the_contracts() { + let request = ReportRequest { + kind: REPORT_KIND_SCHOOL.into(), + run_day: "2026-09-17".into(), + requested_by: "user-9".into(), + school: ReportSchool { + id: "school-1".into(), + slug: "demo".into(), + name: "Demo Okulu".into(), + }, + summaries: Vec::new(), + recommendations: Vec::new(), + profiles: Vec::new(), + runs: Vec::new(), + }; + assert_eq!( + serde_json::to_value(&request).unwrap(), + serde_json::json!({ + "kind": "okul", + "run_day": "2026-09-17", + "requested_by": "user-9", + "school": { "id": "school-1", "slug": "demo", "name": "Demo Okulu" }, + "summaries": [], + "recommendations": [], + "profiles": [], + "runs": [], + }) + ); + } + + /// A report answer without a document is not a partial answer: `html` is + /// the one member the door cannot do without, and a service that omits it + /// is out of step, not empty-handed — the door would rather refuse the + /// dispatch than store a blank artifact. Every other member stays optional. + #[test] + fn a_report_answer_without_html_does_not_parse() { + let missing = serde_json::from_value::(serde_json::json!({ + "kind": "okul", + "run_day": "2026-09-17" + })); + assert!( + missing.is_err(), + "a documentless answer is Malformed, never an empty file" + ); + + let answer: ReportResponse = + serde_json::from_value(serde_json::json!({ "html": "" })) + .expect("the rest is optional"); + assert!(answer.notes.is_empty()); + assert!(!answer.truncated); + assert!(answer.byte_size.is_none()); + } + + /// The report door waits on its dispatch inside ONE HTTP request, so its + /// deadline must stay under the request envelope + /// ([`crate::constant::REQUEST_TIMEOUT_SECS`]): the middleware drops the + /// handler future at that ceiling — mid-write included — and answers its + /// own `503` whose "the write may or may not have applied" prose is wrong + /// for a door that knows nothing was stored. Raising the dispatch deadline + /// without the ceiling silently re-breaks exactly that, so the invariant + /// is checked when this module compiles, not when a test runs. + const _: () = assert!( + AI_INSIGHT_REPORT_TIMEOUT_SECS < crate::constant::REQUEST_TIMEOUT_SECS, + "the report dispatch must lose the race to the middleware on purpose" + ); } diff --git a/src/constant.rs b/src/constant.rs index a141c76..929cf24 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -644,14 +644,21 @@ pub const AI_INSIGHT_REFRESH_TIMEOUT_SECS: u64 = 300; /// way it never composes an insight row. pub const AI_INSIGHT_REPORT_CAPABILITY: &str = "insight.report"; -/// Deadline on one `insight.report` round trip. Sized like the refresh sweep's -/// ceiling, because it renders the same school-wide row set: the service does -/// no reads and no model calls, but a large school's rows are already on its -/// side of the wire when this starts ticking. Unlike the other two insight -/// dispatches, a caller *waits on this one* — the door stores the document -/// before it answers, so the deadline is also the ceiling on what a manager -/// can be made to wait for. -pub const AI_INSIGHT_REPORT_TIMEOUT_SECS: u64 = 300; +/// Deadline on one `insight.report` round trip, and deliberately the shortest +/// whole-school deadline in this file: the report door is the one insight +/// dispatch that **waits** — it stores the returned document before it answers +/// — and every HTTP request shares one envelope, [`REQUEST_TIMEOUT_SECS`]. +/// That middleware drops the handler future wherever it stands, mid-write +/// included, and answers its own `503` whose prose ("the write may or may not +/// have applied") is wrong for a door that knows nothing was stored. +/// +/// So the dispatch deadline stays **under** the middleware ceiling: the +/// service gets 25 s, and the door's own error path fires first with the +/// honest answer (nothing was stored; no temp file was ever opened). The +/// school-wide row reads happen before the dispatch and share the same +/// envelope, which is why the margin is not spent here. Raising this value +/// needs the middleware ceiling raised with it. +pub const AI_INSIGHT_REPORT_TIMEOUT_SECS: u64 = 25; // ---- zeka's storage surface (capabilities the *backend* serves) ------------ //