Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions loopx/control_plane/goals/vision_checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ const GOAL_PATH_DELTA_OUTCOMES = [
"stop",
"wait",
] as const;
const OUTCOME_CHECKPOINT_CONTINUATION_OUTCOMES = [
"continue",
"no_change",
"replan",
] as const;
const GOAL_PATH_DELTA_SCALAR_LIMITS = {
prior_assumption: 320,
observed_reality: 320,
Expand Down Expand Up @@ -883,6 +888,25 @@ function existingVisionContinuityBasis(
};
}

function materialOutcomeCheckpointFailures(
vision: JsonObject,
): string[] {
let closed = false;
try {
closed = lifecycle.closed_states.includes(normalizeGoalVisionState(vision.state));
} catch {
// Invalid lifecycle state is not evidence of a closed frontier.
}
if (closed) return [];

const path = optionalObject(vision.path_delta, "agent_vision.path_delta");
if (path === null) return [];
const outcome = compactText(path.outcome);
return (OUTCOME_CHECKPOINT_CONTINUATION_OUTCOMES as readonly string[]).includes(outcome)
? []
: ["path_outcome_valid"];
}

function deliveryBoundary(value: unknown): DeliveryBoundary {
if (value === null || value === undefined || value === "") {
return "semantic_closeout";
Expand Down Expand Up @@ -1018,17 +1042,38 @@ export function buildVisionCheckpoint(value: unknown): JsonObject {
trigger.kind !== "in_flight_continuation"
);
const required = requiredTriggers.length > 0 || unchanged !== null;
const materialCloseout = request.delivery_outcome !== "outcome_gap" && triggers.some((trigger) =>
trigger.kind === "material_delivery_outcome"
);
const patchedFailures = request.agent_vision !== null && materialCloseout
? materialOutcomeCheckpointFailures(
request.agent_vision,
)
: [];
const unchangedBasis = unchanged !== null
? existingVisionContinuityBasis(request.existing_agent_vision)
: null;
const unchangedFailures =
unchanged !== null && request.existing_agent_vision !== null && materialCloseout
? materialOutcomeCheckpointFailures(
request.existing_agent_vision,
)
: [];
let decision: VisionCheckpointDecision;
let satisfied: boolean;
if (request.agent_vision !== null) {
let unqualifiedComponents: string[] = [];
if (request.agent_vision !== null && patchedFailures.length === 0) {
decision = "patched";
satisfied = true;
} else if (unchanged !== null && request.existing_agent_vision !== null) {
} else if (unchanged !== null && request.existing_agent_vision !== null && unchangedFailures.length === 0) {
decision = "unchanged_with_reason";
satisfied = true;
} else if (unchanged !== null || required) {
decision = "missing_required";
satisfied = false;
unqualifiedComponents = patchedFailures.length > 0
? patchedFailures
: unchangedFailures;
} else {
decision = "not_required";
satisfied = true;
Expand All @@ -1046,14 +1091,14 @@ export function buildVisionCheckpoint(value: unknown): JsonObject {
if (request.agent_vision !== null) {
checkpoint.agent_vision_state = request.agent_vision.state;
}
if (unqualifiedComponents.length > 0) {
checkpoint.unqualified_components = unqualifiedComponents;
}
if (unchanged !== null && request.existing_agent_vision !== null) {
checkpoint.unchanged_reason = unchanged;
checkpoint.agent_vision_state = request.existing_agent_vision.state;
const continuityBasis = existingVisionContinuityBasis(
request.existing_agent_vision,
);
if (continuityBasis !== null) {
checkpoint.continuity_basis = continuityBasis;
if (unchangedBasis !== null) {
checkpoint.continuity_basis = unchangedBasis;
}
} else if (unchanged !== null) {
checkpoint.missing_baseline = true;
Expand Down
1 change: 0 additions & 1 deletion tests/control_plane/test_contract_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from canonical_authority_fixture import initialize_canonical_source_fixture
from loopx.cli_runtime import report_json_text
from loopx.paths import display_report_text
from loopx.contract import check_contract, display_report_value
from loopx.control_plane.goals.contract_health import (
contract_error_diagnostic,
Expand Down
66 changes: 66 additions & 0 deletions tests/control_plane/test_refresh_checkpoint_recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,72 @@ def test_checkpoint_only_recovery_bypasses_open_todo_completion_validation(
assert _spend_run_count(runtime) == 0


def test_invalid_material_path_decision_uses_checkpoint_supplement(tmp_path: Path) -> None:
project, runtime, registry = _write_fixture(tmp_path, provider="sqlite")
binding = (
"--agent-id", AGENT_ID,
"--todo-id", TODO_ID,
"--turn-instance-id", "turn-invalid-material-path",
)
rc, guard = _run_cli(
registry, runtime, "quota", "should-run", "--codex-app",
"--goal-id", GOAL_ID, *binding, cwd=project,
)
assert rc == 0 and guard["decision"] == "run", guard

def vision(outcome: str, name: str) -> Path:
path = tmp_path / name
path.write_text(json.dumps({
"schema_version": "goal_vision_replan_contract_v0",
"state": "vision_patch_proposed",
"vision_patch": {
"acceptance_summary": "The observed result and remaining route are verified.",
},
"path_delta": {
"schema_version": "goal_path_delta_v0",
"outcome": outcome,
"prior_assumption": "The active work required an immediate decision.",
"observed_reality": "The external condition has not changed yet.",
"retained": ["Preserve the verified route and evidence."],
"evidence_refs": ["evidence:material-path"],
},
}), encoding="utf-8")
return path

wait_packet = vision("wait", "wait.json")
delivery = (
"refresh-state", "--goal-id", GOAL_ID, *binding,
"--classification", "verified_wait",
"--delivery-batch-scale", "single_surface",
"--delivery-outcome", "outcome_progress",
"--agent-vision-json", str(wait_packet),
"--no-global-sync", "--suppress-external-sinks",
)
rc, first = _run_cli(registry, runtime, *delivery, cwd=project)
assert rc == 0 and first["appended"] is True, first
assert first["vision_checkpoint"]["decision"] == "missing_required"
assert first["vision_checkpoint"]["satisfied"] is False
assert first["vision_checkpoint"]["unqualified_components"] == [
"path_outcome_valid"
]

rc, context = _run_cli(
registry, runtime, "checkpoint-context", "--goal-id", GOAL_ID,
*binding, cwd=project,
)
assert rc == 0, context
corrected = list(delivery)
corrected[corrected.index(str(wait_packet))] = str(vision("no_change", "corrected.json"))
corrected += ["--checkpoint-read-context", context["read_context_id"]]
rc, repaired = _run_cli(registry, runtime, *corrected, cwd=project)
assert rc == 0 and repaired["appended"] is True, repaired
assert repaired["refresh_recovery"]["decision"] == "supplement_checkpoint"
assert repaired["vision_checkpoint"]["decision"] == "patched"
assert repaired["vision_checkpoint"]["satisfied"] is True
assert repaired["settlement_identity"] == first["settlement_identity"]
assert _spend_run_count(runtime) == 0


def test_invalid_supplement_leaves_original_writeback_intact(tmp_path: Path):
project, runtime, registry = _write_fixture(tmp_path, provider="sqlite")
binding = (
Expand Down
37 changes: 37 additions & 0 deletions tests/control_plane_ts/vision_checkpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,43 @@ test("semantic closeout retains the strict material vision checkpoint", () => {
]);
});

test("material closeout leaves an invalid path decision recoverable", () => {
const packet = (outcome: string) => ({
state: "vision_patch_proposed",
vision_patch: {acceptance_summary: "Verified result; scoped work remains."},
path_delta: {
outcome,
prior_assumption: "The current route remained valid.",
observed_reality: "Fresh evidence confirms the bounded next step.",
retained: ["Continue from the verified evidence."],
evidence_refs: ["evidence:verified-result"],
},
});
const prepared = (outcome: string) =>
buildVisionCheckpoint(prepareRequest({
agent_vision_packet: packet(outcome),
})).agent_vision;

for (const outcome of ["continue", "no_change", "replan"]) {
const result = buildVisionCheckpoint(finalizeRequest({agent_vision: prepared(outcome)}));
assert.equal(result.decision, "patched");
assert.equal(result.satisfied, true);
}
for (const outcome of ["wait", "ask_human", "stop"]) {
const result = buildVisionCheckpoint(finalizeRequest({agent_vision: prepared(outcome)}));
assert.equal(result.decision, "missing_required");
assert.equal(result.satisfied, false);
assert.deepEqual(result.unqualified_components, ["path_outcome_valid"]);
assert.deepEqual(result.required_resolution, ["write_vision_patch"]);
}

const reportedGap = buildVisionCheckpoint(finalizeRequest({
delivery_outcome: "outcome_gap",
agent_vision: prepared("replan"),
}));
assert.equal(reportedGap.decision, "patched");
});

test("missing checkpoint offers unchanged only with a persisted vision", () => {
const baseline = {state: "vision_active", generated_at: "2026-09-01T10:00:00Z"};
const missing = buildVisionCheckpoint(finalizeRequest({
Expand Down
Loading