Files
pricebuddy/app/Services/ExtractionBudget.php
Jeremy 69e6f58c84 fix: stop the extraction budget handing out time it does not have
remainingSecondsForTimeout() rounded up to one second whenever less than one
remained. Since isExhausted() only covers <= 0, a fetch with 0.4s left was both
allowed and given a full second, overspending the deadline on every call - up to
25 times through the agent's tool loop.

It now returns null below one second: an explicit "cannot start", never confused
with Guzzle's "0 means no timeout". HealingContext::fetch() rejects that state -
the null check subsumes its old isExhausted() guard - and caps any explicit
timeout at the remaining budget, so the parameter cannot become a way past the
deadline.

Two other call sites needed the same distinction:

- AiConfigHealer passes this value straight to runAgent(timeout:), where null
  means "use the provider's timeout". Passing the nullable value through would
  have silently restored the unbounded 120s call this whole mechanism exists to
  prevent, so it now separates "no budget" (unbudgeted callers keep the provider
  timeout) from "budget with nothing left" (stop).
- MetaExtractionService::scrapeTimeout() does min($this->timeout, ...), and
  min(10, null) is null in PHP, which would have hit setConnectTimeout(int). The
  one-second floor now lives there, where it belongs: the scrape runs first, so
  the budget is always full, and the floor only bites on a near-zero budget.

The new test pins the actual bug: with 2 seconds of budget and 1.2 elapsed the
budget is not exhausted and has real time left, but has no whole second to hand
out. The old code returned 1 there.

Live re-check against the wedged provider is unchanged: HTTP 200 in 24.16s with
reason "timeout".
2026-08-08 19:49:38 +10:00

67 lines
2.1 KiB
PHP

<?php
namespace App\Services;
/**
* A wall-clock budget for one meta-extraction request.
*
* The endpoint is synchronous and user-facing, so every network call it makes — the
* scrape, an optional browser re-scrape, and the AI healing agent — has to draw from a
* single ceiling rather than each enforcing its own timeout. Callers ask for the
* remaining time and pass it down as a per-call timeout.
*/
class ExtractionBudget
{
protected float $startedAt;
public function __construct(
public readonly int $totalSeconds,
) {
$this->startedAt = microtime(true);
}
public static function start(?int $totalSeconds = null): self
{
return new self($totalSeconds ?? (int) config('price_buddy.meta_extraction.budget_seconds', 25));
}
public function elapsedSeconds(): float
{
return microtime(true) - $this->startedAt;
}
public function remainingSeconds(): float
{
return max(0.0, $this->totalSeconds - $this->elapsedSeconds());
}
/**
* Remaining budget as a whole number of seconds, for the timeout arguments of HTTP
* clients that only take integers, or null when there is no longer enough left to
* start anything safely.
*
* Null rather than a one-second floor: rounding up would hand out time the budget no
* longer has, and a 0 timeout means "no timeout" to Guzzle — the exact failure mode
* this class exists to prevent. Callers must treat null as "do not start", not as
* "no limit".
*/
public function remainingSecondsForTimeout(): ?int
{
return $this->hasAtLeast(1) ? (int) floor($this->remainingSeconds()) : null;
}
/**
* Whether at least this many seconds remain. Used to decide whether an expensive
* step can plausibly finish, rather than starting one that will only be cut off.
*/
public function hasAtLeast(float $seconds): bool
{
return $this->remainingSeconds() >= $seconds;
}
public function isExhausted(): bool
{
return $this->remainingSeconds() <= 0.0;
}
}