@yeaft/webchat-agent 0.1.657 → 0.1.659
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/package.json +1 -1
- package/unify/vp/index.js +0 -7
- package/unify/vp/registry.js +10 -117
- package/unify/vp/core-memory-recall.js +0 -75
- package/unify/vp/engine-binding.js +0 -96
- package/unify/vp/role-instance.js +0 -216
- package/unify/vp/run-turn.js +0 -251
- package/unify/vp/system-prompt.js +0 -311
package/package.json
CHANGED
package/unify/vp/index.js
CHANGED
|
@@ -3,12 +3,5 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
export { parseRoleMd, loadVpFromDir, scanVpLibrary, count, DEFAULT_VP_LIB_DIR } from './vp-store.js';
|
|
6
|
-
export { RoleInstance } from './role-instance.js';
|
|
7
6
|
export { Registry, defaultRegistry } from './registry.js';
|
|
8
7
|
export { VpLoader } from './vp-loader.js';
|
|
9
|
-
|
|
10
|
-
// task-334c
|
|
11
|
-
export { buildSystemPrompt } from './system-prompt.js';
|
|
12
|
-
export { recallCoreMemory, searchCoreMemory } from './core-memory-recall.js';
|
|
13
|
-
export { createEngineBinder } from './engine-binding.js';
|
|
14
|
-
export { createTurnRunner } from './run-turn.js';
|
package/unify/vp/registry.js
CHANGED
|
@@ -1,35 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* registry.js — In-memory VP
|
|
2
|
+
* registry.js — In-memory VP registry.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Holds the process-wide VP map (vpId → VP). VpLoader writes to it on
|
|
5
|
+
* startup + filesystem rescans; vp-bridge.js reads it to serve the
|
|
6
|
+
* `vp_snapshot` and live-diff WS events.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* LRU eviction (per acceptance #3): when active instance count exceeds
|
|
12
|
-
* `softLimit.maxActiveRoleInstances` (default 40), the least-recently-used
|
|
13
|
-
* idle instance is evicted. Instances with `state !== 'idle'` are skipped
|
|
14
|
-
* over during eviction; if no idle candidate exists, the new instance is
|
|
15
|
-
* still created (soft limit — we do not hard-reject).
|
|
8
|
+
* The previous RoleInstance map (per (vpId, groupId) pair, with LRU
|
|
9
|
+
* eviction) was removed in GC.2 — production fans out per-VP via
|
|
10
|
+
* `handleUnifyChat` directly and never instantiated RoleInstance.
|
|
16
11
|
*/
|
|
17
12
|
|
|
18
|
-
import { RoleInstance } from './role-instance.js';
|
|
19
|
-
|
|
20
|
-
const DEFAULT_MAX_ACTIVE_ROLE_INSTANCES = 40;
|
|
21
|
-
|
|
22
13
|
export class Registry {
|
|
23
|
-
constructor(
|
|
14
|
+
constructor() {
|
|
24
15
|
/** @type {Map<string, import('./vp-store.js').VP>} */
|
|
25
16
|
this.vpMap = new Map();
|
|
26
|
-
/** @type {Map<string, RoleInstance>} */
|
|
27
|
-
this.instanceMap = new Map();
|
|
28
|
-
this.softLimit = {
|
|
29
|
-
maxActiveRoleInstances: options.maxActiveRoleInstances ?? DEFAULT_MAX_ACTIVE_ROLE_INSTANCES,
|
|
30
|
-
};
|
|
31
|
-
/** Listeners for eviction / persona-refresh (optional). */
|
|
32
|
-
this._evictListeners = new Set();
|
|
33
17
|
}
|
|
34
18
|
|
|
35
19
|
// ─── VP map ────────────────────────────────────────────────────
|
|
@@ -41,8 +25,8 @@ export class Registry {
|
|
|
41
25
|
|
|
42
26
|
/**
|
|
43
27
|
* Replace a VP's persona fields in-place, preserving identity so any
|
|
44
|
-
*
|
|
45
|
-
*
|
|
28
|
+
* downstream reference keeps its handle stable across hot-reload.
|
|
29
|
+
* Fields copied: name, role, traits, modelHint, persona, personaHash,
|
|
46
30
|
* mtimeMs.
|
|
47
31
|
*/
|
|
48
32
|
updateVpInPlace(next) {
|
|
@@ -56,10 +40,6 @@ export class Registry {
|
|
|
56
40
|
cur.traits = next.traits;
|
|
57
41
|
cur.modelHint = next.modelHint;
|
|
58
42
|
cur.persona = next.persona;
|
|
59
|
-
// task-334c: personaHash must track persona body. 334a-followup added
|
|
60
|
-
// the field; this is the in-place update mirror. (Originally recorded
|
|
61
|
-
// as a 334h nit, but 334c's system-prompt block names the hash in
|
|
62
|
-
// STATIC — carrying a stale hash would bleed into the prompt.)
|
|
63
43
|
cur.personaHash = next.personaHash;
|
|
64
44
|
cur.mtimeMs = next.mtimeMs;
|
|
65
45
|
// dir / memoryDir / id stable
|
|
@@ -68,12 +48,6 @@ export class Registry {
|
|
|
68
48
|
|
|
69
49
|
removeVp(vpId) {
|
|
70
50
|
this.vpMap.delete(vpId);
|
|
71
|
-
// Also drop any RoleInstances bound to a vanished VP.
|
|
72
|
-
for (const [key, ri] of this.instanceMap) {
|
|
73
|
-
if (ri.vpId === vpId) {
|
|
74
|
-
this.instanceMap.delete(key);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
51
|
}
|
|
78
52
|
|
|
79
53
|
getVp(vpId) {
|
|
@@ -87,87 +61,6 @@ export class Registry {
|
|
|
87
61
|
vpCount() {
|
|
88
62
|
return this.vpMap.size;
|
|
89
63
|
}
|
|
90
|
-
|
|
91
|
-
// ─── RoleInstance map ─────────────────────────────────────────
|
|
92
|
-
|
|
93
|
-
_key(groupId, vpId) {
|
|
94
|
-
return `${groupId}::${vpId}`;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Idempotent create: same (vpId, groupId) returns same instance.
|
|
99
|
-
* Triggers LRU eviction when the soft limit is exceeded.
|
|
100
|
-
*
|
|
101
|
-
* @param {string} vpId
|
|
102
|
-
* @param {string} groupId
|
|
103
|
-
* @returns {RoleInstance}
|
|
104
|
-
*/
|
|
105
|
-
getOrCreateRoleInstance(vpId, groupId) {
|
|
106
|
-
const vp = this.vpMap.get(vpId);
|
|
107
|
-
if (!vp) throw new Error(`unknown vpId: ${vpId}`);
|
|
108
|
-
|
|
109
|
-
const key = this._key(groupId, vpId);
|
|
110
|
-
const existing = this.instanceMap.get(key);
|
|
111
|
-
if (existing) {
|
|
112
|
-
existing.touch();
|
|
113
|
-
return existing;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const ri = new RoleInstance({ vp, groupId });
|
|
117
|
-
this.instanceMap.set(key, ri);
|
|
118
|
-
this._maybeEvict(ri);
|
|
119
|
-
return ri;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
getRoleInstance(vpId, groupId) {
|
|
123
|
-
return this.instanceMap.get(this._key(groupId, vpId));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
dropRoleInstance(vpId, groupId) {
|
|
127
|
-
const key = this._key(groupId, vpId);
|
|
128
|
-
const ri = this.instanceMap.get(key);
|
|
129
|
-
if (!ri) return false;
|
|
130
|
-
this.instanceMap.delete(key);
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
activeRoleInstanceCount() {
|
|
135
|
-
return this.instanceMap.size;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
listRoleInstances() {
|
|
139
|
-
return Array.from(this.instanceMap.values());
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
onEvict(listener) {
|
|
143
|
-
this._evictListeners.add(listener);
|
|
144
|
-
return () => this._evictListeners.delete(listener);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
_maybeEvict(exclude) {
|
|
148
|
-
const limit = this.softLimit.maxActiveRoleInstances;
|
|
149
|
-
if (this.instanceMap.size <= limit) return;
|
|
150
|
-
|
|
151
|
-
// Collect idle instances (excluding the just-created one), sort by
|
|
152
|
-
// lastActivityAt ascending.
|
|
153
|
-
const idle = [];
|
|
154
|
-
for (const ri of this.instanceMap.values()) {
|
|
155
|
-
if (ri === exclude) continue;
|
|
156
|
-
if (ri.state === 'idle') idle.push(ri);
|
|
157
|
-
}
|
|
158
|
-
idle.sort((a, b) => a.lastActivityAt - b.lastActivityAt);
|
|
159
|
-
|
|
160
|
-
while (this.instanceMap.size > limit && idle.length > 0) {
|
|
161
|
-
const victim = idle.shift();
|
|
162
|
-
const key = this._key(victim.groupId, victim.vpId);
|
|
163
|
-
this.instanceMap.delete(key);
|
|
164
|
-
for (const l of this._evictListeners) {
|
|
165
|
-
try { l(victim); } catch { /* ignore */ }
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
// If still over limit because no idle candidates exist, we accept the
|
|
169
|
-
// soft-limit breach — per spec, softLimit is a target, not a hard cap.
|
|
170
|
-
}
|
|
171
64
|
}
|
|
172
65
|
|
|
173
66
|
/** Module-level default registry (convenience). */
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* core-memory-recall.js — single-dimension VP memory recall.
|
|
3
|
-
*
|
|
4
|
-
* Per R3 §Δ2.3: recall is scoped to the CURRENT VP only. The `scope`
|
|
5
|
-
* dimension from R2 was deleted; VP memory is a single store per VP and
|
|
6
|
-
* the recall filter is `{ vp: currentVpId, ... }` — no $or across local /
|
|
7
|
-
* global.
|
|
8
|
-
*
|
|
9
|
-
* This module is a thin shim over whatever memory store RoleInstance has
|
|
10
|
-
* (334f wraps 334o's shard-store). It duck-types the store so 334c does
|
|
11
|
-
* not hard-depend on 334f's final shape:
|
|
12
|
-
*
|
|
13
|
-
* store.query({ vp, shard?, kind?, tags?, limit?, recency? })
|
|
14
|
-
* store.search({ vp, query, limit? }) // optional
|
|
15
|
-
*
|
|
16
|
-
* Return shape: array of entries with at minimum `{ body, shard }`.
|
|
17
|
-
* Additional fields are passed through.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
const DEFAULT_LIMIT = 7;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Recall top-K core memory for a VP.
|
|
24
|
-
*
|
|
25
|
-
* @param {object|null} store — memoryStore (may be null)
|
|
26
|
-
* @param {{ vp: string, limit?: number, shard?: string, kind?: string,
|
|
27
|
-
* tags?: string[] }} opts
|
|
28
|
-
* @returns {Promise<Array<{body:string, shard?:string, [k:string]:any}>>}
|
|
29
|
-
*/
|
|
30
|
-
export async function recallCoreMemory(store, opts = {}) {
|
|
31
|
-
if (!store || typeof store.query !== 'function') return [];
|
|
32
|
-
if (!opts.vp || typeof opts.vp !== 'string') {
|
|
33
|
-
throw new Error('recallCoreMemory: opts.vp (vpId) is required');
|
|
34
|
-
}
|
|
35
|
-
const limit = Number.isInteger(opts.limit) && opts.limit > 0
|
|
36
|
-
? opts.limit
|
|
37
|
-
: DEFAULT_LIMIT;
|
|
38
|
-
|
|
39
|
-
const filter = { vp: opts.vp, limit };
|
|
40
|
-
if (opts.shard) filter.shard = opts.shard;
|
|
41
|
-
if (opts.kind) filter.kind = opts.kind;
|
|
42
|
-
if (opts.tags && opts.tags.length) filter.tags = opts.tags;
|
|
43
|
-
|
|
44
|
-
// Support both sync and async stores.
|
|
45
|
-
const out = await Promise.resolve(store.query(filter));
|
|
46
|
-
if (!Array.isArray(out)) return [];
|
|
47
|
-
return out.slice(0, limit);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Optional keyword-search variant — used by the `memory_search` tool in
|
|
52
|
-
* 334d. Also VP-scoped.
|
|
53
|
-
*
|
|
54
|
-
* @param {object|null} store
|
|
55
|
-
* @param {{ vp: string, query: string, limit?: number }} opts
|
|
56
|
-
*/
|
|
57
|
-
export async function searchCoreMemory(store, opts = {}) {
|
|
58
|
-
if (!store) return [];
|
|
59
|
-
if (!opts.vp || !opts.query) return [];
|
|
60
|
-
const limit = opts.limit || DEFAULT_LIMIT;
|
|
61
|
-
if (typeof store.search === 'function') {
|
|
62
|
-
const out = await Promise.resolve(store.search({ vp: opts.vp, query: opts.query, limit }));
|
|
63
|
-
return Array.isArray(out) ? out.slice(0, limit) : [];
|
|
64
|
-
}
|
|
65
|
-
// Fallback: query + naive substring filter.
|
|
66
|
-
if (typeof store.query === 'function') {
|
|
67
|
-
const all = await Promise.resolve(store.query({ vp: opts.vp, limit: 200 }));
|
|
68
|
-
if (!Array.isArray(all)) return [];
|
|
69
|
-
const needle = opts.query.toLowerCase();
|
|
70
|
-
return all
|
|
71
|
-
.filter((e) => (e.body || '').toLowerCase().includes(needle))
|
|
72
|
-
.slice(0, limit);
|
|
73
|
-
}
|
|
74
|
-
return [];
|
|
75
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* engine-binding.js — bind a single Engine instance to a VP.
|
|
3
|
-
*
|
|
4
|
-
* Per §5.1, Engine is a stateless library: `engine.query({prompt, messages,
|
|
5
|
-
* signal, ...})`. Per §5.2, each RoleInstance runs ITS VP's Engine. We keep
|
|
6
|
-
* one Engine per (vpId) — NOT per RoleInstance — because the Engine itself
|
|
7
|
-
* holds no conversation state; messages live on the RoleInstance. Sharing
|
|
8
|
-
* across groups for the same VP is correct and avoids adapter duplication.
|
|
9
|
-
*
|
|
10
|
-
* When a VP is hot-reloaded (persona body changed → personaHash changed),
|
|
11
|
-
* the cached Engine is still valid — the persona is injected via system
|
|
12
|
-
* prompt, not Engine constructor. We keep the cache stable unless the
|
|
13
|
-
* caller passes a new adapter (e.g. modelHint switched).
|
|
14
|
-
*
|
|
15
|
-
* Hard constraint: 334c does NOT import ../engine.js directly, because
|
|
16
|
-
* that pulls in adapter/config/tool machinery that's owned by the caller
|
|
17
|
-
* (web-bridge / eval runner). Instead this module takes a factory:
|
|
18
|
-
*
|
|
19
|
-
* createEngine(vp) → Engine
|
|
20
|
-
*
|
|
21
|
-
* which the caller supplies when they wire up a Registry.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
const DEFAULT_SCOPE = Symbol.for('yeaft.334c.default-engine-scope');
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Build an engine binder. Stateless helper — state lives on the registry-
|
|
28
|
-
* like cache the caller provides (defaults to an internal WeakMap-style).
|
|
29
|
-
*
|
|
30
|
-
* @param {{
|
|
31
|
-
* createEngine: (vp: import('./vp-store.js').VP) => object,
|
|
32
|
-
* cache?: Map<string, object>, // vpId → Engine
|
|
33
|
-
* }} deps
|
|
34
|
-
*/
|
|
35
|
-
export function createEngineBinder({ createEngine, cache } = {}) {
|
|
36
|
-
if (typeof createEngine !== 'function') {
|
|
37
|
-
throw new Error('createEngineBinder: createEngine(vp) factory is required');
|
|
38
|
-
}
|
|
39
|
-
const store = cache instanceof Map ? cache : new Map();
|
|
40
|
-
// Track personaHash at bind time so the caller can decide whether a
|
|
41
|
-
// later hot-reload warrants re-binding (MVP: we just stash it; engine
|
|
42
|
-
// itself is persona-agnostic).
|
|
43
|
-
const meta = new Map(); // vpId → { personaHash, modelHint }
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
/**
|
|
47
|
-
* Resolve (or lazily create) the Engine for a RoleInstance's VP and
|
|
48
|
-
* attach it onto `ri.engine`. Idempotent.
|
|
49
|
-
*
|
|
50
|
-
* @param {import('./role-instance.js').RoleInstance} ri
|
|
51
|
-
* @returns {object} Engine
|
|
52
|
-
*/
|
|
53
|
-
bind(ri) {
|
|
54
|
-
if (!ri || !ri.vp) throw new Error('bind: role instance required');
|
|
55
|
-
const vp = ri.vp;
|
|
56
|
-
let engine = store.get(vp.id);
|
|
57
|
-
if (!engine) {
|
|
58
|
-
engine = createEngine(vp);
|
|
59
|
-
if (!engine || typeof engine.query !== 'function') {
|
|
60
|
-
throw new Error(`bind: createEngine(${vp.id}) did not return an Engine-like object (missing query)`);
|
|
61
|
-
}
|
|
62
|
-
store.set(vp.id, engine);
|
|
63
|
-
meta.set(vp.id, { personaHash: vp.personaHash, modelHint: vp.modelHint });
|
|
64
|
-
}
|
|
65
|
-
ri.engine = engine;
|
|
66
|
-
return engine;
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Mark a VP as "needs re-bind" — drop the cache entry. The next bind()
|
|
71
|
-
* call will invoke createEngine again. Useful when the caller knows
|
|
72
|
-
* the adapter or tool inventory has changed.
|
|
73
|
-
*/
|
|
74
|
-
invalidate(vpId) {
|
|
75
|
-
const engine = store.get(vpId);
|
|
76
|
-
store.delete(vpId);
|
|
77
|
-
meta.delete(vpId);
|
|
78
|
-
// Best-effort: if the engine exposed a dispose, call it.
|
|
79
|
-
if (engine && typeof engine.dispose === 'function') {
|
|
80
|
-
try { engine.dispose(); } catch { /* ignore */ }
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
|
|
84
|
-
/** Drop every cached engine (e.g. on session teardown). */
|
|
85
|
-
clear() {
|
|
86
|
-
for (const vpId of Array.from(store.keys())) this.invalidate(vpId);
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
/** Diagnostics. */
|
|
90
|
-
size() { return store.size; },
|
|
91
|
-
has(vpId) { return store.has(vpId); },
|
|
92
|
-
getMeta(vpId) { return meta.get(vpId) || null; },
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
void DEFAULT_SCOPE; // reserved for cross-module sharing; not used in MVP
|
|
96
|
-
}
|
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* role-instance.js — RoleInstance class (per-group per-VP runtime handle).
|
|
3
|
-
*
|
|
4
|
-
* task-334 §5 RoleInstance. 334a defined the shape; task-334c adds the
|
|
5
|
-
* lifecycle surface:
|
|
6
|
-
* - state machine: idle → queued → running → (idle | error) + standby
|
|
7
|
-
* - inputQueue: enqueue(envelope) + drain(runner) loop
|
|
8
|
-
* - abort: per-instance AbortController, exposed via abort()
|
|
9
|
-
*
|
|
10
|
-
* Hard constraints (inherited from 334a):
|
|
11
|
-
* - does NOT import 334o shard-store directly (memoryStore stays nullable)
|
|
12
|
-
* - does NOT import 334d routing (inputQueue accepts whatever coordinator
|
|
13
|
-
* hands it; runner is the sole consumer)
|
|
14
|
-
*
|
|
15
|
-
* Hard constraint (334c-specific):
|
|
16
|
-
* - the runner callback is injected — RoleInstance does not know about
|
|
17
|
-
* Engine internals. See run-turn.js for the canonical runner.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @typedef {'idle'|'queued'|'running'|'standby'|'error'} RoleInstanceState
|
|
22
|
-
*
|
|
23
|
-
* - idle: no work, ready to accept
|
|
24
|
-
* - queued: inputQueue has items, drain scheduled/running but this
|
|
25
|
-
* iteration is not yet inside the runner (brief transition)
|
|
26
|
-
* - running: inside runner for one envelope
|
|
27
|
-
* - standby: explicit pause via setState('standby'); enqueue is allowed
|
|
28
|
-
* (buffers) but drain is gated until setState('idle')
|
|
29
|
-
* - error: last turn threw; next enqueue re-enters drain and clears
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
let _seq = 0;
|
|
33
|
-
function nextInstanceId(vpId, groupId) {
|
|
34
|
-
_seq = (_seq + 1) >>> 0;
|
|
35
|
-
return `ri_${groupId}_${vpId}_${Date.now().toString(36)}_${_seq.toString(36)}`;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export class RoleInstance {
|
|
39
|
-
/**
|
|
40
|
-
* @param {{ vp: import('./vp-store.js').VP, groupId: string }} params
|
|
41
|
-
*/
|
|
42
|
-
constructor({ vp, groupId }) {
|
|
43
|
-
if (!vp) throw new Error('RoleInstance requires a vp');
|
|
44
|
-
if (!groupId) throw new Error('RoleInstance requires a groupId');
|
|
45
|
-
|
|
46
|
-
this.id = nextInstanceId(vp.id, groupId);
|
|
47
|
-
this.vpId = vp.id;
|
|
48
|
-
this.groupId = groupId;
|
|
49
|
-
/** Live VP reference. Persona hot-reload mutates fields in place. */
|
|
50
|
-
this.vp = vp;
|
|
51
|
-
|
|
52
|
-
// ── Runtime state (preserved across persona hot-reload) ─────
|
|
53
|
-
/** @type {RoleInstanceState} */
|
|
54
|
-
this.state = 'idle';
|
|
55
|
-
/** Conversation messages appended by run-turn (user + assistant pairs). */
|
|
56
|
-
this.messages = [];
|
|
57
|
-
/** Pending input queue (envelopes from coordinator). */
|
|
58
|
-
this.inputQueue = [];
|
|
59
|
-
/** Abort controller for in-flight run. Created per-turn. */
|
|
60
|
-
this.abortController = null;
|
|
61
|
-
|
|
62
|
-
// ── Nullable sub-system handles (attached by later slices) ──
|
|
63
|
-
/** threadStore — optional (334h). */
|
|
64
|
-
this.threadStore = null;
|
|
65
|
-
/** subagentPool — optional. */
|
|
66
|
-
this.subagentPool = null;
|
|
67
|
-
/** memoryStore — 334f wraps 334o; NOT created here. */
|
|
68
|
-
this.memoryStore = null;
|
|
69
|
-
/** Cached Engine for this VP; bound lazily by engine-binding.js. */
|
|
70
|
-
this.engine = null;
|
|
71
|
-
/** Cached systemPrompt string; rebuilt when vp.mtimeMs changes. */
|
|
72
|
-
this.systemPrompt = null;
|
|
73
|
-
/** mtimeMs seen when systemPrompt was last built — invalidation key. */
|
|
74
|
-
this._promptBuiltForMtime = null;
|
|
75
|
-
|
|
76
|
-
// ── Telemetry ───────────────────────────────────────────────
|
|
77
|
-
this.createdAt = Date.now();
|
|
78
|
-
this.lastActivityAt = this.createdAt;
|
|
79
|
-
/** Total turns executed via drain (error turns included). */
|
|
80
|
-
this.turnCount = 0;
|
|
81
|
-
/** Last error thrown by runner; cleared on next successful turn. */
|
|
82
|
-
this.lastError = null;
|
|
83
|
-
|
|
84
|
-
// ── Drain internals ────────────────────────────────────────
|
|
85
|
-
/** Set while a drain loop is active. Prevents re-entry. */
|
|
86
|
-
this._draining = false;
|
|
87
|
-
/** Promise that resolves when the current drain loop exits. */
|
|
88
|
-
this._drainPromise = null;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Update last-activity timestamp (used by LRU eviction). */
|
|
92
|
-
touch() {
|
|
93
|
-
this.lastActivityAt = Date.now();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** Transition helper. Emits a state change to optional listeners. */
|
|
97
|
-
setState(next) {
|
|
98
|
-
this.state = next;
|
|
99
|
-
this.touch();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Enqueue an envelope for processing. Triggers a drain if one isn't
|
|
104
|
-
* already running.
|
|
105
|
-
*
|
|
106
|
-
* Envelope shape (from coordinator.js makeEnvelope): { groupId, taskId,
|
|
107
|
-
* msg, trigger }. Opaque to RoleInstance — handed verbatim to runner.
|
|
108
|
-
*
|
|
109
|
-
* @param {object} envelope
|
|
110
|
-
* @returns {number} new inputQueue length
|
|
111
|
-
*/
|
|
112
|
-
enqueue(envelope) {
|
|
113
|
-
if (!envelope) throw new Error('enqueue: envelope required');
|
|
114
|
-
this.inputQueue.push(envelope);
|
|
115
|
-
this.touch();
|
|
116
|
-
if (this.state === 'idle') this.setState('queued');
|
|
117
|
-
return this.inputQueue.length;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Abort the in-flight run (if any) and clear the pending queue's
|
|
122
|
-
* ability to advance further by signalling abort. The queue is NOT
|
|
123
|
-
* cleared — that's policy left to the caller (e.g. GroupCoordinator
|
|
124
|
-
* may reshedule on next user message).
|
|
125
|
-
*
|
|
126
|
-
* @param {string} [reason]
|
|
127
|
-
*/
|
|
128
|
-
abort(reason = 'user') {
|
|
129
|
-
if (this.abortController && !this.abortController.signal.aborted) {
|
|
130
|
-
try { this.abortController.abort(reason); } catch { /* ignore */ }
|
|
131
|
-
}
|
|
132
|
-
this.touch();
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Drain the inputQueue by invoking `runner(envelope)` for each. One
|
|
137
|
-
* envelope at a time, FIFO. Re-entry safe: if a drain is already in
|
|
138
|
-
* progress, return the in-flight promise instead of starting a second
|
|
139
|
-
* loop. Standby gates the loop (standby → no runner calls until a
|
|
140
|
-
* subsequent `setState('idle')` + enqueue).
|
|
141
|
-
*
|
|
142
|
-
* The runner owns state transitions into 'running' and back to 'idle'
|
|
143
|
-
* for the iteration it's handling; drain only sets the initial queued
|
|
144
|
-
* / final idle around the whole loop.
|
|
145
|
-
*
|
|
146
|
-
* @param {(envelope: object, ri: RoleInstance) => Promise<void>} runner
|
|
147
|
-
* @returns {Promise<{turns:number, errors:number}>}
|
|
148
|
-
*/
|
|
149
|
-
drain(runner) {
|
|
150
|
-
if (typeof runner !== 'function') {
|
|
151
|
-
return Promise.reject(new Error('drain: runner must be a function'));
|
|
152
|
-
}
|
|
153
|
-
if (this._draining) return this._drainPromise;
|
|
154
|
-
|
|
155
|
-
this._draining = true;
|
|
156
|
-
this._drainPromise = (async () => {
|
|
157
|
-
let turns = 0;
|
|
158
|
-
let errors = 0;
|
|
159
|
-
try {
|
|
160
|
-
while (this.inputQueue.length > 0) {
|
|
161
|
-
// Standby blocks the drain — buffer until caller reactivates.
|
|
162
|
-
if (this.state === 'standby') break;
|
|
163
|
-
const envelope = this.inputQueue.shift();
|
|
164
|
-
this.turnCount += 1;
|
|
165
|
-
turns += 1;
|
|
166
|
-
// Fresh controller per turn so abort(reason) only kills one iter
|
|
167
|
-
// and the next envelope starts with a clean signal.
|
|
168
|
-
this.abortController = new AbortController();
|
|
169
|
-
try {
|
|
170
|
-
this.setState('running');
|
|
171
|
-
await runner(envelope, this);
|
|
172
|
-
this.lastError = null;
|
|
173
|
-
} catch (err) {
|
|
174
|
-
errors += 1;
|
|
175
|
-
this.lastError = err;
|
|
176
|
-
this.setState('error');
|
|
177
|
-
// Propagate abort — don't swallow it; but do stop draining so
|
|
178
|
-
// the caller can decide to requeue / resume.
|
|
179
|
-
if (err && err.name === 'AbortError') break;
|
|
180
|
-
// Non-abort errors: continue draining. The next turn gets a
|
|
181
|
-
// fresh state via setState('running') at the top of the loop.
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
// Settle terminal state only if we didn't bail in 'error' or
|
|
185
|
-
// 'standby'. Keep 'standby' sticky so caller must reactivate.
|
|
186
|
-
if (this.state !== 'standby' && this.state !== 'error') {
|
|
187
|
-
this.setState('idle');
|
|
188
|
-
}
|
|
189
|
-
} finally {
|
|
190
|
-
this._draining = false;
|
|
191
|
-
this._drainPromise = null;
|
|
192
|
-
this.abortController = null;
|
|
193
|
-
}
|
|
194
|
-
return { turns, errors };
|
|
195
|
-
})();
|
|
196
|
-
return this._drainPromise;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Snapshot for debug / telemetry. Excludes bulky messages.
|
|
201
|
-
*/
|
|
202
|
-
snapshot() {
|
|
203
|
-
return {
|
|
204
|
-
id: this.id,
|
|
205
|
-
vpId: this.vpId,
|
|
206
|
-
groupId: this.groupId,
|
|
207
|
-
state: this.state,
|
|
208
|
-
messageCount: this.messages.length,
|
|
209
|
-
queueDepth: this.inputQueue.length,
|
|
210
|
-
turnCount: this.turnCount,
|
|
211
|
-
hasError: Boolean(this.lastError),
|
|
212
|
-
lastActivityAt: this.lastActivityAt,
|
|
213
|
-
createdAt: this.createdAt,
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
}
|
package/unify/vp/run-turn.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* run-turn.js — execute one turn for a RoleInstance.
|
|
3
|
-
*
|
|
4
|
-
* A "turn" = consume one envelope from RoleInstance.inputQueue, build a
|
|
5
|
-
* fresh system prompt (§8), call `engine.query({prompt, messages, signal})`,
|
|
6
|
-
* accumulate the streamed text, and append the resulting assistant
|
|
7
|
-
* message both to `ri.messages` (for next-turn context) and — if the
|
|
8
|
-
* caller provides a GroupHandle — to the group's jsonl log via its
|
|
9
|
-
* `appendMessage` API (§334b public surface; hard constraint c: we never
|
|
10
|
-
* hand-write bytes).
|
|
11
|
-
*
|
|
12
|
-
* This is the canonical runner passed to `RoleInstance.drain(runner)`.
|
|
13
|
-
* Tests substitute a fake engine via createEngineBinder to exercise the
|
|
14
|
-
* drain loop / state machine without a live adapter.
|
|
15
|
-
*
|
|
16
|
-
* Streaming contract — event types handled (all optional; Engine.query is
|
|
17
|
-
* the source of truth):
|
|
18
|
-
* { type: 'text', text: string } — accumulate
|
|
19
|
-
* { type: 'text_chunk', text: string } — accumulate
|
|
20
|
-
* { type: 'tool_call', ... } — pass to onEvent
|
|
21
|
-
* { type: 'tool_end', ... } — pass to onEvent
|
|
22
|
-
* { type: 'aborted', reason } — exit loop, throw AbortError
|
|
23
|
-
* { type: 'error', error } — throw
|
|
24
|
-
* { type: 'turn_end', ... } — exit loop
|
|
25
|
-
*
|
|
26
|
-
* Unknown event types are forwarded to `onEvent(evt)` if provided.
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
import { buildSystemPrompt } from './system-prompt.js';
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Build the taskCtx opt for buildSystemPrompt. Pulls active tasks for the
|
|
33
|
-
* VP's current group from the task store, plus the current task (if any).
|
|
34
|
-
* Returns null if no taskStore is wired (legacy / tests).
|
|
35
|
-
*/
|
|
36
|
-
function collectTaskCtx({ taskStore, groupId, currentTaskId }) {
|
|
37
|
-
if (!taskStore || !groupId) return null;
|
|
38
|
-
let allTasks;
|
|
39
|
-
try {
|
|
40
|
-
allTasks = taskStore.list();
|
|
41
|
-
} catch {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
if (!Array.isArray(allTasks)) return null;
|
|
45
|
-
|
|
46
|
-
const inGroup = allTasks.filter(
|
|
47
|
-
(t) => t && t.groupId === groupId && t.status !== 'completed' && t.status !== 'cancelled',
|
|
48
|
-
);
|
|
49
|
-
// Order by lastActivity / updatedAt desc so most-recent surface first.
|
|
50
|
-
inGroup.sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0));
|
|
51
|
-
|
|
52
|
-
const activeTasks = inGroup.map((t) => ({
|
|
53
|
-
id: t.id,
|
|
54
|
-
title: t.title,
|
|
55
|
-
status: t.status,
|
|
56
|
-
members: Array.isArray(t.members) ? t.members.slice() : [],
|
|
57
|
-
initiator: t.initiator || null,
|
|
58
|
-
lastActivityAt: t.updatedAt || t.createdAt || 0,
|
|
59
|
-
}));
|
|
60
|
-
|
|
61
|
-
let currentTask = null;
|
|
62
|
-
if (currentTaskId) {
|
|
63
|
-
const t = allTasks.find((x) => x && x.id === currentTaskId);
|
|
64
|
-
if (t) {
|
|
65
|
-
currentTask = {
|
|
66
|
-
id: t.id,
|
|
67
|
-
title: t.title,
|
|
68
|
-
members: Array.isArray(t.members) ? t.members.slice() : [],
|
|
69
|
-
initiator: t.initiator || null,
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (activeTasks.length === 0 && !currentTask) return null;
|
|
75
|
-
return { activeTasks, currentTask };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Build a runner suitable for RoleInstance.drain().
|
|
80
|
-
*
|
|
81
|
-
* @param {{
|
|
82
|
-
* binder: import('./engine-binding.js').createEngineBinder extends (...args:any)=>infer R ? R : never,
|
|
83
|
-
* registry?: import('./registry.js').Registry,
|
|
84
|
-
* group?: import('../groups/group-store.js').GroupHandle, // optional persistence
|
|
85
|
-
* rosterMembers?: string[],
|
|
86
|
-
* capabilitiesLine?: string,
|
|
87
|
-
* onEvent?: (evt:any, ri:any) => void,
|
|
88
|
-
* buildPromptOverride?: typeof buildSystemPrompt,
|
|
89
|
-
* }} deps
|
|
90
|
-
*/
|
|
91
|
-
export function createTurnRunner(deps = {}) {
|
|
92
|
-
const {
|
|
93
|
-
binder,
|
|
94
|
-
registry,
|
|
95
|
-
group,
|
|
96
|
-
rosterMembers,
|
|
97
|
-
capabilitiesLine,
|
|
98
|
-
onEvent,
|
|
99
|
-
buildPromptOverride,
|
|
100
|
-
taskStore, // R6 §6 trigger #6: enables task_ctx affiliation hint
|
|
101
|
-
} = deps;
|
|
102
|
-
|
|
103
|
-
if (!binder || typeof binder.bind !== 'function') {
|
|
104
|
-
throw new Error('createTurnRunner: binder (from createEngineBinder) is required');
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const buildPrompt = buildPromptOverride || buildSystemPrompt;
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* The actual runner. Called once per envelope by drain().
|
|
111
|
-
*
|
|
112
|
-
* @param {object} envelope — { groupId, taskId, msg, trigger }
|
|
113
|
-
* @param {import('./role-instance.js').RoleInstance} ri
|
|
114
|
-
*/
|
|
115
|
-
return async function runOneTurn(envelope, ri) {
|
|
116
|
-
if (!envelope || !envelope.msg) {
|
|
117
|
-
throw new Error('runOneTurn: envelope.msg missing');
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const engine = binder.bind(ri);
|
|
121
|
-
|
|
122
|
-
// Fresh system prompt per turn — DYNAMIC section changes every turn.
|
|
123
|
-
const taskCtx = collectTaskCtx({
|
|
124
|
-
taskStore,
|
|
125
|
-
groupId: ri.groupId,
|
|
126
|
-
currentTaskId: envelope.taskId || null,
|
|
127
|
-
});
|
|
128
|
-
const systemPrompt = await buildPrompt(ri, {
|
|
129
|
-
registry,
|
|
130
|
-
rosterMembers,
|
|
131
|
-
capabilitiesLine,
|
|
132
|
-
runtimeCtx: {
|
|
133
|
-
taskId: envelope.taskId || null,
|
|
134
|
-
isDream: false,
|
|
135
|
-
},
|
|
136
|
-
taskCtx,
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
// Prompt text = inbound message body. Engine.query spec:
|
|
140
|
-
// { prompt, messages, signal, ... }
|
|
141
|
-
// It prepends the system prompt via adapter-level wiring; we pass
|
|
142
|
-
// `systemPrompt` as an explicit first message if the engine supports
|
|
143
|
-
// it, else rely on the engine's own system injection. For §334c MVP
|
|
144
|
-
// we pass systemPrompt as metadata on messages[0] and let the engine
|
|
145
|
-
// decide — tests supply a fake engine that echoes back.
|
|
146
|
-
const prompt = String(envelope.msg.text || '').trim();
|
|
147
|
-
if (!prompt) {
|
|
148
|
-
// No-op envelope (empty msg): record a stub and return.
|
|
149
|
-
ri.messages.push({ role: 'user', text: '', ts: new Date().toISOString(), meta: envelope });
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
ri.messages.push({
|
|
154
|
-
role: 'user',
|
|
155
|
-
text: prompt,
|
|
156
|
-
ts: envelope.msg.ts || new Date().toISOString(),
|
|
157
|
-
from: envelope.msg.from || 'user',
|
|
158
|
-
msgId: envelope.msg.id || null,
|
|
159
|
-
trigger: envelope.trigger || null,
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
const signal = ri.abortController ? ri.abortController.signal : undefined;
|
|
163
|
-
|
|
164
|
-
// Build prior-messages window for the engine — MVP: the last 20.
|
|
165
|
-
const priorMessages = ri.messages.slice(-20);
|
|
166
|
-
|
|
167
|
-
let accumulated = '';
|
|
168
|
-
let aborted = false;
|
|
169
|
-
let errored = null;
|
|
170
|
-
|
|
171
|
-
const iterator = engine.query({
|
|
172
|
-
prompt,
|
|
173
|
-
messages: priorMessages,
|
|
174
|
-
signal,
|
|
175
|
-
systemPrompt,
|
|
176
|
-
metadata: {
|
|
177
|
-
vpId: ri.vpId,
|
|
178
|
-
groupId: ri.groupId,
|
|
179
|
-
taskId: envelope.taskId || null,
|
|
180
|
-
turnId: `${ri.id}:${ri.turnCount}`,
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
try {
|
|
185
|
-
for await (const evt of iterator) {
|
|
186
|
-
if (!evt || typeof evt !== 'object') continue;
|
|
187
|
-
switch (evt.type) {
|
|
188
|
-
case 'text':
|
|
189
|
-
case 'text_chunk':
|
|
190
|
-
case 'message':
|
|
191
|
-
if (typeof evt.text === 'string') accumulated += evt.text;
|
|
192
|
-
if (onEvent) try { onEvent(evt, ri); } catch { /* ignore */ }
|
|
193
|
-
break;
|
|
194
|
-
case 'aborted':
|
|
195
|
-
aborted = true;
|
|
196
|
-
if (onEvent) try { onEvent(evt, ri); } catch { /* ignore */ }
|
|
197
|
-
break;
|
|
198
|
-
case 'error':
|
|
199
|
-
errored = evt.error instanceof Error ? evt.error : new Error(String(evt.error || 'engine error'));
|
|
200
|
-
if (onEvent) try { onEvent(evt, ri); } catch { /* ignore */ }
|
|
201
|
-
break;
|
|
202
|
-
case 'turn_end':
|
|
203
|
-
if (onEvent) try { onEvent(evt, ri); } catch { /* ignore */ }
|
|
204
|
-
break;
|
|
205
|
-
default:
|
|
206
|
-
if (onEvent) try { onEvent(evt, ri); } catch { /* ignore */ }
|
|
207
|
-
}
|
|
208
|
-
if (aborted || errored) break;
|
|
209
|
-
}
|
|
210
|
-
} catch (err) {
|
|
211
|
-
if (err && err.name === 'AbortError') {
|
|
212
|
-
aborted = true;
|
|
213
|
-
} else {
|
|
214
|
-
errored = err;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (aborted) {
|
|
219
|
-
const e = new Error('aborted');
|
|
220
|
-
e.name = 'AbortError';
|
|
221
|
-
throw e;
|
|
222
|
-
}
|
|
223
|
-
if (errored) throw errored;
|
|
224
|
-
|
|
225
|
-
// Record assistant reply (even if empty — the turn still counted).
|
|
226
|
-
const assistantMsg = {
|
|
227
|
-
role: 'assistant',
|
|
228
|
-
text: accumulated,
|
|
229
|
-
ts: new Date().toISOString(),
|
|
230
|
-
from: ri.vpId,
|
|
231
|
-
taskId: envelope.taskId || null,
|
|
232
|
-
};
|
|
233
|
-
ri.messages.push(assistantMsg);
|
|
234
|
-
|
|
235
|
-
// Hard constraint (c): persistence goes through 334b's appendMessage.
|
|
236
|
-
if (group && typeof group.appendMessage === 'function' && accumulated.trim()) {
|
|
237
|
-
try {
|
|
238
|
-
group.appendMessage({
|
|
239
|
-
from: ri.vpId,
|
|
240
|
-
role: 'assistant',
|
|
241
|
-
text: accumulated,
|
|
242
|
-
taskId: envelope.taskId || null,
|
|
243
|
-
meta: { trigger: envelope.trigger || null, replyTo: envelope.msg.id || null },
|
|
244
|
-
});
|
|
245
|
-
} catch (err) {
|
|
246
|
-
// Non-fatal: the in-memory message is still recorded on ri.messages.
|
|
247
|
-
if (onEvent) try { onEvent({ type: 'persist_error', error: err }, ri); } catch { /* ignore */ }
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
}
|
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* system-prompt.js — assemble a VP's system prompt per architecture §8.
|
|
3
|
-
*
|
|
4
|
-
* Three sections:
|
|
5
|
-
* § STATIC
|
|
6
|
-
* - identity (yeaft base — short)
|
|
7
|
-
* - vp_persona (role.md persona body)
|
|
8
|
-
* - capabilities (tools) — 334d owns the real list; this slice leaves
|
|
9
|
-
* a stub that names the MVP tool surface ("route_forward" ...).
|
|
10
|
-
*
|
|
11
|
-
* § SEMI-DYNAMIC
|
|
12
|
-
* - group_roster (name + on-line status via Registry.activeCount)
|
|
13
|
-
* - (skills / mcp / user_profile — owned by other slices; 334c emits
|
|
14
|
-
* placeholder only if the caller provides them)
|
|
15
|
-
*
|
|
16
|
-
* § DYNAMIC
|
|
17
|
-
* - runtime_ctx { vpId, groupId, taskId?, isDream=false }
|
|
18
|
-
* - core_memory (recall top-K; vp single-dim per R3 §Δ2.3)
|
|
19
|
-
* - task_ctx (R6 §8: lists active tasks in current group + affiliation
|
|
20
|
-
* hint guiding the LLM to call task_create / continue an
|
|
21
|
-
* existing task / reply at group level — this is the
|
|
22
|
-
* mechanism that lets tasks "auto-emerge" from chat per
|
|
23
|
-
* R6 §6 trigger #6.)
|
|
24
|
-
*
|
|
25
|
-
* Caching: the STATIC persona section is cached per RoleInstance via
|
|
26
|
-
* `ri._promptBuiltForMtime === vp.mtimeMs`. DYNAMIC is always rebuilt.
|
|
27
|
-
*
|
|
28
|
-
* Hard constraint (334c): this module does not touch 334f/334g memory
|
|
29
|
-
* internals. It calls `memoryStore.query({vp})` (duck-typed) and falls
|
|
30
|
-
* back to an empty top-K if the store is null.
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
import { recallCoreMemory } from './core-memory-recall.js';
|
|
34
|
-
|
|
35
|
-
const CORE_MEMORY_TOP_K = 7; // §8
|
|
36
|
-
const ROSTER_STATUS_ONLINE = 'online';
|
|
37
|
-
const ROSTER_STATUS_BUSY = 'busy';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Build one complete system prompt string.
|
|
41
|
-
*
|
|
42
|
-
* @param {import('./role-instance.js').RoleInstance} ri
|
|
43
|
-
* @param {{
|
|
44
|
-
* registry?: import('./registry.js').Registry,
|
|
45
|
-
* rosterMembers?: string[], // explicit override (334b GroupHandle.roster)
|
|
46
|
-
* runtimeCtx?: { taskId?: string|null, isDream?: boolean, recentChatSummary?: string },
|
|
47
|
-
* capabilitiesLine?: string, // 334d injects tool inventory
|
|
48
|
-
* userProfile?: string, // 334l injects top-5 user-memory
|
|
49
|
-
* recentGroupChat?: string, // 334h injects N recent msgs
|
|
50
|
-
* taskCtx?: {
|
|
51
|
-
* activeTasks?: Array<{
|
|
52
|
-
* id: string, title?: string, status?: string,
|
|
53
|
-
* members?: string[], initiator?: string,
|
|
54
|
-
* lastActivityAt?: number,
|
|
55
|
-
* }>, // tasks in this group the VP can join/continue
|
|
56
|
-
* currentTask?: { // the task the VP is currently inside
|
|
57
|
-
* id: string, title?: string, members?: string[], initiator?: string,
|
|
58
|
-
* },
|
|
59
|
-
* relatedTasks?: Array<{ id: string, title?: string, groupId?: string }>,
|
|
60
|
-
* },
|
|
61
|
-
* }} opts
|
|
62
|
-
* @returns {string}
|
|
63
|
-
*/
|
|
64
|
-
export async function buildSystemPrompt(ri, opts = {}) {
|
|
65
|
-
if (!ri || !ri.vp) throw new Error('buildSystemPrompt: role instance required');
|
|
66
|
-
const vp = ri.vp;
|
|
67
|
-
|
|
68
|
-
// ─── § STATIC (cached per mtime) ────────────────────────────
|
|
69
|
-
let staticBlock = ri.systemPrompt;
|
|
70
|
-
if (!staticBlock || ri._promptBuiltForMtime !== vp.mtimeMs) {
|
|
71
|
-
staticBlock = buildStatic(vp, opts.capabilitiesLine);
|
|
72
|
-
ri.systemPrompt = staticBlock;
|
|
73
|
-
ri._promptBuiltForMtime = vp.mtimeMs;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ─── § SEMI-DYNAMIC ─────────────────────────────────────────
|
|
77
|
-
const roster = buildRoster(ri, opts.registry, opts.rosterMembers);
|
|
78
|
-
const userProfile = opts.userProfile ? `\n## user_profile\n${opts.userProfile.trim()}\n` : '';
|
|
79
|
-
|
|
80
|
-
// ─── § DYNAMIC ──────────────────────────────────────────────
|
|
81
|
-
const ctx = opts.runtimeCtx || {};
|
|
82
|
-
const runtime = buildRuntimeCtx(ri, ctx);
|
|
83
|
-
const recent = opts.recentGroupChat
|
|
84
|
-
? `\n## recent_group_chat\n${opts.recentGroupChat.trim()}\n`
|
|
85
|
-
: '';
|
|
86
|
-
const coreMem = await buildCoreMemoryBlock(ri, ctx);
|
|
87
|
-
const taskCtx = buildTaskCtxBlock(ri, opts.taskCtx, ctx);
|
|
88
|
-
|
|
89
|
-
return [
|
|
90
|
-
'# § STATIC',
|
|
91
|
-
staticBlock,
|
|
92
|
-
'',
|
|
93
|
-
'# § SEMI-DYNAMIC',
|
|
94
|
-
roster,
|
|
95
|
-
userProfile.trim() ? userProfile : '',
|
|
96
|
-
'',
|
|
97
|
-
'# § DYNAMIC',
|
|
98
|
-
runtime,
|
|
99
|
-
recent.trim() ? recent : '',
|
|
100
|
-
coreMem,
|
|
101
|
-
taskCtx,
|
|
102
|
-
]
|
|
103
|
-
.filter(Boolean)
|
|
104
|
-
.join('\n')
|
|
105
|
-
.replace(/\n{3,}/g, '\n\n')
|
|
106
|
-
.trim();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// ──────────────────────────────────────────────────────────────
|
|
110
|
-
// STATIC
|
|
111
|
-
// ──────────────────────────────────────────────────────────────
|
|
112
|
-
|
|
113
|
-
function buildStatic(vp, capabilitiesLine) {
|
|
114
|
-
const identity =
|
|
115
|
-
'You are a Virtual Person (VP) in a yeaft multi-agent group.\n' +
|
|
116
|
-
'Respect §6 triggers: text @-mentions do NOT route. Use the `route_forward` tool for explicit dispatch.';
|
|
117
|
-
|
|
118
|
-
const persona = vp.persona && vp.persona.trim()
|
|
119
|
-
? vp.persona.trim()
|
|
120
|
-
: `(no persona body for ${vp.id})`;
|
|
121
|
-
|
|
122
|
-
const caps = (capabilitiesLine && capabilitiesLine.trim())
|
|
123
|
-
|| [
|
|
124
|
-
'Tools: route_forward, memory_search, memory_trace, task_summary_post (if initiator).',
|
|
125
|
-
'Sub-agent fan-out: when a single user task is large enough to benefit from parallel execution,',
|
|
126
|
-
'you MAY spawn sub-agents using the `Agent` tool. Each sub-agent inherits your persona +',
|
|
127
|
-
'voice, gets its own ToolRegistry (without Agent / RouteForward / AskUser to prevent recursion),',
|
|
128
|
-
'and runs the same Engine flow. Use `Agent` to spawn (returns agentId), `WaitAgent` to collect',
|
|
129
|
-
'each turn output, `SendMessage` for follow-ups, `CloseAgent` when done. You can fire multiple',
|
|
130
|
-
'`Agent` tool_calls in one assistant turn to launch them in parallel. Pass a self-contained,',
|
|
131
|
-
'markdown mission ("## Goal / ## Context / ## Deliverable / ## Constraints") — the sub-agent',
|
|
132
|
-
'cannot see your conversation history. Only spawn when work is genuinely parallelisable; for',
|
|
133
|
-
'small or strictly-sequential tasks, do it yourself.',
|
|
134
|
-
].join('\n');
|
|
135
|
-
|
|
136
|
-
// personaHash travels in the static block so downstream (334h live-diff)
|
|
137
|
-
// can detect changes without re-hashing.
|
|
138
|
-
return [
|
|
139
|
-
'## identity',
|
|
140
|
-
identity,
|
|
141
|
-
'',
|
|
142
|
-
`## vp_persona (id=${vp.id}, hash=${vp.personaHash || '-'})`,
|
|
143
|
-
`Name: ${vp.name}`,
|
|
144
|
-
vp.role ? `Role: ${vp.role}` : '',
|
|
145
|
-
vp.traits && vp.traits.length ? `Traits: ${vp.traits.join(', ')}` : '',
|
|
146
|
-
'',
|
|
147
|
-
persona,
|
|
148
|
-
'',
|
|
149
|
-
'## capabilities',
|
|
150
|
-
caps,
|
|
151
|
-
].filter(Boolean).join('\n');
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// ──────────────────────────────────────────────────────────────
|
|
155
|
-
// SEMI-DYNAMIC — Roster
|
|
156
|
-
// ──────────────────────────────────────────────────────────────
|
|
157
|
-
|
|
158
|
-
function buildRoster(ri, registry, rosterMembers) {
|
|
159
|
-
const members = Array.isArray(rosterMembers) && rosterMembers.length > 0
|
|
160
|
-
? rosterMembers
|
|
161
|
-
: registry
|
|
162
|
-
? Array.from(new Set(registry.listRoleInstances()
|
|
163
|
-
.filter((r) => r.groupId === ri.groupId)
|
|
164
|
-
.map((r) => r.vpId)))
|
|
165
|
-
: [ri.vpId];
|
|
166
|
-
|
|
167
|
-
const lines = [`## 群成员 (${members.length})`];
|
|
168
|
-
for (const vpId of members) {
|
|
169
|
-
if (vpId === ri.vpId) {
|
|
170
|
-
lines.push(`- 你自己:${vpId}`);
|
|
171
|
-
continue;
|
|
172
|
-
}
|
|
173
|
-
const status = memberStatus(vpId, registry);
|
|
174
|
-
const name = registry?.getVp?.(vpId)?.name || vpId;
|
|
175
|
-
lines.push(`- ${name} (${vpId}) · ${status}`);
|
|
176
|
-
}
|
|
177
|
-
return lines.join('\n');
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function memberStatus(vpId, registry) {
|
|
181
|
-
if (!registry) return ROSTER_STATUS_ONLINE;
|
|
182
|
-
// §8.2: busy if any RoleInstance for this VP has state==='running' in any group.
|
|
183
|
-
// MVP: also 'running' while 'queued'? — spec says "running RoleInstance 数 > 0".
|
|
184
|
-
const ris = registry.listRoleInstances?.() || [];
|
|
185
|
-
const busy = ris.some((r) => r.vpId === vpId && r.state === 'running');
|
|
186
|
-
return busy ? ROSTER_STATUS_BUSY : ROSTER_STATUS_ONLINE;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// ──────────────────────────────────────────────────────────────
|
|
190
|
-
// DYNAMIC
|
|
191
|
-
// ──────────────────────────────────────────────────────────────
|
|
192
|
-
|
|
193
|
-
function buildRuntimeCtx(ri, ctx) {
|
|
194
|
-
return [
|
|
195
|
-
'## runtime_ctx',
|
|
196
|
-
`vpId: ${ri.vpId}`,
|
|
197
|
-
`groupId: ${ri.groupId}`,
|
|
198
|
-
ctx.taskId ? `taskId: ${ctx.taskId}` : null,
|
|
199
|
-
`isDream: ${Boolean(ctx.isDream)}`,
|
|
200
|
-
].filter(Boolean).join('\n');
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
async function buildCoreMemoryBlock(ri, ctx) {
|
|
204
|
-
if (!ri.memoryStore) return '';
|
|
205
|
-
const entries = await recallCoreMemory(ri.memoryStore, {
|
|
206
|
-
vp: ri.vpId,
|
|
207
|
-
limit: CORE_MEMORY_TOP_K,
|
|
208
|
-
});
|
|
209
|
-
if (!entries || entries.length === 0) return '';
|
|
210
|
-
void ctx; // task_ctx injection is 334n's scope; reserved param.
|
|
211
|
-
const lines = ['## core_memory'];
|
|
212
|
-
for (const e of entries) {
|
|
213
|
-
const shard = e.shard || 'general';
|
|
214
|
-
const body = (e.body || '').trim();
|
|
215
|
-
if (!body) continue;
|
|
216
|
-
lines.push(`- [mem:${shard}] ${body}`);
|
|
217
|
-
}
|
|
218
|
-
if (lines.length === 1) return '';
|
|
219
|
-
return lines.join('\n');
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// ──────────────────────────────────────────────────────────────
|
|
223
|
-
// DYNAMIC — task_ctx (R6 §6 trigger #6 + §8)
|
|
224
|
-
//
|
|
225
|
-
// The affiliation hint is the load-bearing piece that lets tasks
|
|
226
|
-
// auto-emerge from chat. Without it, VPs never call task_create —
|
|
227
|
-
// the spec lists the tool but a model with no nudge will just keep
|
|
228
|
-
// chatting at group level. We list active tasks in the group + give
|
|
229
|
-
// crisp guidance on when to (a) continue an existing task, (b) start
|
|
230
|
-
// a new one, or (c) reply at group level.
|
|
231
|
-
// ──────────────────────────────────────────────────────────────
|
|
232
|
-
|
|
233
|
-
function buildTaskCtxBlock(ri, taskCtx, runtimeCtx) {
|
|
234
|
-
if (!taskCtx) return '';
|
|
235
|
-
|
|
236
|
-
const currentTask = taskCtx.currentTask;
|
|
237
|
-
const activeTasks = Array.isArray(taskCtx.activeTasks) ? taskCtx.activeTasks : [];
|
|
238
|
-
const relatedTasks = Array.isArray(taskCtx.relatedTasks) ? taskCtx.relatedTasks : [];
|
|
239
|
-
|
|
240
|
-
const otherTasks = activeTasks
|
|
241
|
-
.filter((t) => t && t.id && (!currentTask || t.id !== currentTask.id))
|
|
242
|
-
.slice(0, 8); // cap to avoid prompt bloat — coordinator orders by recency
|
|
243
|
-
|
|
244
|
-
// Nothing useful to say — drop the block entirely. Without active tasks,
|
|
245
|
-
// a current task, or related tasks, the affiliation_hint has no anchor.
|
|
246
|
-
if (otherTasks.length === 0 && !currentTask && relatedTasks.length === 0) {
|
|
247
|
-
return '';
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const lines = ['## task_ctx'];
|
|
251
|
-
|
|
252
|
-
// (a) currently inside a task → focus on this one
|
|
253
|
-
if (currentTask && currentTask.id) {
|
|
254
|
-
lines.push(`### current_task`);
|
|
255
|
-
lines.push(`- id: ${currentTask.id}`);
|
|
256
|
-
if (currentTask.title) lines.push(`- title: ${currentTask.title}`);
|
|
257
|
-
if (currentTask.initiator) lines.push(`- initiator: ${currentTask.initiator}`);
|
|
258
|
-
if (Array.isArray(currentTask.members) && currentTask.members.length) {
|
|
259
|
-
lines.push(`- members: ${currentTask.members.join(', ')}`);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// (b) other open tasks in the same group — candidates for affiliation
|
|
264
|
-
if (otherTasks.length > 0) {
|
|
265
|
-
lines.push(`### active_tasks_in_group (${otherTasks.length})`);
|
|
266
|
-
for (const t of otherTasks) {
|
|
267
|
-
const parts = [`- ${t.id}`];
|
|
268
|
-
if (t.title) parts.push(`"${t.title}"`);
|
|
269
|
-
if (t.status) parts.push(`(${t.status})`);
|
|
270
|
-
if (Array.isArray(t.members) && t.members.length) {
|
|
271
|
-
parts.push(`members=[${t.members.join(',')}]`);
|
|
272
|
-
}
|
|
273
|
-
lines.push(parts.join(' '));
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// (c) related tasks (recall top-N from §14)
|
|
278
|
-
if (relatedTasks.length > 0) {
|
|
279
|
-
lines.push(`### related_tasks (${relatedTasks.length})`);
|
|
280
|
-
for (const t of relatedTasks.slice(0, 3)) {
|
|
281
|
-
const parts = [`- ${t.id}`];
|
|
282
|
-
if (t.title) parts.push(`"${t.title}"`);
|
|
283
|
-
if (t.groupId && t.groupId !== ri.groupId) parts.push(`(grp:${t.groupId})`);
|
|
284
|
-
lines.push(parts.join(' '));
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// (d) affiliation hint — the actual decision rubric
|
|
289
|
-
// Suppress full hint when already inside a task (focus on continuing).
|
|
290
|
-
const inTask = Boolean(runtimeCtx?.taskId || currentTask?.id);
|
|
291
|
-
if (!inTask) {
|
|
292
|
-
lines.push('');
|
|
293
|
-
lines.push('### affiliation_hint');
|
|
294
|
-
lines.push('Decide BEFORE replying:');
|
|
295
|
-
lines.push('- If this user message clearly continues one of the active_tasks_in_group above,');
|
|
296
|
-
lines.push(' use the `task_message` route (other VPs in that task will see your reply, others will not).');
|
|
297
|
-
lines.push('- If this is a NEW multi-turn collaboration worth tracking (decisions, deliverables,');
|
|
298
|
-
lines.push(' multiple VPs needed), call `TaskCreate` with title + members[] + groupId; you become');
|
|
299
|
-
lines.push(' the task initiator and may post `task_summary_post` to broadcast progress.');
|
|
300
|
-
lines.push('- If this is just a quick question / chitchat / single-shot answer, reply at group level — no task.');
|
|
301
|
-
lines.push('Do NOT create a task for trivial back-and-forth. Do NOT continue a task whose topic clearly diverged.');
|
|
302
|
-
} else {
|
|
303
|
-
lines.push('');
|
|
304
|
-
lines.push('### affiliation_hint');
|
|
305
|
-
lines.push('You are inside a task. Stay focused on its scope. Use `task_summary_post` (initiator only)');
|
|
306
|
-
lines.push('to broadcast progress to the group. Use `route_forward` to hand off; do not silently leave.');
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
if (lines.length === 1) return '';
|
|
310
|
-
return lines.join('\n');
|
|
311
|
-
}
|