@namzu/sdk 55.0.0 → 55.1.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/CHANGELOG.md +11 -0
- package/dist/{manager/resident/learning-cycle.d.ts → learning/cycle.d.ts} +45 -36
- package/dist/learning/cycle.d.ts.map +1 -0
- package/dist/{manager/resident/learning-cycle.js → learning/cycle.js} +52 -20
- package/dist/learning/cycle.js.map +1 -0
- package/dist/learning/deprecated.d.ts +86 -0
- package/dist/learning/deprecated.d.ts.map +1 -0
- package/dist/learning/deprecated.js +22 -0
- package/dist/learning/deprecated.js.map +1 -0
- package/dist/learning/experience-store.d.ts +35 -0
- package/dist/learning/experience-store.d.ts.map +1 -0
- package/dist/learning/experience-store.js +143 -0
- package/dist/learning/experience-store.js.map +1 -0
- package/dist/learning/experience.d.ts +150 -0
- package/dist/learning/experience.d.ts.map +1 -0
- package/dist/learning/experience.js +146 -0
- package/dist/learning/experience.js.map +1 -0
- package/dist/learning/generator.d.ts +118 -0
- package/dist/learning/generator.d.ts.map +1 -0
- package/dist/learning/generator.js +275 -0
- package/dist/learning/generator.js.map +1 -0
- package/dist/learning/leak-tripwire.d.ts +66 -0
- package/dist/learning/leak-tripwire.d.ts.map +1 -0
- package/dist/learning/leak-tripwire.js +208 -0
- package/dist/learning/leak-tripwire.js.map +1 -0
- package/dist/{manager/resident/learning-observation.d.ts → learning/observation.d.ts} +4 -4
- package/dist/learning/observation.d.ts.map +1 -0
- package/dist/{manager/resident/learning-observation.js → learning/observation.js} +3 -3
- package/dist/learning/observation.js.map +1 -0
- package/dist/{manager/resident/learning.d.ts → learning/skill.d.ts} +29 -29
- package/dist/learning/skill.d.ts.map +1 -0
- package/dist/{manager/resident/learning.js → learning/skill.js} +27 -27
- package/dist/learning/skill.js.map +1 -0
- package/dist/{manager/resident/learning-store.d.ts → learning/store.d.ts} +38 -31
- package/dist/learning/store.d.ts.map +1 -0
- package/dist/{manager/resident/learning-store.js → learning/store.js} +52 -20
- package/dist/learning/store.js.map +1 -0
- package/dist/manager/resident/activity.d.ts +30 -6
- package/dist/manager/resident/activity.d.ts.map +1 -1
- package/dist/manager/resident/activity.js.map +1 -1
- package/dist/manager/resident/agenda.d.ts +42 -12
- package/dist/manager/resident/agenda.d.ts.map +1 -1
- package/dist/manager/resident/agenda.js +9 -7
- package/dist/manager/resident/agenda.js.map +1 -1
- package/dist/manager/resident/consumption.d.ts +27 -5
- package/dist/manager/resident/consumption.d.ts.map +1 -1
- package/dist/manager/resident/consumption.js +2 -0
- package/dist/manager/resident/consumption.js.map +1 -1
- package/dist/manager/resident/delivery-window.d.ts +7 -1
- package/dist/manager/resident/delivery-window.d.ts.map +1 -1
- package/dist/manager/resident/delivery-window.js +2 -0
- package/dist/manager/resident/delivery-window.js.map +1 -1
- package/dist/manager/resident/evidence-recall.d.ts +7 -1
- package/dist/manager/resident/evidence-recall.d.ts.map +1 -1
- package/dist/manager/resident/evidence-recall.js +2 -0
- package/dist/manager/resident/evidence-recall.js.map +1 -1
- package/dist/manager/resident/history.d.ts +45 -9
- package/dist/manager/resident/history.d.ts.map +1 -1
- package/dist/manager/resident/history.js.map +1 -1
- package/dist/manager/resident/host.d.ts +44 -10
- package/dist/manager/resident/host.d.ts.map +1 -1
- package/dist/manager/resident/host.js +4 -2
- package/dist/manager/resident/host.js.map +1 -1
- package/dist/manager/resident/initiative.d.ts +32 -6
- package/dist/manager/resident/initiative.d.ts.map +1 -1
- package/dist/manager/resident/initiative.js +2 -0
- package/dist/manager/resident/initiative.js.map +1 -1
- package/dist/manager/resident/loop.d.ts +22 -4
- package/dist/manager/resident/loop.d.ts.map +1 -1
- package/dist/manager/resident/loop.js +7 -1
- package/dist/manager/resident/loop.js.map +1 -1
- package/dist/manager/resident/outbox.d.ts +42 -8
- package/dist/manager/resident/outbox.d.ts.map +1 -1
- package/dist/manager/resident/outbox.js +2 -0
- package/dist/manager/resident/outbox.js.map +1 -1
- package/dist/manager/resident/proposal.d.ts +17 -3
- package/dist/manager/resident/proposal.d.ts.map +1 -1
- package/dist/manager/resident/proposal.js +2 -0
- package/dist/manager/resident/proposal.js.map +1 -1
- package/dist/manager/resident/store.d.ts +27 -5
- package/dist/manager/resident/store.d.ts.map +1 -1
- package/dist/manager/resident/store.js +7 -1
- package/dist/manager/resident/store.js.map +1 -1
- package/dist/manager/resident/tool-evidence.d.ts +45 -9
- package/dist/manager/resident/tool-evidence.d.ts.map +1 -1
- package/dist/manager/resident/tool-evidence.js +5 -1
- package/dist/manager/resident/tool-evidence.js.map +1 -1
- package/dist/prompt/resident-learning.d.ts +12 -2
- package/dist/prompt/resident-learning.d.ts.map +1 -1
- package/dist/prompt/resident-learning.js +6 -5
- package/dist/prompt/resident-learning.js.map +1 -1
- package/dist/prompt/resident-step.d.ts +10 -4
- package/dist/prompt/resident-step.d.ts.map +1 -1
- package/dist/prompt/resident-step.js +6 -4
- package/dist/prompt/resident-step.js.map +1 -1
- package/dist/public-runtime.d.ts +9 -3
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +10 -3
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-types.d.ts +9 -4
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/per-call-review.d.ts +9 -0
- package/dist/runtime/query/iteration/phases/per-call-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/per-call-review.js +19 -0
- package/dist/runtime/query/iteration/phases/per-call-review.js.map +1 -1
- package/dist/tools/resident-history.d.ts +2 -0
- package/dist/tools/resident-history.d.ts.map +1 -1
- package/dist/tools/resident-history.js +2 -0
- package/dist/tools/resident-history.js.map +1 -1
- package/dist/tools/resident-tool-evidence.d.ts +5 -1
- package/dist/tools/resident-tool-evidence.d.ts.map +1 -1
- package/dist/tools/resident-tool-evidence.js +5 -1
- package/dist/tools/resident-tool-evidence.js.map +1 -1
- package/package.json +1 -1
- package/src/{manager/resident/learning-cycle.ts → learning/cycle.ts} +130 -78
- package/src/learning/deprecated.ts +124 -0
- package/src/learning/experience-store.ts +202 -0
- package/src/learning/experience.ts +238 -0
- package/src/learning/generator.ts +388 -0
- package/src/learning/leak-tripwire.ts +280 -0
- package/src/{manager/resident/learning-observation.ts → learning/observation.ts} +5 -5
- package/src/{manager/resident/learning.ts → learning/skill.ts} +64 -67
- package/src/{manager/resident/learning-store.ts → learning/store.ts} +97 -67
- package/src/manager/resident/activity.ts +30 -6
- package/src/manager/resident/agenda.ts +63 -33
- package/src/manager/resident/consumption.ts +27 -5
- package/src/manager/resident/delivery-window.ts +7 -1
- package/src/manager/resident/evidence-recall.ts +7 -1
- package/src/manager/resident/history.ts +45 -9
- package/src/manager/resident/host.ts +45 -11
- package/src/manager/resident/initiative.ts +32 -6
- package/src/manager/resident/loop.ts +22 -4
- package/src/manager/resident/outbox.ts +42 -8
- package/src/manager/resident/proposal.ts +17 -3
- package/src/manager/resident/store.ts +27 -5
- package/src/manager/resident/tool-evidence.ts +45 -9
- package/src/prompt/resident-learning.ts +16 -7
- package/src/prompt/resident-step.ts +19 -13
- package/src/public-runtime.ts +35 -5
- package/src/public-types.ts +96 -34
- package/src/runtime/query/iteration/phases/per-call-review.ts +19 -0
- package/src/tools/resident-history.ts +2 -0
- package/src/tools/resident-tool-evidence.ts +5 -1
- package/dist/manager/resident/learning-cycle.d.ts.map +0 -1
- package/dist/manager/resident/learning-cycle.js.map +0 -1
- package/dist/manager/resident/learning-observation.d.ts.map +0 -1
- package/dist/manager/resident/learning-observation.js.map +0 -1
- package/dist/manager/resident/learning-store.d.ts.map +0 -1
- package/dist/manager/resident/learning-store.js.map +0 -1
- package/dist/manager/resident/learning.d.ts.map +0 -1
- package/dist/manager/resident/learning.js.map +0 -1
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { mkdirSync } from 'node:fs'
|
|
2
|
+
import { createRequire } from 'node:module'
|
|
3
|
+
import { dirname, resolve } from 'node:path'
|
|
4
|
+
import type { DatabaseSync } from 'node:sqlite'
|
|
5
|
+
import { z } from 'zod'
|
|
6
|
+
import type { ProjectId, TenantId } from '../types/ids/index.js'
|
|
7
|
+
import { asProjectId, asTenantId } from '../utils/id.js'
|
|
8
|
+
import {
|
|
9
|
+
type LearningExperienceEntry,
|
|
10
|
+
type LearningExperienceInput,
|
|
11
|
+
type LearningExperienceQuery,
|
|
12
|
+
assertExperienceHasNoLeak,
|
|
13
|
+
normalizeLearningExperience,
|
|
14
|
+
selectLearningExperience,
|
|
15
|
+
} from './experience.js'
|
|
16
|
+
|
|
17
|
+
const SCHEMA = `
|
|
18
|
+
CREATE TABLE experience (
|
|
19
|
+
ordinal INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
20
|
+
tenant_id TEXT NOT NULL, project_id TEXT NOT NULL, agent_key TEXT NOT NULL,
|
|
21
|
+
domain TEXT NOT NULL, cycle_id TEXT NOT NULL, evaluator_revision TEXT NOT NULL,
|
|
22
|
+
decision TEXT NOT NULL, body TEXT NOT NULL,
|
|
23
|
+
UNIQUE(tenant_id, project_id, agent_key, cycle_id)
|
|
24
|
+
);
|
|
25
|
+
CREATE INDEX experience_read ON experience(
|
|
26
|
+
tenant_id, project_id, agent_key, domain, evaluator_revision, ordinal
|
|
27
|
+
);
|
|
28
|
+
`
|
|
29
|
+
|
|
30
|
+
/** The schema version this store reads and writes. */
|
|
31
|
+
export const LEARNING_EXPERIENCE_STORE_VERSION = 1
|
|
32
|
+
|
|
33
|
+
/** @experimental Where the library lives; a separate file from the observation journal. */
|
|
34
|
+
export interface SqliteLearningExperienceStoreOptions {
|
|
35
|
+
readonly databasePath: string
|
|
36
|
+
readonly scope: {
|
|
37
|
+
readonly tenantId: TenantId
|
|
38
|
+
readonly projectId: ProjectId
|
|
39
|
+
readonly agentKey: string
|
|
40
|
+
}
|
|
41
|
+
readonly readOnly?: boolean
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type Row = { ordinal: number; body: string }
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @experimental Durable experience library. Entries are immutable: recording
|
|
48
|
+
* the same cycle again with identical content returns the stored entry, with
|
|
49
|
+
* different content is refused. The artefact is stored whole.
|
|
50
|
+
*/
|
|
51
|
+
export class SqliteLearningExperienceStore {
|
|
52
|
+
readonly databasePath: string
|
|
53
|
+
private readonly scope: SqliteLearningExperienceStoreOptions['scope']
|
|
54
|
+
private readonly readOnly: boolean
|
|
55
|
+
|
|
56
|
+
constructor(options: SqliteLearningExperienceStoreOptions) {
|
|
57
|
+
this.databasePath = resolve(options.databasePath)
|
|
58
|
+
this.scope = Object.freeze({
|
|
59
|
+
tenantId: asTenantId(options.scope.tenantId.toLowerCase()),
|
|
60
|
+
projectId: asProjectId(options.scope.projectId.toLowerCase()),
|
|
61
|
+
agentKey: z
|
|
62
|
+
.string()
|
|
63
|
+
.regex(/^[a-z0-9][a-z0-9_-]{0,63}$/)
|
|
64
|
+
.parse(options.scope.agentKey),
|
|
65
|
+
})
|
|
66
|
+
this.readOnly = options.readOnly === true
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private use<T>(write: boolean, operation: (db: DatabaseSync) => T, whenEmpty?: () => T): T {
|
|
70
|
+
if (write && this.readOnly) throw new Error('Learning experience database is read-only.')
|
|
71
|
+
let Database: typeof DatabaseSync
|
|
72
|
+
try {
|
|
73
|
+
Database = (createRequire(import.meta.url)('node:sqlite') as typeof import('node:sqlite'))
|
|
74
|
+
.DatabaseSync
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw new Error('SqliteLearningExperienceStore requires Node.js 22.13 or newer.', {
|
|
77
|
+
cause: error,
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
if (!this.readOnly) mkdirSync(dirname(this.databasePath), { recursive: true, mode: 0o700 })
|
|
81
|
+
const db = new Database(this.databasePath, { readOnly: this.readOnly })
|
|
82
|
+
try {
|
|
83
|
+
db.exec('PRAGMA busy_timeout = 5000;')
|
|
84
|
+
if (!this.readOnly) db.exec('PRAGMA journal_mode = DELETE; PRAGMA synchronous = FULL;')
|
|
85
|
+
db.exec(write ? 'BEGIN IMMEDIATE' : 'BEGIN')
|
|
86
|
+
try {
|
|
87
|
+
const version = Number(db.prepare('PRAGMA user_version').get()?.user_version)
|
|
88
|
+
if (version === 0 && write) {
|
|
89
|
+
db.exec(`${SCHEMA} PRAGMA user_version = ${LEARNING_EXPERIENCE_STORE_VERSION};`)
|
|
90
|
+
} else if (version === 0) {
|
|
91
|
+
if (whenEmpty) {
|
|
92
|
+
db.exec('COMMIT')
|
|
93
|
+
return whenEmpty()
|
|
94
|
+
}
|
|
95
|
+
throw new Error(
|
|
96
|
+
'Learning experience database is not initialized; open it for writing first.',
|
|
97
|
+
)
|
|
98
|
+
} else if (version !== LEARNING_EXPERIENCE_STORE_VERSION)
|
|
99
|
+
throw new Error(
|
|
100
|
+
`Learning experience database is at schema version ${version}; this store reads version ${LEARNING_EXPERIENCE_STORE_VERSION} only.`,
|
|
101
|
+
)
|
|
102
|
+
const value = operation(db)
|
|
103
|
+
db.exec('COMMIT')
|
|
104
|
+
return value
|
|
105
|
+
} catch (error) {
|
|
106
|
+
db.exec('ROLLBACK')
|
|
107
|
+
throw error
|
|
108
|
+
}
|
|
109
|
+
} finally {
|
|
110
|
+
db.close()
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private scopeArgs(): [string, string, string] {
|
|
115
|
+
return [this.scope.tenantId, this.scope.projectId, this.scope.agentKey]
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private decode(row: Row): LearningExperienceEntry {
|
|
119
|
+
return {
|
|
120
|
+
...(JSON.parse(row.body) as Omit<LearningExperienceEntry, 'ordinal'>),
|
|
121
|
+
ordinal: row.ordinal,
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Record one finished cycle. Throws `LearningLeakError` when the entry carries evaluator-side material. */
|
|
126
|
+
async record(input: LearningExperienceInput): Promise<LearningExperienceEntry> {
|
|
127
|
+
if (!input.leakGuard) throw new Error('Recording an experience entry requires a leakGuard.')
|
|
128
|
+
const entry = normalizeLearningExperience(input)
|
|
129
|
+
assertExperienceHasNoLeak(entry, input.leakGuard)
|
|
130
|
+
const body = JSON.stringify(entry)
|
|
131
|
+
return this.use(true, (db) => {
|
|
132
|
+
const existing = db
|
|
133
|
+
.prepare(
|
|
134
|
+
'SELECT ordinal, body FROM experience WHERE tenant_id=? AND project_id=? AND agent_key=? AND cycle_id=?',
|
|
135
|
+
)
|
|
136
|
+
.get(...this.scopeArgs(), entry.cycleId) as Row | undefined
|
|
137
|
+
if (existing) {
|
|
138
|
+
if (existing.body !== body)
|
|
139
|
+
throw new Error(
|
|
140
|
+
'This cycle already has a different experience entry; entries are immutable.',
|
|
141
|
+
)
|
|
142
|
+
return this.decode(existing)
|
|
143
|
+
}
|
|
144
|
+
const result = db
|
|
145
|
+
.prepare(
|
|
146
|
+
'INSERT INTO experience (tenant_id, project_id, agent_key, domain, cycle_id, evaluator_revision, decision, body) VALUES (?,?,?,?,?,?,?,?)',
|
|
147
|
+
)
|
|
148
|
+
.run(
|
|
149
|
+
...this.scopeArgs(),
|
|
150
|
+
entry.domain,
|
|
151
|
+
entry.cycleId,
|
|
152
|
+
entry.evaluatorRevision,
|
|
153
|
+
entry.decision,
|
|
154
|
+
body,
|
|
155
|
+
)
|
|
156
|
+
return { ...entry, ordinal: Number(result.lastInsertRowid) }
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The ordinal the next entry will be recorded after; pass it as `beforeOrdinal` to read everything so far. */
|
|
161
|
+
async nextOrdinal(): Promise<number> {
|
|
162
|
+
return this.use(
|
|
163
|
+
false,
|
|
164
|
+
(db) => {
|
|
165
|
+
const row = db
|
|
166
|
+
.prepare(
|
|
167
|
+
'SELECT MAX(ordinal) AS last FROM experience WHERE tenant_id=? AND project_id=? AND agent_key=?',
|
|
168
|
+
)
|
|
169
|
+
.get(...this.scopeArgs()) as { last: number | null }
|
|
170
|
+
return (row.last ?? 0) + 1
|
|
171
|
+
},
|
|
172
|
+
() => 1,
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Entries of one domain and evaluator revision recorded before `beforeOrdinal`, newest first. */
|
|
177
|
+
async retrieve(query: LearningExperienceQuery): Promise<LearningExperienceEntry[]> {
|
|
178
|
+
const rows = this.use(
|
|
179
|
+
false,
|
|
180
|
+
(db) =>
|
|
181
|
+
db
|
|
182
|
+
.prepare(
|
|
183
|
+
'SELECT ordinal, body FROM experience WHERE tenant_id=? AND project_id=? AND agent_key=? AND domain=? AND evaluator_revision=? AND ordinal<? ORDER BY ordinal DESC',
|
|
184
|
+
)
|
|
185
|
+
.all(
|
|
186
|
+
...this.scopeArgs(),
|
|
187
|
+
query.domain,
|
|
188
|
+
query.evaluatorRevision,
|
|
189
|
+
query.beforeOrdinal,
|
|
190
|
+
) as Row[],
|
|
191
|
+
() => [],
|
|
192
|
+
)
|
|
193
|
+
if (!query.leakGuard) throw new Error('Retrieving experience entries requires a leakGuard.')
|
|
194
|
+
const selected = selectLearningExperience(
|
|
195
|
+
rows.map((row) => this.decode(row)),
|
|
196
|
+
query,
|
|
197
|
+
)
|
|
198
|
+
for (const { ordinal: _ordinal, ...entry } of selected)
|
|
199
|
+
assertExperienceHasNoLeak(entry, query.leakGuard)
|
|
200
|
+
return selected
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import { type LearningLeakGuard, assertNoLearningLeak } from './leak-tripwire.js'
|
|
3
|
+
import {
|
|
4
|
+
type LearningSkillCandidate,
|
|
5
|
+
hashLearningSkill,
|
|
6
|
+
learningEvidenceSchema,
|
|
7
|
+
normalizeLearningSkill,
|
|
8
|
+
} from './skill.js'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The experience library: what an earlier cycle tried and what came of it,
|
|
12
|
+
* kept beside the observations so a later generator can read it. Three rules
|
|
13
|
+
* hold throughout. An entry stores the adopted or rejected artefact whole,
|
|
14
|
+
* never a summary of it. Retrieval is limited to one domain and to entries
|
|
15
|
+
* recorded before a stated point. And a verdict never claims more than the
|
|
16
|
+
* evidence can separate: when a candidate changed several components at once,
|
|
17
|
+
* the result belongs to the whole change, so each row says `confounded`.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const label = z.string().trim().min(1).max(256)
|
|
21
|
+
const text = z.string().trim().min(1).max(2_000)
|
|
22
|
+
const domainLabel = z.string().regex(/^[a-z0-9][a-z0-9_.-]{0,63}$/)
|
|
23
|
+
|
|
24
|
+
export const LEARNING_EDIT_VERDICTS = [
|
|
25
|
+
'helped',
|
|
26
|
+
'hurt',
|
|
27
|
+
'no observed effect',
|
|
28
|
+
'confounded',
|
|
29
|
+
] as const
|
|
30
|
+
/** @experimental What one edit did, as far as the evidence can separate it from the others. */
|
|
31
|
+
export type LearningEditVerdict = (typeof LEARNING_EDIT_VERDICTS)[number]
|
|
32
|
+
|
|
33
|
+
export const LEARNING_CONFIDENCE_LEVELS = ['high', 'medium', 'low'] as const
|
|
34
|
+
/** @experimental How far a lesson can be trusted. */
|
|
35
|
+
export type LearningConfidence = (typeof LEARNING_CONFIDENCE_LEVELS)[number]
|
|
36
|
+
|
|
37
|
+
const editSchema = z.object({
|
|
38
|
+
/** The part of the harness the edit touched, such as `instruction-skill`. */
|
|
39
|
+
surface: label,
|
|
40
|
+
/** What changed, in a sentence. */
|
|
41
|
+
change: text,
|
|
42
|
+
verdict: z.enum(LEARNING_EDIT_VERDICTS),
|
|
43
|
+
/**
|
|
44
|
+
* True only when this edit was tried alone (an ablation), so its verdict is
|
|
45
|
+
* separable even though the candidate changed several components. The store
|
|
46
|
+
* trusts this flag from the caller; it does not check that an ablation ran.
|
|
47
|
+
*/
|
|
48
|
+
isolated: z.boolean().optional(),
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const lessonSchema = z.object({
|
|
52
|
+
/** The shape of the situation the lesson applies to, not the incident itself. */
|
|
53
|
+
cue: text,
|
|
54
|
+
lesson: text,
|
|
55
|
+
/** References into the observations, session log or verification receipts. */
|
|
56
|
+
evidence: z.array(learningEvidenceSchema).min(1).max(16),
|
|
57
|
+
confidence: z.enum(LEARNING_CONFIDENCE_LEVELS),
|
|
58
|
+
confidenceReason: text,
|
|
59
|
+
/** Indexes into the entry's edits that the lesson rests on. */
|
|
60
|
+
edits: z.array(z.number().int().min(0).max(63)).max(64).optional(),
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
/** @experimental One edit row of the typed ledger. */
|
|
64
|
+
export type LearningEditRow = Readonly<z.infer<typeof editSchema>>
|
|
65
|
+
/** @experimental A lesson, with the confidence the evidence supports. */
|
|
66
|
+
export type LearningLesson = Readonly<
|
|
67
|
+
Omit<z.infer<typeof lessonSchema>, 'evidence' | 'edits'> & {
|
|
68
|
+
readonly evidence: readonly Readonly<z.infer<typeof learningEvidenceSchema>>[]
|
|
69
|
+
readonly edits?: readonly number[]
|
|
70
|
+
}
|
|
71
|
+
>
|
|
72
|
+
|
|
73
|
+
/** What a host passes when it records one finished cycle. */
|
|
74
|
+
export interface LearningExperienceInput {
|
|
75
|
+
/** The kind of task the cycle was about; retrieval never crosses domains. */
|
|
76
|
+
readonly domain: string
|
|
77
|
+
/** The cycle that produced the entry; one entry per cycle. */
|
|
78
|
+
readonly cycleId: string
|
|
79
|
+
/** The evaluator that judged the candidate. Lessons are only comparable under the same revision. */
|
|
80
|
+
readonly evaluatorRevision: string
|
|
81
|
+
readonly decision: 'adopted' | 'rejected'
|
|
82
|
+
/** Why, as the verification recorded it. */
|
|
83
|
+
readonly reason: string
|
|
84
|
+
/** The whole artefact, as proposed. */
|
|
85
|
+
readonly candidate: LearningSkillCandidate
|
|
86
|
+
readonly edits: readonly LearningEditRow[]
|
|
87
|
+
readonly lessons?: readonly LearningLesson[]
|
|
88
|
+
/**
|
|
89
|
+
* Evaluator-side material to refuse in anything the entry would keep. Required:
|
|
90
|
+
* pass `{}` to apply only the built-in markers and secret shapes.
|
|
91
|
+
*/
|
|
92
|
+
readonly leakGuard: LearningLeakGuard
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const inputSchema = z.object({
|
|
96
|
+
domain: domainLabel,
|
|
97
|
+
cycleId: z.string().uuid(),
|
|
98
|
+
evaluatorRevision: label,
|
|
99
|
+
decision: z.enum(['adopted', 'rejected']),
|
|
100
|
+
reason: text,
|
|
101
|
+
edits: z.array(editSchema).min(1).max(64),
|
|
102
|
+
lessons: z.array(lessonSchema).max(32).optional(),
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
/** @experimental A stored entry. `ordinal` is its position in recording order. */
|
|
106
|
+
export interface LearningExperienceEntry {
|
|
107
|
+
readonly ordinal: number
|
|
108
|
+
readonly domain: string
|
|
109
|
+
readonly cycleId: string
|
|
110
|
+
readonly evaluatorRevision: string
|
|
111
|
+
readonly decision: 'adopted' | 'rejected'
|
|
112
|
+
readonly reason: string
|
|
113
|
+
/** The full artefact as proposed, unabridged. */
|
|
114
|
+
readonly candidate: LearningSkillCandidate
|
|
115
|
+
readonly candidateRevision: string
|
|
116
|
+
readonly edits: readonly LearningEditRow[]
|
|
117
|
+
readonly lessons: readonly LearningLesson[]
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const CAP: Record<LearningConfidence, number> = { low: 0, medium: 1, high: 2 }
|
|
121
|
+
const BY_RANK: readonly LearningConfidence[] = ['low', 'medium', 'high']
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Apply the rules that keep a record honest. Several components in one
|
|
125
|
+
* candidate share a single outcome, so unless a row was tried alone its
|
|
126
|
+
* verdict becomes `confounded`. A lesson that rests on a confounded edit is
|
|
127
|
+
* capped at medium; one that rests only on confounded edits is low, and its
|
|
128
|
+
* reason says why.
|
|
129
|
+
*/
|
|
130
|
+
export function normalizeLearningExperience(
|
|
131
|
+
input: LearningExperienceInput,
|
|
132
|
+
): Omit<LearningExperienceEntry, 'ordinal'> {
|
|
133
|
+
const { leakGuard: _guard, candidate: rawCandidate, ...rest } = input
|
|
134
|
+
const parsed = inputSchema.parse(rest)
|
|
135
|
+
const candidate = normalizeLearningSkill(rawCandidate)
|
|
136
|
+
// More than one edit row changed together, whatever its component label.
|
|
137
|
+
const several = parsed.edits.length > 1
|
|
138
|
+
const edits: LearningEditRow[] = parsed.edits.map((edit) => {
|
|
139
|
+
const { isolated, ...row } = edit
|
|
140
|
+
if (several && isolated !== true) return { ...row, verdict: 'confounded' as const }
|
|
141
|
+
return isolated === true ? { ...row, isolated } : row
|
|
142
|
+
})
|
|
143
|
+
const lessons: LearningLesson[] = (parsed.lessons ?? []).map((lesson) => {
|
|
144
|
+
// A lesson that cites no edits rests on all of them.
|
|
145
|
+
const refs = lesson.edits?.length ? lesson.edits : edits.map((_, index) => index)
|
|
146
|
+
for (const index of refs)
|
|
147
|
+
if (index >= edits.length) throw new Error(`Lesson refers to edit ${index}, which is absent.`)
|
|
148
|
+
const confounded = refs.filter((index) => edits[index]?.verdict === 'confounded').length
|
|
149
|
+
if (confounded === 0) return lesson
|
|
150
|
+
const all = confounded === refs.length
|
|
151
|
+
const cap = all ? 'low' : 'medium'
|
|
152
|
+
if (CAP[lesson.confidence] <= CAP[cap]) {
|
|
153
|
+
return {
|
|
154
|
+
...lesson,
|
|
155
|
+
confidenceReason: `${lesson.confidenceReason} (Stands on confounded edits.)`,
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
...lesson,
|
|
160
|
+
confidence: BY_RANK[CAP[cap]] as LearningConfidence,
|
|
161
|
+
confidenceReason: `${lesson.confidenceReason} (Narrowed to ${cap}: ${
|
|
162
|
+
all ? 'every' : 'some'
|
|
163
|
+
} edit it rests on changed together with others.)`,
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
return {
|
|
167
|
+
domain: parsed.domain,
|
|
168
|
+
cycleId: parsed.cycleId.toLowerCase(),
|
|
169
|
+
evaluatorRevision: parsed.evaluatorRevision,
|
|
170
|
+
decision: parsed.decision,
|
|
171
|
+
reason: parsed.reason,
|
|
172
|
+
candidate,
|
|
173
|
+
candidateRevision: hashLearningSkill(candidate),
|
|
174
|
+
edits,
|
|
175
|
+
lessons,
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function collectStrings(
|
|
180
|
+
value: unknown,
|
|
181
|
+
subject: string,
|
|
182
|
+
out: { subject: string; text: string }[],
|
|
183
|
+
): void {
|
|
184
|
+
if (typeof value === 'string') out.push({ subject, text: value })
|
|
185
|
+
else if (Array.isArray(value)) value.forEach((v, i) => collectStrings(v, `${subject}[${i}]`, out))
|
|
186
|
+
else if (value && typeof value === 'object')
|
|
187
|
+
for (const [key, v] of Object.entries(value)) collectStrings(v, `${subject}.${key}`, out)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Refuse an entry that would keep evaluator-side material. Every string the
|
|
192
|
+
* entry holds is scanned, found by walking the entry rather than by naming
|
|
193
|
+
* fields; source keys are also tried as paths. Throws {@link LearningLeakError}.
|
|
194
|
+
*/
|
|
195
|
+
export function assertExperienceHasNoLeak(
|
|
196
|
+
entry: Omit<LearningExperienceEntry, 'ordinal'>,
|
|
197
|
+
guard: LearningLeakGuard | undefined,
|
|
198
|
+
): void {
|
|
199
|
+
const subjects: { subject: string; text?: string; paths?: readonly string[] }[] = []
|
|
200
|
+
const texts: { subject: string; text: string }[] = []
|
|
201
|
+
collectStrings(entry, 'experience', texts)
|
|
202
|
+
subjects.push(...texts)
|
|
203
|
+
for (const [i, source] of (entry.candidate.sources ?? []).entries())
|
|
204
|
+
subjects.push({ subject: `experience.candidate.sources[${i}].path`, paths: [source.key] })
|
|
205
|
+
assertNoLearningLeak(guard, subjects)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** @experimental Where retrieval may look. */
|
|
209
|
+
export interface LearningExperienceQuery {
|
|
210
|
+
readonly domain: string
|
|
211
|
+
/** The evaluator the reader is judged by; entries from another revision are not comparable. */
|
|
212
|
+
readonly evaluatorRevision: string
|
|
213
|
+
/** Evaluator-side material; entries are scanned again before they are returned. */
|
|
214
|
+
readonly leakGuard: LearningLeakGuard
|
|
215
|
+
/** Only entries recorded before this ordinal. The reader's own entry is never among them. */
|
|
216
|
+
readonly beforeOrdinal: number
|
|
217
|
+
readonly decision?: 'adopted' | 'rejected'
|
|
218
|
+
/** Newest first; default 20. */
|
|
219
|
+
readonly limit?: number
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** The retrieval rule, shared by every store: same domain, same evaluator, strictly earlier. */
|
|
223
|
+
export function selectLearningExperience(
|
|
224
|
+
entries: readonly LearningExperienceEntry[],
|
|
225
|
+
query: LearningExperienceQuery,
|
|
226
|
+
): LearningExperienceEntry[] {
|
|
227
|
+
const limit = Math.max(1, Math.min(query.limit ?? 20, 200))
|
|
228
|
+
return entries
|
|
229
|
+
.filter(
|
|
230
|
+
(entry) =>
|
|
231
|
+
entry.domain === query.domain &&
|
|
232
|
+
entry.evaluatorRevision === query.evaluatorRevision &&
|
|
233
|
+
entry.ordinal < query.beforeOrdinal &&
|
|
234
|
+
(query.decision === undefined || entry.decision === query.decision),
|
|
235
|
+
)
|
|
236
|
+
.sort((a, b) => b.ordinal - a.ordinal)
|
|
237
|
+
.slice(0, limit)
|
|
238
|
+
}
|