Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Releases/v4.0.3/.claude/hooks/IntegrityCheck.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,26 @@ interface HookInput {
}

async function readStdin(): Promise<HookInput | null> {
let reader: ReadableStreamDefaultReader<Uint8Array> | null = null;
try {
const decoder = new TextDecoder();
const reader = Bun.stdin.stream().getReader();
reader = Bun.stdin.stream().getReader();
let input = '';
const timeout = new Promise<void>(r => setTimeout(r, 500));
const timeout = new Promise<void>(r => setTimeout(r, 2000));
const read = (async () => {
while (true) {
const { done, value } = await reader.read();
const { done, value } = await reader!.read();
if (done) break;
input += decoder.decode(value, { stream: true });
}
})();
await Promise.race([read, timeout]);
reader.cancel().catch(() => {});
if (input.trim()) return JSON.parse(input) as HookInput;
} catch {}
} catch (err) {
console.error('[IntegrityCheck] readStdin:', err);
if (reader) reader.cancel().catch(() => {});
}
return null;
}

Expand Down
55 changes: 47 additions & 8 deletions Releases/v4.0.3/.claude/hooks/PRDSync.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

import { readFileSync, existsSync } from 'fs';
import { join } from 'path';
import {
parseFrontmatter,
syncToWorkJson,
Expand All @@ -21,15 +22,36 @@ import { setPhaseTab } from './lib/tab-setter';
import type { AlgorithmTabPhase } from './lib/tab-constants';

let input: any;
try {
input = JSON.parse(readFileSync(0, 'utf-8'));
} catch {
process.exit(0);
}
let responded = false;

const toolInput = input.tool_input || {};
async function readStdin(): Promise<any> {
let reader: ReadableStreamDefaultReader<Uint8Array> | null = null;
try {
const decoder = new TextDecoder();
reader = Bun.stdin.stream().getReader();
let raw = '';
const timeout = new Promise<void>(r => setTimeout(r, 2000));
const read = (async () => {
while (true) {
const { done, value } = await reader!.read();
if (done) break;
raw += decoder.decode(value, { stream: true });
}
})();
await Promise.race([read, timeout]);
reader.cancel().catch(() => {});
if (raw.trim()) return JSON.parse(raw);
} catch (err) {
console.error('[PRDSync] readStdin:', err);
if (reader) reader.cancel().catch(() => {});
}
return null;
}

async function main() {
input = await readStdin();
if (!input) process.exit(0);
const toolInput = input.tool_input || {};
// Only trigger for PRD.md files in MEMORY/WORK/
const filePath = toolInput.file_path || '';
if (!filePath.includes('MEMORY/WORK/') || !filePath.endsWith('PRD.md')) return;
Expand All @@ -52,7 +74,24 @@ async function main() {
const registry = readRegistry();
const existing = registry.sessions[fm.slug];
if (existing) oldPhase = (existing.phase || '').toUpperCase();
} catch { /* silent */ }
} catch (err) { console.error('[PRDSync] readRegistry skipping (silent):', err); }
}

// Gate: when phase transitions to COMPLETE, check for reflection JSONL (before sync overwrites oldPhase)
if (newPhase === 'COMPLETE' && oldPhase !== 'COMPLETE' && fm.slug) {
const paiDir = process.env.PAI_DIR || join(process.env.HOME!, '.claude');
const reflPath = join(paiDir, 'MEMORY', 'LEARNING', 'REFLECTIONS', 'algorithm-reflections.jsonl');
let hasReflection = false;
if (existsSync(reflPath)) {
hasReflection = readFileSync(reflPath, 'utf-8').includes(fm.slug);
}
if (!hasReflection) {
console.log(JSON.stringify({
continue: true,
additionalContext: `<system-reminder>\n⚠️ REFLECTION MISSING: PRD "${fm.slug}" set to phase: complete but NO reflection entry in algorithm-reflections.jsonl. Write the reflection JSONL now. Do not end this session without it.\n</system-reminder>`
}));
responded = true;
}
}

// Sync frontmatter + criteria to work.json (pass session_id for session name lookup)
Expand All @@ -71,6 +110,6 @@ async function main() {
}

main().catch(() => {}).finally(() => {
console.log(JSON.stringify({ continue: true }));
if (!responded) console.log(JSON.stringify({ continue: true }));
process.exit(0);
});
9 changes: 6 additions & 3 deletions Releases/v4.0.3/.claude/hooks/lib/hook-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,32 @@ export interface HookInput {
* Returns null if stdin is empty or malformed.
*/
export async function readHookInput(): Promise<HookInput | null> {
let reader: ReadableStreamDefaultReader<Uint8Array> | null = null;
try {
const decoder = new TextDecoder();
const reader = Bun.stdin.stream().getReader();
reader = Bun.stdin.stream().getReader();
let input = '';

const timeoutPromise = new Promise<void>((resolve) => {
setTimeout(() => resolve(), 500);
setTimeout(() => resolve(), 2000);
});

const readPromise = (async () => {
while (true) {
const { done, value } = await reader.read();
const { done, value } = await reader!.read();
if (done) break;
input += decoder.decode(value, { stream: true });
}
})();

await Promise.race([readPromise, timeoutPromise]);
reader.cancel().catch(() => {});

if (input.trim()) {
return JSON.parse(input) as HookInput;
}
} catch (error) {
if (reader) reader.cancel().catch(() => {});
console.error('[hook-io] Error reading stdin:', error);
}
return null;
Expand Down