Skip to content

feat(web): add session diagnostic export - #1646

Merged
wbxl2000 merged 5 commits into
mainfrom
codex/web-session-export
Jul 14, 2026
Merged

feat(web): add session diagnostic export#1646
wbxl2000 merged 5 commits into
mainfrom
codex/web-session-export

Conversation

@wbxl2000

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue. The Web UI had no equivalent of the TUI diagnostic session export.

Problem

Web users could not download a session archive with browser-side diagnostics, which made failures in the REST/WebSocket/UI path difficult to investigate. Reusing the existing export code also required tighter resource ownership because active logs can grow or rotate while an archive is being written, and browser downloads buffer the final ZIP in memory.

What changed

  • Add the Web /export command and POST /api/v1/sessions/{session_id}/export, returning either a ZIP or the existing JSON error envelope.
  • Add a separate always-on, metadata-only browser event ring. Prompt text, WebSocket payloads, and console arguments remain in the opt-in debug trace and are never included in the session archive.
  • Stream session files from bounded, lazily opened file sources; keep at most one session source open, snapshot active log sizes, propagate cancellation, and clean up source handles and temporary archives on every exit path.
  • Limit Web archives to 64 MiB because the browser buffers the response before saving it; larger sessions can still use the CLI or TUI export.
  • Document the command in English and Chinese, update OpenAPI coverage, and add a changeset.

Verification

  • pnpm --filter @moonshot-ai/agent-core-v2 exec vitest run test/app/sessionExport/sessionExport.test.ts (11 tests; repeated serially, including 320 files, append, abort, and size-limit cases)
  • Three isolated RSS samples for that suite: 167,215,104 / 165,134,336 / 166,510,592 bytes
  • pnpm exec vitest run test/sessions.test.ts test/openapi.test.ts test/apiSurface.snapshot.test.ts in packages/kap-server (58 tests)
  • pnpm test in apps/kimi-web (497 tests)
  • pnpm test in packages/protocol (524 tests)
  • Typecheck: protocol, agent-core-v2, kap-server, kimi-web
  • Build: kimi-web, docs
  • pnpm exec oxlint --type-aware --quiet (0 errors)
  • pnpm lint:domain in packages/agent-core-v2
  • git diff --check

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 106ead4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@pkg-pr-new

pkg-pr-new Bot commented Jul 13, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@106ead4
npx https://pkg.pr.new/@moonshot-ai/kimi-code@106ead4

commit: 106ead4

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

Copy link
Copy Markdown

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: 3631a0f555

ℹ️ 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".

Comment thread packages/agent-core-v2/src/app/sessionExport/zip.ts Outdated
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 14530835e8

ℹ️ 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".

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex

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

Copy link
Copy Markdown

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: 106ead4e81

ℹ️ 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".

Comment on lines +59 to +61
rawResponse: {
200: { type: 'string', format: 'binary' },
},

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 using a binary 200 schema for JSON error replies

In the validation, missing-session, and too-large cases this route sends an errEnvelope without changing the status, so those failures are still 200 responses. Because the route registers the only 200 response schema as a binary string, Fastify will select that serializer for the envelope object instead of the JSON envelope shape that postZip() expects, so users can receive a malformed/opaque error rather than the intended SESSION_NOT_FOUND or FILE_TOO_LARGE code. Either send error envelopes with non-200 statuses or avoid installing a binary-only 200 serializer for the error path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

False positive for kap-server: schema.response never drives runtime serialization here. start.ts replaces both compilers with pass-throughs (app.setSerializerCompiler(() => (data) => JSON.stringify(data))), so error envelopes are serialized as plain JSON regardless of the binary 200 schema. This is covered by tests: test/sessions.test.ts asserts the missing-session path returns the 40401 envelope and the oversized-web-log path returns 40001 with details.

The response schema is only used for the OpenAPI document, and the served document is already accurate: openapi/transforms.ts rewrites this route's 200 response to describe both application/zip (binary) and application/json (error envelope) — see GET /openapi.json ("Session export archive or JSON error envelope").

@wbxl2000
wbxl2000 merged commit 5eb6217 into main Jul 14, 2026
14 checks passed
@wbxl2000
wbxl2000 deleted the codex/web-session-export branch July 14, 2026 04:48
@github-actions github-actions Bot mentioned this pull request Jul 14, 2026
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

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

Copy link
Copy Markdown

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: 5eb62178b3

ℹ️ 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".

try {
sessionLogSource = await openOptionalZipSource(sessionLogPath, input.signal);
if (input.request.includeGlobalLog === true && input.globalLogPath !== undefined) {
globalSource = await openOptionalZipSource(input.globalLogPath, input.signal);

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 Keep optional global-log failures from aborting export

When includeGlobalLog is true and the configured global log path exists but cannot be opened or is not a regular file (for example EACCES or EISDIR), this call rethrows through openOptionalZipSource and the whole session export fails. The global log is optional and the previous best-effort path omitted it on read failures, so a bad app log should not prevent users from downloading the session archive; catch non-abort global-log errors here and leave globalLogPath unset.

Useful? React with 👍 / 👎.

Comment on lines +70 to +71
export const exportSessionRequestSchema = z
.object({

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 Accept an omitted export request body

Because the route installs validateBody with this schema, a POST /sessions/{id}/export that has no JSON body parses undefined and returns 40001 before the handler runs. Since web_log is optional and exporting does not otherwise require a body, direct REST clients or generated clients that omit the optional request body cannot download an archive unless they know to send {}; mirror the compact request schema by preprocessing undefined to {}.

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.

1 participant