portable-agent-layer 0.8.1 → 0.10.0
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/assets/templates/AGENTS.md.template +6 -0
- package/assets/templates/STEERING-RULES.md +23 -0
- package/package.json +2 -2
- package/src/hooks/LoadContext.ts +1 -4
- package/src/hooks/handlers/rating.ts +9 -49
- package/src/hooks/handlers/reflect-trigger.ts +83 -0
- package/src/hooks/handlers/relationship.ts +8 -5
- package/src/hooks/handlers/session-name.ts +8 -6
- package/src/hooks/handlers/work-learning.ts +1 -0
- package/src/hooks/handlers/work-session.ts +16 -3
- package/src/hooks/lib/claude-md.ts +12 -2
- package/src/hooks/lib/context.ts +31 -21
- package/src/hooks/lib/graduation.ts +6 -4
- package/src/hooks/lib/learning-store.ts +7 -117
- package/src/hooks/lib/log.ts +1 -3
- package/src/hooks/lib/opinions.ts +191 -0
- package/src/hooks/lib/relationship.ts +5 -4
- package/src/hooks/lib/security.ts +3 -0
- package/src/hooks/lib/stop.ts +3 -0
- package/src/hooks/lib/text-similarity.ts +125 -0
- package/src/hooks/lib/work-tracking.ts +1 -1
- package/src/targets/opencode/install.ts +6 -2
- package/src/targets/opencode/plugin.ts +20 -171
- package/src/tools/analyze.ts +49 -15
- package/src/tools/opinion.ts +250 -0
- package/src/tools/relationship-reflect.ts +215 -105
- package/src/hooks/lib/prompts.ts +0 -11
- package/src/tools/eval-principles.ts +0 -234
|
@@ -43,3 +43,9 @@ Do not create projects for one-off questions or quick fixes.
|
|
|
43
43
|
- When a session produces reusable insights → session learning
|
|
44
44
|
- When something fails significantly (rating < 6) → failure capture
|
|
45
45
|
- Do NOT write memories about trivial exchanges or things already captured in TELOS.
|
|
46
|
+
|
|
47
|
+
## Opinion Tracking
|
|
48
|
+
|
|
49
|
+
PAL tracks confidence-scored opinions about the user. When you notice the user confirming or contradicting a behavioral pattern, update it via `bun run tool:opinion`. Run `bun run tool:opinion -- --help` for full usage and examples. Opinions at ≥85% confidence are automatically injected into every session context.
|
|
50
|
+
|
|
51
|
+
{{STEERING_RULES}}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
## Steering Rules
|
|
2
|
+
|
|
3
|
+
Behavioral directives — act on these, don't just know them.
|
|
4
|
+
|
|
5
|
+
**Surgical fixes only.** When debugging, make precise corrections to the broken behavior. Never delete or rearchitect components as a fix. If you believe a component is the root cause, explain your reasoning and ask before removing it.
|
|
6
|
+
|
|
7
|
+
**Never assert without verification.** Don't say something "is" a certain way unless you've verified it with your tools. After making changes, verify the result before claiming success. Evidence required — tests, diffs, tool output. Never "Done!" without proof.
|
|
8
|
+
|
|
9
|
+
**First principles over bolt-ons.** Most problems are symptoms. Understand → Simplify → Reduce → Add (last resort). Don't accrue technical debt through band-aid solutions.
|
|
10
|
+
|
|
11
|
+
**Read before modifying.** Understand existing code, imports, and patterns before suggesting changes.
|
|
12
|
+
|
|
13
|
+
**One change when debugging.** Isolate, verify, proceed. Don't change multiple things at once.
|
|
14
|
+
|
|
15
|
+
**Minimal scope.** Only change what was asked. No bonus refactoring, no extra cleanup, no unsolicited improvements.
|
|
16
|
+
|
|
17
|
+
**Ask before destructive actions.** Deletes, force pushes, production deploys — always ask first.
|
|
18
|
+
|
|
19
|
+
**Plan means stop.** "Create a plan" = present and STOP. No execution without approval.
|
|
20
|
+
|
|
21
|
+
**Error recovery.** When told you did something wrong — review the session, identify the violation, fix it, then explain what happened and capture the learning. Don't ask "What did I do wrong?"
|
|
22
|
+
|
|
23
|
+
**Act on what you know.** When tracked opinions or relationship notes reveal user preferences, apply them to your behavior. If you know the user prefers concise responses, be concise. If they prefer manual commits, never offer to commit.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "portable-agent-layer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "PAL — Portable Agent Layer: persistent personal context for AI coding assistants",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"ai:fyzz-api": "bun run src/tools/fyzz-api.ts",
|
|
49
49
|
"ai:pdf-download": "bun run src/tools/pdf-download.ts",
|
|
50
50
|
"ai:youtube-analyze": "bun run src/tools/youtube-analyze.ts",
|
|
51
|
-
"tool:eval": "bun run src/tools/eval-principles.ts",
|
|
52
51
|
"tool:analyze": "bun run src/tools/analyze.ts",
|
|
52
|
+
"tool:opinion": "bun run src/tools/opinion.ts",
|
|
53
53
|
"tool:reflect": "bun run src/tools/relationship-reflect.ts",
|
|
54
54
|
"tool:export": "bun run src/tools/export.ts",
|
|
55
55
|
"tool:import": "bun run src/tools/import.ts",
|
package/src/hooks/LoadContext.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { regenerateIfNeeded } from "./lib/claude-md";
|
|
10
|
-
import {
|
|
10
|
+
import { buildSystemReminder } from "./lib/context";
|
|
11
11
|
import { logDebug, logError } from "./lib/log";
|
|
12
12
|
|
|
13
13
|
// --- Skip heavy context for subagents ---
|
|
@@ -28,9 +28,6 @@ try {
|
|
|
28
28
|
logError("LoadContext:regenerate", err);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
// --- Visible greeting to stderr ---
|
|
32
|
-
process.stderr.write(`${buildGreeting().join("\n")}\n`);
|
|
33
|
-
|
|
34
31
|
// --- Dynamic system-reminder to stdout (empty = nothing injected) ---
|
|
35
32
|
try {
|
|
36
33
|
const reminder = buildSystemReminder();
|
|
@@ -10,12 +10,10 @@
|
|
|
10
10
|
|
|
11
11
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
12
12
|
import { resolve } from "node:path";
|
|
13
|
-
import { stringify } from "../lib/frontmatter";
|
|
14
13
|
import { inference } from "../lib/inference";
|
|
15
|
-
import {
|
|
16
|
-
import { ensureDir, paths } from "../lib/paths";
|
|
14
|
+
import { paths } from "../lib/paths";
|
|
17
15
|
import { emitRating } from "../lib/signals";
|
|
18
|
-
import {
|
|
16
|
+
import { now } from "../lib/time";
|
|
19
17
|
import { logTokenUsage } from "../lib/token-usage";
|
|
20
18
|
|
|
21
19
|
/** Read cached last assistant response (written by StopOrchestrator), looked up by session */
|
|
@@ -46,11 +44,11 @@ function getLastResponse(sessionId?: string): string {
|
|
|
46
44
|
* Matches: "7", "8 - good work", "6: needs work", "9 excellent", "10!"
|
|
47
45
|
* Rejects: "3 items", "5 things to fix", "7th thing", "10/10"
|
|
48
46
|
*/
|
|
49
|
-
function parseExplicitRating(
|
|
47
|
+
export function parseExplicitRating(
|
|
50
48
|
prompt: string
|
|
51
49
|
): { rating: number; comment?: string } | null {
|
|
52
50
|
const trimmed = prompt.trim();
|
|
53
|
-
const match = trimmed.match(/^(10|[1-9])(?:\s*[
|
|
51
|
+
const match = trimmed.match(/^(10|[1-9])(?:\s*[-:,]\s*|\s+)?(.*)$/);
|
|
54
52
|
if (!match) return null;
|
|
55
53
|
|
|
56
54
|
const rating = parseInt(match[1], 10);
|
|
@@ -67,6 +65,9 @@ function parseExplicitRating(
|
|
|
67
65
|
const sentenceStarters =
|
|
68
66
|
/^(items?|things?|steps?|files?|lines?|bugs?|issues?|errors?|times?|minutes?|hours?|days?|seconds?|percent|%|th\b|st\b|nd\b|rd\b|of\b|in\b|at\b|to\b|the\b|a\b|an\b)/i;
|
|
69
67
|
if (sentenceStarters.test(rest)) return null;
|
|
68
|
+
|
|
69
|
+
// Reject item selections: "1 and 2", "2 3 5", "1, 3, 5", "1-3"
|
|
70
|
+
if (/^(and\b|\d|,\s*\d|-\d)/.test(rest)) return null;
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
return { rating, comment: rest };
|
|
@@ -234,37 +235,6 @@ const MIN_CONFIDENCE = 0.5;
|
|
|
234
235
|
|
|
235
236
|
// ── Rating Handling ──
|
|
236
237
|
|
|
237
|
-
function writeLearningMarkdown(
|
|
238
|
-
rating: number,
|
|
239
|
-
source: string,
|
|
240
|
-
context: string,
|
|
241
|
-
detailedContext: string,
|
|
242
|
-
responsePreview: string
|
|
243
|
-
): void {
|
|
244
|
-
const category = categorizeLearning(context, detailedContext);
|
|
245
|
-
const dir = ensureDir(resolve(paths.sessionLearning(), monthPath()));
|
|
246
|
-
const filename = `${fileTimestamp()}_${source}-rating-${rating}_${category}.md`;
|
|
247
|
-
|
|
248
|
-
const meta: Record<string, unknown> = {
|
|
249
|
-
title: context.slice(0, 100) || "(low rating)",
|
|
250
|
-
category,
|
|
251
|
-
date: new Date().toISOString().slice(0, 10),
|
|
252
|
-
rating,
|
|
253
|
-
source,
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
const body = [
|
|
257
|
-
"## Context",
|
|
258
|
-
context || "*(unavailable)*",
|
|
259
|
-
"",
|
|
260
|
-
...(detailedContext ? ["## Analysis", detailedContext, ""] : []),
|
|
261
|
-
"## Last Response",
|
|
262
|
-
responsePreview || "*(unavailable)*",
|
|
263
|
-
].join("\n");
|
|
264
|
-
|
|
265
|
-
writeFileSync(resolve(dir, filename), stringify(meta, body), "utf-8");
|
|
266
|
-
}
|
|
267
|
-
|
|
268
238
|
function handleRating(
|
|
269
239
|
rating: number,
|
|
270
240
|
context: string,
|
|
@@ -276,8 +246,8 @@ function handleRating(
|
|
|
276
246
|
const responsePreview = getLastResponse(sessionId).slice(0, 500);
|
|
277
247
|
emitRating(rating, context, source, responsePreview);
|
|
278
248
|
|
|
279
|
-
if (rating <=
|
|
280
|
-
//
|
|
249
|
+
if (rating <= 4) {
|
|
250
|
+
// Low rating — write pending file for Stop handler with full transcript
|
|
281
251
|
const userPreview = userMessage?.slice(0, 400);
|
|
282
252
|
writeFileSync(
|
|
283
253
|
resolve(paths.state(), "pending-failure.json"),
|
|
@@ -296,16 +266,6 @@ function handleRating(
|
|
|
296
266
|
),
|
|
297
267
|
"utf-8"
|
|
298
268
|
);
|
|
299
|
-
// No learning markdown for ≤3 — failure capture covers it with richer analysis + tags
|
|
300
|
-
} else if (rating < 5) {
|
|
301
|
-
// Low but not critical — write learning markdown
|
|
302
|
-
writeLearningMarkdown(
|
|
303
|
-
rating,
|
|
304
|
-
source,
|
|
305
|
-
context,
|
|
306
|
-
detailedContext ?? "",
|
|
307
|
-
responsePreview
|
|
308
|
-
);
|
|
309
269
|
}
|
|
310
270
|
}
|
|
311
271
|
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-trigger for relationship reflect — runs when conditions are met:
|
|
3
|
+
* - 7+ days since last reflect
|
|
4
|
+
* - 10+ new relationship notes since last reflect
|
|
5
|
+
*
|
|
6
|
+
* Spawns `bun run tool:reflect` as a detached background process.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
10
|
+
import { resolve } from "node:path";
|
|
11
|
+
import { logDebug } from "../lib/log";
|
|
12
|
+
import { getLastReflectDate } from "../lib/opinions";
|
|
13
|
+
import { palPkg, paths } from "../lib/paths";
|
|
14
|
+
|
|
15
|
+
const MIN_DAYS_BETWEEN = 7;
|
|
16
|
+
const MIN_NEW_NOTES = 10;
|
|
17
|
+
|
|
18
|
+
function countNotesSince(since: string): number {
|
|
19
|
+
const relDir = paths.relationship();
|
|
20
|
+
if (!existsSync(relDir)) return 0;
|
|
21
|
+
|
|
22
|
+
let count = 0;
|
|
23
|
+
try {
|
|
24
|
+
for (const monthDir of readdirSync(relDir)) {
|
|
25
|
+
if (!/^\d{4}-\d{2}$/.test(monthDir)) continue;
|
|
26
|
+
const monthPath = resolve(relDir, monthDir);
|
|
27
|
+
try {
|
|
28
|
+
for (const file of readdirSync(monthPath)) {
|
|
29
|
+
if (!file.endsWith(".md")) continue;
|
|
30
|
+
const dateStr = file.replace(".md", "");
|
|
31
|
+
if (dateStr > since) {
|
|
32
|
+
const content = readFileSync(resolve(monthPath, file), "utf-8");
|
|
33
|
+
count += (content.match(/^- [OBW]/gm) || []).length;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
} catch {
|
|
37
|
+
/* skip */
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
/* non-critical */
|
|
42
|
+
}
|
|
43
|
+
return count;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function checkReflectTrigger(): Promise<void> {
|
|
47
|
+
const lastReflect = getLastReflectDate();
|
|
48
|
+
const now = new Date();
|
|
49
|
+
|
|
50
|
+
// Trigger if either condition is met (OR logic)
|
|
51
|
+
let timeThreshold = !lastReflect;
|
|
52
|
+
if (lastReflect) {
|
|
53
|
+
const daysSince =
|
|
54
|
+
(now.getTime() - new Date(lastReflect).getTime()) / (1000 * 60 * 60 * 24);
|
|
55
|
+
timeThreshold = daysSince >= MIN_DAYS_BETWEEN;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const newNotes = countNotesSince(lastReflect || "2000-01-01");
|
|
59
|
+
const volumeThreshold = newNotes >= MIN_NEW_NOTES;
|
|
60
|
+
|
|
61
|
+
if (!timeThreshold && !volumeThreshold) {
|
|
62
|
+
logDebug("reflect-trigger", `Skipping: ${newNotes} notes, time threshold not met`);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
logDebug(
|
|
67
|
+
"reflect-trigger",
|
|
68
|
+
`Triggering: ${newNotes} new notes, last: ${lastReflect || "never"}`
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const proc = Bun.spawn(["bun", "run", "tool:reflect"], {
|
|
73
|
+
cwd: palPkg(),
|
|
74
|
+
stdout: "ignore",
|
|
75
|
+
stderr: "ignore",
|
|
76
|
+
stdin: "ignore",
|
|
77
|
+
});
|
|
78
|
+
proc.unref();
|
|
79
|
+
logDebug("reflect-trigger", "Spawned reflect in background");
|
|
80
|
+
} catch (err) {
|
|
81
|
+
logDebug("reflect-trigger", `Failed to spawn: ${err}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -20,8 +20,9 @@ const OBSERVATION_SCHEMA = {
|
|
|
20
20
|
properties: {
|
|
21
21
|
type: {
|
|
22
22
|
type: "string",
|
|
23
|
-
enum: ["O", "W"],
|
|
24
|
-
description:
|
|
23
|
+
enum: ["O", "W", "B"],
|
|
24
|
+
description:
|
|
25
|
+
"O=opinion/preference, W=factual observation, B=belief/behavioral pattern",
|
|
25
26
|
},
|
|
26
27
|
text: { type: "string" },
|
|
27
28
|
confidence: { type: "number" },
|
|
@@ -74,8 +75,10 @@ export async function captureRelationship(
|
|
|
74
75
|
const result = await inference({
|
|
75
76
|
system:
|
|
76
77
|
"You analyze user messages from an AI coding session to extract relationship observations. " +
|
|
77
|
-
"
|
|
78
|
-
"
|
|
78
|
+
"Types: O=opinions/preferences (how they like to work, what they want), " +
|
|
79
|
+
"B=beliefs/behavioral patterns (how they approach problems, decision-making style, recurring habits), " +
|
|
80
|
+
"W=world facts (their situation, projects, tools they use). " +
|
|
81
|
+
"Focus on: preferences, corrections, frustrations, positive reactions, communication style, problem-solving approach. " +
|
|
79
82
|
"Return 0-3 observations. If nothing notable, return empty observations array. Be concise.",
|
|
80
83
|
user: `User messages from this session:\n${userMessages.map((m, i) => `${i + 1}. ${m}`).join("\n")}`,
|
|
81
84
|
maxTokens: 300,
|
|
@@ -93,7 +96,7 @@ export async function captureRelationship(
|
|
|
93
96
|
|
|
94
97
|
try {
|
|
95
98
|
const parsed = JSON.parse(result.output) as {
|
|
96
|
-
observations: Array<{ type: "O" | "W"; text: string; confidence: number }>;
|
|
99
|
+
observations: Array<{ type: "O" | "W" | "B"; text: string; confidence: number }>;
|
|
97
100
|
};
|
|
98
101
|
|
|
99
102
|
logDebug("relationship", `Parsed ${parsed.observations?.length ?? 0} observations`);
|
|
@@ -30,14 +30,16 @@ export async function captureSessionName(
|
|
|
30
30
|
): Promise<void> {
|
|
31
31
|
if (!sessionId) return;
|
|
32
32
|
|
|
33
|
-
// Skip if this session is already named
|
|
33
|
+
// Skip if this session is already named (non-untitled)
|
|
34
34
|
const names = readSessionNames();
|
|
35
|
-
|
|
35
|
+
const existing = names[sessionId];
|
|
36
|
+
if (existing && existing !== "untitled session") return;
|
|
36
37
|
|
|
37
|
-
//
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
// Try deterministic name from this message's keywords
|
|
39
|
+
const name = extractFallbackName(message);
|
|
40
|
+
if (name === "untitled session") return; // not enough keywords yet
|
|
41
|
+
writeSessionName(sessionId, name);
|
|
42
|
+
logDebug("session-name", `Named from prompt: "${name}"`);
|
|
41
43
|
|
|
42
44
|
// TODO: re-enable when a consumer exists (tab titles, dashboard)
|
|
43
45
|
// // 2. Spawn detached background process to upgrade with inference
|
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
* Replaces the old work.ts handler.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
extractFallbackName,
|
|
8
|
+
readSessionNames,
|
|
9
|
+
writeSessionName,
|
|
10
|
+
} from "../lib/session-names";
|
|
7
11
|
import { now } from "../lib/time";
|
|
8
12
|
import {
|
|
9
13
|
extractContent,
|
|
@@ -29,9 +33,18 @@ export async function captureWorkSession(
|
|
|
29
33
|
|
|
30
34
|
const id = sessionId || `session-${Date.now()}`;
|
|
31
35
|
|
|
32
|
-
//
|
|
36
|
+
// Name the session if still untitled and enough messages
|
|
33
37
|
const names = readSessionNames();
|
|
34
|
-
|
|
38
|
+
let name = names[id] || "";
|
|
39
|
+
if ((!name || name === "untitled session") && messages.length >= 6) {
|
|
40
|
+
const userTexts = messages
|
|
41
|
+
.filter((m) => m.role === "user")
|
|
42
|
+
.map((m) => extractContent(m))
|
|
43
|
+
.join(" ");
|
|
44
|
+
name = extractFallbackName(userTexts);
|
|
45
|
+
if (name !== "untitled session") writeSessionName(id, name);
|
|
46
|
+
}
|
|
47
|
+
if (!name || name === "untitled session") name = "untitled session";
|
|
35
48
|
|
|
36
49
|
// Extract content
|
|
37
50
|
const lastUser = extractLastUser(messages);
|
|
@@ -72,7 +72,11 @@ export function needsRebuild(): boolean {
|
|
|
72
72
|
const outputMtime = statSync(outputPath).mtimeMs;
|
|
73
73
|
|
|
74
74
|
// Collect source files: template + setup.json + all telos/*.md
|
|
75
|
-
const sources: string[] = [
|
|
75
|
+
const sources: string[] = [
|
|
76
|
+
TEMPLATE_PATH,
|
|
77
|
+
resolve(dirname(TEMPLATE_PATH), "STEERING-RULES.md"),
|
|
78
|
+
resolve(paths.state(), "setup.json"),
|
|
79
|
+
];
|
|
76
80
|
|
|
77
81
|
const telosDir = paths.telos();
|
|
78
82
|
if (existsSync(telosDir)) {
|
|
@@ -105,10 +109,16 @@ export function buildClaudeMd(): string {
|
|
|
105
109
|
const setupPrompt = state ? buildSetupPrompt(state) : null;
|
|
106
110
|
const telos = loadTelos();
|
|
107
111
|
|
|
112
|
+
const steeringPath = resolve(dirname(TEMPLATE_PATH), "STEERING-RULES.md");
|
|
113
|
+
const steeringRules = existsSync(steeringPath)
|
|
114
|
+
? readFileSync(steeringPath, "utf-8").trim()
|
|
115
|
+
: "";
|
|
116
|
+
|
|
108
117
|
return template
|
|
109
118
|
.replace("{{SETUP_PROMPT}}", setupPrompt ? `${setupPrompt}\n` : "")
|
|
110
119
|
.replace("{{TELOS}}", telos ? `${telos}\n` : "")
|
|
111
|
-
.replace("{{MEMORY_PATHS}}", memoryPaths())
|
|
120
|
+
.replace("{{MEMORY_PATHS}}", memoryPaths())
|
|
121
|
+
.replace("{{STEERING_RULES}}", steeringRules);
|
|
112
122
|
}
|
|
113
123
|
|
|
114
124
|
/** Regenerate AGENTS.md if any source file is newer, and ensure CLAUDE.md symlink exists. Returns true if rebuilt. */
|
package/src/hooks/lib/context.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
|
7
7
|
import { resolve } from "node:path";
|
|
8
8
|
import { parse } from "./frontmatter";
|
|
9
9
|
import { readFailures, readLearnings } from "./learning-store";
|
|
10
|
+
import { loadOpinionContext } from "./opinions";
|
|
10
11
|
import { paths } from "./paths";
|
|
11
12
|
import { loadRecentNotes } from "./relationship";
|
|
12
13
|
import { readSessionNames } from "./session-names";
|
|
@@ -62,22 +63,21 @@ export function countSignals(filename: string): number {
|
|
|
62
63
|
/** Load structured session history + project dashboard */
|
|
63
64
|
export function loadActiveWork(): { text: string; summary: string | null } | null {
|
|
64
65
|
try {
|
|
65
|
-
const
|
|
66
|
+
const cwd = process.cwd();
|
|
67
|
+
const allRecent = recentSessions(48);
|
|
66
68
|
const projects = activeProjects();
|
|
67
69
|
const stale = staleProjects(7);
|
|
68
70
|
|
|
69
|
-
if (
|
|
71
|
+
if (allRecent.length === 0 && projects.length === 0) return null;
|
|
70
72
|
|
|
71
73
|
const lines: string[] = [];
|
|
72
74
|
|
|
73
|
-
if (
|
|
75
|
+
if (allRecent.length > 0) {
|
|
74
76
|
lines.push("## Recent Work (last 48h)");
|
|
75
|
-
for (const s of
|
|
77
|
+
for (const s of allRecent.slice(-10).reverse()) {
|
|
76
78
|
const ago = formatAgo(s.ts);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
lines.push(` Handoff: ${s.handoff.split("\n")[0].slice(0, 120)}`);
|
|
80
|
-
}
|
|
79
|
+
const here = s.cwd === cwd ? " *" : "";
|
|
80
|
+
lines.push(`- [${s.status}] ${s.name} — ${ago}${here}`);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
|
|
@@ -106,7 +106,8 @@ export function loadActiveWork(): { text: string; summary: string | null } | nul
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
// Summary from most recent session
|
|
109
|
-
const
|
|
109
|
+
const cwdSessions = allRecent.filter((s) => s.cwd === cwd);
|
|
110
|
+
const last = cwdSessions.length > 0 ? cwdSessions[cwdSessions.length - 1] : null;
|
|
110
111
|
const summary = last?.summary?.slice(0, 60) || null;
|
|
111
112
|
|
|
112
113
|
return {
|
|
@@ -215,26 +216,33 @@ export function loadWisdomContext(): string {
|
|
|
215
216
|
}
|
|
216
217
|
}
|
|
217
218
|
|
|
218
|
-
/** Load recent session learning files as digest,
|
|
219
|
+
/** Load recent session learning files as digest, with detail for current project */
|
|
219
220
|
export function loadLearningDigest(): string {
|
|
220
221
|
try {
|
|
221
|
-
const
|
|
222
|
+
const cwd = process.cwd();
|
|
223
|
+
const entries = readLearnings(paths.sessionLearning(), 10);
|
|
222
224
|
if (entries.length === 0) return "";
|
|
223
225
|
|
|
224
|
-
const
|
|
225
|
-
const
|
|
226
|
+
const thisProject = entries.filter((e) => e.cwd === cwd).slice(0, 4);
|
|
227
|
+
const other = entries.filter((e) => e.cwd !== cwd).slice(0, 3);
|
|
228
|
+
|
|
229
|
+
if (thisProject.length === 0 && other.length === 0) return "";
|
|
226
230
|
|
|
227
|
-
|
|
231
|
+
const lines: string[] = [];
|
|
228
232
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
+
if (thisProject.length > 0) {
|
|
234
|
+
lines.push("## This Project — Recent Sessions");
|
|
235
|
+
for (const e of thisProject) {
|
|
236
|
+
lines.push(`- **${e.title}**`);
|
|
237
|
+
if (e.insights) lines.push(` ${e.insights.split("\n")[0].slice(0, 150)}`);
|
|
238
|
+
}
|
|
233
239
|
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
240
|
+
|
|
241
|
+
if (other.length > 0) {
|
|
242
|
+
lines.push(thisProject.length > 0 ? "" : "", "## Other Recent Learnings");
|
|
243
|
+
for (const e of other) lines.push(`- ${e.title}`);
|
|
237
244
|
}
|
|
245
|
+
|
|
238
246
|
return lines.join("\n");
|
|
239
247
|
} catch {
|
|
240
248
|
return "";
|
|
@@ -347,8 +355,10 @@ export function buildSystemReminder(): string {
|
|
|
347
355
|
const trends = loadSignalTrends();
|
|
348
356
|
const failures = loadFailurePatterns();
|
|
349
357
|
const synthesis = loadSynthesisRecommendations();
|
|
358
|
+
const opinions = loadOpinionContext();
|
|
350
359
|
const parts: string[] = [];
|
|
351
360
|
if (wisdom) parts.push(wisdom);
|
|
361
|
+
if (opinions) parts.push(opinions);
|
|
352
362
|
if (relationship) parts.push(relationship);
|
|
353
363
|
if (digest) parts.push(digest);
|
|
354
364
|
if (synthesis) parts.push(synthesis);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Unified Learning Analysis — graduation + ratings summary in one pipeline.
|
|
3
3
|
*
|
|
4
4
|
* Reads failures and session learnings via learning-store, detects recurring
|
|
5
|
-
* patterns via
|
|
5
|
+
* patterns via Dice similarity on context text, and generates a ratings summary
|
|
6
6
|
* with recommendations via Haiku inference.
|
|
7
7
|
*
|
|
8
8
|
* A pattern qualifies for graduation when it appears 3+ times across different sessions.
|
|
@@ -13,20 +13,20 @@
|
|
|
13
13
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
14
14
|
import { resolve } from "node:path";
|
|
15
15
|
import {
|
|
16
|
-
extractKeywords,
|
|
17
16
|
type FailureEntry,
|
|
18
17
|
type LearningEntry,
|
|
19
18
|
readFailures,
|
|
20
19
|
readLearnings,
|
|
21
|
-
similarity,
|
|
22
20
|
} from "./learning-store";
|
|
23
21
|
import { logDebug } from "./log";
|
|
24
22
|
import { ensureDir, paths } from "./paths";
|
|
23
|
+
import { extractKeywords, similarity } from "./text-similarity";
|
|
25
24
|
|
|
26
25
|
// ── Types ──
|
|
27
26
|
|
|
28
27
|
export interface AnalysisEntry {
|
|
29
28
|
source: string;
|
|
29
|
+
path: string;
|
|
30
30
|
text: string;
|
|
31
31
|
date: string;
|
|
32
32
|
}
|
|
@@ -86,7 +86,7 @@ function classifyDomain(text: string): string {
|
|
|
86
86
|
// ── Data Collection ──
|
|
87
87
|
|
|
88
88
|
const MIN_TEXT_LENGTH = 30;
|
|
89
|
-
export const SIMILARITY_THRESHOLD = 0.
|
|
89
|
+
export const SIMILARITY_THRESHOLD = 0.3;
|
|
90
90
|
const MIN_OCCURRENCES = 3;
|
|
91
91
|
|
|
92
92
|
function toAnalysisEntries(
|
|
@@ -99,6 +99,7 @@ function toAnalysisEntries(
|
|
|
99
99
|
if (f.context.length >= MIN_TEXT_LENGTH) {
|
|
100
100
|
entries.push({
|
|
101
101
|
source: `failure:${f.slug}`,
|
|
102
|
+
path: f.path,
|
|
102
103
|
text: f.context.slice(0, 300),
|
|
103
104
|
date: f.date,
|
|
104
105
|
});
|
|
@@ -110,6 +111,7 @@ function toAnalysisEntries(
|
|
|
110
111
|
if (text.length >= MIN_TEXT_LENGTH) {
|
|
111
112
|
entries.push({
|
|
112
113
|
source: `learning:${l.filename}`,
|
|
114
|
+
path: l.path,
|
|
113
115
|
text: text.slice(0, 300),
|
|
114
116
|
date: l.date,
|
|
115
117
|
});
|