A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[PR -- Features] Revert #2154/#2155: restore the per-kind harness wrappers and manual refinement methods (#2670)
Seth Kartenacc5bc09a0ad
diff --git a/packages/coding-agent/.changes/res-1336-harness-crud-trio.md b/packages/coding-agent/.changes/res-1336-harness-crud-trio.mddeleted file mode 100644index 6fd0e06547..0000000000--- a/packages/coding-agent/.changes/res-1336-harness-crud-trio.md+++ /dev/null@@ -1,2 +0,0 @@-- Folded the twelve per-kind continual-harness CRUD methods into three: `rlm.harness.create_memory(title, content, kind=...)`, `rlm.harness.update_memory(id, title, content, kind=...)`, and `rlm.harness.delete_memory(id, kind=...)`. Prompt notes, skills, and subagent specs are subtypes of memory, so `kind` selects them; it defaults to `memory`, only skill entries accept `reference`/`arguments`, the nine removed names raise an error naming their replacement, and the positional-kind `create`/`update`/`delete` methods are removed as well.-- Renamed the harness entry grouping from `path` to `topic` in the kernel API, the refinement planner schema, and the harness digest; state files written before the rename still load, and saves keep writing the grouping under both names while older builds can still reach the shared global store.diff --git a/packages/coding-agent/.changes/res-1337-drop-manual-record-refinement.md b/packages/coding-agent/.changes/res-1337-drop-manual-record-refinement.mddeleted file mode 100644index 43922ec54f..0000000000--- a/packages/coding-agent/.changes/res-1337-drop-manual-record-refinement.md+++ /dev/null@@ -1 +0,0 @@-- Removed `rlm.harness.record_refinement` and `plan_refinement` from the model-facing kernel surface. The refinement engine already records an event for every refinement it applies, so the manual call only duplicated those events or added unverified ones. Reading stays unchanged through `rlm.get_harness_state().refinements` and the harness overview; the old name now raises an error explaining that events are recorded automatically.diff --git a/packages/coding-agent/.changes/revert-harness-crud-pair.md b/packages/coding-agent/.changes/revert-harness-crud-pair.mdnew file mode 100644index 0000000000..4179957011--- /dev/null+++ b/packages/coding-agent/.changes/revert-harness-crud-pair.md@@ -0,0 +1,2 @@+- Changed the continual-harness kernel surface back to the twelve per-kind CRUD methods on `rlm.harness` (`create_memory`/`update_memory`/`delete_memory` plus the `*_skill`, `*_subagent`, and `*_prompt_note` methods), the `path` spelling of the harness entry grouping, and callable `rlm.harness.record_refinement`/`plan_refinement`, reverting #2154 and #2155 at the user's request.+- Fixed harness state persisted while the grouping was spelled `topic` losing that grouping after the revert: entries migrate to `path` when they load, and rolling back a refinement recorded in that window restores the entry's original grouping.diff --git a/packages/coding-agent/docs/rlm-runtime.md b/packages/coding-agent/docs/rlm-runtime.mdindex 61b0377eef..a9c1e56e4f 100644--- a/packages/coding-agent/docs/rlm-runtime.md+++ b/packages/coding-agent/docs/rlm-runtime.md@@ -188,8 +188,6 @@ On reload, the aggregate is reapplied to the parent message. Context-tree report`rlm.harness` is a persisted state ledger for prompt notes, memories, reusable skill descriptions, sub-agent specifications, and refinement events. It is not a second execution engine.-Entries are written with three calls: `rlm.harness.create_memory(title, content, kind=...)`, `rlm.harness.update_memory(id, title, content, kind=...)`, and `rlm.harness.delete_memory(id, kind=...)`. Prompt notes, skills, and subagent specs are subtypes of memory, so one method per operation covers every kind. `kind` is `prompt`, `memory`, `skill`, or `subagent` and defaults to `memory`; `topic=` groups entries; only skill entries accept `reference=` and `arguments=`.-Session-local state lives in the session artifact directory under `harness/harness_state.json`. Explicitly global entries live under `~/.prime/agent/harness/`. The Python store reloads after external modification so host-side `/refine` writes and kernel writes do not overwrite each other.`/refine` runs a dedicated review over the current trajectory and applies small create/update/delete edits. Rollback uses recorded before/after snapshots. The base system prompt remains immutable; refinements are supplemental state.diff --git a/packages/coding-agent/src/core/kernel/bootstrap.ts b/packages/coding-agent/src/core/kernel/bootstrap.tsindex 5403026ea5..dd56934df1 100644--- a/packages/coding-agent/src/core/kernel/bootstrap.ts+++ b/packages/coding-agent/src/core/kernel/bootstrap.ts@@ -80,8 +80,22 @@ const REQUIRED_MCP_DISCOVERY_METHODS = ["search_tools","describe_tool",];-const REQUIRED_HARNESS_METHODS = ["create_memory", "update_memory", "delete_memory"];-export const RUNTIME_READY_CHECK = `import inspect; import rlm; from rlm import McpIntegration; import rlm.mcp as mcp; from rlm.harness import HarnessEntry; _harness_methods = ${JSON.stringify(REQUIRED_HARNESS_METHODS)}; assert callable(mcp.list_tools); assert callable(mcp.call_tool); assert all(callable(getattr(mcp, _m, None)) for _m in ${JSON.stringify(REQUIRED_MCP_DISCOVERY_METHODS)}), "rlm.mcp is missing MCP discovery methods (list_plugins, search_plugins, list_connections, search_tools, describe_tool); the kernel venv needs a current prime-agent-runtime"; assert callable(rlm.spawn); assert hasattr(rlm, 'rlm'); assert callable(rlm.rlm.spawn); assert inspect.signature(rlm.spawn).parameters['name'].default is inspect.Parameter.empty; assert not hasattr(rlm, 'run'); assert not hasattr(rlm.rlm, 'run'); assert callable(rlm.host_request); assert callable(rlm.find_models); assert callable(rlm.rlm.find_models); assert callable(rlm.create_session); assert callable(rlm.rlm.create_session); assert callable(rlm.progress_note); assert callable(rlm.rlm.progress_note); assert hasattr(rlm, 'harness'); assert hasattr(rlm, 'get_harness_state'); assert hasattr(rlm.rlm, 'harness'); assert hasattr(rlm.rlm, 'get_harness_state'); assert all(callable(getattr(_harness, _method, None)) for _harness in (rlm.harness, rlm.rlm.harness) for _method in _harness_methods); assert 'reference' in HarnessEntry.__dataclass_fields__; assert 'scope' in HarnessEntry.__dataclass_fields__; assert 'topic' in HarnessEntry.__dataclass_fields__; assert {'kind', 'topic', 'reference', 'arguments', 'global_'} <= set(inspect.signature(rlm.harness.create_memory).parameters); assert {'kind', 'topic', 'reference', 'arguments', 'global_'} <= set(inspect.signature(rlm.harness.update_memory).parameters); assert {'kind', 'global_'} <= set(inspect.signature(rlm.harness.delete_memory).parameters); assert 'global_' in inspect.signature(rlm.get_harness_state).parameters; assert not hasattr(rlm, 'background'); assert not hasattr(rlm.rlm, 'background'); from rlm.bash import BashHandle, BashResult; assert callable(rlm.bash); assert all(callable(getattr(BashHandle, _m, None)) for _m in ('tail', 'output', 'poll', 'kill')); assert {'exit_code', 'output', 'duration'} <= set(BashResult.__dataclass_fields__); import rlm.repl as _repl; assert callable(_repl.main); assert callable(_repl.emit); assert callable(_repl.host_request); assert callable(_repl.is_active); assert _repl.PROTOCOL_VERSION == 3; assert callable(rlm.emit); assert not hasattr(rlm, 'HOST_COMM_TARGET'); assert not hasattr(mcp, 'install_shutdown_hook')`;+const REQUIRED_HARNESS_METHODS = [+ "create_memory",+ "update_memory",+ "delete_memory",+ "create_skill",+ "update_skill",+ "delete_skill",+ "create_subagent",+ "update_subagent",+ "delete_subagent",+ "create_prompt_note",+ "update_prompt_note",+ "delete_prompt_note",+ "record_refinement",+];+export const RUNTIME_READY_CHECK = `import inspect; import rlm; from rlm import McpIntegration; import rlm.mcp as mcp; from rlm.harness import HarnessEntry; _harness_methods = ${JSON.stringify(REQUIRED_HARNESS_METHODS)}; assert callable(mcp.list_tools); assert callable(mcp.call_tool); assert all(callable(getattr(mcp, _m, None)) for _m in ${JSON.stringify(REQUIRED_MCP_DISCOVERY_METHODS)}), "rlm.mcp is missing MCP discovery methods (list_plugins, search_plugins, list_connections, search_tools, describe_tool); the kernel venv needs a current prime-agent-runtime"; assert callable(rlm.spawn); assert hasattr(rlm, 'rlm'); assert callable(rlm.rlm.spawn); assert inspect.signature(rlm.spawn).parameters['name'].default is inspect.Parameter.empty; assert not hasattr(rlm, 'run'); assert not hasattr(rlm.rlm, 'run'); assert callable(rlm.host_request); assert callable(rlm.find_models); assert callable(rlm.rlm.find_models); assert callable(rlm.create_session); assert callable(rlm.rlm.create_session); assert callable(rlm.progress_note); assert callable(rlm.rlm.progress_note); assert hasattr(rlm, 'harness'); assert hasattr(rlm, 'get_harness_state'); assert hasattr(rlm.rlm, 'harness'); assert hasattr(rlm.rlm, 'get_harness_state'); assert all(callable(getattr(_harness, _method, None)) for _harness in (rlm.harness, rlm.rlm.harness) for _method in _harness_methods); assert 'reference' in HarnessEntry.__dataclass_fields__; assert 'scope' in HarnessEntry.__dataclass_fields__; assert 'reference' in inspect.signature(rlm.harness.create_skill).parameters; assert 'reference' in inspect.signature(rlm.harness.update_skill).parameters; assert 'global_' in inspect.signature(rlm.harness.create_memory).parameters; assert 'global_' in inspect.signature(rlm.get_harness_state).parameters; assert not hasattr(rlm, 'background'); assert not hasattr(rlm.rlm, 'background'); from rlm.bash import BashHandle, BashResult; assert callable(rlm.bash); assert all(callable(getattr(BashHandle, _m, None)) for _m in ('tail', 'output', 'poll', 'kill')); assert {'exit_code', 'output', 'duration'} <= set(BashResult.__dataclass_fields__); import rlm.repl as _repl; assert callable(_repl.main); assert callable(_repl.emit); assert callable(_repl.host_request); assert callable(_repl.is_active); assert _repl.PROTOCOL_VERSION == 3; assert callable(rlm.emit); assert not hasattr(rlm, 'HOST_COMM_TARGET'); assert not hasattr(mcp, 'install_shutdown_hook')`;const BOOTSTRAP_VERSION_FILE = ".bootstrap-version";const BOOTSTRAP_VERSION_TMP_FILE = `${BOOTSTRAP_VERSION_FILE}.tmp`;const BOOTSTRAP_LOCK_NAME = ".bootstrap.lock";@@ -949,7 +963,7 @@ async function ensureKernelPythonUncached(const missing: string[] = [];if (!(await hasPrimeAgentRuntime(python))) {missing.push(- "a current prime-agent-runtime with callable rlm.spawn, rlm.create_session, rlm.host_request, rlm.progress_note, and rlm.harness.create_memory/update_memory/delete_memory",+ "a current prime-agent-runtime with callable rlm.spawn, rlm.create_session, rlm.host_request, rlm.progress_note, and explicit harness CRUD methods",);}if (missing.length === 0) {diff --git a/packages/coding-agent/src/core/prompts/rlm.ts b/packages/coding-agent/src/core/prompts/rlm.tsindex ac48f9bfbc..6a669324cf 100644--- a/packages/coding-agent/src/core/prompts/rlm.ts+++ b/packages/coding-agent/src/core/prompts/rlm.ts@@ -44,7 +44,7 @@ const REPL_CONTROL_PROMPT = ["","Python state in the kernel persists across cells: named variables, helper functions, classes, imports, notes, parsed outputs, and helper data structures all remain available in every later turn. Tool calls are themselves Python `await` expressions, so their return values can be bound to variables and composed into program logic just like any other call.","",- "Continual harness state is available as `rlm.harness` and `rlm.get_harness_state()`. CRUD calls are local to this Prime Agent session by default: `rlm.harness.create_memory(title, content, kind=...)`, `rlm.harness.update_memory(id, title, content, kind=...)`, and `rlm.harness.delete_memory(id, kind=...)`, plus `rlm.harness.overview()`. Refinement events are recorded for you whenever a refinement runs. Prompt notes, skills, and subagent specs are subtypes of memory: `kind` is one of `'prompt'`, `'memory'`, `'skill'`, `'subagent'` and defaults to `'memory'`; group entries with `topic=`. Only skill entries accept `reference=` and `arguments=`. Use `global_=True` only for stable cross-session lessons; Python reserves `global`, so literal `global=True` is invalid syntax.",+ "Continual harness state is available as `rlm.harness` and `rlm.get_harness_state()`. CRUD calls are local to this Prime Agent session by default: `rlm.harness.create_memory(...)`, `rlm.harness.update_memory(...)`, `rlm.harness.delete_memory(...)`, `rlm.harness.create_skill(...)`, `rlm.harness.update_skill(...)`, `rlm.harness.delete_skill(...)`, `rlm.harness.create_subagent(...)`, `rlm.harness.update_subagent(...)`, `rlm.harness.delete_subagent(...)`, `rlm.harness.create_prompt_note(...)`, `rlm.harness.update_prompt_note(...)`, `rlm.harness.delete_prompt_note(...)`, plus `rlm.harness.record_refinement(...)` and `rlm.harness.overview()`. Use `global_=True` only for stable cross-session lessons; Python reserves `global`, so literal `global=True` is invalid syntax.","","Terminology: continual harness names the persisted prompt, memory, skill, and subagent layer; RLM names the runtime, Python REPL kernel, and native call interface exposed to the model.","",@@ -193,7 +193,7 @@ export function buildRlmPrompt(options: RlmPromptOptions): string {if (installedSkills.includes("refine")) {parts.push("",- "Treat continual harness refinement as a small, evidence-backed update after observing a repeated failure or reusable tactic: diagnose the issue, update the smallest relevant continual harness component, then validate on the next action. Use `await refine.run()` to turn repeated delegation patterns into reusable subagent specs, repeated procedures into skills, durable facts/preferences into memories, and narrow behavioral policies into prompt addendums. It returns immediately and runs when the current turn ends, so continue working normally after calling it. Do not rewrite the whole continual harness when a focused memory, skill, prompt note, or subagent spec is enough.",+ "Treat continual harness refinement as a small, evidence-backed update after observing a repeated failure or reusable tactic: diagnose the issue, update the smallest relevant continual harness component, validate on the next action, then record the outcome. Use `await refine.run()` to turn repeated delegation patterns into reusable subagent specs, repeated procedures into skills, durable facts/preferences into memories, and narrow behavioral policies into prompt addendums. It returns immediately and runs when the current turn ends, so continue working normally after calling it. Do not rewrite the whole continual harness when a focused memory, skill, prompt note, or subagent spec is enough.",);}}diff --git a/packages/coding-agent/src/core/refinement/refinement.ts b/packages/coding-agent/src/core/refinement/refinement.tsindex cc93c05f74..41fcecf6a3 100644--- a/packages/coding-agent/src/core/refinement/refinement.ts+++ b/packages/coding-agent/src/core/refinement/refinement.ts@@ -30,7 +30,7 @@ const DEFAULT_OVERVIEW_CONTENT_LIMIT = 180;* the shell flag was already false, i.e. identical renders), so equality* across the change is render-safe.*/-const HARNESS_DIGEST_FINGERPRINT_VERSION = 2;+const HARNESS_DIGEST_FINGERPRINT_VERSION = 1;export type RefinementKind = "prompt" | "memory" | "skill" | "subagent";export type RefinementAction = "create" | "update" | "delete";@@ -41,7 +41,7 @@ export interface HarnessEntry {kind: RefinementKind;title: string;content: string;- topic: string;+ path: string;scope?: HarnessScope;reference: Record<string, unknown>;arguments: Record<string, unknown>;@@ -73,7 +73,7 @@ export interface RefinementEdit {id?: string;title?: string;content?: string;- topic?: string;+ path?: string;reference?: Record<string, unknown>;arguments?: Record<string, unknown>;metadata?: Record<string, unknown>;@@ -148,7 +148,7 @@ Scope and persistence policy:- A caller may explicitly request global refinement. Global edits must be stable cross-session lessons, durable user preferences, reusable skills/subagents, or tool/environment facts that should affect future sessions.- Entry ids in the harness overview may carry a display-only \`local:\` or \`global:\` prefix. Always use the bare id (no prefix) in edits.- All edits in one refinement apply only to the requested scope's store. During a local refinement, global entries are read-only context: never propose update or delete edits for them; create a local entry instead when a session-specific override is genuinely needed.-- Project/workspace-specific lessons may be persisted globally only when the title, topic, or content explicitly names the project/workspace and the lesson is likely to be reused in future sessions for that project. Prefer local edits when the lesson only belongs in the current conversation.+- Project/workspace-specific lessons may be persisted globally only when the title, path, or content explicitly names the project/workspace and the lesson is likely to be reused in future sessions for that project. Prefer local edits when the lesson only belongs in the current conversation.- Use memory for declarative facts and preferences, skill for repeatable procedures exposed as Python calls, prompt for narrow behavioral policy addendums, and subagent for reusable delegation roles.- Create or update the smallest relevant component: repeated delegation roles should become subagent specs, repeated procedures should become skills, durable facts/preferences should become memories, and narrow behavioral policies should become prompt addendums.- When an edit is persisted, include metadata such as \`{"scope":"local"}\` or \`{"scope":"global"}\` when that helps future review understand the intended blast radius.@@ -169,7 +169,7 @@ JSON only with this exact shape:"id": "stable id for update/delete, optional for create","title": "required for create/update except delete","content": "required for create/update except delete",- "topic": "optional grouping topic",+ "path": "optional grouping path","reference": {"type": "python", "import": "package.module", "callable": "function_name", "call_pattern": "await function_name(...)"},"arguments": {"name": {"type": "string", "required": true, "description": "accepted input"}},"metadata": {},@@ -285,10 +285,10 @@ function objectRecord(value: unknown): Record<string, unknown> | undefined {return value as Record<string, unknown>;}-/** Grouping label of a stored entry, or undefined when it carries none. Older files spell it `path`. */-function harnessTopic(entry: { topic?: unknown; path?: unknown }): string | undefined {- if (typeof entry.topic === "string") return entry.topic;- return typeof entry.path === "string" ? entry.path : undefined;+/** Grouping label of a persisted entry. State written while the grouping was named `topic` carries no `path`. */+function storedHarnessPath(entry: { path?: unknown; topic?: unknown }): string | undefined {+ if (typeof entry.path === "string") return entry.path;+ return typeof entry.topic === "string" ? entry.topic : undefined;}function normalizeHarnessScope(value: unknown, fallback: HarnessScope): HarnessScope {@@ -356,10 +356,13 @@ export function loadHarnessState(for (const [id, rawEntry] of Object.entries(records)) {const entry = objectRecord(rawEntry);if (!entry) continue;- const { path: _path, ...rest } = entry as unknown as HarnessEntry & { path?: unknown };+ // Migrate a topic-spelled grouping to `path` on load; `topic` is dropped so a later save+ // writes the `path` spelling only.+ const { topic: _topic, ...rest } = entry;+ const path = storedHarnessPath(entry);state.entries[kind][id] = {- ...rest,- topic: harnessTopic(entry) ?? "general",+ ...(rest as unknown as HarnessEntry),+ ...(path === undefined ? {} : { path }),scope: normalizeHarnessScope(entry.scope, scope),reference: objectRecord(entry.reference) ?? {},arguments: objectRecord(entry.arguments) ?? {},@@ -393,28 +396,12 @@ export function mergeHarnessStates(globalState: HarnessState, localState?: Harnereturn merged;}-/**- * Pre-topic builds rewriting the shared global store drop unknown keys; mirror the grouping under- * `path` so their rewrite round-trips it. Drop once no pre-topic build can reach a shared store.- */-function serializeHarnessState(state: HarnessState): HarnessState {- const serialized = emptyHarnessState();- serialized.schema = state.schema;- for (const kind of Object.keys(state.entries) as RefinementKind[]) {- for (const [id, entry] of Object.entries(state.entries[kind])) {- serialized.entries[kind][id] = { ...entry, path: entry.topic } as HarnessEntry;- }- }- serialized.refinements = state.refinements;- return serialized;-}-export function saveHarnessState(harnessStateDir: string, state: HarnessState): string {const statePath = getHarnessStatePath(harnessStateDir);mkdirSync(harnessStateDir, { recursive: true });const targetPath = realpathIfPresentSync(statePath);const mode = existsSync(targetPath) ? statSync(targetPath).mode & 0o777 : 0o600;- writeFileAtomicSync(targetPath, `${JSON.stringify(serializeHarnessState(state), null, 2)}\n`, { mode });+ writeFileAtomicSync(targetPath, `${JSON.stringify(state, null, 2)}\n`, { mode });return statePath;}@@ -610,7 +597,7 @@ export function harnessQueryTermIdf(entries: HarnessEntry[], terms: HarnessQuerydocuments += 1;const title = searchableField(entry.title);const content = searchableField(entry.content);- const identifier = `${searchableField(entry.topic)} ${searchableField(entry.id)}`;+ const identifier = `${searchableField(entry.path)} ${searchableField(entry.id)}`;for (const term of terms.keys()) {if (title.includes(term) || content.includes(term) || identifier.includes(term)) {matches.set(term, (matches.get(term) ?? 0) + 1);@@ -636,13 +623,13 @@ export function scoreHarnessEntryForQuery(if (terms.size === 0) return 0;const title = searchableField(entry.title);const content = searchableField(entry.content);- const identifier = `${searchableField(entry.topic)} ${searchableField(entry.id)}`;+ const identifier = `${searchableField(entry.path)} ${searchableField(entry.id)}`;let score = 0;for (const [term, weight] of terms) {// One match per field counts once per term: coverage over distinct- // fields matters more than repetition inside a single field. Topic+ // fields matters more than repetition inside a single field. Path// and id form a single identifier slot: the id is often embedded in- // the topic, so matching both is one signal, not two.+ // the path, so matching both is one signal, not two.let fields = 0;if (title.includes(term)) fields += 1;if (content.includes(term)) fields += 1;@@ -662,10 +649,10 @@ function compareRankedHarnessEntries(): number {const scoreDifference = scoreHarnessEntryForQuery(b, terms, idf) - scoreHarnessEntryForQuery(a, terms, idf);if (scoreDifference !== 0) return scoreDifference;- // Equal scores tie on stable identifier order (topic, title, id), so+ // Equal scores tie on stable identifier order (path, title, id), so// touching unrelated entries never reshuffles equal-score siblings and the// rendered digest keeps a stable prefix for provider prompt-cache reuse.- return [a.topic, a.title, a.id].join("\0").localeCompare([b.topic, b.title, b.id].join("\0"));+ return [a.path, a.title, a.id].join("\0").localeCompare([b.path, b.title, b.id].join("\0"));}export function formatHarnessStateForPrompt(@@ -718,7 +705,7 @@ export function formatHarnessStateForPrompt(const entries = ranked.sort((a, b) =>queryTerms !== undefined && queryTerms.size > 0? compareRankedHarnessEntries(a, b, queryTerms, idf)- : [a.topic, a.title, a.id].join("\0").localeCompare([b.topic, b.title, b.id].join("\0")),+ : [a.path, a.title, a.id].join("\0").localeCompare([b.path, b.title, b.id].join("\0")),);totalEntries += entries.length;// Render subagent specs as a task-shaped roster the model can match against — the@@ -749,7 +736,7 @@ export function formatHarnessStateForPrompt(? ` ref=${compactText(JSON.stringify(entry.reference), maxContentLength)}`: "";lines.push(- `- [${entry.scope ?? "global"}:${entry.id}] ${entry.title} (${entry.topic}, v${entry.version})${referenceText}${argumentsText}: ${compactText(+ `- [${entry.scope ?? "global"}:${entry.id}] ${entry.title} (${entry.path}, v${entry.version})${referenceText}${argumentsText}: ${compactText(entry.content,maxContentLength,)}`,@@ -819,7 +806,7 @@ export function harnessDigestFingerprint(kind: entry.kind,id: entry.id,title: entry.title,- topic: entry.topic,+ path: entry.path,version: entry.version,content: entry.content,// Only skills render the kernel call contract, so another kind can@@ -877,7 +864,7 @@ function overviewForPrompt(state: HarnessState): string {? ` ref=${JSON.stringify(entry.reference).slice(0, 240)}`: "";lines.push(- `- [${entry.scope ?? "global"}:${entry.id}] ${entry.title} (${entry.topic}, v${entry.version})${referenceText}${argumentsText}: ${content}`,+ `- [${entry.scope ?? "global"}:${entry.id}] ${entry.title} (${entry.path}, v${entry.version})${referenceText}${argumentsText}: ${content}`,);}if (entries.length > 40) {@@ -992,7 +979,7 @@ export function normalizeRefinementProposal(value: unknown): RefinementProposalid: typeof edit.id === "string" ? edit.id : undefined,title: typeof edit.title === "string" ? edit.title : undefined,content: typeof edit.content === "string" ? edit.content : undefined,- topic: harnessTopic(edit),+ path: typeof edit.path === "string" ? edit.path : undefined,reference: objectRecord(edit.reference),arguments: objectRecord(edit.arguments),metadata:@@ -1031,8 +1018,8 @@ function validateEdit(edit: RefinementEdit, computedId?: string): string | undefif (edit.id !== undefined && (typeof edit.id !== "string" || edit.id.length === 0)) {return `${edit.action} requires id to be a non-empty string when provided`;}- if (edit.topic !== undefined && (typeof edit.topic !== "string" || edit.topic.length === 0)) {- return `${edit.action} requires topic to be a non-empty string when provided`;+ if (edit.path !== undefined && (typeof edit.path !== "string" || edit.path.length === 0)) {+ return `${edit.action} requires path to be a non-empty string when provided`;}if (edit.action !== "delete" &&@@ -1136,7 +1123,7 @@ export function applyRefinementProposal(kind: edit.kind,title: edit.title ?? before?.title ?? id,content: edit.content ?? before?.content ?? "",- topic: edit.topic ?? before?.topic ?? "general",+ path: edit.path ?? before?.path ?? "general",scope: before?.scope ?? options.scope ?? "local",reference: edit.reference ?? before?.reference ?? {},arguments: edit.arguments ?? before?.arguments ?? {},@@ -1184,7 +1171,8 @@ function rollbackProposal(target: RefinementResult): RefinementProposal {id: edit.id,title: edit.before.title,content: edit.before.content,- topic: harnessTopic(edit.before),+ // A snapshot recorded while the grouping was named `topic` has no `path` to restore.+ path: storedHarnessPath(edit.before),reference: edit.before.reference,arguments: edit.before.arguments,metadata: edit.before.metadata,diff --git a/packages/coding-agent/src/modes/interactive/components/refinement-outcome-message.ts b/packages/coding-agent/src/modes/interactive/components/refinement-outcome-message.tsindex a2dae0f48d..856ff97bdf 100644--- a/packages/coding-agent/src/modes/interactive/components/refinement-outcome-message.ts+++ b/packages/coding-agent/src/modes/interactive/components/refinement-outcome-message.ts@@ -12,7 +12,7 @@ type EditFieldKey = (typeof EDIT_FIELDS)[number]["key"];const EDIT_FIELDS = [{ key: "title", label: "Title" },{ key: "content", label: "Description" },- { key: "topic", label: "Topic" },+ { key: "path", label: "Path" },{ key: "reference", label: "Reference" },{ key: "arguments", label: "Arguments" },{ key: "metadata", label: "Metadata" },diff --git a/packages/coding-agent/test/acp-kernel-features.test.ts b/packages/coding-agent/test/acp-kernel-features.test.tsindex ecf6d210bb..b7bd99a08d 100644--- a/packages/coding-agent/test/acp-kernel-features.test.ts+++ b/packages/coding-agent/test/acp-kernel-features.test.ts@@ -96,12 +96,11 @@ describe("ACP mode over a real Python kernel", () => {const allKinds = await manager.execute(`import jsonmem = rlm.harness.create_memory(title="m", content="memory content", global_=True)-note = rlm.harness.create_memory(title="n", content="prompt note content", kind="prompt", global_=True)-spec = rlm.harness.create_memory(title="s", content="subagent spec content", kind="subagent", global_=True)-skill = rlm.harness.create_memory(+note = rlm.harness.create_prompt_note(title="n", content="prompt note content", global_=True)+spec = rlm.harness.create_subagent(title="s", content="subagent spec content", global_=True)+skill = rlm.harness.create_skill(title="k",content="skill content",- kind="skill",reference={"type": "python", "import": "pkg.mod", "callable": "run", "call_pattern": "await run(...)"},arguments={"x": {"type": "string", "required": True, "description": "input"}},global_=True,@@ -128,7 +127,7 @@ entry = rlm.harness.create_memory()found = rlm.harness.get("memory", entry.id, global_=True)listed = [item.id for item in rlm.harness.list("memory", global_=True)]-deleted = rlm.harness.delete_memory(entry.id, kind="memory", global_=True)+deleted = rlm.harness.delete("memory", entry.id, global_=True)after = rlm.harness.get("memory", entry.id, global_=True)print(json.dumps({"created": entry.id,diff --git a/packages/coding-agent/test/refinement.test.ts b/packages/coding-agent/test/refinement.test.tsindex 0dc6bf0ad3..f9276b7f59 100644--- a/packages/coding-agent/test/refinement.test.ts+++ b/packages/coding-agent/test/refinement.test.ts@@ -139,7 +139,7 @@ function seedEntry(state: HarnessState, kind: RefinementKind, id = `${kind}_entrid,title: `${kind} title`,content: `${kind} content`,- topic: `${kind}/topic`,+ path: `${kind}/path`,...(kind === "skill" ? skillContract : {}),metadata: { seeded: true },},@@ -226,7 +226,7 @@ describe("harness refinement", () => {id,title: `${kind} title`,content: `${kind} content`,- topic: `${kind}/created`,+ path: `${kind}/created`,...skillFields,metadata: { kind },},@@ -241,7 +241,7 @@ describe("harness refinement", () => {kind,title: `${kind} title`,content: `${kind} content`,- topic: `${kind}/created`,+ path: `${kind}/created`,metadata: { kind },source: "refine",version: 1,@@ -256,7 +256,7 @@ describe("harness refinement", () => {id,title: `${kind} title updated`,content: `${kind} content updated`,- topic: `${kind}/updated`,+ path: `${kind}/updated`,...skillFields,metadata: { updated: kind },},@@ -268,7 +268,7 @@ describe("harness refinement", () => {expect(state.entries[kind][id]).toMatchObject({title: `${kind} title updated`,content: `${kind} content updated`,- topic: `${kind}/updated`,+ path: `${kind}/updated`,metadata: { updated: kind },version: 2,});@@ -281,7 +281,7 @@ describe("harness refinement", () => {expect(state.refinements.at(-1)?.changes).toEqual([`delete ${kind}:${id}`]);});- it("creates ids from titles and uses default topic and metadata when omitted", () => {+ it("creates ids from titles and uses default path and metadata when omitted", () => {const state = loadHarnessState(makeTempDir());const result = applyRefinementProposal(@@ -295,7 +295,7 @@ describe("harness refinement", () => {expect(result.appliedEdits[0]).toMatchObject({applied: true,id: "native_check",- after: { id: "native_check", topic: "general", reference: skillReference, metadata: {}, version: 1 },+ after: { id: "native_check", path: "general", reference: skillReference, metadata: {}, version: 1 },});});@@ -440,9 +440,9 @@ describe("harness refinement", () => {error: "create requires reference to be an object when provided",},{- label: "a create with a numeric topic",- edit: editWith("create", "memory", "bad_topic", { topic: 7 }),- error: "create requires topic to be a non-empty string when provided",+ label: "a create with a numeric path",+ edit: editWith("create", "memory", "bad_path", { path: 7 }),+ error: "create requires path to be a non-empty string when provided",},{label: "a skill with list arguments",@@ -558,23 +558,6 @@ describe("harness refinement", () => {},);- it("loads an entry that stores the grouping as path and resaves it as topic", () => {- const dir = makeTempDir();- writeFileSync(- getHarnessStatePath(dir),- '{"schema":1,"entries":{"memory":{"legacy":{"id":"legacy","kind":"memory","title":"Legacy","content":"c","path":"repo/testing"}}}}',- "utf8",- );-- const state = loadHarnessState(dir, "local");- expect(state.entries.memory.legacy.topic).toBe("repo/testing");-- saveHarnessState(dir, state);- const saved = JSON.parse(readFileSync(getHarnessStatePath(dir), "utf8")).entries.memory.legacy;- expect(saved.topic).toBe("repo/testing");- expect(saved.path).toBe("repo/testing");- });-it("extracts well-formed refinement history from custom session entries", () => {const result: RefinementResult = {id: "refine_1",@@ -610,7 +593,6 @@ describe("harness refinement", () => {id: "native_validation",title: "Native validation",content: "Run validation through the target project environment.",- path: "validation",};completeSimpleMock.mockResolvedValueOnce(assistantText(JSON.stringify({ summary: "s", rationale: "r", expectedOutcome: "o", edits: [edit] })),@@ -636,7 +618,7 @@ describe("harness refinement", () => {headers: { "x-test-header": "1" },});expect(result.appliedEdits[0]).toMatchObject({ kind: "memory", id: "native_validation", applied: true });- expect(state.entries.memory.native_validation).toMatchObject({ content: edit.content, topic: "validation" });+ expect(state.entries.memory.native_validation.content).toBe(edit.content);});it("caps the refinement output budget by the policy ceiling for wide models", async () => {@@ -710,7 +692,7 @@ describe("harness refinement", () => {id: "kept_memory",title: "Updated memory",content: "Updated memory content",- topic: "updated/topic",+ path: "updated/path",metadata: { updated: true },},{ action: "delete", kind: "skill", id: "deleted_skill" },@@ -718,9 +700,7 @@ describe("harness refinement", () => {{ id: "refine_target" },);- // An older build recorded the edit snapshots with the legacy field name.- const recorded = JSON.parse(JSON.stringify(target).replaceAll('"topic":', '"path":')) as RefinementResult;- const rollback = await refineHarness([], state, [recorded], {} as never, "api-key", {+ const rollback = await refineHarness([], state, [target], {} as never, "api-key", {rollbackId: "refine_target",});@@ -734,7 +714,7 @@ describe("harness refinement", () => {expect(state.entries.prompt.created_prompt).toBeUndefined();expect(state.entries.memory.kept_memory).toMatchObject({content: "memory content",- topic: "memory/topic",+ path: "memory/path",metadata: { seeded: true },version: 3,});@@ -756,6 +736,80 @@ describe("harness refinement", () => {});});+describe("topic-era harness state migration", () => {+ it("loads a persisted entry whose grouping is spelled topic and saves it back under path", () => {+ const harnessStateDir = makeTempDir();+ writeFileSync(+ getHarnessStatePath(harnessStateDir),+ `${JSON.stringify({+ schema: 1,+ entries: {+ prompt: {},+ memory: {+ topic_entry: {+ id: "topic_entry",+ kind: "memory",+ title: "Topic entry",+ content: "Window-era content.",+ topic: "window/era",+ scope: "local",+ reference: {},+ arguments: {},+ metadata: {},+ version: 1,+ },+ },+ skill: {},+ subagent: {},+ },+ refinements: [],+ })}\n`,+ );++ const state = loadHarnessState(harnessStateDir, "local");++ expect(state.entries.memory.topic_entry?.path).toBe("window/era");+ expect(formatHarnessStateForPrompt(state)).toContain("(window/era, v1)");+ const persisted = readFileSync(saveHarnessState(harnessStateDir, state), "utf8");+ expect(persisted).toContain('"path": "window/era"');+ expect(persisted).not.toContain('"topic"');+ });++ it("rolls back a legacy snapshot whose recorded before state carries only topic", async () => {+ const state = loadHarnessState(makeTempDir());+ seedEntry(state, "memory", "grouped_memory");+ const target = applyRefinementProposal(+ state,+ proposal("Target refinement", [+ {+ action: "update",+ kind: "memory",+ id: "grouped_memory",+ title: "Updated memory",+ content: "Updated memory content",+ path: "updated/path",+ },+ ]),+ { id: "refine_topic_snapshot" },+ );+ // Record the pre-edit snapshot the way a build that spelled the grouping `topic` wrote it.+ for (const applied of target.appliedEdits) {+ if (!applied.before) continue;+ const before: Record<string, unknown> = { ...applied.before };+ before.topic = before.path;+ delete before.path;+ applied.before = before as unknown as HarnessEntry;+ }++ const rollback = await refineHarness([], state, [target], {} as never, "api-key", {+ rollbackId: "refine_topic_snapshot",+ });++ expect(rollback.appliedEdits.map((applied) => applied.applied)).toEqual([true]);+ expect(state.entries.memory.grouped_memory).toMatchObject({ content: "memory content", path: "memory/path" });+ });+});+describe("global refinement history", () => {function sampleResult(id: string, overrides: Partial<RefinementResult> = {}): RefinementResult {return {@@ -802,7 +856,7 @@ describe("global refinement history", () => {kind: "memory" as const,title: "Legacy global memory",content: "created globally",- topic: "general",+ path: "general",scope: "global" as const,reference: {},arguments: {},@@ -949,7 +1003,7 @@ describe("harness digest relevance ranking", () => {kind: "memory",title,content,- topic: "memory",+ path: "memory",scope: "global",reference: {},arguments: {},@@ -1069,7 +1123,7 @@ describe("harness digest relevance ranking", () => {maxEntriesPerKind: 1,queryTerms: new Map([["worktree", 1]]),});- // Equal scores render in stable identifier order ([topic, title, id]);+ // Equal scores render in stable identifier order ([path, title, id]);// updated_at recency must not hoist the newer entry into the window.expect(ranked).toContain("[global:aaa]");expect(ranked).not.toContain("[global:zzz]");diff --git a/packages/coding-agent/test/suite/agent-session-compaction.test.ts b/packages/coding-agent/test/suite/agent-session-compaction.test.tsindex 760a640704..c2f5073b15 100644--- a/packages/coding-agent/test/suite/agent-session-compaction.test.ts+++ b/packages/coding-agent/test/suite/agent-session-compaction.test.ts@@ -367,7 +367,7 @@ describe("AgentSession compaction", () => {kind: "memory",title: "Compaction test memory",content: "Written before compaction.",- topic: "general",+ path: "general",scope: "local",reference: {},arguments: {},diff --git a/packages/coding-agent/test/suite/agent-session-prompt.test.ts b/packages/coding-agent/test/suite/agent-session-prompt.test.tsindex ab6dc26d65..156e023e9f 100644--- a/packages/coding-agent/test/suite/agent-session-prompt.test.ts+++ b/packages/coding-agent/test/suite/agent-session-prompt.test.ts@@ -994,7 +994,7 @@ describe("Harness digest at cold boundaries", () => {kind: "memory",title,content,- topic: "general",+ path: "general",scope,reference: {},arguments: {},diff --git a/packages/coding-agent/test/suite/agent-session-serialized-refine.test.ts b/packages/coding-agent/test/suite/agent-session-serialized-refine.test.tsindex 68d7ec9c8f..0f29c4f6fb 100644--- a/packages/coding-agent/test/suite/agent-session-serialized-refine.test.ts+++ b/packages/coding-agent/test/suite/agent-session-serialized-refine.test.ts@@ -278,7 +278,7 @@ describe("Serialized refine", () => {kind: "memory",title: "Shared",content: "planning baseline",- topic: "general",+ path: "general",scope: "local",reference: {},arguments: {},diff --git a/prime-agent-runtime/src/rlm/harness.py b/prime-agent-runtime/src/rlm/harness.pyindex 04233a04d1..a10b329ee5 100644--- a/prime-agent-runtime/src/rlm/harness.py+++ b/prime-agent-runtime/src/rlm/harness.py@@ -1,10 +1,10 @@"""Persistent harness-state helpers for Prime Agent's RLM kernel.The state model is intentionally small: it records prompt notes, memory,-skills, subagent specs, and engine-recorded refinement events in the-session-local harness store by default; pass ``global_=True`` for the-cross-session global store. Execution still belongs to Prime Agent's-TypeScript host and the existing ``rlm.spawn`` recursion bridge.+skills, subagent specs, and refinement events in the session-local harness+store by default; pass ``global_=True`` for the cross-session global store.+Execution still belongs to Prime Agent's TypeScript host and the existing+``rlm.spawn`` recursion bridge."""from __future__ import annotations@@ -27,17 +27,6 @@ HarnessScope = Literal["local", "global"]_DEFAULT_FILE_NAME = "harness_state.json"_DEFAULT_HARNESS_DIR_NAME = "harness"_KINDS: tuple[HarnessKind, ...] = ("prompt", "memory", "skill", "subagent")-_REMOVED_METHOD_GUIDANCE = {- "record_refinement": (- "record_refinement was removed; refinement events are recorded automatically when refinements run"- ),- "plan_refinement": "plan_refinement was removed; use await refine.run() to schedule a refinement",-}-_REMOVED_WRAPPER_KINDS: dict[str, HarnessKind] = {- "prompt_note": "prompt",- "skill": "skill",- "subagent": "subagent",-}_state_cache: dict[tuple[Path, HarnessScope], "HarnessState"] = {}@@ -132,8 +121,6 @@ def _agent_dir() -> Path:def _resolve_global_flag(global_: bool = False, extra: dict[str, Any] | None = None) -> bool:extra = dict(extra or {})- if "path" in extra:- raise TypeError("path was renamed to topic; pass topic=")if "global" in extra:value = extra.pop("global")if not isinstance(value, bool):@@ -187,7 +174,7 @@ class HarnessEntry:kind: HarnessKindtitle: strcontent: str- topic: str = "general"+ path: str = "general"scope: HarnessScope = "local"reference: dict[str, Any] = field(default_factory=dict)arguments: dict[str, Any] = field(default_factory=dict)@@ -200,7 +187,7 @@ class HarnessEntry:@dataclassclass RefinementEvent:- """A refinement pass recorded by the refinement engine; ``changes`` is empty when no edit applied."""+ """A recorded online harness-refinement pass."""id: strtrigger: str@@ -234,30 +221,6 @@ def _validate_python_skill_reference(reference: dict[str, Any] | None, entry_namreturn normalized-def _serialize_entry(entry: HarnessEntry) -> dict[str, Any]:- # Pre-topic builds rewriting the shared store drop "topic"; mirror the value under "path"- # so their rewrite round-trips it. Drop once no pre-topic build can reach a shared store.- record = asdict(entry)- record["path"] = entry.topic- return record---def _default_topic(kind: HarnessKind) -> str:- return "policy" if kind == "prompt" else "general"---def _reject_skill_only_fields(- kind: HarnessKind,- reference: dict[str, Any] | None,- arguments: dict[str, Any] | None,-) -> None:- """Reject the skill-only call contract fields on the other kinds."""- if kind != "skill":- for name, value in (("reference", reference), ("arguments", arguments)):- if value is not None:- raise ValueError(f"{name} is only accepted for kind='skill', not kind={kind!r}")--def _type_name(value: Any) -> str:if isinstance(value, list):return "a list"@@ -300,7 +263,7 @@ def _validate_entry_shape(title: Any,content: Any,*,- topic: Any,+ path: Any,reference: Any,arguments: Any,metadata: Any,@@ -318,7 +281,7 @@ def _validate_entry_shape(_require_text(kind, entry_name, "id", entry_id)_require_text(kind, entry_name, "title", title)_require_text(kind, entry_name, "content", content)- _require_optional_text(kind, entry_name, "topic", topic)+ _require_optional_text(kind, entry_name, "path", path)_require_optional_record(kind, entry_name, "reference", reference)_require_optional_record(kind, entry_name, "arguments", arguments)_require_optional_record(kind, entry_name, "metadata", metadata)@@ -333,6 +296,26 @@ def _validate_entry_shape(_validate_python_skill_reference(reference, entry_name)+def _validate_refinement_event(trigger: Any, changes: Any, *, evidence: Any, outcome: Any) -> None:+ """Reject a refinement event whose persisted shape would break the digest."""+ if not isinstance(trigger, str) or not trigger:+ raise ValueError(f"refinement event rejected: trigger must be a non-empty string, got {_type_name(trigger)}")+ if isinstance(changes, str):+ if not changes:+ raise ValueError("refinement event rejected: changes must be a non-empty string or a list of strings")+ elif isinstance(changes, list):+ if not all(isinstance(change, str) and change for change in changes):+ raise ValueError("refinement event rejected: changes must be a list of non-empty strings")+ else:+ raise ValueError(+ f"refinement event rejected: changes must be a string or a list of strings, got {_type_name(changes)}"+ )+ if not isinstance(evidence, str):+ raise ValueError(f"refinement event rejected: evidence must be a string when provided, got {_type_name(evidence)}")+ if not isinstance(outcome, str):+ raise ValueError(f"refinement event rejected: outcome must be a string when provided, got {_type_name(outcome)}")++class HarnessState:"""CRUD store for reset-free harness refinement state."""@@ -366,19 +349,6 @@ class HarnessState:self._loaded_mtime: int | None = Noneself.load()- def _require_kind(self, kind: HarnessKind) -> None:- if kind not in self.entries:- raise ValueError(f"unknown harness kind {kind!r}; expected one of {_KINDS}")-- def _kind_of(self, id: str) -> HarnessKind | None:- """The kind an id is stored under, if any."""- return next((kind for kind in _KINDS if id in self.entries[kind]), None)-- def _missing_entry_message(self, kind: HarnessKind, id: str) -> str:- if (actual := self._kind_of(id)) is not None:- return f"entry {id!r} is kind={actual!r}; pass kind={actual!r}"- return f"{kind} entry {id!r} does not exist"-def _ensure_local_writable(self) -> None:if self._local_write_error is not None:raise RuntimeError(self._local_write_error)@@ -436,10 +406,13 @@ class HarnessState:entry_data.get("content"), str):continue- if not isinstance(entry_data.get("topic"), str):- # Older state files spell the grouping "path"; read both.- stored_path = raw_entry.get("path")- entry_data["topic"] = stored_path if isinstance(stored_path, str) else "general"+ # State written while the grouping was named "topic" carries no "path";+ # migrate it on load so the grouping survives. _ENTRY_FIELDS drops "topic",+ # so a later save writes the "path" spelling only.+ if not isinstance(entry_data.get("path"), str) and isinstance(raw_entry.get("topic"), str):+ entry_data["path"] = raw_entry["topic"]+ if not isinstance(entry_data.get("path"), str):+ entry_data["path"] = "general"if entry_data.get("scope") not in ("local", "global"):entry_data["scope"] = self.scopeif not isinstance(entry_data.get("source"), str):@@ -499,7 +472,7 @@ class HarnessState:data = {"schema": 1,"entries": {- kind: {entry_id: _serialize_entry(entry) for entry_id, entry in records.items()}+ kind: {entry_id: asdict(entry) for entry_id, entry in records.items()}for kind, records in self.entries.items()},"refinements": [asdict(event) for event in self.refinements],@@ -532,10 +505,11 @@ class HarnessState:content: str,*,id: str | None = None,- topic: str = "general",+ path: str = "general",reference: dict[str, Any] | None = None,arguments: dict[str, Any] | None = None,metadata: dict[str, Any] | None = None,+ source: str = "agent",global_: bool = False,**kwargs: Any,) -> HarnessEntry:@@ -546,10 +520,11 @@ class HarnessState:title,content,id=id,- topic=topic,+ path=path,reference=reference,arguments=arguments,metadata=metadata,+ source=source,)self._ensure_local_writable()self._sync_from_disk()@@ -558,10 +533,11 @@ class HarnessState:title,content,id=id,- topic=topic,+ path=path,reference=reference,arguments=arguments,metadata=metadata,+ source=source,)def _upsert(@@ -571,17 +547,18 @@ class HarnessState:content: str,*,id: str | None = None,- topic: str | None = None,+ path: str | None = None,reference: dict[str, Any] | None = None,arguments: dict[str, Any] | None = None,metadata: dict[str, Any] | None = None,source: str = "agent",) -> HarnessEntry:- # Caller is responsible for syncing from disk first. create_memory()/update_memory()- # sync once and then call this directly so their existence check and the write are+ # Caller is responsible for syncing from disk first. create()/update() sync+ # once and then call this directly so their existence check and the write are# not separated by a second reload (which could turn create-or-fail into a# silent update).- self._require_kind(kind)+ if kind not in self.entries:+ raise ValueError(f"unknown harness kind {kind!r}; expected one of {_KINDS}")# Guard before the id slug and the dict lookup: a non-string title or a# non-string id (falsy ids included, which the slug fallback would@@ -597,7 +574,7 @@ class HarnessState:entry_id,title,content,- topic=topic,+ path=path,reference=reference,arguments=arguments,metadata=metadata,@@ -607,12 +584,12 @@ class HarnessState:if existing:existing.title = titleexisting.content = content- # Preserve topic/reference/arguments/metadata when the caller omits them+ # Preserve path/reference/arguments/metadata when the caller omits them# (None) so updating only an entry's title or content does not reset its- # grouping topic or wipe a skill's reference/argument contract. An explicit+ # grouping path or wipe a skill's reference/argument contract. An explicit# value (including {}) still overwrites.- if topic is not None:- existing.topic = topic+ if path is not None:+ existing.path = pathif reference is not None:existing.reference = dict(reference)if arguments is not None:@@ -629,7 +606,7 @@ class HarnessState:kind=kind,title=title,content=content,- topic=topic if topic is not None else "general",+ path=path if path is not None else "general",scope=self.scope,reference=dict(reference or {}),arguments=dict(arguments or {}),@@ -645,9 +622,24 @@ class HarnessState:if target := self._global_target(global_, kwargs):return target.get(kind, id)self._sync_from_disk()- self._require_kind(kind)+ if kind not in self.entries:+ raise ValueError(f"unknown harness kind {kind!r}; expected one of {_KINDS}")return self.entries[kind].get(id)+ def delete(self, kind: HarnessKind, id: str, *, global_: bool = False, **kwargs: Any) -> bool:+ id, global_ = _strip_scope_prefix(id, global_)+ if target := self._global_target(global_, kwargs):+ return target.delete(kind, id)+ self._ensure_local_writable()+ self._sync_from_disk()+ if kind not in self.entries:+ raise ValueError(f"unknown harness kind {kind!r}; expected one of {_KINDS}")+ if id not in self.entries[kind]:+ return False+ del self.entries[kind][id]+ self.save()+ return True+def list(self, kind: HarnessKind | None = None, *, global_: bool = False, **kwargs: Any) -> list[HarnessEntry]:if target := self._global_target(global_, kwargs):return target.list(kind)@@ -655,48 +647,43 @@ class HarnessState:kinds = [kind] if kind else list(_KINDS)records: list[HarnessEntry] = []for current_kind in kinds:- self._require_kind(current_kind)+ if current_kind not in self.entries:+ raise ValueError(f"unknown harness kind {current_kind!r}; expected one of {_KINDS}")records.extend(self.entries[current_kind].values())- return sorted(records, key=lambda entry: (entry.kind, entry.topic, entry.title, entry.id))+ return sorted(records, key=lambda entry: (entry.kind, entry.path, entry.title, entry.id))- def create_memory(+ def create(self,+ kind: HarnessKind,title: str,content: str,*,- kind: HarnessKind = "memory",- topic: str | None = None,id: str | None = None,- metadata: dict[str, Any] | None = None,+ path: str = "general",reference: dict[str, Any] | None = None,arguments: dict[str, Any] | None = None,+ metadata: dict[str, Any] | None = None,+ source: str = "agent",global_: bool = False,**kwargs: Any,) -> HarnessEntry:- """Create a harness entry; fails when the id already exists.-- Prompt notes, skills, and subagent specs are subtypes of memory, so one- method with ``kind`` covers them all. ``reference`` and ``arguments``- describe a skill's Python call contract and are rejected for other kinds.- ``topic`` groups entries and defaults to "policy" for prompt notes and- "general" otherwise.- """- self._require_kind(kind)- _reject_skill_only_fields(kind, reference, arguments)id, global_ = _strip_scope_prefix(id, global_)if target := self._global_target(global_, kwargs):- return target.create_memory(+ return target.create(+ kind,title,content,- kind=kind,- topic=topic,id=id,- metadata=metadata,+ path=path,reference=reference,arguments=arguments,+ metadata=metadata,+ source=source,)self._ensure_local_writable()self._sync_from_disk()+ if kind not in self.entries:+ raise ValueError(f"unknown harness kind {kind!r}; expected one of {_KINDS}")_require_text(kind, _describe_entry(id, title), "title", title)if id is not None:_require_text(kind, _describe_entry(id, title), "id", id)@@ -708,83 +695,257 @@ class HarnessState:title,content,id=entry_id,- topic=topic if topic is not None else _default_topic(kind),+ path=path,reference=reference,arguments=arguments,metadata=metadata,+ source=source,)- def update_memory(+ def update(self,+ kind: HarnessKind,id: str,title: str,content: str,*,- kind: HarnessKind = "memory",- topic: str | None = None,- metadata: dict[str, Any] | None = None,+ path: str | None = None,reference: dict[str, Any] | None = None,arguments: dict[str, Any] | None = None,+ metadata: dict[str, Any] | None = None,+ source: str = "agent",global_: bool = False,**kwargs: Any,) -> HarnessEntry:- """Update an existing harness entry; omitted fields keep their value."""- self._require_kind(kind)- _reject_skill_only_fields(kind, reference, arguments)id, global_ = _strip_scope_prefix(id, global_)if target := self._global_target(global_, kwargs):- return target.update_memory(+ return target.update(+ kind,id,title,content,- kind=kind,- topic=topic,- metadata=metadata,+ path=path,reference=reference,arguments=arguments,+ metadata=metadata,+ source=source,)self._ensure_local_writable()self._sync_from_disk()+ if kind not in self.entries:+ raise ValueError(f"unknown harness kind {kind!r}; expected one of {_KINDS}")_require_text(kind, _describe_entry(id, title), "id", id)if id not in self.entries[kind]:- raise ValueError(self._missing_entry_message(kind, id))+ raise ValueError(f"{kind} entry {id!r} does not exist")return self._upsert(kind,title,content,id=id,- topic=topic,+ path=path,reference=reference,arguments=arguments,metadata=metadata,+ source=source,)- def delete_memory(self, id: str, *, kind: HarnessKind = "memory", global_: bool = False, **kwargs: Any) -> bool:- """Delete a harness entry; returns False when it does not exist."""- self._require_kind(kind)- id, global_ = _strip_scope_prefix(id, global_)+ def create_memory(+ self,+ title: str,+ content: str,+ *,+ id: str | None = None,+ path: str = "general",+ metadata: dict[str, Any] | None = None,+ global_: bool = False,+ **kwargs: Any,+ ) -> HarnessEntry:+ return self.create("memory", title, content, id=id, path=path, metadata=metadata, global_=global_, **kwargs)++ def update_memory(+ self,+ id: str,+ title: str,+ content: str,+ *,+ path: str | None = None,+ metadata: dict[str, Any] | None = None,+ global_: bool = False,+ **kwargs: Any,+ ) -> HarnessEntry:+ return self.update("memory", id, title, content, path=path, metadata=metadata, global_=global_, **kwargs)++ def delete_memory(self, id: str, *, global_: bool = False, **kwargs: Any) -> bool:+ return self.delete("memory", id, global_=global_, **kwargs)++ def create_prompt_note(+ self,+ title: str,+ content: str,+ *,+ id: str | None = None,+ path: str = "policy",+ metadata: dict[str, Any] | None = None,+ global_: bool = False,+ **kwargs: Any,+ ) -> HarnessEntry:+ return self.create("prompt", title, content, id=id, path=path, metadata=metadata, global_=global_, **kwargs)++ def update_prompt_note(+ self,+ id: str,+ title: str,+ content: str,+ *,+ path: str | None = None,+ metadata: dict[str, Any] | None = None,+ global_: bool = False,+ **kwargs: Any,+ ) -> HarnessEntry:+ return self.update("prompt", id, title, content, path=path, metadata=metadata, global_=global_, **kwargs)++ def delete_prompt_note(self, id: str, *, global_: bool = False, **kwargs: Any) -> bool:+ return self.delete("prompt", id, global_=global_, **kwargs)++ def create_skill(+ self,+ title: str,+ content: str,+ *,+ id: str | None = None,+ path: str = "general",+ reference: dict[str, Any] | None = None,+ arguments: dict[str, Any] | None = None,+ metadata: dict[str, Any] | None = None,+ global_: bool = False,+ **kwargs: Any,+ ) -> HarnessEntry:+ return self.create(+ "skill",+ title,+ content,+ id=id,+ path=path,+ reference=_validate_python_skill_reference(reference, _describe_entry(id, title)),+ arguments=arguments,+ metadata=metadata,+ global_=global_,+ **kwargs,+ )++ def update_skill(+ self,+ id: str,+ title: str,+ content: str,+ *,+ path: str | None = None,+ reference: dict[str, Any] | None = None,+ arguments: dict[str, Any] | None = None,+ metadata: dict[str, Any] | None = None,+ global_: bool = False,+ **kwargs: Any,+ ) -> HarnessEntry:+ # Only validate a reference when one is supplied; omitting it preserves the+ # existing reference (see _upsert) rather than forcing every title/content-only+ # update to re-send the full Python reference.+ validated_reference = (+ _validate_python_skill_reference(reference, _describe_entry(id, title)) if reference is not None else None+ )+ return self.update(+ "skill",+ id,+ title,+ content,+ path=path,+ reference=validated_reference,+ arguments=arguments,+ metadata=metadata,+ global_=global_,+ **kwargs,+ )++ def delete_skill(self, id: str, *, global_: bool = False, **kwargs: Any) -> bool:+ return self.delete("skill", id, global_=global_, **kwargs)++ def create_subagent(+ self,+ title: str,+ content: str,+ *,+ id: str | None = None,+ path: str = "general",+ metadata: dict[str, Any] | None = None,+ global_: bool = False,+ **kwargs: Any,+ ) -> HarnessEntry:+ return self.create("subagent", title, content, id=id, path=path, metadata=metadata, global_=global_, **kwargs)++ def update_subagent(+ self,+ id: str,+ title: str,+ content: str,+ *,+ path: str | None = None,+ metadata: dict[str, Any] | None = None,+ global_: bool = False,+ **kwargs: Any,+ ) -> HarnessEntry:+ return self.update("subagent", id, title, content, path=path, metadata=metadata, global_=global_, **kwargs)++ def delete_subagent(self, id: str, *, global_: bool = False, **kwargs: Any) -> bool:+ return self.delete("subagent", id, global_=global_, **kwargs)++ def record_refinement(+ self,+ trigger: str,+ changes: list[str] | str,+ *,+ evidence: str = "",+ outcome: str = "",+ id: str | None = None,+ global_: bool = False,+ **kwargs: Any,+ ) -> RefinementEvent:if target := self._global_target(global_, kwargs):- return target.delete_memory(id, kind=kind)+ return target.record_refinement(trigger, changes, evidence=evidence, outcome=outcome, id=id)self._ensure_local_writable()self._sync_from_disk()- if id not in self.entries[kind]:- if self._kind_of(id) is not None:- raise ValueError(self._missing_entry_message(kind, id))- return False- del self.entries[kind][id]+ _validate_refinement_event(trigger, changes, evidence=evidence, outcome=outcome)+ if id is not None and (not isinstance(id, str) or not id):+ raise ValueError(+ f"refinement event rejected: id must be a non-empty string when provided, got {_type_name(id)}"+ )+ event_id = id or f"refine_{len(self.refinements) + 1:04d}"+ normalized_changes = [changes] if isinstance(changes, str) else list(changes)+ event = RefinementEvent(+ id=event_id,+ trigger=trigger,+ changes=normalized_changes,+ evidence=evidence,+ outcome=outcome,+ )+ self.refinements.append(event)self.save()- return True+ return event- def __getattr__(self, name: str) -> Any:- # Kernels and transcripts still carry the removed names, so name the- # replacement instead of a bare AttributeError.- if guidance := _REMOVED_METHOD_GUIDANCE.get(name):- raise AttributeError(guidance)- action, _, suffix = name.partition("_")- kind = _REMOVED_WRAPPER_KINDS.get(suffix)- if kind and action in ("create", "update", "delete"):- raise AttributeError(f"{name} was removed; use rlm.harness.{action}_memory(..., kind={kind!r})")- raise AttributeError(name)+ def plan_refinement(+ self,+ observation: str,+ *,+ failing_component: str = "",+ next_step: str = "",+ ) -> list[str]:+ target = f" for {failing_component}" if failing_component else ""+ plan = [+ f"Diagnose the repeated failure or opportunity{target}: {observation}",+ "Update the smallest useful prompt note, memory item, skill, or subagent spec.",+ "Run the next action with the changed harness state, then record the outcome.",+ ]+ if next_step:+ plan.append(f"Immediate validation step: {next_step}")+ return plandef overview(self, *, max_entries_per_kind: int = 20, global_: bool = False, **kwargs: Any) -> str:if target := self._global_target(global_, kwargs):@@ -821,7 +982,7 @@ class HarnessState:reference_text = f"{reference_text[:117]}..."reference_summary = f" ref={reference_text}"lines.append(- f" - [{entry.scope}:{entry.id}] {entry.title} ({entry.topic}, v{entry.version})"+ f" - [{entry.scope}:{entry.id}] {entry.title} ({entry.path}, v{entry.version})"f"{reference_summary}{argument_summary}: {summary}")overflow = len(self.entries[kind]) - len(records)@@ -846,7 +1007,7 @@ class HarnessState:) -> list[HarnessEntry]:"""Return harness entries ranked by weighted term overlap with *query*.- Terms are scored against an entry's title, content, topic, and id;+ Terms are scored against an entry's title, content, path, and id;matches in more distinct fields count more. Each matched term isdiscounted by its document frequency across the ranked corpus(tf-idf style, ``weight * log(1 + N / df)``), so a rare,@@ -872,9 +1033,9 @@ class HarnessState:for entry in entries:title = entry.title.lower()content = entry.content.lower()- topic_and_id = f"{entry.topic} {entry.id}".lower()+ path_and_id = f"{entry.path} {entry.id}".lower()for term in terms:- if term in title or term in content or term in topic_and_id:+ if term in title or term in content or term in path_and_id:matches[term] += 1term_idf = {term: math.log(1 + len(entries) / count)@@ -885,11 +1046,11 @@ class HarnessState:def score(entry: HarnessEntry) -> float:title = entry.title.lower()content = entry.content.lower()- topic_and_id = f"{entry.topic} {entry.id}".lower()+ path_and_id = f"{entry.path} {entry.id}".lower()total = 0.0for term, idf in term_idf.items():fields = (1 if term in title else 0) + (1 if term in content else 0) + (- 1 if term in topic_and_id else 0+ 1 if term in path_and_id else 0)if fields:total += idf * (1 + (fields - 1) * 0.5)diff --git a/prime-agent-runtime/test/test_harness.py b/prime-agent-runtime/test/test_harness.pyindex 44353e4aeb..20ae7ceb15 100644--- a/prime-agent-runtime/test/test_harness.py+++ b/prime-agent-runtime/test/test_harness.py@@ -2,7 +2,6 @@ from __future__ import annotationsimport jsonimport os-import reimport subprocessimport sysimport tempfile@@ -27,38 +26,35 @@ class HarnessStateTest(unittest.TestCase):state = HarnessState(Path(temp_dir) / "harness_state.json")created = {- "prompt": state.create_memory(+ "prompt": state.create_prompt_note("Prompt note","Prompt content",id="prompt_entry",- topic="prompt/topic",+ path="prompt/path",metadata={"kind": "prompt"},- kind="prompt",),"memory": state.create_memory("Memory","Memory content",id="memory_entry",- topic="memory/topic",+ path="memory/path",metadata={"kind": "memory"},),- "skill": state.create_memory(+ "skill": state.create_skill("Skill","Skill content",id="skill_entry",- topic="skill/topic",+ path="skill/path",reference=PYTHON_REFERENCE,arguments={"target": {"type": "string", "required": True}},metadata={"kind": "skill"},- kind="skill",),- "subagent": state.create_memory(+ "subagent": state.create_subagent("Subagent","Subagent content",id="subagent_entry",- topic="subagent/topic",+ path="subagent/path",metadata={"kind": "subagent"},- kind="subagent",),}@@ -67,120 +63,56 @@ class HarnessStateTest(unittest.TestCase):self.assertIn("content", state.get(kind, entry.id).content.lower())self.assertIn(entry, state.list(kind))- state.update_memory("prompt_entry", "Prompt note", "Prompt content updated", kind="prompt")+ state.update_prompt_note("prompt_entry", "Prompt note", "Prompt content updated")state.update_memory("memory_entry", "Memory", "Memory content updated")- state.update_memory(+ state.update_skill("skill_entry","Skill","Skill content updated",reference=PYTHON_REFERENCE,arguments={"target": {"type": "string", "required": True}, "mode": {"type": "string"}},- kind="skill",)- state.update_memory("subagent_entry", "Subagent", "Subagent content updated", kind="subagent")-- for mutate in (- lambda: state.delete_memory("prompt_entry"),- lambda: state.update_memory("prompt_entry", "Prompt note", "x"),- ):- with self.assertRaisesRegex(ValueError, "is kind='prompt'; pass kind='prompt'"):- mutate()- self.assertFalse(state.delete_memory("absent"))+ state.update_subagent("subagent_entry", "Subagent", "Subagent content updated")for kind in ("prompt", "memory", "skill", "subagent"):entry_id = f"{kind}_entry"self.assertEqual(state.get(kind, entry_id).version, 2)self.assertIn("updated", state.get(kind, entry_id).content)- self.assertTrue(state.delete_memory(entry_id, kind=kind))+ delete_method = getattr(state, f"delete_{'prompt_note' if kind == 'prompt' else kind}")+ self.assertTrue(delete_method(entry_id))self.assertIsNone(state.get(kind, entry_id))- self.assertFalse(state.delete_memory(entry_id, kind=kind))+ self.assertFalse(delete_method(entry_id))self.assertEqual(state.list(), [])- def test_create_defaults_and_skill_only_fields(self) -> None:- with tempfile.TemporaryDirectory() as temp_dir:- state = HarnessState(Path(temp_dir) / "harness_state.json")-- default_entry = state.create_memory("Default", "No kind given.")- self.assertEqual(default_entry.kind, "memory")- self.assertEqual(default_entry.topic, "general")- self.assertEqual(state.create_memory("Note", "Policy note.", kind="prompt").topic, "policy")-- for kind in ("memory", "prompt", "subagent"):- with self.assertRaisesRegex(ValueError, "only accepted for kind='skill'"):- state.create_memory("Bad", "content", id=f"bad_{kind}", kind=kind, reference=PYTHON_REFERENCE)- with self.assertRaisesRegex(ValueError, "only accepted for kind='skill'"):- state.update_memory(default_entry.id, "Bad", "content", kind=kind, arguments={})-- with self.assertRaisesRegex(TypeError, "path was renamed to topic"):- state.create_memory("Grouped", "content", **{"path": "repo/testing"})-- def test_removed_harness_methods_name_their_replacement(self) -> None:- with tempfile.TemporaryDirectory() as temp_dir:- state = HarnessState(Path(temp_dir) / "harness_state.json")-- for name, message in (- (- "record_refinement",- "record_refinement was removed; refinement events are recorded automatically when refinements run",- ),- ("plan_refinement", "plan_refinement was removed; use await refine.run() to schedule a refinement"),- ):- with self.assertRaisesRegex(AttributeError, re.escape(message)):- getattr(state, name)-- for name, replacement in (- ("update_skill", "update_memory(..., kind='skill')"),- ("delete_prompt_note", "delete_memory(..., kind='prompt')"),- ("create_subagent", "create_memory(..., kind='subagent')"),- ):- with self.assertRaisesRegex(AttributeError, re.escape(f"{name} was removed; use rlm.harness.{replacement}")):- getattr(state, name)-- def test_engine_recorded_refinements_survive_a_kernel_save(self) -> None:- with tempfile.TemporaryDirectory() as temp_dir:- state_path = Path(temp_dir) / "harness_state.json"- kernel_state = HarnessState(state_path)- kernel_state.create_memory("Kernel note", "Written before the refinement.", id="kernel")- # The refinement engine rewrites the same file from the host process.- engine_write = json.loads(state_path.read_text(encoding="utf-8"))- engine_write["refinements"] = [- {"id": "refine_1", "trigger": "engine pass", "changes": ["create memory:kernel"]}- ]- state_path.write_text(json.dumps(engine_write), encoding="utf-8")- # Guarantee the mtime advances even on coarse-resolution filesystems.- future = state_path.stat().st_mtime + 5- os.utime(state_path, (future, future))- kernel_state.create_memory("Later note", "Written after the refinement.", id="later")- reloaded = HarnessState(state_path)- self.assertEqual([event.id for event in reloaded.refinements], ["refine_1"])- self.assertIsNotNone(reloaded.get("memory", "later"))- self.assertIn("refinements: 1", reloaded.overview())-- def test_persists_entries(self) -> None:+ def test_persists_entries_and_refinements(self) -> None:with tempfile.TemporaryDirectory() as temp_dir:state = HarnessState(Path(temp_dir) / "harness_state.json")memory = state.create_memory("Prefer focused patches","Small harness updates are easier to validate than broad rewrites.",- topic="engineering",+ path="engineering",)- skill = state.create_memory(+ skill = state.create_skill("Check failures first","Inspect current failure evidence before editing code.",id="failure_first",reference=PYTHON_REFERENCE,arguments={"failure_log": {"type": "string", "description": "Current failure evidence."}},- kind="skill",)- subagent = state.create_memory(+ subagent = state.create_subagent("Reviewer","Review the proposed patch for regressions and missing tests.",metadata={"max_turns": 3},- kind="subagent",)- state.create_memory("Refinement cadence", "Refine only after repeated evidence.", kind="prompt")+ state.create_prompt_note("Refinement cadence", "Refine only after repeated evidence.")+ event = state.record_refinement(+ "skill failed twice",+ ["updated failure_first skill", "added reviewer subagent"],+ evidence="two failed validations",+ outcome="next validation passed",+ )reloaded = HarnessState(state.file_path)@@ -188,6 +120,7 @@ class HarnessStateTest(unittest.TestCase):self.assertEqual(reloaded.get("skill", skill.id).version, 1)self.assertEqual(reloaded.get("skill", skill.id).arguments["failure_log"]["type"], "string")self.assertEqual(reloaded.get("subagent", subagent.id).metadata["max_turns"], 3)+ self.assertEqual(reloaded.refinements[0].id, event.id)self.assertIn("Prefer focused patches", reloaded.overview())self.assertIn("Call contract: installed Python skills use await <skill_import>(...)",@@ -199,7 +132,7 @@ class HarnessStateTest(unittest.TestCase):self.assertIn("receiver_role='parent'", overview)self.assertIn("await rlm.list_subagents()", overview)self.assertIn("receiver_role='child'", overview)- self.assertIn("(engineering, v1)", overview)+ self.assertIn("refinements: 1", reloaded.overview())def test_save_failure_preserves_previous_state_on_disk(self) -> None:with tempfile.TemporaryDirectory() as temp_dir:@@ -320,12 +253,7 @@ class HarnessStateTest(unittest.TestCase):},"missing_content": {"title": "Missing content",- },- "grouped": {- "title": "Grouped",- "content": "c",- "path": "repo/testing",- },+ }}},"refinements": [@@ -350,7 +278,7 @@ class HarnessStateTest(unittest.TestCase):self.assertEqual(state.get("memory", "known").content, "Loaded despite extra keys.")self.assertEqual(state.get("memory", "known").id, "known")self.assertEqual(state.get("memory", "known").kind, "memory")- self.assertEqual(state.get("memory", "known").topic, "general")+ self.assertEqual(state.get("memory", "known").path, "general")self.assertEqual(state.get("memory", "known").source, "agent")self.assertIsNone(state.get("memory", "mismatched"))self.assertEqual(state.get("memory", "known").version, 2)@@ -360,12 +288,6 @@ class HarnessStateTest(unittest.TestCase):self.assertEqual(state.refinements[0].changes, ["1", "loaded"])self.assertEqual(len(state.refinements), 1)self.assertIn("1, loaded", state.overview())- self.assertEqual(state.get("memory", "grouped").topic, "repo/testing")-- state.update_memory("grouped", "Grouped", "resaved")- saved = json.loads(state_path.read_text(encoding="utf-8"))["entries"]["memory"]["grouped"]- self.assertEqual(saved["topic"], "repo/testing")- self.assertEqual(saved["path"], "repo/testing")updated = state.update_memory("known", "Known memory", "Updated content.")self.assertEqual(updated.version, 3)@@ -374,7 +296,7 @@ class HarnessStateTest(unittest.TestCase):with tempfile.TemporaryDirectory() as temp_dir:state = HarnessState(Path(temp_dir) / "harness_state.json")- created = state.create_memory(+ created = state.create_skill("Edit file","Apply a targeted edit.",id="edit_file",@@ -389,9 +311,8 @@ class HarnessStateTest(unittest.TestCase):"find": {"type": "string", "required": True},"replace": {"type": "string", "required": True},},- kind="skill",)- updated = state.update_memory(+ updated = state.update_skill("edit_file","Edit file","Apply a targeted edit after reading context.",@@ -407,7 +328,6 @@ class HarnessStateTest(unittest.TestCase):"replace": {"type": "string", "required": True},"validate": {"type": "boolean", "default": True},},- kind="skill",)reloaded = HarnessState(state.file_path)@@ -424,24 +344,22 @@ class HarnessStateTest(unittest.TestCase):state = HarnessState(Path(temp_dir) / "harness_state.json")with self.assertRaisesRegex(ValueError, "Python reference"):- state.create_memory("No reference", "missing", arguments={}, kind="skill")+ state.create_skill("No reference", "missing", arguments={})with self.assertRaisesRegex(ValueError, "reference.type must be 'python'"):- state.create_memory(+ state.create_skill("Shell reference","bad",reference={"type": "shell", "command": "edit"},arguments={},- kind="skill",)with self.assertRaisesRegex(ValueError, "Python import"):- state.create_memory("No import", "bad", reference={"type": "python", "callable": "run"}, arguments={}, kind="skill")+ state.create_skill("No import", "bad", reference={"type": "python", "callable": "run"}, arguments={})with self.assertRaisesRegex(ValueError, "callable or call_pattern"):- state.create_memory(+ state.create_skill("No callable","bad",reference={"type": "python", "import": "agent_skills.bad"},arguments={},- kind="skill",)def test_rejects_invalid_entry_fields_before_persisting(self) -> None:@@ -458,7 +376,7 @@ class HarnessStateTest(unittest.TestCase):("numeric id", dict(title="T", content="c", id=7), "id must be a non-empty string, got int"),("unhashable list id", dict(title="T", content="c", id=["x"]), "id must be a non-empty string, got a list"),("falsy numeric id", dict(title="Zero", content="c", id=0), "id must be a non-empty string, got int"),- ("numeric topic", dict(title="T", content="c", topic=7), "topic must be a non-empty string, got int"),+ ("numeric path", dict(title="T", content="c", path=7), "path must be a non-empty string, got int"),("list metadata", dict(title="T", content="c", metadata=["m"]), "metadata must be a dict when provided, got a list"),]:with self.subTest(case=f"create {label}"):@@ -466,21 +384,35 @@ class HarnessStateTest(unittest.TestCase):state.create_memory(**kwargs)with self.subTest(case="subagent with list content"):with self.assertRaisesRegex(ValueError, "content must be a non-empty string, got a list"):- state.create_memory("T", ["one string"], kind="subagent")+ state.create_subagent("T", ["one string"])with self.subTest(case="skill create without a reference"):with self.assertRaisesRegex(ValueError, "skill entries require a Python reference"):- state.create_memory("Skill", "content", id="orphan_skill", kind="skill")+ state.create("skill", "Skill", "content", id="orphan_skill")with self.subTest(case="skill reference as a list"):with self.assertRaisesRegex(- ValueError, "skill entry 'skill' rejected: reference must be a dict when provided, got a list"+ ValueError, "skill entry 'Skill' rejected: skill entries require a Python reference"):- state.create_memory("Skill", "content", reference=["bad"], kind="skill")+ state.create_skill("Skill", "content", reference=["bad"])with self.subTest(case="update with list content"):with self.assertRaisesRegex(ValueError, "content must be a non-empty string, got a list"):state.update_memory("valid_entry", "Valid", ["one string"])self.assertEqual(state_path.read_text(encoding="utf-8"), seeded)+ with self.subTest(case="invalid refinement events"):+ for label, kwargs, message in [+ ("trigger list", dict(trigger=["t"], changes="ok"), "trigger must be a non-empty string, got a list"),+ ("event id list", dict(trigger="t", changes="ok", id=["x"]), "id must be a non-empty string when provided, got a list"),+ ("changes int", dict(trigger="t", changes=7), "changes must be a string or a list of strings, got int"),+ ("mixed changes", dict(trigger="t", changes=["ok", 2]), "changes must be a list of non-empty strings"),+ ("evidence list", dict(trigger="t", changes="ok", evidence=["e"]), "evidence must be a string"),+ ("outcome int", dict(trigger="t", changes="ok", outcome=7), "outcome must be a string"),+ ]:+ with self.subTest(case=label):+ with self.assertRaisesRegex(ValueError, message):+ state.record_refinement(**kwargs)+reloaded = HarnessState(state_path)self.assertEqual([entry.id for entry in reloaded.list("memory")], ["valid_entry"])+ self.assertEqual(reloaded.refinements, [])self.assertIsNone(reloaded.get("skill", "orphan_skill"))def test_load_tolerates_corrupt_or_non_object_state(self) -> None:@@ -500,58 +432,94 @@ class HarnessStateTest(unittest.TestCase):def test_update_skill_preserves_omitted_arguments(self) -> None:with tempfile.TemporaryDirectory() as temp_dir:state = HarnessState(Path(temp_dir) / "harness_state.json")- state.create_memory(+ state.create_skill("Edit file","Apply an edit.",id="edit_file",reference=PYTHON_REFERENCE,arguments={"path": {"type": "string", "required": True}},- kind="skill",)# Updating only title/content (arguments omitted) must keep the contract.- state.update_memory("edit_file", "Edit file", "Apply an edit carefully.", reference=PYTHON_REFERENCE, kind="skill")+ state.update_skill("edit_file", "Edit file", "Apply an edit carefully.", reference=PYTHON_REFERENCE)self.assertEqual(state.get("skill", "edit_file").arguments, {"path": {"type": "string", "required": True}})# An explicit empty dict still clears it.- state.update_memory(- "edit_file", "Edit file", "Now argument-free.", reference=PYTHON_REFERENCE, arguments={}, kind="skill"- )+ state.update_skill("edit_file", "Edit file", "Now argument-free.", reference=PYTHON_REFERENCE, arguments={})self.assertEqual(state.get("skill", "edit_file").arguments, {})def test_update_skill_without_reference_preserves_contract(self) -> None:with tempfile.TemporaryDirectory() as temp_dir:state = HarnessState(Path(temp_dir) / "harness_state.json")- state.create_memory(+ state.create_skill("Edit file","Apply an edit.",id="edit_file",reference=PYTHON_REFERENCE,arguments={"path": {"type": "string", "required": True}},- kind="skill",)# A title/content-only update must not require re-sending the reference,# and must preserve the existing reference and arguments.- updated = state.update_memory("edit_file", "Edit file", "Apply an edit carefully.", kind="skill")+ updated = state.update_skill("edit_file", "Edit file", "Apply an edit carefully.")self.assertEqual(updated.version, 2)self.assertEqual(updated.reference, PYTHON_REFERENCE)self.assertEqual(updated.arguments, {"path": {"type": "string", "required": True}})self.assertEqual(updated.content, "Apply an edit carefully.")- def test_update_preserves_omitted_topic(self) -> None:+ def test_update_preserves_omitted_path(self) -> None:with tempfile.TemporaryDirectory() as temp_dir:state = HarnessState(Path(temp_dir) / "harness_state.json")- state.create_memory("Grouped", "content", id="grouped", topic="repo/testing")+ state.create_memory("Grouped", "content", id="grouped", path="repo/testing")- # Updating without a topic keeps the custom grouping topic.+ # Updating without a path keeps the custom grouping path.state.update_memory("grouped", "Grouped", "new content")- self.assertEqual(state.get("memory", "grouped").topic, "repo/testing")+ self.assertEqual(state.get("memory", "grouped").path, "repo/testing")++ # An explicit path still moves it.+ state.update_memory("grouped", "Grouped", "newer", path="repo/other")+ self.assertEqual(state.get("memory", "grouped").path, "repo/other")++ def test_topic_spelled_grouping_migrates_to_path_on_load(self) -> None:+ with tempfile.TemporaryDirectory() as temp_dir:+ state_path = Path(temp_dir) / "harness_state.json"+ state_path.write_text(+ json.dumps(+ {+ "schema": 1,+ "entries": {+ "prompt": {},+ "memory": {+ "topic_entry": {+ "id": "topic_entry",+ "kind": "memory",+ "title": "Topic entry",+ "content": "Window-era content.",+ "topic": "window/era",+ "scope": "local",+ "reference": {},+ "arguments": {},+ "metadata": {},+ "version": 1,+ }+ },+ "skill": {},+ "subagent": {},+ },+ "refinements": [],+ }+ )+ )- # An explicit topic still moves it.- state.update_memory("grouped", "Grouped", "newer", topic="repo/other")- self.assertEqual(state.get("memory", "grouped").topic, "repo/other")+ state = HarnessState(state_path)++ self.assertEqual(state.get("memory", "topic_entry").path, "window/era")+ # The migration is read-only: a save writes the grouping under "path" only.+ state.save()+ persisted = state_path.read_text()+ self.assertIn('"path": "window/era"', persisted)+ self.assertNotIn('"topic"', persisted)def test_in_memory_state_never_touches_disk(self) -> None:previous = os.environ.get("RLM_HARNESS_STATE_DIR")@@ -562,6 +530,7 @@ class HarnessStateTest(unittest.TestCase):try:state = HarnessState(in_memory=True)created = state.create_memory("Volatile", "in memory only", id="volatile")+ state.record_refinement("trigger", ["change"])self.assertIsNone(state.file_path)self.assertEqual(created.content, "in memory only")@@ -667,7 +636,7 @@ class HarnessStateTest(unittest.TestCase):future = state_path.stat().st_mtime + 5os.utime(state_path, (future, future))- # create_memory() must observe the external entry and honor create-or-fail.+ # create() must observe the external entry and honor create-or-fail.with self.assertRaisesRegex(ValueError, "already exists"):state.create_memory("Local", "Should not overwrite.", id="dup")self.assertEqual(state.get("memory", "dup").content, "Written elsewhere.")@@ -676,13 +645,13 @@ class HarnessStateTest(unittest.TestCase):with tempfile.TemporaryDirectory() as temp_dir:state = HarnessState(Path(temp_dir) / "harness_state.json")- first = state.create_memory("Triage", "old", id="triage", reference=PYTHON_REFERENCE, arguments={}, kind="skill")+ first = state.create_skill("Triage", "old", id="triage", reference=PYTHON_REFERENCE, arguments={})with self.assertRaisesRegex(ValueError, "already exists"):- state.create_memory("Triage", "duplicate", id="triage", reference=PYTHON_REFERENCE, arguments={}, kind="skill")+ state.create_skill("Triage", "duplicate", id="triage", reference=PYTHON_REFERENCE, arguments={})with self.assertRaisesRegex(ValueError, "does not exist"):- state.update_memory("missing", "Missing", "missing", reference=PYTHON_REFERENCE, arguments={}, kind="skill")+ state.update_skill("missing", "Missing", "missing", reference=PYTHON_REFERENCE, arguments={})- second = state.update_memory("triage", "Triage", "new", reference=PYTHON_REFERENCE, arguments={}, kind="skill")+ second = state.update_skill("triage", "Triage", "new", reference=PYTHON_REFERENCE, arguments={})self.assertEqual(first.id, second.id)self.assertEqual(second.content, "new")@@ -1000,6 +969,7 @@ class HarnessStateTest(unittest.TestCase):lambda: package_harness.update_memory("lost", "Lost", "content"),lambda: package_harness.delete_memory("lost"),lambda: package_harness.upsert("memory", "Lost", "content", id="lost"),+ lambda: package_harness.record_refinement("trigger", ["change"]),):with self.assertRaisesRegex(RuntimeError, "Local harness state requires.*global_=True"):mutate()@@ -1128,6 +1098,15 @@ class HarnessStateTest(unittest.TestCase):self.assertIs(callable_rlm.harness, package_harness)self.assertIs(callable_rlm.get_harness_state, get_harness_state)+ def test_record_refinement_accepts_single_change_string(self) -> None:+ with tempfile.TemporaryDirectory() as temp_dir:+ state = HarnessState(Path(temp_dir) / "harness_state.json")++ event = state.record_refinement("manual cli test", "single change")++ self.assertEqual(event.changes, ["single change"])+ self.assertEqual(state.refinements[0].changes, ["single change"])+def test_unknown_kind_rejected(self) -> None:with tempfile.TemporaryDirectory() as temp_dir:state = HarnessState(Path(temp_dir) / "harness_state.json")@@ -1137,7 +1116,7 @@ class HarnessStateTest(unittest.TestCase):with self.assertRaisesRegex(ValueError, "unknown harness kind"):state.get("tool", "tool")with self.assertRaisesRegex(ValueError, "unknown harness kind"):- state.delete_memory("tool", kind="tool")+ state.delete("tool", "tool")with self.assertRaisesRegex(ValueError, "unknown harness kind"):state.list("tool")@@ -1152,7 +1131,7 @@ class HarnessSearchTest(unittest.TestCase):state = HarnessState(Path(temp_dir) / "harness_state.json")state.create_memory("Tea notes", "All about oolong brewing.", id="tea")state.create_memory("Worktree policy", "Use git worktrees for parallel branches.", id="worktree")- state.create_memory("RSI program", "Ship [RSI] PRs from worktrees.", id="rsi", kind="prompt")+ state.create_prompt_note("RSI program", "Ship [RSI] PRs from worktrees.", id="rsi")results = state.search("worktree branches")@@ -1164,8 +1143,8 @@ class HarnessSearchTest(unittest.TestCase):with tempfile.TemporaryDirectory() as temp_dir:state = HarnessState(Path(temp_dir) / "harness_state.json")state.create_memory("Worktree memory", "worktree workflow", id="m1")- state.create_memory("Worktree prompt", "worktree workflow", id="p1", kind="prompt")- state.create_memory("Worktree prompt 2", "worktree workflow", id="p2", kind="prompt")+ state.create_prompt_note("Worktree prompt", "worktree workflow", id="p1")+ state.create_prompt_note("Worktree prompt 2", "worktree workflow", id="p2")prompts = state.search("worktree", kind="prompt")self.assertTrue(prompts)