@wooojin/forgen 0.3.1 → 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 (125) hide show
  1. package/.claude-plugin/plugin.json +7 -2
  2. package/CHANGELOG.md +164 -0
  3. package/README.ja.md +90 -7
  4. package/README.ko.md +44 -1
  5. package/README.md +128 -9
  6. package/README.zh.md +90 -7
  7. package/dist/cli.js +140 -8
  8. package/dist/core/auto-compound-runner.js +16 -5
  9. package/dist/core/dashboard.js +11 -4
  10. package/dist/core/doctor.d.ts +6 -1
  11. package/dist/core/doctor.js +85 -11
  12. package/dist/core/global-config.d.ts +2 -2
  13. package/dist/core/global-config.js +6 -14
  14. package/dist/core/harness.d.ts +3 -5
  15. package/dist/core/harness.js +34 -338
  16. package/dist/core/inspect-cli.js +65 -5
  17. package/dist/core/installer.d.ts +10 -0
  18. package/dist/core/installer.js +185 -0
  19. package/dist/core/paths.d.ts +0 -34
  20. package/dist/core/paths.js +0 -35
  21. package/dist/core/settings-injector.d.ts +13 -0
  22. package/dist/core/settings-injector.js +167 -0
  23. package/dist/core/settings-lock.d.ts +35 -2
  24. package/dist/core/settings-lock.js +65 -7
  25. package/dist/core/spawn.js +100 -39
  26. package/dist/core/state-gc.d.ts +49 -0
  27. package/dist/core/state-gc.js +163 -0
  28. package/dist/core/stats-cli.d.ts +15 -0
  29. package/dist/core/stats-cli.js +143 -0
  30. package/dist/core/uninstall.d.ts +1 -0
  31. package/dist/core/uninstall.js +36 -5
  32. package/dist/core/v1-bootstrap.js +11 -3
  33. package/dist/engine/classify-enforce-cli.d.ts +8 -0
  34. package/dist/engine/classify-enforce-cli.js +61 -0
  35. package/dist/engine/compound-cli.d.ts +27 -2
  36. package/dist/engine/compound-cli.js +69 -16
  37. package/dist/engine/compound-export.d.ts +15 -0
  38. package/dist/engine/compound-export.js +32 -5
  39. package/dist/engine/compound-loop.js +3 -2
  40. package/dist/engine/enforce-classifier.d.ts +31 -0
  41. package/dist/engine/enforce-classifier.js +123 -0
  42. package/dist/engine/learn-cli.js +52 -0
  43. package/dist/engine/lifecycle/bypass-detector.d.ts +34 -0
  44. package/dist/engine/lifecycle/bypass-detector.js +82 -0
  45. package/dist/engine/lifecycle/lifecycle-cli.d.ts +7 -0
  46. package/dist/engine/lifecycle/lifecycle-cli.js +102 -0
  47. package/dist/engine/lifecycle/meta-cli.d.ts +4 -0
  48. package/dist/engine/lifecycle/meta-cli.js +7 -0
  49. package/dist/engine/lifecycle/meta-reclassifier.d.ts +78 -0
  50. package/dist/engine/lifecycle/meta-reclassifier.js +351 -0
  51. package/dist/engine/lifecycle/orchestrator.d.ts +32 -0
  52. package/dist/engine/lifecycle/orchestrator.js +131 -0
  53. package/dist/engine/lifecycle/signals.d.ts +30 -0
  54. package/dist/engine/lifecycle/signals.js +142 -0
  55. package/dist/engine/lifecycle/trigger-t1-correction.d.ts +23 -0
  56. package/dist/engine/lifecycle/trigger-t1-correction.js +78 -0
  57. package/dist/engine/lifecycle/trigger-t2-violation.d.ts +18 -0
  58. package/dist/engine/lifecycle/trigger-t2-violation.js +42 -0
  59. package/dist/engine/lifecycle/trigger-t3-bypass.d.ts +17 -0
  60. package/dist/engine/lifecycle/trigger-t3-bypass.js +39 -0
  61. package/dist/engine/lifecycle/trigger-t4-decay.d.ts +18 -0
  62. package/dist/engine/lifecycle/trigger-t4-decay.js +40 -0
  63. package/dist/engine/lifecycle/trigger-t5-conflict.d.ts +16 -0
  64. package/dist/engine/lifecycle/trigger-t5-conflict.js +78 -0
  65. package/dist/engine/lifecycle/types.d.ts +52 -0
  66. package/dist/engine/lifecycle/types.js +7 -0
  67. package/dist/engine/match-eval-log.js +45 -0
  68. package/dist/engine/rule-toggle-cli.d.ts +13 -0
  69. package/dist/engine/rule-toggle-cli.js +76 -0
  70. package/dist/engine/solution-format.d.ts +0 -2
  71. package/dist/engine/solution-format.js +0 -4
  72. package/dist/engine/solution-matcher.d.ts +8 -0
  73. package/dist/engine/solution-matcher.js +7 -4
  74. package/dist/engine/solution-outcomes.d.ts +4 -0
  75. package/dist/engine/solution-outcomes.js +174 -97
  76. package/dist/engine/solution-writer.d.ts +8 -5
  77. package/dist/engine/solution-writer.js +43 -19
  78. package/dist/fgx.js +9 -2
  79. package/dist/forge/cli.js +7 -7
  80. package/dist/forge/evidence-processor.js +10 -2
  81. package/dist/hooks/context-guard.js +86 -1
  82. package/dist/hooks/hook-config.d.ts +9 -1
  83. package/dist/hooks/hook-config.js +25 -3
  84. package/dist/hooks/internal/run-lifecycle-check.d.ts +2 -0
  85. package/dist/hooks/internal/run-lifecycle-check.js +32 -0
  86. package/dist/hooks/notepad-injector.js +6 -3
  87. package/dist/hooks/permission-handler.d.ts +10 -2
  88. package/dist/hooks/permission-handler.js +31 -12
  89. package/dist/hooks/post-tool-use.js +62 -0
  90. package/dist/hooks/pre-tool-use.js +67 -5
  91. package/dist/hooks/secret-filter.d.ts +10 -0
  92. package/dist/hooks/secret-filter.js +26 -0
  93. package/dist/hooks/session-recovery.js +15 -7
  94. package/dist/hooks/shared/atomic-write.d.ts +8 -1
  95. package/dist/hooks/shared/atomic-write.js +17 -3
  96. package/dist/hooks/shared/hook-response.d.ts +11 -2
  97. package/dist/hooks/shared/hook-response.js +20 -7
  98. package/dist/hooks/shared/hook-timing.js +10 -1
  99. package/dist/hooks/shared/safe-regex.d.ts +25 -0
  100. package/dist/hooks/shared/safe-regex.js +50 -0
  101. package/dist/hooks/shared/stop-triggers.d.ts +19 -0
  102. package/dist/hooks/shared/stop-triggers.js +19 -0
  103. package/dist/hooks/solution-injector.d.ts +21 -0
  104. package/dist/hooks/solution-injector.js +60 -1
  105. package/dist/hooks/stop-guard.d.ts +84 -0
  106. package/dist/hooks/stop-guard.js +482 -0
  107. package/dist/mcp/solution-reader.d.ts +2 -0
  108. package/dist/mcp/solution-reader.js +28 -1
  109. package/dist/mcp/tools.js +24 -4
  110. package/dist/preset/preset-manager.js +12 -2
  111. package/dist/store/evidence-store.d.ts +15 -0
  112. package/dist/store/evidence-store.js +55 -6
  113. package/dist/store/profile-store.d.ts +9 -0
  114. package/dist/store/profile-store.js +25 -4
  115. package/dist/store/rule-lifecycle.d.ts +23 -0
  116. package/dist/store/rule-lifecycle.js +63 -0
  117. package/dist/store/rule-store.d.ts +21 -0
  118. package/dist/store/rule-store.js +133 -13
  119. package/dist/store/types.d.ts +83 -0
  120. package/dist/store/types.js +7 -1
  121. package/hooks/hook-registry.json +1 -0
  122. package/hooks/hooks.json +6 -1
  123. package/package.json +10 -2
  124. package/plugin.json +7 -2
  125. package/scripts/postinstall.js +52 -5
@@ -136,13 +136,27 @@ export function atomicWriteText(filePath, content, options) {
136
136
  throw e;
137
137
  }
138
138
  }
139
- /** JSON 파일을 안전하게 읽기 (파싱 실패 시 fallback 반환) */
139
+ /**
140
+ * JSON 파일을 안전하게 읽기 (파싱 실패 시 fallback 반환).
141
+ *
142
+ * R4-B3 (2026-04-22): UTF-8 BOM () prefix 제거 — Windows 메모장 등으로 저장된
143
+ * rule/settings JSON 이 BOM 으로 시작해 JSON.parse 가 silent 실패하던 문제.
144
+ * R4-SKIP: FORGEN_DEBUG_SIGNALS=1 일 때 파싱 실패를 stderr 로 노출 — silent
145
+ * 누락을 운영자가 추적 가능하도록.
146
+ */
140
147
  export function safeReadJSON(filePath, fallback) {
141
148
  try {
142
149
  if (fs.existsSync(filePath)) {
143
- return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
150
+ let raw = fs.readFileSync(filePath, 'utf-8');
151
+ if (raw.charCodeAt(0) === 0xFEFF)
152
+ raw = raw.slice(1); // strip BOM
153
+ return JSON.parse(raw);
154
+ }
155
+ }
156
+ catch (e) {
157
+ if (process.env.FORGEN_DEBUG_SIGNALS === '1') {
158
+ process.stderr.write(`[forgen:safeReadJSON] ${filePath} parse failed: ${e.message}\n`);
144
159
  }
145
160
  }
146
- catch { /* JSON parse failure — return fallback */ }
147
161
  return fallback;
148
162
  }
@@ -29,8 +29,17 @@ export declare function approveWithWarning(warning: string): string;
29
29
  export declare function deny(reason: string): string;
30
30
  /** 사용자 확인 요청 (PreToolUse 전용) */
31
31
  export declare function ask(reason: string): string;
32
- /** fail-open: 에러 시 안전하게 통과 */
33
- export declare function failOpen(): string;
32
+ /**
33
+ * Stop hook only — block the agent from stopping and feed a self-check
34
+ * question back to Claude so the current session resumes with new guidance.
35
+ *
36
+ * `reason` becomes the next-turn content (Claude reads this verbatim), while
37
+ * `systemMessage` is auxiliary context rendered alongside. Put the whole
38
+ * self-check question in `reason`; keep `systemMessage` to a short rule tag.
39
+ *
40
+ * Source: Stop hook spec — `decision: "block"` "prevents stopping and continues the agent's work".
41
+ */
42
+ export declare function blockStop(reason: string, systemMessage?: string): string;
34
43
  /**
35
44
  * fail-open with error tracking: 에러 시 안전하게 통과하되, 실패 정보를 기록.
36
45
  * forgen doctor의 Hook Health 섹션에서 실패 이력을 표시할 수 있도록 JSONL 로그에 기록.
@@ -14,8 +14,8 @@
14
14
  * 모델에 컨텍스트를 주입하려면 반드시 additionalContext를 사용해야 함.
15
15
  */
16
16
  import * as fs from 'node:fs';
17
- import * as os from 'node:os';
18
17
  import * as path from 'node:path';
18
+ import { STATE_DIR } from '../../core/paths.js';
19
19
  /** 통과 응답 (컨텍스트 없음, 모든 이벤트 공통) */
20
20
  export function approve() {
21
21
  return JSON.stringify({ continue: true });
@@ -59,9 +59,23 @@ export function ask(reason) {
59
59
  },
60
60
  });
61
61
  }
62
- /** fail-open: 에러 시 안전하게 통과 */
63
- export function failOpen() {
64
- return JSON.stringify({ continue: true });
62
+ /**
63
+ * Stop hook only — block the agent from stopping and feed a self-check
64
+ * question back to Claude so the current session resumes with new guidance.
65
+ *
66
+ * `reason` becomes the next-turn content (Claude reads this verbatim), while
67
+ * `systemMessage` is auxiliary context rendered alongside. Put the whole
68
+ * self-check question in `reason`; keep `systemMessage` to a short rule tag.
69
+ *
70
+ * Source: Stop hook spec — `decision: "block"` "prevents stopping and continues the agent's work".
71
+ */
72
+ export function blockStop(reason, systemMessage) {
73
+ return JSON.stringify({
74
+ continue: true,
75
+ decision: 'block',
76
+ reason,
77
+ ...(systemMessage ? { systemMessage } : {}),
78
+ });
65
79
  }
66
80
  /**
67
81
  * fail-open with error tracking: 에러 시 안전하게 통과하되, 실패 정보를 기록.
@@ -71,9 +85,8 @@ export function failOpen() {
71
85
  */
72
86
  export function failOpenWithTracking(hookName) {
73
87
  try {
74
- const stateDir = path.join(os.homedir(), '.forgen', 'state');
75
- fs.mkdirSync(stateDir, { recursive: true });
76
- const logPath = path.join(stateDir, 'hook-errors.jsonl');
88
+ fs.mkdirSync(STATE_DIR, { recursive: true });
89
+ const logPath = path.join(STATE_DIR, 'hook-errors.jsonl');
77
90
  const entry = JSON.stringify({ hook: hookName, at: Date.now() });
78
91
  fs.appendFileSync(logPath, entry + '\n');
79
92
  }
@@ -9,13 +9,22 @@ import * as path from 'node:path';
9
9
  import { STATE_DIR } from '../../core/paths.js';
10
10
  const TIMING_LOG = path.join(STATE_DIR, 'hook-timing.jsonl');
11
11
  const MAX_LINES = 500;
12
+ // P0-2 fix (2026-04-20): rotate를 size gate로 보호. 이전에는 매 hook 완료마다
13
+ // full-file read + length split + write까지 실행해 steady-state(500줄 근처)에서
14
+ // 매 tool call당 ~40KB의 불필요 I/O가 발생했다. statSync 한 번으로 크기만 보고
15
+ // threshold 이하면 read/write 둘 다 skip한다. threshold는 ~80바이트/엔트리 기준
16
+ // MAX_LINES × 1.5 여유를 둠.
17
+ const ROTATE_SIZE_BYTES = MAX_LINES * 80 * 2; // ~80KB
12
18
  export function recordHookTiming(hookName, durationMs, event) {
13
19
  try {
14
20
  fs.mkdirSync(STATE_DIR, { recursive: true });
15
21
  const entry = JSON.stringify({ hook: hookName, ms: durationMs, event, at: Date.now() });
16
22
  fs.appendFileSync(TIMING_LOG, entry + '\n');
17
- // Rotate if too large
23
+ // Rotate if too large — size-gated (statSync only, skip read/write 대부분의 호출)
18
24
  try {
25
+ const size = fs.statSync(TIMING_LOG).size;
26
+ if (size < ROTATE_SIZE_BYTES)
27
+ return;
19
28
  const content = fs.readFileSync(TIMING_LOG, 'utf-8');
20
29
  const lines = content.trim().split('\n');
21
30
  if (lines.length > MAX_LINES) {
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Safe regex compiler — ReDoS 방지용 경량 가드.
3
+ *
4
+ * rule JSON 의 verifier.params.pattern 등 user-controlled regex 를 hook 런타임에
5
+ * 그대로 new RegExp() 하면 catastrophic backtracking 으로 hook hang 위험이 있다.
6
+ * re2 같은 linear-time 엔진 의존은 native binding 을 추가시키므로, 여기서는
7
+ * **패턴 복잡도 제한** + **입력 크기 제한** 으로 1차 방어.
8
+ *
9
+ * 정책:
10
+ * - 패턴 길이 ≤ 500자.
11
+ * - 중첩 quantifier (`(...)+)+` / `(...)*)*` / `(.+)+`) 같은 catastrophic 신호 거부.
12
+ * - backreference `\1..\9` 금지.
13
+ * - compile 실패 또는 거부 시 null 반환 → 호출자가 skip.
14
+ */
15
+ export interface SafeRegexResult {
16
+ regex: RegExp | null;
17
+ reason: string | null;
18
+ }
19
+ /**
20
+ * 패턴을 안전하게 컴파일. 거부되거나 실패 시 { regex: null, reason } 반환.
21
+ * 호출자는 reason 을 log.debug 로 기록하고 skip 하는 것이 권장 사용법.
22
+ */
23
+ export declare function compileSafeRegex(pattern: string, flags?: string): SafeRegexResult;
24
+ /** 입력을 MAX_INPUT_LEN 으로 자른 뒤 regex.test() 수행. 입력 DoS 방어. */
25
+ export declare function safeRegexTest(regex: RegExp, input: string): boolean;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Safe regex compiler — ReDoS 방지용 경량 가드.
3
+ *
4
+ * rule JSON 의 verifier.params.pattern 등 user-controlled regex 를 hook 런타임에
5
+ * 그대로 new RegExp() 하면 catastrophic backtracking 으로 hook hang 위험이 있다.
6
+ * re2 같은 linear-time 엔진 의존은 native binding 을 추가시키므로, 여기서는
7
+ * **패턴 복잡도 제한** + **입력 크기 제한** 으로 1차 방어.
8
+ *
9
+ * 정책:
10
+ * - 패턴 길이 ≤ 500자.
11
+ * - 중첩 quantifier (`(...)+)+` / `(...)*)*` / `(.+)+`) 같은 catastrophic 신호 거부.
12
+ * - backreference `\1..\9` 금지.
13
+ * - compile 실패 또는 거부 시 null 반환 → 호출자가 skip.
14
+ */
15
+ const MAX_PATTERN_LEN = 500;
16
+ const MAX_INPUT_LEN = 65536;
17
+ // Catastrophic backtracking 의 흔한 형태 — 중첩된 quantifier 체인.
18
+ const NESTED_QUANTIFIER = /\([^)]*[+*][^)]*\)[+*]/;
19
+ // Alternation with shared prefix can also be catastrophic — heuristic only.
20
+ const OVERLAPPING_ALT = /\(([^|)]+)\|\1[^)]*\)[+*]/;
21
+ const BACKREFERENCE = /\\[1-9]/;
22
+ /**
23
+ * 패턴을 안전하게 컴파일. 거부되거나 실패 시 { regex: null, reason } 반환.
24
+ * 호출자는 reason 을 log.debug 로 기록하고 skip 하는 것이 권장 사용법.
25
+ */
26
+ export function compileSafeRegex(pattern, flags = '') {
27
+ if (typeof pattern !== 'string')
28
+ return { regex: null, reason: 'non-string pattern' };
29
+ if (pattern.length === 0)
30
+ return { regex: null, reason: 'empty pattern' };
31
+ if (pattern.length > MAX_PATTERN_LEN)
32
+ return { regex: null, reason: `pattern length ${pattern.length} > ${MAX_PATTERN_LEN}` };
33
+ if (NESTED_QUANTIFIER.test(pattern))
34
+ return { regex: null, reason: 'nested quantifier (catastrophic backtracking risk)' };
35
+ if (OVERLAPPING_ALT.test(pattern))
36
+ return { regex: null, reason: 'overlapping alternation with quantifier' };
37
+ if (BACKREFERENCE.test(pattern))
38
+ return { regex: null, reason: 'backreference in user regex (perf risk)' };
39
+ try {
40
+ return { regex: new RegExp(pattern, flags), reason: null };
41
+ }
42
+ catch (e) {
43
+ return { regex: null, reason: `compile error: ${String(e).slice(0, 80)}` };
44
+ }
45
+ }
46
+ /** 입력을 MAX_INPUT_LEN 으로 자른 뒤 regex.test() 수행. 입력 DoS 방어. */
47
+ export function safeRegexTest(regex, input) {
48
+ const truncated = input.length > MAX_INPUT_LEN ? input.slice(0, MAX_INPUT_LEN) : input;
49
+ return regex.test(truncated);
50
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Shared Stop hook default trigger regexes.
3
+ *
4
+ * R6-F2 (2026-04-22): stop-guard 와 enforce-classifier 에 리터럴 중복되던 정규식을
5
+ * 단일 소스로 통합. 한쪽만 고치면 다른 쪽이 drift 하는 sibling-bug 패턴 차단.
6
+ *
7
+ * 설계 결정:
8
+ * - trigger 는 명시적 완료 선언 동사/어미만 — "완료" 단독 매칭 금지 (retraction 오매칭 방지).
9
+ * - exclude 는 retraction/negation/meta 언급 광범위 차단.
10
+ * - A1 spike 결과로 검증됨 (10/10 scenarios pass, FP 0%).
11
+ */
12
+ /** Stop hook 에서 rule trigger 가 명시되지 않을 때의 기본 완료 선언 매칭. */
13
+ export declare const DEFAULT_STOP_TRIGGER_RE = "(\uC644\uB8CC\uD588|\uC644\uC131\uB410|\uC644\uC131\uB418|\uC644\uC131\uD588|done\\.|ready\\.|shipped\\.|LGTM|finished\\.)";
14
+ /** Stop hook 기본 exclude — retraction/negation/meta 맥락 제외. */
15
+ export declare const DEFAULT_STOP_EXCLUDE_RE = "(\uCDE8\uC18C|\uCCA0\uD68C|\uC5C6\uC74C|\uC5C6\uC2B5\uB2C8\uB2E4|\uC54A\uC558|\uD558\uC9C0\\s*\uC54A|\uC544\uB2D9\uB2C8\uB2E4|not\\s*yet|no\\s*longer|retract|withdraw|\uC544\uC9C1\\s*(\uC548|\uC544))";
16
+ /** mock/stub/fake 감지 — R-B2 전용 pattern (자가검증 주장 차단). */
17
+ export declare const MOCK_TRIGGER_RE = "(mock|stub|fake)";
18
+ /** mock trigger 의 exclude — 테스트 맥락은 정상. */
19
+ export declare const MOCK_EXCLUDE_RE = "(\uD14C\uC2A4\uD2B8|test|vi\\.mock|jest\\.mock|spec\\.)";
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Shared Stop hook default trigger regexes.
3
+ *
4
+ * R6-F2 (2026-04-22): stop-guard 와 enforce-classifier 에 리터럴 중복되던 정규식을
5
+ * 단일 소스로 통합. 한쪽만 고치면 다른 쪽이 drift 하는 sibling-bug 패턴 차단.
6
+ *
7
+ * 설계 결정:
8
+ * - trigger 는 명시적 완료 선언 동사/어미만 — "완료" 단독 매칭 금지 (retraction 오매칭 방지).
9
+ * - exclude 는 retraction/negation/meta 언급 광범위 차단.
10
+ * - A1 spike 결과로 검증됨 (10/10 scenarios pass, FP 0%).
11
+ */
12
+ /** Stop hook 에서 rule trigger 가 명시되지 않을 때의 기본 완료 선언 매칭. */
13
+ export const DEFAULT_STOP_TRIGGER_RE = '(완료했|완성됐|완성되|완성했|done\\.|ready\\.|shipped\\.|LGTM|finished\\.)';
14
+ /** Stop hook 기본 exclude — retraction/negation/meta 맥락 제외. */
15
+ export const DEFAULT_STOP_EXCLUDE_RE = '(취소|철회|없음|없습니다|않았|하지\\s*않|아닙니다|not\\s*yet|no\\s*longer|retract|withdraw|아직\\s*(안|아))';
16
+ /** mock/stub/fake 감지 — R-B2 전용 pattern (자가검증 주장 차단). */
17
+ export const MOCK_TRIGGER_RE = '(mock|stub|fake)';
18
+ /** mock trigger 의 exclude — 테스트 맥락은 정상. */
19
+ export const MOCK_EXCLUDE_RE = '(테스트|test|vi\\.mock|jest\\.mock|spec\\.)';
@@ -7,6 +7,27 @@
7
7
  *
8
8
  * knowledge-comes-to-you 원칙: 필요한 지식은 찾아와야 한다
9
9
  */
10
+ /**
11
+ * Minimum relevance thresholds by fitness state (2026-04-21 gate sweep).
12
+ *
13
+ * Motivation: a flat 0.3 floor gave 100% precision but 60% recall on a
14
+ * synthetic 40-query workload — 10 legitimate matches that scored
15
+ * 0.25-0.30 were blocked alongside noise. A pure 0.25 floor pushed recall
16
+ * to 84% but stripped noise protection for unverified solutions.
17
+ *
18
+ * Champion-aware solution: trust graduates more. Solutions whose fitness
19
+ * classification is `champion` or `active` (accept/correct ratio has
20
+ * survived ≥5 injections under the v0.3.2 gates) earn a lower 0.25
21
+ * injection floor; everything else stays at 0.3. On the sweep this hit
22
+ * precision 95.5% / recall 84% / off-topic specificity 100% — best
23
+ * trade in the variant set.
24
+ *
25
+ * If fitness data is unavailable (fresh install, empty outcomes/),
26
+ * every solution falls into the default 0.3 bucket — identical to the
27
+ * pre-0.3.2 gate. No cold-start regression.
28
+ */
29
+ export declare const MIN_INJECT_RELEVANCE = 0.3;
30
+ export declare const MIN_INJECT_RELEVANCE_TRUSTED = 0.25;
10
31
  interface SessionCacheCommitResult {
11
32
  /**
12
33
  * commit 상태:
@@ -30,6 +30,27 @@ import { STATE_DIR } from '../core/paths.js';
30
30
  import { recordHookTiming } from './shared/hook-timing.js';
31
31
  import { appendPending, flushAccept } from '../engine/solution-outcomes.js';
32
32
  const MAX_SOLUTIONS_PER_SESSION = 10;
33
+ /**
34
+ * Minimum relevance thresholds by fitness state (2026-04-21 gate sweep).
35
+ *
36
+ * Motivation: a flat 0.3 floor gave 100% precision but 60% recall on a
37
+ * synthetic 40-query workload — 10 legitimate matches that scored
38
+ * 0.25-0.30 were blocked alongside noise. A pure 0.25 floor pushed recall
39
+ * to 84% but stripped noise protection for unverified solutions.
40
+ *
41
+ * Champion-aware solution: trust graduates more. Solutions whose fitness
42
+ * classification is `champion` or `active` (accept/correct ratio has
43
+ * survived ≥5 injections under the v0.3.2 gates) earn a lower 0.25
44
+ * injection floor; everything else stays at 0.3. On the sweep this hit
45
+ * precision 95.5% / recall 84% / off-topic specificity 100% — best
46
+ * trade in the variant set.
47
+ *
48
+ * If fitness data is unavailable (fresh install, empty outcomes/),
49
+ * every solution falls into the default 0.3 bucket — identical to the
50
+ * pre-0.3.2 gate. No cold-start regression.
51
+ */
52
+ export const MIN_INJECT_RELEVANCE = 0.3;
53
+ export const MIN_INJECT_RELEVANCE_TRUSTED = 0.25;
33
54
  /** 세션별 이미 주입된 솔루션 추적 (중복 방지) */
34
55
  function getSessionCachePath(sessionId) {
35
56
  return path.join(STATE_DIR, `solution-cache-${sanitizeId(sessionId)}.json`);
@@ -299,12 +320,50 @@ async function main() {
299
320
  console.log(approve());
300
321
  return;
301
322
  }
302
- // 어댑티브 프롬프트당 솔루션 수 제한, experiment는 1개 제한
323
+ // 어댑티브 프롬프트당 솔루션 수 제한, experiment는 1개 제한.
324
+ // 2026-04-21: MIN_INJECT_RELEVANCE 게이트 추가. 과거 0.15~0.21짜리 저신뢰 매칭이
325
+ // 거의 모든 세션에 주입되어 error 귀속의 80%를 차지했음.
326
+ //
327
+ // 2026-04-21 (precision audit follow-up): 단일 태그 매칭은 주입 차단.
328
+ // ~/.forgen/state/match-eval-log.jsonl 7406 queries 분석 결과, top-1의
329
+ // 33.5%가 "forgen", "type", "file" 같은 공통 단어 한 개로 매칭되어 희귀
330
+ // 태그 BM25 boost로 0.5~0.8 점수를 받고 사용자 컨텍스트를 오염시켰다.
331
+ // Matcher는 top-5 recall 유지를 위해 permissive 하게 두고 (bootstrap eval
332
+ // 호환), 주입 직전에만 엄격히:
333
+ // - identifier match ≥ 1 (함수/파일 이름 리터럴 매칭 — 강한 신호) OR
334
+ // - matched tags ≥ 2 (의도 교차점 2개 이상)
335
+ // 둘 중 하나를 만족해야 주입.
336
+ // 2026-04-21 (champion-aware gate): fitness 상태가 champion/active인 솔루션은
337
+ // 검증된 신호가 있으므로 임계값 0.25로 완화. draft/underperform 은 0.3 그대로.
338
+ // Fitness 데이터가 없으면 전체 default 0.3 (cold-start 회귀 없음).
339
+ // Gate sweep 결과: precision 95.5% / recall 84% / off-topic specificity 100%.
340
+ const fitnessStateMap = new Map();
341
+ try {
342
+ const { computeFitness } = await import('../engine/solution-fitness.js');
343
+ for (const r of computeFitness()) {
344
+ fitnessStateMap.set(r.solution, r.state);
345
+ }
346
+ }
347
+ catch (e) {
348
+ log.debug('fitness state load 실패 — default 0.3 적용', e);
349
+ }
350
+ function minRelevanceFor(name) {
351
+ const state = fitnessStateMap.get(name);
352
+ return (state === 'champion' || state === 'active')
353
+ ? MIN_INJECT_RELEVANCE_TRUSTED
354
+ : MIN_INJECT_RELEVANCE;
355
+ }
303
356
  let experimentCount = 0;
304
357
  const toInject = [];
305
358
  for (const sol of matches) {
306
359
  if (injected.has(sol.name))
307
360
  continue;
361
+ if (sol.relevance < minRelevanceFor(sol.name))
362
+ continue;
363
+ const idMatches = sol.matchedIdentifiers?.length ?? 0;
364
+ const tagMatches = Math.max(0, sol.matchedTags.length - idMatches);
365
+ if (idMatches < 1 && tagMatches < 2)
366
+ continue;
308
367
  if (sol.status === 'experiment') {
309
368
  if (experimentCount >= 1)
310
369
  continue;
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Forgen — Stop Guard (Mech-B prototype, spike/mech-b-a1)
4
+ *
5
+ * Stop hook: 어시스턴트 직전 응답에서 "완료 선언" 패턴을 감지하고, 연결된
6
+ * Mech-A(artifact_check) / Mech-B(self_check_prompt) 규칙을 평가하여
7
+ * 위반 시 blockStop 으로 세션을 재개시킨다.
8
+ *
9
+ * Prototype scope (spike only — NOT v0.4.0 final):
10
+ * - 규칙은 tests/spike/mech-b-inject/scenarios.json 에서 로드
11
+ * (FORGEN_SPIKE_RULES env 로 override 가능)
12
+ * - 어시스턴트 메시지는 transcript_path 에서 마지막 assistant 턴을 뽑거나
13
+ * FORGEN_SPIKE_LAST_MESSAGE env 로 주입 가능 (runner/단위테스트용)
14
+ * - artifact_check 는 `~/.forgen/state/<relative>` 경로를 기준으로 평가
15
+ *
16
+ * 설계 제약 (ADR-001, Day-1 verification):
17
+ * - self_check_prompt 질문은 **reason** 에 전체를 담는다 (모델 도달).
18
+ * - systemMessage 는 rule tag 한 줄만 (UI 표시 보조).
19
+ * - 외부 LLM API 호출 없음 (β1 유지).
20
+ */
21
+ import type { Rule } from '../store/types.js';
22
+ interface VerifierSpec {
23
+ kind: 'self_check_prompt' | 'artifact_check' | 'tool_arg_regex';
24
+ params: Record<string, string | number | boolean>;
25
+ }
26
+ interface SpikeRule {
27
+ id: string;
28
+ mech: 'A' | 'B' | 'C';
29
+ hook: 'Stop' | 'PreToolUse' | 'PostToolUse' | 'UserPromptSubmit';
30
+ trigger: {
31
+ response_keywords_regex?: string;
32
+ context_exclude_regex?: string;
33
+ };
34
+ verifier: VerifierSpec;
35
+ block_message?: string;
36
+ system_tag?: string;
37
+ }
38
+ /**
39
+ * 프로덕션 rule-store 로더.
40
+ * ~/.forgen/me/rules 의 Rule 중 `enforce_via` 에 `hook: 'Stop'` 이 있는 것만
41
+ * SpikeRule 내부 shape 로 변환해 반환한다.
42
+ *
43
+ * 변환 규칙:
44
+ * - `trigger_keywords_regex` 미지정 → DEFAULT_STOP_TRIGGER_RE (shared)
45
+ * - `trigger_exclude_regex` 미지정 → DEFAULT_STOP_EXCLUDE_RE (shared)
46
+ * - verifier.kind 는 `self_check_prompt` 또는 `artifact_check` 지원
47
+ * - 그 외 verifier 는 skip (PreToolUse 전용 tool_arg_regex 등)
48
+ */
49
+ export declare function rulesFromStore(rules: Rule[]): SpikeRule[];
50
+ /** Pure core — 단위 테스트용. stdin/IO 없음. */
51
+ export declare function evaluateStop(lastAssistantMessage: string, rules: SpikeRule[]): {
52
+ action: 'approve';
53
+ hit: null;
54
+ } | {
55
+ action: 'block';
56
+ hit: SpikeRule;
57
+ reason: string;
58
+ };
59
+ /**
60
+ * 같은 (session, rule) 조합의 연속 block 카운트. approve 가 일어나면 0 으로 초기화.
61
+ * export for tests. 부수효과: 디렉토리 생성 + 파일 쓰기.
62
+ */
63
+ export declare function incrementBlockCount(sessionId: string, ruleId: string): number;
64
+ export declare function resetBlockCount(sessionId: string, ruleId: string): void;
65
+ /**
66
+ * R9-PA2: approve 시점에 같은 session 의 pending block 을 찾아 ack 이벤트로 기록.
67
+ * Mech-B 의 핵심 가치(block → retract → pass)가 실제 작동했음을 관측 가능하게 한다.
68
+ * Best-effort: 실패해도 approve 자체는 영향받지 않는다.
69
+ *
70
+ * 기록 후 block-count 파일은 cleanup — 같은 session 의 같은 rule 이 다시 block 되면
71
+ * 새로운 카운트로 시작 (block-count 의미 보존).
72
+ */
73
+ export declare function acknowledgeSessionBlocks(sessionId: string): number;
74
+ export declare function logDriftEvent(event: {
75
+ kind: string;
76
+ session_id: string;
77
+ rule_id: string;
78
+ count: number;
79
+ reason_preview?: string;
80
+ message_preview?: string;
81
+ }): void;
82
+ export declare function getStuckLoopThreshold(): number;
83
+ export declare function main(): Promise<void>;
84
+ export {};