Scripted Main (Scripts as Modules): Modulino - sgml/signature GitHub Wiki
Production‑Masquerading Workaround with Submodules, Modulinos, and Monkeypatching
A Q&A‑Formatted, Step‑by‑Step Description (No Code)
Q: What is the purpose of this pattern?
The purpose is to create a controlled workflow in which a local or CI environment can temporarily override a specific behavior in the production codebase without modifying the production code itself. The workflow allows the environment to behave as if it were production while still applying a targeted override. This enables deterministic testing, debugging, or analysis of production logic without altering the real production repository.
Q: How should the repository be structured?
- Create a main repository that will orchestrate the workflow.
- Inside this repository, create a directory dedicated to holding the production code.
- Configure this directory as a git submodule pointing to the real production repository.
- Create a separate directory for tooling, which will contain the modulino responsible for applying the override and running the behavior.
- Create a directory for CI configuration, such as a workflows directory, which will contain the GitHub Actions workflow definition.
- Ensure that each of these directories has a single, clear responsibility: production code, override logic, and CI automation.
Q: How is the production code brought into the main repository?
- Identify the production repository and the specific commit or branch that should be used.
- Add the production repository as a git submodule in the designated directory.
- Initialize and update the submodule so that the production code is present on disk.
- Treat the submodule as read‑only; do not modify files inside it.
- When a newer version of production is needed, update the submodule pointer rather than editing the code directly.
Q: What is the role of the modulino?
- The modulino is a module that can be both imported and executed directly.
- When imported, it defines the logic required to locate the production code and prepare the override.
- When executed directly, it performs the sequence of actions required to apply the override and run the behavior.
- It acts as the orchestrator of the workaround, ensuring that the override is applied only at runtime and only in the appropriate environment.
- It keeps the override logic separate from the production code, preserving the integrity of the production repository.
Q: How does the monkeypatch conceptually work?
- Identify the specific behavior in the production code that needs to be overridden.
- Define an alternative behavior that should replace the original one during execution.
- At runtime, before any dependent logic is executed, replace the reference to the original behavior with the alternative one.
- Ensure that this replacement is temporary and applies only within the current process.
- After execution, the production code remains unchanged on disk, and the override disappears automatically.
Q: How does localhost masquerade as production?
- Determine which environment variables normally indicate that the system is running in production.
- In the local environment configuration, set these variables to values that indicate production.
- Add an additional variable that explicitly marks the environment as a local masquerade rather than real production.
- In the modulino, read both the production indicator and the masquerade flag.
- Apply the override only when both conditions are satisfied.
- This ensures that localhost follows the production code path while still applying the override.
Q: How is the pattern executed locally?
- Clone the main repository onto the local machine.
- Initialize and update the submodule so that the production code is available.
- Set the environment variables to indicate production and to mark the environment as a local masquerade.
- Execute the modulino directly from the command line.
- The modulino detects the masquerading environment, applies the override, and runs the behavior.
- The production code remains unchanged after execution.
Q: How is the pattern executed in GitHub Actions without relying on git merge?
- Create a workflow file in the workflows directory of the main repository.
- Configure the workflow to trigger on the desired events.
- Add a step that checks out the main repository and initializes the submodules.
- Add a step that sets the environment variables to indicate production and to mark the environment as a CI masquerade.
- Add a step that executes the modulino.
- The modulino detects the masquerading environment, applies the override, and runs the behavior.
- The workflow does not require any merge operations, making it resilient even when merge functionality is broken.
Q: Why is this approach not a hack or an anti‑pattern?
- The override is isolated in a separate module and does not modify production code.
- The production code is treated as a read‑only dependency.
- The override is applied only at runtime and only in specific environments.
- The workflow is deterministic and reproducible.
- The structure respects separation of concerns.
- The override is reversible and leaves no persistent changes.
- The CI pipeline does not depend on complex git operations.
- The masquerading behavior is explicit and documented.
- The override is targeted and controlled rather than broad or hidden.
- The approach is safer and more auditable than modifying production code directly.
Q: What is the overall execution flow?
- The main repository is checked out locally or in CI.
- The production submodule is initialized and updated.
- Environment variables are set to indicate a production‑like environment and a masquerade flag.
- The modulino is executed.
- The modulino detects the masquerading environment.
- The modulino applies the override to the production behavior.
- The modulino runs the behavior using the overridden logic.
- The process ends, leaving the production code unchanged.