forge
Explore
Log in

Don't you dare go Hollow

Size the report deadline under the request envelope

Browse files at this commit

8ce2e1d3c4246e4177338de85e73c83fbe0db7b3Ahmet Kaan GümüşParent: 541d9b7
  • fix
    • size the report deadline under the request envelope
Show full message
🐛 fix(insight): size the report deadline under the request envelope

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-<uuid>.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.rsmodified+74 -2
@@ -296,8 +296,10 @@ pub async fn refresh(
296 296
297 /// Ask for one school-level report document, awaited. The request carries297 /// Ask for one school-level report document, awaited. The request carries
298 /// every row the document needs, and the answer carries the rendered HTML298 /// every row the document needs, and the answer carries the rendered HTML
299-/// back; the deadline is [`AI_INSIGHT_REPORT_TIMEOUT_SECS`]. The caller299+/// back; the deadline is [`AI_INSIGHT_REPORT_TIMEOUT_SECS`] — short on
300-/// stores the document — this function only moves it.300+/// purpose, so the caller's own error path wins the race against the request
301+/// envelope ([`crate::constant::REQUEST_TIMEOUT_SECS`]). The caller stores the
302+/// document — this function only moves it.
301 pub async fn report(303 pub async fn report(
302 bridge: &AiBridge,304 bridge: &AiBridge,
303 school: &Slug,305 school: &Slug,
@@ -592,4 +594,74 @@ mod tests {
592 assert!(answer.signals.is_empty());594 assert!(answer.signals.is_empty());
593 assert!(answer.coverage.is_none());595 assert!(answer.coverage.is_none());
594 }596 }
597+
598+ /// The report payload's own names, pinned on the encoded JSON for the same
599+ /// reason as the request test above: `hezarfen_zeka` reads `kind`,
600+ /// `run_day`, `school` and the four row lists by name.
601+ #[test]
602+ fn the_report_request_names_are_the_contracts() {
603+ let request = ReportRequest {
604+ kind: REPORT_KIND_SCHOOL.into(),
605+ run_day: "2026-09-17".into(),
606+ requested_by: "user-9".into(),
607+ school: ReportSchool {
608+ id: "school-1".into(),
609+ slug: "demo".into(),
610+ name: "Demo Okulu".into(),
611+ },
612+ summaries: Vec::new(),
613+ recommendations: Vec::new(),
614+ profiles: Vec::new(),
615+ runs: Vec::new(),
616+ };
617+ assert_eq!(
618+ serde_json::to_value(&request).unwrap(),
619+ serde_json::json!({
620+ "kind": "okul",
621+ "run_day": "2026-09-17",
622+ "requested_by": "user-9",
623+ "school": { "id": "school-1", "slug": "demo", "name": "Demo Okulu" },
624+ "summaries": [],
625+ "recommendations": [],
626+ "profiles": [],
627+ "runs": [],
628+ })
629+ );
630+ }
631+
632+ /// A report answer without a document is not a partial answer: `html` is
633+ /// the one member the door cannot do without, and a service that omits it
634+ /// is out of step, not empty-handed — the door would rather refuse the
635+ /// dispatch than store a blank artifact. Every other member stays optional.
636+ #[test]
637+ fn a_report_answer_without_html_does_not_parse() {
638+ let missing = serde_json::from_value::<ReportResponse>(serde_json::json!({
639+ "kind": "okul",
640+ "run_day": "2026-09-17"
641+ }));
642+ assert!(
643+ missing.is_err(),
644+ "a documentless answer is Malformed, never an empty file"
645+ );
646+
647+ let answer: ReportResponse =
648+ serde_json::from_value(serde_json::json!({ "html": "<!doctype html><html></html>" }))
649+ .expect("the rest is optional");
650+ assert!(answer.notes.is_empty());
651+ assert!(!answer.truncated);
652+ assert!(answer.byte_size.is_none());
653+ }
654+
655+ /// The report door waits on its dispatch inside ONE HTTP request, so its
656+ /// deadline must stay under the request envelope
657+ /// ([`crate::constant::REQUEST_TIMEOUT_SECS`]): the middleware drops the
658+ /// handler future at that ceiling — mid-write included — and answers its
659+ /// own `503` whose "the write may or may not have applied" prose is wrong
660+ /// for a door that knows nothing was stored. Raising the dispatch deadline
661+ /// without the ceiling silently re-breaks exactly that, so the invariant
662+ /// is checked when this module compiles, not when a test runs.
663+ const _: () = assert!(
664+ AI_INSIGHT_REPORT_TIMEOUT_SECS < crate::constant::REQUEST_TIMEOUT_SECS,
665+ "the report dispatch must lose the race to the middleware on purpose"
666+ );
595 }667 }
src/constant.rsmodified+15 -8
@@ -644,14 +644,21 @@ pub const AI_INSIGHT_REFRESH_TIMEOUT_SECS: u64 = 300;
644 /// way it never composes an insight row.644 /// way it never composes an insight row.
645 pub const AI_INSIGHT_REPORT_CAPABILITY: &str = "insight.report";645 pub const AI_INSIGHT_REPORT_CAPABILITY: &str = "insight.report";
646 646
647-/// Deadline on one `insight.report` round trip. Sized like the refresh sweep's647+/// Deadline on one `insight.report` round trip, and deliberately the shortest
648-/// ceiling, because it renders the same school-wide row set: the service does648+/// whole-school deadline in this file: the report door is the one insight
649-/// no reads and no model calls, but a large school's rows are already on its649+/// dispatch that **waits** — it stores the returned document before it answers
650-/// side of the wire when this starts ticking. Unlike the other two insight650+/// — and every HTTP request shares one envelope, [`REQUEST_TIMEOUT_SECS`].
651-/// dispatches, a caller *waits on this one* — the door stores the document651+/// That middleware drops the handler future wherever it stands, mid-write
652-/// before it answers, so the deadline is also the ceiling on what a manager652+/// included, and answers its own `503` whose prose ("the write may or may not
653-/// can be made to wait for.653+/// have applied") is wrong for a door that knows nothing was stored.
654-pub const AI_INSIGHT_REPORT_TIMEOUT_SECS: u64 = 300;654+///
655+/// So the dispatch deadline stays **under** the middleware ceiling: the
656+/// service gets 25 s, and the door's own error path fires first with the
657+/// honest answer (nothing was stored; no temp file was ever opened). The
658+/// school-wide row reads happen before the dispatch and share the same
659+/// envelope, which is why the margin is not spent here. Raising this value
660+/// needs the middleware ceiling raised with it.
661+pub const AI_INSIGHT_REPORT_TIMEOUT_SECS: u64 = 25;
655 662
656 // ---- zeka's storage surface (capabilities the *backend* serves) ------------663 // ---- zeka's storage surface (capabilities the *backend* serves) ------------
657 //664 //