@adhd/agent-engine-compiler 2.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.
Files changed (62) hide show
  1. package/drizzle/meta/_journal.json +5 -0
  2. package/package.json +58 -0
  3. package/src/cache/composed-prompt-cache.d.ts +60 -0
  4. package/src/cache/composed-prompt-cache.d.ts.map +1 -0
  5. package/src/cache/composed-prompt-cache.js +120 -0
  6. package/src/cache/composed-prompt-cache.js.map +1 -0
  7. package/src/cli/compile.d.ts +3 -0
  8. package/src/cli/compile.d.ts.map +1 -0
  9. package/src/cli/compile.js +294 -0
  10. package/src/cli/compile.js.map +1 -0
  11. package/src/compile.d.ts +61 -0
  12. package/src/compile.d.ts.map +1 -0
  13. package/src/compile.js +279 -0
  14. package/src/compile.js.map +1 -0
  15. package/src/db/client.d.ts +7 -0
  16. package/src/db/client.d.ts.map +1 -0
  17. package/src/db/client.js +31 -0
  18. package/src/db/client.js.map +1 -0
  19. package/src/db/migrate-runner.d.ts +31 -0
  20. package/src/db/migrate-runner.d.ts.map +1 -0
  21. package/src/db/migrate-runner.js +35 -0
  22. package/src/db/migrate-runner.js.map +1 -0
  23. package/src/db/migrate.d.ts +9 -0
  24. package/src/db/migrate.d.ts.map +1 -0
  25. package/src/db/migrate.js +13 -0
  26. package/src/db/migrate.js.map +1 -0
  27. package/src/db/schema.d.ts +3 -0
  28. package/src/db/schema.d.ts.map +1 -0
  29. package/src/db/schema.js +30 -0
  30. package/src/db/schema.js.map +1 -0
  31. package/src/emit/json.d.ts +50 -0
  32. package/src/emit/json.d.ts.map +1 -0
  33. package/src/emit/json.js +66 -0
  34. package/src/emit/json.js.map +1 -0
  35. package/src/emit/markdown.d.ts +35 -0
  36. package/src/emit/markdown.d.ts.map +1 -0
  37. package/src/emit/markdown.js +90 -0
  38. package/src/emit/markdown.js.map +1 -0
  39. package/src/index.d.ts +20 -0
  40. package/src/index.d.ts.map +1 -0
  41. package/src/index.js +23 -0
  42. package/src/index.js.map +1 -0
  43. package/src/resolve/composition.d.ts +28 -0
  44. package/src/resolve/composition.d.ts.map +1 -0
  45. package/src/resolve/composition.js +33 -0
  46. package/src/resolve/composition.js.map +1 -0
  47. package/src/resolve/model.d.ts +22 -0
  48. package/src/resolve/model.d.ts.map +1 -0
  49. package/src/resolve/model.js +69 -0
  50. package/src/resolve/model.js.map +1 -0
  51. package/src/resolve/policy.d.ts +34 -0
  52. package/src/resolve/policy.d.ts.map +1 -0
  53. package/src/resolve/policy.js +44 -0
  54. package/src/resolve/policy.js.map +1 -0
  55. package/src/resolve/tools.d.ts +33 -0
  56. package/src/resolve/tools.d.ts.map +1 -0
  57. package/src/resolve/tools.js +62 -0
  58. package/src/resolve/tools.js.map +1 -0
  59. package/src/seed/fixtures.d.ts +52 -0
  60. package/src/seed/fixtures.d.ts.map +1 -0
  61. package/src/seed/fixtures.js +148 -0
  62. package/src/seed/fixtures.js.map +1 -0
package/src/compile.js ADDED
@@ -0,0 +1,279 @@
1
+ // ──────────────────────────────────────────────
2
+ // compileAgent — public orchestrator.
3
+ //
4
+ // Turns an agent definition + runtime context into a flat, platform-shaped
5
+ // artifact by composing the four resolve layers and dispatching to the
6
+ // platform's header builder.
7
+ //
8
+ // Decision B (decisions.md) defines three header builders keyed on a
9
+ // platform's `header_format` column:
10
+ //
11
+ // yaml_frontmatter → emit/markdown.ts (claude_code)
12
+ // json_object → emit/json.ts (claude_api / openai / bedrock)
13
+ // none → body text only (cursor / vscode)
14
+ //
15
+ // The builder is selected by DATA (header_format), never by a hard-coded
16
+ // platform name check. Adding a platform that shares an existing
17
+ // header_format requires no code change here.
18
+ //
19
+ // \n\n section separator advisory (carry-forward, decisions.md Decision B.1):
20
+ // resolve/composition.ts:72 joins body sections with a single '\n'.
21
+ // composition.ts is read-only. To produce the '\n\n'-separated body
22
+ // mandated by Decision B.1, this function calls CompositionStore.resolveComposition
23
+ // directly and owns the join, bypassing resolveBody's pre-joined string.
24
+ // This is the emit-layer fix authorised by the advisory — no composition.ts
25
+ // edit required.
26
+ //
27
+ // [def:compile-input] — {agentSlug, platform, context, db}
28
+ // [def:composed-output] — {id, content, tools, componentVersions}
29
+ // [inv:one-db-handle] — ONE shared SQLite handle, all four table prefixes.
30
+ // [inv:real-rows-not-mocks] — the caller seeds real rows; this function
31
+ // reads them through the upstream stores.
32
+ // ──────────────────────────────────────────────
33
+ import { AgentStore, CompositionStore } from '@adhd/agent-store-prompts';
34
+ import { ToolStore, BindingStore } from '@adhd/agent-store-tools';
35
+ import { ToolFormatStore, emitToolsForProvider } from '@adhd/agent-core-provider';
36
+ import { resolveTools } from './resolve/tools.js';
37
+ import { resolveModel } from './resolve/model.js';
38
+ import { resolvePolicyConstraints } from './resolve/policy.js';
39
+ import { emitYamlFrontmatter } from './emit/markdown.js';
40
+ import { emitJsonObject } from './emit/json.js';
41
+ import { lookup as cacheL, write as cacheW, } from './cache/composed-prompt-cache.js';
42
+ // ──────────────────────────────────────────────
43
+ // Internal helpers
44
+ // ──────────────────────────────────────────────
45
+ /**
46
+ * Look up the `header_format` for a platform from `tool_platform_bindings.platforms`.
47
+ * Returns the platform's `headerFormat` column, or 'none' if no row is found.
48
+ */
49
+ function getPlatformHeaderFormat(
50
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
+ db, platform) {
52
+ const bindingStore = new BindingStore(db);
53
+ // BindingStore.readPlatform throws PLATFORM_NOT_FOUND when absent — treat
54
+ // an unknown platform as 'none' (body-only compile).
55
+ try {
56
+ const p = bindingStore.readPlatform(platform);
57
+ return p.headerFormat;
58
+ }
59
+ catch {
60
+ // Unknown platform — treat as 'none' (body-only).
61
+ return 'none';
62
+ }
63
+ }
64
+ /**
65
+ * Extract body sections (in junction order) and componentVersions directly from
66
+ * CompositionStore — bypassing resolveBody's '\n'-joined string so we can
67
+ * join sections with '\n\n' as Decision B.1 mandates.
68
+ *
69
+ * This is the emit-layer fix for the advisory noted in decisions.md.
70
+ */
71
+ function extractBodyParts(
72
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
+ db, agentSlug, context) {
74
+ const store = new CompositionStore(
75
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
+ db);
77
+ const resolved = store.resolveComposition(agentSlug, context);
78
+ const componentVersions = {};
79
+ const bodySections = [];
80
+ for (const rc of resolved) {
81
+ bodySections.push(rc.component.content);
82
+ componentVersions[rc.componentSlug] = rc.resolvedVersion;
83
+ }
84
+ return { bodySections, componentVersions };
85
+ }
86
+ // ──────────────────────────────────────────────
87
+ // Public API
88
+ // ──────────────────────────────────────────────
89
+ /**
90
+ * Compile an agent to a flat, platform-shaped artifact.
91
+ *
92
+ * Orchestrates the four resolve layers:
93
+ * 1. body + componentVersions — CompositionStore.resolveComposition (junction order)
94
+ * 2. tools — resolveTools (platform aliases)
95
+ * 3. model — resolveModel (platform binding or canonical fallback)
96
+ * 4. constraints — resolvePolicyConstraints (direct + inherited)
97
+ *
98
+ * Then dispatches on the platform's `header_format` column to the matching
99
+ * emit function. The `id` field is null until `composed-prompt-caching`
100
+ * wires the cache write ([def:composed-output]).
101
+ *
102
+ * @param input - Agent slug, target platform, runtime context, shared DB handle.
103
+ * @returns Compiled platform artifact plus metadata.
104
+ *
105
+ * @throws {AgentError} AGENT_NOT_FOUND — agentSlug not in registry_agents.
106
+ * @throws {CompositionError} REQUIRED_COMPONENT_EXCLUDED — a required component
107
+ * was excluded by the runtime context.
108
+ */
109
+ export function compileAgent(input) {
110
+ const { agentSlug, platform, context = {}, db } = input;
111
+ // ── 1. Agent metadata (description for frontmatter, model_hint for model step) ──
112
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
+ const agentStore = new AgentStore(db);
114
+ const agent = agentStore.read(agentSlug);
115
+ // ── 2. Determine header_format from the platform row ────────────────────
116
+ const headerFormat = getPlatformHeaderFormat(db, platform);
117
+ // ── 3. Body sections in junction order with '\n\n' separator ────────────
118
+ // Direct call to CompositionStore — bypasses resolveBody's '\n' join so
119
+ // we can apply Decision B.1's '\n\n' between sections at the emit layer.
120
+ // NOTE: extractBodyParts is a cheap read; we run it before the cache check
121
+ // so we have `componentVersions` to fold into the combined context_hash
122
+ // (Decision D: the hash covers BOTH context AND resolved version set).
123
+ const { bodySections, componentVersions } = extractBodyParts(db, agentSlug, context);
124
+ // ── 3b. Cache lookup (BEFORE assembly — [dod.4] negative-control) ────────
125
+ // Decision D: lookup by (agentSlug, platform, context, componentVersions).
126
+ // On HIT: return the persisted {id, content} WITHOUT re-running the resolve
127
+ // layers (steps 4-7 are bypassed). Removing this SELECT causes a duplicate
128
+ // row on every compile — that is the [dod.4] negative-control.
129
+ const hit = cacheL(db, agentSlug, platform, context, componentVersions);
130
+ if (hit !== null) {
131
+ // Cache HIT: serve the persisted artifact. `tools` and `componentVersions`
132
+ // are reconstructed from the already-resolved data (they are cheap reads
133
+ // and do not require re-running the full assembly).
134
+ const resolvedToolsForHit = resolveTools(db, agentSlug, platform);
135
+ if (headerFormat === 'yaml_frontmatter') {
136
+ return {
137
+ id: hit.id,
138
+ content: hit.content,
139
+ tools: resolvedToolsForHit.map((t) => t.platformAlias),
140
+ componentVersions: hit.componentVersions,
141
+ };
142
+ }
143
+ if (headerFormat === 'json_object') {
144
+ // For JSON platforms, tools is a structured array; reconstruct from the
145
+ // same resolve path so the return shape is consistent.
146
+ const toolStore = new ToolStore(db);
147
+ const toolFormatStore = new ToolFormatStore(db);
148
+ const lookupFmt = (pid, ct) => toolFormatStore.getShape(pid, ct);
149
+ const platformToProvider = {
150
+ claude_api: 'anthropic',
151
+ openai: 'openai',
152
+ bedrock: 'bedrock',
153
+ };
154
+ const providerId = platformToProvider[platform] ?? platform;
155
+ const toolDefs = resolvedToolsForHit.map((rt) => {
156
+ let description = '';
157
+ try {
158
+ description = toolStore.read(rt.canonicalName).description;
159
+ }
160
+ catch {
161
+ description = rt.canonicalName;
162
+ }
163
+ return {
164
+ name: rt.canonicalName,
165
+ description,
166
+ inputSchema: {},
167
+ };
168
+ });
169
+ const emittedTools = emitToolsForProvider(toolDefs, providerId, lookupFmt);
170
+ return {
171
+ id: hit.id,
172
+ content: hit.content,
173
+ tools: emittedTools,
174
+ componentVersions: hit.componentVersions,
175
+ };
176
+ }
177
+ // none format — no tools
178
+ return {
179
+ id: hit.id,
180
+ content: hit.content,
181
+ tools: [],
182
+ componentVersions: hit.componentVersions,
183
+ };
184
+ }
185
+ // ── 4. Resolve tools (platform aliases) ─────────────────────────────────
186
+ const resolvedTools = resolveTools(db, agentSlug, platform);
187
+ // ── 5. Resolve model ─────────────────────────────────────────────────────
188
+ const model = resolveModel(db, agentSlug, platform);
189
+ // ── 6. Resolve policy constraints ────────────────────────────────────────
190
+ const constraints = resolvePolicyConstraints(db, agentSlug);
191
+ // ── 7. Dispatch on header_format ─────────────────────────────────────────
192
+ // yaml_frontmatter (claude_code) ──────────────────────────────────────────
193
+ if (headerFormat === 'yaml_frontmatter') {
194
+ const content = emitYamlFrontmatter({
195
+ agentSlug,
196
+ description: agent.description,
197
+ tools: resolvedTools,
198
+ model,
199
+ bodySections,
200
+ constraints,
201
+ });
202
+ // Tools return: string[] of platform aliases for claude_code.
203
+ const toolAliases = resolvedTools.map((t) => t.platformAlias);
204
+ // ── 7a. Cache WRITE (MISS path) ────────────────────────────────────────
205
+ const written = cacheW(db, agentSlug, platform, context, componentVersions, content);
206
+ return { id: written.id, content, tools: toolAliases, componentVersions };
207
+ }
208
+ // json_object (claude_api / openai / bedrock) ─────────────────────────────
209
+ if (headerFormat === 'json_object') {
210
+ // Build structured tool array for the API.
211
+ // We construct minimal ToolDefinition objects from the tool catalog so
212
+ // emitToolsForProvider can shape them. inputSchema defaults to {} since
213
+ // the registry doesn't store JSON schemas for tool grants.
214
+ const toolStore = new ToolStore(db);
215
+ const toolFormatStore = new ToolFormatStore(db);
216
+ const lookupFmt = (providerId, canonicalTool) => toolFormatStore.getShape(providerId, canonicalTool);
217
+ // Map the target platform id to the provider id (Decision B.2: tools shaped
218
+ // via provider_tool_formats). claude_api → 'anthropic' is the seed mapping
219
+ // from SEED_DATA.md §5; openai → 'openai'. Unknown platforms default to
220
+ // the platform id itself as the provider id.
221
+ const platformToProvider = {
222
+ claude_api: 'anthropic',
223
+ openai: 'openai',
224
+ bedrock: 'bedrock',
225
+ };
226
+ const providerId = platformToProvider[platform] ?? platform;
227
+ // Build ToolDefinition[] from the resolved (available) tools.
228
+ const toolDefinitions = resolvedTools.map((rt) => {
229
+ let description = '';
230
+ try {
231
+ const catalogTool = toolStore.read(rt.canonicalName);
232
+ description = catalogTool.description;
233
+ }
234
+ catch {
235
+ // No catalog row — use the canonical name as a fallback description.
236
+ description = rt.canonicalName;
237
+ }
238
+ return {
239
+ name: rt.canonicalName,
240
+ description,
241
+ inputSchema: {},
242
+ };
243
+ });
244
+ // emitToolsForProvider shapes each tool per the provider_tool_formats row.
245
+ // Double-cast via unknown: EmittedTool[] (EmittedServerSideTool lacks an
246
+ // index signature) → unknown[] → StructuredTool[]. Both types are
247
+ // compatible at runtime — all EmittedTool shapes are plain objects.
248
+ const emittedTools = emitToolsForProvider(toolDefinitions, providerId, lookupFmt);
249
+ const content = emitJsonObject({
250
+ agentSlug,
251
+ bodySections,
252
+ constraints,
253
+ tools: resolvedTools,
254
+ structuredTools: emittedTools,
255
+ model,
256
+ });
257
+ // ── 7a. Cache WRITE (MISS path) ────────────────────────────────────────
258
+ const written = cacheW(db, agentSlug, platform, context, componentVersions, content);
259
+ return { id: written.id, content, tools: emittedTools, componentVersions };
260
+ }
261
+ // none (cursor / vscode) — body only, no tools declaration ─────────────────
262
+ // Decision B.3: content = flat body + policy block, tools: [].
263
+ const bodyParts = [...bodySections];
264
+ if (constraints.length > 0) {
265
+ const policyLines = ['## Policies', ''];
266
+ for (const c of constraints) {
267
+ const prefix = c.inheritedFrom
268
+ ? `(inherited from ${c.inheritedFrom}) `
269
+ : '';
270
+ policyLines.push(`- ${prefix}${c.text}`);
271
+ }
272
+ bodyParts.push(policyLines.join('\n'));
273
+ }
274
+ const content = bodyParts.join('\n\n') + '\n';
275
+ // ── 7a. Cache WRITE (MISS path) ─────────────────────────────────────────
276
+ const written = cacheW(db, agentSlug, platform, context, componentVersions, content);
277
+ return { id: written.id, content, tools: [], componentVersions };
278
+ }
279
+ //# sourceMappingURL=compile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile.js","sourceRoot":"","sources":["../../../../../packages/agent/agent-engine-compiler/src/compile.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,sCAAsC;AACtC,EAAE;AACF,2EAA2E;AAC3E,uEAAuE;AACvE,6BAA6B;AAC7B,EAAE;AACF,qEAAqE;AACrE,qCAAqC;AACrC,EAAE;AACF,uDAAuD;AACvD,yEAAyE;AACzE,2DAA2D;AAC3D,EAAE;AACF,yEAAyE;AACzE,kEAAkE;AAClE,8CAA8C;AAC9C,EAAE;AACF,8EAA8E;AAC9E,sEAAsE;AACtE,uEAAuE;AACvE,sFAAsF;AACtF,2EAA2E;AAC3E,8EAA8E;AAC9E,mBAAmB;AACnB,EAAE;AACF,4DAA4D;AAC5D,kEAAkE;AAClE,4EAA4E;AAC5E,wEAAwE;AACxE,4CAA4C;AAC5C,iDAAiD;AAIjD,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAEzE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAGlF,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAGhD,OAAO,EACL,MAAM,IAAI,MAAM,EAChB,KAAK,IAAI,MAAM,GACh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,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,CAAC;IAC7E,CAAC;IAED,6EAA6E;IAC7E,+DAA+D;IAC/D,MAAM,SAAS,GAAa,CAAC,GAAG,YAAY,CAAC,CAAC;IAC9C,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3B,MAAM,WAAW,GAAG,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;QACxC,KAAK,MAAM,CAAC,IAAI,WAAW,EAAE,CAAC;YAC5B,MAAM,MAAM,GAAG,CAAC,CAAC,aAAa;gBAC5B,CAAC,CAAC,mBAAmB,CAAC,CAAC,aAAa,IAAI;gBACxC,CAAC,CAAC,EAAE,CAAC;YACP,WAAW,CAAC,IAAI,CAAC,KAAK,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QAC3C,CAAC;QACD,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACzC,CAAC;IACD,MAAM,OAAO,GAAG,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;IAE9C,2EAA2E;IAC3E,MAAM,OAAO,GAAG,MAAM,CACpB,EAAE,EACF,SAAS,EACT,QAAQ,EACR,OAAO,EACP,iBAAiB,EACjB,OAAO,CACR,CAAC;IACF,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,iBAAiB,EAAE,CAAC;AACnE,CAAC"}
@@ -0,0 +1,7 @@
1
+ import Database from 'better-sqlite3';
2
+ import * as schema from './schema.js';
3
+ export declare const sqlite: Database.Database;
4
+ export declare const db: import("drizzle-orm/better-sqlite3").BetterSQLite3Database<typeof schema> & {
5
+ $client: Database.Database;
6
+ };
7
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/db/client.ts"],"names":[],"mappings":"AAGA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAGtC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAwBtC,eAAO,MAAM,MAAM,EAAE,QAAQ,CAAC,QAAqC,CAAC;AASpE,eAAO,MAAM,EAAE;;CAA8B,CAAC"}
@@ -0,0 +1,31 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import Database from 'better-sqlite3';
4
+ import { drizzle } from 'drizzle-orm/better-sqlite3';
5
+ import * as schema from './schema.js';
6
+ // One shared SQLite file for the whole registry family. This package opens ONE
7
+ // handle and queries all four registry prefixes (registry_*, tool_*, provider_*,
8
+ // policy_*) through it — NO ATTACH DATABASE, NO second DB file, NO cross-package
9
+ // SQLite foreign keys. Cross-prefix reads go through the upstream store classes
10
+ // (CompositionStore, BindingStore, ModelStore, AgentPolicyStore) which are
11
+ // constructed with this same `sqlite` / `db` handle in compileAgent({..., db}).
12
+ // See decisions.md Decision C and REGISTRY-PACKAGE-RULES.md §2.
13
+ const databasePath = process.env['REGISTRY_DATABASE_PATH'] ||
14
+ process.env['DATABASE_PATH'] ||
15
+ './data/registry.db';
16
+ const resolvedPath = path.resolve(databasePath);
17
+ const directory = path.dirname(resolvedPath);
18
+ if (!fs.existsSync(directory)) {
19
+ fs.mkdirSync(directory, {
20
+ recursive: true,
21
+ });
22
+ }
23
+ export const sqlite = new Database(resolvedPath);
24
+ sqlite.pragma('journal_mode = WAL');
25
+ sqlite.pragma('foreign_keys = ON');
26
+ // The compiler's Drizzle instance covers the compiler_ prefix tables only.
27
+ // Upstream schemas are accessed through the upstream packages' store classes
28
+ // (passed the same `db` / `sqlite` handle), not merged here. This keeps each
29
+ // package's schema namespace isolated while sharing one physical connection.
30
+ export const db = drizzle(sqlite, { schema });
31
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/db/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAErD,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAEtC,+EAA+E;AAC/E,iFAAiF;AACjF,iFAAiF;AACjF,gFAAgF;AAChF,2EAA2E;AAC3E,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,YAAY,GAChB,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC;IACrC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC;IAC5B,oBAAoB,CAAC;AAEvB,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;AAEhD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;AAE7C,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;IAC9B,EAAE,CAAC,SAAS,CAAC,SAAS,EAAE;QACtB,SAAS,EAAE,IAAI;KAChB,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,MAAM,MAAM,GAAsB,IAAI,QAAQ,CAAC,YAAY,CAAC,CAAC;AAEpE,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACpC,MAAM,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAEnC,2EAA2E;AAC3E,6EAA6E;AAC7E,6EAA6E;AAC7E,6EAA6E;AAC7E,MAAM,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
2
+ /**
3
+ * Folder holding the drizzle-kit generated migrations (../../drizzle relative
4
+ * to this compiled module). Kept here — with no `client.js` import — so test
5
+ * harnesses can reuse the FK-safe runner without dragging in the production
6
+ * singleton connection (which opens a DB file at import time).
7
+ */
8
+ export declare const MIGRATIONS_FOLDER: string;
9
+ /**
10
+ * Minimal structural type for the bits of a better-sqlite3 connection we need.
11
+ * Avoids importing the better-sqlite3 type just to call `pragma`.
12
+ */
13
+ interface PragmaConn {
14
+ pragma(source: string, options?: {
15
+ simple: boolean;
16
+ }): unknown;
17
+ }
18
+ /**
19
+ * Run Drizzle migrations against an explicit connection with foreign-key
20
+ * enforcement disabled for the duration of the run.
21
+ *
22
+ * WHY: SQLite ignores `PRAGMA foreign_keys` inside a transaction, and drizzle's
23
+ * migrator wraps each migration file in one. Table-recreate migrations (ALTER
24
+ * TABLE via CREATE-INSERT-DROP-RENAME) fire ON DELETE CASCADE when FK enforcement
25
+ * is ON, wiping child rows. Disabling on the connection *before* migrate() —
26
+ * while no transaction is open — is the only way to make it hold for the whole
27
+ * run; we restore the prior setting afterwards.
28
+ */
29
+ export declare function runMigrationsOn(conn: PragmaConn, drizzleDb: Parameters<typeof migrate>[0], migrationsFolder?: string): void;
30
+ export {};
31
+ //# sourceMappingURL=migrate-runner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate-runner.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/db/migrate-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qCAAqC,CAAC;AAE9D;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,QACnB,CAAC;AAEZ;;;GAGG;AACH,UAAU,UAAU;IAClB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,MAAM,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC;CAChE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,UAAU,CAAC,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC,EACxC,gBAAgB,GAAE,MAA0B,GAC3C,IAAI,CAcN"}
@@ -0,0 +1,35 @@
1
+ import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
2
+ /**
3
+ * Folder holding the drizzle-kit generated migrations (../../drizzle relative
4
+ * to this compiled module). Kept here — with no `client.js` import — so test
5
+ * harnesses can reuse the FK-safe runner without dragging in the production
6
+ * singleton connection (which opens a DB file at import time).
7
+ */
8
+ export const MIGRATIONS_FOLDER = new URL('../../drizzle', import.meta.url)
9
+ .pathname;
10
+ /**
11
+ * Run Drizzle migrations against an explicit connection with foreign-key
12
+ * enforcement disabled for the duration of the run.
13
+ *
14
+ * WHY: SQLite ignores `PRAGMA foreign_keys` inside a transaction, and drizzle's
15
+ * migrator wraps each migration file in one. Table-recreate migrations (ALTER
16
+ * TABLE via CREATE-INSERT-DROP-RENAME) fire ON DELETE CASCADE when FK enforcement
17
+ * is ON, wiping child rows. Disabling on the connection *before* migrate() —
18
+ * while no transaction is open — is the only way to make it hold for the whole
19
+ * run; we restore the prior setting afterwards.
20
+ */
21
+ export function runMigrationsOn(conn, drizzleDb, migrationsFolder = MIGRATIONS_FOLDER) {
22
+ const fkWasOn = conn.pragma('foreign_keys', { simple: true }) === 1;
23
+ if (fkWasOn) {
24
+ conn.pragma('foreign_keys = OFF');
25
+ }
26
+ try {
27
+ migrate(drizzleDb, { migrationsFolder });
28
+ }
29
+ finally {
30
+ if (fkWasOn) {
31
+ conn.pragma('foreign_keys = ON');
32
+ }
33
+ }
34
+ }
35
+ //# sourceMappingURL=migrate-runner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate-runner.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/db/migrate-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qCAAqC,CAAC;AAE9D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC,eAAe,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;KACvE,QAAQ,CAAC;AAUZ;;;;;;;;;;GAUG;AACH,MAAM,UAAU,eAAe,CAC7B,IAAgB,EAChB,SAAwC,EACxC,mBAA2B,iBAAiB;IAE5C,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC;IAEpE,IAAI,OAAO,EAAE,CAAC;QACZ,IAAI,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;IACpC,CAAC;IAED,IAAI,CAAC;QACH,OAAO,CAAC,SAAS,EAAE,EAAE,gBAAgB,EAAE,CAAC,CAAC;IAC3C,CAAC;YAAS,CAAC;QACT,IAAI,OAAO,EAAE,CAAC;YACZ,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;QACnC,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Runs pending Drizzle migrations synchronously before the registry is used.
3
+ * Migrations are read from the ./drizzle folder (generated by drizzle-kit).
4
+ *
5
+ * Delegates to the FK-safe {@link runMigrationsOn} runner (see that module for
6
+ * why foreign-key enforcement must be toggled around the migration run).
7
+ */
8
+ export declare function runMigrations(): void;
9
+ //# sourceMappingURL=migrate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/db/migrate.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAEpC"}
@@ -0,0 +1,13 @@
1
+ import { db, sqlite } from './client.js';
2
+ import { runMigrationsOn } from './migrate-runner.js';
3
+ /**
4
+ * Runs pending Drizzle migrations synchronously before the registry is used.
5
+ * Migrations are read from the ./drizzle folder (generated by drizzle-kit).
6
+ *
7
+ * Delegates to the FK-safe {@link runMigrationsOn} runner (see that module for
8
+ * why foreign-key enforcement must be toggled around the migration run).
9
+ */
10
+ export function runMigrations() {
11
+ runMigrationsOn(sqlite, db);
12
+ }
13
+ //# sourceMappingURL=migrate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/db/migrate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,UAAU,aAAa;IAC3B,eAAe,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAC9B,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { index, integer, primaryKey, sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core';
2
+ export { index, integer, primaryKey, sqliteTable, text, uniqueIndex };
3
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/db/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,EACJ,WAAW,EACZ,MAAM,yBAAyB,CAAC;AAuBjC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,30 @@
1
+ import { index, integer, primaryKey, sqliteTable, text, uniqueIndex, } from 'drizzle-orm/sqlite-core';
2
+ // ──────────────────────────────────────────────
3
+ // @adhd/agent-engine-compiler — table prefix: compiler_
4
+ //
5
+ // Every table in THIS package MUST be named with the `compiler_` prefix.
6
+ // All registry-family packages share ONE SQLite file, so the prefix is the only
7
+ // thing preventing cross-package table-name collisions. See REGISTRY-PACKAGE-RULES.md.
8
+ //
9
+ // Invariants enforced here (do not break them):
10
+ // • Cross-package keys are LOGICAL ONLY — plain text columns, no .references()
11
+ // across package prefixes. In-package FKs use .references() normally.
12
+ // • Controlled vocabularies are lookup tables (text PK) or plain text columns,
13
+ // NEVER SQL enums — a new value is a new row, no migration.
14
+ // • Composite PKs use `primaryKey({ columns: [...] })`, never a non-unique index().
15
+ // • Bump-don't-delete: if you retain versions, never delete a prior version row.
16
+ //
17
+ // The drizzle-orm/sqlite-core helpers are re-exported so a downstream module can
18
+ // `import { sqliteTable, text } from "@adhd/agent-engine-compiler"` without re-importing the
19
+ // dependency directly, and so this skeleton's import is exercised before any table
20
+ // exists. Define real tables here and add them to the barrel in ../index.ts.
21
+ // ──────────────────────────────────────────────
22
+ export { index, integer, primaryKey, sqliteTable, text, uniqueIndex };
23
+ // Example (delete this comment, add real tables prefixed `compiler_`):
24
+ //
25
+ // export const exampleTable = sqliteTable("compiler_example", {
26
+ // slug: text("slug").primaryKey(),
27
+ // label: text("label").notNull(),
28
+ // createdAt: text("created_at").notNull(),
29
+ // });
30
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/db/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,EACJ,WAAW,GACZ,MAAM,yBAAyB,CAAC;AAEjC,iDAAiD;AACjD,wDAAwD;AACxD,EAAE;AACF,yEAAyE;AACzE,gFAAgF;AAChF,uFAAuF;AACvF,EAAE;AACF,gDAAgD;AAChD,iFAAiF;AACjF,0EAA0E;AAC1E,iFAAiF;AACjF,gEAAgE;AAChE,sFAAsF;AACtF,mFAAmF;AACnF,EAAE;AACF,iFAAiF;AACjF,6FAA6F;AAC7F,mFAAmF;AACnF,6EAA6E;AAC7E,iDAAiD;AAEjD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;AAEtE,uEAAuE;AACvE,EAAE;AACF,gEAAgE;AAChE,uCAAuC;AACvC,sCAAsC;AACtC,+CAA+C;AAC/C,MAAM"}
@@ -0,0 +1,50 @@
1
+ import type { ResolvedTool } from '../resolve/tools.js';
2
+ import type { Constraint } from '../resolve/policy.js';
3
+ /**
4
+ * A structured tool entry in the json_object output.
5
+ *
6
+ * For claude_api / openai this mirrors the standard `{name, description,
7
+ * input_schema}` custom tool shape. Server-side type-tagged tools are
8
+ * emitted with the `{type, name}` shape (no input_schema).
9
+ *
10
+ * The compile layer passes the already-shaped objects from emitToolsForProvider
11
+ * directly — this type covers both discriminated variants.
12
+ */
13
+ export type StructuredTool = Record<string, unknown>;
14
+ /** Inputs to the json_object emitter. */
15
+ export interface JsonObjectInput {
16
+ /** Agent slug — the `name` key. */
17
+ agentSlug: string;
18
+ /**
19
+ * Body sections in junction order (ascending position), each the raw content
20
+ * of one resolved component. Joined with '\n\n' to form systemPrompt,
21
+ * with the policy block appended (Decision B.2).
22
+ */
23
+ bodySections: string[];
24
+ /** Effective policy constraints (direct + inherited). */
25
+ constraints: Constraint[];
26
+ /**
27
+ * Resolved platform tools — passed through from resolveTools; used here only
28
+ * to confirm 'unavailable' tools are already absent. The STRUCTURED array
29
+ * passed to `tools` is the caller-supplied `structuredTools`.
30
+ */
31
+ tools: ResolvedTool[];
32
+ /**
33
+ * Pre-shaped structured tool array produced by emitToolsForProvider (or the
34
+ * compile layer's equivalent). This is what lands in `tools` in the JSON.
35
+ */
36
+ structuredTools: StructuredTool[];
37
+ /** Resolved full platform model id (e.g. 'claude-sonnet-4-6'), or '' if absent. */
38
+ model: string;
39
+ }
40
+ /**
41
+ * Emit a `json_object` platform artifact for claude_api / openai / bedrock.
42
+ *
43
+ * Returns a JSON STRING (`content = JSON.stringify(...)`) — callers must not
44
+ * wrap it further. The object shape is frozen per Decision B.2.
45
+ *
46
+ * @param input - Resolved compiler inputs; all fields already platform-shaped.
47
+ * @returns JSON string ready to pass as the API system prompt payload.
48
+ */
49
+ export declare function emitJsonObject(input: JsonObjectInput): string;
50
+ //# sourceMappingURL=json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/emit/json.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAMvD;;;;;;;;;GASG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAErD,yCAAyC;AACzC,MAAM,WAAW,eAAe;IAC9B,mCAAmC;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,yDAAyD;IACzD,WAAW,EAAE,UAAU,EAAE,CAAC;IAC1B;;;;OAIG;IACH,KAAK,EAAE,YAAY,EAAE,CAAC;IACtB;;;OAGG;IACH,eAAe,EAAE,cAAc,EAAE,CAAC;IAClC,mFAAmF;IACnF,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,CAuC7D"}
@@ -0,0 +1,66 @@
1
+ // ──────────────────────────────────────────────
2
+ // json_object emitter (claude_api / openai / bedrock — header_format = 'json_object')
3
+ //
4
+ // Emits a JSON STRING (content = JSON.stringify(...)) — NOT YAML, no '---'.
5
+ //
6
+ // Shape per Decision B.2 (decisions.md):
7
+ //
8
+ // {
9
+ // "name": "<agent.slug>",
10
+ // "systemPrompt": "<flat body: composed sections + policy block, joined '\n\n'>",
11
+ // "model": "<resolveModelId(model_hint, platform)>", // full id
12
+ // "tools": [ /* structured tool array (EmittedTool[]) */ ]
13
+ // }
14
+ //
15
+ // Rules:
16
+ // - `systemPrompt` is the FLAT body (same composed sections as yaml_frontmatter,
17
+ // minus the YAML header; policy constraints folded into the prompt text).
18
+ // - `tools` is a STRUCTURED ARRAY via EmittedTool (from agent-provider's
19
+ // emitToolsForProvider), NOT a comma string.
20
+ // - `model` resolves to the FULL platform id (e.g. 'claude-sonnet-4-6'),
21
+ // distinct from the claude_code alias.
22
+ // - Tools whose platform binding is 'unavailable' are omitted by resolveTools
23
+ // before they reach this emitter — this function does NOT re-filter.
24
+ //
25
+ // [dod.7] audit greps for 'json_object' + 'JSON.stringify' + 'systemPrompt' —
26
+ // all three tokens are present in this file.
27
+ // ──────────────────────────────────────────────
28
+ /**
29
+ * Emit a `json_object` platform artifact for claude_api / openai / bedrock.
30
+ *
31
+ * Returns a JSON STRING (`content = JSON.stringify(...)`) — callers must not
32
+ * wrap it further. The object shape is frozen per Decision B.2.
33
+ *
34
+ * @param input - Resolved compiler inputs; all fields already platform-shaped.
35
+ * @returns JSON string ready to pass as the API system prompt payload.
36
+ */
37
+ export function emitJsonObject(input) {
38
+ const { agentSlug, bodySections, constraints, structuredTools, model } = input;
39
+ // ── systemPrompt: flat body with policy block folded in ──────────────────
40
+ // Same composition logic as yaml_frontmatter body — sections '\n\n'-joined,
41
+ // policy constraints appended as text (Decision B.2).
42
+ const bodyParts = [...bodySections];
43
+ if (constraints.length > 0) {
44
+ const policyLines = ['## Policies', ''];
45
+ for (const c of constraints) {
46
+ const prefix = c.inheritedFrom
47
+ ? `(inherited from ${c.inheritedFrom}) `
48
+ : '';
49
+ policyLines.push(`- ${prefix}${c.text}`);
50
+ }
51
+ bodyParts.push(policyLines.join('\n'));
52
+ }
53
+ const systemPrompt = bodyParts.join('\n\n');
54
+ // ── Assemble the JSON payload ─────────────────────────────────────────────
55
+ // Field order: name → systemPrompt → model → tools.
56
+ // `model` key is always present (empty string when no model_hint).
57
+ const payload = {
58
+ name: agentSlug,
59
+ systemPrompt,
60
+ model,
61
+ tools: structuredTools,
62
+ };
63
+ // json_object format: JSON.stringify the payload (Decision B.2).
64
+ return JSON.stringify(payload);
65
+ }
66
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-compiler/src/emit/json.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,sFAAsF;AACtF,EAAE;AACF,4EAA4E;AAC5E,EAAE;AACF,yCAAyC;AACzC,EAAE;AACF,MAAM;AACN,sCAAsC;AACtC,sFAAsF;AACtF,6EAA6E;AAC7E,sEAAsE;AACtE,MAAM;AACN,EAAE;AACF,SAAS;AACT,mFAAmF;AACnF,8EAA8E;AAC9E,2EAA2E;AAC3E,iDAAiD;AACjD,2EAA2E;AAC3E,2CAA2C;AAC3C,gFAAgF;AAChF,yEAAyE;AACzE,EAAE;AACF,8EAA8E;AAC9E,6CAA6C;AAC7C,iDAAiD;AAgDjD;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,KAAsB;IACnD,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,KAAK,EAAE,GACpE,KAAK,CAAC;IAER,4EAA4E;IAC5E,4EAA4E;IAC5E,sDAAsD;IACtD,MAAM,SAAS,GAAa,CAAC,GAAG,YAAY,CAAC,CAAC;IAE9C,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3B,MAAM,WAAW,GAAG,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;QACxC,KAAK,MAAM,CAAC,IAAI,WAAW,EAAE,CAAC;YAC5B,MAAM,MAAM,GAAG,CAAC,CAAC,aAAa;gBAC5B,CAAC,CAAC,mBAAmB,CAAC,CAAC,aAAa,IAAI;gBACxC,CAAC,CAAC,EAAE,CAAC;YACP,WAAW,CAAC,IAAI,CAAC,KAAK,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QAC3C,CAAC;QACD,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC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