Skip to content

fix(diff): reuse a trial whose source was a successful regrade - #4214

Open
lxcxjxhx wants to merge 2 commits into
harbor-framework:mainfrom
lxcxjxhx:fix/reuse-successful-regrade
Open

lxcxjxhx wants to merge 2 commits into
harbor-framework:mainfrom
lxcxjxhx:fix/reuse-successful-regrade

Conversation

@lxcxjxhx

@lxcxjxhx lxcxjxhx commented Nov 11, 2026 •

Copy link
Copy Markdown

What/why

$ python -m pytest tests/unit/test_job_diff.py -k successful_regrade -q
FAILED tests/unit/test_job_diff.py::test_successful_regrade_is_reused
1 failed, 55 deselected

compare_job_trials scheduled a rerun for a trial whose task version, digest, agent settings and
environment all match, because the source trial carries a source_trial pointer and
_same_non_task_inputs compared whole models:

def _same_non_task_inputs(source: TrialLock, target: TrialLock) -> bool:
    return source.model_copy(update={"task": target.task}) == target

source_trial records the trial this one derives from, and a regraded trial always carries one, so its
presence alone made an identical trial look different.

Change, and a correction to the first version

The first commit in this PR said "source_trial records where a result came from, not what produced it"
and simply ignored the pointer. Review showed that is wrong for a regrade: a regrade re-runs
verification in its own environment and restores agent artifacts from the source trial, so the
regrade's lock records the environment it verified in, not the one that produced those artifacts.
Ignoring the pointer let a diff reuse a trial whose agent never ran in the target environment.

The source trial's environment is now recorded and compared (b5ceaf5):

  • SourceTrialLock gains environment, copied from the source lock the same way task already is;
  • _same_non_task_inputs requires the origin's environment to equal the target's, then compares the
    rest as before. task-version handling is untouched.

Two consequences, both conservative:

Verification

# source reverted, new tests kept
FAILED test_a_regrade_from_another_environment_is_not_reused
FAILED test_a_regrade_without_a_recorded_origin_environment_is_not_reused
# with the change
3 passed        (including the existing test_successful_regrade_is_reused)

Whole files: 64 passed, 4 failed. The four failures are
test_resolve_diff_sources_keeps_readable_alternatives[...-inaccessible], which need POSIX permissions
and fail the same way on the unmodified branch here (4 failed, 52 passed before). ruff check shows
the same three pre-existing UP017 warnings before and after.

Known limit

One level only: a regrade of a regrade records its immediate source's environment, not the one two
steps back. Walking that chain would need the origin lock on disk; I left it out rather than guess.

@vercel

vercel Bot commented Nov 11, 2026

Copy link
Copy Markdown

@lxcxjxhx is attempting to deploy a commit to the Harbor Framework Team on Vercel.

A member of the Team first needs to authorize it.

@devin-ai-integration devin-ai-integration 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.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

Devin Review found 1 potential issue.

Devin Review

Comment thread src/harbor/job_diff.py
# otherwise identical trial look different.
return (
source.model_copy(
update={"task": target.task, "source_trial": target.source_trial}

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.

🔴 Regraded trials reuse incompatible agent environments

After a regrade changes environments, _same_non_task_inputs matches its result to a target that runs agents in the new environment. Regrade configuration records the new environment, but agent artifacts still come from the original trial. Diff skips the required agent run and reuses the original environment's result.

Learn more

A regrade runs verification again but restores its agent artifacts from the recorded source instead of starting an agent. Regrade configuration uses the new job's environment, and build_trial_lock records that environment while retaining only the source trial's task and skills in its lock. Ignoring the source pointer therefore compares the target environment with the regrade's verifier-side environment, not the environment that produced the agent artifacts. A successful regrade becomes eligible for reuse even though its agent never ran in the target environment.

Example: Trial A runs with Docker on task version 1.1.0. Job B regrades A to 1.2.0 using a different environment and succeeds. A diff targeting version 1.2.0 in B's environment reuses B, although the agent artifacts were produced in Docker.

Recommended fix: For regraded candidates, resolve the recorded source lock and compare its agent-producing inputs, including its environment, with the target before allowing reuse or another regrade. Preserve the existing version-based handling of task changes.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in b5ceaf5, along the first route in the recommendation.

SourceTrialLock now carries environment, copied from the source trial's lock the same way task already is, and _same_non_task_inputs compares it with the target. task-version handling is untouched.

What changes in behaviour, both conservative:

Verification:

# source reverted, new tests kept
FAILED test_a_regrade_from_another_environment_is_not_reused
FAILED test_a_regrade_without_a_recorded_origin_environment_is_not_reused
# with the change
3 passed (including the existing test_successful_regrade_is_reused)

Whole files: 64 passed, 4 failed — the four test_resolve_diff_sources_keeps_readable_alternatives[...-inaccessible] cases need POSIX permissions and fail the same way on the unmodified branch here (4 failed, 52 passed before).

One level only: a regrade of a regrade records its immediate source's environment, not the one two steps back. Walking that chain would need the origin lock on disk; I left it out rather than guess, and can add it if you want it.

@github-actions

Copy link
Copy Markdown
Contributor

Enjoy a better diff viewing experience by clicking one of these URLs:

…environment

A regrade re-verifies in its own environment but restores agent artifacts from
the source trial, so the regrade's lock records the environment it verified in,
not the one that produced the artifacts. Matching that environment let a diff
reuse a trial whose agent never ran in the target environment.

`SourceTrialLock` now carries the source trial's environment, copied from its
lock the same way `task` already is, and the diff compares it with the target.

Two consequences, both in the safe direction:

* a regrade whose source ran in a different environment is no longer reused;
* a lock written before the field existed records nothing to compare, so it is
  not reused either — the same stance the loaded-trajectory field takes.

task-version handling is untouched.

@devin-ai-integration devin-ai-integration 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.

Devin Review found 3 potential issues.

Devin Review

),
path=source_config.path,
task=source_lock.task if source_lock is not None else None,
environment=source_lock.environment if source_lock is not None else None,

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.

🔴 Chained regrades lose the producing environment

After two regrades, _same_producing_environment can accept artifacts produced in a different environment. build_trial_lock copies the immediate regrade's environment, not the original agent environment. Diff then reuses those artifacts for the wrong environment.

Learn more

Each regrade restores recorded agent outputs rather than running the agent, as implemented by RegradeTrial. For a direct source trial, copying its environment correctly identifies where the agent ran. For a source that is itself a regrade, its top-level environment identifies the verification environment instead. The new environment check trusts this copied value and cannot detect the lost ancestry. Contrary to the helper's comment, the second regrade receives a non-null environment and passes the check.

Example: Run an agent in Docker, regrade its artifacts using Daytona, and regrade that result using Daytona again. The second regrade records Daytona as its source environment even though Docker produced its agent artifacts. A diff targeting Daytona then accepts that second regrade.

Recommended fix: When the source has source_trial, propagate its recorded producing environment rather than source_lock.environment, preserving None for unknown ancestry. Alternatively, explicitly mark chained regrades unknown and reject their reuse. Add a test that builds both regrade locks through build_trial_lock before comparing the final result.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread src/harbor/job_diff.py
Comment on lines +393 to +398
return (
source.model_copy(
update={"task": target.task, "source_trial": target.source_trial}
)
== target
)

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.

🔴 Regrades conceal original agent inputs

_same_non_task_inputs can reuse regrades after extra instructions disappear or Compose overlay contents change. Regrade locks record current settings, while SourceTrialLock preserves neither original instruction digests nor overlay digests. Matching targets therefore reuse artifacts produced with different agent inputs.

Learn more

The new comparison trusts a regrade's top-level lock as a description of the original agent inputs. However, job regrade construction carries the agent and artifact configuration but omits extra instructions, user-agent configuration, and agent-specific timeout settings. Standalone regrade construction also omits those original settings. build_trial_lock therefore records no extra instructions for those regrades, even when the original agent used them. Compose overlays have the same provenance gap: their content digests are rebuilt from current files, while the new source environment stores only their paths. The producing-environment comparison cannot distinguish either mismatch, and removing the provenance pointer from equality makes these regrades newly reusable.

Example: Run a task with an extra instruction saying “use the supplied reference answer,” then successfully regrade it through a source job with otherwise default settings. Diff the regraded job against the same task and agent without extra instructions. Both compared locks now contain no extra instructions, so diff reports reuse despite the original agent receiving additional input. Similarly, modifying an overlay at the same path before regrading replaces its recorded digest with the new content's digest without rerunning the agent.

Recommended fix: Preserve and compare resolved producing-agent inputs, including instruction and Compose digests, separately from re-verification settings. Until that provenance exists, reject reuse when the original resolved agent inputs cannot be established. Add end-to-end lock-building tests for a regrade of an extra-instruction run and an overlay edited between the original run and its regrade.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

self.type,
str(self.trial_id) if self.trial_id is not None else str(self.path),
self.task.digest if self.task is not None else None,
_frozen_value(self.environment),

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.

🟡 Existing regrade jobs fail to resume

After upgrading, SourceTrialLock equality rejects existing regrade job locks when their source locks remain readable. Old locks contain no environment; rebuilt locks populate it. _write_job_lock then raises FileExistsError before the job resumes.

Learn more

Older serialized SourceTrialLock records have no environment field and deserialize with None. The updated build_trial_lock fills that field whenever the source trial's lock is readable, including source locks created before this change. JobLock equality includes each trial's provenance equality. During resume, _write_job_lock compares the persisted lock with the newly resolved lock and rejects this metadata-only difference.

Example: Start a regrade job before upgrading and interrupt it after its job lock is written. Keep its source job unchanged and readable, then resume after upgrading. The saved source environment is absent, the rebuilt source environment is populated, and resume fails with the lock-mismatch error.

Recommended fix: Add an explicit compatibility or migration path for absent source-environment metadata when reconciling persisted job locks. Keep the conservative rejection of unknown producing environments in diff planning separate from resume equality. Add a resume regression test using a serialized pre-change regrade job lock.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant