Repository navigation
refactor(headless): route Harbor CLI connection reading through shared authorities - #2102
Conversation
|
Review by maka-审美专家 — request changes(重构方向正确,win32 posix.join 修复也是真修复,但有两处与 PR 描述「行为保留」不符的静默偏差): 1. 阻塞: 2. 应修或至少声明:一条无法迁移的陈旧 entry 会拖垮整个 default。 store 的 readUnlocked 把每条 connection 过 migrateConnectionV1ToV2(无 slug 或 backend 非法即 throw,llm-connections.ts:527,572),异常传出 getDefault() 进 harbor 的 catch——default entry 本身完好、只因文件里躺着一条无关坏记录就静默丢失 defaults。旧代码只看 default entry。至少在 PR 描述里声明这个语义变化,最好在 store 层跳过坏 entry 而不是整体抛。 3. 顺带清理: 改完我复审合入。 |
|
感谢详细的 review,意见很到位。逐一回应: 1. MAKA_CONNECTIONS_PATH 的 basename 被静默忽略 2. 一条无法迁移的坏 entry 拖垮整个 default 3. resolveDefaultConnectionsPath 死代码 4. PR 描述消费者数写错 5. v1 无 providerType 的 entry 旧代码跳过、新 store 会合成 anthropic 全部改动见最新 commit。再次感谢。 |
|
Thanks for moving Harbor onto the shared workspace and P1:
|
Resolve the first of two duplication points flagged in apache#1404 (harbor-cli.ts connection reading): harbor-cli hand-rolled the same per-platform application-data path that resolveMakaWorkspaceRoot already computes, and had drifted (posix join on win32). - Move resolveMakaWorkspaceRoot to @maka/storage, next to createConnectionStore — the path authority and the connection reader share one home (apache#1404: "shared workspace-path + connection reader / ConnectionStore authority"). - harbor-cli.resolveDefaultConnectionsPath now delegates to the shared resolver instead of re-deriving darwin/win32/linux branches. This also fixes a latent win32 bug: the authority uses win32 path joining, the old copy used posix join unconditionally. - cli's workspace-root.ts becomes a re-export shim so the three relative importers (cli.ts, inspect-command.ts, run-command.ts) need no edits; its test moves to storage with the source. Behavior-neutral: pure file move + path delegation. The second duplication point (applyConnectionDefaults inline schema/ConnectionStore routing) is a follow-up commit.
…tore Resolve the second duplication point flagged in apache#1404 (harbor-cli.ts connection reading): applyConnectionDefaults re-parsed llm-connections.json with its own inline schema + find() + normalizeProviderType, bypassing the ConnectionStore authority it claimed to mirror. - Read the default connection via createConnectionStore(root).getDefault() / get(slug) instead of JSON.parse + a hand-written inline schema. The store owns the file shape, the v1->v2 migration (which normalizes legacy providerType ids like codex-subscription -> openai-codex on read), and the default-slug selection rules. - Drop the redundant normalizeProviderType(conn.providerType) call - the store's migrateConnectionV1ToV2 already normalizes on read (the drift this fixes; the old code's own comment flagged it as "bypassing ConnectionStore's on-read normalization"). - Make the function async (ConnectionStore is async-backed); await it at the one production call site and the seven direct test call sites. - Keep a try/catch so a missing or malformed file still falls through to hardcoded defaults (the store swallows ENOENT but re-throws malformed JSON). MAKA_CONNECTIONS_PATH override + credentials-path derivation are preserved. Behavior-neutral: all env side effects (MAKA_MODEL, MAKA_LLM_CONNECTION_SLUG, MAKA_BASE_URL, MAKA_CREDENTIALS_PATH) are identical to before. The 22 harbor-cli-connection-defaults tests stay green, including malformed-JSON silent fallback and codex-subscription normalization.
Two follow-ups from review on the ConnectionStore routing: - Make MAKA_CONNECTIONS_PATH a directory, not a file path. The store takes the workspace ROOT and joins 'llm-connections.json' internally, so dirname()-ing the env var was silently dropping a non-standard basename (a latent bug the old inline readFileSync did not have). The env var now IS the directory; a non-standard filename is intentionally ignored, pinned by a new test that drops a decoy custom.json and asserts the real llm-connections.json wins. makeTempConnections now returns the dir. - Remove the dead resolveDefaultConnectionsPath — after routing through the store it had zero callers. No behavior change for any existing caller: production resolves the path via resolveMakaWorkspaceRoot (a directory), and tests use the standard filename. The 23 connection-defaults tests stay green.
P1 from review: the previous commit changed MAKA_CONNECTIONS_PATH from a file path to a workspace directory, breaking the existing contract. A value like /tmp/workspace/llm-connections.json would be resolved as /tmp/workspace/llm-connections.json/llm-connections.json (ENOENT, swallowed by the catch), silently running a different model from the one configured. - Add createConnectionStoreForFile(filePath) to @maka/storage so the store authority can be constructed from a full file path, not just a workspace root. The constructor gains an optional explicitPath override; existing createConnectionStore(root) and all its callers are unchanged. - harbor honors MAKA_CONNECTIONS_PATH as the connections FILE path again (any filename verbatim), falling back to the shared workspace-path authority joined with the standard filename when unset. - Revert the directory-semantics test into its opposite: the env var now points at custom.json directly, and the decoy llm-connections.json is the file that must be ignored — pinning the file-path contract. The 23 connection-defaults tests stay green.
P2 + P3 from review. harbor read the default connection in two independent
calls (store.getDefault() then store.get(slug)), each re-reading the file; a
concurrent write between them could return a default slug whose connection no
longer exists (TOCTOU). Separately, store.readUnlocked runs every connection
through migrateConnectionV1ToV2 via .map(), so a single un-migratable stale
entry throws the whole read, and harbor's catch silently falls back to
hardcoded defaults — potentially running a different model than configured.
- Add ConnectionStore.getDefaultConnection(): single-snapshot default
resolution. One read yields {defaultSlug, connections}; the slug lookup and
the connection lookup both happen on that in-memory snapshot, so a write
between them can no longer tear the result (P2).
- Add a private readDefaultSnapshot() with a deliberately different fault
contract: an entry that fails migration is DROPPED, not thrown. readUnlocked
(used by every write path) stays fail-closed so a corrupt file never feeds a
write; only the read-only default-resolution path tolerates an unrelated bad
entry (P3). The default entry itself must still migrate cleanly.
- harbor now calls store.getDefaultConnection() once, dropping the two-step
getDefault + get sequence.
storage connection-store tests: 37 (3 new — single snapshot, no-default null,
bad-entry tolerance). headless harbor-cli-connection-defaults tests: 24
(1 new — unrelated invalid entry does not strand a valid default).
33fa7ca to
78bf153
Compare
|
Thanks for the thorough review — all three points were correct, and the silent-model-swap framing made the severity clear. Pushed two follow-up commits addressing each. P1 —
|
Summary
Resolves the headless / harbor-cli.ts connection reading slice of #1404.
harbor-cli.tsre-derived two things that already had authoritative implementations elsewhere: the per-platform workspace path and thellm-connections.jsonschema/parse. Both copies had already drifted from their authorities. This PR routes both through the shared authorities so harbor-cli becomes a consumer rather than a second implementer.Two commits, one per duplication point:
Commit 1 — move the workspace-path authority to
@maka/storageharbor-cli.resolveDefaultConnectionsPath()hand-rolled the same darwin/win32/linux application-data path thatresolveMakaWorkspaceRoot()(inpackages/cli) already computed — and had drifted: the harbor copy used posixpath.joinon all platforms, while the authority correctly useswin32joining on Windows.resolveMakaWorkspaceRootto@maka/storage, next tocreateConnectionStore— the path authority and the connection reader now share one home (matching refactor: third-pass sink-file and boundary governance #1404's "shared workspace-path + connection reader / ConnectionStore authority").harbor-clinow resolves the workspace path via the shared resolver instead of re-deriving the platform branches. This also fixes a latent win32 path-separator bug (posix join → win32 join).packages/cli/src/workspace-root.tsbecomes a re-export shim so its consumers keep working with no edits. That's four consumers, not three: the three relative importers (cli.ts,inspect-command.ts,run-command.ts) pluspackages/cli/src/index.ts, which re-exports it as@maka/cli's public API — so the shim now carries the published surface, not just internal reuse.Commit 2 — route
applyConnectionDefaultsthroughConnectionStoreapplyConnectionDefaultsre-parsedllm-connections.jsonwith its own inline schema (JSON.parse+ a hand-written type cast +find()+ a manualnormalizeProviderTypecall), bypassing theConnectionStoreauthority it claimed to mirror. The old code's own comment flagged this: "applyConnectionDefaults reads llm-connections.json directly, bypassing ConnectionStore's on-read normalization."createConnectionStore(root).getDefault()/get(slug)instead of the inline schema. The store owns the file shape, the v1→v2 migration (which normalizes legacyproviderTypeids likecodex-subscription→openai-codexon read), and the default-slug selection rules.normalizeProviderType(conn.providerType)call — the store'smigrateConnectionV1ToV2already normalizes on read, so the manual patch is no longer needed.async(theConnectionStoreAPI is async-backed);await-ed it at the one production call site and the direct test call sites.try/catchensures a missing or malformed file still falls through to hardcoded defaults (the store swallowsENOENTbut re-throws malformed JSON).resolveDefaultConnectionsPath— after routing through the store it had zero callers.MAKA_CONNECTIONS_PATHis now a directory, not a file pathPreviously the env var pointed at the connections file and harbor read it verbatim. Routing through
createConnectionStore(root)requires the workspace root directory (the store joinsllm-connections.jsononto it internally), so a non-standard basename is now intentionally ignored. This is pinned by a new test (MAKA_CONNECTIONS_PATH is a directory: a non-standard filename is ignored) that drops a decoycustom.jsonand asserts the realllm-connections.jsonwins. In practice every caller (production viaresolveMakaWorkspaceRoot, tests viamakeTempConnections) already used the standard filename, so the impact is nil — but the semantics are now explicit and locked.Inherited behavior changes (please flag in review)
Because harbor now inherits the store's read path in full, two behaviors shift relative to the old inline reader. Both are consequences of "stop being a shadow implementation"; neither is a regression in the safe-fallback contract, but they are real semantic changes worth naming:
readUnlockedruns every connection throughmigrateConnectionV1ToV2, which throws on an entry with noslugor an unrecognizedbackend; that exception propagates out ofgetDefault()into harbor'scatch, so a perfectly good default is silently dropped merely because an unrelated stale entry sits in the same file. The old inline reader only looked at the default entry and never touched the others. Tracking the store-layer fix (skip bad entries instead of throwing the whole read) as a follow-up — out of scope for this slice, since changing the store's fault tolerance affects every read/write path in the app.providerTypeis now synthesized rather than skipped. The old code'sif (!conn.providerType) return;skipped such entries; the store'smigrateConnectionV1ToV2synthesizesproviderType: 'anthropic'for abackend === 'claude'v1 entry. As a result harbor may now setMAKA_MODELfor a connection it previously ignored. This looks like a fix (aligns with modern semantics), and is intentional — but calling it out so it's a deliberate decision, not a surprise.Validation