aiwcli 0.10.3 → 0.11.1
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/bin/run.js +1 -1
- package/dist/commands/clear.js +28 -131
- package/dist/commands/init/index.js +3 -3
- package/dist/lib/gitignore-manager.d.ts +32 -0
- package/dist/lib/gitignore-manager.js +141 -2
- package/dist/templates/CLAUDE.md +8 -8
- package/dist/templates/_shared/.claude/commands/handoff-resume.md +64 -0
- package/dist/templates/_shared/.claude/commands/handoff.md +16 -10
- package/dist/templates/_shared/.claude/settings.json +7 -7
- package/dist/templates/_shared/hooks-ts/_utils/git-state.ts +2 -0
- package/dist/templates/_shared/hooks-ts/archive_plan.ts +159 -0
- package/dist/templates/_shared/hooks-ts/context_monitor.ts +147 -0
- package/dist/templates/_shared/hooks-ts/file-suggestion.ts +130 -0
- package/dist/templates/_shared/hooks-ts/pre_compact.ts +49 -0
- package/dist/templates/_shared/hooks-ts/session_end.ts +107 -0
- package/dist/templates/_shared/hooks-ts/session_start.ts +144 -0
- package/dist/templates/_shared/hooks-ts/task_create_capture.ts +48 -0
- package/dist/templates/_shared/hooks-ts/task_update_capture.ts +74 -0
- package/dist/templates/_shared/hooks-ts/user_prompt_submit.ts +83 -0
- package/dist/templates/_shared/lib-ts/CLAUDE.md +318 -0
- package/dist/templates/_shared/lib-ts/base/atomic-write.ts +12 -12
- package/dist/templates/_shared/lib-ts/base/constants.ts +22 -15
- package/dist/templates/_shared/lib-ts/base/git-state.ts +1 -1
- package/dist/templates/_shared/lib-ts/base/hook-utils.ts +129 -50
- package/dist/templates/_shared/lib-ts/base/inference.ts +28 -21
- package/dist/templates/_shared/lib-ts/base/logger.ts +15 -2
- package/dist/templates/_shared/lib-ts/base/state-io.ts +9 -7
- package/dist/templates/_shared/lib-ts/base/stop-words.ts +131 -131
- package/dist/templates/_shared/lib-ts/base/subprocess-utils.ts +142 -0
- package/dist/templates/_shared/lib-ts/base/utils.ts +69 -69
- package/dist/templates/_shared/lib-ts/context/context-formatter.ts +30 -24
- package/dist/templates/_shared/lib-ts/context/context-selector.ts +50 -32
- package/dist/templates/_shared/lib-ts/context/context-store.ts +76 -48
- package/dist/templates/_shared/lib-ts/context/plan-manager.ts +43 -23
- package/dist/templates/_shared/lib-ts/context/task-tracker.ts +10 -6
- package/dist/templates/_shared/lib-ts/handoff/document-generator.ts +11 -10
- package/dist/templates/_shared/lib-ts/handoff/handoff-reader.ts +158 -0
- package/dist/templates/_shared/lib-ts/templates/formatters.ts +6 -4
- package/dist/templates/_shared/lib-ts/types.ts +68 -55
- package/dist/templates/_shared/scripts/resolve_context.ts +24 -0
- package/dist/templates/_shared/scripts/resume_handoff.ts +345 -0
- package/dist/templates/_shared/scripts/save_handoff.ts +3 -3
- package/dist/templates/_shared/scripts/status_line.ts +687 -0
- package/dist/templates/cc-native/.claude/settings.json +175 -185
- package/dist/templates/cc-native/TEMPLATE-SCHEMA.md +15 -17
- package/dist/templates/cc-native/_cc-native/agents/CLAUDE.md +0 -2
- package/dist/templates/cc-native/_cc-native/hooks/CLAUDE.md +109 -135
- package/dist/templates/cc-native/_cc-native/hooks/add_plan_context.ts +119 -0
- package/dist/templates/cc-native/_cc-native/hooks/cc-native-plan-review.ts +1027 -0
- package/dist/templates/cc-native/_cc-native/hooks/plan_questions_early.ts +61 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/aggregate-agents.ts +156 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/artifacts.ts +792 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/cc-native-state.ts +199 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/cli-output-parser.ts +144 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/config.ts +57 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/constants.ts +83 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/corroboration.ts +115 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/debug.ts +80 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/index.ts +120 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/json-parser.ts +168 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/nul +3 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/orchestrator.ts +250 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/agent.ts +275 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/codex.ts +130 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/gemini.ts +107 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/index.ts +10 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/types.ts +23 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/state.ts +240 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/tsconfig.json +18 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/types.ts +385 -0
- package/dist/templates/cc-native/_cc-native/lib-ts/verdict.ts +72 -0
- package/dist/templates/cc-native/_cc-native/plan-review.config.json +14 -1
- package/oclif.manifest.json +1 -1
- package/package.json +2 -2
- package/dist/templates/_shared/hooks/__init__.py +0 -16
- package/dist/templates/_shared/hooks/__pycache__/__init__.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/archive_plan.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/context_enforcer.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/context_monitor.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/file-suggestion.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/pre_compact.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/session_end.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/session_start.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/task_create_atomicity.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/task_create_capture.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/task_update_capture.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/__pycache__/user_prompt_submit.cpython-313.pyc +0 -0
- package/dist/templates/_shared/hooks/archive_plan.py +0 -177
- package/dist/templates/_shared/hooks/context_monitor.py +0 -270
- package/dist/templates/_shared/hooks/file-suggestion.py +0 -215
- package/dist/templates/_shared/hooks/pre_compact.py +0 -104
- package/dist/templates/_shared/hooks/session_end.py +0 -173
- package/dist/templates/_shared/hooks/session_start.py +0 -206
- package/dist/templates/_shared/hooks/task_create_capture.py +0 -108
- package/dist/templates/_shared/hooks/task_update_capture.py +0 -145
- package/dist/templates/_shared/hooks/user_prompt_submit.py +0 -139
- package/dist/templates/_shared/lib/__init__.py +0 -1
- package/dist/templates/_shared/lib/__pycache__/__init__.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/__init__.py +0 -65
- package/dist/templates/_shared/lib/base/__pycache__/__init__.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/__pycache__/atomic_write.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/__pycache__/constants.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/__pycache__/hook_utils.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/__pycache__/inference.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/__pycache__/logger.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/__pycache__/stop_words.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/__pycache__/subprocess_utils.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/__pycache__/utils.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/base/atomic_write.py +0 -180
- package/dist/templates/_shared/lib/base/constants.py +0 -358
- package/dist/templates/_shared/lib/base/hook_utils.py +0 -339
- package/dist/templates/_shared/lib/base/inference.py +0 -307
- package/dist/templates/_shared/lib/base/logger.py +0 -305
- package/dist/templates/_shared/lib/base/stop_words.py +0 -221
- package/dist/templates/_shared/lib/base/subprocess_utils.py +0 -46
- package/dist/templates/_shared/lib/base/utils.py +0 -263
- package/dist/templates/_shared/lib/context/__init__.py +0 -102
- package/dist/templates/_shared/lib/context/__pycache__/__init__.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/cache.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/context_extractor.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/context_formatter.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/context_manager.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/context_selector.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/context_store.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/discovery.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/event_log.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/plan_archive.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/plan_manager.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/task_sync.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/__pycache__/task_tracker.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/context/context_formatter.py +0 -317
- package/dist/templates/_shared/lib/context/context_selector.py +0 -508
- package/dist/templates/_shared/lib/context/context_store.py +0 -653
- package/dist/templates/_shared/lib/context/plan_manager.py +0 -303
- package/dist/templates/_shared/lib/context/task_tracker.py +0 -188
- package/dist/templates/_shared/lib/handoff/__init__.py +0 -22
- package/dist/templates/_shared/lib/handoff/__pycache__/__init__.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/handoff/__pycache__/document_generator.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/handoff/document_generator.py +0 -278
- package/dist/templates/_shared/lib/templates/README.md +0 -206
- package/dist/templates/_shared/lib/templates/__init__.py +0 -36
- package/dist/templates/_shared/lib/templates/__pycache__/__init__.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/templates/__pycache__/formatters.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/templates/__pycache__/persona_questions.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/templates/__pycache__/plan_context.cpython-313.pyc +0 -0
- package/dist/templates/_shared/lib/templates/formatters.py +0 -146
- package/dist/templates/_shared/lib/templates/plan_context.py +0 -73
- package/dist/templates/_shared/scripts/__pycache__/save_handoff.cpython-313.pyc +0 -0
- package/dist/templates/_shared/scripts/__pycache__/status_line.cpython-313.pyc +0 -0
- package/dist/templates/_shared/scripts/save_handoff.py +0 -357
- package/dist/templates/_shared/scripts/status_line.py +0 -716
- package/dist/templates/cc-native/.claude/commands/cc-native/fresh-perspective.md +0 -8
- package/dist/templates/cc-native/.windsurf/workflows/cc-native/fresh-perspective.md +0 -8
- package/dist/templates/cc-native/MIGRATION.md +0 -86
- package/dist/templates/cc-native/_cc-native/hooks/__pycache__/add_plan_context.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/hooks/__pycache__/cc-native-plan-review.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/hooks/__pycache__/mark_questions_asked.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/hooks/__pycache__/plan_accepted.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/hooks/__pycache__/plan_questions_early.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/hooks/__pycache__/suggest-fresh-perspective.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/hooks/add_plan_context.py +0 -130
- package/dist/templates/cc-native/_cc-native/hooks/cc-native-plan-review.py +0 -954
- package/dist/templates/cc-native/_cc-native/hooks/plan_questions_early.py +0 -81
- package/dist/templates/cc-native/_cc-native/hooks/suggest-fresh-perspective.py +0 -340
- package/dist/templates/cc-native/_cc-native/lib/CLAUDE.md +0 -265
- package/dist/templates/cc-native/_cc-native/lib/__init__.py +0 -53
- package/dist/templates/cc-native/_cc-native/lib/__pycache__/__init__.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/__pycache__/atomic_write.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/__pycache__/constants.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/__pycache__/debug.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/__pycache__/orchestrator.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/__pycache__/state.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/__pycache__/utils.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/constants.py +0 -45
- package/dist/templates/cc-native/_cc-native/lib/debug.py +0 -139
- package/dist/templates/cc-native/_cc-native/lib/orchestrator.py +0 -362
- package/dist/templates/cc-native/_cc-native/lib/reviewers/__init__.py +0 -28
- package/dist/templates/cc-native/_cc-native/lib/reviewers/__pycache__/__init__.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/reviewers/__pycache__/agent.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/reviewers/__pycache__/base.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/reviewers/__pycache__/codex.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/reviewers/__pycache__/gemini.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/lib/reviewers/agent.py +0 -215
- package/dist/templates/cc-native/_cc-native/lib/reviewers/base.py +0 -88
- package/dist/templates/cc-native/_cc-native/lib/reviewers/codex.py +0 -124
- package/dist/templates/cc-native/_cc-native/lib/reviewers/gemini.py +0 -108
- package/dist/templates/cc-native/_cc-native/lib/state.py +0 -268
- package/dist/templates/cc-native/_cc-native/lib/utils.py +0 -1071
- package/dist/templates/cc-native/_cc-native/scripts/__pycache__/aggregate_agents.cpython-313.pyc +0 -0
- package/dist/templates/cc-native/_cc-native/scripts/aggregate_agents.py +0 -168
- package/dist/templates/cc-native/_cc-native/workflows/fresh-perspective.md +0 -134
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CC-native state accessor for context state.json.
|
|
3
|
+
* Deduplicates state access patterns for cc-native hooks.
|
|
4
|
+
* See cc-native-plan-review-spec.md §4.5
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
CcNativeState,
|
|
9
|
+
IterationState,
|
|
10
|
+
PlanReviewState,
|
|
11
|
+
QuestionsAskedState as _QuestionsAskedState,
|
|
12
|
+
} from "./types.js";
|
|
13
|
+
import { logInfo, logWarn } from "../../_shared/lib-ts/base/logger.js";
|
|
14
|
+
import { nowIso } from "../../_shared/lib-ts/base/utils.js";
|
|
15
|
+
import { getContextBySessionId, saveState } from "../../_shared/lib-ts/context/context-store.js";
|
|
16
|
+
import type { ContextState } from "../../_shared/lib-ts/types.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* ContextState extended with the cc_native method-specific data.
|
|
20
|
+
* ContextState doesn't include cc_native in its type definition because it's
|
|
21
|
+
* a shared type. This local extension bridges the gap for cc-native code.
|
|
22
|
+
*/
|
|
23
|
+
type CcNativeContextState = ContextState & { cc_native?: CcNativeState };
|
|
24
|
+
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// Core State Access
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Get cc_native state from context state.json.
|
|
31
|
+
* Returns the cc_native dict or null if not found.
|
|
32
|
+
*/
|
|
33
|
+
export function getCcNativeState(
|
|
34
|
+
sessionId: string,
|
|
35
|
+
projectRoot: string,
|
|
36
|
+
): CcNativeState | null {
|
|
37
|
+
try {
|
|
38
|
+
const state = getContextBySessionId(sessionId, projectRoot) as CcNativeContextState | null;
|
|
39
|
+
if (state?.cc_native && typeof state.cc_native === "object") {
|
|
40
|
+
return state.cc_native;
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
// Fail-safe: return null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Save cc_native state to context state.json.
|
|
51
|
+
* Returns true on success, false on failure.
|
|
52
|
+
*/
|
|
53
|
+
export function saveCcNativeState(
|
|
54
|
+
sessionId: string,
|
|
55
|
+
projectRoot: string,
|
|
56
|
+
ccNativeData: CcNativeState,
|
|
57
|
+
): boolean {
|
|
58
|
+
try {
|
|
59
|
+
const state = getContextBySessionId(sessionId, projectRoot) as CcNativeContextState | null;
|
|
60
|
+
if (state) {
|
|
61
|
+
state.cc_native = ccNativeData;
|
|
62
|
+
saveState(state.id, state, projectRoot);
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
} catch (error: unknown) {
|
|
66
|
+
logWarn("utils", `Failed to save cc_native state: ${error}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Plan Review State
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Check if this exact plan has already been reviewed in this session.
|
|
78
|
+
*/
|
|
79
|
+
export function isPlanAlreadyReviewed(
|
|
80
|
+
sessionId: string,
|
|
81
|
+
planHash: string,
|
|
82
|
+
projectRoot: string,
|
|
83
|
+
): boolean {
|
|
84
|
+
const ccNative = getCcNativeState(sessionId, projectRoot);
|
|
85
|
+
if (!ccNative) return false;
|
|
86
|
+
const reviewState = ccNative.plan_review;
|
|
87
|
+
return reviewState?.plan_hash === planHash;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Check if this plan hash was previously reviewed and denied.
|
|
92
|
+
* Matches any deny variant: "deny", "hook_deny_iteration", "hook_deny_final".
|
|
93
|
+
*/
|
|
94
|
+
export function wasPlanPreviouslyDenied(
|
|
95
|
+
sessionId: string,
|
|
96
|
+
planHash: string,
|
|
97
|
+
projectRoot: string,
|
|
98
|
+
): boolean {
|
|
99
|
+
const ccNative = getCcNativeState(sessionId, projectRoot);
|
|
100
|
+
if (!ccNative) return false;
|
|
101
|
+
const reviewState = ccNative.plan_review;
|
|
102
|
+
if (reviewState?.plan_hash !== planHash) return false;
|
|
103
|
+
const decision = reviewState.decision ?? "";
|
|
104
|
+
return decision === "deny" || decision.startsWith("hook_deny");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Mark this plan as reviewed (stores hash and decision in state.json).
|
|
109
|
+
*/
|
|
110
|
+
export function markPlanReviewed(
|
|
111
|
+
sessionId: string,
|
|
112
|
+
planHash: string,
|
|
113
|
+
projectRoot: string,
|
|
114
|
+
hookName = "cc-native",
|
|
115
|
+
iterationState?: IterationState,
|
|
116
|
+
decision = "allow",
|
|
117
|
+
): void {
|
|
118
|
+
try {
|
|
119
|
+
const ccNative = getCcNativeState(sessionId, projectRoot) ?? {};
|
|
120
|
+
|
|
121
|
+
const reviewData: PlanReviewState = {
|
|
122
|
+
plan_hash: planHash,
|
|
123
|
+
reviewed_at: nowIso(),
|
|
124
|
+
decision,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
if (iterationState) {
|
|
128
|
+
reviewData.iteration = {
|
|
129
|
+
current: iterationState.current ?? 1,
|
|
130
|
+
max: iterationState.max ?? 1,
|
|
131
|
+
complexity: iterationState.complexity ?? "unknown",
|
|
132
|
+
};
|
|
133
|
+
const {history} = iterationState;
|
|
134
|
+
if (history && history.length > 0) {
|
|
135
|
+
const lastEntry = history.at(-1);
|
|
136
|
+
if (lastEntry) {
|
|
137
|
+
reviewData.iteration.latest_verdict = lastEntry.verdict ?? "unknown";
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
ccNative.plan_review = reviewData;
|
|
143
|
+
|
|
144
|
+
if (saveCcNativeState(sessionId, projectRoot, ccNative)) {
|
|
145
|
+
const iterInfo = iterationState
|
|
146
|
+
? ` (iteration ${reviewData.iteration?.current ?? "?"}/${reviewData.iteration?.max ?? "?"})`
|
|
147
|
+
: "";
|
|
148
|
+
logInfo(hookName, `Saved plan review state (hash: ${planHash})${iterInfo}`);
|
|
149
|
+
} else {
|
|
150
|
+
logWarn(
|
|
151
|
+
hookName,
|
|
152
|
+
`Failed to save plan review state for session ${sessionId}`,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
} catch (error: unknown) {
|
|
156
|
+
logWarn(hookName, `Failed to mark plan reviewed: ${error}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ---------------------------------------------------------------------------
|
|
161
|
+
// Questions Asked State
|
|
162
|
+
// ---------------------------------------------------------------------------
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Check if AskUserQuestion was called this session.
|
|
166
|
+
* Returns false on any error (fail-safe: allow feature to work).
|
|
167
|
+
*/
|
|
168
|
+
export function wasQuestionsAsked(
|
|
169
|
+
sessionId: string,
|
|
170
|
+
projectRoot: string,
|
|
171
|
+
): boolean {
|
|
172
|
+
const ccNative = getCcNativeState(sessionId, projectRoot);
|
|
173
|
+
if (!ccNative) return false;
|
|
174
|
+
return ccNative.questions_asked?.asked === true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Mark that AskUserQuestion was called. Returns true on success.
|
|
179
|
+
* Only stores timestamp, no user data.
|
|
180
|
+
*/
|
|
181
|
+
export function markQuestionsAsked(
|
|
182
|
+
sessionId: string,
|
|
183
|
+
projectRoot: string,
|
|
184
|
+
): boolean {
|
|
185
|
+
try {
|
|
186
|
+
const ccNative = getCcNativeState(sessionId, projectRoot) ?? {};
|
|
187
|
+
|
|
188
|
+
ccNative.questions_asked = {
|
|
189
|
+
asked: true,
|
|
190
|
+
asked_at: nowIso(),
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
return saveCcNativeState(sessionId, projectRoot, ccNative);
|
|
194
|
+
} catch (error: unknown) {
|
|
195
|
+
logWarn("utils", `Failed to mark questions asked: ${error}`);
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified Claude CLI output parser.
|
|
3
|
+
* Deduplicates identical logic from orchestrator.py and reviewers/agent.py.
|
|
4
|
+
* See cc-native-plan-review-spec.md §4.6
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { parseJsonMaybe } from "./json-parser.js";
|
|
8
|
+
import { logDebug, logError, logWarn } from "../../_shared/lib-ts/base/logger.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Parse Claude CLI JSON output, handling various formats.
|
|
12
|
+
*
|
|
13
|
+
* Claude CLI can output in several formats:
|
|
14
|
+
* - Direct structured_output dict
|
|
15
|
+
* - Assistant message with StructuredOutput tool use
|
|
16
|
+
* - List of events with assistant messages
|
|
17
|
+
* - Raw text with embedded JSON (heuristic fallback)
|
|
18
|
+
*
|
|
19
|
+
* @param raw - Raw stdout from Claude CLI
|
|
20
|
+
* @param requireFields - Optional fields to validate in heuristic fallback
|
|
21
|
+
* @returns Parsed JSON dict or null if parsing failed
|
|
22
|
+
*/
|
|
23
|
+
export function parseCliOutput(
|
|
24
|
+
raw: string,
|
|
25
|
+
requireFields?: string[],
|
|
26
|
+
): null | Record<string, unknown> {
|
|
27
|
+
try {
|
|
28
|
+
const result: unknown = JSON.parse(raw);
|
|
29
|
+
|
|
30
|
+
if (result !== null && typeof result === "object" && !Array.isArray(result)) {
|
|
31
|
+
const dict = result as Record<string, unknown>;
|
|
32
|
+
|
|
33
|
+
// Strategy 1: Direct structured_output key
|
|
34
|
+
if ("structured_output" in dict) {
|
|
35
|
+
logDebug("cli_parser", "Found structured_output in root dict");
|
|
36
|
+
return dict.structured_output as Record<string, unknown>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Strategy 1.5: Session result envelope (type: "result")
|
|
40
|
+
// When the model fails to call StructuredOutput, the CLI returns a
|
|
41
|
+
// session metadata object with type/subtype/duration_ms/usage/etc.
|
|
42
|
+
// but no structured_output key. Check for the `result` text field
|
|
43
|
+
// (model may have written review as text) and for error states.
|
|
44
|
+
if (dict.type === "result" || ("duration_ms" in dict && "session_id" in dict)) {
|
|
45
|
+
if (dict.is_error === true || (Array.isArray(dict.errors) && (dict.errors as unknown[]).length > 0)) {
|
|
46
|
+
logWarn("cli_parser", `CLI returned error result: ${JSON.stringify(dict.errors ?? "is_error=true")}`);
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
if (typeof dict.result === "string" && dict.result) {
|
|
50
|
+
logDebug("cli_parser", "Found result text in session envelope, attempting JSON extraction");
|
|
51
|
+
const extracted = parseJsonMaybe(dict.result as string, requireFields);
|
|
52
|
+
if (extracted) return extracted;
|
|
53
|
+
logWarn("cli_parser", "Session envelope result text contained no extractable JSON");
|
|
54
|
+
}
|
|
55
|
+
logDebug("cli_parser", "Session result envelope with no structured_output or extractable result");
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Strategy 2: Assistant message with StructuredOutput tool use
|
|
60
|
+
if (dict.type === "assistant") {
|
|
61
|
+
const message = dict.message as Record<string, unknown> | undefined;
|
|
62
|
+
const content = message?.content;
|
|
63
|
+
if (Array.isArray(content)) {
|
|
64
|
+
for (const item of content) {
|
|
65
|
+
if (
|
|
66
|
+
item !== null &&
|
|
67
|
+
typeof item === "object" &&
|
|
68
|
+
(item as Record<string, unknown>).name === "StructuredOutput"
|
|
69
|
+
) {
|
|
70
|
+
logDebug(
|
|
71
|
+
"cli_parser",
|
|
72
|
+
"Found StructuredOutput in assistant message content",
|
|
73
|
+
);
|
|
74
|
+
return (item as Record<string, unknown>).input as Record<
|
|
75
|
+
string,
|
|
76
|
+
unknown
|
|
77
|
+
>;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
logDebug(
|
|
83
|
+
"cli_parser",
|
|
84
|
+
"Assistant message found but no StructuredOutput tool use in content",
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
} else if (Array.isArray(result)) {
|
|
88
|
+
// Strategy 3: List of events with assistant messages
|
|
89
|
+
logDebug(
|
|
90
|
+
"cli_parser",
|
|
91
|
+
`Received list of ${(result as unknown[]).length} events, searching for assistant message`,
|
|
92
|
+
);
|
|
93
|
+
for (let i = 0; i < (result as unknown[]).length; i++) {
|
|
94
|
+
const event = (result as unknown[])[i];
|
|
95
|
+
if (event === null || typeof event !== "object") continue;
|
|
96
|
+
|
|
97
|
+
const dict = event as Record<string, unknown>;
|
|
98
|
+
if (dict.type === "assistant") {
|
|
99
|
+
const message = dict.message as Record<string, unknown> | undefined;
|
|
100
|
+
const content = message?.content;
|
|
101
|
+
if (Array.isArray(content)) {
|
|
102
|
+
for (const item of content) {
|
|
103
|
+
if (
|
|
104
|
+
item !== null &&
|
|
105
|
+
typeof item === "object" &&
|
|
106
|
+
(item as Record<string, unknown>).name === "StructuredOutput"
|
|
107
|
+
) {
|
|
108
|
+
logDebug(
|
|
109
|
+
"cli_parser",
|
|
110
|
+
`Found StructuredOutput in event[${i}] assistant message`,
|
|
111
|
+
);
|
|
112
|
+
return (item as Record<string, unknown>).input as Record<
|
|
113
|
+
string,
|
|
114
|
+
unknown
|
|
115
|
+
>;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
logDebug(
|
|
123
|
+
"cli_parser",
|
|
124
|
+
"No StructuredOutput found in any assistant message in event list",
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
} catch (error: unknown) {
|
|
128
|
+
if (error instanceof SyntaxError) {
|
|
129
|
+
logWarn("cli_parser", `JSON decode error: ${error.message}`);
|
|
130
|
+
} else {
|
|
131
|
+
logError(
|
|
132
|
+
"cli_parser",
|
|
133
|
+
`Unexpected error during structured parsing: ${error}`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Strategy 4: Heuristic {…} extraction fallback
|
|
139
|
+
logDebug(
|
|
140
|
+
"cli_parser",
|
|
141
|
+
"No structured output found, falling back to heuristic JSON extraction",
|
|
142
|
+
);
|
|
143
|
+
return parseJsonMaybe(raw, requireFields);
|
|
144
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration loading for cc-native plan review.
|
|
3
|
+
* See cc-native-plan-review-spec.md §4.4
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as fs from "node:fs";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
|
|
9
|
+
import type { DisplaySettings, PlanReviewConfig } from "./types.js";
|
|
10
|
+
import { DEFAULT_DISPLAY } from "./types.js";
|
|
11
|
+
import { logWarn } from "../../_shared/lib-ts/base/logger.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Load full CC-Native config from _cc-native/plan-review.config.json.
|
|
15
|
+
*/
|
|
16
|
+
export function loadConfig(projectDir: string): PlanReviewConfig {
|
|
17
|
+
const settingsPath = path.join(
|
|
18
|
+
projectDir,
|
|
19
|
+
"_cc-native",
|
|
20
|
+
"plan-review.config.json",
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
if (!fs.existsSync(settingsPath)) {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const raw = fs.readFileSync(settingsPath, "utf8");
|
|
29
|
+
return JSON.parse(raw) as PlanReviewConfig;
|
|
30
|
+
} catch (error: unknown) {
|
|
31
|
+
logWarn("cc-native", `Failed to load config: ${error}`);
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Get display settings, checking section-specific first, then root.
|
|
38
|
+
*/
|
|
39
|
+
export function getDisplaySettings(
|
|
40
|
+
config: PlanReviewConfig,
|
|
41
|
+
section: string,
|
|
42
|
+
): DisplaySettings {
|
|
43
|
+
const sectionConfig = config[section];
|
|
44
|
+
const sectionDisplay =
|
|
45
|
+
sectionConfig && typeof sectionConfig === "object"
|
|
46
|
+
? ((sectionConfig as Record<string, unknown>).display as
|
|
47
|
+
| Partial<DisplaySettings>
|
|
48
|
+
| undefined) ?? {}
|
|
49
|
+
: {};
|
|
50
|
+
const rootDisplay = config.display ?? {};
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
...DEFAULT_DISPLAY,
|
|
54
|
+
...rootDisplay,
|
|
55
|
+
...sectionDisplay,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security and configuration constants for cc-native plan review.
|
|
3
|
+
* See cc-native-plan-review-spec.md §4.15
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as os from "node:os";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Feature Flags
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
/** Enable atomic writes for plan state files (env: CC_NATIVE_ROBUST_WRITES) */
|
|
14
|
+
export const ENABLE_ROBUST_PLAN_WRITES =
|
|
15
|
+
(process.env.CC_NATIVE_ROBUST_WRITES ?? "true").toLowerCase() === "true";
|
|
16
|
+
|
|
17
|
+
/** Enable plan review notifications (env: CC_NATIVE_NOTIFICATIONS) */
|
|
18
|
+
export const ENABLE_PLAN_NOTIFICATIONS =
|
|
19
|
+
(process.env.CC_NATIVE_NOTIFICATIONS ?? "false").toLowerCase() === "true";
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// Security Constants
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
/** Default plans directory */
|
|
26
|
+
export const PLANS_DIR = path.join(os.homedir(), ".claude", "plans");
|
|
27
|
+
|
|
28
|
+
/** Maximum plan path length */
|
|
29
|
+
export const MAX_PLAN_PATH_LENGTH = 4096;
|
|
30
|
+
|
|
31
|
+
/** Maximum error file size (10KB) */
|
|
32
|
+
export const MAX_ERROR_FILE_SIZE = 10 * 1024;
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Performance Constants
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
/** Fast-fail: 2 attempts max */
|
|
39
|
+
export const MAX_RETRY_ATTEMPTS = 2;
|
|
40
|
+
|
|
41
|
+
/** Backoff schedule: 0.5s, 1s */
|
|
42
|
+
export const RETRY_BACKOFF_MS = [500, 1000];
|
|
43
|
+
|
|
44
|
+
/** 3 seconds total, well under 5s hook timeout */
|
|
45
|
+
export const MAX_TOTAL_RETRY_TIME_MS = 3000;
|
|
46
|
+
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// Path Validation
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Validate and sanitize plan path to prevent traversal attacks.
|
|
53
|
+
* Throws ValueError if path is invalid, too long, or outside allowed directory.
|
|
54
|
+
*/
|
|
55
|
+
export function validatePlanPath(planPath: string): string {
|
|
56
|
+
// Input validation
|
|
57
|
+
if (!planPath || planPath.length > MAX_PLAN_PATH_LENGTH) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`Invalid plan path length: ${planPath ? planPath.length : 0}`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (planPath.includes("\u0000")) {
|
|
64
|
+
throw new Error("Null bytes not allowed in path");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Normalize and resolve to absolute canonical path
|
|
68
|
+
let resolved: string;
|
|
69
|
+
try {
|
|
70
|
+
resolved = path.resolve(planPath);
|
|
71
|
+
} catch (error: unknown) {
|
|
72
|
+
throw new Error(`Path resolution failed: ${error}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Verify path is within allowed directory (case-insensitive on Windows)
|
|
76
|
+
const resolvedLower = resolved.toLowerCase().replaceAll('\\', "/");
|
|
77
|
+
const plansDirLower = PLANS_DIR.toLowerCase().replaceAll('\\', "/");
|
|
78
|
+
if (!resolvedLower.startsWith(plansDirLower)) {
|
|
79
|
+
throw new Error(`Path outside allowed directory: ${PLANS_DIR}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return resolved;
|
|
83
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Corroboration-based verdict computation for plan review.
|
|
3
|
+
*
|
|
4
|
+
* Replaces the old per-verdict aggregation with proportional thresholding:
|
|
5
|
+
* high-severity issues in a dimension only block when the total count
|
|
6
|
+
* exceeds 2× the number of distinct agents contributing to that dimension.
|
|
7
|
+
*
|
|
8
|
+
* **Why proportional thresholding:**
|
|
9
|
+
* The agent pool has dimensional imbalance (e.g., 10 completeness agents vs
|
|
10
|
+
* 1 maintainability agent). A fixed "2+ agents agree = block" would mean
|
|
11
|
+
* any 2 completeness agents always block. Proportional scaling (issues > 2×agents)
|
|
12
|
+
* sets a fair bar regardless of how many agents focus on each dimension.
|
|
13
|
+
*
|
|
14
|
+
* **Convergence problem this solves:**
|
|
15
|
+
* Agents with opposing philosophies (simplicity-guardian vs completeness-gaps)
|
|
16
|
+
* produce contradictory high-severity issues. Because the old system treated
|
|
17
|
+
* every agent's finding as independently authoritative, plans oscillated —
|
|
18
|
+
* addressing one agent's feedback triggered the opposing agent.
|
|
19
|
+
*
|
|
20
|
+
* **Revert path:** Change one line in cc-native-plan-review.ts back to
|
|
21
|
+
* `computeReviewDecision(allVerdicts)`. Old function kept in verdict.ts.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import type {
|
|
25
|
+
ReviewerResult,
|
|
26
|
+
ReviewIssue,
|
|
27
|
+
IssueDimension,
|
|
28
|
+
CorroborationResult,
|
|
29
|
+
CorroboratedGroup,
|
|
30
|
+
SoloFinding,
|
|
31
|
+
} from "./types.js";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Compute a corroboration-based review decision from all reviewer results.
|
|
35
|
+
*
|
|
36
|
+
* Algorithm:
|
|
37
|
+
* 1. Collect all high-severity issues with a `dimension` field
|
|
38
|
+
* 2. Group by dimension, tracking distinct agent names per group
|
|
39
|
+
* 3. For each dimension: block if `issues.length > 2 × agentCount`
|
|
40
|
+
* 4. Issues without `dimension` are unclassified (never block)
|
|
41
|
+
* 5. Non-high issues are ignored (informational only)
|
|
42
|
+
*
|
|
43
|
+
* @param allResults - Map of reviewer name → ReviewerResult (CLI + agent)
|
|
44
|
+
* @returns CorroborationResult with blocking groups, solo findings, and verdict
|
|
45
|
+
*/
|
|
46
|
+
export function computeCorroboratedDecision(
|
|
47
|
+
allResults: Record<string, ReviewerResult>,
|
|
48
|
+
): CorroborationResult {
|
|
49
|
+
// Accumulator: dimension → { issues, agentNames }
|
|
50
|
+
const dimMap = new Map<
|
|
51
|
+
IssueDimension,
|
|
52
|
+
{
|
|
53
|
+
issues: Array<{ agent: string; issue: ReviewIssue }>;
|
|
54
|
+
agentNames: Set<string>;
|
|
55
|
+
}
|
|
56
|
+
>();
|
|
57
|
+
|
|
58
|
+
const unclassified: Array<{ agent: string; issue: ReviewIssue }> = [];
|
|
59
|
+
|
|
60
|
+
for (const [agentName, result] of Object.entries(allResults)) {
|
|
61
|
+
if (!result.data) continue;
|
|
62
|
+
const issues = result.data.issues as ReviewIssue[] | undefined;
|
|
63
|
+
if (!Array.isArray(issues)) continue;
|
|
64
|
+
|
|
65
|
+
for (const issue of issues) {
|
|
66
|
+
// Only high-severity issues participate in corroboration
|
|
67
|
+
if (issue.severity !== "high") continue;
|
|
68
|
+
|
|
69
|
+
// Issues without dimension are unclassified — cannot block
|
|
70
|
+
if (!issue.dimension) {
|
|
71
|
+
unclassified.push({ agent: agentName, issue });
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let group = dimMap.get(issue.dimension);
|
|
76
|
+
if (!group) {
|
|
77
|
+
group = { issues: [], agentNames: new Set() };
|
|
78
|
+
dimMap.set(issue.dimension, group);
|
|
79
|
+
}
|
|
80
|
+
group.issues.push({ agent: agentName, issue });
|
|
81
|
+
group.agentNames.add(agentName);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const blocking: CorroboratedGroup[] = [];
|
|
86
|
+
const solo: SoloFinding[] = [];
|
|
87
|
+
|
|
88
|
+
for (const [dimension, group] of dimMap) {
|
|
89
|
+
const agentCount = group.agentNames.size;
|
|
90
|
+
const threshold = 2 * agentCount;
|
|
91
|
+
|
|
92
|
+
if (group.issues.length > threshold) {
|
|
93
|
+
blocking.push({
|
|
94
|
+
dimension,
|
|
95
|
+
issues: group.issues,
|
|
96
|
+
agentCount,
|
|
97
|
+
threshold,
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
solo.push({
|
|
101
|
+
dimension,
|
|
102
|
+
issues: group.issues,
|
|
103
|
+
agentCount,
|
|
104
|
+
threshold,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
blocking,
|
|
111
|
+
solo,
|
|
112
|
+
unclassified,
|
|
113
|
+
verdict: blocking.length > 0 ? "fail" : "pass",
|
|
114
|
+
};
|
|
115
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-context debug logging for cc-native hooks.
|
|
3
|
+
* Thin delegation layer over the unified logger.
|
|
4
|
+
* See cc-native-plan-review-spec.md §4.13
|
|
5
|
+
*
|
|
6
|
+
* Can be disabled via CCNATIVE_DEBUG_DISABLE=1 environment variable.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as fs from "node:fs";
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
|
|
12
|
+
import { hookLog } from "../../_shared/lib-ts/base/logger.js";
|
|
13
|
+
|
|
14
|
+
/** Feature flag — set CCNATIVE_DEBUG_DISABLE=1 to turn off */
|
|
15
|
+
const DEBUG_ENABLED = !["1", "true", "yes"].includes(
|
|
16
|
+
(process.env.CCNATIVE_DEBUG_DISABLE ?? "").toLowerCase(),
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Get or create debug directory within context folder.
|
|
21
|
+
*/
|
|
22
|
+
export function getDebugDir(contextPath: string): string {
|
|
23
|
+
const debugDir = path.join(contextPath, "debug");
|
|
24
|
+
fs.mkdirSync(debugDir, { recursive: true });
|
|
25
|
+
return debugDir;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Write a debug log entry. Delegates to unified logger.
|
|
30
|
+
*/
|
|
31
|
+
export function debugLog(
|
|
32
|
+
contextPath: string,
|
|
33
|
+
sessionName: string,
|
|
34
|
+
component: string,
|
|
35
|
+
message: string,
|
|
36
|
+
data?: unknown,
|
|
37
|
+
): void {
|
|
38
|
+
if (!DEBUG_ENABLED) return;
|
|
39
|
+
|
|
40
|
+
hookLog("debug", sessionName, message, {
|
|
41
|
+
component,
|
|
42
|
+
data,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Log raw output (stdout, stderr, etc). Delegates to unified logger.
|
|
48
|
+
*/
|
|
49
|
+
export function debugRaw(
|
|
50
|
+
contextPath: string,
|
|
51
|
+
sessionName: string,
|
|
52
|
+
component: string,
|
|
53
|
+
label: string,
|
|
54
|
+
raw: string,
|
|
55
|
+
maxLen = 10_000,
|
|
56
|
+
): void {
|
|
57
|
+
if (!DEBUG_ENABLED) return;
|
|
58
|
+
|
|
59
|
+
const truncated = raw.length > maxLen ? raw.slice(0, maxLen) : raw;
|
|
60
|
+
const suffix =
|
|
61
|
+
raw.length > maxLen ? ` [TRUNCATED from ${raw.length} chars]` : "";
|
|
62
|
+
|
|
63
|
+
hookLog("debug", sessionName, `${label}${suffix}: ${truncated}`, {
|
|
64
|
+
component,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Remove debug folder during context archive.
|
|
70
|
+
*/
|
|
71
|
+
export function cleanupDebugFolder(contextPath: string): void {
|
|
72
|
+
try {
|
|
73
|
+
const debugDir = path.join(contextPath, "debug");
|
|
74
|
+
if (fs.existsSync(debugDir)) {
|
|
75
|
+
fs.rmSync(debugDir, { recursive: true, force: true });
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
// Best effort cleanup
|
|
79
|
+
}
|
|
80
|
+
}
|