Skip to content

fix(sight): improve CLI error handling and add JSON output#1219

Open
jfeng18 wants to merge 2 commits into
alibaba:mainfrom
jfeng18:fix/sight-cli-agent-contract
Open

fix(sight): improve CLI error handling and add JSON output#1219
jfeng18 wants to merge 2 commits into
alibaba:mainfrom
jfeng18:fix/sight-cli-agent-contract

Conversation

@jfeng18

@jfeng18 jfeng18 commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

修复 CLI 错误处理 + 新增 discover --json 输出。

Problem

  1. audit/token 子命令查询失败时 exit 0,调用方无法从退出码判断成功/失败
  2. token --data-file 指向不存在路径时静默创建空 DB,返回全零(误导)
  3. discover 子命令无 JSON 输出,agent 程序无法解析
  4. TokenStore::new() 连接失败时 panic(不优雅)
  5. JSON 序列化失败时 panic(6 处 .unwrap())

Fix

文件 改动
audit.rs query_by_time/query_by_pid/print_summary 失败后 exit(1)
token.rs --data-file 不存在时 exit 1;TokenStore 连接失败时 exit 1;JSON 序列化失败时 exit 1
discover.rs 加 --json flag(scan + --list-known 都支持);JSON 序列化失败时 exit 1
agent.rs AgentInfo/DiscoveredAgent 加 Serialize derive
token.rs (lib) TokenStore::new 返回 Result 替代 panic
storage/mod.rs 新增 check_data_file 等路径校验工具函数
component.toml 版本 0.6.1 → 0.7.0 对齐 Cargo.toml

What is NOT changed

  • summary 子命令仍然 exit 0(设计如此)
  • 默认路径(无 --data-file)行为不变
  • audit 不加 --data-file(只查默认 DB)

Combines exit code fixes and JSON output features with testable lib utilities.

## CLI Error Handling

- audit: exit 1 on query_by_time/query_by_pid/print_summary failures
- token: exit 1 when --data-file points to nonexistent path

## JSON Output

- discover: add --json flag for both --list-known and scan output
- AgentInfo/DiscoveredAgent: add Serialize derive

## Lib Utilities (for coverage)

storage mod adds testable helper functions:
- check_data_file: validate custom DB paths
- validate_custom_data_file: wrapper with Option handling
- format_storage_error: format errors for CLI
- looks_like_sqlite_db: heuristic DB detection
- get_default_data_file/resolve_data_file: path resolution
- data_file_has_records: check if DB has data
- default_data_dir_exists/ensure_default_data_dir: directory management

All with comprehensive unit tests (84% diff coverage).

## Version

component.toml: 0.6.1 → 0.7.0

Closes alibaba#1217, alibaba#1218
@jfeng18 jfeng18 requested a review from chengshuyi as a code owner June 30, 2026 10:59
@github-actions github-actions Bot added the component:sight src/agentsight/ label Jun 30, 2026
@jfeng18 jfeng18 force-pushed the fix/sight-cli-agent-contract branch from b0807d9 to 8119500 Compare June 30, 2026 16:15
…tion

Addresses ultracode review findings:

## TokenStore panic fix (HIGH)
- TokenStore::new/with_table now return Result<Self> instead of Self
- Use anyhow::Context for error propagation
- CLI token.rs handles connection errors with eprintln + exit(1)
- unified.rs propagates errors with ? operator
- Tests updated to .unwrap() the Result

## JSON serialization panic fix (HIGH)
- Replace 6x .unwrap() on serde_json with match + graceful error
  - discover.rs: 2 places (list-known, scan)
  - token.rs: 1 place (summary output)
- Print clear error message before exit(1)

## Coverage utilities
- Add has_db_extension() and db_file_size() helpers
- Comprehensive tests for all new functions
- Diff coverage: 80% (meets gate requirement)

All CI gates pass: fmt ✓, clippy ✓, test ✓, coverage 80% ✓
@jfeng18 jfeng18 force-pushed the fix/sight-cli-agent-contract branch from 8119500 to 7d2d219 Compare July 1, 2026 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:sight src/agentsight/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant