fde-console 0.0.0-stage → 2.9.68

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.
Files changed (85) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +128 -2
  3. package/README.zh-CN.md +130 -0
  4. package/assets/icon.svg +9 -0
  5. package/cordis.patch.yml +7 -0
  6. package/lib/agentActivity.js +1180 -0
  7. package/lib/agentContext.js +146 -0
  8. package/lib/agentTranscript.js +1398 -0
  9. package/lib/analyze.js +625 -0
  10. package/lib/attribute.js +205 -0
  11. package/lib/backup.js +366 -0
  12. package/lib/desktopAgents.js +123 -0
  13. package/lib/diary.js +333 -0
  14. package/lib/dingtalk.js +412 -0
  15. package/lib/feishu.js +580 -0
  16. package/lib/giteaClient.js +810 -0
  17. package/lib/giteeClient.js +372 -0
  18. package/lib/githubClient.js +361 -0
  19. package/lib/heatmap.js +99 -0
  20. package/lib/imNotify.js +378 -0
  21. package/lib/inbox.js +438 -0
  22. package/lib/instanceLock.js +122 -0
  23. package/lib/jobs.js +56 -0
  24. package/lib/learn.js +1388 -0
  25. package/lib/learnImport.js +283 -0
  26. package/lib/learnSeeds.js +97 -0
  27. package/lib/learnSeedsAiEng.js +77 -0
  28. package/lib/mcpCore.js +135 -0
  29. package/lib/members.js +160 -0
  30. package/lib/migration.js +403 -0
  31. package/lib/overview.js +376 -0
  32. package/lib/postmortem.js +148 -0
  33. package/lib/radarIgnore.js +134 -0
  34. package/lib/redact.js +76 -0
  35. package/lib/router.js +246 -0
  36. package/lib/routes-gitea.js +262 -0
  37. package/lib/routes-setup.js +40 -0
  38. package/lib/scorecard.js +111 -0
  39. package/lib/sessionRef.js +227 -0
  40. package/lib/sessionSnapshot.js +67 -0
  41. package/lib/sessionSuggest.js +186 -0
  42. package/lib/settings.js +783 -0
  43. package/lib/setupCheck.js +330 -0
  44. package/lib/ssrfGuard.js +145 -0
  45. package/lib/store.js +564 -0
  46. package/lib/storeDb.js +418 -0
  47. package/lib/taskEvents.js +243 -0
  48. package/lib/webhookOut.js +125 -0
  49. package/lib/weekly.js +439 -0
  50. package/lib/workSearch.js +220 -0
  51. package/locale/en.json +6 -0
  52. package/locale/zh.json +6 -0
  53. package/package.json +46 -4
  54. package/plugin/adapter.js +157 -0
  55. package/plugin/client.js +282 -0
  56. package/plugin/index.mjs +116 -0
  57. package/plugin/static.js +99 -0
  58. package/scripts/backup-data.js +41 -0
  59. package/scripts/mcp-server.mjs +68 -0
  60. package/scripts/migrate-data.js +301 -0
  61. package/scripts/migrate-storage.js +47 -0
  62. package/scripts/seed-demo.js +200 -0
  63. package/scripts/write-agent-context.js +43 -0
  64. package/test/helpers/appAssembler.js +1078 -0
  65. package/web/client/fde-client.js +292 -0
  66. package/web/css/app.css +1510 -0
  67. package/web/index.html +69 -0
  68. package/web/js/api.js +72 -0
  69. package/web/js/app.js +313 -0
  70. package/web/js/icons.js +48 -0
  71. package/web/js/mock.js +1150 -0
  72. package/web/js/state.js +7 -0
  73. package/web/js/ui.js +276 -0
  74. package/web/js/views/activity.js +410 -0
  75. package/web/js/views/agents.js +892 -0
  76. package/web/js/views/board.js +1656 -0
  77. package/web/js/views/learn.js +530 -0
  78. package/web/js/views/members.js +344 -0
  79. package/web/js/views/onboarding.js +103 -0
  80. package/web/js/views/overview.js +508 -0
  81. package/web/js/views/radar.js +1359 -0
  82. package/web/js/views/search.js +149 -0
  83. package/web/js/views/settings.js +1351 -0
  84. package/web/js/views/topology.js +471 -0
  85. package/web/mock.json +2363 -0
@@ -0,0 +1,810 @@
1
+ 'use strict';
2
+ // giteaClient.js — FDE 指挥台 Gitea 只读客户端(SPEC §3/§4/§5,B3 包)。
3
+ // 数据源(真机 Gitea 28.1.0 探针实证):各仓库 activities/feeds 事件流,op_type 映射到
4
+ // feeds 词表 activity.type:pr/commit/branch_created/branch_deleted(旧三接口
5
+ // /pulls、/commits、/branches 路径已整体退役,不留 fallback)。
6
+ // 红线(SPEC §5-③):
7
+ // - 凭据 key 由 config.gitea.baseUrl 推导:git config --global http.<baseUrl>/.extraheader
8
+ // - 凭据读入后仅存本模块内存变量(credCache),绝不写文件、绝不进子进程 env、绝不进日志
9
+ // - 所有日志/错误出口必经 lib/redact.js 的 redact()
10
+ // - GITEA_FIXTURE 环境变量指向 fixture JSON 时完全替代真实网络(读文件,零请求)
11
+
12
+ const fs = require('node:fs');
13
+ const path = require('node:path');
14
+ const { execFileSync } = require('node:child_process');
15
+ const { redact, registerSecret, setRedactKeys } = require('./redact.js');
16
+ const { assertSafeUrl, validateWhitelist } = require('./ssrfGuard.js');
17
+
18
+ const PROJECT_ROOT = path.resolve(__dirname, '..');
19
+ const SEVEN_DAYS_MS = 7 * 24 * 3600 * 1000;
20
+ const REDACTED = '[REDACTED]';
21
+
22
+ // SPEC §5 config.json 默认值(config.json 由使用者/设置接口维护,缺失时以此兜底)
23
+ const DEFAULTS = {
24
+ port: 7788,
25
+ host: '127.0.0.1',
26
+ dataDir: 'data',
27
+ webDir: 'web',
28
+ gitea: {
29
+ baseUrl: 'https://gitea.example.com',
30
+ apiPrefix: '/api/v1',
31
+ org: 'example-org',
32
+ pollIntervalSec: 60,
33
+ staleWarnSec: 14400,
34
+ staleDangerSec: 86400,
35
+ hostWhitelist: ['gitea.example.com'],
36
+ activityTypes: ['pr', 'commit', 'branch_created', 'branch_deleted'],
37
+ feedsLimit: 50,
38
+ maxActivities: 500,
39
+ // v2.9.2 忽略账号(成员册派生排除名单,与 store.DEFAULT_CONFIG.gitea 同步)
40
+ ignoreLogins: ['admin']
41
+ },
42
+ // v2.9 GitHub 组织动态源(lib/githubClient.js 消费;与 store.DEFAULT_CONFIG.github 同步)
43
+ github: {
44
+ enabled: false,
45
+ baseUrl: 'https://api.github.com',
46
+ org: '',
47
+ eventsPages: 3,
48
+ hostWhitelist: ['api.github.com']
49
+ },
50
+ security: {
51
+ allowedProtocols: ['https'],
52
+ credentialSource: 'git-config-extraheader',
53
+ redactKeys: ['Authorization', 'extraheader']
54
+ },
55
+ // v2.9.39 IM 通知(与 store.DEFAULT_CONFIG.im 同步;消费方 lib/imNotify.js)
56
+ im: {
57
+ enabled: false,
58
+ digest: { enabled: false, at: '09:10' },
59
+ events: { review: false, completed: false },
60
+ targets: []
61
+ },
62
+ // v2.9.41 数据备份(与 store.DEFAULT_CONFIG.backup 同步;v2.9.42 起默认关闭)
63
+ backup: {
64
+ enabled: false,
65
+ keep: 14,
66
+ at: '09:05'
67
+ },
68
+ // v2.9.44 知识库学习(与 store.DEFAULT_CONFIG.learn 同步;消费方 lib/learn.js)
69
+ learn: {
70
+ enabled: false,
71
+ root: '',
72
+ scope: ['wiki'],
73
+ exclude: [],
74
+ seeds: ['模型与推理', 'AI 工程', 'Java', 'Agent 开发', 'Python'],
75
+ dailyNew: 1,
76
+ dailyReview: 3,
77
+ minutes: 15,
78
+ engine: { mode: 'auto', timeoutSec: 180, reasoningEffort: 'off', maxTokens: 1200 }
79
+ }
80
+ };
81
+
82
+ // feeds 词表兼容:老三接口时代的词表(commit_pushed/pr_opened/pr_merged)→ feeds 新词表。
83
+ // config.json 由使用方维护,历史部署常带老词表——不归一则 mapper 产出的 commit/pr 每轮
84
+ // 被 activityTypes 过滤丢弃(现象:轮询 ok:true 但动态永远不更新)。读取与使用两处都归一。
85
+ const LEGACY_ACTIVITY_TYPE_MAP = { commit_pushed: 'commit', pr_opened: 'pr', pr_merged: 'pr' };
86
+
87
+ /** 老词表名映射新词表并去重(保序、未知名原样保留),返回新数组 */
88
+ function normalizeActivityTypes(list) {
89
+ const seen = new Set();
90
+ const out = [];
91
+ for (const t of Array.isArray(list) ? list : []) {
92
+ const m = LEGACY_ACTIVITY_TYPE_MAP[t] || t;
93
+ if (!seen.has(m)) { seen.add(m); out.push(m); }
94
+ }
95
+ return out;
96
+ }
97
+
98
+ // ---------------------------------------------------------------- config
99
+
100
+ function deepMerge(base, over) {
101
+ const out = Array.isArray(base) ? base.slice() : { ...base };
102
+ if (!over || typeof over !== 'object' || Array.isArray(over)) return out;
103
+ for (const [k, v] of Object.entries(over)) {
104
+ if (v && typeof v === 'object' && !Array.isArray(v) && out[k] && typeof out[k] === 'object' && !Array.isArray(out[k])) {
105
+ out[k] = deepMerge(out[k], v);
106
+ } else {
107
+ out[k] = v;
108
+ }
109
+ }
110
+ return out;
111
+ }
112
+
113
+ /** 读项目根 config.json(不存在/坏 JSON 用 DEFAULTS 兜底),深合并后返回 cfg */
114
+ function loadConfig() {
115
+ let fileCfg = {};
116
+ try {
117
+ fileCfg = JSON.parse(fs.readFileSync(path.join(PROJECT_ROOT, 'config.json'), 'utf8'));
118
+ } catch {
119
+ fileCfg = {};
120
+ }
121
+ const cfg = deepMerge(DEFAULTS, fileCfg);
122
+ cfg.gitea.activityTypes = normalizeActivityTypes(cfg.gitea.activityTypes); // 历史部署老词表归一
123
+ setRedactKeys(cfg.security && cfg.security.redactKeys);
124
+ return cfg;
125
+ }
126
+
127
+ // ------------------------------------------------------------- credential
128
+
129
+ /** 凭据缓存:gitKey -> value|null。仅内存,进程退出即消失(SPEC §5-③)。 */
130
+ const credCache = new Map();
131
+
132
+ function credentialGitKey(cfg) {
133
+ return `http.${cfg.gitea.baseUrl}/.extraheader`;
134
+ }
135
+
136
+ /**
137
+ * 系统凭据管理器兜底(v2.9.15):`git credential fill` 走 credential.helper(wincred/manager),
138
+ * 让「配置里零明文」的部署也能用指挥台——凭据由 OS 保管,不再写进 ~/.gitconfig 的 extraheader。
139
+ * 该写法与 git push 共用同一份凭据,不会出现「两个 Authorization 头打架」。
140
+ * 失败/未配置/无 helper 一律返回 null;GIT_TERMINAL_PROMPT=0 保证绝不弹交互框。
141
+ * @returns {string|null} 形如 "AUTHORIZATION: Basic xxx"
142
+ */
143
+ function readCredentialFromHelper(cfg) {
144
+ let url;
145
+ try {
146
+ url = new URL(cfg.gitea.baseUrl);
147
+ } catch {
148
+ return null;
149
+ }
150
+ try {
151
+ const out = execFileSync('git', ['credential', 'fill'], {
152
+ input: `protocol=${url.protocol.replace(':', '')}\nhost=${url.host}\n\n`,
153
+ encoding: 'utf8',
154
+ timeout: 5000,
155
+ windowsHide: true,
156
+ // 只放「不许交互」的开关;凭据本身绝不经环境变量传递
157
+ env: Object.assign({}, process.env, { GIT_TERMINAL_PROMPT: '0', GIT_ASKPASS: 'echo', SSH_ASKPASS: 'echo' })
158
+ });
159
+ return credentialHelperHeader(out);
160
+ } catch {
161
+ return null;
162
+ }
163
+ }
164
+
165
+ /** git credential fill 的 `key=value` 输出 → Authorization 头;缺 password 返回 null(纯函数,供单测) */
166
+ function credentialHelperHeader(stdout) {
167
+ const fields = {};
168
+ for (const line of String(stdout || '').split(/\r?\n/)) {
169
+ const i = line.indexOf('=');
170
+ if (i > 0) fields[line.slice(0, i)] = line.slice(i + 1);
171
+ }
172
+ if (!fields.password) return null;
173
+ const user = fields.username || 'git';
174
+ return 'AUTHORIZATION: Basic ' + Buffer.from(`${user}:${fields.password}`, 'utf8').toString('base64');
175
+ }
176
+
177
+ /**
178
+ * 读取 Gitea 预授权凭据:`git config --global http.<baseUrl>/.extraheader` 优先,
179
+ * 没有配置时回退到系统凭据管理器(见 readCredentialFromHelper)。
180
+ * 用 execFileSync 直调、不经 shell;值注册进 redact 注册表。
181
+ * @returns {string|null} 凭据原值(形如 "Authorization: Basic xxx"),无则 null
182
+ */
183
+ function readCredential(cfg) {
184
+ const c = cfg || loadConfig();
185
+ const gitKey = credentialGitKey(c);
186
+ if (credCache.has(gitKey)) return credCache.get(gitKey);
187
+ let value = null;
188
+ try {
189
+ const out = execFileSync('git', ['config', '--global', gitKey], {
190
+ encoding: 'utf8',
191
+ timeout: 5000,
192
+ windowsHide: true
193
+ // 注意:不传 env —— 子进程环境变量里绝不出现凭据(凭据也绝不作为参数传给任何写命令)
194
+ });
195
+ value = String(out || '').trim() || null;
196
+ } catch {
197
+ value = null; // 未配置 = 无凭据,真实请求将以匿名方式发出
198
+ }
199
+ if (!value) value = readCredentialFromHelper(c);
200
+ if (value) {
201
+ registerSecret(value);
202
+ const idx = value.indexOf(':');
203
+ if (idx > 0) registerSecret(value.slice(idx + 1).trim()); // 头值部分单独注册,双保险
204
+ }
205
+ credCache.set(gitKey, value);
206
+ return value;
207
+ }
208
+
209
+ /** 由 extraheader 原值构造请求头:"Name: value" 拆开;无冒号则整体作为 Authorization 值 */
210
+ function authHeaders(credential) {
211
+ if (!credential) return {};
212
+ const idx = credential.indexOf(':');
213
+ if (idx > 0) return { [credential.slice(0, idx).trim()]: credential.slice(idx + 1).trim() };
214
+ return { Authorization: credential };
215
+ }
216
+
217
+ // ---------------------------------------------------------------- fixture
218
+
219
+ function fixtureFile() {
220
+ const p = process.env.GITEA_FIXTURE;
221
+ if (!p) return null;
222
+ return path.isAbsolute(p) ? p : path.resolve(PROJECT_ROOT, p);
223
+ }
224
+
225
+ /** 读 fixture JSON;GITEA_FIXTURE 未设置时返回 null */
226
+ function readFixture() {
227
+ const file = fixtureFile();
228
+ if (!file) return null;
229
+ return JSON.parse(fs.readFileSync(file, 'utf8'));
230
+ }
231
+
232
+ // ------------------------------------------------------------------- http
233
+
234
+ /**
235
+ * 出站 GET。安全前置(qoder #11,纵深防御):
236
+ * 1. 对拼接完成后的最终 URL 再跑一次 assertSafeUrl——防 config 手改
237
+ * (如 apiPrefix 写成 "@evil.com/",借助 userinfo 伪装把请求连同凭据导向白名单外);
238
+ * 2. 校验通过之后才构造带凭据的 headers——凭据绝不会附到发往白名单外 host 的请求上;
239
+ * 3. redirect:'error' 防重定向绕过;全部错误 message 经 redact()。
240
+ */
241
+ async function giteaFetch(url, credential, cfg, timeoutMs = 10000) {
242
+ assertSafeUrl(url, cfg || loadConfig()); // 末级复检:任何出站前
243
+ const ac = new AbortController();
244
+ const timer = setTimeout(() => ac.abort(), timeoutMs);
245
+ try {
246
+ const res = await fetch(url, {
247
+ method: 'GET',
248
+ headers: { Accept: 'application/json', ...authHeaders(credential) },
249
+ signal: ac.signal,
250
+ redirect: 'error'
251
+ });
252
+ if (!res.ok) {
253
+ let detail = '';
254
+ try { detail = (await res.text()).slice(0, 200); } catch { /* ignore */ }
255
+ const err = new Error(redact(`Gitea HTTP ${res.status} ${res.statusText} ${detail} (${url})`));
256
+ err.status = res.status;
257
+ throw err;
258
+ }
259
+ return await res.json();
260
+ } catch (err) {
261
+ if (err instanceof Error && !err.message.includes(REDACTED)) err.message = redact(err.message);
262
+ throw err;
263
+ } finally {
264
+ clearTimeout(timer);
265
+ }
266
+ }
267
+
268
+ /**
269
+ * 分页拉全(Gitea limit/page 风格),页数与每页条数设上限防失控。
270
+ * 可选 stopWhen(arr) 谓词:对某页返回 true(如本页末条目 created 早于时间窗起点)即提前停翻。
271
+ */
272
+ async function listAll(urlBase, credential, cfg, { maxPages = 10, perPage = 50, stopWhen = null } = {}) {
273
+ const out = [];
274
+ for (let page = 1; page <= maxPages; page++) {
275
+ const sep = urlBase.includes('?') ? '&' : '?';
276
+ const data = await giteaFetch(`${urlBase}${sep}limit=${perPage}&page=${page}`, credential, cfg);
277
+ const arr = Array.isArray(data) ? data : [];
278
+ out.push(...arr);
279
+ if (arr.length < perPage) break;
280
+ if (stopWhen && arr.length > 0 && stopWhen(arr)) break;
281
+ }
282
+ return out;
283
+ }
284
+
285
+ /** host 归一化:小写 + 去末尾点(FQDN),用于凭据最小化的同 host 判定 */
286
+ function normalizeHost(h) {
287
+ return String(h || '').toLowerCase().replace(/\.$/, '');
288
+ }
289
+
290
+ // ---------------------------------------------------------------- mapping
291
+
292
+ function firstLine(s) {
293
+ return String(s || '').split('\n')[0].trim();
294
+ }
295
+
296
+ /**
297
+ * v2.0 功能B:提取提交说明正文首段(title 首行之后的正文第一段)。
298
+ * 规则:按 '\n' 切分丢首行;跳过正文开头的空行;收集至下一个空行之前的所有行;
299
+ * 以 '\n' 连接后去首尾空白;总长超 300 字符则截断到 300;无正文返回 ''。
300
+ * @param {string} message commit 完整 message(首行为 title)
301
+ * @returns {string} 正文第一段(无正文时为 '')
302
+ */
303
+ function messageBodyOf(message) {
304
+ const lines = String(message || '').split('\n').slice(1);
305
+ let i = 0;
306
+ while (i < lines.length && lines[i].trim() === '') i += 1; // 跳过正文开头空行
307
+ const para = [];
308
+ while (i < lines.length && lines[i].trim() !== '') { // 收集至下一个空行之前
309
+ para.push(lines[i]);
310
+ i += 1;
311
+ }
312
+ const text = para.join('\n').trim();
313
+ return text.length > 300 ? text.slice(0, 300) : text;
314
+ }
315
+
316
+ /** 去掉 refs/heads/ 前缀(feeds 分支类条目的 ref_name 可能带完整 ref 形态) */
317
+ function stripRefsPrefix(ref) {
318
+ return String(ref || '').replace(/^refs\/heads\//, '');
319
+ }
320
+
321
+ /** 分支网页 URL:按段 encodeURIComponent 后 '/' 连接(沿用旧 /branches 映射口径) */
322
+ function branchUrlOf(htmlUrl, repoFull, branch) {
323
+ const tail = branch.split('/').map(encodeURIComponent).join('/');
324
+ return htmlUrl ? `${htmlUrl}/src/branch/${tail}` : `https://gitea.invalid/${repoFull}/src/branch/${tail}`;
325
+ }
326
+
327
+ /** PR 号提取顺序:comment.pull_request_url → comment.issue_url → comment.html_url → content "#N" */
328
+ function prNumberOf(entry) {
329
+ const comment = entry.comment || {};
330
+ for (const u of [comment.pull_request_url, comment.issue_url, comment.html_url]) {
331
+ if (typeof u !== 'string' || u === '') continue;
332
+ const m = /pulls\/(\d+)/.exec(u) || /issues\/(\d+)/.exec(u);
333
+ if (m) return m[1];
334
+ }
335
+ const m = /#(\d+)/.exec(String(entry.content || ''));
336
+ return m ? m[1] : null;
337
+ }
338
+
339
+ /**
340
+ * feeds 条目 → activity(Gitea activities/feeds 数据源,真机 28.1.0 探针实证口径):
341
+ * - op_type 映射:commit_repo 拆条(content.Commits 逐条 → 'commit',Timestamp 为 occurredAt);
342
+ * create_branch/delete_branch → branch_created/branch_deleted(ref_name 去 refs/heads/ 前缀);
343
+ * merge_pull_request/create_pull_request/pull_request → 'pr'(PR 号按序提取,无号退化 op_type 原文);
344
+ * 其他 op_type(含 create_repo)跳过 → 计数 skippedUnknownOps + distinct 名单 unknownOpTypes
345
+ * - 时间窗:条目 created 早于 sinceMs 整体跳过(fixture 传 -Infinity = 不做窗口过滤);
346
+ * commit_repo 拆条后单 commit occurredAt 落窗外则丢弃该条
347
+ * - 所有产物带 fetchedAt,并显式补齐 branch/sha/messageBody(null/null/'' 兜底)保证 strictActivity 输入完整
348
+ * @returns {{activities: object[], skippedUnknownOps: number, unknownOpTypes: string[]}}
349
+ */
350
+ function mapFeedEntries(entries, repo, sinceMs, fetchedAt) {
351
+ const activities = [];
352
+ const unknownOps = new Set();
353
+ let skippedUnknownOps = 0;
354
+ const full = (repo && (repo.full_name || `${(repo.owner && repo.owner.login) || ''}/${repo.name}`)) || '';
355
+ const html = (repo && repo.html_url) || '';
356
+
357
+ for (const entry of entries || []) {
358
+ if (!entry) continue;
359
+ const created = String(entry.created || '');
360
+ const createdMs = Date.parse(created);
361
+ if (Number.isFinite(createdMs) && createdMs < sinceMs) continue; // 条目整体落窗外
362
+ const actor = (entry.act_user && entry.act_user.login) || 'unknown';
363
+ const op = String(entry.op_type || '');
364
+
365
+ if (op === 'commit_repo') {
366
+ // content 为 JSON 字符串:{"Commits":[...],"HeadCommit":{...},"CompareURL":"...","Len":N}
367
+ let content = null;
368
+ try { content = JSON.parse(entry.content); } catch { content = null; }
369
+ const commits = content && Array.isArray(content.Commits) ? content.Commits : [];
370
+ const branch = stripRefsPrefix(String(entry.ref_name || '').split(',')[0]) || null; // 多分支逗号分隔 → 取第一段
371
+ for (const c of commits) {
372
+ if (!c) continue;
373
+ const ts = Date.parse(c.Timestamp);
374
+ const occurredAt = Number.isFinite(ts) ? String(c.Timestamp) : created; // 解析失败退条目 created
375
+ const occMs = Number.isFinite(ts) ? ts : createdMs;
376
+ if (Number.isFinite(occMs) && occMs < sinceMs) continue; // 该 commit 落窗外 → 丢弃
377
+ const sha = c.Sha1 == null ? null : String(c.Sha1);
378
+ activities.push({
379
+ type: 'commit',
380
+ actor,
381
+ repo: full,
382
+ branch,
383
+ sha,
384
+ title: firstLine(c.Message),
385
+ messageBody: messageBodyOf(c.Message),
386
+ url: sha && html ? `${html}/commit/${sha}` : '',
387
+ occurredAt,
388
+ fetchedAt
389
+ });
390
+ }
391
+ continue;
392
+ }
393
+
394
+ if (op === 'create_branch' || op === 'delete_branch') {
395
+ const branch = stripRefsPrefix(entry.ref_name) || null;
396
+ activities.push({
397
+ type: op === 'create_branch' ? 'branch_created' : 'branch_deleted',
398
+ actor,
399
+ repo: full,
400
+ branch,
401
+ sha: null,
402
+ title: branch || '',
403
+ messageBody: '',
404
+ url: branch ? branchUrlOf(html, full, branch) : '',
405
+ occurredAt: created,
406
+ fetchedAt
407
+ });
408
+ continue;
409
+ }
410
+
411
+ if (op === 'merge_pull_request' || op === 'create_pull_request' || op === 'pull_request') {
412
+ const num = prNumberOf(entry);
413
+ activities.push({
414
+ type: 'pr',
415
+ actor,
416
+ repo: full,
417
+ branch: null,
418
+ sha: null,
419
+ title: num ? `PR #${num}` : op, // 无号:title=op_type 原文
420
+ messageBody: '',
421
+ url: num ? (html ? `${html}/pulls/${num}` : '') : String(html || ''), // 无号:url=repo.html_url
422
+ occurredAt: created,
423
+ fetchedAt
424
+ });
425
+ continue;
426
+ }
427
+
428
+ skippedUnknownOps += 1;
429
+ if (op) unknownOps.add(op);
430
+ }
431
+ return { activities, skippedUnknownOps, unknownOpTypes: [...unknownOps] };
432
+ }
433
+
434
+ /**
435
+ * fixture 数据 → 原始事件映射结果(不做 7 天窗口过滤:fixture 为策展数据,sinceMs=-Infinity)。
436
+ * 新格式 {version:{version}, repos:[{full_name,html_url,default_branch}], feeds:{"<full_name>":[条目…]}}:
437
+ * 各仓库条目走与真实模式同一 mapFeedEntries;version 键继续供 testConnection 用。
438
+ */
439
+ function activitiesFromFixture(fx, fetchedAt) {
440
+ const activities = [];
441
+ let skippedUnknownOps = 0;
442
+ const unknownOps = new Set();
443
+ for (const r of fx.repos || []) {
444
+ const full = r.full_name || `${(r.owner && r.owner.login) || ''}/${r.name}`;
445
+ const entries = (fx.feeds && fx.feeds[full]) || [];
446
+ const mapped = mapFeedEntries(entries, r, -Infinity, fetchedAt);
447
+ activities.push(...mapped.activities);
448
+ skippedUnknownOps += mapped.skippedUnknownOps;
449
+ for (const op of mapped.unknownOpTypes) unknownOps.add(op);
450
+ }
451
+ return { activities, skippedUnknownOps, unknownOpTypes: [...unknownOps] };
452
+ }
453
+
454
+ // ---------------------------------------------------------------- persist
455
+
456
+ /**
457
+ * §4 activity 恰好 11 字段(v2.0 功能B 增补 sha/messageBody;matchedTaskIds 仍永不出现在此)。
458
+ * actor 允许 null(branch 事件无操作者);存量已落盘的 'unknown' 原样保留(容错)。
459
+ */
460
+ function strictActivity(a) {
461
+ return {
462
+ id: String(a.id),
463
+ type: String(a.type),
464
+ actor: a.actor == null ? null : String(a.actor),
465
+ repo: a.repo == null ? null : String(a.repo),
466
+ branch: a.branch == null ? null : String(a.branch),
467
+ sha: a.sha == null ? null : String(a.sha),
468
+ title: String(a.title || ''),
469
+ messageBody: a.messageBody == null ? '' : String(a.messageBody),
470
+ url: String(a.url || ''),
471
+ occurredAt: String(a.occurredAt),
472
+ fetchedAt: String(a.fetchedAt)
473
+ };
474
+ }
475
+
476
+ /** 合并去重(键 type|url)→ occurredAt 倒序 → 滚动 ≤max;已有条目保号,新条目递增序号 */
477
+ function mergeActivities(existing, incoming, max) {
478
+ const byKey = new Map();
479
+ let maxSeq = 0;
480
+ for (const a of existing || []) {
481
+ const key = `${a.type}|${a.url}`;
482
+ if (!byKey.has(key) && a.url) byKey.set(key, a);
483
+ const m = /^a-(\d+)$/.exec(String(a.id || ''));
484
+ if (m) maxSeq = Math.max(maxSeq, Number(m[1]));
485
+ }
486
+ const ordered = [...incoming].sort((x, y) => Date.parse(x.occurredAt) - Date.parse(y.occurredAt));
487
+ for (const a of ordered) {
488
+ if (!a.url) continue;
489
+ const key = `${a.type}|${a.url}`;
490
+ const prev = byKey.get(key);
491
+ if (prev) {
492
+ prev.occurredAt = a.occurredAt;
493
+ prev.fetchedAt = a.fetchedAt;
494
+ prev.title = a.title;
495
+ prev.sha = a.sha; // v2.0 功能B:刷新时同步新字段(存量旧条目再轮询时自然补齐)
496
+ prev.messageBody = a.messageBody;
497
+ continue;
498
+ }
499
+ maxSeq += 1;
500
+ byKey.set(key, { ...a, id: `a-${maxSeq}` });
501
+ }
502
+ return [...byKey.values()]
503
+ .sort((x, y) => Date.parse(y.occurredAt) - Date.parse(x.occurredAt))
504
+ .slice(0, max)
505
+ .map(strictActivity);
506
+ }
507
+
508
+ /** 原子写(tmp + renameSync),SPEC §2 选型口径 */
509
+ function atomicWrite(file, data) {
510
+ const tmp = `${file}.tmp-${process.pid}-${Math.random().toString(36).slice(2, 8)}`;
511
+ fs.writeFileSync(tmp, data);
512
+ fs.renameSync(tmp, file);
513
+ }
514
+
515
+ function resolveDataPath(cfg, name) {
516
+ const dir = path.isAbsolute(cfg.dataDir) ? cfg.dataDir : path.join(PROJECT_ROOT, cfg.dataDir);
517
+ fs.mkdirSync(dir, { recursive: true });
518
+ return path.join(dir, name);
519
+ }
520
+
521
+ function readExistingActivities(cfg) {
522
+ // v2.9.33 批次 2:读走 store 路由(activities 迁 SQLite 后这里必须读 db——
523
+ // fs 直读会拿空基线 merge,轮询一次即丢失全部历史)
524
+ try {
525
+ return require('./store.js').loadActivities({ activitiesPath: resolveDataPath(cfg, 'activities.json') });
526
+ } catch {
527
+ return [];
528
+ }
529
+ }
530
+
531
+ // -------------------------------------------------------------------- API
532
+
533
+ /**
534
+ * 轮询组织仓库最近 7 天 feeds 事件(Gitea activities/feeds)→ §4 activity 数组。
535
+ * 滚动 ≤maxActivities 写 data/activities.json,同时写 data/gitea-status.json
536
+ * (additive 计数字段 skippedUnknownOps = 本轮因未知 op_type 跳过的条目总数)。
537
+ * GITEA_FIXTURE 设置时读 fixture 替代网络(零请求,且不做窗口过滤:sinceMs=-Infinity)。
538
+ * @param {object} [cfg] 缺省 loadConfig()
539
+ * @returns {Promise<object[]>} 落盘后的 activity 数组(倒序)
540
+ * @throws HOST_NOT_ALLOWED / DANGEROUS_WHITELIST_ENTRY / GITEA_ERROR(message 已脱敏)
541
+ */
542
+ async function pollActivities(cfg) {
543
+ const conf = cfg || loadConfig();
544
+ validateWhitelist(conf); // 白名单体检:危险字面量直接拒绝(§5-②)
545
+
546
+ const fetchedAt = new Date().toISOString();
547
+ const sinceMs = Date.now() - SEVEN_DAYS_MS;
548
+ const types = new Set(normalizeActivityTypes(conf.gitea.activityTypes)); // 使用点归一:cfg 可经 configProvider 注入,绕过 loadConfig
549
+ const feedsLimit = Number(conf.gitea.feedsLimit) > 0 ? Number(conf.gitea.feedsLimit) : 50;
550
+ let rawEvents = [];
551
+ let skippedUnknownOps = 0; // 本轮未知 op_type 跳过总数(additive,进 gitea-status.json)
552
+ const unknownOps = new Set(); // 本轮 distinct 未知 op_type 名单
553
+ let ok = true;
554
+ let lastError = null;
555
+ let guardCode = null; // 守卫错误码透传(HOST_NOT_ALLOWED 等),供路由映射 400
556
+
557
+ try {
558
+ const fx = readFixture();
559
+ if (fx) {
560
+ const mapped = activitiesFromFixture(fx, fetchedAt);
561
+ rawEvents = mapped.activities;
562
+ skippedUnknownOps += mapped.skippedUnknownOps;
563
+ for (const op of mapped.unknownOpTypes) unknownOps.add(op);
564
+ } else {
565
+ assertSafeUrl(conf.gitea.baseUrl, conf); // 出站前的首道防线(§5-②,快速失败)
566
+ const credential = readCredential(conf);
567
+ const api = `${String(conf.gitea.baseUrl).replace(/\/+$/, '')}${conf.gitea.apiPrefix || '/api/v1'}`;
568
+ const repos = await listAll(
569
+ `${api}/orgs/${encodeURIComponent(conf.gitea.org)}/repos`, credential, conf, { maxPages: 10 }
570
+ );
571
+ const wanted = repos
572
+ .map((r) => r.full_name || `${(r.owner && r.owner.login) || ''}/${r.name}`)
573
+ .filter(Boolean)
574
+ .slice(0, 50);
575
+ for (const full of wanted) {
576
+ const enc = full.split('/').map(encodeURIComponent).join('/');
577
+ const repo = repos.find((r) => (r.full_name || '') === full) || null;
578
+ // 单仓库失败不拖垮整轮:该仓库 feeds 记 0 条,最终 ok 仍为 false 上报
579
+ const entries = await listAll(
580
+ `${api}/repos/${enc}/activities/feeds?only-performed-by=false`,
581
+ credential, conf,
582
+ {
583
+ maxPages: 2, // 页数兜底
584
+ perPage: feedsLimit,
585
+ stopWhen: (arr) => { // 本页末条目 created 早于窗口起点 → 提前停翻
586
+ const t = Date.parse(arr[arr.length - 1] && arr[arr.length - 1].created);
587
+ return Number.isFinite(t) && t < sinceMs;
588
+ }
589
+ }
590
+ ).catch(() => null);
591
+ if (entries === null) {
592
+ ok = false;
593
+ lastError = lastError || redact(`Gitea 拉取失败: ${full}`);
594
+ }
595
+ const mapped = mapFeedEntries(entries || [], repo || { full_name: full, html_url: '' }, sinceMs, fetchedAt);
596
+ rawEvents.push(...mapped.activities);
597
+ skippedUnknownOps += mapped.skippedUnknownOps;
598
+ for (const op of mapped.unknownOpTypes) unknownOps.add(op);
599
+ }
600
+ }
601
+ } catch (err) {
602
+ ok = false;
603
+ lastError = redact((err && err.message) || String(err));
604
+ if (err && (err.code === 'HOST_NOT_ALLOWED' || err.code === 'DANGEROUS_WHITELIST_ENTRY')) {
605
+ guardCode = err.code; // 末级复检/白名单体检失败 → 保留守卫码,让路由映射 400 而非 502
606
+ }
607
+ }
608
+
609
+ // 每轮 poll 结束:对 distinct 未知 op_type 各 warn 一次(文案经 redact)
610
+ for (const op of unknownOps) {
611
+ console.warn(`[gitea] feeds 未知 op_type 已跳过: ${redact(op)}`);
612
+ }
613
+
614
+ const filtered = rawEvents.filter((e) => types.has(e.type));
615
+ // 存量条目老词表原地改名:type|url 去重键随改名对齐新词表,同 url 的新拉条目直接坍缩进既有条目
616
+ // (不丢任何历史;pr_opened/pr_merged 同 url 并入 pr,合并态语义与现役 mapper 口径一致)
617
+ const existing = readExistingActivities(conf).map((a) => (
618
+ LEGACY_ACTIVITY_TYPE_MAP[a.type] ? { ...a, type: LEGACY_ACTIVITY_TYPE_MAP[a.type] } : a
619
+ ));
620
+ const merged = mergeActivities(existing, filtered, conf.gitea.maxActivities || 500);
621
+
622
+ // v2.9.33 批次 2:activities 写统一出口(存储迁移后经 store.saveActivities 路由)
623
+ require('./store.js').saveActivities({ activitiesPath: resolveDataPath(conf, 'activities.json') }, merged);
624
+ atomicWrite(
625
+ resolveDataPath(conf, 'gitea-status.json'),
626
+ JSON.stringify(
627
+ { ok, lastPollAt: fetchedAt, lastError: lastError === null ? null : String(lastError), skippedUnknownOps },
628
+ null,
629
+ 2
630
+ )
631
+ );
632
+
633
+ // v1.9 分支约定切片:轮询落盘后按精确规则自动归因(lib/attribute.js)。
634
+ // 惰性 require 防模块环;归因失败只记日志——绝不影响轮询成败与已落盘的 activities/status。
635
+ try {
636
+ require('./attribute.js').attributeAfterPoll(conf);
637
+ } catch (err) {
638
+ console.error('[attribute] 自动归因失败(不影响轮询):', redact((err && err.message) || String(err)));
639
+ }
640
+
641
+ if (!ok) {
642
+ const err = new Error(lastError || 'Gitea 拉取失败');
643
+ err.code = guardCode || 'GITEA_ERROR';
644
+ throw err;
645
+ }
646
+ return merged;
647
+ }
648
+
649
+ // ------------------------------------------------------------------- repos
650
+
651
+ /**
652
+ * GET /api/repos 仓库列表缓存(模块级,同进程共享):键 `${baseUrl}|${org}`,值 {at, list}。
653
+ * 仅成功结果入缓存;TTL 内重复调用直接返回缓存(不发请求)。测试经 clearReposCache() 清空。
654
+ */
655
+ const reposCache = new Map();
656
+ const REPOS_CACHE_TTL_MS = 5 * 60 * 1000; // 5 分钟
657
+
658
+ /**
659
+ * 仓库 → 恰 4 字段视图(mock 契约口径,web/js/views/board.js 仓库下拉消费):
660
+ * fullName = full_name 缺省回退 `${owner.login}/${name}`(与 pollActivities 现有口径一致);
661
+ * name = repo.name(fixture 策展数据缺 name 时回退 full_name 第二段,保证恰 4 字段完整);
662
+ * private = !!repo.private;updatedAt = repo.updated_at || null。
663
+ */
664
+ function repoView(r) {
665
+ const fullName = r.full_name || `${(r.owner && r.owner.login) || ''}/${r.name}`;
666
+ const name = r.name || String(fullName).split('/')[1] || '';
667
+ return {
668
+ fullName,
669
+ name,
670
+ private: !!r.private,
671
+ updatedAt: r.updated_at || null
672
+ };
673
+ }
674
+
675
+ /**
676
+ * 列出配置 Gitea 组织下全部仓库(只读,GET /api/repos 后端;任务表单仓库下拉数据源)。
677
+ * fixture 模式读 fixture repos 数组映射(零出网);真机模式 assertSafeUrl 首检 → readCredential →
678
+ * listAll 分页拉全 /orgs/{org}/repos(maxPages 10,与 pollActivities 同参数风格)→ 映射。
679
+ * 只读红线:绝不写 data/ 任何文件、不写 config。
680
+ * @param {object} [cfg] 缺省 loadConfig()
681
+ * @returns {Promise<object[]>} 恰 4 字段(fullName/name/private/updatedAt)的仓库数组
682
+ * @throws HOST_NOT_ALLOWED / DANGEROUS_WHITELIST_ENTRY(原码原样抛出)/ GITEA_ERROR(message 已脱敏)
683
+ */
684
+ async function listOrgRepos(cfg) {
685
+ const conf = cfg || loadConfig();
686
+ validateWhitelist(conf); // 白名单体检:危险字面量直接拒绝(§5-②,沿用 pollActivities 模式)
687
+
688
+ const fx = readFixture();
689
+ if (fx) return (fx.repos || []).map(repoView); // fixture 模式:零出网
690
+
691
+ const base = String(conf.gitea.baseUrl || '');
692
+ const org = String(conf.gitea.org || '');
693
+ const key = `${base}|${org}`;
694
+ const hit = reposCache.get(key);
695
+ if (hit && Date.now() - hit.at < REPOS_CACHE_TTL_MS) return hit.list; // TTL 内不发请求
696
+
697
+ try {
698
+ assertSafeUrl(conf.gitea.baseUrl, conf); // 出站前的首道防线(§5-②,快速失败)
699
+ const credential = readCredential(conf);
700
+ const api = `${base.replace(/\/+$/, '')}${conf.gitea.apiPrefix || '/api/v1'}`;
701
+ const repos = await listAll(
702
+ `${api}/orgs/${encodeURIComponent(org)}/repos`, credential, conf, { maxPages: 10 }
703
+ );
704
+ const list = repos.map(repoView);
705
+ reposCache.set(key, { at: Date.now(), list }); // 仅成功结果入缓存
706
+ return list;
707
+ } catch (err) {
708
+ if (err && (err.code === 'HOST_NOT_ALLOWED' || err.code === 'DANGEROUS_WHITELIST_ENTRY')) throw err;
709
+ const wrapped = new Error(redact((err && err.message) || String(err)));
710
+ wrapped.code = 'GITEA_ERROR';
711
+ throw wrapped;
712
+ }
713
+ }
714
+
715
+ /** 清空仓库列表缓存(测试隔离用:缓存为模块级,同进程所有 app 共享) */
716
+ function clearReposCache() {
717
+ reposCache.clear();
718
+ }
719
+
720
+ /**
721
+ * 连接测试(§3 POST /api/gitea/test 后端)。
722
+ * 白名单外/协议外:任何出站前抛 HOST_NOT_ALLOWED;fixture 模式零网络返回版本。
723
+ * 凭据最小化(qoder #11):仅当目标 host 与 config.gitea.baseUrl 的 host 归一化后
724
+ * 相同时才附 Authorization;异 host 只裸请求(白名单先拦是第一道,这里是纵深)。
725
+ * @param {string} baseUrl
726
+ * @param {object} [cfg]
727
+ * @returns {Promise<{ok:boolean,latencyMs:number,apiVersion:string|null,error?:string}>}
728
+ */
729
+ async function testConnection(baseUrl, cfg) {
730
+ const conf = cfg || loadConfig();
731
+ const target = assertSafeUrl(baseUrl || conf.gitea.baseUrl, conf); // 违规即抛,零出站
732
+ const started = Date.now();
733
+
734
+ const fx = readFixture();
735
+ if (fx) {
736
+ const version = (fx.version && fx.version.version) || null;
737
+ return { ok: true, latencyMs: Date.now() - started, apiVersion: version };
738
+ }
739
+
740
+ let baseHost = '';
741
+ try {
742
+ baseHost = normalizeHost(new URL(String(conf.gitea.baseUrl)).hostname);
743
+ } catch { /* baseUrl 非法 → 视为异 host,不附凭据 */ }
744
+ const targetHost = normalizeHost(target.hostname);
745
+ const credential = targetHost === baseHost ? readCredential(conf) : null;
746
+ const api = `${target.origin}${conf.gitea.apiPrefix || '/api/v1'}`;
747
+ try {
748
+ const data = await giteaFetch(`${api}/version`, credential, conf, 5000);
749
+ return { ok: true, latencyMs: Date.now() - started, apiVersion: (data && data.version) || null };
750
+ } catch (err) {
751
+ return {
752
+ ok: false,
753
+ latencyMs: Date.now() - started,
754
+ apiVersion: null,
755
+ error: redact((err && err.message) || String(err))
756
+ };
757
+ }
758
+ }
759
+
760
+ /**
761
+ * §3 GET /api/settings 脱敏视图(credential 只报来源名,绝不含凭据值)。
762
+ * @param {object} [cfg]
763
+ */
764
+ function getSettingsView(cfg) {
765
+ const conf = cfg || loadConfig();
766
+ const g = conf.gitea;
767
+ return {
768
+ port: conf.port,
769
+ host: conf.host,
770
+ dataDir: conf.dataDir,
771
+ gitea: {
772
+ baseUrl: g.baseUrl,
773
+ apiPrefix: g.apiPrefix,
774
+ org: g.org,
775
+ pollIntervalSec: g.pollIntervalSec,
776
+ staleWarnSec: g.staleWarnSec,
777
+ staleDangerSec: g.staleDangerSec,
778
+ hostWhitelist: [...(g.hostWhitelist || [])],
779
+ activityTypes: [...(g.activityTypes || [])],
780
+ ignoreLogins: [...(g.ignoreLogins || [])],
781
+ feedsLimit: g.feedsLimit,
782
+ maxActivities: g.maxActivities
783
+ },
784
+ // v2.9 GitHub 源视图(enabled/baseUrl/org 无敏感值;凭据恒走 GIT_CONFIG_EXTRAHEADER)
785
+ github: {
786
+ enabled: !!(conf.github && conf.github.enabled === true),
787
+ baseUrl: conf.github ? conf.github.baseUrl : '',
788
+ org: conf.github ? conf.github.org : ''
789
+ },
790
+ credential: 'GIT_CONFIG_EXTRAHEADER'
791
+ };
792
+ }
793
+
794
+ module.exports = {
795
+ loadConfig,
796
+ readCredential,
797
+ readCredentialFromHelper,
798
+ credentialHelperHeader,
799
+ pollActivities,
800
+ testConnection,
801
+ getSettingsView,
802
+ listOrgRepos,
803
+ clearReposCache,
804
+ DEFAULTS,
805
+ // v2.9 GitHub 源复用的内部工具(同一 activities.json 合并落盘,口径必须完全一致)
806
+ mergeActivities,
807
+ readExistingActivities,
808
+ resolveDataPath,
809
+ atomicWrite
810
+ };