loopengine 0.1.12 → 0.1.14

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.
@@ -29,6 +29,14 @@ export interface PackageManifest {
29
29
  * same role .create-loopengine.json already plays for template files. */
30
30
  export interface InstalledPackageRecord {
31
31
  version: string;
32
+ /** The exact spec (bare registry name, "name@version", a git+ssh/
33
+ * git+https URL with a #committish, a file: path, ...) last used to
34
+ * successfully install or upgrade this package — see
35
+ * oldContentSpec's own doc comment for why upgradePackage needs this
36
+ * stored verbatim rather than reconstructed from `packageName`.
37
+ * Optional only because a package installed before this field existed
38
+ * has no recorded value — see oldContentSpec's own fallback. */
39
+ spec?: string;
32
40
  tools: string[];
33
41
  skills: string[];
34
42
  actauthRules: string[];
@@ -99,14 +107,14 @@ export interface UpgradeFileResult {
99
107
  * to `packageName`, i.e. "latest") currently resolves to. Tool and skill
100
108
  * files get a real three-way merge (mine = current file, possibly
101
109
  * hand-edited since install; base = the version recorded at
102
- * install/last-upgrade, refetched via `<spec>@<oldVersion>`; theirs =
103
- * newly fetched) — identical technique to `create-loopengine upgrade`,
104
- * just applied to package-managed files instead of template files.
105
- * actauth rules upgrade per-rule instead: the target actauth.yml holds
106
- * rules from other packages and hand-written ones too, so there's no
107
- * coherent "whole file" base/theirs to merge — a rule unchanged since
108
- * install updates cleanly; a hand-edited one is left alone and reported
109
- * as a conflict rather than overwritten. */
110
+ * install/last-upgrade, refetched via oldContentSpec; theirs = newly
111
+ * fetched) — identical technique to `create-loopengine upgrade`, just
112
+ * applied to package-managed files instead of template files. actauth
113
+ * rules upgrade per-rule instead: the target actauth.yml holds rules
114
+ * from other packages and hand-written ones too, so there's no coherent
115
+ * "whole file" base/theirs to merge — a rule unchanged since install
116
+ * updates cleanly; a hand-edited one is left alone and reported as a
117
+ * conflict rather than overwritten. */
110
118
  export declare function upgradePackage(agentName: string, packageName: string, options?: FetchOptions & {
111
119
  spec?: string;
112
120
  }): Promise<{
@@ -283,6 +283,7 @@ export async function installPackage(agentName, spec, options = {}) {
283
283
  }
284
284
  provenance[manifest.name] = {
285
285
  version: manifest.version,
286
+ spec,
286
287
  tools: toolNames,
287
288
  skills: skillIds,
288
289
  actauthRules: rules.map((r) => r.name),
@@ -321,18 +322,53 @@ function mergeFile(mine, base, theirs) {
321
322
  rmSync(scratchDir, { recursive: true, force: true });
322
323
  return { merged, conflicted };
323
324
  }
325
+ // A git+ssh/git+https/git: URL, a plain https: tarball URL, or a local
326
+ // file:/relative/absolute path all already pin to exact, immutable
327
+ // content on their own (a #committish, or the file/tarball's own
328
+ // content) — appending "@version" to one of these doesn't select an
329
+ // older version the way it does for a registry specifier, it corrupts
330
+ // the spec. Confirmed live: `npm pack 'git+file://...#v1.0.0@1.0.0'`
331
+ // fails outright ("The git reference could not be found... pathspec
332
+ // 'v1.0.0@1.0.0'"), it doesn't fall back to resolving just the tag.
333
+ const PINNED_SPEC = /^(git\+|git:|https?:|file:|\.\.?\/|\/)/;
334
+ /** What to fetch to reconstruct the exact content that was installed or
335
+ * last upgraded to, for use as the three-way merge's `base` — as
336
+ * distinct from `spec`, which is what the *new* content resolves to.
337
+ * A plain registry specifier (bare or scoped name, with or without its
338
+ * own "@version") is repinned to `version` — the manifest's own
339
+ * declared version at that install/upgrade, which is what actually got
340
+ * written to disk, regardless of whether the range originally given
341
+ * would still resolve there today. A git/file/URL spec is returned
342
+ * as-is — see PINNED_SPEC's own doc comment for why appending "@version"
343
+ * to one of those breaks instead of pinning. `recordedSpec` is only
344
+ * absent for a package installed before InstalledPackageRecord.spec
345
+ * existed; falling back to `packageName` there reproduces this
346
+ * function's own old (buggy for a git/file install) behavior exactly —
347
+ * no worse than before, and only for a package that hasn't upgraded
348
+ * since. */
349
+ function oldContentSpec(recordedSpec, packageName, version) {
350
+ const base = recordedSpec ?? packageName;
351
+ if (PINNED_SPEC.test(base))
352
+ return base;
353
+ // Strip any version/tag the spec already carries (a scoped name's own
354
+ // leading '@' isn't this — only a second '@' after the name is) before
355
+ // repinning, so re-upgrading an already-version-pinned install doesn't
356
+ // produce a doubled-up "name@1.0.0@1.0.0".
357
+ const bareName = base.startsWith('@') ? `@${base.slice(1).split('@')[0]}` : base.split('@')[0];
358
+ return `${bareName}@${version}`;
359
+ }
324
360
  /** Upgrades an already-installed package to whatever `spec` (defaulting
325
361
  * to `packageName`, i.e. "latest") currently resolves to. Tool and skill
326
362
  * files get a real three-way merge (mine = current file, possibly
327
363
  * hand-edited since install; base = the version recorded at
328
- * install/last-upgrade, refetched via `<spec>@<oldVersion>`; theirs =
329
- * newly fetched) — identical technique to `create-loopengine upgrade`,
330
- * just applied to package-managed files instead of template files.
331
- * actauth rules upgrade per-rule instead: the target actauth.yml holds
332
- * rules from other packages and hand-written ones too, so there's no
333
- * coherent "whole file" base/theirs to merge — a rule unchanged since
334
- * install updates cleanly; a hand-edited one is left alone and reported
335
- * as a conflict rather than overwritten. */
364
+ * install/last-upgrade, refetched via oldContentSpec; theirs = newly
365
+ * fetched) — identical technique to `create-loopengine upgrade`, just
366
+ * applied to package-managed files instead of template files. actauth
367
+ * rules upgrade per-rule instead: the target actauth.yml holds rules
368
+ * from other packages and hand-written ones too, so there's no coherent
369
+ * "whole file" base/theirs to merge — a rule unchanged since install
370
+ * updates cleanly; a hand-edited one is left alone and reported as a
371
+ * conflict rather than overwritten. */
336
372
  export async function upgradePackage(agentName, packageName, options = {}) {
337
373
  const fetch = options.fetchPackageDir ?? fetchPackageDir;
338
374
  const provenance = readProvenance(agentName);
@@ -341,7 +377,7 @@ export async function upgradePackage(agentName, packageName, options = {}) {
341
377
  throw new PackageNotInstalledError(`Package '${packageName}' isn't installed for '${agentName}'.`);
342
378
  }
343
379
  const spec = options.spec ?? packageName;
344
- const oldDir = fetch(`${spec}@${record.version}`);
380
+ const oldDir = fetch(oldContentSpec(record.spec, packageName, record.version));
345
381
  const newDir = fetch(spec);
346
382
  const oldManifest = readManifest(oldDir);
347
383
  const newManifest = readManifest(newDir);
@@ -416,7 +452,7 @@ export async function upgradePackage(agentName, packageName, options = {}) {
416
452
  updateActauthRule(agentName, ruleName, { scope: newRule.scope, tool: newRule.tool, decision: newRule.decision });
417
453
  results.push({ path, status: 'updated' });
418
454
  }
419
- provenance[packageName] = { ...record, version: newManifest.version, contentHashes: newContentHashes, env: newManifest.env ?? record.env };
455
+ provenance[packageName] = { ...record, version: newManifest.version, spec, contentHashes: newContentHashes, env: newManifest.env ?? record.env };
420
456
  writeProvenance(agentName, provenance);
421
457
  return { files: results };
422
458
  }
@@ -1,6 +1,7 @@
1
1
  import type { Rule, Decision, Scope } from 'actauth';
2
2
  import type { Redis } from 'ioredis';
3
3
  import type { SafetyClassifier } from 'toollane';
4
+ import type { Summarizer } from './compaction.js';
4
5
  /** Which channel a runAgent() call is on — the value RunAgentOptions.channel
5
6
  * carries, checked against directly by AgentConfig.httpNotifier's own
6
7
  * resolution (see its own doc comment for why it only ever matches
@@ -443,6 +444,21 @@ export interface AgentConfig {
443
444
  skillsDirs?: string[];
444
445
  skillIndexBudgetTokens?: number;
445
446
  contextBudgetTokens?: number;
447
+ /** How compaction.ts's Compactor compresses old history once a turn
448
+ * goes over contextBudgetTokens. Omitted entirely defaults to
449
+ * TruncatingSummarizer — a real, dependency-free fallback that just
450
+ * concatenates and cuts text, with no model call and no added latency
451
+ * or cost. Pass `new LLMSummarizer({ modelCall })` (llm-summarizer.js)
452
+ * to compact with a real model call instead — genuinely worth it for
453
+ * an agent whose sessions run long enough to hit compaction at all,
454
+ * since a fact stated once early on (an order number, an id) tends to
455
+ * survive a structured LLM summary but not a hard character cutoff.
456
+ * Not defaulted to LLMSummarizer automatically: that would add a
457
+ * hidden extra model call — cost, latency, and a new failure mode —
458
+ * to every agent, including ones that never expected compaction to do
459
+ * anything but this repo's own tests, which assert exact modelCall
460
+ * call counts. */
461
+ summarizer?: Summarizer;
446
462
  /** Hard cap on model calls in one runAgent() invocation — the only thing
447
463
  * that stops a model stuck re-requesting the same (or ping-ponging)
448
464
  * tool calls forever, which nothing else in this loop bounds. Default
@@ -0,0 +1,24 @@
1
+ import type { Message } from './budget.js';
2
+ import type { ModelCall } from './run-agent.js';
3
+ import { type Summarizer } from './compaction.js';
4
+ export interface LLMSummarizerOptions {
5
+ modelCall: ModelCall;
6
+ /** Replaces the default instructions entirely — not appended to them. */
7
+ systemPrompt?: string;
8
+ }
9
+ /** Calls modelCall with no tools and asks it to compact `messages` per
10
+ * DEFAULT_SYSTEM_PROMPT's own structure (see that constant's own doc
11
+ * comment for why "facts established" is its own named section rather
12
+ * than left to a generic "summarize this" prompt — a fact mentioned once
13
+ * early in a long conversation is exactly what a free-form summary tends
14
+ * to paraphrase away). Falls back to TruncatingSummarizer — rather than
15
+ * throwing, which would surface as a whole turn failing — if the call
16
+ * itself throws (rate limit, network) or comes back with no usable text
17
+ * (a model that only ever returns tool_use has nothing this can use). */
18
+ export declare class LLMSummarizer implements Summarizer {
19
+ private readonly modelCall;
20
+ private readonly systemPrompt;
21
+ private readonly fallback;
22
+ constructor(options: LLMSummarizerOptions);
23
+ summarize(messages: Message[]): Promise<Message>;
24
+ }
@@ -0,0 +1,42 @@
1
+ import { TruncatingSummarizer } from './compaction.js';
2
+ const DEFAULT_SYSTEM_PROMPT = `You are compacting an in-progress agent conversation to free up context window. You will be given a segment of older messages (the most recent messages are kept verbatim elsewhere and are not shown to you). Write a dense summary that lets the agent continue this task without having seen the original messages.
3
+
4
+ Structure your summary with these sections, using only the ones that have content:
5
+ - Facts established: concrete values stated even once — ids, numbers, names, dates, amounts, decisions — verbatim, not paraphrased. A fact that only appears here and gets paraphrased or dropped is lost for good.
6
+ - Actions taken: tool calls made and their outcomes, briefly.
7
+ - Open question / next step: what's still unresolved, if anything.
8
+
9
+ Output only the summary itself — no preamble, no "here is a summary".`;
10
+ /** Calls modelCall with no tools and asks it to compact `messages` per
11
+ * DEFAULT_SYSTEM_PROMPT's own structure (see that constant's own doc
12
+ * comment for why "facts established" is its own named section rather
13
+ * than left to a generic "summarize this" prompt — a fact mentioned once
14
+ * early in a long conversation is exactly what a free-form summary tends
15
+ * to paraphrase away). Falls back to TruncatingSummarizer — rather than
16
+ * throwing, which would surface as a whole turn failing — if the call
17
+ * itself throws (rate limit, network) or comes back with no usable text
18
+ * (a model that only ever returns tool_use has nothing this can use). */
19
+ export class LLMSummarizer {
20
+ modelCall;
21
+ systemPrompt;
22
+ fallback = new TruncatingSummarizer();
23
+ constructor(options) {
24
+ this.modelCall = options.modelCall;
25
+ this.systemPrompt = options.systemPrompt ?? DEFAULT_SYSTEM_PROMPT;
26
+ }
27
+ async summarize(messages) {
28
+ const transcript = messages.map((m) => `[${m.role}] ${m.content}`).join('\n');
29
+ let text;
30
+ try {
31
+ const response = await this.modelCall([{ role: 'user', content: transcript }], this.systemPrompt, []);
32
+ text = response.content.find((block) => block.type === 'text')?.text?.trim();
33
+ }
34
+ catch {
35
+ // modelCall threw — fall through to the truncating fallback below,
36
+ // same as text staying undefined.
37
+ }
38
+ if (!text)
39
+ return this.fallback.summarize(messages);
40
+ return { role: 'system', content: `[compacted ${messages.length} earlier message(s)]\n${text}` };
41
+ }
42
+ }
@@ -336,7 +336,12 @@ async function buildTurnContext(config, modelCall, options) {
336
336
  // 70000-token nudge point) and hardThreshold's 0.92 still leave real
337
337
  // margin before hitting it.
338
338
  const budgetTracker = new BudgetTracker({ budgetTokens: config.contextBudgetTokens ?? 100000 });
339
- const compactor = new Compactor({ budgetTokens: config.contextBudgetTokens ?? 100000, softThreshold: budgetTracker.softThreshold, tailMessages });
339
+ const compactor = new Compactor({
340
+ budgetTokens: config.contextBudgetTokens ?? 100000,
341
+ softThreshold: budgetTracker.softThreshold,
342
+ tailMessages,
343
+ summarizer: config.summarizer,
344
+ });
340
345
  const rules = loadRules(config);
341
346
  // AgentConfig.httpNotifier only ever stands in for the http channel —
342
347
  // see its own doc comment for why cli/http_stream never consult it at
package/dist/index.d.ts CHANGED
@@ -24,6 +24,10 @@ export { discoverAgents, loadAgentModule, synthesizeCreateModelCall } from './co
24
24
  export type { AgentModule } from './core/discover-agents.js';
25
25
  export { agentAsTool } from './core/agent-as-tool.js';
26
26
  export { resumeAgent, loadRules, loadDefaultTools, loadSubagentAsTools, systemTools, systemSkillsDir } from './core/run-agent.js';
27
+ export { Compactor, TruncatingSummarizer } from './core/compaction.js';
28
+ export type { Summarizer, RecoverResult, RecoverAction } from './core/compaction.js';
29
+ export { LLMSummarizer } from './core/llm-summarizer.js';
30
+ export type { LLMSummarizerOptions } from './core/llm-summarizer.js';
27
31
  export { createCheckpointStore } from './core/durable-approvals.js';
28
32
  export type { TurnCheckpoint, CheckpointStore, OutstandingItem } from './core/durable-approvals.js';
29
33
  export { addGatewayTool, agentDir, disconnectComposioAccount, describeGatewayTools, listComposioConnections, listComposioTools, loadGatewayToolsFromDir, removeGatewayTool, removeGatewayToolSlug, GatewayToolExistsError, GatewayToolNotFoundError, } from './core/gateway-tools.js';
package/dist/index.js CHANGED
@@ -29,6 +29,8 @@ export { agentAsTool } from './core/agent-as-tool.js';
29
29
  // copy of adapters/http.ts (which, like agent-registry.ts, is meant to be
30
30
  // owned/edited, not imported — see this file's own header comment).
31
31
  export { resumeAgent, loadRules, loadDefaultTools, loadSubagentAsTools, systemTools, systemSkillsDir } from './core/run-agent.js';
32
+ export { Compactor, TruncatingSummarizer } from './core/compaction.js';
33
+ export { LLMSummarizer } from './core/llm-summarizer.js';
32
34
  export { createCheckpointStore } from './core/durable-approvals.js';
33
35
  export { addGatewayTool, agentDir, disconnectComposioAccount, describeGatewayTools, listComposioConnections, listComposioTools, loadGatewayToolsFromDir, removeGatewayTool, removeGatewayToolSlug, GatewayToolExistsError, GatewayToolNotFoundError, } from './core/gateway-tools.js';
34
36
  export { WebhookNotifier } from './core/http-notify-triggers/webhook.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "loopengine",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "type": "module",
5
5
  "description": "Runtime for tool-using AI agents with a transparent ReAct loop.",
6
6
  "license": "MIT",
@@ -59,6 +59,8 @@
59
59
  "dist/core/budget.d.ts",
60
60
  "dist/core/compaction.js",
61
61
  "dist/core/compaction.d.ts",
62
+ "dist/core/llm-summarizer.js",
63
+ "dist/core/llm-summarizer.d.ts",
62
64
  "dist/core/recovery.js",
63
65
  "dist/core/recovery.d.ts",
64
66
  "dist/core/http-notifier.js",