|
| 1 | +# agents.md — OpenSpoolMan |
| 2 | + |
| 3 | +This document is for AI coding agents (and humans) making changes to **OpenSpoolMan**. |
| 4 | +Follow it as the default “operating manual” when creating PRs. |
| 5 | + |
| 6 | +## 1) Project intent (do not drift) |
| 7 | +OpenSpoolMan augments SpoolMan with Bambu Lab / AMS awareness and optional NFC workflows: |
| 8 | +- Keep all operations **local-first** (LAN where possible). |
| 9 | +- NFC is **optional**; the web UI must remain fully usable without NFC. |
| 10 | +- The system is an “adapter + UI” on top of SpoolMan, not a replacement. |
| 11 | + |
| 12 | +If a proposed change alters any of these fundamentals, stop and propose it as a design discussion first. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## 2) Non-negotiables (hard rules) |
| 17 | +### Security & privacy |
| 18 | +- Never commit secrets (printer access codes, API keys, cookies, tokens, personal URLs). |
| 19 | +- Do not log secrets. Mask them if you must log configuration. |
| 20 | +- Treat everything coming from MQTT / HTTP as untrusted input. |
| 21 | + |
| 22 | +### Backwards compatibility |
| 23 | +- Preserve existing env vars and default behaviors unless explicitly versioned. |
| 24 | +- UI behavior must remain functional for: |
| 25 | + - No NFC usage |
| 26 | + - SpoolMan available/unavailable (graceful handling) |
| 27 | + - AUTO_SPEND disabled |
| 28 | + |
| 29 | +### Reliability |
| 30 | +- Network calls must have **timeouts**, error handling, and retry/backoff where appropriate. |
| 31 | +- Never introduce busy loops. Prefer event-driven updates or bounded polling. |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## 3) Repository map (high-level) |
| 36 | +Key folders/files you will interact with: |
| 37 | +- `app.py` / `wsgi.py`: application entry points |
| 38 | +- `templates/`, `static/`: server-rendered UI assets |
| 39 | +- `mqtt_bambulab.py`: Bambu printer connectivity (LAN / MQTT) |
| 40 | +- `spoolman_client.py`, `spoolman_service.py`: SpoolMan integration layer |
| 41 | +- `filament.py`, `filament_usage_tracker.py`, `print_history.py`: domain logic |
| 42 | +- `scripts/`: helper scripts (e.g., initialization / tooling) |
| 43 | +- `data/`: runtime artifacts (DBs, mismatch logs) |
| 44 | +- `tests/`: Python tests |
| 45 | +- `e2e/`, `playwright.config.js`, `package.json`: end-to-end UI tests |
| 46 | +- `docker-compose.yaml` / `compose.yaml` / `Dockerfile`: containerization |
| 47 | +- `helm/openspoolman`: Helm chart |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## 4) How to run locally (known-good paths) |
| 52 | + |
| 53 | +### 4.1 Local Python run (development) |
| 54 | +1. Configure environment (see §5). Create `config.env` from `config.env.template` or export env vars. |
| 55 | +2. Start the server: |
| 56 | + - `python wsgi.py` |
| 57 | + |
| 58 | +Notes: |
| 59 | +- Default listen port is `8001` (to avoid clashing with SpoolMan). |
| 60 | +- Depending on SSL mode and mapping you may also access `https://<host>:8443`. |
| 61 | + |
| 62 | +### 4.2 Docker (deployment / reproducible dev) |
| 63 | +- Configure env vars, then: |
| 64 | + - `docker compose up -d` |
| 65 | + |
| 66 | +Use `docker compose port openspoolman 8001` to see mapped host port if needed. |
| 67 | + |
| 68 | +### 4.3 Kubernetes (Helm) |
| 69 | +- Use the bundled chart: |
| 70 | + - `helm dependency update helm/openspoolman` |
| 71 | + - `helm upgrade --install openspoolman helm/openspoolman -f values.yaml --namespace openspoolman --create-namespace` |
| 72 | +- Validate: |
| 73 | + - `kubectl get pods -n openspoolman` |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## 5) Configuration contract (environment variables) |
| 78 | +### Required / core |
| 79 | +- `OPENSPOOLMAN_BASE_URL` |
| 80 | + - HTTPS URL where OpenSpoolMan is reachable |
| 81 | + - **No trailing slash** |
| 82 | + - Required for NFC writes |
| 83 | +- `PRINTER_ID` |
| 84 | + - Printer settings → Setting → Device → Printer SN |
| 85 | +- `PRINTER_ACCESS_CODE` |
| 86 | + - Setting → LAN Only Mode → Access Code |
| 87 | + - (LAN Only Mode toggle may stay off) |
| 88 | +- `PRINTER_IP` |
| 89 | + - Setting → LAN Only Mode → IP Address |
| 90 | +- `SPOOLMAN_BASE_URL` |
| 91 | + - URL of SpoolMan without trailing slash |
| 92 | + |
| 93 | +### Feature toggles |
| 94 | +- `AUTO_SPEND` |
| 95 | + - `True` enables legacy slicer-estimate tracking. |
| 96 | +- `TRACK_LAYER_USAGE` |
| 97 | + - `True` switches to per-layer tracking/consumption **only if** `AUTO_SPEND=True`. |
| 98 | + - If `AUTO_SPEND=False`, tracking remains disabled regardless of `TRACK_LAYER_USAGE`. |
| 99 | +- `DISABLE_MISMATCH_WARNING` |
| 100 | + - `True` hides mismatch warnings in the UI (still detected and logged). |
| 101 | +- `CLEAR_ASSIGNMENT_WHEN_EMPTY` |
| 102 | + - `True` clears SpoolMan assignment and resets AMS tray when the printer reports an empty slot. |
| 103 | + |
| 104 | +### Data sources |
| 105 | +- Print history DB default: `data/3d_printer_logs.db` |
| 106 | +- Override via: `OPENSPOOLMAN_PRINT_HISTORY_DB` |
| 107 | +- Mismatch log output: `data/filament_mismatch.json` |
| 108 | + |
| 109 | +### Important operational note |
| 110 | +If you change `OPENSPOOLMAN_BASE_URL`, NFC tags must be reconfigured. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## 6) SpoolMan integration contract (must remain stable) |
| 115 | +### SpoolMan label workflow |
| 116 | +- SpoolMan can print QR-code labels. When using them with OpenSpoolMan: |
| 117 | + - Set SpoolMan’s base URL to OpenSpoolMan **before** generating labels |
| 118 | + - Otherwise labels point back to SpoolMan, not OpenSpoolMan |
| 119 | + |
| 120 | +### Required extra fields in SpoolMan |
| 121 | +Agents must not “simplify away” these fields without an explicit migration plan. |
| 122 | + |
| 123 | +Add these extra fields in SpoolMan: |
| 124 | +- Filaments: |
| 125 | + - `type` (Choice) |
| 126 | + - `nozzle_temperature` (Integer Range) |
| 127 | + - `filament_id` (Text) |
| 128 | +- Spools: |
| 129 | + - `tag` (Text) |
| 130 | + - `active_tray` (Text) |
| 131 | + |
| 132 | +(Exact choice values are defined in the README; keep behavior compatible with existing installations.) |
| 133 | + |
| 134 | +### Windows note (Bambu Studio) |
| 135 | +Filament IDs can be sourced from Bambu Studio’s filament base directory (see README). Do not hardcode user paths; keep it documentation-only. |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## 7) Filament matching rules (do not regress) |
| 140 | +OpenSpoolMan matches SpoolMan spools to AMS tray metadata: |
| 141 | +- Spool `material` must match AMS `tray_type` (main type). |
| 142 | +- For Bambu filaments, AMS reports a sub-brand; it must match the spool’s sub-brand. |
| 143 | + - Model this either as: |
| 144 | + - `material = full Bambu material` (e.g., `PLA Wood`) and `type` empty, OR |
| 145 | + - `material = base` (e.g., `PLA`) and `type = add-on` (e.g., `Wood`) |
| 146 | +- Parenthesized notes in `material` are ignored during matching (e.g., `PLA CF (recycled)`). |
| 147 | + |
| 148 | +If matching fails: |
| 149 | +- Prefer improving diagnostics and tooling. |
| 150 | +- The UI warning can be hidden with `DISABLE_MISMATCH_WARNING=true` but mismatches must still be logged. |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## 8) Change workflow for agents (how to work in this repo) |
| 155 | + |
| 156 | +### 8.1 Before coding |
| 157 | +1. Read `README.md` sections: installation, environment configuration, matching rules, AUTO_SPEND notes. |
| 158 | +2. Identify the minimal module(s) involved: |
| 159 | + - Printer connectivity: `mqtt_bambulab.py` |
| 160 | + - SpoolMan calls: `spoolman_client.py` / `spoolman_service.py` |
| 161 | + - Domain logic: `filament*.py`, `print_history.py` |
| 162 | + - UI: `templates/`, `static/` |
| 163 | +3. Decide whether you need: |
| 164 | + - Python tests (`tests/`) |
| 165 | + - E2E tests (`e2e/` via Playwright) |
| 166 | + |
| 167 | +### 8.2 Coding standards (practical) |
| 168 | +- Keep functions small and testable. |
| 169 | +- Prefer explicit types where they improve clarity (especially for payloads). |
| 170 | +- Validate external payloads defensively (missing keys, type mismatches). |
| 171 | +- When reading runtime state (e.g., `PRINTER_STATE`, MQTT payloads), prefer accessing the original object via `.get(...)` rather than copying into temporary locals unless the value needs transformation; this keeps guard logic close to the source and avoids stale snapshots. |
| 172 | +- Avoid introducing new dependencies without a strong justification. |
| 173 | +- Keep logging structured and helpful; never leak secrets. |
| 174 | + |
| 175 | +### 8.3 Testing expectations |
| 176 | +Minimum expectations before PR: |
| 177 | +- If logic changes: update/add Python tests under `tests/`. |
| 178 | +- If UI changes: ensure at least a smoke check and, when possible, run the E2E suite. |
| 179 | +- If env/config changes: update README + `config.env.template` accordingly. |
| 180 | + |
| 181 | +Notes: |
| 182 | +- Python tests are configured via `pytest.ini`. |
| 183 | +- E2E is set up via `playwright.config.js` and `package.json`. Use the existing npm scripts rather than inventing new ones unless necessary. |
| 184 | + |
| 185 | +### 8.4 PR checklist (agents must include in PR description) |
| 186 | +- [ ] Scope is minimal; no unrelated refactors |
| 187 | +- [ ] No secrets or sensitive values introduced |
| 188 | +- [ ] Errors handled (timeouts, retries/backoff if applicable) |
| 189 | +- [ ] Tests added/updated (or justification if not) |
| 190 | +- [ ] README/config updated if behavior or configuration changed |
| 191 | +- [ ] Docker/Helm impact considered (ports, env vars, volumes) |
| 192 | +- [ ] Filament matching rules preserved (or explicitly enhanced with tests) |
| 193 | + |
| 194 | +--- |
| 195 | + |
| 196 | +## 9) Deployment artifacts (keep in sync) |
| 197 | +If you touch runtime behavior, check: |
| 198 | +- Docker: |
| 199 | + - `Dockerfile` |
| 200 | + - `docker-compose.yaml` / `compose.yaml` env var passing and volumes |
| 201 | +- Helm: |
| 202 | + - `helm/openspoolman` chart values and templates |
| 203 | + - Ensure env vars and defaults align with README |
| 204 | + |
| 205 | +Do not silently change exposed ports or default bindings without updating: |
| 206 | +- README |
| 207 | +- Compose |
| 208 | +- Helm chart |
| 209 | + |
| 210 | +--- |
| 211 | + |
| 212 | +## 10) Troubleshooting guidance (for maintainers and future agents) |
| 213 | +When debugging: |
| 214 | +- Confirm `SPOOLMAN_BASE_URL` and `OPENSPOOLMAN_BASE_URL` have **no trailing slash**. |
| 215 | +- Confirm printer values: |
| 216 | + - `PRINTER_IP` reachable from the OpenSpoolMan host/container |
| 217 | + - `PRINTER_ACCESS_CODE` correct |
| 218 | +- Inspect mismatch log: |
| 219 | + - `data/filament_mismatch.json` |
| 220 | +- Confirm print history DB path: |
| 221 | + - `data/3d_printer_logs.db` or `OPENSPOOLMAN_PRINT_HISTORY_DB` |
| 222 | + |
| 223 | +For AUTO_SPEND / tracking: |
| 224 | +- Ensure `AUTO_SPEND=True` before expecting any tracking. |
| 225 | +- `TRACK_LAYER_USAGE=True` only matters when `AUTO_SPEND=True`. |
| 226 | + |
| 227 | +--- |
| 228 | + |
| 229 | +## 11) What not to do (common failure modes) |
| 230 | +- Do not hardcode user-specific paths, hostnames, or ports. |
| 231 | +- Do not break “no NFC” operation. |
| 232 | +- Do not require cloud access for core workflows. |
| 233 | +- Do not change matching semantics without tests and clear migration notes. |
| 234 | +- Do not broaden logs to include access codes or private URLs. |
| 235 | + |
| 236 | +--- |
| 237 | + |
| 238 | +## 12) AMS tray assignment behavior |
| 239 | +- Cloud prints already contain `ams_mapping` in their `project_file` payload, so OpenSpoolMan can map every logical filament to a tray immediately. |
| 240 | +- Local prints (LAN mode) do not ship `ams_mapping` upfront, so we delay applying AMS mappings until the printer reports a concrete `tray_tar` (typically during stage 4 / filament change). That’s why the MQTT log often shows `tray_tar=255` for seconds and only flips to the real tray once the tray itself is loaded. |
| 241 | + |
| 242 | +--- |
| 243 | + |
| 244 | +## 13) When you are unsure |
| 245 | +Prefer these options, in order: |
| 246 | +1. Add instrumentation and tests rather than guessing. |
| 247 | +2. Make the smallest change that improves correctness. |
| 248 | +3. Document assumptions in the PR description and in code comments where necessary. |
| 249 | + |
| 250 | +End of file. |
0 commit comments