@weave-os/router 0.2.19 → 0.2.21

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/install.sh CHANGED
@@ -5,17 +5,21 @@
5
5
  # Configures Claude Code (default), the OpenAI Codex CLI (`--codex`),
6
6
  # opencode (`--opencode`), or pi (`--pi`) to permanently route through the
7
7
  # Weave Router. For Claude Code this writes the router base URL, router auth
8
- # header, and a status line into Claude Code's settings.json. For Codex it
9
- # writes a `model_providers.weave` entry plus `model_provider = "weave"` into
10
- # ~/.codex/config.toml (managed block delimited by markers). For opencode
11
- # it merges a `provider.weave` block (anthropic-compatible) into
12
- # opencode.json — since the file is JSON, install/uninstall are structural
13
- # (jq) rather than marker-delimited. For pi it merges a `weave` provider into
8
+ # header, and a status line into Claude Code's settings.json unless Claude Code
9
+ # already has an active statusline configured. For Codex it writes a
10
+ # `model_providers.weave` entry plus `model_provider = "weave"` into
11
+ # ~/.codex/config.toml (managed block delimited by markers). For opencode it
12
+ # merges a Responses-format `provider.weave` block into opencode.json — since
13
+ # the file is JSON, install/uninstall are structural (jq) rather than
14
+ # marker-delimited. For pi it merges a `weave` provider into
14
15
  # ~/.pi/agent/models.json, sets it as the default in settings.json, and adds
15
16
  # the @weave-os/router extension (which also adds a parallel subagent
16
17
  # `dispatch` tool) — all structural (jq) merges.
17
18
  #
18
19
  # Two scopes (apply to all targets):
20
+ # --context-window 1m Opt Claude Code into its selected model's 1M variant.
21
+ # Preserves the prior setting for off/uninstall; never disables compaction.
22
+ #
19
23
  # - user (default): ~/.claude/settings.json + ~/.weave/cc-statusline.sh
20
24
  # ~/.codex/config.toml (with --codex)
21
25
  # ~/.config/opencode/opencode.json (with --opencode)
@@ -48,6 +52,7 @@
48
52
  # npx @weave-os/router --dir /tmp/my-sandbox # isolated throwaway install
49
53
  # npx @weave-os/router --local # local router on localhost:8080
50
54
  # npx @weave-os/router --base-url http://localhost:8080 # self-hosted, custom port
55
+ # npx @weave-os/router --email you@example.com # set the router identity email without prompting
51
56
  # npx @weave-os/router --non-interactive # require WEAVE_ROUTER_KEY env var (defaults target to claude)
52
57
  # npx @weave-os/router --quiet # suppress banner, ping check, and trailing tips
53
58
  # npx @weave-os/router --rotate-key # ignore the installed key and prompt for a new one
@@ -73,11 +78,11 @@
73
78
  # Cursor's base URL lives in its own settings UI (no file we own), so there's
74
79
  # nothing to toggle here — flip "Override OpenAI Base URL" in Cursor settings.
75
80
  #
76
- # Which router directives each client gets is declared once in
81
+ # Which router integrations each client gets is declared once in
77
82
  # install/directives.tsv and installed from there: Claude Code and opencode get
78
- # Markdown slash commands, Codex gets native `$name` skills (it reserves `/…`
79
- # for built-ins), pi registers /fm and /ufm in its extension, and Cursor is
80
- # manual. See install/README.md for the full matrix.
83
+ # Markdown slash commands, Codex gets native `$name` skills, pi registers /fm
84
+ # and /ufm in its extension, and Cursor is manual. See install/README.md for
85
+ # the full matrix.
81
86
  #
82
87
  # Inspect and edit which models this installation lets the router pick from —
83
88
  # the same lists the router dashboard's settings page renders. The endpoint and
@@ -109,10 +114,13 @@ npm_package_name="${WEAVE_ROUTER_NPM_PACKAGE:-@weave-os/router}"
109
114
 
110
115
 
111
116
  scope="user"
117
+ context_window=""
112
118
  scope_explicit="false"
113
119
  install_dir=""
114
120
  base_url=""
115
121
  base_url_explicit="false"
122
+ email=""
123
+ email_explicit="false"
116
124
  non_interactive="false"
117
125
  quiet="false"
118
126
  router_key_header="X-Weave-Router-Key"
@@ -120,9 +128,9 @@ router_key_header="X-Weave-Router-Key"
120
128
  # settings.json; "codex" writes ~/.codex/config.toml; "opencode" merges a
121
129
  # provider block into opencode.json. Each target carries its own
122
130
  # credential-passthrough story in the router: Claude Code's logged-in
123
- # Anthropic key flows through unchanged, Codex's `OPENAI_API_KEY` flows
124
- # through via the same header path, and opencode talks to the router via
125
- # its anthropic-compatible API surface. target_explicit tracks whether
131
+ # Anthropic key flows through unchanged, Codex's ChatGPT credential flows
132
+ # through its native provider, and opencode uses the Responses API plus
133
+ # dedicated subscription headers. target_explicit tracks whether
126
134
  # --claude / --codex / --opencode was passed so an interactive run can
127
135
  # prompt for the choice.
128
136
  target="claude"
@@ -362,6 +370,37 @@ refuse_if_symlink() {
362
370
  fi
363
371
  }
364
372
 
373
+ # The marker is part of the installed script, so a user-owned script at the
374
+ # same conventional path can be distinguished from one managed by the router.
375
+ CLAUDE_STATUSLINE_MARKER="# Claude Code statusline for the Weave Router."
376
+
377
+ # claude_statusline_configured reports whether Claude Code has a non-null
378
+ # statusline setting in a settings file.
379
+ claude_statusline_configured() {
380
+ local settings_path="$1"
381
+ [ -f "$settings_path" ] || return 1
382
+ jq -e 'has("statusLine") and (.statusLine != null)' "$settings_path" >/dev/null 2>&1
383
+ }
384
+
385
+ # claude_statusline_router_owned reports whether a settings entry points at the
386
+ # router's statusline. Existing scripts prove ownership with the marker; when a
387
+ # script is missing, the adjacent ownership marker proves that the router wrote
388
+ # it and should recreate it.
389
+ claude_statusline_router_owned() {
390
+ local settings_path="$1" expected_command="$2" script_path="$3" ownership_marker_path="$4" configured_command
391
+ [ -f "$settings_path" ] || return 1
392
+ configured_command="$(jq -r '.statusLine.command // empty' "$settings_path" 2>/dev/null || true)"
393
+ [ "$configured_command" = "$expected_command" ] || return 1
394
+ if [ -f "$script_path" ]; then
395
+ if grep -Fq "$CLAUDE_STATUSLINE_MARKER" "$script_path" 2>/dev/null; then
396
+ return 0
397
+ fi
398
+ [ -r "$script_path" ] && return 1
399
+ fi
400
+ [ -f "$ownership_marker_path" ] \
401
+ && grep -Fq "$CLAUDE_STATUSLINE_MARKER" "$ownership_marker_path"
402
+ }
403
+
365
404
  # Markers that delimit the block this installer manages inside Codex's
366
405
  # config.toml. Kept on disk verbatim so a re-install (or uninstall.sh
367
406
  # --codex) can find and replace the block instead of duplicating it.
@@ -430,12 +469,20 @@ normalize_name() {
430
469
 
431
470
  # resolve_user_email picks the email to plant in router request headers so the
432
471
  # router can attribute traffic to a person even on shared API keys. Priority:
433
- # WEAVE_USER_EMAIL env override → git config user.email → interactive prompt
434
- # (pre-filled with whatever we found). In --non-interactive mode we never
435
- # prompt, so unset/invalid means we ship no header (router treats that as
436
- # account_uuid-only, same as today). Echoes the validated email on stdout.
472
+ # explicit --email → WEAVE_USER_EMAIL env override → git config user.email →
473
+ # interactive prompt (pre-filled with whatever we found). In --non-interactive
474
+ # mode we never prompt, so unset/invalid means we ship no header (router treats
475
+ # that as account_uuid-only, same as today). Echoes the validated email on stdout.
437
476
  resolve_user_email() {
438
477
  local candidate=""
478
+ if [ "$email_explicit" = "true" ]; then
479
+ candidate="$(normalize_email "$email")"
480
+ if [ -z "$candidate" ]; then
481
+ warn "--email=\"$email\" is not a valid email; ignoring."
482
+ fi
483
+ printf '%s' "$candidate"
484
+ return
485
+ fi
439
486
  if [ -n "${WEAVE_USER_EMAIL:-}" ]; then
440
487
  candidate="$(normalize_email "$WEAVE_USER_EMAIL")"
441
488
  if [ -z "$candidate" ]; then
@@ -911,6 +958,9 @@ write_opencode_config() {
911
958
  # upstream model for every request, so presenting pinned model names would
912
959
  # imply a choice that the router intentionally does not honor. Whichever
913
960
  # model serves a turn uses its matching subscription when one is connected.
961
+ # OpenCode interprets omitted custom-model limits as zero, which disables its
962
+ # overflow compaction. The 128K/32K virtual contract is the router's
963
+ # conservative compatibility floor and leaves 96K for model-visible input.
914
964
  #
915
965
  # npm is @ai-sdk/openai and baseURL KEEPS its /v1 here: opencode's
916
966
  # @ai-sdk/openai provider appends /responses, yielding the router's
@@ -927,10 +977,16 @@ write_opencode_config() {
927
977
  --argjson headers "$headers_json" '
928
978
  {
929
979
  npm: "@ai-sdk/openai",
930
- name: "Weave Router",
980
+ name: "Weave Router — Codex plan",
931
981
  options: { apiKey: $key, baseURL: $url, headers: $headers },
932
982
  models: {
933
- auto: { name: "Auto" }
983
+ auto: {
984
+ name: "Auto",
985
+ reasoning: true,
986
+ attachment: true,
987
+ modalities: { input: ["text", "image", "pdf"], output: ["text"] },
988
+ limit: { context: 128000, output: 32000 }
989
+ }
934
990
  }
935
991
  }
936
992
  ')"
@@ -969,20 +1025,43 @@ write_opencode_config() {
969
1025
  plugin_dir="$(cd "$(dirname "$config_file")" && pwd)/.weave"
970
1026
  plugin_spec="$plugin_dir/opencode-weave.ts"
971
1027
  plugin_src="$script_dir/opencode-weave/src/index.ts"
1028
+ plugin_directives_src="$script_dir/opencode-weave/src/directives.ts"
972
1029
  if [ -f "$plugin_src" ]; then
973
1030
  mkdir -p "$plugin_dir"
974
1031
  cp "$plugin_src" "$plugin_spec"
975
1032
  chmod 644 "$plugin_spec"
1033
+ if [ -f "$plugin_directives_src" ]; then
1034
+ cp "$plugin_directives_src" "$plugin_dir/directives.ts"
1035
+ chmod 644 "$plugin_dir/directives.ts"
1036
+ fi
976
1037
  plugin_arg="$plugin_spec"
977
1038
  else
978
1039
  warn "opencode subscription plugin source not found at $plugin_src — skipping the Claude login + subscription routing. (Use a packaged 'npx $npm_package_name' install.)"
979
1040
  fi
980
1041
 
1042
+ # Installing must actually activate the router. Preserve a prior direct model
1043
+ # beside the config so off/uninstall can restore the user's exact choice.
1044
+ local parked_file previous_model
1045
+ parked_file="$(dirname "$config_file")/.weave-parked.json"
1046
+ if [ -f "$config_file" ]; then
1047
+ previous_model="$(json_get "$config_file" '.model')"
1048
+ case "$previous_model" in
1049
+ ""|weave/*|weave-codex/*|weave-claude/*) ;;
1050
+ *)
1051
+ if [ "$scope" = "project" ] || [ -n "$install_dir" ]; then
1052
+ refuse_if_symlink "$parked_file"
1053
+ fi
1054
+ jq -n --arg direct "$previous_model" --arg router "weave/auto" \
1055
+ '{direct_model: $direct, router_model: $router}' >"$parked_file"
1056
+ chmod 600 "$parked_file"
1057
+ ;;
1058
+ esac
1059
+ fi
1060
+
981
1061
  # Merge into any existing opencode.json. We always overwrite provider.weave
982
1062
  # so re-install reflects the latest key/identity, but we leave the rest of the
983
- # file (other providers, mcp, agent settings) untouched. A previously
984
- # installed `weave/*` model is migrated to `weave/auto`; unrelated provider
985
- # choices stay untouched.
1063
+ # file (other providers, mcp, agent settings) untouched. The managed model is
1064
+ # always weave/auto; a prior direct choice was parked above for exact restore.
986
1065
  #
987
1066
  # The weave-claude login provider AND the plugin entry are written together
988
1067
  # only when the bundled plugin was present and copied ($plugin non-empty): the
@@ -1010,12 +1089,7 @@ write_opencode_config() {
1010
1089
  then (.plugin -= [$pluginspec]) | (if (.plugin | length) == 0 then del(.plugin) else . end)
1011
1090
  else . end)
1012
1091
  end)
1013
- | (if (.model // "") == "" then .model = "weave/auto" else . end)
1014
- # Replace any legacy Weave model choice with the single auto-routing
1015
- # choice. Models from unrelated providers remain unchanged.
1016
- | (if (.model // "" | tostring) as $model
1017
- | ($model | startswith("weave/") or startswith("weave-codex/") or startswith("weave-claude/"))
1018
- then .model = "weave/auto" else . end)
1092
+ | .model = "weave/auto"
1019
1093
  | (.["$schema"] //= "https://opencode.ai/config.json")
1020
1094
  ' "$config_file")"
1021
1095
  else
@@ -1301,11 +1375,21 @@ while [ $# -gt 0 ]; do
1301
1375
  [ "$scope" = "user" ] || [ "$scope" = "project" ] || { err "--scope must be 'user' or 'project'."; exit 2; }
1302
1376
  scope_explicit="true"
1303
1377
  ;;
1378
+ --context-window)
1379
+ context_window="${2:-}"
1380
+ [ "$context_window" = "1m" ] || { err "--context-window requires '1m'."; exit 2; }
1381
+ shift 2
1382
+ ;;
1304
1383
  --base-url)
1305
1384
  base_url="${2:-}"; shift 2
1306
1385
  [ -n "$base_url" ] || { err "--base-url requires a value."; exit 2; }
1307
1386
  base_url_explicit="true"
1308
1387
  ;;
1388
+ --email)
1389
+ email="${2:-}"; shift 2
1390
+ [ -n "$email" ] || { err "--email requires a value."; exit 2; }
1391
+ email_explicit="true"
1392
+ ;;
1309
1393
  --local)
1310
1394
  # Shorthand for local dev: localhost:8080 (matches `wv mr` / `make dev` default PORT).
1311
1395
  base_url="http://localhost:8080"
@@ -1399,6 +1483,14 @@ while [ $# -gt 0 ]; do
1399
1483
  esac
1400
1484
  done
1401
1485
 
1486
+ if [ -n "$context_window" ]; then
1487
+ if [ "$target" != "claude" ] || { [ "$mode" != "install" ] && [ "$mode" != "update" ]; }; then
1488
+ err "--context-window is supported only for Claude Code install/update."
1489
+ exit 2
1490
+ fi
1491
+ target_explicit="true"
1492
+ fi
1493
+
1402
1494
  if [ "$mode" = "setup" ]; then
1403
1495
  if [ "$setup_claude" != "true" ] && [ "$setup_codex" != "true" ]; then
1404
1496
  setup_claude="true"
@@ -1410,6 +1502,7 @@ if [ "$mode" = "setup" ]; then
1410
1502
  setup_args=(--scope "$scope")
1411
1503
  [ "$non_interactive" = "true" ] && setup_args+=(--non-interactive)
1412
1504
  [ "$base_url_explicit" = "true" ] && setup_args+=(--base-url "$base_url")
1505
+ [ "$email_explicit" = "true" ] && setup_args+=(--email "$email")
1413
1506
  [ -n "$install_dir" ] && setup_args+=(--dir "$install_dir")
1414
1507
  [ "$quiet" = "true" ] && setup_args+=(--quiet)
1415
1508
  [ "$rotate_key" = "true" ] && setup_args+=(--rotate-key)
@@ -1547,10 +1640,12 @@ fi
1547
1640
 
1548
1641
  # ---------- interactive scope prompt ----------
1549
1642
 
1550
- # If the user didn't pass --scope and we have a controlling terminal, ask which
1551
- # scope to install into. Non-interactive runs (CI, `curl | sh --non-interactive`)
1643
+ # If the user is installing and didn't pass --scope, ask which scope to write
1644
+ # into. Account, status, and model commands only read an existing install, so
1645
+ # they keep the user-scope default unless the caller explicitly selects a
1646
+ # project scope. Non-interactive installs (CI, `curl | sh --non-interactive`)
1552
1647
  # silently use the "user" default.
1553
- if [ -z "$install_dir" ] && [ "$scope_explicit" = "false" ] && [ "$non_interactive" = "false" ] && [ -r /dev/tty ]; then
1648
+ if [ "$mode" = "install" ] && [ -z "$install_dir" ] && [ "$scope_explicit" = "false" ] && [ "$non_interactive" = "false" ] && [ -r /dev/tty ]; then
1554
1649
  # Per-target paths so the prompt text matches what actually gets written.
1555
1650
  case "$target" in
1556
1651
  codex)
@@ -1831,6 +1926,10 @@ else
1831
1926
  fi
1832
1927
 
1833
1928
  if [ "$target" = "claude" ]; then
1929
+ statusline_install="true"
1930
+ statusline_source_file=""
1931
+ statusline_expected_command=""
1932
+ statusline_candidate_file=""
1834
1933
  case "$scope" in
1835
1934
  user)
1836
1935
  settings_dir="$settings_base/.claude"
@@ -1855,6 +1954,37 @@ if [ "$target" = "claude" ]; then
1855
1954
  fi
1856
1955
  ;;
1857
1956
  esac
1957
+ statusline_ownership_file="$statusline_file.weave-router"
1958
+
1959
+ # Check the effective settings layer. A user-owned statusline blocks the
1960
+ # install; a router-owned one is refreshed so reinstall repairs its script.
1961
+ if [ "$scope" = "project" ] \
1962
+ && claude_statusline_configured "$local_settings_file"; then
1963
+ statusline_source_file="$local_settings_file"
1964
+ statusline_expected_command="$statusline_path_for_settings"
1965
+ statusline_candidate_file="$statusline_file"
1966
+ elif claude_statusline_configured "$settings_file"; then
1967
+ statusline_source_file="$settings_file"
1968
+ statusline_expected_command="$statusline_path_for_settings"
1969
+ statusline_candidate_file="$statusline_file"
1970
+ elif [ "$scope" = "project" ] \
1971
+ && claude_statusline_configured "$HOME/.claude/settings.json"; then
1972
+ statusline_source_file="$HOME/.claude/settings.json"
1973
+ statusline_expected_command="$HOME/.weave/cc-statusline.sh"
1974
+ statusline_candidate_file="$HOME/.weave/cc-statusline.sh"
1975
+ fi
1976
+ if [ -n "$statusline_source_file" ] \
1977
+ && ! claude_statusline_router_owned \
1978
+ "$statusline_source_file" "$statusline_expected_command" \
1979
+ "$statusline_candidate_file" "$statusline_candidate_file.weave-router"; then
1980
+ statusline_install="false"
1981
+ skip "Existing Claude Code statusline detected; leaving it unchanged."
1982
+ fi
1983
+ if [ "$statusline_install" = "true" ] \
1984
+ && [ -f "$statusline_candidate_file" ] \
1985
+ && [ ! -w "$statusline_candidate_file" ]; then
1986
+ chmod u+rw "$statusline_candidate_file"
1987
+ fi
1858
1988
 
1859
1989
  # Symlink containment: refuse if any target path is a symlink. User-scope
1860
1990
  # paths under $HOME are trusted; project-scope and --dir paths come from a
@@ -1863,10 +1993,16 @@ if [ "$target" = "claude" ]; then
1863
1993
  refuse_if_symlink "$settings_dir"
1864
1994
  refuse_if_symlink "$settings_file"
1865
1995
  refuse_if_symlink "$local_settings_file"
1866
- refuse_if_symlink "$statusline_file"
1996
+ if [ "$statusline_install" = "true" ]; then
1997
+ refuse_if_symlink "$statusline_file"
1998
+ refuse_if_symlink "$statusline_ownership_file"
1999
+ fi
1867
2000
  fi
1868
2001
 
1869
- mkdir -p "$settings_dir" "$statusline_dir"
2002
+ mkdir -p "$settings_dir"
2003
+ if [ "$statusline_install" = "true" ]; then
2004
+ mkdir -p "$statusline_dir"
2005
+ fi
1870
2006
  elif [ "$target" = "codex" ]; then
1871
2007
  # Codex CLI reads config from ~/.codex/config.toml by default. For project
1872
2008
  # scope we write to <repo>/.codex/config.toml; the user invokes Codex with
@@ -2319,6 +2455,74 @@ gitignore_add() {
2319
2455
  fi
2320
2456
  }
2321
2457
 
2458
+ prepare_claude_context_window() {
2459
+ context_settings_file="$settings_file"
2460
+ if [ "$scope" = "project" ] && [ -z "$install_dir" ]; then
2461
+ context_settings_file="$local_settings_file"
2462
+ fi
2463
+ context_state_file="$settings_dir/.weave-context-window.json"
2464
+ refuse_if_symlink "$context_state_file"
2465
+ [ -n "$context_window" ] || return 0
2466
+
2467
+ local source model="" disabled="${CLAUDE_CODE_DISABLE_1M_CONTEXT:-}" env_model="${ANTHROPIC_MODEL:-}"
2468
+ # Highest-priority settings first; project opt-in is written only locally.
2469
+ for source in "$context_settings_file" "$settings_file" "$HOME/.claude/settings.json"; do
2470
+ [ -f "$source" ] || continue
2471
+ [ -n "$model" ] || model="$(json_get "$source" '.model')"
2472
+ [ -n "$disabled" ] || disabled="$(json_get "$source" '.env.CLAUDE_CODE_DISABLE_1M_CONTEXT')"
2473
+ [ -n "$env_model" ] || env_model="$(json_get "$source" '.env.ANTHROPIC_MODEL')"
2474
+ done
2475
+ if [ -n "$disabled" ] && [ "$disabled" != "0" ] && [ "$disabled" != "false" ]; then
2476
+ err "1M context is disabled in your Claude Code settings; remove that opt-out before using --context-window."
2477
+ return 1
2478
+ fi
2479
+ if [ -n "$env_model" ]; then
2480
+ err "ANTHROPIC_MODEL overrides the model setting. Select its [1m] variant there instead; leaving it unchanged."
2481
+ return 1
2482
+ fi
2483
+ model="${model:-sonnet}"
2484
+ case "$model" in
2485
+ *'[1m]') context_window=""; return 0 ;;
2486
+ sonnet|opus|fable|claude-sonnet-4-6|claude-sonnet-5|claude-opus-4-6|claude-opus-4-7|claude-opus-4-8|claude-opus-5|claude-fable-5|claude-fable-5-1) ;;
2487
+ *) err "Cannot assert 1M support for '$model'. Select a supported Sonnet/Opus/Fable model first; leaving it unchanged."; return 1 ;;
2488
+ esac
2489
+ context_managed_model="${model}[1m]"
2490
+ }
2491
+
2492
+ apply_claude_context_window() {
2493
+ local action="$1" active="$2" state="$settings_dir/.weave-context-window.json" merged tmp
2494
+ refuse_if_symlink "$state"
2495
+ [ -f "$active" ] || return 0
2496
+ if [ "$action" = "install" ] && [ -n "$context_window" ]; then
2497
+ tmp="$(mktemp "$settings_dir/.weave-context.XXXXXX")"
2498
+ jq --arg managed "$context_managed_model" '{had_model: has("model"), original: .model, managed: $managed}' "$active" >"$tmp"
2499
+ chmod 600 "$tmp"
2500
+ mv "$tmp" "$state"
2501
+ merged="$(jq --arg model "$context_managed_model" '.model = $model' "$active")"
2502
+ else
2503
+ [ -f "$state" ] || return 0
2504
+ merged="$(jq --slurpfile state "$state" --arg action "$action" '
2505
+ $state[0] as $s |
2506
+ if $action == "off" then
2507
+ if .model == $s.managed then
2508
+ if $s.had_model then .model = $s.original else del(.model) end
2509
+ else . end
2510
+ elif has("model") == $s.had_model and .model == $s.original then .model = $s.managed
2511
+ else . end
2512
+ ' "$active")"
2513
+ fi
2514
+ tmp="$(mktemp "$settings_dir/.weave-context.XXXXXX")"
2515
+ printf '%s\n' "$merged" >"$tmp"
2516
+ chmod 600 "$tmp"
2517
+ mv "$tmp" "$active"
2518
+ if [ "$action" = "install" ] && [ -n "$context_window" ]; then
2519
+ ok "Claude Code model set to $context_managed_model; automatic compaction stays enabled. Restart Claude Code."
2520
+ if [ "$scope" = "project" ] && [ -z "$install_dir" ]; then
2521
+ gitignore_add ".claude/.weave-context-window.json"
2522
+ fi
2523
+ fi
2524
+ }
2525
+
2322
2526
  toggle_claude() {
2323
2527
  local parked="$settings_dir/.weave-parked.json"
2324
2528
  local proj="false" active committed_base local_base parked_env merged
@@ -2382,6 +2586,7 @@ toggle_claude() {
2382
2586
  info "Claude Code isn't configured for the router. Run the installer first."
2383
2587
  return 0
2384
2588
  fi
2589
+ apply_claude_context_window off "$active"
2385
2590
  if [ "$proj" = "true" ]; then
2386
2591
  # Park the whole local env (carries the key header), then override the
2387
2592
  # base URL to Anthropic in the local file only — committed settings.json
@@ -2467,6 +2672,7 @@ toggle_claude() {
2467
2672
  merged="$(jq --argjson p "$parked_env" '.env = (((.env // {}) | del(.ANTHROPIC_BASE_URL)) + $p)' "$active")"
2468
2673
  printf '%s\n' "$merged" >"$active"
2469
2674
  [ "$proj" = "true" ] && chmod 600 "$active"
2675
+ apply_claude_context_window on "$active"
2470
2676
  rm -f "$parked"
2471
2677
  ok "Claude Code is ${C_BOLD}on${C_RESET} (routing through the Weave Router). Restart Claude Code for it to take effect."
2472
2678
  ;;
@@ -2534,20 +2740,22 @@ toggle_codex() {
2534
2740
 
2535
2741
  toggle_opencode() {
2536
2742
  local f="$opencode_config_file" parked="$opencode_dir/.weave-parked.json"
2537
- local model="" has_weave="false" parked_present="false" on="false" restore_model merged
2743
+ local model="" direct_model="" router_model="weave/auto" has_weave="false" parked_present="false" on="false" merged model_id
2538
2744
  # Symlink containment for the parked sidecar — `off` writes it via shell
2539
2745
  # redirection; a hostile project repo could pre-place it as a symlink. The
2540
2746
  # opencode.json itself is already guarded during path resolution.
2541
2747
  if [ "$scope" = "project" ] || [ -n "$install_dir" ]; then
2542
2748
  refuse_if_symlink "$parked"
2543
2749
  fi
2544
- if [ -f "$parked" ]; then parked_present="true"; fi
2750
+ if [ -f "$parked" ]; then
2751
+ parked_present="true"
2752
+ direct_model="$(json_get "$parked" '.direct_model')"
2753
+ router_model="$(jq -r '.router_model // .model // "weave/auto"' "$parked" 2>/dev/null || echo "weave/auto")"
2754
+ fi
2545
2755
  if [ -f "$f" ]; then
2546
- model="$(jq -r '.model // empty' "$f" 2>/dev/null || true)"
2756
+ model="$(json_get "$f" '.model')"
2547
2757
  if [ "$(jq -r '((.provider // {}) | has("weave"))' "$f" 2>/dev/null || true)" = "true" ]; then has_weave="true"; fi
2548
2758
  fi
2549
- # A managed model counts as router-on: the Responses-shaped `weave/…` default,
2550
- # or a legacy `weave-codex/…` model parked by a pre-upgrade install.
2551
2759
  case "$model" in weave/* | weave-codex/*) on="true" ;; esac
2552
2760
 
2553
2761
  case "$mode" in
@@ -2565,52 +2773,48 @@ toggle_opencode() {
2565
2773
  if [ "$has_weave" = "true" ]; then ok "opencode is already off — nothing to do."; else info "opencode isn't configured for the router. Run the installer first."; fi
2566
2774
  return 0
2567
2775
  fi
2568
- jq '{model: .model}' "$f" >"$parked"
2776
+ router_model="$model"
2777
+ jq -n --arg direct "$direct_model" --arg router "$router_model" \
2778
+ '{direct_model: $direct, router_model: $router}' >"$parked"
2569
2779
  chmod 600 "$parked"
2570
- merged="$(jq 'del(.model)' "$f")"
2780
+ if [ -n "$direct_model" ]; then
2781
+ merged="$(jq --arg model "$direct_model" '.model = $model' "$f")"
2782
+ else
2783
+ merged="$(jq 'del(.model)' "$f")"
2784
+ fi
2571
2785
  printf '%s\n' "$merged" >"$f"
2572
2786
  chmod 600 "$f"
2573
2787
  gitignore_add ".weave-parked.json"
2574
- ok "opencode is ${C_BOLD}off${C_RESET} — pick a non-Weave model with /models. Takes effect on your next opencode run."
2788
+ if [ -n "$direct_model" ]; then
2789
+ ok "opencode is ${C_BOLD}off${C_RESET} — restored $direct_model. Takes effect on your next opencode run."
2790
+ else
2791
+ ok "opencode is ${C_BOLD}off${C_RESET} — choose a direct model with /models. Takes effect on your next opencode run."
2792
+ fi
2575
2793
  ;;
2576
2794
  on)
2577
2795
  if [ "$on" = "true" ]; then ok "opencode is already on — nothing to do."; return 0; fi
2578
- restore_model="weave/auto"
2579
- if [ "$parked_present" = "true" ]; then
2580
- restore_model="$(jq -r '.model // "weave/auto"' "$parked")"
2581
- elif [ "$has_weave" != "true" ]; then
2582
- warn "opencode isn't configured for the router. Run the installer first."; return 0
2583
- else
2584
- # No parked model (sidecar deleted by hand). Derive the default from the
2585
- # installed provider.weave.models block rather than a hardcoded literal
2586
- # that silently diverges when the installer's default changes — prefer
2587
- # the auto-routing entry, else the first model the installer registered.
2588
- restore_model="$(jq -r '
2589
- (.provider.weave.models // {} | keys) as $k
2590
- | (([$k[] | select(. == "auto")] | first) // $k[0] // "auto")
2591
- | "weave/" + .
2592
- ' "$f" 2>/dev/null || echo "weave/auto")"
2796
+ if [ "$has_weave" != "true" ]; then
2797
+ warn "opencode isn't configured for the router. Run the installer first."
2798
+ return 0
2593
2799
  fi
2594
- # Restore only a registered `weave` model. Legacy installations may have
2595
- # parked a pinned model, but a current install exposes only `weave/auto`.
2596
- case "$restore_model" in
2800
+ case "$model" in ""|weave/*|weave-codex/*) ;; *) direct_model="$model" ;; esac
2801
+ case "$router_model" in
2597
2802
  weave/*)
2598
- local model_id="${restore_model#weave/}"
2803
+ model_id="${router_model#weave/}"
2599
2804
  if ! jq -e --arg id "$model_id" '(.provider.weave.models // {}) | has($id)' "$f" >/dev/null 2>&1; then
2600
- restore_model="weave/auto"
2601
- fi
2602
- ;;
2603
- weave-codex/*)
2604
- if [ "$(jq -r '((.provider // {}) | has("weave-codex"))' "$f" 2>/dev/null || true)" != "true" ]; then
2605
- restore_model="weave/auto"
2805
+ router_model="weave/auto"
2606
2806
  fi
2607
2807
  ;;
2808
+ *) router_model="weave/auto" ;;
2608
2809
  esac
2609
- merged="$(jq --arg m "$restore_model" '.model = $m' "$f")"
2810
+ merged="$(jq --arg model "$router_model" '.model = $model' "$f")"
2610
2811
  printf '%s\n' "$merged" >"$f"
2611
2812
  chmod 600 "$f"
2612
- rm -f "$parked"
2613
- ok "opencode is ${C_BOLD}on${C_RESET} (default model $restore_model via the Weave Router). Takes effect on your next opencode run."
2813
+ jq -n --arg direct "$direct_model" --arg router "$router_model" \
2814
+ '{direct_model: $direct, router_model: $router}' >"$parked"
2815
+ chmod 600 "$parked"
2816
+ gitignore_add ".weave-parked.json"
2817
+ ok "opencode is ${C_BOLD}on${C_RESET} (default model $router_model via the Weave Router). Takes effect on your next opencode run."
2614
2818
  ;;
2615
2819
  esac
2616
2820
  }
@@ -3464,28 +3668,37 @@ EOF
3464
3668
  src="$commands_src_dir/$cmd.md"
3465
3669
  dst="$dst_dir/$cmd.md"
3466
3670
  [ -f "$src" ] || continue
3467
- if [ "$scope" = "project" ] || [ -n "$install_dir" ]; then
3468
- refuse_if_symlink "$dst"
3469
- fi
3470
3671
  # Substitute the {{SCOPE}} placeholder (only the router-* wrappers carry it;
3471
3672
  # cp-equivalent for the others since the token is absent).
3472
3673
  local body; body="$(cat "$src")"
3473
3674
  body="${body//\{\{SCOPE\}\}/$scope_args}"
3675
+ ownership_file="$dst.weave-router"
3676
+ if [ "$scope" = "project" ] || [ -n "$install_dir" ]; then
3677
+ refuse_if_symlink "$dst"
3678
+ refuse_if_symlink "$ownership_file"
3679
+ fi
3474
3680
  # Wrappers installed before ownership markers existed carry no marker. A
3475
3681
  # file whose body still matches what this installer would write is one of
3476
3682
  # ours from an older version, so adopt it rather than treating it as
3477
3683
  # user-owned — otherwise every pre-marker install is stranded: never
3478
3684
  # refreshed, never uninstalled, and skipped by the statusline refresh.
3479
- # Anything that does not match byte-for-byte is left alone.
3685
+ # The sidecar comparison also recognizes a router-owned wrapper after its
3686
+ # bundled body changes, while a user edit breaks that comparison.
3480
3687
  if [ -e "$dst" ] && { [ ! -f "$dst" ] || ! grep -Fq "<!-- weave-router managed command: $cmd -->" "$dst"; }; then
3481
3688
  if [ -f "$dst" ] && [ "$(cat "$dst")" = "$body" ]; then
3482
3689
  : # an unmarked copy of our own wrapper; fall through and adopt it
3690
+ elif [ -f "$ownership_file" ] \
3691
+ && [ "$(sed -n '1p' "$ownership_file")" = "weave-router managed command: $cmd" ] \
3692
+ && [ "$(sed '1d' "$ownership_file")" = "$(cat "$dst")" ]; then
3693
+ : # a markerless wrapper whose sidecar still matches its body
3483
3694
  else
3484
3695
  warn "A user-owned $target command already exists at $dst; leaving it untouched."
3485
3696
  continue
3486
3697
  fi
3487
3698
  fi
3488
- printf '%s\n<!-- weave-router managed command: %s -->' "$body" "$cmd" >"$dst"
3699
+ printf '%s\n' "$body" >"$dst"
3700
+ # Keep ownership metadata in a sidecar so Claude Code never sends it as prompt text.
3701
+ printf 'weave-router managed command: %s\n%s\n' "$cmd" "$body" >"$ownership_file"
3489
3702
  done
3490
3703
  seed_command_baseline "$commands_src_dir" "$cmds"
3491
3704
  ok "Slash commands written to $dst_dir ($installed)"
@@ -4486,7 +4699,6 @@ if [ "$target" = "codex" ]; then
4486
4699
  ok "Codex config written to $codex_config_file"
4487
4700
  remove_obsolete_codex_prompt_wrappers "$codex_dir/prompts"
4488
4701
  install_codex_prompt_skills
4489
- info "Codex router directives: begin the message with one space, e.g. ' /force-model gpt-5.6-terra'."
4490
4702
 
4491
4703
  # Project scope: ensure the per-teammate config (which holds the router key)
4492
4704
  # is gitignored. The base URL is the same for every teammate, so a
@@ -4535,7 +4747,9 @@ if [ "$target" = "opencode" ]; then
4535
4747
  refuse_if_symlink "$gitignore"
4536
4748
  for entry in \
4537
4749
  "opencode.json" \
4538
- ".weave/"
4750
+ ".weave/" \
4751
+ ".weave-parked.json" \
4752
+ ".opencode/commands/*.weave-router"
4539
4753
  do
4540
4754
  if [ ! -f "$gitignore" ] || ! grep -qxF "$entry" "$gitignore"; then
4541
4755
  printf '%s\n' "$entry" >>"$gitignore"
@@ -4552,11 +4766,9 @@ if [ "$target" = "opencode" ]; then
4552
4766
  #
4553
4767
  # Router on/off/status are not installed — they run npx shell commands
4554
4768
  # specific to the Claude Code settings model and don't apply to opencode.
4555
- if [ "$scope" = "project" ]; then
4769
+ if [ "$scope" = "project" ] || [ -n "$install_dir" ]; then
4556
4770
  opencode_commands_dir="$opencode_dir/.opencode/commands"
4557
4771
  else
4558
- # User scope and --dir installs: use the global commands path that opencode
4559
- # discovers regardless of working directory.
4560
4772
  opencode_commands_dir="${XDG_CONFIG_HOME:-$HOME/.config}/opencode/commands"
4561
4773
  fi
4562
4774
  install_slash_commands "$opencode_commands_dir"
@@ -4574,12 +4786,12 @@ if [ "$target" = "opencode" ]; then
4574
4786
  # can outlive a plugin-less re-install that stripped the provider, which would
4575
4787
  # make these instructions misleading.
4576
4788
  if jq -e '(.provider // {}) | has("weave-claude")' "$opencode_config_file" >/dev/null 2>&1; then
4577
- info "Optional: connect your AI plans so they pay for the matching turns. Run ${C_BOLD}opencode auth login${C_RESET} → ${C_BOLD}Weave Router${C_RESET} for ${C_BOLD}ChatGPT Pro/Plus${C_RESET} (GPT/Codex turns) and/or ${C_BOLD}Weave Router — Claude plan${C_RESET} for ${C_BOLD}Claude Pro/Max${C_RESET} (Claude turns). The router still routes every turn; your Weave key pays for the rest."
4789
+ info "Optional: connect your AI plans so they pay for the matching turns. Run ${C_BOLD}opencode auth login${C_RESET} → ${C_BOLD}Weave Router — Codex plan${C_RESET} for ${C_BOLD}ChatGPT Pro/Plus${C_RESET} (GPT/Codex turns) and/or ${C_BOLD}Weave Router — Claude plan${C_RESET} for ${C_BOLD}Claude Pro/Max${C_RESET} (Claude turns). The router still routes every turn; your Weave key pays for the rest."
4578
4790
  fi
4579
4791
  if [ -n "$install_dir" ]; then
4580
4792
  # --dir installs land outside opencode's discovery roots, so the caller
4581
4793
  # has to point opencode at the file explicitly.
4582
- info "Run opencode with OPENCODE_CONFIG=$opencode_config_file opencode."
4794
+ info "Run opencode with OPENCODE_CONFIG=$opencode_config_file OPENCODE_CONFIG_DIR=$opencode_dir/.opencode opencode."
4583
4795
  fi
4584
4796
  [ "$mode" = "update" ] || print_uninstall_hint
4585
4797
  exit 0
@@ -4642,6 +4854,7 @@ fi
4642
4854
 
4643
4855
  # ---------- write the statusline script ----------
4644
4856
 
4857
+ if [ "$statusline_install" = "true" ]; then
4645
4858
  cat > "$statusline_file" << 'STATUSLINE_EOF'
4646
4859
  #!/usr/bin/env bash
4647
4860
  #
@@ -4926,8 +5139,13 @@ weave_sync_commands() {
4926
5139
  installed_body="$(sed '/^<!-- weave-router managed command: .* -->$/d' "$installed" 2>/dev/null)" || installed_body=""
4927
5140
  if [ "$prev_body" = "$installed_body" ] && [ "$new_body" != "$installed_body" ]; then
4928
5141
  tmp="$installed.tmp.$$"
4929
- if printf '%s\n<!-- weave-router managed command: %s -->' "$new_body" "$name" >"$tmp" 2>/dev/null; then
5142
+ if printf '%s\n' "$new_body" >"$tmp" 2>/dev/null; then
4930
5143
  mv "$tmp" "$installed" 2>/dev/null || rm -f "$tmp"
5144
+ ownership_file="$installed.weave-router"
5145
+ if [ ! -L "$ownership_file" ]; then
5146
+ printf 'weave-router managed command: %s\n%s\n' "$name" "$new_body" \
5147
+ >"$ownership_file" 2>/dev/null || true
5148
+ fi
4931
5149
  else
4932
5150
  rm -f "$tmp"
4933
5151
  fi
@@ -5603,7 +5821,10 @@ else
5603
5821
  fi
5604
5822
  STATUSLINE_EOF
5605
5823
  chmod +x "$statusline_file"
5824
+ printf '%s\n' "$CLAUDE_STATUSLINE_MARKER" >"$statusline_ownership_file"
5825
+ chmod 600 "$statusline_ownership_file"
5606
5826
  ok "Statusline installed at $statusline_file"
5827
+ fi
5607
5828
 
5608
5829
  # ---------- patch settings.json ----------
5609
5830
 
@@ -5623,6 +5844,7 @@ trap '_spin_cleanup; rm -f "$tmp_patch"' EXIT INT TERM HUP
5623
5844
  # replacement key without re-running the whole install.
5624
5845
  write_claude_settings() {
5625
5846
  local block_key="$1"
5847
+ prepare_claude_context_window
5626
5848
 
5627
5849
  # Claude Code splits ANTHROPIC_CUSTOM_HEADERS on newlines, so multiple headers
5628
5850
  # ride in the same env var separated by \n. Append identity headers alongside
@@ -5642,26 +5864,26 @@ write_claude_settings() {
5642
5864
  # Force tool-search deferral to match first-party Claude Code; "auto" can inline
5643
5865
  # every tool schema when the custom endpoint advertises a 200K context window.
5644
5866
  if [ "$scope" = "project" ] && [ -z "$install_dir" ]; then
5645
- jq -n --arg url "$base_url" --arg sl "$statusline_path_for_settings" '{
5867
+ jq -n --arg url "$base_url" --arg sl "$statusline_path_for_settings" --arg statusline_install "$statusline_install" '{
5646
5868
  env: { ANTHROPIC_BASE_URL: $url, ENABLE_TOOL_SEARCH: "true" },
5647
- statusLine: { type: "command", command: $sl },
5648
5869
  attribution: {
5649
5870
  commit: "Co-Authored-By: Weave Router <router@workweave.ai>",
5650
5871
  pr: "🤖 Generated with [Weave Router](https://router.workweave.ai)"
5651
5872
  }
5652
- }' >"$tmp_patch"
5873
+ } + (if $statusline_install == "true" then {statusLine: { type: "command", command: $sl }} else {} end)' >"$tmp_patch"
5653
5874
  else
5654
- jq -n --arg url "$base_url" --arg header "$custom_headers" --arg sl "$statusline_path_for_settings" '{
5875
+ jq -n --arg url "$base_url" --arg header "$custom_headers" --arg sl "$statusline_path_for_settings" --arg statusline_install "$statusline_install" '{
5655
5876
  env: { ANTHROPIC_BASE_URL: $url, ANTHROPIC_CUSTOM_HEADERS: $header, ENABLE_TOOL_SEARCH: "true" },
5656
- statusLine: { type: "command", command: $sl },
5657
5877
  attribution: {
5658
5878
  commit: "Co-Authored-By: Weave Router <router@workweave.ai>",
5659
5879
  pr: "🤖 Generated with [Weave Router](https://router.workweave.ai)"
5660
5880
  }
5661
- }' >"$tmp_patch"
5881
+ } + (if $statusline_install == "true" then {statusLine: { type: "command", command: $sl }} else {} end)' >"$tmp_patch"
5662
5882
  fi
5663
5883
 
5664
- # Merge with existing settings. Deep-merge env and replace statusLine.
5884
+ # Merge with existing settings. Deep-merge env and replace statusLine only
5885
+ # when this install owns the statusline slot; otherwise the patch omits it so
5886
+ # the user's existing statusline survives unchanged.
5665
5887
  # We strip router-owned auth from the existing settings BEFORE merging —
5666
5888
  # otherwise switching auth mode (key→dev-mode) would leave stale credentials
5667
5889
  # behind. ANTHROPIC_AUTH_TOKEN/apiKeyHelper are also removed to migrate older
@@ -5705,6 +5927,7 @@ write_claude_settings() {
5705
5927
  chmod 600 "$local_settings_file"
5706
5928
  ok "Router key header written to $local_settings_file"
5707
5929
  fi
5930
+ apply_claude_context_window install "$context_settings_file"
5708
5931
  }
5709
5932
 
5710
5933
  # write_claude_settings rewrites the full router config live, so a parked
@@ -5730,13 +5953,17 @@ if [ "$scope" = "project" ] && [ -z "$install_dir" ] && [ -n "${git_root:-}" ];
5730
5953
  # Same symlink containment as the .claude/ paths above: a hostile repo could
5731
5954
  # commit .gitignore as a symlink so the >> below writes outside the repo.
5732
5955
  refuse_if_symlink "$gitignore"
5733
- # Keep the statusline script and per-teammate local settings out of git. The
5734
- # local settings carry the router key header; each teammate gets their own.
5956
+ # Keep the router statusline script (when installed) and per-teammate local
5957
+ # settings out of git. The local settings carry the router key header; each
5958
+ # teammate gets their own.
5735
5959
  for entry in \
5736
5960
  ".claude/settings.local.json" \
5737
5961
  ".claude/.credentials.json" \
5738
- ".claude/cc-statusline.sh"
5962
+ ".claude/cc-statusline.sh" \
5963
+ ".claude/cc-statusline.sh.weave-router" \
5964
+ ".claude/commands/*.weave-router"
5739
5965
  do
5966
+ [[ "$entry" == .claude/cc-statusline.sh* ]] && [ "$statusline_install" != "true" ] && continue
5740
5967
  if [ ! -f "$gitignore" ] || ! grep -qxF "$entry" "$gitignore"; then
5741
5968
  printf '%s\n' "$entry" >>"$gitignore"
5742
5969
  fi