@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.
- package/codex-skills/router-session/SKILL.md +7 -8
- package/codex-skills/router-session/scripts/emit.sh +1 -2
- package/codex-toggle.sh +203 -0
- package/install.sh +412 -225
- package/package.json +2 -2
- package/uninstall.sh +146 -4
- package/codex-directive.sh +0 -236
package/install.sh
CHANGED
|
@@ -483,6 +483,152 @@ resolve_user_email() {
|
|
|
483
483
|
esac
|
|
484
484
|
}
|
|
485
485
|
|
|
486
|
+
# strip_weave_codex_hooks rewrites config.toml without any hook registration
|
|
487
|
+
# this installer owns, matched by the helper filename rather than by position.
|
|
488
|
+
#
|
|
489
|
+
# Two reasons it cannot key on the managed markers. Codex rewrites config.toml
|
|
490
|
+
# through a TOML serializer when it persists its own state, so our registrations
|
|
491
|
+
# routinely end up outside them — a later install then appends a fresh copy and
|
|
492
|
+
# the helper runs twice per event. And the retired directive hook has to be
|
|
493
|
+
# removed from configs that already carry it, wherever it now sits.
|
|
494
|
+
#
|
|
495
|
+
# Removal is per sub-entry, not per group: a [[hooks.Stop]] group can hold our
|
|
496
|
+
# hook and a third party's, and dropping the group would take theirs with it.
|
|
497
|
+
# The group header goes only once nothing is left inside it.
|
|
498
|
+
# Ownership contract for the Codex hook helpers. Every rule here was a bug
|
|
499
|
+
# first; keep them together so the next change has the whole set.
|
|
500
|
+
#
|
|
501
|
+
# 1. Ours is decided per path by that helper's EXACT marker. A prefix match
|
|
502
|
+
# lets the status marker vouch for the directive path, and the two
|
|
503
|
+
# deletion sites check exact markers -- so the strip would unwire a file
|
|
504
|
+
# it then declines to delete.
|
|
505
|
+
# 2. A missing file is ours. Its registration points at nothing, which is
|
|
506
|
+
# residue we left behind.
|
|
507
|
+
# 3. What a symlink means depends on the helper's lifecycle, so the caller
|
|
508
|
+
# says which it is.
|
|
509
|
+
# rewrite (status): the file is rewritten in place and never deleted, so
|
|
510
|
+
# the link is followed to read the marker. Disowning it instead leaves
|
|
511
|
+
# the old registrations while the managed block adds a fresh pair,
|
|
512
|
+
# stacking the duplicates this strip exists to remove.
|
|
513
|
+
# delete (directive): the file is removed, and removal refuses to act
|
|
514
|
+
# through a link. Claiming one would strip the registration and then
|
|
515
|
+
# decline the delete, leaving a helper Codex no longer runs -- rule 4
|
|
516
|
+
# violated in the one case it exists for.
|
|
517
|
+
# Either way the check precedes -e, which is false for a dangling link: it
|
|
518
|
+
# then fails the marker test and stays the user's, rather than being
|
|
519
|
+
# claimed by rule 2.
|
|
520
|
+
# 4. Registration and file must be decided together. Removing one while
|
|
521
|
+
# keeping the other leaves a script Codex never runs, with nothing on
|
|
522
|
+
# screen to explain it.
|
|
523
|
+
# 5. Compare decoded paths. The command value is a TOML basic string and the
|
|
524
|
+
# writer escapes it, so a raw comparison misses any path with a quote or
|
|
525
|
+
# backslash.
|
|
526
|
+
# warn_if_symlink reports a symlinked path and returns non-zero so the caller
|
|
527
|
+
# can skip just that file. refuse_if_symlink exits the process, which is right
|
|
528
|
+
# for a path we are about to write but wrong for these helpers: one symlinked
|
|
529
|
+
# helper would abort the whole run, stranding the config rewrite that is the
|
|
530
|
+
# actual work. Neither is deleted through a link either way.
|
|
531
|
+
warn_if_symlink() {
|
|
532
|
+
local target="$1"
|
|
533
|
+
[ -L "$target" ] || return 0
|
|
534
|
+
warn "$target is a symlink (-> $(readlink "$target")); leaving it untouched."
|
|
535
|
+
return 1
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
weave_owns_codex_helper() {
|
|
539
|
+
local path="$1" marker="$2" lifecycle="$3"
|
|
540
|
+
[ -n "$path" ] && [ -n "$marker" ] && [ -n "$lifecycle" ] || return 1
|
|
541
|
+
if [ -L "$path" ]; then
|
|
542
|
+
[ "$lifecycle" = "rewrite" ] || return 1
|
|
543
|
+
grep -Fq "$marker" "$path" 2>/dev/null
|
|
544
|
+
return $?
|
|
545
|
+
fi
|
|
546
|
+
[ -e "$path" ] || return 0
|
|
547
|
+
grep -Fq "$marker" "$path" 2>/dev/null
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
# weave_owned_codex_helpers prints, one per line, whichever of the two helper
|
|
551
|
+
# paths this installation may unwire.
|
|
552
|
+
weave_owned_codex_helpers() {
|
|
553
|
+
weave_owns_codex_helper "${codex_status_file:-}" '<!-- weave-router managed codex status -->' rewrite \
|
|
554
|
+
&& printf '%s\n' "$codex_status_file"
|
|
555
|
+
# delete, not rewrite: unlike the status helper, a symlink at the directive
|
|
556
|
+
# path is never ours even when its target carries the marker. Classifying it
|
|
557
|
+
# rewrite-owned made a reinstall follow the link and overwrite whatever it
|
|
558
|
+
# pointed at. install_codex_directive_script skips symlinks for the same
|
|
559
|
+
# reason, so the two stay consistent.
|
|
560
|
+
weave_owns_codex_helper "${codex_directive_file:-}" '<!-- weave-router managed codex directive -->' delete \
|
|
561
|
+
&& printf '%s\n' "$codex_directive_file"
|
|
562
|
+
return 0
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
strip_weave_codex_hooks() {
|
|
566
|
+
local config_file="$1"; shift
|
|
567
|
+
[ -f "$config_file" ] || return 0
|
|
568
|
+
# Exact paths, never a filename pattern: a third party's hook is free to be
|
|
569
|
+
# called weave-status.sh, and matching on the basename would delete it.
|
|
570
|
+
local owned="" candidate
|
|
571
|
+
for candidate in "$@"; do
|
|
572
|
+
[ -n "$candidate" ] || continue
|
|
573
|
+
# A unit separator, not a newline: BSD awk rejects an embedded newline in a
|
|
574
|
+
# -v value outright ("newline in string"), which silently disabled the whole
|
|
575
|
+
# pass on macOS.
|
|
576
|
+
owned="${owned}${candidate}$(printf '\037')"
|
|
577
|
+
done
|
|
578
|
+
[ -n "$owned" ] || return 0
|
|
579
|
+
local tmp; tmp="$(mktemp -t weave-codex-hooks.XXXXXX)"
|
|
580
|
+
awk -v owned="$owned" '
|
|
581
|
+
BEGIN {
|
|
582
|
+
n = split(owned, list, "\037")
|
|
583
|
+
for (i = 1; i <= n; i++) if (list[i] != "") own[list[i]] = 1
|
|
584
|
+
}
|
|
585
|
+
function flush_sub( i) {
|
|
586
|
+
if (sub_n == 0) return
|
|
587
|
+
if (!sub_is_weave) {
|
|
588
|
+
if (group_pending != "") {
|
|
589
|
+
print group_pending
|
|
590
|
+
group_pending = ""
|
|
591
|
+
if (group_blank) { print ""; group_blank = 0 }
|
|
592
|
+
}
|
|
593
|
+
for (i = 1; i <= sub_n; i++) print sub_line[i]
|
|
594
|
+
}
|
|
595
|
+
sub_n = 0
|
|
596
|
+
sub_is_weave = 0
|
|
597
|
+
}
|
|
598
|
+
function close_group() { flush_sub(); group_pending = ""; group_blank = 0 }
|
|
599
|
+
/^[[:space:]]*\[\[hooks\.[A-Za-z]+\.hooks\]\][[:space:]]*$/ {
|
|
600
|
+
flush_sub(); sub_n = 1; sub_line[1] = $0; next
|
|
601
|
+
}
|
|
602
|
+
/^[[:space:]]*\[\[hooks\.[A-Za-z]+\]\][[:space:]]*$/ {
|
|
603
|
+
close_group(); group_pending = $0; next
|
|
604
|
+
}
|
|
605
|
+
/^[[:space:]]*\[/ { close_group(); print; next }
|
|
606
|
+
{
|
|
607
|
+
if (sub_n > 0) {
|
|
608
|
+
sub_line[++sub_n] = $0
|
|
609
|
+
if (match($0, /^[[:space:]]*command[[:space:]]*=[[:space:]]*"([^"\\]|\\.)*"/)) {
|
|
610
|
+
v = substr($0, RSTART, RLENGTH)
|
|
611
|
+
sub(/^[^"]*"/, "", v); sub(/"$/, "", v)
|
|
612
|
+
# write_codex_config escapes the path before writing it, so undo that
|
|
613
|
+
# before comparing; quotes first, then backslashes.
|
|
614
|
+
gsub(/\\"/, "\"", v)
|
|
615
|
+
gsub(/\\\\/, "\\", v)
|
|
616
|
+
if (v in own) sub_is_weave = 1
|
|
617
|
+
}
|
|
618
|
+
next
|
|
619
|
+
}
|
|
620
|
+
if (group_pending != "" && $0 ~ /^[[:space:]]*$/) { group_blank = 1; next }
|
|
621
|
+
if (group_pending != "") {
|
|
622
|
+
print group_pending; group_pending = ""
|
|
623
|
+
if (group_blank) { print ""; group_blank = 0 }
|
|
624
|
+
}
|
|
625
|
+
print
|
|
626
|
+
}
|
|
627
|
+
END { close_group() }
|
|
628
|
+
' "$config_file" >"$tmp" && mv "$tmp" "$config_file"
|
|
629
|
+
rm -f "$tmp" 2>/dev/null || true
|
|
630
|
+
}
|
|
631
|
+
|
|
486
632
|
# write_codex_config writes a managed [model_providers.weave] block to the
|
|
487
633
|
# Codex CLI's config.toml. Sets `model_provider = "weave"` at the top level so
|
|
488
634
|
# Codex picks the routed provider by default. The provider requires OpenAI
|
|
@@ -515,7 +661,7 @@ write_codex_config() {
|
|
|
515
661
|
printf '%s' "${s//\"/\\\"}"
|
|
516
662
|
}
|
|
517
663
|
|
|
518
|
-
local esc_key esc_email esc_name esc_url esc_status
|
|
664
|
+
local esc_key esc_email esc_name esc_url esc_status
|
|
519
665
|
esc_key="$(toml_escape "$block_key")"
|
|
520
666
|
esc_email="$(toml_escape "$block_email")"
|
|
521
667
|
esc_name="$(toml_escape "$block_name")"
|
|
@@ -561,6 +707,25 @@ write_codex_config() {
|
|
|
561
707
|
hook_feature_line=""
|
|
562
708
|
fi
|
|
563
709
|
if [ "$codex_hooks_enabled" = "true" ]; then
|
|
710
|
+
# UserPromptSubmit is registered only when OUR hook is actually on disk.
|
|
711
|
+
# Existence alone is the wrong test: install_codex_directive_script leaves
|
|
712
|
+
# a user-owned file at that path untouched, and registering it would hand
|
|
713
|
+
# someone else's script every prompt. Registering a command that is not
|
|
714
|
+
# there is the other failure -- Codex then fails every prompt rather than
|
|
715
|
+
# every toggle -- so the file must exist and carry our marker. -L is
|
|
716
|
+
# checked first because -f follows the link: a symlink to a marked file is
|
|
717
|
+
# still not a file we may point Codex at.
|
|
718
|
+
local directive_hook_block=""
|
|
719
|
+
if [ ! -L "$codex_directive_file" ] && [ -f "$codex_directive_file" ] && grep -Fq '<!-- weave-router managed codex directive -->' "$codex_directive_file"; then
|
|
720
|
+
directive_hook_block="$(cat <<TOML
|
|
721
|
+
|
|
722
|
+
[[hooks.UserPromptSubmit]]
|
|
723
|
+
[[hooks.UserPromptSubmit.hooks]]
|
|
724
|
+
type = "command"
|
|
725
|
+
command = "${esc_directive}"
|
|
726
|
+
TOML
|
|
727
|
+
)"
|
|
728
|
+
fi
|
|
564
729
|
hook_block="$(cat <<TOML
|
|
565
730
|
|
|
566
731
|
[[hooks.SessionStart]]
|
|
@@ -573,20 +738,7 @@ command = "${esc_status}"
|
|
|
573
738
|
type = "command"
|
|
574
739
|
command = "${esc_status}"
|
|
575
740
|
TOML
|
|
576
|
-
)"
|
|
577
|
-
# Only wire the directive hook when its helper actually installed. An empty
|
|
578
|
-
# command would be invalid TOML config, and a Codex that refuses to start is
|
|
579
|
-
# a far worse outcome than directives falling back to their skills.
|
|
580
|
-
if [ -n "$codex_directive_file" ]; then
|
|
581
|
-
hook_block="${hook_block}$(cat <<TOML
|
|
582
|
-
|
|
583
|
-
[[hooks.UserPromptSubmit]]
|
|
584
|
-
[[hooks.UserPromptSubmit.hooks]]
|
|
585
|
-
type = "command"
|
|
586
|
-
command = "${esc_directive}"
|
|
587
|
-
TOML
|
|
588
|
-
)"
|
|
589
|
-
fi
|
|
741
|
+
)${directive_hook_block}"
|
|
590
742
|
fi
|
|
591
743
|
local block
|
|
592
744
|
block="$(cat <<TOML
|
|
@@ -653,6 +805,16 @@ TOML
|
|
|
653
805
|
# <our managed block> ← model_provider stays top-level
|
|
654
806
|
# [model_providers.weave] ← scoped section, OK anywhere
|
|
655
807
|
# <user's sections> ← re-scope, unaffected
|
|
808
|
+
# Drop every registration we own first; the managed block below re-adds
|
|
809
|
+
# exactly the ones this version still ships. The section scan has to run
|
|
810
|
+
# after it, since removing a group can move the first header.
|
|
811
|
+
local owned_helpers=()
|
|
812
|
+
while IFS= read -r owned_helper; do
|
|
813
|
+
[ -n "$owned_helper" ] && owned_helpers+=("$owned_helper")
|
|
814
|
+
done <<EOF
|
|
815
|
+
$(weave_owned_codex_helpers)
|
|
816
|
+
EOF
|
|
817
|
+
strip_weave_codex_hooks "$tmp" ${owned_helpers[@]+"${owned_helpers[@]}"}
|
|
656
818
|
local first_section
|
|
657
819
|
first_section="$(awk '/^[[:space:]]*\[/ { print NR; exit }' "$tmp")"
|
|
658
820
|
if [ -n "$first_section" ]; then
|
|
@@ -3565,6 +3727,27 @@ announce_done() {
|
|
|
3565
3727
|
# latest known routed model in the terminal title. The embedded copy keeps the
|
|
3566
3728
|
# standalone curl installer feature-complete; packaged installs use the
|
|
3567
3729
|
# canonical sibling asset.
|
|
3730
|
+
# remove_codex_directive_helper deletes the UserPromptSubmit helper. Install no
|
|
3731
|
+
# longer calls it — install_codex_directive_script writes the local-toggle hook
|
|
3732
|
+
# in its place — but uninstall still does, and a registration left pointing at a
|
|
3733
|
+
# deleted script would make Codex run a missing command on every prompt, so the
|
|
3734
|
+
# two must go together. strip_weave_codex_hooks removes the registration; this
|
|
3735
|
+
# removes the file.
|
|
3736
|
+
remove_codex_directive_helper() {
|
|
3737
|
+
[ -e "$codex_directive_file" ] || return 0
|
|
3738
|
+
# Skip, do not exit: a symlink here is the user's, and aborting would leave
|
|
3739
|
+
# the install half-applied over something we were never going to touch.
|
|
3740
|
+
warn_if_symlink "$codex_directive_file" || return 0
|
|
3741
|
+
[ -f "$codex_directive_file" ] || return 0
|
|
3742
|
+
if grep -Fq '<!-- weave-router managed codex directive -->' "$codex_directive_file"; then
|
|
3743
|
+
rm -f "$codex_directive_file"
|
|
3744
|
+
ok "Removed the retired Codex directive hook at $codex_directive_file"
|
|
3745
|
+
rmdir "$(dirname "$codex_directive_file")" 2>/dev/null || true
|
|
3746
|
+
else
|
|
3747
|
+
warn "Leaving user-owned file at $codex_directive_file untouched."
|
|
3748
|
+
fi
|
|
3749
|
+
}
|
|
3750
|
+
|
|
3568
3751
|
install_codex_status_script() {
|
|
3569
3752
|
local candidate status_src=""
|
|
3570
3753
|
for candidate in \
|
|
@@ -4007,76 +4190,106 @@ CODEX_STATUS_EOF
|
|
|
4007
4190
|
ok "Codex status integration installed at $codex_status_file"
|
|
4008
4191
|
}
|
|
4009
4192
|
|
|
4010
|
-
#
|
|
4011
|
-
#
|
|
4012
|
-
#
|
|
4013
|
-
#
|
|
4193
|
+
# Install the UserPromptSubmit hook that answers the four local-config toggles
|
|
4194
|
+
# ($router-on/off/status, $disable-routing) without an inference turn.
|
|
4195
|
+
#
|
|
4196
|
+
# Only these four. Everything the router can answer it already answers itself,
|
|
4197
|
+
# and a hook that also intercepted those would re-open the duplicate-path
|
|
4198
|
+
# problem #1257 closed. What is left here cannot move server-side at all: they
|
|
4199
|
+
# rewrite this machine's config.toml, and $router-off in particular must keep
|
|
4200
|
+
# working when the router is the thing that is broken.
|
|
4014
4201
|
#
|
|
4015
|
-
#
|
|
4016
|
-
#
|
|
4017
|
-
#
|
|
4018
|
-
# never drift.
|
|
4202
|
+
# Mirrors install_codex_status_script, with one addition: the {{SCOPE}} token
|
|
4203
|
+
# is substituted the same way install_slash_commands does it, because the
|
|
4204
|
+
# installed hook cannot discover at run time which install it belongs to.
|
|
4019
4205
|
install_codex_directive_script() {
|
|
4020
4206
|
local candidate directive_src=""
|
|
4021
4207
|
for candidate in \
|
|
4022
|
-
"$script_dir/codex-
|
|
4023
|
-
"$script_dir/../codex-
|
|
4208
|
+
"$script_dir/codex-toggle.sh" \
|
|
4209
|
+
"$script_dir/../codex-toggle.sh"
|
|
4024
4210
|
do
|
|
4025
4211
|
if [ -f "$candidate" ]; then
|
|
4026
4212
|
directive_src="$candidate"
|
|
4027
4213
|
break
|
|
4028
4214
|
fi
|
|
4029
4215
|
done
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
if [ -
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
return 1
|
|
4043
|
-
}
|
|
4044
|
-
cp "$directive_src" "$codex_directive_file"
|
|
4045
|
-
else
|
|
4046
|
-
cat >"$codex_directive_file" <<'CODEX_DIRECTIVE_EOF'
|
|
4216
|
+
# Every bail-out below is a skip, never a failure. Unlike the status helper,
|
|
4217
|
+
# this hook has a working fallback -- the toggle skills still shell out to
|
|
4218
|
+
# the same installer -- so refusing to install the router because of a stray
|
|
4219
|
+
# file would cost the user far more than the two turns the hook saves.
|
|
4220
|
+
#
|
|
4221
|
+
# No sibling asset means a standalone `curl | sh` install, which has only
|
|
4222
|
+
# this file. The embedded copy below keeps that path feature-complete;
|
|
4223
|
+
# install/tests/codex-toggle_test.sh diffs the two and fails on drift.
|
|
4224
|
+
local embedded_src=""
|
|
4225
|
+
if [ -z "$directive_src" ]; then
|
|
4226
|
+
embedded_src="$(mktemp -t weave-codex-toggle.XXXXXX)" || return 0
|
|
4227
|
+
cat >"$embedded_src" <<'CODEX_TOGGLE_EOF'
|
|
4047
4228
|
#!/usr/bin/env bash
|
|
4048
4229
|
# <!-- weave-router managed codex directive -->
|
|
4049
4230
|
#
|
|
4050
|
-
# Codex UserPromptSubmit hook for the Weave Router
|
|
4051
|
-
# on stdin carrying the RAW prompt text, before
|
|
4052
|
-
#
|
|
4053
|
-
# Claude Code's slash commands.
|
|
4231
|
+
# Codex UserPromptSubmit hook for the Weave Router's LOCAL config toggles.
|
|
4232
|
+
# Codex passes a JSON object on stdin carrying the RAW prompt text, before
|
|
4233
|
+
# `$skill` expansion and before any inference.
|
|
4054
4234
|
#
|
|
4055
|
-
# Why this exists.
|
|
4056
|
-
#
|
|
4057
|
-
#
|
|
4058
|
-
#
|
|
4059
|
-
#
|
|
4060
|
-
#
|
|
4061
|
-
# by the client and the router short-circuits a user-typed directive into a
|
|
4062
|
-
# synthetic response with no upstream call at all.
|
|
4235
|
+
# Why this exists. The other directives ($fm, $ufm, $rf, $router-session,
|
|
4236
|
+
# a bare $router-models) are router state, and the router answers them itself
|
|
4237
|
+
# with no model turn. These four are not: they rewrite config.toml on this
|
|
4238
|
+
# machine, which a remote HTTP service cannot do. Left as skills they cost two
|
|
4239
|
+
# inference turns each -- one for the model to read the skill and decide to
|
|
4240
|
+
# exec, one to report the tool result.
|
|
4063
4241
|
#
|
|
4064
|
-
#
|
|
4065
|
-
#
|
|
4066
|
-
#
|
|
4242
|
+
# The second reason matters more than the cost. $router-off and
|
|
4243
|
+
# $disable-routing are what you reach for when the router is slow, broken, or
|
|
4244
|
+
# out of credits -- and as skills they need a model turn, which is served
|
|
4245
|
+
# THROUGH the router. The off switch depended on the thing it turns off. This
|
|
4246
|
+
# hook runs locally before any network call, so it works when the router does
|
|
4247
|
+
# not.
|
|
4067
4248
|
#
|
|
4068
|
-
#
|
|
4069
|
-
#
|
|
4070
|
-
#
|
|
4071
|
-
# sends the same Session-Id that Codex sends on its own turns.
|
|
4249
|
+
# Scope: only the four local toggles. Everything the router can answer is
|
|
4250
|
+
# deliberately absent -- a hook that intercepted those would re-open the
|
|
4251
|
+
# duplicate-path problem #1257 closed.
|
|
4072
4252
|
#
|
|
4073
|
-
# FAIL OPEN. Every unexpected condition
|
|
4074
|
-
#
|
|
4075
|
-
#
|
|
4076
|
-
#
|
|
4253
|
+
# FAIL OPEN. Every unexpected condition -- no jq, no npx, a malformed payload,
|
|
4254
|
+
# a command that errors -- must let the prompt through untouched. A directive
|
|
4255
|
+
# that silently swallows the user's prompt is far worse than one that does not
|
|
4256
|
+
# fire.
|
|
4077
4257
|
|
|
4078
4258
|
set -uo pipefail
|
|
4079
4259
|
|
|
4260
|
+
# How long the toggle may take before the hook gives up and passes the prompt
|
|
4261
|
+
# through. npx resolves from cache after first use; the budget covers a cold
|
|
4262
|
+
# fetch without letting a dead network hang the turn indefinitely.
|
|
4263
|
+
#
|
|
4264
|
+
# Validated, not trusted. `[ "$waited" -ge "$seconds" ]` does not compare a
|
|
4265
|
+
# bound it cannot parse -- it errors -- so the cap would never fire and the
|
|
4266
|
+
# hook would hang until npx exited on its own. Two ways to get there: a
|
|
4267
|
+
# non-numeric value, and a digit-only value too large for the shell's integer
|
|
4268
|
+
# type ("[: 99999999999999999999: integer expression expected"). The length
|
|
4269
|
+
# bound covers the second; 4 digits is already ~2.7h, far past anything
|
|
4270
|
+
# meaningful for a prompt hook.
|
|
4271
|
+
#
|
|
4272
|
+
# The diagnostic goes to stderr, never stdout: stdout is the hook protocol
|
|
4273
|
+
# channel and any stray byte there corrupts the JSON. Codex discards stderr,
|
|
4274
|
+
# so this costs a user nothing and tells whoever set the variable why it was
|
|
4275
|
+
# ignored.
|
|
4276
|
+
WEAVE_TOGGLE_TIMEOUT_DEFAULT=45
|
|
4277
|
+
# Resolved into a local first: under `set -u`, ${#WEAVE_TOGGLE_TIMEOUT} on an
|
|
4278
|
+
# unset variable aborts the hook outright -- which would swallow every prompt,
|
|
4279
|
+
# the one failure this script must never have.
|
|
4280
|
+
weave_timeout="${WEAVE_TOGGLE_TIMEOUT:-$WEAVE_TOGGLE_TIMEOUT_DEFAULT}"
|
|
4281
|
+
weave_timeout_invalid=""
|
|
4282
|
+
case "$weave_timeout" in
|
|
4283
|
+
''|*[!0-9]*|0) weave_timeout_invalid=1 ;;
|
|
4284
|
+
*) [ "${#weave_timeout}" -gt 4 ] && weave_timeout_invalid=1 ;;
|
|
4285
|
+
esac
|
|
4286
|
+
if [ -n "$weave_timeout_invalid" ]; then
|
|
4287
|
+
printf 'weave-router: ignoring invalid WEAVE_TOGGLE_TIMEOUT=%s; using %ss\n' \
|
|
4288
|
+
"$weave_timeout" "$WEAVE_TOGGLE_TIMEOUT_DEFAULT" >&2
|
|
4289
|
+
weave_timeout="$WEAVE_TOGGLE_TIMEOUT_DEFAULT"
|
|
4290
|
+
fi
|
|
4291
|
+
WEAVE_TOGGLE_TIMEOUT="$weave_timeout"
|
|
4292
|
+
|
|
4080
4293
|
# ---------- responses ----------
|
|
4081
4294
|
|
|
4082
4295
|
# pass_through hands the prompt to Codex unchanged. Every failure path ends here.
|
|
@@ -4087,9 +4300,12 @@ pass_through() {
|
|
|
4087
4300
|
|
|
4088
4301
|
# block stops the turn before any inference and shows the text to the user.
|
|
4089
4302
|
# Codex renders `reason`; nothing reaches the model.
|
|
4303
|
+
#
|
|
4304
|
+
# Setting only `decision` is deliberate: `continue:false` alongside it wins and
|
|
4305
|
+
# renders a bare "Hook stopped" with the reason discarded.
|
|
4090
4306
|
block() {
|
|
4091
4307
|
local reason="$1" payload
|
|
4092
|
-
payload="$(jq -cn --arg r "$reason" '{decision:"block", reason:$r
|
|
4308
|
+
payload="$(jq -cn --arg r "$reason" '{decision:"block", reason:$r}' 2>/dev/null)" \
|
|
4093
4309
|
|| pass_through
|
|
4094
4310
|
printf '%s\n' "$payload"
|
|
4095
4311
|
exit 0
|
|
@@ -4101,200 +4317,171 @@ payload="$(cat)" || pass_through
|
|
|
4101
4317
|
[ -n "$payload" ] || pass_through
|
|
4102
4318
|
|
|
4103
4319
|
prompt="$(jq -r '.prompt // ""' <<<"$payload" 2>/dev/null)" || pass_through
|
|
4104
|
-
session_id="$(jq -r '.session_id // ""' <<<"$payload" 2>/dev/null)" || pass_through
|
|
4105
|
-
requested_model="$(jq -r '.model // ""' <<<"$payload" 2>/dev/null)" || pass_through
|
|
4106
4320
|
|
|
4107
4321
|
# Only a directive at the very start of the prompt is ours. Prose that merely
|
|
4108
|
-
# mentions `$
|
|
4322
|
+
# mentions `$router-off` in a sentence is the user's text, not a command.
|
|
4109
4323
|
case "$prompt" in
|
|
4110
4324
|
'$'*) ;;
|
|
4111
4325
|
*) pass_through ;;
|
|
4112
4326
|
esac
|
|
4113
4327
|
|
|
4114
4328
|
verb="${prompt#$}"
|
|
4115
|
-
|
|
4329
|
+
# These four take no arguments. A prompt with a tail is not one of ours --
|
|
4330
|
+
# pass it through so the skill can explain itself rather than the hook eating
|
|
4331
|
+
# a message it cannot honour.
|
|
4116
4332
|
case "$verb" in
|
|
4117
|
-
*
|
|
4118
|
-
args="${verb#* }"
|
|
4119
|
-
verb="${verb%% *}"
|
|
4120
|
-
;;
|
|
4333
|
+
*[[:space:]]*) pass_through ;;
|
|
4121
4334
|
esac
|
|
4122
|
-
# Trim surrounding whitespace from the argument tail without touching its
|
|
4123
|
-
# interior: a leading `-`/`+` is a router-feedback verdict and must survive.
|
|
4124
|
-
args="${args#"${args%%[![:space:]]*}"}"
|
|
4125
|
-
args="${args%"${args##*[![:space:]]}"}"
|
|
4126
4335
|
|
|
4127
4336
|
# ---------- directive table ----------
|
|
4128
4337
|
#
|
|
4129
|
-
#
|
|
4130
|
-
#
|
|
4131
|
-
# router state, so a skill remains the right adapter for them.
|
|
4132
|
-
directive=""
|
|
4338
|
+
# Local-config toggles only. router-on/off/status flip this install's
|
|
4339
|
+
# config.toml; disable-routing is `off` plus the reversal hint below.
|
|
4133
4340
|
case "$verb" in
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
# Purely local: the session id is already in the hook payload, so this
|
|
4139
|
-
# answers without a network call and without a model turn.
|
|
4140
|
-
[ -n "$session_id" ] || pass_through
|
|
4141
|
-
block "✦ Weave Router · session id: $session_id"
|
|
4142
|
-
;;
|
|
4341
|
+
router-on) toggle="on" ;;
|
|
4342
|
+
router-off) toggle="off" ;;
|
|
4343
|
+
router-status) toggle="status" ;;
|
|
4344
|
+
disable-routing) toggle="off" ;;
|
|
4143
4345
|
*) pass_through ;;
|
|
4144
4346
|
esac
|
|
4145
4347
|
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
/force-model|/router-feedback)
|
|
4150
|
-
[ -n "$args" ] || pass_through
|
|
4151
|
-
;;
|
|
4152
|
-
esac
|
|
4348
|
+
command -v npx >/dev/null 2>&1 || pass_through
|
|
4349
|
+
|
|
4350
|
+
# ---------- run ----------
|
|
4153
4351
|
|
|
4154
|
-
#
|
|
4155
|
-
#
|
|
4156
|
-
#
|
|
4157
|
-
|
|
4158
|
-
command -v curl >/dev/null 2>&1 || pass_through
|
|
4352
|
+
# WEAVE_TOGGLE_TIMED_OUT is set by run_bounded instead of encoding the timeout
|
|
4353
|
+
# in the exit status. 124 is a status the child can legitimately return, and
|
|
4354
|
+
# reading that as "we timed out" would swallow the toggle's own error output.
|
|
4355
|
+
WEAVE_TOGGLE_TIMED_OUT=0
|
|
4159
4356
|
|
|
4160
|
-
#
|
|
4357
|
+
# run_bounded runs a command with a wall-clock cap. GNU `timeout` is not on
|
|
4358
|
+
# macOS, so this polls the child rather than depending on coreutils.
|
|
4161
4359
|
#
|
|
4162
|
-
#
|
|
4163
|
-
#
|
|
4164
|
-
#
|
|
4165
|
-
|
|
4166
|
-
|
|
4167
|
-
|
|
4168
|
-
|
|
4169
|
-
local
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4173
|
-
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
|
|
4177
|
-
|
|
4178
|
-
|
|
4179
|
-
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
# Only basic strings have escapes; a backslash in a literal string is
|
|
4193
|
-
# data, so consuming the next character there would mis-track the quote.
|
|
4194
|
-
if (indq && c == "\\") { out = out c; esc = 1; continue }
|
|
4195
|
-
if (!insq && c == "\"") { indq = !indq; out = out c; continue }
|
|
4196
|
-
if (!indq && c == "'"'"'") { insq = !insq; out = out c; continue }
|
|
4197
|
-
if (c == "#" && !indq && !insq) break
|
|
4198
|
-
out = out c
|
|
4199
|
-
}
|
|
4200
|
-
return out
|
|
4201
|
-
}
|
|
4202
|
-
{ $0 = weave_strip_comment($0) }
|
|
4203
|
-
# TOML basic string -> raw value: drop the `<header> = "` prefix and the
|
|
4204
|
-
# closing quote, then undo the escaping write_codex_config applied.
|
|
4205
|
-
function weave_unescape(s) {
|
|
4206
|
-
sub(/^"?[A-Za-z][A-Za-z0-9-]*"?[[:space:]]*=[[:space:]]*"/, "", s)
|
|
4207
|
-
sub(/"$/, "", s)
|
|
4208
|
-
gsub(/\\"/, "\"", s)
|
|
4209
|
-
gsub(/\\\\/, "\\", s)
|
|
4210
|
-
return s
|
|
4211
|
-
}
|
|
4212
|
-
/^[[:space:]]*\[/ {
|
|
4213
|
-
in_provider = ($0 ~ /^[[:space:]]*\[[[:space:]]*model_providers[[:space:]]*\.[[:space:]]*weave[[:space:]]*(\.[^]]*)?\][[:space:]]*(#.*)?$/)
|
|
4214
|
-
next
|
|
4215
|
-
}
|
|
4216
|
-
!in_provider { next }
|
|
4217
|
-
match($0, /base_url[[:space:]]*=[[:space:]]*"[^"]*"/) {
|
|
4218
|
-
v = substr($0, RSTART, RLENGTH)
|
|
4219
|
-
sub(/^.*=[[:space:]]*"/, "", v); sub(/"$/, "", v)
|
|
4220
|
-
if (url == "") url = v
|
|
4221
|
-
}
|
|
4222
|
-
match($0, /"?X-Weave-Router-Key"?[[:space:]]*=[[:space:]]*"([^"\\]|\\.)*"/) {
|
|
4223
|
-
if (key == "") key = weave_unescape(substr($0, RSTART, RLENGTH))
|
|
4224
|
-
}
|
|
4225
|
-
match($0, /"?X-Weave-User-Email"?[[:space:]]*=[[:space:]]*"([^"\\]|\\.)*"/) {
|
|
4226
|
-
if (email == "") email = weave_unescape(substr($0, RSTART, RLENGTH))
|
|
4227
|
-
}
|
|
4228
|
-
match($0, /"?X-Weave-User-Name"?[[:space:]]*=[[:space:]]*"([^"\\]|\\.)*"/) {
|
|
4229
|
-
if (name == "") name = weave_unescape(substr($0, RSTART, RLENGTH))
|
|
4230
|
-
}
|
|
4231
|
-
END { if (url != "" && key != "") printf "%s\n%s\n%s\n%s\n", url, key, email, name }
|
|
4232
|
-
' "$config" 2>/dev/null
|
|
4360
|
+
# The child runs in its own process group (`set -m` + `kill -- -PID`) because
|
|
4361
|
+
# npx execs node: signalling only the direct child leaves that node process
|
|
4362
|
+
# running after the hook has already passed the prompt through.
|
|
4363
|
+
run_bounded() {
|
|
4364
|
+
local seconds="$1" out="$2"; shift 2
|
|
4365
|
+
WEAVE_TOGGLE_TIMED_OUT=0
|
|
4366
|
+
|
|
4367
|
+
local had_monitor=0
|
|
4368
|
+
case "$-" in *m*) had_monitor=1 ;; esac
|
|
4369
|
+
set -m
|
|
4370
|
+
"$@" >"$out" 2>&1 &
|
|
4371
|
+
local pid=$!
|
|
4372
|
+
[ "$had_monitor" -eq 1 ] || set +m
|
|
4373
|
+
|
|
4374
|
+
local waited=0
|
|
4375
|
+
while kill -0 "$pid" 2>/dev/null; do
|
|
4376
|
+
if [ "$waited" -ge "$seconds" ]; then
|
|
4377
|
+
WEAVE_TOGGLE_TIMED_OUT=1
|
|
4378
|
+
# Negative pid = the whole group. Fall back to the bare pid if the
|
|
4379
|
+
# group is already gone, so a raced exit still gets reaped.
|
|
4380
|
+
kill -TERM -- "-$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null
|
|
4381
|
+
sleep 1
|
|
4382
|
+
kill -KILL -- "-$pid" 2>/dev/null || kill -KILL "$pid" 2>/dev/null
|
|
4383
|
+
wait "$pid" 2>/dev/null
|
|
4384
|
+
return 0
|
|
4385
|
+
fi
|
|
4386
|
+
sleep 1
|
|
4387
|
+
waited=$((waited + 1))
|
|
4388
|
+
done
|
|
4389
|
+
wait "$pid"
|
|
4233
4390
|
}
|
|
4234
4391
|
|
|
4235
|
-
|
|
4236
|
-
|
|
4237
|
-
|
|
4238
|
-
|
|
4239
|
-
|
|
4240
|
-
|
|
4392
|
+
out_file="$(mktemp -t weave-codex-toggle.XXXXXX)" || pass_through
|
|
4393
|
+
# shellcheck disable=SC2064 # expand out_file now; it never changes after this
|
|
4394
|
+
trap "rm -f '$out_file'" EXIT
|
|
4395
|
+
|
|
4396
|
+
# {{SCOPE}} is substituted at install time by the only code that knows whether
|
|
4397
|
+
# this install is user, project, or --dir scoped. It becomes literal argument
|
|
4398
|
+
# text (the installer printf %q-quotes any path), so there is nothing here to
|
|
4399
|
+
# word-split at runtime -- the braces are a template token, not an expansion.
|
|
4400
|
+
# shellcheck disable=SC1083
|
|
4401
|
+
run_bounded "$WEAVE_TOGGLE_TIMEOUT" "$out_file" \
|
|
4402
|
+
npx --package @weave-os/router -y -- weave-router "$toggle" --codex{{SCOPE}}
|
|
4403
|
+
status=$?
|
|
4404
|
+
|
|
4405
|
+
output="$(cat "$out_file" 2>/dev/null || true)"
|
|
4406
|
+
# Strip ANSI colour the installer emits when it thinks it has a terminal;
|
|
4407
|
+
# Codex renders `reason` as plain text.
|
|
4408
|
+
output="$(printf '%s' "$output" | sed -e 's/\x1b\[[0-9;]*m//g')"
|
|
4409
|
+
|
|
4410
|
+
# A failure is reported, not swallowed: passing through here would hand the
|
|
4411
|
+
# model a prompt the user meant as a command, and it would likely try the same
|
|
4412
|
+
# command again. Only a real timeout passes through -- the toggle never got to
|
|
4413
|
+
# finish, so the skill path is still the honest fallback.
|
|
4414
|
+
if [ "$WEAVE_TOGGLE_TIMED_OUT" -eq 1 ]; then
|
|
4241
4415
|
pass_through
|
|
4242
4416
|
fi
|
|
4243
4417
|
|
|
4244
|
-
|
|
4245
|
-
url="${url%/v1}/v1/chat/completions"
|
|
4418
|
+
[ -n "$output" ] || output="weave-router $toggle --codex exited with status $status."
|
|
4246
4419
|
|
|
4247
|
-
#
|
|
4248
|
-
#
|
|
4249
|
-
#
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
4258
|
-
|
|
4259
|
-
|
|
4260
|
-
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
[ -n "$
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
response="$(curl -fsS --max-time 10 \
|
|
4267
|
-
-H "Content-Type: application/json" \
|
|
4268
|
-
-H "X-Weave-Router-Key: $router_key" \
|
|
4269
|
-
-H "Session-Id: $session_id" \
|
|
4270
|
-
-H "X-App: codex" \
|
|
4271
|
-
${identity_args[@]+"${identity_args[@]}"} \
|
|
4272
|
-
-d "$body" \
|
|
4273
|
-
"$url" 2>/dev/null)" || pass_through
|
|
4274
|
-
[ -n "$response" ] || pass_through
|
|
4275
|
-
|
|
4276
|
-
# `strings` filters to the string type: jq -r would otherwise render an object
|
|
4277
|
-
# or array as JSON, which is non-empty and would block the turn with garbage.
|
|
4278
|
-
marker="$(jq -r '[.choices[0].message.content | strings] | first // ""' <<<"$response" 2>/dev/null)" || pass_through
|
|
4279
|
-
marker="$(printf '%s' "$marker" | sed -e 's/\*\*//g' -e '/^[[:space:]]*$/d')"
|
|
4280
|
-
[ -n "$marker" ] || pass_through
|
|
4281
|
-
|
|
4282
|
-
block "$marker"
|
|
4283
|
-
CODEX_DIRECTIVE_EOF
|
|
4420
|
+
# Both trailers describe a change that actually happened, so they are gated on
|
|
4421
|
+
# a zero exit. A failed toggle rewrote no config; telling the user it takes
|
|
4422
|
+
# effect next launch would send them to restart Codex for nothing.
|
|
4423
|
+
if [ "$status" -eq 0 ] && [ "$verb" != "router-status" ]; then
|
|
4424
|
+
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."
|
|
4425
|
+
fi
|
|
4426
|
+
if [ "$status" -eq 0 ] && [ "$verb" = "disable-routing" ]; then
|
|
4427
|
+
output="$output"$'\n'"Reverse it with \$router-on."
|
|
4428
|
+
fi
|
|
4429
|
+
|
|
4430
|
+
block "$output"
|
|
4431
|
+
CODEX_TOGGLE_EOF
|
|
4432
|
+
directive_src="$embedded_src"
|
|
4433
|
+
fi
|
|
4434
|
+
if ! grep -Fq '<!-- weave-router managed codex directive -->' "$directive_src"; then
|
|
4435
|
+
warn "Codex directive hook has no ownership marker; skipping it. The \$router-on/off/status toggles still work as skills."
|
|
4436
|
+
[ -n "$embedded_src" ] && rm -f "$embedded_src"
|
|
4437
|
+
return 0
|
|
4284
4438
|
fi
|
|
4439
|
+
# A symlink at this path is never ours, whatever it points at and whether or
|
|
4440
|
+
# not the target carries our marker. Writing through one would let a
|
|
4441
|
+
# reinstall overwrite an arbitrary file outside the Codex directory, and a
|
|
4442
|
+
# DANGLING link is invisible to -e, so this test must be -L and must come
|
|
4443
|
+
# first. It skips rather than exits: refuse_if_symlink would abort the whole
|
|
4444
|
+
# installer, and this hook is optional -- the toggle skills still work.
|
|
4445
|
+
if [ -L "$codex_directive_file" ]; then
|
|
4446
|
+
warn "A symlink sits at $codex_directive_file; leaving it untouched and skipping the local-toggle hook. The \$router-on/off/status toggles still work as skills."
|
|
4447
|
+
[ -n "$embedded_src" ] && rm -f "$embedded_src"
|
|
4448
|
+
return 0
|
|
4449
|
+
fi
|
|
4450
|
+
if [ "$scope" = "user" ] && [ -z "$install_dir" ]; then
|
|
4451
|
+
mkdir -p "$(dirname "$codex_directive_file")"
|
|
4452
|
+
fi
|
|
4453
|
+
if [ -e "$codex_directive_file" ] && { [ ! -f "$codex_directive_file" ] || ! grep -Fq '<!-- weave-router managed codex directive -->' "$codex_directive_file"; }; then
|
|
4454
|
+
warn "A user-owned Codex directive helper already exists at $codex_directive_file; leaving it untouched. The \$router-on/off/status toggles still work as skills."
|
|
4455
|
+
[ -n "$embedded_src" ] && rm -f "$embedded_src"
|
|
4456
|
+
return 0
|
|
4457
|
+
fi
|
|
4458
|
+
|
|
4459
|
+
local scope_args=""
|
|
4460
|
+
if [ -n "$install_dir" ]; then
|
|
4461
|
+
scope_args=" --dir $(printf '%q' "$install_dir")"
|
|
4462
|
+
elif [ "$scope" = "project" ]; then
|
|
4463
|
+
scope_args=" --scope project"
|
|
4464
|
+
fi
|
|
4465
|
+
local body
|
|
4466
|
+
body="$(cat "$directive_src")" || { [ -n "$embedded_src" ] && rm -f "$embedded_src"; return 0; }
|
|
4467
|
+
[ -n "$embedded_src" ] && rm -f "$embedded_src"
|
|
4468
|
+
printf '%s\n' "${body//\{\{SCOPE\}\}/$scope_args}" >"$codex_directive_file" || return 0
|
|
4285
4469
|
chmod 700 "$codex_directive_file"
|
|
4286
|
-
ok "Codex
|
|
4470
|
+
ok "Codex local-toggle hook installed at $codex_directive_file"
|
|
4287
4471
|
}
|
|
4288
4472
|
|
|
4473
|
+
|
|
4289
4474
|
if [ "$target" = "codex" ]; then
|
|
4290
4475
|
if ! install_codex_status_script; then
|
|
4291
4476
|
err "Cannot install the Codex status helper safely; refusing to write hooks that could execute unowned code."
|
|
4292
4477
|
exit 1
|
|
4293
4478
|
fi
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4479
|
+
# Before write_codex_config, which registers whatever this leaves on disk:
|
|
4480
|
+
# a registration pointing at a script that is not there yet makes Codex run
|
|
4481
|
+
# a missing command on every prompt. It never fails the install -- the
|
|
4482
|
+
# toggles fall back to their skills -- so its outcome is read off the file,
|
|
4483
|
+
# not off an exit status.
|
|
4484
|
+
install_codex_directive_script
|
|
4298
4485
|
write_codex_config "$codex_config_file" "$base_url" "$api_key" "$user_email" "$user_name"
|
|
4299
4486
|
ok "Codex config written to $codex_config_file"
|
|
4300
4487
|
remove_obsolete_codex_prompt_wrappers "$codex_dir/prompts"
|