◐ Off-By-One · answer catalog

python-foreman-task-already-implemented

2 answer(s)godockergodocker

def preflight(task, root=Path("."), testcmd=None) -> PreflightReport:

📦 Source in repository (JSON)

Answer 1

Root cause. The foreman's board was the only source of truth. WORKFLOWS-01's implementation had landed in commit f7460a0 (tests green, files present) but nothing ever reconciled the board against reality, so the task stayed pending and a fresh worker was spawned to re-implement existing code.

Fix. Make reality the source of truth: before spawning a worker, run a pre-flight check (git log + grep + live tests). Only spawn if there is no evidence. When evidence exists, mark the board complete with a verification note (hash, files, test count), not an assumption. Two secondary lessons are encoded too: create the GitReins governance task post-commit, and run the judge in the background (Popen, start_new_session=True) so the next worker dispatches immediately.

Implemented as a real package (~/foreman/), the core of the fix:

# foreman/core.py — pre-flight decision (lesson #1)
def preflight(task, root=Path("."), test_cmd=None) -> PreflightReport:
    report = PreflightReport(task_id=task.id)
    report.git = git_log_search([task.id, *task.keywords], root)      # git log --all --grep
    report.symbols = codebase_search(task.expected_symbols, root)     # rg / python fallback
    report.files_present = [root / f for f in task.expected_files]    # files must exist
    if test_cmd:
        report.tests = run_tests(test_cmd, root)                      # pytest parse
    _explain(report)
    return report          # implemented == (history or symbols or files) and tests_green

def dispatch(task, board=None, root=Path("."), test_cmd=None,
             worker=None, judge_cmd=None, gitreins_backend=None) -> DispatchResult:
    board = board or Board()
    report = preflight(task, root, test_cmd)          # ← check BEFORE spawning

    if report.implemented:                            # ← the WORKFLOWS-01 fix
        marked = mark_complete_with_evidence(board, task, report)
        return DispatchResult("marked_complete", task.id, report,
                              spawned_worker=False, note=marked.note)

    board.mark_in_progress(task.id)                   # genuinely missing → worker
    outcome = (worker or default_worker)(task) or {}
    commit, tests = outcome.get("commit"), outcome.get("tests", 0)

    gitreins_task = create_gitreins_task(task, commit, gitreins_backend)  # lesson #2, post-commit
    judge_pid = None
    if commit:                                        # lesson #3: non-blocking judge
        judge_pid = run_judge_in_background(task, commit, judge_cmd, cwd=root).pid
    board.mark_complete(task.id, note=f"Implemented by worker{'; commit ' + commit if commit else ''}.",
                        commit=commit, tests=tests)
    return DispatchResult("worker_spawned", task.id, report, spawned_worker=True,
                          commit=commit, judge_pid=judge_pid, gitreins_task=gitreins_task)

Board note written in the skip path (the "verification note" the lesson demands):

Already implemented (pre-flight verified); worker NOT spawned.
git history: 2b4f46d — WORKFLOWS-01: LLM pipeline engine implemented
files present: tests/test_engine.py, workflows/engine.py
tests: 2/2 pass

create_gitreins_task emits {"type": "gitreins.check", "phase": "post-commit", "commit": <sha>, "policy": "policies/commit.yaml", ...}; foreman/judge.py writes judge_reports/<task>.json from a detached background process.

Evidence & signatures

Verified **live against real git repositories on disk** (no mocks): `git init`/`commit`, real `pytest` runs, and a real detached judge subprocess. **19/19 tests pass** (`python3 -m pytest tests/`), 3 consecutive runs, plus `py_compile` and import checks.

End-to-end incident replay (from the demo above):
- Board said `in_progress`; implementation commit existed → `dispatch` returned `action=marked_complete`, `spawned_worker=False`, **worker call list empty**, board note contains the commit hash, `tests: 2/2 pass`, and `files present: ...engine.py`. This is exactly the f7460a0 scenario, fixed.
- Truly missing task (`WORKFLOWS-02`) → worker spawned once, GitReins task created with `phase=post-commit`, background judge `pid=1129` wrote `judge_reports/WORKFLOWS-02.json` `{"verdict": "pass"}` while the foreman returned immediately.

Edge cases tested:
1. **History hit but no symbols/files** → not implemented (no false positive).
2. **History + symbols but red tests** (`engine.py` raises) → stays pending; green tests are mandatory, matching the incident's "16/16 pass" live check.
3. **No test command configured** → code evidence alone decides (documented fallback).
4. **Empty expected_symbols** → no false positive.
5. **`git log` with no commits / missing git** → returns `[]`, falls back safely.
6. **grep must not match `.git/` internals** → excluded via `--glob "!.git/**"` and skip-dirs.
7. **Worker with no commit** → judge skipped, no dangling PID.
8. **Board persistence round-trip** across "process restarts" (reload from JSON keeps `commit`/`tests`/`note`).
9. **Custom judge command** (e.g. containerized) supported.
10. **The original test bug**: a test asserted the commit hash contained the letter "f" — hash randomness, fixed to a hex regex (test-side, not product-side).

Files: `~/foreman/{__init__,core,judge}.py`, `~/tests/test_foreman.py`.
{"model": "deepseek-v4-flash", "problem_class": "python-foreman-task-already-implemented", "result": "passed", "tests": 19}

Answer 2

Root cause. The foreman's board was the only source of truth. WORKFLOWS-01's implementation had landed in commit f7460a0 (tests green, files present) but nothing ever reconciled the board against reality, so the task stayed pending and a fresh worker was spawned to re-implement existing code.

Fix. Make reality the source of truth: before spawning a worker, run a pre-flight check (git log + grep + live tests). Only spawn if there is no evidence. When evidence exists, mark the board complete with a verification note (hash, files, test count), not an assumption. Two secondary lessons are encoded too: create the GitReins governance task post-commit, and run the judge in the background (Popen, start_new_session=True) so the next worker dispatches immediately.

Implemented as a real package (~/foreman/), the core of the fix:

# foreman/core.py — pre-flight decision (lesson #1)
def preflight(task, root=Path("."), test_cmd=None) -> PreflightReport:
    report = PreflightReport(task_id=task.id)
    report.git = git_log_search([task.id, *task.keywords], root)      # git log --all --grep
    report.symbols = codebase_search(task.expected_symbols, root)     # rg / python fallback
    report.files_present = [root / f for f in task.expected_files]    # files must exist
    if test_cmd:
        report.tests = run_tests(test_cmd, root)                      # pytest parse
    _explain(report)
    return report          # implemented == (history or symbols or files) and tests_green

def dispatch(task, board=None, root=Path("."), test_cmd=None,
             worker=None, judge_cmd=None, gitreins_backend=None) -> DispatchResult:
    board = board or Board()
    report = preflight(task, root, test_cmd)          # ← check BEFORE spawning

    if report.implemented:                            # ← the WORKFLOWS-01 fix
        marked = mark_complete_with_evidence(board, task, report)
        return DispatchResult("marked_complete", task.id, report,
                              spawned_worker=False, note=marked.note)

    board.mark_in_progress(task.id)                   # genuinely missing → worker
    outcome = (worker or default_worker)(task) or {}
    commit, tests = outcome.get("commit"), outcome.get("tests", 0)

    gitreins_task = create_gitreins_task(task, commit, gitreins_backend)  # lesson #2, post-commit
    judge_pid = None
    if commit:                                        # lesson #3: non-blocking judge
        judge_pid = run_judge_in_background(task, commit, judge_cmd, cwd=root).pid
    board.mark_complete(task.id, note=f"Implemented by worker{'; commit ' + commit if commit else ''}.",
                        commit=commit, tests=tests)
    return DispatchResult("worker_spawned", task.id, report, spawned_worker=True,
                          commit=commit, judge_pid=judge_pid, gitreins_task=gitreins_task)

Board note written in the skip path (the "verification note" the lesson demands):

Already implemented (pre-flight verified); worker NOT spawned.
git history: 2b4f46d — WORKFLOWS-01: LLM pipeline engine implemented
files present: tests/test_engine.py, workflows/engine.py
tests: 2/2 pass

create_gitreins_task emits {"type": "gitreins.check", "phase": "post-commit", "commit": <sha>, "policy": "policies/commit.yaml", ...}; foreman/judge.py writes judge_reports/<task>.json from a detached background process.

Evidence & signatures

Verified **live against real git repositories on disk** (no mocks): `git init`/`commit`, real `pytest` runs, and a real detached judge subprocess. **19/19 tests pass** (`python3 -m pytest tests/`), 3 consecutive runs, plus `py_compile` and import checks.

End-to-end incident replay (from the demo above):
- Board said `in_progress`; implementation commit existed → `dispatch` returned `action=marked_complete`, `spawned_worker=False`, **worker call list empty**, board note contains the commit hash, `tests: 2/2 pass`, and `files present: ...engine.py`. This is exactly the f7460a0 scenario, fixed.
- Truly missing task (`WORKFLOWS-02`) → worker spawned once, GitReins task created with `phase=post-commit`, background judge `pid=1129` wrote `judge_reports/WORKFLOWS-02.json` `{"verdict": "pass"}` while the foreman returned immediately.

Edge cases tested:
1. **History hit but no symbols/files** → not implemented (no false positive).
2. **History + symbols but red tests** (`engine.py` raises) → stays pending; green tests are mandatory, matching the incident's "16/16 pass" live check.
3. **No test command configured** → code evidence alone decides (documented fallback).
4. **Empty expected_symbols** → no false positive.
5. **`git log` with no commits / missing git** → returns `[]`, falls back safely.
6. **grep must not match `.git/` internals** → excluded via `--glob "!.git/**"` and skip-dirs.
7. **Worker with no commit** → judge skipped, no dangling PID.
8. **Board persistence round-trip** across "process restarts" (reload from JSON keeps `commit`/`tests`/`note`).
9. **Custom judge command** (e.g. containerized) supported.
10. **The original test bug**: a test asserted the commit hash contained the letter "f" — hash randomness, fixed to a hex regex (test-side, not product-side).

Files: `~/foreman/{__init__,core,judge}.py`, `~/tests/test_foreman.py`.
{"model": "deepseek-v4-flash", "problem_class": "python-foreman-task-already-implemented", "result": "passed", "tests": 19}
Generated from the verified corpus · MIT licensedBack to the catalog