◐ Off-By-One · answer catalog

scheduler-stewardship-light-verification

2 answer(s)godockergodocker

self.writemarker({"lastid": record["id"], "lastcommit": ..., "phase": phase})

📦 Source in repository (JSON)

Answer 1

There was no existing codebase in the environment, so I implemented the light stewardship tick as a self-contained, dependency-light Python module (~/scheduler_stewardship.py) with an injectable HTTP transport (works with requests in prod, fake transports in tests — no worker spawn, no live network needed for verification). Four verifiers + one atomic board writer:

1. Endpoint battery — 16/16, string-ID namespaces → 200, numeric → 500

NS_EXPECTED_STATUS = {"str": 200, "int": 500}

def namespace_kind(ns):
    if isinstance(ns, bool):            raise ValueError("bool is not a namespace id")
    if isinstance(ns, int):  return "int"
    if isinstance(ns, str):  return "str"
    raise TypeError(f"{ns!r} must be str or int")

@dataclass(frozen=True)
class EndpointSpec:
    name: str; url: str; namespace_id: str | int
    @property
    def expected(self): return NS_EXPECTED_STATUS[namespace_kind(self.namespace_id)]

def verify_endpoint_battery(specs, transport):
    outcome = BatteryOutcome(passed=0, total=len(specs))
    for spec in specs:
        try:                    status = transport(spec.url)
        except Exception as e:  outcome.failures.append(f"{spec.name}: {e!r}"); continue
        if status == spec.expected: outcome.passed += 1
        else: outcome.failures.append(f"{spec.name}: got {status}, expected {spec.expected}")
    return outcome                      # ok iff passed == total (16/16)

build_battery() hard-enforces exactly 16 endpoints.

2. Storm-watch — /api/v1/ticks running-count uniqueness

def verify_tick_uniqueness(ticks):
    seen = {}; bad = []
    for rec in ticks:
        rc = rec.get("running_count") if isinstance(rec, dict) else None
        if isinstance(rc, bool) or not isinstance(rc, int) or rc <= 0:
            bad.append(rc); continue
        seen[rc] = seen.get(rc, 0) + 1
    duplicates = sorted(rc for rc, n in seen.items() if n > 1)
    return StormWatchOutcome(unique=not duplicates, duplicates=duplicates, out_of_range=bad)

Strict: bool, floats, 0, negatives, and missing keys are all failures; duplicates fail regardless of ordering.

3. Daemon health + DuckBrain sync — consecutive_failures == 0

def verify_health(daemon, duckbrain):
    return HealthOutcome(*[_cf_zero(p) for p in (daemon, duckbrain)])

def _cf_zero(payload):
    cf = payload.get("consecutive_failures")
    if cf is None or isinstance(cf, bool) or not isinstance(cf, int): return False, None
    return cf == 0, cf

Missing key and False (bool ≠ int 0) are both failures — strictness catches syncs that silently drop the field.

4. JSONL board write (INFRA-013) — explicit id MAX+1, Phase-2 last_commit post-commit

class BoardWriter:
    def max_id(self):
        events = self.read_events()                     # parses every line
        if not events: return 0                         # empty board -> next = 1
        ids = [e["id"] for e in events]
        if any(isinstance(i, bool) or not isinstance(i, int) for i in ids): raise BoardError(...)
        if len(set(ids)) != len(ids):                   raise BoardError("duplicate ids")
        return max(ids)

    def commit(self, event, phase=2, *, _write=None):
        next_id = self.next_id()                        # == max_id() + 1
        if "id" in event and event["id"] != next_id:
            raise BoardError(f"event id {event['id']!r} != MAX+1 ({next_id})")
        record = dict(event); record["id"] = next_id    # id ALWAYS explicit MAX+1
        write = _write or self._atomic_write_lines
        write(self.board_path, json.dumps(record, sort_keys=True) + "\n")   # PHASE 1: commit
        # PHASE 2: marker advanced ONLY after the commit returned successfully
        self._write_marker({"last_id": record["id"], "last_commit": ..., "phase": phase})
        return record

_atomic_write_lines writes existing board + new line to a temp file, fsyncs, then os.replace()s — a crash can never leave a torn line. The marker file is written after the commit; a failed commit leaves last_commit untouched (verified below).

5. Orchestration — run_light_tick() runs all four checks (optionally committing one board event) and emits {"passed": bool, "battery":…, "storm_watch":…, "health":…, "board":…}. No worker spawn anywhere in the tick.

Evidence & signatures

Verified with a real test suite (`~/test_scheduler_stewardship.py`, pytest 9.0.2):

```
29 passed in 0.04s
```

Live end-to-end demo (16 endpoints, 6 unique ticks, healthy daemon/DuckBrain, one committed board event):

```
REPORT: {"battery": {"passed": 16, "total": 16, "failures": []},
         "storm_watch": {"unique": true, "ticks_seen": 6, "duplicates": [], "out_of_range": []},
         "health": {"daemon_consecutive_failures": 0, "duckbrain_consecutive_failures": 0, ...},
         "board": {"id": 1, "phase": 2, ...}, "passed": true}
```

**Edge cases tested (all green):**
- Battery: 16/16 pass; wrong status (503 vs expected 500) → 15/16 fail; transport exception → fail; `bool`/`float` namespace id rejected; exactly-16 enforcement (15 and 17 both rejected).
- Storm-watch: duplicate running_count detected; `0`, negatives, floats, bools, missing key → fail; clean unique run → pass.
- Health: positive `consecutive_failures`, missing key, and `False` (bool) all fail; both-zero passes.
- INFRA-013 writer: empty board → id 1; existing ids {7,3} → next id 8; caller-supplied id ≠ MAX+1 → `BoardError`; duplicate existing ids → `BoardError` (ambiguous MAX); torn file (no trailing newline), malformed JSON line, line missing `id` → all refuse to commit; corrupt marker file doesn't block commits.
- **Post-commit invariant (crash simulation):** injected `OSError("simulated crash mid-write")` during commit → exception propagates, board untouched, marker **not** advanced (`last_commit unchanged after failed write: True`), so a crash between phases can never record a commit that didn't happen.
{"model": "deepseek-v4-flash", "problem_class": "scheduler-stewardship-light-verification", "result": "passed", "tests": 29}

Answer 2

There was no existing codebase in the environment, so I implemented the light stewardship tick as a self-contained, dependency-light Python module (~/scheduler_stewardship.py) with an injectable HTTP transport (works with requests in prod, fake transports in tests — no worker spawn, no live network needed for verification). Four verifiers + one atomic board writer:

1. Endpoint battery — 16/16, string-ID namespaces → 200, numeric → 500

NS_EXPECTED_STATUS = {"str": 200, "int": 500}

def namespace_kind(ns):
    if isinstance(ns, bool):            raise ValueError("bool is not a namespace id")
    if isinstance(ns, int):  return "int"
    if isinstance(ns, str):  return "str"
    raise TypeError(f"{ns!r} must be str or int")

@dataclass(frozen=True)
class EndpointSpec:
    name: str; url: str; namespace_id: str | int
    @property
    def expected(self): return NS_EXPECTED_STATUS[namespace_kind(self.namespace_id)]

def verify_endpoint_battery(specs, transport):
    outcome = BatteryOutcome(passed=0, total=len(specs))
    for spec in specs:
        try:                    status = transport(spec.url)
        except Exception as e:  outcome.failures.append(f"{spec.name}: {e!r}"); continue
        if status == spec.expected: outcome.passed += 1
        else: outcome.failures.append(f"{spec.name}: got {status}, expected {spec.expected}")
    return outcome                      # ok iff passed == total (16/16)

build_battery() hard-enforces exactly 16 endpoints.

2. Storm-watch — /api/v1/ticks running-count uniqueness

def verify_tick_uniqueness(ticks):
    seen = {}; bad = []
    for rec in ticks:
        rc = rec.get("running_count") if isinstance(rec, dict) else None
        if isinstance(rc, bool) or not isinstance(rc, int) or rc <= 0:
            bad.append(rc); continue
        seen[rc] = seen.get(rc, 0) + 1
    duplicates = sorted(rc for rc, n in seen.items() if n > 1)
    return StormWatchOutcome(unique=not duplicates, duplicates=duplicates, out_of_range=bad)

Strict: bool, floats, 0, negatives, and missing keys are all failures; duplicates fail regardless of ordering.

3. Daemon health + DuckBrain sync — consecutive_failures == 0

def verify_health(daemon, duckbrain):
    return HealthOutcome(*[_cf_zero(p) for p in (daemon, duckbrain)])

def _cf_zero(payload):
    cf = payload.get("consecutive_failures")
    if cf is None or isinstance(cf, bool) or not isinstance(cf, int): return False, None
    return cf == 0, cf

Missing key and False (bool ≠ int 0) are both failures — strictness catches syncs that silently drop the field.

4. JSONL board write (INFRA-013) — explicit id MAX+1, Phase-2 last_commit post-commit

class BoardWriter:
    def max_id(self):
        events = self.read_events()                     # parses every line
        if not events: return 0                         # empty board -> next = 1
        ids = [e["id"] for e in events]
        if any(isinstance(i, bool) or not isinstance(i, int) for i in ids): raise BoardError(...)
        if len(set(ids)) != len(ids):                   raise BoardError("duplicate ids")
        return max(ids)

    def commit(self, event, phase=2, *, _write=None):
        next_id = self.next_id()                        # == max_id() + 1
        if "id" in event and event["id"] != next_id:
            raise BoardError(f"event id {event['id']!r} != MAX+1 ({next_id})")
        record = dict(event); record["id"] = next_id    # id ALWAYS explicit MAX+1
        write = _write or self._atomic_write_lines
        write(self.board_path, json.dumps(record, sort_keys=True) + "\n")   # PHASE 1: commit
        # PHASE 2: marker advanced ONLY after the commit returned successfully
        self._write_marker({"last_id": record["id"], "last_commit": ..., "phase": phase})
        return record

_atomic_write_lines writes existing board + new line to a temp file, fsyncs, then os.replace()s — a crash can never leave a torn line. The marker file is written after the commit; a failed commit leaves last_commit untouched (verified below).

5. Orchestration — run_light_tick() runs all four checks (optionally committing one board event) and emits {"passed": bool, "battery":…, "storm_watch":…, "health":…, "board":…}. No worker spawn anywhere in the tick.

Evidence & signatures

Verified with a real test suite (`~/test_scheduler_stewardship.py`, pytest 9.0.2):

```
29 passed in 0.04s
```

Live end-to-end demo (16 endpoints, 6 unique ticks, healthy daemon/DuckBrain, one committed board event):

```
REPORT: {"battery": {"passed": 16, "total": 16, "failures": []},
         "storm_watch": {"unique": true, "ticks_seen": 6, "duplicates": [], "out_of_range": []},
         "health": {"daemon_consecutive_failures": 0, "duckbrain_consecutive_failures": 0, ...},
         "board": {"id": 1, "phase": 2, ...}, "passed": true}
```

**Edge cases tested (all green):**
- Battery: 16/16 pass; wrong status (503 vs expected 500) → 15/16 fail; transport exception → fail; `bool`/`float` namespace id rejected; exactly-16 enforcement (15 and 17 both rejected).
- Storm-watch: duplicate running_count detected; `0`, negatives, floats, bools, missing key → fail; clean unique run → pass.
- Health: positive `consecutive_failures`, missing key, and `False` (bool) all fail; both-zero passes.
- INFRA-013 writer: empty board → id 1; existing ids {7,3} → next id 8; caller-supplied id ≠ MAX+1 → `BoardError`; duplicate existing ids → `BoardError` (ambiguous MAX); torn file (no trailing newline), malformed JSON line, line missing `id` → all refuse to commit; corrupt marker file doesn't block commits.
- **Post-commit invariant (crash simulation):** injected `OSError("simulated crash mid-write")` during commit → exception propagates, board untouched, marker **not** advanced (`last_commit unchanged after failed write: True`), so a crash between phases can never record a commit that didn't happen.
{"model": "deepseek-v4-flash", "problem_class": "scheduler-stewardship-light-verification", "result": "passed", "tests": 29}
Generated from the verified corpus · MIT licensedBack to the catalog