This repository was archived by the owner on Oct 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcodegen-propose
More file actions
executable file
·214 lines (188 loc) · 6.86 KB
/
Copy pathcodegen-propose
File metadata and controls
executable file
·214 lines (188 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env bash
# codegen-propose — turn codegen-analyze turn-waste clusters into
# structured, human-gated proposed-change records via one codegen-call per
# qualifying cluster.
#
# Usage: codegen-propose --since YYYY-MM-DD [--max N] [--min-wasted-turns T]
# [--model M]
#
# Read-only against the repo except for its own output sink:
# codegen/analysis-proposals/<from>_<to>.md (ephemeral, gitignored).
# Never writes source files. Never proposes changes itself — every cluster's
# proposal is drafted by an LLM call and requires human review before any
# fix is applied.
#
# Exit: 0 = batch completed (even with some errored/declined clusters).
# 2 = usage error.
set -euo pipefail
# Resolve the codegen root so both direct invocations (from repo) and
# installed copies (~/.local/bin/) can locate harnesses/ and analysis/.
CODEGEN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
if [[ ! -d "$CODEGEN_DIR/harnesses" ]] && [[ -n "${OCG_CODEGEN_DIR:-}" ]]; then
CODEGEN_DIR="$OCG_CODEGEN_DIR"
fi
DEFAULT_MODEL="opus"
DEFAULT_EFFORT="high"
DEFAULT_MAX=5
DEFAULT_MIN_WASTED_TURNS=4
SINCE=""
MAX="$DEFAULT_MAX"
MIN_WASTED_TURNS="$DEFAULT_MIN_WASTED_TURNS"
MODEL="$DEFAULT_MODEL"
EFFORT="$DEFAULT_EFFORT"
usage() {
printf 'Usage: codegen-propose --since YYYY-MM-DD [--max N] [--min-wasted-turns T] [--model M]\n' >&2
exit 2
}
while [[ $# -gt 0 ]]; do
case "$1" in
--since=*)
SINCE="${1#--since=}"
shift
;;
--since)
SINCE="$2"
shift 2
;;
--max=*)
MAX="${1#--max=}"
shift
;;
--max)
MAX="$2"
shift 2
;;
--min-wasted-turns=*)
MIN_WASTED_TURNS="${1#--min-wasted-turns=}"
shift
;;
--min-wasted-turns)
MIN_WASTED_TURNS="$2"
shift 2
;;
--model=*)
MODEL="${1#--model=}"
shift
;;
--model)
MODEL="$2"
shift 2
;;
*)
printf 'codegen-propose: unknown argument %q\n' "$1" >&2
usage
;;
esac
done
if [[ -z "$SINCE" ]]; then
printf 'codegen-propose: --since YYYY-MM-DD is required\n' >&2
usage
fi
FROM_STAMP="$(printf '%s' "$SINCE" | tr -d '-')"
TO_STAMP="$(date -u +%Y%m%d)"
PROPOSALS_DIR="$CODEGEN_DIR/codegen/analysis-proposals"
PROPOSALS_FILE="$PROPOSALS_DIR/${FROM_STAMP}_${TO_STAMP}.md"
mkdir -p "$PROPOSALS_DIR"
SYSTEM_PROMPT_FILE="$CODEGEN_DIR/harnesses/claude/propose-system-prompt.md"
SCHEMA_FILE="$CODEGEN_DIR/harnesses/claude/proposal.schema.json"
# Prefer codegen-analyze on PATH (allows test stubbing); fall back to direct
# module invocation.
if command -v codegen-analyze >/dev/null 2>&1; then
ANALYZE_CMD=(codegen-analyze)
else
ANALYZE_CMD=(env PYTHONPATH="$CODEGEN_DIR" python3 -m analysis)
fi
SELECT_CMD=(env PYTHONPATH="$CODEGEN_DIR" python3 -m analysis.proposer.select
--min-wasted-turns "$MIN_WASTED_TURNS" --max "$MAX")
SELECTED_JSONL="$("${ANALYZE_CMD[@]}" --json --since "$SINCE" | "${SELECT_CMD[@]}")"
if [[ -z "$SELECTED_JSONL" ]]; then
NOTE="no proposals above threshold ${MIN_WASTED_TURNS} wasted turns (since ${SINCE})"
printf '%s\n' "$NOTE" >"$PROPOSALS_FILE"
printf '%s\n' "$NOTE"
exit 0
fi
PROPOSED_COUNT=0
DECLINED_COUNT=0
ERRORED_COUNT=0
RECORDS_TMP="$(mktemp)"
trap 'rm -f "$RECORDS_TMP"' EXIT
while IFS= read -r cluster_line; do
[[ -z "$cluster_line" ]] && continue
counter="$(printf '%s' "$cluster_line" | jq -r '.counter')"
pattern_key="$(printf '%s' "$cluster_line" | jq -r '.pattern_key')"
wasted_turns="$(printf '%s' "$cluster_line" | jq -r '.wasted_turns')"
sessions="$(printf '%s' "$cluster_line" | jq -r '.sessions')"
top_evidence="$(printf '%s' "$cluster_line" | jq -r '.top_evidence')"
prompt="Cluster to draft a proposed-change record for:
counter: ${counter}
pattern_key: ${pattern_key}
wasted_turns: ${wasted_turns}
sessions: ${sessions}
top_evidence: ${top_evidence}"
set +e
call_out=$(codegen-call --harness=claude_code --model="$MODEL" --effort="$EFFORT" \
--system-prompt "@$SYSTEM_PROMPT_FILE" \
--json-schema "@$SCHEMA_FILE" \
-- "$prompt" 2>/dev/null)
call_rc=$?
set -e
if [[ $call_rc -ne 0 ]]; then
ERRORED_COUNT=$((ERRORED_COUNT + 1))
{
printf '## Proposals\n\n'
printf '### %s: %s (ERRORED)\n\n' "$counter" "$pattern_key"
printf -- '- wasted_turns: %s\n' "$wasted_turns"
printf -- '- error: codegen-call exited %s\n\n' "$call_rc"
} >>"$RECORDS_TMP"
continue
fi
status="$(printf '%s' "$call_out" | jq -r '.result.status // empty')"
value="$(printf '%s' "$call_out" | jq -c '.result.value // empty')"
if [[ "$status" != "success" || -z "$value" || "$value" == "null" ]]; then
ERRORED_COUNT=$((ERRORED_COUNT + 1))
reason="$(printf '%s' "$call_out" | jq -r '.result.reason // "no reason given"')"
{
printf '## Proposals\n\n'
printf '### %s: %s (ERRORED)\n\n' "$counter" "$pattern_key"
printf -- '- wasted_turns: %s\n' "$wasted_turns"
printf -- '- error: result.status=%s, reason=%s\n\n' "$status" "$reason"
} >>"$RECORDS_TMP"
continue
fi
confidence="$(printf '%s' "$value" | jq -r '.confidence // "low"')"
change="$(printf '%s' "$value" | jq -c '.change // empty')"
target_file="$(printf '%s' "$value" | jq -r '.target_file // "unknown"')"
anchor="$(printf '%s' "$value" | jq -r '.anchor // ""')"
rationale="$(printf '%s' "$value" | jq -r '.rationale // ""')"
if [[ -z "$change" || "$change" == "null" ]]; then
DECLINED_COUNT=$((DECLINED_COUNT + 1))
{
printf '## Proposals\n\n'
printf '### %s: %s (DECLINED-LOW-CONFIDENCE)\n\n' "$counter" "$pattern_key"
printf -- '- wasted_turns: %s\n' "$wasted_turns"
printf -- '- confidence: %s\n' "$confidence"
printf -- '- rationale: %s\n\n' "$rationale"
} >>"$RECORDS_TMP"
continue
fi
PROPOSED_COUNT=$((PROPOSED_COUNT + 1))
{
printf '## Proposals\n\n'
printf '### %s: %s (PROPOSED)\n\n' "$counter" "$pattern_key"
printf -- '- wasted_turns: %s\n' "$wasted_turns"
printf -- '- confidence: %s\n' "$confidence"
printf -- '- target_file: %s\n' "$target_file"
printf -- '- anchor: %s\n' "$anchor"
printf -- '- rationale: %s\n' "$rationale"
printf -- '- change:\n\n```json\n%s\n```\n\n' "$change"
} >>"$RECORDS_TMP"
done <<<"$SELECTED_JSONL"
{
printf 'Summary: %s proposed / %s declined-low-confidence / %s errored\n\n' \
"$PROPOSED_COUNT" "$DECLINED_COUNT" "$ERRORED_COUNT"
cat "$RECORDS_TMP"
} >"$PROPOSALS_FILE"
printf 'Summary: %s proposed / %s declined-low-confidence / %s errored\n' \
"$PROPOSED_COUNT" "$DECLINED_COUNT" "$ERRORED_COUNT"
printf 'Proposals written: %s\n' "$PROPOSALS_FILE"
exit 0