@noopolis/daimon 0.1.1 → 0.1.2

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 (102) hide show
  1. package/README.md +54 -10
  2. package/dist/core/siblingBuildFreshness.d.ts +21 -0
  3. package/dist/core/siblingBuildFreshness.d.ts.map +1 -0
  4. package/dist/core/siblingBuildFreshness.js +25 -0
  5. package/dist/core/types.d.ts +10 -1
  6. package/dist/core/types.d.ts.map +1 -1
  7. package/dist/examples/exampleCausalId.d.ts +3 -0
  8. package/dist/examples/exampleCausalId.d.ts.map +1 -0
  9. package/dist/examples/exampleCausalId.js +8 -0
  10. package/dist/examples/jungian-play-org.js +3 -2
  11. package/dist/examples/jungian-triad-org.js +3 -2
  12. package/dist/examples/jungianPlayAgent.d.ts +1 -1
  13. package/dist/examples/jungianPlayAgent.d.ts.map +1 -1
  14. package/dist/examples/jungianPlayAgent.js +1 -1
  15. package/dist/examples/jungianProfiles.d.ts +1 -1
  16. package/dist/examples/jungianProfiles.d.ts.map +1 -1
  17. package/dist/examples/jungianTriadProfiles.d.ts +1 -1
  18. package/dist/examples/jungianTriadProfiles.d.ts.map +1 -1
  19. package/dist/examples/pi-agent.js +3 -2
  20. package/dist/examples/pi-memory-org.js +6 -5
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +1 -0
  24. package/dist/mcp/toolServer.d.ts +14 -0
  25. package/dist/mcp/toolServer.d.ts.map +1 -0
  26. package/dist/mcp/toolServer.js +105 -0
  27. package/dist/observability/causalEvents.d.ts +118 -0
  28. package/dist/observability/causalEvents.d.ts.map +1 -0
  29. package/dist/observability/causalEvents.js +135 -0
  30. package/dist/observability/controlCausal.d.ts +117 -0
  31. package/dist/observability/controlCausal.d.ts.map +1 -0
  32. package/dist/observability/controlCausal.js +107 -0
  33. package/dist/observability/emitCausalFixture.d.ts +25 -0
  34. package/dist/observability/emitCausalFixture.d.ts.map +1 -0
  35. package/dist/observability/emitCausalFixture.js +98 -0
  36. package/dist/observability/index.d.ts +2 -0
  37. package/dist/observability/index.d.ts.map +1 -1
  38. package/dist/observability/index.js +2 -0
  39. package/dist/pi/cliSession.d.ts +46 -0
  40. package/dist/pi/cliSession.d.ts.map +1 -0
  41. package/dist/pi/cliSession.js +224 -0
  42. package/dist/pi/index.d.ts +5 -0
  43. package/dist/pi/index.d.ts.map +1 -1
  44. package/dist/pi/index.js +5 -0
  45. package/dist/pi/memoryTools.d.ts +55 -1
  46. package/dist/pi/memoryTools.d.ts.map +1 -1
  47. package/dist/pi/memoryTools.js +144 -24
  48. package/dist/pi/modelRegistry.d.ts +13 -0
  49. package/dist/pi/modelRegistry.d.ts.map +1 -0
  50. package/dist/pi/modelRegistry.js +19 -0
  51. package/dist/pi/piAgentHandle.d.ts +45 -0
  52. package/dist/pi/piAgentHandle.d.ts.map +1 -0
  53. package/dist/pi/piAgentHandle.js +324 -0
  54. package/dist/pi/piAgentWakeSupport.d.ts +68 -0
  55. package/dist/pi/piAgentWakeSupport.d.ts.map +1 -0
  56. package/dist/pi/piAgentWakeSupport.js +147 -0
  57. package/dist/pi/piHarness.d.ts +30 -4
  58. package/dist/pi/piHarness.d.ts.map +1 -1
  59. package/dist/pi/piHarness.js +97 -255
  60. package/dist/pi/prompts.d.ts +9 -0
  61. package/dist/pi/prompts.d.ts.map +1 -0
  62. package/dist/pi/prompts.js +39 -0
  63. package/dist/pi/rawTrainingCapture.d.ts +59 -0
  64. package/dist/pi/rawTrainingCapture.d.ts.map +1 -0
  65. package/dist/pi/rawTrainingCapture.js +175 -0
  66. package/dist/pi/turnCausal.d.ts +73 -0
  67. package/dist/pi/turnCausal.d.ts.map +1 -0
  68. package/dist/pi/turnCausal.js +75 -0
  69. package/dist/pi/turnTrace.d.ts +128 -0
  70. package/dist/pi/turnTrace.d.ts.map +1 -0
  71. package/dist/pi/turnTrace.js +141 -0
  72. package/dist/pi/wakeAcceptance.d.ts +49 -0
  73. package/dist/pi/wakeAcceptance.d.ts.map +1 -0
  74. package/dist/pi/wakeAcceptance.js +281 -0
  75. package/dist/pi/wakeAcceptanceFs.d.ts +44 -0
  76. package/dist/pi/wakeAcceptanceFs.d.ts.map +1 -0
  77. package/dist/pi/wakeAcceptanceFs.js +318 -0
  78. package/dist/pi/wakeAcceptanceSchema.d.ts +75 -0
  79. package/dist/pi/wakeAcceptanceSchema.d.ts.map +1 -0
  80. package/dist/pi/wakeAcceptanceSchema.js +265 -0
  81. package/dist/pi/wakeModes.d.ts +9 -0
  82. package/dist/pi/wakeModes.d.ts.map +1 -0
  83. package/dist/pi/wakeModes.js +19 -0
  84. package/dist/pi/worldNudge.d.ts +22 -0
  85. package/dist/pi/worldNudge.d.ts.map +1 -0
  86. package/dist/pi/worldNudge.js +62 -0
  87. package/dist/pi/worldToolProtocol.d.ts +13 -0
  88. package/dist/pi/worldToolProtocol.d.ts.map +1 -0
  89. package/dist/pi/worldToolProtocol.js +49 -0
  90. package/dist/pi/worldTools.d.ts +32 -0
  91. package/dist/pi/worldTools.d.ts.map +1 -0
  92. package/dist/pi/worldTools.js +373 -0
  93. package/dist/pi/worldTrajectory.d.ts +52 -0
  94. package/dist/pi/worldTrajectory.d.ts.map +1 -0
  95. package/dist/pi/worldTrajectory.js +131 -0
  96. package/package.json +10 -13
  97. package/dist/examples/mixed-engine-org.d.ts +0 -2
  98. package/dist/examples/mixed-engine-org.d.ts.map +0 -1
  99. package/dist/examples/mixed-engine-org.js +0 -351
  100. package/dist/examples/mixedEngineCli.d.ts +0 -15
  101. package/dist/examples/mixedEngineCli.d.ts.map +0 -1
  102. package/dist/examples/mixedEngineCli.js +0 -174
@@ -0,0 +1,75 @@
1
+ import type { WakeEvent } from "../core/types.js";
2
+ export declare const WAKE_ACCEPTANCE_VERSION: "noopolis.wake-acceptance.v1";
3
+ export declare const WAKE_ACCEPTANCE_FILE: "state.v1.json";
4
+ export declare const WAKE_ACCEPTANCE_COMPLETED_TOMBSTONES = 512;
5
+ export declare const WAKE_ACCEPTANCE_FILE_BYTES_MAX = 1048576;
6
+ export declare const WAKE_ACCEPTANCE_FIELD_BYTES_MAX = 512;
7
+ export type WakeAcceptanceState = "accepted" | "invoking" | "completed" | "incomplete";
8
+ export type WakeAcceptanceSafeErrorCode = "wake_delivery_conflict" | "wake_delivery_incomplete" | "wake_acceptance_store_corrupt" | "wake_delivery_invalid";
9
+ export declare class WakeAcceptanceError extends Error {
10
+ readonly code: WakeAcceptanceSafeErrorCode;
11
+ constructor(code: WakeAcceptanceSafeErrorCode);
12
+ }
13
+ export interface WakeAcceptanceAttempt {
14
+ bodySha256: string;
15
+ contextId: string;
16
+ digest: string;
17
+ eventId: string;
18
+ identity: string;
19
+ kind: string;
20
+ sender: string;
21
+ target: string;
22
+ }
23
+ export interface WakeAcceptanceRecord {
24
+ body_sha256: string;
25
+ context_id: string;
26
+ digest: string;
27
+ event_id: string;
28
+ identity: string;
29
+ kind: string;
30
+ sender: string;
31
+ state: WakeAcceptanceState;
32
+ sequence: number;
33
+ target: string;
34
+ }
35
+ export interface WakeAcceptanceStoreState {
36
+ version: string;
37
+ run_id: string;
38
+ agent_id: string;
39
+ next_sequence: number;
40
+ records: WakeAcceptanceRecord[];
41
+ }
42
+ export declare const wakeAcceptanceIdentity: (input: {
43
+ runId: string;
44
+ agentId: string;
45
+ eventId: string;
46
+ }) => string;
47
+ export declare const wakeAcceptanceDigest: (input: {
48
+ bodySha256: string;
49
+ contextId: string;
50
+ eventId: string;
51
+ kind: string;
52
+ sender: string;
53
+ target: string;
54
+ }) => string;
55
+ export declare const candidateFromDelivery: (input: {
56
+ event: WakeEvent;
57
+ runId: string;
58
+ trustedAgentId: string;
59
+ }) => WakeAcceptanceAttempt;
60
+ export declare const candidateFromEvent: (input: {
61
+ event: WakeEvent;
62
+ runId: string;
63
+ agentId: string;
64
+ }) => WakeAcceptanceAttempt;
65
+ export declare const parseWakeAcceptanceState: (raw: unknown, context: {
66
+ runId: string;
67
+ agentId: string;
68
+ }) => WakeAcceptanceStoreState;
69
+ export declare const emptyWakeAcceptanceState: (context: {
70
+ runId: string;
71
+ agentId: string;
72
+ }) => WakeAcceptanceStoreState;
73
+ export declare const pruneCompletedRecords: (records: WakeAcceptanceRecord[]) => WakeAcceptanceRecord[];
74
+ export declare const serializeWakeAcceptanceState: (state: WakeAcceptanceStoreState) => string;
75
+ //# sourceMappingURL=wakeAcceptanceSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wakeAcceptanceSchema.d.ts","sourceRoot":"","sources":["../../src/pi/wakeAcceptanceSchema.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,265 @@
1
+ import { createHash } from "node:crypto";
2
+ export const WAKE_ACCEPTANCE_VERSION = "noopolis.wake-acceptance.v1";
3
+ export const WAKE_ACCEPTANCE_FILE = "state.v1.json";
4
+ export const WAKE_ACCEPTANCE_COMPLETED_TOMBSTONES = 512;
5
+ export const WAKE_ACCEPTANCE_FILE_BYTES_MAX = 1_048_576;
6
+ export const WAKE_ACCEPTANCE_FIELD_BYTES_MAX = 512;
7
+ export class WakeAcceptanceError extends Error {
8
+ code;
9
+ constructor(code) {
10
+ super(code);
11
+ this.code = code;
12
+ this.name = "WakeAcceptanceError";
13
+ }
14
+ }
15
+ const UTF8 = "utf8";
16
+ const IDENTITY_DOMAIN = "daimon.wake-acceptance.v1";
17
+ const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
18
+ const exactKeys = (value, keys) => {
19
+ const current = Object.keys(value);
20
+ if (current.length !== keys.length) {
21
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
22
+ }
23
+ for (const key of keys) {
24
+ if (!Object.prototype.hasOwnProperty.call(value, key)) {
25
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
26
+ }
27
+ }
28
+ };
29
+ const assertInteger = (value, key, allowNegative = false) => {
30
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || (!allowNegative && value < 0)) {
31
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
32
+ }
33
+ return value;
34
+ };
35
+ const textMaxBytes = (value, maxBytes = WAKE_ACCEPTANCE_FIELD_BYTES_MAX) => Buffer.byteLength(value, UTF8) <= maxBytes;
36
+ const assertText = (value, code, allowEmpty = false) => {
37
+ if (typeof value !== "string") {
38
+ throw new WakeAcceptanceError(code);
39
+ }
40
+ if (!allowEmpty && value.length === 0) {
41
+ throw new WakeAcceptanceError(code);
42
+ }
43
+ if (!textMaxBytes(value)) {
44
+ throw new WakeAcceptanceError(code);
45
+ }
46
+ return value;
47
+ };
48
+ const assertTextUnbounded = (value, code) => {
49
+ if (typeof value !== "string") {
50
+ throw new WakeAcceptanceError(code);
51
+ }
52
+ return value;
53
+ };
54
+ const assertObject = (value, code) => {
55
+ if (!isObject(value)) {
56
+ throw new WakeAcceptanceError(code);
57
+ }
58
+ return value;
59
+ };
60
+ const canonical = (left, right) => {
61
+ if (left.sequence !== right.sequence) {
62
+ return left.sequence - right.sequence;
63
+ }
64
+ return left.identity.localeCompare(right.identity);
65
+ };
66
+ const isHex64 = (value) => /^[0-9a-f]{64}$/u.test(value);
67
+ const sha256 = (value) => createHash("sha256").update(value, UTF8).digest("hex");
68
+ const identityPreimage = (input) => `${IDENTITY_DOMAIN}\0${input.runId}\0${input.agentId}\0${input.eventId}`;
69
+ export const wakeAcceptanceIdentity = (input) => sha256(identityPreimage(input));
70
+ export const wakeAcceptanceDigest = (input) => sha256(JSON.stringify({
71
+ body_sha256: input.bodySha256,
72
+ context_id: input.contextId,
73
+ event_id: input.eventId,
74
+ kind: input.kind,
75
+ sender: input.sender,
76
+ target: input.target
77
+ }));
78
+ const parseRecord = (raw) => {
79
+ exactKeys(raw, [
80
+ "body_sha256",
81
+ "context_id",
82
+ "digest",
83
+ "event_id",
84
+ "identity",
85
+ "kind",
86
+ "sender",
87
+ "state",
88
+ "sequence",
89
+ "target"
90
+ ]);
91
+ const body = assertText(raw.body_sha256, "wake_acceptance_store_corrupt");
92
+ const contextId = assertText(raw.context_id, "wake_acceptance_store_corrupt");
93
+ const digest = assertText(raw.digest, "wake_acceptance_store_corrupt");
94
+ const eventId = assertText(raw.event_id, "wake_acceptance_store_corrupt");
95
+ const identity = assertText(raw.identity, "wake_acceptance_store_corrupt");
96
+ const kind = assertText(raw.kind, "wake_acceptance_store_corrupt");
97
+ const sender = assertText(raw.sender, "wake_acceptance_store_corrupt");
98
+ const target = assertText(raw.target, "wake_acceptance_store_corrupt");
99
+ const state = assertText(raw.state, "wake_acceptance_store_corrupt");
100
+ const sequence = assertInteger(raw.sequence, "sequence");
101
+ if (state !== "accepted" && state !== "invoking" && state !== "completed" && state !== "incomplete") {
102
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
103
+ }
104
+ if (!isHex64(body) || !isHex64(digest) || !isHex64(identity)) {
105
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
106
+ }
107
+ return {
108
+ body_sha256: body,
109
+ context_id: contextId,
110
+ digest,
111
+ event_id: eventId,
112
+ identity,
113
+ kind,
114
+ sender,
115
+ state,
116
+ sequence,
117
+ target
118
+ };
119
+ };
120
+ export const candidateFromDelivery = (input) => {
121
+ const eventId = assertText(input.event.id, "wake_delivery_invalid");
122
+ const kind = assertText(input.event.kind, "wake_delivery_invalid");
123
+ if (kind !== "message") {
124
+ throw new WakeAcceptanceError("wake_delivery_invalid");
125
+ }
126
+ if (input.event.delivery === undefined) {
127
+ throw new WakeAcceptanceError("wake_delivery_invalid");
128
+ }
129
+ if (assertText(input.event.delivery.eventId, "wake_delivery_invalid") !== eventId) {
130
+ throw new WakeAcceptanceError("wake_delivery_invalid");
131
+ }
132
+ const sender = assertText(input.event.delivery.sender, "wake_delivery_invalid");
133
+ const target = assertText(input.event.delivery.target, "wake_delivery_invalid");
134
+ const contextId = assertText(input.event.delivery.contextId, "wake_delivery_invalid");
135
+ const from = assertText(input.event.from, "wake_delivery_invalid");
136
+ if (target !== input.trustedAgentId || from !== sender) {
137
+ throw new WakeAcceptanceError("wake_delivery_invalid");
138
+ }
139
+ const bodySha256 = sha256(assertTextUnbounded(input.event.text, "wake_delivery_invalid"));
140
+ return {
141
+ bodySha256,
142
+ contextId,
143
+ digest: wakeAcceptanceDigest({
144
+ bodySha256,
145
+ contextId,
146
+ eventId,
147
+ kind,
148
+ sender,
149
+ target
150
+ }),
151
+ eventId,
152
+ identity: wakeAcceptanceIdentity({ runId: input.runId, agentId: input.trustedAgentId, eventId }),
153
+ kind,
154
+ sender,
155
+ target
156
+ };
157
+ };
158
+ export const candidateFromEvent = (input) => candidateFromDelivery({
159
+ event: input.event,
160
+ runId: input.runId,
161
+ trustedAgentId: input.agentId
162
+ });
163
+ const parseState = (raw, context) => {
164
+ const root = assertObject(raw, "wake_acceptance_store_corrupt");
165
+ exactKeys(root, ["version", "run_id", "agent_id", "next_sequence", "records"]);
166
+ const version = assertText(root.version, "wake_acceptance_store_corrupt");
167
+ const runId = assertText(root.run_id, "wake_acceptance_store_corrupt");
168
+ const agentId = assertText(root.agent_id, "wake_acceptance_store_corrupt");
169
+ const nextSequence = assertInteger(root.next_sequence, "next_sequence");
170
+ if (version !== WAKE_ACCEPTANCE_VERSION || runId !== context.runId || agentId !== context.agentId) {
171
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
172
+ }
173
+ if (!Array.isArray(root.records)) {
174
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
175
+ }
176
+ const records = root.records.map((entry) => {
177
+ const parsed = parseRecord(assertObject(entry, "wake_acceptance_store_corrupt"));
178
+ if (parsed.kind !== "message") {
179
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
180
+ }
181
+ if (parsed.target !== context.agentId) {
182
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
183
+ }
184
+ const expectedIdentity = wakeAcceptanceIdentity({
185
+ runId,
186
+ agentId,
187
+ eventId: parsed.event_id
188
+ });
189
+ if (parsed.identity !== expectedIdentity) {
190
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
191
+ }
192
+ const expectedDigest = wakeAcceptanceDigest({
193
+ bodySha256: parsed.body_sha256,
194
+ contextId: parsed.context_id,
195
+ eventId: parsed.event_id,
196
+ kind: parsed.kind,
197
+ sender: parsed.sender,
198
+ target: parsed.target
199
+ });
200
+ if (parsed.digest !== expectedDigest) {
201
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
202
+ }
203
+ return parsed;
204
+ });
205
+ let completed = 0;
206
+ let previousSequence = 0;
207
+ const seenSequences = new Set();
208
+ const seenIdentities = new Set();
209
+ for (const record of records) {
210
+ if (record.sequence <= 0 || seenSequences.has(record.sequence)) {
211
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
212
+ }
213
+ if (record.sequence <= previousSequence) {
214
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
215
+ }
216
+ if (seenIdentities.has(record.identity)) {
217
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
218
+ }
219
+ if (record.sequence > nextSequence) {
220
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
221
+ }
222
+ seenSequences.add(record.sequence);
223
+ seenIdentities.add(record.identity);
224
+ if (record.state === "completed") {
225
+ completed += 1;
226
+ }
227
+ previousSequence = record.sequence;
228
+ }
229
+ if (records.length > 0 && previousSequence !== nextSequence) {
230
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
231
+ }
232
+ if (records.length === 0 && nextSequence !== 0) {
233
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
234
+ }
235
+ if (completed > WAKE_ACCEPTANCE_COMPLETED_TOMBSTONES) {
236
+ throw new WakeAcceptanceError("wake_acceptance_store_corrupt");
237
+ }
238
+ return {
239
+ version: WAKE_ACCEPTANCE_VERSION,
240
+ run_id: runId,
241
+ agent_id: agentId,
242
+ next_sequence: nextSequence,
243
+ records
244
+ };
245
+ };
246
+ export const parseWakeAcceptanceState = parseState;
247
+ export const emptyWakeAcceptanceState = (context) => ({
248
+ version: WAKE_ACCEPTANCE_VERSION,
249
+ run_id: context.runId,
250
+ agent_id: context.agentId,
251
+ next_sequence: 0,
252
+ records: []
253
+ });
254
+ export const pruneCompletedRecords = (records) => {
255
+ const active = [...records].filter((record) => record.state !== "completed").sort(canonical);
256
+ const completed = [...records]
257
+ .filter((record) => record.state === "completed")
258
+ .sort(canonical);
259
+ if (completed.length > WAKE_ACCEPTANCE_COMPLETED_TOMBSTONES) {
260
+ const keep = completed.slice(completed.length - WAKE_ACCEPTANCE_COMPLETED_TOMBSTONES);
261
+ return [...active, ...keep].sort(canonical);
262
+ }
263
+ return [...active, ...completed].sort(canonical);
264
+ };
265
+ export const serializeWakeAcceptanceState = (state) => JSON.stringify(state);
@@ -0,0 +1,9 @@
1
+ import type { MemoryWakeMode, WakeMemoryContext } from "@noopolis/mneme";
2
+ import type { WakeEvent } from "../core/types.js";
3
+ export declare const wakeModeForEvent: (event: WakeEvent) => MemoryWakeMode;
4
+ export declare const createAwakeThreadId: (memoryContext: WakeMemoryContext, agentId: string) => string;
5
+ export declare const createDreamSessionKey: (event: WakeEvent) => string;
6
+ export declare const createDreamThreadId: (sessionKey: string) => string;
7
+ export declare const createDreamSessionDirectory: (runtimeHomePath: string, sessionKey: string) => string;
8
+ export declare const formatDreamPrompt: (promptText: string, threadId: string) => string;
9
+ //# sourceMappingURL=wakeModes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wakeModes.d.ts","sourceRoot":"","sources":["../../src/pi/wakeModes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEzE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,eAAO,MAAM,gBAAgB,GAAI,OAAO,SAAS,KAAG,cACR,CAAC;AAE7C,eAAO,MAAM,mBAAmB,GAAI,eAAe,iBAAiB,EAAE,SAAS,MAAM,KAAG,MACU,CAAC;AAEnG,eAAO,MAAM,qBAAqB,GAAI,OAAO,SAAS,KAAG,MACS,CAAC;AAEnE,eAAO,MAAM,mBAAmB,GAAI,YAAY,MAAM,KAAG,MAA+B,CAAC;AAEzF,eAAO,MAAM,2BAA2B,GAAI,iBAAiB,MAAM,EAAE,YAAY,MAAM,KAAG,MAC7B,CAAC;AAE9D,eAAO,MAAM,iBAAiB,GAAI,YAAY,MAAM,EAAE,UAAU,MAAM,KAAG,MAO7D,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import path from "node:path";
3
+ export const wakeModeForEvent = (event) => event.kind === "dream" ? "dream" : "awake";
4
+ export const createAwakeThreadId = (memoryContext, agentId) => `${memoryContext.networkId ?? "local"}:${memoryContext.roomId ?? memoryContext.from ?? agentId}`;
5
+ export const createDreamSessionKey = (event) => `${safeSessionPart(event.id)}-${randomBytes(4).toString("hex")}`;
6
+ export const createDreamThreadId = (sessionKey) => `dream:${sessionKey}`;
7
+ export const createDreamSessionDirectory = (runtimeHomePath, sessionKey) => path.join(runtimeHomePath, "sessions", "dream", sessionKey);
8
+ export const formatDreamPrompt = (promptText, threadId) => [
9
+ "## Dream Mode",
10
+ "",
11
+ "This is a one-off memory consolidation session. Use Mneme tools to audit, summarize, promote, or retire memories with explicit evidence. Do not treat this as a normal chat reply.",
12
+ `dream_thread: ${threadId}`,
13
+ "",
14
+ promptText
15
+ ].join("\n");
16
+ const safeSessionPart = (value) => {
17
+ const normalized = value.trim().replace(/[^A-Za-z0-9_.-]+/gu, "-").replace(/^-+|-+$/gu, "");
18
+ return (normalized || "wake").slice(0, 80);
19
+ };
@@ -0,0 +1,22 @@
1
+ import type { WakeEvent } from "../core/types.js";
2
+ export declare const WORLD_NUDGE_VERSION: "simfile.world-nudge.v1";
3
+ export interface PiWorldTurnContext {
4
+ readonly decisionToken?: string;
5
+ readonly requestId: string;
6
+ readonly runId?: string;
7
+ readonly tick?: number;
8
+ readonly wakeId: string;
9
+ }
10
+ export interface PiWorldToolContextRef {
11
+ current?: PiWorldTurnContext;
12
+ }
13
+ /**
14
+ * Recognizes the versioned world nudge envelope and converts transport
15
+ * authority into a turn-local binding. The opaque token never enters the
16
+ * model prompt or tool schema.
17
+ */
18
+ export declare const worldTurnContext: (event: WakeEvent) => PiWorldTurnContext | undefined;
19
+ /** Creates claimable turn-local identity for every organization-owned wake. */
20
+ export declare const worldWakeContext: (event: WakeEvent) => PiWorldTurnContext;
21
+ export declare const formatWorldWakePrompt: (context: PiWorldTurnContext) => string;
22
+ //# sourceMappingURL=worldNudge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worldNudge.d.ts","sourceRoot":"","sources":["../../src/pi/worldNudge.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,eAAO,MAAM,mBAAmB,EAAG,wBAAiC,CAAC;AAErE,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,qBAAqB;IACpC,OAAO,CAAC,EAAE,kBAAkB,CAAC;CAC9B;AAcD;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,OAAO,SAAS,KAAG,kBAAkB,GAAG,SA0BxE,CAAC;AAEF,+EAA+E;AAC/E,eAAO,MAAM,gBAAgB,GAAI,OAAO,SAAS,KAAG,kBAIhD,CAAC;AAEL,eAAO,MAAM,qBAAqB,GAAI,SAAS,kBAAkB,KAAG,MASxD,CAAC"}
@@ -0,0 +1,62 @@
1
+ import { createHash } from "node:crypto";
2
+ export const WORLD_NUDGE_VERSION = "simfile.world-nudge.v1";
3
+ const exactKeys = (value, expected) => {
4
+ const actual = Object.keys(value).sort();
5
+ return actual.length === expected.length
6
+ && actual.every((key, index) => key === [...expected].sort()[index]);
7
+ };
8
+ const validText = (value, maximum) => typeof value === "string"
9
+ && value.length > 0
10
+ && value.length <= maximum
11
+ && value === value.trim();
12
+ /**
13
+ * Recognizes the versioned world nudge envelope and converts transport
14
+ * authority into a turn-local binding. The opaque token never enters the
15
+ * model prompt or tool schema.
16
+ */
17
+ export const worldTurnContext = (event) => {
18
+ if (event.kind !== "message" || event.delivery === undefined)
19
+ return undefined;
20
+ let parsed;
21
+ try {
22
+ parsed = JSON.parse(event.transportText ?? event.text);
23
+ }
24
+ catch {
25
+ return undefined;
26
+ }
27
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed))
28
+ return undefined;
29
+ const record = parsed;
30
+ if (!exactKeys(record, ["decision_token", "run_id", "tick", "version"])
31
+ || record.version !== WORLD_NUDGE_VERSION
32
+ || !validText(record.decision_token, 512)
33
+ || !validText(record.run_id, 256)
34
+ || !Number.isSafeInteger(record.tick)
35
+ || record.tick < 0)
36
+ return undefined;
37
+ const requestId = `daimon-${createHash("sha256")
38
+ .update(`${event.id}\0${record.decision_token}`)
39
+ .digest("hex")}`;
40
+ return Object.freeze({
41
+ decisionToken: record.decision_token,
42
+ requestId,
43
+ runId: record.run_id,
44
+ tick: record.tick,
45
+ wakeId: event.id
46
+ });
47
+ };
48
+ /** Creates claimable turn-local identity for every organization-owned wake. */
49
+ export const worldWakeContext = (event) => worldTurnContext(event) ?? Object.freeze({
50
+ requestId: `daimon-${createHash("sha256").update(event.id).digest("hex")}`,
51
+ wakeId: event.id,
52
+ });
53
+ export const formatWorldWakePrompt = (context) => [
54
+ context.decisionToken === undefined ? "World-capable organization wake:" : "World decision wake:",
55
+ ...(context.runId === undefined ? [] : [`- run_id: ${context.runId}`]),
56
+ ...(context.tick === undefined ? [] : [`- tick: ${context.tick}`]),
57
+ "",
58
+ context.decisionToken === undefined
59
+ ? "Call world_claim before using the other world tools. The harness keeps authority private."
60
+ : "The harness already bound this wake's authority to the world tools.",
61
+ "Observe current state and perform one allowed action now."
62
+ ].join("\n");
@@ -0,0 +1,13 @@
1
+ import type { PiWorldTurnContext } from "./worldNudge.js";
2
+ export declare const WORLD_ACTION_RESULT_PAGE_REQUEST_VERSION: "simfile.world-action-result-page-request.v1";
3
+ export type PiWorldProtocolOperation = "claim" | "status" | "capabilities" | "observe" | "affordances" | "act" | "ledger";
4
+ export interface ParsedWorldClaim {
5
+ readonly decisionId: string;
6
+ readonly decisionToken: string;
7
+ readonly issuedAtTick: number;
8
+ readonly validThroughTick: number;
9
+ }
10
+ export declare const createWorldClaimRequestBody: (context: PiWorldTurnContext | undefined) => Record<string, unknown> | undefined;
11
+ export declare const createWorldRequestBody: (operation: Exclude<PiWorldProtocolOperation, "claim">, params: Record<string, unknown>, context: PiWorldTurnContext | undefined) => Record<string, unknown> | undefined;
12
+ export declare const parseWorldClaimResponse: (value: unknown) => ParsedWorldClaim | undefined;
13
+ //# sourceMappingURL=worldToolProtocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worldToolProtocol.d.ts","sourceRoot":"","sources":["../../src/pi/worldToolProtocol.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAE1D,eAAO,MAAM,wCAAwC,EACnD,6CAAsD,CAAC;AAEzD,MAAM,MAAM,wBAAwB,GAChC,OAAO,GAAG,QAAQ,GAAG,cAAc,GAAG,SAAS,GAC/C,aAAa,GAAG,KAAK,GAAG,QAAQ,CAAC;AAErC,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;CACnC;AAMD,eAAO,MAAM,2BAA2B,GACtC,SAAS,kBAAkB,GAAG,SAAS,KACtC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAGhB,CAAC;AAEd,eAAO,MAAM,sBAAsB,GACjC,WAAW,OAAO,CAAC,wBAAwB,EAAE,OAAO,CAAC,EACrD,QAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,kBAAkB,GAAG,SAAS,KACtC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAoB5B,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAI,OAAO,OAAO,KAAG,gBAAgB,GAAG,SAe3E,CAAC"}
@@ -0,0 +1,49 @@
1
+ export const WORLD_ACTION_RESULT_PAGE_REQUEST_VERSION = "simfile.world-action-result-page-request.v1";
2
+ const text = (value, maximum = 256) => typeof value === "string" && value.length > 0 && value.length <= maximum
3
+ && value === value.trim();
4
+ export const createWorldClaimRequestBody = (context) => context !== undefined
5
+ && context.decisionToken === undefined && text(context.requestId) && text(context.wakeId)
6
+ ? { request_id: context.requestId, wake_id: context.wakeId }
7
+ : undefined;
8
+ export const createWorldRequestBody = (operation, params, context) => {
9
+ const decisionToken = context?.decisionToken;
10
+ if (context === undefined || !text(decisionToken, 512) || params.decision_token !== undefined
11
+ || params.request_id !== undefined)
12
+ return undefined;
13
+ if (operation === "status" || operation === "capabilities" || operation === "affordances") {
14
+ return { decision_token: decisionToken };
15
+ }
16
+ if (operation === "observe")
17
+ return text(params.sense)
18
+ ? { decision_token: decisionToken, sense: params.sense }
19
+ : undefined;
20
+ if (operation === "act") {
21
+ const requestId = context.requestId;
22
+ if (!text(requestId) || !text(params.affordance) || !text(params.target))
23
+ return undefined;
24
+ return { decision_token: decisionToken, request_id: requestId,
25
+ affordance: params.affordance, target: params.target, input: params.input };
26
+ }
27
+ return { decision_token: decisionToken, version: WORLD_ACTION_RESULT_PAGE_REQUEST_VERSION,
28
+ ...(params.limit === undefined ? {} : { limit: params.limit }),
29
+ ...(params.result_after === undefined ? {} : { result_after: params.result_after }) };
30
+ };
31
+ export const parseWorldClaimResponse = (value) => {
32
+ if (value === null || typeof value !== "object" || Array.isArray(value)
33
+ || Object.getPrototypeOf(value) !== Object.prototype)
34
+ return undefined;
35
+ const keys = Object.keys(value).sort();
36
+ if (keys.length !== 4 || keys.some((key, index) => key !== [
37
+ "decision_id", "decision_token", "issued_at_tick", "valid_through_tick",
38
+ ][index]))
39
+ return undefined;
40
+ const record = value;
41
+ if (!text(record.decision_id) || !text(record.decision_token, 512)
42
+ || !Number.isSafeInteger(record.issued_at_tick) || record.issued_at_tick < 0
43
+ || !Number.isSafeInteger(record.valid_through_tick)
44
+ || record.valid_through_tick < record.issued_at_tick)
45
+ return undefined;
46
+ return Object.freeze({ decisionId: record.decision_id, decisionToken: record.decision_token,
47
+ issuedAtTick: record.issued_at_tick,
48
+ validThroughTick: record.valid_through_tick });
49
+ };
@@ -0,0 +1,32 @@
1
+ import { type ToolDefinition } from "@earendil-works/pi-coding-agent";
2
+ import type { PiWorldToolContextRef } from "./worldNudge.js";
3
+ export declare const PI_WORLD_TOOL_NAMES: readonly ["world_claim", "world_status", "world_capabilities", "world_observe", "world_affordances", "world_act", "world_ledger"];
4
+ export declare const PI_WORLD_TOOL_LIMITS: Readonly<{
5
+ requestBytes: number;
6
+ responseBytes: number;
7
+ timeoutMs: 5000;
8
+ }>;
9
+ export { WORLD_ACTION_RESULT_PAGE_REQUEST_VERSION } from "./worldToolProtocol.js";
10
+ export type PiWorldToolName = typeof PI_WORLD_TOOL_NAMES[number];
11
+ export interface PiWorldBinding {
12
+ readonly url: string;
13
+ readonly tokenEnv: string;
14
+ }
15
+ export type PiWorldFetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
16
+ export interface CreatePiWorldToolsInput {
17
+ readonly world: PiWorldBinding;
18
+ readonly contextRef?: PiWorldToolContextRef;
19
+ readonly fetch?: PiWorldFetch;
20
+ readonly readEnvironment?: (name: string) => string | undefined;
21
+ readonly timeoutMs?: number;
22
+ readonly maxResponseBytes?: number;
23
+ }
24
+ export type PiWorldToolErrorCode = "world_auth_unavailable" | "world_request_cancelled" | "world_request_denied" | "world_request_invalid" | "world_request_rejected" | "world_request_timeout" | "world_response_invalid" | "world_transport_unavailable";
25
+ export declare class PiWorldToolError extends Error {
26
+ readonly code: PiWorldToolErrorCode;
27
+ constructor(code: PiWorldToolErrorCode);
28
+ }
29
+ type PiWorldTool = ToolDefinition<any, unknown>;
30
+ export declare const createPiWorldTools: (input: CreatePiWorldToolsInput) => PiWorldTool[];
31
+ export declare const piWorldToolNames: (tools: PiWorldTool[]) => string[];
32
+ //# sourceMappingURL=worldTools.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worldTools.d.ts","sourceRoot":"","sources":["../../src/pi/worldTools.ts"],"names":[],"mappings":"AAGA,OAAO,EAAc,KAAK,cAAc,EAAE,MAAM,iCAAiC,CAAC;AAElF,OAAO,KAAK,EAAE,qBAAqB,EAAsB,MAAM,iBAAiB,CAAC;AAGjF,eAAO,MAAM,mBAAmB,mIAQrB,CAAC;AACZ,eAAO,MAAM,oBAAoB;;;;EAI/B,CAAC;AACH,OAAO,EAAE,wCAAwC,EAAE,MAAM,wBAAwB,CAAC;AAElF,MAAM,MAAM,eAAe,GAAG,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAC;AACjE,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AACD,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AACpG,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAC/B,QAAQ,CAAC,UAAU,CAAC,EAAE,qBAAqB,CAAC;IAC5C,QAAQ,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC;IAC9B,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;IAChE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;CACpC;AACD,MAAM,MAAM,oBAAoB,GAC5B,wBAAwB,GACxB,yBAAyB,GACzB,sBAAsB,GACtB,uBAAuB,GACvB,wBAAwB,GACxB,uBAAuB,GACvB,wBAAwB,GACxB,6BAA6B,CAAC;AAalC,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,SAAgB,IAAI,EAAE,oBAAoB,CAAC;gBAExB,IAAI,EAAE,oBAAoB;CAK9C;AAED,KAAK,WAAW,GAAG,cAAc,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AA4JhD,eAAO,MAAM,kBAAkB,GAAI,OAAO,uBAAuB,KAAG,WAAW,EAuG9E,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,OAAO,WAAW,EAAE,KAAG,MAAM,EAAoC,CAAC"}