@aionis/sdk 0.1.2 → 0.1.7

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/dist/index.js CHANGED
@@ -41,6 +41,361 @@ function rehydrateHintMemoryIds(value) {
41
41
  .map((entry) => asRecord(entry)?.memory_id)
42
42
  .filter((entry) => typeof entry === "string" && entry.length > 0);
43
43
  }
44
+ function rehydrateHintArray(value) {
45
+ if (!Array.isArray(value))
46
+ return [];
47
+ return value.flatMap((entry) => {
48
+ const record = asRecord(entry);
49
+ const memoryId = record?.memory_id;
50
+ if (!record || typeof memoryId !== "string" || memoryId.length === 0)
51
+ return [];
52
+ const reason = typeof record.reason === "string" && record.reason.length > 0 ? record.reason : undefined;
53
+ return [{
54
+ memory_id: memoryId,
55
+ ...(reason ? { reason } : {}),
56
+ required: record.required === undefined ? true : record.required !== false,
57
+ }];
58
+ });
59
+ }
60
+ function commandPostureArray(value) {
61
+ if (!Array.isArray(value))
62
+ return [];
63
+ return value.flatMap((entry) => {
64
+ const record = asRecord(entry);
65
+ if (!record)
66
+ return [];
67
+ const posture = record.posture;
68
+ const surface = record.surface;
69
+ const memoryId = record.memory_id;
70
+ const instruction = record.instruction;
71
+ const reason = record.reason;
72
+ if (!isCommandPostureKind(posture)
73
+ || !isCommandPostureSurface(surface)
74
+ || typeof memoryId !== "string"
75
+ || memoryId.length === 0
76
+ || typeof instruction !== "string"
77
+ || instruction.length === 0
78
+ || typeof reason !== "string"
79
+ || reason.length === 0) {
80
+ return [];
81
+ }
82
+ return [{
83
+ posture,
84
+ surface,
85
+ memory_id: memoryId,
86
+ instruction,
87
+ reason,
88
+ target_files: stringArray(record.target_files),
89
+ }];
90
+ });
91
+ }
92
+ function isRouteContractSource(value) {
93
+ return value === "target_files"
94
+ || value === "should_continue"
95
+ || value === "inspect_first"
96
+ || value === "must_not";
97
+ }
98
+ function routeContractTargetArray(value) {
99
+ if (!Array.isArray(value))
100
+ return [];
101
+ return value.flatMap((entry) => {
102
+ const record = asRecord(entry);
103
+ if (!record || typeof record.target !== "string" || record.target.length === 0 || !isRouteContractSource(record.source)) {
104
+ return [];
105
+ }
106
+ const sourceMemoryId = typeof record.source_memory_id === "string" && record.source_memory_id.length > 0
107
+ ? record.source_memory_id
108
+ : undefined;
109
+ const reason = typeof record.reason === "string" && record.reason.length > 0 ? record.reason : undefined;
110
+ return [{
111
+ target: record.target,
112
+ ...(sourceMemoryId ? { source_memory_id: sourceMemoryId } : {}),
113
+ source: record.source,
114
+ ...(reason ? { reason } : {}),
115
+ }];
116
+ });
117
+ }
118
+ function routeContractActiveTargetArray(value) {
119
+ const records = Array.isArray(value) ? value : [];
120
+ return routeContractTargetArray(records).map((entry, index) => {
121
+ const record = asRecord(records[index]);
122
+ return {
123
+ ...entry,
124
+ artifact_status: record?.artifact_status === "may_be_absent" ? "may_be_absent" : "unknown",
125
+ missing_policy: "restore_or_create_if_task_consistent_or_rehydrate",
126
+ };
127
+ });
128
+ }
129
+ function routeContractPendingArtifactArray(value) {
130
+ const records = Array.isArray(value) ? value : [];
131
+ return routeContractTargetArray(records).map((entry, index) => {
132
+ const record = asRecord(records[index]);
133
+ const actions = stringArray(record?.allowed_actions).filter((action) => action === "create" || action === "restore" || action === "rehydrate" || action === "report_conflict");
134
+ const preferred = stringArray(record?.preferred_action_order).filter((action) => action === "create" || action === "restore" || action === "rehydrate" || action === "report_conflict");
135
+ return {
136
+ ...entry,
137
+ status: "unknown_until_host_observation",
138
+ when: "if_active_target_is_missing",
139
+ allowed_actions: actions.length > 0 ? actions : ["create", "restore", "rehydrate", "report_conflict"],
140
+ preferred_action_order: preferred.length > 0 ? preferred : ["create", "restore", "rehydrate", "report_conflict"],
141
+ terminal_inspect_allowed: false,
142
+ };
143
+ });
144
+ }
145
+ function routeContractEvidenceSourceArray(value) {
146
+ return routeContractTargetArray(value).map((entry) => ({
147
+ ...entry,
148
+ evidence_use: "reference_only",
149
+ direction_policy: "must_not_be_primary_route",
150
+ }));
151
+ }
152
+ function routeContractBlockedRouteArray(value) {
153
+ return routeContractTargetArray(value).map((entry) => ({
154
+ ...entry,
155
+ direction_policy: "blocked_route",
156
+ evidence_use: "counter_evidence_only",
157
+ }));
158
+ }
159
+ function routeContractActionPolicy(value) {
160
+ const record = asRecord(value);
161
+ const preferred = stringArray(record?.missing_active_target_preferred_order).filter((action) => action === "create" || action === "restore" || action === "rehydrate" || action === "report_conflict");
162
+ return {
163
+ missing_active_target_preferred_order: preferred.length > 0 ? preferred : ["create", "restore", "rehydrate", "report_conflict"],
164
+ terminal_inspect_allowed: false,
165
+ reference_fallback_requires: "explicit_raw_evidence_or_operator_confirmation",
166
+ };
167
+ }
168
+ function routeContractMemoryIds(value) {
169
+ const contract = asRecord(value);
170
+ if (!contract)
171
+ return [];
172
+ const rows = [
173
+ ...routeContractTargetArray(contract.active_targets),
174
+ ...routeContractTargetArray(contract.pending_artifacts),
175
+ ...routeContractTargetArray(contract.reference_only_targets),
176
+ ...routeContractTargetArray(contract.blocked_direction_targets),
177
+ ...routeContractTargetArray(contract.evidence_sources),
178
+ ...routeContractTargetArray(contract.blocked_routes),
179
+ ];
180
+ return rows
181
+ .map((entry) => entry.source_memory_id)
182
+ .filter((entry) => typeof entry === "string" && entry.length > 0);
183
+ }
184
+ function uniqueStrings(values) {
185
+ return Array.from(new Set(values.filter((entry) => entry.length > 0)));
186
+ }
187
+ function guideTraceId(value) {
188
+ const entry = asRecord(value)?.guide_trace_id;
189
+ return typeof entry === "string" && entry.length > 0 ? entry : null;
190
+ }
191
+ function safeAgentPromptFromGuide(guide) {
192
+ try {
193
+ return agentPromptFromGuide(guide);
194
+ }
195
+ catch {
196
+ return "";
197
+ }
198
+ }
199
+ function receiptFromGuideTrace(guide) {
200
+ const guideRecord = asRecord(guide);
201
+ const candidates = [
202
+ asRecord(asRecord(guideRecord?.memory_decision_trace)?.memory_use_receipt),
203
+ asRecord(guideRecord?.memory_use_receipt),
204
+ asRecord(asRecord(guideRecord?.agent_context)?.memory_use_receipt),
205
+ ];
206
+ const receipt = candidates.find((entry) => entry?.contract_version === "aionis_memory_use_receipt_v1");
207
+ return receipt ? receipt : null;
208
+ }
209
+ function admissionRecordFromGuideTrace(guide) {
210
+ const guideRecord = asRecord(guide);
211
+ const candidates = [
212
+ asRecord(asRecord(guideRecord?.memory_decision_trace)?.admission_record),
213
+ asRecord(guideRecord?.memory_admission_record),
214
+ asRecord(asRecord(guideRecord?.agent_context)?.memory_admission_record),
215
+ ];
216
+ const record = candidates.find((entry) => entry?.contract_version === "aionis_memory_admission_record_v1");
217
+ return record ? record : null;
218
+ }
219
+ function decisionSummariesFromSurfaces(input) {
220
+ const rows = [];
221
+ const seen = new Set();
222
+ const push = (entry) => {
223
+ if (seen.has(entry.memory_id))
224
+ return;
225
+ seen.add(entry.memory_id);
226
+ rows.push(entry);
227
+ };
228
+ for (const memoryId of input.useNow) {
229
+ push({
230
+ memory_id: memoryId,
231
+ agent_surface: "use_now",
232
+ decision_kind: "used",
233
+ actionable: true,
234
+ reason_codes: ["agent_context.use_now_memory_ids"],
235
+ });
236
+ }
237
+ for (const memoryId of input.inspect) {
238
+ push({
239
+ memory_id: memoryId,
240
+ agent_surface: "inspect_before_use",
241
+ decision_kind: "downgraded",
242
+ actionable: false,
243
+ reason_codes: ["agent_context.inspect_before_use_memory_ids"],
244
+ });
245
+ }
246
+ for (const memoryId of input.doNotUse) {
247
+ push({
248
+ memory_id: memoryId,
249
+ agent_surface: "do_not_use",
250
+ decision_kind: "blocked",
251
+ actionable: false,
252
+ reason_codes: ["agent_context.do_not_use_memory_ids"],
253
+ });
254
+ }
255
+ for (const memoryId of input.rehydrate) {
256
+ push({
257
+ memory_id: memoryId,
258
+ agent_surface: "rehydrate",
259
+ decision_kind: "rehydrate",
260
+ actionable: false,
261
+ reason_codes: ["agent_context.rehydrate_hints"],
262
+ });
263
+ }
264
+ return rows;
265
+ }
266
+ function generatedMemoryUseReceipt(guide) {
267
+ const context = asRecord(agentContextFromGuide(guide));
268
+ const useNow = uniqueStrings(stringArray(context?.use_now_memory_ids));
269
+ const inspect = uniqueStrings(stringArray(context?.inspect_before_use_memory_ids));
270
+ const doNotUse = uniqueStrings(stringArray(context?.do_not_use_memory_ids));
271
+ const rehydrate = uniqueStrings(rehydrateHintMemoryIds(context?.rehydrate_hints));
272
+ const exposed = uniqueStrings([
273
+ ...stringArray(context?.memory_ids),
274
+ ...useNow,
275
+ ...inspect,
276
+ ...doNotUse,
277
+ ...rehydrate,
278
+ ...commandPostureArray(context?.command_posture).map((entry) => entry.memory_id),
279
+ ...routeContractMemoryIds(context?.route_contract),
280
+ ]);
281
+ const prompt = safeAgentPromptFromGuide(guide);
282
+ return {
283
+ contract_version: "aionis_memory_use_receipt_v1",
284
+ intended_use: "memory_use_audit",
285
+ agent_prompt_included: false,
286
+ runtime_mutation: false,
287
+ guide_trace_id: guideTraceId(guide),
288
+ history_used: exposed.length > 0,
289
+ actionable_history_used: useNow.length > 0,
290
+ prompt_char_count: prompt.length,
291
+ exposed_memory_ids: exposed,
292
+ use_now_memory_ids: useNow,
293
+ inspect_before_use_memory_ids: inspect,
294
+ do_not_use_memory_ids: doNotUse,
295
+ rehydrate_memory_ids: rehydrate,
296
+ attributed_memory_ids: [],
297
+ unattributed_recalled_memory_ids: [],
298
+ read_only_signal_memory_ids: uniqueStrings([...inspect, ...doNotUse, ...rehydrate]),
299
+ decision_summaries: decisionSummariesFromSurfaces({ useNow, inspect, doNotUse, rehydrate }),
300
+ risk_flags: stringArray(asRecord(context?.risk)?.reasons),
301
+ summary: useNow.length > 0
302
+ ? "Aionis exposed adjudicated actionable execution memory."
303
+ : exposed.length > 0
304
+ ? "Aionis exposed adjudicated non-actionable memory surfaces."
305
+ : "Aionis did not expose reusable memory for this guide.",
306
+ };
307
+ }
308
+ function targetList(rows) {
309
+ return uniqueStrings(rows.map((entry) => entry.target));
310
+ }
311
+ function repoPresenceMap(state) {
312
+ const out = new Map();
313
+ for (const target of state?.existing_files ?? [])
314
+ out.set(target, true);
315
+ for (const target of state?.missing_files ?? [])
316
+ out.set(target, false);
317
+ for (const entry of state?.files ?? [])
318
+ out.set(entry.target, entry.exists);
319
+ return out;
320
+ }
321
+ function truncateText(text, maxChars) {
322
+ if (maxChars <= 0)
323
+ return "";
324
+ if (text.length <= maxChars)
325
+ return text;
326
+ const marker = "\n...[truncated by Aionis SDK context budget]...";
327
+ if (maxChars <= marker.length)
328
+ return marker.slice(0, maxChars);
329
+ return `${text.slice(0, maxChars - marker.length).trimEnd()}${marker}`;
330
+ }
331
+ function defaultExecutionPromptBudget(profile) {
332
+ switch (profile) {
333
+ case "compact": return 6_000;
334
+ case "high_recall": return 24_000;
335
+ case "balanced": return 12_000;
336
+ }
337
+ }
338
+ function taskLines(task) {
339
+ if (!task)
340
+ return [];
341
+ if (typeof task === "string")
342
+ return [`- ${task}`];
343
+ return [
344
+ task.task_signature ? `- task_signature: ${task.task_signature}` : "",
345
+ task.run_id ? `- run_id: ${task.run_id}` : "",
346
+ task.query_text ? `- query: ${task.query_text}` : "",
347
+ task.goal ? `- goal: ${task.goal}` : "",
348
+ ].filter((entry) => entry.length > 0);
349
+ }
350
+ function bulletLines(values, empty) {
351
+ return values.length > 0 ? values.map((entry) => `- ${entry}`) : [`- ${empty}`];
352
+ }
353
+ function postureLines(rows, posture, empty) {
354
+ const filtered = rows.filter((entry) => entry.posture === posture);
355
+ if (filtered.length === 0)
356
+ return [`- ${empty}`];
357
+ return filtered.map((entry) => {
358
+ const targets = entry.target_files.length > 0 ? ` targets=${entry.target_files.join(", ")}` : "";
359
+ return `- ${entry.memory_id}: ${entry.instruction} (${entry.reason})${targets}`;
360
+ });
361
+ }
362
+ function rehydrateRequestsFromGuide(guide) {
363
+ const context = asRecord(agentContextFromGuide(guide));
364
+ const fromHints = rehydrateHintArray(context?.rehydrate_hints);
365
+ const fromPosture = commandPostureArray(context?.command_posture)
366
+ .filter((entry) => entry.posture === "rehydrate_first")
367
+ .map((entry) => ({
368
+ memory_id: entry.memory_id,
369
+ reason: entry.reason || entry.instruction,
370
+ required: true,
371
+ }));
372
+ const byId = new Map();
373
+ for (const entry of [...fromHints, ...fromPosture]) {
374
+ const previous = byId.get(entry.memory_id);
375
+ byId.set(entry.memory_id, {
376
+ memory_id: entry.memory_id,
377
+ reason: previous?.reason ?? entry.reason,
378
+ required: (previous?.required ?? false) || entry.required,
379
+ });
380
+ }
381
+ return Array.from(byId.values());
382
+ }
383
+ function isCommandPostureKind(value) {
384
+ return value === "must_not"
385
+ || value === "should_continue"
386
+ || value === "inspect_first"
387
+ || value === "rehydrate_first"
388
+ || value === "optional_context";
389
+ }
390
+ function isCommandPostureSurface(value) {
391
+ return value === "current"
392
+ || value === "procedure"
393
+ || value === "use_now"
394
+ || value === "inspect_before_use"
395
+ || value === "do_not_use"
396
+ || value === "rehydrate"
397
+ || value === "context";
398
+ }
44
399
  function rememberNodeType(kind) {
45
400
  switch (kind) {
46
401
  case "preference": return "self_model";
@@ -282,6 +637,9 @@ export class AionisClient {
282
637
  async guide(body, options) {
283
638
  return this.post("/v1/guide", this.guideBody(body, options), options);
284
639
  }
640
+ async governMemory(body, options) {
641
+ return this.post("/v1/memory/govern", body, options);
642
+ }
285
643
  async forget(body, options) {
286
644
  return this.post("/v1/forget", body, options);
287
645
  }
@@ -297,6 +655,9 @@ export class AionisClient {
297
655
  async operatorSnapshot(body, options) {
298
656
  return this.post("/v1/operator/snapshot", body, options);
299
657
  }
658
+ async flightRecorder(body, options) {
659
+ return this.post("/v1/audit/flight-recorder", body, options);
660
+ }
300
661
  async snapshot(body, options) {
301
662
  return this.operatorSnapshot(body, options);
302
663
  }
@@ -326,9 +687,18 @@ export class AionisClient {
326
687
  }
327
688
  guideBody(body, options) {
328
689
  const compactBody = stripUndefined(body);
329
- if (compactBody.mode !== undefined || compactBody.context_mode !== undefined)
330
- return compactBody;
331
690
  const guideMode = options?.guide_mode === undefined ? this.defaultGuideMode : options.guide_mode;
691
+ if (compactBody.mode !== undefined)
692
+ return compactBody;
693
+ if (compactBody.context_mode !== undefined) {
694
+ if (compactBody.context_mode === "compact_agent" && guideMode) {
695
+ return {
696
+ mode: guideMode,
697
+ ...compactBody,
698
+ };
699
+ }
700
+ return compactBody;
701
+ }
332
702
  if (!guideMode)
333
703
  return compactBody;
334
704
  return {
@@ -350,6 +720,9 @@ export class AionisExecutionClient {
350
720
  constructor(client) {
351
721
  this.client = client;
352
722
  }
723
+ compileAgentContext(input) {
724
+ return compileExecutionAgentContext(input);
725
+ }
353
726
  async observeStep(input, options) {
354
727
  return this.client.observe({
355
728
  ...executionObserveBase(input),
@@ -500,6 +873,126 @@ export function agentPromptFromGuide(guide) {
500
873
  }
501
874
  return promptText;
502
875
  }
876
+ export function rehydrateHintsFromGuide(guide) {
877
+ return rehydrateRequestsFromGuide(guide);
878
+ }
879
+ export function memoryUseReceiptFromGuide(guide) {
880
+ return receiptFromGuideTrace(guide) ?? generatedMemoryUseReceipt(guide);
881
+ }
882
+ export function memoryAdmissionRecordFromGuide(guide) {
883
+ const record = admissionRecordFromGuideTrace(guide);
884
+ if (record)
885
+ return record;
886
+ const receipt = memoryUseReceiptFromGuide(guide);
887
+ const entries = receipt.decision_summaries.map((summary) => ({
888
+ memory_id: summary.memory_id,
889
+ title: null,
890
+ domain: "execution",
891
+ memory_type: "unknown",
892
+ lifecycle_state: "unknown",
893
+ authority: "none",
894
+ admission_action: summary.agent_surface,
895
+ decision_kind: summary.decision_kind,
896
+ actionable: summary.actionable,
897
+ prompt_included: summary.agent_surface !== "not_agent_facing",
898
+ agent_used: receipt.attributed_memory_ids.includes(summary.memory_id),
899
+ feedback_outcome: null,
900
+ attribution_strength: null,
901
+ reason_codes: summary.reason_codes,
902
+ evidence_ids: [],
903
+ }));
904
+ return {
905
+ contract_version: "aionis_memory_admission_record_v1",
906
+ intended_use: "memory_admission_audit_dataset",
907
+ source: "memory_decision_trace",
908
+ agent_prompt_included: false,
909
+ runtime_mutation: false,
910
+ guide_trace_id: receipt.guide_trace_id,
911
+ prompt_char_count: receipt.prompt_char_count,
912
+ history_used: receipt.history_used,
913
+ actionable_history_used: receipt.actionable_history_used,
914
+ candidate_memory_count: entries.length,
915
+ prompt_included_memory_count: entries.filter((entry) => entry.prompt_included).length,
916
+ agent_used_memory_count: entries.filter((entry) => entry.agent_used).length,
917
+ entries,
918
+ summary: `Aionis generated ${entries.length} compact admission records from memory use receipt surfaces; full decision details require Runtime memory_decision_trace.`,
919
+ };
920
+ }
921
+ function admissionDatasetOutcomeLabel(entry) {
922
+ if (entry.agent_used && entry.feedback_outcome === "positive")
923
+ return "positive_use";
924
+ if (entry.agent_used && entry.feedback_outcome === "negative")
925
+ return "negative_use";
926
+ if (entry.agent_used && entry.feedback_outcome === "neutral")
927
+ return "neutral_use";
928
+ if (entry.admission_action === "do_not_use")
929
+ return "blocked_or_suppressed";
930
+ if (entry.admission_action === "rehydrate")
931
+ return "rehydrate_requested";
932
+ if (entry.admission_action === "not_agent_facing")
933
+ return "not_agent_facing";
934
+ if (entry.prompt_included && !entry.agent_used)
935
+ return "unused_exposed";
936
+ return "unknown";
937
+ }
938
+ function admissionDatasetString(value) {
939
+ return typeof value === "string" && value.length > 0 ? value : null;
940
+ }
941
+ export function memoryAdmissionDatasetRowsFromRecord(record, options = {}) {
942
+ return record.entries.map((entry, index) => ({
943
+ contract_version: "aionis_memory_admission_dataset_row_v1",
944
+ intended_use: "memory_admission_policy_training_or_audit",
945
+ source: "memory_admission_record",
946
+ agent_prompt_included: false,
947
+ runtime_mutation: false,
948
+ tenant_id: admissionDatasetString(record.tenant_id),
949
+ scope: admissionDatasetString(record.scope),
950
+ guide_trace_id: admissionDatasetString(record.guide_trace_id),
951
+ run_id: admissionDatasetString(options.run_id),
952
+ task_id: admissionDatasetString(options.task_id),
953
+ task_signature: admissionDatasetString(options.task_signature),
954
+ row_index: index,
955
+ memory_id: entry.memory_id,
956
+ title: entry.title,
957
+ memory_origin: entry.memory_origin ?? "aionis",
958
+ source_backend: admissionDatasetString(entry.source_backend ?? null),
959
+ domain: entry.domain,
960
+ memory_type: entry.memory_type,
961
+ lifecycle_state: entry.lifecycle_state,
962
+ authority: entry.authority,
963
+ admission_action: entry.admission_action,
964
+ decision_kind: entry.decision_kind,
965
+ actionable: entry.actionable,
966
+ prompt_included: entry.prompt_included,
967
+ agent_used: entry.agent_used,
968
+ feedback_outcome: entry.feedback_outcome,
969
+ attribution_strength: entry.attribution_strength,
970
+ outcome_label: admissionDatasetOutcomeLabel(entry),
971
+ reason_codes: [...entry.reason_codes],
972
+ evidence_ids: [...entry.evidence_ids],
973
+ prompt_char_count: record.prompt_char_count,
974
+ history_used: record.history_used,
975
+ actionable_history_used: record.actionable_history_used,
976
+ }));
977
+ }
978
+ export function memoryAdmissionDatasetRowsFromRecords(records, options = {}) {
979
+ return records.flatMap((record) => memoryAdmissionDatasetRowsFromRecord(record, options));
980
+ }
981
+ export function memoryAdmissionDatasetRowsFromGuide(guide, options = {}) {
982
+ return memoryAdmissionDatasetRowsFromRecord(memoryAdmissionRecordFromGuide(guide), options);
983
+ }
984
+ export function memoryAdmissionDatasetJsonlFromRows(rows) {
985
+ return rows.length > 0 ? `${rows.map((row) => JSON.stringify(row)).join("\n")}\n` : "";
986
+ }
987
+ export function memoryAdmissionDatasetJsonlFromRecord(record, options = {}) {
988
+ return memoryAdmissionDatasetJsonlFromRows(memoryAdmissionDatasetRowsFromRecord(record, options));
989
+ }
990
+ export function memoryAdmissionDatasetJsonlFromRecords(records, options = {}) {
991
+ return memoryAdmissionDatasetJsonlFromRows(memoryAdmissionDatasetRowsFromRecords(records, options));
992
+ }
993
+ export function memoryAdmissionDatasetJsonlFromGuide(guide, options = {}) {
994
+ return memoryAdmissionDatasetJsonlFromRows(memoryAdmissionDatasetRowsFromGuide(guide, options));
995
+ }
503
996
  export function memoryIdsFromGuide(guide) {
504
997
  const context = asRecord(agentContextFromGuide(guide));
505
998
  const ids = [
@@ -508,9 +1001,196 @@ export function memoryIdsFromGuide(guide) {
508
1001
  ...stringArray(context?.inspect_before_use_memory_ids),
509
1002
  ...stringArray(context?.do_not_use_memory_ids),
510
1003
  ...rehydrateHintMemoryIds(context?.rehydrate_hints),
1004
+ ...commandPostureArray(context?.command_posture).map((entry) => entry.memory_id),
1005
+ ...routeContractMemoryIds(context?.route_contract),
511
1006
  ];
512
1007
  return Array.from(new Set(ids));
513
1008
  }
1009
+ export function routeContractFromGuide(guide) {
1010
+ const contract = asRecord(asRecord(agentContextFromGuide(guide))?.route_contract);
1011
+ if (!contract)
1012
+ return null;
1013
+ return {
1014
+ active_targets: routeContractActiveTargetArray(contract.active_targets),
1015
+ pending_artifacts: routeContractPendingArtifactArray(contract.pending_artifacts),
1016
+ reference_only_targets: routeContractTargetArray(contract.reference_only_targets),
1017
+ blocked_direction_targets: routeContractTargetArray(contract.blocked_direction_targets),
1018
+ evidence_sources: routeContractEvidenceSourceArray(Array.isArray(contract.evidence_sources) ? contract.evidence_sources : contract.reference_only_targets),
1019
+ blocked_routes: routeContractBlockedRouteArray(Array.isArray(contract.blocked_routes) ? contract.blocked_routes : contract.blocked_direction_targets),
1020
+ conflict_policy: "do_not_treat_missing_active_target_as_superseded",
1021
+ fallback_policy: "do_not_promote_reference_or_blocked_targets",
1022
+ action_policy: routeContractActionPolicy(contract.action_policy),
1023
+ };
1024
+ }
1025
+ export function activeRouteTargetsFromGuide(guide) {
1026
+ return routeContractFromGuide(guide)?.active_targets.map((entry) => entry.target) ?? [];
1027
+ }
1028
+ export function pendingArtifactTargetsFromGuide(guide) {
1029
+ return routeContractFromGuide(guide)?.pending_artifacts.map((entry) => entry.target) ?? [];
1030
+ }
1031
+ export function referenceOnlyRouteTargetsFromGuide(guide) {
1032
+ return routeContractFromGuide(guide)?.reference_only_targets.map((entry) => entry.target) ?? [];
1033
+ }
1034
+ export function blockedDirectionRouteTargetsFromGuide(guide) {
1035
+ return routeContractFromGuide(guide)?.blocked_direction_targets.map((entry) => entry.target) ?? [];
1036
+ }
1037
+ export function evidenceSourcesFromGuide(guide) {
1038
+ return routeContractFromGuide(guide)?.evidence_sources ?? [];
1039
+ }
1040
+ export function blockedRoutesFromGuide(guide) {
1041
+ return routeContractFromGuide(guide)?.blocked_routes ?? [];
1042
+ }
1043
+ export function compileExecutionAgentContext(input) {
1044
+ const profile = input.budget_profile ?? "balanced";
1045
+ const maxPromptChars = input.max_prompt_chars ?? defaultExecutionPromptBudget(profile);
1046
+ const basePrompt = safeAgentPromptFromGuide(input.guide);
1047
+ const routeContract = routeContractFromGuide(input.guide);
1048
+ const commandPosture = commandPostureFromGuide(input.guide);
1049
+ const receipt = memoryUseReceiptFromGuide(input.guide);
1050
+ const admissionRecord = memoryAdmissionRecordFromGuide(input.guide);
1051
+ const rehydrateRequests = rehydrateHintsFromGuide(input.guide);
1052
+ const useNowMemoryIds = receipt.use_now_memory_ids;
1053
+ const inspectMemoryIds = receipt.inspect_before_use_memory_ids;
1054
+ const doNotUseMemoryIds = receipt.do_not_use_memory_ids;
1055
+ const activeTargets = routeContract ? targetList(routeContract.active_targets) : [];
1056
+ const pendingArtifacts = routeContract ? targetList(routeContract.pending_artifacts) : [];
1057
+ const referenceOnlyTargets = routeContract ? targetList(routeContract.evidence_sources.length > 0
1058
+ ? routeContract.evidence_sources
1059
+ : routeContract.reference_only_targets) : [];
1060
+ const blockedDirectionTargets = routeContract ? targetList(routeContract.blocked_routes.length > 0
1061
+ ? routeContract.blocked_routes
1062
+ : routeContract.blocked_direction_targets) : [];
1063
+ const presence = repoPresenceMap(input.repo_state);
1064
+ const missingActiveTargets = activeTargets.filter((target) => presence.get(target) === false);
1065
+ const warnings = [];
1066
+ if (missingActiveTargets.length > 0) {
1067
+ warnings.push({
1068
+ code: "missing_active_target",
1069
+ message: "An active route target is absent in the observed workspace; treat it as pending work, not stale memory.",
1070
+ targets: missingActiveTargets,
1071
+ });
1072
+ }
1073
+ if (blockedDirectionTargets.length > 0) {
1074
+ warnings.push({
1075
+ code: "blocked_route_present",
1076
+ message: "Blocked or retired targets are counter-evidence only and must not become the primary route.",
1077
+ targets: blockedDirectionTargets,
1078
+ });
1079
+ }
1080
+ if (referenceOnlyTargets.length > 0) {
1081
+ warnings.push({
1082
+ code: "reference_only_target_present",
1083
+ message: "Reference-only targets may be inspected for evidence but must not be promoted into the chosen route.",
1084
+ targets: referenceOnlyTargets,
1085
+ });
1086
+ }
1087
+ if (rehydrateRequests.length > 0) {
1088
+ warnings.push({
1089
+ code: "rehydrate_recommended",
1090
+ message: "Aionis exposed rehydrate pointers for evidence that should be expanded before exact use.",
1091
+ memory_ids: rehydrateRequests.map((entry) => entry.memory_id),
1092
+ });
1093
+ }
1094
+ const contractSections = [
1095
+ "AIONIS_EXECUTION_AGENT_CONTEXT v1",
1096
+ "Treat this as the SDK-compiled execution-memory contract. Runtime remains the authority for memory admission.",
1097
+ "",
1098
+ "TASK",
1099
+ ...taskLines(input.task),
1100
+ ...(taskLines(input.task).length === 0 ? ["- Continue the current host task."] : []),
1101
+ "",
1102
+ "EXECUTION CONTRACT",
1103
+ "- Follow active targets and should_continue memories as the current execution route.",
1104
+ "- If an active target is missing, treat it as pending work to create or restore when task-consistent; do not fall back to blocked or reference-only paths because they exist.",
1105
+ "- Reference-only targets may be read for evidence, but they are not valid primary routes without explicit host confirmation.",
1106
+ "- Blocked, must_not, stale, failed, or retired routes are counter-evidence only.",
1107
+ "- If compact evidence is insufficient for a precise edit, request rehydrate instead of guessing.",
1108
+ ...(input.additional_instructions ?? []).map((entry) => `- ${entry}`),
1109
+ "",
1110
+ "ACTIVE_TARGETS",
1111
+ ...bulletLines(activeTargets, "none"),
1112
+ "",
1113
+ "MISSING_ACTIVE_TARGETS",
1114
+ ...bulletLines(missingActiveTargets, "none observed"),
1115
+ "",
1116
+ "PENDING_ARTIFACTS",
1117
+ ...bulletLines(pendingArtifacts, "none"),
1118
+ "",
1119
+ "SHOULD_CONTINUE",
1120
+ ...postureLines(commandPosture, "should_continue", "none"),
1121
+ "",
1122
+ "INSPECT_BEFORE_USE",
1123
+ ...postureLines(commandPosture, "inspect_first", "none"),
1124
+ "",
1125
+ "DO_NOT_USE",
1126
+ ...postureLines(commandPosture, "must_not", "none"),
1127
+ "",
1128
+ "REFERENCE_ONLY_TARGETS",
1129
+ ...bulletLines(referenceOnlyTargets, "none"),
1130
+ "",
1131
+ "BLOCKED_DIRECTION_TARGETS",
1132
+ ...bulletLines(blockedDirectionTargets, "none"),
1133
+ "",
1134
+ "REHYDRATE_REQUESTS",
1135
+ ...(rehydrateRequests.length > 0
1136
+ ? rehydrateRequests.map((entry) => `- ${entry.memory_id}${entry.reason ? `: ${entry.reason}` : ""}`)
1137
+ : ["- none"]),
1138
+ ];
1139
+ const contractPrompt = contractSections.join("\n");
1140
+ const includeBasePrompt = input.include_base_prompt ?? true;
1141
+ const baseHeader = "\n\nBASE_AIONIS_CONTEXT\n";
1142
+ const baseBudget = includeBasePrompt ? Math.max(0, maxPromptChars - contractPrompt.length - baseHeader.length) : 0;
1143
+ const renderedBasePrompt = includeBasePrompt && basePrompt.length > 0 ? truncateText(basePrompt, baseBudget) : "";
1144
+ const agentPrompt = renderedBasePrompt
1145
+ ? truncateText(`${contractPrompt}${baseHeader}${renderedBasePrompt}`, maxPromptChars)
1146
+ : truncateText(contractPrompt, maxPromptChars);
1147
+ return {
1148
+ contract_version: "aionis_execution_agent_context_v1",
1149
+ budget_profile: profile,
1150
+ agent_prompt: agentPrompt,
1151
+ base_prompt: basePrompt,
1152
+ prompt_char_count: agentPrompt.length,
1153
+ route_contract: routeContract,
1154
+ command_posture: commandPosture,
1155
+ memory_use_receipt: receipt,
1156
+ memory_admission_record: admissionRecord,
1157
+ rehydrate_requests: rehydrateRequests,
1158
+ use_now_memory_ids: useNowMemoryIds,
1159
+ inspect_before_use_memory_ids: inspectMemoryIds,
1160
+ do_not_use_memory_ids: doNotUseMemoryIds,
1161
+ active_targets: activeTargets,
1162
+ missing_active_targets: missingActiveTargets,
1163
+ pending_artifacts: pendingArtifacts,
1164
+ reference_only_targets: referenceOnlyTargets,
1165
+ blocked_direction_targets: blockedDirectionTargets,
1166
+ execution_warnings: warnings,
1167
+ };
1168
+ }
1169
+ export function compileCodingAgentContext(input) {
1170
+ return compileExecutionAgentContext(input);
1171
+ }
1172
+ export function commandPostureFromGuide(guide, posture) {
1173
+ const rows = commandPostureArray(asRecord(agentContextFromGuide(guide))?.command_posture);
1174
+ return posture ? rows.filter((entry) => entry.posture === posture) : rows;
1175
+ }
1176
+ export function commandPostureMemoryIdsFromGuide(guide, posture) {
1177
+ return Array.from(new Set(commandPostureFromGuide(guide, posture).map((entry) => entry.memory_id)));
1178
+ }
1179
+ export function mustNotMemoryIdsFromGuide(guide) {
1180
+ return commandPostureMemoryIdsFromGuide(guide, "must_not");
1181
+ }
1182
+ export function shouldContinueMemoryIdsFromGuide(guide) {
1183
+ return commandPostureMemoryIdsFromGuide(guide, "should_continue");
1184
+ }
1185
+ export function inspectFirstMemoryIdsFromGuide(guide) {
1186
+ return commandPostureMemoryIdsFromGuide(guide, "inspect_first");
1187
+ }
1188
+ export function rehydrateFirstMemoryIdsFromGuide(guide) {
1189
+ return commandPostureMemoryIdsFromGuide(guide, "rehydrate_first");
1190
+ }
1191
+ export function optionalContextMemoryIdsFromGuide(guide) {
1192
+ return commandPostureMemoryIdsFromGuide(guide, "optional_context");
1193
+ }
514
1194
  export function feedbackFromGuide(input) {
515
1195
  const guide = asRecord(input.guide);
516
1196
  const guideTraceId = guide?.guide_trace_id;