Skip to content

refactor: isolate agent record persistence#14

Merged
kermanx merged 9 commits into
MoonshotAI:mainfrom
kermanx:codex/refactor-agent-record-persistence
May 25, 2026
Merged

refactor: isolate agent record persistence#14
kermanx merged 9 commits into
MoonshotAI:mainfrom
kermanx:codex/refactor-agent-record-persistence

Conversation

@kermanx
Copy link
Copy Markdown
Collaborator

@kermanx kermanx commented May 25, 2026

Related Issue

N/A

Description

  • Move wire metadata emission into the agent record layer so persistence backends only handle storage.
  • Rename the filesystem wire persistence module and add an in-memory persistence backend for tests.
  • Make record persistence append synchronous and let the filesystem backend own queued append, rewrite, flush, and close behavior.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any.
  • I have added tests that prove my fix is effective or that my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@kermanx
Copy link
Copy Markdown
Collaborator Author

kermanx commented May 25, 2026

@codex

@kermanx kermanx force-pushed the codex/refactor-agent-record-persistence branch from ec2bbca to 38c985b Compare May 25, 2026 08:11
@kermanx
Copy link
Copy Markdown
Collaborator Author

kermanx commented May 25, 2026

@codex

@wbxl2000
Copy link
Copy Markdown
Collaborator

@codex

1 similar comment
@kermanx
Copy link
Copy Markdown
Collaborator Author

kermanx commented May 25, 2026

@codex

@wbxl2000
Copy link
Copy Markdown
Collaborator

[P1] Persistence failures can now synchronously abort later agent state updates. After FileSystemAgentRecordPersistence enters its error state, append() throws synchronously; AgentRecords.logRecord() is called before many live in-memory mutations, such as TurnFlow.prompt() launching the turn and ContextMemory.appendMessage() updating history. This means one background wire.jsonl write failure can make later prompts/context/tool updates throw before the agent updates its live state, instead of only reporting records.write_failed and surfacing the failure through flush()/shutdown paths. Consider catching persistence append failures in AgentRecords and preserving the old non-blocking live path while still reporting the error and making flush() fail.

@kermanx kermanx merged commit 0da6073 into MoonshotAI:main May 25, 2026
4 checks passed
@kermanx kermanx deleted the codex/refactor-agent-record-persistence branch May 25, 2026 08:29
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a5ecf5425

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

if (stamped.type === 'metadata') {
this.metadataInitialized = true;
}
this.persistence?.append(stamped);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve async append error handling in record logging

AgentRecords.logRecord now calls persistence.append synchronously and ignores its return value, so a custom persistence passed via AgentConfig.persistence that still implements append as async (the pre-change contract) can reject without being observed. In that case record-write failures are no longer routed through the existing error path and may surface as unhandled promise rejections while turn APIs continue as if persistence succeeded, which is a regression from the previous append(...).catch(...) behavior.

Useful? React with 👍 / 👎.

Comment on lines +93 to +94
append(input: AgentRecord): void;
rewrite(records: readonly AgentRecord[]): void;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid breaking exported persistence interface in patch release

AgentRecordPersistence is exported from the public agent API, and this change makes rewrite mandatory while changing append from Promise<void> to void. Any external persistence implementation compiled against the previous contract will now fail type-checking (missing rewrite) or silently change behavior around async append completion, so this is a breaking API change that should be compatibility-gated rather than shipped as a patch-level contract swap.

Useful? React with 👍 / 👎.

}

append(input: AgentRecord): void {
this.throwIfError();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Prevent record logging from throwing after persistence errors

The filesystem backend now latches write failures (this.error) and append() throws synchronously on every later call via throwIfError(). Because AgentRecords.logRecord calls append without a try/catch, any subsequent prompt/config/tool action that logs a record can fail with a raw filesystem exception instead of only emitting records.write_failed, effectively making the agent unusable after a transient I/O failure.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants