principles-disciple 1.7.0 → 1.7.1

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.
@@ -0,0 +1,79 @@
1
+ export type RuntimeDataQuality = 'authoritative' | 'partial';
2
+ export type RuntimeRewardPolicy = 'frozen_all_positive' | 'frozen_atomic_positive_keep_plan_ready';
3
+ interface RuntimeSummarySource {
4
+ source: string;
5
+ score?: number;
6
+ ts?: string;
7
+ confidence?: number;
8
+ origin?: string;
9
+ }
10
+ interface RuntimePainSignal {
11
+ source: string;
12
+ ts: string | null;
13
+ reason: string | null;
14
+ }
15
+ export interface RuntimeSummary {
16
+ gfi: {
17
+ current: number | null;
18
+ peak: number | null;
19
+ sources: RuntimeSummarySource[];
20
+ dataQuality: RuntimeDataQuality;
21
+ };
22
+ legacyTrust: {
23
+ score: number | null;
24
+ stage: 1 | 2 | 3 | 4 | null;
25
+ frozen: true;
26
+ lastUpdated: string | null;
27
+ rewardPolicy: RuntimeRewardPolicy;
28
+ };
29
+ evolution: {
30
+ queue: {
31
+ pending: number;
32
+ inProgress: number;
33
+ completed: number;
34
+ };
35
+ directive: {
36
+ exists: boolean;
37
+ active: boolean | null;
38
+ ageSeconds: number | null;
39
+ taskPreview: string | null;
40
+ };
41
+ dataQuality: RuntimeDataQuality;
42
+ };
43
+ pain: {
44
+ activeFlag: boolean;
45
+ activeFlagSource: string | null;
46
+ candidates: number | null;
47
+ lastSignal: RuntimePainSignal | null;
48
+ };
49
+ gate: {
50
+ recentBlocks: number | null;
51
+ recentBypasses: number | null;
52
+ dataQuality: RuntimeDataQuality;
53
+ };
54
+ metadata: {
55
+ generatedAt: string;
56
+ workspaceDir: string;
57
+ sessionId: string | null;
58
+ selectedSessionReason: 'explicit' | 'latest_active' | 'none';
59
+ warnings: string[];
60
+ };
61
+ }
62
+ export declare class RuntimeSummaryService {
63
+ static getSummary(workspaceDir: string, options?: {
64
+ sessionId?: string | null;
65
+ }): RuntimeSummary;
66
+ private static readSessions;
67
+ private static selectSession;
68
+ private static mergeSessionSnapshots;
69
+ private static buildQueueStats;
70
+ private static buildDirectiveSummary;
71
+ private static readLegacyTrust;
72
+ private static readEvents;
73
+ private static buildGfiSources;
74
+ private static findLastPainSignal;
75
+ private static buildGateStats;
76
+ private static readJsonFile;
77
+ private static asFiniteNumber;
78
+ }
79
+ export {};
@@ -0,0 +1,319 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { readPainFlagData } from '../core/pain.js';
4
+ import { resolvePdPath } from '../core/paths.js';
5
+ import { listSessions } from '../core/session-tracker.js';
6
+ import { WorkspaceContext } from '../core/workspace-context.js';
7
+ const MAX_SOURCE_EVENTS = 5;
8
+ const LEGACY_TRUST_REWARD_POLICY = 'frozen_all_positive';
9
+ const GFI_PARTIAL_WARNING = 'GFI source attribution remains partial in Phase 2b because only the empathy slice is source-attributed; most non-empathy friction still lacks full per-source attribution.';
10
+ const DAILY_GFI_WARNING = 'daily-stats.gfi is not authoritative in Phase 1 and is used only as a fallback reference.';
11
+ const EVENT_BUFFER_WARNING = 'Live event buffer is unavailable in this context, so status may lag until events.jsonl flushes.';
12
+ function pushWarning(warnings, message) {
13
+ if (!warnings.includes(message)) {
14
+ warnings.push(message);
15
+ }
16
+ }
17
+ export class RuntimeSummaryService {
18
+ static getSummary(workspaceDir, options) {
19
+ const generatedAt = new Date().toISOString();
20
+ const warnings = [];
21
+ const wctx = WorkspaceContext.fromHookContext({ workspaceDir });
22
+ const sessions = this.mergeSessionSnapshots(this.readSessions(wctx.resolve('SESSION_DIR'), warnings), workspaceDir);
23
+ const selectedSession = this.selectSession(sessions, options?.sessionId ?? null);
24
+ const selectedSessionId = selectedSession.session?.sessionId ?? null;
25
+ const persistedEvents = this.readEvents(path.join(wctx.stateDir, 'logs', 'events.jsonl'), warnings);
26
+ const hasBufferedEventAccess = typeof wctx.eventLog.getBufferedEvents === 'function';
27
+ const bufferedEvents = hasBufferedEventAccess
28
+ ? wctx.eventLog.getBufferedEvents()
29
+ : [];
30
+ const events = [...persistedEvents, ...bufferedEvents];
31
+ const dailyStats = this.readJsonFile(path.join(wctx.stateDir, 'logs', 'daily-stats.json'), warnings, false);
32
+ const today = generatedAt.slice(0, 10);
33
+ const dailyGfiPeak = dailyStats?.[today]?.gfi?.peak;
34
+ const gfiCurrent = selectedSession.session && Number.isFinite(selectedSession.session.currentGfi)
35
+ ? Number(selectedSession.session.currentGfi)
36
+ : null;
37
+ const sessionPeak = selectedSession.session && Number.isFinite(selectedSession.session.dailyGfiPeak)
38
+ ? Number(selectedSession.session.dailyGfiPeak)
39
+ : null;
40
+ const gfiPeak = sessionPeak ?? (Number.isFinite(dailyGfiPeak) ? Number(dailyGfiPeak) : null);
41
+ pushWarning(warnings, GFI_PARTIAL_WARNING);
42
+ if (sessionPeak === null && Number.isFinite(dailyGfiPeak)) {
43
+ pushWarning(warnings, DAILY_GFI_WARNING);
44
+ }
45
+ if (!hasBufferedEventAccess) {
46
+ pushWarning(warnings, EVENT_BUFFER_WARNING);
47
+ }
48
+ if (!selectedSession.session) {
49
+ pushWarning(warnings, 'No persisted session state was found; current session GFI is unavailable.');
50
+ }
51
+ const queue = this.readJsonFile(wctx.resolve('EVOLUTION_QUEUE'), warnings, false);
52
+ const directive = this.readJsonFile(wctx.resolve('EVOLUTION_DIRECTIVE'), warnings, false);
53
+ const queueStats = this.buildQueueStats(queue);
54
+ const directiveSummary = this.buildDirectiveSummary(directive, generatedAt, warnings, queueStats);
55
+ const painFlag = readPainFlagData(workspaceDir);
56
+ const painCandidates = this.readJsonFile(wctx.resolve('PAIN_CANDIDATES'), warnings, false);
57
+ const legacyTrust = this.readLegacyTrust(resolvePdPath(workspaceDir, 'AGENT_SCORECARD'), wctx, warnings);
58
+ const lastPainSignal = this.findLastPainSignal(events, selectedSessionId);
59
+ const gfiSources = this.buildGfiSources(events, selectedSessionId);
60
+ const gateStats = this.buildGateStats(events, selectedSessionId, warnings);
61
+ return {
62
+ gfi: {
63
+ current: gfiCurrent,
64
+ peak: gfiPeak,
65
+ sources: gfiSources,
66
+ dataQuality: 'partial',
67
+ },
68
+ legacyTrust,
69
+ evolution: {
70
+ queue: queueStats,
71
+ directive: directiveSummary,
72
+ dataQuality: queue ? 'authoritative' : 'partial',
73
+ },
74
+ pain: {
75
+ activeFlag: Object.keys(painFlag).length > 0,
76
+ activeFlagSource: painFlag.source || null,
77
+ candidates: painCandidates?.candidates && typeof painCandidates.candidates === 'object'
78
+ ? Object.keys(painCandidates.candidates).length
79
+ : null,
80
+ lastSignal: lastPainSignal,
81
+ },
82
+ gate: gateStats,
83
+ metadata: {
84
+ generatedAt,
85
+ workspaceDir,
86
+ sessionId: selectedSessionId,
87
+ selectedSessionReason: selectedSession.reason,
88
+ warnings,
89
+ },
90
+ };
91
+ }
92
+ static readSessions(sessionDir, warnings) {
93
+ if (!fs.existsSync(sessionDir)) {
94
+ pushWarning(warnings, 'No persisted session directory exists yet; session-scoped runtime state is unavailable.');
95
+ return [];
96
+ }
97
+ const sessions = [];
98
+ for (const file of fs.readdirSync(sessionDir)) {
99
+ if (!file.endsWith('.json'))
100
+ continue;
101
+ try {
102
+ const raw = fs.readFileSync(path.join(sessionDir, file), 'utf8');
103
+ const parsed = JSON.parse(raw);
104
+ if (parsed?.sessionId) {
105
+ sessions.push(parsed);
106
+ }
107
+ }
108
+ catch {
109
+ pushWarning(warnings, `Failed to parse session snapshot: ${file}`);
110
+ }
111
+ }
112
+ return sessions.sort((a, b) => (b.lastActivityAt ?? 0) - (a.lastActivityAt ?? 0));
113
+ }
114
+ static selectSession(sessions, explicitSessionId) {
115
+ if (explicitSessionId) {
116
+ const explicit = sessions.find((session) => session.sessionId === explicitSessionId) ?? null;
117
+ return { session: explicit, reason: explicit ? 'explicit' : 'none' };
118
+ }
119
+ if (sessions.length === 0) {
120
+ return { session: null, reason: 'none' };
121
+ }
122
+ return { session: sessions[0], reason: 'latest_active' };
123
+ }
124
+ static mergeSessionSnapshots(persistedSessions, workspaceDir) {
125
+ const merged = new Map();
126
+ for (const session of persistedSessions) {
127
+ merged.set(session.sessionId, { ...session });
128
+ }
129
+ for (const live of listSessions(workspaceDir)) {
130
+ const persisted = merged.get(live.sessionId);
131
+ merged.set(live.sessionId, {
132
+ sessionId: live.sessionId,
133
+ currentGfi: Number.isFinite(live.currentGfi) ? Number(live.currentGfi) : persisted?.currentGfi,
134
+ dailyGfiPeak: Number.isFinite(live.dailyGfiPeak) ? Number(live.dailyGfiPeak) : persisted?.dailyGfiPeak,
135
+ lastActivityAt: Number.isFinite(live.lastActivityAt) ? Number(live.lastActivityAt) : persisted?.lastActivityAt,
136
+ });
137
+ }
138
+ return [...merged.values()].sort((a, b) => (b.lastActivityAt ?? 0) - (a.lastActivityAt ?? 0));
139
+ }
140
+ static buildQueueStats(queue) {
141
+ const stats = { pending: 0, inProgress: 0, completed: 0 };
142
+ if (!queue)
143
+ return stats;
144
+ for (const item of queue) {
145
+ if (item?.status === 'completed') {
146
+ stats.completed++;
147
+ }
148
+ else if (item?.status === 'in_progress') {
149
+ stats.inProgress++;
150
+ }
151
+ else {
152
+ stats.pending++;
153
+ }
154
+ }
155
+ return stats;
156
+ }
157
+ static buildDirectiveSummary(directive, generatedAt, warnings, queueStats) {
158
+ if (!directive) {
159
+ return {
160
+ exists: false,
161
+ active: null,
162
+ ageSeconds: null,
163
+ taskPreview: null,
164
+ };
165
+ }
166
+ const timestampMs = directive.timestamp ? new Date(directive.timestamp).getTime() : NaN;
167
+ const ageSeconds = Number.isFinite(timestampMs)
168
+ ? Math.max(0, Math.floor((new Date(generatedAt).getTime() - timestampMs) / 1000))
169
+ : null;
170
+ if (directive.active && queueStats.pending === 0 && queueStats.inProgress === 0) {
171
+ warnings.push('Directive is active while the queue has no pending or in-progress task; worker state may be stale.');
172
+ }
173
+ return {
174
+ exists: true,
175
+ active: typeof directive.active === 'boolean' ? directive.active : null,
176
+ ageSeconds,
177
+ taskPreview: directive.task ? directive.task.slice(0, 160) : null,
178
+ };
179
+ }
180
+ static readLegacyTrust(scorecardPath, wctx, warnings) {
181
+ const scorecard = this.readJsonFile(scorecardPath, warnings, false);
182
+ const score = Number.isFinite(scorecard?.trust_score) ? Number(scorecard?.trust_score) : null;
183
+ const settings = wctx.config.get('trust');
184
+ const stageThresholds = settings?.stages ?? {
185
+ stage_1_observer: 30,
186
+ stage_2_editor: 60,
187
+ stage_3_developer: 80,
188
+ };
189
+ let stage = null;
190
+ if (score !== null) {
191
+ if (score < (stageThresholds.stage_1_observer ?? 30)) {
192
+ stage = 1;
193
+ }
194
+ else if (score < (stageThresholds.stage_2_editor ?? 60)) {
195
+ stage = 2;
196
+ }
197
+ else if (score < (stageThresholds.stage_3_developer ?? 80)) {
198
+ stage = 3;
199
+ }
200
+ else {
201
+ stage = 4;
202
+ }
203
+ }
204
+ return {
205
+ score,
206
+ stage,
207
+ frozen: true,
208
+ lastUpdated: scorecard?.last_updated ?? null,
209
+ rewardPolicy: LEGACY_TRUST_REWARD_POLICY,
210
+ };
211
+ }
212
+ static readEvents(eventsPath, warnings) {
213
+ if (!fs.existsSync(eventsPath)) {
214
+ warnings.push('No events.jsonl file exists yet; recent pain and gate summaries are partial.');
215
+ return [];
216
+ }
217
+ try {
218
+ const raw = fs.readFileSync(eventsPath, 'utf8').trim();
219
+ if (!raw)
220
+ return [];
221
+ let parseFailures = 0;
222
+ const entries = raw
223
+ .split('\n')
224
+ .map((line) => {
225
+ try {
226
+ return JSON.parse(line);
227
+ }
228
+ catch {
229
+ parseFailures += 1;
230
+ return null;
231
+ }
232
+ })
233
+ .filter((entry) => entry !== null);
234
+ if (parseFailures > 0) {
235
+ pushWarning(warnings, `Skipped ${parseFailures} malformed event line${parseFailures === 1 ? '' : 's'} while reading events.jsonl.`);
236
+ }
237
+ return entries;
238
+ }
239
+ catch {
240
+ pushWarning(warnings, 'Failed to read events.jsonl; recent pain and gate summaries are partial.');
241
+ return [];
242
+ }
243
+ }
244
+ static buildGfiSources(events, sessionId) {
245
+ const filtered = events
246
+ .filter((entry) => {
247
+ if (sessionId && entry.sessionId !== sessionId)
248
+ return false;
249
+ return (entry.type === 'pain_signal' ||
250
+ (entry.type === 'tool_call' && entry.category === 'failure'));
251
+ })
252
+ .slice(-MAX_SOURCE_EVENTS)
253
+ .reverse();
254
+ return filtered.map((entry) => {
255
+ if (entry.type === 'pain_signal') {
256
+ return {
257
+ source: String(entry.data?.source ?? 'pain_signal'),
258
+ score: this.asFiniteNumber(entry.data?.score),
259
+ ts: entry.ts,
260
+ confidence: this.asFiniteNumber(entry.data?.confidence),
261
+ origin: typeof entry.data?.origin === 'string' ? entry.data.origin : undefined,
262
+ };
263
+ }
264
+ return {
265
+ source: `tool_failure:${String(entry.data?.toolName ?? 'unknown')}`,
266
+ score: this.asFiniteNumber(entry.data?.gfi),
267
+ ts: entry.ts,
268
+ };
269
+ });
270
+ }
271
+ static findLastPainSignal(events, sessionId) {
272
+ for (let i = events.length - 1; i >= 0; i--) {
273
+ const entry = events[i];
274
+ if (entry.type !== 'pain_signal')
275
+ continue;
276
+ if (sessionId && entry.sessionId !== sessionId)
277
+ continue;
278
+ return {
279
+ source: String(entry.data?.source ?? 'pain_signal'),
280
+ ts: entry.ts ?? null,
281
+ reason: typeof entry.data?.reason === 'string' ? entry.data.reason : null,
282
+ };
283
+ }
284
+ return null;
285
+ }
286
+ static buildGateStats(events, sessionId, warnings) {
287
+ const scoped = events.filter((entry) => {
288
+ if (sessionId && entry.sessionId !== sessionId)
289
+ return false;
290
+ return entry.type === 'gate_block' || entry.type === 'gate_bypass';
291
+ });
292
+ if (scoped.length === 0) {
293
+ pushWarning(warnings, 'Gate block counts before Phase 1 may be incomplete because older block events were not recorded to event-log.');
294
+ }
295
+ return {
296
+ recentBlocks: scoped.filter((entry) => entry.type === 'gate_block').length,
297
+ recentBypasses: scoped.filter((entry) => entry.type === 'gate_bypass').length,
298
+ dataQuality: scoped.length > 0 ? 'authoritative' : 'partial',
299
+ };
300
+ }
301
+ static readJsonFile(filePath, warnings, warnOnMissing) {
302
+ if (!fs.existsSync(filePath)) {
303
+ if (warnOnMissing) {
304
+ pushWarning(warnings, `Missing expected file: ${path.basename(filePath)}`);
305
+ }
306
+ return null;
307
+ }
308
+ try {
309
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
310
+ }
311
+ catch {
312
+ pushWarning(warnings, `Failed to parse ${path.basename(filePath)}.`);
313
+ return null;
314
+ }
315
+ }
316
+ static asFiniteNumber(value) {
317
+ return Number.isFinite(value) ? Number(value) : undefined;
318
+ }
319
+ }
@@ -40,6 +40,7 @@ export interface ToolCallEventData {
40
40
  export interface PainSignalEventData {
41
41
  score: number;
42
42
  source: string;
43
+ eventId?: string;
43
44
  reason?: string;
44
45
  isRisky?: boolean;
45
46
  origin?: 'assistant_self_report' | 'user_manual' | 'system_infer';
@@ -2,7 +2,7 @@
2
2
  "id": "principles-disciple",
3
3
  "name": "Principles Disciple",
4
4
  "description": "Evolutionary programming agent framework with strategic guardrails and reflection loops.",
5
- "version": "1.7.0",
5
+ "version": "1.7.1",
6
6
  "skills": [
7
7
  "./skills"
8
8
  ],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "principles-disciple",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "description": "Native OpenClaw plugin for Principles Disciple",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",