Layering and Project Ownership Policy¶
Status 2026-10-21, drafted for the maintainer's review. Rules tagged (ruled) come from existing policies, skills or maintainer rulings; rules tagged (proposed) are new and take effect when the maintainer accepts them.
Read this before you add or change a commander, a strategy routine, a worker
step, or any code that reads or writes an sp_project. It exists because
step bodies and project I/O once accumulated in the commanders: each
probability-table, assembly and commit commander built its own params and
builder, re-read the project, and wrote it again, so one iteration paid for four
copies and several full writes. The restructuring of 2026-10-20 removed that
(doc/refactoring_notes/completed/structure2D_and_project_owner.md); this policy
keeps it removed. The per-workflow contracts stay in the workflow policies
(3D/refine3D_policy.md section 4, 2D/solve2D_policy.md); this one states the
rules they share.
1. Before you add or change a step¶
- Search first. The step you need is usually there. Run
rgfor the verb and for the data it touches (sample4update_reprod,ensure_field_halves,merge_algndocs,read_reprojection_model,build_batch_particles3D,qsys_declare_part_finished) and read the callers. If a routine does most of what you need, give it an argument or a hook; do not copy it. - Name the step's kind (section 3) and put the body where that kind lives.
- Write the project contract in one line at the routine's header: what it reads, what it writes, how often, or "none". If you cannot say it in one line, the step is doing too much.
- State who owns the project in the process you are writing (section 5).
If the answer is "the caller", the routine takes
(params, build)and does not read or write the file. - Test the routine on its own
(params, build), not only through the command line, and for a change that should not move results run the identity check (simple_test_exec test=identity,test_environment_policy.md). - Run
scripts/layering_audit.pyonce it exists (section 9) and do not raise any count.
2. Layers¶
The flow is ui -> exec -> commander -> strategy/domain object.
- exec routers dispatch a program name to a commander. Nothing else.
- Commanders are command objects:
commander_basedefersexecute(cline)and nothing else (ruled). A commander normalizes and validatescline, callsparams%new(cline)once, builds the builder, calls a routine, and ends. (In the preprocessing family the strategy'sinitializedoes this instead; the commander only creates the strategy.) It does not own numerics, project choreography or a step that another workflow runs. - Strategy routines (
src/main/strategies,src/main/solve) own "how a workflow runs": shared-memory versus distributed policy, iteration, the master's steps. - Domain objects (
image,project,ori,pftc,volume,class,sigma2, ...) own what the data means and know nothing of commanders or strategies.
Imports point downward. A strategy or domain module does not import a concrete
commander module (simple_commanders_<topic>) (proposed); the controllers
(src/main/solve, the multi-state controller, the final reconstruction, the
external-reference pose initialization, particle_sieving_2's simple_ptcl_sieve2
in src/main/sieve) may, to run whole programs (section 4).
The strategy modules of the three master steps (volume assembly, class-average
assembly, the sigma2 commit) never import the commander that wraps them: a body
that needs an in-process commander takes it as a commander_base dummy
(ruled). Shared-memory refine3D and refine2D now use owner routines on
the caller's (params, build) for assembly and sigma2 consolidation. Their
former commander exception is retired. The probabilistic routes run as one batch pipeline inside the
matchers (refine3D_exec, refine2D_exec) in shared memory and in every
distributed part alike, and the prob_align step, its commanders and its table
workers were retired on 2026-11-05.
simple_commanders_api is an import convenience, not a layer: it re-exports
builder, parameters, cmdline, sp_project and the queue helpers. New
strategy code imports those directly (compile_time_policy.md forbids
widening the umbrella).
3. Kinds of step, and where the body lives¶
| Kind | Examples | Home | Commander |
|---|---|---|---|
| Program | refine3D, reconstruct3D, preprocess |
a controller or strategy (create_<x>_strategy) |
execute(cline) |
| Master step: a part of a stage that the master or another workflow runs in process | volume assembly, class-average assembly, sigma2 commit | strategy routine <x>_exec(params, build, explicit args) in strategies/parallelization with a run_<x>(cline) entry (ruled) |
thin wrapper that calls run_<x> |
| Worker step: the per-partition program a scheduler starts | the matchers' refine3D_exec/refine2D_exec (the probabilistic routes' fills, draws and continuous refinement inside, the same body in shared memory), calc_pspec parts |
the matcher or strategy module beside the master routine for the same step (proposed) | thin shell: params, builder, body, qsys_declare_part_finished, simple_end |
| Domain operation | restore class averages, assemble volumes' reduction, NU filter | domain module | none |
A step that is part of a stage is never a commander called from a strategy.
Typed entry points beside execute(cline) (execute_from_project,
execute_hand_on) were deleted and do not come back (ruled).
Variants share one skeleton (proposed). A worker's frame is params/builder
set-up, reproduction of the master's sample (sample4update_reprod), the
empty-partition contribution, batching with split_nobjs_even, the benchmark
timers, and the completion call. The matchers hold it once for the search
workers; the three probability-table workers that each carried their own copy
were retired with the batch pipeline (2026-11-05). Write that frame once; a
variant supplies only what differs. A new variant that copies the frame is
rejected in review.
A worker reproduces, never redraws (ruled). The master draws the sample and
writes it; a worker reproduces it (sample4update_reprod) and stops with an
error if the field was not sampled. The draws inside a part (the probabilistic
routes' in-plane, shift-map, candidate and assignment draws) are keyed on the
run's seed, the pass, the iteration, the particle and the draw, so a part draws
the same whatever its thread count, batch size or partition (2026-11-04).
Whole programs inside a program. A controller (solve3D, refine3D_auto,
the multi-state controller) may run whole programs as stages by calling their
commanders. Section 4 states what that obliges.
4. A commander calling a commander¶
Allowed only to run a whole program as a stage, from a controller. The caller then owes:
- Hand the project over through the file. Write the segments the callee
reads before the call (
write_segment_inside(oritype)), and read back the segments it wrote after (read_segment). Do not assume the callee shares your in-memory project; it builds its own. - Build the child command line sparsely and set
mkdir=noonceparams%newhas created the execution directory. Do not pass nativeboxorsmpdfor a project run. - Expect
params%newside effects: it reseeds the random generator (seed_rnd) and callsomp_set_num_threads(nthr). The caller saves and restores its own thread count. Moved master steps do not callparams%new; they set the threads they need, and the caller restores its own (ruled 2026-10-20). - Do not redo the callee's work in the caller (reference volumes, sigma2 set-up, even/odd labelling): if the caller needs the product, ask the callee for it or call the routine they share.
- A step that is a part of a stage is not called this way (section 3).
5. Project ownership and I/O¶
The project file is durable workflow state; build%spproj is one process's
copy of it. sp_project has no file locking: a file written while another
process reads it can be seen half-written.
- One owner per project file at a time (ruled for refine3D/refine2D, proposed elsewhere). The distributed master owns its project for the run. A stream stage and the 2D pool each own the files they write. Two processes never write one file.
- Workers read the file and write part files (orientation documents,
partial sums, the readout record of a probabilistic part); a worker does not write the project
(ruled; checked): the project writers stop when a partition worker (
partgiven, not a stream job) writes its input project (worker_projfile_glob, recorded byparams%new). The master reads only what the workers write, and merges their documents in memory withmerge_algndocs(..., write=.false.)(ruled). Part files have one shape per step family (proposed). A step that only changes micrograph rows (motion_correct,ctf_estimate,pick,gen_pspecs_and_thumbs,preprocess) has each worker write a mic-row part document that the master merges withmerge_algndocs, which checks coverage and overlap. A step that creates stacks and particles (extract,reextract,fractionate_model) has each worker write a part project and the master assemble them with one shared routine. The stream's job project, which the worker updates in place, is the documented exception. - Write only the segments you changed, with
write_segment_inside(oritype). A fullwriterewrites the whole file (it deletes and recreates it) and belongs to the end of a command-line step or of a run (ruled). Note thatwrite_segment_insidefalls back to a fullwritewhen the file does not exist yet. - One write of a segment per iteration (ruled). A second write needs a
reader that must see the file (the
outsegment before postprocessing; the particle field before a follow-up pass); the owning workflow policy names it. - Do not read what you hold. A routine that has
builddoes not read the project again; passbuild, not a file name (proposed). Read a segment from the file only when another process has changed it, and read that segment, not the project (read_segment,read_non_data_segments). - Keep
projinfohonest.readnames the file it read. A builder made on a project copied or relocated into another directory callsupdate_projinfoso that writes go to the run's own project, not the original (ruled); the regenerated fixture of the 2026-10-20 run is the incident. - Shared state that must agree across processes has one maker and
verifiers (proposed). The master makes it once, in one transaction;
workers check it and stop if it is absent or inconsistent. The model is
ensure_field_halves(simple_eo_partition.f90): the master labels the even/odd halves, a worker only verifies. The sample, the sigma2 state path, the reprojection model and the class assignment of distributedmake_cavgs(class remap, expansion, time-series split: made by its master and written before the workers start) follow the same rule. Never re-derive such state in a worker "to be safe". - Register run artifacts in the
outsegment through its getters and adders (add_vol2os_out,add_fsc2os_out, ...), not by file-name convention (ruled).
6. Parameters and builders¶
- One
params%new(cline)per commander execution path; normalize and validateclinefirst (ruled). - After
params%new,params%...is authoritative; do not re-read parsed values fromcline(ruled). - Do not build a second builder for the same project in one process. A
different toolbox on the same project is a builder method (
new_vols, the search toolbox, the band, the model), not a second builder (proposed). - A routine on the caller's
(params, build)runs at the thread count it is given, saves and restores nothing but its own, and draws from the random generator without a reseed (ruled); the probabilistic routes draw through the keyed, stateless generator (rnd_keyed), never through a stream a reseed could move.
7. Red flags in review¶
Each is a signal to ask "what already does this?":
params%neworinit_params_and_build_*inside a strategy routine that already receivesparamsandbuild.spproj%read(of a whole project in code that holdsbuild%spproj, or twowrite_segment_insideof one segment on one path.- A worker that calls
write/write_segment_insideon the project file. sample4update_reprod,split_nobjs_evenandqsys_declare_part_finishedtogether in a new commander: a third copy of the worker frame.- A message or
qsys_declare_part_finishedlabel that names another module (evidence of a copy). use simple_commanders_<topic>of a concrete commander module in a strategy or domain module, or a routine added to a commander module for a strategy to call (postprocess_volume_from_fileswas one until 2026-11-14, when it moved tosimple_postprocess_strategybesiderun_postprocess).- A commander routine of more than about 150 lines that contains numerics or project choreography rather than sequencing.
- A new
optionalargument that only one caller omits, added to avoid reading the caller's code.
8. Known deviations (2026-10-21; the probabilistic alignment entries closed 2026-11-05)¶
Measured on the working tree at fd5b5bc8e. They are the ledger of what the
rules above do not yet describe; each one's fix is a planned item, not a
precedent. The entries the batch pipeline of probabilistic alignment closed
(the worker frames of the probability-table workers in commanders, the
prob_align commanders imported by the strategies, the shared-memory
write-and-reread around the prob_align step) are gone from the list.
- Concrete commander modules imported by strategies.
simple_refine3D_strategy(rec3D),simple_refine2D_strategy(make_cavgs,make_cavgs_distr,scale),simple_calc_pspec_strategy(calc_pspec_assemble) andsimple_pick_strategy(cluster_cavgs). The controllers import them by design (simple_solve3D_utils,simple_refine3D_states_controller,simple_external_reference_pose_initialization,simple_sigma2_bootstrap,simple_flex_pca_project_gateway,simple_ptcl_sieve2). - The PCG master writes the project.
execute_rec3D_pcg_distributed_masterwrites the particle field once per call, with nowriteflag like the gridding path has (simple_rec3D_pcg_strategy.f90, after theres,res05,cfarupdate), so a distributedrec_backend=pcgiteration writes the field once more than3D/refine3D_policy.mdsection 4 counts. - A domain module imports a strategy module.
ptcl_read_sessionlives instrategies/search/simple_matcher_ptcl_io.f90butclass/,volume/andflex/use it. - The preprocessing steps use four patterns and no policy states them. Mic-row
part documents merged by
merge_algndocs(motion_correct,ctf_estimate,pick,gen_pspecs_and_thumbs,preprocess); whole part projects assembled by hand in three near-identical copies (extract,reextract,fractionate_model), with count checks only andextractcopying particle fields one by one where the others usetransfer_ori; a job project updated in place by the worker (stream); and stage-owned projects. In the first group the worker reads the whole project to update a few rows and the master reads twice (a segment to count, then the project for the merge);extract's master writes the full project before the workers and again after.segdiam_bin_picker%pickwrites the caller's project twice in one call, and the pick master'smake_pickrefs_implcallsparams%newa second time and runs a commander. In this family the strategy'sinitialize, not the commander, callsparams%new. - The stream shares files by sequencing, not by lock. The master owns no
project; each stage owns its folder and project and hands on by writing a
.tmpfile and renaming it. The exceptions: the pool and itsrefine2Dmaster take turns onrefine2D.simple, and the pool'sfinalisewrites the job protocol's marker files into the folder of the job it has just cancelled;p01/p04rewrite themicsegment of a finished job's project before moving it; the master touchesSTREAM_FINISHEDin a symlinked earlier folder. Nothing butis_runningand the exit-status files stops two processes from writing one stage folder. None of this is written instream/. - solve3D add-on. The final reconstruction rewrites the working project in
full three times (
switch_addon_membership,calc_final_rec_addon). - SINGLE sets
mkdirin its routers.production/single_exec.f90setsmkdir=yeswhen the command line leaves it out, and the SINGLE routers override it per program (single_exec_atom.f90,single_exec_trajectory.f90,single_exec_nano2D.f90), a normalization ofclinethat section 2 leaves to the commander. - Routers choose by key.
solve3D_cavgswithnrestarts_collapsedispatches tocommander_solve3D_cavgs_conditional_restarts, andsolve3Dwithscreentoexec_screen(unreachable:screenis no accepted key, socmdline%parsestops first), a choice of workflow that section 2 leaves to the commander. - Front doors write the project. After the commander returns,
production/simple_exec.f90,single_exec.f90andsimple_stream.f90rewrite the project's job descriptions (update_job_descriptions_in_project), a project write outside any commander. - Routers rerun programs. With
nrestartsgiven, the routers ofsolve2D,refine3D(_auto,_local,_states,_states_cavgs),classify3D_refs,solve3D,solve3D_cavgsandautorefine3D_nanocallrestarted_exec(exec/simple_exec_helpers.f90), which runs the programnrestartstimes as a child process instead of dispatching to its commander. - Long commander routines. Measured on 2026-11-04 with contained procedures
counted (less
exec_bootstrap_cavgs, removed 2026-11-17), 34exec_*routines outside the test commanders exceed 150 lines and 14 exceed 300 (exec_solve3D1468,exec_solve2D_workflow818,exec_trajectory_solve3D624,exec_solve3D_cavgs558,exec_stream_p00_master488, ...). Most are controllers that sequence programs; the numerics and the I/O inside them are the part to move out when they are next changed. - Counts are static. No test measures project reads and writes per iteration; the refine3D contract (section 4 of its policy) was checked by the 2026-10-20 run, and nothing keeps it checked.
9. Checks¶
Existing: the fast gate (scripts/run_fast_gate.sh) runs
check_test_registry.py, check_descr.py and check_flex_dag.py; the
identity and truth suites (test_environment_policy.md) catch a moved step that
changes results.
Proposed, not built:
scripts/layering_audit.py, report-only with a committed baseline, so a count may fall but not rise: strategy/domain modules that import commander modules; commanderexec_*routines over 150 lines;params%newand builder construction in strategy modules;sample4update_reprodoutside the matchers;write/write_segment_insidein code that runs as a worker. Add it to the fast gate once the baseline is clean of false positives.- Project I/O counters in
sp_project(reads and writes by segment, printed underL_BENCH_GLOB), and a high-level test that runs one distributedrefine3Dand onerefine2Diteration and asserts the budgets of the two workflow policies.