Skip to content
Draft
5 changes: 5 additions & 0 deletions .changeset/fix-zai-api-line-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kilocode/core-schemas": patch
---

Fix Z.ai provider schema to support all API endpoints (international_api and china_api)
5 changes: 5 additions & 0 deletions .changeset/zai-logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kilo-code": patch
---

Add comprehensive request/response logging to Z.ai provider handler for verification and debugging. Logs now capture endpoint configuration, authentication details, request parameters (model, tokens, thinking mode), and response characteristics including reasoning content. Includes 6 new integration tests verifying logging behavior across all API endpoints (international_coding, china_coding, international_api, china_api).
5 changes: 4 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ webview-ui/src/i18n/locales/** linguist-generated=true
# Then explicitly mark English directories as NOT generated (override the above)
src/i18n/locales/en/** linguist-generated=false
webview-ui/src/i18n/locales/en/** linguist-generated=false
# This approach uses gitattributes' last-match-wins rule to exclude English while including all other locales
# This approach uses gitattributes' last-match-wins rule to exclude English while including all other locales

# Use bd merge for beads JSONL files
.beads/issues.jsonl merge=beads
49 changes: 49 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ pnpm lint # Run ESLint
pnpm check-types # TypeScript type checking
```

## Issue Tracking

This project uses **bd (beads)** for issue tracking.
Run `bd prime` for workflow context, or install hooks (`bd hooks install`) for auto-injection.

**Quick reference:**
- `bd ready` - Find unblocked work
- `bd create "Title" --type task --priority 2` - Create issue
- `bd close <id>` - Complete work
- `bd sync` - Sync with git (run at session end)

For full workflow details: `bd prime`

## GitHub Workflow

- Use the GitHub CLI (`gh`) for interacting with GitHub (issues, PRs, reviews, releases) instead of the web UI when practical.
- Examples: `gh issue list`, `gh issue view <id>`, `gh pr create`, `gh pr checkout <id>`, `gh pr review`.
- Sign any GitHub comments you leave (issues/PRs/reviews) with your full name.
- Example (end of comment): `— Full Name`
- Sign your commits with your full name.
- Ensure your Git author name is your full name (not a handle): `git config user.name "Full Name"`.
- Include a sign-off line in commits: `git commit -s ...` (adds `Signed-off-by: Full Name`).

## Skills

- **Translation**: `.kilocode/skills/translation/SKILL.md` - Translation and localization guidelines
Expand Down Expand Up @@ -225,3 +248,29 @@ Keep changes to core extension code minimal to reduce merge conflicts during ups
- Use Tailwind CSS classes instead of inline style objects for new markup
- VSCode CSS variables must be added to webview-ui/src/index.css before using them in Tailwind classes
- Example: `<div className="text-md text-vscode-descriptionForeground mb-2" />` instead of style objects

## Landing the Plane (Session Completion)

**When ending a work session**, you MUST complete ALL steps below. Work is NOT complete until `git push` succeeds.

**MANDATORY WORKFLOW:**

1. **File issues for remaining work** - Create issues for anything that needs follow-up
2. **Run quality gates** (if code changed) - Tests, linters, builds
3. **Update issue status** - Close finished work, update in-progress items
4. **PUSH TO REMOTE** - This is MANDATORY:
```bash
git pull --rebase
bd sync
git push
git status # MUST show "up to date with origin"
```
5. **Clean up** - Clear stashes, prune remote branches
6. **Verify** - All changes committed AND pushed
7. **Hand off** - Provide context for next session

**CRITICAL RULES:**
- Work is NOT complete until `git push` succeeds
- NEVER stop before pushing - that leaves work stranded locally
- NEVER say "ready to push when you are" - YOU must push
- If push fails, resolve and retry until it succeeds
103 changes: 103 additions & 0 deletions PR_ATTEMPT_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# PR Attempt Summary: ZAI Provider Not Working (kc-9sq)

## Issue

CLI status bar was not updating after user ran `/provider select` to switch providers (e.g., from xAI to ZAI). The status bar would continue displaying the old provider's model name instead of the new provider's selected model.

## Root Cause Analysis

When user switches providers via `/provider select`:

1. CLI sends `upsertApiConfiguration` message to extension
2. Extension detects the `apiProvider` has changed
3. Handler calls `flushModels()` to clear cache and refresh models for new provider
4. Handler calls `postStateToWebview()` to send updated state to CLI
5. CLI receives state and updates `apiConfigurationAtom` and `routerModelsAtom`
6. StatusBar component re-renders, calling `getModelDisplayName()` which uses `routerModels` to look up display names

## Attempted Fixes

### Attempt 1: Detect Provider Change and Flush Models

**Files modified:**

- `src/core/webview/webviewMessageHandler.ts` (lines 2199-2245)
- `src/core/webview/ClineProvider.ts` (lines 2250-2267)

**Approach:**

- Added detection of `apiProvider` changes (not just `kilocodeOrganizationId`)
- When provider changes, call `flushModels({provider: newProvider}, refresh=true)` to clear cache
- Modified `getStateToPostToWebview()` to fetch fresh models from the new provider
- Include `routerModels` in the returned ExtensionState object (line 2471)

**Test Coverage:**

- Added integration test: `cli/src/__tests__/provider-selection-status-update.integration.test.ts`
- Tests verify `apiConfigurationAtom` updates when state changes
- All 2102 CLI tests pass
- All 233 webview tests pass

### Attempt 2: Dual Message Approach

**Files modified:**

- `src/core/webview/webviewMessageHandler.ts` (lines 2269-2286)

**Approach:**

- After calling `postStateToWebview()`, also send explicit `routerModels` message with fresh models
- This provides two paths for models to reach the CLI:
1. Via state object in `postStateToWebview()`
2. Via separate explicit `routerModels` message

**Problem:** Race condition could occur where the explicit message arrives before the state update, causing the state update to overwrite fresh models with stale ones.

## Why It Didn't Work

The fix was implemented correctly at the logic level, but the issue persists in practice. Possible causes requiring further investigation:

1. **Cache Still Being Hit**: Even after `flushModels(..., refresh=true)`, when `getModels()` is called in `getStateToPostToWebview()`, it checks the cache first (line 201 of modelCache.ts). If models exist in cache for the new provider, they're returned immediately without verification they're actually fresh.

2. **Provider State Not Synchronized**: When `upsertProviderProfile()` updates the global state via `contextProxy.setProviderSettings()` (line 1526 of ClineProvider.ts), there might be a timing issue where `getStateToPostToWebview()` (which calls `getState()`) reads the old provider before the state is fully persisted.

3. **CLI Atom Preservation Logic**: The CLI's `updateExtensionStateAtom` (line 254 of cli/src/state/atoms/extension.ts) preserves old `routerModels` if new state doesn't include them: `set(routerModelsAtom, state.routerModels || currentRouterModels)`. Even with models in state, if the lookup logic in StatusBar is faulty, this wouldn't help.

4. **StatusBar Lookup Logic**: The `getModelDisplayName()` function in StatusBar.tsx uses `getCurrentModelId()` and `getModelsByProvider()` to find model display names. If the provider isn't correctly mapped or models aren't properly indexed, the lookup fails.

## Code Quality

- All tests pass (2102 CLI + 233 webview)
- TypeScript type checking passes
- ESLint passes
- No empty catch blocks
- Proper error handling with try-catch and logging
- Changes marked with `// kilocode_change` comments for upstream merge tracking

## Branch Information

Branch: `fix/zai-provider-not-working` (created from main)

Commits include:

- ZAI provider schema fixes
- ZAI comprehensive request/response logging
- CLI provider configuration tests
- Provider selection status bar update logic
- Task summary documentation

## Recommendation

This appears to be a complex data flow issue involving:

1. Extension-side model fetching and caching logic
2. State serialization and transmission to CLI
3. CLI-side atom updates and preservation logic
4. StatusBar component's model lookup and display logic

The fix attempts address multiple layers of this flow, but without being able to reproduce the issue in a controlled environment or add extensive logging, it's difficult to pinpoint which layer is failing. Suggested next steps:

1. Add debug logging to trace exact model state through each layer
2. Check if `flushModels()` and `refreshModels()` are working correctly
3. Verify `contextProxy.setProviderSettings()` updates are persisted before `getStateToPostToWebview()` reads them
4. Add logging to `getModelDisplayName()` to see what models/providers it's actually receiving
Loading