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
924 changes: 924 additions & 0 deletions loopx/cli_commands/context_delivery_retention.py

Large diffs are not rendered by default.

625 changes: 625 additions & 0 deletions loopx/cli_commands/context_delivery_retention_checkpoint.py

Large diffs are not rendered by default.

919 changes: 919 additions & 0 deletions loopx/cli_commands/context_delivery_retention_scan.py

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion loopx/cli_commands/quota_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
NATIVE_CONTEXT_FIRST_READ_FALLBACK_BYTES, NATIVE_CONTEXT_PAGE_BYTES,
native_context_page_budget as native_context_page_budget,
)
from .context_delivery_retention import maybe_prune_context_delivery_root
from .quota_context_reader import (
budgeted_ready_opening,
_capture_selection_transport_template,
Expand Down Expand Up @@ -379,6 +380,13 @@ def progressive_context_capture_view(payload: dict[str, Any], args: argparse.Nam
and "vision" not in quota_detail_sections_from_args(args))
if directory is None:
root = Path(_private_transport_root(runtime_root))
try:
maybe_prune_context_delivery_root(
root,
runtime_root=runtime_root.expanduser().resolve(),
)
except Exception: # noqa: BLE001 - best-effort pre-allocation pruning is non-blocking.
pass
directory = Path(tempfile.mkdtemp(prefix="context-", dir=root))
if payload.get("schema_version") == "loopx_turn_envelope_v1":
surface = "complete_turn"
Expand Down Expand Up @@ -575,12 +583,17 @@ def fits(value: dict[str, object]) -> bool:
return payload
try:
uid = private_capture_uid()
root = runtime_root.expanduser().resolve() / "context-delivery"
resolved_runtime = runtime_root.expanduser().resolve()
root = resolved_runtime / "context-delivery"
root.mkdir(mode=0o700, exist_ok=True)
metadata = root.lstat()
if (not stat.S_ISDIR(metadata.st_mode) or metadata.st_uid != uid
or metadata.st_mode & 0o077):
raise ValueError("result capture requires a private physical directory owned by this user")
try:
maybe_prune_context_delivery_root(root, runtime_root=resolved_runtime)
except Exception: # noqa: BLE001 - best-effort pre-allocation pruning is non-blocking.
pass
directory = Path(tempfile.mkdtemp(prefix="settlement-", dir=root))
saved = directory / "result.json"
digest = write_decision_capture(saved, payload)
Expand Down
109 changes: 97 additions & 12 deletions loopx/cli_commands/quota_result_delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
context_capture_read_command,
project_captured_context,
)
from .context_delivery_retention import maybe_prune_context_delivery_root
from .quota_capture import (
bind_capture_selection_transport,
budgeted_settlement_result,
Expand Down Expand Up @@ -43,6 +44,66 @@
]


def _prune_ephemeral_context_delivery(
args: argparse.Namespace,
*,
context: QuotaCommandContext | None,
capture_directory: Path | None,
) -> None:
try:
if getattr(args, "dry_run", False) is True:
return
runtime_root = (
context.runtime_root
if context is not None
else getattr(args, "_progressive_context_runtime", None)
)
if not isinstance(runtime_root, (str, Path)):
return
resolved_runtime = Path(runtime_root).expanduser().resolve()
managed_root = resolved_runtime / "context-delivery"
capture_parent: Path | None = None
if isinstance(capture_directory, Path):
try:
capture_parent = capture_directory.expanduser().resolve().parent
except OSError:
capture_parent = capture_directory.expanduser().absolute().parent
protected = (
frozenset({capture_directory.name})
if capture_directory is not None and capture_parent == managed_root
else frozenset()
)
maybe_prune_context_delivery_root(
managed_root,
runtime_root=resolved_runtime,
protected_names=protected,
)
except Exception: # noqa: BLE001 - best-effort retention pruning never blocks emission.
return


def _emit_and_prune_result(
payload: dict[str, object],
args: argparse.Namespace,
*,
renderer: Callable[[dict[str, object]], str],
print_payload: PrintPayload,
context: QuotaCommandContext | None,
capture_directory: Path | None,
exit_code: int,
) -> int:
print_payload(payload, args.format, renderer)
try:
_prune_ephemeral_context_delivery(
args,
context=context,
capture_directory=capture_directory,
)
except Exception: # noqa: BLE001 - stdout already emitted; never emit a second error payload.
pass
return exit_code


def _emit_bounded_settlement(
payload: dict[str, object],
args: argparse.Namespace,
Expand Down Expand Up @@ -259,7 +320,12 @@ def _publish_quota_result(
)
# Compose optional capability guidance after recovery has chosen the plan,
# before capture/compaction. The quota decision owner stays capability-neutral.
cli = (payload.get("interaction_contract") or {}).get("cli_channel")
interaction_contract = payload.get("interaction_contract")
cli = (
interaction_contract.get("cli_channel")
if isinstance(interaction_contract, Mapping)
else None
)
if isinstance(cli, dict) and isinstance(cli.get("settlement_plan"), Mapping):
from ..capabilities.explore.turn_context import project_settlement_attachment

Expand Down Expand Up @@ -317,8 +383,14 @@ def _publish_quota_result(
# Bind the existing immutable reader route before signing. Its sidecar
# receives the final byte digest after publication, avoiding a self-hash.
source = capture_directory / "decision.json"
existing_detail_ref = payload.get("detail_ref")
detail_ref = (
dict(existing_detail_ref)
if isinstance(existing_detail_ref, Mapping)
else {}
)
payload = {**payload, "detail_ref": {
**payload.get("detail_ref", {}),
**detail_ref,
"full_decision": context_capture_read_command(source, "",
reference_file=source.with_suffix(".reader.json"), reader_argv=reader_argv),
}}
Expand Down Expand Up @@ -377,29 +449,35 @@ def _publish_quota_result(
prepared_capture_reference,
)

print_payload(
return _emit_and_prune_result(
prepared_capture_reference(payload, captured_digest),
args.format,
_quota_renderer(args),
args,
renderer=_quota_renderer(args),
print_payload=print_payload,
context=context,
capture_directory=capture_directory,
exit_code=0,
)
return 0
if (
payload.get("ok") is False
and context_delivery_enabled
and capture_directory
and args.format == "json"
and not getattr(args, "defer_context_presentation", False)
):
print_payload(
return _emit_and_prune_result(
quota_module._export("captured_quota_failure_view")(
payload,
capture_directory=capture_directory,
max_page_output_bytes=getattr(args, "max_page_output_bytes", None),
),
args.format,
_quota_renderer(args, failure=True),
args,
renderer=_quota_renderer(args, failure=True),
print_payload=print_payload,
context=context,
capture_directory=capture_directory,
exit_code=1,
)
return 1
if payload.get("ok") is True:
delivery_request = handoff_decision(
capture_directory,
Expand Down Expand Up @@ -494,5 +572,12 @@ def _publish_quota_result(
raise QuotaDecisionTransportError(
"Current context recovery could not be prepared. Read the saved original decision and receipt."
) from exc
print_payload(payload, args.format, _quota_renderer(args, failure=not succeeded))
return 0 if succeeded else 1
return _emit_and_prune_result(
payload,
args,
renderer=_quota_renderer(args, failure=not succeeded),
print_payload=print_payload,
context=context,
capture_directory=capture_directory,
exit_code=0 if succeeded else 1,
)
1 change: 1 addition & 0 deletions tests/architecture/test_content_digest_single_owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
"loopx.chat.result_files_api",
"loopx.chat.action_normalization",
"loopx.capabilities.manager_context.chat_history",
"loopx.cli_commands.context_delivery_retention_scan",
"loopx.cli_commands.quota_capture",
"loopx.cli_commands.quota_context_reader",
"loopx.cli_commands.update_artifact",
Expand Down
Loading
Loading