Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
72.9 KB
Raw Blame
package agent

import (
	"context"
	"encoding/json"
	"fmt"
	"log/slog"
	"os"
	"path/filepath"
	"reflect"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/multica-ai/multica/server/pkg/taskfailure"
)

func TestKimiToolNameFromTitle(t *testing.T) {
	t.Parallel()
	tests := []struct {
		title string
		want  string
	}{
		{"Read file: /tmp/foo.go", "read_file"},
		{"read", "read_file"},
		{"Write: /tmp/bar.go", "write_file"},
		{"Edit", "edit_file"},
		{"Patch: /tmp/x", "edit_file"},
		{"Shell: ls -la", "terminal"},
		{"Bash", "terminal"},
		{"Run command: pwd", "terminal"},
		{"Search: foo", "search_files"},
		{"Glob: *.go", "glob"},
		{"Web search: golang acp", "web_search"},
		{"Fetch: https://example.com", "web_fetch"},
		{"Todo Write", "todo_write"},
		// Fallback: snake_case the title.
		{"Custom Thing", "custom_thing"},
		// Empty input returns empty — caller decides how to react.
		{"", ""},
	}
	for _, tt := range tests {
		got := kimiToolNameFromTitle(tt.title)
		if got != tt.want {
			t.Errorf("kimiToolNameFromTitle(%q) = %q, want %q", tt.title, got, tt.want)
		}
	}
}

// fakeKimiACPScript returns a POSIX-sh script that impersonates
// `kimi acp` for a single short ACP session: it acks initialize /
// session/new and then replies to session/set_model with a JSON-RPC
// error — the scenario the kimiBackend must propagate as a failed
// task rather than silently falling back to the default model.
func fakeKimiACPScript() string {
	return `#!/bin/sh
# Fake ` + "`kimi`" + ` binary — used by TestKimiBackendSetModelFailureFailsTask
# and TestKimiBackendPassesYoloFlag.
#
# Writes the full argv (one arg per line) to $KIMI_ARGS_FILE if that env
# var is set, so tests can assert that the daemon invokes us with the
# right flags (` + "`--yolo acp`" + `, not bare ` + "`acp`" + `).
#
# Then reads one JSON-RPC request per line from stdin, matches on the
# method name, and writes back a canned response. Exits after set_model
# so the kimiBackend cleanup path can run.
if [ -n "$KIMI_ARGS_FILE" ]; then
  for arg in "$@"; do
    printf '%s\n' "$arg" >> "$KIMI_ARGS_FILE"
  done
fi
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/new"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_fake"}}\n' "$id"
      ;;
    *'"method":"session/set_model"'*)
      printf '{"jsonrpc":"2.0","id":%s,"error":{"code":-32602,"message":"model not available: bogus-model"}}\n' "$id"
      exit 0
      ;;
  esac
done
`
}

// TestKimiBackendSetModelFailureFailsTask pins the "don't silently
// fall back" behaviour that landed in this PR: when kimi rejects the
// caller-selected model via session/set_model, the task result must
// report status=failed with a message that names the model and the
// upstream error — not claim success while actually running on the
// default model.
func TestKimiBackendSetModelFailureFailsTask(t *testing.T) {
	t.Parallel()

	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPScript()))

	backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
		Model:   "bogus-model",
		Timeout: 5 * time.Second,
	})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	// Drain message stream so the lifecycle goroutine can progress.
	go func() {
		for range session.Messages {
		}
	}()

	select {
	case result, ok := <-session.Result:
		if !ok {
			t.Fatal("result channel closed without a value")
		}
		if result.Status != "failed" {
			t.Fatalf("expected status=failed, got %q (error=%q)", result.Status, result.Error)
		}
		if !strings.Contains(result.Error, `could not switch to model "bogus-model"`) {
			t.Errorf("expected error to name the requested model, got %q", result.Error)
		}
		if !strings.Contains(result.Error, "model not available") {
			t.Errorf("expected error to surface upstream message, got %q", result.Error)
		}
		// A fresh session that never reached session/prompt must NOT be
		// published as a resume pointer: the runtime may never have persisted
		// it, and a pointer to a session that does not exist wedges the whole
		// conversation forever (GH #8116). There is no transcript behind an id
		// that never ran a prompt, so nothing is lost by withholding it.
		if result.SessionID != "" {
			t.Errorf("expected the never-prompted session id to be withheld, got %q", result.SessionID)
		}
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
	}
}

func fakeKimiACPThinkingScript() string {
	return `#!/bin/sh
while IFS= read -r line; do
  if [ -n "$KIMI_REQUESTS_FILE" ]; then
    printf '%s\n' "$line" >> "$KIMI_REQUESTS_FILE"
  fi
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{"loadSession":true}}}\n' "$id"
      ;;
    *'"method":"session/new"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_thinking"}}\n' "$id"
      ;;
    *'"method":"session/resume"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_thinking"}}\n' "$id"
      ;;
    *'"method":"session/set_model"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{}}\n' "$id"
      ;;
    *'"method":"session/set_config_option"'*)
      if [ "$KIMI_SET_CONFIG_ERROR" = "unsupported" ]; then
        printf '{"jsonrpc":"2.0","id":%s,"error":{"code":-32602,"message":"Invalid params","data":{"value":"Unsupported thinking level"}}}\n' "$id"
        continue
      fi
      requested=$(printf '%s' "$line" | sed -n 's/.*"value":"\([^"]*\)".*/\1/p')
      if [ "$KIMI_SET_CONFIG_RESULT" = "mismatch" ]; then
        printf '{"jsonrpc":"2.0","id":%s,"result":{"configOptions":[{"id":"thinking","currentValue":"low"}]}}\n' "$id"
      elif [ "$KIMI_SET_CONFIG_RESULT" = "missing" ]; then
        printf '{"jsonrpc":"2.0","id":%s,"result":{"configOptions":[]}}\n' "$id"
      else
        printf '{"jsonrpc":"2.0","id":%s,"result":{"configOptions":[{"id":"thinking","currentValue":"%s"}]}}\n' "$id" "$requested"
      fi
      ;;
    *'"method":"session/prompt"'*)
      printf '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"ses_thinking","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"pong"}}}}\n'
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id"
      exit 0
      ;;
  esac
done
`
}

func runKimiThinkingTest(t *testing.T, fakePath string, env map[string]string, opts ExecOptions) Result {
	t.Helper()
	backend, err := New("kimi", Config{
		ExecutablePath: fakePath,
		Logger:         slog.Default(),
		Env:            env,
	})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}
	if opts.Timeout == 0 {
		opts.Timeout = 5 * time.Second
	}
	session, err := backend.Execute(context.Background(), "reply with pong", opts)
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()
	select {
	case result := <-session.Result:
		return result
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
		return Result{}
	}
}

func TestKimiBackendSetsThinkingLevelBeforePrompt(t *testing.T) {
	t.Parallel()
	recordPath := filepath.Join(t.TempDir(), "requests.jsonl")
	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPThinkingScript()))

	result := runKimiThinkingTest(t, fakePath, map[string]string{
		"KIMI_REQUESTS_FILE": recordPath,
	}, ExecOptions{Model: "kimi-code/k3", ThinkingLevel: "high"})
	if result.Status != "completed" {
		t.Fatalf("status = %q, error = %q", result.Status, result.Error)
	}

	raw, err := os.ReadFile(recordPath)
	if err != nil {
		t.Fatalf("read recorded requests: %v", err)
	}
	var methods []string
	var thinkingParams map[string]any
	for _, line := range strings.Split(strings.TrimSpace(string(raw)), "\n") {
		var frame map[string]any
		if err := json.Unmarshal([]byte(line), &frame); err != nil {
			t.Fatalf("decode recorded request: %v", err)
		}
		method, _ := frame["method"].(string)
		methods = append(methods, method)
		if method == "session/set_config_option" {
			thinkingParams, _ = frame["params"].(map[string]any)
		}
	}
	wantOrder := []string{"initialize", "session/new", "session/set_model", "session/set_config_option", "session/prompt"}
	if !reflect.DeepEqual(methods, wantOrder) {
		t.Fatalf("ACP method order = %v, want %v", methods, wantOrder)
	}
	if thinkingParams["sessionId"] != "ses_thinking" ||
		thinkingParams["configId"] != "thinking" ||
		thinkingParams["value"] != "high" {
		t.Errorf("set_config_option params = %+v", thinkingParams)
	}
}

func TestKimiBackendThinkingFailureWarnsAndContinues(t *testing.T) {
	t.Parallel()
	recordPath := filepath.Join(t.TempDir(), "requests.jsonl")
	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPThinkingScript()))

	result := runKimiThinkingTest(t, fakePath, map[string]string{
		"KIMI_REQUESTS_FILE":    recordPath,
		"KIMI_SET_CONFIG_ERROR": "unsupported",
	}, ExecOptions{Model: "kimi-code/k3", ThinkingLevel: "max"})
	if result.Status != "completed" {
		t.Fatalf("status = %q, error = %q", result.Status, result.Error)
	}
	raw, err := os.ReadFile(recordPath)
	if err != nil {
		t.Fatalf("read recorded requests: %v", err)
	}
	if !strings.Contains(string(raw), `"method":"session/prompt"`) {
		t.Fatal("prompt was not sent after thinking configuration failed")
	}
}

func TestKimiBackendThinkingConfirmationFailureWarnsAndContinues(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name   string
		result string
	}{
		{name: "mismatch", result: "mismatch"},
		{name: "missing", result: "missing"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			recordPath := filepath.Join(t.TempDir(), "requests.jsonl")
			fakePath := filepath.Join(t.TempDir(), "kimi")
			writeTestExecutable(t, fakePath, []byte(fakeKimiACPThinkingScript()))

			result := runKimiThinkingTest(t, fakePath, map[string]string{
				"KIMI_REQUESTS_FILE":     recordPath,
				"KIMI_SET_CONFIG_RESULT": tt.result,
			}, ExecOptions{Model: "kimi-code/k3", ThinkingLevel: "max"})
			if result.Status != "completed" {
				t.Fatalf("status = %q, error = %q", result.Status, result.Error)
			}
			if result.ResumeRejected {
				t.Fatal("confirmation failure must not be classified as a rejected resume")
			}
			raw, err := os.ReadFile(recordPath)
			if err != nil {
				t.Fatalf("read recorded requests: %v", err)
			}
			if !strings.Contains(string(raw), `"method":"session/prompt"`) {
				t.Fatal("prompt was not sent after thinking confirmation failed")
			}
		})
	}
}

func TestKimiBackendOmitsThinkingConfigWhenUnset(t *testing.T) {
	t.Parallel()
	recordPath := filepath.Join(t.TempDir(), "requests.jsonl")
	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPThinkingScript()))

	result := runKimiThinkingTest(t, fakePath, map[string]string{
		"KIMI_REQUESTS_FILE": recordPath,
	}, ExecOptions{Model: "kimi-code/k3"})
	if result.Status != "completed" {
		t.Fatalf("status = %q, error = %q", result.Status, result.Error)
	}
	raw, err := os.ReadFile(recordPath)
	if err != nil {
		t.Fatalf("read recorded requests: %v", err)
	}
	if strings.Contains(string(raw), `"method":"session/set_config_option"`) {
		t.Fatal("thinking config was sent for an empty override")
	}
}

// recordedKimiMethods returns the ACP method names the fake binary saw, in
// order, from the file $KIMI_REQUESTS_FILE points at.
func recordedKimiMethods(t *testing.T, recordPath string) []string {
	t.Helper()
	raw, err := os.ReadFile(recordPath)
	if err != nil {
		t.Fatalf("read recorded requests: %v", err)
	}
	var methods []string
	for _, line := range strings.Split(strings.TrimSpace(string(raw)), "\n") {
		var frame map[string]any
		if err := json.Unmarshal([]byte(line), &frame); err != nil {
			t.Fatalf("decode recorded request: %v", err)
		}
		method, _ := frame["method"].(string)
		methods = append(methods, method)
	}
	return methods
}

// TestKimiBackendSetsThinkingLevelOnResumedSession covers the half of the
// contract the fresh-session tests miss: a resumed session already carries
// whatever thinking level its previous turn left behind, so the configured
// level has to be re-applied — still before session/prompt, and without a
// session/set_model call to piggyback on.
func TestKimiBackendSetsThinkingLevelOnResumedSession(t *testing.T) {
	t.Parallel()
	recordPath := filepath.Join(t.TempDir(), "requests.jsonl")
	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPThinkingScript()))

	result := runKimiThinkingTest(t, fakePath, map[string]string{
		"KIMI_REQUESTS_FILE": recordPath,
	}, ExecOptions{ResumeSessionID: "ses_thinking", ThinkingLevel: "high"})
	if result.Status != "completed" {
		t.Fatalf("status = %q, error = %q", result.Status, result.Error)
	}

	wantOrder := []string{"initialize", "session/resume", "session/set_config_option", "session/prompt"}
	if methods := recordedKimiMethods(t, recordPath); !reflect.DeepEqual(methods, wantOrder) {
		t.Fatalf("ACP method order = %v, want %v", methods, wantOrder)
	}
}

// TestKimiBackendOmitsThinkingConfigOnResumedSessionWhenUnset states the known
// gap deliberately rather than leaving it undefined: with no configured level,
// the backend sends nothing, so a resumed session keeps the level its previous
// turn set. Switching an agent back to "Follow CLI config" therefore does not
// clear a level already applied to a live session. Changing that means
// deciding what "follow the CLI config" resolves to on the wire, which is
// tracked separately; until then this test is what makes the behaviour visible.
func TestKimiBackendOmitsThinkingConfigOnResumedSessionWhenUnset(t *testing.T) {
	t.Parallel()
	recordPath := filepath.Join(t.TempDir(), "requests.jsonl")
	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPThinkingScript()))

	result := runKimiThinkingTest(t, fakePath, map[string]string{
		"KIMI_REQUESTS_FILE": recordPath,
	}, ExecOptions{ResumeSessionID: "ses_thinking"})
	if result.Status != "completed" {
		t.Fatalf("status = %q, error = %q", result.Status, result.Error)
	}

	wantOrder := []string{"initialize", "session/resume", "session/prompt"}
	if methods := recordedKimiMethods(t, recordPath); !reflect.DeepEqual(methods, wantOrder) {
		t.Fatalf("ACP method order = %v, want %v", methods, wantOrder)
	}
}

// fakeKimiACPStaleResumeSetModelScript impersonates kimi-cli when a
// resumed session is gone and the caller picked a model:
// session/resume echoes the requested sessionId back, then
// session/set_model rejects the unknown session the way kimi-cli
// actually does — RequestError.invalid_params (-32602) with
// {"session_id": "Session not found"} in data
// (src/kimi_cli/acp/server.py, set_session_model).
// fakeKimiACPPromptScript is a fake `kimi` binary that completes a full
// ACP turn: initialize, session/new, session/prompt with a streamed
// message chunk, then the prompt response. When $KIMI_LATE_CHUNK is set
// it emits one more chunk shortly AFTER the session/prompt response, the
// way a real ACP agent can, so tests can prove the backend drains
// trailing notifications instead of cutting the process off at the
// response boundary.
func fakeKimiACPPromptScript() string {
	return `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/new"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_fake"}}\n' "$id"
      ;;
    *'"method":"session/prompt"'*)
      printf '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"ses_fake","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"pong"}}}}\n'
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id"
      if [ -n "$KIMI_LATE_CHUNK" ]; then
        sleep 0.05
        printf '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"ses_fake","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":" tail"}}}}\n'
      fi
      exit 0
      ;;
  esac
done
`
}

func fakeKimiACPStaleResumeSetModelScript() string {
	return `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/resume"'*)
      sid=$(printf '%s' "$line" | sed -n 's/.*"sessionId":"\([^"]*\)".*/\1/p')
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"%s"}}\n' "$id" "$sid"
      ;;
    *'"method":"session/set_model"'*)
      printf '{"jsonrpc":"2.0","id":%s,"error":{"code":-32602,"message":"Invalid params","data":{"session_id":"Session not found"}}}\n' "$id"
      exit 0
      ;;
  esac
done
`
}

// TestKimiBackendClearsSessionIDWhenSetModelSessionNotFound pins the
// set_model sibling of the resumed-session fix: with a model override,
// session/set_model runs before session/prompt, so a dead resumed
// session surfaces there. The Result must carry an empty SessionID so
// the daemon's fresh-session retry (gated on SessionID == "") fires.
func TestKimiBackendClearsSessionIDWhenSetModelSessionNotFound(t *testing.T) {
	t.Parallel()

	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPStaleResumeSetModelScript()))

	backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
		Timeout:         5 * time.Second,
		ResumeSessionID: "ses_stale",
		Model:           "kimi-for-coding",
	})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()

	select {
	case result, ok := <-session.Result:
		if !ok {
			t.Fatal("result channel closed without a value")
		}
		if result.Status != "failed" {
			t.Fatalf("expected status=failed, got %q (error=%q)", result.Status, result.Error)
		}
		if !strings.Contains(result.Error, `could not switch to model "kimi-for-coding"`) {
			t.Errorf("expected error to name the requested model, got %q", result.Error)
		}
		if result.SessionID != "" {
			t.Errorf("expected empty session id so the daemon's fresh-session retry fires, got %q", result.SessionID)
		}
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
	}
}

// TestKimiBackendInvokesACPSubcommand pins the argv for `kimi`. An
// earlier fix tried passing `--yolo` to bypass per-tool approval
// prompts, but the `acp` subcommand in kimi-cli takes no options
// (see cli/__init__.py @cli.command def acp()), so `--yolo` was a
// no-op and the daemon still hung for 5 min on the first Shell call.
// The actual bypass is in hermesClient.handleAgentRequest, which
// auto-approves session/request_permission. This test catches
// accidental re-introduction of the dead flag.
func TestKimiBackendInvokesACPSubcommand(t *testing.T) {
	t.Parallel()

	tempDir := t.TempDir()
	argsFile := filepath.Join(tempDir, "argv.txt")
	fakePath := filepath.Join(tempDir, "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPScript()))

	backend, err := New("kimi", Config{
		ExecutablePath: fakePath,
		Logger:         slog.Default(),
		Env:            map[string]string{"KIMI_ARGS_FILE": argsFile},
	})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	// Set Model so the fake binary exits on set_model and we don't
	// have to wait for the prompt branch. We only care about argv here.
	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
		Model:   "bogus-model",
		Timeout: 5 * time.Second,
	})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()
	<-session.Result

	raw, err := os.ReadFile(argsFile)
	if err != nil {
		t.Fatalf("read args file: %v", err)
	}
	lines := strings.Split(strings.TrimSpace(string(raw)), "\n")
	if len(lines) < 1 {
		t.Fatalf("expected at least 1 arg (acp), got %d: %q", len(lines), lines)
	}
	if lines[0] != "acp" {
		t.Errorf("expected first arg to be acp, got %q (full: %q)", lines[0], lines)
	}
	for _, l := range lines {
		switch l {
		case "--yolo", "--auto-approve", "--yes", "-y":
			t.Errorf("kimi acp doesn't accept %q; auto-approval is handled in hermesClient.handleAgentRequest", l)
		}
	}
}

// TestKimiResumeIncludesMcpServers pins the same contract as the matching
// Hermes test: session/resume must carry the managed MCP set so a resumed
// Kimi task has the same MCP tools as a fresh one.
func TestKimiResumeIncludesMcpServers(t *testing.T) {
	t.Parallel()

	recordPath := filepath.Join(t.TempDir(), "frames.jsonl")
	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeACPRecordingScript(recordPath, "ses_resume", `{}`)))

	backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
		Timeout:         5 * time.Second,
		ResumeSessionID: "ses_resume",
		McpConfig:       json.RawMessage(`{"mcpServers":{"fetch":{"command":"uvx"}}}`),
	})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()
	select {
	case <-session.Result:
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
	}

	frame := findRecordedFrame(t, recordPath, "session/resume")
	params := frame["params"].(map[string]any)
	servers, ok := params["mcpServers"].([]any)
	if !ok {
		t.Fatalf("session/resume.mcpServers: got %T, want []any", params["mcpServers"])
	}
	if len(servers) != 1 || servers[0].(map[string]any)["name"] != "fetch" {
		t.Fatalf("session/resume.mcpServers: got %v, want one entry named fetch", servers)
	}
}

// TestKimiFreshSessionIncludesMcpServers is the session/new counterpart to the
// resume test above: a fresh Kimi task must carry the managed MCP set too.
// Kimi takes MCP over ACP rather than as a CLI flag, so a bare `kimi acp`
// launch line is not evidence that the servers were dropped (MUL-5846) — this
// pins the payload that actually carries them.
func TestKimiFreshSessionIncludesMcpServers(t *testing.T) {
	t.Parallel()

	recordPath := filepath.Join(t.TempDir(), "frames.jsonl")
	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeACPRecordingScript(recordPath, "ses_new", `{}`)))

	backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
		Timeout:   5 * time.Second,
		McpConfig: json.RawMessage(`{"mcpServers":{"fetch":{"command":"uvx","args":["mcp-server-fetch"]}}}`),
	})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()
	select {
	case <-session.Result:
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
	}

	initFrame := findRecordedFrame(t, recordPath, "initialize")
	initParams := initFrame["params"].(map[string]any)
	capabilities := initParams["clientCapabilities"].(map[string]any)
	if capabilities["terminal"] != true {
		t.Fatalf("initialize.clientCapabilities.terminal = %#v, want true", capabilities["terminal"])
	}

	frame := findRecordedFrame(t, recordPath, "session/new")
	params := frame["params"].(map[string]any)
	servers, ok := params["mcpServers"].([]any)
	if !ok {
		t.Fatalf("session/new.mcpServers: got %T, want []any", params["mcpServers"])
	}
	if len(servers) != 1 {
		t.Fatalf("session/new.mcpServers: got %d entries, want 1", len(servers))
	}
	entry := servers[0].(map[string]any)
	if entry["name"] != "fetch" || entry["command"] != "uvx" {
		t.Fatalf("session/new.mcpServers[0]: got %v, want {name:fetch,command:uvx,...}", entry)
	}
}

// fakeKimiPoisonedHistoryScript mimics the kimi CLI when asked to resume a
// session whose conversation history contains an empty assistant message. The
// adapter emits the "provider.api_error: 400 … role 'assistant' must not be
// empty" line to stderr and returns a completed (end_turn) JSON-RPC response —
// matching the real kimi adapter behaviour that prompted MUL-5154.
func fakeKimiPoisonedHistoryScript() string {
	return `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/resume"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_poison"}}\n' "$id"
      ;;
    *'"method":"session/prompt"'*)
      printf '%s\n' "error: failed to run prompt: provider.api_error: 400 the message at position 43 with role 'assistant' must not be empty" >&2
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id"
      exit 0
      ;;
  esac
done
`
}

// TestKimiBackendPoisonedHistorySetsResumeRejected verifies that when the kimi
// backend receives the "assistant must not be empty" 400 error while resuming
// a session, it sets ResumeRejected=true so the daemon clears the broken
// session pointer and starts fresh on the next task.
func TestKimiBackendPoisonedHistorySetsResumeRejected(t *testing.T) {
	t.Parallel()

	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiPoisonedHistoryScript()))

	backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
		Timeout:         5 * time.Second,
		ResumeSessionID: "ses_poison",
	})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()

	select {
	case result, ok := <-session.Result:
		if !ok {
			t.Fatal("result channel closed without a value")
		}
		if result.Status != "failed" {
			t.Fatalf("expected status=failed for poisoned history, got %q", result.Status)
		}
		if !result.ResumeRejected {
			t.Error("expected ResumeRejected=true so the daemon clears the broken session")
		}
		if !strings.Contains(result.Error, "must not be empty") {
			t.Errorf("expected error to mention the poisoned-history detail, got %q", result.Error)
		}
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
	}
}

// TestKimiBackendPoisonedHistoryFreshRunNoResumeRejected verifies that when
// the same poisoned-history error occurs on a FRESH run (ResumeSessionID==""),
// ResumeRejected must stay false. A fresh run cannot inherit a broken history,
// so flagging it would incorrectly prevent the next task from resuming a good
// session.
func TestKimiBackendPoisonedHistoryFreshRunNoResumeRejected(t *testing.T) {
	t.Parallel()

	fakePath := filepath.Join(t.TempDir(), "kimi")
	script := `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/new"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_fresh"}}\n' "$id"
      ;;
    *'"method":"session/prompt"'*)
      printf '%s\n' "error: failed to run prompt: provider.api_error: 400 the message at position 1 with role 'assistant' must not be empty" >&2
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id"
      exit 0
      ;;
  esac
done
`
	writeTestExecutable(t, fakePath, []byte(script))

	backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
		Timeout: 5 * time.Second,
	})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()
	select {
	case result, ok := <-session.Result:
		if !ok {
			t.Fatal("result channel closed without a value")
		}
		if result.Status != "failed" {
			t.Fatalf("expected status=failed, got %q (error=%q)", result.Status, result.Error)
		}
		if result.ResumeRejected {
			t.Error("ResumeRejected must be false on a fresh run (no ResumeSessionID)")
		}
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
	}
}

// TestKimiBackendPoisonedHistoryTwoLineFormat verifies that the two-line kimi
// stderr format (status on one line, detail on the next) is correctly detected
// as a poisoned session. Both isPoisonedHistory and classifyPoisonedError must
// fire even when "provider.api_error: 400" and "must not be empty" arrive on
// separate lines.
func TestKimiBackendPoisonedHistoryTwoLineFormat(t *testing.T) {
	t.Parallel()

	fakePath := filepath.Join(t.TempDir(), "kimi")
	script := `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/resume"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_poison_2line"}}\n' "$id"
      ;;
    *'"method":"session/prompt"'*)
      printf '%s\n' "error: failed to run prompt: provider.api_error: 400" >&2
      printf '%s\n' "detail: messages[43].content: content must not be empty" >&2
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id"
      exit 0
      ;;
  esac
done
`
	writeTestExecutable(t, fakePath, []byte(script))

	backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
		Timeout:         5 * time.Second,
		ResumeSessionID: "ses_poison_2line",
	})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()

	select {
	case result, ok := <-session.Result:
		if !ok {
			t.Fatal("result channel closed without a value")
		}
		if result.Status != "failed" {
			t.Fatalf("two-line: expected status=failed, got %q (error=%q)", result.Status, result.Error)
		}
		if !result.ResumeRejected {
			t.Error("two-line: expected ResumeRejected=true — isPoisonedHistory must detect across stderr lines")
		}
		if !strings.Contains(result.Error, "provider.api_error: 400") {
			t.Errorf("two-line: expected error to contain provider.api_error: 400, got %q", result.Error)
		}
		if !strings.Contains(result.Error, "must not be empty") {
			t.Errorf("two-line: expected error to contain must not be empty, got %q", result.Error)
		}
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
	}
}

// TestKimiPoisonedHistorySurfacedErrorIsUnresumable pins the cross-package
// contract this PR exists to satisfy: the Result.Error string the kimi sniffer
// surfaces must be recognized by taskfailure.UnresumableHistory, the daemon's
// backend-agnostic predicate (#6083) that retires the session so a later issue
// mention or chat cannot resume the poisoned transcript. isPoisonedHistory now
// delegates to that same predicate, but the daemon classifies the surfaced
// string independently of ResumeRejected — via shouldRetryWithFreshSession and
// GetLastTaskSession / GetLastChatTaskSession — so the string itself must carry
// the emptiness complaint and the message locator through the sniffer's
// extraction, for both Kimi stderr shapes. A regression that only checked
// ResumeRejected would not catch a messageLocked change that dropped the
// locator and silently broke the daemon-side exclusion.
func TestKimiPoisonedHistorySurfacedErrorIsUnresumable(t *testing.T) {
	t.Parallel()

	cases := []struct {
		name       string
		script     string
		resumeID   string
		wantLocate string // a token the surfaced error must retain
	}{
		{
			name:       "single-line",
			script:     fakeKimiPoisonedHistoryScript(),
			resumeID:   "ses_poison",
			wantLocate: "message at position",
		},
		{
			name: "two-line",
			script: `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/resume"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_poison_2line"}}\n' "$id"
      ;;
    *'"method":"session/prompt"'*)
      printf '%s\n' "error: failed to run prompt: provider.api_error: 400" >&2
      printf '%s\n' "detail: messages[43].content: content must not be empty" >&2
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id"
      exit 0
      ;;
  esac
done
`,
			resumeID:   "ses_poison_2line",
			wantLocate: "messages[43",
		},
	}

	for _, tc := range cases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			fakePath := filepath.Join(t.TempDir(), "kimi")
			writeTestExecutable(t, fakePath, []byte(tc.script))

			backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
			if err != nil {
				t.Fatalf("new kimi backend: %v", err)
			}

			ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
			defer cancel()

			session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
				Timeout:         5 * time.Second,
				ResumeSessionID: tc.resumeID,
			})
			if err != nil {
				t.Fatalf("execute: %v", err)
			}
			go func() {
				for range session.Messages {
				}
			}()

			select {
			case result, ok := <-session.Result:
				if !ok {
					t.Fatal("result channel closed without a value")
				}
				if !strings.Contains(result.Error, tc.wantLocate) {
					t.Errorf("surfaced error dropped the history locator %q: %q", tc.wantLocate, result.Error)
				}
				if !taskfailure.UnresumableHistory(result.Error) {
					t.Errorf("taskfailure.UnresumableHistory(%q) = false; the daemon would not retire the poisoned session", result.Error)
				}
			case <-time.After(10 * time.Second):
				t.Fatal("timeout waiting for result")
			}
		})
	}
}

// TestKimiDrainsNotificationsAfterPromptResponse pins the trailing-notification
// drain. kimi ACP can emit a final session update just after the
// session/prompt response returns; closing stdin and cancelling the context at
// that boundary raced the stdout reader and silently truncated the last chunk.
// The same defect was fixed for the sibling ACP backends in #5440 (grok) and
// #5675 (hermes).
func TestKimiDrainsNotificationsAfterPromptResponse(t *testing.T) {
	t.Parallel()

	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPPromptScript()))

	backend, err := New("kimi", Config{
		ExecutablePath: fakePath,
		Logger:         slog.Default(),
		Env:            map[string]string{"KIMI_LATE_CHUNK": "1"},
	})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	session, err := backend.Execute(context.Background(), "task", ExecOptions{Timeout: 5 * time.Second})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()

	result := <-session.Result
	if result.Status != "completed" {
		t.Fatalf("expected completed, got status=%q error=%q", result.Status, result.Error)
	}
	if !strings.Contains(result.Output, "pong tail") {
		t.Fatalf("late output was truncated: %q", result.Output)
	}
}

// fakeKimiACPResumeHistoryReplayScript impersonates kimi acp during session
// resume: after session/resume responds it emits a history-replay notification
// (the way a real Kimi CLI replays prior turns when loadSession:true). Then on
// session/prompt it emits a new chunk with the actual answer. The gate must
// ensure the history chunk never reaches output.
func fakeKimiACPResumeHistoryReplayScript() string {
	return `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{"loadSession":true}}}\n' "$id"
      ;;
    *'"method":"session/resume"'*)
      # Emit history replay BEFORE responding — this is what real Kimi does.
      printf '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"ses_resumed","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"old history from previous turn"}}}}\n'
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_resumed"}}\n' "$id"
      ;;
    *'"method":"session/prompt"'*)
      printf '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"ses_resumed","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"fresh answer"}}}}\n'
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id"
      exit 0
      ;;
  esac
done
`
}

// TestKimiBackendDropsHistoryReplayOnResume pins the streaming gate: when a
// Kimi session is resumed, the runtime may replay prior-turn transcripts as
// ACP notifications before the client sends session/prompt. Without the
// streamingCurrentTurn gate those replayed messages would be appended to the
// message channel and contaminate Result.Output with the previous answer.
func TestKimiBackendDropsHistoryReplayOnResume(t *testing.T) {
	t.Parallel()

	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPResumeHistoryReplayScript()))

	backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	session, err := backend.Execute(ctx, "do something new", ExecOptions{
		Timeout:         5 * time.Second,
		ResumeSessionID: "ses_resumed",
	})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}

	var messages []Message
	done := make(chan struct{})
	go func() {
		defer close(done)
		for m := range session.Messages {
			messages = append(messages, m)
		}
	}()

	result := <-session.Result
	<-done

	if result.Status != "completed" {
		t.Fatalf("expected completed, got status=%q error=%q", result.Status, result.Error)
	}
	if result.SessionID != "ses_resumed" {
		t.Errorf("expected session id ses_resumed, got %q", result.SessionID)
	}
	// The gate must block the history replay chunk.
	if strings.Contains(result.Output, "old history") {
		t.Fatalf("history replay leaked into Result.Output: %q", result.Output)
	}
	// The current-turn answer must pass through.
	if !strings.Contains(result.Output, "fresh answer") {
		t.Fatalf("expected current-turn output to contain 'fresh answer', got %q", result.Output)
	}
	// Also verify the message channel didn't receive the history chunk.
	for _, m := range messages {
		if m.Type == MessageText && strings.Contains(m.Content, "old history") {
			t.Fatalf("history replay leaked into message stream: %+v", m)
		}
	}
}

// kimiScanTotal folds a per-model scan result into one total, for assertions
// that do not care about model attribution.
func kimiScanTotal(scan kimiUsageScan) TokenUsage {
	var total TokenUsage
	for _, u := range scanKimiSessionUsage(scan) {
		total.InputTokens += u.InputTokens
		total.OutputTokens += u.OutputTokens
		total.CacheReadTokens += u.CacheReadTokens
		total.CacheWriteTokens += u.CacheWriteTokens
	}
	return total
}

// kimiScanSingle asserts the scan produced exactly one model bucket and
// returns it with its model name.
func kimiScanSingle(tb testing.TB, scan kimiUsageScan) (TokenUsage, string) {
	tb.Helper()
	byModel := scanKimiSessionUsage(scan)
	if len(byModel) != 1 {
		tb.Fatalf("expected exactly one model bucket, got %v", byModel)
	}
	for model, u := range byModel {
		return u, model
	}
	return TokenUsage{}, ""
}

// kimiWireRecordLine is a verbatim `usage.record` line as kimi-code 0.33.0
// writes it, captured from a real session.
func kimiWireRecordLine(inputOther, output, cacheRead, cacheCreate int64) string {
	return kimiWireRecordLineAt(time.Now(), inputOther, output, cacheRead, cacheCreate)
}

// kimiWireRecordLineAt stamps the record, so resume tests can place records on
// either side of a turn boundary.
func kimiWireRecordLineAt(at time.Time, inputOther, output, cacheRead, cacheCreate int64) string {
	return kimiWireRecordLineFor("moonshot-cn/kimi-k2.7-code", at, inputOther, output, cacheRead, cacheCreate)
}

// kimiWireRecordLineFor names the model on the record, the way kimi does.
func kimiWireRecordLineFor(model string, at time.Time, inputOther, output, cacheRead, cacheCreate int64) string {
	return fmt.Sprintf(`{"type":"usage.record","model":%q,"usage":{"inputOther":%d,"output":%d,"inputCacheRead":%d,"inputCacheCreation":%d},"usageScope":"turn","time":%d}`,
		model, inputOther, output, cacheRead, cacheCreate, at.UnixMilli())
}

// kimiWireStepEndLine is the `step.end` loop event kimi writes alongside the
// usage record, repeating the same counters. Summing both double-bills.
func kimiWireStepEndLine(inputOther, output, cacheRead, cacheCreate int64) string {
	return fmt.Sprintf(`{"type":"context.append_loop_event","event":{"type":"step.end","uuid":"a3799b31","turnId":"0","step":1,"finishReason":"end_turn","usage":{"inputOther":%d,"output":%d,"inputCacheRead":%d,"inputCacheCreation":%d}},"time":1785997224849}`,
		inputOther, output, cacheRead, cacheCreate)
}

// writeKimiWireLog lays out a wire log at the path kimi uses:
// <root>/sessions/<workspace>/<sessionID>/agents/<agent>/wire.jsonl
func writeKimiWireLog(tb testing.TB, kimiHome, sessionID, agentName string, lines ...string) string {
	tb.Helper()
	dir := filepath.Join(kimiHome, "sessions", "wd_test_abc123", sessionID, "agents", agentName)
	if err := os.MkdirAll(dir, 0o755); err != nil {
		tb.Fatalf("mkdir wire log dir: %v", err)
	}
	path := filepath.Join(dir, "wire.jsonl")
	body := strings.Join(lines, "\n") + "\n"
	if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
		tb.Fatalf("write wire log: %v", err)
	}
	return path
}

// TestScanKimiSessionUsageSumsRecordsIgnoringStepEnd is the core guard for
// MUL-5773: kimi logs each LLM call's usage twice — once as `usage.record`
// and once inside the `step.end` loop event. Counting both doubles every
// number on the dashboard.
func TestScanKimiSessionUsageSumsRecordsIgnoringStepEnd(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	// A real two-step turn: the second call's uncached input collapses as
	// the prefix moves into cache.
	writeKimiWireLog(t, home, "session_abc", "main",
		`{"type":"metadata","protocol_version":"1"}`,
		kimiWireRecordLine(4883, 200, 17664, 0),
		kimiWireStepEndLine(4883, 200, 17664, 0),
		kimiWireRecordLine(244, 8, 22528, 0),
	)

	usage, model := kimiScanSingle(t, kimiUsageScan{startTime: time.Now().Add(-time.Minute), kimiHome: home, sessionID: "session_abc", fallbackModel: "unknown"})

	if usage.InputTokens != 5127 {
		t.Errorf("InputTokens = %d, want 5127 (4883+244)", usage.InputTokens)
	}
	if usage.OutputTokens != 208 {
		t.Errorf("OutputTokens = %d, want 208 (200+8)", usage.OutputTokens)
	}
	if usage.CacheReadTokens != 40192 {
		t.Errorf("CacheReadTokens = %d, want 40192 (17664+22528)", usage.CacheReadTokens)
	}
	if usage.CacheWriteTokens != 0 {
		t.Errorf("CacheWriteTokens = %d, want 0", usage.CacheWriteTokens)
	}
	if model != "moonshot-cn/kimi-k2.7-code" {
		t.Errorf("model = %q, want moonshot-cn/kimi-k2.7-code", model)
	}
}

// TestScanKimiSessionUsageMapsCacheCreation pins inputCacheCreation onto the
// cache-write bucket, the half of #6448 that stayed unfixed.
func TestScanKimiSessionUsageMapsCacheCreation(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	writeKimiWireLog(t, home, "session_w", "main", kimiWireRecordLine(100, 20, 300, 400))

	usage := kimiScanTotal(kimiUsageScan{startTime: time.Now().Add(-time.Minute), kimiHome: home, sessionID: "session_w", fallbackModel: "unknown"})
	if usage.CacheWriteTokens != 400 {
		t.Errorf("CacheWriteTokens = %d, want 400", usage.CacheWriteTokens)
	}
	if usage.CacheReadTokens != 300 {
		t.Errorf("CacheReadTokens = %d, want 300", usage.CacheReadTokens)
	}
}

// TestScanKimiSessionUsageBindsToSessionID: a concurrent kimi session must
// never be billed to this task.
func TestScanKimiSessionUsageBindsToSessionID(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	writeKimiWireLog(t, home, "session_mine", "main", kimiWireRecordLine(10, 1, 0, 0))
	writeKimiWireLog(t, home, "session_theirs", "main", kimiWireRecordLine(9999, 9999, 9999, 9999))

	usage := kimiScanTotal(kimiUsageScan{startTime: time.Now().Add(-time.Minute), kimiHome: home, sessionID: "session_mine", fallbackModel: "unknown"})
	if usage.InputTokens != 10 || usage.OutputTokens != 1 {
		t.Fatalf("billed the wrong session: %+v", usage)
	}
}

// TestScanKimiSessionUsageCountsSubagents: kimi's Agent tool gives each
// subagent its own wire log; delegated work must not vanish from the bill.
func TestScanKimiSessionUsageCountsSubagents(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	writeKimiWireLog(t, home, "session_s", "main", kimiWireRecordLine(100, 10, 0, 0))
	writeKimiWireLog(t, home, "session_s", "subagent-1", kimiWireRecordLine(50, 5, 0, 0))

	usage := kimiScanTotal(kimiUsageScan{startTime: time.Now().Add(-time.Minute), kimiHome: home, sessionID: "session_s", fallbackModel: "unknown"})
	if usage.InputTokens != 150 || usage.OutputTokens != 15 {
		t.Fatalf("subagent usage not counted: %+v", usage)
	}
}

// TestScanKimiSessionUsageSkipsLogsUntouchedSinceStart: on a resumed session
// the previous run's log is still on disk and was already billed.
func TestScanKimiSessionUsageSkipsLogsUntouchedSinceStart(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	path := writeKimiWireLog(t, home, "session_old", "main", kimiWireRecordLine(500, 50, 0, 0))
	stale := time.Now().Add(-2 * time.Hour)
	if err := os.Chtimes(path, stale, stale); err != nil {
		t.Fatalf("chtimes: %v", err)
	}

	usage := kimiScanTotal(kimiUsageScan{startTime: time.Now().Add(-time.Minute), kimiHome: home, sessionID: "session_old", resumed: true, fallbackModel: "unknown"})
	if acpTokenUsagePresent(usage) {
		t.Fatalf("re-billed a log from before this turn: %+v", usage)
	}
}

// TestScanKimiSessionUsageSkipsMalformedLines: the log is appended to live, so
// the tail can be a partial write. Partial data beats no data.
func TestScanKimiSessionUsageSkipsMalformedLines(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	writeKimiWireLog(t, home, "session_m", "main",
		kimiWireRecordLine(100, 10, 200, 0),
		`{"type":"usage.record","model":"x","usage":{"inputOther":7`,
	)

	usage := kimiScanTotal(kimiUsageScan{startTime: time.Now().Add(-time.Minute), kimiHome: home, sessionID: "session_m", fallbackModel: "unknown"})
	if usage.InputTokens != 100 || usage.CacheReadTokens != 200 {
		t.Fatalf("complete records dropped because of a truncated tail: %+v", usage)
	}
}

// fakeKimiACPNoUsageScript reproduces kimi-code 0.33.0 exactly: the
// session/prompt result carries only stopReason — no `usage` — while the real
// counters go to the session wire log.
func fakeKimiACPNoUsageScript() string {
	return `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/new"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"session_e2e"}}\n' "$id"
      ;;
    *'"method":"session/prompt"'*)
      dir="${KIMI_CODE_HOME:-$HOME/.kimi-code}/sessions/wd_test_abc123/session_e2e/agents/main"
      mkdir -p "$dir"
      # The scan drops records stamped before the turn began, so the test
      # injects the timestamp. ` + "`date +%s`" + ` would truncate to the start of
      # the current second and land before a sub-second startTime.
      printf '{"type":"usage.record","model":"moonshot-cn/kimi-k2.7-code","usage":{"inputOther":7694,"output":21,"inputCacheRead":14848,"inputCacheCreation":0},"usageScope":"turn","time":%s}\n' "$KIMI_TEST_RECORD_TIME" >> "$dir/wire.jsonl"
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id"
      exit 0
      ;;
  esac
done
`
}

func fakeKimiACPPromptUsageScript() string {
	return `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/new"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"session_prompt_usage"}}\n' "$id"
      ;;
    *'"method":"session/prompt"'*)
      printf '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"session_prompt_usage","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"done"}}}}\n'
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn","usage":{"inputTokens":17,"outputTokens":5,"cachedReadTokens":3,"cachedWriteTokens":2,"costUsdTicks":900}}}\n' "$id"
      exit 0
      ;;
  esac
done
`
}

func TestKimiBackendPropagatesACPPromptUsage(t *testing.T) {
	t.Parallel()

	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPPromptUsageScript()))

	backend, err := New("kimi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}
	session, err := backend.Execute(context.Background(), "task", ExecOptions{Timeout: 5 * time.Second})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()

	result := <-session.Result
	if result.Status != "completed" {
		t.Fatalf("status=%q error=%q", result.Status, result.Error)
	}
	want := TokenUsage{InputTokens: 17, OutputTokens: 5, CacheReadTokens: 3, CacheWriteTokens: 2, CostUSDTicks: 900}
	if usage := result.Usage["unknown"]; usage != want {
		t.Fatalf("usage = %+v, want %+v", usage, want)
	}
}

// TestKimiBackendReportsUsageFromWireLog is the end-to-end proof for
// MUL-5773 / #6448: with an ACP peer that reports no usage at all — which is
// what kimi-code 0.33.0 actually does — the task still lands on the dashboard
// with its real token split, keyed by the model the wire log names.
func TestKimiBackendReportsUsageFromWireLog(t *testing.T) {
	t.Parallel()
	assertKimiBackendReportsWireUsage(t, map[string]string{"KIMI_CODE_HOME": t.TempDir()}, "")
}

func TestKimiBackendReportsWireUsageFromRelativeHome(t *testing.T) {
	t.Parallel()
	assertKimiBackendReportsWireUsage(t, map[string]string{"KIMI_CODE_HOME": "runtime-data"}, t.TempDir())
}

func TestKimiBackendReportsWireUsageFromTaskHome(t *testing.T) {
	// Exercise the backend's HOME fallback. The daemon rejects HOME in custom agent environments.
	t.Setenv("KIMI_CODE_HOME", "")
	assertKimiBackendReportsWireUsage(t, map[string]string{"HOME": t.TempDir()}, "")
}

func assertKimiBackendReportsWireUsage(t *testing.T, env map[string]string, cwd string) {
	t.Helper()
	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiACPNoUsageScript()))

	// Timestamp the record comfortably after the turn starts.
	env["KIMI_TEST_RECORD_TIME"] = strconv.FormatInt(time.Now().Add(time.Hour).UnixMilli(), 10)
	backend, err := New("kimi", Config{
		ExecutablePath: fakePath,
		Logger:         slog.Default(),
		Env:            env,
	})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	session, err := backend.Execute(ctx, "task", ExecOptions{Cwd: cwd, Timeout: 20 * time.Second})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()

	result := <-session.Result
	if result.Status != "completed" {
		t.Fatalf("status=%q error=%q", result.Status, result.Error)
	}

	usage, ok := result.Usage["moonshot-cn/kimi-k2.7-code"]
	if !ok {
		t.Fatalf("expected usage keyed by the wire log's model, got %v", result.Usage)
	}
	if usage.InputTokens != 7694 {
		t.Errorf("InputTokens = %d, want 7694", usage.InputTokens)
	}
	if usage.OutputTokens != 21 {
		t.Errorf("OutputTokens = %d, want 21", usage.OutputTokens)
	}
	if usage.CacheReadTokens != 14848 {
		t.Errorf("CacheReadTokens = %d, want 14848", usage.CacheReadTokens)
	}
}

// TestScanKimiSessionUsageSkipsPriorRunOnResume is the double-billing guard:
// kimi appends to the same wire log when a session is resumed, so the file
// still holds records the earlier task already reported. Only records stamped
// after this turn began may be billed.
func TestScanKimiSessionUsageSkipsPriorRunOnResume(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	turnStart := time.Now().Truncate(time.Second).Add(500 * time.Millisecond)
	path := writeKimiWireLog(t, home, "session_r", "main",
		kimiWireRecordLineAt(turnStart.Add(-time.Hour), 5000, 500, 9000, 0), // previous task
		kimiWireRecordLineAt(turnStart.Add(time.Second), 120, 12, 340, 0),   // this task
	)
	// Simulate a filesystem that stores mtimes at one-second precision: the
	// log was touched during this turn, but its rounded mtime precedes the
	// sub-second turn boundary.
	coarseMtime := turnStart.Truncate(time.Second)
	if err := os.Chtimes(path, coarseMtime, coarseMtime); err != nil {
		t.Fatalf("chtimes: %v", err)
	}

	usage := kimiScanTotal(kimiUsageScan{startTime: turnStart, kimiHome: home, sessionID: "session_r", resumed: true, fallbackModel: "unknown"})

	if usage.InputTokens != 120 || usage.OutputTokens != 12 || usage.CacheReadTokens != 340 {
		t.Fatalf("re-billed the previous run on resume: %+v", usage)
	}
}

// TestScanKimiSessionUsageUntimedRecords pins the fallback for a hypothetical
// future format that drops the timestamp: bill it on a fresh session, drop it
// on a resume rather than risk charging twice.
func TestScanKimiSessionUsageUntimedRecords(t *testing.T) {
	t.Parallel()

	for _, tc := range []struct {
		name    string
		resumed bool
		want    int64
	}{
		{name: "fresh session bills untimed records", resumed: false, want: 100},
		{name: "resumed session drops untimed records", resumed: true, want: 0},
	} {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			home := t.TempDir()
			writeKimiWireLog(t, home, "session_u", "main",
				`{"type":"usage.record","model":"m","usage":{"inputOther":100,"output":0,"inputCacheRead":0,"inputCacheCreation":0}}`)

			usage := kimiScanTotal(kimiUsageScan{startTime: time.Now().Add(-time.Minute), kimiHome: home, sessionID: "session_u", resumed: tc.resumed, fallbackModel: "unknown"})
			if usage.InputTokens != tc.want {
				t.Errorf("InputTokens = %d, want %d", usage.InputTokens, tc.want)
			}
		})
	}
}

// TestScanKimiSessionUsageSplitsByModel: a subagent may run a different model
// from the main agent. Folding both into one bucket prices one of them wrong —
// cache-read alone spans a 10x range across kimi models.
func TestScanKimiSessionUsageSplitsByModel(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	now := time.Now()
	writeKimiWireLog(t, home, "session_mm", "main",
		kimiWireRecordLineFor("moonshot-cn/kimi-k3", now, 1000, 100, 2000, 0))
	writeKimiWireLog(t, home, "session_mm", "subagent-1",
		kimiWireRecordLineFor("moonshot-cn/kimi-k2.5", now, 30, 3, 40, 0))

	byModel := scanKimiSessionUsage(kimiUsageScan{
		startTime: now.Add(-time.Minute), kimiHome: home,
		sessionID: "session_mm", fallbackModel: "unknown",
	})

	if len(byModel) != 2 {
		t.Fatalf("expected one bucket per model, got %v", byModel)
	}
	if got := byModel["moonshot-cn/kimi-k3"]; got.InputTokens != 1000 || got.CacheReadTokens != 2000 {
		t.Errorf("k3 bucket = %+v", got)
	}
	if got := byModel["moonshot-cn/kimi-k2.5"]; got.InputTokens != 30 || got.CacheReadTokens != 40 {
		t.Errorf("k2.5 bucket = %+v", got)
	}
}

// TestScanKimiSessionUsageFallbackModel: a record with no model of its own is
// billed to the model the task asked for, not dropped.
func TestScanKimiSessionUsageFallbackModel(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	writeKimiWireLog(t, home, "session_f", "main",
		fmt.Sprintf(`{"type":"usage.record","usage":{"inputOther":42,"output":1,"inputCacheRead":0,"inputCacheCreation":0},"time":%d}`,
			time.Now().UnixMilli()))

	byModel := scanKimiSessionUsage(kimiUsageScan{
		startTime: time.Now().Add(-time.Minute), kimiHome: home,
		sessionID: "session_f", fallbackModel: "picked-model",
	})
	if got := byModel["picked-model"]; got.InputTokens != 42 {
		t.Fatalf("expected the fallback model to be billed, got %v", byModel)
	}
}

func TestKimiChildHomeUsesEffectiveEnvironment(t *testing.T) {
	inherited := t.TempDir()
	override := t.TempDir()
	t.Setenv("KIMI_CODE_HOME", inherited)
	if got := kimiChildHome(buildEnv(nil), ""); got != inherited {
		t.Errorf("inherited KIMI_CODE_HOME = %q, want %q", got, inherited)
	}
	if got := kimiChildHome(buildEnv(map[string]string{"KIMI_CODE_HOME": override}), ""); got != override {
		t.Errorf("overridden KIMI_CODE_HOME = %q, want %q", got, override)
	}
	if got, want := kimiChildHome(buildEnv(map[string]string{"KIMI_CODE_HOME": "kimi-data"}), override), filepath.Join(override, "kimi-data"); got != want {
		t.Errorf("relative KIMI_CODE_HOME = %q, want %q", got, want)
	}
}

// TestKimiSessionWireLogsRejectsTraversal: the session id comes from the
// agent and is joined into a path.
func TestKimiSessionWireLogsRejectsTraversal(t *testing.T) {
	t.Parallel()

	root := t.TempDir()
	for _, id := range []string{"", ".", "..", "../escape", "a/b"} {
		if got := kimiSessionWireLogs(root, id); got != nil {
			t.Errorf("sessionID %q should be rejected, got %v", id, got)
		}
	}
}

// kimiWireTurnEndedLine builds an `agent.turn.ended` wire record like the ones
// kimi-code appends to agents/main/wire.jsonl when a turn terminates.
func kimiWireTurnEndedLine(outcome, errorMessage string, at time.Time) string {
	return fmt.Sprintf(`{"turnId":0,"outcome":%q,"errorMessage":%q,"type":"agent.turn.ended","time":%d,"kind":"event"}`,
		outcome, errorMessage, at.UnixMilli())
}

// TestScanKimiMainTurnFailureFlipsOnFailedTerminalEvent covers #9054: kimi's
// ACP adapter answered session/prompt successfully while its own wire log
// recorded the main turn as failed (a bare OAuthConnectionError no stderr
// sniffer signature matches). The persisted outcome is authoritative.
func TestScanKimiMainTurnFailureFlipsOnFailedTerminalEvent(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	now := time.Now()
	writeKimiWireLog(t, home, "session_kf", "main",
		`{"type":"metadata","protocol_version":"1"}`,
		kimiWireTurnEndedLine("failed",
			"OAuthConnectionError: OAuth request to https://auth.kimi.com/api/oauth/token failed: fetch failed: Connect Timeout Error",
			now),
	)

	detail, failed := scanKimiMainTurnFailure(kimiUsageScan{
		startTime: now.Add(-time.Minute), kimiHome: home,
		sessionID: "session_kf", fallbackModel: "unknown",
	})
	if !failed {
		t.Fatal("expected the failed main-agent terminal event to be reported")
	}
	if !strings.Contains(detail, "OAuthConnectionError") {
		t.Errorf("detail should carry kimi's diagnostic, got %q", detail)
	}
}

// TestScanKimiMainTurnFailureSanitizesWireDiagnostic covers the review on
// #9057: kimi's diagnostic is provider-authored text, and a JSON credential
// embedded in it (request={"api_key":...}) survives both the raw string and
// the shared redact.Text — the diagnostic must take the same
// sanitizeAgentDiagnostic pass as every child-process diagnostic that
// reaches Result.Error.
func TestScanKimiMainTurnFailureSanitizesWireDiagnostic(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	now := time.Now()
	const fakeSecret = "review-only-fake-secret"
	writeKimiWireLog(t, home, "session_secret", "main",
		kimiWireTurnEndedLine("failed",
			`request={"api_key":"`+fakeSecret+`"} while calling https://api.kimi.com`,
			now),
	)

	detail, failed := scanKimiMainTurnFailure(kimiUsageScan{
		startTime: now.Add(-time.Minute), kimiHome: home,
		sessionID: "session_secret", fallbackModel: "unknown",
	})
	if !failed {
		t.Fatal("expected the failed main-agent terminal event to be reported")
	}
	if strings.Contains(detail, fakeSecret) {
		t.Errorf("secret leaked through the wire diagnostic: %q", detail)
	}
	if !strings.Contains(detail, `api_key":"[REDACTED]"`) {
		t.Errorf("expected the JSON secret field redacted, got %q", detail)
	}
}

func TestScanKimiMainTurnFailureErrorMessageFormats(t *testing.T) {
	t.Parallel()

	for _, tc := range []struct {
		name string
		raw  string
		want string
	}{
		{name: "string", raw: `"Connection error."`, want: "Connection error."},
		{name: "opaque string", raw: `" [object Object] "`},
		{name: "empty string", raw: `""`},
		{name: "null", raw: `null`},
		{name: "object", raw: `{"code":"provider.failure","message":"Connection error.","details":"private-details","retryable":true}`, want: "[provider.failure] Connection error."},
		{name: "message only", raw: `{"message":"Connection error."}`, want: "Connection error."},
		{name: "code only", raw: `{"code":"provider.failure","message":"[object Object]"}`, want: "[provider.failure]"},
		{name: "invalid code", raw: `{"code":{},"message":"Connection error."}`, want: "Connection error."},
		{name: "empty object", raw: `{}`},
		{name: "invalid message", raw: `{"message":{"secret":"private-details"}}`},
		{name: "array", raw: `["private-details"]`},
		{name: "number", raw: `42`},
		{name: "boolean", raw: `true`},
		{name: "redacted object", raw: `{"code":"provider.failure","message":"api_key=fixture-secret","details":"private-details"}`, want: "[provider.failure] [REDACTED CREDENTIAL]"},
	} {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			home := t.TempDir()
			now := time.Now()
			line := fmt.Sprintf(`{"type":"agent.turn.ended","outcome":"failed","time":%d,"errorMessage":%s}`, now.UnixMilli(), tc.raw)
			writeKimiWireLog(t, home, "session_formats", "main", line)
			detail, failed := scanKimiMainTurnFailure(kimiUsageScan{
				startTime: now.Add(-time.Minute), kimiHome: home, sessionID: "session_formats",
			})
			if !failed {
				t.Fatal("errorMessage shape must not hide the failed terminal event")
			}
			if detail != tc.want {
				t.Errorf("detail = %q, want %q", detail, tc.want)
			}
		})
	}
}

// TestScanKimiMainTurnFailureIgnoresHealthyStaleAndDelegated guards the flip
// against false positives: a successful main turn stays completed, a failed
// record from a previous task on a resumed session is outside the turn
// boundary, and a delegated agent's failure is that agent's own task path —
// only the main agent's terminal event fails the run.
func TestScanKimiMainTurnFailureIgnoresHealthyStaleAndDelegated(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	now := time.Now()

	writeKimiWireLog(t, home, "session_ok", "main", kimiWireTurnEndedLine("success", "", now))
	writeKimiWireLog(t, home, "session_del", "main", kimiWireTurnEndedLine("success", "", now))
	writeKimiWireLog(t, home, "session_del", "researcher", kimiWireTurnEndedLine("failed", "delegated boom", now))
	writeKimiWireLog(t, home, "session_stale", "main", kimiWireTurnEndedLine("failed", "stale boom", now.Add(-time.Hour)))

	for _, tc := range []struct {
		sessionID string
		resumed   bool
	}{
		{sessionID: "session_ok"},
		{sessionID: "session_del"},
		{sessionID: "session_stale", resumed: true},
	} {
		if _, failed := scanKimiMainTurnFailure(kimiUsageScan{
			startTime: now.Add(-time.Minute), kimiHome: home,
			sessionID: tc.sessionID, resumed: tc.resumed, fallbackModel: "unknown",
		}); failed {
			t.Errorf("session %q: expected no main-turn failure", tc.sessionID)
		}
	}
}

// TestScanKimiMainTurnFailureReadsPastOversizedRecord covers the review on
// #9057: newAgentStreamScanner stops for good at a line beyond
// agentStreamMaxLineBytes and the old scan loop never checked scanner.Err(),
// so a valid but oversized context.append record ahead of the terminal event
// hid the failed outcome and kept the run completed. The scan must discard
// the oversized record and keep reading.
func TestScanKimiMainTurnFailureReadsPastOversizedRecord(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	now := time.Now()
	oversized := `{"type":"context.append","content":"` + strings.Repeat("x", agentStreamMaxLineBytes) + `"}`
	writeKimiWireLog(t, home, "session_big", "main",
		oversized,
		kimiWireTurnEndedLine("failed", "boom after the oversized record", now),
	)

	detail, failed := scanKimiMainTurnFailure(kimiUsageScan{
		startTime: now.Add(-time.Minute), kimiHome: home,
		sessionID: "session_big", fallbackModel: "unknown",
	})
	if !failed {
		t.Fatal("expected the failed terminal event past the oversized record to be reported")
	}
	if !strings.Contains(detail, "boom after the oversized record") {
		t.Errorf("detail should carry the diagnostic, got %q", detail)
	}
}

// TestScanKimiMainTurnFailureWithoutWireLog: no session directory (scan ran
// before kimi wrote anything, or the home is unset) must be a no-op.
func TestScanKimiMainTurnFailureWithoutWireLog(t *testing.T) {
	t.Parallel()

	if _, failed := scanKimiMainTurnFailure(kimiUsageScan{
		startTime: time.Now().Add(-time.Minute), kimiHome: t.TempDir(),
		sessionID: "session_absent", fallbackModel: "unknown",
	}); failed {
		t.Fatal("expected no failure without a wire log")
	}
}

// fakeKimiWireTurnFailureScript mimics the #9054 incident shape: the ACP
// layer answers session/prompt successfully (end_turn) after emitting real
// narration, while kimi's own wire log records the main turn as failed with
// a bare OAuthConnectionError that matches no stderr sniffer signature.
func fakeKimiWireTurnFailureScript(sessionID string) string {
	script := `#!/bin/sh
while IFS= read -r line; do
  id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
  case "$line" in
    *'"method":"initialize"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"
      ;;
    *'"method":"session/new"'*)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_wirefail"}}\n' "$id"
      ;;
    *'"method":"session/prompt"'*)
      printf '{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"ses_wirefail","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"partial narration before the failure"}}}}\n'
      dir="$KIMI_CODE_HOME/sessions/wd_test_abc123/ses_wirefail/agents/main"
      mkdir -p "$dir"
      # time:0 = untimed record; kimiWireRecordInTurn counts those on fresh
      # runs, and a shell cannot produce millisecond timestamps portably.
      printf '{"turnId":0,"outcome":"failed","errorMessage":"OAuthConnectionError: OAuth request to https://auth.kimi.com/api/oauth/token failed: fetch failed: Connect Timeout Error","type":"agent.turn.ended","time":0,"kind":"event"}\n' >> "$dir/wire.jsonl"
      printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn"}}\n' "$id"
      exit 0
      ;;
  esac
done
`
	return strings.ReplaceAll(script, "ses_wirefail", sessionID)
}

// TestKimiBackendWireTurnFailureFailsCompletedRun is the end-to-end guard for
// #9054: a successful ACP prompt response with nonempty narration must not
// publish the run as completed when kimi's wire log says the main turn
// failed. The partial narration stays in the transcript; the status and the
// sanitized diagnostic must reach the daemon so retry policy sees the truth.
func TestKimiBackendWireTurnFailureFailsCompletedRun(t *testing.T) {
	t.Parallel()

	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiWireTurnFailureScript("ses_wirefail")))

	backend, err := New("kimi", Config{
		ExecutablePath: fakePath,
		Logger:         slog.Default(),
		Env:            map[string]string{"KIMI_CODE_HOME": "runtime-data"},
	})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{Cwd: t.TempDir(), Timeout: 5 * time.Second})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()

	select {
	case result, ok := <-session.Result:
		if !ok {
			t.Fatal("result channel closed without a value")
		}
		if result.Status != "failed" {
			t.Fatalf("expected status=failed from the wire turn outcome, got %q (error=%q)", result.Status, result.Error)
		}
		if !strings.Contains(result.Error, "OAuthConnectionError") {
			t.Errorf("expected kimi's diagnostic in the error, got %q", result.Error)
		}
		if !strings.Contains(result.Output, "partial narration") {
			t.Errorf("partial narration should stay in the transcript, got %q", result.Output)
		}
		if result.ResumeRejected {
			t.Error("an OAuth transport failure is not a poisoned history; ResumeRejected must stay false")
		}
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
	}
}

func TestKimiBackendWireTurnFailureDiagnostics(t *testing.T) {
	t.Parallel()

	for _, tc := range []struct {
		name string
		raw  string
		want string
	}{
		{name: "opaque", raw: `"[object Object]"`, want: "kimi turn failed: runtime did not record error details (error category unknown)"},
		{name: "empty", raw: `""`, want: "kimi turn failed: runtime did not record error details (error category unknown)"},
		{name: "object", raw: `{"code":"provider.failure","message":"Unspecified failure","details":"private-details"}`, want: "kimi turn failed: [provider.failure] Unspecified failure"},
	} {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			home := t.TempDir()
			fakePath := filepath.Join(t.TempDir(), "kimi")
			script := fakeKimiWireTurnFailureScript("ses_diagnostic")
			script = strings.Replace(script,
				`"OAuthConnectionError: OAuth request to https://auth.kimi.com/api/oauth/token failed: fetch failed: Connect Timeout Error"`, tc.raw, 1)
			writeTestExecutable(t, fakePath, []byte(script))
			result := runKimiThinkingTest(t, fakePath, map[string]string{"KIMI_CODE_HOME": home}, ExecOptions{})
			if result.Status != "failed" || result.Error != tc.want {
				t.Fatalf("status=%q error=%q, want failed with %q", result.Status, result.Error, tc.want)
			}
			if !strings.Contains(result.Output, "partial narration") || result.ResumeRejected {
				t.Errorf("partial output or session state changed: output=%q resumeRejected=%v", result.Output, result.ResumeRejected)
			}
			if got := taskfailure.Classify(result.Error); got != taskfailure.ReasonAgentUnknown {
				t.Errorf("unspecified failure classified as %q, want unknown", got)
			}
		})
	}
}

// TestKimiBackendWireTurnFailurePastOversizedRecord reproduces the review's
// offline repro on #9057: a valid context.append record at the scan's 32 MiB
// bound sits ahead of the failed agent.turn.ended record. With the old
// scanner loop — which stops for good at an over-bound line and never checks
// scanner.Err() — the backend reported Status="completed", Error="".
func TestKimiBackendWireTurnFailurePastOversizedRecord(t *testing.T) {
	t.Parallel()

	home := t.TempDir()
	dir := filepath.Join(home, "sessions", "wd_test_abc123", "ses_big", "agents", "main")
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatalf("mkdir wire log dir: %v", err)
	}
	oversized := `{"type":"context.append","content":"` + strings.Repeat("x", agentStreamMaxLineBytes) + `"}`
	if err := os.WriteFile(filepath.Join(dir, "wire.jsonl"), []byte(oversized+"\n"), 0o644); err != nil {
		t.Fatalf("write oversized record: %v", err)
	}

	fakePath := filepath.Join(t.TempDir(), "kimi")
	writeTestExecutable(t, fakePath, []byte(fakeKimiWireTurnFailureScript("ses_big")))

	backend, err := New("kimi", Config{
		ExecutablePath: fakePath,
		Logger:         slog.Default(),
		Env:            map[string]string{"KIMI_CODE_HOME": home},
	})
	if err != nil {
		t.Fatalf("new kimi backend: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{Timeout: 5 * time.Second})
	if err != nil {
		t.Fatalf("execute: %v", err)
	}
	go func() {
		for range session.Messages {
		}
	}()

	select {
	case result, ok := <-session.Result:
		if !ok {
			t.Fatal("result channel closed without a value")
		}
		if result.Status != "failed" {
			t.Fatalf("expected status=failed past the oversized record, got %q (error=%q)", result.Status, result.Error)
		}
		if !strings.Contains(result.Error, "OAuthConnectionError") {
			t.Errorf("expected kimi's diagnostic in the error, got %q", result.Error)
		}
	case <-time.After(10 * time.Second):
		t.Fatal("timeout waiting for result")
	}
}