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
29 changes: 28 additions & 1 deletion loopx/control_plane/coordination/todo_lifecycle_decision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,17 @@ function authority(request: LifecycleDecisionRequest):
if (actor !== null) {
const rejection = registeredTodoMutationRejection(todo, actor, registered);
if (rejection !== null) return result("rejected", rejection);
} else if (todo.claimed_by !== null || todo.bound_agent !== null ||
return { mode: "single_agent_compatibility", ownershipGate: "not_required" };
}
// Single-agent goals have no second agent to act for the owner, and the
// terminal CLI has no human owner actor, so an actorless owner decision
// closing an exact linked user gate uses the same override as multi-agent
// mode. The override requires the exact decision scope link; unlinked
// owned/bound/excluded records still require an accountable actor.
if (exactUserGateOverride(request)) {
return { mode: "exact_user_gate_decision_scope_override", ownershipGate: "not_required" };
}
if (todo.claimed_by !== null || todo.bound_agent !== null ||
todo.blocks_agent !== null || todo.excluded_agents.length > 0) {
// Actorless compatibility is limited to genuinely unowned Todo records;
// ownership, binding, and exclusion facts require an accountable actor.
Expand Down Expand Up @@ -468,6 +478,23 @@ function terminalFence(
? {...lease, active: false, status: "released"} : null,
});
}
// In single-agent goals there is no peer set to attribute an execution
// lease across, and the terminal CLI has no human owner actor: an owner
// decision on an exact linked user gate records a decision rather than
// executing work, and retires any expired lease lineage with the Todo
// transition. Multi-agent hard-lease goals keep their attributed-actor
// gate contract, registered actors keep the ordinary lease requirement, and
// an active holder or an explicitly supplied fence is never bypassed.
if (request.registered_agents.length <= 1 && request.command === "complete" &&
authorityMode === "exact_user_gate_decision_scope_override" &&
request.todo.role === "user" && request.todo.task_class === "user_gate" &&
request.actor_agent_id === null && !timeActive && !explicitFence) {
return result("apply", "terminal_fence_not_required", {
authority_mode: authorityMode, lease_fence: "not_required",
next_lease: lease?.present && lease.status !== "released"
? {...lease, active: false, status: "released"} : null,
});
}
// Deferred work cannot acquire a lease. Superseding a retired wait is a
// terminal lifecycle edit, not execution, and the provider CAS retires any
// expired lease lineage together with the Todo transition.
Expand Down
76 changes: 75 additions & 1 deletion tests/control_plane/test_canonical_handoff_invariants.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def _write_workspace(
*,
handoff_mode: str | None = None,
lifecycle_authority: list[dict[str, Any]] | None = None,
registered_agents: list[str] | None = None,
) -> tuple[Path, Path]:
repo = tmp_path / "repo"
repo.mkdir(exist_ok=True)
Expand All @@ -76,7 +77,11 @@ def _write_workspace(
)
coordination: dict[str, Any] = {
"agent_model": "peer_v1",
"registered_agents": [AGENT_A, AGENT_B, ORCHESTRATOR],
"registered_agents": (
[AGENT_A, AGENT_B, ORCHESTRATOR]
if registered_agents is None
else list(registered_agents)
),
}
if lifecycle_authority is not None:
coordination["todo_lifecycle_authority"] = lifecycle_authority
Expand Down Expand Up @@ -777,6 +782,75 @@ def test_exact_user_gate_completion_preserves_execution_fence(
assert result["decision_scope_resolution"]["state"] == "resolved"


@pytest.mark.parametrize("mode", ["soft_claim", "hard_lease"])
@pytest.mark.parametrize(
"agents",
[[AGENT_A], [AGENT_A, AGENT_B, ORCHESTRATOR]],
ids=["single-agent", "multi-agent"],
)
def test_owner_closes_exact_user_gate_without_an_agent_actor(
tmp_path, canonical_workspace, mode, agents
):
"""The owner who is not registered as an agent can close a single-agent gate.

A single-agent goal has no second agent to act for the owner, and the
terminal CLI has no human owner actor. Multi-agent hard-lease goals retain
their attributed-actor execution fence contract when no actor is named.
"""
registry, state = canonical_workspace(
tmp_path, handoff_mode=mode, registered_agents=agents
)
target, gate = _add_exact_user_gate_pair(registry)
unlinked = add_goal_todo(
registry_path=registry,
goal_id=GOAL_ID,
role="user",
text="Confirm one unlinked owner decision.",
task_class="user_gate",
blocks_agent=AGENT_A,
)
before, display = _snapshot(registry), state.read_bytes()
with pytest.raises(LocalCoordinationAuthorityRejection) as error:
_terminal(
registry,
unlinked["todo_id"],
role="user",
decision_outcome="approve",
agent_id=None,
)
assert error.value.code == "actor_required"
_unchanged(registry, state, before, display)

if mode == "hard_lease" and len(agents) > 1:
with pytest.raises(LocalCoordinationAuthorityRejection) as error:
_terminal(
registry,
gate["todo_id"],
role="user",
decision_outcome="approve",
agent_id=None,
)
assert error.value.code == "handoff_mode_requires_lease"
_unchanged(registry, state, before, display)
return

result = _terminal(
registry,
gate["todo_id"],
role="user",
decision_outcome="approve",
agent_id=None,
)
assert (
result["mutation_authority"]["authority_mode"]
== "exact_user_gate_decision_scope_override"
)
assert result["task_lease_fence"]["lease_fence"] == "not_required"
assert _agent_todo(state, gate["todo_id"])["status"] == "done"
assert not _agent_todo(state, target["todo_id"]).get("required_decision_scopes")
assert result["decision_scope_resolution"]["state"] == "resolved"


def test_bound_user_action_completes_without_gate_or_execution_claim(
tmp_path, canonical_workspace
):
Expand Down
193 changes: 193 additions & 0 deletions tests/control_plane_ts/todo_terminal_decision.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,199 @@ test("exact linked user gate decision scope does not invent Agent authority", ()
assert.equal(mismatch.code, "actor_required");
});

test("single-agent goals consult the exact user gate override for owner decisions", () => {
const scope = {kind: "direction", granularity: "action", scope_key: "release"};
const gate = {
todo_id: "todo_gate",
status: "open",
role: "user",
task_class: "user_gate",
claimed_by: null,
excluded_agents: [],
bound_agent: "agent-a",
blocks_agent: null,
decision_scope: scope,
required_decision_scopes: [],
unblocks_todo_id: "todo_target",
};
const target = {
...request().todo as object,
todo_id: "todo_target",
required_decision_scopes: [scope],
};
// Single registered agent: an owner decision (no agent actor) closing the
// exact linked gate must use the override instead of failing actor admission.
const accepted = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
todo: gate,
decision_target: target,
actor_agent_id: null,
decision_outcome: "approve",
}));
assert.equal(accepted.outcome, "apply");
assert.equal(accepted.authority_mode, "exact_user_gate_decision_scope_override");

// Without the exact scope link the override does not fire: the ownership
// facts still require an accountable actor.
const unlinked = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
todo: gate,
decision_target: { ...target, required_decision_scopes: [] },
actor_agent_id: null,
decision_outcome: "approve",
}));
assert.equal(unlinked.outcome, "rejected");
assert.equal(unlinked.code, "actor_required");

// Registered actors keep ordinary single-agent compatibility, while
// unregistered or excluded actors remain rejected even when the scope link
// matches.
const viaActor = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
todo: gate,
decision_target: target,
actor_agent_id: "agent-a",
decision_outcome: "approve",
}));
assert.equal(viaActor.outcome, "apply");
assert.equal(viaActor.authority_mode, "single_agent_compatibility");

const unregistered = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
todo: gate,
decision_target: target,
actor_agent_id: "bogus",
decision_outcome: "approve",
}));
assert.equal(unregistered.outcome, "rejected");
assert.equal(unregistered.code, "actor_not_registered");

const excluded = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
todo: { ...gate, excluded_agents: ["agent-a"] },
decision_target: target,
actor_agent_id: "agent-a",
decision_outcome: "approve",
}));
assert.equal(excluded.outcome, "rejected");
assert.equal(excluded.code, "actor_excluded");
});

test("actorless owner gate decisions do not require an execution lease", () => {
const scope = {kind: "direction", granularity: "action", scope_key: "release"};
const gate = {
todo_id: "todo_gate",
status: "open",
role: "user",
task_class: "user_gate",
claimed_by: null,
excluded_agents: [],
bound_agent: null,
blocks_agent: "agent-a",
decision_scope: scope,
required_decision_scopes: [],
unblocks_todo_id: "todo_target",
};
const target = {
...request().todo as object,
todo_id: "todo_target",
required_decision_scopes: [scope],
};
// A single-agent hard-lease goal has no peer set or agent actor to hold a
// lease for an owner decision: the gate records a decision, it does not execute work.
const owner = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
handoff_mode: "hard_lease",
todo: gate,
decision_target: target,
actor_agent_id: null,
decision_outcome: "approve",
}));
assert.equal(owner.outcome, "apply");
assert.equal(owner.authority_mode, "exact_user_gate_decision_scope_override");
assert.equal(owner.lease_fence, "not_required");
assert.equal(owner.next_lease, null);

// A registered actor keeps the ordinary lease requirement: the owner
// exception never widens an agent's execution admission.
const actor = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
handoff_mode: "hard_lease",
todo: gate,
decision_target: target,
actor_agent_id: "agent-a",
decision_outcome: "approve",
}));
assert.equal(actor.outcome, "rejected");
assert.equal(actor.code, "handoff_mode_requires_lease");
assert.equal(actor.lease_fence, "required");

// Any expired lease lineage is retired together with the Todo transition,
// while an active holder or explicit lease fence is never bypassed.
const expiredLease = {
present: true,
active: false,
status: "active",
owner: "agent-a",
idempotency_key: "old-lease",
version: 2,
lease_epoch: 1,
write_scopes: [],
};
const retired = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
handoff_mode: "hard_lease",
todo: gate,
decision_target: target,
lease: expiredLease,
actor_agent_id: null,
decision_outcome: "approve",
}));
assert.equal(retired.outcome, "apply");
assert.equal(retired.lease_fence, "not_required");
assert.equal(retired.next_lease?.status, "released");

const activeHolder = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
handoff_mode: "hard_lease",
todo: gate,
decision_target: target,
lease: { ...expiredLease, active: true },
actor_agent_id: null,
decision_outcome: "approve",
}));
assert.equal(activeHolder.outcome, "rejected");
assert.equal(activeHolder.code, "lease_fence_required");

const explicitFence = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a"],
handoff_mode: "hard_lease",
todo: gate,
decision_target: target,
actor_agent_id: null,
decision_outcome: "approve",
lease_idempotency_key: "stale-key",
}));
assert.equal(explicitFence.outcome, "rejected");
assert.equal(explicitFence.code, "handoff_mode_requires_lease");

// Multi-agent hard-lease goals preserve their attributed-actor contract
// even when auto-acquire is enabled for the gate.
for (const allowAuto of [false, true]) {
const multiAgent = evaluateCoordinationTodoTerminalDecision(request({
registered_agents: ["agent-a", "agent-b"],
handoff_mode: "hard_lease",
allow_user_gate_auto_acquire: allowAuto,
todo: gate,
decision_target: target,
actor_agent_id: null,
decision_outcome: "approve",
}));
assert.equal(multiAgent.outcome, "rejected");
assert.equal(multiAgent.code, "handoff_mode_requires_lease");
}
});

test("hard-lease terminal transition releases the exact owned generation", () => {
const lease = {
present: true,
Expand Down
Loading