Skip to content

Commit 7ff02a5

Browse files
committed
Optimize RLCR context reuse
Generate an immutable context-pack.md for each RLCR run and reference it from implementation and routine review prompts. The context pack carries a stable goal/AC digest, source identity, and source-of-truth rules, while the original plan remains authoritative and full-alignment reviews still read the full plan directly. Keep routine review prompt prefixes stable by removing the round number from the title and moving round-specific payloads, summary content, commit history, prompt path, and review output path behind a Dynamic Round Payload section. Preserve existing review semantics such as Claude summary boundary markers, review intent wording, Original Implementation Plan headings, and the 1-2 target AC round contract rule. Replace fragile sed-based plan goal/AC extraction with section-aware parsing so context-pack generation is reliable. Record the context pack path in loop state and pass it through stop-hook generated regular-review and next-round prompts, with fallback to the original plan for older loops without a context pack. Final auxiliary validation showed regular review reusable-prefix ratio improving from 0.24% to 88.35%, average reusable prefix growing from 22 bytes to 5296 bytes, and a representative Round 0 prompt estimate dropping 72.2% by replacing embedded plan text with context references. Auxiliary validation tools are kept out of this commit and saved separately in stash.
1 parent 2da7def commit 7ff02a5

4 files changed

Lines changed: 163 additions & 24 deletions

File tree

hooks/loop-codex-stop-hook.sh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,10 @@ fi
10331033

10341034
# Calculate derived values for templates
10351035
LOOP_TIMESTAMP=$(basename "$LOOP_DIR")
1036+
CONTEXT_PACK_FILE=".humanize/rlcr/${LOOP_TIMESTAMP}/context-pack.md"
1037+
if [[ ! -f "$PROJECT_ROOT/$CONTEXT_PACK_FILE" ]]; then
1038+
CONTEXT_PACK_FILE="$PLAN_FILE"
1039+
fi
10361040
COMPLETED_ITERATIONS=$((CURRENT_ROUND + 1))
10371041
# Clamp previous round indices to 0 minimum to avoid negative file references
10381042
# This can happen with --full-review-round 2 where first alignment check is at round 1
@@ -1084,13 +1088,21 @@ Review Claude's work against the plan and goal tracker. Check all goals are bein
10841088
10851089
Write your review to {{REVIEW_RESULT_FILE}}. End with COMPLETE if done, or list issues."
10861090

1087-
REGULAR_REVIEW_FALLBACK="# Code Review (Round {{CURRENT_ROUND}})
1091+
REGULAR_REVIEW_FALLBACK="# Code Review
10881092
1093+
Read @{{CONTEXT_PACK_FILE}} and @{{GOAL_TRACKER_FILE}} first. The original plan remains authoritative at @{{PLAN_FILE}}; read it if the stable context is insufficient or ambiguous.
10891094
Review Claude's work for this round.
10901095
1091-
## Claude's Summary
1092-
{{SUMMARY_CONTENT}}
10931096
1097+
## Dynamic Round Payload
1098+
Round: {{CURRENT_ROUND}}
1099+
1100+
---
1101+
Below is Claude's summary of the work completed:
1102+
<!-- CLAUDE's WORK SUMMARY START -->
1103+
{{SUMMARY_CONTENT}}
1104+
<!-- CLAUDE's WORK SUMMARY END -->
1105+
---
10941106
{{COMMIT_HISTORY_SECTION}}
10951107
10961108
{{GOAL_TRACKER_UPDATE_SECTION}}
@@ -1118,6 +1130,7 @@ else
11181130
load_and_render_safe "$TEMPLATE_DIR" "codex/regular-review.md" "$REGULAR_REVIEW_FALLBACK" \
11191131
"CURRENT_ROUND=$CURRENT_ROUND" \
11201132
"PLAN_FILE=$PLAN_FILE" \
1133+
"CONTEXT_PACK_FILE=$CONTEXT_PACK_FILE" \
11211134
"PROMPT_FILE=$PROMPT_FILE" \
11221135
"SUMMARY_CONTENT=$SUMMARY_CONTENT" \
11231136
"GOAL_TRACKER_FILE=$GOAL_TRACKER_FILE" \
@@ -2069,6 +2082,7 @@ if [[ "$DRIFT_REPLAN_REQUIRED" == "true" ]]; then
20692082
"PLAN_FILE=$PLAN_FILE" \
20702083
"REVIEW_CONTENT=$REVIEW_CONTENT" \
20712084
"GOAL_TRACKER_FILE=$GOAL_TRACKER_FILE" \
2085+
"CONTEXT_PACK_FILE=$CONTEXT_PACK_FILE" \
20722086
"BITLESSON_FILE=$BITLESSON_FILE" \
20732087
"ROUND_CONTRACT_FILE=$NEXT_CONTRACT_FILE" \
20742088
"CURRENT_ROUND=$NEXT_ROUND" \
@@ -2079,6 +2093,7 @@ else
20792093
"PLAN_FILE=$PLAN_FILE" \
20802094
"REVIEW_CONTENT=$REVIEW_CONTENT" \
20812095
"GOAL_TRACKER_FILE=$GOAL_TRACKER_FILE" \
2096+
"CONTEXT_PACK_FILE=$CONTEXT_PACK_FILE" \
20822097
"BITLESSON_FILE=$BITLESSON_FILE" \
20832098
"ROUND_CONTRACT_FILE=$NEXT_CONTRACT_FILE" \
20842099
"CURRENT_ROUND=$NEXT_ROUND" \

prompt-template/claude/next-round-prompt.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ Your work is not finished. Read and execute the below with ultrathink.
22

33
## Original Implementation Plan
44

5-
**IMPORTANT**: Before proceeding, review the original plan you are implementing:
5+
**IMPORTANT**: Before proceeding, review the stable context pack first:
6+
@{{CONTEXT_PACK_FILE}}
7+
8+
The original plan remains authoritative at:
69
@{{PLAN_FILE}}
710

811
This plan contains the full scope of work and requirements. Ensure your work aligns with this plan.
912

13+
Read the original plan whenever the context pack is insufficient, ambiguous, or conflicts with Codex feedback, code, summaries, or goal-tracker.md.
14+
1015
---
1116

1217
## Round Re-anchor (REQUIRED FIRST STEP)
1318

1419
Before writing code:
15-
- Re-read @{{PLAN_FILE}}
20+
- Re-read @{{CONTEXT_PACK_FILE}}
21+
- Re-read @{{PLAN_FILE}} only if the context pack is insufficient, ambiguous, or in conflict with the review feedback
1622
- Re-read @{{GOAL_TRACKER_FILE}}
1723
- Re-read the most recent round summaries/reviews that led to this round
1824
- Write the current round contract to @{{ROUND_CONTRACT_FILE}}

prompt-template/codex/regular-review.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
# Code Review - Round {{CURRENT_ROUND}}
1+
# Code Review
22

33
## Original Implementation Plan
44

55
**IMPORTANT**: The original plan that Claude is implementing is located at:
66
@{{PLAN_FILE}}
77

8-
You MUST read this plan file first to understand the full scope of work before conducting your review.
98
This plan contains the complete requirements and implementation details that Claude should be following.
109

11-
Based on the original plan and @{{PROMPT_FILE}}, Claude claims to have completed the work. Please conduct a thorough critical review to verify this.
10+
For routine reviews, keep the reusable context prefix stable:
11+
- Read @{{CONTEXT_PACK_FILE}} and @{{GOAL_TRACKER_FILE}} first.
12+
- Do not re-read the original plan by default during routine review.
13+
- Read @{{PLAN_FILE}} if the context pack is insufficient, ambiguous, conflicts with the current summary, omits a requirement relevant to this round, or if an acceptance criterion, implementation boundary, plan change, deferral, or scope reinterpretation is unclear.
1214

13-
---
14-
Below is Claude's summary of the work completed:
15-
<!-- CLAUDE's WORK SUMMARY START -->
16-
{{SUMMARY_CONTENT}}
17-
<!-- CLAUDE's WORK SUMMARY END -->
18-
---
15+
Based on the stable context and the current round prompt listed in the dynamic payload, Claude claims to have completed the work below. Please conduct a thorough critical review to verify this.
1916

20-
{{COMMIT_HISTORY_SECTION}}
2117

2218
## Part 1: Implementation Review
2319

@@ -37,7 +33,7 @@ Below is Claude's summary of the work completed:
3733
Read @{{GOAL_TRACKER_FILE}} and verify:
3834

3935
1. **Acceptance Criteria Progress**: For each AC, is progress being made? Are any ACs being ignored?
40-
2. **Forgotten Items**: Are there tasks from the original plan that are not tracked in Active/Completed/Deferred?
36+
2. **Forgotten Items**: Are there tasks from the original plan that are not tracked in Active/Completed/Deferred? If the context pack is insufficient to answer this, read @{{PLAN_FILE}}.
4137
3. **Deferred Items**: Are deferrals justified? Do they block any ACs?
4238
4. **Plan Evolution**: If Claude modified the plan, is the justification valid?
4339

@@ -68,9 +64,26 @@ If Claude mostly worked on queued side issues and failed to advance the mainline
6864

6965
- In short, your review comments can include: problems/findings/blockers; claims that don't match reality; implementation plans for deferred work (to be implemented now); implementation plans for unfinished work; goal alignment issues.
7066
- Your output should be structured so Claude can tell which items are mainline gaps, blocking side issues, and queued side issues.
71-
- If after your investigation the actual situation does not match what Claude claims to have completed, or there is pending work to be done, output your review comments to @{{REVIEW_RESULT_FILE}}.
67+
- If after your investigation the actual situation does not match what Claude claims to have completed, or there is pending work to be done, output your review comments to the review result file listed in the dynamic payload.
7268
- **CRITICAL**: Only output "COMPLETE" as the last line if ALL tasks from the original plan are FULLY completed with no deferrals
7369
- DEFERRED items are considered INCOMPLETE - do NOT output COMPLETE if any task is deferred
7470
- UNFINISHED items are considered INCOMPLETE - do NOT output COMPLETE if any task is pending
7571
- The ONLY condition for COMPLETE is: all original plan tasks are done, all ACs are met, no deferrals or pending work allowed
7672
- The word COMPLETE on the last line will stop Claude.
73+
74+
## Dynamic Round Payload
75+
76+
Round: {{CURRENT_ROUND}}
77+
Current round prompt: @{{PROMPT_FILE}}
78+
79+
Review result file: @{{REVIEW_RESULT_FILE}}
80+
81+
82+
---
83+
Below is Claude's summary of the work completed:
84+
<!-- CLAUDE's WORK SUMMARY START -->
85+
{{SUMMARY_CONTENT}}
86+
<!-- CLAUDE's WORK SUMMARY END -->
87+
---
88+
89+
{{COMMIT_HISTORY_SECTION}}

scripts/setup-rlcr-loop.sh

Lines changed: 112 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,30 @@ PRIVACY_MODE="true"
5656

5757
extract_plan_goal_content() {
5858
local plan_path="$1"
59-
local goal_section=""
60-
61-
goal_section=$({ sed -n '/^##[[:space:]]*[Gg]oal\|^##[[:space:]]*[Oo]bjective\|^##[[:space:]]*[Pp]urpose/,/^##/p' "$plan_path" 2>/dev/null || true; } | head -20 | tail -n +2 | head -10)
59+
local goal_section
60+
goal_section=$(awk '
61+
BEGIN { in_section = 0; emitted = 0 }
62+
/^##[[:space:]]+/ {
63+
lower = tolower($0)
64+
if (in_section) {
65+
exit
66+
}
67+
if (lower ~ /^##[[:space:]]*(goal|objective|purpose)([[:space:]]|$)/) {
68+
in_section = 1
69+
}
70+
next
71+
}
72+
in_section {
73+
if (emitted == 0 && $0 ~ /^[[:space:]]*$/) {
74+
next
75+
}
76+
print
77+
emitted++
78+
if (emitted >= 10) {
79+
exit
80+
}
81+
}
82+
' "$plan_path")
6283
if [[ -n "$goal_section" ]]; then
6384
printf '%s\n' "$goal_section"
6485
return
@@ -85,7 +106,29 @@ extract_plan_goal_content() {
85106

86107
extract_plan_ac_content() {
87108
local plan_path="$1"
88-
{ sed -n '/^##[[:space:]]*[Aa]cceptance\|^##[[:space:]]*[Cc]riteria\|^##[[:space:]]*[Rr]equirements/,/^##/p' "$plan_path" 2>/dev/null || true; } | head -30 | tail -n +2 | head -25
109+
awk '
110+
BEGIN { in_section = 0; emitted = 0 }
111+
/^##[[:space:]]+/ {
112+
lower = tolower($0)
113+
if (in_section) {
114+
exit
115+
}
116+
if (lower ~ /^##[[:space:]]*(acceptance|criteria|requirements)([[:space:]]|$)/) {
117+
in_section = 1
118+
}
119+
next
120+
}
121+
in_section {
122+
if (emitted == 0 && $0 ~ /^[[:space:]]*$/) {
123+
next
124+
}
125+
print
126+
emitted++
127+
if (emitted >= 25) {
128+
exit
129+
}
130+
}
131+
' "$plan_path"
89132
}
90133

91134
show_help() {
@@ -855,6 +898,61 @@ else
855898
cp "$FULL_PLAN_PATH" "$LOOP_DIR/plan.md"
856899
fi
857900

901+
# Create an immutable context pack once per loop. Follow-up prompts reference this
902+
# stable file instead of re-embedding the full plan, which keeps reusable prompt
903+
# prefixes stable while preserving the original plan as the source of truth.
904+
CONTEXT_PACK_FILE="$LOOP_DIR/context-pack.md"
905+
CONTEXT_PACK_REL=".humanize/rlcr/$TIMESTAMP/context-pack.md"
906+
if command -v sha256sum >/dev/null 2>&1; then
907+
PLAN_SHA256=$(sha256sum "$LOOP_DIR/plan.md" | awk '{print $1}')
908+
else
909+
PLAN_SHA256=$(shasum -a 256 "$LOOP_DIR/plan.md" | awk '{print $1}')
910+
fi
911+
PLAN_GOAL_DIGEST=$(extract_plan_goal_content "$LOOP_DIR/plan.md")
912+
PLAN_AC_DIGEST=$(extract_plan_ac_content "$LOOP_DIR/plan.md")
913+
914+
if [[ -z "$PLAN_GOAL_DIGEST" ]]; then
915+
PLAN_GOAL_DIGEST="Use the original plan at $PLAN_FILE as the source of truth for the loop objective."
916+
fi
917+
918+
if [[ -z "$PLAN_AC_DIGEST" ]]; then
919+
PLAN_AC_DIGEST="- Acceptance criteria must be read from the original plan at $PLAN_FILE before implementation or review decisions."
920+
fi
921+
922+
cat > "$CONTEXT_PACK_FILE" << EOF
923+
# RLCR Context Pack
924+
925+
This file is generated once when the RLCR loop starts. Treat it as an immutable
926+
stable context prefix. If the plan evolves, record the change in goal-tracker.md
927+
or the current round contract instead of editing this file.
928+
929+
## Source Identity
930+
931+
- Plan file: $PLAN_FILE
932+
- Plan SHA256: $PLAN_SHA256
933+
- Project root: $PROJECT_ROOT
934+
- Start branch: $START_BRANCH
935+
- Base branch: $BASE_BRANCH
936+
- Base commit: $BASE_COMMIT
937+
938+
## Goal Digest
939+
940+
$PLAN_GOAL_DIGEST
941+
942+
## Acceptance Criteria Digest
943+
944+
$PLAN_AC_DIGEST
945+
946+
## Source of Truth Rules
947+
948+
- The original plan remains available at @$PLAN_FILE and is authoritative.
949+
- Use this context pack as the stable first read for routine re-anchoring.
950+
- Read the original plan whenever this digest is insufficient, ambiguous, or in conflict with code, summaries, reviews, or goal-tracker.md.
951+
- Full alignment reviews must read the original plan directly.
952+
- Deferrals are incomplete unless explicitly approved by the user and recorded in goal-tracker.md.
953+
EOF
954+
955+
858956
# Docs path default
859957
DOCS_PATH="docs"
860958

@@ -904,6 +1002,7 @@ privacy_mode: $PRIVACY_MODE
9041002
bitlesson_required: $BITLESSON_STATE_VALUE
9051003
bitlesson_file: $BITLESSON_FILE_REL
9061004
bitlesson_allow_empty_none: $BITLESSON_ALLOW_EMPTY_NONE
1005+
context_pack_file: $CONTEXT_PACK_REL
9071006
mainline_stall_count: 0
9081007
last_mainline_verdict: unknown
9091008
drift_status: normal
@@ -1299,7 +1398,7 @@ Read and execute below with ultrathink
12991398
13001399
Before starting implementation, you MUST initialize the Goal Tracker:
13011400
1302-
1. Read @$GOAL_TRACKER_FILE
1401+
1. Read @$CONTEXT_PACK_REL, then read @$GOAL_TRACKER_FILE
13031402
2. If the "Ultimate Goal" section says "[To be extracted...]", extract a clear goal statement from the plan
13041403
3. If the "Acceptance Criteria" section says "[To be defined...]", define 3-7 specific, testable criteria
13051404
4. Populate the "Active Tasks" table with MAINLINE tasks from the plan, mapping each to an AC and filling Tag/Owner
@@ -1322,6 +1421,14 @@ Use this contract to keep the round focused. Do NOT let non-blocking bugs or cle
13221421
13231422
---
13241423
1424+
## Stable Context
1425+
1426+
Read @$CONTEXT_PACK_REL first. The context pack is the stable digest for routine re-anchoring and prompt-cache reuse.
1427+
1428+
The original full plan is still the source of truth at @$PLAN_FILE. Read the original plan whenever the context pack is insufficient, ambiguous, or conflicts with code, summaries, reviews, or goal-tracker.md.
1429+
1430+
---
1431+
13251432
## Implementation Plan
13261433
13271434
For all tasks that need to be completed, please use the Task system (TaskCreate, TaskUpdate, TaskList).
@@ -1348,8 +1455,6 @@ Each task must have one routing tag from the plan: \`coding\` or \`analyze\`.
13481455
13491456
EOF
13501457

1351-
# Append plan content directly (avoids command substitution size limits for large files)
1352-
cat "$LOOP_DIR/plan.md" >> "$LOOP_DIR/round-0-prompt.md"
13531458

13541459
# Append BitLesson Selection section
13551460
cat >> "$LOOP_DIR/round-0-prompt.md" << EOF

0 commit comments

Comments
 (0)