feat: add HTTP response chaos rules - #2317
Draft
pmcelhaney wants to merge 1 commit into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a runtime-owned chaos API for HTTP response fault injection, wires it into the dispatcher pipeline, and exposes a shared registry through the REPL and multi-API Counterfact simulators.
Changes:
- Add
ChaosRule/ChaosRegistryAPIs (status/delay/header/body mutations, probability + boundednext()rules). - Apply chaos rules after normal dispatcher response processing and expose
chaos(pathPrefix?)in the REPL when a server-owned registry is provided. - Share exactly one
ChaosRegistryacross Counterfact API groups; add tests + docs + changeset.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/runtime/test/server/chaos.test.ts | New unit/integration coverage for chaos rules, registry selection, and dispatcher integration. |
| packages/runtime/src/server/dispatcher.ts | Applies selected chaos rule (including optional delay) after normal response normalization/validation. |
| packages/runtime/src/server/chaos.ts | Implements ChaosRule and ChaosRegistry for response-layer fault injection. |
| packages/runtime/src/index.ts | Exports chaos public API from @counterfact/runtime. |
| packages/runtime/README.md | Documents runtime usage of ChaosRegistry and key constraints (indefinite by default, Content-Type protected). |
| packages/repl/test/repl.test.ts | Verifies REPL only exposes chaos() when supplied a server-owned registry. |
| packages/repl/src/repl.ts | Adds optional chaos() global + help line when a registry is provided. |
| packages/repl/README.md | Documents how to supply a registry and share it across dispatchers + REPL. |
| packages/counterfact/test/app.test.ts | Ensures a single shared registry is used across groups and REPL rules affect multiple groups. |
| packages/counterfact/test/api-runner.test.ts | Verifies ApiRunner passes a supplied chaos registry through to the dispatcher. |
| packages/counterfact/src/app.ts | Creates one simulator-level ChaosRegistry and injects it into all runners + REPL sessions. |
| packages/counterfact/src/api-runner.ts | Threads optional/shared ChaosRegistry through runner construction into the dispatcher. |
| packages/counterfact/docs/reference.md | Adds Chaos API reference section describing behavior/precedence and constraints. |
| packages/counterfact/docs/patterns/test-fault-scenarios-with-chaos.md | New usage pattern page describing fault-testing workflow via REPL chaos rules. |
| packages/counterfact/docs/patterns/index.md | Links the new chaos pattern page and fixes the patterns listing/table formatting. |
| packages/counterfact/docs/features/repl.md | Documents chaos() usage from the REPL and multi-API implications. |
| .changeset/calm-chaos-rules.md | Patch changeset for runtime, REPL, and counterfact releases. |
Suppressed comments (2)
packages/runtime/src/server/chaos.ts:70
status(code)accepts any number today (includingNaN,Infinity, or values outside the HTTP status range), which can lead to invalid responses and confusing downstream behavior. Consider validating thatcodeis an integer in the 100–599 range and throwing aRangeErrorwhen it is not.
public status(code: number): this {
this.statusCode = code;
this.touch();
return this;
}
packages/runtime/src/server/chaos.ts:76
delay(ms)currently accepts negative,NaN, or infinite values. Negative delays are silently ignored by the dispatcher (> 0check), which can hide mistakes. Consider validatingmsas a finite, non-negative number and throwing aRangeErrorfor invalid inputs.
public delay(ms: number): this {
this.delayMilliseconds = ms;
this.touch();
return this;
}
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+48
to
+52
| public next(count = 1): this { | ||
| this.remaining = count; | ||
| this.touch(); | ||
| return this; | ||
| } |
14 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ChaosRuleandChaosRegistryAPIs for HTTP-response fault injectionOriginal Prompt
Add a
chaos()API for HTTP-layer fault injection.Manual acceptance tests
chaos("/orders").next(3).status(503)and confirm exactly three matching responses return 503.Content-Typeremains unchanged.Tasks
@counterfact/runtime.@counterfact/replwithout deep imports.counterfact.Repository learning check
always()ortimeout().Verification
./node_modules/.bin/tsc --build packages/runtime/tsconfig.json packages/repl/tsconfig.json packages/counterfact/tsconfig.json --force --pretty falsenode scripts/check-package-boundaries.mjsnpm_config_cache=/private/tmp/counterfact-npm-cache node scripts/check-publishable.mjsnpm_config_cache=/private/tmp/counterfact-npm-cache npm exec --yes --package=yarn@1.22.22 -- node scripts/test-package-closures.mjsgit diff --checkThe polling override used for watcher-heavy Jest runs avoids the current macOS sandbox's file-descriptor limit; production watcher configuration is unchanged.