Repository navigation
feat(agents): 供应商密钥只写不读,Lithe 保存供应商列表,修改前备份并可恢复 - #249
Conversation
agentProviders.apply 的 addProvider/updateProvider 可带 key、clearKey;configuration.providers 新增 listKeptBy;替换配置文件前由 Core 备份,新增 agentProviders.restore。agents 0 内的补充,不升能力版本。
| for (id, file) in &record.files { | ||
| let (_, path) = files | ||
| .iter() | ||
| .find(|(declared, _)| declared == id) | ||
| .ok_or(RestoreRefusal::Failed)?; | ||
| let real = real_file(path).map_err(|_| RestoreRefusal::Failed)?; |
There was a problem hiding this comment.
🔴 Restore overwrites the wrong configuration
After an agent’s configuration directory changes, restore applies the old backup to the new directory. Confirming discardLaterChanges lets it overwrite the new configuration or delete it if the original file was absent.
Learn more
The backup record identifies a file only by its manifest ID. agent_paths resolves that ID using current environment values, so its path can differ from the one edited. edit_staged already has the actual path when it captures the original bytes. Restore checks the current bytes only when discard is false, and then applies the old baseline to the newly resolved path.
Example: Back up an agent’s config.json under one CLI home, then restart with a different CLI home. Restore reports later changes. Confirming discard replaces the second home’s configuration even though Lithe never edited it.
Recommended fix: Bind each baseline and last-written digest to the resolved file identity at apply time. Refuse a mismatched target independently of discardLaterChanges, and test switching an environment-based root between apply and restore.
Was this helpful? React with 👍 or 👎 to provide feedback.
| let listed = saved_after != saved_before; | ||
| if listed { | ||
| let _writing = self.lock_writing(); | ||
| saved::store(&config, &agent_key, saved_after.clone())?; | ||
| } |
There was a problem hiding this comment.
🔴 Overlapping saves lose provider entries
When two additions for the same agent read the same saved list, apply_providers_checked replaces it twice. Both can succeed, but the last write drops the other provider. A failed edit’s rollback can also drop an intervening save.
Learn more
The host command reaches the runtime without serializing provider operations. apply_providers_checked reads and checks the list before locking, then store replaces the whole list for that agent. The separate rollback also replaces the list without checking for intervening changes.
Example: Two settings surfaces add different providers to the same agent. Both read the original list before either writes. The second write replaces the first addition while both requests report success.
Recommended fix: Serialize the full provider-list transaction per agent, or use revision-checked commits and rollbacks. Keep the library-wide write lock out of backend calls.
Was this helpful? React with 👍 or 👎 to provide feedback.
| for (id, real, current, baseline) in &targets { | ||
| keep_history(&directory, id, current.as_deref()).map_err(|_| RestoreRefusal::Failed)?; | ||
| let put_back = match baseline { | ||
| Some(bytes) => write_replacing(real, bytes), | ||
| None => match fs::remove_file(real) { | ||
| Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error), | ||
| _ => Ok(()), | ||
| }, | ||
| }; | ||
| if put_back.is_err() { | ||
| // Files already put back are no longer backed up; the rest are. | ||
| let _ = store(&directory, &remaining); | ||
| return Err(RestoreRefusal::Failed); | ||
| } | ||
| remaining.files.remove(id); | ||
| let _ = fs::remove_file(directory.join(BASELINE_DIRECTORY).join(file_key(id))); | ||
| } | ||
| store(&directory, &remaining).map_err(|_| RestoreRefusal::Failed)?; |
There was a problem hiding this comment.
🔴 Partial restore blocks future recovery
If keep_history fails after one file is restored, restore exits with the old backup record. That record names a deleted baseline, so retries cannot restore the remaining files.
Learn more
Restore preloads all recorded baselines before any file is restored. It removes each baseline from disk as soon as that file is restored, but does not persist the shortened record on the history-write error path. A later retry stops during preflight before reaching any remaining write, even with discard enabled. Failure to persist the shortened record has the same effect.
Example: An agent has backups of two configuration files. The first restore succeeds, but the disk has no room for the second file’s history entry. Freeing space does not unblock restore because the record still references the first file’s deleted baseline.
Recommended fix: Persist recoverable progress before deleting baseline files. Stage all required history entries before touching the live files, and cover a failure after the first replacement with a retry test.
Was this helpful? React with 👍 or 👎 to provide feedback.
| let previous = load(&directory); | ||
| let mut record = previous.clone().unwrap_or_default(); | ||
| let mut baseline = None; | ||
| if !record.files.contains_key(file_id) { | ||
| let path = directory.join(BASELINE_DIRECTORY).join(file_key(file_id)); | ||
| let _ = fs::remove_file(&path); |
There was a problem hiding this comment.
🔴 Unreadable records erase original backups
When the backup record is unreadable or invalid, take treats the next provider edit as the first. It deletes the existing baseline, losing the original recovery point.
Learn more
load returns the same value for a missing record and for a record that cannot be read or parsed. That is adequate for hiding unavailable status, but take uses it to decide whether any earlier baseline exists. The next apply deletes the baseline at the known file ID before attempting to store a replacement.
Example: The backup record becomes invalid while the original baseline remains intact. Changing the model again replaces the original baseline with the currently edited version. Restore can no longer recover the original.
Recommended fix: Distinguish missing records from read and validation errors. Fail apply with backupFailed on an unreadable or invalid record, preserving existing baselines and history.
Was this helpful? React with 👍 or 👎 to provide feedback.
| let written = real | ||
| .parent() | ||
| .map_or(Ok(()), ensure_directory) | ||
| .and_then(|()| write_replacing(&real, &edited).map_err(|_| write_failed())); |
There was a problem hiding this comment.
关联
改动
都是 API-45、
agents能力 0 内的补充,不升版本,没有新增插件可调用的宿主方法或通知。一、密钥只写不读
agentProviders.apply的addProvider/updateProvider可带key;updateProvider可带clearKey。两者不能同时给,都不给表示密钥不变。key为 1–1024 字节,不含控制字符和换行,否则agentProviders.invalidKey。上限按 Windows Credential Manager 单条 2560 字节留出余量。edit的参数里。Core 不写日志、不进错误信息和事件,类型的Debug输出打码(有测试)。agentProviders.read的每个供应商新增keySet(布尔)。没有任何字段能把密钥值读回来。二、由 Lithe 保存的供应商列表
configuration.providers.listKeptBy:cli(默认)或lithe。lithe用于配置里只能有一个生效的自定义接口的 CLI:用户保存的供应商列表由 Core 存在资源库目录的saved-providers.json(仅所有者可读,不含密钥),密钥由插件用已有的lithe/host/credentials/*存进自己的命名空间。baseUrl对上已保存项,ID 和名称取已保存项,其余以回读的配置为准。每项新增saved、origin(configuration/lithe)、可选model;每个 Agent 新增listKeptBy。lithe模式下:新增必须带baseUrl;接口地址与已保存项重复是agentProviders.endpointExists;删除当前生效的已保存项是agentProviders.activeProvider。三、修改前备份,可以恢复
apply替换配置文件前,Core 把原字节存到资源库目录的backups/<Agent 摘要>/:baseline/是自上次恢复以来第一次修改前的原样,后续修改不覆盖;history/留最近 20 份。备份失败则不修改(agentProviders.backupFailed)。backup {savedAtMs, modifiedSince}。agentProviders.restore {agent, discardLaterChanges?},只接受用户发起:文件在 Lithe 最后一次写入后被别的程序改过时以agentProviders.changedSinceApply拒绝,带discardLaterChanges才覆盖;恢复前把当前文件也存进历史;原本不存在的文件恢复时删除。agentProviders.noBackup、changedSinceApply、restoreFailed。日志
agent.providers.restore_applied/restore_failed,只记文件数、耗时和错误码。文档与契约
reference/domains/agents.md、capabilities-and-versions.md、api-catalog.md的 API-45 行、协议更新记录(新增 feat(agents): 供应商密钥只写不读,Lithe 保存供应商列表,修改前备份并可恢复 #249 一行)、四语 SDK README、rust/contracts/rust-core-api.md。agentProviders.*是宿主到 Core 的命令,不是插件调用的宿主方法,所以方法表和“宿主方法(按 API 编号)”索引没有加行,与 feat(agents): Agent 插件上报并修改模型与供应商,资源库读取支持筛选与排序 #248 一致。兼容
listKeptBy、key、clearKey当未知字段拒绝,不认识agentProviders.restore。listKeptBy的插件行为与 feat(agents): Agent 插件上报并修改模型与供应商,资源库读取支持筛选与排序 #248 相同,只是修改前多了备份。验证(macOS,本机)
cargo fmt --checkcargo test -p lithe-agent-runtime -p lithe-plugin-agents -p lithe-core -p lithe-diagnosticscargo clippy --workspace --all-targets --all-features -- -D warningsscripts/rust-core/verify-rust-core-comments.shnode shared/plugins/scripts/verify-docs.mjsnode shared/plugins/scripts/sdk-e2e.mjs未执行:整个 workspace 的测试只跑了上面四个 crate;Windows 上的文件权限与恢复没有实机验证。测试里的密钥只用合成字符串。