Skip to content

Latest commit

 

History

History
153 lines (116 loc) · 16.2 KB

CODE_GUIDES.md

File metadata and controls

153 lines (116 loc) · 16.2 KB

Code Guides

Rules for everyone who edits code in this repo. Numbers, formulas and module maps live in docs/AI_CHEATSHEET.md and its docs/cheatsheet/ files; this file links to them rather than repeating values. Code comments cite these rules as CODE_GUIDES.md#<section>, so keep the section headings stable.

Scope

  • Machine scripts run existing machines and coordinate them. They don't set machines up.
  • Exceptions: deploys from a control panel that need operator input, and undeploying machines whose step is finished.
  • Founding outposts happens only in autoplay/. Each founding raises the cost of the next outpost (outposts can be decommissioned, so it is not permanent). Code that founds an outpost logs why in its debug() trail.

Portability

  • Plan save-agnostic. Code must work on an empty map and on a crowded one.
  • Discover at runtime: capability probing, building discovery via outpost_network, location queries. No hardcoded save-specific ids or coordinates.
  • Decide only from data a script can read in game: components, points_of_interest(), journal, the archive. Never hardcode world knowledge or read the save file to drive behavior.
  • Exempt: formulas and fixed game limits (kit prices, building caps, fixed rates).
  • A live save shows how a mechanic works. It doesn't show which cases matter, so don't rank a case as rare because the current save already handles it.
  • Partly known world data (unsurveyed sites): use runtime-derived priors with a confidence value, or request a survey trip. Don't guess.

Game interpreter

The game runs a restricted Python interpreter. CPython, Pyright and the offline tests accept code that fails in game.

  • Import only our own lib/ modules and the game modules listed in stub_tests.md §10. No standard library (time, copy, os, sys, ...). Enforced by tests/test_game_imports.py.
  • No module-level import cycles between lib/ modules. In game, from X import NAME gets a half-loaded module and raises ImportError. Break a cycle by importing inside the function that needs it. The same test checks the highest tier's copies.
  • Keep module bodies cheap. The game runs a lib/ module's body, together with every module it imports for the first time, as one unit under the 10,000-step cap (decompiled executeModuleBodySync); going over ends the importing script with StepLimitError at whatever line hit the cap. The autoplay planner's import chain sits close to it: a few module-level comprehensions (~1,300 steps of tile offset tables) crashed it at load. Build tables lazily on first use (grid_geom._offsets()), not at module level.
  • One-line imports only. The game parser rejects from x import (a, b). Enforced by tests/test_game_syntax.py; ruff.toml in scripts/ and autoplay/ keeps Ruff from wrapping imports.
  • No backslash line continuation. Wrap a long expression in parentheses instead. Enforced by tests/test_game_syntax.py.
  • Check a builtin before using it: "Built-in Functions" in docs/guide/builtins_and_commands.md and docs/guide/language_reference.md. Known missing: frozenset (use a tuple), id(). Enforced by tests/test_game_builtins.py.
  • No with statements. Don't rely on finally: a stopped script is killed without unwinding.
  • Unsure about other syntax: check that an existing lib already uses it live.
  • Never set or delete an attribute on a game object (a machine, vehicle or other Component). Since build 58191a1 this raises AttributeError ("object attribute 'x' is read-only" for a game field). Keep per-machine values in our own class or a dict keyed by machine id.
  • Hardware-changing calls on another machine (e.g. input.eject()) work only from that machine's own script, even when the docs don't say "self only". A remote script may read (count(), stacks()) and use run_control. Coordinate cross-machine changes through the archive or Signal Bus.

Module layout

  • Source lives in scripts/<tier>/. lib/x.py means the logical module x, physically at scripts/<tier>/lib/x.py for the highest tier that defines it (dev_workflow.md §9).
  • Put reusable logic in small, focused lib/ modules (module map, cheatsheet §0).
  • Entrypoint scripts (solar.py, rover.py, status_panel.py, ...) stay thin. They use shared controllers from lib/ and never keep their own copies of tier lists, thresholds or budget formulas.
  • One definition per constant. Before adding one, look upwards: does it exist already?
    • Used in one function or class: define it there.
    • Shared, and tied to one module's logic: define it in that module and import it from there (Plants bands in plant_terraformer_common, tier ladders in item_tiers, claim expiry in fleet_claims_common).
    • A fixed game fact (ids, sizes, rates) used in several places: lib/game_rules.py, in its section. It imports nothing; never put our tunables there.

Mixins

  • Controllers composed of mixins (vehicle, drone, pioneer, harvester, plant terraformer, ...) type self through one _host property per mixin:
    if TYPE_CHECKING:
        from vehicle import VehicleController
    
    class VehicleMiningMixin:
        @property
        def _host(self) -> "VehicleController":
            return self  # type: ignore[return-value]
    Methods use self._host.<attr>, including self._host.log.
  • Don't use a fake base class (_Base = Controller if TYPE_CHECKING else object). It creates an inheritance cycle at the composition site.
  • After changing mixin typing, run Pyright on the composition site (lib/vehicle.py, lib/drone.py, ...), not only on the mixin file.
  • Vehicle layout (layer table: docs/plans/mixin_refactor/README.md):
    • A mixin is one ability and calls only lower layers. No role loop inside a mixin.
    • A Job (lib/job_*.py) owns its role's state and step logic and never stores state on the host. It types the host under TYPE_CHECKING; pioneer.py, rover.py and drone.py import the job modules. Drone jobs derive from DroneJob (lib/drone_job.py); FleetUnitMixin.run_job_loop() runs both kinds.
    • Mission state has one writer, VehicleClaimsMixin / DroneClaimsMixin begin_mission/end_mission. Readers use self.mission; current_target* are read-only property shims.
    • Pure math and archive I/O are module functions with explicit arguments.

Errors

  • Probe capabilities at runtime and handle missing or unpowered components.
  • Every except Exception that recovers calls swallowed(where, error) from lib/swallow.py as its first line. A broad except can't tell a missing component from a bug in our own call. Usage and the allowed exceptions: cheatsheet §0b.

Archive

The Data Archive has a fixed key-count cap that doesn't grow with fleet or building count. Keys and conventions: archive_ipc.md §4.

  • One shared dict per concern, never one key per entity. Store per-entity state as {entity_id: value} under one key (e.g. vehicle.recall, drone.home_depots), written via archive.transaction().
  • Never use __proto__, prototype or constructor as a dictionary key anywhere in a value: set()/transaction() refuse the whole value (invalid_value). Store such names as values (a list of entries) instead. Check the result status of a new key's first write.
  • Prune entries for entities that no longer exist.
  • Keep data bounded: fixed-size histories, compact summaries, JSON-safe structures. No large binary data or unbounded logs.
  • Use the archive for telemetry, production history and order tracking, so every component sees one consistent state.
  • Persist enough state to resume after a power loss or restart. Example: a smelter mid-production resumes its run; a rover in transit resumes its trip and finishes the mission.

Signal Bus

  • Use the Signal Bus (get_component("comms")) for real-time orders and event broadcasts. Check message age with latest_info() and treat stale messages as missing.
  • When a publisher is missing or stale, fall back to reading the component directly.

Logging

Use TreeConsole (lib/tree_console.py) for all console output, never ad hoc get_component("console").print(). Pattern and format: cheatsheet §0a.

  • Info level: major blocks and outcomes, skimmable at a glance.
  • debug(): the decision trail: which branch was taken, candidates considered and rejected, computed thresholds and estimates. With debug on, the log tells the whole story of a run.
  • trace(): per-item and hot-loop detail. Never put per-item lines at info level, also not in one-off probe scripts. Prefer aggregates (counts, group-by) to listing items.
  • Every console call costs 0.1 s of simulation time, even when hidden (docs/BENCHMARK.md). TreeConsole buffers consecutive debug lines into one message. Call log.flush() before every sleep().
  • Wrap each coherent unit of work (trip, order cycle, build, sweep, active tick phase) in log.start(...) … log.end(<outcome>). Close every start() on every path: early return, error branch, continue, break. Enforced by tests/test_log_blocks_balanced.py. For a debug block around a whole function with several exits, prefer @method_block() / log.run() (cheatsheet §0a, "Wrapped blocks").
  • Each top-level run* loop starts every tick with reset_all(), because an escaping exception leaves a block open (tests/test_reset_in_run_loops.py).
  • No block on an idle tick.

Script cost

Above 50 running scripts, every running script (sleeping ones included) shrinks every other script's step allowance. The count matters, not how busy each script is. Model and numbers: dev_workflow.md §1d-1, measurements in docs/BENCHMARK.md.

  • Don't add a running script where an existing one can do the work.
  • Prefer scripts that end when their machine has nothing to do and get restarted (run_control.start()) when it does, where the machine's idle state is acceptable.
  • Cache discovery and pure geometry. Compute shared results once centrally (the orchestrator_automation.py Automation, published to the archive), not in every instance.
  • Replace per-item Python loops with builtins and comprehensions.
  • A Control Room card loop ends with panel_pace.pace(panel); never let a card redraw every tick. Its cost is the Python that builds a frame, not the widgets.
  • FluidPort.connections() re-validates the pipe analysis over every machine on each call (O(machines)), even when nothing changed. Cache its result in hot loops; connected_id() is cheap.
  • Optimizing tick cost, in this order:
    1. Cut real work: repeated reads of the same key or discovery inside a loop, per-item calls that one bulk read covers (stacks() over one count() per item), repeated walks that one pass covers.
    2. Then consider lib/atomic.py (run_atomic/run_batched/run_chunked) for the remaining read-only, non-logging work. It avoids the per-tick step limiter but doesn't reduce work. Each callback is capped at 10,000 steps; exceeding the cap ends the script. No writes, archive writes, scans or sleeps inside a callback. Logging through the passed-in log (TreeConsole) or swallowed() is fine inside these runners: atomic.console_print() queues the lines and writes them after the call. Raw map()/sorted(key=)/lru_cache callbacks stay log-free. Size chunks with devtools/perf/step_profile.py.

Domain invariants

Implement these exactly as the linked sections describe; don't second-guess them.

  • Power grid: tiered load shedding by PowerGridManager (lib/power.py). It is deliberately not "protect terraforming first". Tiers and thresholds: power_fluids.md §1a.
  • Vehicles and drones: vehicles_drones.md.
    • There-and-back energy budget with a safety margin, a hard emergency-reserve floor and per-vehicle calibrated Wh/m (lib/vehicle_energy.py, §2a).
    • Atomic site reservations in the archive with heartbeat renewal and timeout expiry (lib/vehicle_claims.py).
    • Deadlock and terrain-stall detection, staggered base staging slots (lib/vehicle_navigation.py).
  • Production: demand-driven (production_logistics.md, lib/production.py).
    • Harvest and mine to match active recipe and Earth order deficits. Don't overproduce when inventory or storage is full.
    • Plan ahead from current and upcoming orders, placed blueprints and available resources.
    • Demand priority: one ladder (lib/priority.py, fabricator_demand.md §2a-p). A new demand gets a band on an existing ladder; never a parallel flag, tier list or side key. Haulers rank by band only; the fine value breaks ties inside machines and the dock planner.
    • Stock netting: one count per question. "Make more of X?" nets against SourceCache.fab_have() / network_have(): stock plus what producers already hold (output not yet in storage, one unit per running craft, loaded inputs). "Can I take X now?" and "protect X" count held_stock() / local_stock(). Moving a unit between storage and a machine must never change a target. A new producer reports its holdings through SourceCache, never a private stock read. Mechanics: production_logistics.md, "Pipeline netting".
  • Display names belong to the player. Never key logic on them; use .id. Code may preseed a name only when it creates the machine (pioneer_commission.commission_name()), may carry the old name over when it replaces one (the fleet_upgrade drone swap), and may replace a name it gave itself (a Pioneer refit, pioneer_refit.is_code_name()). An operator-set script variable that names a machine (HOME_DEPOT) may accept a display name; resolve it to the id once.

Documentation

  • Comments, docstrings and the cheatsheet describe what the code does now, plus any non-obvious constraint. No narration: no "previously X", "now Y", "was changed because", "bug fixed on …", "found live". Remove narration you find in files you touch.
  • The reason for a change goes in the commit message. Lasting design decisions go to the matching topic file in docs/design_history/ (hub: docs/DESIGN_HISTORY.md).
  • A tunable constant (margin, tier, threshold, stale-tick count, budget) has one documented home in the cheatsheet. Update it in the same change as the code. Elsewhere, link to it or name the constant.

Tests

  • Offline suite: python -m unittest discover -s tests. Details: stub_tests.md §10.
  • Extend the shared fake world in tests/game_stubs.py; never add a private fake.
  • A bug found in game: name the general mechanic behind it, check every other place it can hit, and fix or report those too. Model the mechanic in the shared fakes so every controller's tests can exercise it.

Checks

  • Ruff config: ruff.toml, which the game's VS Code extension also reads. Never fix RUF100 under --select: it then strips every noqa for a rule outside the selection.
  • ruff format excludes Markdown (game reference text keeps hand layout).
  • Pyright config: pyrightconfig.json. It reads the gitignored .pyright-resolved/lib copies, so run .venv/Scripts/python.exe devtools/scripts_sync.py resolve-preview after editing scripts/*/lib (plain python lacks typer); stale copies hide cross-module errors.
  • When you restructure a function or file listed in a scan ignore file (devtools/code_scan/*_ignore.txt), recheck its reason in the same change; re-pin (size_scan.py --pin) or drop the entry. The size scan re-flags entries that grew past their pin.

Commits

  • Conventional Commits: <type>(<scope>): <imperative summary>. Types: feat, fix, refactor, perf, docs, test, chore, build, ci, style, revert.
  • Subject: imperative, 50 characters when possible, 72 at most, no trailing period.
  • Body only when needed: the non-obvious why, breaking changes, migration notes. Wrap at 72.
  • State intent; the diff shows what changed.