146146DIRECTIVES = {"planner" : None , "strip" : True } # filled from config in main()
147147_ROUTE_ALIASES = {} # normalized token -> concrete route id
148148
149+ # 1M context window: Claude Code sizes its context meter (and auto-compaction) to
150+ # 1M only when the model id it holds carries a "[1m]" suffix. For a real-Claude
151+ # passthrough route whose upstream model is 1M-capable, we ADVERTISE the picker id
152+ # with that suffix on /v1/models + /healthz, so even an in-session /model switch
153+ # (not just a launch-time pick) gets the 1M window. The suffix is a client-side
154+ # convention, not an Anthropic model id: it is stripped before routing and
155+ # normalized off the sticky orchestrator/worker selection, so internal ids stay
156+ # clean. Disable with UC_ADVERTISE_1M=0. See docs/DIRECTIVES.md / PR #8 + #10.
157+ _ONEM_SUFFIX = "[1m]"
158+ ADVERTISE_1M = os .environ .get ("UC_ADVERTISE_1M" , "1" ) != "0"
159+ _CONTEXT_1M_UPSTREAM = set (t .strip () for t in os .environ .get (
160+ "UC_1M_UPSTREAM" ,
161+ "claude-opus-4-8,claude-opus-4-7,claude-opus-4-6,claude-sonnet-4-6" ).split ("," ) if t .strip ())
162+
163+
164+ def _strip_1m (mid ):
165+ """Model id without a trailing [1m] window suffix (the client convention)."""
166+ if isinstance (mid , str ) and mid .endswith (_ONEM_SUFFIX ):
167+ return mid [:- len (_ONEM_SUFFIX )]
168+ return mid
169+
170+
171+ def _advertise_id (model_entry ):
172+ """The id to advertise for a configured model on /v1/models + /healthz. Appends
173+ [1m] when ADVERTISE_1M is on and the model is a real-Claude PASSTHROUGH route to
174+ a 1M-capable upstream model, so Claude Code renders the 1M window for it (incl.
175+ in-session /model picks). Worker entries and non-passthrough routes are returned
176+ unchanged. Never raises."""
177+ mid = model_entry .get ("id" ) if isinstance (model_entry , dict ) else None
178+ if not (ADVERTISE_1M and isinstance (mid , str )):
179+ return mid
180+ if mid .endswith (_ONEM_SUFFIX ) or mid .startswith (WORKER_ID_PREFIX ):
181+ return mid
182+ slot = UC_SLOT_MAP .get (mid )
183+ if not isinstance (slot , dict ) or slot .get ("type" ) not in (None , "anthropic" ):
184+ return mid # passthrough (real Claude) only
185+ if (slot .get ("model" ) or mid ) in _CONTEXT_1M_UPSTREAM :
186+ return mid + _ONEM_SUFFIX
187+ return mid
188+
149189try :
150190 UC_MODEL_MAP = json .loads (os .environ .get ("UC_MODEL_MAP" , "" ) or "{}" )
151191 if not isinstance (UC_MODEL_MAP , dict ):
@@ -566,6 +606,7 @@ def _set_selection(orch=None, worker=None):
566606 """Directly pre-set the sticky orchestrator/worker selection (used by the
567607 two-column pre-launch selector via POST /uc/select). Either may be None to
568608 leave that tier unchanged. Returns the resolved active selection dict."""
609+ orch , worker = _strip_1m (orch ), _strip_1m (worker ) # selections store clean ids
569610 with _SEL_LOCK :
570611 if orch is not None :
571612 _ACTIVE ["orch" ] = orch or None
@@ -584,6 +625,7 @@ def _select_target(mid, tier: str):
584625 fresh sessions behave exactly as before."""
585626 if not ORCH_WORKER :
586627 return mid
628+ mid = _strip_1m (mid ) # a [1m]-suffixed pick maps to its clean route id
587629 with _SEL_LOCK :
588630 if mid in _WORKER_MAP :
589631 _ACTIVE ["worker" ] = _WORKER_MAP [mid ]
@@ -893,14 +935,14 @@ def transform_messages_body(raw: bytes):
893935 route = {}
894936
895937 # 1M context window: Claude Code appends a "[1m]" suffix to a model id to ask
896- # the client for the 1M window (it also sends the context-1m beta header; see
897- # the launchers' UC_FORCE_1M / [1m] handling). That suffix is a client-side
898- # convention, not an Anthropic model id, so it must not reach routing ( it would
899- # not match a configured route or an orchestrator/worker pick) or the upstream.
900- # Strip it up front so "<id>[1m]" behaves exactly like "<id>" everywhere below;
901- # the 1M window is unaffected because it rides the beta header, left untouched.
902- if isinstance ( model_before , str ) and model_before . endswith ( "[1m]" ) :
903- model_before = model_before [: - len ( "[1m]" )]
938+ # the client for the 1M window (it also sends the context-1m beta header). That
939+ # suffix is a client-side convention, not an Anthropic model id, so it must not
940+ # reach routing (it wouldn't match a route) or the upstream. Strip it up front
941+ # so "<id>[1m]" behaves exactly like "<id>" everywhere below; the 1M window is
942+ # unaffected because it comes from the beta header, which we leave untouched.
943+ stripped = _strip_1m ( model_before )
944+ if stripped != model_before :
945+ model_before = stripped
904946 body ["model" ] = model_before
905947 changed = True
906948
@@ -1918,7 +1960,7 @@ def _maybe_health(self) -> bool:
19181960 "candidates" : [{"id" : c ["id" ], "cost" : c .get ("cost" )}
19191961 for c in _router_available_candidates ()],
19201962 },
1921- "custom_models" : [{"id" : m [ "id" ] , "display_name" : m ["display_name" ]}
1963+ "custom_models" : [{"id" : _advertise_id ( m ) , "display_name" : m ["display_name" ]}
19221964 for m in UC_MODELS ],
19231965 "stock_models" : [{"id" : m ["id" ], "display_name" : m ["display_name" ]}
19241966 for m in _stock_models ()],
@@ -2036,9 +2078,10 @@ def _handle_models(self) -> bool:
20362078 existing .add (m ["id" ])
20372079 stock_families .add (fam )
20382080 for m in UC_MODELS :
2039- if m ["id" ] not in existing :
2040- data .append (dict (m ))
2041- existing .add (m ["id" ])
2081+ adv = _advertise_id (m )
2082+ if adv not in existing :
2083+ data .append ({** m , "id" : adv })
2084+ existing .add (adv )
20422085 self ._raw (200 , "application/json" , json .dumps (base ).encode ("utf-8" ))
20432086 return True
20442087
0 commit comments