principles-disciple 1.7.3 → 1.7.5

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 (67) hide show
  1. package/dist/commands/evolution-status.js +4 -2
  2. package/dist/commands/focus.js +30 -155
  3. package/dist/constants/diagnostician.d.ts +16 -0
  4. package/dist/constants/diagnostician.js +60 -0
  5. package/dist/constants/tools.d.ts +2 -2
  6. package/dist/constants/tools.js +1 -1
  7. package/dist/core/config.d.ts +23 -0
  8. package/dist/core/config.js +26 -1
  9. package/dist/core/evolution-engine.js +1 -1
  10. package/dist/core/evolution-logger.d.ts +137 -0
  11. package/dist/core/evolution-logger.js +256 -0
  12. package/dist/core/evolution-reducer.d.ts +23 -0
  13. package/dist/core/evolution-reducer.js +73 -29
  14. package/dist/core/evolution-types.d.ts +6 -0
  15. package/dist/core/focus-history.d.ts +145 -0
  16. package/dist/core/focus-history.js +919 -0
  17. package/dist/core/init.js +24 -0
  18. package/dist/core/profile.js +1 -1
  19. package/dist/core/risk-calculator.d.ts +15 -0
  20. package/dist/core/risk-calculator.js +48 -0
  21. package/dist/core/trajectory.d.ts +73 -0
  22. package/dist/core/trajectory.js +206 -0
  23. package/dist/hooks/gate.js +130 -20
  24. package/dist/hooks/lifecycle.js +104 -0
  25. package/dist/hooks/pain.js +31 -0
  26. package/dist/hooks/prompt.js +136 -38
  27. package/dist/hooks/subagent.d.ts +1 -0
  28. package/dist/hooks/subagent.js +200 -18
  29. package/dist/http/principles-console-route.d.ts +7 -0
  30. package/dist/http/principles-console-route.js +301 -1
  31. package/dist/index.js +0 -2
  32. package/dist/service/central-database.d.ts +104 -0
  33. package/dist/service/central-database.js +648 -0
  34. package/dist/service/control-ui-query-service.d.ts +2 -0
  35. package/dist/service/control-ui-query-service.js +4 -0
  36. package/dist/service/empathy-observer-manager.d.ts +8 -0
  37. package/dist/service/empathy-observer-manager.js +40 -0
  38. package/dist/service/evolution-query-service.d.ts +155 -0
  39. package/dist/service/evolution-query-service.js +258 -0
  40. package/dist/service/evolution-worker.d.ts +4 -0
  41. package/dist/service/evolution-worker.js +185 -63
  42. package/dist/service/phase3-input-filter.d.ts +37 -0
  43. package/dist/service/phase3-input-filter.js +106 -0
  44. package/dist/service/runtime-summary-service.d.ts +15 -0
  45. package/dist/service/runtime-summary-service.js +111 -23
  46. package/dist/tools/deep-reflect.js +8 -2
  47. package/dist/utils/subagent-probe.d.ts +34 -0
  48. package/dist/utils/subagent-probe.js +81 -0
  49. package/openclaw.plugin.json +1 -1
  50. package/package.json +6 -4
  51. package/templates/langs/en/core/AGENTS.md +15 -3
  52. package/templates/langs/en/core/BOOTSTRAP.md +24 -1
  53. package/templates/langs/en/core/TOOLS.md +9 -0
  54. package/templates/langs/zh/core/AGENTS.md +15 -3
  55. package/templates/langs/zh/core/BOOTSTRAP.md +24 -1
  56. package/templates/langs/zh/core/TOOLS.md +9 -0
  57. package/templates/langs/zh/skills/pd-auditor/SKILL.md +61 -0
  58. package/templates/langs/zh/skills/pd-diagnostician/SKILL.md +287 -0
  59. package/templates/langs/zh/skills/pd-explorer/SKILL.md +65 -0
  60. package/templates/langs/zh/skills/pd-implementer/SKILL.md +68 -0
  61. package/templates/langs/zh/skills/pd-planner/SKILL.md +65 -0
  62. package/templates/langs/zh/skills/pd-reporter/SKILL.md +78 -0
  63. package/templates/langs/zh/skills/pd-reviewer/SKILL.md +66 -0
  64. package/dist/core/agent-loader.d.ts +0 -44
  65. package/dist/core/agent-loader.js +0 -147
  66. package/dist/tools/agent-spawn.d.ts +0 -54
  67. package/dist/tools/agent-spawn.js +0 -445
@@ -1,5 +1,6 @@
1
1
  import { WorkspaceContext } from '../core/workspace-context.js';
2
2
  import { trackFriction } from '../core/session-tracker.js';
3
+ import { isSubagentRuntimeAvailable } from '../utils/subagent-probe.js';
3
4
  const OBSERVER_SESSION_PREFIX = 'empathy_obs:';
4
5
  export class EmpathyObserverManager {
5
6
  static instance;
@@ -11,12 +12,51 @@ export class EmpathyObserverManager {
11
12
  }
12
13
  return EmpathyObserverManager.instance;
13
14
  }
15
+ /**
16
+ * Probe whether the subagent runtime is actually functional.
17
+ * api.runtime.subagent always exists (it's a Proxy), but in embedded mode
18
+ * every method throws "only available during a gateway request".
19
+ * We cache the result to avoid repeated probing.
20
+ */
21
+ subagentAvailableCache = null;
22
+ isSubagentAvailable(api) {
23
+ if (this.subagentAvailableCache !== null)
24
+ return this.subagentAvailableCache;
25
+ try {
26
+ // Accessing .run on the Proxy is safe; calling it with bad params
27
+ // will throw the unavailability error synchronously before any async work.
28
+ // We use a deliberate no-op probe: pass an empty object and catch immediately.
29
+ const probe = api.runtime?.subagent?.run;
30
+ if (typeof probe !== 'function') {
31
+ this.subagentAvailableCache = false;
32
+ return false;
33
+ }
34
+ // Call with intentionally invalid params — the unavailable runtime throws
35
+ // synchronously, the real runtime returns a rejected Promise.
36
+ const result = probe.call(api.runtime.subagent, {});
37
+ if (result && typeof result.catch === 'function') {
38
+ // It returned a Promise → runtime is real (even if the call fails for bad params)
39
+ result.catch(() => { });
40
+ this.subagentAvailableCache = true;
41
+ return true;
42
+ }
43
+ this.subagentAvailableCache = false;
44
+ return false;
45
+ }
46
+ catch {
47
+ // Threw synchronously → unavailable runtime
48
+ this.subagentAvailableCache = false;
49
+ return false;
50
+ }
51
+ }
14
52
  shouldTrigger(api, sessionId) {
15
53
  if (!api || !sessionId)
16
54
  return false;
17
55
  const enabled = api.config?.empathy_engine?.enabled !== false;
18
56
  if (!enabled)
19
57
  return false;
58
+ if (!isSubagentRuntimeAvailable(api.runtime?.subagent))
59
+ return false;
20
60
  return !this.sessionLocks.has(sessionId);
21
61
  }
22
62
  async spawn(api, sessionId, userMessage) {
@@ -0,0 +1,155 @@
1
+ /**
2
+ * EvolutionQueryService - 进化流程查询服务
3
+ *
4
+ * 提供 WebUI 所需的查询 API:
5
+ * - getTasks() - 获取任务列表
6
+ * - getEvents() - 获取事件流
7
+ * - getTrace() - 获取完整追踪
8
+ * - getStats() - 获取统计数据
9
+ */
10
+ import type { TrajectoryDatabase } from '../core/trajectory.js';
11
+ export interface TaskListFilters {
12
+ status?: string;
13
+ dateFrom?: string;
14
+ dateTo?: string;
15
+ page?: number;
16
+ pageSize?: number;
17
+ }
18
+ export interface EventFilters {
19
+ traceId?: string;
20
+ stage?: string;
21
+ limit?: number;
22
+ offset?: number;
23
+ }
24
+ export interface TasksResponse {
25
+ items: Array<{
26
+ taskId: string;
27
+ traceId: string;
28
+ source: string;
29
+ reason: string | null;
30
+ score: number;
31
+ status: string;
32
+ enqueuedAt: string | null;
33
+ startedAt: string | null;
34
+ completedAt: string | null;
35
+ duration: number | null;
36
+ resolution: string | null;
37
+ eventCount: number;
38
+ createdAt: string;
39
+ }>;
40
+ pagination: {
41
+ page: number;
42
+ pageSize: number;
43
+ total: number;
44
+ totalPages: number;
45
+ };
46
+ }
47
+ export interface EventsResponse {
48
+ items: Array<{
49
+ id: number;
50
+ traceId: string;
51
+ taskId: string | null;
52
+ stage: string;
53
+ stageLabel: string;
54
+ stageColor: string;
55
+ level: string;
56
+ message: string;
57
+ summary: string | null;
58
+ metadata: Record<string, unknown>;
59
+ createdAt: string;
60
+ }>;
61
+ pagination: {
62
+ limit: number;
63
+ offset: number;
64
+ hasMore: boolean;
65
+ };
66
+ }
67
+ export interface TraceDetailResponse {
68
+ traceId: string;
69
+ task: {
70
+ taskId: string;
71
+ traceId: string;
72
+ source: string;
73
+ reason: string | null;
74
+ score: number;
75
+ status: string;
76
+ enqueuedAt: string | null;
77
+ startedAt: string | null;
78
+ completedAt: string | null;
79
+ duration: number | null;
80
+ resolution: string | null;
81
+ createdAt: string;
82
+ updatedAt: string;
83
+ };
84
+ events: Array<{
85
+ id: number;
86
+ traceId: string;
87
+ taskId: string | null;
88
+ stage: string;
89
+ stageLabel: string;
90
+ stageColor: string;
91
+ level: string;
92
+ message: string;
93
+ summary: string | null;
94
+ metadata: Record<string, unknown>;
95
+ createdAt: string;
96
+ }>;
97
+ timeline: Array<{
98
+ stage: string;
99
+ stageLabel: string;
100
+ stageColor: string;
101
+ timestamp: string;
102
+ message: string;
103
+ summary: string | null;
104
+ }>;
105
+ }
106
+ export interface EvolutionStatsResponse {
107
+ total: number;
108
+ pending: number;
109
+ inProgress: number;
110
+ completed: number;
111
+ failed: number;
112
+ recentActivity: Array<{
113
+ day: string;
114
+ created: number;
115
+ completed: number;
116
+ }>;
117
+ stageDistribution: Array<{
118
+ stage: string;
119
+ stageLabel: string;
120
+ count: number;
121
+ }>;
122
+ }
123
+ /**
124
+ * EvolutionQueryService 类
125
+ * 封装进化流程的查询逻辑
126
+ */
127
+ export declare class EvolutionQueryService {
128
+ private readonly trajectory;
129
+ constructor(trajectory: TrajectoryDatabase);
130
+ /**
131
+ * 释放资源
132
+ * 注意:不关闭 trajectory,因为它是单例由 TrajectoryRegistry 管理
133
+ */
134
+ dispose(): void;
135
+ /**
136
+ * 获取任务列表(带分页、筛选)
137
+ */
138
+ getTasks(filters?: TaskListFilters): TasksResponse;
139
+ /**
140
+ * 获取事件流
141
+ */
142
+ getEvents(filters?: EventFilters): EventsResponse;
143
+ /**
144
+ * 获取完整追踪(单个 trace 的所有事件)
145
+ */
146
+ getTrace(traceId: string): TraceDetailResponse | null;
147
+ /**
148
+ * 获取统计数据
149
+ */
150
+ getStats(): EvolutionStatsResponse;
151
+ }
152
+ /**
153
+ * 获取 EvolutionQueryService 实例(单例)
154
+ */
155
+ export declare function getEvolutionQueryService(trajectory: TrajectoryDatabase): EvolutionQueryService;
@@ -0,0 +1,258 @@
1
+ /**
2
+ * EvolutionQueryService - 进化流程查询服务
3
+ *
4
+ * 提供 WebUI 所需的查询 API:
5
+ * - getTasks() - 获取任务列表
6
+ * - getEvents() - 获取事件流
7
+ * - getTrace() - 获取完整追踪
8
+ * - getStats() - 获取统计数据
9
+ */
10
+ import { STAGE_LABELS, STAGE_COLORS } from '../core/evolution-logger.js';
11
+ /**
12
+ * 计算任务持续时间(毫秒)
13
+ */
14
+ function calculateDuration(task) {
15
+ if (!task.startedAt)
16
+ return null;
17
+ const start = new Date(task.startedAt).getTime();
18
+ const end = task.completedAt ? new Date(task.completedAt).getTime() : Date.now();
19
+ return end - start;
20
+ }
21
+ /**
22
+ * EvolutionQueryService 类
23
+ * 封装进化流程的查询逻辑
24
+ */
25
+ export class EvolutionQueryService {
26
+ trajectory;
27
+ constructor(trajectory) {
28
+ this.trajectory = trajectory;
29
+ }
30
+ /**
31
+ * 释放资源
32
+ * 注意:不关闭 trajectory,因为它是单例由 TrajectoryRegistry 管理
33
+ */
34
+ dispose() {
35
+ // EvolutionQueryService 不拥有 trajectory,所以不关闭它
36
+ // trajectory 是由 TrajectoryRegistry 管理的单例
37
+ }
38
+ /**
39
+ * 获取任务列表(带分页、筛选)
40
+ */
41
+ getTasks(filters = {}) {
42
+ const page = filters.page ?? 1;
43
+ const pageSize = filters.pageSize ?? 20;
44
+ const offset = (page - 1) * pageSize;
45
+ // 获取任务列表
46
+ const tasks = this.trajectory.listEvolutionTasks({
47
+ status: filters.status,
48
+ dateFrom: filters.dateFrom,
49
+ dateTo: filters.dateTo,
50
+ limit: pageSize + 1, // 多取一条判断 hasMore
51
+ offset,
52
+ });
53
+ // 获取总数(简化实现,使用全量计数)
54
+ const allTasks = this.trajectory.listEvolutionTasks({
55
+ status: filters.status,
56
+ dateFrom: filters.dateFrom,
57
+ dateTo: filters.dateTo,
58
+ limit: 10000,
59
+ offset: 0,
60
+ });
61
+ const total = allTasks.length;
62
+ const totalPages = Math.ceil(total / pageSize);
63
+ // 组装响应
64
+ const items = tasks.slice(0, pageSize).map((task) => {
65
+ // 获取事件数量
66
+ const events = this.trajectory.listEvolutionEvents(task.traceId, { limit: 1000 });
67
+ return {
68
+ taskId: task.taskId,
69
+ traceId: task.traceId,
70
+ source: task.source,
71
+ reason: task.reason,
72
+ score: task.score,
73
+ status: task.status,
74
+ enqueuedAt: task.enqueuedAt,
75
+ startedAt: task.startedAt,
76
+ completedAt: task.completedAt,
77
+ duration: calculateDuration(task),
78
+ resolution: task.resolution,
79
+ eventCount: events.length,
80
+ createdAt: task.createdAt,
81
+ };
82
+ });
83
+ return {
84
+ items,
85
+ pagination: {
86
+ page,
87
+ pageSize,
88
+ total,
89
+ totalPages,
90
+ },
91
+ };
92
+ }
93
+ /**
94
+ * 获取事件流
95
+ */
96
+ getEvents(filters = {}) {
97
+ const limit = filters.limit ?? 100;
98
+ const offset = filters.offset ?? 0;
99
+ // 获取更多事件以支持 stage 过滤
100
+ const fetchLimit = filters.stage ? 500 : limit + 1;
101
+ const events = this.trajectory.listEvolutionEvents(filters.traceId, {
102
+ limit: fetchLimit,
103
+ offset: 0, // 从头获取,后续再过滤和分页
104
+ });
105
+ // 应用 stage 过滤
106
+ let filteredEvents = events;
107
+ if (filters.stage) {
108
+ filteredEvents = events.filter(event => event.stage === filters.stage);
109
+ }
110
+ // 应用分页
111
+ const hasMore = filteredEvents.length > offset + limit;
112
+ const items = filteredEvents.slice(offset, offset + limit).map((event) => ({
113
+ id: event.id,
114
+ traceId: event.traceId,
115
+ taskId: event.taskId,
116
+ stage: event.stage,
117
+ stageLabel: STAGE_LABELS[event.stage] || event.stage,
118
+ stageColor: STAGE_COLORS[event.stage] || '#6b7280',
119
+ level: event.level,
120
+ message: event.message,
121
+ summary: event.summary,
122
+ metadata: event.metadata,
123
+ createdAt: event.createdAt,
124
+ }));
125
+ return {
126
+ items,
127
+ pagination: {
128
+ limit,
129
+ offset,
130
+ hasMore,
131
+ },
132
+ };
133
+ }
134
+ /**
135
+ * 获取完整追踪(单个 trace 的所有事件)
136
+ */
137
+ getTrace(traceId) {
138
+ // 获取任务信息
139
+ const task = this.trajectory.getEvolutionTaskByTraceId(traceId);
140
+ if (!task)
141
+ return null;
142
+ // 获取所有事件
143
+ const events = this.trajectory.listEvolutionEvents(traceId, { limit: 1000 });
144
+ // 构建时间线
145
+ const timeline = events.map((event) => ({
146
+ stage: event.stage,
147
+ stageLabel: STAGE_LABELS[event.stage] || event.stage,
148
+ stageColor: STAGE_COLORS[event.stage] || '#6b7280',
149
+ timestamp: event.createdAt,
150
+ message: event.message,
151
+ summary: event.summary,
152
+ }));
153
+ return {
154
+ traceId,
155
+ task: {
156
+ taskId: task.taskId,
157
+ traceId: task.traceId,
158
+ source: task.source,
159
+ reason: task.reason,
160
+ score: task.score,
161
+ status: task.status,
162
+ enqueuedAt: task.enqueuedAt,
163
+ startedAt: task.startedAt,
164
+ completedAt: task.completedAt,
165
+ duration: calculateDuration(task),
166
+ resolution: task.resolution,
167
+ createdAt: task.createdAt,
168
+ updatedAt: task.updatedAt,
169
+ },
170
+ events: events.map((event) => ({
171
+ id: event.id,
172
+ traceId: event.traceId,
173
+ taskId: event.taskId,
174
+ stage: event.stage,
175
+ stageLabel: STAGE_LABELS[event.stage] || event.stage,
176
+ stageColor: STAGE_COLORS[event.stage] || '#6b7280',
177
+ level: event.level,
178
+ message: event.message,
179
+ summary: event.summary,
180
+ metadata: event.metadata,
181
+ createdAt: event.createdAt,
182
+ })),
183
+ timeline,
184
+ };
185
+ }
186
+ /**
187
+ * 获取统计数据
188
+ */
189
+ getStats() {
190
+ // 获取基础统计
191
+ const stats = this.trajectory.getEvolutionStats();
192
+ // 获取近期活动(最近 7 天)
193
+ const now = new Date();
194
+ const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
195
+ const recentTasks = this.trajectory.listEvolutionTasks({
196
+ dateFrom: sevenDaysAgo.toISOString(),
197
+ limit: 10000,
198
+ });
199
+ // 按天分组
200
+ const activityByDay = new Map();
201
+ for (let i = 0; i < 7; i++) {
202
+ const day = new Date(now.getTime() - i * 24 * 60 * 60 * 1000);
203
+ const dayStr = day.toISOString().split('T')[0];
204
+ activityByDay.set(dayStr, { created: 0, completed: 0 });
205
+ }
206
+ for (const task of recentTasks) {
207
+ const createdDay = task.createdAt.split('T')[0];
208
+ if (activityByDay.has(createdDay)) {
209
+ const entry = activityByDay.get(createdDay);
210
+ entry.created++;
211
+ }
212
+ if (task.completedAt) {
213
+ const completedDay = task.completedAt.split('T')[0];
214
+ if (activityByDay.has(completedDay)) {
215
+ const entry = activityByDay.get(completedDay);
216
+ entry.completed++;
217
+ }
218
+ }
219
+ }
220
+ const recentActivity = Array.from(activityByDay.entries())
221
+ .map(([day, data]) => ({ day, ...data }))
222
+ .sort((a, b) => a.day.localeCompare(b.day));
223
+ // 获取阶段分布
224
+ const allEvents = this.trajectory.listEvolutionEvents(undefined, { limit: 10000 });
225
+ const stageCount = new Map();
226
+ for (const event of allEvents) {
227
+ stageCount.set(event.stage, (stageCount.get(event.stage) || 0) + 1);
228
+ }
229
+ const stageDistribution = Array.from(stageCount.entries())
230
+ .map(([stage, count]) => ({
231
+ stage,
232
+ stageLabel: STAGE_LABELS[stage] || stage,
233
+ count,
234
+ }))
235
+ .sort((a, b) => b.count - a.count);
236
+ return {
237
+ ...stats,
238
+ recentActivity,
239
+ stageDistribution,
240
+ };
241
+ }
242
+ }
243
+ // 单例缓存
244
+ const serviceCache = new Map();
245
+ /**
246
+ * 获取 EvolutionQueryService 实例(单例)
247
+ */
248
+ export function getEvolutionQueryService(trajectory) {
249
+ // 使用 trajectory 的 dbPath 作为缓存键
250
+ const cacheKey = trajectory.dbPath || 'default';
251
+ const cached = serviceCache.get(cacheKey);
252
+ if (cached) {
253
+ return cached;
254
+ }
255
+ const service = new EvolutionQueryService(trajectory);
256
+ serviceCache.set(cacheKey, service);
257
+ return service;
258
+ }
@@ -13,6 +13,10 @@ export interface EvolutionQueueItem {
13
13
  assigned_session_key?: string;
14
14
  trigger_text_preview?: string;
15
15
  status: 'pending' | 'in_progress' | 'completed';
16
+ resolution?: 'marker_detected' | 'auto_completed_timeout';
17
+ session_id?: string;
18
+ agent_id?: string;
19
+ traceId?: string;
16
20
  }
17
21
  export declare const EVOLUTION_QUEUE_LOCK_SUFFIX = ".lock";
18
22
  export declare const PAIN_CANDIDATES_LOCK_SUFFIX = ".candidates.lock";