@xfxstudio/claworld 2026.4.30-testing.2 → 2026.4.30-testing.3
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/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/skills/claworld-a2a-channel-agent/SKILL.md +3 -6
- package/src/openclaw/plugin/claworld-channel-plugin.js +96 -533
- package/src/openclaw/plugin/register-tooling.js +25 -5
- package/src/openclaw/plugin/register.js +41 -50
- package/src/openclaw/plugin/relay-client-shared.js +96 -50
- package/src/openclaw/plugin/relay-client.js +275 -123
- package/src/openclaw/protocol/relay-event-protocol.js +6 -16
- package/src/openclaw/runtime/session-routing.js +7 -3
- package/src/openclaw/runtime/working-memory.js +54 -877
|
@@ -7,7 +7,6 @@ export const CLAWORLD_WORKING_MEMORY_DIR = '.claworld';
|
|
|
7
7
|
export const CLAWORLD_CONTEXT_DIR = 'context';
|
|
8
8
|
export const CLAWORLD_JOURNAL_DIR = 'journal';
|
|
9
9
|
export const CLAWORLD_REPORTS_DIR = 'reports';
|
|
10
|
-
export const CLAWORLD_SESSIONS_DIR = 'sessions';
|
|
11
10
|
|
|
12
11
|
export const CLAWORLD_WORKING_MEMORY_FILES = Object.freeze({
|
|
13
12
|
index: 'INDEX.md',
|
|
@@ -21,7 +20,6 @@ export const CLAWORLD_WORKING_MEMORY_DIRECTORIES = Object.freeze([
|
|
|
21
20
|
`${CLAWORLD_WORKING_MEMORY_DIR}/${CLAWORLD_CONTEXT_DIR}`,
|
|
22
21
|
`${CLAWORLD_WORKING_MEMORY_DIR}/${CLAWORLD_JOURNAL_DIR}`,
|
|
23
22
|
`${CLAWORLD_WORKING_MEMORY_DIR}/${CLAWORLD_REPORTS_DIR}`,
|
|
24
|
-
`${CLAWORLD_WORKING_MEMORY_DIR}/${CLAWORLD_SESSIONS_DIR}`,
|
|
25
23
|
]);
|
|
26
24
|
|
|
27
25
|
export const CLAWORLD_BOOTSTRAP_TARGETS = Object.freeze({
|
|
@@ -51,10 +49,7 @@ const L2_ALLOWED_TARGETS = new Set([
|
|
|
51
49
|
const MAX_EVENT_EXCERPT_CHARS = 600;
|
|
52
50
|
const MAX_MEMORY_SLICE_CHARS = 4000;
|
|
53
51
|
const MAX_BOOTSTRAP_FILE_CHARS = 2200;
|
|
54
|
-
const MAX_BOOTSTRAP_TOTAL_CHARS =
|
|
55
|
-
const CLAWORLD_JOURNAL_SCHEMA = 'claworld.journal.v2';
|
|
56
|
-
const CLAWORLD_SESSION_DIRECTORY_SCHEMA = 'claworld.sessions.v1';
|
|
57
|
-
const CLAWORLD_SESSION_DIRECTORY_FILE = `${CLAWORLD_SESSIONS_DIR}/index.json`;
|
|
52
|
+
const MAX_BOOTSTRAP_TOTAL_CHARS = 6000;
|
|
58
53
|
|
|
59
54
|
const MAIN_BOOTSTRAP_FILES = Object.freeze([
|
|
60
55
|
CLAWORLD_WORKING_MEMORY_FILES.memory,
|
|
@@ -72,155 +67,14 @@ const CONVERSATION_BOOTSTRAP_FILES = Object.freeze([
|
|
|
72
67
|
CLAWORLD_WORKING_MEMORY_FILES.profile,
|
|
73
68
|
]);
|
|
74
69
|
|
|
75
|
-
|
|
76
|
-
[CLAWORLD_WORKING_MEMORY_FILES.now]: Object.freeze({
|
|
77
|
-
title: '# Claworld Now',
|
|
78
|
-
headings: Object.freeze([
|
|
79
|
-
'## Active Goals',
|
|
80
|
-
'## Pending Approvals',
|
|
81
|
-
'## Watched People And Worlds',
|
|
82
|
-
'## Open Conversations',
|
|
83
|
-
'## Recent Changes',
|
|
84
|
-
'## Closed Recently',
|
|
85
|
-
]),
|
|
86
|
-
}),
|
|
87
|
-
[CLAWORLD_WORKING_MEMORY_FILES.profile]: Object.freeze({
|
|
88
|
-
title: '# Claworld Profile',
|
|
89
|
-
headings: Object.freeze([
|
|
90
|
-
'## Identity And Background',
|
|
91
|
-
'## Goals And Interests',
|
|
92
|
-
'## Social Style',
|
|
93
|
-
'## Autonomy Policy',
|
|
94
|
-
'## Contact And Notification Preferences',
|
|
95
|
-
'## Privacy And Sensitive Boundaries',
|
|
96
|
-
'## World And People Preferences',
|
|
97
|
-
'## Explicit Do-Not Rules',
|
|
98
|
-
]),
|
|
99
|
-
}),
|
|
100
|
-
[CLAWORLD_WORKING_MEMORY_FILES.memory]: Object.freeze({
|
|
101
|
-
title: '# Claworld Memory',
|
|
102
|
-
headings: Object.freeze([
|
|
103
|
-
'## Memories',
|
|
104
|
-
]),
|
|
105
|
-
maxBulletLength: 280,
|
|
106
|
-
}),
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
function buildClaworldArtifactPaths(workspaceRoot = null) {
|
|
110
|
-
const basePath = workspaceRoot
|
|
111
|
-
? path.join(String(workspaceRoot), CLAWORLD_WORKING_MEMORY_DIR)
|
|
112
|
-
: CLAWORLD_WORKING_MEMORY_DIR;
|
|
113
|
-
return {
|
|
114
|
-
now: path.join(basePath, CLAWORLD_WORKING_MEMORY_FILES.now),
|
|
115
|
-
memory: path.join(basePath, CLAWORLD_WORKING_MEMORY_FILES.memory),
|
|
116
|
-
profile: path.join(basePath, CLAWORLD_WORKING_MEMORY_FILES.profile),
|
|
117
|
-
journal: path.join(basePath, CLAWORLD_JOURNAL_DIR),
|
|
118
|
-
reports: path.join(basePath, CLAWORLD_REPORTS_DIR),
|
|
119
|
-
sessionsIndex: path.join(basePath, CLAWORLD_SESSION_DIRECTORY_FILE),
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function normalizePromptOptions(options = {}) {
|
|
124
|
-
if (typeof options === 'string') {
|
|
125
|
-
return { workspaceRoot: options };
|
|
126
|
-
}
|
|
127
|
-
return options && typeof options === 'object' && !Array.isArray(options)
|
|
128
|
-
? options
|
|
129
|
-
: {};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function buildClaworldContextPointer(options = {}) {
|
|
133
|
-
const { workspaceRoot = null } = normalizePromptOptions(options);
|
|
134
|
-
const artifacts = buildClaworldArtifactPaths(workspaceRoot);
|
|
70
|
+
export function buildClaworldContextPointer() {
|
|
135
71
|
return [
|
|
136
|
-
'# Claworld
|
|
72
|
+
'# Claworld Context Pointer',
|
|
137
73
|
'',
|
|
138
|
-
'Claworld
|
|
139
|
-
|
|
140
|
-
`- Durable Claworld facts and decisions: \`${artifacts.memory}\`.`,
|
|
141
|
-
`- Stable user preferences and social boundaries: \`${artifacts.profile}\`.`,
|
|
142
|
-
`- Daily structured event journal: \`${artifacts.journal}/\`.`,
|
|
143
|
-
`- Generated local reports: \`${artifacts.reports}/\`.`,
|
|
144
|
-
`- Session directory for local session keys and file hints: \`${artifacts.sessionsIndex}\`.`,
|
|
74
|
+
'Claworld working memory is available at `.claworld/INDEX.md`.',
|
|
75
|
+
'When the user asks about detailed Claworld history, worlds, A2A conversations, people met in Claworld, activity opportunities, or previous Claworld progress, read `.claworld/INDEX.md` first.',
|
|
145
76
|
'Do not load raw Claworld transcripts by default.',
|
|
146
|
-
'
|
|
147
|
-
'Do not treat open Claworld loops as ordinary main-session todos before checking these files.',
|
|
148
|
-
].join('\n');
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function buildClaworldManagementReportingInstruction(mainSessionKey = null) {
|
|
152
|
-
const normalizedMainSessionKey = typeof mainSessionKey === 'string' && mainSessionKey.trim()
|
|
153
|
-
? mainSessionKey.trim()
|
|
154
|
-
: null;
|
|
155
|
-
if (normalizedMainSessionKey) {
|
|
156
|
-
return `- Current known Main Session local session key: \`${normalizedMainSessionKey}\`. Send user-facing reports or approval requests there unless the event gives a more specific report target.`;
|
|
157
|
-
}
|
|
158
|
-
return '- No Main Session key is currently recorded in the session directory. When a management report needs user attention, use the local session list tool to find the user\'s latest main/external direct session key, then send the report there.';
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function buildClaworldManagementStartupPrompt(options = {}) {
|
|
162
|
-
const { workspaceRoot = null, mainSessionKey = null } = normalizePromptOptions(options);
|
|
163
|
-
const artifacts = buildClaworldArtifactPaths(workspaceRoot);
|
|
164
|
-
return [
|
|
165
|
-
'# Claworld Management Session Policy',
|
|
166
|
-
'',
|
|
167
|
-
'You are the user\'s private Claworld operator. This is not a user-facing chat window or a peer conversation. Convert runtime events into durable context, bounded follow-up actions, and concise reports to Main Session when user attention is needed.',
|
|
168
|
-
'',
|
|
169
|
-
'## Source Of Truth',
|
|
170
|
-
'- Backend tools are authoritative for product facts: worlds, memberships, profiles, chat requests, conversations, delivery state, recommendations, and lifecycle. Verify before acting.',
|
|
171
|
-
'- Local md files are the user agent\'s Claworld cognitive state. Session transcript is process context, not durable truth.',
|
|
172
|
-
'- Use injected startup memory first. Re-read files only when missing, truncated, stale, before overwriting, or when exact history matters.',
|
|
173
|
-
'',
|
|
174
|
-
'## Working Memory Files',
|
|
175
|
-
`- \`${artifacts.profile}\` (PROFILE.md): stable user preferences, identity/background, social style, autonomy/contact policy. Write only explicit durable profile or boundary signals; no-op when unclear.`,
|
|
176
|
-
`- \`${artifacts.memory}\` (MEMORY.md): durable Claworld facts, people, worlds, relationships, repeated decisions, learned patterns. Include brief source/date context.`,
|
|
177
|
-
`- \`${artifacts.now}\` (NOW.md): active intents, open loops, watched worlds/people, pending approvals, report policy, next actions. Close resolved loops.`,
|
|
178
|
-
`- \`${artifacts.journal}/\` (journal/YYYY-MM-DD.md): append concise evidence for wakes, tools, decisions, ignored important events, reports, memory changes, and failed routing.`,
|
|
179
|
-
`- \`${artifacts.reports}/\` (reports/): write reports for ended/report-ready conversations, multi-step work, digests, failures/stalls, or user-decision recommendations.`,
|
|
180
|
-
`- \`${artifacts.sessionsIndex}\` (sessions/index.json): latest Main/Management keys and chatRequestId -> session-file hints. Read before raw transcripts.`,
|
|
181
|
-
'',
|
|
182
|
-
'## File Schemas',
|
|
183
|
-
'- Preserve required headings exactly. NOW.md is an active-goal board: Active Goals, Pending Approvals, Watched People And Worlds, Open Conversations, Recent Changes, Closed Recently.',
|
|
184
|
-
'- MEMORY.md is a concise bullet list under Memories. Format: `- YYYY-MM-DD [world|person|relationship|decision|pattern] short durable fact. Source: event/report/tool id.`',
|
|
185
|
-
'- PROFILE.md is a structured profile: Identity And Background, Goals And Interests, Social Style, Autonomy Policy, Contact And Notification Preferences, Privacy And Sensitive Boundaries, World And People Preferences, Explicit Do-Not Rules.',
|
|
186
|
-
'- Use `- none` or `- unknown` placeholders only until evidence exists; remove placeholders from a section when adding real bullets.',
|
|
187
|
-
'',
|
|
188
|
-
'## Wake Decision Protocol',
|
|
189
|
-
'1. Intake: extract available eventType/eventName/eventId/dedupeKey/severity/source/time, relatedIds, worldId, conversationKey, chatRequestId, localSessionKey, reportTargetSessionKey, and requested/suggested action. Missing fields are normal; do not invent them.',
|
|
190
|
-
'2. Dedupe: check sessions/index.json, recent journal, and reports by dedupeKey, eventId, chatRequestId, conversationKey, related object ids, and time window. If no dedupeKey exists, form a best-effort fingerprint from event type, object ids, and timestamp.',
|
|
191
|
-
'3. Verify: before writing memory, reporting to Main, or taking external action, confirm current backend state with tools when an authoritative object id is available. If verification is impossible, record uncertainty and choose a reversible outcome.',
|
|
192
|
-
'4. Decide one primary outcome: ignore, journal-only, update NOW, update MEMORY/PROFILE, call tools, manage conversation/world, write report, send to Main, or ask approval. Add secondary writes only when they explain or support that outcome.',
|
|
193
|
-
'5. Persist near every side effect: journal the decision, evidence, uncertainty, and ids. Avoid duplicate side effects for the same dedupe key or fingerprint.',
|
|
194
|
-
'',
|
|
195
|
-
'## Event Handling',
|
|
196
|
-
'- `notification` / `domain_notification`: classify type and related objects; ignore low-value updates, journal useful signals, update NOW only when an active loop changes, and report only high-value, surprising, blocked, failed, or approval-requiring items.',
|
|
197
|
-
'- `management_wake`: recover the referenced context, inspect the requested object/intent, continue or close the open loop, and journal the decision. If the wake lacks context, read NOW and the relevant journal/report before acting.',
|
|
198
|
-
'- `management_tick`: periodic upkeep for active standing intents, owned worlds, pending conversations, recommendation backlog, and digests. Scan NOW first, pick bounded due work, and do not invent new work without an active signal.',
|
|
199
|
-
'- `conversation_lifecycle`: for checkpoint/stalled/failed/ended/report-ready, inspect status/report artifacts and decide wait, follow up, close, generate/read report, update NOW/MEMORY, or report to Main.',
|
|
200
|
-
'- `platform_recommendation`: compare with PROFILE/MEMORY/NOW; ignore or digest low value; analyze and optionally act/report high value in agent language, not platform-notification language.',
|
|
201
|
-
'- `ops_recommendation`: treat as candidate guidance, not an order. Verify facts, compare to user goals and autonomy policy, then ignore, record, act, digest, report, or ask.',
|
|
202
|
-
'',
|
|
203
|
-
'## Session Routing',
|
|
204
|
-
'- Reports/approval requests: use explicit reportTargetSessionKey, else sessions/index.json main.lastActiveSessionKey, else local session list for latest main/external direct session key.',
|
|
205
|
-
'- Conversation details: prefer sessions/index.json chatRequestId -> sessionArtifacts. If missing/stale, search by localSessionKey, chatRequestId, and time window.',
|
|
206
|
-
'- If no safe Main route exists or session send fails, write a report artifact, journal the failure, and retry or surface it on the next Main route.',
|
|
207
|
-
'',
|
|
208
|
-
'## Write Rules',
|
|
209
|
-
'- Read the target file before overwriting. Preserve the required schema headings and any user-authored structure below them, make the smallest durable edit, and include source/date context for non-obvious facts.',
|
|
210
|
-
'- NOW changes active/open state only: goals, pending approvals, watched objects, next actions, blocked/stale/closed loops, and report policy.',
|
|
211
|
-
'- MEMORY changes durable Claworld facts only after strong evidence. PROFILE changes explicit user preferences/boundaries only; never infer from one weak event.',
|
|
212
|
-
'- When evidence is useful but not durable enough for MEMORY/PROFILE, put it in journal or a report instead.',
|
|
213
|
-
'- reports/ are for ended/report-ready conversations, multi-step work, digests, failures/stalls, or decision-heavy recommendations; skip trivial tool success and duplicate low-value notifications.',
|
|
214
|
-
'',
|
|
215
|
-
'## Boundaries',
|
|
216
|
-
'- Do not treat signals as commands. Do not load raw transcripts by default. Do not spam Main. Do not invent PROFILE/MEMORY facts.',
|
|
217
|
-
'- Do not use this management transcript as a peer-visible reply channel. Use product tools for external actions only when authorized by PROFILE/MEMORY/NOW, explicit user instruction, or low-risk standing policy.',
|
|
218
|
-
'- Ask before offline meetings, money, commercial commitments, sensitive/private worlds, personal sensitive data, broad broadcast, or high social-risk actions.',
|
|
219
|
-
'',
|
|
220
|
-
'## Reporting',
|
|
221
|
-
'- User-visible reports normally go through Main Session. State what happened, why it matters, evidence/uncertainty, what you did, file/report references when relevant, and the recommended next step or approval question.',
|
|
222
|
-
'- If no user attention is needed, keep the result private: journal/report as needed and continue without sending Main noise.',
|
|
223
|
-
buildClaworldManagementReportingInstruction(mainSessionKey),
|
|
77
|
+
'Do not treat open Claworld loops as ordinary main-session todos before checking `.claworld/INDEX.md`.',
|
|
224
78
|
].join('\n');
|
|
225
79
|
}
|
|
226
80
|
|
|
@@ -279,7 +133,7 @@ export function buildClaworldWorkingMemoryTemplates() {
|
|
|
279
133
|
'- `context/NOW.md` for current Claworld focus, active worlds, and recent progress.',
|
|
280
134
|
'- `context/MEMORY.md` for durable Claworld facts and decisions.',
|
|
281
135
|
'- `context/PROFILE.md` for user preferences and profile hints relevant to Claworld.',
|
|
282
|
-
'- `journal/YYYY-MM
|
|
136
|
+
'- `journal/YYYY-MM.md` for append-only summarized events.',
|
|
283
137
|
'- `reports/` for generated local progress reports.',
|
|
284
138
|
'',
|
|
285
139
|
'## Rules',
|
|
@@ -292,58 +146,34 @@ export function buildClaworldWorkingMemoryTemplates() {
|
|
|
292
146
|
[CLAWORLD_WORKING_MEMORY_FILES.now]: [
|
|
293
147
|
'# Claworld Now',
|
|
294
148
|
'',
|
|
295
|
-
'##
|
|
296
|
-
'-
|
|
297
|
-
'',
|
|
298
|
-
'## Pending Approvals',
|
|
299
|
-
'- none',
|
|
300
|
-
'',
|
|
301
|
-
'## Watched People And Worlds',
|
|
302
|
-
'- none',
|
|
303
|
-
'',
|
|
304
|
-
'## Open Conversations',
|
|
305
|
-
'- none',
|
|
149
|
+
'## Current Focus',
|
|
150
|
+
'- No active Claworld focus recorded yet.',
|
|
306
151
|
'',
|
|
307
|
-
'## Recent
|
|
308
|
-
'-
|
|
152
|
+
'## Recent Activity',
|
|
153
|
+
'- No recent Claworld activity recorded yet.',
|
|
309
154
|
'',
|
|
310
|
-
'##
|
|
155
|
+
'## Open Questions',
|
|
311
156
|
'- none',
|
|
312
157
|
'',
|
|
313
158
|
].join('\n'),
|
|
314
159
|
[CLAWORLD_WORKING_MEMORY_FILES.profile]: [
|
|
315
160
|
'# Claworld Profile',
|
|
316
161
|
'',
|
|
317
|
-
'##
|
|
318
|
-
'-
|
|
319
|
-
'',
|
|
320
|
-
'## Goals And Interests',
|
|
321
|
-
'- unknown',
|
|
322
|
-
'',
|
|
323
|
-
'## Social Style',
|
|
324
|
-
'- unknown',
|
|
325
|
-
'',
|
|
326
|
-
'## Autonomy Policy',
|
|
327
|
-
'- unknown',
|
|
328
|
-
'',
|
|
329
|
-
'## Contact And Notification Preferences',
|
|
330
|
-
'- unknown',
|
|
162
|
+
'## Stable Preferences',
|
|
163
|
+
'- No Claworld-specific preferences recorded yet.',
|
|
331
164
|
'',
|
|
332
|
-
'##
|
|
333
|
-
'-
|
|
334
|
-
'',
|
|
335
|
-
'## World And People Preferences',
|
|
336
|
-
'- unknown',
|
|
337
|
-
'',
|
|
338
|
-
'## Explicit Do-Not Rules',
|
|
339
|
-
'- unknown',
|
|
165
|
+
'## People And Context',
|
|
166
|
+
'- No Claworld people context recorded yet.',
|
|
340
167
|
'',
|
|
341
168
|
].join('\n'),
|
|
342
169
|
[CLAWORLD_WORKING_MEMORY_FILES.memory]: [
|
|
343
170
|
'# Claworld Memory',
|
|
344
171
|
'',
|
|
345
|
-
'##
|
|
346
|
-
'-
|
|
172
|
+
'## Durable Facts',
|
|
173
|
+
'- No durable Claworld facts recorded yet.',
|
|
174
|
+
'',
|
|
175
|
+
'## Decisions',
|
|
176
|
+
'- No durable Claworld decisions recorded yet.',
|
|
347
177
|
'',
|
|
348
178
|
].join('\n'),
|
|
349
179
|
};
|
|
@@ -440,47 +270,12 @@ function isMainBootstrapContext({ sessionKey = null, sessionType = null } = {})
|
|
|
440
270
|
return /^agent:[^:]+:main(?:$|:)/i.test(normalizeText(sessionKey, ''));
|
|
441
271
|
}
|
|
442
272
|
|
|
443
|
-
function isExternalMainBootstrapContext({
|
|
444
|
-
channel = null,
|
|
445
|
-
sessionKey = null,
|
|
446
|
-
sessionType = null,
|
|
447
|
-
} = {}) {
|
|
448
|
-
const normalizedChannel = normalizeText(channel, null)?.toLowerCase() || null;
|
|
449
|
-
const normalizedSessionType = normalizeSessionType(sessionType);
|
|
450
|
-
const normalizedSessionKey = normalizeText(sessionKey, '');
|
|
451
|
-
if (normalizedChannel === 'claworld') return false;
|
|
452
|
-
if (
|
|
453
|
-
isManagementBootstrapContext({ sessionKey: normalizedSessionKey, sessionType: normalizedSessionType })
|
|
454
|
-
|| isClaworldConversationBootstrapContext({ channel: normalizedChannel, sessionKey: normalizedSessionKey, sessionType: normalizedSessionType })
|
|
455
|
-
) {
|
|
456
|
-
return false;
|
|
457
|
-
}
|
|
458
|
-
if (
|
|
459
|
-
normalizedSessionType === 'direct'
|
|
460
|
-
|| normalizedSessionType === 'dm'
|
|
461
|
-
|| normalizedSessionType === 'direct_message'
|
|
462
|
-
) {
|
|
463
|
-
return true;
|
|
464
|
-
}
|
|
465
|
-
return /^agent:[^:]+:[^:]+:direct:/i.test(normalizedSessionKey);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
273
|
function isManagementBootstrapContext({ sessionKey = null, sessionType = null } = {}) {
|
|
469
274
|
const normalizedSessionType = normalizeSessionType(sessionType);
|
|
470
|
-
if (
|
|
471
|
-
normalizedSessionType === 'management'
|
|
472
|
-
|| normalizedSessionType === 'management_session'
|
|
473
|
-
|| normalizedSessionType === 'orchestration'
|
|
474
|
-
|| normalizedSessionType === 'orchestration_session'
|
|
475
|
-
|| normalizedSessionType === 'operator'
|
|
476
|
-
|| normalizedSessionType === 'operator_session'
|
|
477
|
-
) {
|
|
275
|
+
if (normalizedSessionType === 'management' || normalizedSessionType === 'management_session') {
|
|
478
276
|
return true;
|
|
479
277
|
}
|
|
480
|
-
|
|
481
|
-
return /^management:[^:]+/i.test(normalizedSessionKey)
|
|
482
|
-
|| /^agent:[^:]+:management:[^:]+/i.test(normalizedSessionKey)
|
|
483
|
-
|| /^agent:[^:]+:claworld:(orchestration|operator|management)(?::|$)/i.test(normalizedSessionKey);
|
|
278
|
+
return /^management:[^:]+/i.test(normalizeText(sessionKey, ''));
|
|
484
279
|
}
|
|
485
280
|
|
|
486
281
|
function isClaworldConversationBootstrapContext({
|
|
@@ -491,13 +286,8 @@ function isClaworldConversationBootstrapContext({
|
|
|
491
286
|
const normalizedChannel = normalizeText(channel, null)?.toLowerCase() || null;
|
|
492
287
|
const normalizedSessionType = normalizeSessionType(sessionType);
|
|
493
288
|
const normalizedSessionKey = normalizeText(sessionKey, null);
|
|
494
|
-
const hasClaworldConversationSessionKey = (
|
|
495
|
-
/^agent:[^:]+:conversation:.*:(direct|world)(:|$)/i.test(normalizedSessionKey || '')
|
|
496
|
-
|| /^conversation:.*:(direct|world)(:|$)/i.test(normalizedSessionKey || '')
|
|
497
|
-
);
|
|
498
289
|
const hasClaworldChannel = normalizedChannel === 'claworld'
|
|
499
|
-
|| /:claworld:/i.test(normalizedSessionKey || '')
|
|
500
|
-
|| hasClaworldConversationSessionKey;
|
|
290
|
+
|| /:claworld:/i.test(normalizedSessionKey || '');
|
|
501
291
|
if (!hasClaworldChannel) return false;
|
|
502
292
|
if (
|
|
503
293
|
normalizedSessionType === 'conversation'
|
|
@@ -510,7 +300,6 @@ function isClaworldConversationBootstrapContext({
|
|
|
510
300
|
}
|
|
511
301
|
return (
|
|
512
302
|
/^agent:[^:]+:claworld:(direct|world):/i.test(normalizedSessionKey || '')
|
|
513
|
-
|| /^agent:[^:]+:conversation:.*:(direct|world)(:|$)/i.test(normalizedSessionKey || '')
|
|
514
303
|
|| /^conversation:.*:(direct|world)(:|$)/i.test(normalizedSessionKey || '')
|
|
515
304
|
);
|
|
516
305
|
}
|
|
@@ -618,9 +407,9 @@ export function resolveClaworldBootstrapContext(...sources) {
|
|
|
618
407
|
collectBootstrapRecords(source, records);
|
|
619
408
|
}
|
|
620
409
|
return {
|
|
621
|
-
channel: firstBootstrapField(records, ['channel', 'channelId'
|
|
622
|
-
sessionKey: firstBootstrapField(records, ['sessionKey', 'localSessionKey'
|
|
623
|
-
sessionType: firstBootstrapField(records, ['sessionType', 'sessionKind', 'sessionMode', 'mode'
|
|
410
|
+
channel: firstBootstrapField(records, ['channel', 'channelId']),
|
|
411
|
+
sessionKey: firstBootstrapField(records, ['sessionKey', 'localSessionKey']),
|
|
412
|
+
sessionType: firstBootstrapField(records, ['sessionType', 'sessionKind', 'sessionMode', 'mode']),
|
|
624
413
|
};
|
|
625
414
|
}
|
|
626
415
|
|
|
@@ -635,43 +424,9 @@ export function resolveClaworldBootstrapTarget(context = {}) {
|
|
|
635
424
|
if (isClaworldConversationBootstrapContext(normalizedContext)) {
|
|
636
425
|
return CLAWORLD_BOOTSTRAP_TARGETS.CLAWORLD_CONVERSATION;
|
|
637
426
|
}
|
|
638
|
-
if (isExternalMainBootstrapContext(normalizedContext)) {
|
|
639
|
-
return CLAWORLD_BOOTSTRAP_TARGETS.MAIN;
|
|
640
|
-
}
|
|
641
427
|
return CLAWORLD_BOOTSTRAP_TARGETS.NONE;
|
|
642
428
|
}
|
|
643
429
|
|
|
644
|
-
function resolveClaworldSessionDirectoryKind(input = {}, relations = {}) {
|
|
645
|
-
const explicitScope = normalizeText(input.scope, null);
|
|
646
|
-
if (explicitScope === 'main') return 'main';
|
|
647
|
-
if (explicitScope === 'management') return 'management';
|
|
648
|
-
if (explicitScope === 'conversation') return 'conversation';
|
|
649
|
-
const context = isPlainObject(input.context) ? input.context : {};
|
|
650
|
-
const target = resolveClaworldBootstrapTarget({
|
|
651
|
-
sessionKey: firstText(
|
|
652
|
-
relations.localSessionKey,
|
|
653
|
-
relations.sessionKey,
|
|
654
|
-
input.localSessionKey,
|
|
655
|
-
input.sessionKey,
|
|
656
|
-
context.SessionKey,
|
|
657
|
-
context.sessionKey,
|
|
658
|
-
),
|
|
659
|
-
sessionType: firstText(
|
|
660
|
-
input.sessionType,
|
|
661
|
-
input.sessionKind,
|
|
662
|
-
context.SessionType,
|
|
663
|
-
context.ChatType,
|
|
664
|
-
context.sessionType,
|
|
665
|
-
context.sessionKind,
|
|
666
|
-
),
|
|
667
|
-
channel: firstText(context.OriginatingChannel, context.channel),
|
|
668
|
-
});
|
|
669
|
-
if (target === CLAWORLD_BOOTSTRAP_TARGETS.MAIN) return 'main';
|
|
670
|
-
if (target === CLAWORLD_BOOTSTRAP_TARGETS.MANAGEMENT) return 'management';
|
|
671
|
-
if (target === CLAWORLD_BOOTSTRAP_TARGETS.CLAWORLD_CONVERSATION) return 'conversation';
|
|
672
|
-
return null;
|
|
673
|
-
}
|
|
674
|
-
|
|
675
430
|
async function readTextIfPresent(filePath) {
|
|
676
431
|
try {
|
|
677
432
|
return await fs.readFile(filePath, 'utf8');
|
|
@@ -681,17 +436,6 @@ async function readTextIfPresent(filePath) {
|
|
|
681
436
|
}
|
|
682
437
|
}
|
|
683
438
|
|
|
684
|
-
async function readJsonIfPresent(filePath) {
|
|
685
|
-
const text = await readTextIfPresent(filePath);
|
|
686
|
-
if (text == null) return null;
|
|
687
|
-
try {
|
|
688
|
-
const parsed = JSON.parse(text);
|
|
689
|
-
return isPlainObject(parsed) ? parsed : null;
|
|
690
|
-
} catch {
|
|
691
|
-
return null;
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
|
|
695
439
|
async function atomicWriteText(filePath, content, {
|
|
696
440
|
backup = true,
|
|
697
441
|
rejectEmptyOverwrite = true,
|
|
@@ -786,8 +530,8 @@ function toIsoTimestamp(value = null) {
|
|
|
786
530
|
return date.toISOString();
|
|
787
531
|
}
|
|
788
532
|
|
|
789
|
-
function
|
|
790
|
-
return toIsoTimestamp(timestamp).slice(0,
|
|
533
|
+
function toMonthKey(timestamp) {
|
|
534
|
+
return toIsoTimestamp(timestamp).slice(0, 7);
|
|
791
535
|
}
|
|
792
536
|
|
|
793
537
|
function truncateText(value, maxChars = MAX_EVENT_EXCERPT_CHARS) {
|
|
@@ -796,387 +540,10 @@ function truncateText(value, maxChars = MAX_EVENT_EXCERPT_CHARS) {
|
|
|
796
540
|
return `${text.slice(0, Math.max(0, maxChars - 3))}...`;
|
|
797
541
|
}
|
|
798
542
|
|
|
799
|
-
function resolveClaworldSessionDirectoryPath(workspaceRoot) {
|
|
800
|
-
return path.join(
|
|
801
|
-
workspaceRoot,
|
|
802
|
-
CLAWORLD_WORKING_MEMORY_DIR,
|
|
803
|
-
CLAWORLD_SESSION_DIRECTORY_FILE,
|
|
804
|
-
);
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
function createEmptyClaworldSessionDirectory(timestamp = null) {
|
|
808
|
-
return {
|
|
809
|
-
schema: CLAWORLD_SESSION_DIRECTORY_SCHEMA,
|
|
810
|
-
version: 1,
|
|
811
|
-
updatedAt: toIsoTimestamp(timestamp),
|
|
812
|
-
main: {},
|
|
813
|
-
management: {},
|
|
814
|
-
conversationSessions: {},
|
|
815
|
-
};
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
function normalizeClaworldSessionDirectory(value = null) {
|
|
819
|
-
const source = isPlainObject(value) ? value : {};
|
|
820
|
-
const directory = {
|
|
821
|
-
...source,
|
|
822
|
-
schema: CLAWORLD_SESSION_DIRECTORY_SCHEMA,
|
|
823
|
-
version: 1,
|
|
824
|
-
updatedAt: normalizeText(source.updatedAt, toIsoTimestamp()),
|
|
825
|
-
main: isPlainObject(source.main) ? { ...source.main } : {},
|
|
826
|
-
management: isPlainObject(source.management) ? { ...source.management } : {},
|
|
827
|
-
conversationSessions: isPlainObject(source.conversationSessions)
|
|
828
|
-
? { ...source.conversationSessions }
|
|
829
|
-
: {},
|
|
830
|
-
};
|
|
831
|
-
return directory;
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
function normalizeChatRequestId(input = {}, relations = {}) {
|
|
835
|
-
return firstText(
|
|
836
|
-
relations.chatRequestId,
|
|
837
|
-
relations.requestId,
|
|
838
|
-
input.chatRequestId,
|
|
839
|
-
input.requestId,
|
|
840
|
-
input.refs?.chatRequestId,
|
|
841
|
-
input.refs?.requestId,
|
|
842
|
-
);
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
function compactDirectoryObject(value = {}) {
|
|
846
|
-
return cleanJournalObject(value);
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
function upsertSessionArtifact(artifacts = [], artifact = {}) {
|
|
850
|
-
const normalizedArtifact = compactDirectoryObject(artifact);
|
|
851
|
-
if (
|
|
852
|
-
!normalizedArtifact.sessionId
|
|
853
|
-
&& !normalizedArtifact.sessionFile
|
|
854
|
-
&& !normalizedArtifact.transcriptPath
|
|
855
|
-
) {
|
|
856
|
-
return artifacts;
|
|
857
|
-
}
|
|
858
|
-
const key = [
|
|
859
|
-
normalizedArtifact.sessionId || '',
|
|
860
|
-
normalizedArtifact.sessionFile || '',
|
|
861
|
-
normalizedArtifact.transcriptPath || '',
|
|
862
|
-
].join('\u0000');
|
|
863
|
-
const nextArtifacts = Array.isArray(artifacts) ? [...artifacts] : [];
|
|
864
|
-
const index = nextArtifacts.findIndex((entry) => [
|
|
865
|
-
entry?.sessionId || '',
|
|
866
|
-
entry?.sessionFile || '',
|
|
867
|
-
entry?.transcriptPath || '',
|
|
868
|
-
].join('\u0000') === key);
|
|
869
|
-
if (index >= 0) {
|
|
870
|
-
nextArtifacts[index] = compactDirectoryObject({
|
|
871
|
-
...nextArtifacts[index],
|
|
872
|
-
...normalizedArtifact,
|
|
873
|
-
firstSeenAt: nextArtifacts[index].firstSeenAt || normalizedArtifact.firstSeenAt,
|
|
874
|
-
lastSeenAt: normalizedArtifact.lastSeenAt || nextArtifacts[index].lastSeenAt,
|
|
875
|
-
});
|
|
876
|
-
return nextArtifacts;
|
|
877
|
-
}
|
|
878
|
-
nextArtifacts.push(normalizedArtifact);
|
|
879
|
-
return nextArtifacts;
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
function applySessionArtifactHints(target = {}, relations = {}) {
|
|
883
|
-
const nextTarget = { ...target };
|
|
884
|
-
if (relations.sessionId) nextTarget.latestSessionId = relations.sessionId;
|
|
885
|
-
if (relations.sessionFile) nextTarget.latestSessionFile = relations.sessionFile;
|
|
886
|
-
if (relations.transcriptPath) nextTarget.latestTranscriptPath = relations.transcriptPath;
|
|
887
|
-
return nextTarget;
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
function buildSessionArtifact({ relations = {}, timestamp = null } = {}) {
|
|
891
|
-
return compactDirectoryObject({
|
|
892
|
-
sessionId: relations.sessionId,
|
|
893
|
-
sessionFile: relations.sessionFile,
|
|
894
|
-
sessionStorePath: relations.sessionStorePath,
|
|
895
|
-
transcriptPath: relations.transcriptPath,
|
|
896
|
-
deliveryId: relations.deliveryId,
|
|
897
|
-
eventId: relations.eventId,
|
|
898
|
-
firstSeenAt: timestamp,
|
|
899
|
-
lastSeenAt: timestamp,
|
|
900
|
-
});
|
|
901
|
-
}
|
|
902
|
-
|
|
903
|
-
function applyClaworldSessionDirectoryUpdate(directory = {}, input = {}) {
|
|
904
|
-
const refs = isPlainObject(input.refs) ? input.refs : {};
|
|
905
|
-
const relations = resolveJournalRelations(input, refs);
|
|
906
|
-
const kind = resolveClaworldSessionDirectoryKind(input, relations);
|
|
907
|
-
const timestamp = toIsoTimestamp(input.timestamp || Date.now());
|
|
908
|
-
const localSessionKey = firstText(relations.localSessionKey, relations.sessionKey, input.localSessionKey, input.sessionKey);
|
|
909
|
-
const relaySessionKey = firstText(relations.relaySessionKey, input.relaySessionKey);
|
|
910
|
-
if (!kind || !localSessionKey) {
|
|
911
|
-
return { updated: false, reason: 'missing_session_reference', directory };
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
const nextDirectory = normalizeClaworldSessionDirectory(directory);
|
|
915
|
-
nextDirectory.updatedAt = timestamp;
|
|
916
|
-
|
|
917
|
-
if (kind === 'main') {
|
|
918
|
-
nextDirectory.main = compactDirectoryObject({
|
|
919
|
-
...nextDirectory.main,
|
|
920
|
-
lastActiveSessionKey: localSessionKey,
|
|
921
|
-
lastUpdatedAt: timestamp,
|
|
922
|
-
localAgentId: relations.localAgentId,
|
|
923
|
-
source: input.source,
|
|
924
|
-
});
|
|
925
|
-
return { updated: true, kind, directory: nextDirectory };
|
|
926
|
-
}
|
|
927
|
-
|
|
928
|
-
if (kind === 'management') {
|
|
929
|
-
nextDirectory.management = compactDirectoryObject({
|
|
930
|
-
...nextDirectory.management,
|
|
931
|
-
lastActiveLocalSessionKey: localSessionKey,
|
|
932
|
-
relaySessionKey,
|
|
933
|
-
localAgentId: relations.localAgentId,
|
|
934
|
-
targetAgentId: relations.targetAgentId,
|
|
935
|
-
lastUpdatedAt: timestamp,
|
|
936
|
-
});
|
|
937
|
-
return { updated: true, kind, directory: nextDirectory };
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
const conversationSessions = isPlainObject(nextDirectory.conversationSessions)
|
|
941
|
-
? { ...nextDirectory.conversationSessions }
|
|
942
|
-
: {};
|
|
943
|
-
const currentSession = isPlainObject(conversationSessions[localSessionKey])
|
|
944
|
-
? conversationSessions[localSessionKey]
|
|
945
|
-
: {};
|
|
946
|
-
let nextSession = applySessionArtifactHints(
|
|
947
|
-
compactDirectoryObject({
|
|
948
|
-
...currentSession,
|
|
949
|
-
localSessionKey,
|
|
950
|
-
relaySessionKey,
|
|
951
|
-
conversationKey: firstText(currentSession.conversationKey, relations.conversationKey),
|
|
952
|
-
worldId: firstText(currentSession.worldId, relations.worldId),
|
|
953
|
-
localAgentId: firstText(currentSession.localAgentId, relations.localAgentId),
|
|
954
|
-
firstSeenAt: currentSession.firstSeenAt || timestamp,
|
|
955
|
-
lastSeenAt: timestamp,
|
|
956
|
-
}),
|
|
957
|
-
relations,
|
|
958
|
-
);
|
|
959
|
-
|
|
960
|
-
const chatRequestId = normalizeChatRequestId(input, relations);
|
|
961
|
-
if (chatRequestId) {
|
|
962
|
-
const chatRequests = isPlainObject(nextSession.chatRequests)
|
|
963
|
-
? { ...nextSession.chatRequests }
|
|
964
|
-
: {};
|
|
965
|
-
const currentRequest = isPlainObject(chatRequests[chatRequestId])
|
|
966
|
-
? chatRequests[chatRequestId]
|
|
967
|
-
: {};
|
|
968
|
-
const artifact = buildSessionArtifact({ relations, timestamp });
|
|
969
|
-
const nextRequest = applySessionArtifactHints(
|
|
970
|
-
compactDirectoryObject({
|
|
971
|
-
...currentRequest,
|
|
972
|
-
chatRequestId,
|
|
973
|
-
firstSeenAt: currentRequest.firstSeenAt || timestamp,
|
|
974
|
-
lastSeenAt: timestamp,
|
|
975
|
-
sessionArtifacts: upsertSessionArtifact(currentRequest.sessionArtifacts, artifact),
|
|
976
|
-
}),
|
|
977
|
-
relations,
|
|
978
|
-
);
|
|
979
|
-
chatRequests[chatRequestId] = nextRequest;
|
|
980
|
-
nextSession = compactDirectoryObject({
|
|
981
|
-
...nextSession,
|
|
982
|
-
chatRequests,
|
|
983
|
-
});
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
conversationSessions[localSessionKey] = nextSession;
|
|
987
|
-
nextDirectory.conversationSessions = conversationSessions;
|
|
988
|
-
return { updated: true, kind, directory: nextDirectory };
|
|
989
|
-
}
|
|
990
|
-
|
|
991
|
-
export async function readClaworldSessionDirectory(options = {}, readOptions = {}) {
|
|
992
|
-
const workspaceRoot = resolveClaworldWorkspaceRoot(options, readOptions.homeDir || os.homedir());
|
|
993
|
-
const sessionDirectoryPath = resolveClaworldSessionDirectoryPath(workspaceRoot);
|
|
994
|
-
const current = await readJsonIfPresent(sessionDirectoryPath);
|
|
995
|
-
return {
|
|
996
|
-
workspaceRoot,
|
|
997
|
-
sessionDirectoryPath,
|
|
998
|
-
directory: normalizeClaworldSessionDirectory(current || createEmptyClaworldSessionDirectory()),
|
|
999
|
-
exists: current != null,
|
|
1000
|
-
};
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
export async function updateClaworldSessionDirectory(options = {}, input = {}, updateOptions = {}) {
|
|
1004
|
-
const workspaceRoot = resolveClaworldWorkspaceRoot(options, updateOptions.homeDir || os.homedir());
|
|
1005
|
-
await ensureClaworldWorkingMemory(workspaceRoot, updateOptions);
|
|
1006
|
-
const sessionDirectoryPath = resolveClaworldSessionDirectoryPath(workspaceRoot);
|
|
1007
|
-
const current = await readJsonIfPresent(sessionDirectoryPath);
|
|
1008
|
-
const baseDirectory = current || createEmptyClaworldSessionDirectory(input.timestamp || updateOptions.timestamp);
|
|
1009
|
-
const result = applyClaworldSessionDirectoryUpdate(baseDirectory, input);
|
|
1010
|
-
if (!result.updated) {
|
|
1011
|
-
return {
|
|
1012
|
-
ok: true,
|
|
1013
|
-
updated: false,
|
|
1014
|
-
reason: result.reason,
|
|
1015
|
-
workspaceRoot,
|
|
1016
|
-
sessionDirectoryPath,
|
|
1017
|
-
directory: normalizeClaworldSessionDirectory(baseDirectory),
|
|
1018
|
-
};
|
|
1019
|
-
}
|
|
1020
|
-
await atomicWriteText(
|
|
1021
|
-
sessionDirectoryPath,
|
|
1022
|
-
`${JSON.stringify(result.directory, null, 2)}\n`,
|
|
1023
|
-
{
|
|
1024
|
-
backup: false,
|
|
1025
|
-
rejectEmptyOverwrite: false,
|
|
1026
|
-
},
|
|
1027
|
-
);
|
|
1028
|
-
return {
|
|
1029
|
-
ok: true,
|
|
1030
|
-
updated: true,
|
|
1031
|
-
kind: result.kind,
|
|
1032
|
-
workspaceRoot,
|
|
1033
|
-
sessionDirectoryPath,
|
|
1034
|
-
directory: result.directory,
|
|
1035
|
-
};
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
543
|
function flattenInline(value) {
|
|
1039
544
|
return truncateText(String(value ?? '').replace(/\s+/g, ' ').trim());
|
|
1040
545
|
}
|
|
1041
546
|
|
|
1042
|
-
function firstText(...values) {
|
|
1043
|
-
for (const value of values) {
|
|
1044
|
-
const normalized = normalizeText(value, null);
|
|
1045
|
-
if (normalized) return normalized;
|
|
1046
|
-
}
|
|
1047
|
-
return null;
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
function hasStructuredValue(value) {
|
|
1051
|
-
if (value == null) return false;
|
|
1052
|
-
if (typeof value === 'string') return normalizeText(value, null) != null;
|
|
1053
|
-
if (Array.isArray(value)) return value.length > 0;
|
|
1054
|
-
if (isPlainObject(value)) return Object.keys(value).length > 0;
|
|
1055
|
-
return true;
|
|
1056
|
-
}
|
|
1057
|
-
|
|
1058
|
-
function cleanJournalValue(value) {
|
|
1059
|
-
if (value == null) return null;
|
|
1060
|
-
if (value instanceof Date) return toIsoTimestamp(value);
|
|
1061
|
-
if (typeof value === 'string') return normalizeText(value, null);
|
|
1062
|
-
if (typeof value === 'number' || typeof value === 'boolean') return value;
|
|
1063
|
-
if (Array.isArray(value)) {
|
|
1064
|
-
const cleanedArray = value
|
|
1065
|
-
.map((entry) => cleanJournalValue(entry))
|
|
1066
|
-
.filter(hasStructuredValue);
|
|
1067
|
-
return cleanedArray.length > 0 ? cleanedArray : null;
|
|
1068
|
-
}
|
|
1069
|
-
if (isPlainObject(value)) {
|
|
1070
|
-
const cleanedObject = {};
|
|
1071
|
-
for (const [key, entry] of Object.entries(value)) {
|
|
1072
|
-
const cleaned = cleanJournalValue(entry);
|
|
1073
|
-
if (hasStructuredValue(cleaned)) {
|
|
1074
|
-
cleanedObject[key] = cleaned;
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
return Object.keys(cleanedObject).length > 0 ? cleanedObject : null;
|
|
1078
|
-
}
|
|
1079
|
-
return normalizeText(value, null);
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
|
-
function cleanJournalObject(value = {}) {
|
|
1083
|
-
return cleanJournalValue(value) || {};
|
|
1084
|
-
}
|
|
1085
|
-
|
|
1086
|
-
function resolveJournalScope(input = {}, relations = {}) {
|
|
1087
|
-
const directScope = normalizeText(input.scope, null);
|
|
1088
|
-
if (directScope) return directScope;
|
|
1089
|
-
const context = isPlainObject(input.context) ? input.context : {};
|
|
1090
|
-
const normalizedTarget = resolveClaworldBootstrapTarget({
|
|
1091
|
-
sessionKey: firstText(
|
|
1092
|
-
input.localSessionKey,
|
|
1093
|
-
input.sessionKey,
|
|
1094
|
-
relations.localSessionKey,
|
|
1095
|
-
relations.sessionKey,
|
|
1096
|
-
context.SessionKey,
|
|
1097
|
-
context.sessionKey,
|
|
1098
|
-
),
|
|
1099
|
-
sessionType: firstText(
|
|
1100
|
-
input.sessionType,
|
|
1101
|
-
input.sessionKind,
|
|
1102
|
-
context.SessionType,
|
|
1103
|
-
context.ChatType,
|
|
1104
|
-
context.sessionType,
|
|
1105
|
-
context.sessionKind,
|
|
1106
|
-
),
|
|
1107
|
-
channel: firstText(context.OriginatingChannel, context.channel),
|
|
1108
|
-
});
|
|
1109
|
-
if (normalizedTarget === CLAWORLD_BOOTSTRAP_TARGETS.CLAWORLD_CONVERSATION) return 'conversation';
|
|
1110
|
-
if (normalizedTarget === CLAWORLD_BOOTSTRAP_TARGETS.MANAGEMENT) return 'management';
|
|
1111
|
-
if (normalizedTarget === CLAWORLD_BOOTSTRAP_TARGETS.MAIN) return 'main';
|
|
1112
|
-
return firstText(input.sessionKind, input.sessionType, context.sessionKind, context.SessionType, 'runtime');
|
|
1113
|
-
}
|
|
1114
|
-
|
|
1115
|
-
function resolveJournalRelations(input = {}, refs = {}) {
|
|
1116
|
-
const relations = isPlainObject(input.relations)
|
|
1117
|
-
? input.relations
|
|
1118
|
-
: isPlainObject(input.correlation)
|
|
1119
|
-
? input.correlation
|
|
1120
|
-
: {};
|
|
1121
|
-
const context = isPlainObject(input.context) ? input.context : {};
|
|
1122
|
-
const artifacts = isPlainObject(input.artifacts) ? input.artifacts : {};
|
|
1123
|
-
const requestId = firstText(
|
|
1124
|
-
relations.requestId,
|
|
1125
|
-
relations.chatRequestId,
|
|
1126
|
-
input.requestId,
|
|
1127
|
-
input.chatRequestId,
|
|
1128
|
-
refs.requestId,
|
|
1129
|
-
refs.chatRequestId,
|
|
1130
|
-
refs.friendRequestId,
|
|
1131
|
-
);
|
|
1132
|
-
return cleanJournalObject({
|
|
1133
|
-
requestId,
|
|
1134
|
-
chatRequestId: firstText(relations.chatRequestId, input.chatRequestId, refs.chatRequestId, requestId),
|
|
1135
|
-
deliveryId: firstText(relations.deliveryId, input.deliveryId, refs.deliveryId, context.RelayDeliveryId),
|
|
1136
|
-
eventId: firstText(relations.eventId, input.eventId, input.id, refs.eventId, context.RelayEventId),
|
|
1137
|
-
notificationId: firstText(relations.notificationId, refs.notificationId),
|
|
1138
|
-
inboxItemId: firstText(relations.inboxItemId, refs.inboxItemId),
|
|
1139
|
-
conversationKey: firstText(relations.conversationKey, input.conversationKey, refs.conversationKey),
|
|
1140
|
-
worldId: firstText(relations.worldId, input.worldId, refs.worldId),
|
|
1141
|
-
accountId: firstText(relations.accountId, input.accountId, refs.accountId, context.AccountId),
|
|
1142
|
-
agentCode: firstText(relations.agentCode, refs.agentCode),
|
|
1143
|
-
localAgentId: firstText(relations.localAgentId, input.localAgentId, context.AgentId, context.agentId),
|
|
1144
|
-
targetAgentId: firstText(relations.targetAgentId, input.targetAgentId, refs.targetAgentId, context.RelayTargetAgentId),
|
|
1145
|
-
fromAgentId: firstText(relations.fromAgentId, input.fromAgentId, refs.fromAgentId, context.RelayFromAgentId),
|
|
1146
|
-
sessionKey: firstText(relations.sessionKey, input.sessionKey, context.SessionKey, context.sessionKey),
|
|
1147
|
-
localSessionKey: firstText(relations.localSessionKey, input.localSessionKey, context.LocalSessionKey, context.SessionKey),
|
|
1148
|
-
relaySessionKey: firstText(relations.relaySessionKey, input.relaySessionKey, context.RelaySessionKey),
|
|
1149
|
-
sessionId: firstText(relations.sessionId, input.sessionId, artifacts.sessionId, context.SessionId),
|
|
1150
|
-
sessionFile: firstText(relations.sessionFile, input.sessionFile, artifacts.sessionFile, artifacts.sessionPath, context.SessionFile),
|
|
1151
|
-
sessionStorePath: firstText(relations.sessionStorePath, input.sessionStorePath, artifacts.sessionStorePath),
|
|
1152
|
-
transcriptPath: firstText(relations.transcriptPath, input.transcriptPath, artifacts.transcriptPath),
|
|
1153
|
-
reportPath: firstText(relations.reportPath, input.reportPath, artifacts.reportPath),
|
|
1154
|
-
});
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
function buildJournalKey(relations = {}, fallbackEventId = null) {
|
|
1158
|
-
const event = firstText(relations.eventId, fallbackEventId);
|
|
1159
|
-
const request = firstText(relations.requestId, relations.chatRequestId);
|
|
1160
|
-
const session = firstText(relations.localSessionKey, relations.relaySessionKey, relations.sessionKey);
|
|
1161
|
-
const continuity = request
|
|
1162
|
-
? `request:${request}`
|
|
1163
|
-
: firstText(
|
|
1164
|
-
relations.conversationKey ? `conversation:${relations.conversationKey}` : null,
|
|
1165
|
-
session ? `session:${session}` : null,
|
|
1166
|
-
relations.worldId ? `world:${relations.worldId}` : null,
|
|
1167
|
-
event ? `event:${event}` : null,
|
|
1168
|
-
);
|
|
1169
|
-
return cleanJournalObject({
|
|
1170
|
-
event,
|
|
1171
|
-
continuity,
|
|
1172
|
-
request,
|
|
1173
|
-
session,
|
|
1174
|
-
sessionId: relations.sessionId,
|
|
1175
|
-
world: relations.worldId,
|
|
1176
|
-
conversation: relations.conversationKey,
|
|
1177
|
-
});
|
|
1178
|
-
}
|
|
1179
|
-
|
|
1180
547
|
export function buildClaworldMaintenanceEvent(input = {}) {
|
|
1181
548
|
const toolName = normalizeText(input.toolName, null);
|
|
1182
549
|
const source = normalizeText(input.source, toolName ? 'claworld_tool' : 'claworld_runtime');
|
|
@@ -1190,26 +557,14 @@ export function buildClaworldMaintenanceEvent(input = {}) {
|
|
|
1190
557
|
.filter(([, value]) => value != null),
|
|
1191
558
|
)
|
|
1192
559
|
: {};
|
|
1193
|
-
const id = normalizeText(input.id, `${source}:${kind}:${timestamp}`);
|
|
1194
|
-
const relations = resolveJournalRelations({ ...input, eventId: input.eventId || id }, refs);
|
|
1195
|
-
const scope = resolveJournalScope(input, relations);
|
|
1196
560
|
return {
|
|
1197
|
-
|
|
1198
|
-
id,
|
|
561
|
+
id: normalizeText(input.id, `${source}:${kind}:${timestamp}`),
|
|
1199
562
|
timestamp,
|
|
1200
563
|
source,
|
|
1201
564
|
kind,
|
|
1202
|
-
eventType: normalizeText(input.eventType, kind),
|
|
1203
|
-
scope,
|
|
1204
565
|
summary,
|
|
1205
566
|
excerpt: truncateText(input.excerpt || ''),
|
|
1206
567
|
refs,
|
|
1207
|
-
key: buildJournalKey(relations, id),
|
|
1208
|
-
relations,
|
|
1209
|
-
actor: cleanJournalObject(input.actor || {}),
|
|
1210
|
-
tool: cleanJournalObject(input.tool || {}),
|
|
1211
|
-
artifacts: cleanJournalObject(input.artifacts || {}),
|
|
1212
|
-
maintenance: cleanJournalObject(input.maintenance || {}),
|
|
1213
568
|
};
|
|
1214
569
|
}
|
|
1215
570
|
|
|
@@ -1242,11 +597,9 @@ function compactResultPayload(payload = {}) {
|
|
|
1242
597
|
const keys = [
|
|
1243
598
|
'status',
|
|
1244
599
|
'tool',
|
|
1245
|
-
'action',
|
|
1246
600
|
'accountId',
|
|
1247
601
|
'worldId',
|
|
1248
602
|
'displayName',
|
|
1249
|
-
'requestId',
|
|
1250
603
|
'chatRequestId',
|
|
1251
604
|
'conversationKey',
|
|
1252
605
|
'feedbackId',
|
|
@@ -1263,96 +616,29 @@ function compactResultPayload(payload = {}) {
|
|
|
1263
616
|
return compact;
|
|
1264
617
|
}
|
|
1265
618
|
|
|
1266
|
-
function readNestedObject(value, key) {
|
|
1267
|
-
const nested = value?.[key];
|
|
1268
|
-
return nested && typeof nested === 'object' && !Array.isArray(nested) ? nested : {};
|
|
1269
|
-
}
|
|
1270
|
-
|
|
1271
619
|
export function buildClaworldToolMaintenanceEvent({
|
|
1272
620
|
toolName,
|
|
1273
621
|
params = {},
|
|
1274
622
|
result = null,
|
|
1275
623
|
timestamp = null,
|
|
1276
|
-
context = {},
|
|
1277
624
|
} = {}) {
|
|
1278
625
|
const normalizedToolName = normalizeText(toolName, null);
|
|
1279
626
|
if (!normalizedToolName || !normalizedToolName.startsWith('claworld_')) return null;
|
|
1280
627
|
const payload = parseToolResultPayload(result) || {};
|
|
1281
|
-
const chatRequest = readNestedObject(payload, 'chatRequest');
|
|
1282
|
-
const request = readNestedObject(payload, 'request');
|
|
1283
|
-
const kickoff = readNestedObject(payload, 'kickoff');
|
|
1284
|
-
const conversation = readNestedObject(payload, 'conversation');
|
|
1285
628
|
const compactPayload = compactResultPayload(payload);
|
|
1286
|
-
const requestId = firstText(
|
|
1287
|
-
params.requestId,
|
|
1288
|
-
params.chatRequestId,
|
|
1289
|
-
payload.requestId,
|
|
1290
|
-
payload.chatRequestId,
|
|
1291
|
-
chatRequest.chatRequestId,
|
|
1292
|
-
chatRequest.requestId,
|
|
1293
|
-
request.requestId,
|
|
1294
|
-
);
|
|
1295
|
-
const localSessionKey = firstText(
|
|
1296
|
-
context.sessionKey,
|
|
1297
|
-
context.SessionKey,
|
|
1298
|
-
context.localSessionKey,
|
|
1299
|
-
payload.localSessionKey,
|
|
1300
|
-
kickoff.localSessionKey,
|
|
1301
|
-
);
|
|
1302
|
-
const relaySessionKey = firstText(
|
|
1303
|
-
context.RelaySessionKey,
|
|
1304
|
-
payload.sessionKey,
|
|
1305
|
-
kickoff.sessionKey,
|
|
1306
|
-
conversation.sessionKey,
|
|
1307
|
-
);
|
|
1308
|
-
const sessionKind = resolveJournalScope({
|
|
1309
|
-
sessionKey: localSessionKey || relaySessionKey,
|
|
1310
|
-
sessionType: firstText(context.sessionType, context.SessionType, context.ChatType, context.sessionKind),
|
|
1311
|
-
context,
|
|
1312
|
-
});
|
|
1313
629
|
const refs = {
|
|
1314
630
|
accountId: params.accountId || payload.accountId,
|
|
1315
631
|
worldId: params.worldId || payload.worldId,
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
conversationKey: params.conversationKey || payload.conversationKey || conversation.conversationKey,
|
|
632
|
+
chatRequestId: params.chatRequestId || payload.chatRequestId,
|
|
633
|
+
conversationKey: params.conversationKey || payload.conversationKey,
|
|
1319
634
|
agentCode: params.agentCode || payload.agentCode,
|
|
1320
|
-
sessionKey: localSessionKey || relaySessionKey,
|
|
1321
|
-
relaySessionKey,
|
|
1322
635
|
};
|
|
1323
636
|
return buildClaworldMaintenanceEvent({
|
|
1324
637
|
source: 'claworld_tool',
|
|
1325
638
|
kind: normalizedToolName,
|
|
1326
|
-
eventType: 'tool_call',
|
|
1327
|
-
scope: sessionKind,
|
|
1328
639
|
toolName: normalizedToolName,
|
|
1329
640
|
timestamp,
|
|
1330
641
|
refs,
|
|
1331
|
-
relations: {
|
|
1332
|
-
requestId,
|
|
1333
|
-
chatRequestId: refs.chatRequestId,
|
|
1334
|
-
conversationKey: refs.conversationKey,
|
|
1335
|
-
worldId: refs.worldId,
|
|
1336
|
-
accountId: refs.accountId,
|
|
1337
|
-
agentCode: refs.agentCode,
|
|
1338
|
-
localSessionKey,
|
|
1339
|
-
relaySessionKey,
|
|
1340
|
-
sessionKey: localSessionKey || relaySessionKey,
|
|
1341
|
-
localAgentId: firstText(context.agentId, context.AgentId),
|
|
1342
|
-
sessionId: firstText(context.sessionId, context.SessionId),
|
|
1343
|
-
sessionFile: firstText(context.sessionFile, context.SessionFile, context.sessionPath),
|
|
1344
|
-
transcriptPath: firstText(context.transcriptPath),
|
|
1345
|
-
},
|
|
1346
|
-
actor: {
|
|
1347
|
-
sessionKind,
|
|
1348
|
-
agentId: firstText(context.agentId, context.AgentId),
|
|
1349
|
-
sessionKey: localSessionKey || relaySessionKey,
|
|
1350
|
-
},
|
|
1351
|
-
tool: {
|
|
1352
|
-
name: normalizedToolName,
|
|
1353
|
-
action: firstText(params.action, payload.action),
|
|
1354
|
-
status: firstText(payload.status, 'succeeded'),
|
|
1355
|
-
},
|
|
1356
642
|
summary: `${normalizedToolName} succeeded.`,
|
|
1357
643
|
excerpt: Object.keys(compactPayload).length > 0
|
|
1358
644
|
? JSON.stringify(compactPayload)
|
|
@@ -1360,84 +646,45 @@ export function buildClaworldToolMaintenanceEvent({
|
|
|
1360
646
|
});
|
|
1361
647
|
}
|
|
1362
648
|
|
|
1363
|
-
function
|
|
1364
|
-
return
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
timestamp: event.timestamp,
|
|
1369
|
-
source: event.source,
|
|
1370
|
-
scope: event.scope,
|
|
1371
|
-
eventType: event.eventType,
|
|
1372
|
-
kind: event.kind,
|
|
1373
|
-
summary: event.summary,
|
|
1374
|
-
refs: event.refs,
|
|
1375
|
-
relations: event.relations,
|
|
1376
|
-
actor: event.actor,
|
|
1377
|
-
tool: event.tool,
|
|
1378
|
-
artifacts: event.artifacts,
|
|
1379
|
-
maintenance: event.maintenance,
|
|
1380
|
-
excerpt: event.excerpt || null,
|
|
1381
|
-
});
|
|
1382
|
-
}
|
|
1383
|
-
|
|
1384
|
-
function buildJournalEntry(event = {}) {
|
|
1385
|
-
const title = flattenInline(event.summary || event.kind || 'Claworld event recorded.');
|
|
1386
|
-
const heading = [
|
|
1387
|
-
event.timestamp,
|
|
1388
|
-
event.scope || event.source,
|
|
1389
|
-
event.kind,
|
|
1390
|
-
title,
|
|
1391
|
-
].filter(Boolean).join(' · ');
|
|
1392
|
-
return [
|
|
1393
|
-
`## ${heading}`,
|
|
1394
|
-
'',
|
|
1395
|
-
'```json',
|
|
1396
|
-
JSON.stringify(buildJournalEntryPayload(event), null, 2),
|
|
1397
|
-
'```',
|
|
1398
|
-
'',
|
|
1399
|
-
].join('\n');
|
|
649
|
+
function formatRefs(refs = {}) {
|
|
650
|
+
return Object.entries(refs)
|
|
651
|
+
.filter(([, value]) => value != null)
|
|
652
|
+
.map(([key, value]) => `${key}=${flattenInline(value)}`)
|
|
653
|
+
.join(', ');
|
|
1400
654
|
}
|
|
1401
655
|
|
|
1402
656
|
export async function appendClaworldJournalEvent(options = {}, event = {}, appendOptions = {}) {
|
|
1403
657
|
const workspaceRoot = resolveClaworldWorkspaceRoot(options, appendOptions.homeDir || os.homedir());
|
|
1404
658
|
await ensureClaworldWorkingMemory(workspaceRoot, appendOptions);
|
|
1405
659
|
const normalizedEvent = buildClaworldMaintenanceEvent(event);
|
|
1406
|
-
const
|
|
660
|
+
const monthKey = toMonthKey(normalizedEvent.timestamp);
|
|
1407
661
|
const journalPath = path.join(
|
|
1408
662
|
workspaceRoot,
|
|
1409
663
|
CLAWORLD_WORKING_MEMORY_DIR,
|
|
1410
664
|
CLAWORLD_JOURNAL_DIR,
|
|
1411
|
-
`${
|
|
665
|
+
`${monthKey}.md`,
|
|
1412
666
|
);
|
|
1413
667
|
const currentContent = await readTextIfPresent(journalPath);
|
|
1414
|
-
const
|
|
668
|
+
const refsLine = formatRefs(normalizedEvent.refs);
|
|
669
|
+
const entry = [
|
|
670
|
+
`## ${normalizedEvent.timestamp} - ${normalizedEvent.kind}`,
|
|
671
|
+
`- Source: ${normalizedEvent.source}`,
|
|
672
|
+
`- Summary: ${flattenInline(normalizedEvent.summary)}`,
|
|
673
|
+
normalizedEvent.excerpt ? `- Excerpt: ${flattenInline(normalizedEvent.excerpt)}` : null,
|
|
674
|
+
refsLine ? `- Refs: ${refsLine}` : null,
|
|
675
|
+
'',
|
|
676
|
+
].filter((line) => line != null).join('\n');
|
|
1415
677
|
if (currentContent == null) {
|
|
1416
|
-
await atomicWriteText(journalPath, `# Claworld Journal ${
|
|
678
|
+
await atomicWriteText(journalPath, `# Claworld Journal ${monthKey}\n\n${entry}`, {
|
|
1417
679
|
backup: false,
|
|
1418
680
|
rejectEmptyOverwrite: false,
|
|
1419
681
|
});
|
|
1420
682
|
} else {
|
|
1421
683
|
await fs.appendFile(journalPath, `${currentContent.endsWith('\n') ? '' : '\n'}${entry}`, 'utf8');
|
|
1422
684
|
}
|
|
1423
|
-
let sessionDirectory = null;
|
|
1424
|
-
if (appendOptions.updateSessionDirectory !== false) {
|
|
1425
|
-
try {
|
|
1426
|
-
sessionDirectory = await updateClaworldSessionDirectory(workspaceRoot, normalizedEvent, {
|
|
1427
|
-
...appendOptions,
|
|
1428
|
-
timestamp: normalizedEvent.timestamp,
|
|
1429
|
-
});
|
|
1430
|
-
} catch (error) {
|
|
1431
|
-
sessionDirectory = {
|
|
1432
|
-
ok: false,
|
|
1433
|
-
error: error?.message || String(error),
|
|
1434
|
-
};
|
|
1435
|
-
}
|
|
1436
|
-
}
|
|
1437
685
|
return {
|
|
1438
686
|
ok: true,
|
|
1439
687
|
journalPath,
|
|
1440
|
-
sessionDirectory,
|
|
1441
688
|
event: normalizedEvent,
|
|
1442
689
|
};
|
|
1443
690
|
}
|
|
@@ -1484,27 +731,11 @@ export async function buildClaworldBootstrapPromptContext(context = {}, options
|
|
|
1484
731
|
})
|
|
1485
732
|
: { slices: {} };
|
|
1486
733
|
const pointerInjected = target === CLAWORLD_BOOTSTRAP_TARGETS.MAIN;
|
|
1487
|
-
const managementPolicyInjected = target === CLAWORLD_BOOTSTRAP_TARGETS.MANAGEMENT;
|
|
1488
|
-
let managementMainSessionKey = null;
|
|
1489
|
-
if (managementPolicyInjected && resolvedWorkspaceRoot) {
|
|
1490
|
-
try {
|
|
1491
|
-
const sessionDirectory = await readClaworldSessionDirectory(resolvedWorkspaceRoot);
|
|
1492
|
-
managementMainSessionKey = normalizeText(
|
|
1493
|
-
sessionDirectory.directory?.main?.lastActiveSessionKey,
|
|
1494
|
-
null,
|
|
1495
|
-
);
|
|
1496
|
-
} catch {
|
|
1497
|
-
managementMainSessionKey = null;
|
|
1498
|
-
}
|
|
1499
|
-
}
|
|
1500
734
|
const parts = [];
|
|
1501
735
|
let truncated = false;
|
|
1502
736
|
if (pointerInjected) {
|
|
1503
737
|
const pointerBudget = maxTotalChars - measureBootstrapParts(parts) - (parts.length > 0 ? 2 : 0);
|
|
1504
|
-
const fittedPointer = truncateBootstrapText(
|
|
1505
|
-
buildClaworldContextPointer({ workspaceRoot: resolvedWorkspaceRoot }),
|
|
1506
|
-
pointerBudget,
|
|
1507
|
-
);
|
|
738
|
+
const fittedPointer = truncateBootstrapText(buildClaworldContextPointer(), pointerBudget);
|
|
1508
739
|
if (fittedPointer.text) {
|
|
1509
740
|
parts.push(fittedPointer.text);
|
|
1510
741
|
}
|
|
@@ -1512,22 +743,6 @@ export async function buildClaworldBootstrapPromptContext(context = {}, options
|
|
|
1512
743
|
truncated = true;
|
|
1513
744
|
}
|
|
1514
745
|
}
|
|
1515
|
-
if (managementPolicyInjected) {
|
|
1516
|
-
const policyBudget = maxTotalChars - measureBootstrapParts(parts) - (parts.length > 0 ? 2 : 0);
|
|
1517
|
-
const fittedPolicy = truncateBootstrapText(
|
|
1518
|
-
buildClaworldManagementStartupPrompt({
|
|
1519
|
-
workspaceRoot: resolvedWorkspaceRoot,
|
|
1520
|
-
mainSessionKey: managementMainSessionKey,
|
|
1521
|
-
}),
|
|
1522
|
-
policyBudget,
|
|
1523
|
-
);
|
|
1524
|
-
if (fittedPolicy.text) {
|
|
1525
|
-
parts.push(fittedPolicy.text);
|
|
1526
|
-
}
|
|
1527
|
-
if (fittedPolicy.truncated) {
|
|
1528
|
-
truncated = true;
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
746
|
const sectionTitle = target === CLAWORLD_BOOTSTRAP_TARGETS.MAIN
|
|
1532
747
|
? '# Claworld Startup Memory'
|
|
1533
748
|
: target === CLAWORLD_BOOTSTRAP_TARGETS.MANAGEMENT
|
|
@@ -1558,7 +773,6 @@ export async function buildClaworldBootstrapPromptContext(context = {}, options
|
|
|
1558
773
|
workspaceRoot: resolvedWorkspaceRoot,
|
|
1559
774
|
files: selectedFiles.map((relativePath) => buildBootstrapFileLabel(relativePath)),
|
|
1560
775
|
pointerInjected,
|
|
1561
|
-
managementPolicyInjected,
|
|
1562
776
|
fallbackFiles: fileSections.fallbackFiles,
|
|
1563
777
|
omittedFiles: fileSections.omittedFiles,
|
|
1564
778
|
truncated,
|
|
@@ -1620,40 +834,6 @@ function assertAllowedTarget(runType, target) {
|
|
|
1620
834
|
}
|
|
1621
835
|
}
|
|
1622
836
|
|
|
1623
|
-
function normalizeMarkdownNewlines(content) {
|
|
1624
|
-
return String(content ?? '').replace(/\r\n/g, '\n');
|
|
1625
|
-
}
|
|
1626
|
-
|
|
1627
|
-
function assertContextMarkdownUsesBullets(target, normalizedContent, schema) {
|
|
1628
|
-
const maxBulletLength = schema.maxBulletLength || null;
|
|
1629
|
-
const lines = normalizedContent.split('\n');
|
|
1630
|
-
for (const line of lines) {
|
|
1631
|
-
const trimmed = line.trim();
|
|
1632
|
-
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
1633
|
-
if (!trimmed.startsWith('- ')) {
|
|
1634
|
-
throw new Error(`Claworld maintenance patch for ${target} must use bullet lines under schema sections.`);
|
|
1635
|
-
}
|
|
1636
|
-
if (maxBulletLength && trimmed.length > maxBulletLength) {
|
|
1637
|
-
throw new Error(`Claworld maintenance patch for ${target} has a bullet longer than ${maxBulletLength} characters.`);
|
|
1638
|
-
}
|
|
1639
|
-
}
|
|
1640
|
-
}
|
|
1641
|
-
|
|
1642
|
-
function assertContextFileSchema(target, content) {
|
|
1643
|
-
const schema = CLAWORLD_CONTEXT_FILE_SCHEMAS[target];
|
|
1644
|
-
if (!schema) return;
|
|
1645
|
-
const normalizedContent = normalizeMarkdownNewlines(content);
|
|
1646
|
-
if (!normalizedContent.startsWith(`${schema.title}\n`)) {
|
|
1647
|
-
throw new Error(`Claworld maintenance patch for ${target} must start with ${schema.title}.`);
|
|
1648
|
-
}
|
|
1649
|
-
for (const heading of schema.headings) {
|
|
1650
|
-
if (!normalizedContent.includes(`\n${heading}\n`)) {
|
|
1651
|
-
throw new Error(`Claworld maintenance patch for ${target} is missing required section ${heading}.`);
|
|
1652
|
-
}
|
|
1653
|
-
}
|
|
1654
|
-
assertContextMarkdownUsesBullets(target, normalizedContent, schema);
|
|
1655
|
-
}
|
|
1656
|
-
|
|
1657
837
|
function normalizeReportPatch(report, index) {
|
|
1658
838
|
const filename = normalizeText(report?.filename ?? report?.name, `report-${index + 1}.md`);
|
|
1659
839
|
const normalizedFilename = path.posix.basename(filename.endsWith('.md') ? filename : `${filename}.md`);
|
|
@@ -1710,10 +890,10 @@ export function normalizeClaworldMaintenanceOutput(runType, output = {}, options
|
|
|
1710
890
|
patches.push(normalizeFilePatchValue(output.memoryMd, CLAWORLD_WORKING_MEMORY_FILES.memory, 'memoryMd'));
|
|
1711
891
|
}
|
|
1712
892
|
if (Object.prototype.hasOwnProperty.call(output, 'journalAppendMd')) {
|
|
1713
|
-
const
|
|
893
|
+
const monthKey = toMonthKey(options.timestamp || output.timestamp || Date.now());
|
|
1714
894
|
patches.push({
|
|
1715
895
|
operation: 'append_section',
|
|
1716
|
-
target: `${CLAWORLD_JOURNAL_DIR}/${
|
|
896
|
+
target: `${CLAWORLD_JOURNAL_DIR}/${monthKey}.md`,
|
|
1717
897
|
content: String(output.journalAppendMd ?? ''),
|
|
1718
898
|
});
|
|
1719
899
|
}
|
|
@@ -1746,9 +926,6 @@ export function normalizeClaworldMaintenanceOutput(runType, output = {}, options
|
|
|
1746
926
|
target,
|
|
1747
927
|
content: String(patch.content ?? ''),
|
|
1748
928
|
};
|
|
1749
|
-
if (operation === 'replace') {
|
|
1750
|
-
assertContextFileSchema(target, normalizedPatch.content);
|
|
1751
|
-
}
|
|
1752
929
|
const rationale = normalizeText(patch.rationale, null);
|
|
1753
930
|
if (rationale) normalizedPatch.rationale = rationale;
|
|
1754
931
|
return normalizedPatch;
|
|
@@ -1778,8 +955,8 @@ async function applyMaintenancePatch(workspaceRoot, patch) {
|
|
|
1778
955
|
await fs.mkdir(path.dirname(absolutePath), { recursive: true });
|
|
1779
956
|
const currentContent = await readTextIfPresent(absolutePath);
|
|
1780
957
|
if (currentContent == null) {
|
|
1781
|
-
const
|
|
1782
|
-
await atomicWriteText(absolutePath, `# Claworld Journal ${
|
|
958
|
+
const monthKey = path.basename(patch.target, '.md');
|
|
959
|
+
await atomicWriteText(absolutePath, `# Claworld Journal ${monthKey}\n\n${patch.content}`, {
|
|
1783
960
|
backup: false,
|
|
1784
961
|
rejectEmptyOverwrite: false,
|
|
1785
962
|
});
|