Files
aios/tests/task_plane/test_legacy_attachment.py
T
AIOS Governed Continuation 11944ef3f4 test(harness): fix V2R2C3 test import-layering & harness defects (production-consistent)
Successor test-harness fix on governed source 5cf20128 (tree c5368428).
Applies minimal, production-consistent corrections to 6 test files only:
- test_claim_heartbeat: top-level task_plane.* -> app.task_plane.* (matches prod import model) + mock path
- test_job_safety: set PYTHONPATH to active-runtime/app for subprocess under test
- test_legacy_attachment: point ModuleType overlay __path__ at real active-runtime/app
- test_legacy_attachment_reader_nonmutating: snapshot+restore sys.modules[app.*] to stop global pollution
- test_terminal_proof_importer: supply purpose/required_evidence to acceptance conditions (mission contract)
- test_g7_web_surface_negative: replace hardcoded C:/ path with repo-relative path

No production source changes. Task: V2R2C3 successor harness repair (governed).

task/V2R2C3-succ-harness-20260915
2026-09-15 11:01:01 +08:00

634 lines
23 KiB
Python

from __future__ import annotations
import concurrent.futures
import hashlib
import sys
import threading
import types
from copy import deepcopy
from dataclasses import replace
from pathlib import Path
import pytest
# Exercise the overlay exactly as deployment will see it: changed modules from
# this artifact plus unchanged dependencies from the checked-out repository.
_APP_DIR = Path(__file__).resolve().parents[2] / "active-runtime" / "app"
# Production resolves ``app`` via the checkout-root symlink ``app -> active-runtime/app``
# (and tests/conftest.py places active-runtime/app on sys.path). Keep the overlay
# intent (changed modules + unchanged dependencies from the checked-out repo) by
# pointing the ``app``/``app.task_plane`` namespace at the REAL active-runtime/app,
# never at non-existent source//repo/ dirs (which poisoned sys.modules and broke
# collection of the other app.*-qualified tests in the full suite).
_app = types.ModuleType("app")
_app.__path__ = [str(_APP_DIR)]
_app.__package__ = "app"
sys.modules["app"] = _app
_task_plane = types.ModuleType("app.task_plane")
_task_plane.__path__ = [str(_APP_DIR / "task_plane")]
_task_plane.__package__ = "app.task_plane"
sys.modules["app.task_plane"] = _task_plane
from app.aios_task_mission_authority_server import (
AUDIENCE,
SignedMissionIngress,
canonical_bytes,
)
from app.task_plane.api import TaskPlaneService
from app.task_plane.errors import ConflictError, ForbiddenError
from app.task_plane.legacy_attachment import (
ATTACH_EVIDENCE_SCHEMA,
ATTACH_REQUEST_SCHEMA,
AuthorityReadbackRef,
VerifiedLegacyAttachmentEvidence,
)
from app.task_plane.mission import MissionAuthority, MissionPrincipalBinding
from app.task_plane.policy import PolicyEngine
from app.task_plane.store import TaskPlaneStore
OWNER = "local-owner-authority"
OTHER = "other-owner-authority"
LEGACY_TASK = "TASK-P0-CHATGPT-BRIDGE-V2-CONTROL-PLANE-CONVERGENCE-INTEGRATE-20260913T184412Z-19b7"
EXTERNAL_JOB = "job-7457a322d5f9d6931c7cd99ce3ec6c18"
INTERNAL_JOB = "job-1eaae20deef71a85638f36926091392b"
EXECUTION_KEY = "1eaae20deef71a85638f36926091392b1032de9b302457912fca4ac14ec93b3b"
ACCEPTANCE = {
"schema": "identity-lineage-root-acceptance.v1",
"conditions": [
{
"key": "C04",
"purpose": "LEGACY-LINEAGE-MIGRATION",
"required_evidence": [
"verified legacy ownership and acceptance relevance",
"unchanged task/job/idempotency/reconciliation identity",
"authoritative attach readback",
],
}
],
"legacy_candidate": {"task_id": LEGACY_TASK, "job_id": EXTERNAL_JOB},
}
def evidence() -> VerifiedLegacyAttachmentEvidence:
refs = tuple(
AuthorityReadbackRef(
role=role,
authority=f"authority-{role}",
readback_ref=f"readonly:{role}:stable-ref",
canonical_sha256=hashlib.sha256(role.encode()).hexdigest(),
observed_at="2026-09-14T01:00:00Z",
revision=4 if role in {"agent_task", "bridge_job"} else 1,
)
for role in ("agent_task", "admission", "owner_registry", "bridge_job")
)
return VerifiedLegacyAttachmentEvidence(
schema=ATTACH_EVIDENCE_SCHEMA,
source_kind="agent-task-bridge-job",
source_task_id=LEGACY_TASK,
source_task_created_at="2026-09-13T18:44:12Z",
source_task_status="EXPIRED",
source_task_revision=4,
source_task_attempt=1,
source_task_max_attempts=1,
external_job_id=EXTERNAL_JOB,
internal_job_id=INTERNAL_JOB,
internal_idempotency_key=EXECUTION_KEY,
job_branch="refs/heads/task/legacy-recovery",
job_state="RECONCILIATION_REQUIRED",
job_revision=4,
side_effect_state="EXECUTION_OUTCOME_UNKNOWN",
grant_id="grant-historic",
grant_digest="a" * 64,
execution_digest="b" * 64,
epoch_id="epoch-historic",
epoch_digest="c" * 64,
historical_signer_id=OWNER,
historical_signer_fingerprint="owner-key-fingerprint",
registry_generation=7,
signature_verified=True,
epoch_currently_valid=False,
readbacks=refs,
)
class Provider:
def __init__(self, value=None):
self.value = value or evidence()
self.calls = []
def read_verified(self, **request):
self.calls.append(deepcopy(request))
return self.value
def service(path: Path, provider=...):
store = TaskPlaneStore(str(path))
bindings = [
MissionPrincipalBinding(
authenticated_identity=identity,
owner_principal_id=identity,
acceptance_verifier_id="identity-lineage-live-closure-v1",
preferred_owner_alias="LOCALOWNER" if identity == OWNER else "OTHEROWNER",
allowed_task_capabilities=("code.read",),
max_risk="low",
)
for identity in (OWNER, OTHER)
]
kwargs = {} if provider is ... else {"legacy_attachment_evidence_provider": provider}
authority = MissionAuthority(store, principal_bindings=bindings, **kwargs)
return TaskPlaneService(
store,
policy=PolicyEngine(
execution_enabled=False,
identity_capabilities={
OWNER: {"task.creator", "task.read", "task.delegate"},
OTHER: {"task.creator", "task.read", "task.delegate"},
},
),
mission_authority=authority,
)
def prepare(
svc,
mission_key="mission-create",
anchor_key="c04-anchor",
*,
canonical_contract=False,
):
mission = svc.create_mission(
{"goal_slug": "restore-canonical-task-lineage", "original_acceptance": ACCEPTANCE},
mission_key,
actor_identity=OWNER,
)
anchor = svc.delegate_mission(
mission["root_task_id"],
{
"task_type": "mission.acceptance",
"intent": (
"LEGACY-LINEAGE-MIGRATION"
if canonical_contract
else "attach verified legacy execution subject"
),
"risk": "low",
"required_capabilities": ["code.read"],
"metadata": (
{
"acceptance_condition": "C04",
"required_evidence": ACCEPTANCE["conditions"][0][
"required_evidence"
],
}
if canonical_contract
else {"acceptance_condition": "C04"}
),
},
anchor_key,
actor_identity=OWNER,
)
root = svc.store.get_task(mission["root_task_id"])
anchor_record = svc.store.get_task(anchor["task_id"])
request = {
"schema": ATTACH_REQUEST_SCHEMA,
"anchor_task_id": anchor["task_id"],
"source": {
"kind": "agent-task-bridge-job",
"task_id": LEGACY_TASK,
"external_job_id": EXTERNAL_JOB,
},
"expected": {
"root_revision": root.revision,
"anchor_revision": anchor_record.revision,
"agent_task_revision": 4,
"agent_task_attempt": 1,
"internal_job_id": INTERNAL_JOB,
"job_revision": 4,
"internal_idempotency_key": EXECUTION_KEY,
},
}
return mission, anchor, request
def test_attach_persists_recovery_display_and_preserves_unknown_source(tmp_path):
provider = Provider()
svc = service(tmp_path / "core.db", provider)
mission, anchor, request = prepare(svc)
tasks_before = {
row["task_id"]: row["task_json"]
for row in svc.store.conn.execute("SELECT task_id,task_json FROM tasks")
}
provider_before = deepcopy(provider.value.to_dict())
receipt = svc.attach_legacy_attachment(
mission["mission_id"], request, "attach-once", actor_identity=OWNER
)
assert receipt["attachment_kind"] == "LEGACY_EXECUTION_SUBJECT"
assert receipt["relation_type"] == "RECOVERY"
assert receipt["anchor_task_id"] == anchor["task_id"]
assert receipt["display_name"].startswith("TASK-LOCALOWNER-20260913T184412Z-R")
assert receipt["display_name"].endswith("-RECOVERY-412Z19B7")
assert receipt["source"]["source_task_id"] == LEGACY_TASK
assert receipt["source"]["job_state"] == "RECONCILIATION_REQUIRED"
assert receipt["source"]["side_effect_state"] == "EXECUTION_OUTCOME_UNKNOWN"
assert receipt["source"]["internal_job_id"] == INTERNAL_JOB
assert receipt["source"]["internal_idempotency_key"] == EXECUTION_KEY
assert receipt["mutation_scope"] == "MISSION_ATTACHMENT_ONLY"
assert receipt["source_mutated"] is False
assert receipt["reconciliation_executed"] is False
assert receipt["terminal_status_granted"] is False
assert provider.value.to_dict() == provider_before
assert tasks_before == {
row["task_id"]: row["task_json"]
for row in svc.store.conn.execute("SELECT task_id,task_json FROM tasks")
}
readback = svc.get_legacy_attachment(
mission["mission_id"], receipt["attachment_id"], actor_identity=OWNER
)
assert readback["receipt_digest"] == receipt["receipt_digest"]
assert readback["authoritative_readback"]["ledger_verified"] is True
assert readback["authoritative_readback"]["identity_match"] is True
svc.store.close()
def test_response_loss_restart_exact_replay_works_without_provider(tmp_path):
path = tmp_path / "restart.db"
provider = Provider()
first = service(path, provider)
mission, _, request = prepare(first)
written = first.attach_legacy_attachment(
mission["mission_id"], request, "lost-response", actor_identity=OWNER
)
first.store.close()
restarted = service(path, None)
replay = restarted.attach_legacy_attachment(
mission["mission_id"], request, "lost-response", actor_identity=OWNER
)
assert replay["idempotent"] is True
assert replay["attachment_id"] == written["attachment_id"]
assert replay["receipt_digest"] == written["receipt_digest"]
assert restarted.store.conn.execute(
"SELECT COUNT(*) FROM task_mission_legacy_attachments"
).fetchone()[0] == 1
with pytest.raises(ForbiddenError, match="provider is not configured"):
restarted.get_legacy_attachment(
mission["mission_id"], written["attachment_id"], actor_identity=OWNER
)
restarted.store.close()
reader = service(path, Provider())
assert reader.get_legacy_attachment(
mission["mission_id"], written["attachment_id"], actor_identity=OWNER
)["source"]["job_state"] == "RECONCILIATION_REQUIRED"
reader.store.close()
def test_fail_closed_conflicts_closed_root_and_other_owner_do_not_write(tmp_path):
svc = service(tmp_path / "guards.db", None)
mission, _, request = prepare(svc)
with pytest.raises(ForbiddenError, match="provider is not configured"):
svc.attach_legacy_attachment(
mission["mission_id"], request, "no-provider", actor_identity=OWNER
)
with pytest.raises(ForbiddenError):
svc.attach_legacy_attachment(
mission["mission_id"], request, "other-owner", actor_identity=OTHER
)
svc.store.conn.execute(
"UPDATE tasks SET status='COMPLETED' WHERE task_id=?",
(mission["root_task_id"],),
)
svc.missions._legacy_attachment_provider = Provider()
with pytest.raises(ConflictError, match="completed root"):
svc.attach_legacy_attachment(
mission["mission_id"], request, "closed-root", actor_identity=OWNER
)
assert svc.store.conn.execute(
"SELECT COUNT(*) FROM task_mission_legacy_attachments"
).fetchone()[0] == 0
svc.store.close()
def test_provider_revision_or_historical_owner_mismatch_never_writes(tmp_path):
path = tmp_path / "provider-mismatch.db"
mismatched = replace(evidence(), historical_signer_id=OTHER)
svc = service(path, Provider(mismatched))
mission, _, request = prepare(svc)
with pytest.raises(ForbiddenError, match="historical Owner"):
svc.attach_legacy_attachment(
mission["mission_id"], request, "wrong-owner", actor_identity=OWNER
)
svc.missions._legacy_attachment_provider = Provider(
replace(evidence(), job_revision=5)
)
with pytest.raises(ConflictError, match="provider evidence mismatch"):
svc.attach_legacy_attachment(
mission["mission_id"], request, "wrong-revision", actor_identity=OWNER
)
assert svc.store.conn.execute(
"SELECT COUNT(*) FROM task_mission_legacy_attachments"
).fetchone()[0] == 0
svc.store.close()
def test_concurrent_exact_replay_and_source_uniqueness(tmp_path):
path = tmp_path / "concurrent.db"
first_provider, second_provider = Provider(), Provider()
left = service(path, first_provider)
mission, _, request = prepare(left)
right = service(path, second_provider)
def attach(svc):
return svc.attach_legacy_attachment(
mission["mission_id"], request, "concurrent-key", actor_identity=OWNER
)
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:
receipts = list(pool.map(attach, (left, right)))
assert {item["attachment_id"] for item in receipts}.__len__() == 1
assert {item["idempotent"] for item in receipts} == {False, True}
assert left.store.conn.execute(
"SELECT COUNT(*) FROM task_mission_legacy_attachments"
).fetchone()[0] == 1
with pytest.raises(ConflictError, match="already attached"):
left.attach_legacy_attachment(
mission["mission_id"], request, "different-key", actor_identity=OWNER
)
other_mission, _, other_request = prepare(left, "mission-two", "c04-two")
with pytest.raises(ConflictError, match="already attached"):
left.attach_legacy_attachment(
other_mission["mission_id"],
other_request,
"different-mission",
actor_identity=OWNER,
)
with pytest.raises(ConflictError, match="replay conflict"):
left.attach_legacy_attachment(
mission["mission_id"],
{**request, "expected": {**request["expected"], "job_revision": 5}},
"concurrent-key",
actor_identity=OWNER,
)
left.store.close()
right.store.close()
def test_acceptance_child_binding_rejects_spoof_and_duplicate(tmp_path):
svc = service(tmp_path / "binding.db", Provider())
mission, anchor, _ = prepare(svc)
binding = svc.store.conn.execute(
"SELECT * FROM task_mission_acceptance_child_bindings "
"WHERE mission_id=? AND condition_key='C04'",
(mission["mission_id"],),
).fetchone()
assert binding["child_task_id"] == anchor["task_id"]
with pytest.raises(ConflictError, match="binding is ambiguous"):
svc.delegate_mission(
mission["root_task_id"],
{
"task_type": "mission.acceptance",
"intent": "unrelated-spoof-child",
"risk": "low",
"required_capabilities": ["code.read"],
"metadata": {
"acceptance_condition": "C04",
"required_evidence": ACCEPTANCE["conditions"][0][
"required_evidence"
],
},
},
"spoof-c04",
actor_identity=OWNER,
)
with pytest.raises(ConflictError, match="ambiguous"):
svc.delegate_mission(
mission["root_task_id"],
{
"task_type": "mission.acceptance",
"intent": "LEGACY-LINEAGE-MIGRATION",
"risk": "low",
"required_capabilities": ["code.read"],
"metadata": {
"acceptance_condition": "C04",
"required_evidence": ACCEPTANCE["conditions"][0][
"required_evidence"
],
},
},
"duplicate-c04",
actor_identity=OWNER,
)
assert svc.store.list_children(mission["root_task_id"]) == [anchor["task_id"]]
svc.store.close()
def test_existing_canonical_child_is_backfilled_without_recreation(tmp_path):
path = tmp_path / "binding-backfill.db"
first = service(path, Provider())
mission, anchor, request = prepare(first, canonical_contract=True)
first.store.conn.execute(
"DELETE FROM task_mission_acceptance_child_bindings WHERE mission_id=?",
(mission["mission_id"],),
)
task_ids = set(first.store.list_children(mission["root_task_id"]))
first.store.close()
restarted = service(path, Provider())
receipt = restarted.attach_legacy_attachment(
mission["mission_id"], request, "backfill-existing", actor_identity=OWNER
)
binding = restarted.store.conn.execute(
"SELECT * FROM task_mission_acceptance_child_bindings "
"WHERE mission_id=? AND condition_key='C04'",
(mission["mission_id"],),
).fetchone()
assert binding["child_task_id"] == anchor["task_id"]
assert receipt["anchor_task_id"] == anchor["task_id"]
assert set(restarted.store.list_children(mission["root_task_id"])) == task_ids
restarted.store.close()
def test_condition_binding_is_generic_not_c04_only(tmp_path):
svc = service(tmp_path / "generic-binding.db", Provider())
acceptance = deepcopy(ACCEPTANCE)
acceptance["conditions"].insert(
0,
{
"key": "C03",
"purpose": "ROOT-ACTION-EXECUTOR-BINDING",
"required_evidence": ["real executor receipt"],
},
)
mission = svc.create_mission(
{"goal_slug": "generic-condition-binding", "original_acceptance": acceptance},
"generic-binding-mission",
actor_identity=OWNER,
)
child = svc.delegate_mission(
mission["root_task_id"],
{
"task_type": "mission.acceptance",
"intent": "ROOT-ACTION-EXECUTOR-BINDING",
"risk": "low",
"required_capabilities": ["code.read"],
"metadata": {
"acceptance_condition": "C03",
"required_evidence": ["real executor receipt"],
},
},
"generic-c03-child",
actor_identity=OWNER,
)
binding = svc.store.conn.execute(
"SELECT * FROM task_mission_acceptance_child_bindings "
"WHERE mission_id=? AND condition_key='C03'",
(mission["mission_id"],),
).fetchone()
assert binding["child_task_id"] == child["task_id"]
svc.store.close()
def test_preexisting_ambiguous_c04_children_fail_closed_before_provider(tmp_path):
provider = Provider()
svc = service(tmp_path / "preexisting-ambiguous.db", provider)
mission, _, request = prepare(svc)
svc.store.conn.execute(
"DELETE FROM task_mission_acceptance_child_bindings WHERE mission_id=?",
(mission["mission_id"],),
)
binder = svc.missions._bind_declared_acceptance_child
svc.missions._bind_declared_acceptance_child = lambda *_args, **_kwargs: None
try:
svc.delegate_mission(
mission["root_task_id"],
{
"task_type": "mission.acceptance",
"intent": "unrelated-spoof-child",
"risk": "low",
"required_capabilities": ["code.read"],
"metadata": {
"acceptance_condition": "C04",
"required_evidence": ACCEPTANCE["conditions"][0][
"required_evidence"
],
},
},
"pre-v2-spoof-c04",
actor_identity=OWNER,
)
finally:
svc.missions._bind_declared_acceptance_child = binder
with pytest.raises(ConflictError, match="binding is ambiguous"):
svc.attach_legacy_attachment(
mission["mission_id"], request, "ambiguous-attach", actor_identity=OWNER
)
assert provider.calls == []
assert svc.store.conn.execute(
"SELECT COUNT(*) FROM task_mission_legacy_attachments"
).fetchone()[0] == 0
svc.store.close()
def test_duplicate_acceptance_admission_racing_attach_rolls_back(tmp_path):
path = tmp_path / "binding-race.db"
entered = threading.Event()
release = threading.Event()
class BlockingProvider(Provider):
def read_verified(self, **request):
entered.set()
assert release.wait(timeout=5)
return super().read_verified(**request)
left = service(path, BlockingProvider())
mission, anchor, request = prepare(left)
right = service(path, Provider())
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:
attached = pool.submit(
left.attach_legacy_attachment,
mission["mission_id"],
request,
"binding-race-attach",
actor_identity=OWNER,
)
assert entered.wait(timeout=5)
duplicate = pool.submit(
right.delegate_mission,
mission["root_task_id"],
{
"task_type": "mission.acceptance",
"intent": "LEGACY-LINEAGE-MIGRATION",
"risk": "low",
"required_capabilities": ["code.read"],
"metadata": {
"acceptance_condition": "C04",
"required_evidence": ACCEPTANCE["conditions"][0][
"required_evidence"
],
},
},
"binding-race-duplicate",
actor_identity=OWNER,
)
with pytest.raises(ConflictError, match="binding is ambiguous"):
duplicate.result(timeout=5)
release.set()
receipt = attached.result(timeout=5)
assert receipt["anchor_task_id"] == anchor["task_id"]
assert left.store.list_children(mission["root_task_id"]) == [anchor["task_id"]]
assert left.store.conn.execute(
"SELECT COUNT(*) FROM task_mission_acceptance_child_bindings"
).fetchone()[0] == 1
assert left.store.conn.execute(
"SELECT COUNT(*) FROM task_mission_legacy_attachments"
).fetchone()[0] == 1
left.store.close()
right.store.close()
class OwnerVerifier:
signer_id = OWNER
def verify(self, *, namespace, payload, signature, key_id):
return key_id == "owner-key" and signature == hashlib.sha256(payload).hexdigest()
def signed(method, path, body, key):
request = {
"schema": "aios.signed-mission-request.v1",
"audience": AUDIENCE,
"method": method,
"path": path,
"body": body,
"idempotency_key": key,
"issued_at": 100,
"expires_at": 200,
"nonce": "legacy_attach_nonce_1234",
}
return {
"request": request,
"key_id": "owner-key",
"signature": hashlib.sha256(canonical_bytes(request)).hexdigest(),
}
def test_real_signed_ingress_post_get_and_replay_status(tmp_path):
svc = service(tmp_path / "signed.db", Provider())
mission, _, request = prepare(svc)
ingress = SignedMissionIngress(svc, OwnerVerifier(), clock=lambda: 150)
path = f"/missions/{mission['mission_id']}/legacy-attachments"
status, first = ingress.handle(signed("POST", path, request, "signed-attach"))
assert status == 201
status, replay = ingress.handle(signed("POST", path, request, "signed-attach"))
assert status == 200 and replay["attachment_id"] == first["attachment_id"]
get_path = f"{path}/{first['attachment_id']}"
status, fetched = ingress.handle(signed("GET", get_path, None, None))
assert status == 200 and fetched["receipt_digest"] == first["receipt_digest"]
with pytest.raises(ForbiddenError):
ingress.handle(signed("GET", f"{path}/../bad", None, None))
svc.store.close()