intent-prompt-graph 0.1.2 → 0.1.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/package.json +1 -1
- package/src/labeler.ts +57 -4
- package/src/session.ts +42 -0
- package/web/view.ts +10 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "intent-prompt-graph",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A live browser graph of a coding-agent conversation, grouped by intent. Standalone `ipg` CLI plus plugins for OMP and Claude Code that add /ipg.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai-agents",
|
package/src/labeler.ts
CHANGED
|
@@ -34,6 +34,8 @@ export interface GraphExtractInput {
|
|
|
34
34
|
nodes: { id: string; kind: NodeKind; title: string; rounds?: number }[];
|
|
35
35
|
edges: { from: string; to: string; kind: EdgeKind }[];
|
|
36
36
|
};
|
|
37
|
+
/** Present on a tidy-up request: no new turns, the reply only merges nodes to reach about this many. */
|
|
38
|
+
consolidate?: { targetNodes: number };
|
|
37
39
|
turns: RedactedTurn[];
|
|
38
40
|
}
|
|
39
41
|
|
|
@@ -46,16 +48,21 @@ Your goal is to help the human owner understand the entire session in 5 seconds:
|
|
|
46
48
|
4. What decisions, pivots, constraints, or course corrections happened ("decision")
|
|
47
49
|
5. What final paths, architectures, or fixes were chosen/verified ("chosen")
|
|
48
50
|
6. Repetitive check/review/polling loops collapsed into a single node ("loop", with "rounds")
|
|
49
|
-
7.
|
|
51
|
+
7. A tangent that left the main line of work for several turns ("side")
|
|
50
52
|
|
|
51
53
|
Rules:
|
|
52
54
|
- Synthesize crisp, specific titles (<= 8 words). NEVER copy filler openers like "You're right", "Okay", "Not quite", or raw file paths as titles.
|
|
53
55
|
- When the owner rejects or pushes back on an agent proposal, mark the AGENT's rejected proposal as "rejected" (with the owner's objection as "rationale") and branch the owner's new direction as "decision" or "chosen".
|
|
56
|
+
- "rejected" needs the owner saying no. An alternative that was merely not picked is "option". "decision" needs someone deciding something: never use it for a question, a fact, a status report or a caveat.
|
|
57
|
+
- Fewer nodes is better. A node is one step that changed where the session went: a direction the owner set, a proposal that was rejected, a result that shipped or was verified. These do NOT get a node of their own; fold them into the "desc" of the node they belong to: a question the agent simply answered, a caveat, a warning, a next-step note, a side effect, each item of a list of options or steps.
|
|
54
58
|
- Merge repetitive back-and-forth checks (like "chk agn" / "chk now" or repeated CI/blocker status polls) into ONE "loop" node with all their turn refs in "turnIds" and an integer "rounds".
|
|
55
59
|
- Keep the graph concise and high-signal (typically 5-18 nodes). Do not create separate nodes for routine acknowledgements.
|
|
56
60
|
- Connect nodes with directed edges (from -> to) using kinds: "chosen", "rejected", "option", "decision", "loop", "side", with a short edge "label" (<= 4 words). Every non-goal node should be reachable from the goal.
|
|
57
61
|
- Without "existingGraph", return the complete first graph for the given turns.
|
|
58
|
-
- If "existingGraph" is provided, it outlines the graph built so far (node id, kind, title, rounds; edge from, to, kind). Reply with a PATCH for the new turns, NOT the whole graph: "nodes" holds ONLY nodes that are new or changed, and "edges" holds ONLY new edges. NEVER repeat an unchanged node. Nodes you leave out are kept exactly as they are
|
|
62
|
+
- If "existingGraph" is provided, it outlines the graph built so far (node id, kind, title, rounds; edge from, to, kind). Reply with a PATCH for the new turns, NOT the whole graph: "nodes" holds ONLY nodes that are new or changed, and "edges" holds ONLY new edges. NEVER repeat an unchanged node. Nodes you leave out are kept exactly as they are.
|
|
63
|
+
- In a patch, first try to extend an existing node: return its "id" with the new turn refs in "turnIds" (and a better "title" or "desc" if the step moved on). Add a new node only when the new turns start a genuinely new step. One owner message plus its reply adds at most ONE new node, plus one "rejected" node if the owner refused something. Most patches add zero or one node.
|
|
64
|
+
- To fold an existing node into another, return {"id":"<node to remove>","mergeInto":"<node that absorbs it>"}. Its turn refs and edges move to the absorbing node.
|
|
65
|
+
- If "consolidate" is provided there are no new turns: the graph has grown too long. Shrink it to about "consolidate.targetNodes" nodes using "mergeInto". Fold questions, caveats, notes, unpicked options and small sub-steps into the decision or result they belong to. Keep the goal, real owner rejections, and the main outcomes. You may also return an absorbing node with a new "title" and "desc" that cover what it absorbed. Return no new nodes.
|
|
59
66
|
- To change an existing node, return its "id" plus ONLY the fields that change (e.g. "kind" with a matching "badge" and "rationaleLabel", or "rounds"). "turnIds" on a changed node lists only the newly added turn refs; they are added to the ones it already has.
|
|
60
67
|
- New nodes need ids not used in "existingGraph". New edges may connect new and existing node ids. To change the kind or label of an existing edge, return it again with the same "from" and "to".
|
|
61
68
|
- An empty "nodes" array is valid when the new turns add nothing to an existing graph.
|
|
@@ -259,7 +266,8 @@ function updatedNode(old: GraphNode, rec: Record<string, unknown>, turnTimestamp
|
|
|
259
266
|
* Validates the LLM's Decision & Path Graph JSON reply and merges it into the existing graph.
|
|
260
267
|
* Without an existing graph the reply is the complete first graph. With one, the reply is a patch:
|
|
261
268
|
* its nodes are added or partially update a node with the same id, its edges are added or replace
|
|
262
|
-
* the kind/label of the existing edge with the same from+to.
|
|
269
|
+
* the kind/label of the existing edge with the same from+to. A node entry with "mergeInto" folds an
|
|
270
|
+
* existing node into another: its turn refs and edges move there, so no turn loses its node.
|
|
263
271
|
* Node errors throw so callers never commit corrupt graphs; an unusable edge is dropped on its own.
|
|
264
272
|
*
|
|
265
273
|
* Args:
|
|
@@ -286,6 +294,7 @@ export function parseGraphOutput(raw: string, turnTimestamps: ReadonlyMap<string
|
|
|
286
294
|
const byId = new Map<string, GraphNode>();
|
|
287
295
|
for (const n of existing?.nodes ?? []) byId.set(n.id, n);
|
|
288
296
|
const seenIds = new Set<string>();
|
|
297
|
+
const mergeInto = new Map<string, string>();
|
|
289
298
|
|
|
290
299
|
for (const item of root.nodes) {
|
|
291
300
|
if (typeof item !== "object" || item === null) throw bad("node is not an object");
|
|
@@ -294,6 +303,11 @@ export function parseGraphOutput(raw: string, turnTimestamps: ReadonlyMap<string
|
|
|
294
303
|
if (seenIds.has(id)) throw bad(`duplicate node id ${id}`);
|
|
295
304
|
seenIds.add(id);
|
|
296
305
|
const old = byId.get(id);
|
|
306
|
+
const into = optionalString(rec, "mergeInto", 48);
|
|
307
|
+
if (into !== undefined) {
|
|
308
|
+
if (old && into !== id) mergeInto.set(id, into);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
297
311
|
byId.set(id, old ? updatedNode(old, rec, turnTimestamps) : newNode(id, rec, turnTimestamps));
|
|
298
312
|
}
|
|
299
313
|
|
|
@@ -338,7 +352,46 @@ export function parseGraphOutput(raw: string, turnTimestamps: ReadonlyMap<string
|
|
|
338
352
|
}
|
|
339
353
|
}
|
|
340
354
|
|
|
341
|
-
return { title, nodes: [...byId.values()], edges, mode: "llm" };
|
|
355
|
+
if (mergeInto.size === 0) return { title, nodes: [...byId.values()], edges, mode: "llm" };
|
|
356
|
+
|
|
357
|
+
// Follow merge chains (a into b, b into c) to the node that survives. A merge whose chain ends
|
|
358
|
+
// on an unknown node, loops, or would remove the goal is ignored.
|
|
359
|
+
const survivor = (id: string): string => {
|
|
360
|
+
let cur = id;
|
|
361
|
+
for (let hops = 0; hops <= mergeInto.size; hops++) {
|
|
362
|
+
const next = mergeInto.get(cur);
|
|
363
|
+
if (next === undefined) return byId.has(cur) ? cur : id;
|
|
364
|
+
cur = next;
|
|
365
|
+
}
|
|
366
|
+
return id;
|
|
367
|
+
};
|
|
368
|
+
for (const id of [...mergeInto.keys()]) {
|
|
369
|
+
if (byId.get(id)?.kind === "goal" || survivor(id) === id) mergeInto.delete(id);
|
|
370
|
+
}
|
|
371
|
+
for (const id of mergeInto.keys()) {
|
|
372
|
+
const gone = byId.get(id);
|
|
373
|
+
const keepId = survivor(id);
|
|
374
|
+
const keep = byId.get(keepId);
|
|
375
|
+
if (!gone || !keep) continue;
|
|
376
|
+
// Oldest first, so the inspector shows the absorbed turns in the order they happened.
|
|
377
|
+
const turnIds = [...new Set([...keep.turnIds, ...gone.turnIds])].sort(
|
|
378
|
+
(a, b) => (turnTimestamps.get(a) ?? Infinity) - (turnTimestamps.get(b) ?? Infinity),
|
|
379
|
+
);
|
|
380
|
+
byId.set(keepId, { ...keep, turnIds, at: Math.max(keep.at, gone.at) });
|
|
381
|
+
}
|
|
382
|
+
for (const id of mergeInto.keys()) byId.delete(id);
|
|
383
|
+
|
|
384
|
+
const merged: GraphEdge[] = [];
|
|
385
|
+
const seenPairs = new Set<string>();
|
|
386
|
+
for (const e of edges) {
|
|
387
|
+
const from = survivor(e.from);
|
|
388
|
+
const to = survivor(e.to);
|
|
389
|
+
const key = pairKey(from, to);
|
|
390
|
+
if (from === to || seenPairs.has(key)) continue;
|
|
391
|
+
seenPairs.add(key);
|
|
392
|
+
merged.push({ ...e, from, to });
|
|
393
|
+
}
|
|
394
|
+
return { title, nodes: [...byId.values()], edges: merged, mode: "llm" };
|
|
342
395
|
}
|
|
343
396
|
|
|
344
397
|
/**
|
package/src/session.ts
CHANGED
|
@@ -9,6 +9,21 @@ import { tailJsonl, type TailEvent } from "./tail";
|
|
|
9
9
|
export const BATCH_TURNS = 16;
|
|
10
10
|
export const BATCH_CHARS = 12_000;
|
|
11
11
|
export const RETRY_MS = 60_000;
|
|
12
|
+
/** A graph is tidied up when it has this many nodes more than its target size. */
|
|
13
|
+
export const CONSOLIDATE_SLACK = 6;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Node budget for a session: about one node per three turns, never under 12 or over 40.
|
|
17
|
+
*
|
|
18
|
+
* Args:
|
|
19
|
+
* turnCount (number): Number of turns the graph covers.
|
|
20
|
+
*
|
|
21
|
+
* Returns:
|
|
22
|
+
* number: Target node count.
|
|
23
|
+
*/
|
|
24
|
+
export function targetNodes(turnCount: number): number {
|
|
25
|
+
return Math.min(40, Math.max(12, Math.round(turnCount / 3)));
|
|
26
|
+
}
|
|
12
27
|
|
|
13
28
|
export type SnapshotEvent = Extract<ServerEvent, { type: "snapshot" }>;
|
|
14
29
|
|
|
@@ -43,6 +58,8 @@ export async function openSession(opts: { harness: Harness; ref: SessionRef; con
|
|
|
43
58
|
let ready = false;
|
|
44
59
|
let labelerError: string | undefined;
|
|
45
60
|
let retry: Timer | undefined;
|
|
61
|
+
/** Node count at the last tidy-up attempt; 0 before the first one. */
|
|
62
|
+
let consolidatedAt = 0;
|
|
46
63
|
let heuristicGraph: SessionGraph = buildHeuristicGraph([], ref.title);
|
|
47
64
|
|
|
48
65
|
const emit = (ev: ServerEvent) => {
|
|
@@ -133,11 +150,36 @@ export async function openSession(opts: { harness: Harness; ref: SessionRef; con
|
|
|
133
150
|
return extractFrom(nextBatch());
|
|
134
151
|
}
|
|
135
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Asks the labeler to fold nodes together when the graph has outgrown its budget. Runs again only
|
|
155
|
+
* after the graph has grown by another CONSOLIDATE_SLACK nodes, so a reply that merges too little
|
|
156
|
+
* is not retried on every turn. A failed tidy-up keeps the graph as it is.
|
|
157
|
+
*/
|
|
158
|
+
async function consolidate(): Promise<void> {
|
|
159
|
+
const before = store.graph;
|
|
160
|
+
if (!before || stopped) return;
|
|
161
|
+
const target = targetNodes(store.processedTurnIds.size);
|
|
162
|
+
if (before.nodes.length < Math.max(target, consolidatedAt) + CONSOLIDATE_SLACK) return;
|
|
163
|
+
consolidatedAt = before.nodes.length;
|
|
164
|
+
let graph: SessionGraph;
|
|
165
|
+
try {
|
|
166
|
+
const input: GraphExtractInput = { ...buildInput([]), consolidate: { targetNodes: target } };
|
|
167
|
+
graph = await extractGraph(labelerCfg, input, timestampsMap(), before);
|
|
168
|
+
} catch {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (stopped || graph.nodes.length >= before.nodes.length) return;
|
|
172
|
+
consolidatedAt = graph.nodes.length;
|
|
173
|
+
store.saveGraph(graph, []);
|
|
174
|
+
emit({ type: "graph", graph });
|
|
175
|
+
}
|
|
176
|
+
|
|
136
177
|
async function drain(): Promise<void> {
|
|
137
178
|
if (!llmEnabled || busy || stopped) return;
|
|
138
179
|
busy = true;
|
|
139
180
|
try {
|
|
140
181
|
await extractFrom(nextBatch());
|
|
182
|
+
await consolidate();
|
|
141
183
|
} catch (err) {
|
|
142
184
|
if (stopped) return;
|
|
143
185
|
setLabelerError(err instanceof Error ? err.message : String(err));
|
package/web/view.ts
CHANGED
|
@@ -247,6 +247,13 @@ export function summarizeGraph(graph: SessionGraph): GraphSummary {
|
|
|
247
247
|
};
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
+
/** Counts owner rejections and merely unpicked options apart, e.g. "4 rejected, 18 not chosen". */
|
|
251
|
+
function notTakenLabel(nodes: readonly GraphNode[]): string {
|
|
252
|
+
const rejected = nodes.filter((n) => n.kind === "rejected").length;
|
|
253
|
+
const options = nodes.length - rejected;
|
|
254
|
+
return [rejected > 0 ? `${rejected} rejected` : "", options > 0 ? `${options} not chosen` : ""].filter(Boolean).join(", ");
|
|
255
|
+
}
|
|
256
|
+
|
|
250
257
|
function turnsLabel(n: GraphNode): string {
|
|
251
258
|
const count = n.turnIds.length;
|
|
252
259
|
const time = clock(n.at);
|
|
@@ -290,7 +297,7 @@ export function paintSidebar(s: State, els: Els): void {
|
|
|
290
297
|
const pills = [
|
|
291
298
|
`${turnCount} turns → ${nodeCount} nodes`,
|
|
292
299
|
sum.decisionsCount > 0 ? `${sum.decisionsCount} decisions` : "",
|
|
293
|
-
sum.rejected
|
|
300
|
+
notTakenLabel(sum.rejected),
|
|
294
301
|
sum.loopsCount > 0 ? `${sum.loopsCount} loops collapsed` : "",
|
|
295
302
|
]
|
|
296
303
|
.filter(Boolean)
|
|
@@ -453,8 +460,8 @@ export function paintTimeline(s: State, els: Els): void {
|
|
|
453
460
|
summaryLine("Goal", sum.goal?.title ?? "No goal identified yet", sum.goal) +
|
|
454
461
|
summaryLine("Now", sum.now?.title ?? "Waiting for turns in this session…", sum.now) +
|
|
455
462
|
summaryLine(
|
|
456
|
-
"
|
|
457
|
-
ruledOut === 0 ? "Nothing
|
|
463
|
+
"Not taken",
|
|
464
|
+
ruledOut === 0 ? "Nothing rejected yet" : `${notTakenLabel(sum.rejected)} · show the first`,
|
|
458
465
|
sum.rejected[0],
|
|
459
466
|
),
|
|
460
467
|
);
|