Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
@@ -0,0 +1 @@
{"file": ".trellis/spec/cli/unit-test/index.md", "reason": "verify test conventions and minimal mocking"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Design: getConfiguredPlatforms 增加磁盘存在校验

## Problem

`getConfiguredPlatforms(cwd)`(`packages/cli/src/configurators/index.ts:138`)的判定条件:

```ts
const hasTrackedTemplate = [...(templates?.keys() ?? [])].some(
(relativePath) =>
(relativePath === configDir ||
relativePath.startsWith(`${configDir}/`)) &&
hashes[relativePath] !== undefined, // ← 只看 hash,不看磁盘
);
```

hash manifest 是"曾经写过"的记录,不是"现在存在"的事实。平台目录被删除(换机 clone 未提交、`.gitignore` 忽略如 `.cursor/`、手动清理)后,`init --<platform>` re-init 路径(`commands/init.ts:880`)误判 skipping,`platforms`(`commands/platforms` 经同一函数)虚报,`update`(`commands/update.ts:693,870`)对不存在的目录做收集。

## Decision

在 `hasTrackedTemplate` 为真之外,叠加一个条件:**`configDir` 在磁盘上存在**。

```ts
if (hasTrackedTemplate && fs.existsSync(path.join(cwd, configDir))) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
platforms.add(id);
}
```

### Why configDir existence (而不是逐文件校验)

1. **便宜**:每平台一次 `stat`,O(platforms);逐文件是 O(files),hash 有 560 条。
2. **与检测键对齐**:现有检测只统计 `configDir` 前缀下的跟踪路径,`configDir` 就是判定锚点;目录缺失是最常见、最完整的"未配置"形态(本次复现即 7 个目录整体缺失)。
3. **有先例**:同一函数内的 legacy Windsurf→devin 分支就是"hash + 磁盘(readdir trellis-*)"双条件。
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
4. **部分缺失归 `update` 管**:单个文件被删属于内容漂移,应由 `update` 的 diff/修复逻辑处理,不在本函数职责内(Non-Goals)。

### Considered alternative: 要求 ≥1 个被跟踪文件存在于磁盘

更精确,能覆盖"目录在但文件被掏空"的部分缺失;但引入 O(files) 的 I/O,且"部分缺失算不算已配置"本身是产品语义问题(`init` 跳过 vs `update` 修复),不宜在这次顺手定。保持最小修复。

## Blast radius

`getConfiguredPlatforms` 的消费方(同函数语义自动跟随,无需逐个改):

| Caller | 修复后的行为变化 |
|---|---|
| `commands/init.ts:783` re-init / `:840,:870` 未配置列表 | 目录缺失的平台重新出现在可添加列表,`--<platform>` 不再误 skipping ✅ 目标修复 |
| `commands/platforms`(经 `configurators/index`)/ `cli/index.ts:364` | 不再虚报已删除的平台 |
| `commands/update.ts:693,870` | 不再对磁盘缺失的平台做模板收集(之前是对着不存在的目录算 diff,实际无输出) |
| `commands/uninstall.ts:207` | 已删除的平台不再出现在可卸载列表(删本来就不存在的东西无意义) |
| `commands/ablate.ts:381` | 同上跟随 |

无反向风险:`configDir` 存在 + hash 命中的平台行为与之前完全一致;原生目录(无 hash)仍不算已配置,#501 语义保留。

## Edge cases

- 嵌套 `configDir`(kiro `.kiro/skills`、snow `.snow/skills`):`existsSync(join(cwd, configDir))` 对嵌套路径同样有效;父目录在但 `skills/` 被删 → 判未配置 → 重装,符合预期。
- `extraManagedPaths` / 共享 `.agents/skills`:检测键本来就不含它们(#501 已排除),本次不碰。
- manifest 缺失/损坏:`loadHashes` 返回空 → 本来就是空集,`existsSync` 短路在 `hasTrackedTemplate` 之后,无新增 I/O 失败面(`existsSync` 不抛异常)。

## Tests

在 `packages/cli/test/configurators/platforms.test.ts` 的 `getConfiguredPlatforms` describe 内新增:

1. `does not report a platform whose tracked directory was deleted` —— 为某平台生成 hash(复用现有 `configurePlatform` + `initializeHashes` 写法),然后 `rmSync(configDir)`,断言不再包含该平台(且其它平台不受影响可选)。
2. 现有用例即回归网:`detects every platform from the files Trellis tracked`(存在+hash → 检测到)、`does not treat native platform directories`(存在+无 hash → 不检测)保持全绿。

验证命令:`pnpm test test/configurators/platforms.test.ts`,另跑 `pnpm lint` + `pnpm typecheck`。
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"file": ".trellis/spec/cli/backend/index.md", "reason": "CLI backend command/configurator conventions for init + platform detection"}
{"file": ".trellis/spec/cli/unit-test/index.md", "reason": "vitest conventions for the regression test in platforms.test.ts"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 修复 getConfiguredPlatforms 误判:hash 有记录但磁盘文件缺失时 init --opencode 误报 skipping

## Goal

`getConfiguredPlatforms()` 增加磁盘存在校验:当 `.trellis/.template-hashes.json` 有某平台的跟踪记录、但该平台配置目录在磁盘上已缺失时,不再将其判定为"已配置"。`init --<platform>`、`platforms`、`update` 等消费方行为保持一致。

## Background

线上复现(WanLingBot 仓库):`.template-hashes.json` 跟踪 560 个文件(含 `.opencode` 57 条),但磁盘上 `.opencode/` 等 7 个平台目录全部缺失(仅 `.agent` 存在)。此时 `trellis init --opencode` 走 re-init 快捷路径,命中 `configuredPlatforms.has("opencode")`,直接输出 `○ OpenCode already configured, skipping`,什么文件都没写。`trellis platforms` 同样虚报 8 个已配置。

根因:`packages/cli/src/configurators/index.ts:getConfiguredPlatforms` 只检查 hash manifest,不检查磁盘。

## Requirements

- [ ] R1: `getConfiguredPlatforms(cwd)` 对每个平台,在 hash 命中之外,额外要求该平台 `configDir` 在磁盘上存在(`fs.existsSync(path.join(cwd, configDir))`),两者同时满足才算已配置。
- [ ] R2: hash 有记录 + 目录缺失 → 判定为未配置,`init --opencode` 正常走配置流程重写文件;`platforms` 不再虚报。
- [ ] R3: 现有语义不变:原生(非 Trellis 写入)的平台目录仍不算已配置(#501 的修复保持);hash 无记录仍不算已配置;legacy Windsurf→devin 分支逻辑不变。
- [ ] R4: 回归测试覆盖:hash 有记录但目录被删除 → 未配置;正常配置 → 仍检测到。

## Acceptance Criteria

- [ ] A1: 复现场景验证通过:在 tmp 目录为某平台生成 hash 后删除其配置目录,`getConfiguredPlatforms` 不再包含该平台。
- [ ] A2: `pnpm test test/configurators/platforms.test.ts` 全绿;`pnpm lint`、`pnpm typecheck` 通过。
- [ ] A3: 手动验证:`trellis init --opencode` 在目录缺失+hash 残留的项目里会真实写文件(而非 skipping)。
- [ ] A4: 独立分支 + PR 至远程。

## Non-Goals

- 不做缺失文件的"修复/补写"模式(`update` 的 drift 修复另议);本次只修"是否已配置"的判定。
- 不清理残留的 stale hash 条目;重写配置时的 hash 合并逻辑保持现状。
- 不改 `uninstall`/`ablate` 的删除逻辑(它们消费同一函数,语义自动跟随)。

## Notes

- Keep `prd.md` focused on requirements, constraints, and acceptance criteria.
- 设计见 `design.md`。
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id": "fix-configured-platforms-disk-check",
"name": "fix-configured-platforms-disk-check",
"title": "修复 getConfiguredPlatforms 误判:hash 有记录但磁盘文件缺失时 init --opencode 误报 skipping",
"description": "getConfiguredPlatforms 增加磁盘存在校验,hash 有记录但平台目录缺失时不再误判为已配置,init/platforms/update 行为一致",
"status": "completed",
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
"dev_type": null,
"scope": null,
"package": "cli",
"priority": "P2",
"creator": "alan",
"assignee": "alan",
"createdAt": "2026-09-03",
"completedAt": "2026-09-03",
"branch": "feat/antigravity-hooks",
"base_branch": "main",
"worktree_path": null,
"commit": null,
"pr_url": null,
"subtasks": [],
"children": [],
"parent": null,
"relatedFiles": [],
"notes": "",
"meta": {}
}
42 changes: 42 additions & 0 deletions .trellis/workspace/alan/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Workspace Index - alan

> Journal tracking for AI development sessions.

---

## Current Status

<!-- @@@auto:current-status -->
- **Active File**: `journal-1.md`
- **Total Sessions**: 2
- **Last Active**: 2026-09-03
<!-- @@@/auto:current-status -->

---

## Active Documents

<!-- @@@auto:active-documents -->
| File | Lines | Status |
|------|-------|--------|
| `journal-1.md` | ~70 | Active |
<!-- @@@/auto:active-documents -->

---

## Session History

<!-- @@@auto:session-history -->
| # | Date | Title | Commits | Branch |
|---|------|-------|---------|--------|
| 2 | 2026-09-03 | 修复 getConfiguredPlatforms 误判导致 init --opencode 空跳过 | `7cba6373` | `fix/configured-platforms-disk-check` |
| 1 | 2026-09-03 | feat(antigravity): 支持 hooks.json 生命周期钩子与会话状态注入 | `cf6663d3`, `d64de828` | `feat/antigravity-hooks` |
<!-- @@@/auto:session-history -->

---

## Notes

- Sessions are appended to journal files
- New journal file created when current exceeds 2000 lines
- Use `add_session.py` to record sessions
70 changes: 70 additions & 0 deletions .trellis/workspace/alan/journal-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Journal - alan (Part 1)

> AI development session journal
> Started: 2026-09-03

---



## Session 1: feat(antigravity): 支持 hooks.json 生命周期钩子与会话状态注入
<!-- trellis-session: v=2 fp=c6e3211153bb0cbe -->

**Date**: 2026-09-03
**Task**: feat(antigravity): 支持 hooks.json 生命周期钩子与会话状态注入
**Package**: cli
**Branch**: `feat/antigravity-hooks`

### Summary

为 Antigravity 平台添加 lifecycle hooks 支持,包含工作流状态注入和 shell 会话桥接

### Main Changes

- 在 packages/cli 中添加 Antigravity hooks.json 模板与配置器
- 适配 inject-workflow-state.py 与 inject-shell-session-context.py 支持 Antigravity 契约
- 修正 hooks.json 相对路径以适配 Antigravity 工作目录机制

### Git Commits

| Hash | Message |
|------|---------|
| `cf6663d3` | feat(antigravity): 支持 hooks.json 生命周期钩子与会话状态注入 (#599) |
| `d64de828` | fix(antigravity): 修正 hook 执行相对路径与宿主目录解析 (#599) |

### Testing

- [OK] Core 测试全过 (372/372)
- [OK] CLI 全量测试通过 (1900/1900)
- [OK] 新增 Antigravity 集成测试通过

### Status

[OK] **Completed**

### Next Steps

- 等待上游 PR #600 审核合并


## Session 2: 修复 getConfiguredPlatforms 误判导致 init --opencode 空跳过
<!-- trellis-session: v=2 fp=b818a0ab242583af -->

**Date**: 2026-09-03
**Task**: 修复 getConfiguredPlatforms 误判导致 init --opencode 空跳过
**Package**: cli
**Branch**: `fix/configured-platforms-disk-check`

### Summary

getConfiguredPlatforms 叠加 configDir 磁盘存在校验,hash 残留但目录缺失时不再误判已配置;回归测试+lint+typecheck+端到端验证通过,PR #601

### Git Commits

| Hash | Message |
|------|---------|
| `7cba6373` | fix(cli): getConfiguredPlatforms 增加磁盘存在校验 |

### Status

[OK] **Completed**
37 changes: 32 additions & 5 deletions packages/cli/src/configurators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ export const ALL_MANAGED_DIRS = [".trellis", ...new Set(PLATFORM_MANAGED_DIRS)];
* A platform directory may predate Trellis. The template hash manifest records
* only files Trellis actually wrote, while the platform template registry
* supplies each platform's distinct file layout.
*
* Both conditions must hold: a tracked hash entry under the platform's
* configDir AND the configDir actually existing on disk as a directory. The
* manifest alone is a record of what was once written — the directory may
* since have been deleted (fresh clone without committed platform files,
* gitignored dirs such as `.cursor/`, manual cleanup). Without the disk
* check, `trellis init --<platform>` mis-reports "already configured,
* skipping" and writes nothing.
*/
export function getConfiguredPlatforms(cwd: string): Set<AITool> {
const platforms = new Set<AITool>();
Expand All @@ -148,7 +156,7 @@ export function getConfiguredPlatforms(cwd: string): Set<AITool> {
relativePath.startsWith(`${configDir}/`)) &&
hashes[relativePath] !== undefined,
);
if (hasTrackedTemplate) {
if (hasTrackedTemplate && isDirectoryOnDisk(cwd, configDir)) {
platforms.add(id);
}
}
Expand All @@ -158,20 +166,39 @@ export function getConfiguredPlatforms(cwd: string): Set<AITool> {
const hasTrackedWindsurfTemplate = Object.keys(hashes).some((relativePath) =>
relativePath.startsWith(`${legacyWindsurfRoot}/trellis-`),
);
let legacyWindsurfRootExists = false;
let hasLegacyWindsurfTemplate = false;
try {
hasLegacyWindsurfTemplate = fs
.readdirSync(path.join(cwd, legacyWindsurfRoot))
.some((name) => name.startsWith("trellis-"));
const entries = fs.readdirSync(path.join(cwd, legacyWindsurfRoot));
legacyWindsurfRootExists = true;
hasLegacyWindsurfTemplate = entries.some((name) =>
name.startsWith("trellis-"),
);
} catch {
// Missing or unreadable legacy directory is not a configured platform.
}
if (hasTrackedWindsurfTemplate || hasLegacyWindsurfTemplate) {
if (
(hasTrackedWindsurfTemplate && legacyWindsurfRootExists) ||
hasLegacyWindsurfTemplate
) {
platforms.add("devin");
}
return platforms;
}

/**
* True when `relDir` exists under `cwd` and is a directory. Missing paths,
* regular files, and unreadable paths all report false instead of throwing,
* so none of them can read as a configured platform.
*/
function isDirectoryOnDisk(cwd: string, relDir: string): boolean {
try {
return fs.statSync(path.join(cwd, relDir)).isDirectory();
Comment thread
wang93wei marked this conversation as resolved.
Resolved
} catch {
return false;
}
}

/**
* Get platform IDs that have Python hooks (for Windows encoding detection)
*/
Expand Down
67 changes: 67 additions & 0 deletions packages/cli/test/configurators/platforms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,73 @@ describe("getConfiguredPlatforms", () => {
}
});

it("does not report a platform whose tracked directory was deleted", async () => {
// Regression: the hash manifest alone is not proof of installation. When
// the platform directory is gone (fresh clone without committed platform
// files, gitignored dir, manual cleanup), the platform must read as
// unconfigured so `init --<platform>` rewrites it instead of printing
// "already configured, skipping".
const platformRoot = path.join(tmpDir, "stale-hash");
fs.mkdirSync(platformRoot, { recursive: true });
const written = startRecordingWrites(platformRoot);
try {
await configurePlatform("opencode", platformRoot);
} finally {
stopRecordingWrites();
}
fs.mkdirSync(path.join(platformRoot, ".trellis"), { recursive: true });
initializeHashes(platformRoot, { trackedPaths: written });
expect(getConfiguredPlatforms(platformRoot).has("opencode")).toBe(true);

fs.rmSync(path.join(platformRoot, AI_TOOLS.opencode.configDir), {
recursive: true,
force: true,
});
expect(getConfiguredPlatforms(platformRoot).has("opencode")).toBe(false);
});

it("does not report a platform whose configDir path is a regular file", async () => {
const platformRoot = path.join(tmpDir, "file-not-dir");
fs.mkdirSync(platformRoot, { recursive: true });
const written = startRecordingWrites(platformRoot);
try {
await configurePlatform("opencode", platformRoot);
} finally {
stopRecordingWrites();
}
fs.mkdirSync(path.join(platformRoot, ".trellis"), { recursive: true });
initializeHashes(platformRoot, { trackedPaths: written });
expect(getConfiguredPlatforms(platformRoot).has("opencode")).toBe(true);

fs.rmSync(path.join(platformRoot, AI_TOOLS.opencode.configDir), {
recursive: true,
force: true,
});
fs.writeFileSync(
path.join(platformRoot, AI_TOOLS.opencode.configDir),
"not a directory",
);
expect(getConfiguredPlatforms(platformRoot).has("opencode")).toBe(false);
});

it("does not report devin from stale legacy Windsurf hashes alone", () => {
const platformRoot = path.join(tmpDir, "stale-windsurf");
const workflowsDir = path.join(platformRoot, ".windsurf", "workflows");
fs.mkdirSync(workflowsDir, { recursive: true });
fs.writeFileSync(path.join(workflowsDir, "trellis-continue.md"), "# Trellis");
fs.mkdirSync(path.join(platformRoot, ".trellis"), { recursive: true });
initializeHashes(platformRoot, {
trackedPaths: [".windsurf/workflows/trellis-continue.md"],
});
expect(getConfiguredPlatforms(platformRoot).has("devin")).toBe(true);

fs.rmSync(path.join(platformRoot, ".windsurf"), {
recursive: true,
force: true,
});
expect(getConfiguredPlatforms(platformRoot).has("devin")).toBe(false);
});

it("ignores unrelated directories", () => {
fs.mkdirSync(path.join(tmpDir, ".vscode"));
fs.mkdirSync(path.join(tmpDir, ".git"));
Expand Down