@devflow-tools/database 0.16.20 → 0.16.22

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,181 @@
1
+ export type LearningCandidateState =
2
+ | 'observed'
3
+ | 'candidate'
4
+ | 'shadow'
5
+ | 'evaluated'
6
+ | 'active'
7
+ | 'rejected'
8
+ | 'retired';
9
+
10
+ export type LearningCandidateKind =
11
+ | 'convention'
12
+ | 'workflow'
13
+ | 'tool_preference'
14
+ | 'correction';
15
+
16
+ export type LearningEvidencePolarity = 'supporting' | 'contradicting';
17
+ export type LearningOutcomeState = 'positive' | 'negative' | 'unknown';
18
+
19
+ export interface LearningCandidateRecord {
20
+ id: string;
21
+ projectRoot: string;
22
+ scope: 'project' | 'global';
23
+ state: LearningCandidateState;
24
+ kind: LearningCandidateKind;
25
+ trigger: Record<string, unknown>;
26
+ instruction: string;
27
+ confidence: number;
28
+ overlayVersion: number;
29
+ supportingSessions: number;
30
+ successfulOutcomes: number;
31
+ contradictions: number;
32
+ manualOnly: boolean;
33
+ risk: 'low' | 'medium' | 'high';
34
+ graderReceipt?: string;
35
+ expiresAt?: number;
36
+ createdAt: number;
37
+ updatedAt: number;
38
+ }
39
+
40
+ export interface LearningCandidateVersionRecord {
41
+ candidateId: string;
42
+ version: number;
43
+ trigger: Record<string, unknown>;
44
+ instruction: string;
45
+ createdAt: number;
46
+ }
47
+
48
+ export interface LearningCandidateEvidenceRecord {
49
+ id: string;
50
+ candidateId: string;
51
+ projectRoot: string;
52
+ sessionId: string;
53
+ executionId?: string;
54
+ memoryObservationId?: string;
55
+ outcomeId?: string;
56
+ sourceType: 'memory' | 'correction' | 'workflow_outcome' | 'tool_preference' | 'convention' | 'grader' | 'manual';
57
+ polarity: LearningEvidencePolarity;
58
+ outcome: LearningOutcomeState;
59
+ evidenceHash: string;
60
+ receipt?: string;
61
+ payload: Record<string, unknown>;
62
+ createdAt: number;
63
+ resolvedAt?: number;
64
+ }
65
+
66
+ export interface UpsertLearningCandidateInput {
67
+ id: string;
68
+ projectRoot: string;
69
+ scope: 'project' | 'global';
70
+ kind: LearningCandidateKind;
71
+ trigger: Record<string, unknown>;
72
+ instruction: string;
73
+ confidence: number;
74
+ manualOnly?: boolean;
75
+ risk?: 'low' | 'medium' | 'high';
76
+ expiresAt?: number;
77
+ }
78
+
79
+ export interface AddLearningCandidateEvidenceInput {
80
+ id: string;
81
+ candidateId: string;
82
+ projectRoot: string;
83
+ sessionId: string;
84
+ executionId?: string;
85
+ memoryObservationId?: string;
86
+ outcomeId?: string;
87
+ sourceType: LearningCandidateEvidenceRecord['sourceType'];
88
+ polarity: LearningEvidencePolarity;
89
+ outcome?: LearningOutcomeState;
90
+ evidenceHash: string;
91
+ receipt?: string;
92
+ payload?: Record<string, unknown>;
93
+ createdAt?: number;
94
+ }
95
+
96
+ export interface TransitionLearningCandidateInput {
97
+ target: LearningCandidateState;
98
+ reason: string;
99
+ graderReceipt?: string;
100
+ manualApproval?: boolean;
101
+ changedAt?: number;
102
+ }
103
+
104
+ export function mapLearningCandidateRow(row: Record<string, unknown>): LearningCandidateRecord {
105
+ return {
106
+ id: String(row.id),
107
+ projectRoot: String(row.project_root),
108
+ scope: row.scope === 'global' ? 'global' : 'project',
109
+ state: row.state as LearningCandidateState,
110
+ kind: row.kind as LearningCandidateKind,
111
+ trigger: parseRecord(row.trigger_json),
112
+ instruction: String(row.instruction),
113
+ confidence: Number(row.confidence),
114
+ overlayVersion: Number(row.overlay_version),
115
+ supportingSessions: Number(row.supporting_sessions),
116
+ successfulOutcomes: Number(row.successful_outcomes),
117
+ contradictions: Number(row.contradictions),
118
+ manualOnly: Number(row.manual_only) === 1,
119
+ risk: row.risk === 'high' ? 'high' : row.risk === 'medium' ? 'medium' : 'low',
120
+ graderReceipt: optionalString(row.grader_receipt),
121
+ expiresAt: optionalNumber(row.expires_at),
122
+ createdAt: Number(row.created_at),
123
+ updatedAt: Number(row.updated_at),
124
+ };
125
+ }
126
+
127
+ export function mapLearningEvidenceRow(row: Record<string, unknown>): LearningCandidateEvidenceRecord {
128
+ return {
129
+ id: String(row.id),
130
+ candidateId: String(row.candidate_id),
131
+ projectRoot: String(row.project_root),
132
+ sessionId: String(row.session_id),
133
+ executionId: optionalString(row.execution_id),
134
+ memoryObservationId: optionalString(row.memory_observation_id),
135
+ outcomeId: optionalString(row.outcome_id),
136
+ sourceType: row.source_type as LearningCandidateEvidenceRecord['sourceType'],
137
+ polarity: row.polarity as LearningEvidencePolarity,
138
+ outcome: row.outcome as LearningOutcomeState,
139
+ evidenceHash: String(row.evidence_hash),
140
+ receipt: optionalString(row.receipt),
141
+ payload: parseRecord(row.payload_json),
142
+ createdAt: Number(row.created_at),
143
+ resolvedAt: optionalNumber(row.resolved_at),
144
+ };
145
+ }
146
+
147
+ export function mapLearningVersionRow(row: Record<string, unknown>): LearningCandidateVersionRecord {
148
+ return {
149
+ candidateId: String(row.candidate_id),
150
+ version: Number(row.version),
151
+ trigger: parseRecord(row.trigger_json),
152
+ instruction: String(row.instruction),
153
+ createdAt: Number(row.created_at),
154
+ };
155
+ }
156
+
157
+ export function stableLearningJson(value: unknown): string {
158
+ if (Array.isArray(value)) return `[${value.map(stableLearningJson).join(',')}]`;
159
+ if (!value || typeof value !== 'object') return JSON.stringify(value) ?? 'null';
160
+ const record = value as Record<string, unknown>;
161
+ return `{${Object.keys(record).sort().map(key => `${JSON.stringify(key)}:${stableLearningJson(record[key])}`).join(',')}}`;
162
+ }
163
+
164
+ function parseRecord(value: unknown): Record<string, unknown> {
165
+ try {
166
+ const parsed = typeof value === 'string' ? JSON.parse(value) as unknown : value;
167
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
168
+ ? parsed as Record<string, unknown>
169
+ : {};
170
+ } catch {
171
+ return {};
172
+ }
173
+ }
174
+
175
+ function optionalString(value: unknown): string | undefined {
176
+ return typeof value === 'string' && value.length > 0 ? value : undefined;
177
+ }
178
+
179
+ function optionalNumber(value: unknown): number | undefined {
180
+ return value === null || value === undefined ? undefined : Number(value);
181
+ }
package/src/work-queue.ts CHANGED
@@ -4,6 +4,8 @@ export type WorkKind =
4
4
  | 'memory.turn_capture'
5
5
  | 'memory.turn_distill'
6
6
  | 'memory.vector_backfill'
7
+ | 'context.prefetch'
8
+ | 'learning.analyze_session'
7
9
  | 'session.bootstrap'
8
10
  | 'session.finalize'
9
11
  | 'telemetry.reconcile';