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
3 changes: 2 additions & 1 deletion loopx/control_plane/projects/registry_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from ..content_digest import ENVELOPED_SHA256_PATTERN
from ..goals.activation import require_current_registry_goal_activation
from ..runtime.document_io import fsync_state_directory
from ..runtime.private_permissions import protect_private_path, require_private_path
from ...file_lock import exclusive_cross_runtime_file_lock
from ...paths import GLOBAL_REGISTRY_FILENAME
Expand Down Expand Up @@ -437,6 +436,8 @@ def backup_preimage(self) -> Path:
protect_private_path(directory, directory=True)
target = directory / f"{self._path.name}.{hashlib.sha256(raw).hexdigest()}.bak"
if not target.exists():
from ..runtime.document_io import fsync_state_directory

with target.open("xb") as stream:
protect_private_path(target)
stream.write(raw)
Expand Down
29 changes: 27 additions & 2 deletions loopx/control_plane/quota/accounting_artifact_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,16 +538,41 @@ export async function readQuotaSpendEventFromRun(
return inline;
}
if (typeof run.json_path !== "string" || !run.json_path.trim()) return null;
const runsDirReal = await realpath(runsDir);
const rawPath = run.json_path.trim();
const relocatedCandidate = (() => {
if (!isAbsolute(rawPath)) return null;
const normalized = resolve(rawPath);
const fileName = basename(normalized);
const runsParent = dirname(normalized);
const goalDir = dirname(runsParent);
const goalsDir = dirname(goalDir);
if (
!fileName || fileName === "." || fileName === ".." || extname(fileName) !== ".json" ||
basename(runsParent) !== "runs" || basename(goalDir) !== goalId || basename(goalsDir) !== "goals"
) {
return null;
}
return join(runsDirReal, fileName);
})();
const initialRelative = relative(runsDirReal, resolve(rawPath));
const staysInsideInitial = Boolean(
initialRelative && initialRelative !== ".." &&
!initialRelative.startsWith(`..${sep}`) && !isAbsolute(initialRelative)
);
const candidatePath = staysInsideInitial || relocatedCandidate === null
? resolve(rawPath)
: relocatedCandidate;
let targetPath: string;
try {
targetPath = await realpath(resolve(run.json_path.trim()));
targetPath = await realpath(candidatePath);
} catch (error) {
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
return null;
}
throw error;
}
const relativePath = relative(await realpath(runsDir), targetPath);
const relativePath = relative(runsDirReal, targetPath);
if (
!relativePath ||
relativePath === ".." ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ def test_replan_stages_read_original_authority_capture_without_registry(
assert page["next_offset"] == len(observed)
command = page["next_command"]
guard = json.loads(observed)
from loopx.control_plane.quota.turn_envelope import _turn_envelope_result
assert "review" not in guard["replan_action_packet"]
staged = _turn_envelope_result("replan_cli_view", payload=source)["replan_action_packet"]
missing = tmp_path / "unrelated-missing-registry.json"

def snapshot():
Expand All @@ -228,7 +231,7 @@ def read(command):
assert value["obligation_id"] == source["replan_action_packet"]["obligation_id"]
return value

review = read(guard["replan_action_packet"]["review"])
review = read(staged["review"])
writer = read(review["next"]["writeback"])
assert writer["context_id"] == review["context_id"]
assert writer["source_hash"] == review["source_hash"]
Expand All @@ -237,7 +240,7 @@ def read(command):
assert len(index["outcomes"]) == 1
acceptance = read(index["read_action_template"].replace("{outcome_ref}", index["outcomes"][0]["outcome_ref"]))
assert acceptance["outcome"]["requirements"][0]["description"] == "Stop before publication."
explore = read(guard["replan_action_packet"]["review"].replace("--replan-stage review", "--replan-stage explore"))
explore = read(staged["review"].replace("--replan-stage review", "--replan-stage explore"))
assert explore["explore_basis"] == source["autonomous_replan_obligation"]["replan_context"].get("explore_basis")
assert snapshot() == before, "captured readers cannot admit, settle, repair routes or rewrite authority"
assert not missing.exists()
Expand Down
13 changes: 12 additions & 1 deletion tests/control_plane/test_context_first_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,8 @@ def read(command):
if warm:
prior = capture("wide-prior")
_, prior_ref = read_context_read_reference(prior["recovery"]["reference_file"])
rows = json.loads(Path(prior_ref["path"]).read_bytes())["interaction_contract"]["agent_channel"]["work_context"]["sources"]
prior_decision = json.loads(Path(prior_ref["path"]).read_bytes())
rows = prior_decision["interaction_contract"]["agent_channel"]["work_context"]["sources"]
index = next(i for i, source in enumerate(rows) if source["source"] == "goal_state")
body_pointer = f"/interaction_contract/agent_channel/work_context/sources/{index}/content/text"
prior_read = prior["recovery"]["command"] + " --select " + body_pointer
Expand All @@ -769,8 +770,11 @@ def read(command):
if requested_scopes:
assert prior_facts["/goal_boundary/write_scope"] == scopes
deliver(journal, event, prior_pages)
proof = prior_decision["execution_preparation"]["lease_proof"]
cli("refresh-state", *binding, "--turn-instance-id", "wide-prior", "--classification", "fixture_verified",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
"--task-lease-idempotency-key", proof["idempotency_key"],
"--task-lease-expected-version", str(proof["expected_version"]),
"--delivery-boundary", "in_flight_continuation", "--no-global-sync", "--suppress-external-sinks")
cli("quota", "spend-slot", *binding, "--turn-instance-id", "wide-prior",
"--slots", "1", "--source", "heartbeat", "--execute")
Expand Down Expand Up @@ -2097,9 +2101,13 @@ def capture(turn, registry_path=registry):
registry_path=registry_path)

def settle(turn, registry_path=registry):
lease = cli("task-lease", "inspect", "--goal-id", GOAL_ID, "--todo-id", TODO_ID,
registry_path=registry_path)["lease"]
cli("refresh-state", *binding(turn), "--classification", "fixture_validated",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
"--delivery-boundary", "in_flight_continuation", "--delivery-workspace-path", str(project),
"--task-lease-idempotency-key", lease["idempotency_key"],
"--task-lease-expected-version", str(lease["version"]),
"--no-global-sync", "--suppress-external-sinks", registry_path=registry_path)
spent = cli("quota", "spend-slot", *binding(turn), "--slots", "1", "--source", "heartbeat", "--execute",
registry_path=registry_path)
Expand Down Expand Up @@ -2261,9 +2269,12 @@ def capture(turn, todo_id=TODO_ID):
"--decision-output-root", str(captures), "--codex-context-dir", str(journal))

def settle(turn):
lease = cli("task-lease", "inspect", "--goal-id", GOAL_ID, "--todo-id", TODO_ID)["lease"]
cli("refresh-state", *binding(turn), "--classification", "fixture_validated",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
"--delivery-boundary", "in_flight_continuation", "--delivery-workspace-path", str(project),
"--task-lease-idempotency-key", lease["idempotency_key"],
"--task-lease-expected-version", str(lease["version"]),
"--no-global-sync", "--suppress-external-sinks")
spent = cli("quota", "spend-slot", *binding(turn), "--slots", "1", "--source", "heartbeat", "--execute")
assert spent["appended"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ def test_declared_leased_completion_writeback_spend_terminal_and_replay(
completion_command = recovery["ordered_steps"][0]["command_template"]
assert "--project" in recovery["ordered_steps"][1]["command_template"]
assert "--state-file" in recovery["ordered_steps"][1]["command_template"]
assert "--project" not in recovery["ordered_steps"][2]["command_template"]
assert "--settle-turn" in recovery["ordered_steps"][1]["command_template"]
assert recovery["ordered_steps"][2]["owner"] == "runtime"
assert "command_template" not in recovery["ordered_steps"][2]
assert "--no-follow-up" not in shlex.split(completion_command)
assert completion_command in render_todo_markdown(premature)
completion_command = completion_command.replace("<validated evidence>", complete_args[-1])
Expand Down
4 changes: 3 additions & 1 deletion tests/control_plane/test_dependency_dispatch_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def test_local_wait_and_user_gates_preserve_only_verified_independent_work(publi
host.run()
assert not host.counts.exists()

update(goal, gate, "--unblocks-todo-id", waiting)
second_gated = add(goal, "Validate another gated artifact after owner approval.",
"--task-class", "advancement_task", "--action-kind", "validate", "--status", "blocked")
update(goal, gate, "--unblocks-todo-id", second_gated)
fresh = guard(goal)
assert fresh["should_run"] is True and fresh["requires_user_action"] is True
assert fresh["selected_todo"]["todo_id"] == independent
Expand Down
3 changes: 3 additions & 0 deletions tests/control_plane/test_explore_capture_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_first_capture_allocates_identity_and_recovers_without_rewriting_scope(
rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app",
*binding, cwd=project)
assert rc == 0, guard
proof = guard["execution_preparation"]["lease_proof"]
attachment = deepcopy(ATTACHMENT)
del attachment["node_id"]
packet = {"schema_version": "goal_vision_replan_contract_v0",
Expand All @@ -52,6 +53,8 @@ def test_first_capture_allocates_identity_and_recovers_without_rewriting_scope(
vision = tmp_path / "vision.json"
args = ("refresh-state", *binding, "--classification", "validated_change",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
"--task-lease-idempotency-key", proof["idempotency_key"],
"--task-lease-expected-version", str(proof["expected_version"]),
"--no-global-sync", "--suppress-external-sinks", "--agent-vision-json", str(vision))
log = explore_result_log_path(runtime, GOAL_ID)
if conflict:
Expand Down
3 changes: 3 additions & 0 deletions tests/control_plane/test_explore_durable_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ def test_ninth_capture_keeps_history_and_survives_real_cli_replay(tmp_path, prov
rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app",
*binding, cwd=project)
assert rc == 0, guard
proof = guard["execution_preparation"]["lease_proof"]
vision = tmp_path / "vision.json"
vision.write_text(json.dumps({"schema_version": "goal_vision_replan_contract_v0",
"vision_patch": {"acceptance_summary": "Require a uniform tail bound."},
"explore_result": deepcopy(ATTACHMENT)}))
command = ("refresh-state", *binding, "--classification", "validated_change",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
"--task-lease-idempotency-key", proof["idempotency_key"],
"--task-lease-expected-version", str(proof["expected_version"]),
"--no-global-sync", "--suppress-external-sinks", "--agent-vision-json", str(vision))
rc, captured = _run_cli(registry, runtime, *command, cwd=project)
assert rc == 0 and captured["explore_result_delivery"]["ok"], captured
Expand Down
66 changes: 49 additions & 17 deletions tests/control_plane/test_explore_result_writeback.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r
cwd=project,
)
assert rc == 0, claimed
if terminal:
from loopx.todos import complete_goal_todo

binding = (
"--goal-id",
Expand All @@ -398,14 +396,21 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r
)
assert rc == 0, guard
if terminal:
complete_goal_todo(
registry_path=path,
goal_id=GOAL_ID,
todo_id=TODO_ID,
agent_id=AGENT_ID,
evidence="Synthetic validation passed",
**terminal_proof,
rc, completed = _run_cli(
path,
runtime,
"todo",
"complete",
*binding,
"--evidence",
"Synthetic validation passed",
"--task-lease-idempotency-key",
terminal_proof["task_lease_idempotency_key"],
"--task-lease-expected-version",
str(terminal_proof["task_lease_expected_version"]),
cwd=project,
)
assert rc == 0, completed
source = tmp_path / "result.json"
plan = guard["interaction_contract"]["cli_channel"]["settlement_plan"]
template = plan["ordered_steps"][1]["optional_attachments"][0]["attachment_template"]
Expand Down Expand Up @@ -452,6 +457,12 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r
attachment_args += ("--explore-result-json", str(source))
if source_mode in {"vision", "both", "path_delta", "wide_path_delta", "linked_path_delta"}:
attachment_args += ("--agent-vision-json", str(vision))
proof = guard.get("execution_preparation", {}).get("lease_proof")
lease_args = (
("--task-lease-idempotency-key", proof["idempotency_key"],
"--task-lease-expected-version", str(proof["expected_version"]))
if proof else ()
)
args = (
"refresh-state",
*binding,
Expand All @@ -461,6 +472,7 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r
"implementation",
"--delivery-outcome",
"outcome_progress",
*lease_args,
"--no-global-sync",
"--suppress-external-sinks",
*attachment_args,
Expand Down Expand Up @@ -884,26 +896,46 @@ def run(*command):
assert not log.exists()
assert list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"] == todos

# Supported recovery: fresh admission and an active lease, then the same capture.
run(*guard_command)
current = run("task-lease", "acquire", *lease_binding, "--idempotency-key", "recovery")["lease"]
captured = run(*refresh)
# Supported recovery after retiring the original generation: supersede onto a
# fresh Todo/Turn with an active lease, then commit and test replay after release.
reclaimed = run("todo", "claim", "--goal-id", GOAL_ID, "--todo-id", TODO_ID,
"--agent-id", AGENT_ID, "--claimed-by", AGENT_ID)
superseded = run("todo", "supersede", "--goal-id", GOAL_ID, "--todo-id", TODO_ID,
"--agent-id", AGENT_ID, "--claim-operation-id", reclaimed["claim_operation_id"],
"--reason", "Original lease was released before writeback",
"--next-agent-todo", "[P1] Continue tail bound check",
"--next-claimed-by", AGENT_ID)
recovery_todo_id = superseded["successor_todo_ids"][0]
recovery_lease_binding = ["--goal-id", GOAL_ID, "--todo-id", recovery_todo_id, "--owner", AGENT_ID]
current = run("task-lease", "acquire", *recovery_lease_binding, "--idempotency-key", "recovery")["lease"]
recovery_binding = ["--goal-id", GOAL_ID, "--agent-id", AGENT_ID,
"--todo-id", recovery_todo_id, "--turn-instance-id", f"{TURN_ID}-recovery"]
recovery_guard = run("quota", "should-run", "--codex-app", *recovery_binding)
proof = recovery_guard["execution_preparation"]["lease_proof"]
recovery_refresh = [
"refresh-state", *recovery_binding, "--classification", "validated_change",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
"--task-lease-idempotency-key", proof["idempotency_key"],
"--task-lease-expected-version", str(proof["expected_version"]),
"--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks",
]
captured = run(*recovery_refresh)
assert captured["appended"] and captured["explore_result_delivery"]["ok"]
display_input = captured["explore_result_delivery"]["hook"]["intents"][0]["payload"]
assert display_input == {"attachment_ref": "$.explore_result"}
assert captured["explore_result"] == ATTACHMENT
assert len(load_explore_result_events(log)) == 2
committed_index, committed_log = index.read_bytes(), log.read_bytes()
rows = list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"]
target = next(row for row in rows if row["todo_id"] == TODO_ID)
target = next(row for row in rows if row["todo_id"] == recovery_todo_id)
assert target["status"] == "open" and target["claimed_by"] == AGENT_ID
assert target["explore_result_node_refs"] == [ATTACHMENT["node_id"]]
run("task-lease", "release", *lease_binding, "--idempotency-key", "recovery",
run("task-lease", "release", *recovery_lease_binding, "--idempotency-key", "recovery",
"--expected-version", str(current["version"]))
leases = read_canonical_todos_if_promoted(
runtime_root=runtime, goal_id=GOAL_ID, include_leases=True)["leases"]
assert leases[0]["status"] == "released"
replay = run(*refresh)
assert all(lease["status"] == "released" for lease in leases), leases
replay = run(*recovery_refresh)
assert replay["idempotent_replay"] and replay["explore_result_delivery"]["idempotent_replay"]
assert replay["explore_result"] == captured["explore_result"]
assert replay["explore_result_delivery"]["hook"]["intents"][0]["payload"] == display_input
Expand Down
Loading
Loading