devzone 7.3.0 → 7.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.
@@ -1,247 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Drives opencode on a brief: rotation, silence at start, resume.
3
- # ai module (#221). devzone agent <folder> <brief> | state | release.
4
- # shellcheck disable=SC2034 # RACINE, FREE_MODELS, PAUSE_RANK: read by the sourced agent-*.sh
5
- set -euo pipefail
6
- # Folder of the script, symbolic links followed by hand: readlink -f does not exist
7
- # on macOS (tests under bash 3.2).
8
- SOURCE="${BASH_SOURCE[0]}"
9
- while [ -L "$SOURCE" ]; do
10
- DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
11
- SOURCE="$(readlink "$SOURCE")"
12
- case "$SOURCE" in
13
- /*) ;;
14
- *) SOURCE="$DIR/$SOURCE" ;;
15
- esac
16
- done
17
- ICI="$(cd -P "$(dirname "$SOURCE")" && pwd)"
18
- RACINE="${DEVZONE_ROOT:-$(dirname "$ICI")}"
19
- # shellcheck source=agent-errors.sh disable=SC1091
20
- . "$ICI/agent-errors.sh"
21
- # shellcheck source=agent-rights.sh disable=SC1091
22
- . "$ICI/agent-rights.sh"
23
- # shellcheck source=SCRIPTDIR/agent-session.sh disable=SC1091
24
- . "$ICI/agent-session.sh"
25
- # shellcheck source=agent-workspace.sh disable=SC1091
26
- . "$ICI/agent-workspace.sh"
27
- # shellcheck source=agent-local.sh disable=SC1091
28
- . "$ICI/agent-local.sh"
29
- # shellcheck source=SCRIPTDIR/agent-model.sh disable=SC1091
30
- . "$ICI/agent-model.sh"
31
- FREE_MODELS=(big-pickle deepseek-v4.1-flash-free hy4-flash-free nemotron-3.5-super-vl-free
32
- nemotron-3.5-lightning-free mimo-v2.6-flash-free beam-free flint-40b-free longcat-2.5-free)
33
- # Ephemeral session pod (#222)
34
- # shellcheck source=agent-pod.sh disable=SC1091
35
- . "$ICI/agent-pod.sh"
36
- SILENCE="${DEVZONE_AGENT_SILENCE:-45}"
37
- FROZEN="${DEVZONE_AGENT_FROZEN:-600}"
38
- AGENT_STATE="${DEVZONE_AGENT_STATE:-$HOME/.local/state/oc-lancer}"
39
- # Pause between read and write of the rank (tests only): active only when
40
- # DEVZONE_AGENT_PAUSE_RANK is set under bats; never a pause by default.
41
- PAUSE_RANK=0
42
- if [ -n "${DEVZONE_AGENT_PAUSE_RANK:-}" ] && [ -n "${BATS_TEST_TMPDIR:-}" ]; then PAUSE_RANK="$DEVZONE_AGENT_PAUSE_RANK"; fi
43
- # Retries of the same session for a network cause, per model, before the next model
44
- NETWORK_RETRIES=3
45
- refuse() { echo "devzone agent: $*" >&2; exit 2; }
46
- agent_launch() {
47
- local folder="$1" brief="$2"
48
- local journal state start m i pid code offset size started freeze discard new
49
- local brief_current resumed=0 now last output recent cause start_t
50
- local network_for="" network_n=0
51
- # Paths relative to the calling folder: made absolute before any cd, lock or
52
- # state write; the name of the brief is kept.
53
- [ -d "$folder" ] || refuse "folder not found: $folder (searched from $(pwd))"
54
- [ -r "$brief" ] || refuse "brief not found: $brief (searched from $(pwd))"
55
- folder="$(cd "$folder" && pwd -P)"
56
- brief="$(cd "$(dirname "$brief")" && pwd -P)/$(basename "$brief")"
57
- # ai module required: without assistants, nothing to drive.
58
- if ! bash "$ICI/devzone" is-enabled ai >/dev/null 2>&1; then
59
- refuse "ai module disabled: devzone enable ai"
60
- fi
61
- configure
62
- journal="$(journal_of "$folder")"
63
- export AGENT_JOURNAL="$journal"
64
- state="$AGENT_STATE"
65
- mkdir -p "$(dirname "$journal")" "$state"
66
- rights_snapshot "$folder"
67
- models_load "$journal"
68
- local_charge
69
- start="$(rank_take "$state" "$journal")" || refuse "model rank locked: retry later"
70
- brief_current="$brief"
71
- # Resume: a new session restarts from the work already present (git status,
72
- # diff, log), without conversation history.
73
- resume() {
74
- if [ "$resumed" = 1 ]; then return 0; fi
75
- resumed=1
76
- local c
77
- c="$(mktemp)"
78
- cat "$brief_current" >"$c"
79
- printf '\n\nRESUME: a previous session was interrupted. Read git status, git diff and git log in the folder, check this work against the brief above, fix what deviates from it and finish what is missing.\n' >>"$c"
80
- brief_current="$c"
81
- }
82
- model_order "$start"
83
- i=0
84
- while [ "$i" -lt "${#ORDER[@]}" ]; do
85
- m="${ORDER[$i]}"
86
- if [ "$m" != "$network_for" ]; then network_for="$m"; network_n=0; fi
87
- if model_excluded "$m"; then
88
- model_requested_excluded "$m" "$(model_exclusion_cause "$m")" "$journal"
89
- i=$(( i + 1 ))
90
- continue
91
- fi
92
- model_requested_chosen "$m" "$journal"
93
- echo "=== model opencode/$m start $(date -u +%Y-%m-%dT%H:%M:%SZ)" >>"$journal"
94
- offset="$(wc -c <"$journal")"
95
- opencode_launch "$folder" "$m" "$brief_current" "$journal" "$state"
96
- pid=$!
97
- started=0
98
- discard=0
99
- start_t="$(date +%s)"
100
- while kill -0 "$pid" 2>/dev/null; do
101
- if session_fatal_error "$pid" "$journal" "$offset" "$m" "$folder"; then discard=1; break; fi
102
- if [ "$(wc -c <"$journal")" -gt "$offset" ]; then
103
- new="$(tail -c "+$(( offset + 1 ))" "$journal" 2>/dev/null || true)"
104
- if [ "$(error_cause "$(error_lines "$new")")" != "quota" ]; then started=1; break; fi
105
- fi
106
- if [ $(( $(date +%s) - start_t )) -ge "$SILENCE" ]; then break; fi
107
- sleep "${DEVZONE_AGENT_STEP:-1}"
108
- done
109
- if [ "$discard" = 1 ]; then resume; i=$(( i + 1 )); continue; fi
110
- if kill -0 "$pid" 2>/dev/null && [ "$started" = 0 ]; then
111
- pkill -P "$pid" 2>/dev/null || true
112
- kill "$pid" 2>/dev/null || true
113
- wait "$pid" 2>/dev/null || true
114
- rights_verify "$folder"
115
- echo "=== opencode/$m silent at start: excluded 1 h, next model" >>"$journal"
116
- model_exclude "$m" mute
117
- model_requested_excluded "$m" mute "$journal"
118
- resume
119
- i=$(( i + 1 ))
120
- continue
121
- fi
122
- # Frozen session: nothing written for FROZEN seconds during the run -> stopped,
123
- # then a new session with the resume brief.
124
- freeze=0
125
- if kill -0 "$pid" 2>/dev/null && [ "$started" = 1 ]; then
126
- last="$(date +%s)"
127
- size="$(wc -c <"$journal")"
128
- while kill -0 "$pid" 2>/dev/null; do
129
- if session_fatal_error "$pid" "$journal" "$offset" "$m" "$folder"; then discard=1; break; fi
130
- sleep "${DEVZONE_AGENT_STEP:-1}"
131
- now="$(date +%s)"
132
- if [ "$(wc -c <"$journal")" -gt "$size" ]; then
133
- size="$(wc -c <"$journal")"
134
- last="$now"
135
- fi
136
- if [ $(( now - last )) -gt "$FROZEN" ]; then
137
- pkill -P "$pid" 2>/dev/null || true
138
- kill "$pid" 2>/dev/null || true
139
- wait "$pid" 2>/dev/null || true
140
- rights_verify "$folder"
141
- freeze=1
142
- break
143
- fi
144
- done
145
- fi
146
- if [ "$discard" = 1 ]; then resume; i=$(( i + 1 )); continue; fi
147
- if [ "$freeze" = 1 ]; then
148
- echo "=== opencode/$m frozen ($FROZEN s without writing): new session, next model" >>"$journal"
149
- resume
150
- i=$(( i + 1 ))
151
- continue
152
- fi
153
- code=0
154
- wait "$pid" || code=$?
155
- rights_verify "$folder"
156
- # Only the output of this attempt counts: an error of a previous model must not
157
- # condemn the next one. Only its last 30 lines are read, and only when the exit
158
- # code is non-zero or opencode wrote its own error line: a printed "Free usage
159
- # limit" in text that ends normally is not a failure.
160
- output="$(tail -c "+$(( offset + 1 ))" "$journal" 2>/dev/null || true)"
161
- recent="$(printf '%s\n' "$output" | tail -n 30)"
162
- if [ "$code" -ne 0 ] || printf '%s\n' "$recent" | grep -qE "$ERROR_LINE"; then
163
- if printf '%s\n' "$recent" | grep -qE "$ERRORS"; then
164
- cause="$(error_cause "$(printf '%s\n' "$recent" | grep -oE "$ERRORS" | tail -1)")"
165
- # A network error is kept only if the network is down now: otherwise it
166
- # would repeat endlessly on a good network.
167
- if [ "$cause" = reseau ] && network_reachable; then
168
- echo "=== network error reported but network reachable: treated as other" >>"$journal"
169
- cause=autre
170
- fi
171
- if [ "$cause" = reseau ] && [ "$network_n" -ge "$NETWORK_RETRIES" ]; then
172
- echo "=== opencode/$m network down $NETWORK_RETRIES times: next model, no exclusion" >>"$journal"
173
- cause=autre
174
- fi
175
- if [ "$cause" = reseau ]; then
176
- if network_wait; then
177
- network_n=$(( network_n + 1 ))
178
- resume
179
- continue
180
- fi
181
- echo "=== network unavailable" >>"$journal"
182
- exit 4
183
- fi
184
- if [ "$cause" = autre ]; then
185
- echo "=== opencode/$m error, no exclusion: next model" >>"$journal"
186
- else
187
- model_exclude "$m" "$cause"
188
- model_requested_excluded "$m" "$cause" "$journal"
189
- echo "=== opencode/$m excluded ($(cause_label "$cause"), $(exclusion_label "$cause")): next model" >>"$journal"
190
- fi
191
- resume
192
- i=$(( i + 1 ))
193
- continue
194
- fi
195
- fi
196
- if [ "$code" -ne 0 ]; then
197
- echo "=== opencode/$m stopped on error (code $code): next model" >>"$journal"
198
- resume
199
- i=$(( i + 1 ))
200
- continue
201
- fi
202
- echo "=== end with opencode/$m (code $code)" >>"$journal"
203
- exit "$code"
204
- done
205
- echo "=== no free model available" >>"$journal"
206
- exit 3
207
- }
208
- case "${1:-}" in
209
- status) agent_pod_status ;;
210
- state) if [ "${2:-}" = "--json" ]; then exec bash "$ICI/agent-state-json.sh"; else agent_state; fi ;;
211
- release)
212
- [ -n "${2:-}" ] || { agent_help >&2; exit 2; }
213
- exclusion_clear "$2"
214
- ;;
215
- -h | --help | help) agent_help ;;
216
- "")
217
- agent_help
218
- exit 2
219
- ;;
220
- *)
221
- if [ "${1:-}" = "--model" ]; then [ -n "${2:-}" ] || { agent_help >&2; exit 2; }; MODEL_LIST="$2"; shift 2; fi
222
- if [ "${1:-}" = "--pod" ]; then
223
- [ -z "$MODEL_LIST" ] || refuse "--model is not supported with --pod"
224
- shift
225
- if [ -z "${1:-}" ] || [ -z "${2:-}" ]; then
226
- agent_help >&2
227
- exit 2
228
- fi
229
- agent_pod_launch "$1" "$2"
230
- fi
231
- local_mode=0
232
- if [ "${1:-}" = "--local" ]; then
233
- local_mode=1
234
- shift
235
- fi
236
- if [ "$#" -lt 2 ]; then
237
- agent_help >&2
238
- exit 2
239
- fi
240
- if [ "$local_mode" = 0 ] && workspace_available; then
241
- code=0
242
- agent_workspace "$1" "$2" || code=$?
243
- exit "$code"
244
- fi
245
- agent_launch "$1" "$2"
246
- ;;
247
- esac