Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
701 changes: 0 additions & 701 deletions ATTRIBUTIONS-Node.md

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions integrations/openclaw/src/hook-replay/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ function replayLlmOutput(params: {
const metadata = toJsonRecord({
source,
runId: event.runId,
sessionId: event.sessionId,
sessionId: session.sessionId,
sessionKey: session.sessionKey,
agentId: session.agentId,
provider: event.provider,
model: event.model,
callId: timing?.callId,
Expand Down Expand Up @@ -791,7 +793,15 @@ function existingSessionForMessageWrite(
const key = resolveSessionKey(manager.state, {
sessionKey: event.sessionKey ?? ctx.sessionKey,
});
return key === undefined ? undefined : manager.state.sessions.get(key);
if (key === undefined) {
return undefined;
}
const session = manager.state.sessions.get(key);
const sessionKey = event.sessionKey ?? ctx.sessionKey;
if (session && session.sessionKey === undefined && sessionKey !== undefined) {
session.sessionKey = sessionKey;
}
return session;
}

/** Build a minimal request placeholder when only an llm_output hook is available. */
Expand Down
3 changes: 2 additions & 1 deletion integrations/openclaw/src/hook-replay/marks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ export function emitMark(params: {
session: SessionState;
name: string;
data: JsonRecord;
metadata?: JsonRecord | null;
timestamp?: number;
}): void {
if (!params.session.rootHandle) {
params.state.counters.skippedEvents += 1;
return;
}

params.nf.event(params.name, params.session.rootHandle, params.data, null, params.timestamp ?? null);
params.nf.event(params.name, params.session.rootHandle, params.data, params.metadata ?? null, params.timestamp ?? null);
params.state.counters.marksEmitted += 1;
}

Expand Down
33 changes: 29 additions & 4 deletions integrations/openclaw/src/hook-replay/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
import type { PluginLogger } from 'openclaw/plugin-sdk/plugin-entry';
import type { JsonObject as JsonRecord } from 'nemo-relay-node/typed';
import type { NemoRelayRuntimeModule } from '../modules.js';
import { toJsonRecord } from './marks.js';

export type SessionLookupInput = {
sessionId?: string | undefined;
Expand Down Expand Up @@ -213,13 +214,15 @@ export function ensureSession(manager: SessionManager, input: EnsureSessionInput

const existing = manager.state.sessions.get(key);
if (existing) {
enrichSession(existing, input);
rememberSessionAliases(manager.state, existing, input);
return existing;
}

const canonicalSessionId = input.sessionId ?? key;
const aliased = manager.state.sessions.get(canonicalSessionId);
if (aliased) {
enrichSession(aliased, input);
rememberSessionAliases(manager.state, aliased, input);
return aliased;
}
Expand Down Expand Up @@ -247,6 +250,19 @@ export function ensureSession(manager: SessionManager, input: EnsureSessionInput
return session;
}

/** Fill stable session identifiers from later hooks without clobbering established values. */
function enrichSession(session: SessionState, input: EnsureSessionInput): void {
if (session.sessionKey === undefined && input.sessionKey !== undefined) {
session.sessionKey = input.sessionKey;
}
if (session.agentId === undefined && input.agentId !== undefined) {
session.agentId = input.agentId;
}
if (session.resumedFrom === undefined && input.resumedFrom !== undefined) {
session.resumedFrom = input.resumedFrom;
}
}

/** Flush pending LLM output/timing state before the root session closes. */
export function drainSession(manager: SessionManager, session: SessionState): void {
cancelPendingLlmOutputTimers(manager.state, session);
Expand All @@ -261,14 +277,15 @@ export function closeSessionRoot(
session: SessionState,
summary: JsonRecord,
rootOutput: JsonRecord = summary,
metadata?: JsonRecord | null,
timestamp?: number,
): void {
manager.emitCapturedUnderSession('session_end', session, () => {
if (!session.rootHandle) {
return;
}

manager.nf.event('openclaw.session_end', session.rootHandle, summary, null, timestamp ?? null);
manager.nf.event('openclaw.session_end', session.rootHandle, summary, metadata ?? null, timestamp ?? null);
manager.state.counters.marksEmitted += 1;
manager.nf.popScope(session.rootHandle, rootOutput, timestamp ?? null);
delete session.rootHandle;
Expand Down Expand Up @@ -313,6 +330,14 @@ function openSessionRoot(manager: SessionManager, session: SessionState, input:
...(input.runId === undefined ? {} : { runId: input.runId }),
...(session.resumedFrom === undefined ? {} : { resumedFrom: session.resumedFrom }),
};
const metadata = toJsonRecord({
source: session.source === 'session_start' ? 'openclaw.session_start' : 'openclaw.lazy_session',
hook_event_name: session.source === 'session_start' ? 'session_start' : undefined,
sessionId: session.sessionId,
sessionKey: session.sessionKey,
agentId: session.agentId,
runId: input.runId,
});

manager.emitCapturedUnderSession('session_start', session, () => {
session.rootHandle = manager.nf.pushScope(
Expand All @@ -321,11 +346,11 @@ function openSessionRoot(manager: SessionManager, session: SessionState, input:
null,
null,
data,
null,
null,
metadata,
data,
input.timestamp ?? null,
);
manager.nf.event('openclaw.session_start', session.rootHandle, data, null, input.timestamp ?? null);
manager.nf.event('openclaw.session_start', session.rootHandle, data, metadata, input.timestamp ?? null);
manager.state.counters.marksEmitted += 1;
});
}
Expand Down
14 changes: 12 additions & 2 deletions integrations/openclaw/src/hook-replay/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ export function replayAfterToolCall(
session,
name: 'openclaw.tool_blocked',
data: blockedDetails,
metadata: toJsonRecord({
source: 'openclaw.after_tool_call',
hook_event_name: 'after_tool_call',
sessionId: session.sessionId,
sessionKey: session.sessionKey,
agentId: session.agentId,
runId: event.runId ?? ctx.runId,
toolCallId: event.toolCallId ?? ctx.toolCallId,
}),
});
});
return;
Expand All @@ -80,8 +89,9 @@ export function replayAfterToolCall(
const metadata = toJsonRecord({
source: 'openclaw.after_tool_call',
runId: event.runId ?? ctx.runId,
sessionId: ctx.sessionId,
sessionKey: ctx.sessionKey,
sessionId: session.sessionId,
sessionKey: session.sessionKey,
agentId: session.agentId,
toolCallId: event.toolCallId ?? ctx.toolCallId,
durationMs: event.durationMs,
});
Expand Down
24 changes: 21 additions & 3 deletions integrations/openclaw/src/hooks-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,17 @@ export class HookReplayBackend {
return;
}

await this.closeSession(session, sessionEndSummary(event));
await this.closeSession(
session,
sessionEndSummary(event),
toJsonRecord({
source: 'openclaw.session_end',
hook_event_name: 'session_end',
sessionId: session.sessionId,
sessionKey: event.sessionKey ?? ctx.sessionKey,
agentId: ctx.agentId,
}),
);
}

/** Buffer an LLM request snapshot until a matching response or trajectory replay arrives. */
Expand Down Expand Up @@ -373,9 +383,9 @@ export class HookReplayBackend {
}

/** Drain, close, export, and delete one session. */
private async closeSession(session: SessionState, summary: JsonRecord): Promise<void> {
private async closeSession(session: SessionState, summary: JsonRecord, metadata?: JsonRecord): Promise<void> {
drainSession(this.sessionManager(), session);
closeSessionRoot(this.sessionManager(), session, summary, session.finalOutput ?? summary);
closeSessionRoot(this.sessionManager(), session, summary, session.finalOutput ?? summary, metadata);
this.flushSubscriberDelivery('session_close');
deleteSession(this.stateValue, session);
}
Expand All @@ -389,6 +399,14 @@ export class HookReplayBackend {
session,
name,
data,
metadata: toJsonRecord({
source: name,
hook_event_name: name.startsWith('openclaw.') ? name.slice('openclaw.'.length) : undefined,
sessionId: session.sessionId,
sessionKey: session.sessionKey,
agentId: session.agentId,
runId: typeof data.runId === 'string' ? data.runId : undefined,
}),
});
});
}
Expand Down
Loading
Loading