@causyne/agent 1.20.26 → 1.20.28
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/agentLoop.js +52 -4
- package/src/agentLoop.ts +48 -4
- package/src/browser/causyneBrowser.js +1 -1
- package/src/browser/causyneBrowser.ts +1 -1
- package/src/browser/searchProviders.js +1 -1
- package/src/browser/searchProviders.ts +1 -1
- package/src/decideTools.js +9 -1
- package/src/decideTools.ts +7 -1
- package/src/dynamicCatalogue.js +3 -1
- package/src/dynamicCatalogue.ts +3 -1
- package/src/launchIntent.js +1 -1
- package/src/launchIntent.ts +1 -1
- package/src/mcp/mcpRegistry.js +6 -2
- package/src/mcp/mcpRegistry.ts +5 -2
- package/src/memory/actrRanker.js +78 -0
- package/src/memory/actrRanker.ts +111 -0
- package/src/memory/contextBudget.js +131 -0
- package/src/memory/contextBudget.ts +155 -0
- package/src/memory/episodicStore.js +651 -0
- package/src/memory/episodicStore.ts +787 -0
- package/src/memory/episodicTypes.js +8 -0
- package/src/memory/episodicTypes.ts +124 -0
- package/src/memory/nearDuplicate.js +179 -0
- package/src/memory/nearDuplicate.ts +232 -0
- package/src/memory/symbolFolder.js +301 -0
- package/src/memory/symbolFolder.ts +327 -0
- package/src/toolPacks.js +1 -0
- package/src/toolPacks.ts +1 -0
- package/src/tools/desktopOpen.js +9 -2
- package/src/tools/desktopOpen.ts +8 -2
- package/src/tools/memoryRecall.js +211 -0
- package/src/tools/memoryRecall.ts +251 -0
- package/src/tools/nativeEngine.js +16 -2
- package/src/tools/nativeEngine.ts +16 -2
- package/src/tools/registry.js +2 -1
- package/src/tools/subagentOrchestrator.js +3 -2
- package/src/tools/toolSelfRepair.js +4 -1
- package/src/tools/toolSelfRepair.ts +4 -1
- package/src/toolsList.js +1 -0
- package/src/toolsList.ts +1 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/memory/contextBudget.ts
|
|
3
|
+
* Measured Iterative Context Budgeting for Causyne Episodic Memory.
|
|
4
|
+
*
|
|
5
|
+
* Instead of guessing fixed item counts or slicing strings mid-line, `fitEpisodicContextToBudget`
|
|
6
|
+
* renders the context and iteratively drops the most expensive components in a deterministic
|
|
7
|
+
* loss order until the output fits within `maxChars`:
|
|
8
|
+
* 1. Drop expanded observation narratives (keep compact index lines)
|
|
9
|
+
* 2. Drop full prior-session summary narratives (keep request/completed headline)
|
|
10
|
+
* 3. Drop prior session's trailing note
|
|
11
|
+
* 4. Halve session summary count until 0
|
|
12
|
+
* 5. Halve observation count until 1
|
|
13
|
+
*
|
|
14
|
+
* Guarantees valid, well-formed Markdown where headers and core timeline rows survive.
|
|
15
|
+
*
|
|
16
|
+
* Authorship: Strict TypeScript (Causyne Agent).
|
|
17
|
+
*/
|
|
18
|
+
export const DEFAULT_EPISODIC_BUDGET_CHARS = 3600;
|
|
19
|
+
const MAX_REDUCTION_ITERATIONS = 16;
|
|
20
|
+
function formatShortTime(epochMs) {
|
|
21
|
+
try {
|
|
22
|
+
const d = new Date(epochMs);
|
|
23
|
+
const yyyy = d.getUTCFullYear();
|
|
24
|
+
const mm = String(d.getUTCMonth() + 1).padStart(2, '0');
|
|
25
|
+
const dd = String(d.getUTCDate()).padStart(2, '0');
|
|
26
|
+
const hh = String(d.getUTCHours()).padStart(2, '0');
|
|
27
|
+
const min = String(d.getUTCMinutes()).padStart(2, '0');
|
|
28
|
+
return `${yyyy}-${mm}-${dd} ${hh}:${min}`;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return 'recent';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Render the episodic context block for a given state configuration.
|
|
36
|
+
*/
|
|
37
|
+
export function renderEpisodicContext(input) {
|
|
38
|
+
if (input.observations.length === 0 && input.summaries.length === 0 && !input.priorTrailingNote) {
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
const lines = [
|
|
42
|
+
`# EPISODIC WORKSPACE MEMORY (${input.project || 'workspace'}) — ACT-R Ranked`,
|
|
43
|
+
'Use `memory_recall` (`search` -> `timeline` -> `get`) to inspect full historical details on demand.'
|
|
44
|
+
];
|
|
45
|
+
if (input.observations.length > 0) {
|
|
46
|
+
lines.push('## Key Workspace Observations');
|
|
47
|
+
for (const obs of input.observations) {
|
|
48
|
+
const rBadge = obs.reinforcementCount > 0 ? ` [x${obs.reinforcementCount + 1}]` : '';
|
|
49
|
+
const files = obs.filesModified.length > 0
|
|
50
|
+
? ` (${obs.filesModified.slice(0, 2).join(', ')})`
|
|
51
|
+
: obs.filesRead.length > 0
|
|
52
|
+
? ` (${obs.filesRead.slice(0, 1).join(', ')})`
|
|
53
|
+
: '';
|
|
54
|
+
lines.push(`- #${obs.id} [${obs.type}]${rBadge} ${obs.title}${files} (${formatShortTime(obs.createdAtEpochMs)})`);
|
|
55
|
+
if (input.fullObservationIds?.has(obs.id) && obs.narrative) {
|
|
56
|
+
lines.push(` Narrative: ${obs.narrative.slice(0, 280)}`);
|
|
57
|
+
if (obs.facts.length > 0) {
|
|
58
|
+
lines.push(` Facts: ${obs.facts.slice(0, 3).join('; ')}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (input.summaries.length > 0) {
|
|
64
|
+
lines.push('## Recent Session Checkpoints');
|
|
65
|
+
for (let i = 0; i < input.summaries.length; i++) {
|
|
66
|
+
const s = input.summaries[i];
|
|
67
|
+
if (!s)
|
|
68
|
+
continue;
|
|
69
|
+
lines.push(`- S#${s.id}: ${s.request.slice(0, 120)} -> ${s.completed.slice(0, 140)}`);
|
|
70
|
+
if (i === 0 && input.showLastSummaryDetails) {
|
|
71
|
+
if (s.learned)
|
|
72
|
+
lines.push(` Learned: ${s.learned.slice(0, 200)}`);
|
|
73
|
+
if (s.nextSteps)
|
|
74
|
+
lines.push(` Next: ${s.nextSteps.slice(0, 160)}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (input.priorTrailingNote) {
|
|
79
|
+
lines.push(`## Prior Context Note\n${input.priorTrailingNote.slice(0, 240)}`);
|
|
80
|
+
}
|
|
81
|
+
return lines.join('\n');
|
|
82
|
+
}
|
|
83
|
+
function reduceStateOnce(state) {
|
|
84
|
+
// Stage 1: Drop full observation narratives (keep compact timeline titles)
|
|
85
|
+
if (state.fullObservationIds && state.fullObservationIds.size > 0) {
|
|
86
|
+
return { ...state, fullObservationIds: new Set() };
|
|
87
|
+
}
|
|
88
|
+
// Stage 2: Drop full last-session summary details
|
|
89
|
+
if (state.showLastSummaryDetails) {
|
|
90
|
+
return { ...state, showLastSummaryDetails: false };
|
|
91
|
+
}
|
|
92
|
+
// Stage 3: Drop prior trailing note
|
|
93
|
+
if (state.priorTrailingNote !== undefined) {
|
|
94
|
+
return { ...state, priorTrailingNote: undefined };
|
|
95
|
+
}
|
|
96
|
+
// Stage 4: Halve session summaries down to 0
|
|
97
|
+
if (state.summaries.length > 0) {
|
|
98
|
+
const nextCount = Math.floor(state.summaries.length / 2);
|
|
99
|
+
return { ...state, summaries: state.summaries.slice(0, nextCount) };
|
|
100
|
+
}
|
|
101
|
+
// Stage 5: Halve observations down to 1
|
|
102
|
+
if (state.observations.length > 1) {
|
|
103
|
+
const nextCount = Math.max(1, Math.floor(state.observations.length / 2));
|
|
104
|
+
return { ...state, observations: state.observations.slice(0, nextCount) };
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Iteratively reduce `input` until `renderEpisodicContext(state).length <= maxChars`,
|
|
110
|
+
* or until the floor (1 compact observation title, 0 summaries) is reached.
|
|
111
|
+
*/
|
|
112
|
+
export function fitEpisodicContextToBudget(input, maxChars = DEFAULT_EPISODIC_BUDGET_CHARS) {
|
|
113
|
+
if (maxChars <= 0)
|
|
114
|
+
return '';
|
|
115
|
+
let state = {
|
|
116
|
+
...input,
|
|
117
|
+
fullObservationIds: input.fullObservationIds ? new Set(input.fullObservationIds) : new Set(),
|
|
118
|
+
showLastSummaryDetails: input.showLastSummaryDetails ?? true
|
|
119
|
+
};
|
|
120
|
+
let rendered = renderEpisodicContext(state);
|
|
121
|
+
let iterations = 0;
|
|
122
|
+
while (rendered.length > maxChars && iterations < MAX_REDUCTION_ITERATIONS) {
|
|
123
|
+
const next = reduceStateOnce(state);
|
|
124
|
+
if (!next)
|
|
125
|
+
break;
|
|
126
|
+
state = next;
|
|
127
|
+
rendered = renderEpisodicContext(state);
|
|
128
|
+
iterations += 1;
|
|
129
|
+
}
|
|
130
|
+
return rendered;
|
|
131
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/memory/contextBudget.ts
|
|
3
|
+
* Measured Iterative Context Budgeting for Causyne Episodic Memory.
|
|
4
|
+
*
|
|
5
|
+
* Instead of guessing fixed item counts or slicing strings mid-line, `fitEpisodicContextToBudget`
|
|
6
|
+
* renders the context and iteratively drops the most expensive components in a deterministic
|
|
7
|
+
* loss order until the output fits within `maxChars`:
|
|
8
|
+
* 1. Drop expanded observation narratives (keep compact index lines)
|
|
9
|
+
* 2. Drop full prior-session summary narratives (keep request/completed headline)
|
|
10
|
+
* 3. Drop prior session's trailing note
|
|
11
|
+
* 4. Halve session summary count until 0
|
|
12
|
+
* 5. Halve observation count until 1
|
|
13
|
+
*
|
|
14
|
+
* Guarantees valid, well-formed Markdown where headers and core timeline rows survive.
|
|
15
|
+
*
|
|
16
|
+
* Authorship: Strict TypeScript (Causyne Agent).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { EpisodicObservation, EpisodicSessionSummary } from './episodicTypes.js';
|
|
20
|
+
|
|
21
|
+
export const DEFAULT_EPISODIC_BUDGET_CHARS = 3600;
|
|
22
|
+
|
|
23
|
+
export interface EpisodicContextInput {
|
|
24
|
+
readonly project: string;
|
|
25
|
+
readonly observations: readonly EpisodicObservation[];
|
|
26
|
+
readonly summaries: readonly EpisodicSessionSummary[];
|
|
27
|
+
readonly fullObservationIds?: ReadonlySet<number> | undefined;
|
|
28
|
+
readonly showLastSummaryDetails?: boolean | undefined;
|
|
29
|
+
readonly priorTrailingNote?: string | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const MAX_REDUCTION_ITERATIONS = 16;
|
|
33
|
+
|
|
34
|
+
function formatShortTime(epochMs: number): string {
|
|
35
|
+
try {
|
|
36
|
+
const d = new Date(epochMs);
|
|
37
|
+
const yyyy = d.getUTCFullYear();
|
|
38
|
+
const mm = String(d.getUTCMonth() + 1).padStart(2, '0');
|
|
39
|
+
const dd = String(d.getUTCDate()).padStart(2, '0');
|
|
40
|
+
const hh = String(d.getUTCHours()).padStart(2, '0');
|
|
41
|
+
const min = String(d.getUTCMinutes()).padStart(2, '0');
|
|
42
|
+
return `${yyyy}-${mm}-${dd} ${hh}:${min}`;
|
|
43
|
+
} catch {
|
|
44
|
+
return 'recent';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Render the episodic context block for a given state configuration.
|
|
50
|
+
*/
|
|
51
|
+
export function renderEpisodicContext(input: EpisodicContextInput): string {
|
|
52
|
+
if (input.observations.length === 0 && input.summaries.length === 0 && !input.priorTrailingNote) {
|
|
53
|
+
return '';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const lines: string[] = [
|
|
57
|
+
`# EPISODIC WORKSPACE MEMORY (${input.project || 'workspace'}) — ACT-R Ranked`,
|
|
58
|
+
'Use `memory_recall` (`search` -> `timeline` -> `get`) to inspect full historical details on demand.'
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
if (input.observations.length > 0) {
|
|
62
|
+
lines.push('## Key Workspace Observations');
|
|
63
|
+
for (const obs of input.observations) {
|
|
64
|
+
const rBadge = obs.reinforcementCount > 0 ? ` [x${obs.reinforcementCount + 1}]` : '';
|
|
65
|
+
const files = obs.filesModified.length > 0
|
|
66
|
+
? ` (${obs.filesModified.slice(0, 2).join(', ')})`
|
|
67
|
+
: obs.filesRead.length > 0
|
|
68
|
+
? ` (${obs.filesRead.slice(0, 1).join(', ')})`
|
|
69
|
+
: '';
|
|
70
|
+
lines.push(`- #${obs.id} [${obs.type}]${rBadge} ${obs.title}${files} (${formatShortTime(obs.createdAtEpochMs)})`);
|
|
71
|
+
if (input.fullObservationIds?.has(obs.id) && obs.narrative) {
|
|
72
|
+
lines.push(` Narrative: ${obs.narrative.slice(0, 280)}`);
|
|
73
|
+
if (obs.facts.length > 0) {
|
|
74
|
+
lines.push(` Facts: ${obs.facts.slice(0, 3).join('; ')}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (input.summaries.length > 0) {
|
|
81
|
+
lines.push('## Recent Session Checkpoints');
|
|
82
|
+
for (let i = 0; i < input.summaries.length; i++) {
|
|
83
|
+
const s = input.summaries[i];
|
|
84
|
+
if (!s) continue;
|
|
85
|
+
lines.push(`- S#${s.id}: ${s.request.slice(0, 120)} -> ${s.completed.slice(0, 140)}`);
|
|
86
|
+
if (i === 0 && input.showLastSummaryDetails) {
|
|
87
|
+
if (s.learned) lines.push(` Learned: ${s.learned.slice(0, 200)}`);
|
|
88
|
+
if (s.nextSteps) lines.push(` Next: ${s.nextSteps.slice(0, 160)}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (input.priorTrailingNote) {
|
|
94
|
+
lines.push(`## Prior Context Note\n${input.priorTrailingNote.slice(0, 240)}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return lines.join('\n');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function reduceStateOnce(state: EpisodicContextInput): EpisodicContextInput | null {
|
|
101
|
+
// Stage 1: Drop full observation narratives (keep compact timeline titles)
|
|
102
|
+
if (state.fullObservationIds && state.fullObservationIds.size > 0) {
|
|
103
|
+
return { ...state, fullObservationIds: new Set<number>() };
|
|
104
|
+
}
|
|
105
|
+
// Stage 2: Drop full last-session summary details
|
|
106
|
+
if (state.showLastSummaryDetails) {
|
|
107
|
+
return { ...state, showLastSummaryDetails: false };
|
|
108
|
+
}
|
|
109
|
+
// Stage 3: Drop prior trailing note
|
|
110
|
+
if (state.priorTrailingNote !== undefined) {
|
|
111
|
+
return { ...state, priorTrailingNote: undefined };
|
|
112
|
+
}
|
|
113
|
+
// Stage 4: Halve session summaries down to 0
|
|
114
|
+
if (state.summaries.length > 0) {
|
|
115
|
+
const nextCount = Math.floor(state.summaries.length / 2);
|
|
116
|
+
return { ...state, summaries: state.summaries.slice(0, nextCount) };
|
|
117
|
+
}
|
|
118
|
+
// Stage 5: Halve observations down to 1
|
|
119
|
+
if (state.observations.length > 1) {
|
|
120
|
+
const nextCount = Math.max(1, Math.floor(state.observations.length / 2));
|
|
121
|
+
return { ...state, observations: state.observations.slice(0, nextCount) };
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Iteratively reduce `input` until `renderEpisodicContext(state).length <= maxChars`,
|
|
128
|
+
* or until the floor (1 compact observation title, 0 summaries) is reached.
|
|
129
|
+
*/
|
|
130
|
+
export function fitEpisodicContextToBudget(
|
|
131
|
+
input: EpisodicContextInput,
|
|
132
|
+
maxChars: number = DEFAULT_EPISODIC_BUDGET_CHARS
|
|
133
|
+
): string {
|
|
134
|
+
if (maxChars <= 0) return '';
|
|
135
|
+
|
|
136
|
+
let state: EpisodicContextInput = {
|
|
137
|
+
...input,
|
|
138
|
+
fullObservationIds: input.fullObservationIds ? new Set(input.fullObservationIds) : new Set<number>(),
|
|
139
|
+
showLastSummaryDetails: input.showLastSummaryDetails ?? true
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
let rendered = renderEpisodicContext(state);
|
|
143
|
+
let iterations = 0;
|
|
144
|
+
|
|
145
|
+
while (rendered.length > maxChars && iterations < MAX_REDUCTION_ITERATIONS) {
|
|
146
|
+
const next = reduceStateOnce(state);
|
|
147
|
+
if (!next) break;
|
|
148
|
+
state = next;
|
|
149
|
+
rendered = renderEpisodicContext(state);
|
|
150
|
+
iterations += 1;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return rendered;
|
|
154
|
+
}
|
|
155
|
+
|