@dv.nghiem/flowdeck 0.3.8 → 0.4.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.
Files changed (136) hide show
  1. package/README.md +13 -122
  2. package/dist/agents/code-explorer.d.ts.map +1 -1
  3. package/dist/agents/mapper.d.ts.map +1 -1
  4. package/dist/agents/orchestrator.d.ts.map +1 -1
  5. package/dist/agents/planner.d.ts.map +1 -1
  6. package/dist/agents/specialist.d.ts.map +1 -1
  7. package/dist/dashboard/server.mjs +12 -2
  8. package/dist/hooks/compaction-hook.d.ts.map +1 -1
  9. package/dist/hooks/file-tracker.d.ts +6 -0
  10. package/dist/hooks/file-tracker.d.ts.map +1 -1
  11. package/dist/hooks/notifications.d.ts.map +1 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +752 -785
  14. package/dist/lib/completion-validator.d.ts +51 -0
  15. package/dist/lib/completion-validator.d.ts.map +1 -0
  16. package/dist/lib/recommended-question.d.ts +24 -0
  17. package/dist/lib/recommended-question.d.ts.map +1 -0
  18. package/dist/lib/research-gate.d.ts +97 -0
  19. package/dist/lib/research-gate.d.ts.map +1 -0
  20. package/dist/lib/research-gate.test.d.ts +2 -0
  21. package/dist/lib/research-gate.test.d.ts.map +1 -0
  22. package/dist/mcp/index.d.ts +14 -3
  23. package/dist/mcp/index.d.ts.map +1 -1
  24. package/dist/services/codegraph.d.ts +36 -0
  25. package/dist/services/codegraph.d.ts.map +1 -0
  26. package/dist/services/codegraph.test.d.ts +2 -0
  27. package/dist/services/codegraph.test.d.ts.map +1 -0
  28. package/dist/services/command-validator.d.ts +11 -0
  29. package/dist/services/command-validator.d.ts.map +1 -1
  30. package/dist/services/preflight-explorer.d.ts +130 -0
  31. package/dist/services/preflight-explorer.d.ts.map +1 -0
  32. package/dist/services/preflight-explorer.test.d.ts +25 -0
  33. package/dist/services/preflight-explorer.test.d.ts.map +1 -0
  34. package/dist/services/question-guard.d.ts +96 -0
  35. package/dist/services/question-guard.d.ts.map +1 -0
  36. package/dist/services/quick-router.d.ts +40 -1
  37. package/dist/services/quick-router.d.ts.map +1 -1
  38. package/dist/services/recommended-question.test.d.ts +2 -0
  39. package/dist/services/recommended-question.test.d.ts.map +1 -0
  40. package/dist/services/supervisor-binding.d.ts +3 -1
  41. package/dist/services/supervisor-binding.d.ts.map +1 -1
  42. package/dist/tools/codebase-index.d.ts +30 -0
  43. package/dist/tools/codebase-index.d.ts.map +1 -0
  44. package/dist/tools/codebase-index.test.d.ts +2 -0
  45. package/dist/tools/codebase-index.test.d.ts.map +1 -0
  46. package/dist/tools/codegraph-tool.d.ts +3 -0
  47. package/dist/tools/codegraph-tool.d.ts.map +1 -0
  48. package/dist/tools/planning-state-lib.d.ts +23 -0
  49. package/dist/tools/planning-state-lib.d.ts.map +1 -1
  50. package/docs/agents/index.md +154 -0
  51. package/docs/commands/fd-ask.md +71 -39
  52. package/docs/commands/fd-checkpoint.md +63 -8
  53. package/docs/commands/fd-deploy-check.md +166 -9
  54. package/docs/commands/fd-design.md +101 -0
  55. package/docs/commands/fd-discuss.md +87 -20
  56. package/docs/commands/fd-doctor.md +100 -13
  57. package/docs/commands/fd-done.md +215 -0
  58. package/docs/commands/fd-execute.md +104 -0
  59. package/docs/commands/fd-fix-bug.md +144 -24
  60. package/docs/commands/fd-map-codebase.md +85 -21
  61. package/docs/commands/fd-multi-repo.md +155 -40
  62. package/docs/commands/fd-new-feature.md +63 -19
  63. package/docs/commands/fd-plan.md +80 -27
  64. package/docs/commands/fd-quick.md +143 -29
  65. package/docs/commands/fd-reflect.md +81 -13
  66. package/docs/commands/fd-resume.md +65 -8
  67. package/docs/commands/fd-status.md +80 -12
  68. package/docs/commands/fd-suggest.md +114 -0
  69. package/docs/commands/fd-translate-intent.md +69 -9
  70. package/docs/commands/fd-verify.md +71 -14
  71. package/docs/commands/fd-write-docs.md +121 -8
  72. package/docs/concepts/architecture.md +163 -0
  73. package/docs/concepts/governance.md +242 -0
  74. package/docs/concepts/intelligence.md +145 -0
  75. package/docs/concepts/multi-repo.md +227 -0
  76. package/docs/concepts/workflows.md +205 -0
  77. package/docs/configuration/index.md +208 -0
  78. package/docs/configuration/opencode-settings.md +98 -0
  79. package/docs/getting-started/first-project.md +126 -0
  80. package/docs/getting-started/installation.md +73 -0
  81. package/docs/getting-started/quick-start.md +74 -0
  82. package/docs/index.md +36 -72
  83. package/docs/reference/hooks.md +176 -0
  84. package/docs/reference/rules.md +109 -0
  85. package/docs/skills/code-review.md +47 -0
  86. package/docs/skills/index.md +148 -0
  87. package/docs/skills/planning.md +39 -0
  88. package/package.json +1 -1
  89. package/src/commands/fd-deploy-check.md +2 -2
  90. package/src/commands/fd-discuss.md +128 -7
  91. package/src/commands/fd-done.md +196 -0
  92. package/src/commands/fd-execute.md +43 -6
  93. package/src/commands/fd-fix-bug.md +43 -6
  94. package/src/commands/fd-map-codebase.md +100 -20
  95. package/src/commands/fd-multi-repo.md +1 -1
  96. package/src/commands/fd-new-feature.md +14 -5
  97. package/src/commands/fd-plan.md +38 -1
  98. package/src/commands/fd-quick.md +77 -14
  99. package/src/commands/fd-resume.md +1 -1
  100. package/src/commands/fd-status.md +1 -1
  101. package/src/commands/fd-verify.md +16 -2
  102. package/src/commands/fd-write-docs.md +30 -5
  103. package/src/rules/common/behavioral.md +63 -0
  104. package/src/skills/codebase-mapping/SKILL.md +1 -1
  105. package/src/skills/context-load/SKILL.md +1 -1
  106. package/src/skills/multi-repo/SKILL.md +1 -1
  107. package/src/skills/repo-memory-graph/SKILL.md +1 -1
  108. package/dist/hooks/memory-hook.d.ts +0 -28
  109. package/dist/hooks/memory-hook.d.ts.map +0 -1
  110. package/dist/services/memory-store.d.ts +0 -73
  111. package/dist/services/memory-store.d.ts.map +0 -1
  112. package/dist/services/memory-store.test.d.ts +0 -2
  113. package/dist/services/memory-store.test.d.ts.map +0 -1
  114. package/dist/tools/memory-search.d.ts +0 -3
  115. package/dist/tools/memory-search.d.ts.map +0 -1
  116. package/dist/tools/memory-status.d.ts +0 -3
  117. package/dist/tools/memory-status.d.ts.map +0 -1
  118. package/docs/USER_GUIDE.md +0 -20
  119. package/docs/agents.md +0 -544
  120. package/docs/best-practices.md +0 -47
  121. package/docs/commands/fd-new-project.md +0 -24
  122. package/docs/commands.md +0 -557
  123. package/docs/configuration.md +0 -325
  124. package/docs/design-first-workflow.md +0 -94
  125. package/docs/feature-integration-architecture.md +0 -227
  126. package/docs/installation.md +0 -123
  127. package/docs/intelligence.md +0 -370
  128. package/docs/memory.md +0 -69
  129. package/docs/multi-repo.md +0 -201
  130. package/docs/notifications.md +0 -170
  131. package/docs/optimization-baseline.md +0 -21
  132. package/docs/quick-start.md +0 -197
  133. package/docs/rules.md +0 -432
  134. package/docs/skills.md +0 -417
  135. package/docs/workflows.md +0 -134
  136. package/src/commands/fd-new-project.md +0 -114
@@ -1,10 +1,123 @@
1
- ---
2
- description: Write documentation — explore public APIs, writer drafts, reviewer accuracy check
3
- ---
4
- Run the FlowDeck write-docs workflow to generate accurate project documentation.
1
+ # /fd-write-docs
5
2
 
6
- ## What Next?
3
+ **Purpose:** Explore APIs and generate accurate, up-to-date documentation from the codebase.
7
4
 
8
- 1. **Review documentation** → `/fd-verify staged`
9
- 2. **Deploy check** → `/fd-deploy-check`
10
- 3. **Create checkpoint** → `/fd-checkpoint`
5
+ ## Usage
6
+
7
+ /fd-write-docs [--scope=path] [--format=api,guide,readme]
8
+
9
+ ## Arguments
10
+
11
+ - `--scope=path` (optional) — limit documentation scope to a specific path
12
+ - `--format=api|guide|readme` (optional) — output format: `api` (API reference), `guide` (usage guide), `readme` (README). Default: all formats
13
+
14
+ ## What Happens
15
+
16
+ ### Step 1: CodeGraph Intelligence Check
17
+
18
+ Before any file exploration, check if codegraph provides a pre-built symbol index:
19
+ ```
20
+ codegraph action=check
21
+ ```
22
+
23
+ - **If codegraph indexed (fresh)**: Use `codegraph_search`, `codegraph_explore`, `codegraph_node` to enumerate exported symbols and API entry points. Log: "codegraph available — using symbol index for API discovery".
24
+ - **If codegraph absent or stale**: Fall through to @mapper-based exploration.
25
+
26
+ ### Step 2: Explore APIs
27
+
28
+ **If codegraph is available:**
29
+ ```
30
+ codegraph_search("export ") # exported symbols
31
+ codegraph_explore("<scope or src/>") # survey module structure
32
+ codegraph_context("<key entry points>") # full context per area
33
+ ```
34
+
35
+ **If codegraph is not available:**
36
+ Spawn `@mapper` to:
37
+ - Find all exported functions, classes, and types
38
+ - Identify public API entry points
39
+ - Map key workflows and integration points
40
+
41
+ Fallback commands:
42
+ ```bash
43
+ grep -rn "export " src/ --include="*.ts"
44
+ grep -rn "export interface\|export type\|export class" src/ --include="*.ts"
45
+ ```
46
+
47
+ ### Step 3: Draft Documentation
48
+
49
+ Spawn `@writer` to produce:
50
+
51
+ **API Reference:**
52
+ ```markdown
53
+ ## functionName(param: Type): ReturnType
54
+
55
+ Description of what the function does.
56
+
57
+ **Parameters:**
58
+ - `param` (Type) — description
59
+
60
+ **Returns:** description
61
+
62
+ **Example:**
63
+ \`\`\`typescript
64
+ const result = functionName(value);
65
+ \`\`\`
66
+ ```
67
+
68
+ **Usage Guide:**
69
+ - Step-by-step workflow with examples
70
+ - Common patterns and best practices
71
+ - Configuration options
72
+
73
+ **Troubleshooting:**
74
+ - Common errors and their solutions
75
+
76
+ ### Step 4: Review for Accuracy
77
+
78
+ Spawn `@reviewer` to verify:
79
+ - Every documented function/method actually exists
80
+ - Parameter types match the actual signatures
81
+ - Examples are syntactically correct
82
+ - No outdated API references
83
+
84
+ If codegraph is available: use `codegraph_node` to verify specific function signatures against documentation.
85
+
86
+ ### Step 5: Finalize
87
+
88
+ Writer incorporates feedback and writes final docs to:
89
+ - `README.md` — project overview and quick start
90
+ - `docs/API.md` — complete API reference
91
+ - `docs/USER_GUIDE.md` — detailed usage guide
92
+
93
+ ## Output / State
94
+
95
+ Updated documentation files:
96
+ - `README.md` — project overview and quick start
97
+ - `docs/API.md` — complete API reference
98
+ - `docs/USER_GUIDE.md` — detailed usage guide
99
+
100
+ Report: files written/updated, public APIs documented, any gaps found, codegraph used status.
101
+
102
+ ## Examples
103
+
104
+ **Generate all documentation formats:**
105
+ ```
106
+ /fd-write-docs
107
+ ```
108
+
109
+ **Generate only API reference for a specific scope:**
110
+ ```
111
+ /fd-write-docs --scope=src/api --format=api
112
+ ```
113
+
114
+ **Generate README for the project root:**
115
+ ```
116
+ /fd-write-docs --format=readme
117
+ ```
118
+
119
+ ## Related Commands
120
+
121
+ - `/fd-map-codebase` — generate the codebase documentation that informs API exploration
122
+ - `/fd-discuss` — explore before writing to understand what matters most
123
+ - `/fd-verify` — validate documentation accuracy after generation
@@ -0,0 +1,163 @@
1
+ # Architecture
2
+
3
+ FlowDeck is a plugin that runs inside OpenCode. It layers a structured multi-agent orchestration system on top of the base OpenCode runtime, contributing commands, specialist agents, runtime services, and event-driven hooks.
4
+
5
+ ## Layering
6
+
7
+ ```
8
+ OpenCode
9
+ └── FlowDeck Plugin
10
+ ├── Commands (CLI entry points)
11
+ ├── Agents (25 specialists, delegated by orchestrator)
12
+ ├── Services (governance, intelligence, council)
13
+ └── Hooks (session-start, compaction, shell-env, etc.)
14
+ ```
15
+
16
+ **OpenCode** provides the underlying runtime: tool execution, file I/O, shell access, MCP integrations, and the conversation UI.
17
+
18
+ **FlowDeck** adds the workflow layer on top. It does not replace OpenCode's core — it extends it with opinionated orchestration, persistent state, and AI safety services.
19
+
20
+ ## Four Subsystems
21
+
22
+ ### Commands
23
+
24
+ Commands are the user-facing entry points. They are registered as slash commands in the OpenCode CLI (e.g., `/fd-map-codebase`, `/fd-new-feature`, `/fd-plan`, `/fd-execute`). Each command:
25
+
26
+ 1. Reads current planning or codebase state
27
+ 2. Invokes the appropriate specialist agents via the `delegate` or `run-pipeline` tools
28
+ 3. Writes results back to `.planning/` state files
29
+ 4. Optionally triggers hooks to react to the state change
30
+
31
+ Commands are implemented as Markdown templates with frontmatter metadata in `src/commands/`. The plugin loader reads them and registers them at startup.
32
+
33
+ ### Agents
34
+
35
+ FlowDeck ships 25 specialist agents, each responsible for a narrow domain:
36
+
37
+ | Agent | Role |
38
+ |-------|------|
39
+ | `@orchestrator` | Coordinates the workflow; delegates to specialists |
40
+ | `@architect` | Designs system structure and component boundaries |
41
+ | `@planner` | Breaks features into wave-structured tasks |
42
+ | `@coder` | Implements features; follows TDD discipline |
43
+ | `@tester` | Writes and maintains tests |
44
+ | `@reviewer` | Reviews code quality and style |
45
+ | `@debugger` | Diagnoses and fixes failures |
46
+ | `@risk-analyst` | Identifies technical risk in plans |
47
+ | `@policy-enforcer` | Validates compliance with project rules |
48
+ | `@discusser` | Runs structured pre-planning Q&A |
49
+ | `@designer` | UI/UX design decisions |
50
+ | ... and 15 more | |
51
+
52
+ The orchestrator is the default agent. All other agents are invoked via the `delegate` tool or `run-pipeline` tool. Every agent inherits the currently active OpenCode model by default; individual agents can be overridden in `flowdeck.json`.
53
+
54
+ ### Services
55
+
56
+ Services are runtime components that run continuously, not as part of a linear workflow:
57
+
58
+ - **Governance services** — validate agent contracts, enforce delegation budgets, detect loops, and score workflow quality
59
+ - **Intelligence services** — compute patch trust scores, volatility maps, failure replays, and regression predictions
60
+ - **Council service** — synthesizes consensus from multiple specialized agents via the `council` tool
61
+
62
+ Services are invoked by hooks (before/after tool execution) or by commands that need on-demand analysis.
63
+
64
+ ### Hooks
65
+
66
+ Hooks are event handlers registered with OpenCode's plugin API. FlowDeck registers the following hooks:
67
+
68
+ | Hook | Trigger | Purpose |
69
+ |------|---------|---------|
70
+ | `session.started` | New session begins | Initialize planning state, load config |
71
+ | `session.idle` | Session idle detected | Generate idle summary, auto-learn |
72
+ | `experimental.session.compacting` | Context window near full | Compact session state |
73
+ | `tool.execute.before` | Before any tool runs | Patch trust, guard rails, telemetry, supervisor preflight |
74
+ | `tool.execute.after` | After any tool completes | Telemetry, supervisor post-execution review |
75
+ | `file.edited` | File changed on disk | Track file modifications |
76
+ | `shell.env` | Shell command runs | Inject FlowDeck state into shell |
77
+ | `todo.updated` | Todo list changes | Sync todo state |
78
+
79
+ ## State Flow
80
+
81
+ State flows through the system in a one-way pipeline:
82
+
83
+ ```
84
+ Commands
85
+ │ (invoke agents via delegate / run-pipeline)
86
+
87
+ Agents
88
+ │ (produce artifacts, write state)
89
+
90
+ Services (governance + intelligence)
91
+ │ (validate, score, predict — write to .codebase/)
92
+
93
+ Hooks
94
+ │ (react to tool events, trigger re-entry)
95
+
96
+ State files
97
+ ├── .planning/
98
+ │ STATE.md — current workflow phase, active feature, checkpoint
99
+ │ PLAN.md — wave-structured execution plan
100
+ │ PROJECT.md — project overview and constraints
101
+ │ ROADMAP.md — feature roadmap
102
+ │ FEATURE.md — current feature context
103
+ │ DISCUSS.md — pre-planning decisions
104
+ │ multi-repo/ — multi-repo coordination state
105
+ └── .codebase/
106
+ AGENT_SPANS.jsonl — causal delegation spans
107
+ BUDGETS.json — delegation budget consumption
108
+ DEADLOCK_SIGNALS.jsonl — loop/bounce detections
109
+ SCORECARDS.jsonl — per-run quality scores
110
+ CODEGRAPH.json — codebase structure index
111
+ VOLATILITY.json — change-frequency map
112
+ ```
113
+
114
+ Commands read from and write to `.planning/`. Services write to `.codebase/`. Hooks read both directories and may trigger re-entry into the command pipeline.
115
+
116
+ ## Model-Agnostic Design
117
+
118
+ FlowDeck makes no model assumptions. Every agent call passes through the active OpenCode model unless overridden per-agent in `flowdeck.json`:
119
+
120
+ ```json
121
+ {
122
+ "agents": {
123
+ "planner": { "model": "anthropic/claude-opus-4" },
124
+ "tester": { "model": "openai/gpt-4o-mini" }
125
+ }
126
+ }
127
+ ```
128
+
129
+ Agents not listed inherit the global model. Override at the agent level, not the system level.
130
+
131
+ ## Tool Map
132
+
133
+ FlowDeck registers these tools for use by agents and commands:
134
+
135
+ | Tool | Purpose |
136
+ |------|---------|
137
+ | `planning-state` | Read/write `.planning/STATE.md` |
138
+ | `codebase-state` | Read/write `.codebase/` state files |
139
+ | `workspace-state` | Snapshot workspace files and git status |
140
+ | `run-pipeline` | Execute a defined pipeline of agent steps |
141
+ | `delegate` | Invoke a named specialist agent |
142
+ | `council` | Run multiple agents and synthesize consensus |
143
+ | `volatility-map` | Compute change frequency per file |
144
+ | `failure-replay` | Reproduce and trace a prior failure |
145
+ | `decision-trace` | Record and replay decision rationale |
146
+ | `hash-edit` | Compute a content hash for an edit |
147
+ | `policy-engine` | Evaluate agent actions against project rules |
148
+ | `repo-memory` | Persistent memory across sessions |
149
+ | `context-generator` | Generate focused context summaries |
150
+ | `codegraph` | Query codebase structure from indexed graph |
151
+
152
+ ## Plugin Initialization
153
+
154
+ On startup, the plugin:
155
+
156
+ 1. Reads `flowdeck.json` from the project directory
157
+ 2. Registers all slash commands from `src/commands/`
158
+ 3. Registers all agents from `src/agents/`
159
+ 4. Registers all hooks with the OpenCode plugin API
160
+ 5. Registers tools, MCP servers, and skills directories
161
+ 6. Sets `default_agent` to `orchestrator` if not already configured
162
+
163
+ The plugin is passive until a user invokes a FlowDeck command or an OpenCode event triggers a hook.
@@ -0,0 +1,242 @@
1
+ # Governance
2
+
3
+ FlowDeck's governance layer makes multi-agent execution trustworthy and auditable. It consists of six runtime services that run continuously, intercepting agent tool calls, tracking delegation, enforcing budgets, and scoring workflow quality.
4
+
5
+ Governance is transparent — every service writes its output to a machine-readable file in `.codebase/` so runs can be audited after the fact.
6
+
7
+ ---
8
+
9
+ ## 1. Agent Contract Registry
10
+
11
+ Every agent type has a **contract**: a declarative specification of what it is allowed to do and what it must not do.
12
+
13
+ A contract defines:
14
+
15
+ - **allowed-tools** — the list of tools the agent may call
16
+ - **forbidden-tools** — tools the agent may never call
17
+ - **required-inputs** — fields that must be present in the delegation call
18
+ - **success-criteria** — conditions that must be true after execution
19
+
20
+ Example contract for `@coder`:
21
+
22
+ ```json
23
+ {
24
+ "agent": "coder",
25
+ "allowed-tools": ["read", "edit", "write", "bash", "run-pipeline"],
26
+ "forbidden-tools": ["delete", "remove", "drop"],
27
+ "required-inputs": ["prompt", "files"],
28
+ "success-criteria": [
29
+ "all edited files pass linter",
30
+ "no test coverage decrease"
31
+ ]
32
+ }
33
+ ```
34
+
35
+ Contracts are defined in `src/agents/` as part of each agent's configuration. The registry is consulted by the Agent Validator before and after every agent invocation.
36
+
37
+ ---
38
+
39
+ ## 2. Agent Validator
40
+
41
+ The Agent Validator checks every agent call against the agent's contract. It operates in three modes:
42
+
43
+ | Mode | Behavior |
44
+ |------|----------|
45
+ | `off` | No checking — governance overhead is zero |
46
+ | `advisory` | Logs violations; execution continues |
47
+ | `strict` | Throws an error and halts execution on violation |
48
+
49
+ **Before invocation checks:**
50
+
51
+ - The agent name resolves to a known contract
52
+ - All `required-inputs` are present in the delegation call
53
+ - No forbidden tools are in the call
54
+
55
+ **After invocation checks:**
56
+
57
+ - `success-criteria` conditions are evaluated against the execution result
58
+ - Violations are logged to `.codebase/AGENT_SPANS.jsonl` under the span's metadata
59
+
60
+ Configuration in `flowdeck.json`:
61
+
62
+ ```json
63
+ {
64
+ "governance": {
65
+ "validator": { "mode": "advisory" }
66
+ }
67
+ }
68
+ ```
69
+
70
+ ---
71
+
72
+ ## 3. Inter-Agent Trace Graph
73
+
74
+ Every delegation — the orchestrator invoking a specialist, or a specialist invoking a sub-agent — is recorded as a **causal span** in `.codebase/AGENT_SPANS.jsonl`.
75
+
76
+ Each span records:
77
+
78
+ ```json
79
+ {
80
+ "span_id": "s1a2b3c",
81
+ "parent_id": "s0a1b2c",
82
+ "agent": "coder",
83
+ "tool": "delegate",
84
+ "prompt": "Implement user authentication",
85
+ "files": ["src/auth/login.ts"],
86
+ "started_at": "2026-05-26T10:00:00Z",
87
+ "finished_at": "2026-05-26T10:05:00Z",
88
+ "violations": [],
89
+ "result": "success"
90
+ }
91
+ ```
92
+
93
+ Spans form a tree rooted at the orchestrator. This trace is used by:
94
+
95
+ - The Deadlock Detector to identify circular delegation
96
+ - The Workflow Scorecard to measure delegation depth
97
+ - Post-session audits to reconstruct exactly what ran
98
+
99
+ ---
100
+
101
+ ## 4. Delegation Budget
102
+
103
+ Every run has a **delegation budget** — per-run limits that prevent runaway agent chains. Budgets are tracked in `.codebase/BUDGETS.json`.
104
+
105
+ ```json
106
+ {
107
+ "run_id": "run-2026-05-26-001",
108
+ "limits": {
109
+ "maxToolCalls": 200,
110
+ "maxDepth": 8,
111
+ "maxSameStepRetries": 3,
112
+ "maxSubAgentDelegations": 40
113
+ },
114
+ "consumed": {
115
+ "toolCalls": 47,
116
+ "depth": 3,
117
+ "sameStepRetries": 1,
118
+ "subAgentDelegations": 12
119
+ }
120
+ }
121
+ ```
122
+
123
+ When a budget limit is reached, the agent receives an error and must stop or request user approval to continue.
124
+
125
+ Configuration:
126
+
127
+ ```json
128
+ {
129
+ "governance": {
130
+ "delegationBudget": {
131
+ "maxToolCalls": 200,
132
+ "maxDepth": 8,
133
+ "maxSameStepRetries": 3
134
+ }
135
+ }
136
+ }
137
+ ```
138
+
139
+ ---
140
+
141
+ ## 5. Deadlock / Loop Detector
142
+
143
+ The Deadlock Detector monitors spans and budget consumption for patterns that indicate an agent chain is stuck:
144
+
145
+ | Pattern | Detection |
146
+ |---------|-----------|
147
+ | **Bounce loop** | Same task delegated back to the same agent 3+ times |
148
+ | **Circular delegation** | Span tree contains a cycle (A → B → A) |
149
+ | **Step retry loop** | Same plan step attempted 3+ times without progress |
150
+ | **Stage stall** | No span completion for a configured time threshold |
151
+
152
+ When a pattern is detected, a signal is written to `.codebase/DEADLOCK_SIGNALS.jsonl`:
153
+
154
+ ```json
155
+ {
156
+ "signal_id": "dl-001",
157
+ "type": "bounce_loop",
158
+ "agent": "coder",
159
+ "task": "Implement user authentication",
160
+ "bounce_count": 3,
161
+ "last_span": "s1a2b3c",
162
+ "detected_at": "2026-05-26T10:15:00Z",
163
+ "auto_stop": false
164
+ }
165
+ ```
166
+
167
+ If `auto_stop` is `true` in the config, the orchestrator halts execution. Otherwise, it logs the signal and continues, notifying the user.
168
+
169
+ Configuration:
170
+
171
+ ```json
172
+ {
173
+ "governance": {
174
+ "deadlockDetection": {
175
+ "enabled": true,
176
+ "bounceThreshold": 3,
177
+ "autoStop": false
178
+ }
179
+ }
180
+ }
181
+ ```
182
+
183
+ ---
184
+
185
+ ## 6. Workflow Scorecard
186
+
187
+ After each `/fd-verify` run, a 10-dimension quality scorecard is written to `.codebase/SCORECARDS.jsonl`.
188
+
189
+ | Dimension | Description |
190
+ |-----------|-------------|
191
+ | `tdd_discipline` | Tests written before implementation |
192
+ | `design_first` | Discuss and plan completed before execute |
193
+ | `approval_gated` | Critical steps required explicit CONFIRM |
194
+ | `budget_efficiency` | Tool calls and depth used vs. limits |
195
+ | `conflict_resolved` | Merge conflicts resolved without force |
196
+ | `rule_compliance` | Project rules and contracts respected |
197
+ | `rollback_ready` | Every task had a rollback plan |
198
+ | `context_preserved` | No context loss between phases |
199
+ | `safety_gated` | Phase gating enforced throughout |
200
+ | `governance_traced` | All agent calls have spans |
201
+
202
+ Each dimension scores 0.0–1.0. The overall score is the weighted average. Scorecards enable post-hoc comparison of runs and identification of process regressions.
203
+
204
+ ---
205
+
206
+ ## Complete Configuration Example
207
+
208
+ Here is a fully annotated `flowdeck.json` section covering all governance options:
209
+
210
+ ```json
211
+ {
212
+ "governance": {
213
+ // Agent Validator — checks agent calls against contracts
214
+ "validator": {
215
+ "mode": "advisory" // "off" | "advisory" | "strict"
216
+ },
217
+
218
+ // Delegation Budget — per-run limits
219
+ "delegationBudget": {
220
+ "maxToolCalls": 200, // total tool calls allowed
221
+ "maxDepth": 8, // max delegation chain depth
222
+ "maxSameStepRetries": 3, // retries of the same plan step
223
+ "maxSubAgentDelegations": 40 // sub-agent calls per orchestrator call
224
+ },
225
+
226
+ // Deadlock / Loop Detector
227
+ "deadlockDetection": {
228
+ "enabled": true,
229
+ "bounceThreshold": 3, // bounces before signal fires
230
+ "circularThreshold": 2, // circular spans before signal fires
231
+ "autoStop": false // halt execution on first signal
232
+ },
233
+
234
+ // Workflow Scorecard
235
+ "scorecard": {
236
+ "enabled": true
237
+ }
238
+ }
239
+ }
240
+ ```
241
+
242
+ The governance layer adds measurable overhead only when set to `advisory` or `strict` mode. In `off` mode, no contract checks are performed and no spans are written (though deadlock detection still runs if enabled).
@@ -0,0 +1,145 @@
1
+ # Intelligence
2
+
3
+ FlowDeck's intelligence layer evaluates every change before it is applied. It scores edit safety, tracks file change history, reproduces prior failures, predicts regressions, and enforces workflow discipline through phase gating. All intelligence services run as hooks on every tool execution — no additional commands needed.
4
+
5
+ ---
6
+
7
+ ## Patch Trust Score
8
+
9
+ Before any `edit` tool call is applied, the **patch trust scorer** evaluates the edit and assigns a trust score from 0.0 (dangerous) to 1.0 (safe).
10
+
11
+ The score is computed from:
12
+
13
+ | Factor | Weight | What it measures |
14
+ |--------|--------|-----------------|
15
+ | File volatility | 25% | Historical change frequency from `.codebase/VOLATILITY.json` |
16
+ | Edit scope | 20% | Lines changed vs. file total — large rewrites score lower |
17
+ | Context window pressure | 15% | Remaining context space — edits under pressure score lower |
18
+ | Agent history | 20% | Historical failure rate of the calling agent on this file |
19
+ | Rule compliance | 20% | Does the edit violate project rules from `.flowdeck/rules/` |
20
+
21
+ **Threshold behavior:**
22
+
23
+ - Score >= 0.8 — edit applied automatically
24
+ - Score 0.5–0.8 — user notified; edit applied with confirmation prompt
25
+ - Score < 0.5 — edit blocked; user must resolve concerns or override explicitly
26
+
27
+ The `@policy-enforcer` agent is invoked automatically when the score is below threshold. The score is logged in the tool span metadata.
28
+
29
+ ---
30
+
31
+ ## Volatility Map
32
+
33
+ The **volatility map** tracks change frequency per file over the session and across sessions. It is maintained by the `volatility-map` tool and stored in `.codebase/VOLATILITY.json`.
34
+
35
+ ```json
36
+ {
37
+ "src/auth/login.ts": {
38
+ "changeCount": 7,
39
+ "lastChanged": "2026-05-26T09:30:00Z",
40
+ "avgRevisionsPerSession": 3.2,
41
+ "risk": "high"
42
+ },
43
+ "src/config/default.ts": {
44
+ "changeCount": 1,
45
+ "lastChanged": "2026-05-25T14:00:00Z",
46
+ "avgRevisionsPerSession": 0.4,
47
+ "risk": "low"
48
+ }
49
+ }
50
+ ```
51
+
52
+ Files with high volatility:
53
+
54
+ - Receive lower Patch Trust Scores automatically
55
+ - Trigger `@risk-analyst` review before large refactors
56
+ - Are flagged in the Workflow Scorecard under `context_preserved` if they change frequently within a single session
57
+
58
+ The volatility map is rebuilt on first session start and incrementally updated on each `edit` tool call.
59
+
60
+ ---
61
+
62
+ ## Failure Replay
63
+
64
+ When a task fails (test failure, build error, runtime crash), the **failure replay** service can reproduce the failure in isolation to generate a clean diagnostic trace.
65
+
66
+ Invoked by the `@debugger` agent via the `failure-replay` tool:
67
+
68
+ 1. The tool reads the original error context from the failed span in `AGENT_SPANS.jsonl`
69
+ 2. It re-runs the minimal subset of the task that caused the failure
70
+ 3. The trace is written to `.codebase/FAILURE_REPLAY.jsonl`:
71
+
72
+ ```json
73
+ {
74
+ "replay_id": "fr-001",
75
+ "original_span": "s1a2b3c",
76
+ "task": "Run user authentication tests",
77
+ "reproduced": true,
78
+ "root_cause": "Missing mock for auth service in test environment",
79
+ "trace": [
80
+ "Step 1: npm test src/auth/login.test.ts",
81
+ "Error: Cannot read property 'validate' of undefined",
82
+ "Step 2: Mock auth service — result: test passes"
83
+ ],
84
+ "fix_suggestion": "Add mock for auth service in login.test.ts line 42"
85
+ }
86
+ ```
87
+
88
+ Replays are deterministic — the same failure will reproduce the same trace. This prevents "heisenbugs" that only appear in full runs.
89
+
90
+ ---
91
+
92
+ ## Regression Prediction
93
+
94
+ Before a plan is executed, the **regression predictor** evaluates the planned changes against the volatility map and historical failure data to predict which tasks are likely to break tests.
95
+
96
+ The predictor runs as a pre-execution check in `/fd-execute`:
97
+
98
+ 1. For each task in the plan, look up every file the task will modify in `VOLATILITY.json`
99
+ 2. Cross-reference with historical failure data in `SCORECARDS.jsonl` — if similar changes broke tests before, flag the task
100
+ 3. Assign a regression probability score (0.0–1.0) per task and per wave
101
+
102
+ **Output appended to PLAN.md:**
103
+
104
+ ```markdown
105
+ ## Regression Predictions
106
+
107
+ | Task | Files | Regression Probability | Flag |
108
+ |------|-------|----------------------|------|
109
+ | 1a: Write user model | src/models/user.ts | 0.15 | low |
110
+ | 1b: Refactor auth service | src/auth/login.ts | 0.72 | high — historically fragile |
111
+ | 2a: Integration tests | src/auth/*.test.ts | 0.31 | medium |
112
+ ```
113
+
114
+ Tasks flagged `high` are presented to the user before `CONFIRM` in `/fd-plan`. The user may choose to skip, refactor, or add additional test coverage before proceeding.
115
+
116
+ ---
117
+
118
+ ## Phase Gating
119
+
120
+ Phase gating enforces workflow discipline by blocking entry into a phase unless the prerequisites from the previous phase are satisfied.
121
+
122
+ | Transition | Gate |
123
+ |------------|------|
124
+ | `/fd-new-feature` → `/fd-discuss` | `DISCUSS.md` must exist and have at least 3 Q&A entries |
125
+ | `/fd-discuss` → `/fd-plan` | `DISCUSS.md` must have a risk summary section |
126
+ | `/fd-plan` → `/fd-execute` | User must type `CONFIRM`; no blockers in regression predictions |
127
+ | `/fd-execute` → `/fd-verify` | All plan tasks must be marked `done` or `skipped` |
128
+
129
+ If a gate check fails, the command exits with a descriptive error listing the missing prerequisites. The user resolves them before retrying.
130
+
131
+ Phase gating is implemented by the `guard-rails` hook running in `tool.execute.before` — it intercepts command invocations and validates prerequisites before the command logic runs.
132
+
133
+ ---
134
+
135
+ ## Intelligence Tool Summary
136
+
137
+ | Tool / Hook | Service | Purpose |
138
+ |-------------|---------|---------|
139
+ | `patch-trust` hook | Patch Trust Score | Score edits before application |
140
+ | `volatility-map` tool + hook | Volatility Map | Track per-file change frequency |
141
+ | `failure-replay` tool | Failure Replay | Reproduce and trace prior failures |
142
+ | Regression predictor (in `/fd-plan`) | Regression Prediction | Score planned changes for breakage risk |
143
+ | `guard-rails` hook | Phase Gating | Enforce workflow discipline at phase boundaries |
144
+ | `policy-engine` tool | Rule Compliance | Evaluate edits against project rules |
145
+ | `hash-edit` tool | Edit Hashing | Content-address edits for deduplication |