Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ AI 提交信息使用独立的服务商配置和可配置的生成规则,支

不要这样做:界面直接读取服务商的认证文件并把完整解析结果写进界面状态或普通设置。这样会让凭据混入界面状态和设置导出。

### 配置发现测试隔离本机账号

服务商发现和配置路径查询把主目录、环境快照作为显式输入。生产入口仍读取真实主目录和 `loginenv`;真实 Core 集成测试传入临时主目录和测试自己构造的完整环境,空快照也不补读本机环境。路径发现、配置解析和凭据定位因此使用同一份输入,断言仍覆盖服务商数量、元数据和文件/环境凭据(#2214)。

只替换配置文件来源不能隔离 Claude 的纯环境配置;逐个清空已知变量又会漏掉以后支持的变量。因此不采用这两种做法,也不通过替换真实解析器、跳过测试或放宽数量断言来避免失败。代价是两个平台输入需要显式传递;测试无需改动进程环境,失败信息只报告匹配结果,不输出环境值或凭据。

### 以暂存改动生成草稿

生成依据是提交面板中当前变更列表的暂存改动(`desktop/internal/workbench/aicommit.go`)。Core 的 `git.commitMessageContext` 采集证据,并基于完整未截断的内容、仓库状态和索引标志生成令牌;应用生成结果前重新核对这个令牌,状态变化时不应用结果。
Expand Down Expand Up @@ -67,6 +73,7 @@ Codex/Claude 的 OAuth 登录不等同于可用的 API 密钥,本次不实现
## 验证

- `scripts/desktop/desktop-go.sh test ./internal/ai/... ./internal/workbench/...`:配置解析、凭据不序列化、协议映射、截断和错误边界。
- `TestProviderDiscoveryUsesRealCoreParser`:无配置、两个单独来源、Claude 纯环境配置、双源并存和配置目录覆写;即使运行测试的进程带有 Claude 环境变量,结果也只取决于显式快照。
- Core 仓库的 [AI 提交契约](https://github.com/Lithe-IDEA/Lithe-core-rust/blob/main/rust/contracts/ai-commit.md) 与 [共享夹具](https://github.com/Lithe-IDEA/Lithe-core-rust/blob/main/rust/fixtures/ai/commit-generation-v1.json)。
- `node scripts/agent-notes/verify-agent-notes.sh`

Expand All @@ -76,6 +83,8 @@ Codex/Claude 的 OAuth 登录不等同于可用的 API 密钥,本次不实现

- `desktop/internal/ai/commit.go`
- `desktop/internal/ai/http.go`
- `desktop/internal/ai/provider.go`
- `desktop/internal/ai/provider_test.go`
- `desktop/internal/workbench/aicommit.go`
- `desktop/internal/credentials/backend_darwin.go`
- `desktop/internal/credentials/backend_other.go`
Expand Down
18 changes: 14 additions & 4 deletions desktop/internal/ai/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ func Sources() []string { return []string{"codex", "claude"} }
// credential locator. A source that cannot be read or parsed contributes no provider; no error
// carries a secret.
func Discover(ctx context.Context, k Kernel) map[string][]Provider {
providers := map[string][]Provider{}
home, err := os.UserHomeDir()
if err != nil {
return providers
return map[string][]Provider{}
}
environment := Environment()
return discoverProviders(ctx, k, home, Environment())
}

// discoverProviders uses one explicit home and environment snapshot for file discovery,
// parsing and credential resolution. It never supplements the snapshot with the real user environment.
func discoverProviders(ctx context.Context, k Kernel, home string, environment map[string]string) map[string][]Provider {
providers := map[string][]Provider{}
for _, source := range Sources() {
if provider, ok := discover(ctx, k, source, home, environment); ok {
providers[source] = append(providers[source], provider)
Expand Down Expand Up @@ -184,7 +189,12 @@ func ConfigurationPath(ctx context.Context, k Kernel, source string) string {
if err != nil {
return ""
}
files, err := k.ConfigurationSources(ctx, source, home, Environment())
return configurationPath(ctx, k, source, home, Environment())
}

// configurationPath resolves the native file against the same explicit inputs as discovery.
func configurationPath(ctx context.Context, k Kernel, source, home string, environment map[string]string) string {
files, err := k.ConfigurationSources(ctx, source, home, environment)
if err != nil || len(files) == 0 {
return ""
}
Expand Down
159 changes: 118 additions & 41 deletions desktop/internal/ai/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ai

import (
"context"
"maps"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -33,47 +33,124 @@ func TestCredentialLocatorResolution(t *testing.T) {
}
}

// fileKernel uses the real Core parser with files the test writes.
type fileKernel struct {
*Client
files []ConfigurationFile
}

func (k fileKernel) ConfigurationSources(_ context.Context, source, _ string, _ map[string]string) ([]ConfigurationFile, error) {
if source != "codex" {
return nil, nil
}
return k.files, nil
}

func TestProviderDiscoveryUsesRealCoreParser(t *testing.T) {
directory := t.TempDir()
configPath := filepath.Join(directory, "config.toml")
authPath := filepath.Join(directory, "auth.json")
if err := os.WriteFile(configPath, []byte("model = 'fixture-model'\nmodel_provider = 'gateway'\n[model_providers.gateway]\nbase_url = 'https://example.test/v1'\nrequires_openai_auth = true\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(authPath, []byte(`{"OPENAI_API_KEY":"fixture-file-secret"}`), 0o600); err != nil {
t.Fatal(err)
}
kernel := fileKernel{Client: testClient(), files: []ConfigurationFile{
{File: "config", Path: configPath},
{File: "auth", Path: authPath},
}}
providers := Discover(testContext(t), kernel)
if len(providers["codex"]) != 1 || len(providers["claude"]) != 0 {
t.Fatalf("providers = %d codex, %d claude; want one Codex provider", len(providers["codex"]), len(providers["claude"]))
}
provider := providers["codex"][0]
if provider.Issue != "" || provider.Endpoint != "https://example.test/v1" || provider.Model != "fixture-model" ||
provider.Protocol != "responses" || provider.APIKey != "fixture-file-secret" {
t.Fatalf("provider issue=%q endpoint=%q model=%q protocol=%q keyPresent=%v", provider.Issue, provider.Endpoint, provider.Model, provider.Protocol, provider.APIKey != "")
}
if _, ok := Pick(providers, Sources()); !ok {
t.Fatal("the discovered provider is not usable for generation")
}
if got := ConfigurationPath(testContext(t), kernel, "codex"); got != configPath {
t.Fatalf("configuration path = %q, want %q", got, configPath)
const codexConfig = "model = 'fixture-model'\nmodel_provider = 'gateway'\n[model_providers.gateway]\nbase_url = 'https://example.test/v1'\nrequires_openai_auth = true\n"
const codexAuth = `{"OPENAI_API_KEY":"fixture-file-secret"}`
const claudeSettings = `{"model":"fixture-claude-model","env":{"ANTHROPIC_BASE_URL":"https://claude.example.test/v1","ANTHROPIC_API_KEY":"fixture-claude-file-secret"}}`
codex := Provider{
Source: "codex", Name: "Codex · gateway", Endpoint: "https://example.test/v1", Model: "fixture-model",
Protocol: "responses", Authentication: "bearer", APIKey: "fixture-file-secret",
}
claudeEnvironment := Provider{
Source: "claude", Name: "Claude", Endpoint: "https://claude.example.test/v1", Model: "fixture-claude-model",
Protocol: "anthropicMessages", Authentication: "apiKey", APIKey: "fixture-environment-secret",
}
claudeFile := claudeEnvironment
claudeFile.APIKey = "fixture-claude-file-secret"
environment := map[string]string{
"ANTHROPIC_BASE_URL": "https://claude.example.test/v1",
"ANTHROPIC_MODEL": "fixture-claude-model",
"ANTHROPIC_API_KEY": "fixture-environment-secret",
}
for _, test := range []struct {
name string
files map[string]string
environment map[string]string
want map[string]Provider
wantPick string
codexPath string
}{
{name: "no configuration"},
{
name: "Codex files only",
files: map[string]string{".codex/config.toml": codexConfig, ".codex/auth.json": codexAuth},
want: map[string]Provider{"codex": codex},
wantPick: "codex",
codexPath: ".codex/config.toml",
},
{
name: "Claude files only",
files: map[string]string{".claude/settings.json": claudeSettings},
want: map[string]Provider{"claude": claudeFile},
wantPick: "claude",
},
{
name: "Claude environment without files",
environment: environment,
want: map[string]Provider{"claude": claudeEnvironment},
wantPick: "claude",
},
{
name: "both sources",
files: map[string]string{".codex/config.toml": codexConfig, ".codex/auth.json": codexAuth},
environment: environment,
want: map[string]Provider{"codex": codex, "claude": claudeEnvironment},
wantPick: "codex",
},
{
name: "environment selects configuration directories",
files: map[string]string{
"codex-home/config.toml": codexConfig,
"codex-home/auth.json": codexAuth,
"claude-home/settings.json": claudeSettings,
},
environment: map[string]string{"CODEX_HOME": "codex-home", "CLAUDE_CONFIG_DIR": "claude-home"},
want: map[string]Provider{"codex": codex, "claude": claudeFile},
wantPick: "codex",
codexPath: "codex-home/config.toml",
},
} {
t.Run(test.name, func(t *testing.T) {
home := t.TempDir()
for name, contents := range test.files {
path := filepath.Join(home, name)
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
t.Fatal(err)
}
}
environment := maps.Clone(test.environment)
for _, key := range []string{"CODEX_HOME", "CLAUDE_CONFIG_DIR"} {
if relative := environment[key]; relative != "" {
environment[key] = filepath.Join(home, relative)
}
}
// Both native path discovery and parsing use the real Core. Neither the home
// nor the environment comes from the developer's account, even when nil.
kernel := testClient()
providers := discoverProviders(testContext(t), kernel, home, environment)
if len(providers) != len(test.want) {
t.Fatalf("provider sources = %d, want %d", len(providers), len(test.want))
}
for _, source := range Sources() {
want, configured := test.want[source]
if !configured {
if len(providers[source]) != 0 {
t.Fatalf("unexpected provider for %s", source)
}
continue
}
if len(providers[source]) != 1 {
t.Fatalf("providers for %s = %d, want 1", source, len(providers[source]))
}
provider := providers[source][0]
keyMatches := provider.APIKey == want.APIKey
provider.APIKey, want.APIKey = "", ""
if provider != want || !keyMatches {
t.Fatalf("%s provider: metadataMatch=%v credentialMatch=%v", source, provider == want, keyMatches)
}
}
if provider, ok := Pick(providers, Sources()); ok != (test.wantPick != "") || provider.Source != test.wantPick {
t.Fatal("provider selection does not match the fixture")
}
if test.codexPath != "" {
if got := configurationPath(testContext(t), kernel, "codex", home, environment); got != filepath.Join(home, test.codexPath) {
t.Fatal("configuration path does not match the fixture")
}
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
]
},
"owner": "Git / AI",
"verification": "用相同 diff、规则和 Provider 配置比较请求计划、取消、错误和生成文本。 共享提交上下文的真实 Git 回归覆盖同路径重新暂存、未缓存工作区变化、截断外内容、多仓库同名文件、敏感重命名及目录展开;macOS 确认替换时再次核对并保留新输入,Windows 测试验证同一路径的内容变化和取消。两端原生完整界面验收仍为 pending。",
"verification": "用相同 diff、规则和 Provider 配置比较请求计划、取消、错误和生成文本。 共享提交上下文的真实 Git 回归覆盖同路径重新暂存、未缓存工作区变化、截断外内容、多仓库同名文件、敏感重命名及目录展开;macOS 确认替换时再次核对并保留新输入,Windows 测试验证同一路径的内容变化和取消。两端原生完整界面验收仍为 pending。 服务商发现使用临时主目录和显式环境快照,覆盖无配置、Codex/Claude 单源、Claude 纯环境配置、双源并存及配置目录覆写;保留真实 Core 路径发现、配置解析和文件/环境凭据断言,不读取本机账号环境。",
"notes": "2026-11-10:Viva Commit 面板新增“生成提交信息”按钮:Core 的 git.commitMessageContext(暂存区证据与状态令牌)、ai.planCommit、ai.decodeCommitMessage 经 desktop/internal/core 封装,Go 只从 Codex/Claude 本机配置选取首个带凭据的服务商、附加认证头并发起 HTTP(45 秒期限、不跟随重定向、响应 2 MiB 上限、错误不回显响应体),生成后再校验令牌;输入框已有文字时需确认替换并保留确认期间的新输入。生成永不暂存、提交或推送。未接入:设置页中的服务商选择与生成选项(语言、格式、长度、推理强度,当前用契约默认值)、手动服务商与密钥;真实供应商和原生界面验收仍为 pending。 2026-11-12:Agent 对话删除后,服务商发现移到 desktop/internal/workbench/ai_provider.go(只依赖 Core 的配置定位与解析),生成选项的设置页改为“设置 › AI › 生成”。 2026-11-14:AI 代码移到独立 Go 包 desktop/internal/ai(Core AI 命令封装、Codex/Claude 服务商发现、HTTP 与生成流程),desktop/internal/core 不再含 AI 代码,workbench 只保留界面状态与本地化错误文案。",
"legacy": {
"revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36",
Expand Down
Loading