@jiyr0119/dsh-service-console 0.9.0 → 0.11.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/README.md +22 -6
- package/README.zh.md +8 -3
- package/assets/service-console-0.11.0.png +0 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/awesome-submission.yml +7 -9
- package/docs/compatibility.zh.md +24 -0
- package/docs/preview.svg +65 -0
- package/docs/rc5-upgrade-review.zh.md +16 -0
- package/docs/troubleshooting.zh.md +33 -0
- package/docs/user-guide.zh.md +33 -0
- package/icon.svg +4 -4
- package/lib/client/dictionaries.js +12 -0
- package/lib/client/locales/de.js +91 -0
- package/lib/client/locales/es.js +91 -0
- package/lib/client/locales/fr.js +91 -0
- package/lib/client/locales/ja.js +91 -0
- package/lib/client/locales/pt.js +91 -0
- package/lib/client/locales/ru.js +91 -0
- package/lib/client.js +641 -66
- package/lib/host/config-schema.js +45 -0
- package/lib/host/config.js +80 -27
- package/lib/host/ledger-projection.js +223 -0
- package/lib/host/ownership.js +2 -1
- package/lib/host/process-inspector.js +174 -49
- package/lib/host/service-registry.js +5 -4
- package/lib/host/service-tool.js +3 -1
- package/lib/host/snapshot-cache.js +59 -0
- package/lib/index.js +157 -24
- package/lib/types/client/ServiceConsole.d.ts +2 -0
- package/lib/types/client/dictionaries.d.ts +12 -0
- package/lib/types/client/locales/de.d.ts +89 -0
- package/lib/types/client/locales/es.d.ts +89 -0
- package/lib/types/client/locales/fr.d.ts +89 -0
- package/lib/types/client/locales/ja.d.ts +89 -0
- package/lib/types/client/locales/pt.d.ts +89 -0
- package/lib/types/client/locales/ru.d.ts +89 -0
- package/lib/types/host/config-schema.d.ts +52 -0
- package/lib/types/host/config.d.ts +27 -2
- package/lib/types/host/ledger-projection.d.ts +98 -0
- package/lib/types/host/process-inspector.d.ts +51 -2
- package/lib/types/host/service-registry.d.ts +1 -1
- package/lib/types/host/service-tool.d.ts +1 -1
- package/lib/types/host/snapshot-cache.d.ts +39 -0
- package/lib/types/host/types.d.ts +9 -0
- package/lib/types/index.d.ts +26 -1
- package/locale/de.json +7 -0
- package/locale/en.json +2 -1
- package/locale/es.json +7 -0
- package/locale/fr.json +7 -0
- package/locale/ja.json +7 -0
- package/locale/pt.json +7 -0
- package/locale/ru.json +7 -0
- package/locale/zh.json +2 -1
- package/package.json +16 -7
- package/assets/service-console-0.2.0.gif +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import z from "@deepseek-ai/schemastery";
|
|
2
|
+
// src/host/config-schema.ts
|
|
3
|
+
/**
|
|
4
|
+
* 插件配置 schema(rc.5 契约:`Config` 由 Loader 校验组合行里的 `config:`)
|
|
5
|
+
*
|
|
6
|
+
* 与 `config.ts` 的关系:
|
|
7
|
+
* - 本文件是**唯一声明**(默认值 + 取值范围 + 类型),供 `cordis.yml` 的 `config:` 行、
|
|
8
|
+
* Loader 校验、以及运行时对 JSON 覆盖文件的二次校验共用。
|
|
9
|
+
* - `config.ts` 负责「默认值 ← UI 覆盖文件 ← 组合行」的合并与读写。
|
|
10
|
+
*
|
|
11
|
+
* 注意:这里**不使用** `.volatile()`。volatile 字段会进入 rc.5 的 Settings → Plugins
|
|
12
|
+
* 表单(写回 profile patch),而我们面板的设置页写的是插件自己的 JSON 文件——两条写入
|
|
13
|
+
* 路径会互相覆盖。保持单一写入者:面板(JSON),组合行只做部署期覆盖。
|
|
14
|
+
*/
|
|
15
|
+
/** 自动刷新档位(PRD FR-07:关闭 / 2s / 5s / 10s / 30s) */
|
|
16
|
+
const REFRESH_INTERVAL_CHOICES = [
|
|
17
|
+
0,
|
|
18
|
+
2e3,
|
|
19
|
+
5e3,
|
|
20
|
+
1e4,
|
|
21
|
+
3e4
|
|
22
|
+
];
|
|
23
|
+
const Config = z.object({
|
|
24
|
+
/** 自动刷新间隔(毫秒,仅上述离散档位;0 表示关闭) */
|
|
25
|
+
refreshInterval: z.number().min(0).max(3e4).default(5e3),
|
|
26
|
+
/** 面板默认范围 */
|
|
27
|
+
defaultScope: z.union([
|
|
28
|
+
z.const("conversation"),
|
|
29
|
+
z.const("workspace"),
|
|
30
|
+
z.const("machine")
|
|
31
|
+
]).default("workspace"),
|
|
32
|
+
/** 优雅停止超时(毫秒) */
|
|
33
|
+
gracefulTimeout: z.number().min(1e3).max(15e3).default(3e3),
|
|
34
|
+
/** 是否展示归属未知的服务 */
|
|
35
|
+
showUnknown: z.boolean().default(true),
|
|
36
|
+
/** 允许 SIGKILL 兜底 */
|
|
37
|
+
forceKill: z.boolean().default(false),
|
|
38
|
+
/** 关闭面板时是否自动清理(当前恒为 false,保留字段以兼容旧配置文件) */
|
|
39
|
+
autoCleanup: z.boolean().default(false),
|
|
40
|
+
/** 一次扫描请求的预算(毫秒):HTTP deadline 与 service_list 的工具预算共用 */
|
|
41
|
+
scanTimeoutMs: z.number().min(1e3).max(3e4).default(8e3),
|
|
42
|
+
/** 单条宿主命令的预算(毫秒) */
|
|
43
|
+
commandTimeoutMs: z.number().min(500).max(15e3).default(5e3)
|
|
44
|
+
});
|
|
45
|
+
export { Config, REFRESH_INTERVAL_CHOICES };
|
package/lib/host/config.js
CHANGED
|
@@ -1,44 +1,86 @@
|
|
|
1
|
+
import { Config, REFRESH_INTERVAL_CHOICES } from "./config-schema.js";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
1
3
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
4
|
import { homedir } from "node:os";
|
|
3
|
-
import { dirname, join } from "node:path";
|
|
4
5
|
// src/host/config.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
defaultScope: "workspace",
|
|
8
|
-
gracefulTimeout: 3e3,
|
|
9
|
-
showUnknown: true,
|
|
10
|
-
forceKill: false,
|
|
11
|
-
autoCleanup: false
|
|
12
|
-
};
|
|
13
|
-
const REFRESH_INTERVAL_CHOICES = [
|
|
14
|
-
0,
|
|
15
|
-
2e3,
|
|
16
|
-
5e3,
|
|
17
|
-
1e4,
|
|
18
|
-
3e4
|
|
19
|
-
];
|
|
6
|
+
/** schema 默认值(唯一声明处) */
|
|
7
|
+
const DEFAULT_CONFIG = Config({});
|
|
20
8
|
const CONFIG_LIMITS = { gracefulTimeout: [1e3, 15e3] };
|
|
21
9
|
function configFilePath() {
|
|
22
10
|
return join(homedir(), ".dsh", "plugins", "dsh-service-console", "config.json");
|
|
23
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* 读取 UI 覆盖文件,并用同一份 schema 校验/补默认值。
|
|
14
|
+
* @returns 合并后的配置(坏文件退化为默认值,不抛)
|
|
15
|
+
*/
|
|
24
16
|
function loadConfigFromFile() {
|
|
17
|
+
return normalizeConfig(readConfigFile());
|
|
18
|
+
}
|
|
19
|
+
/** 读原始文件(失败返回空对象) */
|
|
20
|
+
function readConfigFile() {
|
|
25
21
|
try {
|
|
26
22
|
const file = configFilePath();
|
|
27
|
-
if (!existsSync(file)) return {
|
|
23
|
+
if (!existsSync(file)) return {};
|
|
28
24
|
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
29
|
-
return {
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
return typeof parsed === "object" && parsed !== null ? parsed : {};
|
|
26
|
+
} catch {
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* schema 校验 + 合并:schema 默认值 ← 组合行(部署基线)← UI 覆盖文件(用户显式选择)。
|
|
32
|
+
*
|
|
33
|
+
* 为什么 UI 覆盖文件优先:rc.5 的 Loader 总是把**补全后的完整** config 传给
|
|
34
|
+
* `apply`,所以「组合行最高」会让文件永远不可达(用户改设置无效)。这里的语义是
|
|
35
|
+
* 「部署行给基线,用户在面板里的显式修改压过它」——面板是该插件唯一的写入者,
|
|
36
|
+
* 不存在两个写入路径互相覆盖的问题(rc.5 的 Settings→Plugins 表单只投影
|
|
37
|
+
* `.volatile()` 字段,本插件未声明 volatile,故不会出现第二个写入者)。
|
|
38
|
+
* @param overrides UI 覆盖文件内容
|
|
39
|
+
* @param rowConfig cordis.yml 组合行里的 `config:`
|
|
40
|
+
*/
|
|
41
|
+
function normalizeConfig(overrides = {}, rowConfig = {}) {
|
|
42
|
+
const base = {
|
|
43
|
+
...DEFAULT_CONFIG,
|
|
44
|
+
...stripUnknown(rowConfig)
|
|
45
|
+
};
|
|
46
|
+
const safeOverrides = {};
|
|
47
|
+
for (const [key, value] of Object.entries(stripUnknown(overrides))) {
|
|
48
|
+
const candidate = {
|
|
49
|
+
...base,
|
|
50
|
+
[key]: value
|
|
32
51
|
};
|
|
52
|
+
try {
|
|
53
|
+
safeOverrides[key] = Config(candidate)[key];
|
|
54
|
+
} catch {}
|
|
55
|
+
}
|
|
56
|
+
const merged = {
|
|
57
|
+
...base,
|
|
58
|
+
...safeOverrides
|
|
59
|
+
};
|
|
60
|
+
try {
|
|
61
|
+
const validated = Config(merged);
|
|
62
|
+
if (!REFRESH_INTERVAL_CHOICES.includes(validated.refreshInterval)) validated.refreshInterval = DEFAULT_CONFIG.refreshInterval;
|
|
63
|
+
return validated;
|
|
33
64
|
} catch {
|
|
34
65
|
return { ...DEFAULT_CONFIG };
|
|
35
66
|
}
|
|
36
67
|
}
|
|
37
|
-
|
|
68
|
+
/** 只保留 schema 声明的键(未知键忽略而不是写进运行时) */
|
|
69
|
+
function stripUnknown(input) {
|
|
70
|
+
const out = {};
|
|
71
|
+
for (const key of Object.keys(DEFAULT_CONFIG)) if (key in input) out[key] = input[key];
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 持久化 **UI 覆盖补丁**(不是合并后的配置)。
|
|
76
|
+
* 只写用户真正改过的字段:把合并结果写回文件会把部署行的值冻结成用户覆盖,
|
|
77
|
+
* 之后组合行的修改就再也影响不到运行时。
|
|
78
|
+
*/
|
|
79
|
+
function saveConfigToFile(overrides) {
|
|
38
80
|
try {
|
|
39
81
|
const file = configFilePath();
|
|
40
82
|
mkdirSync(dirname(file), { recursive: true });
|
|
41
|
-
writeFileSync(file, JSON.stringify(
|
|
83
|
+
writeFileSync(file, JSON.stringify(overrides, null, 2), "utf8");
|
|
42
84
|
} catch {}
|
|
43
85
|
}
|
|
44
86
|
function validateConfigPatch(patch) {
|
|
@@ -49,10 +91,6 @@ function validateConfigPatch(patch) {
|
|
|
49
91
|
message: "unknown config key: " + key
|
|
50
92
|
};
|
|
51
93
|
const v = patch[key];
|
|
52
|
-
if (typeof v === "boolean") {
|
|
53
|
-
next[key] = v;
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
94
|
if (key === "refreshInterval") {
|
|
57
95
|
if (typeof v === "number" && REFRESH_INTERVAL_CHOICES.includes(v)) {
|
|
58
96
|
next.refreshInterval = v;
|
|
@@ -75,14 +113,29 @@ function validateConfigPatch(patch) {
|
|
|
75
113
|
next.defaultScope = v;
|
|
76
114
|
continue;
|
|
77
115
|
}
|
|
116
|
+
if (typeof v === "boolean") {
|
|
117
|
+
next[key] = v;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
78
120
|
return {
|
|
79
121
|
ok: false,
|
|
80
122
|
message: "invalid value for: " + key
|
|
81
123
|
};
|
|
82
124
|
}
|
|
125
|
+
try {
|
|
126
|
+
Config({
|
|
127
|
+
...DEFAULT_CONFIG,
|
|
128
|
+
...next
|
|
129
|
+
});
|
|
130
|
+
} catch (error) {
|
|
131
|
+
return {
|
|
132
|
+
ok: false,
|
|
133
|
+
message: "invalid config: " + String(error?.message ?? error)
|
|
134
|
+
};
|
|
135
|
+
}
|
|
83
136
|
return {
|
|
84
137
|
ok: true,
|
|
85
138
|
next
|
|
86
139
|
};
|
|
87
140
|
}
|
|
88
|
-
export { CONFIG_LIMITS, DEFAULT_CONFIG, REFRESH_INTERVAL_CHOICES, loadConfigFromFile, saveConfigToFile, validateConfigPatch };
|
|
141
|
+
export { CONFIG_LIMITS, DEFAULT_CONFIG, REFRESH_INTERVAL_CHOICES, loadConfigFromFile, normalizeConfig, readConfigFile, saveConfigToFile, validateConfigPatch };
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { DEV_COMMAND_RE } from "./ledger.js";
|
|
2
|
+
import { isAbsolute, join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
// src/host/ledger-projection.ts
|
|
5
|
+
/**
|
|
6
|
+
* M10 · 会话启动台账的 durable projection(rc.5 `ctx.sessionProjections`)
|
|
7
|
+
*
|
|
8
|
+
* 为什么迁:内存台账在宿主重启后丢失、也不 fork-aware。会话日志里已经记录了
|
|
9
|
+
* 每次工具调用,因此台账可以纯函数地折叠出来:
|
|
10
|
+
* - **重启**:projection 由日志重建(含 checkpoint 加速),归属仍正确;
|
|
11
|
+
* - **fork**:子会话继承父会话事件前缀,折叠后同一血脉的启动记录都算数;
|
|
12
|
+
* - **纯函数**:`apply` 同步、无关事件返回同一引用(框架依赖 Object.is)。
|
|
13
|
+
*
|
|
14
|
+
* 记录时机是**结算**而不是发起:`tool/call` 发生在 pre-execute 策略之前,
|
|
15
|
+
* 被拒绝/取消的调用不该进台账。因此 `tool/call` 先进 `pending`,只有对应的
|
|
16
|
+
* `tool/result`(非 isError)才落成条目。PTC 模式(`run_code` 里的子调用)走
|
|
17
|
+
* `tool/ptc-dispatch-start` / `tool/ptc-dispatch` 这套独立词汇,同样折叠——否则
|
|
18
|
+
* PTC 用户起的服务会完全失去归属。
|
|
19
|
+
*
|
|
20
|
+
* 保留 `ledger.ts` 的内存实现作为降级路径(没有 projection 服务、会话不可读、
|
|
21
|
+
* 未恢复的冷会话),插件其余功能不因此失效。
|
|
22
|
+
*/
|
|
23
|
+
/** 每个会话保留的台账条数上限(旧条目先丢) */
|
|
24
|
+
const MAX_LEDGER_ENTRIES = 200;
|
|
25
|
+
/** 未结算调用(call 已记录、result 还没到)的上限 */
|
|
26
|
+
const MAX_PENDING_CALLS = 32;
|
|
27
|
+
/** 条目保留时长(毫秒):开发服务的归属窗口远小于一天 */
|
|
28
|
+
const LEDGER_TTL_MS = 36e5;
|
|
29
|
+
const entrySchema = z.object({
|
|
30
|
+
at: z.number(),
|
|
31
|
+
command: z.string(),
|
|
32
|
+
cwd: z.string().nullable(),
|
|
33
|
+
sessionKey: z.string().nullable(),
|
|
34
|
+
redacted: z.string()
|
|
35
|
+
});
|
|
36
|
+
const pendingSchema = z.object({
|
|
37
|
+
id: z.string(),
|
|
38
|
+
at: z.number(),
|
|
39
|
+
command: z.string(),
|
|
40
|
+
cwd: z.string().nullable(),
|
|
41
|
+
redacted: z.string()
|
|
42
|
+
});
|
|
43
|
+
/** 状态 schema:projection 用它在 checkpoint 反序列化时校验(不可信输入) */
|
|
44
|
+
const ledgerStateSchema = z.object({
|
|
45
|
+
sessionKey: z.string(),
|
|
46
|
+
sessionCwd: z.string().nullable(),
|
|
47
|
+
entries: z.array(entrySchema),
|
|
48
|
+
pending: z.array(pendingSchema).default([])
|
|
49
|
+
});
|
|
50
|
+
function asCommandArgs(raw) {
|
|
51
|
+
let value = raw;
|
|
52
|
+
if (typeof raw === "string") {
|
|
53
|
+
if (raw === "") return null;
|
|
54
|
+
try {
|
|
55
|
+
value = JSON.parse(raw);
|
|
56
|
+
} catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (typeof value !== "object" || value === null) return null;
|
|
61
|
+
const rec = value;
|
|
62
|
+
const command = typeof rec.command === "string" ? rec.command : "";
|
|
63
|
+
if (command === "") return null;
|
|
64
|
+
return {
|
|
65
|
+
command,
|
|
66
|
+
workdir: typeof rec.workdir === "string" && rec.workdir !== "" ? rec.workdir : null
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** 相对 workdir 按会话当前目录解析(`cd sub && npm run dev` 这类常见写法) */
|
|
70
|
+
function resolveWorkdir(workdir, sessionCwd) {
|
|
71
|
+
if (workdir === null) return sessionCwd;
|
|
72
|
+
if (isAbsolute(workdir)) return workdir;
|
|
73
|
+
return sessionCwd === null ? null : join(sessionCwd, workdir);
|
|
74
|
+
}
|
|
75
|
+
/** 事件时间守卫:NaN/负数一律按「未知」处理(否则会污染 at 并被 schema 拒绝) */
|
|
76
|
+
function eventTime(event) {
|
|
77
|
+
const time = event.time;
|
|
78
|
+
return typeof time === "number" && Number.isFinite(time) && time > 0 ? time : Date.now();
|
|
79
|
+
}
|
|
80
|
+
/** 补一条结算后的条目(保留有界 + 按时间丢旧) */
|
|
81
|
+
function settled(state, pending) {
|
|
82
|
+
const cutoff = (pending.at || Date.now()) - LEDGER_TTL_MS;
|
|
83
|
+
const kept = state.entries.filter((entry) => entry.at >= cutoff);
|
|
84
|
+
kept.push({
|
|
85
|
+
at: pending.at,
|
|
86
|
+
command: pending.command,
|
|
87
|
+
cwd: pending.cwd,
|
|
88
|
+
sessionKey: state.sessionKey,
|
|
89
|
+
redacted: pending.redacted
|
|
90
|
+
});
|
|
91
|
+
if (kept.length > 200) kept.splice(0, kept.length - 200);
|
|
92
|
+
return {
|
|
93
|
+
...state,
|
|
94
|
+
entries: kept
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function rememberPending(state, entry) {
|
|
98
|
+
const pending = [...state.pending, entry];
|
|
99
|
+
if (pending.length > 32) pending.splice(0, pending.length - 32);
|
|
100
|
+
return {
|
|
101
|
+
...state,
|
|
102
|
+
pending
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function takePending(state, id) {
|
|
106
|
+
const index = state.pending.findIndex((entry) => entry.id === id);
|
|
107
|
+
if (index < 0) return {
|
|
108
|
+
hit: void 0,
|
|
109
|
+
rest: state.pending
|
|
110
|
+
};
|
|
111
|
+
const rest = state.pending.slice();
|
|
112
|
+
const [hit] = rest.splice(index, 1);
|
|
113
|
+
return {
|
|
114
|
+
hit,
|
|
115
|
+
rest
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 纯 reducer:一条已提交事件 → 新状态(无关事件返回**同一引用**)。
|
|
120
|
+
* @param redact 命令脱敏函数(注入以便单测;生产用 `redaction.redact`)
|
|
121
|
+
*/
|
|
122
|
+
function foldLedgerEvent(state, event, redact) {
|
|
123
|
+
const data = event.data ?? {};
|
|
124
|
+
const at = eventTime(event);
|
|
125
|
+
if (event.type === "working-directory/change") {
|
|
126
|
+
const cwd = typeof data.cwd === "string" && data.cwd.startsWith("/") ? data.cwd : null;
|
|
127
|
+
if (cwd === null || cwd === state.sessionCwd) return state;
|
|
128
|
+
return {
|
|
129
|
+
...state,
|
|
130
|
+
sessionCwd: cwd
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (event.type === "tool/call") {
|
|
134
|
+
if (data.name !== "bash") return state;
|
|
135
|
+
const args = asCommandArgs(data.arguments);
|
|
136
|
+
if (args === null || !DEV_COMMAND_RE.test(args.command)) return state;
|
|
137
|
+
const id = typeof data.callId === "string" ? data.callId : "";
|
|
138
|
+
if (id === "") return state;
|
|
139
|
+
return rememberPending(state, {
|
|
140
|
+
id,
|
|
141
|
+
at,
|
|
142
|
+
command: args.command,
|
|
143
|
+
cwd: resolveWorkdir(args.workdir, state.sessionCwd),
|
|
144
|
+
redacted: redact(args.command) ?? args.command
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (event.type === "tool/result") {
|
|
148
|
+
const message = typeof data.message === "object" && data.message !== null ? data.message : {};
|
|
149
|
+
const id = typeof message.toolCallId === "string" ? message.toolCallId : "";
|
|
150
|
+
if (id === "") return state;
|
|
151
|
+
const { hit, rest } = takePending(state, id);
|
|
152
|
+
if (hit === void 0) return state;
|
|
153
|
+
return message.isError === true ? {
|
|
154
|
+
...state,
|
|
155
|
+
pending: rest
|
|
156
|
+
} : { ...settled({
|
|
157
|
+
...state,
|
|
158
|
+
pending: rest
|
|
159
|
+
}, hit) };
|
|
160
|
+
}
|
|
161
|
+
if (event.type === "tool/ptc-dispatch-start") {
|
|
162
|
+
if (data.name !== "bash") return state;
|
|
163
|
+
const args = asCommandArgs(data.arguments);
|
|
164
|
+
if (args === null || !DEV_COMMAND_RE.test(args.command)) return state;
|
|
165
|
+
const id = typeof data.subCallId === "string" ? data.subCallId : "";
|
|
166
|
+
if (id === "") return state;
|
|
167
|
+
return rememberPending(state, {
|
|
168
|
+
id,
|
|
169
|
+
at,
|
|
170
|
+
command: args.command,
|
|
171
|
+
cwd: resolveWorkdir(args.workdir, state.sessionCwd),
|
|
172
|
+
redacted: redact(args.command) ?? args.command
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
if (event.type === "tool/ptc-dispatch") {
|
|
176
|
+
const id = typeof data.subCallId === "string" ? data.subCallId : "";
|
|
177
|
+
if (id === "") return state;
|
|
178
|
+
const { hit, rest } = takePending(state, id);
|
|
179
|
+
if (hit === void 0) return state;
|
|
180
|
+
return data.isError === true ? {
|
|
181
|
+
...state,
|
|
182
|
+
pending: rest
|
|
183
|
+
} : { ...settled({
|
|
184
|
+
...state,
|
|
185
|
+
pending: rest
|
|
186
|
+
}, hit) };
|
|
187
|
+
}
|
|
188
|
+
return state;
|
|
189
|
+
}
|
|
190
|
+
/** 初始状态(`init` 用):空台账 + 会话元数据 */
|
|
191
|
+
function initLedgerState(header) {
|
|
192
|
+
return {
|
|
193
|
+
sessionKey: header.id,
|
|
194
|
+
sessionCwd: typeof header.cwd === "string" && header.cwd !== "" ? header.cwd : null,
|
|
195
|
+
entries: [],
|
|
196
|
+
pending: []
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 构造 rc.5 的 `ProjectionDefinition`。
|
|
201
|
+
* @param redact 命令脱敏
|
|
202
|
+
* @returns 可直接交给 `ctx.sessionProjections.register(...)` 的定义
|
|
203
|
+
*/
|
|
204
|
+
function createLedgerProjection(redact) {
|
|
205
|
+
return {
|
|
206
|
+
key: "serviceConsoleLedger",
|
|
207
|
+
stateSchema: ledgerStateSchema,
|
|
208
|
+
stateVersion: 1,
|
|
209
|
+
init: (header) => initLedgerState(header),
|
|
210
|
+
apply: (state, event) => foldLedgerEvent(state, event, redact)
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* 从投影状态里取条目;形状不对时返回空数组(宁可少归属,也不抛)。
|
|
215
|
+
* @param state `stateOf(session)` 的返回值(unknown:可能来自旧版本 checkpoint)
|
|
216
|
+
*/
|
|
217
|
+
function entriesOf(state, now = Date.now()) {
|
|
218
|
+
const parsed = ledgerStateSchema.safeParse(state);
|
|
219
|
+
if (!parsed.success) return [];
|
|
220
|
+
const cutoff = now - LEDGER_TTL_MS;
|
|
221
|
+
return parsed.data.entries.filter((entry) => Number.isFinite(entry.at) && entry.at >= cutoff);
|
|
222
|
+
}
|
|
223
|
+
export { LEDGER_TTL_MS, MAX_LEDGER_ENTRIES, MAX_PENDING_CALLS, createLedgerProjection, entriesOf, foldLedgerEvent, initLedgerState, ledgerStateSchema };
|
package/lib/host/ownership.js
CHANGED
|
@@ -9,8 +9,9 @@ function classifyOwnership(proc, ledgers, workspaces, context = {}) {
|
|
|
9
9
|
let confidence = "unknown";
|
|
10
10
|
let ledgerEntry = null;
|
|
11
11
|
if (cwd && ledgers.length > 0) {
|
|
12
|
-
const
|
|
12
|
+
const inWindow = (l) => Boolean(l.cwd) && cwd.indexOf(l.cwd) === 0 && proc.startedAt !== null && Math.abs(proc.startedAt - l.at) < LEDGER_WINDOW_MS;
|
|
13
13
|
const sessionId = context.sessionId ?? null;
|
|
14
|
+
const candidate = (sessionId === null ? void 0 : ledgers.find((l) => l.sessionKey === sessionId && inWindow(l))) ?? ledgers.find(inWindow) ?? null;
|
|
14
15
|
if (candidate !== null && sessionId !== null && candidate.sessionKey !== null && candidate.sessionKey !== sessionId) evidence.push("launch ledger entry belongs to another session: " + String(candidate.sessionKey).slice(0, 16));
|
|
15
16
|
else ledgerEntry = candidate;
|
|
16
17
|
}
|