mobile menu icon

Adding an Architectural Rule to Forbid Global Augmentation in TypeScript

Published by Manuel Rivero on 28/11/2026

Agentic-AI, AI experiments, Architecture, Testing


Introduction

In this short lab, we use our architecture-rules skill to add a rule that forbids global augmentation in TypeScript. We previously showed the skill in action when changing an existing rule in another lab.

The rule and why it uses TypeScript’s syntax tree

> 1. Add an architectural rule in tests/architecture-rules/type-conventions.spec.ts asserting that production code (src/**/*.ts) must not contain declare global. | 2. Add an explicit constraint in AGENTS.md under repository constraints instructing agents never to augment global scope (declare global) and to always use explicit structural dependency types instead.
I’ll use the frontend architecture-rules skill to add the check and document the constraint in AGENTS.md.
Show repository and skill inspection
• Explored
  └ Search ^## Repository architecture constraints|^## Scope in AGENTS.md
• Ran git status --short; rg --files --hidden -g AGENTS.md -g '!node_modules' -g '!.git' tests .; cat .SKILLS/architecture-rules/SKILL.md
  └ gate after the architecture workflow: run `npm run format:changed` immediately
    before `npm run agent-validate`. Report any command that could not run and its
    exact reason.
• Explored
  └ List architecture-rules
    Search type-conventions|assert.*Production|SourceFile|global in architecture-rules
• Ran cat tests/architecture-rules/type-conventions.spec.ts tests/architecture-rules/testHelpers.ts …
  └   "scripts.test:architecture": "vitest run tests/architecture-rules",
      "scripts.test:architecture:mutation": "python3 -m unittest discover -q -s scripts/architecture-test-mutation -p 'test_*.py' && python3 scripts/architecture-test-mutation/architectureTestMutationTesting.py"
    }
• Explored
  └ Read AGENTS.md
    Read AGENTS.md
    List src
• Ran cat tests/architecture-rules/counter-examples/return-type-of-typeof.md …
  └ 324:    return parser.parse_args(argv)
    352:        return 1
    363:    return 0 if all(result.killed for result in results) else 1
The rule belongs in type-conventions.spec.ts. I’ll use TypeScript’s syntax tree so it catches global augmentations even across line breaks while allowing mentions in comments and strings.

Adding the rule and its counter-example

Show file edits and diff check
• Exited (status 1) cat tests/architecture-rules/counter-examples/{production-code-uses-raw-register-event-handler,tests-do-not-use-reflection-set}.md …
  └ 
    if __name__ == "__main__":
        raise SystemExit(main())
        
• Edited 4 files (+62 -0)
  └ AGENTS.md (+3 -0)
+Never augment the global scope with `declare global`. Always use explicit
+structural types for dependencies.
+

  └ tests/architecture-rules/counter-examples/counter-examples.json (+12 -0)
+    "id": "production-typescript-augments-global-scope",
+    "description": "Production TypeScript augments the global Window type.",
+    "document": "production-typescript-augments-global-scope.md",

  └ tests/architecture-rules/counter-examples/production-typescript-augments-global-scope.md (+23 -0)
+# Global augmentation counter-example
+
+Create `src/types/GlobalAugmentationCounterExample.d.ts` with these contents.

  └ tests/architecture-rules/type-conventions.spec.ts (+24 -0)
+import { isModuleDeclaration, NodeFlags, ScriptKind } from "typescript";
+  containsAstNode,
+  parseSourceFile,
• Ran git diff --check; git diff --stat; rg -n 'production TypeScript|Never augment' AGENTS.md tests/architecture-rules/type-conventions.spec.ts tests/architecture-rules/counter-examples/production-typescript-augments-global-scope.md
  └ tests/architecture-rules/counter-examples/production-typescript-augments-global-scope.md:21:The rule `production TypeScript should not augment global scope` should fail
    tests/architecture-rules/type-conventions.spec.ts:31:    "production TypeScript should not augment global scope",
    AGENTS.md:231:Never augment the global scope with `declare global`. Always use explicit

Checking that the rule rejects global augmentation

The new rule passes against the current source tree. The mutation runner is checking the registered examples, including a temporary .d.ts file that augments Window.
Show repeated diff check
• Ran git diff --check; git diff --stat; rg -n 'production TypeScript|Never augment' AGENTS.md tests/architecture-rules/type-conventions.spec.ts tests/architecture-rules/counter-examples/production-typescript-augments-global-scope.md
  └ tests/architecture-rules/counter-examples/production-typescript-augments-global-scope.md:21:The rule `production TypeScript should not augment global scope` should fail
    tests/architecture-rules/type-conventions.spec.ts:31:    "production TypeScript should not augment global scope",
    AGENTS.md:231:Never augment the global scope with `declare global`. Always use explicit
The mutation runner has reached the last few counter-examples. It removes each temporary source file after testing it and reports which rule failed.
Show focused test and mutation run
• Ran npm run test:architecture -- --testNamePattern='production TypeScript should not augment global scope' && PYTHONDONTWRITEBYTECODE=1 npm run test:architecture:mutation
  └ Architectural rules without counter-examples:
      - none
    Warning: counter-example count (25) does not match architectural rule count (17).
    

Reviewing the counter-example

Added an AST-based rule in type-conventions.spec.ts that rejects declare global in src/**/*.ts, including .d.ts files. AGENTS.md now requires explicit structural dependency types.

The focused test passed on the current source. The counter-example augmented Window and triggered the new rule as expected. All 25 mutation counter-examples were caught.

The architecture-rules skill calls for review of the counter-example before the final validation gate. npm run agent-validate is pending that review.

This time, the agent followed the skill’s review step and paused before final validation to ask us to review the counter-example. The agent had skipped that step in our earlier lab, Changing an Architectural Rule Skilfully.

> I think the counter-example is correct

Running the full validation

I’ll verify that the mutation runner removed the temporary file, then run the focused test and the full frontend validation gate.
Show focused test and frontend validation
• Ran git status --short; git diff --check; if test -e src/types/GlobalAugmentationCounterExample.d.ts; then ls -l src/types/GlobalAugmentationCounterExample.d.ts; else test ! -e src/types/GlobalAugmentationCounterExample.d.ts; fi; npm run test:architecture -- --testName…
  └              persist transforms across runs with fsModuleCache: true
                 learn more: https://vitest.dev/guide/improving-performance#caching-between-reruns
    
• Ran /bin/bash -lc "npm run format:changed && npm run agent-validate > /tmp/hooks-agent-validate.log 2>&1"
  └ 
    tests/architecture-rules/counter-examples/counter-examples.json 43ms (unchanged)
    tests/architecture-rules/type-conventions.spec.ts 37ms (unchanged)
• Ran rg -n 'Test Files|Tests |built in' /tmp/hooks-agent-validate.log; git diff --check; git status --short
  └  M tests/architecture-rules/counter-examples/counter-examples.json
     M tests/architecture-rules/type-conventions.spec.ts
    ?? tests/architecture-rules/counter-examples/production-typescript-augments-global-scope.md
The declare global rule and the AGENTS.md constraint are complete. The focused test passed after the mutation runner removed the temporary counter-example. npm run agent-validate passed lint, all 1,830 tests, and the production build.
Back to Raw AI Labs