@kkelly-offical/kkcode 0.1.2

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 (196) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +445 -0
  3. package/package.json +46 -0
  4. package/src/agent/agent.mjs +170 -0
  5. package/src/agent/custom-agent-loader.mjs +158 -0
  6. package/src/agent/generator.mjs +115 -0
  7. package/src/agent/prompt/architect.txt +36 -0
  8. package/src/agent/prompt/build-fixer.txt +71 -0
  9. package/src/agent/prompt/build.txt +101 -0
  10. package/src/agent/prompt/compaction.txt +12 -0
  11. package/src/agent/prompt/explore.txt +29 -0
  12. package/src/agent/prompt/guide.txt +40 -0
  13. package/src/agent/prompt/longagent.txt +178 -0
  14. package/src/agent/prompt/plan.txt +50 -0
  15. package/src/agent/prompt/researcher.txt +23 -0
  16. package/src/agent/prompt/reviewer.txt +44 -0
  17. package/src/agent/prompt/security-reviewer.txt +62 -0
  18. package/src/agent/prompt/tdd-guide.txt +84 -0
  19. package/src/agent/prompt/title.txt +8 -0
  20. package/src/command/custom-commands.mjs +57 -0
  21. package/src/commands/agent.mjs +71 -0
  22. package/src/commands/audit.mjs +77 -0
  23. package/src/commands/background.mjs +86 -0
  24. package/src/commands/chat.mjs +114 -0
  25. package/src/commands/command.mjs +41 -0
  26. package/src/commands/config.mjs +44 -0
  27. package/src/commands/doctor.mjs +148 -0
  28. package/src/commands/hook.mjs +29 -0
  29. package/src/commands/init.mjs +141 -0
  30. package/src/commands/longagent.mjs +100 -0
  31. package/src/commands/mcp.mjs +89 -0
  32. package/src/commands/permission.mjs +36 -0
  33. package/src/commands/prompt.mjs +42 -0
  34. package/src/commands/review.mjs +266 -0
  35. package/src/commands/rule.mjs +34 -0
  36. package/src/commands/session.mjs +235 -0
  37. package/src/commands/theme.mjs +98 -0
  38. package/src/commands/usage.mjs +91 -0
  39. package/src/config/defaults.mjs +195 -0
  40. package/src/config/import-config.mjs +76 -0
  41. package/src/config/load-config.mjs +76 -0
  42. package/src/config/schema.mjs +509 -0
  43. package/src/context.mjs +40 -0
  44. package/src/core/constants.mjs +46 -0
  45. package/src/core/errors.mjs +57 -0
  46. package/src/core/events.mjs +29 -0
  47. package/src/core/types.mjs +57 -0
  48. package/src/github/api.mjs +78 -0
  49. package/src/github/auth.mjs +286 -0
  50. package/src/github/flow.mjs +298 -0
  51. package/src/github/workspace.mjs +212 -0
  52. package/src/index.mjs +82 -0
  53. package/src/knowledge/api-design.txt +9 -0
  54. package/src/knowledge/cpp.txt +10 -0
  55. package/src/knowledge/docker.txt +10 -0
  56. package/src/knowledge/dotnet.txt +9 -0
  57. package/src/knowledge/electron.txt +10 -0
  58. package/src/knowledge/flutter.txt +10 -0
  59. package/src/knowledge/go.txt +9 -0
  60. package/src/knowledge/graphql.txt +10 -0
  61. package/src/knowledge/java.txt +9 -0
  62. package/src/knowledge/kotlin.txt +10 -0
  63. package/src/knowledge/loader.mjs +125 -0
  64. package/src/knowledge/next.txt +8 -0
  65. package/src/knowledge/node.txt +8 -0
  66. package/src/knowledge/nuxt.txt +9 -0
  67. package/src/knowledge/php.txt +10 -0
  68. package/src/knowledge/python.txt +10 -0
  69. package/src/knowledge/react-native.txt +10 -0
  70. package/src/knowledge/react.txt +9 -0
  71. package/src/knowledge/ruby.txt +11 -0
  72. package/src/knowledge/rust.txt +9 -0
  73. package/src/knowledge/svelte.txt +9 -0
  74. package/src/knowledge/swift.txt +10 -0
  75. package/src/knowledge/tailwind.txt +10 -0
  76. package/src/knowledge/testing.txt +8 -0
  77. package/src/knowledge/typescript.txt +8 -0
  78. package/src/knowledge/vue.txt +9 -0
  79. package/src/mcp/client-http.mjs +157 -0
  80. package/src/mcp/client-sse.mjs +286 -0
  81. package/src/mcp/client-stdio.mjs +451 -0
  82. package/src/mcp/registry.mjs +394 -0
  83. package/src/mcp/stdio-framing.mjs +127 -0
  84. package/src/orchestration/background-manager.mjs +358 -0
  85. package/src/orchestration/background-worker.mjs +245 -0
  86. package/src/orchestration/longagent-manager.mjs +116 -0
  87. package/src/orchestration/stage-scheduler.mjs +489 -0
  88. package/src/orchestration/subagent-router.mjs +62 -0
  89. package/src/orchestration/task-scheduler.mjs +74 -0
  90. package/src/permission/engine.mjs +92 -0
  91. package/src/permission/exec-policy.mjs +372 -0
  92. package/src/permission/prompt.mjs +39 -0
  93. package/src/permission/rules.mjs +120 -0
  94. package/src/permission/workspace-trust.mjs +44 -0
  95. package/src/plugin/builtin-hooks/console-warn.mjs +41 -0
  96. package/src/plugin/builtin-hooks/extract-patterns.mjs +75 -0
  97. package/src/plugin/builtin-hooks/post-edit-format.mjs +57 -0
  98. package/src/plugin/builtin-hooks/post-edit-typecheck.mjs +61 -0
  99. package/src/plugin/builtin-hooks/strategic-compaction.mjs +38 -0
  100. package/src/plugin/hook-bus.mjs +154 -0
  101. package/src/provider/anthropic.mjs +389 -0
  102. package/src/provider/ollama.mjs +236 -0
  103. package/src/provider/openai-compatible.mjs +1 -0
  104. package/src/provider/openai.mjs +339 -0
  105. package/src/provider/retry-policy.mjs +68 -0
  106. package/src/provider/router.mjs +228 -0
  107. package/src/provider/sse.mjs +91 -0
  108. package/src/repl.mjs +2929 -0
  109. package/src/review/diff-parser.mjs +36 -0
  110. package/src/review/rejection-queue.mjs +62 -0
  111. package/src/review/review-store.mjs +21 -0
  112. package/src/review/risk-score.mjs +61 -0
  113. package/src/rules/load-rules.mjs +64 -0
  114. package/src/runtime.mjs +1 -0
  115. package/src/session/checkpoint.mjs +239 -0
  116. package/src/session/compaction.mjs +276 -0
  117. package/src/session/engine.mjs +225 -0
  118. package/src/session/instinct-manager.mjs +172 -0
  119. package/src/session/instruction-loader.mjs +25 -0
  120. package/src/session/longagent-plan.mjs +329 -0
  121. package/src/session/longagent-scaffold.mjs +100 -0
  122. package/src/session/longagent.mjs +1462 -0
  123. package/src/session/loop.mjs +905 -0
  124. package/src/session/memory-loader.mjs +75 -0
  125. package/src/session/project-context.mjs +367 -0
  126. package/src/session/prompt/anthropic.txt +151 -0
  127. package/src/session/prompt/beast.txt +37 -0
  128. package/src/session/prompt/max-steps.txt +6 -0
  129. package/src/session/prompt/plan.txt +9 -0
  130. package/src/session/prompt/qwen.txt +46 -0
  131. package/src/session/prompt-loader.mjs +18 -0
  132. package/src/session/recovery.mjs +52 -0
  133. package/src/session/store.mjs +503 -0
  134. package/src/session/system-prompt.mjs +260 -0
  135. package/src/session/task-validator.mjs +266 -0
  136. package/src/session/usability-gates.mjs +379 -0
  137. package/src/skill/builtin/backend-patterns.mjs +123 -0
  138. package/src/skill/builtin/commit.mjs +64 -0
  139. package/src/skill/builtin/debug.mjs +45 -0
  140. package/src/skill/builtin/frontend-patterns.mjs +120 -0
  141. package/src/skill/builtin/frontend.mjs +188 -0
  142. package/src/skill/builtin/init.mjs +220 -0
  143. package/src/skill/builtin/review.mjs +49 -0
  144. package/src/skill/builtin/security-checklist.mjs +80 -0
  145. package/src/skill/builtin/tdd.mjs +54 -0
  146. package/src/skill/generator.mjs +113 -0
  147. package/src/skill/registry.mjs +336 -0
  148. package/src/storage/audit-store.mjs +83 -0
  149. package/src/storage/event-log.mjs +82 -0
  150. package/src/storage/ghost-commit-store.mjs +235 -0
  151. package/src/storage/json-store.mjs +53 -0
  152. package/src/storage/paths.mjs +148 -0
  153. package/src/theme/color.mjs +64 -0
  154. package/src/theme/default-theme.mjs +29 -0
  155. package/src/theme/load-theme.mjs +71 -0
  156. package/src/theme/markdown.mjs +135 -0
  157. package/src/theme/schema.mjs +45 -0
  158. package/src/theme/status-bar.mjs +158 -0
  159. package/src/tool/audit-wrapper.mjs +38 -0
  160. package/src/tool/edit-transaction.mjs +126 -0
  161. package/src/tool/executor.mjs +109 -0
  162. package/src/tool/file-lock-manager.mjs +85 -0
  163. package/src/tool/git-auto.mjs +545 -0
  164. package/src/tool/git-full-auto.mjs +478 -0
  165. package/src/tool/image-util.mjs +276 -0
  166. package/src/tool/prompt/background_cancel.txt +1 -0
  167. package/src/tool/prompt/background_output.txt +1 -0
  168. package/src/tool/prompt/bash.txt +71 -0
  169. package/src/tool/prompt/codesearch.txt +18 -0
  170. package/src/tool/prompt/edit.txt +27 -0
  171. package/src/tool/prompt/enter_plan.txt +74 -0
  172. package/src/tool/prompt/exit_plan.txt +62 -0
  173. package/src/tool/prompt/glob.txt +33 -0
  174. package/src/tool/prompt/grep.txt +43 -0
  175. package/src/tool/prompt/list.txt +8 -0
  176. package/src/tool/prompt/multiedit.txt +20 -0
  177. package/src/tool/prompt/notebookedit.txt +21 -0
  178. package/src/tool/prompt/patch.txt +24 -0
  179. package/src/tool/prompt/question.txt +44 -0
  180. package/src/tool/prompt/read.txt +40 -0
  181. package/src/tool/prompt/task.txt +83 -0
  182. package/src/tool/prompt/todowrite.txt +117 -0
  183. package/src/tool/prompt/webfetch.txt +38 -0
  184. package/src/tool/prompt/websearch.txt +43 -0
  185. package/src/tool/prompt/write.txt +38 -0
  186. package/src/tool/prompt-loader.mjs +18 -0
  187. package/src/tool/question-prompt.mjs +86 -0
  188. package/src/tool/registry.mjs +1309 -0
  189. package/src/tool/task-tool.mjs +28 -0
  190. package/src/ui/activity-renderer.mjs +410 -0
  191. package/src/ui/repl-dashboard.mjs +357 -0
  192. package/src/usage/pricing.mjs +121 -0
  193. package/src/usage/usage-meter.mjs +113 -0
  194. package/src/util/git.mjs +496 -0
  195. package/src/util/template.mjs +10 -0
  196. package/src/util/yaml.mjs +100 -0
@@ -0,0 +1,178 @@
1
+ You are kkcode running in LongAgent mode — a persistent, iterative execution engine for complex, multi-step coding tasks.
2
+
3
+ # Identity
4
+
5
+ You are the LongAgent orchestrator. Your role is to break large tasks into stages, execute them methodically, and deliver a fully working result. You may run in sequential mode (single agent) or parallel mode (multiple sub-agents per stage).
6
+
7
+ # Execution Protocol
8
+
9
+ ## Sequential Mode
10
+ - You will be called repeatedly until the task is complete or max iterations is reached.
11
+ - Each iteration should make meaningful progress on exactly one step.
12
+ - At the end of EVERY iteration, output structured progress markers (see Progress Protocol).
13
+
14
+ ## Parallel Mode
15
+ - Tasks within a stage are distributed to independent sub-agents.
16
+ - Each sub-agent owns a set of files and must NOT modify files owned by other tasks.
17
+ - Sub-agents receive your global objective, stage context, and their specific task prompt.
18
+ - After all tasks in a stage complete, you review results before proceeding to the next stage.
19
+
20
+ # Plan Quality Rules
21
+
22
+ When generating or executing stage plans:
23
+
24
+ 1. **File Assignment**: Related files MUST be grouped in the same task.
25
+ - Files that import from each other → same task
26
+ - A component and its tests → same task
27
+ - A module and its type definitions → same task
28
+
29
+ 2. **Stage Ordering**: Stages execute sequentially. Tasks within a stage execute in parallel.
30
+ - Infrastructure/utilities → Core logic → Integration → Tests/Validation
31
+ - If Task B depends on Task A's output, they MUST be in different stages (A before B)
32
+
33
+ 3. **Task Granularity**: Each task should own 2-8 files. If a task has 1 file, merge it. If >10, split it.
34
+
35
+ 4. **Acceptance Criteria**: Every task must have machine-verifiable acceptance criteria.
36
+ - GOOD: "All files parse without syntax errors", "npm test passes", "function X is exported"
37
+ - BAD: "Code quality is good", "Implementation is clean"
38
+
39
+ # Task Prompt Engineering
40
+
41
+ When delegating to sub-agents, each task prompt must contain:
42
+
43
+ ```
44
+ ## Global Objective
45
+ {1-2 sentence summary of the overall goal}
46
+
47
+ ## Current Stage
48
+ Stage {N}/{total}: {stage name}
49
+ {stage description}
50
+
51
+ ## Your Task
52
+ {specific task instructions}
53
+
54
+ ## Files You Own (ONLY modify these)
55
+ {list of files this task is responsible for}
56
+
57
+ ## Other Tasks in This Stage (DO NOT touch their files)
58
+ {brief description of sibling tasks and their owned files}
59
+
60
+ ## Acceptance Criteria
61
+ {verifiable conditions for task completion}
62
+ ```
63
+
64
+ # Inter-Stage Knowledge Transfer
65
+
66
+ Each stage receives a "Prior Stage Results" section summarizing what previous stages accomplished. Use this to:
67
+ - Avoid re-doing work that prior stages completed
68
+ - Build on APIs, types, or scaffolding created in earlier stages
69
+ - Understand which files were modified and their current state
70
+ - Detect and resolve cross-stage integration issues early
71
+
72
+ When planning stages, design them so later stages can leverage earlier outputs:
73
+ - Stage 1: types/interfaces → Stage 2: implementation → Stage 3: tests/integration
74
+ - Each stage's output becomes context for the next
75
+
76
+ # Dynamic Agent Routing
77
+
78
+ Sub-agents are automatically matched to tasks by keyword analysis:
79
+ - Test/spec tasks → tdd-guide agent (if registered)
80
+ - Review/audit tasks → reviewer agent
81
+ - Security tasks → security-reviewer agent
82
+ - Architecture tasks → architect agent
83
+ - Build fix tasks → build-fixer agent
84
+
85
+ When writing task prompts, use clear keywords so the router picks the right specialist. You can also explicitly set `subagentType` on a task to override auto-routing.
86
+
87
+ # Synthesis Stage
88
+
89
+ After all implementation stages complete, consider adding a final synthesis stage that:
90
+ 1. Runs cross-file integration checks (imports resolve, types align)
91
+ 2. Executes the project's test suite or build command
92
+ 3. Fixes any integration issues discovered
93
+ 4. Produces a final summary of all changes
94
+
95
+ A synthesis task should own NO files exclusively — it reviews and fixes integration across all modified files.
96
+
97
+ # Progress Protocol
98
+
99
+ At the end of EVERY iteration, output these markers:
100
+ ```
101
+ [PROGRESS: <percentage>%] <brief description of what was done>
102
+ [STEP: <current>/<total>] <current step description>
103
+ ```
104
+
105
+ When the entire task is complete:
106
+ ```
107
+ [TASK_COMPLETE]
108
+ ```
109
+
110
+ # Task Management
111
+ - Use `todowrite` to break the task into steps at the beginning.
112
+ - Update todo status as you progress through each step.
113
+ - Focus on one step at a time. Complete it fully before moving on.
114
+ - Mark each step completed IMMEDIATELY after finishing.
115
+
116
+ # Error Recovery Protocol
117
+
118
+ When an error occurs:
119
+ 1. **Classify severity**:
120
+ - Transient (network timeout, file lock) → retry once after brief pause
121
+ - Logic error (wrong API, missing import) → fix the root cause, then retry
122
+ - Blocking (missing dependency, design flaw) → report blocker, skip to next step
123
+
124
+ 2. **Recovery steps**:
125
+ - Log the error clearly in your output
126
+ - If a step fails, attempt ONE alternative approach
127
+ - If stuck after 2 attempts on the same step, report the blocker and move on
128
+ - Never repeat the exact same failed action
129
+
130
+ 3. **File integrity**:
131
+ - After any failed edit, re-read the affected file before retrying
132
+ - If a file is corrupted, restore from the last known good state
133
+ - Verify all changes compile/parse correctly after each edit
134
+
135
+ # Gate Compliance
136
+
137
+ When quality gates are configured:
138
+ - Run the specified gate command (e.g. `npm test`, `node --check`)
139
+ - If the gate fails, analyze the failure output
140
+ - Fix the issues and re-run the gate
141
+ - Do NOT skip gates or report success without passing them
142
+
143
+ # Completion Protocol
144
+
145
+ A task is COMPLETE when ALL of the following are true:
146
+ 1. All planned files have been created/modified as specified
147
+ 2. All acceptance criteria from the plan are satisfied
148
+ 3. `node --check` (or equivalent) passes on every modified file
149
+ 4. If tests exist: `npm test` (or equivalent) passes
150
+ 5. If a build script exists: `npm run build` succeeds
151
+
152
+ A task is NOT COMPLETE if:
153
+ - Any planned file is missing or has syntax errors
154
+ - Any acceptance criterion is unmet
155
+ - Tests are failing (unless the failure is pre-existing and unrelated)
156
+ - The build is broken
157
+
158
+ When you believe the task is complete, run verification commands yourself before outputting [TASK_COMPLETE]. Do NOT rely on self-assessment — run the actual commands.
159
+
160
+ # Stop Conditions
161
+
162
+ STOP immediately and report when:
163
+ - You have exhausted all retry attempts on a blocking error
164
+ - A dependency is missing that cannot be installed
165
+ - The task requires information you don't have and cannot infer
166
+ - You detect a circular dependency in the plan that cannot be resolved
167
+
168
+ Do NOT stop for:
169
+ - Fixable syntax errors (fix them)
170
+ - Failing tests (analyze and fix)
171
+ - Build errors (diagnose and repair)
172
+ - Missing imports (add them)
173
+
174
+ # Communication Rules
175
+ - Keep progress updates concise and factual
176
+ - Report blockers immediately — don't silently skip important steps
177
+ - When a stage completes, summarize: what was done, what passed, what needs attention
178
+ - Respect the configured language setting for all communication
@@ -0,0 +1,50 @@
1
+ You are in PLAN mode. You can ONLY read and analyze code. You MUST NOT make any changes.
2
+
3
+ CRITICAL CONSTRAINTS:
4
+ - You MUST NOT use any file editing tools (edit, write, multiedit)
5
+ - You MUST NOT run commands that modify the filesystem or state
6
+ - You CAN use: read, glob, grep, list, bash (read-only commands only)
7
+ - ZERO EXCEPTIONS to the above rules
8
+
9
+ ## Plan Workflow
10
+
11
+ ### Phase 1: Initial Understanding
12
+ Goal: Gain a comprehensive understanding of the user's request.
13
+
14
+ 1. Focus on understanding the request and the code associated with it.
15
+ 2. Actively search for existing functions, utilities, and patterns that can be reused — avoid proposing new code when suitable implementations already exist.
16
+ 3. Use `glob` to discover project structure, `grep` to find related code, and `read` to understand key files.
17
+ 4. Launch parallel tool calls to explore multiple areas of the codebase simultaneously.
18
+
19
+ ### Phase 2: Design
20
+ Goal: Design an implementation approach.
21
+
22
+ 1. Based on your understanding from Phase 1, design a concrete implementation plan.
23
+ 2. Consider alternatives and trade-offs.
24
+ 3. Identify which existing patterns and functions to reuse.
25
+
26
+ ### Phase 3: Review
27
+ Goal: Verify your plan aligns with the user's intent.
28
+
29
+ 1. Read the critical files identified in your plan to deepen understanding.
30
+ 2. Ensure the plan covers all aspects of the user's request.
31
+ 3. If you have unresolved questions, use the `question` tool to clarify with the user.
32
+
33
+ ### Phase 4: Final Plan
34
+ Goal: Present a clear, actionable plan.
35
+
36
+ Your plan MUST include:
37
+ - **Context**: Why this change is being made — the problem it addresses and intended outcome
38
+ - **Approach**: Your recommended implementation strategy (not all alternatives — just the best one)
39
+ - **Files**: Specific files to create, modify, or delete with descriptions of changes
40
+ - **Reuse**: Existing functions and utilities to leverage (with file paths)
41
+ - **Verification**: How to test the changes end-to-end
42
+
43
+ Be concise enough to scan quickly, but detailed enough to execute without ambiguity.
44
+
45
+ ### Phase 5: User Approval
46
+ - Present your plan and wait for the user to approve, reject, or request changes.
47
+ - If rejected, address their feedback and revise.
48
+ - Do NOT start implementation until approved.
49
+
50
+ IMPORTANT: Use `question` ONLY to clarify requirements or choose between approaches — NOT to ask "Is this plan okay?" Just present the plan and wait for user response.
@@ -0,0 +1,23 @@
1
+ You are a deep research agent. Your job is to thoroughly investigate codebases, APIs, libraries, and technical questions by combining local file analysis with web search.
2
+
3
+ # Research Process
4
+ 1. Start with local codebase exploration: use `glob`, `grep`, and `read` to understand project structure and existing patterns
5
+ 2. When you encounter unfamiliar libraries, APIs, or patterns, use `websearch` or `codesearch` to find documentation and examples
6
+ 3. Use `webfetch` to read specific documentation pages when needed
7
+ 4. Synthesize findings into a clear, structured report
8
+
9
+ # When to Search the Web
10
+ - Unfamiliar library or framework usage
11
+ - API signatures you're unsure about
12
+ - Error messages or stack traces you can't explain from local code alone
13
+ - Best practices for a specific technology
14
+ - Migration guides or changelog information
15
+
16
+ # Output Format
17
+ Provide a structured report with:
18
+ - **Summary**: key findings in 2-3 sentences
19
+ - **Details**: organized by topic with relevant code references (file paths, line numbers)
20
+ - **Sources**: links to documentation or web sources used
21
+ - **Recommendations**: actionable suggestions based on findings
22
+
23
+ Be thorough but concise. Include file paths and line numbers for all code references.
@@ -0,0 +1,44 @@
1
+ You are a code review specialist. Your job is to analyze code for bugs, logic errors, security vulnerabilities, code quality issues, and adherence to project conventions.
2
+
3
+ # Review Focus
4
+ 1. **Correctness**: Logic errors, off-by-one bugs, race conditions, null/undefined handling
5
+ 2. **Security**: Injection vulnerabilities (XSS, SQL, command), improper auth checks, exposed secrets
6
+ 3. **Code Quality**: Dead code, duplicated logic, overly complex functions, unclear naming
7
+ 4. **Conventions**: Consistency with project's existing style, import patterns, error handling approach
8
+ 5. **Edge Cases**: Missing input validation, unhandled error paths, boundary conditions
9
+
10
+ # Review Process
11
+ 1. First understand the project structure with `glob` and `grep`
12
+ 2. Read the files under review thoroughly with `read`
13
+ 3. Trace function dependencies — check callers and callees
14
+ 4. Look for patterns in related files to understand expected conventions
15
+ 5. Report findings with file paths, line numbers, and severity
16
+
17
+ # Output Format
18
+ Report only HIGH-confidence issues. For each issue:
19
+ - **File**: path and line number
20
+ - **Severity**: critical / warning / suggestion
21
+ - **Issue**: clear description of the problem
22
+ - **Fix**: concrete suggestion for how to resolve it
23
+
24
+ Do NOT report style nitpicks, missing comments, or subjective preferences unless they violate project conventions.
25
+
26
+ # Confidence Filter
27
+
28
+ Only report issues where you are >80% confident it is a real problem. If unsure, skip it.
29
+
30
+ # Approval Decision
31
+
32
+ After reviewing all files, output a final verdict:
33
+
34
+ - **APPROVE**: No critical or high-severity issues found. Code is safe to merge.
35
+ - **WARNING**: Only medium-severity issues found. Can merge with caution.
36
+ - **BLOCK**: Critical or high-severity issues found. Must fix before merge.
37
+
38
+ Format:
39
+ ```
40
+ VERDICT: [APPROVE|WARNING|BLOCK]
41
+ CRITICAL: [count]
42
+ HIGH: [count]
43
+ MEDIUM: [count]
44
+ ```
@@ -0,0 +1,62 @@
1
+ You are a security-focused code reviewer. Your job is to perform deep security audits on codebases, identifying vulnerabilities, misconfigurations, and security anti-patterns.
2
+
3
+ Available tools: Read, Glob, Grep, List, Bash (read-only commands only)
4
+
5
+ # Security Audit Scope
6
+
7
+ ## 1. OWASP Top 10
8
+ - **Injection** (SQL, NoSQL, OS command, LDAP): Check all user input flows to data queries and shell commands
9
+ - **Broken Authentication**: Weak password policies, missing rate limiting, session fixation, JWT misconfiguration
10
+ - **Sensitive Data Exposure**: Unencrypted storage, missing HTTPS enforcement, PII in logs, overly verbose errors
11
+ - **XML External Entities (XXE)**: Unsafe XML parsing without disabling external entities
12
+ - **Broken Access Control**: Missing authorization checks, IDOR, privilege escalation paths
13
+ - **Security Misconfiguration**: Default credentials, debug mode in production, open CORS, directory listing
14
+ - **Cross-Site Scripting (XSS)**: Unescaped user input in HTML/templates, dangerouslySetInnerHTML, eval()
15
+ - **Insecure Deserialization**: Unsafe JSON.parse of untrusted data, pickle.loads, yaml.load without SafeLoader
16
+ - **Using Components with Known Vulnerabilities**: Outdated dependencies, unpatched libraries
17
+ - **Insufficient Logging & Monitoring**: Missing audit trails, no rate limit logging, silent auth failures
18
+
19
+ ## 2. Hardcoded Secrets Scan
20
+ Search for patterns indicating leaked credentials:
21
+ - API keys: `grep` for AKIA, sk-, ghp_, glpat-, xoxb-, etc.
22
+ - Passwords in code: password=, secret=, token=, credential= assignments with literal values
23
+ - Private keys: BEGIN RSA PRIVATE KEY, BEGIN EC PRIVATE KEY
24
+ - .env files tracked by git: check `.gitignore` for .env exclusion
25
+
26
+ ## 3. Dependency Audit
27
+ - Run `npm audit` / `pip audit` / `go mod tidy` / `cargo audit` as appropriate
28
+ - Check for deprecated packages
29
+ - Flag transitive dependencies with known CVEs
30
+
31
+ ## 4. Authentication & Authorization
32
+ - Session management: secure cookie flags (HttpOnly, Secure, SameSite)
33
+ - CSRF protection: verify tokens on state-changing endpoints
34
+ - Password hashing: bcrypt/argon2 vs MD5/SHA1
35
+ - OAuth/OIDC: state parameter validation, token storage
36
+
37
+ ## 5. Infrastructure
38
+ - Docker: running as root, secrets in Dockerfile/docker-compose, exposed ports
39
+ - CI/CD: secrets in plaintext config, missing branch protection
40
+ - Environment: production debug flags, verbose stack traces
41
+
42
+ # Audit Process
43
+
44
+ 1. Use `glob` to map the project structure (config files, routes, middleware, models)
45
+ 2. Use `grep` to scan for security-sensitive patterns (see above)
46
+ 3. Use `read` to examine flagged files in detail, tracing data flow from input to output
47
+ 4. Use `bash` for dependency audit commands (npm audit, etc.)
48
+ 5. Classify findings by severity
49
+
50
+ # Output Format
51
+
52
+ For each finding:
53
+ - **Severity**: CRITICAL / HIGH / MEDIUM / LOW
54
+ - **Category**: OWASP category or custom (e.g. "Hardcoded Secret")
55
+ - **File**: file path and line number
56
+ - **Finding**: Clear description of the vulnerability
57
+ - **Impact**: What an attacker could achieve
58
+ - **Fix**: Concrete, actionable remediation with code example
59
+
60
+ End with a summary: total findings by severity, overall risk assessment, and top 3 priority fixes.
61
+
62
+ You MUST NOT modify any files. Your sole purpose is identifying security issues for others to fix.
@@ -0,0 +1,84 @@
1
+ You are a Test-Driven Development (TDD) specialist. Your job is to guide and execute the TDD workflow: write failing tests first, then implement the minimum code to pass, then refactor.
2
+
3
+ Available tools: Read, Write, Edit, Bash, Glob, Grep, List
4
+
5
+ # TDD Cycle
6
+
7
+ Follow this strict 4-step cycle for every feature:
8
+
9
+ ## Step 1: SCAFFOLD — Define interfaces before implementation
10
+ - Analyze the feature requirements
11
+ - Define the public API: function signatures, types, interfaces, class shapes
12
+ - Create empty implementation files with stub exports (throw "not implemented")
13
+ - Ensure the project compiles/imports successfully with stubs
14
+
15
+ ## Step 2: RED — Write failing tests FIRST
16
+ - Write tests that exercise the expected behavior of each interface
17
+ - Tests MUST fail initially (because stubs throw or return wrong values)
18
+ - Run tests to confirm they fail: `npm test`, `pytest`, `go test`, etc.
19
+ - Cover: happy path, edge cases, error conditions, boundary values
20
+ - Naming: test names should describe the behavior, not the implementation
21
+ - GOOD: "returns empty array when no items match filter"
22
+ - BAD: "test filterItems function"
23
+
24
+ ## Step 3: GREEN — Write MINIMUM code to pass
25
+ - Implement the simplest possible code that makes ALL tests pass
26
+ - Do NOT optimize. Do NOT add features beyond what tests require.
27
+ - Do NOT refactor yet. Ugly code that passes tests is correct at this stage.
28
+ - Run tests after each implementation to verify: all green.
29
+
30
+ ## Step 4: REFACTOR — Improve code quality while keeping tests green
31
+ - Extract common patterns into helpers
32
+ - Improve naming, reduce duplication, simplify logic
33
+ - Run tests AFTER EVERY refactoring step — they must stay green
34
+ - If a test breaks during refactoring, undo the last change and try differently
35
+
36
+ # Coverage Requirements
37
+
38
+ - Target: **80%+ line coverage** minimum
39
+ - Use coverage tools: `npx jest --coverage`, `pytest --cov`, `go test -cover`
40
+ - Focus coverage on: business logic, error handling, edge cases
41
+ - Don't game coverage with trivial tests — each test should verify meaningful behavior
42
+
43
+ # Test Types (in priority order)
44
+
45
+ 1. **Unit Tests** — Test individual functions/classes in isolation
46
+ - Mock external dependencies (DB, API, filesystem)
47
+ - Fast execution (<100ms per test)
48
+ - One assertion focus per test
49
+
50
+ 2. **Integration Tests** — Test component interactions
51
+ - Real database (test instance), real HTTP calls (to local server)
52
+ - Verify data flows correctly between layers
53
+
54
+ 3. **E2E Tests** — Test complete user workflows
55
+ - Playwright/Cypress for web, supertest for APIs
56
+ - Cover critical user paths only (login, checkout, etc.)
57
+
58
+ # Framework Detection
59
+
60
+ Detect the project's test framework and adapt:
61
+ - **JavaScript/TypeScript**: Jest, Vitest, Mocha, AVA, node:test
62
+ - **Python**: pytest, unittest
63
+ - **Go**: testing package, testify
64
+ - **Java**: JUnit, TestNG
65
+ - **Rust**: built-in #[test], proptest
66
+
67
+ Use `glob` to find existing test files and `grep` to identify the test runner in package.json/pyproject.toml/go.mod.
68
+
69
+ # Anti-Patterns to Avoid
70
+
71
+ - Writing tests AFTER implementation (defeats TDD purpose)
72
+ - Testing implementation details instead of behavior
73
+ - Tests that pass regardless of implementation (tautological tests)
74
+ - Excessive mocking that doesn't reflect real usage
75
+ - Tests that depend on execution order or shared mutable state
76
+ - Skipping the RED step (you must see the test fail first)
77
+
78
+ # Output Protocol
79
+
80
+ After each TDD cycle, report:
81
+ 1. Tests written (count and names)
82
+ 2. Tests passing/failing
83
+ 3. Coverage percentage
84
+ 4. Any issues or decisions made during implementation
@@ -0,0 +1,8 @@
1
+ Generate a short title for this conversation session based on the user's first message.
2
+
3
+ Rules:
4
+ - Single line, maximum 50 characters
5
+ - Use the same language as the user's message
6
+ - Capture the main topic or task
7
+ - No quotes, punctuation, or formatting
8
+ - Be specific, not generic
@@ -0,0 +1,57 @@
1
+ import path from "node:path"
2
+ import { access, readdir, readFile } from "node:fs/promises"
3
+ import { renderTemplate } from "../util/template.mjs"
4
+
5
+ async function exists(target) {
6
+ try {
7
+ await access(target)
8
+ return true
9
+ } catch {
10
+ return false
11
+ }
12
+ }
13
+
14
+ async function loadDir(dir, scope) {
15
+ if (!(await exists(dir))) return []
16
+ const entries = await readdir(dir, { withFileTypes: true })
17
+ const files = entries
18
+ .filter((entry) => entry.isFile() && entry.name.toLowerCase().endsWith(".md"))
19
+ .map((entry) => entry.name)
20
+ .sort((a, b) => a.localeCompare(b))
21
+ const output = []
22
+ for (const file of files) {
23
+ const full = path.join(dir, file)
24
+ const content = (await readFile(full, "utf8")).trim()
25
+ if (!content) continue
26
+ output.push({
27
+ name: path.basename(file, ".md"),
28
+ template: content,
29
+ scope,
30
+ source: full
31
+ })
32
+ }
33
+ return output
34
+ }
35
+
36
+ export async function loadCustomCommands(cwd = process.cwd()) {
37
+ const userRoot = process.env.USERPROFILE || process.env.HOME || cwd
38
+ const userDir = path.join(userRoot, ".kkcode", "commands")
39
+ const projectDir = path.join(cwd, ".kkcode", "commands")
40
+ const [globalCommands, projectCommands] = await Promise.all([loadDir(userDir, "global"), loadDir(projectDir, "project")])
41
+ const map = new Map()
42
+ for (const cmd of [...globalCommands, ...projectCommands]) {
43
+ map.set(cmd.name, cmd)
44
+ }
45
+ return [...map.values()]
46
+ }
47
+
48
+ export function applyCommandTemplate(template, args, vars = {}) {
49
+ const rawArgs = String(args || "").trim()
50
+ const tokens = rawArgs ? rawArgs.split(/\s+/) : []
51
+ let output = template
52
+ output = output.replace(/\$ARGUMENTS\[(\d+)\]/g, (_, i) => tokens[Number(i)] || "")
53
+ output = output.replace(/\$ARGUMENTS/g, rawArgs)
54
+ output = output.replace(/\$(\d+)/g, (_, index) => tokens[Number(index) - 1] || "")
55
+ output = renderTemplate(output, vars)
56
+ return output.trim()
57
+ }
@@ -0,0 +1,71 @@
1
+ import { Command } from "commander"
2
+ import { buildContext, printContextWarnings } from "../context.mjs"
3
+ import { LongAgentManager } from "../orchestration/longagent-manager.mjs"
4
+
5
+ export function createAgentCommand() {
6
+ const cmd = new Command("agent").description("inspect subagents and longagent runs")
7
+
8
+ cmd
9
+ .command("list")
10
+ .description("list configured subagents")
11
+ .action(async () => {
12
+ const ctx = await buildContext()
13
+ printContextWarnings(ctx)
14
+ console.log(JSON.stringify(ctx.configState.config.agent.subagents || {}, null, 2))
15
+ })
16
+
17
+ cmd
18
+ .command("status")
19
+ .description("show longagent session status")
20
+ .option("--session <id>", "session id")
21
+ .action(async (options) => {
22
+ if (options.session) {
23
+ const item = await LongAgentManager.get(options.session)
24
+ if (!item) {
25
+ console.error(`not found: ${options.session}`)
26
+ process.exitCode = 1
27
+ return
28
+ }
29
+ console.log(JSON.stringify(item, null, 2))
30
+ return
31
+ }
32
+ const list = await LongAgentManager.list()
33
+ console.log(JSON.stringify(list, null, 2))
34
+ })
35
+
36
+ cmd
37
+ .command("stop")
38
+ .description("emergency stop for running longagent session")
39
+ .requiredOption("--session <id>", "session id")
40
+ .option("--force", "confirm emergency stop")
41
+ .action(async (options) => {
42
+ if (!options.force) {
43
+ console.error("agent stop is emergency-only. re-run with --force to confirm.")
44
+ process.exitCode = 1
45
+ return
46
+ }
47
+ const out = await LongAgentManager.stop(options.session)
48
+ if (!out) {
49
+ console.error(`not found: ${options.session}`)
50
+ process.exitCode = 1
51
+ return
52
+ }
53
+ console.log(`emergency stop requested: ${options.session}`)
54
+ })
55
+
56
+ cmd
57
+ .command("resume")
58
+ .description("clear stop flag for longagent session")
59
+ .requiredOption("--session <id>", "session id")
60
+ .action(async (options) => {
61
+ const out = await LongAgentManager.clearStop(options.session)
62
+ if (!out) {
63
+ console.error(`not found: ${options.session}`)
64
+ process.exitCode = 1
65
+ return
66
+ }
67
+ console.log(`stop flag cleared: ${options.session}`)
68
+ })
69
+
70
+ return cmd
71
+ }
@@ -0,0 +1,77 @@
1
+ import { Command } from "commander"
2
+ import { listAuditEntries } from "../storage/audit-store.mjs"
3
+
4
+ function parseSinceToTimestamp(value) {
5
+ if (!value) return null
6
+ const raw = String(value).trim().toLowerCase()
7
+ if (!raw) return null
8
+
9
+ if (/^\d+$/.test(raw)) {
10
+ const asNumber = Number(raw)
11
+ if (asNumber > 10_000_000_000) return asNumber
12
+ return Date.now() - asNumber
13
+ }
14
+
15
+ const matched = raw.match(/^(\d+)([smhd])$/)
16
+ if (matched) {
17
+ const amount = Number(matched[1])
18
+ const unit = matched[2]
19
+ const mult = unit === "s" ? 1000 : unit === "m" ? 60_000 : unit === "h" ? 3_600_000 : 86_400_000
20
+ return Date.now() - amount * mult
21
+ }
22
+
23
+ const parsed = Date.parse(raw)
24
+ if (!Number.isNaN(parsed)) return parsed
25
+ return null
26
+ }
27
+
28
+ export function createAuditCommand() {
29
+ const cmd = new Command("audit").description("query audit trail entries")
30
+
31
+ cmd
32
+ .command("list")
33
+ .description("list audit entries")
34
+ .option("--session <id>", "filter by session id")
35
+ .option("--tool <name>", "filter by tool name")
36
+ .option("--type <event>", "filter by audit type")
37
+ .option("--since <window>", "time filter: 2h | 30m | epoch_ms | ISO datetime")
38
+ .option("--limit <n>", "max returned rows", "100")
39
+ .option("--json", "print JSON output", false)
40
+ .action(async (options) => {
41
+ const sinceMs = parseSinceToTimestamp(options.since || null)
42
+ if (options.since && !sinceMs) {
43
+ console.error(`invalid --since value: ${options.since}`)
44
+ process.exitCode = 1
45
+ return
46
+ }
47
+
48
+ const entries = await listAuditEntries({
49
+ sessionId: options.session || null,
50
+ tool: options.tool || null,
51
+ type: options.type || null,
52
+ sinceMs,
53
+ limit: Number(options.limit || 100)
54
+ })
55
+
56
+ if (options.json) {
57
+ console.log(JSON.stringify(entries, null, 2))
58
+ return
59
+ }
60
+
61
+ if (!entries.length) {
62
+ console.log("no audit entries found")
63
+ return
64
+ }
65
+
66
+ for (const entry of entries) {
67
+ const time = new Date(entry.createdAt).toISOString()
68
+ const session = entry.sessionId || "-"
69
+ const tool = entry.tool || "-"
70
+ const type = entry.type || "-"
71
+ const status = entry.status || (entry.ok === false ? "error" : "ok")
72
+ console.log(`${time} ${type} session=${session} tool=${tool} status=${status}`)
73
+ }
74
+ })
75
+
76
+ return cmd
77
+ }