Skip to content
Open
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
15 changes: 15 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,15 @@ jobs:
python -m pytest -q --no-cov
tests/test_stage2c_dependency_lock.py
tests/architecture/test_top_level_module_budget.py
tests/architecture/test_external_request_lint.py
tests/test_repository_instruction_delivery.py
tests/test_contributing_guide.py

- name: Lint external request dispatch boundaries
run: |
python -m ruff check --ignore-noqa --config scripts/ci/external-request-lint.toml loopx packages scripts apps
python -m ruff check --ignore-noqa --config scripts/ci/external-transport-lint.toml loopx packages/*/src

- name: Lint test suite
run: >-
python -m ruff check
Expand Down Expand Up @@ -308,6 +314,9 @@ jobs:
- name: Install test dependencies
run: python -m pip install --disable-pip-version-check -e ".[test]"

- name: Check external provider recovery
run: python -m pytest -q --no-cov tests/extensions/test_governed_capability_execution.py

- name: Verify file authority on the core runtime
run: >-
node --no-warnings --experimental-strip-types --test
Expand Down Expand Up @@ -372,6 +381,12 @@ jobs:
python -m pip install --disable-pip-version-check -e ".[test]"
npm ci --ignore-scripts

- name: Lint external JS and TS transports
if: matrix.shard == 1
run: |
node --test scripts/ci/lint-external-transports.test.mjs
node scripts/ci/lint-external-transports.mjs

- name: Type-check the TypeScript Effect core
if: matrix.shard == 1
run: npm run typecheck:control-plane
Expand Down
79 changes: 76 additions & 3 deletions docs/reference/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,15 +546,88 @@ materialized proposal is checkpointed immediately. Monitor upserts belong to
the pre-settlement phase; monitor completion belongs to the post-settlement
phase. A crash between either Todo write and its checkpoint recovers by the
proposal's stable monitor key and completion identity instead of duplicating
work. A crash after an external
effect replays with the same idempotency key and reconciles the receipt instead
of starting an unrelated operation. One settlement effect id owns exactly one
work. If the journal contains an armed start intent but no accepted provider result,
a second start is refused, including with the original idempotency key. A trusted
integration may pass `read_start_result` to `start_governed_external_capability`
with the original request and current admission to look up the original
operation by provider, invocation, effect key and request digest. This callback
must only observe the original operation. An unresolved lookup returns `None`;
the journal is retained and no quota is spent. A matching result goes through
the normal provider-result validation, asynchronous polling and settlement.
For a provider without reliable lookup, the operation remains unresolved.
In a shared run directory, an unsettled write for the same Goal, Todo,
capability and operation also holds starts from a different Agent or Turn.
The error identifies the original invocation. The reservation index is built
from existing journals on first use. Quiesce older writers when upgrading
and retain the run directory during handoff. A new independently admitted
Turn can start after the prior invocation has committed. Separate business
intents across Todos or run directories still need provider-level identity.

**Recovery from an older backup.** `backup-state verify --execute` leaves a
`.loopx-restored-inert` entry in each extracted runtime root. Keep it when
copying the runtime. The verified copy grants no execution authority. The
governed writer checks the Goal's source and invoked runtime as well as the
journal path, including when an integration selects a different run directory.
Ordinary fresh start and its preview on that runtime report that provider
reconciliation is required. A running operation requires a trusted
`read_running_result(request)` for polling on a restored runtime.

A successor with current runnable admission for the same Goal, Todo and
applicable operation can supply the original invocation arguments and
`read_start_result`. The result retains the original effect identity for
writeback and quota. On a restored runtime, even if the backup predates the
journal, the trusted service lookup must return `{request_digest, target_key, result}` from its own
records, with the original digest, admitted target, and matching typed provider
result. An unknown result, a mismatched identity or a missing original team
plan basis holds recovery. Readback alone never dispatches a new start.

When the service can prove the prior attempt reached a terminal state without
an effect, a trusted integration may supply `reserve_restored_start(query)`.
The query contains the original-form provider request, the admitted target,
a stable operation key (Goal, capability, operation and target), an intent
digest (Goal binding, provider, context and input), the current request digest,
invocation id and a fresh challenge. The service must check the business
operation across all prior attempts, fence any outstanding writer, and
atomically grant **one** conditional start. Its receipt must echo
`operation_key`, `intent_digest`, `request_digest`, `invocation_id` and
`challenge`, set `status` to `reserved_after_terminal_no_effect`, and include
a bounded non-secret `reservation_ref`. The provider must validate and
consume this grant atomically with delivery, using
`request.lifecycle.restored_start_reservation`. An empty lookup, timeout or
cached grant does not authorize dispatch. While obtaining the grant, the local
journal remains unarmed. If the process exits here, the original or successor
Turn can ask the service again. The service must reconcile any outstanding
grant by operation and intent and fence the old grant before issuing a fresh
one. LoopX publishes the validated grant and the armed state in one durable
checkpoint immediately before calling the provider. After that point an
unknown delivery requires original-result readback. Older armed records with
no saved grant also require service reconciliation: local state alone cannot
establish whether an older writer sent the request. Integrations without a
suitable service primitive stay held.

The interlock covers runtime copies published by this verifier, including
older compatible archives verified with the updated code. A raw filesystem
rollback, a previously extracted unmarked copy, an omitted marker or an older
writer does not carry this protection. Quiesce older writers and retain
independent provider records across recovery.

One settlement effect id owns exactly one
material invocation: retrying the same request replays it, while attempting a
different operation or input under the same Turn receipt fails before provider
dispatch. A new invocation also requires `should_run=true`; an existing journal
may still be recovered with its exact typed receipt after the runnable decision
has changed.

CI checks Python dispatch with Ruff (`scripts/ci/external-request-lint.toml`).
Production Python HTTP transports under `loopx/` and `packages/*/src/` are
listed in `scripts/ci/external-transport-lint.toml`. For JS/TS in the same
trees, `node scripts/ci/lint-external-transports.mjs` checks direct fetch
and common HTTP module references. Adding a raw transport outside an existing
owner fails the applicable check. Review new owners for operation identity,
remote readback and recovery before allowing them. These source checks cover
the named transports. Provider internals, dynamic loading, third-party SDKs
and shell effects require enforcement by their respective runtime or provider.

Transition proposals are deliberately narrower than arbitrary Goal mutation.
The first version supports only continuous-monitor upsert and completion. It
cannot create a general advancement Todo, change a Goal, widen capability
Expand Down
24 changes: 24 additions & 0 deletions loopx/control_plane/state_recovery_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import zlib
from typing import Any, BinaryIO, Iterator, NoReturn

from ..paths import RESTORED_RUNTIME_MARKER
from .effect_runtime import _node_executable, effect_runtime_result, effect_runtime_request_scope
from .runtime.document_io import fsync_state_directory
from .runtime.node_probe import node_typescript_flags

MAX_ARCHIVE_BYTES = 128 * 1024 * 1024
Expand Down Expand Up @@ -419,6 +421,27 @@ def _scan(handle: BinaryIO, archive_size: int, output: Path | None = None) -> di
"entry_count": len(names)}


def _mark_restored_runtimes(data: Path) -> None:
# This interlock travels with a copied runtime. Never traverse an archived link
# or replace a member of the verified archive.
roots = [data / "runtime-root"]
sources = data / "source-runtimes"
if sources.is_symlink():
raise StateRecoveryError("recovery_runtime_not_physical")
if sources.is_dir():
roots.extend(sources.iterdir())
for root in roots:
if root.is_symlink():
raise StateRecoveryError("recovery_runtime_not_physical")
if root.is_dir():
marker = root / RESTORED_RUNTIME_MARKER
try:
marker.touch(mode=0o600, exist_ok=False)
except FileExistsError:
pass # Any entry, including a dangling link, already holds dispatch.
fsync_state_directory(marker)


@effect_runtime_request_scope()
def verify_state_backup(
*, archive: Path, manifest: Path, destination: Path, execute: bool = False,
Expand Down Expand Up @@ -473,6 +496,7 @@ def verify_state_backup(
with (data / member["archive_path"]).open("rb") as handle:
digest = hashlib.file_digest(handle, "sha256").hexdigest()
_require(digest == member["sha256"], "recovery_owner_mutated_source")
_mark_restored_runtimes(data)
audit_path = staging / "recovery-audit.json"
with audit_path.open("x", encoding="utf-8") as handle:
os.chmod(audit_path, 0o600)
Expand Down
4 changes: 2 additions & 2 deletions loopx/extensions/capability_admission.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..registry import find_registry_goal
from .manifest import EXTERNAL_CAPABILITY_PROFILE_SCHEMA_VERSION
from .runtime import (
execute_extension_runtime_binding,
_execute_extension_runtime_binding,
extension_catalog_entries,
resolve_extension_runtime_binding,
)
Expand Down Expand Up @@ -661,7 +661,7 @@ def invoke_external_capability(
}
if not execute:
return receipt
provider_result = execute_extension_runtime_binding(
provider_result = _execute_extension_runtime_binding(
binding,
request=request,
environment=environment,
Expand Down
Loading
Loading