@namzu/sdk 54.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 +43 -0
- package/dist/agents/QueryAgent.d.ts.map +1 -1
- package/dist/agents/QueryAgent.js +1 -0
- package/dist/agents/QueryAgent.js.map +1 -1
- package/dist/compaction/extractor.d.ts.map +1 -1
- package/dist/compaction/extractor.js +5 -1
- package/dist/compaction/extractor.js.map +1 -1
- package/dist/compaction/managers/structured.d.ts.map +1 -1
- package/dist/compaction/managers/structured.js +5 -0
- package/dist/compaction/managers/structured.js.map +1 -1
- package/dist/compaction/manual.d.ts.map +1 -1
- package/dist/compaction/manual.js +6 -0
- package/dist/compaction/manual.js.map +1 -1
- package/dist/compaction/serializer.d.ts +6 -0
- package/dist/compaction/serializer.d.ts.map +1 -1
- package/dist/compaction/serializer.js +21 -3
- package/dist/compaction/serializer.js.map +1 -1
- package/dist/compaction/verifier.d.ts.map +1 -1
- package/dist/compaction/verifier.js +27 -6
- package/dist/compaction/verifier.js.map +1 -1
- package/dist/constants/compaction/index.d.ts +1 -0
- package/dist/constants/compaction/index.d.ts.map +1 -1
- package/dist/constants/compaction/index.js +1 -0
- package/dist/constants/compaction/index.js.map +1 -1
- 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 +19 -5
- 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 +31 -9
- 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/coding-agent-doctrine.d.ts +3 -3
- package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
- package/dist/prompt/coding-agent-doctrine.js +18 -20
- package/dist/prompt/coding-agent-doctrine.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 +10 -4
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +11 -4
- 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/checkpoint.d.ts +12 -2
- package/dist/runtime/query/checkpoint.d.ts.map +1 -1
- package/dist/runtime/query/checkpoint.js +42 -8
- package/dist/runtime/query/checkpoint.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +39 -1
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +115 -7
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts +18 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +20 -1
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +17 -8
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/context.d.ts +24 -2
- package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/context.js +22 -22
- package/dist/runtime/query/iteration/phases/context.js.map +1 -1
- package/dist/runtime/query/iteration/phases/per-call-review.d.ts +87 -0
- package/dist/runtime/query/iteration/phases/per-call-review.d.ts.map +1 -0
- package/dist/runtime/query/iteration/phases/per-call-review.js +513 -0
- package/dist/runtime/query/iteration/phases/per-call-review.js.map +1 -0
- package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.js +129 -62
- package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
- package/dist/runtime/query/iteration/step-context.d.ts +14 -0
- package/dist/runtime/query/iteration/step-context.d.ts.map +1 -1
- package/dist/runtime/query/iteration/step-context.js +29 -0
- package/dist/runtime/query/iteration/step-context.js.map +1 -1
- package/dist/runtime/query/iteration/step-shaping.d.ts.map +1 -1
- package/dist/runtime/query/iteration/step-shaping.js +6 -2
- package/dist/runtime/query/iteration/step-shaping.js.map +1 -1
- package/dist/runtime/query/prepare-turn.d.ts.map +1 -1
- package/dist/runtime/query/prepare-turn.js +16 -3
- package/dist/runtime/query/prepare-turn.js.map +1 -1
- package/dist/runtime/query/project-instructions.d.ts +24 -5
- package/dist/runtime/query/project-instructions.d.ts.map +1 -1
- package/dist/runtime/query/project-instructions.js +107 -35
- package/dist/runtime/query/project-instructions.js.map +1 -1
- package/dist/runtime/query/prompt.d.ts +6 -0
- package/dist/runtime/query/prompt.d.ts.map +1 -1
- package/dist/runtime/query/prompt.js +10 -49
- package/dist/runtime/query/prompt.js.map +1 -1
- package/dist/runtime/query/resume-pending.d.ts +8 -0
- package/dist/runtime/query/resume-pending.d.ts.map +1 -1
- package/dist/runtime/query/resume-pending.js +101 -0
- package/dist/runtime/query/resume-pending.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +18 -0
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +10 -0
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/steering.d.ts +55 -56
- package/dist/runtime/query/steering.d.ts.map +1 -1
- package/dist/runtime/query/steering.js +78 -72
- package/dist/runtime/query/steering.js.map +1 -1
- package/dist/tools/builtins/bash.d.ts +8 -0
- package/dist/tools/builtins/bash.d.ts.map +1 -1
- package/dist/tools/builtins/bash.js +13 -18
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/browser.d.ts.map +1 -1
- package/dist/tools/builtins/browser.js +38 -12
- package/dist/tools/builtins/browser.js.map +1 -1
- package/dist/tools/builtins/edit.js +4 -4
- package/dist/tools/builtins/edit.js.map +1 -1
- package/dist/tools/builtins/job.js +1 -1
- package/dist/tools/builtins/job.js.map +1 -1
- package/dist/tools/builtins/skill.js +1 -1
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/tools/builtins/structuredOutput.js +1 -1
- package/dist/tools/builtins/structuredOutput.js.map +1 -1
- package/dist/tools/builtins/verify-outputs.js +1 -1
- package/dist/tools/builtins/verify-outputs.js.map +1 -1
- package/dist/tools/builtins/write-file.js +4 -4
- package/dist/tools/builtins/write-file.js.map +1 -1
- package/dist/tools/coordinator/ask-user-question.d.ts +6 -0
- package/dist/tools/coordinator/ask-user-question.d.ts.map +1 -1
- package/dist/tools/coordinator/ask-user-question.js +6 -1
- package/dist/tools/coordinator/ask-user-question.js.map +1 -1
- package/dist/tools/coordinator/index.d.ts.map +1 -1
- package/dist/tools/coordinator/index.js +10 -3
- package/dist/tools/coordinator/index.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/dist/tools/schedules/loop-tool.d.ts.map +1 -1
- package/dist/tools/schedules/loop-tool.js +4 -2
- package/dist/tools/schedules/loop-tool.js.map +1 -1
- package/dist/tools/schedules/schedule-tool.js +2 -2
- package/dist/tools/schedules/schedule-tool.js.map +1 -1
- package/dist/tools/task/create.js +1 -1
- package/dist/tools/task/create.js.map +1 -1
- package/dist/tools/untrusted-envelope.js +1 -1
- package/dist/tools/untrusted-envelope.js.map +1 -1
- package/dist/turn/evidence-query.d.ts.map +1 -1
- package/dist/turn/evidence-query.js +16 -1
- package/dist/turn/evidence-query.js.map +1 -1
- package/dist/turn/evidence-recall.js +1 -1
- package/dist/turn/evidence-recall.js.map +1 -1
- package/dist/types/agent/base.d.ts +7 -0
- package/dist/types/agent/base.d.ts.map +1 -1
- package/dist/types/hitl/index.d.ts +66 -0
- package/dist/types/hitl/index.d.ts.map +1 -1
- package/dist/types/hitl/index.js.map +1 -1
- package/dist/types/message/index.d.ts +8 -0
- package/dist/types/message/index.d.ts.map +1 -1
- package/dist/types/message/index.js +12 -2
- package/dist/types/message/index.js.map +1 -1
- package/dist/types/message/notice-frame.d.ts +41 -0
- package/dist/types/message/notice-frame.d.ts.map +1 -0
- package/dist/types/message/notice-frame.js +65 -0
- package/dist/types/message/notice-frame.js.map +1 -0
- package/package.json +1 -1
- package/src/agents/QueryAgent.ts +1 -0
- package/src/compaction/extractor.ts +8 -1
- package/src/compaction/managers/structured.ts +3 -0
- package/src/compaction/manual.ts +5 -0
- package/src/compaction/serializer.ts +28 -3
- package/src/compaction/verifier.ts +32 -6
- package/src/constants/compaction/index.ts +1 -0
- 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/coding-agent-doctrine.ts +18 -20
- package/src/prompt/resident-learning.ts +16 -7
- package/src/prompt/resident-step.ts +19 -13
- package/src/public-runtime.ts +36 -5
- package/src/public-types.ts +96 -34
- package/src/runtime/query/checkpoint.ts +49 -8
- package/src/runtime/query/executor.ts +165 -15
- package/src/runtime/query/index.ts +45 -1
- package/src/runtime/query/iteration/index.ts +19 -6
- package/src/runtime/query/iteration/phases/context.ts +37 -11
- package/src/runtime/query/iteration/phases/per-call-review.ts +617 -0
- package/src/runtime/query/iteration/phases/tool-review.ts +168 -83
- package/src/runtime/query/iteration/step-context.ts +29 -0
- package/src/runtime/query/iteration/step-shaping.ts +6 -2
- package/src/runtime/query/prepare-turn.ts +18 -1
- package/src/runtime/query/project-instructions.ts +119 -38
- package/src/runtime/query/prompt.ts +11 -54
- package/src/runtime/query/resume-pending.ts +131 -0
- package/src/runtime/query/review-policy.ts +28 -0
- package/src/runtime/query/steering.ts +100 -96
- package/src/tools/builtins/bash.ts +13 -19
- package/src/tools/builtins/browser.ts +38 -12
- package/src/tools/builtins/edit.ts +4 -4
- package/src/tools/builtins/job.ts +1 -1
- package/src/tools/builtins/skill.ts +1 -1
- package/src/tools/builtins/structuredOutput.ts +1 -1
- package/src/tools/builtins/verify-outputs.ts +1 -1
- package/src/tools/builtins/write-file.ts +4 -4
- package/src/tools/coordinator/ask-user-question.ts +12 -1
- package/src/tools/coordinator/index.ts +10 -3
- package/src/tools/resident-history.ts +2 -0
- package/src/tools/resident-tool-evidence.ts +5 -1
- package/src/tools/schedules/loop-tool.ts +4 -2
- package/src/tools/schedules/schedule-tool.ts +2 -2
- package/src/tools/task/create.ts +1 -1
- package/src/tools/untrusted-envelope.ts +1 -1
- package/src/turn/evidence-query.ts +19 -1
- package/src/turn/evidence-recall.ts +1 -1
- package/src/types/agent/base.ts +8 -0
- package/src/types/hitl/index.ts +67 -0
- package/src/types/message/index.ts +20 -2
- package/src/types/message/notice-frame.ts +88 -0
- 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,143 @@
|
|
|
1
|
+
import { mkdirSync } from 'node:fs';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { dirname, resolve } from 'node:path';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { asProjectId, asTenantId } from '../utils/id.js';
|
|
6
|
+
import { assertExperienceHasNoLeak, normalizeLearningExperience, selectLearningExperience, } from './experience.js';
|
|
7
|
+
const SCHEMA = `
|
|
8
|
+
CREATE TABLE experience (
|
|
9
|
+
ordinal INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
10
|
+
tenant_id TEXT NOT NULL, project_id TEXT NOT NULL, agent_key TEXT NOT NULL,
|
|
11
|
+
domain TEXT NOT NULL, cycle_id TEXT NOT NULL, evaluator_revision TEXT NOT NULL,
|
|
12
|
+
decision TEXT NOT NULL, body TEXT NOT NULL,
|
|
13
|
+
UNIQUE(tenant_id, project_id, agent_key, cycle_id)
|
|
14
|
+
);
|
|
15
|
+
CREATE INDEX experience_read ON experience(
|
|
16
|
+
tenant_id, project_id, agent_key, domain, evaluator_revision, ordinal
|
|
17
|
+
);
|
|
18
|
+
`;
|
|
19
|
+
/** The schema version this store reads and writes. */
|
|
20
|
+
export const LEARNING_EXPERIENCE_STORE_VERSION = 1;
|
|
21
|
+
/**
|
|
22
|
+
* @experimental Durable experience library. Entries are immutable: recording
|
|
23
|
+
* the same cycle again with identical content returns the stored entry, with
|
|
24
|
+
* different content is refused. The artefact is stored whole.
|
|
25
|
+
*/
|
|
26
|
+
export class SqliteLearningExperienceStore {
|
|
27
|
+
databasePath;
|
|
28
|
+
scope;
|
|
29
|
+
readOnly;
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.databasePath = resolve(options.databasePath);
|
|
32
|
+
this.scope = Object.freeze({
|
|
33
|
+
tenantId: asTenantId(options.scope.tenantId.toLowerCase()),
|
|
34
|
+
projectId: asProjectId(options.scope.projectId.toLowerCase()),
|
|
35
|
+
agentKey: z
|
|
36
|
+
.string()
|
|
37
|
+
.regex(/^[a-z0-9][a-z0-9_-]{0,63}$/)
|
|
38
|
+
.parse(options.scope.agentKey),
|
|
39
|
+
});
|
|
40
|
+
this.readOnly = options.readOnly === true;
|
|
41
|
+
}
|
|
42
|
+
use(write, operation, whenEmpty) {
|
|
43
|
+
if (write && this.readOnly)
|
|
44
|
+
throw new Error('Learning experience database is read-only.');
|
|
45
|
+
let Database;
|
|
46
|
+
try {
|
|
47
|
+
Database = createRequire(import.meta.url)('node:sqlite')
|
|
48
|
+
.DatabaseSync;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
throw new Error('SqliteLearningExperienceStore requires Node.js 22.13 or newer.', {
|
|
52
|
+
cause: error,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (!this.readOnly)
|
|
56
|
+
mkdirSync(dirname(this.databasePath), { recursive: true, mode: 0o700 });
|
|
57
|
+
const db = new Database(this.databasePath, { readOnly: this.readOnly });
|
|
58
|
+
try {
|
|
59
|
+
db.exec('PRAGMA busy_timeout = 5000;');
|
|
60
|
+
if (!this.readOnly)
|
|
61
|
+
db.exec('PRAGMA journal_mode = DELETE; PRAGMA synchronous = FULL;');
|
|
62
|
+
db.exec(write ? 'BEGIN IMMEDIATE' : 'BEGIN');
|
|
63
|
+
try {
|
|
64
|
+
const version = Number(db.prepare('PRAGMA user_version').get()?.user_version);
|
|
65
|
+
if (version === 0 && write) {
|
|
66
|
+
db.exec(`${SCHEMA} PRAGMA user_version = ${LEARNING_EXPERIENCE_STORE_VERSION};`);
|
|
67
|
+
}
|
|
68
|
+
else if (version === 0) {
|
|
69
|
+
if (whenEmpty) {
|
|
70
|
+
db.exec('COMMIT');
|
|
71
|
+
return whenEmpty();
|
|
72
|
+
}
|
|
73
|
+
throw new Error('Learning experience database is not initialized; open it for writing first.');
|
|
74
|
+
}
|
|
75
|
+
else if (version !== LEARNING_EXPERIENCE_STORE_VERSION)
|
|
76
|
+
throw new Error(`Learning experience database is at schema version ${version}; this store reads version ${LEARNING_EXPERIENCE_STORE_VERSION} only.`);
|
|
77
|
+
const value = operation(db);
|
|
78
|
+
db.exec('COMMIT');
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
db.exec('ROLLBACK');
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
db.close();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
scopeArgs() {
|
|
91
|
+
return [this.scope.tenantId, this.scope.projectId, this.scope.agentKey];
|
|
92
|
+
}
|
|
93
|
+
decode(row) {
|
|
94
|
+
return {
|
|
95
|
+
...JSON.parse(row.body),
|
|
96
|
+
ordinal: row.ordinal,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** Record one finished cycle. Throws `LearningLeakError` when the entry carries evaluator-side material. */
|
|
100
|
+
async record(input) {
|
|
101
|
+
if (!input.leakGuard)
|
|
102
|
+
throw new Error('Recording an experience entry requires a leakGuard.');
|
|
103
|
+
const entry = normalizeLearningExperience(input);
|
|
104
|
+
assertExperienceHasNoLeak(entry, input.leakGuard);
|
|
105
|
+
const body = JSON.stringify(entry);
|
|
106
|
+
return this.use(true, (db) => {
|
|
107
|
+
const existing = db
|
|
108
|
+
.prepare('SELECT ordinal, body FROM experience WHERE tenant_id=? AND project_id=? AND agent_key=? AND cycle_id=?')
|
|
109
|
+
.get(...this.scopeArgs(), entry.cycleId);
|
|
110
|
+
if (existing) {
|
|
111
|
+
if (existing.body !== body)
|
|
112
|
+
throw new Error('This cycle already has a different experience entry; entries are immutable.');
|
|
113
|
+
return this.decode(existing);
|
|
114
|
+
}
|
|
115
|
+
const result = db
|
|
116
|
+
.prepare('INSERT INTO experience (tenant_id, project_id, agent_key, domain, cycle_id, evaluator_revision, decision, body) VALUES (?,?,?,?,?,?,?,?)')
|
|
117
|
+
.run(...this.scopeArgs(), entry.domain, entry.cycleId, entry.evaluatorRevision, entry.decision, body);
|
|
118
|
+
return { ...entry, ordinal: Number(result.lastInsertRowid) };
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/** The ordinal the next entry will be recorded after; pass it as `beforeOrdinal` to read everything so far. */
|
|
122
|
+
async nextOrdinal() {
|
|
123
|
+
return this.use(false, (db) => {
|
|
124
|
+
const row = db
|
|
125
|
+
.prepare('SELECT MAX(ordinal) AS last FROM experience WHERE tenant_id=? AND project_id=? AND agent_key=?')
|
|
126
|
+
.get(...this.scopeArgs());
|
|
127
|
+
return (row.last ?? 0) + 1;
|
|
128
|
+
}, () => 1);
|
|
129
|
+
}
|
|
130
|
+
/** Entries of one domain and evaluator revision recorded before `beforeOrdinal`, newest first. */
|
|
131
|
+
async retrieve(query) {
|
|
132
|
+
const rows = this.use(false, (db) => db
|
|
133
|
+
.prepare('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')
|
|
134
|
+
.all(...this.scopeArgs(), query.domain, query.evaluatorRevision, query.beforeOrdinal), () => []);
|
|
135
|
+
if (!query.leakGuard)
|
|
136
|
+
throw new Error('Retrieving experience entries requires a leakGuard.');
|
|
137
|
+
const selected = selectLearningExperience(rows.map((row) => this.decode(row)), query);
|
|
138
|
+
for (const { ordinal: _ordinal, ...entry } of selected)
|
|
139
|
+
assertExperienceHasNoLeak(entry, query.leakGuard);
|
|
140
|
+
return selected;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=experience-store.js.map
|
|
@@ -0,0 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AAA;IACF,CAAC;IAED,kGAAkG;IAClG,KAAK,CAAC,QAAQ,CAAC,KAA8B;QAC5C,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CACpB,KAAK,EACL,CAAC,EAAE,EAAE,EAAE,CACN,EAAE;aACA,OAAO,CACP,mKAAmK,CACnK;aACA,GAAG,CACH,GAAG,IAAI,CAAC,SAAS,EAAE,EACnB,KAAK,CAAC,MAAM,EACZ,KAAK,CAAC,iBAAiB,EACvB,KAAK,CAAC,aAAa,CACV,EACZ,GAAG,EAAE,CAAC,EAAE,CACR,CAAA;QACD,IAAI,CAAC,KAAK,CAAC,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAA;QAC5F,MAAM,QAAQ,GAAG,wBAAwB,CACxC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EACnC,KAAK,CACL,CAAA;QACD,KAAK,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAE,IAAI,QAAQ;YACrD,yBAAyB,CAAC,KAAK,EAAE,KAAK,CAAC,SAAS,CAAC,CAAA;QAClD,OAAO,QAAQ,CAAA;IAChB,CAAC;CACD"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { type LearningLeakGuard } from './leak-tripwire.js';
|
|
3
|
+
import { type LearningSkillCandidate, learningEvidenceSchema } from './skill.js';
|
|
4
|
+
export declare const LEARNING_EDIT_VERDICTS: readonly ["helped", "hurt", "no observed effect", "confounded"];
|
|
5
|
+
/** @experimental What one edit did, as far as the evidence can separate it from the others. */
|
|
6
|
+
export type LearningEditVerdict = (typeof LEARNING_EDIT_VERDICTS)[number];
|
|
7
|
+
export declare const LEARNING_CONFIDENCE_LEVELS: readonly ["high", "medium", "low"];
|
|
8
|
+
/** @experimental How far a lesson can be trusted. */
|
|
9
|
+
export type LearningConfidence = (typeof LEARNING_CONFIDENCE_LEVELS)[number];
|
|
10
|
+
declare const editSchema: z.ZodObject<{
|
|
11
|
+
/** The part of the harness the edit touched, such as `instruction-skill`. */
|
|
12
|
+
surface: z.ZodString;
|
|
13
|
+
/** What changed, in a sentence. */
|
|
14
|
+
change: z.ZodString;
|
|
15
|
+
verdict: z.ZodEnum<["helped", "hurt", "no observed effect", "confounded"]>;
|
|
16
|
+
/**
|
|
17
|
+
* True only when this edit was tried alone (an ablation), so its verdict is
|
|
18
|
+
* separable even though the candidate changed several components. The store
|
|
19
|
+
* trusts this flag from the caller; it does not check that an ablation ran.
|
|
20
|
+
*/
|
|
21
|
+
isolated: z.ZodOptional<z.ZodBoolean>;
|
|
22
|
+
}, "strip", z.ZodTypeAny, {
|
|
23
|
+
verdict: "helped" | "hurt" | "no observed effect" | "confounded";
|
|
24
|
+
change: string;
|
|
25
|
+
surface: string;
|
|
26
|
+
isolated?: boolean | undefined;
|
|
27
|
+
}, {
|
|
28
|
+
verdict: "helped" | "hurt" | "no observed effect" | "confounded";
|
|
29
|
+
change: string;
|
|
30
|
+
surface: string;
|
|
31
|
+
isolated?: boolean | undefined;
|
|
32
|
+
}>;
|
|
33
|
+
declare const lessonSchema: z.ZodObject<{
|
|
34
|
+
/** The shape of the situation the lesson applies to, not the incident itself. */
|
|
35
|
+
cue: z.ZodString;
|
|
36
|
+
lesson: z.ZodString;
|
|
37
|
+
/** References into the observations, session log or verification receipts. */
|
|
38
|
+
evidence: z.ZodArray<z.ZodObject<{
|
|
39
|
+
key: z.ZodString;
|
|
40
|
+
source: z.ZodString;
|
|
41
|
+
reason: z.ZodString;
|
|
42
|
+
}, "strip", z.ZodTypeAny, {
|
|
43
|
+
key: string;
|
|
44
|
+
reason: string;
|
|
45
|
+
source: string;
|
|
46
|
+
}, {
|
|
47
|
+
key: string;
|
|
48
|
+
reason: string;
|
|
49
|
+
source: string;
|
|
50
|
+
}>, "many">;
|
|
51
|
+
confidence: z.ZodEnum<["high", "medium", "low"]>;
|
|
52
|
+
confidenceReason: z.ZodString;
|
|
53
|
+
/** Indexes into the entry's edits that the lesson rests on. */
|
|
54
|
+
edits: z.ZodOptional<z.ZodArray<z.ZodNumber, "many">>;
|
|
55
|
+
}, "strip", z.ZodTypeAny, {
|
|
56
|
+
confidence: "low" | "medium" | "high";
|
|
57
|
+
evidence: {
|
|
58
|
+
key: string;
|
|
59
|
+
reason: string;
|
|
60
|
+
source: string;
|
|
61
|
+
}[];
|
|
62
|
+
cue: string;
|
|
63
|
+
lesson: string;
|
|
64
|
+
confidenceReason: string;
|
|
65
|
+
edits?: number[] | undefined;
|
|
66
|
+
}, {
|
|
67
|
+
confidence: "low" | "medium" | "high";
|
|
68
|
+
evidence: {
|
|
69
|
+
key: string;
|
|
70
|
+
reason: string;
|
|
71
|
+
source: string;
|
|
72
|
+
}[];
|
|
73
|
+
cue: string;
|
|
74
|
+
lesson: string;
|
|
75
|
+
confidenceReason: string;
|
|
76
|
+
edits?: number[] | undefined;
|
|
77
|
+
}>;
|
|
78
|
+
/** @experimental One edit row of the typed ledger. */
|
|
79
|
+
export type LearningEditRow = Readonly<z.infer<typeof editSchema>>;
|
|
80
|
+
/** @experimental A lesson, with the confidence the evidence supports. */
|
|
81
|
+
export type LearningLesson = Readonly<Omit<z.infer<typeof lessonSchema>, 'evidence' | 'edits'> & {
|
|
82
|
+
readonly evidence: readonly Readonly<z.infer<typeof learningEvidenceSchema>>[];
|
|
83
|
+
readonly edits?: readonly number[];
|
|
84
|
+
}>;
|
|
85
|
+
/** What a host passes when it records one finished cycle. */
|
|
86
|
+
export interface LearningExperienceInput {
|
|
87
|
+
/** The kind of task the cycle was about; retrieval never crosses domains. */
|
|
88
|
+
readonly domain: string;
|
|
89
|
+
/** The cycle that produced the entry; one entry per cycle. */
|
|
90
|
+
readonly cycleId: string;
|
|
91
|
+
/** The evaluator that judged the candidate. Lessons are only comparable under the same revision. */
|
|
92
|
+
readonly evaluatorRevision: string;
|
|
93
|
+
readonly decision: 'adopted' | 'rejected';
|
|
94
|
+
/** Why, as the verification recorded it. */
|
|
95
|
+
readonly reason: string;
|
|
96
|
+
/** The whole artefact, as proposed. */
|
|
97
|
+
readonly candidate: LearningSkillCandidate;
|
|
98
|
+
readonly edits: readonly LearningEditRow[];
|
|
99
|
+
readonly lessons?: readonly LearningLesson[];
|
|
100
|
+
/**
|
|
101
|
+
* Evaluator-side material to refuse in anything the entry would keep. Required:
|
|
102
|
+
* pass `{}` to apply only the built-in markers and secret shapes.
|
|
103
|
+
*/
|
|
104
|
+
readonly leakGuard: LearningLeakGuard;
|
|
105
|
+
}
|
|
106
|
+
/** @experimental A stored entry. `ordinal` is its position in recording order. */
|
|
107
|
+
export interface LearningExperienceEntry {
|
|
108
|
+
readonly ordinal: number;
|
|
109
|
+
readonly domain: string;
|
|
110
|
+
readonly cycleId: string;
|
|
111
|
+
readonly evaluatorRevision: string;
|
|
112
|
+
readonly decision: 'adopted' | 'rejected';
|
|
113
|
+
readonly reason: string;
|
|
114
|
+
/** The full artefact as proposed, unabridged. */
|
|
115
|
+
readonly candidate: LearningSkillCandidate;
|
|
116
|
+
readonly candidateRevision: string;
|
|
117
|
+
readonly edits: readonly LearningEditRow[];
|
|
118
|
+
readonly lessons: readonly LearningLesson[];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Apply the rules that keep a record honest. Several components in one
|
|
122
|
+
* candidate share a single outcome, so unless a row was tried alone its
|
|
123
|
+
* verdict becomes `confounded`. A lesson that rests on a confounded edit is
|
|
124
|
+
* capped at medium; one that rests only on confounded edits is low, and its
|
|
125
|
+
* reason says why.
|
|
126
|
+
*/
|
|
127
|
+
export declare function normalizeLearningExperience(input: LearningExperienceInput): Omit<LearningExperienceEntry, 'ordinal'>;
|
|
128
|
+
/**
|
|
129
|
+
* Refuse an entry that would keep evaluator-side material. Every string the
|
|
130
|
+
* entry holds is scanned, found by walking the entry rather than by naming
|
|
131
|
+
* fields; source keys are also tried as paths. Throws {@link LearningLeakError}.
|
|
132
|
+
*/
|
|
133
|
+
export declare function assertExperienceHasNoLeak(entry: Omit<LearningExperienceEntry, 'ordinal'>, guard: LearningLeakGuard | undefined): void;
|
|
134
|
+
/** @experimental Where retrieval may look. */
|
|
135
|
+
export interface LearningExperienceQuery {
|
|
136
|
+
readonly domain: string;
|
|
137
|
+
/** The evaluator the reader is judged by; entries from another revision are not comparable. */
|
|
138
|
+
readonly evaluatorRevision: string;
|
|
139
|
+
/** Evaluator-side material; entries are scanned again before they are returned. */
|
|
140
|
+
readonly leakGuard: LearningLeakGuard;
|
|
141
|
+
/** Only entries recorded before this ordinal. The reader's own entry is never among them. */
|
|
142
|
+
readonly beforeOrdinal: number;
|
|
143
|
+
readonly decision?: 'adopted' | 'rejected';
|
|
144
|
+
/** Newest first; default 20. */
|
|
145
|
+
readonly limit?: number;
|
|
146
|
+
}
|
|
147
|
+
/** The retrieval rule, shared by every store: same domain, same evaluator, strictly earlier. */
|
|
148
|
+
export declare function selectLearningExperience(entries: readonly LearningExperienceEntry[], query: LearningExperienceQuery): LearningExperienceEntry[];
|
|
149
|
+
export {};
|
|
150
|
+
//# sourceMappingURL=experience.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"experience.d.ts","sourceRoot":"","sources":["../../src/learning/experience.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,KAAK,iBAAiB,EAAwB,MAAM,oBAAoB,CAAA;AACjF,OAAO,EACN,KAAK,sBAAsB,EAE3B,sBAAsB,EAEtB,MAAM,YAAY,CAAA;AAgBnB,eAAO,MAAM,sBAAsB,iEAKzB,CAAA;AACV,+FAA+F;AAC/F,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAA;AAEzE,eAAO,MAAM,0BAA0B,oCAAqC,CAAA;AAC5E,qDAAqD;AACrD,MAAM,MAAM,kBAAkB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AA;IAC1C,gCAAgC;IAChC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CACvB;AAED,gGAAgG;AAChG,wBAAgB,wBAAwB,CACvC,OAAO,EAAE,SAAS,uBAAuB,EAAE,EAC3C,KAAK,EAAE,uBAAuB,GAC5B,uBAAuB,EAAE,CAY3B"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { assertNoLearningLeak } from './leak-tripwire.js';
|
|
3
|
+
import { hashLearningSkill, learningEvidenceSchema, normalizeLearningSkill, } from './skill.js';
|
|
4
|
+
/**
|
|
5
|
+
* The experience library: what an earlier cycle tried and what came of it,
|
|
6
|
+
* kept beside the observations so a later generator can read it. Three rules
|
|
7
|
+
* hold throughout. An entry stores the adopted or rejected artefact whole,
|
|
8
|
+
* never a summary of it. Retrieval is limited to one domain and to entries
|
|
9
|
+
* recorded before a stated point. And a verdict never claims more than the
|
|
10
|
+
* evidence can separate: when a candidate changed several components at once,
|
|
11
|
+
* the result belongs to the whole change, so each row says `confounded`.
|
|
12
|
+
*/
|
|
13
|
+
const label = z.string().trim().min(1).max(256);
|
|
14
|
+
const text = z.string().trim().min(1).max(2_000);
|
|
15
|
+
const domainLabel = z.string().regex(/^[a-z0-9][a-z0-9_.-]{0,63}$/);
|
|
16
|
+
export const LEARNING_EDIT_VERDICTS = [
|
|
17
|
+
'helped',
|
|
18
|
+
'hurt',
|
|
19
|
+
'no observed effect',
|
|
20
|
+
'confounded',
|
|
21
|
+
];
|
|
22
|
+
export const LEARNING_CONFIDENCE_LEVELS = ['high', 'medium', 'low'];
|
|
23
|
+
const editSchema = z.object({
|
|
24
|
+
/** The part of the harness the edit touched, such as `instruction-skill`. */
|
|
25
|
+
surface: label,
|
|
26
|
+
/** What changed, in a sentence. */
|
|
27
|
+
change: text,
|
|
28
|
+
verdict: z.enum(LEARNING_EDIT_VERDICTS),
|
|
29
|
+
/**
|
|
30
|
+
* True only when this edit was tried alone (an ablation), so its verdict is
|
|
31
|
+
* separable even though the candidate changed several components. The store
|
|
32
|
+
* trusts this flag from the caller; it does not check that an ablation ran.
|
|
33
|
+
*/
|
|
34
|
+
isolated: z.boolean().optional(),
|
|
35
|
+
});
|
|
36
|
+
const lessonSchema = z.object({
|
|
37
|
+
/** The shape of the situation the lesson applies to, not the incident itself. */
|
|
38
|
+
cue: text,
|
|
39
|
+
lesson: text,
|
|
40
|
+
/** References into the observations, session log or verification receipts. */
|
|
41
|
+
evidence: z.array(learningEvidenceSchema).min(1).max(16),
|
|
42
|
+
confidence: z.enum(LEARNING_CONFIDENCE_LEVELS),
|
|
43
|
+
confidenceReason: text,
|
|
44
|
+
/** Indexes into the entry's edits that the lesson rests on. */
|
|
45
|
+
edits: z.array(z.number().int().min(0).max(63)).max(64).optional(),
|
|
46
|
+
});
|
|
47
|
+
const inputSchema = z.object({
|
|
48
|
+
domain: domainLabel,
|
|
49
|
+
cycleId: z.string().uuid(),
|
|
50
|
+
evaluatorRevision: label,
|
|
51
|
+
decision: z.enum(['adopted', 'rejected']),
|
|
52
|
+
reason: text,
|
|
53
|
+
edits: z.array(editSchema).min(1).max(64),
|
|
54
|
+
lessons: z.array(lessonSchema).max(32).optional(),
|
|
55
|
+
});
|
|
56
|
+
const CAP = { low: 0, medium: 1, high: 2 };
|
|
57
|
+
const BY_RANK = ['low', 'medium', 'high'];
|
|
58
|
+
/**
|
|
59
|
+
* Apply the rules that keep a record honest. Several components in one
|
|
60
|
+
* candidate share a single outcome, so unless a row was tried alone its
|
|
61
|
+
* verdict becomes `confounded`. A lesson that rests on a confounded edit is
|
|
62
|
+
* capped at medium; one that rests only on confounded edits is low, and its
|
|
63
|
+
* reason says why.
|
|
64
|
+
*/
|
|
65
|
+
export function normalizeLearningExperience(input) {
|
|
66
|
+
const { leakGuard: _guard, candidate: rawCandidate, ...rest } = input;
|
|
67
|
+
const parsed = inputSchema.parse(rest);
|
|
68
|
+
const candidate = normalizeLearningSkill(rawCandidate);
|
|
69
|
+
// More than one edit row changed together, whatever its component label.
|
|
70
|
+
const several = parsed.edits.length > 1;
|
|
71
|
+
const edits = parsed.edits.map((edit) => {
|
|
72
|
+
const { isolated, ...row } = edit;
|
|
73
|
+
if (several && isolated !== true)
|
|
74
|
+
return { ...row, verdict: 'confounded' };
|
|
75
|
+
return isolated === true ? { ...row, isolated } : row;
|
|
76
|
+
});
|
|
77
|
+
const lessons = (parsed.lessons ?? []).map((lesson) => {
|
|
78
|
+
// A lesson that cites no edits rests on all of them.
|
|
79
|
+
const refs = lesson.edits?.length ? lesson.edits : edits.map((_, index) => index);
|
|
80
|
+
for (const index of refs)
|
|
81
|
+
if (index >= edits.length)
|
|
82
|
+
throw new Error(`Lesson refers to edit ${index}, which is absent.`);
|
|
83
|
+
const confounded = refs.filter((index) => edits[index]?.verdict === 'confounded').length;
|
|
84
|
+
if (confounded === 0)
|
|
85
|
+
return lesson;
|
|
86
|
+
const all = confounded === refs.length;
|
|
87
|
+
const cap = all ? 'low' : 'medium';
|
|
88
|
+
if (CAP[lesson.confidence] <= CAP[cap]) {
|
|
89
|
+
return {
|
|
90
|
+
...lesson,
|
|
91
|
+
confidenceReason: `${lesson.confidenceReason} (Stands on confounded edits.)`,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
...lesson,
|
|
96
|
+
confidence: BY_RANK[CAP[cap]],
|
|
97
|
+
confidenceReason: `${lesson.confidenceReason} (Narrowed to ${cap}: ${all ? 'every' : 'some'} edit it rests on changed together with others.)`,
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
domain: parsed.domain,
|
|
102
|
+
cycleId: parsed.cycleId.toLowerCase(),
|
|
103
|
+
evaluatorRevision: parsed.evaluatorRevision,
|
|
104
|
+
decision: parsed.decision,
|
|
105
|
+
reason: parsed.reason,
|
|
106
|
+
candidate,
|
|
107
|
+
candidateRevision: hashLearningSkill(candidate),
|
|
108
|
+
edits,
|
|
109
|
+
lessons,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function collectStrings(value, subject, out) {
|
|
113
|
+
if (typeof value === 'string')
|
|
114
|
+
out.push({ subject, text: value });
|
|
115
|
+
else if (Array.isArray(value))
|
|
116
|
+
value.forEach((v, i) => collectStrings(v, `${subject}[${i}]`, out));
|
|
117
|
+
else if (value && typeof value === 'object')
|
|
118
|
+
for (const [key, v] of Object.entries(value))
|
|
119
|
+
collectStrings(v, `${subject}.${key}`, out);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Refuse an entry that would keep evaluator-side material. Every string the
|
|
123
|
+
* entry holds is scanned, found by walking the entry rather than by naming
|
|
124
|
+
* fields; source keys are also tried as paths. Throws {@link LearningLeakError}.
|
|
125
|
+
*/
|
|
126
|
+
export function assertExperienceHasNoLeak(entry, guard) {
|
|
127
|
+
const subjects = [];
|
|
128
|
+
const texts = [];
|
|
129
|
+
collectStrings(entry, 'experience', texts);
|
|
130
|
+
subjects.push(...texts);
|
|
131
|
+
for (const [i, source] of (entry.candidate.sources ?? []).entries())
|
|
132
|
+
subjects.push({ subject: `experience.candidate.sources[${i}].path`, paths: [source.key] });
|
|
133
|
+
assertNoLearningLeak(guard, subjects);
|
|
134
|
+
}
|
|
135
|
+
/** The retrieval rule, shared by every store: same domain, same evaluator, strictly earlier. */
|
|
136
|
+
export function selectLearningExperience(entries, query) {
|
|
137
|
+
const limit = Math.max(1, Math.min(query.limit ?? 20, 200));
|
|
138
|
+
return entries
|
|
139
|
+
.filter((entry) => entry.domain === query.domain &&
|
|
140
|
+
entry.evaluatorRevision === query.evaluatorRevision &&
|
|
141
|
+
entry.ordinal < query.beforeOrdinal &&
|
|
142
|
+
(query.decision === undefined || entry.decision === query.decision))
|
|
143
|
+
.sort((a, b) => b.ordinal - a.ordinal)
|
|
144
|
+
.slice(0, limit);
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=experience.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version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AAK,EAAE,EAAE,CACT,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM;QAC7B,KAAK,CAAC,iBAAiB,KAAK,KAAK,CAAC,iBAAiB;QACnD,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,aAAa;QACnC,CAAC,KAAK,CAAC,QAAQ,KAAK,SAAS,IAAI,KAAK,CAAC,QAAQ,KAAK,KAAK,CAAC,QAAQ,CAAC,CACpE;SACA,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;SACrC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAA;AAClB,CAAC"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { LearningCycleResult } from './cycle.js';
|
|
2
|
+
import type { LearningReceipt } from './cycle.js';
|
|
3
|
+
import type { LearningExperienceEntry, LearningExperienceInput } from './experience.js';
|
|
4
|
+
import { type LearningLeakGuard } from './leak-tripwire.js';
|
|
5
|
+
import type { LearningObservationRecord } from './observation.js';
|
|
6
|
+
import { type LearningSkillCandidate } from './skill.js';
|
|
7
|
+
/**
|
|
8
|
+
* The built-in generator, first version: one step, one surface. It reads what
|
|
9
|
+
* conversations kept and what earlier cycles learned in the same domain, asks a
|
|
10
|
+
* model for one revision of an instruction skill, and checks the answer against
|
|
11
|
+
* the editable surface before anything else sees it. It never judges its own
|
|
12
|
+
* work: the candidate goes to the learning cycle's paired verification.
|
|
13
|
+
*/
|
|
14
|
+
/** The parts of the harness a candidate may edit. Only instruction text today. */
|
|
15
|
+
export declare const LEARNING_SURFACE_KINDS: readonly ["instruction-skill"];
|
|
16
|
+
/** @experimental A part of the harness a generator may edit. */
|
|
17
|
+
export type LearningSurfaceKind = (typeof LEARNING_SURFACE_KINDS)[number];
|
|
18
|
+
/**
|
|
19
|
+
* The safety floor. These are never editable by a learning cycle, whatever a
|
|
20
|
+
* configuration says: a candidate cannot be admitted that changes how
|
|
21
|
+
* permissions are reviewed, which paths are protected, which command patterns
|
|
22
|
+
* are always refused, or the learning code itself.
|
|
23
|
+
*/
|
|
24
|
+
export declare const LEARNING_FLOOR_COMPONENTS: readonly ["permission-review", "protected-paths", "dangerous-patterns", "learning-code"];
|
|
25
|
+
/** @experimental The allowlist of what a generator may propose to change. */
|
|
26
|
+
export interface LearningSurface {
|
|
27
|
+
readonly kinds: readonly LearningSurfaceKind[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Validate a surface configuration. Unknown kinds and every safety-floor
|
|
31
|
+
* component are refused, so a misconfiguration fails loudly instead of widening
|
|
32
|
+
* what a generator may touch. Omitted means the default surface.
|
|
33
|
+
*/
|
|
34
|
+
export declare function resolveLearningSurface(config?: {
|
|
35
|
+
readonly kinds?: readonly string[];
|
|
36
|
+
}): LearningSurface;
|
|
37
|
+
/** One call to the model the host configured. */
|
|
38
|
+
export interface LearningModelRequest {
|
|
39
|
+
readonly system: string;
|
|
40
|
+
readonly prompt: string;
|
|
41
|
+
readonly maxTokens: number;
|
|
42
|
+
readonly signal: AbortSignal;
|
|
43
|
+
}
|
|
44
|
+
/** What the model said and what it cost. A null count means the host could not measure it. */
|
|
45
|
+
export interface LearningModelReply {
|
|
46
|
+
readonly text: string;
|
|
47
|
+
readonly tokens: number | null;
|
|
48
|
+
readonly costUsd: number | null;
|
|
49
|
+
}
|
|
50
|
+
/** @experimental The model seam. The host chooses the model and counts the spend. */
|
|
51
|
+
export type LearningModel = (request: LearningModelRequest) => Promise<LearningModelReply>;
|
|
52
|
+
/** One change a proposal makes, as the typed edit ledger records it. */
|
|
53
|
+
export interface LearningProposedEdit {
|
|
54
|
+
readonly surface: LearningSurfaceKind;
|
|
55
|
+
readonly change: string;
|
|
56
|
+
}
|
|
57
|
+
export interface LearningProposeOptions {
|
|
58
|
+
/** The skill this proposal revises; the candidate keeps exactly this name. */
|
|
59
|
+
readonly skillName: string;
|
|
60
|
+
/** The active revision, or null when none exists. */
|
|
61
|
+
readonly baseline: LearningSkillCandidate | null;
|
|
62
|
+
/** The domain's observations, newest last. Only fixed-wording records, never copied text. */
|
|
63
|
+
readonly observations: readonly LearningObservationRecord[];
|
|
64
|
+
/** Earlier cycles of the same domain and evaluator, already retrieved. */
|
|
65
|
+
readonly experience?: readonly LearningExperienceEntry[];
|
|
66
|
+
readonly surface?: LearningSurface;
|
|
67
|
+
/** Evaluator-side material to keep out of the prompt and out of the candidate. */
|
|
68
|
+
readonly leakGuard: LearningLeakGuard;
|
|
69
|
+
readonly model: LearningModel;
|
|
70
|
+
readonly signal: AbortSignal;
|
|
71
|
+
/** Called once per model call so the cycle can count the spend. */
|
|
72
|
+
readonly recordUsage?: (receipt: LearningReceipt) => Promise<void>;
|
|
73
|
+
/** The session the receipts belong to. */
|
|
74
|
+
readonly sessionId?: string;
|
|
75
|
+
/** Per call output bound; default 1,500 tokens. */
|
|
76
|
+
readonly maxTokens?: number;
|
|
77
|
+
}
|
|
78
|
+
/** @experimental The result of one proposal. */
|
|
79
|
+
export type LearningProposal = {
|
|
80
|
+
readonly status: 'proposed';
|
|
81
|
+
readonly candidate: LearningSkillCandidate;
|
|
82
|
+
readonly edits: readonly LearningProposedEdit[];
|
|
83
|
+
readonly attempts: number;
|
|
84
|
+
/** Every model call reported a token count. */
|
|
85
|
+
readonly usageComplete: boolean;
|
|
86
|
+
} | {
|
|
87
|
+
readonly status: 'refused';
|
|
88
|
+
readonly reason: string;
|
|
89
|
+
/** What the last answer tried to touch outside the surface, by name; never its text. */
|
|
90
|
+
readonly offSurface: readonly string[];
|
|
91
|
+
readonly attempts: number;
|
|
92
|
+
readonly usageComplete: boolean;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Ask the model for one revision of the skill and admit it only when every
|
|
96
|
+
* edit sits on the surface and the text stays clear of the safety floor. A
|
|
97
|
+
* problem is reported back to the model once with the part that failed; a
|
|
98
|
+
* second failure refuses the proposal. The model is never asked to judge its
|
|
99
|
+
* own result.
|
|
100
|
+
*/
|
|
101
|
+
export declare function proposeLearningCandidate(options: LearningProposeOptions): Promise<LearningProposal>;
|
|
102
|
+
/**
|
|
103
|
+
* The experience entry a finished cycle leaves behind. An activated cycle is
|
|
104
|
+
* `adopted` and its edit `helped`; a rejected one is `rejected`, `hurt` when
|
|
105
|
+
* the review found a regression and `no observed effect` otherwise. A cycle
|
|
106
|
+
* that did not reach a verdict (inconclusive, failed, cancelled) leaves
|
|
107
|
+
* nothing, because there is nothing true to record about its edits.
|
|
108
|
+
*/
|
|
109
|
+
export declare function learningExperienceFromCycle(input: {
|
|
110
|
+
readonly domain: string;
|
|
111
|
+
readonly evaluatorRevision: string;
|
|
112
|
+
readonly proposal: Extract<LearningProposal, {
|
|
113
|
+
status: 'proposed';
|
|
114
|
+
}>;
|
|
115
|
+
readonly result: LearningCycleResult;
|
|
116
|
+
readonly leakGuard: LearningLeakGuard;
|
|
117
|
+
}): LearningExperienceInput | null;
|
|
118
|
+
//# sourceMappingURL=generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/learning/generator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AACjD,OAAO,KAAK,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAA;AACvF,OAAO,EACN,KAAK,iBAAiB,EAGtB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAA;AACjE,OAAO,EAAE,KAAK,sBAAsB,EAA0B,MAAM,YAAY,CAAA;AAEhF;;;;;;GAMG;AAEH,kFAAkF;AAClF,eAAO,MAAM,sBAAsB,gCAAiC,CAAA;AACpE,gEAAgE;AAChE,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,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