@weave-os/router 0.2.17 → 0.2.19

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.
@@ -16,12 +16,11 @@ not type a Codex slash command.
16
16
  The router carries this id as `Session-Id` on every request and stores it for
17
17
  analytics joins, so it is also the lookup key in the router dashboard.
18
18
 
19
- The Weave Router also installs a `UserPromptSubmit` hook that answers
20
- `$router-session` directly, with no model turn at all — that is the normal path,
21
- and it works whether or not this skill runs.
19
+ This skill needs `CODEX_SESSION_ID` in the environment. If it is unset the script
20
+ exits non-zero rather than reporting an id: the alternative is reading the newest
21
+ rollout transcript, which names a different session whenever two run at once, and
22
+ a wrong id sends someone else's session to support. Report the error as-is instead
23
+ of guessing.
22
24
 
23
- This skill is only the fallback for sessions where the hook is unavailable, and it
24
- needs `CODEX_SESSION_ID` in the environment. If that is unset the script exits
25
- non-zero rather than reporting an id: the alternative is reading the newest rollout
26
- transcript, which names a different session whenever two run at once. Report the
27
- error as-is instead of guessing an id.
25
+ Codex also prints the same id in its own session banner, which is the quickest
26
+ answer when this skill cannot produce one.
@@ -12,6 +12,5 @@ fi
12
12
  # which is a different session whenever two run concurrently -- and a wrong id
13
13
  # silently attributes feedback to someone else's session. Fail instead.
14
14
  echo "session id unavailable: CODEX_SESSION_ID is not set in this environment." >&2
15
- echo "The Weave Router's UserPromptSubmit hook answers \$router-session directly" >&2
16
- echo "from the session id Codex gives it; re-run the installer to enable it." >&2
15
+ echo "Codex prints the same id in its session banner." >&2
17
16
  exit 1
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env bash
2
+ # <!-- weave-router managed codex directive -->
3
+ #
4
+ # Codex UserPromptSubmit hook for the Weave Router's LOCAL config toggles.
5
+ # Codex passes a JSON object on stdin carrying the RAW prompt text, before
6
+ # `$skill` expansion and before any inference.
7
+ #
8
+ # Why this exists. The other directives ($fm, $ufm, $rf, $router-session,
9
+ # a bare $router-models) are router state, and the router answers them itself
10
+ # with no model turn. These four are not: they rewrite config.toml on this
11
+ # machine, which a remote HTTP service cannot do. Left as skills they cost two
12
+ # inference turns each -- one for the model to read the skill and decide to
13
+ # exec, one to report the tool result.
14
+ #
15
+ # The second reason matters more than the cost. $router-off and
16
+ # $disable-routing are what you reach for when the router is slow, broken, or
17
+ # out of credits -- and as skills they need a model turn, which is served
18
+ # THROUGH the router. The off switch depended on the thing it turns off. This
19
+ # hook runs locally before any network call, so it works when the router does
20
+ # not.
21
+ #
22
+ # Scope: only the four local toggles. Everything the router can answer is
23
+ # deliberately absent -- a hook that intercepted those would re-open the
24
+ # duplicate-path problem #1257 closed.
25
+ #
26
+ # FAIL OPEN. Every unexpected condition -- no jq, no npx, a malformed payload,
27
+ # a command that errors -- must let the prompt through untouched. A directive
28
+ # that silently swallows the user's prompt is far worse than one that does not
29
+ # fire.
30
+
31
+ set -uo pipefail
32
+
33
+ # How long the toggle may take before the hook gives up and passes the prompt
34
+ # through. npx resolves from cache after first use; the budget covers a cold
35
+ # fetch without letting a dead network hang the turn indefinitely.
36
+ #
37
+ # Validated, not trusted. `[ "$waited" -ge "$seconds" ]` does not compare a
38
+ # bound it cannot parse -- it errors -- so the cap would never fire and the
39
+ # hook would hang until npx exited on its own. Two ways to get there: a
40
+ # non-numeric value, and a digit-only value too large for the shell's integer
41
+ # type ("[: 99999999999999999999: integer expression expected"). The length
42
+ # bound covers the second; 4 digits is already ~2.7h, far past anything
43
+ # meaningful for a prompt hook.
44
+ #
45
+ # The diagnostic goes to stderr, never stdout: stdout is the hook protocol
46
+ # channel and any stray byte there corrupts the JSON. Codex discards stderr,
47
+ # so this costs a user nothing and tells whoever set the variable why it was
48
+ # ignored.
49
+ WEAVE_TOGGLE_TIMEOUT_DEFAULT=45
50
+ # Resolved into a local first: under `set -u`, ${#WEAVE_TOGGLE_TIMEOUT} on an
51
+ # unset variable aborts the hook outright -- which would swallow every prompt,
52
+ # the one failure this script must never have.
53
+ weave_timeout="${WEAVE_TOGGLE_TIMEOUT:-$WEAVE_TOGGLE_TIMEOUT_DEFAULT}"
54
+ weave_timeout_invalid=""
55
+ case "$weave_timeout" in
56
+ ''|*[!0-9]*|0) weave_timeout_invalid=1 ;;
57
+ *) [ "${#weave_timeout}" -gt 4 ] && weave_timeout_invalid=1 ;;
58
+ esac
59
+ if [ -n "$weave_timeout_invalid" ]; then
60
+ printf 'weave-router: ignoring invalid WEAVE_TOGGLE_TIMEOUT=%s; using %ss\n' \
61
+ "$weave_timeout" "$WEAVE_TOGGLE_TIMEOUT_DEFAULT" >&2
62
+ weave_timeout="$WEAVE_TOGGLE_TIMEOUT_DEFAULT"
63
+ fi
64
+ WEAVE_TOGGLE_TIMEOUT="$weave_timeout"
65
+
66
+ # ---------- responses ----------
67
+
68
+ # pass_through hands the prompt to Codex unchanged. Every failure path ends here.
69
+ pass_through() {
70
+ printf '{"continue":true}\n'
71
+ exit 0
72
+ }
73
+
74
+ # block stops the turn before any inference and shows the text to the user.
75
+ # Codex renders `reason`; nothing reaches the model.
76
+ #
77
+ # Setting only `decision` is deliberate: `continue:false` alongside it wins and
78
+ # renders a bare "Hook stopped" with the reason discarded.
79
+ block() {
80
+ local reason="$1" payload
81
+ payload="$(jq -cn --arg r "$reason" '{decision:"block", reason:$r}' 2>/dev/null)" \
82
+ || pass_through
83
+ printf '%s\n' "$payload"
84
+ exit 0
85
+ }
86
+
87
+ command -v jq >/dev/null 2>&1 || pass_through
88
+
89
+ payload="$(cat)" || pass_through
90
+ [ -n "$payload" ] || pass_through
91
+
92
+ prompt="$(jq -r '.prompt // ""' <<<"$payload" 2>/dev/null)" || pass_through
93
+
94
+ # Only a directive at the very start of the prompt is ours. Prose that merely
95
+ # mentions `$router-off` in a sentence is the user's text, not a command.
96
+ case "$prompt" in
97
+ '$'*) ;;
98
+ *) pass_through ;;
99
+ esac
100
+
101
+ verb="${prompt#$}"
102
+ # These four take no arguments. A prompt with a tail is not one of ours --
103
+ # pass it through so the skill can explain itself rather than the hook eating
104
+ # a message it cannot honour.
105
+ case "$verb" in
106
+ *[[:space:]]*) pass_through ;;
107
+ esac
108
+
109
+ # ---------- directive table ----------
110
+ #
111
+ # Local-config toggles only. router-on/off/status flip this install's
112
+ # config.toml; disable-routing is `off` plus the reversal hint below.
113
+ case "$verb" in
114
+ router-on) toggle="on" ;;
115
+ router-off) toggle="off" ;;
116
+ router-status) toggle="status" ;;
117
+ disable-routing) toggle="off" ;;
118
+ *) pass_through ;;
119
+ esac
120
+
121
+ command -v npx >/dev/null 2>&1 || pass_through
122
+
123
+ # ---------- run ----------
124
+
125
+ # WEAVE_TOGGLE_TIMED_OUT is set by run_bounded instead of encoding the timeout
126
+ # in the exit status. 124 is a status the child can legitimately return, and
127
+ # reading that as "we timed out" would swallow the toggle's own error output.
128
+ WEAVE_TOGGLE_TIMED_OUT=0
129
+
130
+ # run_bounded runs a command with a wall-clock cap. GNU `timeout` is not on
131
+ # macOS, so this polls the child rather than depending on coreutils.
132
+ #
133
+ # The child runs in its own process group (`set -m` + `kill -- -PID`) because
134
+ # npx execs node: signalling only the direct child leaves that node process
135
+ # running after the hook has already passed the prompt through.
136
+ run_bounded() {
137
+ local seconds="$1" out="$2"; shift 2
138
+ WEAVE_TOGGLE_TIMED_OUT=0
139
+
140
+ local had_monitor=0
141
+ case "$-" in *m*) had_monitor=1 ;; esac
142
+ set -m
143
+ "$@" >"$out" 2>&1 &
144
+ local pid=$!
145
+ [ "$had_monitor" -eq 1 ] || set +m
146
+
147
+ local waited=0
148
+ while kill -0 "$pid" 2>/dev/null; do
149
+ if [ "$waited" -ge "$seconds" ]; then
150
+ WEAVE_TOGGLE_TIMED_OUT=1
151
+ # Negative pid = the whole group. Fall back to the bare pid if the
152
+ # group is already gone, so a raced exit still gets reaped.
153
+ kill -TERM -- "-$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null
154
+ sleep 1
155
+ kill -KILL -- "-$pid" 2>/dev/null || kill -KILL "$pid" 2>/dev/null
156
+ wait "$pid" 2>/dev/null
157
+ return 0
158
+ fi
159
+ sleep 1
160
+ waited=$((waited + 1))
161
+ done
162
+ wait "$pid"
163
+ }
164
+
165
+ out_file="$(mktemp -t weave-codex-toggle.XXXXXX)" || pass_through
166
+ # shellcheck disable=SC2064 # expand out_file now; it never changes after this
167
+ trap "rm -f '$out_file'" EXIT
168
+
169
+ # {{SCOPE}} is substituted at install time by the only code that knows whether
170
+ # this install is user, project, or --dir scoped. It becomes literal argument
171
+ # text (the installer printf %q-quotes any path), so there is nothing here to
172
+ # word-split at runtime -- the braces are a template token, not an expansion.
173
+ # shellcheck disable=SC1083
174
+ run_bounded "$WEAVE_TOGGLE_TIMEOUT" "$out_file" \
175
+ npx --package @weave-os/router -y -- weave-router "$toggle" --codex{{SCOPE}}
176
+ status=$?
177
+
178
+ output="$(cat "$out_file" 2>/dev/null || true)"
179
+ # Strip ANSI colour the installer emits when it thinks it has a terminal;
180
+ # Codex renders `reason` as plain text.
181
+ output="$(printf '%s' "$output" | sed -e 's/\x1b\[[0-9;]*m//g')"
182
+
183
+ # A failure is reported, not swallowed: passing through here would hand the
184
+ # model a prompt the user meant as a command, and it would likely try the same
185
+ # command again. Only a real timeout passes through -- the toggle never got to
186
+ # finish, so the skill path is still the honest fallback.
187
+ if [ "$WEAVE_TOGGLE_TIMED_OUT" -eq 1 ]; then
188
+ pass_through
189
+ fi
190
+
191
+ [ -n "$output" ] || output="weave-router $toggle --codex exited with status $status."
192
+
193
+ # Both trailers describe a change that actually happened, so they are gated on
194
+ # a zero exit. A failed toggle rewrote no config; telling the user it takes
195
+ # effect next launch would send them to restart Codex for nothing.
196
+ if [ "$status" -eq 0 ] && [ "$verb" != "router-status" ]; then
197
+ output="$output"$'\n\n'"Takes effect on your next \`codex\` launch: Codex reads its provider config at startup, so this session keeps routing as it already was."
198
+ fi
199
+ if [ "$status" -eq 0 ] && [ "$verb" = "disable-routing" ]; then
200
+ output="$output"$'\n'"Reverse it with \$router-on."
201
+ fi
202
+
203
+ block "$output"