|
| 1 | +# Codex Agent Handbook |
| 2 | + |
| 3 | +This guide captures project-specific expectations for OpenAI Codex agents assisting in the FlashForgeUI-Electron repository. It summarizes operational guardrails, preferred workflows, and high-value reference points so future Codex sessions can ramp up quickly without depending on Claude-only resources. |
| 4 | + |
| 5 | +## Operating Rules for Codex |
| 6 | + |
| 7 | +- **Shell usage**: Always invoke commands through `["bash","-lc", "<command>"]` and set the `workdir` on every call. Prefer `rg`/`rg --files` for search. |
| 8 | +- **File editing**: Use `apply_patch` for hand-written edits; avoid it for auto-generated outputs (e.g., `npm install`, formatters). Never touch files outside user instructions and never revert user-owned changes. |
| 9 | +- **Planning discipline**: Create multi-step plans for anything non-trivial and keep the plan in sync as work progresses. Skip planning only for the simplest tasks. |
| 10 | +- **Testing mindset**: Favor targeted verification (type checking, linting) over full builds unless the user requests otherwise or build validation is obviously necessary. |
| 11 | +- **Escalation etiquette**: Sandbox is `workspace-write`, network is restricted, and approval policy is `on-request`. Request elevation only when essential, providing concise justification. |
| 12 | +- **Interaction style**: Keep responses concise, friendly, and actionable. Reference files with `path:line` syntax. Suggest next steps only when they are natural. |
| 13 | + |
| 14 | +## Key Repository Facts |
| 15 | + |
| 16 | +- **Project scope**: FlashForgeUI is an Electron desktop application that supports multiple simultaneous printer contexts with tabbed switching, camera streaming, and backend polling coordination. |
| 17 | +- **Architecture anchors**: |
| 18 | + - `src/managers/PrinterContextManager.ts`: lifecycle for per-printer contexts. |
| 19 | + - `src/services/MultiContextPollingCoordinator.ts`: adjusts polling cadence per context. |
| 20 | + - `src/ui/components/printer-tabs/`: renderer-side UI for active context selection. |
| 21 | + - `src/utils/PortAllocator.ts`: assigns unique camera proxy ports (8181-8191). |
| 22 | +- **Feature status**: Multi-printer support is fully implemented but still untested in runtime scenarios (filament tracking, WebUI flows, simultaneous jobs, camera streams, etc.). |
| 23 | +- **Documentation standard**: Every TypeScript/JavaScript source file should carry an `@fileoverview` header describing purpose, responsibilities, dependencies, and usage context. |
| 24 | + |
| 25 | +### Sample `@fileoverview` Blocks |
| 26 | + |
| 27 | +```ts |
| 28 | +/** |
| 29 | + * @fileoverview Connection flow orchestrator for managing printer discovery and connection workflows. |
| 30 | + * |
| 31 | + * Provides high-level coordination of printer connection operations in multi-context environment: |
| 32 | + * - Network discovery flow management with printer selection |
| 33 | + * - Direct IP connection support with check code prompts |
| 34 | + * - Auto-connect functionality for previously connected printers |
| 35 | + * - Saved printer management and connection restoration |
| 36 | + * - Connection state tracking and event forwarding |
| 37 | + * - Multi-context connection flow tracking for concurrent connections |
| 38 | + * |
| 39 | + * Key exports: |
| 40 | + * - ConnectionFlowManager class: Main connection orchestrator |
| 41 | + * - getPrinterConnectionManager(): Singleton accessor function |
| 42 | + * |
| 43 | + * The manager coordinates multiple specialized services: |
| 44 | + * - PrinterDiscoveryService: Network scanning and printer detection |
| 45 | + * - SavedPrinterService: Persistent printer storage |
| 46 | + * - AutoConnectService: Automatic connection on startup |
| 47 | + * - ConnectionStateManager: Connection state tracking |
| 48 | + * - DialogIntegrationService: User interaction dialogs |
| 49 | + * - ConnectionEstablishmentService: Low-level connection setup |
| 50 | + * |
| 51 | + * Supports concurrent connection flows with unique flow IDs and context tracking, |
| 52 | + * enabling multi-printer connections while maintaining proper state isolation. |
| 53 | + */ |
| 54 | +``` |
| 55 | + |
| 56 | +```ts |
| 57 | +/** |
| 58 | + * @fileoverview Central WebUI server coordinator managing Express HTTP server and WebSocket lifecycle. |
| 59 | + * |
| 60 | + * Provides comprehensive management of the WebUI server including Express HTTP server initialization, |
| 61 | + * static file serving, middleware configuration, API route registration, WebSocket server setup, |
| 62 | + * and integration with printer backend services. Automatically starts when a printer connects |
| 63 | + * (if enabled in settings) and stops on disconnect. Handles administrator privilege requirements |
| 64 | + * on Windows platforms, network interface detection for LAN access, and configuration changes |
| 65 | + * for dynamic server restart. Coordinates between HTTP API routes, WebSocket real-time updates, |
| 66 | + * and polling data from the main process to provide seamless remote printer control and monitoring. |
| 67 | + * |
| 68 | + * Key exports: |
| 69 | + * - WebUIManager class: Main server coordinator with singleton pattern |
| 70 | + * - getWebUIManager(): Singleton accessor function |
| 71 | + * - Lifecycle: start, stop, initialize, startForPrinter, stopForPrinter |
| 72 | + * - Status: getStatus, isServerRunning, getExpressApp, getHttpServer |
| 73 | + * - Integration: handlePollingUpdate (receives status from main process) |
| 74 | + * - Events: 'server-started', 'server-stopped', 'printer-connected', 'printer-disconnected' |
| 75 | + */ |
| 76 | +``` |
| 77 | + |
| 78 | +```ts |
| 79 | +/** |
| 80 | + * @fileoverview Printer Tabs Component for Multi-Printer Support |
| 81 | + * |
| 82 | + * This component provides a tabbed interface for managing multiple printer connections |
| 83 | + * similar to Orca-FlashForge's tabbed interface. It extends EventEmitter to notify |
| 84 | + * the renderer process of user interactions with tabs. |
| 85 | + * |
| 86 | + * Key features: |
| 87 | + * - Tab management (add, remove, switch, update) |
| 88 | + * - Connection status indicators (connected, connecting, disconnected, error) |
| 89 | + * - Close buttons on tabs with hover effects |
| 90 | + * - "Add Printer" button for creating new connections |
| 91 | + * - Event emission for tab interactions (click, close, add) |
| 92 | + * - Visual distinction between active and inactive tabs |
| 93 | + * |
| 94 | + * Events: |
| 95 | + * - 'tab-clicked': Emitted when a tab is clicked (contextId: string) |
| 96 | + * - 'tab-closed': Emitted when a tab's close button is clicked (contextId: string) |
| 97 | + * - 'add-printer-clicked': Emitted when the add printer button is clicked |
| 98 | + */ |
| 99 | +``` |
| 100 | + |
| 101 | +## High-Value Scripts & Commands |
| 102 | + |
| 103 | +Use these npm scripts to enforce quality checks and gather insights: |
| 104 | + |
| 105 | +| Command | Purpose | Notes | |
| 106 | +| --- | --- | --- | |
| 107 | +| `npm run type-check` | TypeScript validation (`tsc --noEmit`). | Report success/fail clearly; fix root causes before completion. | |
| 108 | +| `npm run lint` / `npm run lint:fix` | ESLint checks and auto-fixes on `src/**/*.ts`. | Auto-fix first; re-run lint to confirm clean state. | |
| 109 | +| `npm run docs:check` | PowerShell script scanning for missing `@fileoverview`. | Use findings to prioritize documentation updates. | |
| 110 | +| `npm run knip` (+ variants) | Dead code and dependency analysis via Knip. | Review results carefully; Electron patterns create false positives. | |
| 111 | +| `npm run build:*` | Build main, renderer, WebUI, or platform packages. | Run only when requested or required for verification. | |
| 112 | + |
| 113 | +Remember PowerShell scripts (`docs:*`, `linecount`) assume Windows-friendly environment; confirm availability before invoking. |
| 114 | + |
| 115 | +## Recommended Workflow Templates |
| 116 | + |
| 117 | +### General Code Change |
| 118 | + |
| 119 | +1. **Context**: Review relevant files (`rg`, `codebase exploration`) and check `CLAUDE.md` for recent architecture notes. |
| 120 | +2. **Plan**: Outline steps, call out affected modules and validation strategy. |
| 121 | +3. **Implement**: Modify files via `apply_patch` with minimal, well-structured commits. |
| 122 | +4. **Document**: Ensure new/changed files keep `@fileoverview` headers up to date. |
| 123 | +5. **Verify**: Run `npm run type-check` and `npm run lint`; add other checks only if warranted. |
| 124 | +6. **Report**: Summarize changes, mention validations, and suggest next steps (e.g., build, documentation review). |
| 125 | + |
| 126 | +### Documentation-Only Task |
| 127 | + |
| 128 | +1. Identify files lacking headers with `npm run docs:check`. |
| 129 | +2. Read the target file to understand responsibilities. |
| 130 | +3. Add or update the `@fileoverview` block using succinct prose that covers purpose, key exports, dependencies, and usage notes. |
| 131 | +4. Re-run `npm run docs:check` if the user requests confirmation. |
| 132 | + |
| 133 | +### Bug Investigation |
| 134 | + |
| 135 | +1. Capture current state with `git status`/`git diff` if necessary (respect sandbox rules). |
| 136 | +2. Trace the data flow and key code paths using targeted `rg` queries. |
| 137 | +3. Hypothesize root cause; confirm by inspecting adjacent modules (services, managers, IPC handlers). |
| 138 | +4. Plan remediation, communicate assumptions, and only then implement. |
| 139 | + |
| 140 | +### Review Request |
| 141 | + |
| 142 | +1. Focus on identifying defects, risky patterns, missing tests, or documentation gaps. |
| 143 | +2. Reference findings with precise file locations (`path:line`). |
| 144 | +3. Keep high-level summaries short; lead with concrete issues. |
| 145 | + |
| 146 | +## Testing & Runtime Limitations |
| 147 | + |
| 148 | +- Cannot launch the Electron app, manipulate real printers, or validate UI visually. |
| 149 | +- Restricted to static analysis, type checking, linting, and script-driven tooling. |
| 150 | +- Treat any runtime-dependent assumption as unverified; call it out in deliverables. |
| 151 | + |
| 152 | +## Helpful Reference Files |
| 153 | + |
| 154 | +- `CLAUDE.md`: High-level development guide (multi-printer architecture, testing limitations). |
| 155 | +- `ai_reference/ARCHITECTURE.md`: Deep dive into system components and interactions. |
| 156 | +- `ai_reference` folder: Central knowledge base for this project. Add new long-form guidance here and consult existing references before tackling related work. |
| 157 | +- `ai_reference/typescript-best-practices.md` & `ai_reference/electron-typescript-best-practices.md`: **Mandatory pre-reading before any programming task.** Revisit these documents at the start of every coding session to stay aligned with project standards. |
| 158 | +- `.claude/commands/*.md`: Predefined workflows (load changes, lint, type-check, auto-document, dead code analysis). |
| 159 | + |
| 160 | +Consult these resources early in a session to align with established expectations. |
| 161 | + |
| 162 | +## Recent Lessons |
| 163 | + |
| 164 | +- Component dialog preload files must never import `.d.ts` modules at runtime—use `import type {} from '../../types/global';` to keep typings without breaking the window bootstrap. |
| 165 | +- The dialog relies on untouched `polling-update` payloads; avoid “fixing” the shape or you risk swallowing updates and rendering an empty dialog. |
| 166 | +- GridStack initialization already performs component registration/initialization; removing or duplicating that logic leaves global references (e.g., log panel) unset. Preserve the existing flow unless you have a tested replacement. |
| 167 | + |
| 168 | +## Practical Tips for Codex |
| 169 | + |
| 170 | +- Prefer incremental `rg` searches over full `find`/`grep` when locating files or symbols. |
| 171 | +- When editing large files, consider using `sed -n 'start,endp'` to preview relevant sections before patching. |
| 172 | +- Keep diffs minimal and purposeful; bundle related changes together. |
| 173 | +- If instructions conflict, prioritize user directives → developer message → system message hierarchy. |
| 174 | +- Surface uncertainties explicitly and propose validation steps the user can run (e.g., `npm run build`, manual UI tests). |
| 175 | + |
| 176 | +By following this handbook, Codex agents can contribute confidently while honoring repository standards and operational constraints. |
0 commit comments