Skip to content

Keep a lease up while finished jobs' results are unfetched - #59

Merged
lydakis merged 5 commits into
mainfrom
claude/errand-results-keep-lease
Oct 6, 2026
Merged

lydakis merged 5 commits into
mainfrom
claude/errand-results-keep-lease

Conversation

@lydakis

@lydakis lydakis commented Oct 6, 2026 •

Copy link
Copy Markdown
Owner

Before: a leased machine was released once its runner had no staging, starting, running or queued jobs for idle_timeout, even when a finished job's retained workspace changes were still on it and no client had fetched them. A detached run, or a foreground run that does not apply, left its results on the machine, and they ended with the lease.

After: a finished job whose retained changes no client has downloaded yet counts as work, as a running job does, if the runner admitted the job during the lease, so the lease stays up until they are fetched or max_lifetime ends it. Results left on a reused runner (the static pool, or any provider that hands out existing machines) by an earlier lease or the machine's own users do not hold a new lease, since its owner cannot fetch them. On a new machine every job ran during the lease, so every result counts there. Once fetched, the lease is released at the next idle deadline as before. A job that changed nothing never counts. Persistent workspaces do not count, nor do the changes of a job run on one, so they cannot pin a lease to its lifetime by themselves; they still end with the lease. A foreground run that applies its changes is covered from the job's exit to its download: the job is counted before it leaves the running set.

What "fetched" means: a client downloaded the job's retained change bundle whole through GET /v0/jobs/<id>/changes, which is what errand fetch (stage, apply or --output), --apply and a run that applies its changes all do. Until now the runner kept no record of this, so the smallest one is added: on the first complete download it writes fetched.json ({"fetched_at": ...}) once in the job's receipt directory, so the count survives a runner restart. Removing the job (errand gc jobs, or a client's change GC) also ends the count. Retention expiry does not exist for unfetched receipts today, so max_lifetime remains the bound.

How: the daemon keeps the finished jobs with unfetched changes, Daemon.unfetched (job to admission time), under the lock /v0/info already takes, and a per-job flag under the same lock for whether the job's changes were fetched, set from fetched.json at start-up and by every whole download. A job is counted when it finishes with a non-empty change bundle and no persistent workspace, or is loaded at start-up that way, unless it was fetched. finalize publishes the terminal state and result and counts the job in one d.mu critical section, before the job leaves the running set. Fetch, job GC, admission rollback and /v0/info all take d.mu, so none of them sees a result before it is counted. Counting requires the job to still be in d.jobs, so a removed job is never added, and removing a job that is not counted does nothing. A whole download always marks the job fetched, writes fetched.json once and drops the job. Removing the job also drops it. The daemon also keeps when it admitted its most recent job, Daemon.lastAdmitted, updated under the lock admission already holds and at start-up.

/v0/info reports two runner-clock times (fields at the end of proto.Info): latest_admitted, its most recent job, and latest_unfetched, the most recent job whose changes are unfetched (a scan of the unfetched set, no I/O). When a lease becomes ready the cloud peer records latest_admitted from the answer that found it ready (admitted_before, persisted in the lease record so it survives a cloud peer restart; zero on a new machine), and busy counts results only when latest_unfetched is later. Both sides of that comparison are the runner's own times, so the cloud peer's clock is never compared with the runner's. Owner scoping was not used: the runner does not know a lease's owner, and over SSH the owner reaches it as a local user. Setup's quiesce does not count unfetched results: they survive a runner restart.

Tests: TestInfoCountsUnfetchedResults (a job that changed nothing never counts but is the latest admitted; the latest unfetched admission moves back when the newer job is downloaded; a second download changes nothing; both survive a restart; job GC ends the count). TestFetchBeforeResultsAreCountedIsRecorded (a download that finishes before the job is counted is recorded, and the count that follows sees it). TestWorkspaceJobResultsAreNotCounted (a workspace job's changes are not counted). TestJobGCRightAfterPublishLeavesNoCount (job GC run from a test hook as soon as the result is visible leaves no count behind). TestUnfetchedResultsKeepLease (unfetched results keep a lease past its idle deadline; after the fetch it is released at the next idle deadline; results never fetched keep it only until max_lifetime; the machine's clock is 3h behind the cloud peer's). TestEarlierResultsDoNotHoldLease (a reused machine with an old unfetched result: the new lease idles out, across a cloud peer restart; a later lease's own job's result holds it until fetched; the machine's clock is 3h ahead).

This branch is cut from main, separate from #57; both add fields at the end of proto.Info and change the broker's busy expression, so whichever merges second has a small conflict there.

Closes the #33 item "Results end with the lease".

🤖 Generated with Claude Code

https://claude.ai/code/session_013BQxiCNDXMnjv5pvmgo5E6


Generated by Claude Code

A finished job whose retained workspace changes no client has downloaded
yet now counts as work on the runner: /v0/info reports such jobs as
unfetched, and the cloud peer's idle check counts them, so a lease is not
released with results still on the machine, until max_lifetime. A client
downloading the changes whole (fetch, --apply, or a run that applies
them) records fetched.json in the job's receipt, which ends the count and
outlives a restart. Persistent workspaces do not count.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013BQxiCNDXMnjv5pvmgo5E6
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-07T08:05:40.545399Z 1d46613 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

lydakis commented Oct 6, 2026

Copy link
Copy Markdown
Owner Author

github-advanced-security fails on GitHub's side on every PR in this repo (Copilot code scanning: unsupported model / monthly quota), not because of this change; a re-run returns 403. The repo's own checks (tests on Linux, macOS and Windows, CodeQL, GitGuardian) decide this PR.


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7523e6b548

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/daemon/results_fetched.go Resolved
A changes download could finish after finalize published the job's result
but before it counted the job as unfetched. resultsFetched saw no count and
returned without writing fetched.json, and finalize then counted the job
for good, keeping the lease up until max_lifetime.

The job now carries whether its changes were fetched, set from fetched.json
at load and by every whole download, which always writes the record once.
noteResultsLocked consults it, so the count and the record agree in
whichever order the download and finalize run.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013BQxiCNDXMnjv5pvmgo5E6
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a199530e37

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/daemon/server.go Resolved

lydakis commented Oct 6, 2026

Copy link
Copy Markdown
Owner Author

@codex security review


Generated by Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a199530e37

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/cloud/broker.go Resolved
The unfetched count was global to the runner. On a reused machine, such
as one from the static pool, results left by an earlier lease or by the
machine's own users kept every probe busy, though the new owner could not
fetch them, so the lease ran to max_lifetime.

/v0/info now reports, by the runner's clock, when it admitted its most
recent job and when it admitted the most recent job whose results are
unfetched. The cloud peer records the first from the answer that found
the lease ready, persisted with the lease, and counts results only when
the second is later. Both are the runner's times, so the cloud peer's
clock is never compared with them. A new machine has admitted nothing
before the lease, so every result there still holds it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013BQxiCNDXMnjv5pvmgo5E6
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: de1be43ce0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/cloud/broker.go Resolved
Comment thread internal/cloud/broker.go Resolved
Comment thread internal/daemon/results_fetched.go Resolved
Comment thread internal/daemon/job.go Resolved
A job's terminal result was published under its own lock and counted as
unfetched later under the daemon's. Job GC, which takes the daemon's lock,
could see the result in between and remove the job, and the count then kept
an entry for a job the runner no longer had until a restart. The result is
now published and counted in one critical section of the daemon's lock,
which fetch, job GC, admission rollback and /v0/info all take, so none of
them sees a result before it is counted. Counting also requires the job to
still be the runner's, and the count is keyed by job, so removing a job
that is not counted does nothing.

A job on a persistent workspace retains changes too, but they changed the
workspace, which ends with a lease without holding it, so they are no
longer counted.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013BQxiCNDXMnjv5pvmgo5E6
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

lydakis commented Oct 6, 2026

Copy link
Copy Markdown
Owner Author

@codex security review


Generated by Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9a4353a2c6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/cloud/broker.go Resolved

lydakis commented Oct 6, 2026

Copy link
Copy Markdown
Owner Author

@codex security review


Generated by Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

lydakis commented Oct 6, 2026

Copy link
Copy Markdown
Owner Author

@codex security review


Generated by Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

1 similar comment
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

Keeps both #57's workspace transfers and this branch's unfetched results:
/v0/info reports transfers and the latest admitted/unfetched times, and the
broker counts transfers as work alongside jobs and the lease's own results.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013BQxiCNDXMnjv5pvmgo5E6
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1d4661320d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/cloud/broker.go Resolved
@lydakis
lydakis merged commit 25e8648 into main Oct 6, 2026
8 of 9 checks passed
@lydakis
lydakis deleted the claude/errand-results-keep-lease branch October 6, 2026 22:12

lydakis commented Oct 7, 2026

Copy link
Copy Markdown
Owner Author

@codex security review


Generated by Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 25e86485b5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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.

2 participants