Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix(harness): validate refinement writes and skip malformed entries in the digest (#2463)

Seth Karten41e4e0e1868f
diff --git a/packages/coding-agent/.changes/harness-refinement-validation.md b/packages/coding-agent/.changes/harness-refinement-validation.md
new file mode 100644
index 0000000000..3151be60f3
--- /dev/null
+++ b/packages/coding-agent/.changes/harness-refinement-validation.md
@@ -0,0 +1,2 @@
+- Harness store writes now validate entry shape before persisting: `rlm.harness` create/update/upsert calls reject non-string or empty `title`/`content`, empty or non-string ids, malformed paths, non-dict `reference`/`arguments`/`metadata`, and skill entries without a valid Python reference, with a clear error naming the entry and the violated entry field; refinement events reject non-string triggers and invalid `changes`/ids. `/refine` and rollback edits reject the same shapes through `validateEdit`.
+- The harness digest and refine overview now skip malformed persisted entries and refinement events (non-object events plus events with non-string ids, triggers, change elements, or outcomes, labeled by bounded type instead of value) with a `harness: skipped malformed entry <id> (...)` diagnostic line instead of crashing session creation, so a single corrupt store entry can no longer brick every session and child spawn.
diff --git a/packages/coding-agent/src/core/refinement/refinement.ts b/packages/coding-agent/src/core/refinement/refinement.ts
index 28f5badf05..619666a919 100644
--- a/packages/coding-agent/src/core/refinement/refinement.ts
+++ b/packages/coding-agent/src/core/refinement/refinement.ts
@@ -263,7 +263,9 @@ function emptyHarnessState(): HarnessState {
}
function slug(raw: string, fallback: string): string {
- const normalized = raw
+ // A malformed value (for example a non-string title) cannot be normalized; resolve
+ // to the fallback so apply-time validation can still reject the edit by id.
+ const normalized = (typeof raw === "string" ? raw : fallback)
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, "_")
@@ -452,6 +454,42 @@ export function mergeRefinementHistory(
return [...byId.values()];
}
+/** Why a persisted harness entry cannot be rendered safely: the field whose
+ * stored type violates the entry contract (write paths reject these shapes).
+ * Render paths skip such entries with a diagnostic instead of throwing, so one
+ * corrupt entry (from an older build or a hand-edited store) can never break
+ * session creation by crashing the harness digest. */
+export function harnessEntryMalformation(entry: HarnessEntry): string | undefined {
+ if (typeof entry.content !== "string") return "content not a string";
+ if (typeof entry.title !== "string") return "title not a string";
+ return undefined;
+}
+
+/** Same contract for refinement events: the digest renders id, trigger, changes,
+ * and outcome with string operations, so a non-string id or trigger, non-array
+ * changes, non-string change elements, or non-string outcome must be skipped
+ * with a diagnostic rather than crash the digest or render junk. */
+export function harnessRefinementMalformation(event: HarnessRefinementEvent): string | undefined {
+ if (typeof event !== "object" || event === null) return "event not an object";
+ if (typeof event.id !== "string") return "id not a string";
+ if (typeof event.trigger !== "string") return "trigger not a string";
+ if (!Array.isArray(event.changes)) return "changes not an array";
+ if (!event.changes.every((change) => typeof change === "string")) return "changes contain a non-string";
+ if (event.outcome !== undefined && typeof event.outcome !== "string") return "outcome not a string";
+ return undefined;
+}
+
+/** Bounded label for a skipped malformed refinement event. Non-object elements
+ * and invalid ids are labeled by type, never by value: a corrupt store element
+ * must not inject arbitrary unbounded text into every session's prompt digest. */
+function malformedRefinementEventLabel(event: HarnessRefinementEvent): string {
+ if (event === null) return "null";
+ if (typeof event === "undefined") return "undefined";
+ if (typeof event !== "object") return `a ${typeof event}`;
+ if (Array.isArray(event)) return "an array";
+ return typeof event.id === "string" ? event.id : `a ${typeof event.id} id`;
+}
+
function compactText(text: string, maxLength: number): string {
const normalized = text.replace(/\s+/g, " ").trim();
if (normalized.length <= maxLength) {
@@ -467,11 +505,12 @@ export function formatRefinementNoticeBody(result: RefinementResult): string {
if (!edit.applied) continue;
const entry = edit.after ?? edit.before;
const scope = entry?.scope ?? result.scope ?? "local";
+ const malformation = entry ? harnessEntryMalformation(entry) : undefined;
lines.push(
- `- ${edit.action} ${edit.kind} [${scope}:${edit.id}] ${entry?.title ?? edit.id}: ${compactText(
- entry?.content ?? "",
+ `- ${edit.action} ${edit.kind} [${scope}:${edit.id}] ${malformation ? edit.id : (entry?.title ?? edit.id)}: ${compactText(
+ malformation ? "" : (entry?.content ?? ""),
DEFAULT_OVERVIEW_CONTENT_LIMIT,
- )}`,
+ )}${malformation ? ` (skipped malformed entry: ${malformation})` : ""}`,
);
}
return lines.join("\n");
@@ -672,6 +711,11 @@ export function formatHarnessStateForPrompt(
lines.push("(entries ranked by relevance to the current task; see harness.search)");
}
for (const entry of entries.slice(0, maxEntriesPerKind)) {
+ const malformation = harnessEntryMalformation(entry);
+ if (malformation) {
+ lines.push(`harness: skipped malformed entry ${entry.id} (${malformation})`);
+ continue;
+ }
const argumentsText =
entry.kind === "skill" && Object.keys(entry.arguments).length > 0
? ` args=${compactText(JSON.stringify(entry.arguments), maxContentLength)}`
@@ -700,6 +744,13 @@ export function formatHarnessStateForPrompt(
lines.push(`recent refinements: ${state.refinements.length}`);
for (const event of state.refinements.slice(-maxRefinements)) {
+ const malformation = harnessRefinementMalformation(event);
+ if (malformation) {
+ lines.push(
+ `harness: skipped malformed refinement event ${malformedRefinementEventLabel(event)} (${malformation})`,
+ );
+ continue;
+ }
const changes = event.changes.length > 0 ? event.changes.join(", ") : "no applied edits";
const outcome = event.outcome ? `; outcome: ${compactText(event.outcome, maxContentLength)}` : "";
lines.push(`- [${event.id}] ${compactText(event.trigger, maxContentLength)}: ${changes}${outcome}`);
@@ -720,7 +771,8 @@ export function formatHarnessStateForPrompt(
*
* Covered: entry identity and content (entry order is normalized away, as is
* the call contract on non-skill entries, which the formatter never prints),
- * plus the render flags and each refinement's printed fields in stored order,
+ * plus the render flags and each refinement's printed fields in stored order
+ * (a malformed event's printed fields are its skip-line label and reason),
* since the formatter renders a positional newest tail. The shell-examples
* flag participates only when IPython examples are not rendered: the formatter
* never reads it then, so it is normalized out of the fingerprint to keep an
@@ -755,12 +807,16 @@ export function harnessDigestFingerprint(
// Refinements keep their stored order: the formatter renders the newest
// tail of the array, so an order-only change renders differently and must
// not reuse the previous digest.
- const refinements = state.refinements.map((event) => ({
- id: event.id,
- trigger: event.trigger,
- changes: event.changes,
- outcome: event.outcome,
- }));
+ const refinements = state.refinements.map((event) => {
+ const malformation = harnessRefinementMalformation(event);
+ // A malformed event renders as a skip line (label + reason), not its
+ // fields, so that pair is the fingerprint material for it: fingerprint
+ // equality implies identical renders, corrupted stores included.
+ if (malformation !== undefined) {
+ return { malformed: malformation, label: malformedRefinementEventLabel(event) };
+ }
+ return { id: event.id, trigger: event.trigger, changes: event.changes, outcome: event.outcome };
+ });
// The formatter renders the shell call-contract only when IPython examples
// are absent, so the shell flag cannot change the digest while IPython
// examples take precedence; fingerprint only the flags the render reads.
@@ -782,6 +838,11 @@ function overviewForPrompt(state: HarnessState): string {
const entries = Object.values(state.entries[kind]);
lines.push(`${kind}: ${entries.length}`);
for (const entry of entries.slice(0, 40)) {
+ const malformation = harnessEntryMalformation(entry);
+ if (malformation) {
+ lines.push(`- harness: skipped malformed entry ${entry.id} (${malformation})`);
+ continue;
+ }
const content = entry.content.replace(/\s+/g, " ").slice(0, 240);
const argumentsText =
entry.kind === "skill" && Object.keys(entry.arguments).length > 0
@@ -943,6 +1004,27 @@ function validateEdit(edit: RefinementEdit, computedId?: string): string | undef
if (edit.action !== "delete" && (!edit.title || !edit.content)) {
return `${edit.action} requires title and content`;
}
+ if (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.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" &&
+ (typeof edit.title !== "string" || typeof edit.content !== "string" || !edit.title || !edit.content)
+ ) {
+ return `${edit.action} requires title and content to be non-empty strings`;
+ }
+ if (edit.reference !== undefined && objectRecord(edit.reference) === undefined) {
+ return `${edit.action} requires reference to be an object when provided`;
+ }
+ if (edit.arguments !== undefined && objectRecord(edit.arguments) === undefined) {
+ return `${edit.action} requires arguments to be an object when provided`;
+ }
+ if (edit.metadata !== undefined && objectRecord(edit.metadata) === undefined) {
+ return `${edit.action} requires metadata to be an object when provided`;
+ }
if (edit.action !== "delete" && edit.kind === "skill" && edit.arguments === undefined) {
return `${edit.action} skill requires arguments`;
}
diff --git a/packages/coding-agent/test/refinement.test.ts b/packages/coding-agent/test/refinement.test.ts
index def8b95c34..74cd4b0ce4 100644
--- a/packages/coding-agent/test/refinement.test.ts
+++ b/packages/coding-agent/test/refinement.test.ts
@@ -298,6 +298,22 @@ describe("harness refinement", () => {
seed?: RefinementKind;
};
const skillFieldsFor = (kind: RefinementKind) => (kind === "skill" ? skillContract : {});
+ // Raw (unnormalized) edit shapes for apply-time validation; overrides carry the violation.
+ const editWith = (
+ action: RefinementAction,
+ kind: RefinementKind,
+ id: string,
+ overrides: Record<string, unknown> = {},
+ ): RefinementProposal["edits"][number] =>
+ ({
+ action,
+ kind,
+ id,
+ title: "t",
+ content: "c",
+ ...skillFieldsFor(kind),
+ ...overrides,
+ }) as RefinementProposal["edits"][number];
it.each<InvalidCase>([
...kinds.map(
(kind): InvalidCase => ({
@@ -388,6 +404,48 @@ describe("harness refinement", () => {
edit: { action: "create", kind: "prompt", title: "Base System Prompt", content: "c" },
error: "base system prompt",
},
+ {
+ label: "a create with list content",
+ edit: editWith("create", "memory", "list_content", { content: ["one string"] }),
+ error: "create requires title and content to be non-empty strings",
+ },
+ {
+ label: "a create with list title",
+ edit: editWith("create", "prompt", "list_title", { title: ["t"] }),
+ error: "create requires title and content to be non-empty strings",
+ },
+ {
+ label: "an update restoring list content",
+ seed: "memory",
+ edit: editWith("update", "memory", "memory_entry", { content: ["one string"] }),
+ error: "update requires title and content to be non-empty strings",
+ },
+ {
+ label: "a create with a numeric id",
+ edit: editWith("create", "memory", "numeric_id", { id: 7 }),
+ error: "create requires id to be a non-empty string when provided",
+ },
+ {
+ label: "a skill with a list reference",
+ edit: editWith("create", "skill", "list_reference", { reference: ["bad"] }),
+ error: "create requires reference to be an object 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",
+ edit: editWith("create", "skill", "list_arguments", { arguments: ["a"] }),
+ error: "create requires arguments to be an object when provided",
+ },
+ {
+ label: "an update with list metadata",
+ seed: "memory",
+ edit: editWith("update", "memory", "memory_entry", { metadata: ["m"] }),
+ error: "update requires metadata to be an object when provided",
+ },
])("rejects $label without mutating state", ({ seed, edit, error }) => {
const state = loadHarnessState(makeTempDir());
if (seed) seedEntry(state, seed);
diff --git a/packages/coding-agent/test/suite/agent-session-prompt.test.ts b/packages/coding-agent/test/suite/agent-session-prompt.test.ts
index 942ffd1e3d..65c5a2b4cd 100644
--- a/packages/coding-agent/test/suite/agent-session-prompt.test.ts
+++ b/packages/coding-agent/test/suite/agent-session-prompt.test.ts
@@ -1004,6 +1004,40 @@ describe("Harness digest at cold boundaries", () => {
};
}
+ it("delivers a diagnostic digest instead of crashing on a malformed global entry", async () => {
+ // Regression for the fleet-wide incident: one entry with list content in
+ // the global store bricked all child spawn creation via the digest crash.
+ const agentDir = isolatedAgentDir("pi-digest-malformed");
+ mkdirSync(join(agentDir, "harness"), { recursive: true });
+ writeFileSync(
+ join(agentDir, "harness", "harness_state.json"),
+ '{"schema":1,"entries":{"prompt":{},"skill":{},"subagent":{},"memory":{"broken_memory":{"id":"broken_memory","kind":"memory","title":"Breaking memory","content":["one string"],"path":"arc","scope":"global","version":1},"valid_memory":{"id":"valid_memory","kind":"memory","title":"Valid memory","content":"Worktree workflow notes.","path":"general","scope":"global","version":1}}},"refinements":[{"id":"refine_bad","trigger":["not a string"],"changes":[],"evidence":"","outcome":""},null,"RAWLEAK-5f1e",{"id":null,"trigger":"t","changes":["update memory:m"]},{"id":"bad_changes","trigger":"t","changes":[7]}]}',
+ );
+
+ const harness = await createHarness({ persistSession: true });
+ harnesses.push(harness);
+ harness.setResponses([fauxAssistantMessage("hi")]);
+ await harness.session.prompt("hello");
+
+ const digests = digestMessages(harness);
+ expect(digests).toHaveLength(1);
+ const digest = getMessageText(digests[0]);
+ expect(digest).toContain("harness: skipped malformed entry broken_memory (content not a string)");
+ expect(digest).toContain("harness: skipped malformed refinement event refine_bad (trigger not a string)");
+ expect(digest).toContain("harness: skipped malformed refinement event null (event not an object)");
+ // Non-object elements are labeled by type only: the raw value must not leak.
+ expect(digest).toContain("harness: skipped malformed refinement event a string (event not an object)");
+ expect(digest).not.toContain("RAWLEAK-5f1e");
+ // Non-string ids and non-string change elements are skipped by type label, not rendered.
+ expect(digest).toContain("harness: skipped malformed refinement event a object id (id not a string)");
+ expect(digest).toContain(
+ "harness: skipped malformed refinement event bad_changes (changes contain a non-string)",
+ );
+ expect(digest).toContain("[global:valid_memory]");
+ // The malformed content itself must never leak into the digest.
+ expect(digest).not.toContain("one string");
+ });
+
it("skips digest re-delivery on resume when only query terms drifted and the state is unchanged", async () => {
// Hermetic store: the ambient developer harness would crowd the ranked window.
isolatedAgentDir("pi-digest-resume");
diff --git a/prime-agent-runtime/src/rlm/harness.py b/prime-agent-runtime/src/rlm/harness.py
index 5040a2fcff..03dabfaf2e 100644
--- a/prime-agent-runtime/src/rlm/harness.py
+++ b/prime-agent-runtime/src/rlm/harness.py
@@ -201,19 +201,121 @@ _ENTRY_FIELDS = {field.name for field in fields(HarnessEntry)}
_REFINEMENT_FIELDS = {field.name for field in fields(RefinementEvent)}
-def _validate_python_skill_reference(reference: dict[str, Any] | None) -> dict[str, Any]:
+def _validate_python_skill_reference(reference: dict[str, Any] | None, entry_name: str = "") -> dict[str, Any]:
+ # Rejections name the entry so the caller can repair the right skill; the
+ # suffix keeps the historical message text greppable.
+ prefix = f"skill entry {entry_name!r} rejected: " if entry_name else ""
+
+ def reject(message: str) -> None:
+ raise ValueError(f"{prefix}{message}")
+
if not isinstance(reference, dict):
- raise ValueError("skill entries require a Python reference")
+ reject("skill entries require a Python reference")
normalized = dict(reference)
if normalized.get("type") != "python":
- raise ValueError("skill reference.type must be 'python'")
+ reject("skill reference.type must be 'python'")
if not any(isinstance(normalized.get(key), str) and normalized[key] for key in ("import", "python_import")):
- raise ValueError("skill reference requires a Python import")
+ reject("skill reference requires a Python import")
if not any(isinstance(normalized.get(key), str) and normalized[key] for key in ("callable", "call_pattern")):
- raise ValueError("skill reference requires a callable or call_pattern")
+ reject("skill reference requires a callable or call_pattern")
return normalized
+def _type_name(value: Any) -> str:
+ if isinstance(value, list):
+ return "a list"
+ if value == "":
+ return "an empty string"
+ return type(value).__name__
+
+
+def _describe_entry(id: Any, title: Any) -> str:
+ """Best available entry name for rejection messages."""
+ if isinstance(id, str) and id:
+ return id
+ if isinstance(title, str) and title:
+ return title
+ return "<unnamed>"
+
+
+def _require_text(kind: str, entry_name: str, field: str, value: Any) -> None:
+ if not isinstance(value, str) or not value:
+ raise ValueError(
+ f"{kind} entry {entry_name!r} rejected: {field} must be a non-empty string, got {_type_name(value)}"
+ )
+
+
+def _require_optional_text(kind: str, entry_name: str, field: str, value: Any) -> None:
+ if value is not None:
+ _require_text(kind, entry_name, field, value)
+
+
+def _require_optional_record(kind: str, entry_name: str, field: str, value: Any) -> None:
+ if value is not None and not isinstance(value, dict):
+ raise ValueError(
+ f"{kind} entry {entry_name!r} rejected: {field} must be a dict when provided, got {_type_name(value)}"
+ )
+
+
+def _validate_entry_shape(
+ kind: str,
+ entry_id: Any,
+ title: Any,
+ content: Any,
+ *,
+ path: Any,
+ reference: Any,
+ arguments: Any,
+ metadata: Any,
+ source: Any,
+ existing: "HarnessEntry | None",
+) -> None:
+ """Reject an invalid harness entry before anything is persisted.
+
+ Every create/update/upsert write funnels through here, so a malformed
+ entry (content as a list, title as a number) fails with an actionable
+ error naming the entry and the field instead of being saved and later
+ crashing the host digest that renders every session's system prompt.
+ """
+ entry_name = _describe_entry(entry_id, title)
+ _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, "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)
+ _require_text(kind, entry_name, "source", source)
+ if kind == "skill":
+ if reference is None:
+ # A new skill without a Python reference is invalid; an update that
+ # omits it preserves the existing reference instead.
+ if existing is None:
+ raise ValueError(f"skill entry {entry_name!r} rejected: skill entries require a Python reference")
+ else:
+ _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."""
@@ -453,8 +555,27 @@ class HarnessState:
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
+ # silently collapse) must fail with a clear rejection, not an
+ # AttributeError inside slug normalization or a TypeError from the lookup.
+ _require_text(kind, _describe_entry(id, title), "title", title)
+ if id is not None:
+ _require_text(kind, _describe_entry(id, title), "id", id)
entry_id = id or _slug(title, kind)
existing = self.entries[kind].get(entry_id)
+ _validate_entry_shape(
+ kind,
+ entry_id,
+ title,
+ content,
+ path=path,
+ reference=reference,
+ arguments=arguments,
+ metadata=metadata,
+ source=source,
+ existing=existing,
+ )
if existing:
existing.title = title
existing.content = content
@@ -558,6 +679,9 @@ class HarnessState:
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)
entry_id = id or _slug(title, kind)
if entry_id in self.entries[kind]:
raise ValueError(f"{kind} entry {entry_id!r} already exists")
@@ -605,6 +729,7 @@ class HarnessState:
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(f"{kind} entry {id!r} does not exist")
return self._upsert(
@@ -696,7 +821,7 @@ class HarnessState:
content,
id=id,
path=path,
- reference=_validate_python_skill_reference(reference),
+ reference=_validate_python_skill_reference(reference, _describe_entry(id, title)),
arguments=arguments,
metadata=metadata,
global_=global_,
@@ -719,7 +844,9 @@ class HarnessState:
# 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) if reference is not None else None
+ validated_reference = (
+ _validate_python_skill_reference(reference, _describe_entry(id, title)) if reference is not None else None
+ )
return self.update(
"skill",
id,
@@ -780,6 +907,11 @@ class HarnessState:
return target.record_refinement(trigger, changes, evidence=evidence, outcome=outcome, id=id)
self._ensure_local_writable()
self._sync_from_disk()
+ _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(
diff --git a/prime-agent-runtime/test/test_harness.py b/prime-agent-runtime/test/test_harness.py
index fcd3be2f07..1b2ed97693 100644
--- a/prime-agent-runtime/test/test_harness.py
+++ b/prime-agent-runtime/test/test_harness.py
@@ -362,6 +362,59 @@ class HarnessStateTest(unittest.TestCase):
arguments={},
)
+ def test_rejects_invalid_entry_fields_before_persisting(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ state_path = Path(temp_dir) / "harness_state.json"
+ state = HarnessState(state_path)
+ state.create_memory("Valid", "seed content", id="valid_entry")
+ seeded = state_path.read_text(encoding="utf-8")
+
+ for label, kwargs, message in [
+ ("content list", dict(title="T", content=["one string"]), "content must be a non-empty string, got a list"),
+ ("title list", dict(title=["T"], content="c"), "title must be a non-empty string, got a list"),
+ ("empty title", dict(title="", content="c"), "title must be a non-empty string, got an empty string"),
+ ("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 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}"):
+ with self.assertRaisesRegex(ValueError, message):
+ 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_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("skill", "Skill", "content", id="orphan_skill")
+ with self.subTest(case="skill reference as a list"):
+ with self.assertRaisesRegex(
+ ValueError, "skill entry 'Skill' rejected: skill entries require a Python reference"
+ ):
+ 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:
for payload in ("not json at all", "null", "[]", '"a string"', "123"):
with tempfile.TemporaryDirectory() as temp_dir: