Skip to content

Add opt-in adoption of existing solver objects to BaseFBAPkg - #172

Merged
Fxe merged 1 commit into
ModelSEED:devfrom
cshenry:propose-opt-in-adoption-of-existing-solver-objects
Nov 18, 2026
Merged

Fxe merged 1 commit into
ModelSEED:devfrom
cshenry:propose-opt-in-adoption-of-existing-solver-objects

Conversation

@cshenry

@cshenry cshenry commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

This adds a capability behind an opt-in flag. Our fork runs the scan unconditionally in __init__; it is upstreamed as opt-in instead, for the reason in the hazard section below.

The failure it exists for

Model.copy() carries a package's variables and constraints onto the copy, but the copy gets its own MSPackageManager and therefore a fresh, empty package object. Rebuilding on the copy re-adds names the solver already holds:

ContainerAlreadyContains: Container ... already contains an object with
name 'EX_cpd00027_e0_tf'

So a BaseFBAPkg subclass is simply unusable on a copied model — which matters, because copying models is routine in msmodelutl, gapfilling and ATP correction.

What this adds

BaseFBAPkg.adopt_existing_objects() rebinds a package to the matching objects already on model.solver, by matching the final underscore-delimited segment of each object's name against the package's registered types — the inverse of the name + "_" + obj_type scheme build_variable and build_constraint use. An adopt_existing=False keyword on __init__ runs it at construction time.

After adoption the package owns the copy's objects, and a subsequent build_package() is a no-op instead of a collision.

Why it is opt-in, and the hazard

The match is on the name alone, so it adopts any solver object whose name ends in _<registered type> whether or not the package created it. Measured, on a model holding an ordinary reaction called rxn00042_tf:

  • TotalFluxPkg adopts that reaction's own flux variable as the total-flux variable for rxn00042;
  • build_package() then finds the key taken and never builds the real total-flux variable or its coupling constraint;
  • nothing raises — the bound is the reaction's 1000 rather than the tf bound of 100, and the model silently optimises something other than what the package describes;
  • clear() would delete that reaction's variable from the solver outright, leaving the reaction in model.reactions with flux_expression raising KeyError.

Two-character type names (tf, pk, fu, ru) make that collision cheap to hit by accident. A test pins the hazard rather than leaving it undocumented.

So: default off, available where the caller knows the model's naming does not collide. A stricter match — recording the names a package created and adopting only those — would remove the hazard and make a default-on scan safe, but it is a wider change to build_variable and build_constraint, so this PR keeps the name-suffix match behind the flag. Request the stricter form here if you would rather have that and I will build it.

Tests

tests/fbapkg/test_basefbapkg_adoption.py, 5 cases:

  • adoption is off by default;
  • the ContainerAlreadyContains failure on a copied model that motivates the capability;
  • adoption rebinds a package on a copied model to the copy's own objects, and a rebuild is then a no-op;
  • the constructor keyword runs the scan;
  • the name-collision hazard.

3 of the 5 fail against the unpatched tree. The other two characterise existing upstream behaviour and hold on both sides — that is deliberate, since their job is to pin the default and the motivating failure, not to prove the change.

Full suite: 86 passed. pre-commit run --all-files is clean apart from the pre-existing check that executables have shebangs hook on upstream file modes.

Relationship to the other two

Independent of #170, and independent of #171 though both touch basefbapkg.py in different regions (__init__ here, clear() and a media accessor there). Whichever of the two merges second may want a trivial rebase — tell me and I will do it.

🤖 Generated with Claude Code

Model.copy() carries a package's variables and constraints onto the copy, but
the copy gets its own MSPackageManager and therefore a FRESH, empty package
object. Rebuilding on the copy then re-adds names the solver already holds:

  ContainerAlreadyContains: Container ... already contains an object with
  name 'EX_cpd00027_e0_tf'

so a package is unusable on a copied model. This adds
BaseFBAPkg.adopt_existing_objects(), which rebinds a package to the matching
objects already on model.solver by matching the final underscore-delimited
segment of each name against the package's registered types -- the inverse of
the `name + "_" + obj_type` scheme the build methods use. An
`adopt_existing=False` keyword on __init__ runs it at construction.

It is OPT-IN, and the default is off, so nothing changes for existing callers.
The reason is that the match is on the name alone, so it adopts any solver
object whose name ends in `_<registered type>` whether or not the package
created it. Measured: on a model holding an ordinary reaction `rxn00042_tf`,
TotalFluxPkg adopts that reaction's own flux variable as the total-flux
variable for `rxn00042`; build_package then finds the key taken and never
builds the real variable or its coupling constraint, with no error raised, and
clear() would delete the reaction's variable from the solver outright. A test
pins that hazard rather than leaving it undocumented.

Tests: tests/fbapkg/test_basefbapkg_adoption.py, 5 cases -- the default being
off, the ContainerAlreadyContains failure that motivates the capability,
adoption rebinding a package on a copied model and leaving a rebuild a no-op,
the constructor keyword, and the name-collision hazard. 3 of the 5 fail against
the unpatched tree; the other two characterise existing behaviour and hold on
both sides, which is why they are there.

Full suite: 86 passed. pre-commit run --all-files is clean apart from the
pre-existing executable-shebang hook on upstream file modes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Fxe
Fxe merged commit e21f978 into ModelSEED:dev Nov 18, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants