Skip to content

fix(box): guard InstanceRepair.record and revert on exact owner class - #1050

Draft
haoruilee wants to merge 2 commits into
InferMatrixLab:mainfrom
haoruilee:fix/box-instance-repair-derived-guard
Draft

haoruilee wants to merge 2 commits into
InferMatrixLab:mainfrom
haoruilee:fix/box-instance-repair-derived-guard

Conversation

@haoruilee

@haoruilee haoruilee commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Governing Issue and delivery slice

Refs #1048

  • Accepted scope / delegation: Proposed in bug(box): guard InstanceRepair.record and revert against derived or class-reassigned instances #1048; this PR remains Draft until Box / Runtime maintainers confirm scope, assign an accountable owner, and delegate implementation to @haoruilee.
  • Anchors touched: none
  • What this PR delivers:
    • src/inferrsi/box/kernel/repair.py:
      • Records self.after and self.prefix_length = len(before.body) in InstanceRepair.__init__() and inserts a pre-RHS exact-owner check (self.hook_name(self.receiver)) at self.prefix_length in InstanceRepair.instrument() — right after the original constructor prefix and before the first added field statement begins evaluating (preserving index-0 docstrings before witness injection).
      • Extends InstanceRepair.record(self, obj, name=MISSING, value=MISSING) to reject if type(obj) is not self.owner: via syntax.reject("repair of a class with live derived instances requires restart") on both the 1-arg pre-RHS guard (name is MISSING -> returns None) and the defensive 3-arg per-field path before _state(), _history(), _remember(), or STORE_ATTR.
      • In InstanceRepair.revert(), checks if type(obj) is not self.owner: before _instance_dict(obj) or journaling mutations and raises BoxError("in_doubt", "repaired instance changed its class"), isolating safely on both uninstall() and failed-install() rollback paths if a repaired instance had its __class__ reassigned.
    • tests/box/conformance/test_instance_repair.py:
      • Adds test_derived_instance_constructed_during_repair_is_rejected_cleanly verifying that both plain (PlainDerived) and custom-__setattr__ (CustomDerived) subclasses raise BoxError with code == "restart_required" before a computed added field RHS helper (compute_extra) is invoked (rhs_calls == []), while preserving original-constructor writes (derived.writes == ["config"], derived.registry == {"config": config}), leaving hook_name and repaired fields unwritten, and verifying exact mod.Layer construction and box.uninstall() succeed cleanly (code == "ok", not box.isolated).
      • Adds test_repaired_instance_reassigned_to_derived_class_isolates_on_uninstall verifying that reassigning a repaired instance's __class__ to GuardedDerived (tracing __getattribute__, __setattr__, __delattr__, plus wrapping repair._instance_dict) isolates on box.uninstall() with code == "in_doubt" and box.isolated, with zero attribute-hook calls, zero _instance_dict reads on the reassigned instance, and an unchanged instance dictionary.
  • What it does not deliver: Support for repairing live derived instances without restart, changes outside src/inferrsi/box/kernel/repair.py, or Issue closure.

Knowledge authorship declaration

No path protected by docs/knowledge-model-policy.md is changed. The declaration is filled for completeness against the current head.

{
  "schema_version": 1,
  "head_sha": "761adf72bbd4c250635ce36cf6d0f3aee76afa30",
  "human_only": false,
  "material_ai_authors": [
    {
      "tool": "antigravity-cli",
      "model_id": "gemini-4-neon",
      "effort": "xhigh",
      "role": "author"
    }
  ]
}

Changed artifacts and evidence

Requirement (#1048) Actual change Check / evidence and result Remaining gap
Reject derived instances constructed during an active repair before evaluating added field logic or writing repair metadata src/inferrsi/box/kernel/repair.py: InstanceRepair.instrument() inserts pre-RHS exact-owner check at self.prefix_length; InstanceRepair.record() checks if type(obj) is not self.owner: on both pre-RHS guard and per-field paths (restart_required) Before (9f6cbb01): install=ok uninstall=ok has_extra=False registry={'cfg': {'k': 2}, 'extra': 42}; after (761adf72): BoxError("restart_required") raised with rhs_calls == [], original-constructor write intact (derived.writes == ["config"]), no hook_name or added fields written, and clean uninstall() == "ok"; verified by test_derived_instance_constructed_during_repair_is_rejected_cleanly + 4-mutant kill checks + docstring/positional-only/mid-constructor flip probes on Py3.10 & Py3.11 Upstream Ready CI for 761adf72, Windows, macOS, and live NPU verification pending
Isolate with in_doubt in InstanceRepair.revert() when a repaired instance changed its class without reading or mutating the reassigned class's attribute dict src/inferrsi/box/kernel/repair.py: InstanceRepair.revert() checks if type(obj) is not self.owner: before _instance_dict(obj) and raises BoxError("in_doubt", "repaired instance changed its class") Covered by test_repaired_instance_reassigned_to_derived_class_isolates_on_uninstall (code == "in_doubt", box.isolated, attr_hooks == [], zero _instance_dict reads on fresh, unchanged __dict__) + mutant kill check + hostile __dict__/__setattr__/__getattribute__ probes on Py3.10 & Py3.11 Upstream Ready CI for 761adf72 pending

Checks and dependencies

  • Checks actually run (result and tested scope): Linux x86_64 on 761adf72bbd4c250635ce36cf6d0f3aee76afa30 (under env -u PYTHONPATH):
    • ruff check src tests tools (Python 3.10.20 & 3.11.14): passed (All checks passed!).
    • git diff origin/main..HEAD --check: passed.
    • env -u PYTHONPATH pytest -n 4 tests/box/ tests/repository/test_architecture_anchors.py: 302 passed on both Python 3.10.20 (8.35s) and Python 3.11.14 (8.74s).
    • Independent 4-mutant kill checks (unpatched 9f6cbb01 -> 2 failed; missing pre-RHS AST insert -> 1 failed; missing record() guard -> 1 failed; missing revert() guard -> 1 failed) plus 5 adversarial probes (docstring-only prefix, positional-only custom receiver + AnnAssign, mid-constructor __class__ flips in field 1 and field 2 RHS, and hostile EvilDerived with exploding __dict__/__class__/__getattribute__/__setattr__ and spoofing metaclass __eq__) passed on both Python 3.10.20 and Python 3.11.14.
    • Local tools/check_pr_references.py, tools/check_knowledge_model_policy.py, and git merge-tree --write-tree origin/main HEAD (9f6cbb01): passed.
  • Failed, skipped, timed-out or not-run checks (if applicable):
    • Not run locally: full Ready test suite (local host has git 2.43.0 < 2.45), Windows/macOS advisory host-compatibility, installed-package, dashboard-browser, live NPU runtime tests, and upstream GitHub Actions CI for 761adf72 (triggered on push; prior head b7d984b2 passed 10/10 checks in run 47256992078).
  • Dependencies / shared interfaces / integration assumptions (if applicable):
    • Base origin/main 9f6cbb013839384a032a27e103318b30de35d56c. Zero file overlap with open PRs; pre-install _instances and BoxError protocol contracts are unchanged.

Remaining work and Issue closure

Check type(obj) is not self.owner at the start of InstanceRepair.record() and reject with restart_required before writing repair metadata or recording constructor fields on derived instances. Also check type(obj) is not self.owner in InstanceRepair.revert() before reading _instance_dict() and raise in_doubt when a repaired instance changed its class.

Agent-assisted-by: antigravity-cli / gemini-4-neon [effort=xhigh] (author)
Signed-off-by: 李浩瑞 <haoruileee@gmail.com>
@Dong1017 Dong1017 added the area:harness Harness module: profile, A/B loop, quality gates, evidence reports label Dec 4, 2026
Insert an exact-owner check at self.prefix_length in InstanceRepair.instrument() so derived subclasses reject with restart_required after the original constructor prefix and before evaluating any added field RHS expression, while preserving index-0 docstrings and keeping defensive exact-owner guards in InstanceRepair.record() and InstanceRepair.revert().

Strengthen conformance tests in test_instance_repair.py to verify added RHS expressions are not called on derived construction, custom setter writes distinguish original-constructor writes from patch-added writes, and post-install __class__ reassignment isolates with in_doubt with zero attribute-hook or _instance_dict accesses.

Agent-assisted-by: antigravity-cli / gemini-4-neon [effort=xhigh] (author)
Signed-off-by: 李浩瑞 <haoruileee@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:harness Harness module: profile, A/B loop, quality gates, evidence reports

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants