@cassiomc1/forgeloop 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursor/rules/project-loop.mdc +18 -0
- package/.forgeloop/.gitignore +2 -0
- package/.github/copilot-instructions.md +16 -0
- package/AGENTS.md +16 -0
- package/AGENT_COMPATIBILITY.md +147 -0
- package/CLAUDE.md +14 -0
- package/CONTRACT_COVERAGE.md +27 -0
- package/DELEGATION_PROTOCOL.md +91 -0
- package/ENG/accessibility-eng.md +155 -0
- package/ENG/clean-code-eng.md +223 -0
- package/ENG/design-code-eng.md +511 -0
- package/ENG/games-code-design-web-eng.md +751 -0
- package/ENG/perf-code-eng.md +441 -0
- package/ENG/premium-sites-studio-eng.md +320 -0
- package/ENG/sec-code-eng.md +706 -0
- package/ENG/test-code-eng.md +257 -0
- package/EXECUTION_STATE.md +107 -0
- package/GUIDE_ROUTER.md +274 -0
- package/LICENSE +21 -0
- package/LICENSE-DOCS.md +13 -0
- package/LOOP_ENGINEERING.md +551 -0
- package/LOOP_SYSTEM_DESIGN.md +394 -0
- package/ORCHESTRATOR_INTEGRATION.md +106 -0
- package/PROJECT_PROFILE.md +124 -0
- package/QUALITY_SCORECARD.md +54 -0
- package/README.md +492 -0
- package/TERMINOLOGY.md +21 -0
- package/THIRD_PARTY_NOTICES.md +129 -0
- package/THREAT_MODEL.md +35 -0
- package/package.json +51 -0
- package/schemas/delegated-result.schema.json +33 -0
- package/schemas/evidence.schema.json +15 -0
- package/schemas/execution-receipt.schema.json +46 -0
- package/schemas/routing-input.schema.json +17 -0
- package/schemas/routing-result.schema.json +17 -0
- package/schemas/task-brief.schema.json +24 -0
- package/schemas/work-state.schema.json +46 -0
- package/src/cli.js +341 -0
- package/src/commands/clear-state.js +11 -0
- package/src/commands/doctor.js +165 -0
- package/src/commands/init.js +42 -0
- package/src/commands/inspect.js +17 -0
- package/src/commands/route.js +32 -0
- package/src/commands/status.js +29 -0
- package/src/commands/update.js +109 -0
- package/src/commands/validate-protocol.js +133 -0
- package/src/commands/validate-receipt.js +19 -0
- package/src/commands/validate-state.js +30 -0
- package/src/core/agent-support.js +89 -0
- package/src/core/conformance.js +133 -0
- package/src/core/delegation.js +283 -0
- package/src/core/evidence.js +56 -0
- package/src/core/filesystem.js +122 -0
- package/src/core/inspect.js +115 -0
- package/src/core/json-safety.js +54 -0
- package/src/core/manifest.js +75 -0
- package/src/core/protocol.js +81 -0
- package/src/core/receipt.js +129 -0
- package/src/core/repository.js +19 -0
- package/src/core/router.js +296 -0
- package/src/core/schema-validation.js +179 -0
- package/src/core/templates.js +56 -0
- package/src/core/work-state.js +471 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: clean-code-eng
|
|
3
|
+
language: en
|
|
4
|
+
description: "Practices for readable, observable, secure code operated by AI agents."
|
|
5
|
+
version: "2026.09"
|
|
6
|
+
last-reviewed: "2026-08-10"
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# Clean Code for AI Agents
|
|
10
|
+
|
|
11
|
+
> Original operational synthesis influenced by Fabio Akita's "Clean Code for AI Agents" article. It is not a translation, reproduction, or work officially associated with the author.
|
|
12
|
+
>
|
|
13
|
+
> **Related documents**: for language-specific testing frameworks and tools, see [`test-code-eng.md`](./test-code-eng.md). For security, see [`sec-code-eng.md`](./sec-code-eng.md), the canonical reference for secrets, authorization, cryptography, and sensitive-data redaction. For visual/UX guidelines, see [`design-code-eng.md`](./design-code-eng.md). For HTML-based video and motion, see [HyperFrames](https://hyperframes.heygen.com). This guide focuses on code quality, structure, and operation.
|
|
14
|
+
>
|
|
15
|
+
> **Tooling policy**: identify the stack, the stage, and the applicable checks; prefer an already available equivalent that produces compatible evidence. Ask for authorization before installing a tool or changing the environment. If no safe equivalent exists, record the required check as blocked and never claim that it passed. Do not install merely optional resources.
|
|
16
|
+
|
|
17
|
+
## Context
|
|
18
|
+
|
|
19
|
+
Clean code is infrastructure for people and AI agents. In both cases, the reader needs to locate intent, validate a change, and understand consequences without relying on tacit knowledge. The practices below favor cohesive units, explicit boundaries, execution evidence, and safe operation.
|
|
20
|
+
|
|
21
|
+
## Real constraints of AI agents
|
|
22
|
+
|
|
23
|
+
- **Partial reads**: agents read files in chunks; extensive or multi-purpose files make context retrieval less reliable.
|
|
24
|
+
- **Attention and latency**: larger context does not eliminate attention degradation; concise test output and logs reduce cost and ambiguity.
|
|
25
|
+
- **Name-driven search**: specific, consistent names make `rg` and equivalent tools useful for locating the right unit.
|
|
26
|
+
- **Operational evidence**: predictable commands, automated tests, and structured observability make it possible to verify hypotheses rather than fill gaps by assumption.
|
|
27
|
+
|
|
28
|
+
## Priority practices
|
|
29
|
+
|
|
30
|
+
### 1. Cohesive functions and files
|
|
31
|
+
|
|
32
|
+
Make a function express one understandable responsibility and keep the file navigable. Ranges such as 4–20 lines for functions, 200–300 lines for files, and 500 lines as a ceiling are **review signals**, not mechanical rules. Split when cohesion, domain vocabulary, or reading improve; keep a documented exception when splitting would harm those criteria.
|
|
33
|
+
|
|
34
|
+
### 2. Single responsibility
|
|
35
|
+
|
|
36
|
+
Each module should have a clear reason to change. Separate business rules, use-case coordination, integration, and presentation so a local change does not require unnecessary reading or testing of the rest of the system.
|
|
37
|
+
|
|
38
|
+
### 3. Meaningful and searchable names
|
|
39
|
+
|
|
40
|
+
Prefer names that reveal intent and belong to the domain vocabulary. `InvoiceLineItemTotal` communicates more than `process`; if a search for a name returns many irrelevant results, treat that as a review signal, not as an absolute numerical target.
|
|
41
|
+
|
|
42
|
+
### 4. Comments with context, not noise
|
|
43
|
+
|
|
44
|
+
Record the why, an external constraint, a decision, or the provenance of non-obvious behavior. Update or remove stale comments: preserving a comment merely because it exists propagates incorrect context. Avoid describing syntax that the code already makes clear.
|
|
45
|
+
|
|
46
|
+
### 5. Explicit types and contracts
|
|
47
|
+
|
|
48
|
+
Types, schemas, and preconditions make inputs, outputs, and invalid states visible. Make contracts explicit at boundaries and validate external data; do not use annotations merely to satisfy a tool when they obscure the domain.
|
|
49
|
+
|
|
50
|
+
### 6. Intentional DRY
|
|
51
|
+
|
|
52
|
+
Extract truly repeated logic when the copies share the same rule and will evolve together. Do not join snippets merely because they look similar: a premature abstraction can hide important domain differences.
|
|
53
|
+
|
|
54
|
+
### 7. Tests the agent can run
|
|
55
|
+
|
|
56
|
+
Keep a documented, repeatable, self-contained command when the project provides one. Do not invent a command or install a tool merely to make a check runnable; record a required unavailable check as blocked. Every behavior change needs tests appropriate to its risk: unit tests for rules, integration tests for boundaries, and regression tests for real failures. See [`test-code-eng.md`](./test-code-eng.md) for language-specific choices.
|
|
57
|
+
|
|
58
|
+
### 8. Predictable structure
|
|
59
|
+
|
|
60
|
+
Use stack conventions when they make responsibilities easier to locate, but do not force a framework structure on a project that does not use it. Predictability should serve navigation and boundaries, not the appearance of compliance.
|
|
61
|
+
|
|
62
|
+
### 9. Risk-proportionate dependencies
|
|
63
|
+
|
|
64
|
+
Introduce project-owned interfaces or wrappers for I/O, volatile SDKs, expensive integrations, side-effecting dependencies, or when a fake makes the test clearer. Direct imports are acceptable for stable, pure, well-understood libraries. Do not create an abstraction without a consumer or an explicit decision.
|
|
65
|
+
|
|
66
|
+
### 10. Simple flow
|
|
67
|
+
|
|
68
|
+
Prefer guard clauses, early returns, and decomposition when they reduce implicit states. Two indentation levels are a useful review signal, not a prohibition: keep a larger block when it makes the business rule more readable.
|
|
69
|
+
|
|
70
|
+
### 11. Safe, actionable errors
|
|
71
|
+
|
|
72
|
+
Use stable codes for programmatic handling and include only safe diagnostic context. Never include raw values, secrets, passwords, tokens, PII, card numbers, or complete payloads in error messages, traces, or logs. Follow [`sec-code-eng.md`](./sec-code-eng.md) for classification, retention, access, and redaction.
|
|
73
|
+
|
|
74
|
+
### 12. Automatic formatting
|
|
75
|
+
|
|
76
|
+
Use the formatter accepted by the stack and apply it consistently. The tool resolves repetitive style; human and agent reviews should focus on intent, security, behavior, and contracts.
|
|
77
|
+
|
|
78
|
+
### 13. Obvious comments are debt
|
|
79
|
+
|
|
80
|
+
Remove comments that merely repeat the code and rewrite comments that have lost validity. A short, correct comment about a decision is worth more than a long, inaccurate history.
|
|
81
|
+
|
|
82
|
+
## Architecture and boundaries
|
|
83
|
+
|
|
84
|
+
Separate the system into three clear roles:
|
|
85
|
+
|
|
86
|
+
- **Domain** contains rules, types, and invariants that do not depend on network, disk, clock, or SDKs.
|
|
87
|
+
- **Application** coordinates use cases, transactions, flow authorization, and required ports.
|
|
88
|
+
- **Infrastructure** implements I/O, persistence, messaging, clock, SDKs, and adapters at the edges.
|
|
89
|
+
|
|
90
|
+
Keep I/O at the edges and make the domain depend on contracts rather than infrastructure details. Test contracts at both ends of a boundary and record relevant architecture decisions — context, decision, alternatives, and consequence — in an ADR or equivalent record. Tailor the separation to the system's size; the goal is to make dependencies and effects observable, not to impose a fashionable architecture.
|
|
91
|
+
|
|
92
|
+
## Async, concurrency, and external effects
|
|
93
|
+
|
|
94
|
+
- Propagate cancellation to every operation that can block and define an explicit timeout for every external call.
|
|
95
|
+
- Retry only transient failures, with a limited number of attempts, backoff, and jitter. Do not retry non-idempotent operations without an idempotency key or strategy.
|
|
96
|
+
- Limit concurrency to protect dependencies and local resources; clean up files, connections, tasks, and locks on success, failure, and cancellation.
|
|
97
|
+
- Define what can be repeated without changing the outcome and persist the evidence needed to detect duplicates.
|
|
98
|
+
- Test timeout, cancellation, cleanup, and race conditions with a controllable clock, scheduler, or dependencies; do not rely on arbitrary waits.
|
|
99
|
+
|
|
100
|
+
## Safe observability
|
|
101
|
+
|
|
102
|
+
Emit structured events with, at minimum, `event`, `level`, `request_id` or correlation ID, duration, result, and already-redacted fields. Establish stable names for events and results so alerts and queries do not depend on free text.
|
|
103
|
+
|
|
104
|
+
- Use **logs** for discrete events and safe diagnostic context.
|
|
105
|
+
- Use **metrics** for aggregated volume, latency, errors, and capacity.
|
|
106
|
+
- Use **traces** to follow a request across boundaries and dependencies.
|
|
107
|
+
- Define retention, access, and disposal according to operational need, cost, and security policy; shorter retention does not replace redaction.
|
|
108
|
+
|
|
109
|
+
Never treat logs, metrics, or traces as a place for sensitive data. The canonical reference for redaction and handling of this data is [`sec-code-eng.md`](./sec-code-eng.md).
|
|
110
|
+
|
|
111
|
+
## Evidence-driven debugging
|
|
112
|
+
|
|
113
|
+
If the root cause is not clear, formulate a hypothesis and add minimal temporary instrumentation in a controlled, non-production environment. Apply redaction/masking before emitting any data, reproduce the problem, compare the evidence with the hypothesis, and only then change behavior. Confirm removal of temporary instrumentation after the investigation; retain only structured events that are permanently useful.
|
|
114
|
+
|
|
115
|
+
## Framework-independent examples
|
|
116
|
+
|
|
117
|
+
### Safe error with a stable code
|
|
118
|
+
|
|
119
|
+
```text
|
|
120
|
+
return error(
|
|
121
|
+
code = "ACCOUNT_ID_INVALID",
|
|
122
|
+
message = "account identifier is invalid",
|
|
123
|
+
safe_context = { field = "account_id" }
|
|
124
|
+
)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The client can react to `ACCOUNT_ID_INVALID`; the internal detail belongs in a protected, already-redacted channel, not in the returned text.
|
|
128
|
+
|
|
129
|
+
### Structured event with redacted fields
|
|
130
|
+
|
|
131
|
+
```json
|
|
132
|
+
{
|
|
133
|
+
"event": "invoice.fetch.completed",
|
|
134
|
+
"level": "info",
|
|
135
|
+
"request_id": "req-7f3c",
|
|
136
|
+
"duration_ms": 84,
|
|
137
|
+
"result": "success",
|
|
138
|
+
"customer_reference": "[REDACTED]"
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
The event preserves correlation and outcome without recording payload, token, card, or PII in plain text.
|
|
143
|
+
|
|
144
|
+
### Propagated timeout and cancellation
|
|
145
|
+
|
|
146
|
+
```text
|
|
147
|
+
result = fetch_invoice(
|
|
148
|
+
invoice_id,
|
|
149
|
+
timeout = 1500ms,
|
|
150
|
+
cancellation = request.cancellation
|
|
151
|
+
)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
The caller treats timeout and cancellation as expected results, releases resources, and does not begin a blind retry.
|
|
155
|
+
|
|
156
|
+
### Injectable dependency for an I/O edge
|
|
157
|
+
|
|
158
|
+
```text
|
|
159
|
+
interface InvoiceStore:
|
|
160
|
+
load(invoice_id, cancellation) -> Invoice
|
|
161
|
+
|
|
162
|
+
class InvoiceService(store: InvoiceStore):
|
|
163
|
+
get(invoice_id, cancellation) -> Invoice:
|
|
164
|
+
return store.load(invoice_id, cancellation)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`InvoiceStore` is a justified port because it represents I/O; a pure mathematical function can be imported directly.
|
|
168
|
+
|
|
169
|
+
### Limited idempotent retry
|
|
170
|
+
|
|
171
|
+
```text
|
|
172
|
+
retry(
|
|
173
|
+
operation = send_receipt(idempotency_key),
|
|
174
|
+
attempts = 3,
|
|
175
|
+
backoff = exponential_with_jitter
|
|
176
|
+
)
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
The retry is limited and is safe only because the operation receives a verifiable idempotency key.
|
|
180
|
+
|
|
181
|
+
## Template for `CLAUDE.md` / `AGENTS.md`
|
|
182
|
+
|
|
183
|
+
Adapt this block to the repository and language:
|
|
184
|
+
|
|
185
|
+
```text
|
|
186
|
+
## Code and architecture
|
|
187
|
+
|
|
188
|
+
- Use cohesive functions and files; treat size and indentation limits as review signals.
|
|
189
|
+
- Keep domain, application, and infrastructure separate; I/O stays at the edges.
|
|
190
|
+
- Create wrappers for I/O, volatile SDKs, expensive integrations, or useful fakes; import stable, pure libraries directly.
|
|
191
|
+
- Update or remove stale comments. Document cohesion exceptions or relevant architecture decisions.
|
|
192
|
+
|
|
193
|
+
## Errors, async, and observability
|
|
194
|
+
|
|
195
|
+
- Use stable error codes and safe context. Never log raw values, secrets, tokens, PII, cards, or complete payloads.
|
|
196
|
+
- Propagate cancellation and configure timeouts. Retry only transient failures, with limited attempts, backoff, jitter, and idempotency before repeating external effects.
|
|
197
|
+
- Limit concurrency to protect dependencies and clean up resources on success, failure, and cancellation.
|
|
198
|
+
- Emit structured events with event, level, request_id, duration, result, and redacted fields.
|
|
199
|
+
- Use logs for safe diagnosis, metrics for aggregates, and traces to cross boundaries. Define retention and access.
|
|
200
|
+
|
|
201
|
+
## Debugging and tests
|
|
202
|
+
|
|
203
|
+
- Investigate through hypothesis and evidence in a controlled non-production environment; apply redaction and confirm removal of temporary instrumentation.
|
|
204
|
+
- Test rules, contracts, timeout, cancellation, cleanup, and race conditions according to risk.
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Definition of Done
|
|
208
|
+
|
|
209
|
+
- [ ] Functions, files, and abstractions were reviewed for cohesion, readability, and domain vocabulary; relevant exceptions are documented.
|
|
210
|
+
- [ ] Domain, application, and infrastructure have explicit boundaries; I/O is at the edges and integration contracts were tested.
|
|
211
|
+
- [ ] Errors expose a stable code and safe context; messages, logs, and traces contain no raw values, secrets, tokens, PII, cards, or complete payloads.
|
|
212
|
+
- [ ] External calls have a timeout, propagate cancellation, clean up resources, and use limited backoff/jitter retries only when idempotency permits.
|
|
213
|
+
- [ ] Concurrency limits and duplication risks were defined and tested, including relevant timeout, cancellation, and race conditions.
|
|
214
|
+
- [ ] Structured events include `event`, `level`, `request_id`, duration, result, and redacted fields; logs, metrics, traces, access, and retention have a defined purpose.
|
|
215
|
+
- [ ] Temporary investigation occurred only in a controlled environment, produced redacted evidence, and had its instrumentation removed or intentionally promoted.
|
|
216
|
+
|
|
217
|
+
## Summary
|
|
218
|
+
|
|
219
|
+
Clean code for agents reveals intent, bounds effects, and produces safe evidence. Size metrics help start a review conversation, but cohesion and clarity decide; observability, cancellation, and boundaries make the system verifiable in operation.
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
Influence source: [Clean Code for AI Agents, Fabio Akita](https://akitaonrails.com/en/2026/04/20/clean-code-for-ai-agents/).
|