@leviyuan/lodestar 0.21.15 → 0.21.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dsh-bridge.js +4 -4
- package/dist/lodestar-agent.js +50 -50
- package/dist/lodestar-claude.js +18 -18
- package/dist/lodestar-setup.js +53 -53
- package/dist/lodestar-stop.js +3 -3
- package/dist/lodestar-update.js +49 -49
- package/dist/lodestar-version.js +12 -12
- package/dist/lodestar.js +251 -257
- package/docs/claude-agent-backend.md +1 -1
- package/docs/configuration.md +5 -3
- package/docs/usage.md +9 -10
- package/package.json +16 -11
- package/scripts/postinstall.cjs +4 -2
- package/src/codex-plugin-cache.cjs +188 -0
- package/src/data-dir.cjs +5 -1
|
@@ -78,7 +78,7 @@ Flash 的请求按普通对话组织:固定的简短 system 提示词,按顺
|
|
|
78
78
|
|
|
79
79
|
翻译上下文是 Lodestar 自行记录的输入输出历史,按工作目录和 Claude session id 隔离,不读取 Claude Code 的完整上下文或原生 transcript。记录用户已提交的原文及英文译文,以及经验证用于飞书展示的 Claude 自然语言原文和中文译文;尚未消费的输入单独标记,取消后移除。宿主注入规则、内部用户或系统消息、主会话未展示的问答及任务回执、思考内容、文件内容和原始工具数据不进入这份历史;子 Agent 卡片实际展示的回执仍翻译并记录。
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
翻译调用不额外保存请求、响应或原始正文调试日志。调用失败按统一日志规则记录不含用户身份的故障摘要;原始错误继续反馈给对应调用方。翻译上下文用于术语一致性、续跑和分叉,仍按上述规则保存。
|
|
82
82
|
|
|
83
83
|
续跑恢复本地保存的翻译历史;分叉只继承指定 checkpoint 及以前的记录,再绑定新 session id。私有记录使用 v2 的独立原文和译文字段;v1 仅按已知且无歧义的双语包装格式迁移,不猜测、不读取原生历史重建。旧会话尚无本地翻译记录时,从本次启用开始积累。已有记录损坏、归属不符、旧格式歧义或分叉锚点缺失时明确报错。
|
|
84
84
|
|
package/docs/configuration.md
CHANGED
|
@@ -57,12 +57,14 @@ Codex、Claude、DSH 在 `[runtime.agent_auto_update]` 下分别设置 `codex`
|
|
|
57
57
|
|
|
58
58
|
清单还包括原生附件的 `im:resource`、机器人发消息的 `im:message:send_as_bot`、Card Kit 权限,以及排障时读取租户和认证信息的权限。需要审批的权限应完成审批,再创建并发布飞书应用版本。取得应用访问凭据不代表业务权限已生效。
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
升级已有应用时也应补齐清单并发布应用版本。文件按大小自动交付:不超过 30 MB 发聊天附件,超过 30 MB 传云空间,无需切换命令。大文件交付缺少云空间权限时会明确报错。文件夹权限与实际文件的分享入口会分别处理,具体行为见[文件与生图](usage.md#文件与生图)。
|
|
61
61
|
|
|
62
62
|
## 本机配置
|
|
63
63
|
|
|
64
64
|
默认配置文件是 `~/.config/lodestar/config.toml`,可通过 `LODESTAR_CONFIG` 指定文件。日志和会话状态位于 `~/.local/share/lodestar/`;Windows 使用相应的应用数据目录。完整路径定义见 [src/paths.ts](../src/paths.ts)。
|
|
65
65
|
|
|
66
|
+
运行日志按日保存,保留近 7 天。正常流程仅记录关键状态;不记录协议大包、工具输出、代码 diff、请求/响应正文或内部阶段流水。日志省略用户、群、项目和账号名称、路径、聊天/消息/会话/任务标识,以及人数和群数,不生成可关联的匿名编号。故障记录保留固定操作说明、错误类别、错误码和 HTTP 状态;相同摘要在 10 分钟内合并,不显示重复次数。用户收到的错误详情不因此省略。会话历史、任务结果和恢复绑定是功能状态,仍由相应模块保存。
|
|
67
|
+
|
|
66
68
|
```toml
|
|
67
69
|
[runtime]
|
|
68
70
|
projects_root = "/abs/projects"
|
|
@@ -91,9 +93,9 @@ bin = "/abs/path/to/claude-wrapper" # 可选的 Claude 可执行文件
|
|
|
91
93
|
|
|
92
94
|
`[claude].translation_validation_rules` 指定可选的输入审查规则;未配置或留空时只做忠实翻译,不推断身份、地区或账号风险。启用安全模式的会话复用本机 GLM Key,通过国际 Coding Plan `https://api.z.ai/api/coding/paas/v4/chat/completions` 调用 `glm-5.3-flash`。拒绝或调用失败会明确报错,不换模型或放行原文。
|
|
93
95
|
|
|
94
|
-
`[claude.safe_mode]` 按 Token Source 与模型目录中的 ID 精确匹配,只作用于 Claude Code。`"*"` 表示该来源全部模型,`"GLM-5.3,GLM-5.2"` 表示指定模型,`""` 表示关闭该来源;所有来源(包括 `claude-sub`)默认关闭,只有显式配置的来源和模型才会开启。主会话、委派、续跑与分叉共用此配置。同来源切模型若跨越安全模式边界,只能在空闲时换进程并保留恢复点;同模式仍沿用原热切换。修改本机配置后重启 daemon
|
|
96
|
+
`[claude.safe_mode]` 按 Token Source 与模型目录中的 ID 精确匹配,只作用于 Claude Code。`"*"` 表示该来源全部模型,`"GLM-5.3,GLM-5.2"` 表示指定模型,`""` 表示关闭该来源;所有来源(包括 `claude-sub`)默认关闭,只有显式配置的来源和模型才会开启。主会话、委派、续跑与分叉共用此配置。同来源切模型若跨越安全模式边界,只能在空闲时换进程并保留恢复点;同模式仍沿用原热切换。修改本机配置后重启 daemon 生效。
|
|
95
97
|
|
|
96
|
-
上下文自动按 Claude
|
|
98
|
+
上下文自动按 Claude 会话保存,只包含带来源说明的可见原文与译文,不读取原生完整上下文。用户输入译为英文,已有的飞书自然语言展示译为中文;主模型输出语言不限,代码、文件与指令保持原样。不额外保存翻译请求/响应调试日志;错误只记录不含身份的故障摘要。完整边界见 [Claude 启动配置](claude-agent-backend.md#claude-启动配置)。
|
|
97
99
|
|
|
98
100
|
项目名用于定位工作目录;工具、MCP 等启动配置按实际目录匹配。同目录的 BTW/FK 与项目别名共享配置,别名配置冲突时明确报错。WT 的目录独立,不自动套用主目录的启动配置;需要配置某个 WT 时,可新增一个项目配置项,将 `cwd` 指向该 WT 的实际路径。工作目录内的 Agent 原生配置仍由相应后端读取。
|
|
99
101
|
|
package/docs/usage.md
CHANGED
|
@@ -28,9 +28,6 @@
|
|
|
28
28
|
| `codex-account-delete 备注` | — | 删除额外账号的本地记录与独立凭据,清除各群对它的指定 |
|
|
29
29
|
| `codex-auto` | — | 清除本群手动指定,恢复按优先级与额度评分自动选号 |
|
|
30
30
|
| `task` | — | 创建、查看或删除绑定的飞书任务清单 |
|
|
31
|
-
| `files` | — | 查看当前工作目录的交付模式及本群文件夹入口 |
|
|
32
|
-
| `files on` | — | 开启当前工作目录的云空间交付,创建或复用本群文件夹 |
|
|
33
|
-
| `files off` | — | 当前工作目录恢复聊天附件,保留云空间文件和群目录绑定 |
|
|
34
31
|
|
|
35
32
|
模型列表来自各账号的模型目录,获取失败显示 `MISS`。同账号切换 Claude 或 DSH 模型从后续回复生效,Codex 的持久模型设置需重启会话生效;跨账号或后端切换只允许在空闲时进行。账号接入、模型补录与额度说明见[账号与模型](models.md)。
|
|
36
33
|
|
|
@@ -40,6 +37,8 @@ Codex 和 Claude 执行任务时,可以直接发消息补充要求或纠正方
|
|
|
40
37
|
|
|
41
38
|
默认账号沿用设备上直接执行 `codex` 的登录态:使用 daemon 原始环境中的 `CODEX_HOME`,未设置时为 `~/.codex`。`codex-account` 只改变当前群后续 Codex 进程的认证选择,不覆盖默认账号,也不改变其他群。额外账号的备注名可包含中文和空格,不能包含换行或路径分隔符;`default`、`默认` 为保留名称。
|
|
42
39
|
|
|
40
|
+
每个 Codex 账号的 `plugins` 目录及插件缓存独立,避免不同安装清单在同步时互相删包;会话历史、Skill 和公共配置仍按原规则共享。安装或更新 Lodestar 时会自动把旧的共享插件链接迁为独立副本,保留默认缓存、各账号登录状态和安装清单,无需重新登录。跳过 npm 安装脚本或直接使用源码时,额外账号下次启动前也会自动迁移。安装命令须使用与 daemon 相同的 `LODESTAR_DATA_DIR` / `XDG_DATA_HOME` 和自定义 `CODEX_HOME`;迁移失败会明确报错。安装不会重启 daemon;仍在运行的旧版会把独立插件目录视为共享路径异常,升级后须按正常流程重启 daemon,再新建或切换额外账号会话。
|
|
41
|
+
|
|
43
42
|
发送 `codex-reset` 直接为当前运行的 Codex 账号使用一次重置卡;`codex-reset 工作` 为指定账号使用,`codex-reset default` 为设备默认账号使用。当前没有运行的 Codex 账号或正在等待选号时,必须带备注。命令不会切换账号或重启会话,额度不足时也不会自动使用重置卡。回执区分已重置、本次请求此前已完成、没有可重置窗口、没有可用卡,并重新读取剩余卡数和额度;查询失败显示 `MISS`,已确认的扣卡结果仍保留。同一消息的网络重试复用幂等标识,重新发送命令则是新的一次使用请求。接口失败或结果未知时,先用 `hi` 核对卡数、`codex-accounts` 核对额度。
|
|
44
43
|
|
|
45
44
|
额外账号不再使用时,发送 `codex-account-delete 工作`。删除会移除该账号的本地记录、独立认证目录和额度缓存,并清除所有群对它的指定,这些群下次启动恢复自动选号;共享会话历史、配置和其他账号保留。正在被会话、委派任务、额度或模型查询使用的账号不能删除;登录中的账号先用 `codex-login-cancel 工作` 取消。备注必填,设备默认账号不能通过此命令删除。需要重新添加时使用 `codex-login 工作`;本地删除不会注销 OpenAI 账号或取消订阅。
|
|
@@ -83,7 +82,7 @@ Linux/macOS 下,额外账号通过软链接共用默认账号的配置、会
|
|
|
83
82
|
| `worktree` | `wt` | 列出项目的 `work/*` 分支和工作区状态 |
|
|
84
83
|
| `worktree feature-x` | `wt feature-x` | 创建或加入 `<project>[feature-x]` 群和同级 worktree 目录,使用 `work/feature-x` 分支 |
|
|
85
84
|
|
|
86
|
-
WT
|
|
85
|
+
WT 使用独立工作目录,其项目工具/MCP 配置与主目录分开。BTW/FK 始终共享来源目录的这些配置,包括从 WT 创建的临时群。文件交付统一按大小自动选择,云空间目录各群独立。任务清单属于项目级,主群、所有 WT 及其临时群共用。临时群的模型、账号在创建时继承,之后各自切换、互不影响。
|
|
87
86
|
|
|
88
87
|
已合并且未挂载的分支会折叠隐藏,再次启用时更新到主线。卡片上的“删”会检查对应群没有运行中的会话、工作区没有未提交变更,再解散群并删除 worktree;Git 分支保留。
|
|
89
88
|
|
|
@@ -199,22 +198,22 @@ curl -sS -X POST http://127.0.0.1:9876/notify \
|
|
|
199
198
|
|
|
200
199
|
## 文件与生图
|
|
201
200
|
|
|
202
|
-
Agent 用 `[[send: /abs/path]]`
|
|
201
|
+
Agent 用 `[[send: /abs/path]]` 交付本地文件。**单文件不超过 30 MB(30 × 1024 × 1024 字节)自动发聊天附件,超过 30 MB 自动传云空间**。按每个文件的实际大小分别选择,同一轮可同时交付聊天附件和云空间文件,无需切换命令。原来的文件、图片消息通道保留。
|
|
203
202
|
|
|
204
203
|
Codex、Claude Code 和 DSH 使用统一的飞书通道插件约定:按需读取附件、检查文件后输出交付标记,以及为 shell 命令提供中文摘要。Claude 安全模式使用对应英文版本。交付方式由通道插件管理,不需要 Agent 查询配置或调用上传接口,也无需额外安装 Skill。交付标记是提交请求,实际结果以附件、交付卡及错误提示为准。
|
|
205
204
|
|
|
206
205
|
安装或更新时会自动删除旧 `lodestar-files` Skill 在 Codex、Claude 和共享插件目录中的受管副本;新版 daemon 启动时也会清理。清理沿用本机数据目录设置,失败会明确报错;显式设置 `LODESTAR_DISABLE_SKILL_SYNC=1` 时保留手工管理的 Skill。
|
|
207
206
|
|
|
208
|
-
|
|
207
|
+
每个群首次交付超过 30 MB 的文件时,会自动创建独立云空间文件夹,并按 `chat_id` 保存固定绑定。文件夹名与实际群名一致,之后该群各轮的大文件都直接放入此文件夹。重启后继续复用原目录,同名群、主群及 BTW/FK/WT 群之间独立。群改名后,下次云空间交付会同步文件夹名称;目录丢失或不可访问时明确报错,不自动新建空目录掩盖问题。
|
|
209
208
|
|
|
210
|
-
|
|
209
|
+
有云空间文件的轮次结束后,会发送独立“交付文件”卡片,**只列本轮新交付的云空间文件**的名称、大小和访问链接。点击文件名打开文件;点击“管理群文件”打开飞书原生文件夹,查看该群所有历史云空间交付文件,并使用重命名、移动和删除等功能。交付发起人会获得群文件夹管理权限(`full_access`),群成员可查看(`view`)。文件不自动清理;历史卡片是交付时的记录,文件夹展示当前状态,已删除文件的旧链接会失效。
|
|
211
210
|
|
|
212
|
-
|
|
211
|
+
群文件夹绑定保存在私有 `file-delivery-groups.json`。升级后忽略旧版的工作目录和群开关,已有文件夹绑定、管理者和云空间文件继续保留;之前发送到聊天的附件不删除,也不自动迁移至云空间。
|
|
213
212
|
|
|
214
|
-
**云空间需要同时开通应用身份权限 `drive:drive`、`drive:file:upload`**;后者是文件夹自动改名接口单独要求的权限,前者不能替代。安装向导的[完整权限清单](feishu-permissions.json)
|
|
213
|
+
**云空间需要同时开通应用身份权限 `drive:drive`、`drive:file:upload`**;后者是文件夹自动改名接口单独要求的权限,前者不能替代。安装向导的[完整权限清单](feishu-permissions.json)已包含这两项,升级用户可补齐后发布飞书应用版本。整个过程不依赖飞书 CLI,也不需要用户授权登录。云空间交付须有可识别的发起人,缺少身份或权限时明确报错。超过 30 MB 的文件按飞书策略分片上传;实际大小上限与容量由账号决定,不能把企业基础版的认证限额直接套用于飞书个人版。交付失败不会自动切换通道。自动生图仍沿用下面的图片展示规则。
|
|
215
214
|
|
|
216
215
|
新上传文件会单独关闭“组织内或互联网获得链接即可访问”的分享入口,并读回确认后才发布交付链接;文件夹关闭链接分享不会自动覆盖新文件的默认设置。群成员通过群文件夹的协作者权限访问。已有协作者仍可按飞书权限设置进行人工分享,本功能不阻止人工转授权。
|
|
217
216
|
|
|
218
217
|
聊天消息、卡片写入和聊天附件/图片上传遇到短暂故障时,会在一分钟恢复窗口内自动重试;窗口内恢复成功不在群里告警,仍失败才提示,并保留具体诊断。卡片的重试、写入确认与流式重开共用同一个窗口,服务端要求更长等待时不会提前重发。每次实际操作有自己的恢复窗口,排队与上传后发送等多个步骤的耗时可能累加。本地文件问题和主动取消立即处理。云空间创建目录、上传完成等操作仍保持有限重试,避免扩大重复创建风险;分片重试复用相同内容,上传期间本地大文件发生修改会中止完成上传。停止或重启会话会取消未完成的云空间上传,已上传文件保留;链接查询、权限或交付卡发送失败也会明确显示。
|
|
219
218
|
|
|
220
|
-
Codex
|
|
219
|
+
Codex 生图完成后,不超过 30 MB 的图片与提示词放在同一个折叠面板中,点击展开查看图片并放大预览。无法嵌入时,图片仍会单独发送;超过 30 MB 的图片自动转云空间交付。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leviyuan/lodestar",
|
|
3
|
-
"version": "0.21.
|
|
3
|
+
"version": "0.21.17",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -61,6 +61,7 @@
|
|
|
61
61
|
"docs/",
|
|
62
62
|
"scripts/postinstall.cjs",
|
|
63
63
|
"src/data-dir.cjs",
|
|
64
|
+
"src/codex-plugin-cache.cjs",
|
|
64
65
|
"src/managed-skill-cleanup.cjs",
|
|
65
66
|
"README.md",
|
|
66
67
|
"LICENSE"
|
|
@@ -75,21 +76,22 @@
|
|
|
75
76
|
"@resvg/resvg-js": "^2.6.2",
|
|
76
77
|
"@xmldom/xmldom": "0.9.12",
|
|
77
78
|
"axios": "1.20.0",
|
|
79
|
+
"body-parser": "2.4.0",
|
|
78
80
|
"cross-spawn": "^7.0.6",
|
|
79
|
-
"fast-uri": "3.1.
|
|
81
|
+
"fast-uri": "3.1.9",
|
|
80
82
|
"fflate": "0.8.3",
|
|
81
83
|
"follow-redirects": "1.16.1",
|
|
82
84
|
"hono": "4.13.12",
|
|
83
85
|
"http-proxy-agent": "^7.0.2",
|
|
84
86
|
"https-proxy-agent": "^7.0.6",
|
|
85
|
-
"ip-address": "10.7.
|
|
87
|
+
"ip-address": "10.7.4",
|
|
86
88
|
"mathjax-full": "^3.2.2",
|
|
87
89
|
"node-fetch": "^3.3.2",
|
|
88
90
|
"protobufjs": "7.6.5",
|
|
89
91
|
"proxy-addr": "2.0.8",
|
|
90
|
-
"qs": "6.16.
|
|
92
|
+
"qs": "6.16.1",
|
|
91
93
|
"ws": "8.21.3",
|
|
92
|
-
"zod": "
|
|
94
|
+
"zod": "4.6.6"
|
|
93
95
|
},
|
|
94
96
|
"bundleDependencies": [
|
|
95
97
|
"mathjax-full"
|
|
@@ -99,15 +101,17 @@
|
|
|
99
101
|
"@modelcontextprotocol/sdk": "1.32.1",
|
|
100
102
|
"@xmldom/xmldom": "0.9.12",
|
|
101
103
|
"axios": "1.20.0",
|
|
102
|
-
"fast-uri": "3.1.
|
|
104
|
+
"fast-uri": "3.1.9",
|
|
103
105
|
"fflate": "0.8.3",
|
|
104
106
|
"hono": "4.13.12",
|
|
105
|
-
"ip-address": "10.7.
|
|
107
|
+
"ip-address": "10.7.4",
|
|
106
108
|
"protobufjs": "7.6.5",
|
|
107
109
|
"proxy-addr": "2.0.8",
|
|
108
|
-
"qs": "6.16.
|
|
110
|
+
"qs": "6.16.1",
|
|
109
111
|
"ws": "8.21.3",
|
|
110
|
-
"follow-redirects": "1.16.1"
|
|
112
|
+
"follow-redirects": "1.16.1",
|
|
113
|
+
"body-parser": "2.4.0",
|
|
114
|
+
"zod": "4.6.6"
|
|
111
115
|
},
|
|
112
116
|
"devDependencies": {
|
|
113
117
|
"@anthropic-ai/claude-agent-sdk": "latest",
|
|
@@ -356,12 +360,13 @@
|
|
|
356
360
|
"koffi"
|
|
357
361
|
],
|
|
358
362
|
"optionalDependencies": {
|
|
359
|
-
"undici": "^6.
|
|
363
|
+
"undici": "^6.29.1"
|
|
360
364
|
},
|
|
361
365
|
"agentRuntimeOverrides": {
|
|
362
366
|
"dsh": {
|
|
363
367
|
"@modelcontextprotocol/client": "2.3.1",
|
|
364
|
-
"@modelcontextprotocol/core": "2.3.1"
|
|
368
|
+
"@modelcontextprotocol/core": "2.3.1",
|
|
369
|
+
"undici": "8.12.2"
|
|
365
370
|
}
|
|
366
371
|
}
|
|
367
372
|
}
|
package/scripts/postinstall.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
//
|
|
2
|
+
// 迁移账号插件缓存、清理废弃的受管 Skill 并提示下一步,不启动 Agent 或 daemon。
|
|
3
3
|
//
|
|
4
4
|
// 为什么不自动拉向导: npm 7+ pipe 了 postinstall 的 stdio, 自动拉向导只能
|
|
5
5
|
// 靠接管 /dev/tty 把终端塞给子进程 —— 这套机制脆弱 (作者原注 "Tricky")。
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
//
|
|
10
10
|
// 稳的做法是 trigger-on-first-run: 用户在真终端首跑 lodestar-daemon 时,
|
|
11
11
|
// cli.ts 发现没 config + isTTY 就自动进入向导 (真 TTY, readline 一定正常)。
|
|
12
|
-
//
|
|
12
|
+
// 迁移与清理仅操作本地插件和 Skill 文件,不读凭据,也不启动子进程。
|
|
13
13
|
|
|
14
14
|
const fs = require('fs')
|
|
15
15
|
|
|
@@ -29,6 +29,8 @@ function termWrite (msg) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
async function main () {
|
|
32
|
+
const { migrateCodexPluginCaches } = require('../src/codex-plugin-cache.cjs')
|
|
33
|
+
migrateCodexPluginCaches()
|
|
32
34
|
const { removeManagedSkill } = require('../src/managed-skill-cleanup.cjs')
|
|
33
35
|
removeManagedSkill('lodestar-files')
|
|
34
36
|
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// Shared by account startup and npm postinstall. No config, auth or Agent imports.
|
|
2
|
+
const fs = require('node:fs')
|
|
3
|
+
const { homedir } = require('node:os')
|
|
4
|
+
const { dirname, isAbsolute, join, relative, resolve, sep } = require('node:path')
|
|
5
|
+
const { randomUUID } = require('node:crypto')
|
|
6
|
+
const { codexAccountsDir } = require('./data-dir.cjs')
|
|
7
|
+
|
|
8
|
+
const BACKUP = '.lodestar-plugins-shared-link'
|
|
9
|
+
const LOCK = '.lodestar-plugins-migration.lock'
|
|
10
|
+
|
|
11
|
+
function stat (path) {
|
|
12
|
+
try { return fs.lstatSync(path) } catch (error) {
|
|
13
|
+
if (error.code === 'ENOENT') return undefined
|
|
14
|
+
throw error
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Resolve existing ancestors too, so relative links, home aliases and missing
|
|
19
|
+
// legacy targets can all be checked without creating anything at the target.
|
|
20
|
+
function canonical (path) {
|
|
21
|
+
try { return fs.realpathSync(path) } catch (error) {
|
|
22
|
+
if (error.code !== 'ENOENT') throw error
|
|
23
|
+
const parent = dirname(path)
|
|
24
|
+
if (parent === path) throw error
|
|
25
|
+
return resolve(canonical(parent), relative(parent, path))
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isWithin (root, path) {
|
|
30
|
+
const rel = relative(root, path)
|
|
31
|
+
return rel === '' || (!isAbsolute(rel) && rel !== '..' && !rel.startsWith(`..${sep}`))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function assertLegacyLink (path, source) {
|
|
35
|
+
const info = stat(path)
|
|
36
|
+
if (!info?.isSymbolicLink()) throw new Error(`Codex 插件迁移路径不是旧共享链接:${path}`)
|
|
37
|
+
const target = resolve(dirname(path), fs.readlinkSync(path))
|
|
38
|
+
if (canonical(target) !== canonical(source)) throw new Error(`Codex 插件共享链接指向未知位置:${path}`)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function assertHome (home, defaultHome) {
|
|
42
|
+
const info = stat(home)
|
|
43
|
+
if (!info || !info.isDirectory() || info.isSymbolicLink()) throw new Error(`Codex 账号目录不是独立目录:${home}`)
|
|
44
|
+
if (canonical(home) === canonical(defaultHome)) throw new Error('额外账号不能使用默认认证目录')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function preflight (home, defaultHome) {
|
|
48
|
+
assertHome(home, defaultHome)
|
|
49
|
+
const source = join(defaultHome, 'plugins')
|
|
50
|
+
const plugins = join(home, 'plugins')
|
|
51
|
+
const backup = join(home, BACKUP)
|
|
52
|
+
if (stat(backup)) assertLegacyLink(backup, source)
|
|
53
|
+
const current = stat(plugins)
|
|
54
|
+
if (current?.isSymbolicLink()) assertLegacyLink(plugins, source)
|
|
55
|
+
else if (current && !current.isDirectory()) throw new Error(`Codex 插件路径不是目录:${plugins}`)
|
|
56
|
+
// A custom data directory may sit under the native plugin directory. Copying
|
|
57
|
+
// that source would also copy this account and its newly created staging tree.
|
|
58
|
+
if ((current?.isSymbolicLink() || (!current && stat(backup))) && isWithin(canonical(source), canonical(home))) {
|
|
59
|
+
throw new Error(`Codex 账号目录位于默认插件目录内,拒绝递归复制:${home}`)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function acquireLock (home) {
|
|
64
|
+
const path = join(home, LOCK)
|
|
65
|
+
// A hard link publishes the complete owner atomically; observers never see
|
|
66
|
+
// an empty PID file. A live owner fails visibly instead of blocking startup.
|
|
67
|
+
const candidate = join(home, `${LOCK}.${randomUUID()}`)
|
|
68
|
+
fs.writeFileSync(candidate, JSON.stringify({ pid: process.pid }), { flag: 'wx', mode: 0o600 })
|
|
69
|
+
try {
|
|
70
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
71
|
+
try {
|
|
72
|
+
fs.linkSync(candidate, path)
|
|
73
|
+
const owned = fs.lstatSync(path)
|
|
74
|
+
return () => {
|
|
75
|
+
const current = stat(path)
|
|
76
|
+
if (current && current.ino === owned.ino && current.dev === owned.dev) fs.unlinkSync(path)
|
|
77
|
+
}
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (error.code !== 'EEXIST') throw error
|
|
80
|
+
const before = stat(path)
|
|
81
|
+
if (!before) continue
|
|
82
|
+
if (!before.isFile() || before.isSymbolicLink()) throw new Error(`Codex 插件迁移锁无效:${path}`)
|
|
83
|
+
const owner = JSON.parse(fs.readFileSync(path, 'utf8'))
|
|
84
|
+
if (!Number.isSafeInteger(owner.pid) || owner.pid <= 0) throw new Error(`Codex 插件迁移锁无效:${path}`)
|
|
85
|
+
try {
|
|
86
|
+
process.kill(owner.pid, 0)
|
|
87
|
+
throw new Error('Codex 插件目录正在迁移,请稍后重试')
|
|
88
|
+
} catch (status) {
|
|
89
|
+
if (status.code !== 'ESRCH') throw status
|
|
90
|
+
}
|
|
91
|
+
const after = stat(path)
|
|
92
|
+
if (after && before.ino === after.ino && before.dev === after.dev) fs.unlinkSync(path)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
throw new Error('Codex 插件迁移锁持续变化,请稍后重试')
|
|
96
|
+
} finally {
|
|
97
|
+
fs.unlinkSync(candidate)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function copyTree (source, target, sourceRoot, targetRoot, finalRoot) {
|
|
102
|
+
const info = fs.lstatSync(source)
|
|
103
|
+
if (info.isSymbolicLink()) {
|
|
104
|
+
const linked = canonical(resolve(dirname(source), fs.readlinkSync(source)))
|
|
105
|
+
if (!isWithin(sourceRoot, linked)) throw new Error(`Codex 插件缓存包含目录外链接,拒绝复制:${source}`)
|
|
106
|
+
const isolated = join(targetRoot, relative(sourceRoot, linked))
|
|
107
|
+
let directory = false
|
|
108
|
+
try { directory = fs.statSync(source).isDirectory() } catch (error) { if (error.code !== 'ENOENT') throw error }
|
|
109
|
+
fs.symlinkSync(process.platform === 'win32' && directory ? join(finalRoot, relative(sourceRoot, linked)) : relative(dirname(target), isolated),
|
|
110
|
+
target, directory && process.platform === 'win32' ? 'junction' : directory ? 'dir' : 'file')
|
|
111
|
+
} else if (info.isDirectory()) {
|
|
112
|
+
fs.mkdirSync(target, { mode: 0o700 })
|
|
113
|
+
for (const child of fs.readdirSync(source)) copyTree(join(source, child), join(target, child), sourceRoot, targetRoot, finalRoot)
|
|
114
|
+
} else if (info.isFile()) {
|
|
115
|
+
fs.copyFileSync(source, target, fs.constants.COPYFILE_EXCL)
|
|
116
|
+
} else {
|
|
117
|
+
throw new Error(`Codex 插件缓存包含不支持的文件类型:${source}`)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function prepareCodexPluginDirectory (home, defaultHome) {
|
|
122
|
+
home = resolve(home)
|
|
123
|
+
defaultHome = resolve(defaultHome)
|
|
124
|
+
preflight(home, defaultHome)
|
|
125
|
+
const plugins = join(home, 'plugins')
|
|
126
|
+
const source = join(defaultHome, 'plugins')
|
|
127
|
+
const backup = join(home, BACKUP)
|
|
128
|
+
// Keep existing independent directories completely untouched.
|
|
129
|
+
if (stat(plugins)?.isDirectory() && !stat(backup)) return
|
|
130
|
+
const release = acquireLock(home)
|
|
131
|
+
try {
|
|
132
|
+
preflight(home, defaultHome)
|
|
133
|
+
if (stat(backup)) {
|
|
134
|
+
// A crash before publication leaves the old link here; a crash after
|
|
135
|
+
// publication leaves an independent directory plus this redundant link.
|
|
136
|
+
if (!stat(plugins)) fs.renameSync(backup, plugins)
|
|
137
|
+
else if (stat(plugins).isDirectory()) fs.unlinkSync(backup)
|
|
138
|
+
else throw new Error(`Codex 插件迁移存在冲突链接:${backup}`)
|
|
139
|
+
}
|
|
140
|
+
const current = stat(plugins)
|
|
141
|
+
if (!current) {
|
|
142
|
+
fs.mkdirSync(plugins, { mode: 0o700 })
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
if (current.isDirectory()) return
|
|
146
|
+
assertLegacyLink(plugins, source)
|
|
147
|
+
const temporary = fs.mkdtempSync(join(home, '.lodestar-plugins-copy-'))
|
|
148
|
+
const staged = join(temporary, 'plugins')
|
|
149
|
+
try {
|
|
150
|
+
let sourceRoot
|
|
151
|
+
try { sourceRoot = fs.realpathSync(source) } catch (error) { if (error.code !== 'ENOENT') throw error }
|
|
152
|
+
if (sourceRoot) {
|
|
153
|
+
if (!fs.statSync(sourceRoot).isDirectory()) throw new Error(`Codex 默认插件路径不是目录:${source}`)
|
|
154
|
+
copyTree(sourceRoot, staged, sourceRoot, staged, plugins)
|
|
155
|
+
} else fs.mkdirSync(staged, { mode: 0o700 })
|
|
156
|
+
// Copy failure never removes the old link. Recheck immediately before
|
|
157
|
+
// swapping; an unexpected path is not overwritten or recursively removed.
|
|
158
|
+
assertLegacyLink(plugins, source)
|
|
159
|
+
fs.renameSync(plugins, backup)
|
|
160
|
+
try {
|
|
161
|
+
fs.renameSync(staged, plugins)
|
|
162
|
+
} catch (error) {
|
|
163
|
+
if (!stat(plugins)) fs.renameSync(backup, plugins)
|
|
164
|
+
throw error
|
|
165
|
+
}
|
|
166
|
+
fs.unlinkSync(backup)
|
|
167
|
+
} finally {
|
|
168
|
+
fs.rmSync(temporary, { recursive: true, force: true })
|
|
169
|
+
}
|
|
170
|
+
} finally {
|
|
171
|
+
release()
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function migrateCodexPluginCaches (options = {}) {
|
|
176
|
+
const defaultHome = resolve(options.defaultHome || process.env.CODEX_HOME || join(homedir(), '.codex'))
|
|
177
|
+
const root = resolve(options.accountsRoot || codexAccountsDir())
|
|
178
|
+
const info = stat(root)
|
|
179
|
+
if (!info) return
|
|
180
|
+
if (!info.isDirectory() || info.isSymbolicLink()) throw new Error(`Codex 账号根目录不是独立目录:${root}`)
|
|
181
|
+
// Do not read credentials or infer arbitrary homes from names. Unregistered
|
|
182
|
+
// UUID homes from an interrupted account creation need the same repair.
|
|
183
|
+
const homes = fs.readdirSync(root).filter(name => /^[a-f0-9-]{36}$/.test(name)).map(name => join(root, name))
|
|
184
|
+
for (const home of homes) preflight(home, defaultHome)
|
|
185
|
+
for (const home of homes) prepareCodexPluginDirectory(home, defaultHome)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
module.exports = { prepareCodexPluginDirectory, migrateCodexPluginCaches }
|
package/src/data-dir.cjs
CHANGED
|
@@ -15,4 +15,8 @@ function managedClaudePluginDir () {
|
|
|
15
15
|
return join(lodestarDataDir(), 'managed-claude-plugin')
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
function codexAccountsDir () {
|
|
19
|
+
return join(lodestarDataDir(), 'codex-accounts')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { lodestarDataDir, managedClaudePluginDir, codexAccountsDir }
|