@pleri/olam-cli 0.1.158 → 0.1.160
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-stream/agent-sdk-to-chunks.js +3 -0
- package/dist/agent-stream/driver-runner.js +9 -4
- package/dist/agent-stream/host-driver-launch.js +48 -0
- package/dist/commands/doctor.d.ts +21 -10
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +95 -39
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/flywheel/check-persona-skeleton.d.ts +30 -2
- package/dist/commands/flywheel/check-persona-skeleton.d.ts.map +1 -1
- package/dist/commands/flywheel/check-persona-skeleton.js +143 -6
- package/dist/commands/flywheel/check-persona-skeleton.js.map +1 -1
- package/dist/commands/flywheel/diversity-check.d.ts +12 -2
- package/dist/commands/flywheel/diversity-check.d.ts.map +1 -1
- package/dist/commands/flywheel/diversity-check.js +56 -6
- package/dist/commands/flywheel/diversity-check.js.map +1 -1
- package/dist/commands/flywheel/index.d.ts.map +1 -1
- package/dist/commands/flywheel/index.js +2 -0
- package/dist/commands/flywheel/index.js.map +1 -1
- package/dist/commands/flywheel/install-shims.d.ts +36 -3
- package/dist/commands/flywheel/install-shims.d.ts.map +1 -1
- package/dist/commands/flywheel/install-shims.js +118 -7
- package/dist/commands/flywheel/install-shims.js.map +1 -1
- package/dist/commands/flywheel/k10-measure.d.ts +12 -2
- package/dist/commands/flywheel/k10-measure.d.ts.map +1 -1
- package/dist/commands/flywheel/k10-measure.js +55 -6
- package/dist/commands/flywheel/k10-measure.js.map +1 -1
- package/dist/commands/flywheel/migrate-overlays.d.ts +115 -0
- package/dist/commands/flywheel/migrate-overlays.d.ts.map +1 -0
- package/dist/commands/flywheel/migrate-overlays.js +766 -0
- package/dist/commands/flywheel/migrate-overlays.js.map +1 -0
- package/dist/commands/flywheel/sanitize-persona-output.d.ts +33 -2
- package/dist/commands/flywheel/sanitize-persona-output.d.ts.map +1 -1
- package/dist/commands/flywheel/sanitize-persona-output.js +94 -6
- package/dist/commands/flywheel/sanitize-persona-output.js.map +1 -1
- package/dist/commands/memory/index.d.ts.map +1 -1
- package/dist/commands/memory/index.js +2 -0
- package/dist/commands/memory/index.js.map +1 -1
- package/dist/commands/memory/install-hooks.d.ts +22 -0
- package/dist/commands/memory/install-hooks.d.ts.map +1 -0
- package/dist/commands/memory/install-hooks.js +156 -0
- package/dist/commands/memory/install-hooks.js.map +1 -0
- package/dist/commands/skills-doctor.js +2 -2
- package/dist/commands/skills-doctor.js.map +1 -1
- package/dist/commands/skills-source.d.ts.map +1 -1
- package/dist/commands/skills-source.js +10 -0
- package/dist/commands/skills-source.js.map +1 -1
- package/dist/commands/skills.d.ts.map +1 -1
- package/dist/commands/skills.js +169 -1
- package/dist/commands/skills.js.map +1 -1
- package/dist/image-digests.json +7 -7
- package/dist/index.js +3592 -905
- package/dist/index.js.map +1 -1
- package/dist/lib/flywheel-probes.d.ts +58 -0
- package/dist/lib/flywheel-probes.d.ts.map +1 -0
- package/dist/lib/flywheel-probes.js +163 -0
- package/dist/lib/flywheel-probes.js.map +1 -0
- package/dist/lib/host-side-proxy.d.ts +67 -0
- package/dist/lib/host-side-proxy.d.ts.map +1 -0
- package/dist/lib/host-side-proxy.js +177 -0
- package/dist/lib/host-side-proxy.js.map +1 -0
- package/dist/lib/shim-generator.d.ts +51 -0
- package/dist/lib/shim-generator.d.ts.map +1 -0
- package/dist/lib/shim-generator.js +88 -0
- package/dist/lib/shim-generator.js.map +1 -0
- package/dist/lib/skills-apply-overlays.d.ts +35 -0
- package/dist/lib/skills-apply-overlays.d.ts.map +1 -0
- package/dist/lib/skills-apply-overlays.js +243 -0
- package/dist/lib/skills-apply-overlays.js.map +1 -0
- package/dist/lib/upgrade-kubernetes.d.ts +13 -12
- package/dist/lib/upgrade-kubernetes.d.ts.map +1 -1
- package/dist/lib/upgrade-kubernetes.js +87 -134
- package/dist/lib/upgrade-kubernetes.js.map +1 -1
- package/dist/mcp-server.js +1106 -453
- package/hermes-bundle/version.json +1 -1
- package/host-cp/k8s/host-side/docker-socket-proxy.compose.yaml +58 -0
- package/host-cp/k8s/manifests/50-deployment.yaml +47 -70
- package/host-cp/k8s/manifests/auth-service/50-deployment.yaml +1 -1
- package/host-cp/k8s/manifests/docker-socket-proxy/60-service.yaml +37 -0
- package/host-cp/k8s/manifests/kg-service/50-deployment.yaml +1 -1
- package/host-cp/k8s/manifests/mcp-auth-service/50-deployment.yaml +1 -1
- package/host-cp/k8s/manifests/memory-service/30-configmap.yaml +11 -0
- package/host-cp/k8s/manifests/memory-service/35-configmap-iii-config.yaml +76 -0
- package/host-cp/k8s/manifests/memory-service/50-deployment.yaml +11 -1
- package/host-cp/src/crystallize-planning.mjs +261 -0
- package/host-cp/src/plan-chat-service.mjs +84 -2
- package/host-cp/src/planning-sessions.mjs +270 -0
- package/host-cp/src/server.mjs +9 -0
- package/host-cp/src/tasks-route.mjs +191 -0
- package/package.json +1 -1
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// crystallize-planning — atomic-or-compensating chunk-copy from a planning
|
|
2
|
+
// session (_planning world) into a freshly provisioned real world.
|
|
3
|
+
//
|
|
4
|
+
// APPEND-ONLY CONSTRAINT: The chunks table has a NO_DELETE + NO_UPDATE
|
|
5
|
+
// trigger (chunks_append_only_trigger). If chunk-copy fails mid-batch,
|
|
6
|
+
// any chunks already INSERTed under the new worldId STAY in the database.
|
|
7
|
+
// Compensating cleanup only calls destroyWorld (world container teardown) —
|
|
8
|
+
// it CANNOT delete the orphaned chunks. Those orphan chunks are harmless:
|
|
9
|
+
// • idx_chunks_planning only covers world_id='_planning' rows.
|
|
10
|
+
// • The destroyed world container no longer exists, so no subscriber
|
|
11
|
+
// will ever observe those orphans through the normal shape proxy.
|
|
12
|
+
// • Any future re-crystallize creates a fresh worldId, fresh session_id.
|
|
13
|
+
//
|
|
14
|
+
// IDEMPOTENCY:
|
|
15
|
+
// • If crystallize_status is 'crystallized' (with a stored worldId),
|
|
16
|
+
// return immediately — the work is already done.
|
|
17
|
+
// • If crystallize_status is 'in_progress', we cannot safely resume
|
|
18
|
+
// (we don't know how far the previous copy got, and the chunk INSERT
|
|
19
|
+
// is not idempotent by worldId+sessionId alone — the PRIMARY KEY is
|
|
20
|
+
// (message_id, seq), so the same chunk could be re-inserted into a
|
|
21
|
+
// different new session without collision). Safe behavior: return
|
|
22
|
+
// the current status so the UI can display "in progress" and the
|
|
23
|
+
// operator can force-retry after manual inspection.
|
|
24
|
+
//
|
|
25
|
+
// SLUG RULE: lowercased, non-alphanum → hyphens, max 40 chars.
|
|
26
|
+
// Matches the dev-substrate stub in plan-chat-spa/src/server/index.ts
|
|
27
|
+
// (confirmed by reading that file's crystallize stub, around line 983).
|
|
28
|
+
|
|
29
|
+
import { randomUUID } from 'node:crypto';
|
|
30
|
+
import { PLANNING_WORLD_ID } from '@olam/chunks/schema';
|
|
31
|
+
import { setCrystallizeStatus } from './planning-sessions.mjs';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Slug a plan title into a world-name-safe string.
|
|
35
|
+
* Lowercased, non-alphanum → hyphens, max 40 chars, leading/trailing
|
|
36
|
+
* hyphens removed. Falls back to 'plan' if result is empty.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} title
|
|
39
|
+
* @returns {string}
|
|
40
|
+
*/
|
|
41
|
+
function slugTitle(title) {
|
|
42
|
+
const base = title
|
|
43
|
+
.toLowerCase()
|
|
44
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
45
|
+
.replace(/^-+|-+$/g, '')
|
|
46
|
+
.slice(0, 40);
|
|
47
|
+
return base || 'plan';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Read the current crystallize_status + crystallized_world_id for a session.
|
|
52
|
+
*
|
|
53
|
+
* @param {object} pool
|
|
54
|
+
* @param {string} sessionId
|
|
55
|
+
* @returns {Promise<{crystallize_status: string, crystallized_world_id: string | null}>}
|
|
56
|
+
*/
|
|
57
|
+
async function readCrystallizeState(pool, sessionId) {
|
|
58
|
+
const result = await pool.query(
|
|
59
|
+
`SELECT crystallize_status, crystallized_world_id
|
|
60
|
+
FROM planning_sessions
|
|
61
|
+
WHERE session_id = $1`,
|
|
62
|
+
[sessionId],
|
|
63
|
+
);
|
|
64
|
+
if (result.rows.length === 0) {
|
|
65
|
+
return { crystallize_status: 'open', crystallized_world_id: null };
|
|
66
|
+
}
|
|
67
|
+
const row = result.rows[0];
|
|
68
|
+
return {
|
|
69
|
+
crystallize_status: row.crystallize_status,
|
|
70
|
+
crystallized_world_id: row.crystallized_world_id ?? null,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* SELECT all planning chunks for a session, ordered by seq.
|
|
76
|
+
*
|
|
77
|
+
* @param {object} pool
|
|
78
|
+
* @param {string} sessionId
|
|
79
|
+
* @returns {Promise<Array<{world_id, session_id, message_id, seq, actor_id, actor_type, role, chunk, chunk_type}>>}
|
|
80
|
+
*/
|
|
81
|
+
async function selectPlanningChunks(pool, sessionId) {
|
|
82
|
+
const result = await pool.query(
|
|
83
|
+
`SELECT world_id, session_id, message_id, seq, actor_id, actor_type, role, chunk, chunk_type
|
|
84
|
+
FROM chunks
|
|
85
|
+
WHERE world_id = $1 AND session_id = $2
|
|
86
|
+
ORDER BY seq ASC`,
|
|
87
|
+
[PLANNING_WORLD_ID, sessionId],
|
|
88
|
+
);
|
|
89
|
+
return result.rows;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* INSERT a single chunk into the new world's session.
|
|
94
|
+
* Uses the original message_id + seq verbatim; only world_id and
|
|
95
|
+
* session_id change to point at the new world's session.
|
|
96
|
+
*
|
|
97
|
+
* @param {object} pool
|
|
98
|
+
* @param {object} chunk — row from the planning session
|
|
99
|
+
* @param {string} newWorldId
|
|
100
|
+
* @param {string} newSessionId
|
|
101
|
+
* @returns {Promise<void>}
|
|
102
|
+
*/
|
|
103
|
+
async function insertChunkIntoNewWorld(pool, chunk, newWorldId, newSessionId) {
|
|
104
|
+
await pool.query(
|
|
105
|
+
`INSERT INTO chunks
|
|
106
|
+
(world_id, session_id, message_id, seq, actor_id, actor_type, role, chunk, chunk_type)
|
|
107
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
|
|
108
|
+
[
|
|
109
|
+
newWorldId,
|
|
110
|
+
newSessionId,
|
|
111
|
+
chunk.message_id,
|
|
112
|
+
chunk.seq,
|
|
113
|
+
chunk.actor_id,
|
|
114
|
+
chunk.actor_type,
|
|
115
|
+
chunk.role,
|
|
116
|
+
chunk.chunk,
|
|
117
|
+
chunk.chunk_type,
|
|
118
|
+
],
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* INSERT a system marker chunk into the ORIGINAL planning session to
|
|
124
|
+
* leave an audit trail of crystallization. The marker lands at
|
|
125
|
+
* world_id='_planning' + the original sessionId.
|
|
126
|
+
*
|
|
127
|
+
* @param {object} pool
|
|
128
|
+
* @param {string} sessionId — original planning session id
|
|
129
|
+
* @param {string} worldId — newly created world id
|
|
130
|
+
* @param {number} phaseCount — number of phases in the plan
|
|
131
|
+
* @returns {Promise<void>}
|
|
132
|
+
*/
|
|
133
|
+
async function insertMarkerChunk(pool, sessionId, worldId, phaseCount) {
|
|
134
|
+
const messageId = randomUUID();
|
|
135
|
+
// Find the current max seq so the marker doesn't collide.
|
|
136
|
+
const seqResult = await pool.query(
|
|
137
|
+
`SELECT COALESCE(MAX(seq), -1) AS max_seq
|
|
138
|
+
FROM chunks
|
|
139
|
+
WHERE world_id = $1 AND session_id = $2`,
|
|
140
|
+
[PLANNING_WORLD_ID, sessionId],
|
|
141
|
+
);
|
|
142
|
+
const nextSeq = Number(seqResult.rows[0].max_seq) + 1;
|
|
143
|
+
await pool.query(
|
|
144
|
+
`INSERT INTO chunks
|
|
145
|
+
(world_id, session_id, message_id, seq, actor_id, actor_type, role, chunk, chunk_type)
|
|
146
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
|
|
147
|
+
[
|
|
148
|
+
PLANNING_WORLD_ID,
|
|
149
|
+
sessionId,
|
|
150
|
+
messageId,
|
|
151
|
+
nextSeq,
|
|
152
|
+
'system',
|
|
153
|
+
'system',
|
|
154
|
+
'system',
|
|
155
|
+
`Plan crystallized into world "${worldId}" (${phaseCount} phase${phaseCount === 1 ? '' : 's'}).`,
|
|
156
|
+
'text',
|
|
157
|
+
],
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* crystallizePlanningSession
|
|
163
|
+
*
|
|
164
|
+
* 4-phase atomic-or-compensating process:
|
|
165
|
+
* 1. Set crystallize_status='in_progress'
|
|
166
|
+
* 2. Call createWorld({ name: slugged-title }) → { id: worldId }
|
|
167
|
+
* 3. SELECT all chunks in _planning/sessionId; INSERT each into new world
|
|
168
|
+
* 4. Set crystallize_status='crystallized' (with worldId); INSERT marker chunk
|
|
169
|
+
*
|
|
170
|
+
* Compensating pattern on partial failure:
|
|
171
|
+
* - If createWorld throws: set status='failed', rethrow. destroyWorld NOT called.
|
|
172
|
+
* - If chunk-copy throws mid-batch: set status='failed', call destroyWorld(worldId),
|
|
173
|
+
* rethrow. Orphan chunks already INSERTed stay (append-only; see file header).
|
|
174
|
+
*
|
|
175
|
+
* Idempotency:
|
|
176
|
+
* - Already 'crystallized': return immediately without re-running.
|
|
177
|
+
* - Already 'in_progress': return current status (safe short-circuit; see header).
|
|
178
|
+
*
|
|
179
|
+
* @param {object} opts
|
|
180
|
+
* @param {object} opts.pool — pg.Pool-compatible with .query()
|
|
181
|
+
* @param {string} opts.sessionId — planning session to crystallize
|
|
182
|
+
* @param {string} opts.planTitle — plan title (used for world name slug)
|
|
183
|
+
* @param {Array} opts.planPhases — array of phase objects (name, acceptance, risks?)
|
|
184
|
+
* @param {Function} opts.createWorld — async ({ name }) => { id: string, ... }
|
|
185
|
+
* @param {Function} opts.destroyWorld — async (worldId) => void
|
|
186
|
+
*
|
|
187
|
+
* @returns {Promise<{worldId: string, status: string, new_session_id: string}>}
|
|
188
|
+
* @throws on failure (crystallize_status already set to 'failed' when thrown)
|
|
189
|
+
*/
|
|
190
|
+
export async function crystallizePlanningSession({
|
|
191
|
+
pool,
|
|
192
|
+
sessionId,
|
|
193
|
+
planTitle,
|
|
194
|
+
planPhases,
|
|
195
|
+
createWorld,
|
|
196
|
+
destroyWorld,
|
|
197
|
+
}) {
|
|
198
|
+
// ── Idempotency guard ────────────────────────────────────────────────────
|
|
199
|
+
const currentState = await readCrystallizeState(pool, sessionId);
|
|
200
|
+
|
|
201
|
+
if (currentState.crystallize_status === 'crystallized') {
|
|
202
|
+
return {
|
|
203
|
+
worldId: currentState.crystallized_world_id,
|
|
204
|
+
status: `crystallized:${currentState.crystallized_world_id}`,
|
|
205
|
+
new_session_id: null,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (currentState.crystallize_status === 'in_progress') {
|
|
210
|
+
// Cannot safely resume without knowing how far the copy got.
|
|
211
|
+
// Return current status so the UI shows 'in_progress'.
|
|
212
|
+
return {
|
|
213
|
+
worldId: currentState.crystallized_world_id,
|
|
214
|
+
status: 'in_progress',
|
|
215
|
+
new_session_id: null,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ── Phase 1: mark in_progress ────────────────────────────────────────────
|
|
220
|
+
await setCrystallizeStatus({ pool, sessionId, status: 'in_progress', worldId: null });
|
|
221
|
+
|
|
222
|
+
// ── Phase 2: create world ────────────────────────────────────────────────
|
|
223
|
+
let worldId;
|
|
224
|
+
try {
|
|
225
|
+
const worldName = slugTitle(planTitle);
|
|
226
|
+
const world = await createWorld({ name: worldName });
|
|
227
|
+
worldId = world.id;
|
|
228
|
+
} catch (err) {
|
|
229
|
+
await setCrystallizeStatus({ pool, sessionId, status: 'failed', worldId: null });
|
|
230
|
+
throw err;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ── Phase 3: copy chunks into new world ──────────────────────────────────
|
|
234
|
+
const newSessionId = randomUUID();
|
|
235
|
+
try {
|
|
236
|
+
const chunks = await selectPlanningChunks(pool, sessionId);
|
|
237
|
+
for (const chunk of chunks) {
|
|
238
|
+
await insertChunkIntoNewWorld(pool, chunk, worldId, newSessionId);
|
|
239
|
+
}
|
|
240
|
+
} catch (err) {
|
|
241
|
+
await setCrystallizeStatus({ pool, sessionId, status: 'failed', worldId: null });
|
|
242
|
+
try {
|
|
243
|
+
await destroyWorld(worldId);
|
|
244
|
+
} catch {
|
|
245
|
+
// Compensating destroy failure is non-fatal — the world may already
|
|
246
|
+
// be partially torn down or the destroy operation may not be
|
|
247
|
+
// reversible. Log is left to the caller's context.
|
|
248
|
+
}
|
|
249
|
+
throw err;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ── Phase 4: mark crystallized + insert marker ───────────────────────────
|
|
253
|
+
await setCrystallizeStatus({ pool, sessionId, status: 'crystallized', worldId });
|
|
254
|
+
await insertMarkerChunk(pool, sessionId, worldId, planPhases.length);
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
worldId,
|
|
258
|
+
status: `crystallized:${worldId}`,
|
|
259
|
+
new_session_id: newSessionId,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
@@ -35,6 +35,8 @@ import { Readable } from 'node:stream';
|
|
|
35
35
|
import { URL } from 'node:url';
|
|
36
36
|
import pg from 'pg';
|
|
37
37
|
import { ensureSecret, timingSafeEqual, SECRET_PATH } from './plan-chat-secret.mjs';
|
|
38
|
+
import { listPlanningSessions } from './planning-sessions.mjs';
|
|
39
|
+
import { crystallizePlanningSession } from './crystallize-planning.mjs';
|
|
38
40
|
|
|
39
41
|
const DEFAULT_PORT = 3200;
|
|
40
42
|
const DEFAULT_DB_URL = 'postgres://postgres:spike@localhost:54321/chunks';
|
|
@@ -42,6 +44,7 @@ const DEFAULT_ELECTRIC_URL = 'http://localhost:30001';
|
|
|
42
44
|
|
|
43
45
|
const ACTOR_TYPES = new Set(['agent', 'operator', 'codex', 'system']);
|
|
44
46
|
const ROLES = new Set(['user', 'assistant', 'tool', 'system']);
|
|
47
|
+
const CHUNK_TYPES = new Set(['text', 'tool_use']);
|
|
45
48
|
|
|
46
49
|
// PB2 — scope-ID shape. world_id + session_id query params are interpolated
|
|
47
50
|
// into the upstream Electric `where` clause; the regex IS the SQL-injection
|
|
@@ -172,6 +175,10 @@ function validateChunkInput(body) {
|
|
|
172
175
|
if (!Number.isInteger(body.seq) || body.seq < 0) return 'seq must be a non-negative integer';
|
|
173
176
|
if (typeof body.chunk !== 'string') return 'chunk must be a string';
|
|
174
177
|
if (!ROLES.has(body.role)) return `role must be one of ${[...ROLES].join(', ')}`;
|
|
178
|
+
// Optional chunk_type field — defaults to 'text' when absent.
|
|
179
|
+
if ('chunk_type' in body && !CHUNK_TYPES.has(body.chunk_type)) {
|
|
180
|
+
return `chunk_type must be one of ${[...CHUNK_TYPES].join(', ')}`;
|
|
181
|
+
}
|
|
175
182
|
// actor_id and actor_type from the client are IGNORED (server-derived).
|
|
176
183
|
return null;
|
|
177
184
|
}
|
|
@@ -195,6 +202,8 @@ export function createHandler({
|
|
|
195
202
|
electricUrl,
|
|
196
203
|
shapeDebug,
|
|
197
204
|
shapeDebugLog,
|
|
205
|
+
createWorld,
|
|
206
|
+
destroyWorld,
|
|
198
207
|
}) {
|
|
199
208
|
if (!pool) throw new Error('createHandler: { pool } required');
|
|
200
209
|
if (typeof bearer !== 'string' || bearer.length === 0) {
|
|
@@ -231,8 +240,8 @@ export function createHandler({
|
|
|
231
240
|
try {
|
|
232
241
|
await pool.query(
|
|
233
242
|
`INSERT INTO chunks
|
|
234
|
-
(world_id, session_id, message_id, seq, actor_id, actor_type, role, chunk)
|
|
235
|
-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
|
243
|
+
(world_id, session_id, message_id, seq, actor_id, actor_type, role, chunk, chunk_type)
|
|
244
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
|
|
236
245
|
[
|
|
237
246
|
body.world_id,
|
|
238
247
|
body.session_id,
|
|
@@ -242,6 +251,7 @@ export function createHandler({
|
|
|
242
251
|
principal.actorType,
|
|
243
252
|
body.role,
|
|
244
253
|
body.chunk,
|
|
254
|
+
body.chunk_type ?? 'text',
|
|
245
255
|
],
|
|
246
256
|
);
|
|
247
257
|
} catch (err) {
|
|
@@ -257,6 +267,7 @@ export function createHandler({
|
|
|
257
267
|
seq: body.seq,
|
|
258
268
|
actor_id: principal.actorId,
|
|
259
269
|
actor_type: principal.actorType,
|
|
270
|
+
chunk_type: body.chunk_type ?? 'text',
|
|
260
271
|
});
|
|
261
272
|
}
|
|
262
273
|
|
|
@@ -413,11 +424,82 @@ export function createHandler({
|
|
|
413
424
|
}
|
|
414
425
|
}
|
|
415
426
|
|
|
427
|
+
async function handleGetPlanningSessions(req, res, url) {
|
|
428
|
+
if (!checkAuth(req)) return unauthorized(res);
|
|
429
|
+
// Derive actorId from the bearer principal (no body on GET).
|
|
430
|
+
const principal = principalFromBearer(bearer, null);
|
|
431
|
+
const limitParam = url.searchParams.get('limit');
|
|
432
|
+
const limit = limitParam ? Math.min(parseInt(limitParam, 10) || 50, 200) : 50;
|
|
433
|
+
try {
|
|
434
|
+
const sessions = await listPlanningSessions({ pool, actorId: principal.actorId, limit });
|
|
435
|
+
return send(res, 200, { sessions });
|
|
436
|
+
} catch (err) {
|
|
437
|
+
return send(res, 500, { error: 'list-failed', message: String(err?.message ?? err) });
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
async function handlePostCrystallize(req, res) {
|
|
442
|
+
if (!checkAuth(req)) return unauthorized(res);
|
|
443
|
+
let body;
|
|
444
|
+
try {
|
|
445
|
+
body = await readJson(req);
|
|
446
|
+
} catch (err) {
|
|
447
|
+
return send(res, err?.status ?? 400, { error: err?.message ?? 'bad-request' });
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// Validate required fields: session_id + plan.title + plan.phases
|
|
451
|
+
if (!body || typeof body.session_id !== 'string' || body.session_id.length === 0) {
|
|
452
|
+
return badRequest(res, 'session_id required');
|
|
453
|
+
}
|
|
454
|
+
if (!body.plan || typeof body.plan.title !== 'string' || body.plan.title.length === 0) {
|
|
455
|
+
return badRequest(res, 'plan.title required');
|
|
456
|
+
}
|
|
457
|
+
if (!Array.isArray(body.plan.phases)) {
|
|
458
|
+
return badRequest(res, 'plan.phases must be an array');
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// createWorld + destroyWorld callbacks must be wired at handler creation
|
|
462
|
+
// time (production: WorldManager closures; tests: stubs). If they are not
|
|
463
|
+
// provided, surface a clear 501 rather than crashing.
|
|
464
|
+
if (typeof createWorld !== 'function' || typeof destroyWorld !== 'function') {
|
|
465
|
+
return send(res, 501, {
|
|
466
|
+
ok: false,
|
|
467
|
+
error: 'crystallize-not-wired',
|
|
468
|
+
status: 'failed',
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
try {
|
|
473
|
+
const result = await crystallizePlanningSession({
|
|
474
|
+
pool,
|
|
475
|
+
sessionId: body.session_id,
|
|
476
|
+
planTitle: body.plan.title,
|
|
477
|
+
planPhases: body.plan.phases,
|
|
478
|
+
createWorld,
|
|
479
|
+
destroyWorld,
|
|
480
|
+
});
|
|
481
|
+
return send(res, 200, {
|
|
482
|
+
ok: true,
|
|
483
|
+
created_world_id: result.worldId,
|
|
484
|
+
world_url: result.worldId ? `/world/${result.worldId}` : null,
|
|
485
|
+
status: result.status,
|
|
486
|
+
});
|
|
487
|
+
} catch (err) {
|
|
488
|
+
return send(res, 500, {
|
|
489
|
+
ok: false,
|
|
490
|
+
error: String(err?.message ?? err),
|
|
491
|
+
status: 'failed',
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
416
496
|
return async function handler(req, res) {
|
|
417
497
|
const url = new URL(req.url ?? '/', `http://${req.headers.host}`);
|
|
418
498
|
if (req.method === 'GET' && url.pathname === '/livez') return send(res, 200, { ok: true });
|
|
419
499
|
if (req.method === 'POST' && url.pathname === '/v1/chunks') return handlePostChunks(req, res);
|
|
420
500
|
if (req.method === 'GET' && url.pathname === '/v1/shape') return handleGetShape(req, res, url);
|
|
501
|
+
if (req.method === 'GET' && url.pathname === '/v1/planning-sessions') return handleGetPlanningSessions(req, res, url);
|
|
502
|
+
if (req.method === 'POST' && url.pathname === '/v1/crystallize') return handlePostCrystallize(req, res);
|
|
421
503
|
return send(res, 404, { error: 'not-found' });
|
|
422
504
|
};
|
|
423
505
|
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// planning-sessions — host-cp surface for creating and inspecting in-flight
|
|
2
|
+
// planning sessions stored under world_id = PLANNING_WORLD_ID ('_planning').
|
|
3
|
+
//
|
|
4
|
+
// Formalises what the plan-chat-spa dev substrate does ad hoc:
|
|
5
|
+
//
|
|
6
|
+
// createPlanningSession({ actorId, pool })
|
|
7
|
+
// Seeds a session with one 'system' chunk so the Electric shape subscriber
|
|
8
|
+
// gets a non-empty initial response on its first long-poll cycle. Also
|
|
9
|
+
// INSERTs a row into the planning_sessions sidecar table inside the same
|
|
10
|
+
// transaction so no partial state can exist (chunk written, no metadata row).
|
|
11
|
+
// Returns the allocated world_id, session_id, and the inserted seed chunk.
|
|
12
|
+
//
|
|
13
|
+
// loadPlanningSession({ pool, sessionId })
|
|
14
|
+
// Lightweight metadata read: chunk count, first/last timestamps, first
|
|
15
|
+
// operator content (for title derivation). SPA still streams live chunks
|
|
16
|
+
// via the existing /v1/shape proxy — this is metadata-only.
|
|
17
|
+
//
|
|
18
|
+
// recordPlanningSession({ pool, sessionId, actorId, summary })
|
|
19
|
+
// UPSERT into planning_sessions. Used by createPlanningSession (wrapped in
|
|
20
|
+
// a transaction) and later to update the summary as the session evolves.
|
|
21
|
+
//
|
|
22
|
+
// setCrystallizeStatus({ pool, sessionId, status, worldId })
|
|
23
|
+
// UPDATE planning_sessions.crystallize_status + crystallized_world_id.
|
|
24
|
+
// Throws if status is not in PLANNING_SESSION_STATUSES.
|
|
25
|
+
//
|
|
26
|
+
// listPlanningSessions({ pool, actorId, limit })
|
|
27
|
+
// SELECT rows for actorId, ordered created_at DESC. Returns array.
|
|
28
|
+
//
|
|
29
|
+
// Neither function calls validateChunkInput — that's for the public POST
|
|
30
|
+
// surface. INSERTs here are built directly against the chunks column list.
|
|
31
|
+
//
|
|
32
|
+
// Pool errors surface loudly (never swallowed) so the caller sees the full
|
|
33
|
+
// pg error message and can diagnose connectivity or constraint failures.
|
|
34
|
+
|
|
35
|
+
import { randomUUID } from 'node:crypto';
|
|
36
|
+
import { PLANNING_WORLD_ID, PLANNING_SESSION_STATUSES } from '@olam/chunks/schema';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* UPSERT a row in planning_sessions for the given sessionId.
|
|
40
|
+
*
|
|
41
|
+
* On first call (from createPlanningSession): inserts a fresh row.
|
|
42
|
+
* On subsequent calls: updates summary + updated_at only (leaves
|
|
43
|
+
* crystallize_status and crystallized_world_id untouched).
|
|
44
|
+
*
|
|
45
|
+
* @param {object} opts
|
|
46
|
+
* @param {object} opts.pool
|
|
47
|
+
* @param {string} opts.sessionId
|
|
48
|
+
* @param {string} opts.actorId
|
|
49
|
+
* @param {string | null} [opts.summary]
|
|
50
|
+
*/
|
|
51
|
+
export async function recordPlanningSession({ pool, sessionId, actorId, summary = null }) {
|
|
52
|
+
await pool.query(
|
|
53
|
+
`INSERT INTO planning_sessions (session_id, actor_id, summary)
|
|
54
|
+
VALUES ($1, $2, $3)
|
|
55
|
+
ON CONFLICT (session_id) DO UPDATE
|
|
56
|
+
SET summary = EXCLUDED.summary,
|
|
57
|
+
updated_at = NOW()`,
|
|
58
|
+
[sessionId, actorId, summary],
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Create a new in-flight planning session under world_id='_planning'.
|
|
64
|
+
*
|
|
65
|
+
* Allocates a fresh session_id (UUID v4) and, inside a single transaction:
|
|
66
|
+
* 1. INSERTs a seed chunk (actor_type='system', seq=0) so the Electric shape
|
|
67
|
+
* subscriber receives a non-empty initial long-poll response.
|
|
68
|
+
* 2. INSERTs a planning_sessions sidecar row (via recordPlanningSession) so
|
|
69
|
+
* listPlanningSessions can return it immediately.
|
|
70
|
+
*
|
|
71
|
+
* Transaction guarantee: both INSERTs succeed or both roll back. A chunk
|
|
72
|
+
* written without a corresponding planning_sessions row is the partial-state
|
|
73
|
+
* bug this transaction prevents.
|
|
74
|
+
*
|
|
75
|
+
* @param {object} opts
|
|
76
|
+
* @param {string} opts.actorId — actor_id to attribute the seed chunk to
|
|
77
|
+
* (typically 'system' or the host-cp service id)
|
|
78
|
+
* @param {object} opts.pool — pg.Pool-compatible object with .query() and
|
|
79
|
+
* optionally .connect() for transactional clients.
|
|
80
|
+
* Tests may supply a stub with a transaction-aware
|
|
81
|
+
* .query() (BEGIN / INSERT / INSERT / COMMIT).
|
|
82
|
+
* @returns {Promise<{
|
|
83
|
+
* world_id: string,
|
|
84
|
+
* session_id: string,
|
|
85
|
+
* seed_chunk: {
|
|
86
|
+
* world_id: string, session_id: string, message_id: string, seq: number,
|
|
87
|
+
* actor_id: string, actor_type: string, role: string, chunk: string,
|
|
88
|
+
* chunk_type: string,
|
|
89
|
+
* },
|
|
90
|
+
* }>}
|
|
91
|
+
*/
|
|
92
|
+
export async function createPlanningSession({ actorId, pool }) {
|
|
93
|
+
const sessionId = randomUUID();
|
|
94
|
+
const messageId = randomUUID();
|
|
95
|
+
const seq = 0;
|
|
96
|
+
const actorType = 'system';
|
|
97
|
+
const role = 'system';
|
|
98
|
+
const chunk = 'Planning session created.';
|
|
99
|
+
const chunkType = 'text';
|
|
100
|
+
|
|
101
|
+
// Use a transactional client when pool.connect() is available (real pg.Pool).
|
|
102
|
+
// Test stubs that only implement .query() fall through to the flat path;
|
|
103
|
+
// the transactional contract is proven by the test that uses a stub whose
|
|
104
|
+
// second .query() throws and asserts the chunk INSERT was rolled back.
|
|
105
|
+
if (typeof pool.connect === 'function') {
|
|
106
|
+
const client = await pool.connect();
|
|
107
|
+
try {
|
|
108
|
+
await client.query('BEGIN');
|
|
109
|
+
await client.query(
|
|
110
|
+
`INSERT INTO chunks
|
|
111
|
+
(world_id, session_id, message_id, seq, actor_id, actor_type, role, chunk, chunk_type)
|
|
112
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
|
|
113
|
+
[PLANNING_WORLD_ID, sessionId, messageId, seq, actorId, actorType, role, chunk, chunkType],
|
|
114
|
+
);
|
|
115
|
+
await recordPlanningSession({ pool: client, sessionId, actorId, summary: null });
|
|
116
|
+
await client.query('COMMIT');
|
|
117
|
+
} catch (err) {
|
|
118
|
+
await client.query('ROLLBACK');
|
|
119
|
+
throw err;
|
|
120
|
+
} finally {
|
|
121
|
+
client.release();
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
// Flat path for test stubs: queries run sequentially on the stub pool.
|
|
125
|
+
await pool.query(
|
|
126
|
+
`INSERT INTO chunks
|
|
127
|
+
(world_id, session_id, message_id, seq, actor_id, actor_type, role, chunk, chunk_type)
|
|
128
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
|
|
129
|
+
[PLANNING_WORLD_ID, sessionId, messageId, seq, actorId, actorType, role, chunk, chunkType],
|
|
130
|
+
);
|
|
131
|
+
await recordPlanningSession({ pool, sessionId, actorId, summary: null });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
world_id: PLANNING_WORLD_ID,
|
|
136
|
+
session_id: sessionId,
|
|
137
|
+
seed_chunk: {
|
|
138
|
+
world_id: PLANNING_WORLD_ID,
|
|
139
|
+
session_id: sessionId,
|
|
140
|
+
message_id: messageId,
|
|
141
|
+
seq,
|
|
142
|
+
actor_id: actorId,
|
|
143
|
+
actor_type: actorType,
|
|
144
|
+
role,
|
|
145
|
+
chunk,
|
|
146
|
+
chunk_type: chunkType,
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Update crystallize_status (and optionally crystallized_world_id) for a
|
|
153
|
+
* planning session.
|
|
154
|
+
*
|
|
155
|
+
* @param {object} opts
|
|
156
|
+
* @param {object} opts.pool
|
|
157
|
+
* @param {string} opts.sessionId
|
|
158
|
+
* @param {string} opts.status — must be in PLANNING_SESSION_STATUSES
|
|
159
|
+
* @param {string | null} [opts.worldId] — required when status='crystallized'
|
|
160
|
+
*/
|
|
161
|
+
export async function setCrystallizeStatus({ pool, sessionId, status, worldId = null }) {
|
|
162
|
+
if (!PLANNING_SESSION_STATUSES.includes(status)) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`setCrystallizeStatus: invalid status "${status}"; ` +
|
|
165
|
+
`must be one of ${PLANNING_SESSION_STATUSES.join(', ')}`,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
await pool.query(
|
|
169
|
+
`UPDATE planning_sessions
|
|
170
|
+
SET crystallize_status = $2,
|
|
171
|
+
crystallized_world_id = $3,
|
|
172
|
+
updated_at = NOW()
|
|
173
|
+
WHERE session_id = $1`,
|
|
174
|
+
[sessionId, status, worldId],
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* List planning sessions for a given actorId, ordered by created_at DESC.
|
|
180
|
+
*
|
|
181
|
+
* @param {object} opts
|
|
182
|
+
* @param {object} opts.pool
|
|
183
|
+
* @param {string} opts.actorId
|
|
184
|
+
* @param {number} [opts.limit=50]
|
|
185
|
+
* @returns {Promise<Array<{
|
|
186
|
+
* session_id: string,
|
|
187
|
+
* summary: string | null,
|
|
188
|
+
* crystallize_status: string,
|
|
189
|
+
* crystallized_world_id: string | null,
|
|
190
|
+
* created_at: string,
|
|
191
|
+
* updated_at: string,
|
|
192
|
+
* }>>}
|
|
193
|
+
*/
|
|
194
|
+
export async function listPlanningSessions({ pool, actorId, limit = 50 }) {
|
|
195
|
+
const result = await pool.query(
|
|
196
|
+
`SELECT session_id, summary, crystallize_status, crystallized_world_id,
|
|
197
|
+
created_at, updated_at
|
|
198
|
+
FROM planning_sessions
|
|
199
|
+
WHERE actor_id = $1
|
|
200
|
+
ORDER BY created_at DESC
|
|
201
|
+
LIMIT $2`,
|
|
202
|
+
[actorId, limit],
|
|
203
|
+
);
|
|
204
|
+
return result.rows;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Load lightweight metadata for an existing in-flight planning session.
|
|
209
|
+
*
|
|
210
|
+
* Performs two queries scoped to world_id='_planning' AND session_id=<sessionId>:
|
|
211
|
+
* 1. Aggregate: chunk_count, first_chunk_at, last_chunk_at.
|
|
212
|
+
* 2. First operator content: earliest chunk where actor_type='operator',
|
|
213
|
+
* used by the SPA for session title derivation.
|
|
214
|
+
*
|
|
215
|
+
* The SPA streams live chunks via the existing /v1/shape proxy; this function
|
|
216
|
+
* is metadata-only and does NOT subscribe to any Electric shape.
|
|
217
|
+
*
|
|
218
|
+
* @param {object} opts
|
|
219
|
+
* @param {object} opts.pool — pg.Pool-compatible object with a .query(sql, params) method
|
|
220
|
+
* @param {string} opts.sessionId — UUID of the planning session to inspect
|
|
221
|
+
* @returns {Promise<{
|
|
222
|
+
* world_id: string,
|
|
223
|
+
* session_id: string,
|
|
224
|
+
* exists: boolean,
|
|
225
|
+
* chunk_count: number,
|
|
226
|
+
* first_chunk_at: string | null,
|
|
227
|
+
* last_chunk_at: string | null,
|
|
228
|
+
* first_operator_content: string | null,
|
|
229
|
+
* }>}
|
|
230
|
+
*/
|
|
231
|
+
export async function loadPlanningSession({ pool, sessionId }) {
|
|
232
|
+
const aggResult = await pool.query(
|
|
233
|
+
`SELECT COUNT(*) AS chunk_count,
|
|
234
|
+
MIN(created_at) AS first_chunk_at,
|
|
235
|
+
MAX(created_at) AS last_chunk_at
|
|
236
|
+
FROM chunks
|
|
237
|
+
WHERE world_id = $1 AND session_id = $2`,
|
|
238
|
+
[PLANNING_WORLD_ID, sessionId],
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
const row = aggResult.rows[0];
|
|
242
|
+
const chunkCount = Number(row.chunk_count);
|
|
243
|
+
const exists = chunkCount > 0;
|
|
244
|
+
|
|
245
|
+
let firstOperatorContent = null;
|
|
246
|
+
|
|
247
|
+
if (exists) {
|
|
248
|
+
const opResult = await pool.query(
|
|
249
|
+
`SELECT chunk
|
|
250
|
+
FROM chunks
|
|
251
|
+
WHERE world_id = $1 AND session_id = $2 AND actor_type = 'operator'
|
|
252
|
+
ORDER BY created_at ASC
|
|
253
|
+
LIMIT 1`,
|
|
254
|
+
[PLANNING_WORLD_ID, sessionId],
|
|
255
|
+
);
|
|
256
|
+
if (opResult.rows.length > 0) {
|
|
257
|
+
firstOperatorContent = opResult.rows[0].chunk;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
world_id: PLANNING_WORLD_ID,
|
|
263
|
+
session_id: sessionId,
|
|
264
|
+
exists,
|
|
265
|
+
chunk_count: chunkCount,
|
|
266
|
+
first_chunk_at: exists ? row.first_chunk_at : null,
|
|
267
|
+
last_chunk_at: exists ? row.last_chunk_at : null,
|
|
268
|
+
first_operator_content: firstOperatorContent,
|
|
269
|
+
};
|
|
270
|
+
}
|
package/host-cp/src/server.mjs
CHANGED
|
@@ -53,6 +53,7 @@ import {
|
|
|
53
53
|
normalizeName,
|
|
54
54
|
} from './world-names-store.mjs';
|
|
55
55
|
import { createLocalWorldsSource } from './local-worlds-source.mjs';
|
|
56
|
+
import { dispatchTasksRoute } from './tasks-route.mjs';
|
|
56
57
|
import { createPylonWorldsSource } from './pylon-worlds-source.mjs';
|
|
57
58
|
import { composeWorldsSources } from './compose-worlds-sources.mjs';
|
|
58
59
|
import { createWorldPrStateStore } from './world-pr-state.mjs';
|
|
@@ -793,6 +794,14 @@ const server = http.createServer(instrumentHandler('host-cp', async (req, res) =
|
|
|
793
794
|
}));
|
|
794
795
|
}
|
|
795
796
|
|
|
797
|
+
// /api/tasks/* — B2.2: @olam/tasks-write-api mount via pgPoolExecutor
|
|
798
|
+
// adapter. Bearer-auth already applied above; per-request scopes + olamNodeId
|
|
799
|
+
// come from X-Olam-* headers; RLS enforced server-side per D-B-23.
|
|
800
|
+
if (url.pathname.startsWith('/api/tasks')) {
|
|
801
|
+
const handled = await dispatchTasksRoute(req, res, url);
|
|
802
|
+
if (handled) return;
|
|
803
|
+
}
|
|
804
|
+
|
|
796
805
|
// /api/version/status: returns the current version snapshot (baked SHA
|
|
797
806
|
// vs operator's local HEAD). No auth required beyond the existing gate
|
|
798
807
|
// (already applied above). Phase 1 only — detection, no auto-upgrade.
|