@epoch-agent/infra 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,219 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
203
+
204
+ --------------------------------------------------------------------------------
205
+
206
+ epoch-agent
207
+ Copyright 2024-2026 bowen
208
+
209
+ Portions of this software are derived from the Gemini CLI
210
+ (https://github.com/google-gemini/gemini-cli), Copyright 2025 Google LLC,
211
+ licensed under the Apache License, Version 2.0. Those files have been modified;
212
+ each one retains its original copyright notice and SPDX-License-Identifier
213
+ header. See the NOTICE file in the project source repository for the complete
214
+ list of derived files.
215
+
216
+ This software also quotes, with attribution, two lines of prose from OpenAI
217
+ Codex (https://github.com/openai/codex), Copyright 2025 OpenAI, licensed under
218
+ the Apache License, Version 2.0. No source file is a derived work of Codex.
219
+ See the NOTICE file for details.
package/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # @epoch-agent/infra
2
+
3
+ 基础设施层。跨模块共用的**叶子**能力。
4
+
5
+ - ✅ **做**:路径真源、SQLite 连接与迁移、日志、凭据存储、平台兼容、命令安全启发式表、
6
+ 工作区归属判定(`workspace.ts`)、外部可执行文件的探测与降级(`ripgrep.ts`)、异步小工具
7
+ - ❌ **不做**:任何领域逻辑(ReAct / 记忆 / 权限判定都属于 [core](../core))、任何 UI
8
+ - **依赖**:只有 node 内置模块 + `better-sqlite3` + `zod`。不依赖任何 `@epoch-agent/*` 包
9
+
10
+ 判断一个东西该不该放这里:**无状态或只管进程级资源,且被两层以上共用**。
11
+
12
+ `workspace.ts` 和 `command-safety.ts` 看着像「权限判定」,其实是同一类东西:
13
+ 一张**纯判据**(这个路径在不在地盘里 / 这条命令像不像手滑),谁都能问、问了不改变
14
+ 任何状态。「问到之后要不要放行、要不要弹审批」才是权限判定,那部分在 core。
15
+ 判据落在 infra 是因为按分层 `plugin-*` 只能依赖 protocol + infra,够不着 core ——
16
+ 放 core 就意味着两个 plugin 各抄一份,而这次 `--add-dir` 就是被那三份抄本坑到才收归一处的。
17
+
18
+ ## 文件
19
+
20
+ | 文件 | 内容 |
21
+ | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
22
+ | `paths.ts` | `~/.epoch/` 下每个文件的**唯一**真源,外加机器级的 `managedSettingsPath()`。别处再 `join(homedir(), '.epoch')` 就是埋雷 |
23
+ | `artifact-store.ts` | 往 artifact 目录写文件的**唯一**写法:`0700` 目录 / `0600` 文件 / `open(path,'wx')` 独占写。core 的多模态闸门和 plugin-terminal 的大输出落盘共用 |
24
+ | `text-slice.ts` | 按 **Unicode 码点**取头尾,截断不许切出半个字符(emoji 和 CJK 扩展 B 是代理对)。命令输出预览和工具结果裁剪共用 |
25
+ | `workspace.ts` | `isInWorkspace(target, workDir, extraRoots)`——「这个路径算不算在地盘里」的**唯一**判定,权限层 / plugin-file / plugin-terminal 共用 |
26
+ | `sqlite.ts` | 连接池(引用计数)+ 迁移。`openDatabase` / `releaseDatabase` 配对使用 |
27
+ | `logger.ts` | 结构化日志 |
28
+ | `mask.ts` | 脱敏(`maskApiKey` / `isSensitiveKey`),`epoch config show` 靠它 |
29
+ | `platform.ts` | Windows / POSIX 差异:shell 选择、spawn 与 pty 参数、控制台编码探测与解码 |
30
+ | `ripgrep.ts` | `rg` 从哪来的三级解析:系统 PATH → 随包带的 vendor 二进制 → 都没有。`file_search` 和将来的 `@` 补全共用 |
31
+ | `process-tree.ts` | 进程树清理——杀干净孙进程,且只杀该杀的 |
32
+ | `child-process.ts` | 长期子进程(后台任务 / language server)的启动、登记、回收;`detached: false` 的理由写在文件头。还有 `EXEC_PATH_AS_NODE_ENV`——「拿 `process.execPath` 当 node 使」的地方必须带的那一格(嵌入 Electron 的宿主里它不是 node),两个消费者分在 core 和 plugin-lsp |
33
+ | `shell-parse.ts` | 命令分词与分段(引号感知,带 POSIX / Windows 两种口味)。危险判定、权限规则匹配、斜杠命令的位置参数都建在它上面 |
34
+ | `command-safety.ts` | 危险命令与混淆的启发式表,建在 `shell-parse` 之上。**是护栏不是沙箱** |
35
+ | `sandbox/` | **这个才是沙箱**:Seatbelt(macOS)/ bubblewrap(Linux,运行时暂未验证)/ 探测不到就降级。`confine()` 按调用带 policy 并如实报强制完整度;两组 stderr 分类器分开「被挡住」和「沙箱没起来」。2026-08-16 从 core 搬来,理由同 `command-safety.ts`:`code_exec` 和 `plugin-terminal` 共用,而 plugin 够不着 core |
36
+ | `schema.ts` | 配置校验(zod + 宽松解析,坏字段降级而不是整份拒绝) |
37
+ | `i18n.ts` | `t()` / 语言解析 / catalog 加载与三层回落。文案真源是仓库根的 `locales/*.yaml` |
38
+ | `async.ts` | 异步小工具 |
39
+ | `secret/` | 凭据存储:DPAPI(Windows)/ Keychain(macOS)/ libsecret / 明文回落 + 迁移 |
40
+
41
+ ## 界面文案:`t()` 与三层回落
42
+
43
+ `i18n.ts` 也是一个进程级状态(`setLang('zh' | 'en')`,形状同 `setShell`)。
44
+ `Lang` 的值域在这里和 [protocol](../protocol) 各写了一份 —— 同 `ShellKind`,
45
+ 本包零内部依赖,import 不了 protocol;根 `__tests__/i18n-catalog.test.ts`
46
+ 里有一条用例锁着两边**逐项相等**。
47
+
48
+ 文案的真源是**仓库根的 `locales/{zh,en}.yaml`**,不在本包里。发布时由
49
+ `scripts/copy-locales.mjs` 在 `tsup` 之后拷进 `dist/locales`
50
+ (`files: ["dist"]` 只发 dist,不拷就等于装了包的人满屏 key 路径)。
51
+
52
+ `t()` **永远返回字符串、永远不抛**,三层回落:
53
+
54
+ ```
55
+ 目标语言的 key → zh(DEFAULT_LANG)的 key → 返回 key 路径本身
56
+ ```
57
+
58
+ 第三层是关键:catalog 写坏了不能让 agent 起不来。同一条原则下,坏 YAML / 缺文件
59
+ 只产出 `i18nDiagnostics()` 里的一条 warn,宿主拿去塞进启动诊断。
60
+ 语言怎么选出来(`EPOCH_LANGUAGE` → `display.language` → 系统 locale → 中文)
61
+ 见 [docs/CONFIGURATION.md](../../docs/CONFIGURATION.md#界面语言)。
62
+
63
+ ## Windows 上的 shell 选择
64
+
65
+ `platform.ts` 里有一个进程级状态:`setShell('cmd' | 'powershell' | 'pwsh')`,
66
+ 装配层([runtime](../runtime))读完配置调一次,`shellSpawnArgs` / `shellPtyArgs`
67
+ 据此拼参数。形状跟 `setLogLevel` / `setSecretStore` 一致。POSIX 上是**空操作**,
68
+ 永远 `/bin/sh` —— macOS 走的就是这一支。
69
+
70
+ 只收这三个具名值,**不收任意可执行文件路径**:每加一种都要重新验「引号怎么转义」
71
+ 和「退出码怎么传」两件事,放开成任意路径等于承诺一件没验过的事。
72
+
73
+ PowerShell 那条路上有两个**不报错**的坑,都写进了 `platform.ts` 的注释:
74
+
75
+ - PowerShell 5.1 不传播被调命令的退出码(实测 `-Command "cmd /c exit 3"` → `1`),
76
+ 所以命令后面必须接一段同时读 `$?` 和 `$LASTEXITCODE` 的收尾语句。只读后者更糟:
77
+ 命令不存在时它是 `$null`,`exit $null` → **0**,一次彻底失败被报成成功
78
+ - 命令和收尾之间必须是**真换行**,不能用 `;`。用 `;` 的话命令末尾带一句 `# 注释`
79
+ 就把整段收尾注释掉了,实测 `cmd /c exit 5 # note` 退出码变成 `1`
80
+
81
+ `ShellKind` 的值域在这里和 [protocol](../protocol) 各写了一份——本包零内部依赖,
82
+ import 不了 protocol。[core](../core) 里有一条用例锁着两边相等。
83
+
84
+ ## 凭据存储的后端顺序
85
+
86
+ `secret/catalog.ts` 按平台探测可用后端,探不到就降级,最后回落到 `~/.epoch/.env` 明文。
87
+ `epoch config secret` 打的就是这个判定结果。
88
+
89
+ > ⚠️ **POSIX 分支不等于 Linux 分支,别顺手删。** macOS 走的就是 POSIX 那一支
90
+ > (`pgrep` / `kill` 进程树、`/bin/sh`、危险命令表的 POSIX 那张)——删这些等于砍掉
91
+ > macOS。真正只有 Linux 会走到的是显式判 `platform() === 'linux'` 的分支
92
+ > (`secret/libsecret.ts`):Linux 暂不支持,所以那条路径**尚未实测**,但代码留着,
93
+ > 将来补 Linux 支持时从它接着往下走。
94
+
95
+ ## 开发
96
+
97
+ ```bash
98
+ pnpm --filter @epoch-agent/infra test
99
+ ```