@namzu/sdk 55.0.0 → 56.0.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.
Files changed (219) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/compaction/serializer.d.ts.map +1 -1
  3. package/dist/compaction/serializer.js +27 -15
  4. package/dist/compaction/serializer.js.map +1 -1
  5. package/dist/compaction/verifier.d.ts.map +1 -1
  6. package/dist/compaction/verifier.js +3 -2
  7. package/dist/compaction/verifier.js.map +1 -1
  8. package/dist/constants/compaction/index.d.ts +3 -1
  9. package/dist/constants/compaction/index.d.ts.map +1 -1
  10. package/dist/constants/compaction/index.js +3 -1
  11. package/dist/constants/compaction/index.js.map +1 -1
  12. package/dist/{manager/resident/learning-cycle.d.ts → learning/cycle.d.ts} +45 -36
  13. package/dist/learning/cycle.d.ts.map +1 -0
  14. package/dist/{manager/resident/learning-cycle.js → learning/cycle.js} +52 -20
  15. package/dist/learning/cycle.js.map +1 -0
  16. package/dist/learning/deprecated.d.ts +86 -0
  17. package/dist/learning/deprecated.d.ts.map +1 -0
  18. package/dist/learning/deprecated.js +22 -0
  19. package/dist/learning/deprecated.js.map +1 -0
  20. package/dist/learning/experience-store.d.ts +35 -0
  21. package/dist/learning/experience-store.d.ts.map +1 -0
  22. package/dist/learning/experience-store.js +143 -0
  23. package/dist/learning/experience-store.js.map +1 -0
  24. package/dist/learning/experience.d.ts +150 -0
  25. package/dist/learning/experience.d.ts.map +1 -0
  26. package/dist/learning/experience.js +146 -0
  27. package/dist/learning/experience.js.map +1 -0
  28. package/dist/learning/generator.d.ts +118 -0
  29. package/dist/learning/generator.d.ts.map +1 -0
  30. package/dist/learning/generator.js +275 -0
  31. package/dist/learning/generator.js.map +1 -0
  32. package/dist/learning/leak-tripwire.d.ts +66 -0
  33. package/dist/learning/leak-tripwire.d.ts.map +1 -0
  34. package/dist/learning/leak-tripwire.js +208 -0
  35. package/dist/learning/leak-tripwire.js.map +1 -0
  36. package/dist/{manager/resident/learning-observation.d.ts → learning/observation.d.ts} +4 -4
  37. package/dist/learning/observation.d.ts.map +1 -0
  38. package/dist/{manager/resident/learning-observation.js → learning/observation.js} +3 -3
  39. package/dist/learning/observation.js.map +1 -0
  40. package/dist/{manager/resident/learning.d.ts → learning/skill.d.ts} +29 -29
  41. package/dist/learning/skill.d.ts.map +1 -0
  42. package/dist/{manager/resident/learning.js → learning/skill.js} +27 -27
  43. package/dist/learning/skill.js.map +1 -0
  44. package/dist/{manager/resident/learning-store.d.ts → learning/store.d.ts} +38 -31
  45. package/dist/learning/store.d.ts.map +1 -0
  46. package/dist/{manager/resident/learning-store.js → learning/store.js} +52 -20
  47. package/dist/learning/store.js.map +1 -0
  48. package/dist/manager/resident/activity.d.ts +30 -6
  49. package/dist/manager/resident/activity.d.ts.map +1 -1
  50. package/dist/manager/resident/activity.js.map +1 -1
  51. package/dist/manager/resident/agenda.d.ts +42 -12
  52. package/dist/manager/resident/agenda.d.ts.map +1 -1
  53. package/dist/manager/resident/agenda.js +9 -7
  54. package/dist/manager/resident/agenda.js.map +1 -1
  55. package/dist/manager/resident/consumption.d.ts +27 -5
  56. package/dist/manager/resident/consumption.d.ts.map +1 -1
  57. package/dist/manager/resident/consumption.js +2 -0
  58. package/dist/manager/resident/consumption.js.map +1 -1
  59. package/dist/manager/resident/delivery-window.d.ts +7 -1
  60. package/dist/manager/resident/delivery-window.d.ts.map +1 -1
  61. package/dist/manager/resident/delivery-window.js +2 -0
  62. package/dist/manager/resident/delivery-window.js.map +1 -1
  63. package/dist/manager/resident/evidence-recall.d.ts +7 -1
  64. package/dist/manager/resident/evidence-recall.d.ts.map +1 -1
  65. package/dist/manager/resident/evidence-recall.js +2 -0
  66. package/dist/manager/resident/evidence-recall.js.map +1 -1
  67. package/dist/manager/resident/history.d.ts +45 -9
  68. package/dist/manager/resident/history.d.ts.map +1 -1
  69. package/dist/manager/resident/history.js.map +1 -1
  70. package/dist/manager/resident/host.d.ts +44 -10
  71. package/dist/manager/resident/host.d.ts.map +1 -1
  72. package/dist/manager/resident/host.js +4 -2
  73. package/dist/manager/resident/host.js.map +1 -1
  74. package/dist/manager/resident/initiative.d.ts +32 -6
  75. package/dist/manager/resident/initiative.d.ts.map +1 -1
  76. package/dist/manager/resident/initiative.js +2 -0
  77. package/dist/manager/resident/initiative.js.map +1 -1
  78. package/dist/manager/resident/loop.d.ts +22 -4
  79. package/dist/manager/resident/loop.d.ts.map +1 -1
  80. package/dist/manager/resident/loop.js +7 -1
  81. package/dist/manager/resident/loop.js.map +1 -1
  82. package/dist/manager/resident/outbox.d.ts +42 -8
  83. package/dist/manager/resident/outbox.d.ts.map +1 -1
  84. package/dist/manager/resident/outbox.js +2 -0
  85. package/dist/manager/resident/outbox.js.map +1 -1
  86. package/dist/manager/resident/proposal.d.ts +17 -3
  87. package/dist/manager/resident/proposal.d.ts.map +1 -1
  88. package/dist/manager/resident/proposal.js +2 -0
  89. package/dist/manager/resident/proposal.js.map +1 -1
  90. package/dist/manager/resident/store.d.ts +27 -5
  91. package/dist/manager/resident/store.d.ts.map +1 -1
  92. package/dist/manager/resident/store.js +7 -1
  93. package/dist/manager/resident/store.js.map +1 -1
  94. package/dist/manager/resident/tool-evidence.d.ts +45 -9
  95. package/dist/manager/resident/tool-evidence.d.ts.map +1 -1
  96. package/dist/manager/resident/tool-evidence.js +5 -1
  97. package/dist/manager/resident/tool-evidence.js.map +1 -1
  98. package/dist/prompt/resident-learning.d.ts +12 -2
  99. package/dist/prompt/resident-learning.d.ts.map +1 -1
  100. package/dist/prompt/resident-learning.js +6 -5
  101. package/dist/prompt/resident-learning.js.map +1 -1
  102. package/dist/prompt/resident-step.d.ts +10 -4
  103. package/dist/prompt/resident-step.d.ts.map +1 -1
  104. package/dist/prompt/resident-step.js +6 -4
  105. package/dist/prompt/resident-step.js.map +1 -1
  106. package/dist/public-runtime.d.ts +9 -3
  107. package/dist/public-runtime.d.ts.map +1 -1
  108. package/dist/public-runtime.js +10 -3
  109. package/dist/public-runtime.js.map +1 -1
  110. package/dist/public-types.d.ts +9 -4
  111. package/dist/public-types.d.ts.map +1 -1
  112. package/dist/runtime/query/file-evidence-context.d.ts +4 -0
  113. package/dist/runtime/query/file-evidence-context.d.ts.map +1 -1
  114. package/dist/runtime/query/file-evidence-context.js +5 -1
  115. package/dist/runtime/query/file-evidence-context.js.map +1 -1
  116. package/dist/runtime/query/iteration/index.d.ts +2 -0
  117. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  118. package/dist/runtime/query/iteration/index.js +19 -11
  119. package/dist/runtime/query/iteration/index.js.map +1 -1
  120. package/dist/runtime/query/iteration/phases/per-call-review.d.ts +9 -0
  121. package/dist/runtime/query/iteration/phases/per-call-review.d.ts.map +1 -1
  122. package/dist/runtime/query/iteration/phases/per-call-review.js +19 -0
  123. package/dist/runtime/query/iteration/phases/per-call-review.js.map +1 -1
  124. package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
  125. package/dist/runtime/query/iteration/phases/tool-review.js +4 -1
  126. package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
  127. package/dist/runtime/query/iteration/step-context.d.ts +42 -0
  128. package/dist/runtime/query/iteration/step-context.d.ts.map +1 -1
  129. package/dist/runtime/query/iteration/step-context.js +114 -0
  130. package/dist/runtime/query/iteration/step-context.js.map +1 -1
  131. package/dist/runtime/query/iteration/step-shaping.d.ts +9 -1
  132. package/dist/runtime/query/iteration/step-shaping.d.ts.map +1 -1
  133. package/dist/runtime/query/iteration/step-shaping.js +21 -11
  134. package/dist/runtime/query/iteration/step-shaping.js.map +1 -1
  135. package/dist/runtime/query/steering.d.ts +13 -0
  136. package/dist/runtime/query/steering.d.ts.map +1 -1
  137. package/dist/runtime/query/steering.js +42 -0
  138. package/dist/runtime/query/steering.js.map +1 -1
  139. package/dist/scheduler/completion-inbox.js +1 -1
  140. package/dist/scheduler/completion-inbox.js.map +1 -1
  141. package/dist/tools/builtins/bash.d.ts +3 -5
  142. package/dist/tools/builtins/bash.d.ts.map +1 -1
  143. package/dist/tools/builtins/bash.js +14 -15
  144. package/dist/tools/builtins/bash.js.map +1 -1
  145. package/dist/tools/builtins/edit.js +1 -1
  146. package/dist/tools/builtins/edit.js.map +1 -1
  147. package/dist/tools/env-scrub.d.ts +8 -0
  148. package/dist/tools/env-scrub.d.ts.map +1 -1
  149. package/dist/tools/env-scrub.js +13 -0
  150. package/dist/tools/env-scrub.js.map +1 -1
  151. package/dist/tools/resident-history.d.ts +2 -0
  152. package/dist/tools/resident-history.d.ts.map +1 -1
  153. package/dist/tools/resident-history.js +2 -0
  154. package/dist/tools/resident-history.js.map +1 -1
  155. package/dist/tools/resident-tool-evidence.d.ts +5 -1
  156. package/dist/tools/resident-tool-evidence.d.ts.map +1 -1
  157. package/dist/tools/resident-tool-evidence.js +5 -1
  158. package/dist/tools/resident-tool-evidence.js.map +1 -1
  159. package/dist/turn/evidence-recall.d.ts +15 -3
  160. package/dist/turn/evidence-recall.d.ts.map +1 -1
  161. package/dist/turn/evidence-recall.js +116 -27
  162. package/dist/turn/evidence-recall.js.map +1 -1
  163. package/dist/types/message/notice-frame.d.ts +1 -1
  164. package/dist/types/message/notice-frame.d.ts.map +1 -1
  165. package/dist/types/message/notice-frame.js +1 -0
  166. package/dist/types/message/notice-frame.js.map +1 -1
  167. package/package.json +1 -1
  168. package/src/compaction/serializer.ts +29 -17
  169. package/src/compaction/verifier.ts +3 -2
  170. package/src/constants/compaction/index.ts +3 -1
  171. package/src/{manager/resident/learning-cycle.ts → learning/cycle.ts} +130 -78
  172. package/src/learning/deprecated.ts +124 -0
  173. package/src/learning/experience-store.ts +202 -0
  174. package/src/learning/experience.ts +238 -0
  175. package/src/learning/generator.ts +388 -0
  176. package/src/learning/leak-tripwire.ts +280 -0
  177. package/src/{manager/resident/learning-observation.ts → learning/observation.ts} +5 -5
  178. package/src/{manager/resident/learning.ts → learning/skill.ts} +64 -67
  179. package/src/{manager/resident/learning-store.ts → learning/store.ts} +97 -67
  180. package/src/manager/resident/activity.ts +30 -6
  181. package/src/manager/resident/agenda.ts +63 -33
  182. package/src/manager/resident/consumption.ts +27 -5
  183. package/src/manager/resident/delivery-window.ts +7 -1
  184. package/src/manager/resident/evidence-recall.ts +7 -1
  185. package/src/manager/resident/history.ts +45 -9
  186. package/src/manager/resident/host.ts +45 -11
  187. package/src/manager/resident/initiative.ts +32 -6
  188. package/src/manager/resident/loop.ts +22 -4
  189. package/src/manager/resident/outbox.ts +42 -8
  190. package/src/manager/resident/proposal.ts +17 -3
  191. package/src/manager/resident/store.ts +27 -5
  192. package/src/manager/resident/tool-evidence.ts +45 -9
  193. package/src/prompt/resident-learning.ts +16 -7
  194. package/src/prompt/resident-step.ts +19 -13
  195. package/src/public-runtime.ts +35 -5
  196. package/src/public-types.ts +96 -34
  197. package/src/runtime/query/file-evidence-context.ts +8 -1
  198. package/src/runtime/query/iteration/index.ts +27 -13
  199. package/src/runtime/query/iteration/phases/per-call-review.ts +19 -0
  200. package/src/runtime/query/iteration/phases/tool-review.ts +3 -0
  201. package/src/runtime/query/iteration/step-context.ts +138 -0
  202. package/src/runtime/query/iteration/step-shaping.ts +39 -13
  203. package/src/runtime/query/steering.ts +51 -0
  204. package/src/scheduler/completion-inbox.ts +1 -1
  205. package/src/tools/builtins/bash.ts +14 -14
  206. package/src/tools/builtins/edit.ts +1 -1
  207. package/src/tools/env-scrub.ts +13 -0
  208. package/src/tools/resident-history.ts +2 -0
  209. package/src/tools/resident-tool-evidence.ts +5 -1
  210. package/src/turn/evidence-recall.ts +125 -30
  211. package/src/types/message/notice-frame.ts +3 -1
  212. package/dist/manager/resident/learning-cycle.d.ts.map +0 -1
  213. package/dist/manager/resident/learning-cycle.js.map +0 -1
  214. package/dist/manager/resident/learning-observation.d.ts.map +0 -1
  215. package/dist/manager/resident/learning-observation.js.map +0 -1
  216. package/dist/manager/resident/learning-store.d.ts.map +0 -1
  217. package/dist/manager/resident/learning-store.js.map +0 -1
  218. package/dist/manager/resident/learning.d.ts.map +0 -1
  219. package/dist/manager/resident/learning.js.map +0 -1
@@ -3,25 +3,32 @@ import { z } from 'zod'
3
3
  import {
4
4
  type HarnessProtectionPlan,
5
5
  normalizeHarnessProtection,
6
- } from '../../eval/harness-protection.js'
6
+ } from '../eval/harness-protection.js'
7
7
  import {
8
8
  type HarnessReview,
9
9
  type HarnessVerificationBatch,
10
10
  reviewHarnessCandidate,
11
- } from '../../eval/harness-verification.js'
12
- import type { DiskResidentAgenda } from './agenda.js'
11
+ } from '../eval/harness-verification.js'
12
+ import type { DiskResidentAgenda } from '../manager/resident/agenda.js'
13
+ import { ResidentConflictError } from '../manager/resident/store.js'
13
14
  import {
14
- type ResidentLearningEvidence,
15
- type ResidentSkillCandidate,
16
- hashResidentSkill,
17
- normalizeResidentSkill,
18
- promoteResidentSkill,
19
- residentLearningEvidenceSchema,
20
- } from './learning.js'
21
- import { ResidentConflictError } from './store.js'
15
+ LearningLeakError,
16
+ type LearningLeakFinding,
17
+ type LearningLeakGuard,
18
+ type LearningLeakSubject,
19
+ assertNoLearningLeak,
20
+ } from './leak-tripwire.js'
21
+ import {
22
+ type LearningEvidence,
23
+ type LearningSkillCandidate,
24
+ hashLearningSkill,
25
+ learningEvidenceSchema,
26
+ normalizeLearningSkill,
27
+ promoteLearningSkill,
28
+ } from './skill.js'
22
29
 
23
30
  /** @experimental One explicitly requested experiment; callbacks own inference and evaluation. */
24
- export type ResidentLearningStage = 'explore' | 'generate' | 'verification' | 'confirmation'
31
+ export type LearningStage = 'explore' | 'generate' | 'verification' | 'confirmation'
25
32
 
26
33
  const receiptSchema = z.object({
27
34
  /** The session and turn of the execution this usage belongs to; one receipt per turn. */
@@ -32,10 +39,10 @@ const receiptSchema = z.object({
32
39
  })
33
40
 
34
41
  /** @experimental One non-overlapping execution's usage, including its retries. Null is unknown. */
35
- export type ResidentLearningReceipt = Readonly<z.infer<typeof receiptSchema>>
42
+ export type LearningReceipt = Readonly<z.infer<typeof receiptSchema>>
36
43
 
37
44
  /** @experimental Recorded values are lower bounds when a receipt or stage is incomplete. */
38
- export interface ResidentLearningConsumption {
45
+ export interface LearningConsumption {
39
46
  readonly tokens: number
40
47
  readonly costUsd: number
41
48
  readonly receipts: number
@@ -45,7 +52,7 @@ export interface ResidentLearningConsumption {
45
52
  }
46
53
 
47
54
  /** @experimental Append in sequence to host-owned durable storage before acknowledging. */
48
- export interface ResidentLearningCycleEvent {
55
+ export interface LearningCycleEvent {
49
56
  readonly cycleId: string
50
57
  readonly sequence: number
51
58
  readonly kind:
@@ -58,80 +65,86 @@ export interface ResidentLearningCycleEvent {
58
65
  | 'evaluation'
59
66
  | 'activation-requested'
60
67
  | 'finished'
61
- readonly stage?: ResidentLearningStage
68
+ readonly stage?: LearningStage
62
69
  readonly data: Readonly<Record<string, unknown>>
63
70
  }
64
71
 
65
72
  /** @experimental Use this stage's signal for every owned execution; await every usage append. */
66
- export interface ResidentLearningCycleContext {
73
+ export interface LearningCycleContext {
67
74
  readonly cycleId: string
68
- readonly stage: ResidentLearningStage
75
+ readonly stage: LearningStage
69
76
  readonly signal: AbortSignal
70
77
  /** Planning allowance, not a reservation or a cap on a callback's in-flight work. */
71
78
  readonly remainingUnits: number
72
- recordUsage(receipt: ResidentLearningReceipt): Promise<void>
79
+ recordUsage(receipt: LearningReceipt): Promise<void>
73
80
  }
74
81
 
75
82
  /** @experimental Original failure and active baseline; never contains confirmation tasks. */
76
- export interface ResidentLearningExplorationContext extends ResidentLearningCycleContext {
83
+ export interface LearningExplorationContext extends LearningCycleContext {
77
84
  readonly skillName: string
78
85
  /** Admitted by the host before generation; cannot be changed by a candidate. */
79
86
  readonly purpose?: 'task' | 'exploration'
80
- readonly failure: Readonly<{ evidence: ResidentLearningEvidence; trace: string }>
81
- readonly baseline: ResidentSkillCandidate | null
87
+ readonly failure: Readonly<{ evidence: LearningEvidence; trace: string }>
88
+ readonly baseline: LearningSkillCandidate | null
82
89
  }
83
90
 
84
91
  /** @experimental Host-retained environment observations, not a model's claim of success. */
85
- export interface ResidentLearningExploration {
86
- readonly evidence: ResidentLearningEvidence
92
+ export interface LearningExploration {
93
+ readonly evidence: LearningEvidence
87
94
  readonly trace: string
88
95
  }
89
96
 
90
97
  /** @experimental Generation sees completed exploration, never held-out evaluation cases. */
91
- export interface ResidentLearningGenerationContext extends ResidentLearningExplorationContext {
92
- readonly exploration?: Readonly<ResidentLearningExploration>
98
+ export interface LearningGenerationContext extends LearningExplorationContext {
99
+ readonly exploration?: Readonly<LearningExploration>
93
100
  }
94
101
 
95
102
  /** @experimental The host supplies independently scored, retained traces for these exact revisions. */
96
- export interface ResidentLearningEvaluationContext extends ResidentLearningCycleContext {
97
- readonly baseline: ResidentSkillCandidate | null
98
- readonly candidate: ResidentSkillCandidate
103
+ export interface LearningEvaluationContext extends LearningCycleContext {
104
+ readonly baseline: LearningSkillCandidate | null
105
+ readonly candidate: LearningSkillCandidate
99
106
  readonly baselineRevision: string
100
107
  readonly candidateRevision: string
101
108
  }
102
109
 
103
- /** @experimental No provider, default background loop or executable-code activation is installed. */
104
- export interface ResidentLearningCycleOptions {
110
+ /** @experimental The SDK installs no provider, background loop or executable-code activation; the host supplies them. */
111
+ export interface LearningCycleOptions {
105
112
  /** Omitted means task guidance. Explicitly select exploration to improve an explorer policy. */
106
113
  readonly purpose?: 'task' | 'exploration'
107
114
  /** Fixed before generation; both rounds must preserve every named task. */
108
115
  readonly protection: HarnessProtectionPlan
109
116
  readonly agenda: Pick<DiskResidentAgenda, 'read' | 'promoteSkill'>
110
117
  readonly skillName: string
111
- readonly failure: { readonly evidence: ResidentLearningEvidence; readonly trace: string }
118
+ readonly failure: { readonly evidence: LearningEvidence; readonly trace: string }
112
119
  readonly parentCycleId?: string
120
+ /**
121
+ * Evaluator-side material to keep out of the evidence and the guidance built
122
+ * from it. The built-in markers and secret shapes always apply. A hit refuses
123
+ * the cycle as a leak (see `LearningCycleResult.leak`), before anything is built.
124
+ */
125
+ readonly leakGuard?: LearningLeakGuard
113
126
  readonly signal: AbortSignal
114
127
  /** Stops subsequent stages/activation after observed excess; callbacks enforce in-flight limits. */
115
128
  readonly resources: { readonly unit: 'tokens' | 'usd'; readonly maxUnits: number }
116
- readonly record: (event: ResidentLearningCycleEvent) => Promise<void>
129
+ readonly record: (event: LearningCycleEvent) => Promise<void>
117
130
  /** Optional active tool exploration before synthesis; the host retains actual observations. */
118
- readonly explore?: (context: ResidentLearningExplorationContext) => Promise<{
119
- readonly observations: ResidentLearningExploration
131
+ readonly explore?: (context: LearningExplorationContext) => Promise<{
132
+ readonly observations: LearningExploration
120
133
  readonly usageComplete: boolean
121
134
  }>
122
- readonly generate: (context: ResidentLearningGenerationContext) => Promise<{
123
- readonly candidate: ResidentSkillCandidate
135
+ readonly generate: (context: LearningGenerationContext) => Promise<{
136
+ readonly candidate: LearningSkillCandidate
124
137
  /** All executions, including failures and side calls, have supplied receipts. */
125
138
  readonly usageComplete: boolean
126
139
  }>
127
- readonly evaluate: (context: ResidentLearningEvaluationContext) => Promise<{
140
+ readonly evaluate: (context: LearningEvaluationContext) => Promise<{
128
141
  readonly batch: HarnessVerificationBatch
129
142
  readonly usageComplete: boolean
130
143
  }>
131
144
  }
132
145
 
133
146
  /** @experimental An ambiguous activation must be inspected, never automatically replayed. */
134
- export interface ResidentLearningCycleResult {
147
+ export interface LearningCycleResult {
135
148
  readonly cycleId: string
136
149
  readonly status:
137
150
  | 'activated'
@@ -142,12 +155,14 @@ export interface ResidentLearningCycleResult {
142
155
  | 'failed'
143
156
  | 'activation-unknown'
144
157
  readonly reason: string
145
- readonly candidate?: ResidentSkillCandidate
158
+ readonly candidate?: LearningSkillCandidate
146
159
  readonly candidateRevision?: string
147
160
  readonly baselineRevision?: string
148
161
  readonly agendaRevision?: number
149
162
  readonly review?: HarnessReview
150
- readonly consumption: ResidentLearningConsumption
163
+ /** Present when the cycle was refused as a leak: where it was seen, never the text. */
164
+ readonly leak?: readonly LearningLeakFinding[]
165
+ readonly consumption: LearningConsumption
151
166
  readonly auditComplete: boolean
152
167
  }
153
168
 
@@ -156,9 +171,9 @@ export interface ResidentLearningCycleResult {
156
171
  * activate through the existing exact-revision transaction. No internal inference
157
172
  * loop, retry or model-generated success claim replaces the host's evaluator.
158
173
  */
159
- export async function runResidentLearningCycle(
160
- options: ResidentLearningCycleOptions,
161
- ): Promise<ResidentLearningCycleResult> {
174
+ export async function runLearningCycle(
175
+ options: LearningCycleOptions,
176
+ ): Promise<LearningCycleResult> {
162
177
  const protection = normalizeHarnessProtection(options.protection)
163
178
  const purpose = z.enum(['task', 'exploration']).parse(options.purpose ?? 'task')
164
179
  const purposeContext = options.purpose === undefined ? {} : { purpose }
@@ -167,7 +182,7 @@ export async function runResidentLearningCycle(
167
182
  .regex(/^[a-z0-9][a-z0-9_-]{0,63}$/)
168
183
  .parse(options.skillName)
169
184
  const failure = Object.freeze({
170
- evidence: Object.freeze(residentLearningEvidenceSchema.parse(options.failure.evidence)),
185
+ evidence: Object.freeze(learningEvidenceSchema.parse(options.failure.evidence)),
171
186
  trace: z.string().trim().min(1).max(32_000).parse(options.failure.trace),
172
187
  })
173
188
  const resources = z
@@ -176,18 +191,19 @@ export async function runResidentLearningCycle(
176
191
  const parentCycleId =
177
192
  options.parentCycleId === undefined ? undefined : z.string().uuid().parse(options.parentCycleId)
178
193
  const cycleId = randomUUID()
179
- const receipts = new Map<string, ResidentLearningReceipt>()
194
+ const receipts = new Map<string, LearningReceipt>()
180
195
  let sequence = 0
181
196
  let pending = Promise.resolve()
182
197
  let auditComplete = true
183
198
  let unfinishedStages = 0
184
- let candidate: ResidentSkillCandidate | undefined
199
+ let candidate: LearningSkillCandidate | undefined
185
200
  let candidateRevision: string | undefined
186
201
  let baselineRevision: string | undefined
187
202
  let review: HarnessReview | undefined
188
203
  let activatedRevision: number | undefined
189
204
  let activationRequested = false
190
- const usage = (): ResidentLearningConsumption => ({
205
+ let leak: readonly LearningLeakFinding[] | undefined
206
+ const usage = (): LearningConsumption => ({
191
207
  tokens: [...receipts.values()].reduce((n, r) => n + (r.tokens ?? 0), 0),
192
208
  costUsd: [...receipts.values()].reduce((n, r) => n + (r.costUsd ?? 0), 0),
193
209
  receipts: receipts.size,
@@ -196,9 +212,9 @@ export async function runResidentLearningCycle(
196
212
  unfinishedStages,
197
213
  })
198
214
  const append = (
199
- kind: ResidentLearningCycleEvent['kind'],
215
+ kind: LearningCycleEvent['kind'],
200
216
  data: Record<string, unknown>,
201
- stage?: ResidentLearningStage,
217
+ stage?: LearningStage,
202
218
  ) => {
203
219
  const event = Object.freeze({
204
220
  cycleId,
@@ -215,16 +231,14 @@ export async function runResidentLearningCycle(
215
231
  })
216
232
  return pending
217
233
  }
218
- const result = (
219
- status: ResidentLearningCycleResult['status'],
220
- reason: string,
221
- ): ResidentLearningCycleResult => ({
234
+ const result = (status: LearningCycleResult['status'], reason: string): LearningCycleResult => ({
222
235
  cycleId,
223
236
  status,
224
237
  reason,
225
238
  ...(candidate ? { candidate, candidateRevision } : {}),
226
239
  ...(baselineRevision ? { baselineRevision } : {}),
227
240
  ...(review ? { review } : {}),
241
+ ...(leak ? { leak } : {}),
228
242
  ...(activatedRevision !== undefined ? { agendaRevision: activatedRevision } : {}),
229
243
  consumption: usage(),
230
244
  auditComplete,
@@ -245,8 +259,17 @@ export async function runResidentLearningCycle(
245
259
  return 'The recorded resource allowance is exhausted before the next stage.'
246
260
  return null
247
261
  }
248
- const execute = async (): Promise<ResidentLearningCycleResult> => {
262
+ const execute = async (): Promise<LearningCycleResult> => {
249
263
  options.signal.throwIfAborted()
264
+ const guard = (...subjects: LearningLeakSubject[]) =>
265
+ assertNoLearningLeak(options.leakGuard, subjects)
266
+ guard(
267
+ {
268
+ subject: 'failure.evidence',
269
+ text: `${failure.evidence.key}\n${failure.evidence.source}\n${failure.evidence.reason}`,
270
+ },
271
+ { subject: 'failure.trace', text: failure.trace },
272
+ )
250
273
  const snapshot = structuredClone(await options.agenda.read())
251
274
  if (!snapshot) throw new Error('No resident agenda exists for this learning cycle.')
252
275
  if (snapshot.paused || snapshot.pursuits.some((p) => p.state.phase === 'running'))
@@ -256,8 +279,18 @@ export async function runResidentLearningCycle(
256
279
  throw new Error(
257
280
  'The active skill has a different learning purpose; use a distinct skill name.',
258
281
  )
259
- const baseline = current ? normalizeResidentSkill(current) : null
260
- baselineRevision = baseline ? hashResidentSkill(baseline) : 'none'
282
+ const baseline = current ? normalizeLearningSkill(current) : null
283
+ baselineRevision = baseline ? hashLearningSkill(baseline) : 'none'
284
+ if (baseline)
285
+ guard({
286
+ subject: 'baseline',
287
+ text: [
288
+ baseline.name,
289
+ baseline.description,
290
+ baseline.body,
291
+ ...(baseline.sources ? [JSON.stringify(baseline.sources)] : []),
292
+ ].join('\n'),
293
+ })
261
294
  await append('started', {
262
295
  tenantId: snapshot.tenantId,
263
296
  agentKey: snapshot.agentKey,
@@ -284,8 +317,8 @@ export async function runResidentLearningCycle(
284
317
  throw new ResidentConflictError()
285
318
  }
286
319
  const stage = async <T extends { usageComplete: boolean }>(
287
- name: ResidentLearningStage,
288
- callback: (context: ResidentLearningCycleContext) => Promise<T>,
320
+ name: LearningStage,
321
+ callback: (context: LearningCycleContext) => Promise<T>,
289
322
  ): Promise<T> => {
290
323
  await check()
291
324
  await append('stage-started', {}, name)
@@ -296,7 +329,7 @@ export async function runResidentLearningCycle(
296
329
  const before = receipts.size
297
330
  try {
298
331
  const measured = usage()
299
- const context: ResidentLearningCycleContext = Object.freeze({
332
+ const context: LearningCycleContext = Object.freeze({
300
333
  cycleId,
301
334
  stage: name,
302
335
  signal: options.signal,
@@ -304,7 +337,7 @@ export async function runResidentLearningCycle(
304
337
  0,
305
338
  resources.maxUnits - (resources.unit === 'tokens' ? measured.tokens : measured.costUsd),
306
339
  ),
307
- recordUsage: async (input: ResidentLearningReceipt) => {
340
+ recordUsage: async (input: LearningReceipt) => {
308
341
  try {
309
342
  if (!open) throw new Error('A closed learning stage cannot record new usage.')
310
343
  const receipt = Object.freeze(receiptSchema.parse(input))
@@ -344,18 +377,23 @@ export async function runResidentLearningCycle(
344
377
  open = false
345
378
  }
346
379
  }
347
- let exploration: Readonly<ResidentLearningExploration> | undefined
380
+ let exploration: Readonly<LearningExploration> | undefined
348
381
  if (options.explore) {
349
382
  const explore = options.explore
350
383
  const observed = await stage('explore', (context) =>
351
384
  explore(Object.freeze({ ...context, skillName, ...purposeContext, failure, baseline })),
352
385
  )
353
386
  exploration = Object.freeze({
354
- evidence: Object.freeze(
355
- residentLearningEvidenceSchema.parse(observed.observations.evidence),
356
- ),
387
+ evidence: Object.freeze(learningEvidenceSchema.parse(observed.observations.evidence)),
357
388
  trace: z.string().trim().min(1).max(32_000).parse(observed.observations.trace),
358
389
  })
390
+ guard(
391
+ {
392
+ subject: 'exploration.evidence',
393
+ text: `${exploration.evidence.key}\n${exploration.evidence.source}\n${exploration.evidence.reason}`,
394
+ },
395
+ { subject: 'exploration.trace', text: exploration.trace },
396
+ )
359
397
  await append(
360
398
  'exploration',
361
399
  {
@@ -379,8 +417,19 @@ export async function runResidentLearningCycle(
379
417
  }),
380
418
  ),
381
419
  )
382
- candidateRevision = hashResidentSkill(generated.candidate)
383
- candidate = normalizeResidentSkill(generated.candidate)
420
+ const built = normalizeLearningSkill(generated.candidate)
421
+ guard(
422
+ { subject: 'candidate.name', text: built.name },
423
+ { subject: 'candidate.description', text: built.description },
424
+ { subject: 'candidate.body', text: built.body },
425
+ {
426
+ subject: 'candidate.sources',
427
+ text: (built.sources ?? []).map((s) => `${s.key}\n${s.revision}`).join('\n'),
428
+ paths: (built.sources ?? []).map((s) => s.key),
429
+ },
430
+ )
431
+ candidateRevision = hashLearningSkill(generated.candidate)
432
+ candidate = built
384
433
  if ((candidate.purpose ?? 'task') !== purpose)
385
434
  throw new Error('Generated skill purpose does not match the host-admitted learning purpose.')
386
435
  if (candidate.name !== skillName)
@@ -396,7 +445,7 @@ export async function runResidentLearningCycle(
396
445
  Object.freeze({
397
446
  ...context,
398
447
  baseline,
399
- candidate: candidate as ResidentSkillCandidate,
448
+ candidate: candidate as LearningSkillCandidate,
400
449
  baselineRevision: baselineRevision as string,
401
450
  candidateRevision: candidateRevision as string,
402
451
  }),
@@ -446,14 +495,14 @@ export async function runResidentLearningCycle(
446
495
  if (review.decision !== 'accept')
447
496
  return result(review.decision === 'reject' ? 'rejected' : 'inconclusive', review.reason)
448
497
  await check()
449
- const evidence: ResidentLearningEvidence = {
498
+ const evidence: LearningEvidence = {
450
499
  key: cycleId,
451
500
  source: `resident-learning-cycle:${cycleId}`,
452
501
  reason: `Paired verification and fresh confirmation of guidance derived from evidence ${failure.evidence.key}.`,
453
502
  }
454
503
  // Validate all existing activation contracts before entering the ambiguous
455
504
  // commit interval. The store repeats this against its atomic snapshot.
456
- promoteResidentSkill(
505
+ promoteLearningSkill(
457
506
  snapshot.learning,
458
507
  candidate,
459
508
  { verification, confirmation, protection },
@@ -475,18 +524,21 @@ export async function runResidentLearningCycle(
475
524
  activatedRevision = updated.revision
476
525
  return result('activated', 'Evaluated guidance was committed for subsequent admissions.')
477
526
  }
478
- let outcome: ResidentLearningCycleResult
527
+ let outcome: LearningCycleResult
479
528
  try {
480
529
  outcome = await execute()
481
530
  } catch (error) {
531
+ if (error instanceof LearningLeakError) leak = error.findings
482
532
  outcome = result(
483
- error instanceof ResidentConflictError
484
- ? 'conflict'
485
- : activationRequested
486
- ? 'activation-unknown'
487
- : options.signal.aborted
488
- ? 'cancelled'
489
- : 'failed',
533
+ error instanceof LearningLeakError
534
+ ? 'failed'
535
+ : error instanceof ResidentConflictError
536
+ ? 'conflict'
537
+ : activationRequested
538
+ ? 'activation-unknown'
539
+ : options.signal.aborted
540
+ ? 'cancelled'
541
+ : 'failed',
490
542
  error instanceof Error ? error.message : String(error),
491
543
  )
492
544
  }
@@ -0,0 +1,124 @@
1
+ /**
2
+ * The learning module used to live under `manager/resident` and carried a
3
+ * `Resident` prefix. Learning is a feature of all of Namzu, not of residents,
4
+ * so every name now lives in `learning/` without the prefix. The old names stay
5
+ * as aliases of the new ones until the next major release.
6
+ */
7
+ import { runLearningCycle } from './cycle.js'
8
+ import type * as Cycle from './cycle.js'
9
+ import type * as Observation from './observation.js'
10
+ import { hashLearningSkill, projectLearning } from './skill.js'
11
+ import type * as Skill from './skill.js'
12
+ import {
13
+ SqliteLearningStore,
14
+ runStoredLearningCycle,
15
+ runStoredLearningFromObservations,
16
+ } from './store.js'
17
+ import type * as Store from './store.js'
18
+
19
+ /** @deprecated Renamed to `LearningEvidence`; the old name is removed in the next major release. */
20
+ export type ResidentLearningEvidence = Skill.LearningEvidence
21
+
22
+ /** @deprecated Renamed to `LearningSkillCandidate`; the old name is removed in the next major release. */
23
+ export type ResidentSkillCandidate = Skill.LearningSkillCandidate
24
+
25
+ /** @deprecated Renamed to `LearnedSkill`; the old name is removed in the next major release. */
26
+ export type ResidentLearnedSkill = Skill.LearnedSkill
27
+
28
+ /** @deprecated Renamed to `LearningState`; the old name is removed in the next major release. */
29
+ export type ResidentLearningState = Skill.LearningState
30
+
31
+ /** @deprecated Renamed to `LearningProfileUpdate`; the old name is removed in the next major release. */
32
+ export type ResidentProfileUpdate = Skill.LearningProfileUpdate
33
+
34
+ /** @deprecated Renamed to `LearningSkillEvaluation`; the old name is removed in the next major release. */
35
+ export type ResidentSkillEvaluation = Skill.LearningSkillEvaluation
36
+
37
+ /** @deprecated Renamed to `LearningProjectionOptions`; the old name is removed in the next major release. */
38
+ export type ResidentLearningProjectionOptions = Skill.LearningProjectionOptions
39
+
40
+ /** @deprecated Renamed to `LearningSource`; the old name is removed in the next major release. */
41
+ export type ResidentLearningSource = Skill.LearningSource
42
+
43
+ /** @deprecated Renamed to `LearningProjection`; the old name is removed in the next major release. */
44
+ export type ResidentLearningProjection = Skill.LearningProjection
45
+
46
+ /** @deprecated Renamed to `LearningStage`; the old name is removed in the next major release. */
47
+ export type ResidentLearningStage = Cycle.LearningStage
48
+
49
+ /** @deprecated Renamed to `LearningReceipt`; the old name is removed in the next major release. */
50
+ export type ResidentLearningReceipt = Cycle.LearningReceipt
51
+
52
+ /** @deprecated Renamed to `LearningConsumption`; the old name is removed in the next major release. */
53
+ export type ResidentLearningConsumption = Cycle.LearningConsumption
54
+
55
+ /** @deprecated Renamed to `LearningCycleEvent`; the old name is removed in the next major release. */
56
+ export type ResidentLearningCycleEvent = Cycle.LearningCycleEvent
57
+
58
+ /** @deprecated Renamed to `LearningCycleContext`; the old name is removed in the next major release. */
59
+ export type ResidentLearningCycleContext = Cycle.LearningCycleContext
60
+
61
+ /** @deprecated Renamed to `LearningGenerationContext`; the old name is removed in the next major release. */
62
+ export type ResidentLearningGenerationContext = Cycle.LearningGenerationContext
63
+
64
+ /** @deprecated Renamed to `LearningExplorationContext`; the old name is removed in the next major release. */
65
+ export type ResidentLearningExplorationContext = Cycle.LearningExplorationContext
66
+
67
+ /** @deprecated Renamed to `LearningExploration`; the old name is removed in the next major release. */
68
+ export type ResidentLearningExploration = Cycle.LearningExploration
69
+
70
+ /** @deprecated Renamed to `LearningEvaluationContext`; the old name is removed in the next major release. */
71
+ export type ResidentLearningEvaluationContext = Cycle.LearningEvaluationContext
72
+
73
+ /** @deprecated Renamed to `LearningCycleOptions`; the old name is removed in the next major release. */
74
+ export type ResidentLearningCycleOptions = Cycle.LearningCycleOptions
75
+
76
+ /** @deprecated Renamed to `LearningCycleResult`; the old name is removed in the next major release. */
77
+ export type ResidentLearningCycleResult = Cycle.LearningCycleResult
78
+
79
+ /** @deprecated Renamed to `SqliteLearningStoreOptions`; the old name is removed in the next major release. */
80
+ export type SqliteResidentLearningStoreOptions = Store.SqliteLearningStoreOptions
81
+
82
+ /** @deprecated Renamed to `LearningArtifact`; the old name is removed in the next major release. */
83
+ export type ResidentLearningArtifact = Store.LearningArtifact
84
+
85
+ /** @deprecated Renamed to `LearningRecordedUsage`; the old name is removed in the next major release. */
86
+ export type ResidentLearningRecordedUsage = Store.LearningRecordedUsage
87
+
88
+ /** @deprecated Renamed to `LearningCycleSummary`; the old name is removed in the next major release. */
89
+ export type ResidentLearningCycleSummary = Store.LearningCycleSummary
90
+
91
+ /** @deprecated Renamed to `LearningDiscoveryOptions`; the old name is removed in the next major release. */
92
+ export type ResidentLearningDiscoveryOptions = Store.LearningDiscoveryOptions
93
+
94
+ /** @deprecated Renamed to `LearningDiscoveryResult`; the old name is removed in the next major release. */
95
+ export type ResidentLearningDiscoveryResult = Store.LearningDiscoveryResult
96
+
97
+ /** @deprecated Renamed to `LearningObservation`; the old name is removed in the next major release. */
98
+ export type ResidentLearningObservation = Observation.LearningObservation
99
+
100
+ /** @deprecated Renamed to `LearningObservationRecord`; the old name is removed in the next major release. */
101
+ export type ResidentLearningObservationRecord = Observation.LearningObservationRecord
102
+
103
+ /** @deprecated Renamed to `LearningTarget`; the old name is removed in the next major release. */
104
+ export type ResidentLearningTarget = Observation.LearningTarget
105
+
106
+ /** @deprecated Renamed to `hashLearningSkill`; the old name is removed in the next major release. */
107
+ export const hashResidentSkill = hashLearningSkill
108
+
109
+ /** @deprecated Renamed to `projectLearning`; the old name is removed in the next major release. */
110
+ export const projectResidentLearning = projectLearning
111
+
112
+ /** @deprecated Renamed to `runLearningCycle`; the old name is removed in the next major release. */
113
+ export const runResidentLearningCycle = runLearningCycle
114
+
115
+ /** @deprecated Renamed to `runStoredLearningCycle`; the old name is removed in the next major release. */
116
+ export const runStoredResidentLearningCycle = runStoredLearningCycle
117
+
118
+ /** @deprecated Renamed to `runStoredLearningFromObservations`; the old name is removed in the next major release. */
119
+ export const runStoredResidentLearningFromObservations = runStoredLearningFromObservations
120
+
121
+ /** @deprecated Renamed to `SqliteLearningStore`; the old name is removed in the next major release. */
122
+ export const SqliteResidentLearningStore = SqliteLearningStore
123
+ /** @deprecated Renamed to `SqliteLearningStore`; the old name is removed in the next major release. */
124
+ export type SqliteResidentLearningStore = SqliteLearningStore