Repository navigation
Recover native execution preparation without repeating claims #8307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
loopx-agent
wants to merge
1
commit into
main
Choose a base branch
from
codex/native-preparation-readback-20261107
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−34
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,14 @@ | |
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import shlex | ||
| import subprocess | ||
| from collections.abc import Callable, Mapping, Sequence | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from ...command_invocation import current_loopx_cli_argv | ||
| from ..content_digest import BARE_SHA256_PATTERN | ||
|
|
||
|
|
||
| CommandRunner = Callable[..., subprocess.CompletedProcess[str]] | ||
|
|
@@ -133,6 +135,45 @@ def prepared_capture_reference(payload: Mapping[str, Any], digest: str) -> dict[ | |
| } | ||
|
|
||
|
|
||
| def _native_claim_operation_id(request: Mapping[str, Any]) -> str: | ||
| """Reuse the captured managed claim identity on an authorized retry.""" | ||
| digest = request.get("source_sha256") | ||
| if not isinstance(digest, str) or BARE_SHA256_PATTERN.fullmatch(digest) is None: | ||
| raise ValueError("Native execution preparation requires its original capture digest") | ||
| return f"todo-claim:codex-capture:{digest}" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P1] 旧捕获会重新取得已释放的 lease。 旧 CLI 的 claim ID 不等于这里新派生的 ID。旧进程在 claim/ready 后退出,释放 epoch 1,再由当前源码或 wheel 重入同一捕获,File 和 SQLite 均出现 active epoch 2。绑定可证的旧回执与原 generation,或在旧捕获缺绑定时要求新的有效准备。 |
||
|
|
||
|
|
||
| def codex_execution_preparation_readback_context(request: Mapping[str, Any]) -> str | None: | ||
| """Expose only existing receipt and lease readers after an interrupted preparation.""" | ||
| goal, agent, todo, turn = (request.get(key) for key in ("goal_id", "agent_id", "todo_id", "turn_instance_id")) | ||
| route = request.get("authority_route") | ||
| if not isinstance(route, Mapping) or not all( | ||
| isinstance(part, str) and part for part in (goal, agent, todo, turn) | ||
| ): | ||
| return None | ||
| registry_path, runtime_root = route.get("registry_path"), route.get("runtime_root") | ||
| if not all(isinstance(part, str) and Path(part).is_absolute() for part in (registry_path, runtime_root)): | ||
| return None | ||
| operation_id = _native_claim_operation_id(request) | ||
| base = [*current_loopx_cli_argv(), "--registry", registry_path, | ||
| "--runtime-root", runtime_root, "--format", "json"] | ||
| facts = { | ||
| "event": "host_execution_preparation_readback_required", | ||
| "goal_id": goal, "agent_id": agent, "todo_id": todo, "turn_instance_id": turn, | ||
| "claim_operation_id": operation_id, | ||
| "receipt_command": shlex.join([*base, "todo", "receipt", "--goal-id", goal, "--operation-id", operation_id]), | ||
| "lease_command": shlex.join([*base, "task-lease", "inspect", "--goal-id", goal, "--todo-id", todo]), | ||
| } | ||
| return ( | ||
| "Automatic preparation started for this captured Turn and may still be finishing. " | ||
| "Read the original claim receipt and current lease before deciding how to recover. " | ||
| "With the matching active lease, follow the current captured same-Todo and same-Turn readiness check before work. " | ||
| "For an authorized retry of this claim, pass the original --claim-operation-id. " | ||
| "A retired lease requires a fresh eligible capture. " | ||
| "Receipt: " + json.dumps(facts, ensure_ascii=False, separators=(",", ":")) | ||
| ) | ||
|
|
||
|
|
||
| def execute_codex_execution_preparation( | ||
| request: Mapping[str, Any], | ||
| *, | ||
|
|
@@ -189,16 +230,24 @@ def execute_codex_execution_preparation( | |
| ): | ||
| raise ValueError("Codex execution preparation requires a physical workspace") | ||
| invoke = runner or _default_runner | ||
| if context_directory is not None and (capture_directory is None or not turn_instance_id): | ||
| raise ValueError("Native execution preparation requires a bound captured Turn") | ||
| claim_args = expected_claim | ||
| claim_operation_id = None | ||
| if context_directory is not None: | ||
| if capture_directory is None or not turn_instance_id: | ||
| raise ValueError("Native execution preparation requires a bound captured Turn") | ||
| claim_operation_id = _native_claim_operation_id(request) | ||
| claim_args = [*expected_claim, "--claim-operation-id", claim_operation_id] | ||
| claimed = _run_json( | ||
| expected_claim, | ||
| claim_args, | ||
| registry_path=registry_path, | ||
| runtime_root=runtime_root, | ||
| cwd=workspace, | ||
| timeout=timeout / 2, | ||
| runner=invoke, | ||
| ) | ||
| claim_reference = claimed.get("claim_operation_id") | ||
| if claim_operation_id is not None and claim_reference != claim_operation_id: | ||
| raise RuntimeError("Native execution preparation did not recover its original managed claim") | ||
| guard = [ | ||
| "quota", "should-run", | ||
| "--goal-id", goal_id, | ||
|
|
@@ -259,7 +308,6 @@ def execute_codex_execution_preparation( | |
| if not isinstance(digest, str) or len(digest) != 64: | ||
| raise ValueError("The guard did not return its published capture digest") | ||
| receipt["capture_sha256"] = digest | ||
| claim_reference = claimed.get("claim_operation_id") | ||
| if isinstance(claim_reference, str) and claim_reference: | ||
| receipt["claim_operation_id"] = claim_reference | ||
| # The Host already paid for the captured complete decision. Its caller | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] 这里在 stdout 前已抑制同源恢复提示。 第一次 Hook 在 claim 后退出,第二次在 marker 落盘后、提示输出前退出,随后 PostToolUse 和 SessionStart 均不给提示。保留可重复读取的原回执或 ready 地址;有送达证据后再抑制。