From 990540b496aa762ffc642cd6cf9339e6abdc9a4e Mon Sep 17 00:00:00 2001 From: Gavin-Meng <236174226+Gavin-Meng@users.noreply.github.com> Date: Thu, 3 Dec 2026 21:12:11 +0800 Subject: [PATCH] fix(harbor): skip shadowed tasks in training prompt rows --- envs/harbor_env/harness.py | 29 +++++++++++++---------- tests/envs/test_harbor_session_factory.py | 15 ++++++++---- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/envs/harbor_env/harness.py b/envs/harbor_env/harness.py index 7fe0d34587..08d6a2098a 100644 --- a/envs/harbor_env/harness.py +++ b/envs/harbor_env/harness.py @@ -533,10 +533,6 @@ def tasks(self) -> list[dict[str, Any]]: continue self._by_instruction[key] = index if collisions: - # Last-write-wins here would be invisible and wrong: two tasks with identical - # instructions produce two dataset rows that both resolve to one index, so a group - # trains on a task it was not given while every log line looks normal. Keeping the - # first and saying how many were shadowed at least makes it findable. logger.warning( "%d task(s) share an instruction with an earlier task and are unreachable " "through prompt lookup; the first occurrence wins", @@ -548,16 +544,23 @@ def prompt_rows(self) -> list[dict[str, Any]]: """Dataset rows for a trainer: the instruction as the prompt, plus columns worth logging. All `num_generations` of a group share a row, so they all get the same task and the group - baseline is well formed without any seed plumbing. + baseline is well formed without any seed plumbing. When tasks share an instruction, only + the task that `create()` resolves is included. `tasks()` still lists every selected task. """ - return [ - { - "prompt": [{"role": "user", "content": t.get("instruction") or ""}], - "task_name": t.get("task_name") or "", - "task_index": int(t.get("index", i)), - } - for i, t in enumerate(self.tasks()) - ] + rows = [] + for i, task in enumerate(self.tasks()): + instruction = task.get("instruction") or "" + task_index = int(task.get("index", i)) + if self._by_instruction.get(instruction_id(instruction)) != task_index: + continue + rows.append( + { + "prompt": [{"role": "user", "content": instruction}], + "task_name": task.get("task_name") or "", + "task_index": task_index, + } + ) + return rows def create( self, diff --git a/tests/envs/test_harbor_session_factory.py b/tests/envs/test_harbor_session_factory.py index 2e7fb4d242..3321c245ff 100644 --- a/tests/envs/test_harbor_session_factory.py +++ b/tests/envs/test_harbor_session_factory.py @@ -351,23 +351,28 @@ def test_the_factory_survives_pickling(): def test_duplicate_instructions_do_not_silently_shadow_each_other(caplog): - """Two tasks with one instruction produced two rows that both resolved to the LAST index, so a - group trained on a task it was never given and nothing said so.""" + """Each trainer row must resolve to the task named in that row.""" env = FakeEnv( result(), tasks=[ {"index": 0, "task_name": "a", "instruction": "same text"}, {"index": 1, "task_name": "b", "instruction": "same text"}, + {"index": 2, "task_name": "c", "instruction": "unique text"}, ], ) f = factory_with(env) with caplog.at_level("WARNING"): - f.tasks() + rows = f.prompt_rows() assert any("share an instruction" in r.message for r in caplog.records), ( "a collision that changes which task runs must be said out loud" ) - # First occurrence wins, deterministically, rather than whichever happened to be last. - assert f.create([{"role": "user", "content": "same text"}])._task_index == 0 + assert [task["index"] for task in f.tasks()] == [0, 1, 2] + assert [(row["task_index"], row["task_name"]) for row in rows] == [ + (0, "a"), + (2, "c"), + ] + for row in rows: + assert f.create(row["prompt"])._task_index == row["task_index"] def test_a_zero_timeout_defers_to_the_task_file():