Skip to content

fix(builds): bound the unmeasured build duration by the build's timeout - #14029

Merged
loks0n merged 6 commits into
mainfrom
fix/cap-unmeasured-build-duration
Sep 30, 2026
Merged

loks0n merged 6 commits into
mainfrom
fix/cap-unmeasured-build-duration

Conversation

@loks0n

@loks0n loks0n commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

When a build's exit callback carries no measured duration, Jobs::duration() falls back to wall-clock time since buildStartedAt, and that duration is what usage bills as build compute (memory × buildDuration × cpus in Usage/Build.php).

#13430 capped the duration at the build timeout. #13582 removed that cap so a measured duration that runs past the timeout (termination grace) is kept, which was right, but it also left the fallback unbounded. In production, an exit reported seven weeks after its build started was billed as 50 days of build compute on a 4 GB build.

Change (one file)

  • The unmeasured fallback in Jobs::duration() is capped at the build timeout plus the 300s of headroom Deployments already grants the build's credentials.
  • The bound is buildTimeout() + 300s. buildTimeout() is a protected hook that defaults to _APP_COMPUTE_BUILD_TIMEOUT; an extension whose builds are submitted with per-project timeouts overrides it to return the timeout that build actually had.
  • Measured durations are unchanged.

Tests
None in this PR. AGENTS.md keeps unit tests to libraries and leaves workers to e2e, and no e2e can produce an exit that arrives weeks late without a measurement. While developing, a unit harness around the exit handler confirmed the behaviour: main records the full 50 days, this change records the timeout plus 300s, and a measured duration past the timeout is kept. That harness isn't included, per the rule.

🤖 Generated with Claude Code

When a build's exit callback carries no measured duration, Jobs::duration()
falls back to wall clock since buildStartedAt. #13582 removed the clamp
from #13430 so measured durations past the timeout (termination grace) are
kept, but that also left the fallback unbounded: an exit reported seven
weeks after its build started was billed as 50 days of build compute.

The build's timeout now travels in the job's meta and comes back on its
exit callback. The fallback is capped at that timeout plus the 300s of
headroom Deployments already grants the build's credentials, or at
_APP_COMPUTE_BUILD_TIMEOUT for jobs submitted before this change.
Measured durations are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n loks0n self-assigned this Sep 30, 2026

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@ghost

ghost commented Sep 30, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

Limited by an open minor finding: Preserve the timeout used when the build was submitted

The pull request caps unmeasured build duration at the build timeout plus 300 seconds while preserving valid measured durations. A protected timeout hook defaults to the environment configuration and allows downstream workers to supply project-specific timeouts. The latest changes pass the platform database and project into that hook and pass its result explicitly into the duration calculation.

Latest changes: The timeout hook now receives the platform database and project, and its result is passed explicitly to duration().

Verdict New comments Fixed Still open
✅ Approved 0 0 2
Fix with agent prompt
### Issue 1
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php:459-460
**Preserve the timeout used when the build was submitted**

This reads the current timeout, not necessarily the one used to queue the build: changing `_APP_COMPUTE_BUILD_TIMEOUT` before a delayed exit arrives changes the billed cap for an existing build. Please retain the submitted timeout in callback metadata or deployment state, using current configuration only for older jobs without it.

### Issue 2
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php:826-828
**Restore regression coverage for the unmeasured duration cap**

Moving the calculation back into the worker leaves the late, unmeasured callback case without regression coverage: the existing Functions duration test only exercises an ordinary successful build. Please cover a delayed unmeasured exit and a measured duration beyond the cap through an integration test, or restore a tested domain calculation; AGENTS.md disallows unit-testing private worker methods.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 1
File Change
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php Caps fallback elapsed duration at timeout plus 300 seconds and adds a project-aware overridable timeout hook, leaving measured durations unchanged.
⏳ Still open from earlier reviews · 2

Reviewed the commits since 1237cae · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Review on #14029: the worker already has the project's plan, which is
where the build was queued with its timeout, so there's no need to echo
the timeout through the job's meta; the Deployments change is reverted.

The duration logic moves into Appwrite\Deployment\BuildDuration, which
takes the clock as an argument, and is unit-tested there instead of
through the worker, as AGENTS.md asks (workers are covered by e2e).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/cap-unmeasured-build-duration (after).

Metric Before After Change
🚀 Requests/sec 202.21 207.98 ⚪ +2.9%
⏱️ Latency P50 84.7 ms 82.79 ms ⚪ -2.3%
⏱️ Latency P95 197.7 ms 193.9 ms ⚪ -1.9%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 82.79 193.9 12,882 207.98 -3.79
Account 153.28 287.45 678 11.26 -18.71
TablesDB 77.9 146.04 7,006 115.13 -11.46
Storage 80.73 176.01 3,390 57.67 -0.3
Functions 122.6 249.22 1,808 31.43 +17.83

Top API waits (after)

API request Max wait (ms)
account.name.update 406.25
functions.create 403.78
account.prefs.update 379.93
functions.variables.update 350.53
functions.variables.create 341.7

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Comment on lines +459 to +460
// The same timeout the build was queued with: the plan's, else the operator's.
$timeout = (int) ($plan['buildTimeout'] ?? System::getEnv('_APP_COMPUTE_BUILD_TIMEOUT', 900));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve the timeout used when the build was submitted

This reads the current timeout, not necessarily the one used to queue the build: changing _APP_COMPUTE_BUILD_TIMEOUT before a delayed exit arrives changes the billed cap for an existing build. Please retain the submitted timeout in callback metadata or deployment state, using current configuration only for older jobs without it.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php
Line: 459-460

Comment:
**Preserve the timeout used when the build was submitted**

This reads the current timeout, not necessarily the one used to queue the build: changing `_APP_COMPUTE_BUILD_TIMEOUT` before a delayed exit arrives changes the billed cap for an existing build. Please retain the submitted timeout in callback metadata or deployment state, using current configuration only for older jobs without it.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · bug · Reply if this doesn't apply.

Drops the BuildDuration class and its test from the previous commit: the
bound is two lines in Jobs::duration(), with the timeout taken from the
worker's injected plan at the call site.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@loks0n
loks0n merged commit 47b3e24 into main Sep 30, 2026
49 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant