mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-10-11 03:38:07 +00:00
* test(release): add fixed-answer native tool accuracy
* fix(release): gate stable artifacts on pinned paired evaluations
Apply review findings #1-4: gate Docker publication, grade in-flight retries, preserve repository context, and execute negative controls in CI.
* fix(eval): verify native context and resolve harness security findings
Inspect Claude's first startup reminder for ordinary repository guidance.
Bound launcher-option matching and escape Markdown backslashes and pipes.
Exclude only the parsed synthetic accuracy corpus from CodeQL; keep the
benchmark harness and evaluator under security analysis.
Validated focused Python and TypeScript regressions plus core typecheck.
* fix(eval): wake the EC2 runner and prove paired evaluator execution
Bootstrap the existing dedicated instance from a protected hosted job, bound
native runner pickup, and stop only instances started by this run. Keep paid
sessions inside the fixed stop window and add a runner-only dispatch.
Require a contained six-cell prepare/session/oracle/report canary in CI,
including a failed repair that remains in the measured denominator.
* fix(ci): reuse existing configuration and disambiguate accuracy cases
Remove the new AWS credential/settings path and release opt-in variable. Bound native runner pickup with the existing GitHub token and retain private EventBridge startup until its existing mechanism can be reused. Reuse current workflow environment names and evolution schedule controls.
Name malformed-observation cases explicitly so the execution audit distinguishes null from missing and actually tests invalid arrays.
* fix(review): isolate release builds and preserve benchmark evidence
* fix(release): gate RC publication on paired quality and manage EC2 lifecycle
* fix(ci): clarify release shell redirects and build script
* fix(eval): remove temporary patch sinks after capture
* fix(bench): retire tool-accuracy allowances repaired on main
Main at 50aa4be3b repaired three #3487 route checks (#3505) and all four
#3499 Python scope checks (#3502, #3504). The ratchet correctly failed the
merged head with stale allowances, so remove them and keep the fixed answers
as release protection: 18/31 pass, 13 recorded gaps remain.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(release): gate RCs on accuracy only and keep paid runs for stable
Follow the cost split proposed in discussion #3493: fixed-answer tool
accuracy on every RC, paired agent runs before stable releases and on a
weekly schedule against the latest RC.
RC publication no longer prepares a bundle, calls release-evaluation.yml
and waits for a paid run of up to 21 hours on the shared EC2 lock. Every
release attaches this run's accuracy report; stable npm publication still
requires exact-commit paired evidence. release-evaluation.yml drops its
workflow_call path and the now-unused candidate bundle module.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(release): address PR #3503 review feedback
- Page through recent release-evaluation runs instead of reading only
the newest 50, so valid stable evidence cannot be pushed off page one.
- Bound each runner-pickup jobs request by the remaining deadline.
- Replace non-finite floats at any depth in failure receipts so the
incomplete report still serializes with allow_nan=False.
- Clarify that the summary.cap case has 501 names over 1001 sites.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(review): harden candidate build and close review gaps
- Mount the candidate checkout's .git read-only inside the build sandbox,
so lifecycle scripts cannot plant git config (e.g. core.fsmonitor) that
host git later runs in that checkout during actions/checkout cleanup.
The real-Bubblewrap canary now also attempts that write.
- Type tool-accuracy observations instead of using explicit any.
- Cover the fail-closed branches reviewers found untested: unsafe or
missing evidence artifacts, mismatched task pins, malformed repo/SHA,
an instance that stops again during bootstrap, and unbalanced GitNexus
guidance markers.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* docs(release): describe RC accuracy gate and stable evidence step
- CONTRIBUTING now states that RCs are gated by CI (including the
fixed-answer tool-accuracy check) without a paid run, and that stable
publication needs a passing Release evaluation for the exact commit
within seven days.
- The oracle-control fixture passes tarfile's data filter only where it
exists (3.11.4+), matching the declared Python 3.11 floor.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* refactor(bench): pin tool-accuracy fixture line anchors in one place
run.ts bucketed rename edits and explain findings by line literals that
expectations.ts duplicated. Export FIXTURE_ANCHORS (file, line, pinned text)
from expectations.ts, build the fixed answers and run.ts classifiers from it,
and fail loudly (runner and unit test) when a fixture line no longer holds
the pinned text. Expected answers and observations are unchanged.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(bench): fail closed when ripgrep is missing from the tool-accuracy run
rename's text-search pass shells out to rg and only logs a degraded warning
when it is absent, so a runner without ripgrep scored a degraded rename
(homonym.ts:1 missing) and still passed. Check rg --version before indexing,
record it in accuracy.json source.externalTools, and install ripgrep in the
ci-tests tool-accuracy job and the docker release-gate (same if: condition).
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* refactor(eval): split baseline guidance, share bwrap preamble, drop dead bare option, harden patch cleanup
- Move the baseline (no-GitNexus) guidance scrubbing out of proposer_sandbox.py
into baseline_guidance.py, with its unit tests.
- Expose real_directory, runtime_mount_args and bwrap_base_args publicly so
release_build no longer imports private helpers or re-assembles the shared
bubblewrap preamble; the candidate build command is byte-identical.
- Remove the unused run_claude `bare` parameter and its --bare/--tools branches.
- capture_patch: a failed temporary-directory removal no longer discards the
read patch or replaces an earlier error; it is attached as a note, or warned
on stderr when there was no earlier error.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(ec2-runner): keep stop observing through transient describe failures
- stop() treats a failed or timed-out describe-instances call as unknown
state and keeps observing until the stop deadline; a persistent failure
is still raised at the deadline with the last command error. Unusable
states and identity mismatches still fail at once (new CommandError
subclass separates command failures from semantic ones).
- Readiness CLI has one paid mode: --job-name NAME. --evolve is removed;
skill evolution passes its job name, and tests tie the watchdog command
to the paid job's actual name in both EC2 workflows.
- Jobs-API requests never get a timeout larger than the remaining pickup
budget (previously rounded up to 1s past the deadline).
- Document that the shared concurrency group keeps only one pending run,
so a queued release evaluation can be replaced and must be confirmed.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* docs(eval): record verified EC2 lifecycle configuration
The README still described the AWS settings as assumed and unverified.
The OIDC role, environment settings and runner_only lifecycle were
configured and verified live on 2026-10-09.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(release-eval): grade both runtimes against one pinned task toolchain
Candidate and stable each staged task dependencies (node_modules,
gitnexus-shared/dist) from their own runtime checkout, so the same v1.6.12
task source was graded with different TypeScript, Vitest and LadybugDB
versions and a per-task solve delta also measured dependency drift.
- Check out and build the pinned task commit once (tasks-base) and point
every task's repo at it for both runtimes; the runtime under test reaches
sessions only through --gitnexus-root. prepare requires --task-repo at the
single pinned task commit (new task-sha subcommand resolves it).
- Retain sandbox_dependency_content_digest per measured row and make
release_gate fail when any task's cells, across candidate and stable, do not
share one recorded dependency digest.
- Move the task-pin comparison into validate_report(task_pins=...) and use
it from release_report check and release_gate.
- Cover the gate CLI with a missing and a corrupt candidate report.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* docs(ci): warn that a pending release evaluation can be replaced
GitHub keeps one pending run per concurrency group, so a newer queued
EC2 run cancels a pending release evaluation. Operators must confirm the
startup job ran before relying on the evidence.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* test(eval): prove hidden oracles against the grading toolchain
Release evaluation now grades every task with dependencies built at the
pinned task commit (TypeScript 5.9.3, Vitest 4.1.11), but the oracle
controls still ran against the current checkout's toolchain. The Ubuntu
containment job now builds the pinned task toolchain and points the
controls at it. All seven controls pass locally against that build.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* test(ci): pin the duplicated EC2 lifecycle jobs together
Both EC2 workflows carry their own copy of the live-verified start,
check, readiness and stop jobs. A reusable workflow would change that
verified job structure, so instead a test requires the copies to stay
identical apart from the job display name and the paid-job dependency.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(release-eval): accept stable evidence only from main's current evaluator
Stable publish validated downloaded agent evidence against the release
commit's eval/ tree and never tied a report to the run that produced it.
Evidence from an older evaluator stayed acceptable for seven days after
graders changed, while task changes on main made every fresh evaluation
mismatch the tag. The first invalid artifact also hid older valid runs.
The composite action now extracts eval/ from main's head and runs the
validator there with locked base dependencies only (no dev extras or
project build). A report counts only when its harness_sha is the head_sha
of its trusted main run and the compare API shows no change under eval/,
the release-evaluation workflow or the pinned agent CLI between that
harness and the evaluator; a missing, diverged or 300-file comparison
fails closed. Runs are tried newest first, rejected runs are skipped
with a bounded reason list, every gh call has a timeout, and task pins
use validate_report's task_pins check.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(eval): break the sandbox import cycle and resolve code-scanning alerts
- baseline_guidance is now pure text handling with its own GuidanceError;
the baseline mount builder lives in proposer_sandbox again and converts
that error to SandboxError, so imports run one way only.
- task_assets uses the public real_directory name; the unused private
alias is gone.
- The oracle-control fixture always extracts with tarfile's data filter
and skips on Pythons that lack it, instead of extracting unfiltered.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(release-eval): run candidate lifecycle scripts offline; reject smuggled report fields
- build_candidate now downloads locked dependencies with --ignore-scripts,
then runs every lifecycle script (dependency installs, prepare, build)
in a fresh network namespace, so candidate code cannot reach host-local
services. Verified locally that the current head and the pinned task
commit build this way; the real-Bubblewrap canary now asserts only
loopback is visible to a lifecycle script.
- validate_report requires the evidence document to equal its
field-whitelisted rebuild, so per-run or top-level fields outside the
published schema are rejected before the publisher copies the file.
- The sandbox test suite also covers a no-GitNexus sandbox refusing
unbalanced guidance markers.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* docs(eval): describe the offline candidate lifecycle
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(release-eval): pin downloads to the npm registry; require dependency binding
Address review feedback on PR #3503:
- The candidate's online install phase now refuses lockfile entries that
resolve outside https://registry.npmjs.org/ (other than in-checkout
workspace links) and refuses shipped .npmrc files, so a candidate
cannot steer npm at loopback, private or metadata endpoints.
- Every measured row must carry a 64-hex task-dependency digest, so a
single report cannot be accepted with its grading toolchain unbound.
- Oversized JSON integers mark a measurement untrustworthy instead of
crashing report generation.
- Baseline guidance stripping recognises headings with up to three
leading spaces (CommonMark).
- Remove a stale comment about the old private helper name.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(eval): parse CommonMark ATX headings; compare evidence by exact JSON
- Baseline guidance stripping now parses headings as CommonMark ATX
headings (0-3 leading spaces, optional closing # run, tabs), so a
GitNexus section heading like '## GitNexus rules ##' is removed too.
- Evidence validation compares the serialized rebuild with the report,
so a retyped value (true for 1, 2.0 for 2) no longer passes.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(eval): drop continuations of every CommonMark list item in baseline guidance
A removed GitNexus instruction left its indented continuation behind when
the item used '+' or an ordered marker (1. / 1)). The scrubber now
recognises every CommonMark list-item marker.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(eval): isolate candidate MCP from agent task writes (#3503)
Run MCP in a nested Bubblewrap boundary with read-only task, graph,
registry and runtime mounts, private state and isolated processes/network.
Keep task writes on the agent's built-in tools and remove mutating MCP grants.
Add startup/tool mutation canaries and preserve explicit unsafe diagnostics.
Validation: focused pytest 138 passed, 16 skipped. Full locked evaluator
996 passed, 29 skipped; real containment canaries require Ubuntu CI.
Note: eight pre-existing comparator-reuse failures reproduce on the
unchanged head due to missing os.supports_dir_fd support. One process-control
timeout failed in the full run and passed alone and on the unchanged head.
* fix(eval): close baseline mount overlap and test symlink evidence (#3503)
Reject supplied no-MCP mounts that cover forbidden GitNexus paths, including ancestor mounts and lexical aliases. Keep ordinary dependency mounts available.
Exercise evidence symlink rejection with a real valid-report target and retain dangling-link coverage.
Validation: 1026 evaluator tests passed, 29 skipped; targeted mount tests 36 passed and evidence tests 34 passed. Removing the symlink guard in memory makes the repaired regression fail. Ruff and diff checks passed.
Note: eight pre-existing comparator-reuse failures remain in this environment because os.supports_dir_fd lacks the required os.lstat support.
---------
Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
425 lines
17 KiB
Python
425 lines
17 KiB
Python
"""Exercise the dedicated EC2 lifecycle without accessing AWS."""
|
|
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from datetime import UTC, datetime, timedelta
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
SCRIPT = Path(__file__).resolve().parents[2] / ".github/scripts/evolution-runner.py"
|
|
spec = importlib.util.spec_from_file_location("evolution_runner", SCRIPT)
|
|
lifecycle = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(lifecycle)
|
|
|
|
CONFIG = {
|
|
"AWS_REGION": "us-east-1",
|
|
"AWS_ROLE_ARN": "arn:aws:iam::123456789012:role/evolution",
|
|
"EC2_INSTANCE_ID": "i-0123456789abcdef0",
|
|
"STOP_SCHEDULE_UTC": "SUN 03:00",
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def configured(monkeypatch, tmp_path):
|
|
for key, value in CONFIG.items():
|
|
monkeypatch.setenv(lifecycle.PREFIX + key, value)
|
|
output = tmp_path / "outputs"
|
|
monkeypatch.setenv("GITHUB_OUTPUT", str(output))
|
|
return output
|
|
|
|
|
|
def driver(monkeypatch, configured, states, *, statuses=None, errors=None):
|
|
calls = []
|
|
clock = [0.0]
|
|
errors = errors or {}
|
|
monkeypatch.setattr(lifecycle.time, "monotonic", lambda: clock[0])
|
|
monkeypatch.setattr(lifecycle.time, "sleep", lambda value: clock.__setitem__(0, clock[0] + value))
|
|
monkeypatch.setattr(lifecycle, "stop_deadline", lambda schedule: 2000000000)
|
|
|
|
def run(command, **kwargs):
|
|
operation = command[2]
|
|
calls.append(operation)
|
|
assert command[3:7] == ["--instance-ids", CONFIG["EC2_INSTANCE_ID"], "--region", CONFIG["AWS_REGION"]]
|
|
assert 0 < kwargs["timeout"] <= 30
|
|
assert kwargs["capture_output"] and kwargs["text"]
|
|
if operation == "start-instances":
|
|
assert "cleanup_required=true" in configured.read_text()
|
|
problem = errors.get(operation)
|
|
if isinstance(problem, list):
|
|
# A list scripts one outcome per call; None lets that call succeed.
|
|
problem = problem.pop(0) if problem else None
|
|
if problem:
|
|
if isinstance(problem, BaseException):
|
|
raise problem
|
|
return SimpleNamespace(returncode=1, stdout="", stderr="private account details")
|
|
if operation == "describe-instances":
|
|
state = states.pop(0) if len(states) > 1 else states[0]
|
|
response = {
|
|
"Reservations": [{"Instances": [{"InstanceId": CONFIG["EC2_INSTANCE_ID"], "State": {"Name": state}}]}]
|
|
}
|
|
elif operation == "describe-instance-status":
|
|
status = statuses.pop(0) if statuses and len(statuses) > 1 else (statuses or ["ok"])[0]
|
|
response = {
|
|
"InstanceStatuses": [
|
|
{
|
|
"InstanceId": CONFIG["EC2_INSTANCE_ID"],
|
|
"SystemStatus": {"Status": status},
|
|
"InstanceStatus": {"Status": status},
|
|
}
|
|
]
|
|
}
|
|
else:
|
|
response = {}
|
|
return SimpleNamespace(returncode=0, stdout=json.dumps(response), stderr="")
|
|
|
|
monkeypatch.setattr(lifecycle.subprocess, "run", run)
|
|
return calls, clock
|
|
|
|
|
|
def test_start_waits_for_running_and_both_health_checks(monkeypatch, configured):
|
|
calls, _ = driver(
|
|
monkeypatch, configured, ["stopped", "pending", "running", "running"], statuses=["initializing", "ok"]
|
|
)
|
|
lifecycle.start(CONFIG, timeout=50)
|
|
assert calls.count("start-instances") == 1
|
|
assert calls.count("describe-instance-status") == 2
|
|
assert "cleanup_required=true" in configured.read_text()
|
|
assert "stop_deadline_epoch=2000000000" in configured.read_text()
|
|
|
|
|
|
def test_start_waits_out_an_existing_shutdown_before_restart(monkeypatch, configured):
|
|
calls, _ = driver(monkeypatch, configured, ["stopping", "stopped", "pending", "running"])
|
|
lifecycle.start(CONFIG, timeout=50)
|
|
assert calls.count("start-instances") == 1
|
|
assert calls.index("start-instances") == 2
|
|
|
|
|
|
def test_closed_startup_window_cannot_change_power_state(monkeypatch, configured):
|
|
calls, _ = driver(monkeypatch, configured, ["stopped"])
|
|
|
|
def closed_window(schedule):
|
|
raise ValueError("insufficient time")
|
|
|
|
monkeypatch.setattr(lifecycle, "stop_deadline", closed_window)
|
|
with pytest.raises(ValueError, match="insufficient time"):
|
|
lifecycle.start(CONFIG)
|
|
assert calls == []
|
|
assert not configured.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("initial", ["running", "pending"])
|
|
def test_existing_active_dedicated_instance_is_claimed_for_cleanup(monkeypatch, configured, initial):
|
|
calls, _ = driver(monkeypatch, configured, [initial, "running"])
|
|
lifecycle.start(CONFIG, timeout=30)
|
|
assert "start-instances" not in calls
|
|
assert "cleanup_required=true" in configured.read_text()
|
|
|
|
|
|
def test_start_response_lost_still_attempts_stop_and_waits_for_stopped(monkeypatch, configured):
|
|
problem = subprocess.TimeoutExpired(["aws", CONFIG["EC2_INSTANCE_ID"]], 30, stderr="private details")
|
|
calls, _ = driver(
|
|
monkeypatch,
|
|
configured,
|
|
["stopped", "pending", "running", "stopping", "stopped"],
|
|
errors={"start-instances": problem},
|
|
)
|
|
with pytest.raises(RuntimeError, match="start-instances") as exc:
|
|
lifecycle.start(CONFIG, timeout=60)
|
|
assert "cleanup_required=true" in configured.read_text()
|
|
assert "stop_deadline_epoch" not in configured.read_text()
|
|
assert "stop-instances" in calls
|
|
assert calls[-1] == "describe-instances"
|
|
assert "private" not in str(exc.value)
|
|
assert CONFIG["EC2_INSTANCE_ID"] not in str(exc.value)
|
|
|
|
|
|
def test_instance_that_stops_again_after_start_fails_without_a_second_start(monkeypatch, configured):
|
|
calls, _ = driver(monkeypatch, configured, ["stopped", "pending", "stopped"])
|
|
with pytest.raises(RuntimeError, match="stopped again during bootstrap"):
|
|
lifecycle.start(CONFIG, timeout=60)
|
|
assert calls == [
|
|
"describe-instances",
|
|
"start-instances",
|
|
"describe-instances",
|
|
"describe-instances",
|
|
"describe-instances",
|
|
]
|
|
assert "cleanup_required=true" in configured.read_text()
|
|
assert "stop_deadline_epoch" not in configured.read_text()
|
|
|
|
|
|
def test_denied_start_still_cleans_up_claimed_instance(monkeypatch, configured):
|
|
calls, _ = driver(monkeypatch, configured, ["stopped", "stopped"], errors={"start-instances": "denied"})
|
|
with pytest.raises(RuntimeError, match="start-instances") as exc:
|
|
lifecycle.start(CONFIG, timeout=30)
|
|
assert calls == ["describe-instances", "start-instances", "describe-instances"]
|
|
assert "private" not in str(exc.value)
|
|
assert "cleanup_required=true" in configured.read_text()
|
|
|
|
|
|
def test_health_failure_attempts_bounded_cleanup(monkeypatch, configured, capsys):
|
|
calls, clock = driver(monkeypatch, configured, ["running"], statuses=["impaired"])
|
|
with pytest.raises(RuntimeError, match="startup/health deadline"):
|
|
lifecycle.start(CONFIG, timeout=20, cleanup_timeout=25)
|
|
assert "stop-instances" in calls
|
|
assert clock[0] == 45
|
|
assert "cleanup also failed" in capsys.readouterr().out
|
|
assert "stop_deadline_epoch" not in configured.read_text()
|
|
|
|
|
|
def test_stop_waits_for_pending_then_confirms_stopped(monkeypatch, configured):
|
|
calls, _ = driver(monkeypatch, configured, ["pending", "running", "stopping", "stopped"])
|
|
lifecycle.stop(CONFIG, timeout=40)
|
|
assert calls == [
|
|
"describe-instances",
|
|
"describe-instances",
|
|
"stop-instances",
|
|
"describe-instances",
|
|
"describe-instances",
|
|
]
|
|
|
|
|
|
def test_stop_is_idempotent(monkeypatch, configured):
|
|
calls, _ = driver(monkeypatch, configured, ["stopped"])
|
|
lifecycle.stop(CONFIG, timeout=20)
|
|
assert calls == ["describe-instances"]
|
|
|
|
|
|
def test_stop_command_acceptance_does_not_prove_stopped(monkeypatch, configured):
|
|
calls, clock = driver(monkeypatch, configured, ["running", "stopping"])
|
|
with pytest.raises(RuntimeError, match="shutdown deadline"):
|
|
lifecycle.stop(CONFIG, timeout=25)
|
|
assert calls.count("stop-instances") == 1
|
|
assert clock[0] == 25
|
|
|
|
|
|
def test_perpetually_pending_instance_has_bounded_shutdown(monkeypatch, configured):
|
|
calls, clock = driver(monkeypatch, configured, ["pending"])
|
|
with pytest.raises(RuntimeError, match="shutdown deadline"):
|
|
lifecycle.stop(CONFIG, timeout=25)
|
|
assert "stop-instances" not in calls
|
|
assert clock[0] == 25
|
|
|
|
|
|
def test_stop_lost_response_can_still_confirm_stopped(monkeypatch, configured):
|
|
calls, _ = driver(
|
|
monkeypatch,
|
|
configured,
|
|
["running", "stopping", "stopped"],
|
|
errors={"stop-instances": subprocess.TimeoutExpired("aws", 30)},
|
|
)
|
|
lifecycle.stop(CONFIG, timeout=40)
|
|
assert calls.count("stop-instances") == 1
|
|
|
|
|
|
@pytest.mark.parametrize("problem", [subprocess.TimeoutExpired(["aws", CONFIG["EC2_INSTANCE_ID"]], 30), "denied"])
|
|
def test_stop_keeps_observing_after_a_transient_describe_failure(monkeypatch, configured, problem):
|
|
calls, clock = driver(
|
|
monkeypatch, configured, ["running", "stopping", "stopped"], errors={"describe-instances": [problem]}
|
|
)
|
|
lifecycle.stop(CONFIG, timeout=60)
|
|
assert calls == [
|
|
"describe-instances",
|
|
"describe-instances",
|
|
"stop-instances",
|
|
"describe-instances",
|
|
"describe-instances",
|
|
]
|
|
assert clock[0] == 30
|
|
|
|
|
|
@pytest.mark.parametrize("problem", [subprocess.TimeoutExpired(["aws", CONFIG["EC2_INSTANCE_ID"]], 30), "denied"])
|
|
def test_persistent_describe_failure_still_raises_at_the_stop_deadline(monkeypatch, configured, problem):
|
|
calls, clock = driver(monkeypatch, configured, ["running"], errors={"describe-instances": problem})
|
|
with pytest.raises(RuntimeError, match="shutdown deadline expired after: EC2 describe-instances") as exc:
|
|
lifecycle.stop(CONFIG, timeout=25)
|
|
assert calls == ["describe-instances"] * 3
|
|
assert clock[0] == 25
|
|
assert "private" not in str(exc.value)
|
|
assert CONFIG["EC2_INSTANCE_ID"] not in str(exc.value)
|
|
|
|
|
|
def test_denied_stop_fails_bounded_without_private_diagnostics(monkeypatch, configured):
|
|
_, clock = driver(monkeypatch, configured, ["running"], errors={"stop-instances": "denied"})
|
|
with pytest.raises(RuntimeError, match="shutdown deadline") as exc:
|
|
lifecycle.stop(CONFIG, timeout=20)
|
|
assert clock[0] == 20
|
|
assert "private" not in str(exc.value)
|
|
|
|
|
|
@pytest.mark.parametrize("operation", ["start", "stop"])
|
|
@pytest.mark.parametrize("state", ["terminated", "shutting-down", "unknown"])
|
|
def test_unusable_state_fails_loud_without_power_changes(monkeypatch, configured, operation, state):
|
|
calls, _ = driver(monkeypatch, configured, [state])
|
|
with pytest.raises(RuntimeError, match="state"):
|
|
getattr(lifecycle, operation)(CONFIG, timeout=20)
|
|
assert calls == ["describe-instances"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"response",
|
|
[
|
|
{"Reservations": []},
|
|
{"Reservations": [{"Instances": [{"InstanceId": "i-fffffffffffffffff", "State": {"Name": "running"}}]}]},
|
|
{
|
|
"Reservations": [
|
|
{"Instances": [{"InstanceId": CONFIG["EC2_INSTANCE_ID"]}, {"InstanceId": CONFIG["EC2_INSTANCE_ID"]}]}
|
|
]
|
|
},
|
|
],
|
|
)
|
|
def test_instance_identity_must_match_exactly(monkeypatch, response):
|
|
monkeypatch.setattr(lifecycle, "aws", lambda *args, **kwargs: response)
|
|
with pytest.raises(RuntimeError, match="exactly the configured instance"):
|
|
lifecycle.instance_state(CONFIG)
|
|
|
|
|
|
@pytest.mark.parametrize("field", ["SystemStatus", "InstanceStatus"])
|
|
def test_both_health_checks_are_required(monkeypatch, field):
|
|
response = {
|
|
"InstanceStatuses": [
|
|
{
|
|
"InstanceId": CONFIG["EC2_INSTANCE_ID"],
|
|
"SystemStatus": {"Status": "ok"},
|
|
"InstanceStatus": {"Status": "ok"},
|
|
}
|
|
]
|
|
}
|
|
response["InstanceStatuses"][0][field]["Status"] = "impaired"
|
|
monkeypatch.setattr(lifecycle, "aws", lambda *args, **kwargs: response)
|
|
assert not lifecycle.healthy(CONFIG)
|
|
|
|
|
|
def test_health_response_cannot_substitute_another_instance(monkeypatch):
|
|
response = {
|
|
"InstanceStatuses": [
|
|
{"InstanceId": "i-fffffffffffffffff", "SystemStatus": {"Status": "ok"}, "InstanceStatus": {"Status": "ok"}}
|
|
]
|
|
}
|
|
monkeypatch.setattr(lifecycle, "aws", lambda *args, **kwargs: response)
|
|
with pytest.raises(RuntimeError, match="configured instance"):
|
|
lifecycle.healthy(CONFIG)
|
|
|
|
|
|
def test_aws_command_timeout_never_exceeds_remaining_poll_budget(monkeypatch):
|
|
monkeypatch.setattr(lifecycle.time, "monotonic", lambda: 10)
|
|
|
|
def run(command, **kwargs):
|
|
assert kwargs["timeout"] == 2
|
|
return SimpleNamespace(returncode=0, stdout="{}")
|
|
|
|
monkeypatch.setattr(lifecycle.subprocess, "run", run)
|
|
assert lifecycle.aws(CONFIG, "describe-instances", until=12) == {}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"result",
|
|
[
|
|
SimpleNamespace(returncode=1, stdout="", stderr="private details"),
|
|
SimpleNamespace(returncode=0, stdout="private details"),
|
|
],
|
|
)
|
|
def test_aws_errors_do_not_echo_response(monkeypatch, result):
|
|
monkeypatch.setattr(lifecycle.subprocess, "run", lambda *args, **kwargs: result)
|
|
with pytest.raises(RuntimeError) as exc:
|
|
lifecycle.aws(CONFIG, "describe-instances")
|
|
assert "private" not in str(exc.value)
|
|
|
|
|
|
def test_missing_aws_cli_returns_safe_error(monkeypatch):
|
|
def missing(*args, **kwargs):
|
|
raise FileNotFoundError("private/path/aws")
|
|
|
|
monkeypatch.setattr(lifecycle.subprocess, "run", missing)
|
|
with pytest.raises(RuntimeError, match="command failed") as exc:
|
|
lifecycle.aws(CONFIG, "describe-instances")
|
|
assert "private" not in str(exc.value)
|
|
|
|
|
|
def test_command_is_not_started_after_deadline(monkeypatch):
|
|
monkeypatch.setattr(lifecycle.time, "monotonic", lambda: 10)
|
|
|
|
def unexpected(*args, **kwargs):
|
|
pytest.fail("AWS command ran after deadline")
|
|
|
|
monkeypatch.setattr(lifecycle.subprocess, "run", unexpected)
|
|
with pytest.raises(RuntimeError, match="deadline expired"):
|
|
lifecycle.aws(CONFIG, "describe-instances", until=10)
|
|
|
|
|
|
def test_configuration_reuses_only_historical_names(configured):
|
|
assert lifecycle.configuration() == CONFIG
|
|
|
|
|
|
@pytest.mark.parametrize("key", CONFIG)
|
|
def test_missing_configuration_fails_closed(monkeypatch, configured, key):
|
|
monkeypatch.delenv(lifecycle.PREFIX + key)
|
|
with pytest.raises(ValueError, match=lifecycle.PREFIX + key):
|
|
lifecycle.configuration()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("key", "value"),
|
|
[
|
|
("AWS_REGION", "us-east-1;echo bad"),
|
|
("AWS_ROLE_ARN", "arbitrary"),
|
|
("EC2_INSTANCE_ID", "--all"),
|
|
("STOP_SCHEDULE_UTC", "SUN 27:00"),
|
|
],
|
|
)
|
|
def test_invalid_configuration_is_rejected(monkeypatch, configured, key, value):
|
|
monkeypatch.setenv(lifecycle.PREFIX + key, value)
|
|
with pytest.raises(ValueError):
|
|
lifecycle.configuration()
|
|
|
|
|
|
def test_stop_deadline_caps_manual_dispatch_at_24_hours():
|
|
now = datetime(2026, 10, 7, 12, 0, tzinfo=UTC)
|
|
assert lifecycle.stop_deadline("SUN 03:00", now=now) == int((now + timedelta(hours=24)).timestamp())
|
|
|
|
|
|
def test_stop_deadline_obeys_the_actual_weekly_shutdown():
|
|
now = datetime(2026, 10, 10, 23, 0, tzinfo=UTC)
|
|
assert lifecycle.stop_deadline("SUN 03:00", now=now) == int(datetime(2026, 10, 11, 3, 0, tzinfo=UTC).timestamp())
|
|
|
|
|
|
@pytest.mark.parametrize("minute", [0, 30, 59])
|
|
def test_dispatch_during_stop_minute_is_rejected(minute):
|
|
with pytest.raises(ValueError, match="due now"):
|
|
lifecycle.stop_deadline("SUN 03:00", now=datetime(2026, 10, 11, 3, 0, minute, tzinfo=UTC))
|
|
|
|
|
|
def test_dispatch_without_startup_and_90_minute_reserve_is_rejected():
|
|
with pytest.raises(ValueError, match="insufficient time"):
|
|
lifecycle.stop_deadline("SUN 03:00", now=datetime(2026, 10, 11, 1, 21, tzinfo=UTC))
|
|
|
|
|
|
def test_cli_invokes_fake_aws_with_scoped_instance_and_confirms_state(tmp_path):
|
|
fake = tmp_path / "aws"
|
|
record = tmp_path / "calls.jsonl"
|
|
fake.write_text(
|
|
f"#!{sys.executable}\n"
|
|
"import json, sys\n"
|
|
f"with open({str(record)!r}, 'a') as log: log.write(json.dumps(sys.argv[1:]) + '\\n')\n"
|
|
"instance = sys.argv[sys.argv.index('--instance-ids') + 1]\n"
|
|
"print(json.dumps({'Reservations': [{'Instances': [{'InstanceId': instance, 'State': {'Name': 'stopped'}}]}]}))\n"
|
|
)
|
|
fake.chmod(0o755)
|
|
environment = {**os.environ, "PATH": str(tmp_path) + os.pathsep + os.environ["PATH"]}
|
|
environment.update({lifecycle.PREFIX + key: value for key, value in CONFIG.items()})
|
|
result = subprocess.run(
|
|
[sys.executable, str(SCRIPT), "stop"], env=environment, capture_output=True, text=True, timeout=10
|
|
)
|
|
assert result.returncode == 0, result.stderr + result.stdout
|
|
assert "confirmed stopped" in result.stdout
|
|
calls = [json.loads(line) for line in record.read_text().splitlines()]
|
|
assert len(calls) == 1
|
|
assert calls[0][:4] == ["ec2", "describe-instances", "--instance-ids", CONFIG["EC2_INSTANCE_ID"]]
|
|
assert "--force" not in calls[0]
|
|
assert CONFIG["EC2_INSTANCE_ID"] not in result.stdout
|
|
assert CONFIG["AWS_ROLE_ARN"] not in result.stdout
|