diff --git a/docs/architecture/rfcs/agent-im-openviking-collaboration-v0.md b/docs/architecture/rfcs/agent-im-openviking-collaboration-v0.md index 877d680aca..6914c43863 100644 --- a/docs/architecture/rfcs/agent-im-openviking-collaboration-v0.md +++ b/docs/architecture/rfcs/agent-im-openviking-collaboration-v0.md @@ -284,6 +284,35 @@ Measure outcome quality rather than message volume: - false acceptance or rejection of controlled transitions; - verified goal outcomes produced after handoff. +## Current Delivery Checkpoint + +Task [#5198](https://github.com/loopx-project/loopx/issues/5198) tracks composition. +A first implementation stage proposed a canonical room-work Agent CLI with +scoped IM claim-button offers, callbacks, result recovery and a private +reconnect composition. Following the maintainer scope review of 2026-11-20, +that channel workflow is **deferred**: current main already owns delegation, +candidate selection, ordinary claiming, execution preparation, result return +and scoped Turn Recall, and no principal-limited in-room claim interaction is +requested this round. The deferred implementation remains recoverable in the +proposal branch history; reviving it must re-derive its deadline and +source-witness needs onto current main's reviewed managed-claim basis rather +than re-aliasing the v3 wire. + +What this proposal currently ships on the shared owner is narrower: the +canonical Todo claim transaction rejects two previously silently-accepted +unauthorized inputs for every caller, with the Lark room surface absent: + +- a Todo explicitly bound to another Agent (`bound_agent`) cannot be claimed + by a different Agent; the bound Agent claims normally; +- an explicit `goal_ref` on any claim wire is a machine-checked protocol + failure instead of being silently dropped or implying unimplemented + lifetime binding. + +Both are disclosed shared-contract tightenings validated by owner-level +File/SQLite regressions; ordinary unclaimed self-claims and same-key replay +keep their existing semantics. This checkpoint does not claim any +room-channel, multi-host, daemon or live-provider acceptance. + ## Open Questions 1. Which LoopX projection fields are stable enough for the first public diff --git a/loopx/control_plane/coordination/local_authority_runtime.ts b/loopx/control_plane/coordination/local_authority_runtime.ts index b891036f9e..8a1b168ca2 100644 --- a/loopx/control_plane/coordination/local_authority_runtime.ts +++ b/loopx/control_plane/coordination/local_authority_runtime.ts @@ -61,6 +61,7 @@ import { import { COORDINATION_TODO_CLAIM_RESULT_SCHEMA, executeCoordinationTodoClaim, + unqualifiedGoalRefClaimFailure, } from "./todo_claim.ts"; import { COORDINATION_TODO_CREATE_RESULT_SCHEMA, @@ -1048,6 +1049,11 @@ export async function claimLocalCoordinationTodo( let sourceAuthority = "file_v0"; try { const input = requireJsonObject(value, "local coordination Todo claim request"); + // Wire integrity lives before the field whitelist: an explicit goal_ref + // must be rejected, not silently dropped, on every claim wire version. + const scopeFailure = unqualifiedGoalRefClaimFailure(input); + if (scopeFailure !== null) return {...scopeFailure, source_authority: null, + decision_read_from_provider: false, legacy_fallback_used: false}; const reviewed = input.schema_version === REVIEWED_MANAGED_CLAIM_REQUEST_SCHEMA; const managed = reviewed || input.schema_version === LOCAL_COORDINATION_TODO_MANAGED_CLAIM_REQUEST_SCHEMA; if (input.schema_version !== LOCAL_COORDINATION_TODO_CLAIM_REQUEST_SCHEMA && diff --git a/loopx/control_plane/coordination/todo_claim.ts b/loopx/control_plane/coordination/todo_claim.ts index 1b0c075ca0..fce122c646 100644 --- a/loopx/control_plane/coordination/todo_claim.ts +++ b/loopx/control_plane/coordination/todo_claim.ts @@ -153,6 +153,12 @@ function decisionFailure( }; } +/** The legacy claim contract must never imply unimplemented lifetime binding. */ +export function unqualifiedGoalRefClaimFailure(value: object): CoordinationTodoClaimResult | null { + return Object.hasOwn(value, "goal_ref") ? failure("goal_ref_claim_contract_unqualified", + "This claim contract does not bind the head and operation receipt to GoalRef; a qualified lifetime-bound claim owner is required.") : null; +} + function rejectInvalidClaimActor( input: CoordinationTodoClaimInput, ): ClaimRejection { @@ -200,6 +206,10 @@ function rejectIneligibleTodo( { requested_role: input.expected_role, todo_role: todo.role }, ); } + if (todo.bound_agent != null && todo.bound_agent !== "" && + normalizeTodoAgent(todo.bound_agent, "todo.bound_agent") !== input.claimed_by) { + return decisionFailure("bound_agent_mismatch", "Todo is bound to another agent"); + } if (todo.status !== "open") { return decisionFailure( "todo_not_open", @@ -366,6 +376,8 @@ async function executeClaimAttempt( ): Promise { let input: CoordinationTodoClaimInput; try { + const scopeFailure = unqualifiedGoalRefClaimFailure(rawInput); + if (scopeFailure !== null) return scopeFailure; input = { ...rawInput, goal_id: requireAuthorityStoreId(rawInput.goal_id, "goal id"), diff --git a/tests/control_plane/test_claim_owner_guards.py b/tests/control_plane/test_claim_owner_guards.py new file mode 100644 index 0000000000..f237bef861 --- /dev/null +++ b/tests/control_plane/test_claim_owner_guards.py @@ -0,0 +1,117 @@ +"""Owner-level regressions for the canonical Todo claim guards. + +These cases drive the TypeScript claim owner directly through the effect +runtime on disposable real File/SQLite stores. They hold with or without any +Lark room surface: the guards protect every caller of the canonical claim. +""" +import json +from pathlib import Path + +import pytest + +from canonical_authority_fixture import initialize_canonical_authority, isolate_sqlite_runtime +from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted +from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection +from loopx.control_plane.effect_runtime import effect_runtime_result + +GOAL = "claim-guard-fixture-goal" +TODO = "todo_claim_guard" + + +PROVIDER_AUTHORITY = {"file": "file_v0", "sqlite": "sqlite_v0"} + + +@pytest.fixture(params=["file", "sqlite"]) +def authority(tmp_path, monkeypatch, request): + isolate_sqlite_runtime(tmp_path, monkeypatch) + provider = request.param + runtime, registry, state = tmp_path / "runtime", tmp_path / ".loopx/registry.json", tmp_path / "state.md" + registry.parent.mkdir() + state.write_text("## Agent Todo\n") + registry.write_text(json.dumps({"common_runtime_root": str(runtime), "goals": [{ + "id": GOAL, "repo": str(tmp_path), "state_file": state.name, + "coordination": {"registered_agents": ["agent-a", "agent-b"]}}]})) + yield runtime, registry, state, provider + effect_runtime_result("runtime.shutdown", {}, retry_safe=False) + + +def _seed(runtime: Path, todos: list[dict], state: Path, provider: str) -> None: + projection = build_todo_runtime_shadow_projection(goal_id=GOAL, todos=todos, handoff_mode="soft_claim") + initialize_canonical_authority(runtime, GOAL, projection, state_path=state, provider=provider) + + +def _claim(runtime: Path, registry: Path, actor: str, key: str, **extra): + import hashlib + with open(registry, "rb") as handle: + source = {"path": str(registry.resolve()), + "sha256": hashlib.sha256(handle.read()).hexdigest()} + return effect_runtime_result("coordination.local_authority.todo_claim", { + "schema_version": "loopx_local_coordination_todo_claim_request_v1", + "runtime_root": str(runtime), "goal_id": GOAL, "todo_id": TODO, + "role": "agent", "claimed_by": actor, "actor_agent_id": actor, + "registered_agents": ["agent-a", "agent-b"], "registry_source": source, + "operation_id": key, "lease_request": None, "observed_at": "2026-11-23T00:00:00Z", + "dry_run": False, **extra}) + + +def _todo(runtime: Path) -> dict: + snapshot = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id=GOAL) + return next(todo for todo in snapshot["todos"] if todo["todo_id"] == TODO) + + +def _open_todo(**fields) -> dict: + return {"schema_version": "todo_item_v0", "todo_id": TODO, "role": "agent", "status": "open", + "done": False, "text": "PRIVATE_ORGANIZATIONAL_CONTEXT", "archive_state": "active", + "source_section": "Agent Todo", "index": 1, "task_class": "advancement_task", **fields} + + +def test_claim_on_todo_bound_to_another_agent_is_rejected_without_side_effects(authority): + runtime, registry, state, provider = authority + _seed(runtime, [_open_todo(bound_agent="agent-b")], state, provider) + rejected = _claim(runtime, registry, "agent-a", "guard-bound-mismatch") + assert rejected["status"] == "failed" and rejected["reason_code"] == "bound_agent_mismatch", rejected + assert rejected["decision_read_from_provider"] is True + assert rejected["source_authority"] == PROVIDER_AUTHORITY[provider], rejected + todo = _todo(runtime) + assert not todo.get("claimed_by") + receipt = effect_runtime_result("coordination.local_authority.operation_receipt", { + "schema_version": "loopx_local_coordination_operation_receipt_request_v0", + "runtime_root": str(runtime), "goal_id": GOAL, "operation_id": "guard-bound-mismatch"}) + assert receipt["status"] == "missing", receipt + # The bound agent itself still claims normally. + accepted = _claim(runtime, registry, "agent-b", "guard-bound-owner") + assert accepted["status"] == "applied", accepted + assert accepted["source_authority"] == PROVIDER_AUTHORITY[provider], accepted + assert _todo(runtime)["claimed_by"] == "agent-b" + + +def test_claim_wires_reject_an_unqualified_goal_ref_before_any_provider_effect(authority): + runtime, registry, state, provider = authority + _seed(runtime, [_open_todo()], state, provider) + for goal_ref in (None, {"goal_id": GOAL, "goal_instance_id": "ginst_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}): + rejected = _claim(runtime, registry, "agent-a", "guard-goal-ref", goal_ref=goal_ref) + assert rejected["status"] == "failed", rejected + assert rejected["reason_code"] == "goal_ref_claim_contract_unqualified", rejected + assert rejected["decision_read_from_provider"] is False + assert rejected["legacy_fallback_used"] is False + assert not _todo(runtime).get("claimed_by") + # Omitting the field keeps the ordinary claim path. + accepted = _claim(runtime, registry, "agent-a", "guard-goal-ref-clean") + assert accepted["status"] == "applied", accepted + assert accepted["source_authority"] == PROVIDER_AUTHORITY[provider], accepted + assert _todo(runtime)["claimed_by"] == "agent-a" + + +def test_ordinary_unclaimed_self_claim_and_same_key_replay_keep_room_off_parity(authority): + runtime, registry, state, provider = authority + _seed(runtime, [_open_todo()], state, provider) + first = _claim(runtime, registry, "agent-a", "guard-parity-key") + assert first["status"] == "applied" and first.get("claimed_by") == "agent-a", first + assert first["source_authority"] == PROVIDER_AUTHORITY[provider], first + snapshot = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id=GOAL) + assert snapshot["source_authority"] == PROVIDER_AUTHORITY[provider], snapshot + revision = snapshot["provider_revision"] + replay = _claim(runtime, registry, "agent-a", "guard-parity-key") + assert replay["status"] == "replayed", replay + assert replay["source_authority"] == PROVIDER_AUTHORITY[provider], replay + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id=GOAL)["provider_revision"] == revision diff --git a/tests/control_plane_ts/goal_ref_claim_compatibility.test.ts b/tests/control_plane_ts/goal_ref_claim_compatibility.test.ts new file mode 100644 index 0000000000..baf12f733c --- /dev/null +++ b/tests/control_plane_ts/goal_ref_claim_compatibility.test.ts @@ -0,0 +1,18 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; +import {executeCoordinationTodoClaim} from "../../loopx/control_plane/coordination/todo_claim.ts"; + +test("GoalRef-bearing legacy claims reject before provider or historical receipt access", async () => { + const store = new Proxy({} as AuthorityStore, {get() {throw new Error("provider must not be accessed");}}); + const base = {goal_id: "goal", todo_id: "todo", claimed_by: "agent", actor_agent_id: "agent", + registered_agents: ["agent"], expected_role: "agent", operation_id: "old-operation", dry_run: false, + now: new Date("2026-09-29T00:00:00Z")}; + for (const goalRef of [null, "invalid", {goal_id: "goal", goal_instance_id: "ginst_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]) { + const request = {...base, goal_ref: goalRef}; + const result = await executeCoordinationTodoClaim(store, request); + assert.equal(result.status, "failed"); + assert.equal(result.reason_code, "goal_ref_claim_contract_unqualified"); + assert.equal(result.failure_kind, "protocol_failure"); + } +});