@noopolis/daimon 0.1.0 → 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.
- package/README.md +141 -5
- package/dist/core/siblingBuildFreshness.d.ts +21 -0
- package/dist/core/siblingBuildFreshness.d.ts.map +1 -0
- package/dist/core/siblingBuildFreshness.js +25 -0
- package/dist/core/types.d.ts +20 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/examples/exampleCausalId.d.ts +3 -0
- package/dist/examples/exampleCausalId.d.ts.map +1 -0
- package/dist/examples/exampleCausalId.js +8 -0
- package/dist/examples/jungian-play-org.d.ts +2 -0
- package/dist/examples/jungian-play-org.d.ts.map +1 -0
- package/dist/examples/jungian-play-org.js +281 -0
- package/dist/examples/jungian-triad-org.d.ts +2 -0
- package/dist/examples/jungian-triad-org.d.ts.map +1 -0
- package/dist/examples/jungian-triad-org.js +281 -0
- package/dist/examples/jungianConversationPlan.d.ts +10 -0
- package/dist/examples/jungianConversationPlan.d.ts.map +1 -0
- package/dist/examples/jungianConversationPlan.js +64 -0
- package/dist/examples/jungianPiRepresentative.d.ts +36 -0
- package/dist/examples/jungianPiRepresentative.d.ts.map +1 -0
- package/dist/examples/jungianPiRepresentative.js +81 -0
- package/dist/examples/jungianPlayAgent.d.ts +36 -0
- package/dist/examples/jungianPlayAgent.d.ts.map +1 -0
- package/dist/examples/jungianPlayAgent.js +111 -0
- package/dist/examples/jungianProfiles.d.ts +34 -0
- package/dist/examples/jungianProfiles.d.ts.map +1 -0
- package/dist/examples/jungianProfiles.js +190 -0
- package/dist/examples/jungianTrace.d.ts +47 -0
- package/dist/examples/jungianTrace.d.ts.map +1 -0
- package/dist/examples/jungianTrace.js +101 -0
- package/dist/examples/jungianTriadProfiles.d.ts +35 -0
- package/dist/examples/jungianTriadProfiles.d.ts.map +1 -0
- package/dist/examples/jungianTriadProfiles.js +81 -0
- package/dist/examples/pi-agent.js +3 -2
- package/dist/examples/pi-memory-org.d.ts +2 -0
- package/dist/examples/pi-memory-org.d.ts.map +1 -0
- package/dist/examples/pi-memory-org.js +231 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/mcp/toolServer.d.ts +14 -0
- package/dist/mcp/toolServer.d.ts.map +1 -0
- package/dist/mcp/toolServer.js +105 -0
- package/dist/observability/causalEvents.d.ts +118 -0
- package/dist/observability/causalEvents.d.ts.map +1 -0
- package/dist/observability/causalEvents.js +135 -0
- package/dist/observability/controlCausal.d.ts +117 -0
- package/dist/observability/controlCausal.d.ts.map +1 -0
- package/dist/observability/controlCausal.js +107 -0
- package/dist/observability/emitCausalFixture.d.ts +25 -0
- package/dist/observability/emitCausalFixture.d.ts.map +1 -0
- package/dist/observability/emitCausalFixture.js +98 -0
- package/dist/observability/index.d.ts +4 -0
- package/dist/observability/index.d.ts.map +1 -0
- package/dist/observability/index.js +3 -0
- package/dist/observability/orgObserver.d.ts +98 -0
- package/dist/observability/orgObserver.d.ts.map +1 -0
- package/dist/observability/orgObserver.js +181 -0
- package/dist/pi/cliSession.d.ts +46 -0
- package/dist/pi/cliSession.d.ts.map +1 -0
- package/dist/pi/cliSession.js +224 -0
- package/dist/pi/index.d.ts +9 -0
- package/dist/pi/index.d.ts.map +1 -0
- package/dist/pi/index.js +8 -0
- package/dist/pi/memoryTools.d.ts +69 -0
- package/dist/pi/memoryTools.d.ts.map +1 -0
- package/dist/pi/memoryTools.js +190 -0
- package/dist/pi/modelRegistry.d.ts +13 -0
- package/dist/pi/modelRegistry.d.ts.map +1 -0
- package/dist/pi/modelRegistry.js +19 -0
- package/dist/pi/piAgentHandle.d.ts +45 -0
- package/dist/pi/piAgentHandle.d.ts.map +1 -0
- package/dist/pi/piAgentHandle.js +324 -0
- package/dist/pi/piAgentWakeSupport.d.ts +68 -0
- package/dist/pi/piAgentWakeSupport.d.ts.map +1 -0
- package/dist/pi/piAgentWakeSupport.js +147 -0
- package/dist/pi/piHarness.d.ts +36 -2
- package/dist/pi/piHarness.d.ts.map +1 -1
- package/dist/pi/piHarness.js +100 -127
- package/dist/pi/prompts.d.ts +9 -0
- package/dist/pi/prompts.d.ts.map +1 -0
- package/dist/pi/prompts.js +39 -0
- package/dist/pi/rawTrainingCapture.d.ts +59 -0
- package/dist/pi/rawTrainingCapture.d.ts.map +1 -0
- package/dist/pi/rawTrainingCapture.js +175 -0
- package/dist/pi/turnCausal.d.ts +73 -0
- package/dist/pi/turnCausal.d.ts.map +1 -0
- package/dist/pi/turnCausal.js +75 -0
- package/dist/pi/turnTrace.d.ts +128 -0
- package/dist/pi/turnTrace.d.ts.map +1 -0
- package/dist/pi/turnTrace.js +141 -0
- package/dist/pi/wakeAcceptance.d.ts +49 -0
- package/dist/pi/wakeAcceptance.d.ts.map +1 -0
- package/dist/pi/wakeAcceptance.js +281 -0
- package/dist/pi/wakeAcceptanceFs.d.ts +44 -0
- package/dist/pi/wakeAcceptanceFs.d.ts.map +1 -0
- package/dist/pi/wakeAcceptanceFs.js +318 -0
- package/dist/pi/wakeAcceptanceSchema.d.ts +75 -0
- package/dist/pi/wakeAcceptanceSchema.d.ts.map +1 -0
- package/dist/pi/wakeAcceptanceSchema.js +265 -0
- package/dist/pi/wakeModes.d.ts +9 -0
- package/dist/pi/wakeModes.d.ts.map +1 -0
- package/dist/pi/wakeModes.js +19 -0
- package/dist/pi/worldNudge.d.ts +22 -0
- package/dist/pi/worldNudge.d.ts.map +1 -0
- package/dist/pi/worldNudge.js +62 -0
- package/dist/pi/worldToolProtocol.d.ts +13 -0
- package/dist/pi/worldToolProtocol.d.ts.map +1 -0
- package/dist/pi/worldToolProtocol.js +49 -0
- package/dist/pi/worldTools.d.ts +32 -0
- package/dist/pi/worldTools.d.ts.map +1 -0
- package/dist/pi/worldTools.js +373 -0
- package/dist/pi/worldTrajectory.d.ts +52 -0
- package/dist/pi/worldTrajectory.d.ts.map +1 -0
- package/dist/pi/worldTrajectory.js +131 -0
- package/package.json +23 -4
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { emitTurnInputSubmitted, emitTurnOutputCompleted, resolveRunId, sha256Hex } from "../observability/causalEvents.js";
|
|
2
|
+
import { summarizePrompt } from "./turnTrace.js";
|
|
3
|
+
/**
|
|
4
|
+
* Principal grammar per `specs/CAUSAL.md` §3
|
|
5
|
+
* (`^(agent|operator|system):.+`): the authenticated agent identity this
|
|
6
|
+
* harness instance was started under, never a bare id and never model
|
|
7
|
+
* output. See `stampTurnInputSubmitted`/`stampTurnOutputCompleted` below for
|
|
8
|
+
* why `agentId` itself is trustworthy at this layer.
|
|
9
|
+
*/
|
|
10
|
+
export const agentPrincipalId = (agentId) => `agent:${agentId}`;
|
|
11
|
+
/**
|
|
12
|
+
* Stamps `turn.input.submitted` for one Pi turn, wiring piHarness's own
|
|
13
|
+
* variables into `@noopolis/daimon`'s causal envelope (`../observability/
|
|
14
|
+
* causalEvents.ts`).
|
|
15
|
+
*
|
|
16
|
+
* - `principal_id` is `agent:<agentId>` (`agentPrincipalId`, per the
|
|
17
|
+
* `specs/CAUSAL.md` §3 principal grammar). `src/pi/auth.ts` scopes LLM
|
|
18
|
+
* provider auth (Codex / Claude / API key) per harness instance rather
|
|
19
|
+
* than exposing a separate network identity token, so `agentId` — the
|
|
20
|
+
* identity this harness instance was started under (`AgentStartInput.id`,
|
|
21
|
+
* which also scopes its own `authPath`/`runtimeHomePath`) — is the
|
|
22
|
+
* truthful authenticated identity available at this layer.
|
|
23
|
+
* - `cause_event_ids` chains to `event.id` (the WakeEvent id) plus any mneme
|
|
24
|
+
* recall ids from `prepared.recalledCausalEventIds`. `event.id` is no
|
|
25
|
+
* longer a same-process stand-in for the upstream moltnet
|
|
26
|
+
* `message.accepted` id: moltnet's bridge control POST now carries a real
|
|
27
|
+
* `event_id` (`protocol.MessageEventID`-shaped, `"moltnet:<messageID>"`)
|
|
28
|
+
* for every non-bootstrap wake, and the Pi control source
|
|
29
|
+
* (`src/runtime/pi/appControlSource.ts` `formatControlEventId`) threads
|
|
30
|
+
* that value verbatim into `WakeEvent.id` in preference to its own
|
|
31
|
+
* `context_id`+timestamp fallback. `WakeEvent` still does not carry a
|
|
32
|
+
* separately namespaced moltnet field — Daimon stays detached from
|
|
33
|
+
* Moltnet wiring (see repo `AGENTS.md`) — but `event.id` is now the same
|
|
34
|
+
* id moltnet itself stamped on `message.accepted`, so this chain is
|
|
35
|
+
* id-joined across authorities rather than merely locally consistent.
|
|
36
|
+
* `prepared.recalledCausalEventIds` (not `prepared.recall.selectedEventIds`)
|
|
37
|
+
* for the same reason: `recall.selectedEventIds` are mneme's raw
|
|
38
|
+
* kernel-log recall ids (`evt_<...>`), a different id namespace than the
|
|
39
|
+
* `mneme:<uuid>` ids mneme's own `memory.recalled` causal events are
|
|
40
|
+
* stamped under (`contract/causal.ts` `mnemeCausalEventId`). Chaining the
|
|
41
|
+
* raw recall id would never resolve against mneme's causal stream; the
|
|
42
|
+
* `recalledCausalEventIds` mneme exposes on `MemoryPrepareTurnResult` are
|
|
43
|
+
* the actual `event_id`s of the `memory.recalled` events it appended for
|
|
44
|
+
* this turn, so this cause link is id-joined the same way the moltnet
|
|
45
|
+
* link above is.
|
|
46
|
+
* - `run_id` always comes from `resolveRunId()` (`NOOPOLIS_RUN_ID`), never
|
|
47
|
+
* from `event` or model output.
|
|
48
|
+
*/
|
|
49
|
+
export const stampTurnInputSubmitted = (input) => emitTurnInputSubmitted({
|
|
50
|
+
agentId: input.agentId,
|
|
51
|
+
causeEventIds: [input.event.id, ...(input.prepared?.recalledCausalEventIds ?? [])],
|
|
52
|
+
inputContentSha256: sha256Hex(input.event.text),
|
|
53
|
+
inputMessageIds: [input.event.id],
|
|
54
|
+
principalId: agentPrincipalId(input.agentId),
|
|
55
|
+
promptSha256: summarizePrompt(input.promptText).sha256,
|
|
56
|
+
runId: resolveRunId(),
|
|
57
|
+
runtimeHomePath: input.runtimeHomePath,
|
|
58
|
+
turnId: input.event.id
|
|
59
|
+
});
|
|
60
|
+
/**
|
|
61
|
+
* Stamps `turn.output.completed` once a Pi turn finishes successfully,
|
|
62
|
+
* chained back via `cause_event_ids` to the `turn.input.submitted` id for
|
|
63
|
+
* the same turn. `output_sha256` is over the harness-extracted reply text;
|
|
64
|
+
* the model has no path to influence `cause_event_ids`, `run_id`, or
|
|
65
|
+
* `principal_id` here.
|
|
66
|
+
*/
|
|
67
|
+
export const stampTurnOutputCompleted = (input) => emitTurnOutputCompleted({
|
|
68
|
+
agentId: input.agentId,
|
|
69
|
+
causeEventIds: [input.causeEventId],
|
|
70
|
+
outputSha256: sha256Hex(input.outputText),
|
|
71
|
+
principalId: agentPrincipalId(input.agentId),
|
|
72
|
+
runId: resolveRunId(),
|
|
73
|
+
runtimeHomePath: input.runtimeHomePath,
|
|
74
|
+
turnId: input.turnId
|
|
75
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { MemoryPrepareTurnResult, MemoryWakeMode } from "@noopolis/mneme";
|
|
2
|
+
import type { HarnessModelSpec, WakeEvent } from "../core/types.js";
|
|
3
|
+
export interface PiTurnTraceModel {
|
|
4
|
+
authMethod: NonNullable<HarnessModelSpec["auth"]>["method"];
|
|
5
|
+
model: string;
|
|
6
|
+
provider: string;
|
|
7
|
+
}
|
|
8
|
+
export interface PiTurnTraceToolEvent {
|
|
9
|
+
contentCount?: number;
|
|
10
|
+
decision?: string;
|
|
11
|
+
durationMs?: number;
|
|
12
|
+
error?: string;
|
|
13
|
+
kind: "memory" | "session";
|
|
14
|
+
name: string;
|
|
15
|
+
redactionCount?: number;
|
|
16
|
+
status?: string;
|
|
17
|
+
type?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface PiTurnTraceRecord {
|
|
20
|
+
agent_id: string;
|
|
21
|
+
completed_at: string;
|
|
22
|
+
engine: {
|
|
23
|
+
auth_method: NonNullable<HarnessModelSpec["auth"]>["method"];
|
|
24
|
+
kind: "pi";
|
|
25
|
+
model: string;
|
|
26
|
+
provider: string;
|
|
27
|
+
};
|
|
28
|
+
error?: {
|
|
29
|
+
message: string;
|
|
30
|
+
stage: string;
|
|
31
|
+
};
|
|
32
|
+
memory: {
|
|
33
|
+
enabled: boolean;
|
|
34
|
+
prepare?: {
|
|
35
|
+
duration_ms: number;
|
|
36
|
+
principal?: {
|
|
37
|
+
agent_id: string;
|
|
38
|
+
qualifier?: string;
|
|
39
|
+
scope: string;
|
|
40
|
+
};
|
|
41
|
+
recall?: {
|
|
42
|
+
redaction_count: number;
|
|
43
|
+
selected_count: number;
|
|
44
|
+
token_budget_used: number;
|
|
45
|
+
total_candidates: number;
|
|
46
|
+
};
|
|
47
|
+
status: "completed" | "failed";
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
prompt: {
|
|
51
|
+
chars: number;
|
|
52
|
+
has_active_environment: boolean;
|
|
53
|
+
has_dream_mode: boolean;
|
|
54
|
+
has_memory_context: boolean;
|
|
55
|
+
lines: number;
|
|
56
|
+
sha256: string;
|
|
57
|
+
};
|
|
58
|
+
reply: {
|
|
59
|
+
output_chars: number;
|
|
60
|
+
reply_given: boolean;
|
|
61
|
+
};
|
|
62
|
+
schema: "daimon.turn_trace.v1";
|
|
63
|
+
session: {
|
|
64
|
+
dispose_after_wake: boolean;
|
|
65
|
+
mode: "awake" | "dream";
|
|
66
|
+
thread_id: string;
|
|
67
|
+
};
|
|
68
|
+
started_at: string;
|
|
69
|
+
status: "completed" | "failed";
|
|
70
|
+
timings_ms: {
|
|
71
|
+
engine_prompt?: number;
|
|
72
|
+
memory_prepare?: number;
|
|
73
|
+
total: number;
|
|
74
|
+
};
|
|
75
|
+
tools: PiTurnTraceToolEvent[];
|
|
76
|
+
turn_id: string;
|
|
77
|
+
wake: {
|
|
78
|
+
context?: WakeEvent["context"];
|
|
79
|
+
delivery_authenticated?: boolean;
|
|
80
|
+
event_id: string;
|
|
81
|
+
from?: string;
|
|
82
|
+
kind: WakeEvent["kind"];
|
|
83
|
+
transport_text_present?: boolean;
|
|
84
|
+
world_context_bound?: boolean;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export interface PiMemoryPrepareTraceInput {
|
|
88
|
+
durationMs?: number;
|
|
89
|
+
prepared?: MemoryPrepareTurnResult;
|
|
90
|
+
status: "completed" | "failed";
|
|
91
|
+
}
|
|
92
|
+
export interface BuildPiTurnTraceRecordInput {
|
|
93
|
+
agentId: string;
|
|
94
|
+
completedAt: Date;
|
|
95
|
+
enginePromptMs?: number;
|
|
96
|
+
error?: {
|
|
97
|
+
message: string;
|
|
98
|
+
stage: string;
|
|
99
|
+
};
|
|
100
|
+
event: WakeEvent;
|
|
101
|
+
memoryEnabled: boolean;
|
|
102
|
+
memoryPrepare?: PiMemoryPrepareTraceInput;
|
|
103
|
+
model: PiTurnTraceModel;
|
|
104
|
+
outputText: string;
|
|
105
|
+
promptText: string;
|
|
106
|
+
session: {
|
|
107
|
+
disposeAfterWake: boolean;
|
|
108
|
+
mode: MemoryWakeMode;
|
|
109
|
+
threadId: string;
|
|
110
|
+
};
|
|
111
|
+
startedAt: Date;
|
|
112
|
+
status: "completed" | "failed";
|
|
113
|
+
tools: PiTurnTraceToolEvent[];
|
|
114
|
+
totalMs: number;
|
|
115
|
+
worldContextBound?: boolean;
|
|
116
|
+
}
|
|
117
|
+
export interface PersistPiTurnTraceInput extends Omit<BuildPiTurnTraceRecordInput, "completedAt" | "session"> {
|
|
118
|
+
runtimeHomePath: string;
|
|
119
|
+
session?: BuildPiTurnTraceRecordInput["session"];
|
|
120
|
+
}
|
|
121
|
+
export declare const redactTraceText: (value: unknown) => string;
|
|
122
|
+
export declare const summarizePrompt: (prompt: string) => PiTurnTraceRecord["prompt"];
|
|
123
|
+
export declare const summarizeSessionEvent: (event: unknown) => PiTurnTraceToolEvent | undefined;
|
|
124
|
+
export declare const sanitizeTraceFileId: (value: string) => string;
|
|
125
|
+
export declare const buildPiTurnTraceRecord: (input: BuildPiTurnTraceRecordInput) => PiTurnTraceRecord;
|
|
126
|
+
export declare const writeTurnTraceRecord: (runtimeHomePath: string, record: PiTurnTraceRecord) => Promise<void>;
|
|
127
|
+
export declare const persistPiTurnTrace: (input: PersistPiTurnTraceInput) => Promise<void>;
|
|
128
|
+
//# sourceMappingURL=turnTrace.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"turnTrace.d.ts","sourceRoot":"","sources":["../../src/pi/turnTrace.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,uBAAuB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAE/E,OAAO,KAAK,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAEpE,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,WAAW,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;IAC5D,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,oBAAoB;IACnC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,QAAQ,GAAG,SAAS,CAAC;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE;QACN,WAAW,EAAE,WAAW,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;QAC7D,IAAI,EAAE,IAAI,CAAC;QACX,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC;IACF,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OAAO,CAAC;CAC7B;AAED,MAAM,WAAW,uBAAwB,SAAQ,IAAI,CAAC,2BAA2B,EAAE,aAAa,GAAG,SAAS,CAAC;IAC3G,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,CAAC,EAAE,2BAA2B,CAAC,SAAS,CAAC,CAAC;CAClD;AAED,eAAO,MAAM,eAAe,GAAI,OAAO,OAAO,KAAG,MAWhD,CAAC;AAIF,eAAO,MAAM,eAAe,GAAI,QAAQ,MAAM,KAAG,iBAAiB,CAAC,QAAQ,CAOzE,CAAC;AA+BH,eAAO,MAAM,qBAAqB,GAAI,OAAO,OAAO,KAAG,oBAAoB,GAAG,SAsB7E,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,OAAO,MAAM,KAAG,MAGnD,CAAC;AAEF,eAAO,MAAM,sBAAsB,GAAI,OAAO,2BAA2B,KAAG,iBAuD1E,CAAC;AAEH,eAAO,MAAM,oBAAoB,GAC/B,iBAAiB,MAAM,EACvB,QAAQ,iBAAiB,KACxB,OAAO,CAAC,IAAI,CAOd,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAU,OAAO,uBAAuB,KAAG,OAAO,CAAC,IAAI,CAWrF,CAAC"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { appendFile, mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export const redactTraceText = (value) => {
|
|
5
|
+
let redacted = String(value);
|
|
6
|
+
redacted = redacted.replace(/\bBearer\s+[A-Za-z0-9._~+/=-]{12,}/giu, "Bearer [REDACTED]");
|
|
7
|
+
redacted = redacted.replace(/\bmagt_v1_[A-Za-z0-9_-]{16,}\b/gu, "[REDACTED]");
|
|
8
|
+
redacted = redacted.replace(/\b(?:sk|sk-proj)-[A-Za-z0-9_-]{20,}\b/gu, "[REDACTED]");
|
|
9
|
+
redacted = redacted.replace(/("([^"]*(?:api[_-]?key|token|secret|password)[^"]*)"\s*:\s*")([^"]+)(")/giu, "$1[REDACTED]$4");
|
|
10
|
+
redacted = redacted.replace(/\/(?:Users|home|private|tmp|var|opt|run)\/[^\s"']+/gu, "[path]");
|
|
11
|
+
return redacted.length > 1000 ? `${redacted.slice(0, 1000)}...` : redacted;
|
|
12
|
+
};
|
|
13
|
+
const sha256 = (value) => createHash("sha256").update(value, "utf8").digest("hex");
|
|
14
|
+
export const summarizePrompt = (prompt) => ({
|
|
15
|
+
chars: prompt.length,
|
|
16
|
+
has_active_environment: prompt.includes("Active environment context:"),
|
|
17
|
+
has_dream_mode: prompt.includes("## Dream Mode"),
|
|
18
|
+
has_memory_context: prompt.includes("Memory context") || prompt.includes("# Mneme"),
|
|
19
|
+
lines: prompt.length === 0 ? 0 : prompt.split(/\r?\n/u).length,
|
|
20
|
+
sha256: sha256(prompt)
|
|
21
|
+
});
|
|
22
|
+
const summarizeMemoryPrepare = (input) => ({
|
|
23
|
+
duration_ms: input.durationMs ?? 0,
|
|
24
|
+
...(input.prepared ? {
|
|
25
|
+
principal: {
|
|
26
|
+
agent_id: input.prepared.principal.agentId,
|
|
27
|
+
...(input.prepared.principal.qualifier ? { qualifier: input.prepared.principal.qualifier } : {}),
|
|
28
|
+
scope: input.prepared.principal.scope
|
|
29
|
+
},
|
|
30
|
+
recall: {
|
|
31
|
+
redaction_count: input.prepared.recall.redactionCount,
|
|
32
|
+
selected_count: input.prepared.recall.selectedEventIds.length,
|
|
33
|
+
token_budget_used: input.prepared.recall.tokenBudgetUsed,
|
|
34
|
+
total_candidates: input.prepared.recall.totalCandidates
|
|
35
|
+
}
|
|
36
|
+
} : {}),
|
|
37
|
+
status: input.status
|
|
38
|
+
});
|
|
39
|
+
const asObject = (value) => typeof value === "object" && value !== null ? value : undefined;
|
|
40
|
+
const asString = (value) => typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
|
|
41
|
+
const asNumber = (value) => typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
42
|
+
export const summarizeSessionEvent = (event) => {
|
|
43
|
+
const record = asObject(event);
|
|
44
|
+
const type = asString(record?.type);
|
|
45
|
+
if (!record || !type || type === "turn_end") {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
const tool = asObject(record.tool) ?? asObject(record.toolCall) ?? asObject(record.call);
|
|
49
|
+
const name = asString(record.tool_name) ??
|
|
50
|
+
asString(record.toolName) ??
|
|
51
|
+
asString(record.name) ??
|
|
52
|
+
asString(tool?.name) ??
|
|
53
|
+
"session_event";
|
|
54
|
+
return {
|
|
55
|
+
durationMs: asNumber(record.durationMs) ?? asNumber(record.duration_ms),
|
|
56
|
+
kind: "session",
|
|
57
|
+
name,
|
|
58
|
+
status: asString(record.status),
|
|
59
|
+
type
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
export const sanitizeTraceFileId = (value) => {
|
|
63
|
+
const normalized = value.replace(/[^A-Za-z0-9._-]+/gu, "_").slice(0, 128);
|
|
64
|
+
return normalized.length > 0 ? normalized : "turn";
|
|
65
|
+
};
|
|
66
|
+
export const buildPiTurnTraceRecord = (input) => ({
|
|
67
|
+
agent_id: input.agentId,
|
|
68
|
+
completed_at: input.completedAt.toISOString(),
|
|
69
|
+
engine: {
|
|
70
|
+
auth_method: input.model.authMethod,
|
|
71
|
+
kind: "pi",
|
|
72
|
+
model: input.model.model,
|
|
73
|
+
provider: input.model.provider
|
|
74
|
+
},
|
|
75
|
+
...(input.error ? {
|
|
76
|
+
error: {
|
|
77
|
+
message: redactTraceText(input.error.message),
|
|
78
|
+
stage: input.error.stage
|
|
79
|
+
}
|
|
80
|
+
} : {}),
|
|
81
|
+
memory: {
|
|
82
|
+
enabled: input.memoryEnabled,
|
|
83
|
+
...(input.memoryPrepare ? { prepare: summarizeMemoryPrepare(input.memoryPrepare) } : {})
|
|
84
|
+
},
|
|
85
|
+
prompt: summarizePrompt(input.promptText),
|
|
86
|
+
reply: {
|
|
87
|
+
output_chars: input.outputText.length,
|
|
88
|
+
reply_given: input.outputText.trim().length > 0
|
|
89
|
+
},
|
|
90
|
+
schema: "daimon.turn_trace.v1",
|
|
91
|
+
session: {
|
|
92
|
+
dispose_after_wake: input.session.disposeAfterWake,
|
|
93
|
+
mode: input.session.mode,
|
|
94
|
+
thread_id: input.session.threadId
|
|
95
|
+
},
|
|
96
|
+
started_at: input.startedAt.toISOString(),
|
|
97
|
+
status: input.status,
|
|
98
|
+
timings_ms: {
|
|
99
|
+
...(input.enginePromptMs !== undefined ? { engine_prompt: input.enginePromptMs } : {}),
|
|
100
|
+
...(input.memoryPrepare?.durationMs !== undefined ? { memory_prepare: input.memoryPrepare.durationMs } : {}),
|
|
101
|
+
total: input.totalMs
|
|
102
|
+
},
|
|
103
|
+
tools: input.tools.map((tool) => ({
|
|
104
|
+
...tool,
|
|
105
|
+
...(tool.error ? { error: redactTraceText(tool.error) } : {})
|
|
106
|
+
})),
|
|
107
|
+
turn_id: input.event.id,
|
|
108
|
+
wake: {
|
|
109
|
+
...(input.event.context ? { context: input.event.context } : {}),
|
|
110
|
+
...(input.worldContextBound === undefined
|
|
111
|
+
? {}
|
|
112
|
+
: {
|
|
113
|
+
delivery_authenticated: input.event.delivery !== undefined,
|
|
114
|
+
transport_text_present: typeof input.event.transportText === "string",
|
|
115
|
+
world_context_bound: input.worldContextBound
|
|
116
|
+
}),
|
|
117
|
+
event_id: input.event.id,
|
|
118
|
+
...(input.event.from ? { from: input.event.from } : {}),
|
|
119
|
+
kind: input.event.kind
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
export const writeTurnTraceRecord = async (runtimeHomePath, record) => {
|
|
123
|
+
const telemetryPath = path.join(runtimeHomePath, "telemetry");
|
|
124
|
+
const turnsPath = path.join(telemetryPath, "turns");
|
|
125
|
+
await mkdir(turnsPath, { recursive: true });
|
|
126
|
+
const body = `${JSON.stringify(record, null, 2)}\n`;
|
|
127
|
+
await writeFile(path.join(turnsPath, `${sanitizeTraceFileId(record.turn_id)}.json`), body, "utf8");
|
|
128
|
+
await appendFile(path.join(telemetryPath, "turns.ndjson"), `${JSON.stringify(record)}\n`, "utf8");
|
|
129
|
+
};
|
|
130
|
+
export const persistPiTurnTrace = async (input) => {
|
|
131
|
+
const record = buildPiTurnTraceRecord({
|
|
132
|
+
...input,
|
|
133
|
+
completedAt: new Date(),
|
|
134
|
+
session: input.session ?? {
|
|
135
|
+
disposeAfterWake: false,
|
|
136
|
+
mode: "awake",
|
|
137
|
+
threadId: "unavailable"
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
await writeTurnTraceRecord(input.runtimeHomePath, record);
|
|
141
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { WakeEvent } from "../core/types.js";
|
|
2
|
+
import { type WakeAcceptanceAttempt, type WakeAcceptanceStoreState } from "./wakeAcceptanceSchema.js";
|
|
3
|
+
import { WakeAcceptanceFs } from "./wakeAcceptanceFs.js";
|
|
4
|
+
export type WakeAcceptanceAdmission = {
|
|
5
|
+
mode: "replay";
|
|
6
|
+
} | {
|
|
7
|
+
mode: "run";
|
|
8
|
+
capability: WakeAcceptanceCapability;
|
|
9
|
+
};
|
|
10
|
+
declare const CAPABILITY_BRAND: unique symbol;
|
|
11
|
+
export interface WakeAcceptanceCapability {
|
|
12
|
+
[CAPABILITY_BRAND]: "accepted" | "invoking";
|
|
13
|
+
}
|
|
14
|
+
export interface WakeAcceptanceStoreLike {
|
|
15
|
+
candidateFromDelivery(event: WakeEvent): WakeAcceptanceAttempt;
|
|
16
|
+
begin(event: WakeEvent): Promise<WakeAcceptanceAdmission>;
|
|
17
|
+
markInvoking(capability: WakeAcceptanceCapability): Promise<WakeAcceptanceCapability>;
|
|
18
|
+
markCompleted(capability: WakeAcceptanceCapability): Promise<void>;
|
|
19
|
+
markIncomplete(capability: WakeAcceptanceCapability): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export declare class WakeAcceptanceStore {
|
|
22
|
+
readonly runtimeHomePath: string;
|
|
23
|
+
readonly agentId: string;
|
|
24
|
+
private readonly fs;
|
|
25
|
+
readonly runId: string;
|
|
26
|
+
private readonly capabilities;
|
|
27
|
+
constructor(runtimeHomePath: string, agentId: string, fs?: WakeAcceptanceFs);
|
|
28
|
+
getAcceptanceFilePath(): string;
|
|
29
|
+
candidateFromDelivery(event: WakeEvent): WakeAcceptanceAttempt;
|
|
30
|
+
candidateFromEvent(event: WakeEvent): WakeAcceptanceAttempt;
|
|
31
|
+
begin(event: WakeEvent): Promise<WakeAcceptanceAdmission>;
|
|
32
|
+
private beginFromAttempt;
|
|
33
|
+
markInvoking(capability: WakeAcceptanceCapability): Promise<WakeAcceptanceCapability>;
|
|
34
|
+
markCompleted(capability: WakeAcceptanceCapability): Promise<void>;
|
|
35
|
+
markIncomplete(capability: WakeAcceptanceCapability): Promise<void>;
|
|
36
|
+
loadState(): Promise<WakeAcceptanceStoreState>;
|
|
37
|
+
private loadStateUnsafe;
|
|
38
|
+
private makeRecord;
|
|
39
|
+
private makeError;
|
|
40
|
+
private canRelease;
|
|
41
|
+
private withClaim;
|
|
42
|
+
private createCapability;
|
|
43
|
+
private consumeCapability;
|
|
44
|
+
private transitionAttempt;
|
|
45
|
+
private persistState;
|
|
46
|
+
private nextSequence;
|
|
47
|
+
}
|
|
48
|
+
export { WAKE_ACCEPTANCE_VERSION, WakeAcceptanceError, type WakeAcceptanceAttempt, type WakeAcceptanceRecord, type WakeAcceptanceState, type WakeAcceptanceStoreState } from "./wakeAcceptanceSchema.js";
|
|
49
|
+
//# sourceMappingURL=wakeAcceptance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wakeAcceptance.d.ts","sourceRoot":"","sources":["../../src/pi/wakeAcceptance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAEL,KAAK,qBAAqB,EAG1B,KAAK,wBAAwB,EAQ9B,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,gB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