devzone 7.2.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,242 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Session of the agent supervisor: opencode configuration, shared rank, journal,
3
- # state (#328). Library sourced by agent.sh: no command is launched here.
4
-
5
- # Journal path of a folder in workspace/.agents/: outside the repo, ignored by git.
6
- journal_of() {
7
- echo "$RACINE/workspace/.agents/$(basename "$1").log"
8
- }
9
-
10
- # Last write time (epoch), GNU (stat -c) or BSD/macOS (stat -f).
11
- mtime() {
12
- if stat -c %Y "$1" >/dev/null 2>&1; then
13
- stat -c %Y "$1"
14
- else
15
- stat -f %m "$1"
16
- fi
17
- }
18
-
19
- # Readable UTC date of an epoch, GNU then BSD date: shown by devzone agent state.
20
- utc_time() {
21
- if date -u -d "@$1" '+%Y-%m-%d %H:%M:%S' >/dev/null 2>&1; then
22
- date -u -d "@$1" '+%Y-%m-%d %H:%M:%S'
23
- else
24
- date -u -r "$1" '+%Y-%m-%d %H:%M:%S'
25
- fi
26
- }
27
-
28
- # Lock trace in the agent journal ($AGENT_JOURNAL, set by agent.sh): one line when
29
- # taken and one when released, with the clock and the pid, to diagnose simultaneous
30
- # launches of the rank in CI (#421). Without a journal, nothing is written. An
31
- # optional mention (second argument) gives the path, the age or the pid read.
32
- lock_trace() {
33
- local d
34
- [ -n "${AGENT_JOURNAL:-}" ] || return 0
35
- d="$(date +%s.%N 2>/dev/null)" || d=""
36
- case "$d" in "" | *N*) d="$(date +%s)" ;; esac
37
- echo "=== lock $1 $d pid $$${2:+ $2}" >>"$AGENT_JOURNAL"
38
- }
39
-
40
- # Shared rank lock: two simultaneous launches must not start on the same model.
41
- # mkdir is atomic and present everywhere (flock is missing on macOS); only the age
42
- # of the lock decides (#377), the pid written being a diagnostic detail only: a pid
43
- # read from another container of the volume does not exist here.
44
- # When flock exists, the rank section is also taken under its lock on rang.lock
45
- # (#421): if two mkdir succeeded, flock still serializes the read and the write of
46
- # the rank. Each branch is traced (mkdir failure, purge, taking) to establish in CI
47
- # which of the two launches succeeded in mkdir.
48
- lock_take() {
49
- local end age lock_pid
50
- end=$(( $(date +%s) + ${DEVZONE_AGENT_LOCK_MAX:-10} ))
51
- while ! mkdir "$AGENT_STATE/lock" 2>/dev/null; do
52
- # A lock is orphaned only by its age: the rank is held for a few milliseconds,
53
- # 30 s can only be a stop to take over; otherwise wait, then give up (code 1).
54
- age=""
55
- if [ -d "$AGENT_STATE/lock" ]; then
56
- age="$(mtime "$AGENT_STATE/lock" 2>/dev/null)" || age=""
57
- fi
58
- lock_pid="$(cat "$AGENT_STATE/lock/pid" 2>/dev/null)" || lock_pid=""
59
- lock_trace "mkdir failed" "age=${age:-unknown} lock-pid=${lock_pid:-none}"
60
- if [ -n "$age" ] && [ "$(( $(date +%s) - age ))" -gt 30 ]; then
61
- rm -f "$AGENT_STATE/lock/pid"
62
- if rmdir "$AGENT_STATE/lock" 2>/dev/null; then
63
- lock_trace "purged" "age=$age"
64
- continue
65
- fi
66
- fi
67
- if [ "$(date +%s)" -ge "$end" ]; then return 1; fi
68
- sleep 1
69
- done
70
- # pid before flock: the other agent never removes the folder while we wait on
71
- # rang.lock, otherwise our pid write would fail with ENOENT.
72
- echo "$$" >"$AGENT_STATE/lock/pid"
73
- if command -v flock >/dev/null 2>&1; then
74
- # exec in an if makes a failure a condition instead of killing the shell
75
- # (bash 3.2 and GNU alike); without flock, mkdir remains the only guard (macOS).
76
- if { exec 9>"$AGENT_STATE/rang.lock"; } && flock -w "${DEVZONE_AGENT_LOCK_MAX:-10}" 9; then
77
- AGENT_LOCK_FLOCK=1
78
- else
79
- lock_release
80
- return 1
81
- fi
82
- fi
83
- lock_trace taken "path=$AGENT_STATE/lock"
84
- }
85
-
86
- # Releases the shared rank taken by lock_take: removes the pid, then the folder, only
87
- # when this lock belongs to the calling agent (pid in the file == $$). A lock held by
88
- # another agent is never removed here. flock (#421) is released in every case, even
89
- # when the pid read does not match: otherwise the next caller would stay blocked on
90
- # rang.lock.
91
- lock_release() {
92
- local pid
93
- pid="$(cat "$AGENT_STATE/lock/pid" 2>/dev/null)" || pid=""
94
- if [ "$pid" = "$$" ]; then
95
- rm -f "$AGENT_STATE/lock/pid"
96
- rmdir "$AGENT_STATE/lock" 2>/dev/null || true
97
- lock_trace released
98
- fi
99
- if [ "${AGENT_LOCK_FLOCK:-0}" = 1 ]; then
100
- flock -u 9 2>/dev/null || true
101
- exec 9>&-
102
- AGENT_LOCK_FLOCK=0
103
- fi
104
- }
105
-
106
- # Takes the shared rank and returns the start of the rotation (#421). The rank
107
- # advances only when no requested model is available (#420). The "=== rank" line is
108
- # written to the journal in every case. Called in a command substitution: the lock is
109
- # taken and released in that same process.
110
- rank_take() {
111
- local state="$1" journal="$2" start
112
- lock_take || return 1
113
- start=$(( $(cat "$state/rang" 2>/dev/null || echo 0) % ${#FREE_MODELS[@]} ))
114
- if [ "$PAUSE_RANK" -gt 0 ]; then sleep "$PAUSE_RANK"; fi
115
- if model_rank_advance; then echo $(( start + 1 )) >"$state/rang"; fi
116
- lock_release
117
- echo "=== rank $(( start + 1 ))/${#FREE_MODELS[@]}" >>"$journal"
118
- echo "$start"
119
- }
120
-
121
- # Turns permission.edit into an object and adds, last, one deny per rights file (#361):
122
- # the last matching rule wins, so the caller cannot remove it.
123
- rights_edit_deny() {
124
- local patterns f
125
- patterns="[]"
126
- for f in "${RIGHTS_FILES[@]}"; do
127
- patterns="$(jq -c --arg f "$f" '. + [$f, "**/" + $f]' <<<"$patterns")"
128
- done
129
- jq -ce --argjson patterns "$patterns" '
130
- (.permission.edit // "allow") as $e |
131
- (if ($e | type) == "object" then $e else {"*": $e} end) as $o |
132
- .permission.edit = (reduce $patterns[] as $k ($o; del(.[$k])) + ($patterns | map({(.): "deny"}) | add))
133
- ' <<<"$1"
134
- }
135
-
136
- # Safe configuration: agent.json by default, completed by OPENCODE_CONFIG_CONTENT when
137
- # it is set. opencode applies the last matching rule: devzone's deny entries are taken
138
- # out of the merge, then added back at the end (the key order of a jq object is the
139
- # insertion order), so the caller can add permissions but never remove a devzone deny.
140
- # ~/bash32 is always protected: the container's bash 3.2 was already replaced by a
141
- # link (#210). Also called by agent-rights-gh.bats.
142
- configure() {
143
- local def config
144
- def="$ICI/opencode/agent.json"
145
- [ -r "$def" ] || refuse "default configuration missing: $def"
146
- if [ -n "${OPENCODE_CONFIG_CONTENT:-}" ]; then
147
- config="$(jq -ce -s '
148
- .[0] as $def | .[1] as $env |
149
- ($def.permission.bash // {}) as $bashdef |
150
- ($env.permission.bash // {}) as $bashenv |
151
- ([ $bashdef | to_entries[] | select(.value == "deny") | .key ]) as $deny |
152
- (($def * $env)
153
- | .permission.bash = (($bashdef + $bashenv) | delpaths($deny | map([.])))
154
- | reduce $deny[] as $k (. ; .permission.bash[$k] = "deny"))
155
- ' "$def" <(printf '%s' "$OPENCODE_CONFIG_CONTENT"))" ||
156
- refuse "opencode configuration unreadable: nothing launched"
157
- else
158
- config="$(jq -ce . "$def")" || refuse "opencode configuration unreadable: nothing launched"
159
- fi
160
- config="$(jq -ce '.permission.bash = ((.permission.bash // {}) + {
161
- "git commit*": "allow",
162
- "ln *bash32*": "deny",
163
- "cp *bash32*": "deny",
164
- "mv *bash32*": "deny",
165
- "rm *bash32*": "deny",
166
- "chmod *bash32*": "deny",
167
- "install *bash32*": "deny"
168
- })' <<<"$config")" || refuse "opencode configuration unreadable: nothing launched"
169
- config="$(rights_edit_deny "$config")" || refuse "opencode configuration unreadable: nothing launched"
170
- grep -q '"git push\*":"deny"' <<<"$config" || refuse "git push not denied: nothing launched"
171
- export OPENCODE_CONFIG_CONTENT="$config"
172
- }
173
-
174
- # Free models of the installed version, "opencode/" prefix removed:
175
- # "opencode models opencode" may return "opencode/big-pickle".
176
- list_models() {
177
- local l
178
- l="$(opencode models opencode 2>/dev/null | sed 's#^opencode/##' |
179
- grep -E '(-free$|^big-pickle$)' | awk 'NF && !seen[$0]++' || true)"
180
- if [ -n "$l" ]; then
181
- printf '%s\n' "$l"
182
- else
183
- printf '%s\n' "${FREE_MODELS[@]}"
184
- fi
185
- }
186
-
187
- # Model and start of a session (#308): read only on the last "=== model opencode/"
188
- # line of the supervisor; text written by the model in the journal (a possible
189
- # "opencode/agent.json") is not read.
190
- session_model_start() {
191
- local line
192
- line="$(grep '^=== model opencode/' "$1" 2>/dev/null | tail -1 || true)"
193
- MODEL_SESSION="$(sed -nE 's/^=== model opencode\/([^ ]+) start .*/\1/p' <<<"$line")"
194
- START_SESSION="$(sed -nE 's/.* start ([^ ]+).*/\1/p' <<<"$line")"
195
- }
196
-
197
- # Models asked with devzone agent --model (#420): the last header line
198
- # "=== --model : " of the journal, empty when nothing was asked.
199
- session_request() {
200
- local line
201
- line="$(grep '^=== --model : ' "$1" 2>/dev/null | tail -1 || true)"
202
- REQUEST_SESSION="${line#=== --model : }"
203
- if [ "$REQUEST_SESSION" = none ]; then REQUEST_SESSION=""; fi
204
- }
205
-
206
- # A session is running (#308) when a live agent.sh process has as first argument (the
207
- # folder, absolute since #287) a path whose name is exactly the journal without ".log";
208
- # a plain folder containing that name is not enough, nor a path that is not an agent
209
- # folder.
210
- session_running() {
211
- local line folder
212
- while IFS= read -r line; do
213
- case "$line" in
214
- *agent.sh\ *) folder="${line#*agent.sh }" ;;
215
- *) continue ;;
216
- esac
217
- folder="${folder%% *}"
218
- [ -n "$folder" ] || continue
219
- if [ "$(basename "$folder")" = "$1" ]; then return 0; fi
220
- done < <(ps -eo args= 2>/dev/null || true)
221
- return 1
222
- }
223
-
224
- # Known sessions (journal, last model, start, last written) and models excluded
225
- # by the supervisor, with the end time of each exclusion.
226
- agent_state() {
227
- local f m start last seen=0
228
- echo "devzone agent: state"
229
- for f in "$RACINE"/workspace/.agents/*.log; do
230
- [ -e "$f" ] || continue
231
- seen=1
232
- session_model_start "$f"
233
- m="${MODEL_SESSION:+opencode/$MODEL_SESSION}"
234
- start="$START_SESSION"
235
- last="$(utc_time "$(mtime "$f")")"
236
- session_request "$f"
237
- echo " session $(basename "$f"): model ${m:-unknown}${REQUEST_SESSION:+ (requested: $REQUEST_SESSION)}, since ${start:-unknown}, last written $last"
238
- done
239
- [ "$seen" = 1 ] || echo " no session"
240
- agent_exclusions
241
- rights_alerts
242
- }
@@ -1,117 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Machine output of devzone agent state (--json): sessions, exclusions, rights alerts.
3
- # Public format, schema 2: the keys are English names; the values stored in the
4
- # state files (causes such as pays, quota, muet) keep their stored names.
5
- set -euo pipefail
6
-
7
- SOURCE="${BASH_SOURCE[0]}"
8
- while [ -L "$SOURCE" ]; do
9
- DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
10
- SOURCE="$(readlink "$SOURCE")"
11
- case "$SOURCE" in
12
- /*) ;;
13
- *) SOURCE="$DIR/$SOURCE" ;;
14
- esac
15
- done
16
- ICI="$(cd -P "$(dirname "$SOURCE")" && pwd)"
17
- RACINE="${DEVZONE_ROOT:-$(dirname "$ICI")}"
18
-
19
- # shellcheck source=agent-errors.sh
20
- # shellcheck disable=SC1091
21
- . "$ICI/agent-errors.sh"
22
- # shellcheck source=agent-rights.sh
23
- # shellcheck disable=SC1091
24
- . "$ICI/agent-rights.sh"
25
- # shellcheck source=agent-session.sh
26
- # shellcheck disable=SC1091
27
- . "$ICI/agent-session.sh"
28
-
29
- AGENT_STATE="${DEVZONE_AGENT_STATE:-$HOME/.local/state/oc-lancer}"
30
-
31
- # UTC ISO-8601 date of an epoch, GNU then BSD date: the end field of the exclusions.
32
- iso() {
33
- if date -u -d "@$1" '+%Y-%m-%dT%H:%M:%SZ' >/dev/null 2>&1; then
34
- date -u -d "@$1" '+%Y-%m-%dT%H:%M:%SZ'
35
- else
36
- date -u -r "$1" '+%Y-%m-%dT%H:%M:%SZ'
37
- fi
38
- }
39
-
40
- # Agent state in JSON: reuses the data of agent_state, agent_exclusions and
41
- # rights_alerts, without copying their logic.
42
- agent_state_json() {
43
- local sessions_json='[]' exclusions_json='[]' alerts_json='[]'
44
- local f journal model start last project running name
45
- local e cause d end end_iso now
46
-
47
- for f in "$RACINE"/workspace/.agents/*.log; do
48
- [ -e "$f" ] || continue
49
- journal="$(basename "$f")"
50
- session_model_start "$f"
51
- session_request "$f"
52
- model="$MODEL_SESSION"
53
- [ -n "$model" ] || model="unknown"
54
- start="$START_SESSION"
55
- last="$(utc_time "$(mtime "$f")")"
56
- project="${journal%.log}"
57
- running=false
58
- if session_running "$project"; then running=true; fi
59
- sessions_json="$(jq -c \
60
- --arg journal "$journal" \
61
- --arg model "$model" \
62
- --arg start "$start" \
63
- --arg last "$last" \
64
- --arg request "$REQUEST_SESSION" \
65
- --argjson running "$running" \
66
- '. + [{journal:$journal,model:$model,start:(if $start=="" then null else $start end),last_written:(if $last=="" then null else $last end),requested:(if $request=="" then null else $request end),running:$running}]' \
67
- <<<"$sessions_json")"
68
- done
69
-
70
- now="$(date +%s)"
71
- for f in "$AGENT_STATE"/ecart-*; do
72
- [ -e "$f" ] || continue
73
- model="${f##*/ecart-}"
74
- read -r e cause d <<<"$(cat "$f" 2>/dev/null || true)"
75
- [ -n "$e" ] || continue
76
- end=$(( e + ${d:-3600} ))
77
- [ "$now" -lt "$end" ] || continue
78
- end_iso="$(iso "$end")"
79
- exclusions_json="$(jq -c \
80
- --arg model "$model" \
81
- --arg cause "${cause:-autre}" \
82
- --arg end "$end_iso" \
83
- '. + [{model:$model,cause:$cause,end:$end}]' \
84
- <<<"$exclusions_json")"
85
- done
86
- for f in "$AGENT_STATE"/muet-*; do
87
- [ -e "$f" ] || continue
88
- model="${f##*/muet-}"
89
- e="$(cat "$f" 2>/dev/null || true)"
90
- [ -n "$e" ] || continue
91
- end=$(( e + 3600 ))
92
- [ "$now" -lt "$end" ] || continue
93
- end_iso="$(iso "$end")"
94
- exclusions_json="$(jq -c \
95
- --arg model "$model" \
96
- --arg cause "muet" \
97
- --arg end "$end_iso" \
98
- '. + [{model:$model,cause:$cause,end:$end}]' \
99
- <<<"$exclusions_json")"
100
- done
101
-
102
- for f in "$AGENT_STATE"/droits-*.alerte; do
103
- [ -e "$f" ] || continue
104
- name="${f##*/droits-}"
105
- name="${name%.alerte}"
106
- alerts_json="$(jq -c --arg n "$name" '. + [$n]' <<<"$alerts_json")"
107
- done
108
-
109
- jq -n \
110
- --argjson schema 2 \
111
- --argjson sessions "$sessions_json" \
112
- --argjson exclusions "$exclusions_json" \
113
- --argjson alerts "$alerts_json" \
114
- '{schema:$schema,sessions:$sessions,exclusions:$exclusions,rights_alerts:$alerts}'
115
- }
116
-
117
- agent_state_json
@@ -1,97 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Why: outside the pod, devzone agent launches opencode in the Kubernetes workspace
3
- # already started (projects and journals of the pod), or locally with --local; the
4
- # journal of the pod is brought back into that of the workstation (#431).
5
- # Library sourced by agent.sh: no command is launched here.
6
-
7
- # True in the workspace container of the cluster, which carries the chart marker.
8
- agent_in_pod() {
9
- [ "${DEVZONE_WORKSPACE:-}" = kubernetes ]
10
- }
11
-
12
- # kubectl of the workstation: devzone context if it exists, then the workspace namespace.
13
- agent_kc() {
14
- if kubectl config get-contexts -o name 2>/dev/null | grep -qx devzone; then
15
- kubectl --context devzone --namespace devzone-workspace "$@"
16
- else
17
- kubectl --namespace devzone-workspace "$@"
18
- fi
19
- }
20
-
21
- # Why: launching opencode is common to each model attempt, and the kubeconfig
22
- # preparation depends on the Kubernetes workspace; this block lives here with the rest
23
- # of the pod (#431), agent.sh only keeps the model loop.
24
- opencode_launch() {
25
- local folder="$1" m="$2" brief="$3" journal="$4" state="$5"
26
- local agent_kube="$state/kubeconfig"
27
- if agent_in_pod; then
28
- # In the pod, the limited service account is the only access: no workstation
29
- # kubeconfig enters the environment of opencode.
30
- (cd "$folder" && env -u KUBECONFIG DEVZONE_AGENT_SESSION=1 opencode run --print-logs --log-level ERROR --auto -m "$(model_ref "$m")" "$(cat "$brief")" </dev/null >>"$journal" 2>&1) &
31
- else
32
- if [ -f "$HOME/.kube/config" ]; then
33
- (umask 077; KUBECONFIG="$HOME/.kube/config" kubectl config view --minify --flatten --context devzone >"$agent_kube") 2>/dev/null || : >"$agent_kube"
34
- chmod 600 "$agent_kube"
35
- [ -s "$agent_kube" ] || echo "=== kubernetes: no devzone context, session without access" >>"$journal"
36
- else
37
- : >"$agent_kube"
38
- fi
39
- (cd "$folder" && KUBECONFIG="$agent_kube" DEVZONE_AGENT_SESSION=1 opencode run --print-logs --log-level ERROR --auto -m "$(model_ref "$m")" "$(cat "$brief")" </dev/null >>"$journal" 2>&1) &
40
- fi
41
- }
42
-
43
- # True when the pod of the workspace runs and the command comes from the workstation.
44
- workspace_available() {
45
- if agent_in_pod; then return 1; fi
46
- local phase
47
- phase="$(agent_kc get pod devzone-workspace-0 -o jsonpath='{.status.phase}' --request-timeout=5s 2>/dev/null)" || return 1
48
- [ "$phase" = Running ]
49
- }
50
-
51
- # Runs the brief in the pod, then brings its journal back even on failure.
52
- # A /workspaces/projets/... path absent from here designates a folder of the pod
53
- # (clone or worktree of devzone ticket, #558): nothing is copied.
54
- agent_workspace() {
55
- local folder="$1" brief="$2" name journal code=0 in_pod=""
56
- local pod_root=/home/vscode/.local/share/devzone
57
- case "$folder" in
58
- /workspaces/projets/*) [ -d "$folder" ] || in_pod="$folder" ;;
59
- esac
60
- if [ -n "$in_pod" ]; then
61
- name="$(basename "$in_pod")"
62
- if ! agent_kc exec devzone-workspace-0 -c workspace -- test -d "$in_pod" 2>/dev/null; then
63
- refuse "folder not found in the pod: $in_pod (devzone workspace clone, then devzone ticket <n> in the pod)"
64
- fi
65
- else
66
- [ -d "$folder" ] || refuse "folder not found: $folder (searched from $(pwd))"
67
- name="$(basename "$(cd "$folder" && pwd -P)")"
68
- in_pod="/workspaces/projets/$name"
69
- if ! agent_kc exec devzone-workspace-0 -c workspace -- test -d "$in_pod" 2>/dev/null; then
70
- refuse "project $name missing from the Kubernetes workspace: devzone workspace copy-to $folder (on this machine), or --local"
71
- fi
72
- fi
73
- echo "devzone agent: Kubernetes workspace (pod devzone-workspace-0, $in_pod); --local for the container"
74
- agent_kc cp "$brief" "devzone-workspace-0:/tmp/devzone-agent-$name.md" -c workspace
75
- # shellcheck disable=SC2016 # the pod shell expands MODEL_OPT, not this one
76
- agent_kc exec devzone-workspace-0 -c workspace -- env DEVZONE_ROOT="$pod_root" MODEL_OPT="$MODEL_LIST" bash -lc 'devzone agent ${MODEL_OPT:+--model "$MODEL_OPT"} "$1" "$2"' _ "$in_pod" "/tmp/devzone-agent-$name.md" || code=$?
77
- journal="$(journal_of "$name")"
78
- mkdir -p "$(dirname "$journal")"
79
- if ! agent_kc exec devzone-workspace-0 -c workspace -- cat "$pod_root/workspace/.agents/$name.log" >>"$journal"; then
80
- echo "=== pod journal not found" >>"$journal"
81
- fi
82
- return "$code"
83
- }
84
-
85
- agent_help() {
86
- cat <<'AGENTEOF2'
87
- usage: devzone agent [--model <m>[,<m2>…]] [--local] <folder> <brief-file>
88
- runs opencode on the brief, free models in rotation
89
- --model: models tried first, in order, then the rotation
90
- Kubernetes workspace first if it runs; --local: this container
91
- a /workspaces/projets/... path absent from here is a folder of the pod, without copy
92
- devzone agent state [--json]
93
- known sessions and excluded models
94
- devzone agent release [<model>|--all]
95
- lifts a model exclusion
96
- AGENTEOF2
97
- }