@zhijiewang/openharness 2.3.1 → 2.4.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.
@@ -993,6 +993,73 @@ register("rebuild-sessions", "Rebuild session search index", () => {
993
993
  });
994
994
  return { output: "Rebuilding session search index...", handled: true };
995
995
  });
996
+ // ── Skill Management ──
997
+ register("skill-create", "Create a new skill file", (args) => {
998
+ const name = args.trim();
999
+ if (!name)
1000
+ return { output: "Usage: /skill-create <name>", handled: true };
1001
+ if (name.includes("..") || name.includes("/") || name.includes("\\")) {
1002
+ return { output: "Error: Invalid skill name.", handled: true };
1003
+ }
1004
+ const dir = join(process.cwd(), ".oh", "skills");
1005
+ mkdirSync(dir, { recursive: true });
1006
+ const slug = name.toLowerCase().replace(/[^a-z0-9]+/g, "-");
1007
+ const filePath = join(dir, `${slug}.md`);
1008
+ if (existsSync(filePath)) {
1009
+ return { output: `Skill "${slug}" already exists at ${filePath}`, handled: true };
1010
+ }
1011
+ const template = `---
1012
+ name: ${slug}
1013
+ description: TODO — describe what this skill does
1014
+ trigger: ${slug}
1015
+ ---
1016
+
1017
+ # ${name}
1018
+
1019
+ ## When to Use
1020
+ Describe when this skill should be triggered.
1021
+
1022
+ ## Procedure
1023
+ 1. Step one
1024
+ 2. Step two
1025
+ 3. Step three
1026
+
1027
+ ## Pitfalls
1028
+ - Common mistakes to avoid
1029
+
1030
+ ## Verification
1031
+ How to confirm the skill worked correctly.
1032
+ `;
1033
+ writeFileSync(filePath, template);
1034
+ return { output: `Created skill: ${filePath}\nEdit the file to customize it.`, handled: true };
1035
+ });
1036
+ register("skill-delete", "Delete a skill file", (args) => {
1037
+ const name = args.trim();
1038
+ if (!name)
1039
+ return { output: "Usage: /skill-delete <name>", handled: true };
1040
+ const { findSkill } = require("../harness/plugins.js");
1041
+ const skill = findSkill(name);
1042
+ if (!skill)
1043
+ return { output: `Skill "${name}" not found.`, handled: true };
1044
+ try {
1045
+ const { unlinkSync } = require("node:fs");
1046
+ unlinkSync(skill.filePath);
1047
+ return { output: `Deleted skill: ${skill.filePath}`, handled: true };
1048
+ }
1049
+ catch (err) {
1050
+ return { output: `Error deleting skill: ${err.message}`, handled: true };
1051
+ }
1052
+ });
1053
+ register("skill-edit", "Show skill file path for editing", (args) => {
1054
+ const name = args.trim();
1055
+ if (!name)
1056
+ return { output: "Usage: /skill-edit <name>", handled: true };
1057
+ const { findSkill } = require("../harness/plugins.js");
1058
+ const skill = findSkill(name);
1059
+ if (!skill)
1060
+ return { output: `Skill "${name}" not found.`, handled: true };
1061
+ return { output: `Skill file: ${skill.filePath}\nEdit this file to update the skill.`, handled: true };
1062
+ });
996
1063
  // ── Command Parser ──
997
1064
  /**
998
1065
  * Check if input is a slash command. If so, execute it.
@@ -68,6 +68,13 @@ export type OhConfig = {
68
68
  balanced?: string;
69
69
  powerful?: string;
70
70
  };
71
+ /** Fallback providers — tried in order when primary fails */
72
+ fallbackProviders?: Array<{
73
+ provider: string;
74
+ model?: string;
75
+ apiKey?: string;
76
+ baseUrl?: string;
77
+ }>;
71
78
  /** Effort level for LLM reasoning depth */
72
79
  effortLevel?: "low" | "medium" | "high" | "max";
73
80
  /** Opt-in telemetry (default: off) */
@@ -9,6 +9,7 @@
9
9
  */
10
10
  import type { Provider } from "../providers/base.js";
11
11
  import type { Message } from "../types/message.js";
12
+ export declare function memoryVersion(): number;
12
13
  /**
13
14
  * Memory types — supports both legacy and Claude Code-compatible names.
14
15
  * Legacy: convention, preference, project, debugging
@@ -28,7 +29,7 @@ export type MemoryEntry = {
28
29
  };
29
30
  /** Load all memories from project and global dirs */
30
31
  export declare function loadMemories(): MemoryEntry[];
31
- /** Build a system prompt section from loaded memories */
32
+ /** Build a system prompt section from loaded memories (capped at MEMORY_PROMPT_MAX_CHARS) */
32
33
  export declare function memoriesToPrompt(memories: MemoryEntry[]): string;
33
34
  /** Save a memory entry to the project memory directory */
34
35
  export declare function saveMemory(name: string, type: MemoryType, description: string, content: string, global?: boolean): string;
@@ -13,6 +13,11 @@ import { join, resolve, sep } from "node:path";
13
13
  import { createUserMessage } from "../types/message.js";
14
14
  const PROJECT_MEMORY_DIR = join(".oh", "memory");
15
15
  const GLOBAL_MEMORY_DIR = join(homedir(), ".oh", "memory");
16
+ // Version counter — incremented on every save, used by query loop for live injection
17
+ let _memoryVersion = 0;
18
+ export function memoryVersion() {
19
+ return _memoryVersion;
20
+ }
16
21
  /** Load all memories from project and global dirs */
17
22
  export function loadMemories() {
18
23
  const entries = [];
@@ -60,12 +65,19 @@ function parseMemory(raw, filePath) {
60
65
  accessCount: accessCountMatch ? parseInt(accessCountMatch[1], 10) : 0,
61
66
  };
62
67
  }
63
- /** Build a system prompt section from loaded memories */
68
+ /** Build a system prompt section from loaded memories (capped at MEMORY_PROMPT_MAX_CHARS) */
64
69
  export function memoriesToPrompt(memories) {
65
70
  if (memories.length === 0)
66
71
  return "";
67
- const lines = memories.map((m) => `- **${m.name}** (${m.type}): ${m.content.slice(0, 200)}`);
68
- return `# Remembered Context\n${lines.join("\n")}`;
72
+ const header = "# Remembered Context\n";
73
+ let result = header;
74
+ for (const m of memories) {
75
+ const line = `- **${m.name}** (${m.type}): ${m.content.slice(0, 200)}\n`;
76
+ if (result.length + line.length > MEMORY_PROMPT_MAX_CHARS)
77
+ break;
78
+ result += line;
79
+ }
80
+ return result.trimEnd();
69
81
  }
70
82
  /** Save a memory entry to the project memory directory */
71
83
  export function saveMemory(name, type, description, content, global = false) {
@@ -90,6 +102,7 @@ accessCount: 0
90
102
  ${content}
91
103
  `;
92
104
  writeFileSync(filePath, md);
105
+ _memoryVersion++;
93
106
  updateMemoryIndex(dir);
94
107
  return filePath;
95
108
  }
@@ -274,7 +287,8 @@ export function consolidateMemories() {
274
287
  }
275
288
  // ── User Profile ──
276
289
  const USER_PROFILE_FILE = "USER.md";
277
- const USER_PROFILE_MAX_CHARS = 2000;
290
+ const USER_PROFILE_MAX_CHARS = 1375; // Matches Hermes USER.md limit
291
+ const MEMORY_PROMPT_MAX_CHARS = 2200; // Matches Hermes MEMORY.md limit
278
292
  /** Load the user profile from .oh/memory/USER.md */
279
293
  export function loadUserProfile() {
280
294
  const filePath = join(PROJECT_MEMORY_DIR, USER_PROFILE_FILE);
@@ -307,6 +321,7 @@ updatedAt: ${Date.now()}
307
321
  ${truncated}
308
322
  `;
309
323
  writeFileSync(join(PROJECT_MEMORY_DIR, USER_PROFILE_FILE), md);
324
+ _memoryVersion++;
310
325
  }
311
326
  /** Format user profile for system prompt injection */
312
327
  export function userProfileToPrompt() {
@@ -0,0 +1,27 @@
1
+ /**
2
+ * FallbackProvider — wraps a primary provider with fallback chain.
3
+ *
4
+ * When the primary provider fails (rate limit, 5xx, network), transparently
5
+ * tries the next provider in the chain. Matches Hermes Agent pattern.
6
+ *
7
+ * Design notes:
8
+ * - Streaming fallback only activates if primary fails BEFORE yielding events.
9
+ * Once events are streaming, partial output can't be un-sent, so we don't
10
+ * catch mid-stream errors (they propagate to the caller for retry).
11
+ * - 401/403 are NOT retriable (they're permanent auth failures). Different
12
+ * providers have different keys, so this is handled at the config level.
13
+ */
14
+ import type { Provider } from "./base.js";
15
+ export type FallbackConfig = {
16
+ provider: Provider;
17
+ model?: string;
18
+ };
19
+ /**
20
+ * Create a provider that falls back to alternatives on failure.
21
+ * The primary provider is tried first. If it fails with a retriable error
22
+ * BEFORE streaming begins, each fallback is tried in order.
23
+ */
24
+ export declare function createFallbackProvider(primary: Provider, fallbacks: FallbackConfig[]): Provider & {
25
+ readonly activeFallback: string | null;
26
+ };
27
+ //# sourceMappingURL=fallback.d.ts.map
@@ -0,0 +1,115 @@
1
+ /**
2
+ * FallbackProvider — wraps a primary provider with fallback chain.
3
+ *
4
+ * When the primary provider fails (rate limit, 5xx, network), transparently
5
+ * tries the next provider in the chain. Matches Hermes Agent pattern.
6
+ *
7
+ * Design notes:
8
+ * - Streaming fallback only activates if primary fails BEFORE yielding events.
9
+ * Once events are streaming, partial output can't be un-sent, so we don't
10
+ * catch mid-stream errors (they propagate to the caller for retry).
11
+ * - 401/403 are NOT retriable (they're permanent auth failures). Different
12
+ * providers have different keys, so this is handled at the config level.
13
+ */
14
+ /**
15
+ * Create a provider that falls back to alternatives on failure.
16
+ * The primary provider is tried first. If it fails with a retriable error
17
+ * BEFORE streaming begins, each fallback is tried in order.
18
+ */
19
+ export function createFallbackProvider(primary, fallbacks) {
20
+ let _activeFallback = null;
21
+ const obj = {
22
+ name: primary.name,
23
+ get activeFallback() {
24
+ return _activeFallback;
25
+ },
26
+ async *stream(messages, systemPrompt, tools, model) {
27
+ // Collect first event to detect early failure vs mid-stream failure.
28
+ // If the provider fails before ANY event, try fallback.
29
+ // If it fails mid-stream, propagate the error (partial output already sent).
30
+ const providers = [
31
+ { provider: primary, model },
32
+ ...fallbacks.map((fb) => ({ provider: fb.provider, model: fb.model ?? model })),
33
+ ];
34
+ for (let i = 0; i < providers.length; i++) {
35
+ const p = providers[i];
36
+ try {
37
+ let hasYielded = false;
38
+ for await (const event of p.provider.stream(messages, systemPrompt, tools, p.model)) {
39
+ hasYielded = true;
40
+ yield event;
41
+ }
42
+ _activeFallback = i === 0 ? null : p.provider.name;
43
+ return;
44
+ }
45
+ catch (err) {
46
+ // Mid-stream failure: can't un-send events, propagate error
47
+ if (i > 0 || !isRetriableError(err))
48
+ throw err;
49
+ // Pre-stream failure on primary: try next provider
50
+ _activeFallback = null;
51
+ continue;
52
+ }
53
+ }
54
+ _activeFallback = null;
55
+ throw new Error("All providers failed (primary + fallbacks)");
56
+ },
57
+ async complete(messages, systemPrompt, tools, model) {
58
+ // complete() is atomic — safe to retry with any provider
59
+ const providers = [
60
+ { provider: primary, model },
61
+ ...fallbacks.map((fb) => ({ provider: fb.provider, model: fb.model ?? model })),
62
+ ];
63
+ for (let i = 0; i < providers.length; i++) {
64
+ const p = providers[i];
65
+ try {
66
+ const result = await p.provider.complete(messages, systemPrompt, tools, p.model);
67
+ _activeFallback = i === 0 ? null : p.provider.name;
68
+ return result;
69
+ }
70
+ catch (err) {
71
+ if (!isRetriableError(err))
72
+ throw err;
73
+ }
74
+ }
75
+ _activeFallback = null;
76
+ throw new Error("All providers failed (primary + fallbacks)");
77
+ },
78
+ listModels() {
79
+ return primary.listModels();
80
+ },
81
+ async healthCheck() {
82
+ if (await primary.healthCheck())
83
+ return true;
84
+ for (const fb of fallbacks) {
85
+ if (await fb.provider.healthCheck())
86
+ return true;
87
+ }
88
+ return false;
89
+ },
90
+ estimateTokens: primary.estimateTokens?.bind(primary),
91
+ getModelInfo: primary.getModelInfo?.bind(primary),
92
+ };
93
+ return obj;
94
+ }
95
+ /** Check if an error is worth retrying with a different provider */
96
+ function isRetriableError(err) {
97
+ if (!(err instanceof Error))
98
+ return false;
99
+ const msg = err.message.toLowerCase();
100
+ return (msg.includes("rate limit") ||
101
+ msg.includes("429") ||
102
+ msg.includes("too many requests") ||
103
+ msg.includes("overloaded") ||
104
+ msg.includes("503") ||
105
+ msg.includes("529") ||
106
+ msg.includes("service unavailable") ||
107
+ msg.includes("econnrefused") ||
108
+ msg.includes("network") ||
109
+ msg.includes("timeout")
110
+ // Note: 401/403 are NOT retriable — they're permanent auth failures.
111
+ // Different providers use different API keys, so auth issues don't
112
+ // benefit from fallback. The user should fix their API key.
113
+ );
114
+ }
115
+ //# sourceMappingURL=fallback.js.map
@@ -57,6 +57,13 @@ export async function* query(userMessage, config, existingMessages = []) {
57
57
  }
58
58
  }
59
59
  catch { /* skills optional */ }
60
+ // Track memory version for live injection
61
+ let lastMemoryVer = 0;
62
+ try {
63
+ const { memoryVersion } = await import("../harness/memory.js");
64
+ lastMemoryVer = memoryVersion();
65
+ }
66
+ catch { /* ignore */ }
60
67
  const state = {
61
68
  messages: [...existingMessages, createUserMessage(userMessage)],
62
69
  turn: 0,
@@ -93,13 +100,43 @@ export async function* query(userMessage, config, existingMessages = []) {
93
100
  }
94
101
  }
95
102
  }
103
+ // ── Dynamic prompt: refresh memories if changed, inject warnings ──
104
+ try {
105
+ const { memoryVersion, loadActiveMemories, memoriesToPrompt } = await import("../harness/memory.js");
106
+ const currentVer = memoryVersion();
107
+ if (currentVer > lastMemoryVer) {
108
+ const fresh = memoriesToPrompt(loadActiveMemories());
109
+ // Replace or append memory section in fullSystemPrompt
110
+ if (fullSystemPrompt.includes("# Remembered Context")) {
111
+ fullSystemPrompt = fullSystemPrompt.replace(/# Remembered Context[\s\S]*?(?=\n# |$)/, fresh);
112
+ }
113
+ else if (fresh) {
114
+ fullSystemPrompt += `\n\n${fresh}`;
115
+ }
116
+ lastMemoryVer = currentVer;
117
+ }
118
+ }
119
+ catch { /* memory refresh optional */ }
120
+ let turnPrompt = fullSystemPrompt;
121
+ if (config.maxCost && config.maxCost > 0) {
122
+ const pct = state.totalCost / config.maxCost;
123
+ if (pct >= 0.9) {
124
+ turnPrompt += `\n\n⚠️ BUDGET CRITICAL: Only $${(config.maxCost - state.totalCost).toFixed(4)} remaining. Provide final response NOW.`;
125
+ }
126
+ else if (pct >= 0.7) {
127
+ turnPrompt += `\n\n⚠️ BUDGET WARNING: ${Math.round((1 - pct) * 100)}% budget remaining. Start consolidating.`;
128
+ }
129
+ }
130
+ if (state.turn >= maxTurns * 0.9 && maxTurns > 1) {
131
+ turnPrompt += `\n\n⚠️ TURN LIMIT: ${maxTurns - state.turn} turn(s) remaining. Wrap up.`;
132
+ }
96
133
  // ── LLM call with streaming ──
97
134
  let assistantContent = "";
98
135
  const toolCalls = [];
99
136
  let streamError = null;
100
137
  const streamingExecutor = new StreamingToolExecutor(config.tools, toolContext, config.permissionMode, config.askUser, config.abortSignal);
101
138
  try {
102
- for await (const event of config.provider.stream(state.messages, fullSystemPrompt, apiTools, config.model)) {
139
+ for await (const event of config.provider.stream(state.messages, turnPrompt, apiTools, config.model)) {
103
140
  if (config.abortSignal?.aborted)
104
141
  break;
105
142
  switch (event.type) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhijiewang/openharness",
3
- "version": "2.3.1",
3
+ "version": "2.4.0",
4
4
  "description": "Open-source terminal coding agent. Works with any LLM.",
5
5
  "type": "module",
6
6
  "bin": {