Преглед изворни кода

Sync SiliconFlow stack after DashScope squash merge

saladday пре 2 месеци
родитељ
комит
23aacf0b72
44 измењених фајлова са 1525 додато и 181 уклоњено
  1. 35 0
      docs/examples.md
  2. 19 0
      docs/tui-capabilities.md
  3. 8 17
      packages/local-runtime-v2/src/application/session/process-local-application-contract.ts
  4. 5 13
      packages/local-runtime-v2/src/application/session/process-local-application.ts
  5. 1 0
      packages/local-runtime-v2/src/service/model-system/contracts.ts
  6. 25 0
      packages/local-runtime-v2/src/service/model-system/management/service-input.test.ts
  7. 3 0
      packages/local-runtime-v2/src/service/model-system/management/service-input.ts
  8. 13 0
      packages/local-runtime-v2/src/service/model-system/resolution/model-ref.test.ts
  9. 11 0
      packages/local-runtime-v2/src/service/turn-system/agent-host/runner/local-agent-turn-runner.ts
  10. 39 20
      packages/local-runtime/src/content-safety/api-v2.ts
  11. 34 41
      packages/local-runtime/src/content-safety/api.ts
  12. 27 0
      packages/local-runtime/src/content-safety/diagnostics.ts
  13. 11 0
      packages/local-runtime/src/runtime/host.ts
  14. 62 38
      packages/local-runtime/test/unit/content-safety-api-fail-policy.test.ts
  15. 83 1
      packages/local-runtime/test/unit/content-safety-api-v2.test.ts
  16. 88 1
      packages/local-runtime/test/unit/local-runtime-host-output-safety-concurrency.test.ts
  17. 50 4
      packages/shared/src/safety-check-v2.ts
  18. 3 1
      packages/tui/docs/status-line-config.md
  19. 14 0
      packages/tui/src/cli/contract.ts
  20. 7 1
      packages/tui/src/cli/environment.ts
  21. 2 0
      packages/tui/src/cli/main.ts
  22. 3 0
      packages/tui/src/cli/program.ts
  23. 2 0
      packages/tui/src/cli/provider-command.ts
  24. 3 0
      packages/tui/src/provider/contract.ts
  25. 1 1
      packages/tui/src/tui/controller/product/command-flow.ts
  26. 1 1
      packages/tui/src/tui/controller/runtime/runtime-event-flow.ts
  27. 11 4
      packages/tui/src/tui/engine/LOCAL_CHANGES.json
  28. 9 0
      packages/tui/src/tui/engine/LOCAL_CHANGES.md
  29. 8 2
      packages/tui/src/tui/engine/components/scroll-view.ts
  30. 42 18
      packages/tui/src/tui/engine/tui-alt-screen.ts
  31. 1 0
      packages/tui/src/tui/features/settings/status-line-copy.ts
  32. 27 3
      packages/tui/src/tui/launcher.ts
  33. 2 0
      packages/tui/src/tui/shell/chat-layout.ts
  34. 20 8
      packages/tui/src/tui/shell/chrome.ts
  35. 4 1
      packages/tui/src/tui/shell/status-line-items.ts
  36. 247 5
      packages/tui/test/unit/tui-app.test.ts
  37. 305 0
      packages/tui/test/unit/tui-scrollbar-interaction.test.ts
  38. 191 0
      packages/tui/test/unit/tui-status-line-items.test.ts
  39. 29 0
      packages/tui/test/unit/tui-workspace-status-line.test.ts
  40. 14 1
      packages/tui/test/unit/tui/controller/product/command-flow.test.ts
  41. 17 0
      packages/tui/test/unit/tui/features/settings/status-line-picker.test.ts
  42. 2 0
      release/public-source.json
  43. 45 0
      test/byok.test.mjs
  44. 1 0
      test/vitest-suites.json

+ 35 - 0
docs/examples.md

@@ -90,6 +90,41 @@ custom_provider:
 
 
 ## 3. Search and image input
 ## 3. Search and image input
 
 
+For a custom BYOK model, declare image input support explicitly when adding the
+provider. Use this only if the selected provider endpoint and model accept images:
+
+```bash
+pnpm mcode provider add --name my-vision-provider --base-url https://example.com/v1 \
+  --api-format openai-completions --model my-vision-model \
+  --api-key-env MCODE_PROVIDER_API_KEY --support-image --use
+```
+
+`--support-image` applies to every repeated `--model` and saves
+`capabilities.support_image: true`. Without it, adding an unknown custom model
+does not infer image support from its name. `--use` and `provider test` check
+connectivity with a text request; they do not verify vision support.
+
+For an existing provider, close MCode and add the capability to the matching model
+in the active profile's `config.yaml` (normally `~/.minimax/config.yaml`; see
+[Accounts and data](installation.md#accounts-and-data) for profiles and overrides).
+Merge this fragment into the existing provider and keep its other settings:
+
+```yaml
+custom_provider:
+  my-vision-provider:
+    models:
+      my-vision-model:
+        capabilities:
+          support_image: true
+```
+
+The existing `modalities: { input: [text, image], output: [text] }` model setting
+also enables image input. Either declaration is sufficient. To make a model
+text-only again, remove `image` from `modalities.input` and remove or set
+`capabilities.support_image` to `false`. Generic `attachment: true` alone does not
+declare image input support. Restart MCode after editing configuration and select
+the configured model with `/provider`, or use `exec --model` for a single run.
+
 After signing in to MiniMax, try a task that explicitly requires search:
 After signing in to MiniMax, try a task that explicitly requires search:
 
 
 > Use web_search to find the official Node.js test runner documentation. Summarize how to run tests and include the source URL. If the tool is unavailable, say so.
 > Use web_search to find the official Node.js test runner documentation. Summarize how to run tests and include the source URL. If the tool is unavailable, say so.

+ 19 - 0
docs/tui-capabilities.md

@@ -46,3 +46,22 @@ Every collected diagnostic artifact, including prioritized session artifacts, is
 There is no raw-attachment upload option in this flow. Prompts, conversation text, tool arguments/results, command output, workspace excerpts, raw errors and unknown fields are excluded even if they contain no recognizable credential pattern. This intentionally reduces diagnostic detail: reproducing an exact response or inspecting an original stack is not possible from these uploads. Future raw attachments would require a separate, explicit review and consent surface describing their contents and scope.
 There is no raw-attachment upload option in this flow. Prompts, conversation text, tool arguments/results, command output, workspace excerpts, raw errors and unknown fields are excluded even if they contain no recognizable credential pattern. This intentionally reduces diagnostic detail: reproducing an exact response or inspecting an original stack is not possible from these uploads. Future raw attachments would require a separate, explicit review and consent surface describing their contents and scope.
 
 
 The regression tests use temporary synthetic files and intercepted HTTP only. They decrypt the final automatic-report request as a receiver would, and unzip the actual feedback PUT body after real session-report collection. They do not validate production ingestion of the new schemas, retention policies, live services, other telemetry paths or other platforms.
 The regression tests use temporary synthetic files and intercepted HTTP only. They decrypt the final automatic-report request as a receiver would, and unzip the actual feedback PUT body after real session-report collection. They do not validate production ingestion of the new schemas, retention policies, live services, other telemetry paths or other platforms.
+
+
+## Interactive startup model
+
+Use `mcode -m <provider-id>/<model-id>` or
+`mcode "Fix the failing tests" --model <provider-id>/<model-id>` to select the
+model for the startup Session. References use the same syntax as `exec --model`,
+including `custom_provider:<id>/<model-id>`, model IDs containing `/`, and the
+optional `#variant` suffix. The provider must already be configured and the model
+must be available to Runtime; invalid references fail before the initial prompt runs.
+
+Without a resume option, this creates a new Session even when no prompt is given.
+With `--session <id>` or `--continue`, it updates the opened Session's model before
+submitting the prompt. The selection is saved with that Session, so later turns
+and resumes keep it. It does not change the global default, and `/new` returns to
+the configured default. Omitting `--model` preserves existing startup behavior.
+The untargeted `--session` picker cannot be combined with `--model`; provide an ID
+or use `--continue` instead. If opening or continuing a Session fails, the override
+and initial prompt are not applied.

+ 8 - 17
packages/local-runtime-v2/src/application/session/process-local-application-contract.ts

@@ -26,8 +26,13 @@ import type {
   CodexOAuthStartResult,
   CodexOAuthStartResult,
   CodexOAuthLoginOptions,
   CodexOAuthLoginOptions,
   CodexOAuthStatus,
   CodexOAuthStatus,
+  UserModelInputView,
 } from "../../service/model-system/index.js";
 } from "../../service/model-system/index.js";
 
 
+export type ProcessLocalModelInput = Omit<UserModelInputView, "modalities"> & {
+  modalities?: { input?: readonly string[]; output?: readonly string[] };
+};
+
 export interface ProcessLocalMiniAppLaunch {
 export interface ProcessLocalMiniAppLaunch {
   readonly miniApp: MiniAppSurfaceSummary;
   readonly miniApp: MiniAppSurfaceSummary;
   /** Main-process-only launch coordinates. Never forward this object to a renderer. */
   /** Main-process-only launch coordinates. Never forward this object to a renderer. */
@@ -262,7 +267,7 @@ export interface LocalRuntimeApplication {
       baseUrl: string;
       baseUrl: string;
       apiKey: string;
       apiKey: string;
       apiFormat?: string;
       apiFormat?: string;
-      models?: readonly { modelId: string; displayName?: string }[];
+      models?: readonly ProcessLocalModelInput[];
       saveAndUse?: boolean;
       saveAndUse?: boolean;
     }): Promise<unknown>;
     }): Promise<unknown>;
     discoverCandidate(input: {
     discoverCandidate(input: {
@@ -278,21 +283,7 @@ export interface LocalRuntimeApplication {
         baseUrl: string;
         baseUrl: string;
         apiKey?: string;
         apiKey?: string;
         apiFormat?: string;
         apiFormat?: string;
-        models?: readonly {
-          modelId: string;
-          displayName?: string;
-          configurationSource?: string;
-          enabled?: boolean;
-          attachment?: boolean;
-          reasoning?: boolean;
-          toolCall?: boolean;
-          temperature?: boolean;
-          modalities?: {
-            input?: readonly string[];
-            output?: readonly string[];
-          };
-          limit?: { context?: number; output?: number };
-        }[];
+        models?: readonly ProcessLocalModelInput[];
       };
       };
       modelId?: string;
       modelId?: string;
       saveAndUse?: boolean;
       saveAndUse?: boolean;
@@ -309,7 +300,7 @@ export interface LocalRuntimeApplication {
       apiKey?: string;
       apiKey?: string;
       apiFormat?: string;
       apiFormat?: string;
       enabled?: boolean;
       enabled?: boolean;
-      models?: readonly { modelId: string; displayName?: string }[];
+      models?: readonly ProcessLocalModelInput[];
       saveAndUse?: boolean;
       saveAndUse?: boolean;
     }): Promise<unknown>;
     }): Promise<unknown>;
     delete(input: { providerId: string }): Promise<void>;
     delete(input: { providerId: string }): Promise<void>;

+ 5 - 13
packages/local-runtime-v2/src/application/session/process-local-application.ts

@@ -10,7 +10,10 @@ import type {
 } from '../../service/model-system/index.js';
 } from '../../service/model-system/index.js';
 import { watchGlobalEvents, watchProcessEvents } from '../events.js';
 import { watchGlobalEvents, watchProcessEvents } from '../events.js';
 import type { ModelProviderApplication } from './model-provider-application.js';
 import type { ModelProviderApplication } from './model-provider-application.js';
-import type { LocalRuntimeApplication } from './process-local-application-contract.js';
+import type {
+  LocalRuntimeApplication,
+  ProcessLocalModelInput,
+} from './process-local-application-contract.js';
 import type { SessionReportCapability } from '../../service/session-system/index.js';
 import type { SessionReportCapability } from '../../service/session-system/index.js';
 
 
 export interface ProcessLocalApplicationOptions {
 export interface ProcessLocalApplicationOptions {
@@ -201,18 +204,7 @@ function toProviderRecord(provider: ModelProviderView): Record<string, unknown>
 }
 }
 
 
 function toUserModelInputs(
 function toUserModelInputs(
-  models: readonly {
-    modelId: string;
-    displayName?: string;
-    configurationSource?: string;
-    enabled?: boolean;
-    attachment?: boolean;
-    reasoning?: boolean;
-    toolCall?: boolean;
-    temperature?: boolean;
-    modalities?: { input?: readonly string[]; output?: readonly string[] };
-    limit?: { context?: number; output?: number };
-  }[],
+  models: readonly ProcessLocalModelInput[],
 ): UserModelInputView[] {
 ): UserModelInputView[] {
   return models.map(({ modalities, ...model }) => ({
   return models.map(({ modalities, ...model }) => ({
     ...model,
     ...model,

+ 1 - 0
packages/local-runtime-v2/src/service/model-system/contracts.ts

@@ -414,6 +414,7 @@ export interface UserModelInputView {
   reasoning?: boolean;
   reasoning?: boolean;
   toolCall?: boolean;
   toolCall?: boolean;
   temperature?: boolean;
   temperature?: boolean;
+  capabilities?: LocalModelConfig['capabilities'];
   modalities?: { input?: string[]; output?: string[] };
   modalities?: { input?: string[]; output?: string[] };
   limit?: { context?: number; output?: number };
   limit?: { context?: number; output?: number };
   thinkingConfig?: { mode?: string; defaultValue?: string };
   thinkingConfig?: { mode?: string; defaultValue?: string };

+ 25 - 0
packages/local-runtime-v2/src/service/model-system/management/service-input.test.ts

@@ -13,6 +13,31 @@ import {
 } from './service-input.js';
 } from './service-input.js';
 
 
 describe('model provider input normalization', () => {
 describe('model provider input normalization', () => {
+  it('preserves declared capabilities on create and merges updates without losing limits', () => {
+    const created = modelsFromInputs([
+      {
+        modelId: 'vision-model',
+        capabilities: { support_image: true, max_image_bytes_inline: 1024 },
+      },
+    ]);
+    expect(created['vision-model']?.capabilities).toEqual({
+      support_image: true,
+      max_image_bytes_inline: 1024,
+    });
+    const patch = [{ modelId: 'vision-model', capabilities: { support_image: false } }];
+    for (const updated of [modelsFromInputs(patch, created), mergeModelsFromInputs(created, patch)]) {
+      expect(updated['vision-model']?.capabilities).toEqual({
+        support_image: false,
+        max_image_bytes_inline: 1024,
+      });
+      const renamed = mergeModelsFromInputs(updated, [
+        { modelId: 'vision-model', displayName: 'Renamed' },
+      ]);
+      expect(renamed['vision-model']?.capabilities).toEqual(updated['vision-model']?.capabilities);
+    }
+    expect(created['vision-model']?.capabilities?.support_image).toBe(true);
+  });
+
   it('normalizes API keys and formats', () => {
   it('normalizes API keys and formats', () => {
     expect(() => assertValidRawApiKey(undefined as unknown as string)).toThrow(
     expect(() => assertValidRawApiKey(undefined as unknown as string)).toThrow(
       LocalModelProviderError,
       LocalModelProviderError,

+ 3 - 0
packages/local-runtime-v2/src/service/model-system/management/service-input.ts

@@ -120,6 +120,9 @@ function modelFromInput(
   const nextModel: LocalModelConfig = {
   const nextModel: LocalModelConfig = {
     ...existing,
     ...existing,
     ...modelMetadataFields(model),
     ...modelMetadataFields(model),
+    ...(model.capabilities
+      ? { capabilities: { ...existing?.capabilities, ...model.capabilities } }
+      : {}),
     ...modelReasoningFields(model, effortOptions, defaultThinkingConfig),
     ...modelReasoningFields(model, effortOptions, defaultThinkingConfig),
     ...modelThinkingConfigFields(model, defaultThinkingConfig),
     ...modelThinkingConfigFields(model, defaultThinkingConfig),
     ...modelEffortFields(effortOptions),
     ...modelEffortFields(effortOptions),

+ 13 - 0
packages/local-runtime-v2/src/service/model-system/resolution/model-ref.test.ts

@@ -626,6 +626,19 @@ describe('resolveThinkingLevel', () => {
 });
 });
 
 
 describe('model capability helpers', () => {
 describe('model capability helpers', () => {
+  it('requires explicit image input metadata instead of generic attachment support', () => {
+    expect(capabilitiesFromModelConfig({ attachment: true }).support_image).toBe(false);
+    expect(
+      capabilitiesFromModelConfig({ modalities: { input: ['text'], output: ['image'] } }).support_image,
+    ).toBe(false);
+    expect(
+      capabilitiesFromModelConfig({ modalities: { input: ['text', 'image'] } }).support_image,
+    ).toBe(true);
+    expect(
+      capabilitiesFromModelConfig({ capabilities: { support_image: false } }).support_image,
+    ).toBe(false);
+  });
+
   it('uses explicit capability flags and forced thinking modes', () => {
   it('uses explicit capability flags and forced thinking modes', () => {
     expect(
     expect(
       capabilitiesFromModelConfig({
       capabilitiesFromModelConfig({

+ 11 - 0
packages/local-runtime-v2/src/service/turn-system/agent-host/runner/local-agent-turn-runner.ts

@@ -1,3 +1,4 @@
+import { logger } from '../../../../infra/logging/index.js';
 import {
 import {
   PiTurnRunner,
   PiTurnRunner,
   type LLMModelConfig,
   type LLMModelConfig,
@@ -633,6 +634,16 @@ async function settleNetworkStop(
   safetyWriter: LocalOutputSafetyEventWriter,
   safetyWriter: LocalOutputSafetyEventWriter,
 ): Promise<void> {
 ): Promise<void> {
   if (safetyWriter.terminalFailed) return;
   if (safetyWriter.terminalFailed) return;
+  logger.warn(
+    {
+      sessionId: input.sessionId,
+      turnId: input.turnId,
+      reviewOutcome: safetyWriter.reviewStopVariant === 'auth' ? 'auth_error' : 'unavailable',
+      outputSuppressed: true,
+      terminalStatus: 'completed',
+    },
+    '[content-safety] output review stopped turn',
+  );
   state.networkStopped = true;
   state.networkStopped = true;
   state.retractionVariant = safetyWriter.reviewStopVariant;
   state.retractionVariant = safetyWriter.reviewStopVariant;
   state.approvedPartial = safetyWriter.getApprovedPartial();
   state.approvedPartial = safetyWriter.getApprovedPartial();

+ 39 - 20
packages/local-runtime/src/content-safety/api-v2.ts

@@ -16,6 +16,7 @@ import {
   managedBackendRoutingHeaders,
   managedBackendRoutingHeaders,
   type LocalRuntimeRoutingContext,
   type LocalRuntimeRoutingContext,
 } from '../runtime/routing-headers.js';
 } from '../runtime/routing-headers.js';
+import { createSafetyFailureReporter } from './diagnostics.js';
 import { resolveSafetyApiBase } from './api-base.js';
 import { resolveSafetyApiBase } from './api-base.js';
 
 
 /** biz-gateway uses V2 numeric scenes with its desktop HTTP envelope. */
 /** biz-gateway uses V2 numeric scenes with its desktop HTTP envelope. */
@@ -50,26 +51,44 @@ export async function callLocalSafetyCheckV2(input: {
   const buildEnv = (input.buildEnv ?? getRuntimeBuildEnv)();
   const buildEnv = (input.buildEnv ?? getRuntimeBuildEnv)();
   const region = (input.region ?? getRuntimeRegion)();
   const region = (input.region ?? getRuntimeRegion)();
   const token = input.authContext?.accessToken?.trim() || process.env.MAVIS_ACCESS_TOKEN?.trim();
   const token = input.authContext?.accessToken?.trim() || process.env.MAVIS_ACCESS_TOKEN?.trim();
-  const result = await postSafetyCheckV2({
-    url: `${resolveSafetyApiBase(region, buildEnv, input.testBaseURL)}/mavis/api/v2/content?require_auth=true`,
-    headers: {
-      'User-Agent': 'MiniMaxAgent',
-      ...(token ? { Authorization: `Bearer ${token}` } : {}),
-      ...managedBackendRoutingHeaders(input.routingContext, input.buildEnv ? buildEnv : undefined),
-    },
-    body: {
-      scene: input.request.scene,
-      ...(input.request.content_text !== undefined
-        ? { content_text: input.request.content_text }
-        : {}),
-      ...(input.request.sessionId !== undefined ? { sessionId: input.request.sessionId } : {}),
-      ...(input.request.image_url !== undefined ? { image_url: input.request.image_url } : {}),
-      ...(input.request.files !== undefined ? { files: input.request.files } : {}),
-    },
-    fetchImpl: input.fetchImpl,
-    signal: input.signal,
-  });
-  return parseLocalSafetyCheckV2Result(result);
+  const url = `${resolveSafetyApiBase(region, buildEnv, input.testBaseURL)}/mavis/api/v2/content?require_auth=true`;
+  const reportFailure = createSafetyFailureReporter(url, 'v2', input.request.scene);
+  try {
+    const result = await postSafetyCheckV2({
+      url,
+      headers: {
+        'User-Agent': 'MiniMaxAgent',
+        ...(token ? { Authorization: `Bearer ${token}` } : {}),
+        ...managedBackendRoutingHeaders(
+          input.routingContext,
+          input.buildEnv ? buildEnv : undefined,
+        ),
+      },
+      body: {
+        scene: input.request.scene,
+        ...(input.request.content_text !== undefined
+          ? { content_text: input.request.content_text }
+          : {}),
+        ...(input.request.sessionId !== undefined ? { sessionId: input.request.sessionId } : {}),
+        ...(input.request.image_url !== undefined ? { image_url: input.request.image_url } : {}),
+        ...(input.request.files !== undefined ? { files: input.request.files } : {}),
+      },
+      fetchImpl: input.fetchImpl,
+      signal: input.signal,
+    });
+    return parseLocalSafetyCheckV2Result(result);
+  } catch (error) {
+    reportFailure(
+      error instanceof SafetyCheckV2Error
+        ? {
+            failureKind: error.kind,
+            statusCode: error.statusCode,
+            transportKind: error.transportKind,
+          }
+        : { failureKind: 'internal' },
+    );
+    throw error;
+  }
 }
 }
 
 
 function parseLocalSafetyCheckV2Result(body: Record<string, unknown>): LocalSafetyCheckV2Result {
 function parseLocalSafetyCheckV2Result(body: Record<string, unknown>): LocalSafetyCheckV2Result {

+ 34 - 41
packages/local-runtime/src/content-safety/api.ts

@@ -1,3 +1,8 @@
+import {
+  classifySafetyTransportError,
+  isSafetyCheckV2Record,
+} from "@mavis/shared/safety-check-v2";
+import { createSafetyFailureReporter } from "./diagnostics.js";
 import {
 import {
   getRuntimeBuildEnv,
   getRuntimeBuildEnv,
   getRuntimeRegion,
   getRuntimeRegion,
@@ -8,7 +13,6 @@ import {
 import { callTurnSafetyApi } from "./turn-api.js";
 import { callTurnSafetyApi } from "./turn-api.js";
 import { resolveSafetyApiBase } from "./api-base.js";
 import { resolveSafetyApiBase } from "./api-base.js";
 
 
-import { logger } from "../common/logger.js";
 import type { LocalRuntimeAuthContext } from "../runtime/model-resolver.js";
 import type { LocalRuntimeAuthContext } from "../runtime/model-resolver.js";
 import {
 import {
   managedBackendRoutingHeaders,
   managedBackendRoutingHeaders,
@@ -126,12 +130,7 @@ export async function callSafetyApi(input: {
   const accessToken =
   const accessToken =
     input.authContext?.accessToken?.trim() ||
     input.authContext?.accessToken?.trim() ||
     process.env.MAVIS_ACCESS_TOKEN?.trim();
     process.env.MAVIS_ACCESS_TOKEN?.trim();
-  // TODO(shared-oauth): Remove this local-only diagnostic after the content-safety
-  // resource server accepts mcode-public Bearer tokens in the joint test environment.
-  // Never add the token, request body/content, or response body to these fields.
-  const logLocalUpstream =
-    resolvedBuildEnv === "dev" || resolvedBuildEnv === "test";
-  const startedAt = Date.now();
+  const reportFailure = createSafetyFailureReporter(url, "v1", input.scene);
   let response: Response;
   let response: Response;
   try {
   try {
     response = await input.fetchImpl(url, {
     response = await input.fetchImpl(url, {
@@ -162,20 +161,10 @@ export async function callSafetyApi(input: {
       signal: AbortSignal.timeout(CONTENT_REVIEW_TIMEOUT_MS),
       signal: AbortSignal.timeout(CONTENT_REVIEW_TIMEOUT_MS),
     });
     });
   } catch (error) {
   } catch (error) {
-    if (logLocalUpstream) {
-      logger.info(
-        {
-          url,
-          method: "POST",
-          status: null,
-          scene: input.scene,
-          hasBearer: Boolean(accessToken),
-          durationMs: Date.now() - startedAt,
-          errorType: error instanceof Error ? error.name : typeof error,
-        },
-        "[content-safety] upstream transport failure",
-      );
-    }
+    reportFailure({
+      failureKind: "transport",
+      transportKind: classifySafetyTransportError(error),
+    });
     // fetch threw (offline / DNS / connection refused / TLS) or the 10s timeout
     // fetch threw (offline / DNS / connection refused / TLS) or the 10s timeout
     // fired: we never reached a verdict. No gateway confirmation → fail closed.
     // fired: we never reached a verdict. No gateway confirmation → fail closed.
     return {
     return {
@@ -185,26 +174,21 @@ export async function callSafetyApi(input: {
     };
     };
   }
   }
 
 
-  if (logLocalUpstream) {
-    logger.info(
-      {
-        url,
-        method: "POST",
-        status: response.status,
-        scene: input.scene,
-        hasBearer: Boolean(accessToken),
-        durationMs: Date.now() - startedAt,
-        upstreamTraceId:
-          response.headers.get("trace-id") ??
-          response.headers.get("x-trace-id") ??
-          response.headers.get("x-request-id") ??
-          null,
-      },
-      "[content-safety] upstream response",
-    );
+  let text: string;
+  try {
+    text = await response.text();
+  } catch (error) {
+    reportFailure({
+      failureKind: "transport",
+      transportKind: classifySafetyTransportError(error),
+      statusCode: response.status,
+    });
+    return {
+      pass: false,
+      reason: "Service response could not be read",
+      errorKind: "local_error",
+    };
   }
   }
-
-  const text = await response.text();
   if (!response.ok) {
   if (!response.ok) {
     // Non-2xx from the gateway, split by class so an auth failure can never be a
     // Non-2xx from the gateway, split by class so an auth failure can never be a
     // bypass:
     // bypass:
@@ -215,6 +199,10 @@ export async function callSafetyApi(input: {
     //          acceptable, so there is NO valid verdict on our side → `local_error`
     //          acceptable, so there is NO valid verdict on our side → `local_error`
     //          (fail closed). Treating 401/403 as `api_error` would let an
     //          (fail closed). Treating 401/403 as `api_error` would let an
     //          unauthenticated user bypass every content gate.
     //          unauthenticated user bypass every content gate.
+    reportFailure({
+      failureKind: response.status === 401 || response.status === 403 ? "auth" : "http",
+      statusCode: response.status,
+    });
     const error = readErrorText(text);
     const error = readErrorText(text);
     return {
     return {
       pass: false,
       pass: false,
@@ -240,6 +228,7 @@ export async function callSafetyApi(input: {
   try {
   try {
     data = JSON.parse(text) as typeof data;
     data = JSON.parse(text) as typeof data;
   } catch {
   } catch {
+    reportFailure({ failureKind: "response", statusCode: response.status });
     // 2xx but the body is not JSON — we cannot read a verdict. Treat as no
     // 2xx but the body is not JSON — we cannot read a verdict. Treat as no
     // gateway confirmation → fail closed on the input path.
     // gateway confirmation → fail closed on the input path.
     return {
     return {
@@ -249,8 +238,9 @@ export async function callSafetyApi(input: {
     };
     };
   }
   }
 
 
-  const pass = data.pass ?? data.safe;
+  const pass = isSafetyCheckV2Record(data) ? data.pass ?? data.safe : undefined;
   if (typeof pass !== "boolean") {
   if (typeof pass !== "boolean") {
+    reportFailure({ failureKind: "response", statusCode: response.status });
     // 2xx JSON but no boolean pass/safe field — again no usable verdict → fail
     // 2xx JSON but no boolean pass/safe field — again no usable verdict → fail
     // closed on the input path.
     // closed on the input path.
     return {
     return {
@@ -262,6 +252,9 @@ export async function callSafetyApi(input: {
   const activeRegion = region();
   const activeRegion = region();
   const upstreamCode =
   const upstreamCode =
     typeof data.errorCode === "number" ? data.errorCode : undefined;
     typeof data.errorCode === "number" ? data.errorCode : undefined;
+  if (!pass && upstreamCode === UPSTREAM_CODE.SERVICE_UNAVAILABLE) {
+    reportFailure({ failureKind: "upstream", statusCode: upstreamCode });
+  }
   return {
   return {
     pass,
     pass,
     reason:
     reason:

+ 27 - 0
packages/local-runtime/src/content-safety/diagnostics.ts

@@ -0,0 +1,27 @@
+import { randomUUID } from 'node:crypto';
+import type { SafetyTransportKind } from '@mavis/shared/safety-check-v2';
+import { logger } from '../common/logger.js';
+
+/** One bounded record per failed check, in every build. Never log raw upstream data. */
+export function createSafetyFailureReporter(url: string, apiVersion: 'v1' | 'v2', scene: number) {
+  const startedAt = Date.now();
+  const reviewId = randomUUID();
+  const endpointHost = new URL(url).hostname;
+  return (failure: {
+    failureKind: 'transport' | 'auth' | 'http' | 'response' | 'upstream' | 'internal';
+    transportKind?: SafetyTransportKind;
+    statusCode?: number;
+  }): void => {
+    logger.warn(
+      {
+        reviewId,
+        endpointHost,
+        apiVersion,
+        scene,
+        durationMs: Date.now() - startedAt,
+        ...failure,
+      },
+      '[content-safety] review failed',
+    );
+  };
+}

+ 11 - 0
packages/local-runtime/src/runtime/host.ts

@@ -1,3 +1,4 @@
+import { logger } from "../common/logger.js";
 import type { LocalEventWriter } from "../events/sink.js";
 import type { LocalEventWriter } from "../events/sink.js";
 import { PiTurnRunner } from "@mavis/agent-core/pi-turn-runner";
 import { PiTurnRunner } from "@mavis/agent-core/pi-turn-runner";
 import { RuntimeEventStatus, RuntimeEventType } from "@mavis/protocol";
 import { RuntimeEventStatus, RuntimeEventType } from "@mavis/protocol";
@@ -437,6 +438,16 @@ export class LocalRuntimeHost {
         // writer, so keep it authoritative and do not append a synthetic
         // writer, so keep it authoritative and do not append a synthetic
         // COMPLETED/network-notice outcome.
         // COMPLETED/network-notice outcome.
         if (safetyWriter.terminalFailed) break;
         if (safetyWriter.terminalFailed) break;
+        logger.warn(
+          {
+            sessionId: input.sessionId,
+            turnId: input.turnId,
+            reviewOutcome: "unavailable",
+            outputSuppressed: true,
+            terminalStatus: "completed",
+          },
+          "[content-safety] output review stopped turn",
+        );
         networkStopped = true;
         networkStopped = true;
         retractionVariant = "network";
         retractionVariant = "network";
         // Capture the approved prefix so the orchestration can rewrite pi history
         // Capture the approved prefix so the orchestration can rewrite pi history

+ 62 - 38
packages/local-runtime/test/unit/content-safety-api-fail-policy.test.ts

@@ -276,46 +276,70 @@ describe('callSafetyApi fail-policy classification', () => {
     expect(headers.get('Token')).toBeNull();
     expect(headers.get('Token')).toBeNull();
   });
   });
 
 
-  it('logs a sanitized upstream response in local test environments', async () => {
-    const log = vi.spyOn(logger, 'info').mockImplementation(() => {});
-    const fetchImpl = vi.fn(async () =>
-      new Response(JSON.stringify({ code: 401, message: 'auth failed' }), {
-        status: 401,
-        headers: {
-          'Content-Type': 'application/json',
-          'Trace-Id': 'trace-content-safety-test',
-        },
-      }),
-    ) as unknown as typeof fetch;
-
-    await callSafetyApi({
-      content: 'must-not-appear-in-log',
-      scene: SAFETY_SCENE.UserInput,
-      authContext: { accessToken: 'must-not-appear-token' },
-      fetchImpl,
-      region,
-      buildEnv,
-    });
-
-    expect(log).toHaveBeenCalledWith(
-      expect.objectContaining({
-        url: 'https://matrix-test.example.invalid/mavis/api/v1/content',
-        method: 'POST',
-        status: 401,
+  it.each([
+    ['auth', () => jsonResponse({ error: 'private-response' }, 401), 401],
+    ['http', () => jsonResponse({ error: 'private-response' }, 503), 503],
+    ['response', () => new Response('private-response'), 200],
+    ['response', () => jsonResponse(null), 200],
+    ['response', () => jsonResponse({ secret: 'private-response' }), 200],
+    [
+      'transport',
+      () => {
+        throw new TypeError('private-error https://secret.invalid/?token=private-token', {
+          cause: { code: 'ENOTFOUND' },
+        });
+      },
+      undefined,
+    ],
+    [
+      'transport',
+      () =>
+        new Response(
+          new ReadableStream({
+            start(controller) {
+              controller.error(new DOMException('private-error', 'TimeoutError'));
+            },
+          }),
+        ),
+      200,
+    ],
+  ] as const)(
+    'logs redacted %s failures in production',
+    async (failureKind, response, statusCode) => {
+      const log = vi.spyOn(logger, 'warn').mockImplementation(() => {});
+      const result = await callSafetyApi({
+        content: 'private-content',
         scene: SAFETY_SCENE.UserInput,
         scene: SAFETY_SCENE.UserInput,
-        hasBearer: true,
-        durationMs: expect.any(Number),
-        upstreamTraceId: 'trace-content-safety-test',
-      }),
-      '[content-safety] upstream response',
-    );
-    const serialized = JSON.stringify(log.mock.calls);
-    expect(serialized).not.toContain('must-not-appear-token');
-    expect(serialized).not.toContain('must-not-appear-in-log');
-  });
+        authContext: { accessToken: 'private-token' },
+        attachments: [{ fileUrl: 'https://secret.invalid/private-file', fileName: 'private-name' }],
+        fetchImpl: async () => response(),
+        region,
+        buildEnv: () => 'prod',
+      });
+      expect(result.pass).toBe(false);
+      expect(reviewBlocks(result)).toBe(failureKind !== 'http');
+      expect(log).toHaveBeenCalledOnce();
+      expect(log).toHaveBeenCalledWith(
+        expect.objectContaining({
+          reviewId: expect.any(String),
+          endpointHost: 'agent.minimax.cn',
+          apiVersion: 'v1',
+          scene: SAFETY_SCENE.UserInput,
+          durationMs: expect.any(Number),
+          failureKind,
+          ...(statusCode === undefined ? { transportKind: 'dns' } : { statusCode }),
+        }),
+        '[content-safety] review failed',
+      );
+      const serialized = JSON.stringify(log.mock.calls);
+      expect(serialized).not.toContain('private-');
+      expect(serialized).not.toContain('secret.invalid');
+      expect(serialized).not.toContain('https://');
+    },
+  );
 
 
-  it('does not emit the temporary upstream diagnostic in production', async () => {
-    const log = vi.spyOn(logger, 'info').mockImplementation(() => {});
+  it('does not log successful reviews in production', async () => {
+    const log = vi.spyOn(logger, 'warn').mockImplementation(() => {});
 
 
     await callSafetyApi({
     await callSafetyApi({
       content: 'hello',
       content: 'hello',

+ 83 - 1
packages/local-runtime/test/unit/content-safety-api-v2.test.ts

@@ -1,3 +1,4 @@
+import { logger } from '../../src/common/logger.js';
 import { afterEach, describe, expect, it, vi } from 'vitest';
 import { afterEach, describe, expect, it, vi } from 'vitest';
 import { SAFETY_CHECK_V2_SCENE } from '@mavis/shared/safety-check-v2';
 import { SAFETY_CHECK_V2_SCENE } from '@mavis/shared/safety-check-v2';
 import { callLocalSafetyCheckV2 } from '../../src/content-safety/api-v2.js';
 import { callLocalSafetyCheckV2 } from '../../src/content-safety/api-v2.js';
@@ -14,7 +15,10 @@ const defaults = {
 };
 };
 const fetchResponse = (body: unknown) =>
 const fetchResponse = (body: unknown) =>
   vi.fn<typeof fetch>().mockImplementation(async () => Response.json(body));
   vi.fn<typeof fetch>().mockImplementation(async () => Response.json(body));
-afterEach(() => vi.unstubAllEnvs());
+afterEach(() => {
+  vi.unstubAllEnvs();
+  vi.restoreAllMocks();
+});
 
 
 describe('local biz-gateway SafetyCheckV2 client', () => {
 describe('local biz-gateway SafetyCheckV2 client', () => {
   it('uses the V2 endpoint and scene with the current Bearer/routing headers', async () => {
   it('uses the V2 endpoint and scene with the current Bearer/routing headers', async () => {
@@ -248,3 +252,81 @@ describe('local biz-gateway SafetyCheckV2 client', () => {
     });
     });
   });
   });
 });
 });
+
+describe('V2 production failure diagnostics', () => {
+  it.each([
+    [{ cause: { code: 'ENOTFOUND' } }, 'dns'],
+    [{ cause: { code: 'ECONNREFUSED' } }, 'connect'],
+    [{ cause: { code: 'ERR_TLS_CERT_ALTNAME_INVALID' } }, 'tls'],
+    [{ name: 'TimeoutError' }, 'timeout'],
+    [{ cause: { code: 'UND_ERR_BODY_TIMEOUT' } }, 'timeout'],
+    [{ name: 'AbortError' }, 'aborted'],
+    [{ cause: { code: 'private-code' } }, 'unknown'],
+  ] as const)('retains only the transport category %j', async (details, transportKind) => {
+    const log = vi.spyOn(logger, 'warn').mockImplementation(() => {});
+    const error = Object.assign(
+      new Error('private-error https://secret.invalid/?token=private-token'),
+      details,
+    );
+    await expect(
+      callLocalSafetyCheckV2({
+        ...defaults,
+        buildEnv: () => 'prod',
+        authContext: { accessToken: 'private-token' },
+        request: { scene: 100, content_text: 'private-content', sessionId: 'private-session' },
+        fetchImpl: async () => {
+          throw error;
+        },
+      }),
+    ).rejects.toMatchObject({ kind: 'transport', transportKind });
+    expect(log).toHaveBeenCalledOnce();
+    expect(log).toHaveBeenCalledWith(
+      expect.objectContaining({
+        reviewId: expect.any(String),
+        endpointHost: 'agent.minimax.cn',
+        apiVersion: 'v2',
+        scene: 100,
+        durationMs: expect.any(Number),
+        failureKind: 'transport',
+        transportKind,
+      }),
+      '[content-safety] review failed',
+    );
+    expect(JSON.stringify(log.mock.calls)).not.toContain('private-');
+    expect(JSON.stringify(log.mock.calls)).not.toContain('secret.invalid');
+  });
+
+  it.each([
+    ['auth', () => new Response('private-body', { status: 401 })],
+    ['http', () => new Response('private-body', { status: 503 })],
+    ['response', () => new Response('private-body')],
+    ['response', () => Response.json({ unexpected: 'private-body' })],
+    ['upstream', () => Response.json({ errorCode: 50200, error: 'private-body' })],
+    [
+      'transport',
+      () =>
+        new Response(
+          new ReadableStream({
+            start(controller) {
+              controller.error(new DOMException('private-body', 'TimeoutError'));
+            },
+          }),
+        ),
+    ],
+  ] as const)('logs failed %s reviews without response data', async (kind, response) => {
+    const log = vi.spyOn(logger, 'warn').mockImplementation(() => {});
+    await expect(
+      callLocalSafetyCheckV2({
+        ...defaults,
+        buildEnv: () => 'prod',
+        fetchImpl: async () => response(),
+      }),
+    ).rejects.toMatchObject({ kind });
+    expect(log).toHaveBeenCalledOnce();
+    expect(log).toHaveBeenCalledWith(
+      expect.objectContaining({ failureKind: kind }),
+      '[content-safety] review failed',
+    );
+    expect(JSON.stringify(log.mock.calls)).not.toContain('private-body');
+  });
+});

+ 88 - 1
packages/local-runtime/test/unit/local-runtime-host-output-safety-concurrency.test.ts

@@ -1,3 +1,5 @@
+import { LocalAgentTurnRunner } from '../../../local-runtime-v2/src/service/turn-system/agent-host/runner/local-agent-turn-runner.js';
+import { logger } from '../../src/common/logger.js';
 // Concurrency isolation for the output-safety host loop.
 // Concurrency isolation for the output-safety host loop.
 //
 //
 // The blocked flag / abort / review buffers live on per-attempt writer instances
 // The blocked flag / abort / review buffers live on per-attempt writer instances
@@ -8,7 +10,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
 
 
 import { LocalRuntimeHost, type LocalTurnRunner } from '../../src/runtime/host.js';
 import { LocalRuntimeHost, type LocalTurnRunner } from '../../src/runtime/host.js';
 import { RespDataType } from '@mavis/agent-core/protocol/agent-message';
 import { RespDataType } from '@mavis/agent-core/protocol/agent-message';
-import { RuntimeEventType, type IRuntimeEvent } from '@mavis/protocol';
+import { RuntimeEventStatus, RuntimeEventType, type IRuntimeEvent } from '@mavis/protocol';
 
 
 function finalMessage(content: string): IRuntimeEvent {
 function finalMessage(content: string): IRuntimeEvent {
   return {
   return {
@@ -57,6 +59,91 @@ describe('LocalRuntimeHost output safety — concurrency isolation', () => {
     delete process.env.IDC;
     delete process.env.IDC;
   });
   });
 
 
+  it('records a review stop while retaining completed terminal semantics', async () => {
+    const log = vi.spyOn(logger, 'warn').mockImplementation(() => {});
+    const fetchMock = vi
+      .spyOn(globalThis, 'fetch')
+      .mockRejectedValue(new DOMException('timeout', 'TimeoutError'));
+    const host = new LocalRuntimeHost({
+      safetyApiVersion: 'v2',
+      outputSafetyRetryDelay: async () => {},
+      piRunner: {
+        async runTurn(input) {
+          await input.eventWriter.pushRuntime(finalMessage('unreviewed-output'));
+        },
+      },
+    });
+    const output = await host.runTurn({
+      workspaceDir: '/tmp/ws',
+      systemPrompt: '',
+      userMessage: { text: 'hi' },
+      llm: { model: {} as never },
+      sessionId: 'ses',
+      turnId: 'turn',
+    });
+    expect(fetchMock).toHaveBeenCalledTimes(4);
+    expect(output).toMatchObject({ networkStopped: true, retracted: false });
+    expect(output.events.filter((event) => event.type === RuntimeEventType.STREAM_RESP)).toEqual(
+      [],
+    );
+    expect(output.events).toContainEqual(
+      expect.objectContaining({
+        type: RuntimeEventType.SESSION_STATUS,
+        payload: expect.objectContaining({ status: RuntimeEventStatus.COMPLETED }),
+      }),
+    );
+    expect(log).toHaveBeenCalledWith(
+      {
+        sessionId: 'ses',
+        turnId: 'turn',
+        reviewOutcome: 'unavailable',
+        outputSuppressed: true,
+        terminalStatus: 'completed',
+      },
+      '[content-safety] output review stopped turn',
+    );
+    expect(JSON.stringify(log.mock.calls)).not.toContain('unreviewed-output');
+  });
+
+  it.each([false, true])('diagnoses the V2 review stop without overriding runner failure: %s', async (runnerFailed) => {
+    const log = vi.spyOn(logger, 'warn').mockImplementation(() => {});
+    const events: IRuntimeEvent[] = [];
+    const eventWriter = {
+      events,
+      async pushRuntime(event: IRuntimeEvent) { events.push(event); },
+      async appendEvents(next: IRuntimeEvent[]) { events.push(...next); },
+    };
+    const reviewContent = vi.fn(async () => ({ pass: false, errorKind: 'local_error' as const }));
+    const runner = new LocalAgentTurnRunner({
+      reviewContent,
+      outputSafetyRetryDelay: async () => {},
+      piRunner: { async runTurn(input) {
+        await input.eventWriter.pushRuntime(finalMessage('unreviewed-output'));
+        if (runnerFailed) await input.eventWriter.pushRuntime({
+          ...finalMessage(''), type: RuntimeEventType.SESSION_STATUS,
+          payload: { status: RuntimeEventStatus.FAILED },
+        });
+      } },
+    });
+    const output = await runner.runTurn({
+      workspaceDir: '/tmp/ws', systemPrompt: '', userMessage: { text: 'hi' },
+      llm: { model: {} as never }, sessionId: 'ses', turnId: 'turn', eventWriter,
+      toolContext: { agentName: 'test', parentAgentConfig: {}, trustedExactWritePaths: [], eventWriter } as never,
+    });
+    expect(reviewContent).toHaveBeenCalledTimes(4);
+    expect(events.filter(event => event.type === RuntimeEventType.STREAM_RESP)).toEqual([]);
+    expect(output.outcome.status).toBe(runnerFailed ? 'failed' : 'completed');
+    if (runnerFailed) {
+      expect(log).not.toHaveBeenCalled();
+    } else {
+      expect(output).toMatchObject({ networkStopped: true, retracted: false, reconcile: { kind: 'network-reconcile' } });
+      expect(log).toHaveBeenCalledWith({
+        sessionId: 'ses', turnId: 'turn', reviewOutcome: 'unavailable',
+        outputSuppressed: true, terminalStatus: 'completed',
+      }, '[content-safety] output review stopped turn');
+    }
+  });
+
   it('a rejected turn never contaminates a concurrent clean turn', async () => {
   it('a rejected turn never contaminates a concurrent clean turn', async () => {
     const fetchMock = installContentAwareFetch();
     const fetchMock = installContentAwareFetch();
     // One shared runner + one shared host drive both turns. Each turn emits
     // One shared runner + one shared host drive both turns. Each turn emits

+ 50 - 4
packages/shared/src/safety-check-v2.ts

@@ -36,11 +36,52 @@ export type SafetyCheckV2Headers = NonNullable<RequestInit['headers']>;
 
 
 export type SafetyCheckV2ErrorKind = 'auth' | 'http' | 'transport' | 'response' | 'upstream';
 export type SafetyCheckV2ErrorKind = 'auth' | 'http' | 'transport' | 'response' | 'upstream';
 
 
+export type SafetyTransportKind = 'dns' | 'connect' | 'tls' | 'timeout' | 'aborted' | 'unknown';
+
+/** Only allowlisted categories survive; never retain exception text, URLs or causes. */
+export function classifySafetyTransportError(error: unknown): SafetyTransportKind {
+  let current = error;
+  for (let depth = 0; depth < 4 && isSafetyCheckV2Record(current); depth += 1) {
+    const { name } = current;
+    const code = typeof current.code === 'string' ? current.code : '';
+    if (
+      name === 'TimeoutError' ||
+      [
+        'ETIMEDOUT',
+        'UND_ERR_CONNECT_TIMEOUT',
+        'UND_ERR_HEADERS_TIMEOUT',
+        'UND_ERR_BODY_TIMEOUT',
+      ].includes(code)
+    )
+      return 'timeout';
+    if (name === 'AbortError' || code === 'ABORT_ERR') return 'aborted';
+    if (code === 'ENOTFOUND' || code === 'EAI_AGAIN') return 'dns';
+    if (
+      ['ECONNREFUSED', 'ECONNRESET', 'ENETUNREACH', 'EHOSTUNREACH', 'UND_ERR_SOCKET'].includes(code)
+    )
+      return 'connect';
+    if (
+      [
+        'CERT_HAS_EXPIRED',
+        'DEPTH_ZERO_SELF_SIGNED_CERT',
+        'SELF_SIGNED_CERT_IN_CHAIN',
+        'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
+        'ERR_TLS_CERT_ALTNAME_INVALID',
+        'UNABLE_TO_GET_ISSUER_CERT_LOCALLY',
+      ].includes(code)
+    )
+      return 'tls';
+    current = current.cause;
+  }
+  return 'unknown';
+}
+
 /** A failed check has no verdict. Callers must handle it separately from Allow. */
 /** A failed check has no verdict. Callers must handle it separately from Allow. */
 export class SafetyCheckV2Error extends Error {
 export class SafetyCheckV2Error extends Error {
   constructor(
   constructor(
     readonly kind: SafetyCheckV2ErrorKind,
     readonly kind: SafetyCheckV2ErrorKind,
     readonly statusCode?: number,
     readonly statusCode?: number,
+    readonly transportKind?: SafetyTransportKind,
   ) {
   ) {
     super(`SafetyCheckV2 ${kind} error`);
     super(`SafetyCheckV2 ${kind} error`);
     this.name = 'SafetyCheckV2Error';
     this.name = 'SafetyCheckV2Error';
@@ -71,8 +112,8 @@ export async function postSafetyCheckV2(input: {
       signal: input.signal ? AbortSignal.any([input.signal, timeout]) : timeout,
       signal: input.signal ? AbortSignal.any([input.signal, timeout]) : timeout,
       redirect: 'error',
       redirect: 'error',
     });
     });
-  } catch {
-    throw new SafetyCheckV2Error('transport');
+  } catch (error) {
+    throw new SafetyCheckV2Error('transport', undefined, classifySafetyTransportError(error));
   }
   }
   if (!response.ok) {
   if (!response.ok) {
     throw new SafetyCheckV2Error(
     throw new SafetyCheckV2Error(
@@ -83,8 +124,13 @@ export async function postSafetyCheckV2(input: {
   let body: unknown;
   let body: unknown;
   try {
   try {
     body = await response.json();
     body = await response.json();
-  } catch {
-    throw new SafetyCheckV2Error('response');
+  } catch (error) {
+    const transportKind = classifySafetyTransportError(error);
+    throw new SafetyCheckV2Error(
+      transportKind === 'unknown' ? 'response' : 'transport',
+      response.status,
+      transportKind,
+    );
   }
   }
   if (!isSafetyCheckV2Record(body)) throw new SafetyCheckV2Error('response');
   if (!isSafetyCheckV2Record(body)) throw new SafetyCheckV2Error('response');
   return body;
   return body;

+ 3 - 1
packages/tui/docs/status-line-config.md

@@ -60,6 +60,7 @@ Configuration loading checks that the value is an array. The TUI validates indiv
 | `token-quota` | Token quota / plan |
 | `token-quota` | Token quota / plan |
 | `cache-read-ratio` | Aggregate cache read ratio for the current session, such as `Cache 80%` |
 | `cache-read-ratio` | Aggregate cache read ratio for the current session, such as `Cache 80%` |
 | `context-remaining` | Remaining context percentage from the same runtime snapshot as `/context`; hidden if unavailable |
 | `context-remaining` | Remaining context percentage from the same runtime snapshot as `/context`; hidden if unavailable |
+| `context-meter` | Remaining context gauge, e.g. `Context ▕██████░░▏ 77% left`, from the same runtime snapshot as `/context`; hidden if unavailable; opt-in |
 | `custom-command` | External command stdout; first line inline by default, optional separate multiline block; opt-in |
 | `custom-command` | External command stdout; first line inline by default, optional separate multiline block; opt-in |
 
 
 `cache-read-ratio` aggregates persisted session usage as `cacheRead / (input + cacheRead + cacheWrite)`, where `input` is fresh uncached input. It uses existing provider usage fields; missing cache counts contribute zero, so sessions mixing providers can underestimate cache reads. The item is hidden before any prompt tokens exist. In-process notifications refresh the session after usage persists; read failures stay silent. `/usage` also shows Read, Fresh, and Write token counts.
 `cache-read-ratio` aggregates persisted session usage as `cacheRead / (input + cacheRead + cacheWrite)`, where `input` is fresh uncached input. It uses existing provider usage fields; missing cache counts contribute zero, so sessions mixing providers can underestimate cache reads. The item is hidden before any prompt tokens exist. In-process notifications refresh the session after usage persists; read failures stay silent. `/usage` also shows Read, Fresh, and Write token counts.
@@ -78,6 +79,7 @@ Configuration loading checks that the value is an array. The TUI validates indiv
 | `quota` / `token-plan` | `token-quota` |
 | `quota` / `token-plan` | `token-quota` |
 | `cache-read` | `cache-read-ratio` |
 | `cache-read` | `cache-read-ratio` |
 | `context` / `context-left` | `context-remaining` |
 | `context` / `context-left` | `context-remaining` |
+| `context-bar` / `context-gauge` | `context-meter` |
 | `custom` | `custom-command` |
 | `custom` | `custom-command` |
 
 
 ## Defaults
 ## Defaults
@@ -90,7 +92,7 @@ current-dir · session-title · git-branch · review-link · plan-mode · approv
 
 
 `build-mode` is **not a default**. Explicitly listing it is the only way to enable the machine-readable protocol; the build type does not matter. It may appear anywhere in the list and takes over the entire line to keep free text out of the strict record. Remove it to disable the protocol.
 `build-mode` is **not a default**. Explicitly listing it is the only way to enable the machine-readable protocol; the build type does not matter. It may appear anywhere in the list and takes over the entire line to keep free text out of the strict record. Remove it to disable the protocol.
 
 
-By default, `context-window` shows capacity before usage is available; `context-remaining` replaces it once a runtime usage snapshot exists. Explicit item lists retain the selected presentation. Session changes clear old data, and refresh failures do not reuse stale values. Context comes from the latest runtime snapshot, not a live per-token counter. `cache-read-ratio` remains opt-in. Narrow terminals prioritize permissions, context warnings, model, and session title. Even the shortest context display retains `Ctx` to distinguish it from `5h` / `W` plan quotas.
+By default, `context-window` shows capacity before usage is available; `context-remaining` replaces it once a runtime usage snapshot exists. Explicit item lists retain the selected presentation. Session changes clear old data, and refresh failures do not reuse stale values. Context comes from the latest runtime snapshot, not a live per-token counter. `cache-read-ratio` and `context-meter` remain opt-in; `context-meter` renders the same remaining-headroom value and thresholds as `context-remaining` with an added gauge, and takes precedence over `context-remaining` when both are listed, regardless of order. Other items retain their order, and the meter stays at its configured position. It shrinks from eight gauge cells to six, then to `Ctx N%` as space runs out. Narrow terminals prioritize permissions, context warnings, model, and session title. Even the shortest context display retains `Ctx` to distinguish it from `5h` / `W` plan quotas.
 
 
 `custom-command` is also **not a default**. It runs an external command and requires both an explicit item and `tui.customStatusLine.command`.
 `custom-command` is also **not a default**. It runs an external command and requires both an explicit item and `tui.customStatusLine.command`.
 
 

+ 14 - 0
packages/tui/src/cli/contract.ts

@@ -1,4 +1,5 @@
 import { InvalidArgumentError, Option, type Command } from 'commander';
 import { InvalidArgumentError, Option, type Command } from 'commander';
+import { parseHeadlessModelOverride } from '../headless/model-selection.js';
 import type { TuiMode } from '../tui/engine/public.js';
 import type { TuiMode } from '../tui/engine/public.js';
 import { parseTuiStartupEnvironment } from './environment.js';
 import { parseTuiStartupEnvironment } from './environment.js';
 import type { TuiBuildEnvironment } from '../auth/environment.js';
 import type { TuiBuildEnvironment } from '../auth/environment.js';
@@ -7,6 +8,7 @@ const RETIRED_TOP_LEVEL_COMMAND_NAMES = new Set(['git', 'changes', 'projects']);
 
 
 export interface TuiInteractiveLaunchRequest {
 export interface TuiInteractiveLaunchRequest {
   readonly initialPrompt?: string;
   readonly initialPrompt?: string;
+  readonly model?: string;
   readonly sessionId?: string;
   readonly sessionId?: string;
   readonly showSessionPicker?: boolean;
   readonly showSessionPicker?: boolean;
   readonly continueLatestSession?: boolean;
   readonly continueLatestSession?: boolean;
@@ -17,6 +19,7 @@ export interface TuiInteractiveLaunchRequest {
 }
 }
 
 
 export interface RawTuiInteractiveOptions {
 export interface RawTuiInteractiveOptions {
+  readonly model?: string;
   readonly session?: string | boolean;
   readonly session?: string | boolean;
   readonly continue?: boolean;
   readonly continue?: boolean;
   readonly resume?: string;
   readonly resume?: string;
@@ -31,6 +34,7 @@ export function applyInteractiveCliContract(
 ): Command {
 ): Command {
   const configured = command
   const configured = command
     .argument('[prompt]', 'task to execute in the interactive TUI')
     .argument('[prompt]', 'task to execute in the interactive TUI')
+    .addOption(new Option('-m, --model <provider/model>', 'select the model for this Session only'))
     .addOption(new Option('--lane <lane>', 'managed backend lane for test or staging builds'))
     .addOption(new Option('--lane <lane>', 'managed backend lane for test or staging builds'))
     .addOption(
     .addOption(
       new Option('--session [id]', 'open a Session by id, or browse Sessions when id is omitted'),
       new Option('--session [id]', 'open a Session by id, or browse Sessions when id is omitted'),
@@ -75,8 +79,18 @@ export function resolveInteractiveLaunchRequest(
     throw new Error('--session, --continue, and --resume cannot be combined');
     throw new Error('--session, --continue, and --resume cannot be combined');
   }
   }
   const sessionId = explicitSessionId || compatibilitySessionId;
   const sessionId = explicitSessionId || compatibilitySessionId;
+  const model = commandOptions.model?.trim();
+  if (commandOptions.model !== undefined) {
+    parseHeadlessModelOverride(model ?? '');
+    if (showSessionPicker) {
+      throw new Error(
+        '--model requires a Session id with --session; use --session <id> or --continue.',
+      );
+    }
+  }
   return {
   return {
     ...(prompt ? { initialPrompt: prompt } : {}),
     ...(prompt ? { initialPrompt: prompt } : {}),
+    ...(model ? { model } : {}),
     ...(sessionId ? { sessionId } : {}),
     ...(sessionId ? { sessionId } : {}),
     ...(showSessionPicker ? { showSessionPicker: true } : {}),
     ...(showSessionPicker ? { showSessionPicker: true } : {}),
     ...(commandOptions.continue ? { continueLatestSession: true } : {}),
     ...(commandOptions.continue ? { continueLatestSession: true } : {}),

+ 7 - 1
packages/tui/src/cli/environment.ts

@@ -17,7 +17,13 @@ import { readTuiRegionPreference } from '../auth/region-preference.js';
 
 
 const MANAGED_BACKEND_LANE_PATTERN = /^[A-Za-z0-9._-]+$/u;
 const MANAGED_BACKEND_LANE_PATTERN = /^[A-Za-z0-9._-]+$/u;
 const STARTUP_ENVIRONMENT_OPTION = '--env';
 const STARTUP_ENVIRONMENT_OPTION = '--env';
-const ROOT_OPTIONS_WITH_REQUIRED_VALUES = new Set(['--lane', '--resume', '--tui-mode']);
+const ROOT_OPTIONS_WITH_REQUIRED_VALUES = new Set([
+  '--lane',
+  '--resume',
+  '--tui-mode',
+  '-m',
+  '--model',
+]);
 const STARTUP_ENVIRONMENT_ALIASES: Readonly<Record<string, TuiBuildEnvironment>> = Object.freeze({
 const STARTUP_ENVIRONMENT_ALIASES: Readonly<Record<string, TuiBuildEnvironment>> = Object.freeze({
   test: 'test',
   test: 'test',
   pre: 'staging',
   pre: 'staging',

+ 2 - 0
packages/tui/src/cli/main.ts

@@ -103,6 +103,7 @@ export async function runTuiCli(dependencies: RunTuiCliDependencies = {}): Promi
       allowStartupEnvironmentSelection: dependencies.allowStartupEnvironmentSelection,
       allowStartupEnvironmentSelection: dependencies.allowStartupEnvironmentSelection,
       launchTui: async ({
       launchTui: async ({
         initialPrompt,
         initialPrompt,
+        model,
         sessionId,
         sessionId,
         showSessionPicker,
         showSessionPicker,
         continueLatestSession,
         continueLatestSession,
@@ -114,6 +115,7 @@ export async function runTuiCli(dependencies: RunTuiCliDependencies = {}): Promi
         await launchTui({
         await launchTui({
           version: MINIMAX_CODE_VERSION,
           version: MINIMAX_CODE_VERSION,
           ...(initialPrompt ? { initialPrompt } : {}),
           ...(initialPrompt ? { initialPrompt } : {}),
+          ...(model ? { model } : {}),
           ...(sessionId ? { sessionId } : {}),
           ...(sessionId ? { sessionId } : {}),
           ...(showSessionPicker ? { showSessionPicker: true } : {}),
           ...(showSessionPicker ? { showSessionPicker: true } : {}),
           ...(continueLatestSession ? { continueLatestSession: true } : {}),
           ...(continueLatestSession ? { continueLatestSession: true } : {}),

+ 3 - 0
packages/tui/src/cli/program.ts

@@ -194,6 +194,7 @@ export function createTuiProgram(options: CreateTuiProgramOptions): Command {
     .option('--api-key-env <name>', 'environment variable containing the API key')
     .option('--api-key-env <name>', 'environment variable containing the API key')
     .option('--context-limit <tokens>', 'context limit for every listed model', parsePositiveSafeInteger)
     .option('--context-limit <tokens>', 'context limit for every listed model', parsePositiveSafeInteger)
     .option('--output-limit <tokens>', 'output limit for every listed model', parsePositiveSafeInteger)
     .option('--output-limit <tokens>', 'output limit for every listed model', parsePositiveSafeInteger)
+    .option('--support-image', 'declare image input support for every listed model')
     .option('--use', 'test the first model, then save and select it as the default')
     .option('--use', 'test the first model, then save and select it as the default')
     .action(
     .action(
       (commandOptions: {
       (commandOptions: {
@@ -203,6 +204,7 @@ export function createTuiProgram(options: CreateTuiProgramOptions): Command {
         model: string[];
         model: string[];
         contextLimit?: number;
         contextLimit?: number;
         outputLimit?: number;
         outputLimit?: number;
+        supportImage?: boolean;
         apiKeyEnv?: string;
         apiKeyEnv?: string;
         use?: boolean;
         use?: boolean;
       }) => {
       }) => {
@@ -217,6 +219,7 @@ export function createTuiProgram(options: CreateTuiProgramOptions): Command {
           models: commandOptions.model,
           models: commandOptions.model,
           contextLimit: commandOptions.contextLimit,
           contextLimit: commandOptions.contextLimit,
           outputLimit: commandOptions.outputLimit,
           outputLimit: commandOptions.outputLimit,
+          supportImage: commandOptions.supportImage,
           apiKeyEnv: commandOptions.apiKeyEnv,
           apiKeyEnv: commandOptions.apiKeyEnv,
           saveAndUse: commandOptions.use,
           saveAndUse: commandOptions.use,
         });
         });

+ 2 - 0
packages/tui/src/cli/provider-command.ts

@@ -14,6 +14,7 @@ export type McodeProviderCliRequest =
       readonly models: readonly string[];
       readonly models: readonly string[];
       readonly contextLimit?: number;
       readonly contextLimit?: number;
       readonly outputLimit?: number;
       readonly outputLimit?: number;
+      readonly supportImage?: boolean;
       readonly apiKeyEnv?: string;
       readonly apiKeyEnv?: string;
       readonly saveAndUse?: boolean;
       readonly saveAndUse?: boolean;
     }
     }
@@ -67,6 +68,7 @@ export async function runMcodeProviderCommand(
         apiFormat: request.apiFormat,
         apiFormat: request.apiFormat,
         models: request.models.map((modelId) => ({
         models: request.models.map((modelId) => ({
           modelId,
           modelId,
+          ...(request.supportImage ? { capabilities: { support_image: true } } : {}),
           ...(request.contextLimit !== undefined || request.outputLimit !== undefined
           ...(request.contextLimit !== undefined || request.outputLimit !== undefined
             ? {
             ? {
                 limit: {
                 limit: {

+ 3 - 0
packages/tui/src/provider/contract.ts

@@ -1,3 +1,5 @@
+import type { ModelConfig } from '@mavis/config';
+
 export const MCODE_PROVIDER_API_FORMATS = [
 export const MCODE_PROVIDER_API_FORMATS = [
   'anthropic-messages',
   'anthropic-messages',
   'openai-completions',
   'openai-completions',
@@ -74,6 +76,7 @@ export interface McodeProviderModelInput {
   readonly reasoning?: boolean;
   readonly reasoning?: boolean;
   readonly toolCall?: boolean;
   readonly toolCall?: boolean;
   readonly temperature?: boolean;
   readonly temperature?: boolean;
+  readonly capabilities?: Readonly<NonNullable<ModelConfig['capabilities']>>;
   readonly modalities?: { readonly input?: readonly string[]; readonly output?: readonly string[] };
   readonly modalities?: { readonly input?: readonly string[]; readonly output?: readonly string[] };
   readonly limit?: { readonly context?: number; readonly output?: number };
   readonly limit?: { readonly context?: number; readonly output?: number };
 }
 }

+ 1 - 1
packages/tui/src/tui/controller/product/command-flow.ts

@@ -1218,7 +1218,7 @@ export class TuiCommandFlow {
           this.options.append(
           this.options.append(
             formatTuiActionFailure(error, {
             formatTuiActionFailure(error, {
               summary: "Couldn't reload TUI configuration.",
               summary: "Couldn't reload TUI configuration.",
-              nextStep: 'Fix keybindings.json and retry /reload.',
+              nextStep: 'Resolve the reported configuration or plugin refresh error, then retry /reload.',
             }),
             }),
             'warning',
             'warning',
           );
           );

+ 1 - 1
packages/tui/src/tui/controller/runtime/runtime-event-flow.ts

@@ -458,7 +458,7 @@ export class TuiRuntimeEventFlow {
         event.variant === 'auth'
         event.variant === 'auth'
           ? 'Content review authentication expired. Run /login, then retry.'
           ? 'Content review authentication expired. Run /login, then retry.'
           : event.variant === 'network'
           : event.variant === 'network'
-            ? 'The content review service could not be reached, so this response stopped. Check your network and proxy or VPN settings, then retry.'
+            ? 'Content review did not return a usable result, so this response stopped. Unreviewed output was withheld. Retry; if this persists, use /feedback to report the review failure.'
             : 'Content review withdrew this response. Rephrase your request, then retry.',
             : 'Content review withdrew this response. Rephrase your request, then retry.',
         'warning',
         'warning',
       );
       );

+ 11 - 4
packages/tui/src/tui/engine/LOCAL_CHANGES.json

@@ -47,6 +47,13 @@
       "reason": "Keep strict TypeScript fixes and product Markdown hooks, plus Pi's post-baseline wrapped-table style restoration. 链接标签使用独立样式上下文,防止外层正文或标题颜色覆盖链接颜色。 稳定 Markdown 块缓存",
       "reason": "Keep strict TypeScript fixes and product Markdown hooks, plus Pi's post-baseline wrapped-table style restoration. 链接标签使用独立样式上下文,防止外层正文或标题颜色覆盖链接颜色。 稳定 Markdown 块缓存",
       "behaviorImpact": "MCode keeps its compact code presentation without reimplementing Pi Markdown or leaking wrapped link styles into table borders and adjacent cells. 链接保留嵌套强调与行内代码,并在结束后恢复外层样式。 保留两个尾部块并对引用回退全文。"
       "behaviorImpact": "MCode keeps its compact code presentation without reimplementing Pi Markdown or leaking wrapped link styles into table borders and adjacent cells. 链接保留嵌套强调与行内代码,并在结束后恢复外层样式。 保留两个尾部块并对引用回退全文。"
     },
     },
+    {
+      "path": "components/scroll-view.ts",
+      "currentSha256": "6f82fb1f426fdf81ca7058e6a1ea4a94c9ddd8f0e15d69c9020f9c66ca5411dd",
+      "changeIds": ["L035"],
+      "reason": "Allow hosts to reserve a wider always-visible scrollbar gutter while retaining at least one content column.",
+      "behaviorImpact": "The fullscreen transcript reserves three columns; other scroll views retain the default one-column gutter."
+    },
     {
     {
       "path": "components/settings-list.ts",
       "path": "components/settings-list.ts",
       "currentSha256": "959f7db23190e0e526ca84c3032443d7d15248d891809f28c837cfa958692b5c",
       "currentSha256": "959f7db23190e0e526ca84c3032443d7d15248d891809f28c837cfa958692b5c",
@@ -148,11 +155,11 @@
     },
     },
     {
     {
       "path": "tui-alt-screen.ts",
       "path": "tui-alt-screen.ts",
-      "currentSha256": "dd8acaa441bab2e674e6cba946dcb130b97e51098adf158bda1f73bd027d7a4b",
-      "changeIds": ["L005", "L011", "L019", "L020", "L025", "L031"],
+      "currentSha256": "b1e6b332e976c5224f77ce82ee850d35c601ad18c3719d9c824b7c9db1245a78",
+      "changeIds": ["L005", "L011", "L019", "L020", "L025", "L031", "L035"],
       "upstreamCommit": "4e494929998d6bc4fccf75e0a233f727db4b70ee,1ac6128e66bd44668b58db773c2a76c29163ade5",
       "upstreamCommit": "4e494929998d6bc4fccf75e0a233f727db4b70ee,1ac6128e66bd44668b58db773c2a76c29163ade5",
-      "reason": "Keep strict TypeScript fixes and layout-first mouse dispatch, join path and kebab-case word segments, and expose configurable fullscreen selection copying helpers. 活动搜索索引与命中复用 独立面板声明键盘分页归属。",
-      "behaviorImpact": "Fullscreen product interaction clicks and Pi-owned text selection coexist; double-click selects complete path-like tokens, and hosts can disable automatic copy or copy an active selection explicitly. 搜索交互复用索引。 handlesViewportKeys 为 true 时,fullscreen 分页交给焦点面板;默认仍由外层视口处理。"
+      "reason": "Keep strict TypeScript fixes and layout-first mouse dispatch, join path and kebab-case word segments, and expose configurable fullscreen selection copying helpers. 活动搜索索引与命中复用 独立面板声明键盘分页归属。 Reserve scrollbar hit targets for gutter columns and support track jumps and thumb dragging.",
+      "behaviorImpact": "Fullscreen product interaction clicks and Pi-owned text selection coexist; double-click selects complete path-like tokens, and hosts can disable automatic copy or copy an active selection explicitly. 搜索交互复用索引。 handlesViewportKeys 为 true 时,fullscreen 分页交给焦点面板;默认仍由外层视口处理。 Fullscreen track presses jump and drag without consuming adjacent content; visible overlays cancel active scrollbar drags."
     },
     },
     {
     {
       "path": "tui-main-screen.ts",
       "path": "tui-main-screen.ts",

+ 9 - 0
packages/tui/src/tui/engine/LOCAL_CHANGES.md

@@ -106,3 +106,12 @@ Remove `L024` when the selected Pi baseline natively matches legacy-terminal `Ct
 - Change: strip leading zone prefixes before comparing screen lines, so initial frames, differential redraws, and history caches stay clean. Fullscreen behavior is unchanged.
 - Change: strip leading zone prefixes before comparing screen lines, so initial frames, differential redraws, and history caches stay clean. Fullscreen behavior is unchanged.
 - Evidence: `test/unit/tui-engine-local-deltas.test.ts` checks initial and differential writes.
 - Evidence: `test/unit/tui-engine-local-deltas.test.ts` checks initial and differential writes.
 - Removal condition: the selected Pi baseline supplies equivalent normal-mode filtering.
 - Removal condition: the selected Pi baseline supplies equivalent normal-mode filtering.
+
+## L035: Fullscreen scrollbar track interaction
+
+- Origin: adapted from [drowzeys/minimax-code commit 0b24a7778741fda335a9a2074464c69911feb1aa](https://github.com/drowzeys/minimax-code/commit/0b24a7778741fda335a9a2074464c69911feb1aa), contributed by drowzeys under the existing MIT license in [issue #216](https://github.com/MiniMax-AI/minimax-code/issues/216).
+- Product contract: the fullscreen transcript enables an always-visible scrollbar with a three-column hit gutter. Track presses center the thumb and jump before dragging; thumb presses retain their grab offset without an initial jump.
+- Minimal difference: `components/scroll-view.ts` accepts an optional `scrollbarGutter` for always-visible bars, defaulting to one column and retaining at least one content column on narrow terminals. `tui-alt-screen.ts` restricts wide hit targets to that reserved gutter, factors drag mapping into `dragScrollbarTo`, and cancels a drag when an overlay receives the next mouse event. Auto bars retain a one-column target. `layout.ts` stays at the Pi baseline.
+- Adaptation: the original contribution widened hits over two live content columns. The reserved gutter prevents those presses from swallowing content clicks or text selection. `chat-layout.ts` opts into the three-column gutter.
+- Evidence: `test/unit/tui-scrollbar-interaction.test.ts` drives SGR press, motion, release and wheel events through VirtualTerminal, covering track jumps, thumb grabs, narrow terminals, content routing, selection, overlays and the actual fullscreen ChatLayout.
+- Removal condition: the selected Pi baseline provides equivalent reserved-gutter track and drag interaction and MCode migrates to it.

+ 8 - 2
packages/tui/src/tui/engine/components/scroll-view.ts

@@ -9,6 +9,8 @@ export interface ScrollViewOptions {
 	primary?: boolean;
 	primary?: boolean;
 	overscroll?: "chain" | "contain";
 	overscroll?: "chain" | "contain";
 	scrollbar?: ScrollViewScrollbar;
 	scrollbar?: ScrollViewScrollbar;
+	/** Columns reserved for an always-visible scrollbar, including its hit gutter. */
+	scrollbarGutter?: number;
 	scrollbarStyle?: (text: string) => string;
 	scrollbarStyle?: (text: string) => string;
 	scrollbarHideDelayMs?: number;
 	scrollbarHideDelayMs?: number;
 }
 }
@@ -24,6 +26,7 @@ export class ScrollView extends Container {
 	readonly primary: boolean;
 	readonly primary: boolean;
 	readonly overscroll: "chain" | "contain";
 	readonly overscroll: "chain" | "contain";
 	readonly scrollbarStyle: (text: string) => string;
 	readonly scrollbarStyle: (text: string) => string;
+	private readonly scrollbarGutter: number;
 	private currentScrollbar: ScrollViewScrollbar;
 	private currentScrollbar: ScrollViewScrollbar;
 	private readonly scrollbarHideDelayMs: number;
 	private readonly scrollbarHideDelayMs: number;
 	private currentScrollTop = 0;
 	private currentScrollTop = 0;
@@ -48,6 +51,9 @@ export class ScrollView extends Container {
 		this.primary = options.primary ?? false;
 		this.primary = options.primary ?? false;
 		this.overscroll = options.overscroll ?? "chain";
 		this.overscroll = options.overscroll ?? "chain";
 		this.currentScrollbar = options.scrollbar ?? "hidden";
 		this.currentScrollbar = options.scrollbar ?? "hidden";
+		this.scrollbarGutter = Number.isFinite(options.scrollbarGutter)
+			? Math.max(1, Math.floor(options.scrollbarGutter ?? 1))
+			: 1;
 		this.scrollbarStyle = options.scrollbarStyle ?? ((text) => `\x1b[100m${text}\x1b[49m`);
 		this.scrollbarStyle = options.scrollbarStyle ?? ((text) => `\x1b[100m${text}\x1b[49m`);
 		this.scrollbarHideDelayMs = Math.max(0, Math.floor(options.scrollbarHideDelayMs ?? 1000));
 		this.scrollbarHideDelayMs = Math.max(0, Math.floor(options.scrollbarHideDelayMs ?? 1000));
 	}
 	}
@@ -84,7 +90,7 @@ export class ScrollView extends Container {
 	}
 	}
 
 
 	getContentWidth(width: number): number {
 	getContentWidth(width: number): number {
-		return this.scrollbar === "always" && width > 1 ? width - 1 : width;
+		return this.scrollbar === "always" && width > 1 ? Math.max(1, width - this.scrollbarGutter) : width;
 	}
 	}
 
 
 	private markScrollbarActivity(): void {
 	private markScrollbarActivity(): void {
@@ -207,7 +213,7 @@ export class ScrollView extends Container {
 	override render(width: number): string[] {
 	override render(width: number): string[] {
 		const contentWidth = this.getContentWidth(width);
 		const contentWidth = this.getContentWidth(width);
 		const lines = this.child.render(contentWidth);
 		const lines = this.child.render(contentWidth);
-		return contentWidth === width ? lines : lines.map((line) => `${line} `);
+		return contentWidth === width ? lines : lines.map((line) => `${line}${" ".repeat(width - contentWidth)}`);
 	}
 	}
 
 
 	[LAYOUT_NODE](): ScrollLayoutNode {
 	[LAYOUT_NODE](): ScrollLayoutNode {

+ 42 - 18
packages/tui/src/tui/engine/tui-alt-screen.ts

@@ -776,11 +776,18 @@ export class TuiAltScreen extends TuiBase implements ViewportTUI {
 		for (const scrollView of getScrollViewsAt(this.currentLayout, x, y)) {
 		for (const scrollView of getScrollViewsAt(this.currentLayout, x, y)) {
 			const box = getScrollViewBox(this.currentLayout, scrollView);
 			const box = getScrollViewBox(this.currentLayout, scrollView);
 			const geometry = box ? getScrollbarGeometry(box) : undefined;
 			const geometry = box ? getScrollbarGeometry(box) : undefined;
+			// Only reserved gutter columns may capture presses beside the painted bar.
+			const hitColumns = box
+				? scrollView.scrollbar === "always"
+					? box.rect.width - scrollView.getContentWidth(box.rect.width)
+					: 1
+				: 0;
 			if (
 			if (
 				geometry &&
 				geometry &&
-				x === geometry.column &&
-				y >= geometry.thumbTop &&
-				y < geometry.thumbTop + geometry.thumbHeight
+				x >= geometry.column - (hitColumns - 1) &&
+				x <= geometry.column &&
+				y >= geometry.trackTop &&
+				y < geometry.trackTop + geometry.trackHeight
 			) {
 			) {
 				return { scrollView, geometry };
 				return { scrollView, geometry };
 			}
 			}
@@ -804,25 +811,16 @@ export class TuiAltScreen extends TuiBase implements ViewportTUI {
 	}
 	}
 
 
 	private handleScrollbarMouseEvent(event: SgrMouseEvent): boolean {
 	private handleScrollbarMouseEvent(event: SgrMouseEvent): boolean {
+		if (this.hasOverlay()) {
+			this.stopScrollbarDrag();
+			return false;
+		}
 		if (this.scrollbarDrag) {
 		if (this.scrollbarDrag) {
 			if (event.release) {
 			if (event.release) {
 				this.stopScrollbarDrag();
 				this.stopScrollbarDrag();
 				return true;
 				return true;
 			}
 			}
-			const box = this.currentLayout
-				? getScrollViewBox(this.currentLayout, this.scrollbarDrag.scrollView)
-				: undefined;
-			const geometry = box ? getScrollbarGeometry(box) : undefined;
-			if (geometry) {
-				const maxThumbOffset = geometry.trackHeight - geometry.thumbHeight;
-				const thumbOffset = Math.max(
-					0,
-					Math.min(maxThumbOffset, event.y - geometry.trackTop - this.scrollbarDrag.grabOffset),
-				);
-				const scrollTop =
-					maxThumbOffset === 0 ? 0 : Math.round((thumbOffset / maxThumbOffset) * geometry.maxScrollTop);
-				this.scrollbarDrag.scrollView.scrollTo(scrollTop);
-			}
+			this.dragScrollbarTo(event.y);
 			return true;
 			return true;
 		}
 		}
 
 
@@ -839,13 +837,39 @@ export class TuiAltScreen extends TuiBase implements ViewportTUI {
 		this.pressedUrl = undefined;
 		this.pressedUrl = undefined;
 		this.selectionDragged = false;
 		this.selectionDragged = false;
 		this.setScrollbarHover(target.scrollView);
 		this.setScrollbarHover(target.scrollView);
+		const onThumb =
+			event.y >= target.geometry.thumbTop &&
+			event.y < target.geometry.thumbTop + target.geometry.thumbHeight;
 		this.scrollbarDrag = {
 		this.scrollbarDrag = {
 			scrollView: target.scrollView,
 			scrollView: target.scrollView,
-			grabOffset: event.y - target.geometry.thumbTop,
+			grabOffset: onThumb
+				? event.y - target.geometry.thumbTop
+				: Math.max(0, Math.floor(target.geometry.thumbHeight / 2)),
 		};
 		};
+		// A press on the empty track jumps the thumb so its center follows the
+		// pointer, then keeps dragging from there; a press on the thumb itself
+		// grabs it in place without moving.
+		if (!onThumb) this.dragScrollbarTo(event.y);
 		return true;
 		return true;
 	}
 	}
 
 
+	private dragScrollbarTo(y: number): void {
+		if (!this.scrollbarDrag || !this.currentLayout) return;
+		const box = getScrollViewBox(this.currentLayout, this.scrollbarDrag.scrollView);
+		const geometry = box ? getScrollbarGeometry(box) : undefined;
+		if (!geometry) return;
+		const maxThumbOffset = geometry.trackHeight - geometry.thumbHeight;
+		const thumbOffset = Math.max(
+			0,
+			Math.min(maxThumbOffset, y - geometry.trackTop - this.scrollbarDrag.grabOffset),
+		);
+		const scrollTop =
+			maxThumbOffset === 0
+				? 0
+				: Math.round((thumbOffset / maxThumbOffset) * geometry.maxScrollTop);
+		this.scrollbarDrag.scrollView.scrollTo(scrollTop);
+	}
+
 	private stopScrollbarDrag(): void {
 	private stopScrollbarDrag(): void {
 		this.scrollbarDrag = undefined;
 		this.scrollbarDrag = undefined;
 	}
 	}

+ 1 - 0
packages/tui/src/tui/features/settings/status-line-copy.ts

@@ -52,6 +52,7 @@ const descriptions: Record<Exclude<TuiStatusLineItem, 'build-mode'>, readonly [s
   'token-quota': ['Account token quota', '账户额度'],
   'token-quota': ['Account token quota', '账户额度'],
   'cache-read-ratio': ['Session cache read ratio', '会话缓存读取比例'],
   'cache-read-ratio': ['Session cache read ratio', '会话缓存读取比例'],
   'context-remaining': ['Remaining context window', '剩余上下文窗口'],
   'context-remaining': ['Remaining context window', '剩余上下文窗口'],
+  'context-meter': ['Remaining context gauge', '剩余上下文刻度条'],
   'custom-command': ['Configured custom command output', '已配置的自定义命令输出'],
   'custom-command': ['Configured custom command output', '已配置的自定义命令输出'],
 };
 };
 
 

+ 27 - 3
packages/tui/src/tui/launcher.ts

@@ -21,7 +21,8 @@ import {
 } from '../host/tui-keybindings.js';
 } from '../host/tui-keybindings.js';
 import { createTuiApp, type CreateTuiAppOptions, type TuiApp } from './app.js';
 import { createTuiApp, type CreateTuiAppOptions, type TuiApp } from './app.js';
 import type { CreatedTuiRuntime, CreateTuiRuntimeDependencies } from '../runtime/lifecycle.js';
 import type { CreatedTuiRuntime, CreateTuiRuntimeDependencies } from '../runtime/lifecycle.js';
-import type { TuiWorkspaceRoot } from '../runtime/port.js';
+import { parseHeadlessModelOverride } from '../headless/model-selection.js';
+import type { TuiRuntime, TuiWorkspaceRoot } from '../runtime/port.js';
 import { createDeferredTuiRuntime } from '../runtime/deferred.js';
 import { createDeferredTuiRuntime } from '../runtime/deferred.js';
 import {
 import {
   captureTuiIncidentBestEffort,
   captureTuiIncidentBestEffort,
@@ -66,6 +67,7 @@ const MINIMAX_CODE_EXIT_SLOGAN = 'Intelligence with everyone, bye~';
 export interface LaunchTuiOptions {
 export interface LaunchTuiOptions {
   version: string;
   version: string;
   initialPrompt?: string;
   initialPrompt?: string;
+  model?: string;
   sessionId?: string;
   sessionId?: string;
   showSessionPicker?: boolean;
   showSessionPicker?: boolean;
   continueLatestSession?: boolean;
   continueLatestSession?: boolean;
@@ -83,7 +85,8 @@ export interface LaunchTuiOptions {
 type LaunchApp = Pick<TuiApp, 'ready' | 'firstFrame' | 'start' | 'stop' | 'stopped' | 'submit'> & {
 type LaunchApp = Pick<TuiApp, 'ready' | 'firstFrame' | 'start' | 'stop' | 'stopped' | 'submit'> & {
   readonly editor?: Pick<TuiApp['editor'], 'disableSubmit'>;
   readonly editor?: Pick<TuiApp['editor'], 'disableSubmit'>;
   readonly setStartupStatus?: TuiApp['setStartupStatus'];
   readonly setStartupStatus?: TuiApp['setStartupStatus'];
-  readonly controller?: Pick<TuiApp['controller'], 'snapshot'>;
+  readonly controller?: Pick<TuiApp['controller'], 'snapshot'> &
+    Partial<Pick<TuiApp['controller'], 'ensureSession'>>;
   readonly openSession?: TuiApp['openSession'];
   readonly openSession?: TuiApp['openSession'];
   readonly continueLatestSession?: TuiApp['continueLatestSession'];
   readonly continueLatestSession?: TuiApp['continueLatestSession'];
   readonly suspend?: TuiApp['suspend'];
   readonly suspend?: TuiApp['suspend'];
@@ -150,6 +153,12 @@ export async function launchTui(
   options: LaunchTuiOptions,
   options: LaunchTuiOptions,
   dependencies: LaunchTuiDependencies = {},
   dependencies: LaunchTuiDependencies = {},
 ): Promise<void> {
 ): Promise<void> {
+  if (options.model !== undefined) parseHeadlessModelOverride(options.model.trim());
+  if (options.model !== undefined && options.showSessionPicker) {
+    throw new Error(
+      '--model requires a Session id with --session; use --session <id> or --continue.',
+    );
+  }
   if (!options.terminal && (!process.stdin.isTTY || !process.stdout.isTTY)) {
   if (!options.terminal && (!process.stdin.isTTY || !process.stdout.isTTY)) {
     throw new Error('Minimax Code interactive mode requires a TTY.');
     throw new Error('Minimax Code interactive mode requires a TTY.');
   }
   }
@@ -521,7 +530,7 @@ export async function launchTui(
         durationMs: performance.now() - runtimeStartedAt,
         durationMs: performance.now() - runtimeStartedAt,
       });
       });
       await app.ready;
       await app.ready;
-      initialStateReady = await prepareInitialTuiState(app, observability, options);
+      initialStateReady = await prepareInitialTuiState(app, observability, options, runtime.adapter);
       startupStatus.stop();
       startupStatus.stop();
       startupStatus = undefined;
       startupStatus = undefined;
       if (app.editor) app.editor.disableSubmit = false;
       if (app.editor) app.editor.disableSubmit = false;
@@ -884,6 +893,7 @@ async function prepareInitialTuiState(
   app: LaunchApp,
   app: LaunchApp,
   observability: TuiObservability,
   observability: TuiObservability,
   options: LaunchTuiOptions,
   options: LaunchTuiOptions,
+  runtime: TuiRuntime,
 ): Promise<boolean> {
 ): Promise<boolean> {
   const sessionId = options.sessionId?.trim();
   const sessionId = options.sessionId?.trim();
   if (sessionId) {
   if (sessionId) {
@@ -912,6 +922,20 @@ async function prepareInitialTuiState(
       return false;
       return false;
     }
     }
   }
   }
+  if (options.model !== undefined) {
+    const model = parseHeadlessModelOverride(options.model.trim());
+    if (!app.controller?.ensureSession || !app.openSession) {
+      throw new Error('TUI Session model selection is unavailable.');
+    }
+    const session = await app.controller.ensureSession();
+    // The normal model picker also saves the global default. Startup overrides
+    // must use the Session-only operation, before login checks or any submission.
+    if (!(await runtime.selectSessionModel(model, session.sessionId))) {
+      throw new Error(`Could not select --model ${options.model}. Check the provider and model id.`);
+    }
+    // Rehydrate account status, model/effort and queued-input state from Runtime.
+    await app.openSession(session.sessionId);
+  }
   if (options.showSessionPicker) await app.submit('/sessions');
   if (options.showSessionPicker) await app.submit('/sessions');
   return true;
   return true;
 }
 }

+ 2 - 0
packages/tui/src/tui/shell/chat-layout.ts

@@ -124,6 +124,8 @@ export class TuiChatLayout implements Component {
       follow: 'end',
       follow: 'end',
       primary: true,
       primary: true,
       overscroll: 'contain',
       overscroll: 'contain',
+      scrollbar: 'always',
+      scrollbarGutter: 3,
     });
     });
     this.fullscreenLayoutRoot = new VStack([
     this.fullscreenLayoutRoot = new VStack([
       {
       {

+ 20 - 8
packages/tui/src/tui/shell/chrome.ts

@@ -134,7 +134,12 @@ export class TuiStatusLine implements Component {
 function resolveStatusLineItems(state: TuiShellState): readonly TuiStatusLineItem[] {
 function resolveStatusLineItems(state: TuiShellState): readonly TuiStatusLineItem[] {
   const items = state.statusLineItems ?? TUI_STATUS_LINE_DEFAULT_ITEMS;
   const items = state.statusLineItems ?? TUI_STATUS_LINE_DEFAULT_ITEMS;
   if (!items.includes('build-mode')) {
   if (!items.includes('build-mode')) {
-    if (state.statusLineItems !== undefined) return items;
+    if (state.statusLineItems !== undefined) {
+      // The opt-in meter replaces the percentage at the meter's configured position.
+      return items.includes('context-meter')
+        ? items.filter((item) => item !== 'context-remaining')
+        : items;
+    }
     const remaining = contextRemainingPercent(state);
     const remaining = contextRemainingPercent(state);
     return items.filter((item) =>
     return items.filter((item) =>
       remaining === undefined ? item !== 'context-remaining' : item !== 'context-window',
       remaining === undefined ? item !== 'context-remaining' : item !== 'context-window',
@@ -249,11 +254,12 @@ function buildStatusSegment(
         { shrinkPriority: 48, dropPriority: 10 },
         { shrinkPriority: 48, dropPriority: 10 },
       );
       );
     case 'context-remaining':
     case 'context-remaining':
+    case 'context-meter':
       return createStatusSegment(
       return createStatusSegment(
         [
         [
-          renderContextRemaining(state, 'full'),
-          renderContextRemaining(state, 'compact'),
-          renderContextRemaining(state, 'minimal'),
+          renderContextRemaining(state, 'full', item === 'context-meter'),
+          renderContextRemaining(state, 'compact', item === 'context-meter'),
+          renderContextRemaining(state, 'minimal', item === 'context-meter'),
         ],
         ],
         { shrinkPriority: 50, dropPriority: 80 },
         { shrinkPriority: 50, dropPriority: 80 },
       );
       );
@@ -443,19 +449,25 @@ function contextRemainingPercent(state: TuiShellState): number | undefined {
   return Math.round(((contextWindow - used) / contextWindow) * 100);
   return Math.round(((contextWindow - used) / contextWindow) * 100);
 }
 }
 
 
+/** Both presentations share usage, fallback, thresholds and the minimal label. */
 function renderContextRemaining(
 function renderContextRemaining(
   state: TuiShellState,
   state: TuiShellState,
   density: 'full' | 'compact' | 'minimal' = 'full',
   density: 'full' | 'compact' | 'minimal' = 'full',
+  meter = false,
 ): string {
 ): string {
   const remaining = contextRemainingPercent(state);
   const remaining = contextRemainingPercent(state);
   if (remaining === undefined) return '';
   if (remaining === undefined) return '';
   const color = remaining <= 10 ? colors.error : remaining <= 25 ? colors.warning : colors.muted;
   const color = remaining <= 10 ? colors.error : remaining <= 25 ? colors.warning : colors.muted;
+  let gauge = '';
+  if (meter && density !== 'minimal') {
+    const cells = density === 'full' ? 8 : 6;
+    const filled = Math.round((remaining / 100) * cells);
+    gauge = `▕${'█'.repeat(filled)}${'░'.repeat(cells - filled)}▏ `;
+  }
   const label =
   const label =
     density === 'full'
     density === 'full'
-      ? `Context ${remaining}% left`
-      : density === 'compact'
-        ? `Ctx ${remaining}%`
-        : `Ctx ${remaining}%`;
+      ? `Context ${gauge}${remaining}% left`
+      : `Ctx ${gauge}${remaining}%`;
   return chalk.hex(color)(label);
   return chalk.hex(color)(label);
 }
 }
 
 

+ 4 - 1
packages/tui/src/tui/shell/status-line-items.ts

@@ -9,7 +9,7 @@
  * (`tui.statusLine`), so renaming one is a breaking change. Legacy aliases keep
  * (`tui.statusLine`), so renaming one is a breaking change. Legacy aliases keep
  * older configuration working.
  * older configuration working.
  *
  *
- * `cache-read-ratio`, `context-remaining` and `review-link` are conditional
+ * `cache-read-ratio`, `context-remaining`, `context-meter` and `review-link` are conditional
  * items. They only render when the Runtime surfaces their matching projection
  * items. They only render when the Runtime surfaces their matching projection
  * into the shell state, so unsupported or not-yet-recorded metrics stay silent.
  * into the shell state, so unsupported or not-yet-recorded metrics stay silent.
  *
  *
@@ -34,6 +34,7 @@ export const TUI_STATUS_LINE_ITEMS = [
   'token-quota',
   'token-quota',
   'cache-read-ratio',
   'cache-read-ratio',
   'context-remaining',
   'context-remaining',
+  'context-meter',
   'custom-command',
   'custom-command',
 ] as const;
 ] as const;
 
 
@@ -67,6 +68,8 @@ const TUI_STATUS_LINE_ITEM_ALIASES: Readonly<Record<string, TuiStatusLineItem>>
   'cache-read': 'cache-read-ratio',
   'cache-read': 'cache-read-ratio',
   context: 'context-remaining',
   context: 'context-remaining',
   'context-left': 'context-remaining',
   'context-left': 'context-remaining',
+  'context-bar': 'context-meter',
+  'context-gauge': 'context-meter',
   custom: 'custom-command',
   custom: 'custom-command',
 };
 };
 
 

+ 247 - 5
packages/tui/test/unit/tui-app.test.ts

@@ -8,6 +8,10 @@ import {
   VStack,
   VStack,
   type Terminal,
   type Terminal,
 } from "../../src/tui/engine/public.js";
 } from "../../src/tui/engine/public.js";
+import { createTuiProgram } from "../../src/cli/program.js";
+import { resolveTuiStartupEnvironmentOption } from "../../src/cli/environment.js";
+import { runTuiCli } from "../../src/cli/main.js";
+import { launchTui } from "../../src/tui/launcher.js";
 import { createTuiApp } from "../../src/tui/app.js";
 import { createTuiApp } from "../../src/tui/app.js";
 import type {
 import type {
   TuiModel,
   TuiModel,
@@ -10607,7 +10611,10 @@ describe("createTuiApp", () => {
     await app.stop();
     await app.stop();
   });
   });
 
 
-  it("restores a content-review retraction to the Composer without a failed Queue item", async () => {
+  it.each([
+    ['content', 'Content review withdrew this response. Rephrase your request, then retry.'],
+    ['network', 'Content review did not return a usable result, so this response stopped.'],
+  ] as const)("presents a %s review stop without a failed Queue item", async (variant, notice) => {
     const terminal = new FakeTerminal();
     const terminal = new FakeTerminal();
     const runtime = createRuntime();
     const runtime = createRuntime();
     const busEvents: TuiRuntimeEvent[] = [];
     const busEvents: TuiRuntimeEvent[] = [];
@@ -10655,7 +10662,7 @@ describe("createTuiApp", () => {
         type: "content.retry.exceeded",
         type: "content.retry.exceeded",
         timestamp: 100,
         timestamp: 100,
         source: "runtime-v2",
         source: "runtime-v2",
-        payload: { sessionId: "session-1", variant: "content" },
+        payload: { sessionId: "session-1", variant },
       }),
       }),
       runtimeEvent({
       runtimeEvent({
         type: "message.rewind",
         type: "message.rewind",
@@ -10671,9 +10678,8 @@ describe("createTuiApp", () => {
       expect(app.editor.getText()).toBe("review this response"),
       expect(app.editor.getText()).toBe("review this response"),
     );
     );
     const rendered = app.tui.render(100).join("\n");
     const rendered = app.tui.render(100).join("\n");
-    expect(rendered).toContain(
-      "Content review withdrew this response. Rephrase your request, then retry.",
-    );
+    expect(rendered).toContain(notice);
+    expect(rendered).not.toContain('Check your network');
     expect(rendered).not.toContain("Message kept under Failed");
     expect(rendered).not.toContain("Message kept under Failed");
     await app.submit("/queue");
     await app.submit("/queue");
     expect(app.interaction.current()).toBeUndefined();
     expect(app.interaction.current()).toBeUndefined();
@@ -13174,3 +13180,239 @@ describe("createTuiApp", () => {
     }
     }
   });
   });
 });
 });
+
+describe("interactive CLI model startup", () => {
+  it.each([
+    [
+      "new with prompt",
+      ["-m", "custom_provider:relay/vendor/model#thinking", "hello"],
+      "session-1",
+      true,
+    ],
+    [
+      "new without prompt",
+      ["--model=custom_provider:relay/vendor/model#thinking"],
+      "session-1",
+      false,
+    ],
+    [
+      "explicit session",
+      ["--session", "existing", "--model", "custom_provider:relay/vendor/model#thinking", "hello"],
+      "existing",
+      true,
+    ],
+    [
+      "continue",
+      ["--continue", "--model", "custom_provider:relay/vendor/model#thinking", "hello"],
+      "existing",
+      true,
+    ],
+    [
+      "missing session",
+      ["--session", "missing", "-m", "custom_provider:relay/vendor/model#thinking", "hello"],
+      "missing",
+      false,
+    ],
+    [
+      "empty continue",
+      ["--continue", "-m", "custom_provider:relay/vendor/model#thinking", "hello"],
+      "existing",
+      false,
+    ],
+    ["invalid provider", ["--model", "missing/model", "hello"], "session-1", false],
+    ["invalid model", ["--model", "custom_provider:relay/missing", "hello"], "session-1", false],
+  ] as const)(
+    "routes %s through launch, hydration and submission",
+    async (_name, args, sessionId, hasPrompt) => {
+      const dataDir = await mkdtemp(join(tmpdir(), "mcode-model-startup-"));
+      const terminal = new FakeTerminal();
+      const runtime = createRuntime();
+      runtime.listBackgroundTasks = vi.fn(async () => []);
+      const models = new Map<string, { providerId: string; modelId: string; variant?: string }>();
+      const requested = {
+        providerId: "custom_provider:relay",
+        modelId: "vendor/model",
+        variant: "thinking",
+      };
+      const existing = { sessionId: "existing", workspaceDir: "/workspace", updatedAt: 10 };
+      vi.mocked(runtime.listSessions).mockResolvedValue([existing]);
+      vi.mocked(runtime.listSessionPage).mockResolvedValue({
+        sessions: [existing],
+        hasMore: false,
+      });
+      vi.mocked(runtime.getSession).mockImplementation(async (id) => ({
+        sessionId: id,
+        workspaceDir: "/workspace",
+        model: models.get(id),
+      }));
+      const failedResume = _name === "missing session" || _name === "empty continue";
+      if (_name === "missing session")
+        vi.mocked(runtime.getSession).mockRejectedValue(new Error("Session not found"));
+      if (_name === "empty continue") vi.mocked(runtime.listSessions).mockResolvedValue([]);
+      runtime.selectModel = vi.fn(async () => true);
+      runtime.selectSessionModel = vi.fn(async (model, id) => {
+        if (model.providerId !== requested.providerId || model.modelId !== requested.modelId) {
+          throw new Error("Unknown provider or model");
+        }
+        models.set(id, model);
+        return true;
+      });
+      vi.mocked(runtime.listModels).mockImplementation(async (id) => [
+        { ...requested, name: "Startup model", selected: Boolean(id && models.has(id)) },
+      ]);
+      // A global managed default needs login; the selected BYOK Session does not.
+      vi.mocked(runtime.getAccountStatus).mockImplementation(async (id) => ({
+        status: id && models.has(id) ? "ready" : "needs-login",
+        modelSource: id && models.has(id) ? "byok" : "token-plan",
+        managedTokenPresent: false,
+        warnings: [],
+      }));
+      const sentModels: unknown[] = [];
+      vi.mocked(runtime.sendMessage).mockImplementation(async function* (request) {
+        sentModels.push(models.get(request.id));
+        yield { type: "delta", content: "offline reply" };
+        yield { type: "done" };
+      });
+      const invalid = _name.startsWith("invalid");
+      const stderr = vi.fn((_text: string, callback?: () => void) => callback?.());
+      const processRef = {
+        title: "test",
+        argv: ["node", "cli.js", ...args],
+        env: {},
+        versions: { node: "24.2.0" },
+        stdout: {
+          destroyed: false,
+          writableEnded: false,
+          write: vi.fn((_text: string, cb?: () => void) => cb?.()),
+        },
+        stderr: { destroyed: false, writableEnded: false, write: stderr },
+        exit: vi.fn(),
+        exitCode: 0,
+      };
+      let app: ReturnType<typeof createTuiApp> | undefined;
+      const running = runTuiCli({
+        processRef,
+        configureNetworkProxy: () => undefined,
+        launchTui: (input) =>
+          launchTui(
+            { ...input, dataDir, terminal, workspaceDir: "/workspace" },
+            {
+              createObservability,
+              readTelemetryEnabled: () => false,
+              installProcessGuards: () => () => undefined,
+              loadRuntimeLifecycle: async () => ({
+                createTuiRuntime: async () => ({ adapter: runtime }) as never,
+                shutdownTuiRuntime: async () => false,
+              }),
+              loadUpdateApplication: async () =>
+                ({ inspect: async () => ({ status: "up-to-date" }) }) as never,
+              createApp: (options) => {
+                app = createTuiApp({ ...options, productFeatures: { queue: false } });
+                return app;
+              },
+              writeExitMessage: () => undefined,
+            },
+          ),
+      });
+      try {
+        if (invalid) {
+          await running;
+          expect(processRef.exitCode).toBe(1);
+          expect(stderr.mock.calls.flat().join(" ")).toContain("Unknown provider or model");
+          expect(runtime.sendMessage).not.toHaveBeenCalled();
+        } else if (failedResume) {
+          await vi.waitFor(() => expect(app?.editor.disableSubmit).toBe(false));
+          expect(runtime.selectSessionModel).not.toHaveBeenCalled();
+          expect(runtime.createSession).not.toHaveBeenCalled();
+          expect(runtime.sendMessage).not.toHaveBeenCalled();
+        } else {
+          await vi.waitFor(() => {
+            expect(app?.editor.disableSubmit).toBe(false);
+            expect(app?.controller.snapshot().session?.model).toEqual(requested);
+            if (hasPrompt) expect(sentModels).toEqual([requested]);
+          });
+          expect(runtime.selectSessionModel).toHaveBeenCalledWith(requested, sessionId);
+          expect(app?.controller.snapshot().account?.modelSource).toBe("byok");
+          if (sessionId === "existing") expect(runtime.createSession).not.toHaveBeenCalled();
+          if (!hasPrompt) {
+            expect(runtime.sendMessage).not.toHaveBeenCalled();
+            await app?.submit("later prompt");
+            expect(sentModels).toEqual([requested]);
+          }
+          await vi.waitFor(() => expect(app?.controller.snapshot().status).toBe("idle"));
+          await app?.submit("/new");
+          expect(app?.controller.snapshot().session).toBeUndefined();
+          expect(runtime.selectSessionModel).toHaveBeenCalledOnce();
+        }
+        expect(runtime.selectModel).not.toHaveBeenCalled();
+      } finally {
+        await app?.stop();
+        await running;
+        await rm(dataDir, { recursive: true, force: true });
+      }
+    },
+  );
+});
+
+describe("interactive model argument contract", () => {
+  function program() {
+    const launch = vi.fn(async () => undefined);
+    const command = createTuiProgram({
+      version: "test",
+      launchTui: launch,
+      runExec: vi.fn(),
+      runLogin: vi.fn(),
+      runLogout: vi.fn(),
+      runUpdate: vi.fn(),
+    })
+      .exitOverride()
+      .configureOutput({ writeErr: () => undefined });
+    return { command, launch };
+  }
+
+  it.each(["-m", "--model"])("rejects missing values for %s", async (flag) => {
+    const { command, launch } = program();
+    await expect(command.parseAsync([flag], { from: "user" })).rejects.toThrow();
+    expect(launch).not.toHaveBeenCalled();
+  });
+
+  it.each(["", "bare-model", "provider/", "/model", "provider/model#"])(
+    "rejects malformed model %j before launch",
+    async (model) => {
+      const { command, launch } = program();
+      await expect(command.parseAsync(["--model", model], { from: "user" })).rejects.toThrow(
+        "--model must use provider/model",
+      );
+      expect(launch).not.toHaveBeenCalled();
+    },
+  );
+
+  it("rejects a model with an untargeted Session picker", async () => {
+    const { command, launch } = program();
+    await expect(
+      command.parseAsync(["--session", "--model", "provider/model"], { from: "user" }),
+    ).rejects.toThrow("requires a Session id");
+    expect(launch).not.toHaveBeenCalled();
+  });
+
+  it("retains the default launch contract without a model", async () => {
+    const { command, launch } = program();
+    await command.parseAsync(["hello"], { from: "user" });
+    expect(launch).toHaveBeenCalledWith({ initialPrompt: "hello" });
+  });
+
+  it.each(["-m", "--model"])("scans startup environment after %s values", (flag) => {
+    expect(
+      resolveTuiStartupEnvironmentOption([flag, "provider/model", "--env", "staging"], true),
+    ).toBe("staging");
+    expect(() =>
+      resolveTuiStartupEnvironmentOption([flag, "provider/model", "--env", "prod"], false),
+    ).toThrow("only available");
+    expect(
+      resolveTuiStartupEnvironmentOption(
+        [flag, "provider/model", "exec", "--env", "staging"],
+        true,
+      ),
+    ).toBeUndefined();
+  });
+});

+ 305 - 0
packages/tui/test/unit/tui-scrollbar-interaction.test.ts

@@ -0,0 +1,305 @@
+import { stripVTControlCharacters } from "node:util";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import {
+  ScrollView,
+  TuiAltScreen,
+  type Component,
+  type TuiMouseEvent,
+  visibleWidth,
+} from "../../src/tui/engine/public.js";
+import { TuiChatLayout } from "../../src/tui/shell/chat-layout.js";
+import { VirtualTerminal } from "../pi-084-upstream/virtual-terminal.js";
+
+const COLUMNS = 20;
+const ROWS = 10;
+const CONTENT_LINES = 60;
+const screens: TuiAltScreen[] = [];
+afterEach(() => {
+  for (const tui of screens.splice(0)) tui.stop();
+});
+
+class ScrollableLines implements Component {
+  private readonly lines: string[] = [];
+
+  constructor(count: number) {
+    for (let index = 0; index < count; index++) {
+      this.lines.push(`line-${String(index).padStart(2, "0")}`);
+    }
+  }
+
+  render(): string[] {
+    return [...this.lines];
+  }
+
+  invalidate(): void {}
+}
+
+function viewportText(terminal: VirtualTerminal): string {
+  return stripVTControlCharacters(terminal.getViewport().join("\n"));
+}
+
+function sgrPress(x: number, y: number): string {
+  return `\x1b[<0;${x + 1};${y + 1}M`;
+}
+
+function sgrMotion(x: number, y: number): string {
+  return `\x1b[<32;${x + 1};${y + 1}M`;
+}
+
+function sgrRelease(x: number, y: number): string {
+  return `\x1b[<0;${x + 1};${y + 1}m`;
+}
+
+async function createScrollbarScreen(
+  options: {
+    columns?: number;
+    gutter?: number;
+    content?: Component;
+    copySelection?: (text: string) => Promise<boolean>;
+  } = {},
+): Promise<{
+  terminal: VirtualTerminal;
+  tui: TuiAltScreen;
+  scrollView: ScrollView;
+}> {
+  const terminal = new VirtualTerminal(options.columns ?? COLUMNS, ROWS);
+  const content = options.content ?? new ScrollableLines(CONTENT_LINES);
+  const scrollView = new ScrollView(content, {
+    primary: true,
+    overscroll: "contain",
+    scrollbar: "always",
+    scrollbarGutter: options.gutter ?? 3,
+  });
+  const tui = new TuiAltScreen(terminal, false, undefined, {
+    copySelection: options.copySelection,
+  });
+  screens.push(tui);
+  tui.setLayoutRoot(scrollView);
+  tui.start();
+  await terminal.waitForRender();
+  return { terminal, tui, scrollView };
+}
+
+describe("Fullscreen scrollbar interaction", () => {
+  it("shows the top of the content before any interaction", async () => {
+    const { terminal } = await createScrollbarScreen();
+    expect(viewportText(terminal)).toContain("line-00");
+    expect(viewportText(terminal)).not.toContain("line-59");
+  });
+
+  it("jumps the thumb to a track press and keeps dragging from there", async () => {
+    const { terminal } = await createScrollbarScreen();
+    // Press the empty track at the bottom of the right-edge scrollbar.
+    terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+    expect(viewportText(terminal)).toContain("line-59");
+    expect(viewportText(terminal)).not.toContain("line-00");
+    terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+  });
+
+  it("grabs near-miss clicks within the widened hit span", async () => {
+    const { terminal } = await createScrollbarScreen();
+    // Press two columns left of the painted one-column bar.
+    terminal.sendInput(sgrPress(COLUMNS - 3, ROWS - 1));
+    await terminal.waitForRender();
+    expect(viewportText(terminal)).toContain("line-59");
+    expect(viewportText(terminal)).not.toContain("line-00");
+    terminal.sendInput(sgrRelease(COLUMNS - 3, ROWS - 1));
+    await terminal.waitForRender();
+  });
+
+  it("keeps an on-thumb press in place and drags proportionally", async () => {
+    const { terminal } = await createScrollbarScreen();
+    // Jump to the end first via a track press at the bottom, then release.
+    terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+    terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+    // The thumb now spans the bottom rows; press it directly.
+    terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 2));
+    await terminal.waitForRender();
+    // An on-thumb press must not move content on its own.
+    expect(viewportText(terminal)).toContain("line-59");
+    // Drag the thumb up the track.
+    terminal.sendInput(sgrMotion(COLUMNS - 1, Math.floor(ROWS / 2)));
+    await terminal.waitForRender();
+    const text = viewportText(terminal);
+    expect(text).not.toContain("line-59");
+    expect(text).not.toContain("line-00");
+    terminal.sendInput(sgrRelease(COLUMNS - 1, Math.floor(ROWS / 2)));
+    await terminal.waitForRender();
+  });
+
+  it("ignores presses outside the hit span", async () => {
+    const { terminal } = await createScrollbarScreen();
+    // Jump to the end via a track press, then release.
+    terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+    terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+    // Press three columns left of the bar, outside the hit span.
+    terminal.sendInput(sgrPress(COLUMNS - 4, 0));
+    await terminal.waitForRender();
+    terminal.sendInput(sgrRelease(COLUMNS - 4, 0));
+    await terminal.waitForRender();
+    expect(viewportText(terminal)).toContain("line-59");
+  });
+});
+
+describe("Scrollbar interaction boundaries", () => {
+  it.each([1, 3])(
+    "routes the last content columns with a %i-column gutter to content",
+    async (gutter) => {
+      const handleMouse = vi.fn((_event: TuiMouseEvent) => true);
+      const content = {
+        render: (width: number) =>
+          Array.from({ length: CONTENT_LINES }, () => "x".repeat(width)),
+        invalidate() {},
+        handleMouse,
+      };
+      const { terminal, scrollView } = await createScrollbarScreen({
+        gutter,
+        content,
+      });
+      for (const x of [COLUMNS - gutter - 2, COLUMNS - gutter - 1]) {
+        terminal.sendInput(sgrPress(x, ROWS - 1));
+        terminal.sendInput(sgrRelease(x, ROWS - 1));
+        await terminal.waitForRender();
+        expect(handleMouse).toHaveBeenCalledWith(
+          expect.objectContaining({ action: "press", x }),
+        );
+        expect(scrollView.scrollTop).toBe(0);
+      }
+    },
+  );
+
+  it.each([1, 3])(
+    "keeps text selection next to a %i-column gutter",
+    async (gutter) => {
+      const copySelection = vi.fn(async (_text: string) => true);
+      const content = {
+        render: (width: number) =>
+          Array.from(
+            { length: CONTENT_LINES },
+            () => "x".repeat(width - 2) + "YZ",
+          ),
+        invalidate() {},
+      };
+      const { terminal, scrollView } = await createScrollbarScreen({
+        gutter,
+        content,
+        copySelection,
+      });
+      terminal.sendInput(sgrPress(COLUMNS - gutter - 2, 4));
+      terminal.sendInput(sgrMotion(COLUMNS - gutter - 1, 4));
+      terminal.sendInput(sgrRelease(COLUMNS - gutter - 1, 4));
+      await terminal.waitForRender();
+      expect(copySelection).toHaveBeenCalledWith("YZ");
+      expect(scrollView.scrollTop).toBe(0);
+    },
+  );
+
+  it.each([1, 2, 3, 4, 8])(
+    "preserves content and bounded mouse targets at width %i",
+    async (columns) => {
+      const widths: number[] = [];
+      const handleMouse = vi.fn(() => true);
+      const content = {
+        render: (width: number) => {
+          widths.push(width);
+          return Array.from({ length: CONTENT_LINES }, () => "x".repeat(width));
+        },
+        invalidate() {},
+        handleMouse,
+      };
+      const { terminal, scrollView } = await createScrollbarScreen({
+        columns,
+        content,
+      });
+      expect(widths.every((width) => width === Math.max(1, columns - 3))).toBe(
+        true,
+      );
+      expect(
+        terminal.getViewport().every((line) => visibleWidth(line) <= columns),
+      ).toBe(true);
+      terminal.sendInput(sgrPress(0, ROWS - 1));
+      terminal.sendInput(sgrRelease(0, ROWS - 1));
+      await terminal.waitForRender();
+      expect(handleMouse).toHaveBeenCalled();
+      expect(scrollView.scrollTop).toBe(0);
+      if (columns > 1) {
+        terminal.sendInput(sgrPress(columns - 1, ROWS - 1));
+        await terminal.waitForRender();
+        expect(scrollView.scrollTop).toBe(CONTENT_LINES - ROWS);
+      }
+    },
+  );
+
+  it("does not capture a track press while an overlay is visible", async () => {
+    const { terminal, tui, scrollView } = await createScrollbarScreen();
+    tui.showOverlay(new ScrollableLines(2), { width: 10 });
+    await terminal.waitForRender();
+    terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
+    terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+    expect(scrollView.scrollTop).toBe(0);
+  });
+
+  it("cancels a drag when an overlay opens and does not resume it after dismissal", async () => {
+    const { terminal, tui, scrollView } = await createScrollbarScreen();
+    terminal.sendInput(sgrPress(COLUMNS - 1, 0));
+    const overlay = tui.showOverlay(new ScrollableLines(2), { width: 10 });
+    await terminal.waitForRender();
+    terminal.sendInput(sgrMotion(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+    expect(scrollView.scrollTop).toBe(0);
+    overlay.hide();
+    await terminal.waitForRender();
+    terminal.sendInput(sgrMotion(COLUMNS - 1, ROWS - 1));
+    terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+    expect(scrollView.scrollTop).toBe(0);
+  });
+
+  it("routes wheel events normally and ends dragging on release", async () => {
+    const { terminal, scrollView } = await createScrollbarScreen();
+    terminal.sendInput(`\x1b[<65;${COLUMNS};5M`);
+    await terminal.waitForRender();
+    expect(scrollView.scrollTop).toBeGreaterThan(0);
+    terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
+    terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
+    await terminal.waitForRender();
+    terminal.sendInput(sgrMotion(COLUMNS - 1, 0));
+    await terminal.waitForRender();
+    expect(scrollView.scrollTop).toBe(CONTENT_LINES - ROWS);
+  });
+
+  it("enables a reserved gutter in the actual fullscreen chat layout", async () => {
+    const terminal = new VirtualTerminal(40, 15);
+    const empty = { render: () => [], invalidate() {} };
+    const layout = new TuiChatLayout(terminal, {
+      surface: () => "conversation",
+      transcript: new ScrollableLines(CONTENT_LINES),
+      welcome: empty,
+      interaction: { ...empty, isActive: () => false },
+      activity: empty,
+      followUp: empty,
+      composer: { render: () => ["composer"], invalidate() {} },
+      status: empty,
+    });
+    const tui = new TuiAltScreen(terminal);
+    screens.push(tui);
+    tui.setLayoutRoot(layout.fullscreenLayoutRoot);
+    tui.start();
+    await terminal.waitForRender();
+    expect(viewportText(terminal)).toContain("line-59");
+    terminal.sendInput(sgrPress(37, 0));
+    terminal.sendInput(sgrRelease(37, 0));
+    await terminal.waitForRender();
+    expect(viewportText(terminal)).toContain("line-00");
+    expect(viewportText(terminal)).not.toContain("line-59");
+    expect(viewportText(terminal)).toContain("composer");
+  });
+});

+ 191 - 0
packages/tui/test/unit/tui-status-line-items.test.ts

@@ -1,5 +1,7 @@
 import { afterEach, describe, expect, it } from 'vitest';
 import { afterEach, describe, expect, it } from 'vitest';
 
 
+import { applyTuiRenderTheme, getTuiThemeSnapshot, tuiChalk, tuiColors } from '../../src/tui/theme/runtime.js';
+import { MINIMAX_CODE_DARK_THEME, MINIMAX_CODE_LIGHT_THEME } from '../../src/tui/theme/palettes.js';
 import { TuiStatusLine } from '../../src/tui/shell/chrome.js';
 import { TuiStatusLine } from '../../src/tui/shell/chrome.js';
 import { stripAnsi, visibleWidth } from '../../src/tui/rendering/text.js';
 import { stripAnsi, visibleWidth } from '../../src/tui/rendering/text.js';
 import { setCapabilityOverrides } from '../../src/tui/engine/public.js';
 import { setCapabilityOverrides } from '../../src/tui/engine/public.js';
@@ -582,3 +584,192 @@ describe('review link status item', () => {
     expect(narrow).toContain('!312');
     expect(narrow).toContain('!312');
   });
   });
 });
 });
+
+describe('context meter item', () => {
+  it('resolves the canonical id and its aliases', () => {
+    expect(parseTuiStatusLineItem('context-meter')).toBe('context-meter');
+    expect(parseTuiStatusLineItem('context-bar')).toBe('context-meter');
+    expect(parseTuiStatusLineItem('context-gauge')).toBe('context-meter');
+  });
+
+  it('stays out of the default status line', () => {
+    expect(TUI_STATUS_LINE_DEFAULT_ITEMS).toEqual([
+      'current-dir', 'session-title', 'git-branch', 'review-link', 'plan-mode',
+      'approval-mode', 'model-with-reasoning', 'context-window', 'subagent',
+      'token-quota', 'context-remaining',
+    ]);
+    const state = {
+      ...BASE_STATE,
+      contextUsage: { usedTokens: 20_000, contextWindowTokens: 100_000 },
+    };
+    expect(stripAnsi(render(state))).not.toContain('▕');
+  });
+
+  it('renders the remaining-headroom gauge when configured explicitly', () => {
+    const state = {
+      ...BASE_STATE,
+      statusLineItems: ['context-meter'],
+      contextUsage: { usedTokens: 20_000, contextWindowTokens: 100_000 },
+    };
+    expect(stripAnsi(render(state))).toContain('Context ▕██████░░▏ 80% left');
+  });
+
+  it('drains the gauge with the remaining headroom', () => {
+    const state = {
+      ...BASE_STATE,
+      statusLineItems: ['context-meter'],
+      contextUsage: { usedTokens: 50_000, contextWindowTokens: 100_000 },
+    };
+    expect(stripAnsi(render(state))).toContain('▕████░░░░▏ 50% left');
+  });
+
+  it('shrinks to a shorter gauge in narrow terminals', () => {
+    const state = {
+      ...BASE_STATE,
+      statusLineItems: ['context-meter'],
+      contextUsage: { usedTokens: 50_000, contextWindowTokens: 100_000 },
+    };
+    const narrow = stripAnsi(render(state, 20));
+    expect(narrow).toContain('Ctx');
+    expect(narrow).toContain('50%');
+    expect(narrow).not.toContain('left');
+    expect(narrow).not.toContain('▕████░░░░▏');
+  });
+
+  it('is hidden without usage and never shows NaN', () => {
+    expect(render({ ...BASE_STATE, statusLineItems: ['context-meter'], contextWindowTokens: 100_000 })).toBe('');
+    expect(
+      stripAnsi(
+        render({
+          ...BASE_STATE,
+          statusLineItems: ['context-meter'],
+          contextUsage: { usedTokens: Number.NaN, contextWindowTokens: 100_000 },
+        }),
+      ),
+    ).not.toContain('NaN');
+  });
+});
+
+describe('context meter presentation contract', () => {
+  it.each([
+    ['context-remaining', 'model', 'context-bar', 'context-gauge'],
+    ['context-gauge', 'model', 'context-left'],
+  ])('prefers the meter at its configured position for %j', (...configured) => {
+    const statusLineItems = parseTuiStatusLineItems(configured);
+    const line = stripAnsi(render({
+      ...BASE_STATE,
+      statusLineItems,
+      contextUsage: { usedTokens: 50_000, contextWindowTokens: 100_000 },
+    }));
+    expect(line.match(/50%/gu)).toHaveLength(1);
+    expect(line).toContain('Context ▕████░░░░▏ 50% left');
+    expect(line.indexOf('Context') < line.indexOf('m2')).toBe(configured[0] === 'context-gauge');
+  });
+});
+
+describe('context meter boundaries', () => {
+  it.each([
+    [undefined, 100_000, 50],
+    [200_000, 100_000, 75],
+    [Number.NaN, 100_000, 50],
+    [0, 100_000, 50],
+    [-1, 100_000, 50],
+    [Number.POSITIVE_INFINITY, 100_000, 50],
+  ])('resolves snapshot window %s before model window %s', (snapshot, model, remaining) => {
+    for (const item of ['context-meter', 'context-remaining'] as const) {
+      const line = stripAnsi(render({
+        ...BASE_STATE,
+        statusLineItems: [item],
+        contextWindowTokens: model,
+        contextUsage: { usedTokens: 50_000, contextWindowTokens: snapshot },
+      }));
+      expect(line).toContain(`${remaining}% left`);
+    }
+  });
+
+  it.each([undefined, 0, -1, Number.NaN, Number.POSITIVE_INFINITY])(
+    'hides the meter when neither window is usable (%s)', (window) => {
+      expect(render({
+        ...BASE_STATE,
+        statusLineItems: ['context-meter'],
+        contextWindowTokens: window,
+        contextUsage: { usedTokens: 1, contextWindowTokens: window },
+      })).toBe('');
+    },
+  );
+
+  it.each([Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY])(
+    'hides invalid usage %s even with valid model capacity', (usedTokens) => {
+      expect(render({
+        ...BASE_STATE,
+        statusLineItems: ['context-meter'],
+        contextWindowTokens: 100_000,
+        contextUsage: { usedTokens },
+      })).toBe('');
+    },
+  );
+
+  it.each([
+    [-1, '▕████████▏ 100% left'],
+    [0, '▕████████▏ 100% left'],
+    [100, '▕░░░░░░░░▏ 0% left'],
+    [150, '▕░░░░░░░░▏ 0% left'],
+  ])('clamps used tokens %d to a valid gauge', (usedTokens, expected) => {
+    expect(stripAnsi(render({
+      ...BASE_STATE,
+      statusLineItems: ['context-meter'],
+      contextUsage: { usedTokens, contextWindowTokens: 100 },
+    }))).toContain(expected);
+  });
+
+  it.each([
+    [30, 'Context ▕████░░░░▏ 50% left'],
+    [20, 'Ctx ▕███░░░▏ 50%'],
+    [7, 'Ctx 50%'],
+    [6, ''],
+  ])('fits a width of %d with the expected presentation', (width, expected) => {
+    const line = render({
+      ...BASE_STATE,
+      statusLineItems: ['context-meter'],
+      contextUsage: { usedTokens: 50, contextWindowTokens: 100 },
+    }, width);
+    expect(stripAnsi(line).trim()).toBe(expected);
+    expect(line.split('\n').every((row) => visibleWidth(row) <= width)).toBe(true);
+  });
+
+  it('uses the same warning and error boundaries as the percentage item', () => {
+    const previous = getTuiThemeSnapshot();
+    applyTuiRenderTheme(MINIMAX_CODE_DARK_THEME, 3);
+    try {
+      for (const [remaining, color] of [
+        [26, tuiColors.muted], [25, tuiColors.warning],
+        [11, tuiColors.warning], [10, tuiColors.error], [0, tuiColors.error],
+      ] as const) {
+        for (const item of ['context-meter', 'context-remaining'] as const) {
+          const line = render({
+            ...BASE_STATE,
+            statusLineItems: [item],
+            contextUsage: { usedTokens: 100 - remaining, contextWindowTokens: 100 },
+          }).trim();
+          expect(line).not.toBe(stripAnsi(line));
+          expect(line).toBe(tuiChalk.hex(color)(stripAnsi(line)));
+        }
+      }
+    } finally {
+      applyTuiRenderTheme(
+        previous.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME,
+        previous.colorLevel,
+      );
+    }
+  });
+
+  it('keeps build-mode in control when the meter is selected', () => {
+    const line = stripAnsi(render({
+      ...BASE_STATE,
+      statusLineItems: ['context-meter', 'build-mode', 'context-remaining'],
+      contextUsage: { usedTokens: 50, contextWindowTokens: 100 },
+    }));
+    expect(line).toContain('[V]');
+    expect(line).not.toContain('Context');
+  });
+});

+ 29 - 0
packages/tui/test/unit/tui-workspace-status-line.test.ts

@@ -508,3 +508,32 @@ describe('TuiWorkspaceStatusLine custom status command', () => {
     expect(runProcess).toHaveBeenCalledTimes(1);
     expect(runProcess).toHaveBeenCalledTimes(1);
   });
   });
 });
 });
+
+
+describe('context meter projection', () => {
+  it('previews and refreshes current usage without retaining cleared data', () => {
+    const runtime = { getWorkspaceGitMetadata: vi.fn() };
+    const state = {
+      version: '0.1', workspace: '/repo', runtimeStatus: 'ready' as const,
+      statusLineItems: ['context-meter', 'context-remaining'] as const,
+      contextWindowTokens: 100,
+    };
+    const status = new TuiWorkspaceStatusLine(state, runtime, vi.fn());
+    try {
+      expect(status.preview(['context-meter'], 80, 3).unavailable).toEqual(['context-meter']);
+      expect(status.render(80)).toEqual([]);
+      status.setState({ ...state, contextUsage: { usedTokens: 20 } });
+      const preview = status.preview(['context-meter', 'context-remaining'], 80, 3);
+      expect(preview.unavailable).toEqual([]);
+      expect(preview.lines.join('')).toContain('▕██████░░▏ 80% left');
+      expect(status.render(80).join('').match(/80%/gu)).toHaveLength(1);
+      status.setState({ ...state, contextUsage: { usedTokens: 90 } });
+      expect(status.render(80).join('')).toContain('▕█░░░░░░░▏ 10% left');
+      status.setState(state);
+      expect(status.render(80)).toEqual([]);
+      expect(runtime.getWorkspaceGitMetadata).not.toHaveBeenCalled();
+    } finally {
+      status.dispose();
+    }
+  });
+});

+ 14 - 1
packages/tui/test/unit/tui/controller/product/command-flow.test.ts

@@ -37,6 +37,7 @@ function createReadinessCommandFlow(options: {
   };
   };
   persistTuiMode?: (mode: "regular" | "fullscreen") => void;
   persistTuiMode?: (mode: "regular" | "fullscreen") => void;
   reloadTui?: () => Promise<void>;
   reloadTui?: () => Promise<void>;
+  append?: (text: string, kind?: "info" | "warning" | "error") => void;
   queuedCount?: number;
   queuedCount?: number;
 }) {
 }) {
   return new TuiCommandFlow({
   return new TuiCommandFlow({
@@ -86,7 +87,7 @@ function createReadinessCommandFlow(options: {
     ...(options.whenControllerReady
     ...(options.whenControllerReady
       ? { whenControllerReady: options.whenControllerReady }
       ? { whenControllerReady: options.whenControllerReady }
       : {}),
       : {}),
-    append: vi.fn(),
+    append: options.append ?? vi.fn(),
     setHint: options.setHint ?? vi.fn(),
     setHint: options.setHint ?? vi.fn(),
     onChanged: vi.fn(),
     onChanged: vi.fn(),
   });
   });
@@ -347,6 +348,18 @@ describe("TuiCommandFlow", () => {
     expect(setHint).toHaveBeenLastCalledWith("TUI configuration reloaded.");
     expect(setHint).toHaveBeenLastCalledWith("TUI configuration reloaded.");
   });
   });
 
 
+  it.each(['Plugin refresh failed', 'Invalid keybindings.json'])("preserves /reload cause: %s", async (message) => {
+    const append = vi.fn();
+    const flow = createReadinessCommandFlow({
+      whenReady: async () => {}, append,
+      reloadTui: async () => { throw new Error(message); },
+    });
+    await expect(flow.submit('/reload')).resolves.toBe('consumed');
+    expect(append).toHaveBeenCalledWith(expect.stringContaining(message), 'warning');
+    expect(append).toHaveBeenCalledWith(expect.stringContaining('configuration or plugin refresh error'), 'warning');
+    expect(JSON.stringify(append.mock.calls)).not.toContain('Fix keybindings.json');
+  });
+
   it("retains /reload while queued work is present", async () => {
   it("retains /reload while queued work is present", async () => {
     const reloadTui = vi.fn(async () => undefined);
     const reloadTui = vi.fn(async () => undefined);
     const setHint = vi.fn();
     const setHint = vi.fn();

+ 17 - 0
packages/tui/test/unit/tui/features/settings/status-line-picker.test.ts

@@ -145,3 +145,20 @@ describe('status line picker', () => {
     expect(picker.render(120).join('\n')).not.toContain('0%');
     expect(picker.render(120).join('\n')).not.toContain('0%');
   });
   });
 });
 });
+
+
+describe('context meter settings', () => {
+  it.each([
+    ['en', 'Remaining context gauge'],
+    ['zh-Hans', '剩余上下文刻度条'],
+  ])('describes and saves the opt-in item in %s', async (locale, description) => {
+    const { picker, options } = fixture({ locale });
+    picker.handleInput('context-meter');
+    expect(picker.render(100).join('\n')).toContain(description);
+    expect(picker.render(100).join('\n')).toContain('[ ] context-meter');
+    picker.handleInput(' ');
+    picker.handleInput('\r');
+    await vi.waitFor(() => expect(options.onClose).toHaveBeenCalledOnce());
+    expect(options.save).toHaveBeenCalledWith(['model', 'current-dir', 'context-meter']);
+  });
+});

+ 2 - 0
release/public-source.json

@@ -2235,6 +2235,7 @@
     "packages/local-runtime/src/content-safety/api-v2.ts",
     "packages/local-runtime/src/content-safety/api-v2.ts",
     "packages/local-runtime/src/content-safety/api.ts",
     "packages/local-runtime/src/content-safety/api.ts",
     "packages/local-runtime/src/content-safety/config-fields.ts",
     "packages/local-runtime/src/content-safety/config-fields.ts",
+    "packages/local-runtime/src/content-safety/diagnostics.ts",
     "packages/local-runtime/src/content-safety/turn-api.ts",
     "packages/local-runtime/src/content-safety/turn-api.ts",
     "packages/local-runtime/src/context/context-usage-calibration.ts",
     "packages/local-runtime/src/context/context-usage-calibration.ts",
     "packages/local-runtime/src/context/context-usage-provider-diagnostics.ts",
     "packages/local-runtime/src/context/context-usage-provider-diagnostics.ts",
@@ -3243,6 +3244,7 @@
     "packages/tui/test/unit/tui-provider-manager.test.ts",
     "packages/tui/test/unit/tui-provider-manager.test.ts",
     "packages/tui/test/unit/tui-provider-onboarding.test.ts",
     "packages/tui/test/unit/tui-provider-onboarding.test.ts",
     "packages/tui/test/unit/tui-runtime-adapter.test.ts",
     "packages/tui/test/unit/tui-runtime-adapter.test.ts",
+    "packages/tui/test/unit/tui-scrollbar-interaction.test.ts",
     "packages/tui/test/unit/tui-session-manager.test.ts",
     "packages/tui/test/unit/tui-session-manager.test.ts",
     "packages/tui/test/unit/tui-shell-block.test.ts",
     "packages/tui/test/unit/tui-shell-block.test.ts",
     "packages/tui/test/unit/tui-status-line-items.test.ts",
     "packages/tui/test/unit/tui-status-line-items.test.ts",

+ 45 - 0
test/byok.test.mjs

@@ -298,6 +298,7 @@ test(
       "provider", "add", "--name", "Limited", "--base-url", baseUrl,
       "provider", "add", "--name", "Limited", "--base-url", baseUrl,
       "--api-format", "openai-completions", "--model", "fixture-model",
       "--api-format", "openai-completions", "--model", "fixture-model",
       "--model", "second-model", "--context-limit", "32768", "--output-limit", "4096", "--use",
       "--model", "second-model", "--context-limit", "32768", "--output-limit", "4096", "--use",
+      "--support-image",
     ];
     ];
     const beforeFailure = readFileSync(configPath, "utf8");
     const beforeFailure = readFileSync(configPath, "utf8");
     rejectConnection = true;
     rejectConnection = true;
@@ -312,6 +313,7 @@ test(
     assert.equal(config.defaultModel, "custom_provider:limited/fixture-model");
     assert.equal(config.defaultModel, "custom_provider:limited/fixture-model");
     for (const model of ["fixture-model", "second-model"]) {
     for (const model of ["fixture-model", "second-model"]) {
       assert.deepEqual(config.custom_provider.limited.models[model].limit, { context: 32768, output: 4096 });
       assert.deepEqual(config.custom_provider.limited.models[model].limit, { context: 32768, output: 4096 });
+      assert.deepEqual(config.custom_provider.limited.models[model].capabilities, { support_image: true });
     }
     }
     const configured = JSON.parse(await run(["provider", "list", "--json"])).providers.find(
     const configured = JSON.parse(await run(["provider", "list", "--json"])).providers.find(
       (provider) => provider.name === "Limited",
       (provider) => provider.name === "Limited",
@@ -330,8 +332,10 @@ test(
       await run([
       await run([
         "provider", "add", "--name", name, "--base-url", baseUrl,
         "provider", "add", "--name", name, "--base-url", baseUrl,
         "--api-format", "openai-completions", "--model", "fixture-model", flag, "32768",
         "--api-format", "openai-completions", "--model", "fixture-model", flag, "32768",
+        "--support-image",
       ]);
       ]);
       assert.deepEqual(savedConfig().custom_provider[name].models["fixture-model"].limit, limit);
       assert.deepEqual(savedConfig().custom_provider[name].models["fixture-model"].limit, limit);
+      assert.deepEqual(savedConfig().custom_provider[name].models["fixture-model"].capabilities, { support_image: true });
       assert.equal(savedConfig().defaultModel, config.defaultModel);
       assert.equal(savedConfig().defaultModel, config.defaultModel);
     }
     }
     assert.equal(requests.length, beforeSaveOnly, "Limits alone must not test or select a model");
     assert.equal(requests.length, beforeSaveOnly, "Limits alone must not test or select a model");
@@ -342,7 +346,48 @@ test(
       reasoning: true,
       reasoning: true,
       thinking: { effortOptions: ["low", "high", "max"] },
       thinking: { effortOptions: ["low", "high", "max"] },
     };
     };
+    effortConfig.custom_provider.fixture.models["vision-modalities"] = {
+      modalities: { input: ["text", "image"], output: ["text"] },
+    };
+    effortConfig.custom_provider.fixture.models["attachment-only"] = { attachment: true };
     writeFileSync(configPath, stringifyYaml(effortConfig));
     writeFileSync(configPath, stringifyYaml(effortConfig));
+    // The packaged command must carry the declaration through saving, a fresh
+    // process/config reload, attachment preparation and SDK serialization.
+    const imageBase64 = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGUlEQVR4nGP4n8LwnxLMMGrAqAGjBgwXAwCOqWIfmQV0zAAAAABJRU5ErkJggg==";
+    const imagePath = path.join(workspaceDir, "fixture.png");
+    writeFileSync(imagePath, Buffer.from(imageBase64, "base64"));
+    for (const [model, supportsImage] of [
+      ["custom_provider:limited/fixture-model", true],
+      ["custom_provider:context-only/fixture-model", true],
+      ["custom_provider:fixture/vision-modalities", true],
+      ["custom_provider:fixture/fixture-model", false],
+      ["custom_provider:fixture/attachment-only", false],
+    ]) {
+      const beforeImage = requests.length;
+      await run(["exec", "IMAGE_INPUT_TEST", "--model", model, "--file", imagePath,
+        "--timeout", "20s", "--max-steps", "1"]);
+      const turns = requests.slice(beforeImage).filter((r) => r.body.stream === true);
+      assert.ok(turns.length > 0, `Expected a runtime request for ${model}`);
+      for (const { body } of turns) {
+        const images = body.messages.flatMap((message) =>
+          Array.isArray(message.content)
+            ? message.content.filter((part) => part.type === "image_url")
+            : []);
+        if (supportsImage) {
+          assert.equal(images.length, 1, `Expected an inline image for ${model}`);
+          // PNG inputs are transcoded to JPEG by the real attachment pipeline.
+          const url = images[0].image_url.url;
+          assert.match(url, /^data:image\/jpeg;base64,/);
+          const bytes = Buffer.from(url.split(",")[1], "base64");
+          assert.deepEqual([...bytes.subarray(0, 3)], [0xff, 0xd8, 0xff]);
+          assert.deepEqual([...bytes.subarray(-2)], [0xff, 0xd9]);
+          assert.doesNotMatch(JSON.stringify(body.messages), /current model cannot read this image inline/);
+        } else {
+          assert.deepEqual(images, [], `${model} must remain text-only`);
+          assert.match(JSON.stringify(body.messages), /current model cannot read this image inline/);
+        }
+      }
+    }
     const effortModel = `${selected.providerId}/kimi-k3`;
     const effortModel = `${selected.providerId}/kimi-k3`;
     for (const effort of ["low", "high", "max"]) {
     for (const effort of ["low", "high", "max"]) {
       const beforeEffort = requests.length;
       const beforeEffort = requests.length;

+ 1 - 0
test/vitest-suites.json

@@ -77,6 +77,7 @@
       "packages/tui/test/unit/tui-chat-controller.test.ts",
       "packages/tui/test/unit/tui-chat-controller.test.ts",
       "packages/tui/test/unit/tui-duration.test.ts",
       "packages/tui/test/unit/tui-duration.test.ts",
       "packages/tui/test/unit/tui-engine-local-deltas.test.ts",
       "packages/tui/test/unit/tui-engine-local-deltas.test.ts",
+      "packages/tui/test/unit/tui-scrollbar-interaction.test.ts",
       "packages/tui/test/unit/tui-image-preview.test.ts",
       "packages/tui/test/unit/tui-image-preview.test.ts",
       "packages/tui/test/unit/tui-session-manager.test.ts",
       "packages/tui/test/unit/tui-session-manager.test.ts",
       "packages/tui/test/unit/tui-shell-block.test.ts",
       "packages/tui/test/unit/tui-shell-block.test.ts",