Skip to content

W-22203667: add timestamp, session type, and index to agent preview sessions output#400

Open
andresrivas-sf wants to merge 2 commits intomainfrom
W-22203667/add-timestamp-sessiontype-to-preview-sessions
Open

W-22203667: add timestamp, session type, and index to agent preview sessions output#400
andresrivas-sf wants to merge 2 commits intomainfrom
W-22203667/add-timestamp-sessiontype-to-preview-sessions

Conversation

@andresrivas-sf
Copy link
Copy Markdown

@andresrivas-sf andresrivas-sf commented Apr 24, 2026

Summary

  • Adds timestamp (ISO string) and sessionType (simulated/live/published) to session-meta.json written on agent preview start
  • Introduces index.json in the sessions directory to maintain creation-ordered session metadata for easier browsing by humans and agents
  • Surfaces new fields as Started At and Session Type columns in agent preview sessions table and JSON output
  • listCachedSessions prefers the index for ordered results, falls back to directory scan for sessions created before this change

Test plan

  • All existing unit tests pass (yarn test)
  • New tests added for index write, removal, ordering, and directory-scan fallback
  • Schema regenerated for agent-preview-sessions command

@W-22203667@

🤖 Generated with Claude Code

…s output

W-22203667

- Add `timestamp` (ISO string) and `sessionType` (simulated/live/published)
  to `session-meta.json` written by `agent preview start`
- Introduce `index.json` in the sessions directory to maintain creation-ordered
  session metadata for easier browsing by humans and agents
- Surface new fields as "Started At" and "Session Type" columns in
  `agent preview sessions` table and JSON output
- `listCachedSessions` prefers the index for ordered results, falls back to
  directory scan for sessions created before this change
- Update JSON schema and add tests for index write, removal, ordering, and fallback

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@andresrivas-sf andresrivas-sf requested a review from a team as a code owner April 24, 2026 19:01
@andresrivas-sf andresrivas-sf changed the title feat(W-22203667): add timestamp, session type, and index to agent preview sessions output W-22203667: add timestamp, session type, and index to agent preview sessions output Apr 24, 2026
@andresrivas-sf
Copy link
Copy Markdown
Author

The NUT failures in this run are pre-existing and unrelated to this PR's changes. The same assertion failure at test/nuts/z2.agent.publish.nut.ts:132 (expected SyntaxError: Unexpected 'syem') is also failing on PR #399 (fp/add-agent-api-name-to-preview-json) from a different author, confirming this is a flaky test in agent publish authoring-bundle error handling that predates these changes.

Comment thread src/previewSessionStore.ts Outdated
const sessionsDir = dirname(historyDir);
const indexPath = join(sessionsDir, SESSION_INDEX_FILE);
const index = await readSessionIndex(indexPath);
if (!index.some((e) => e.sessionId === sessionId)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we're writing a file in the session history, it should be done in the library, so VSC and plugin conversations save the same data to the same locations

/**
* Read the sessions index file, returning an empty array if missing or unreadable.
*/
async function readSessionIndex(indexPath: string): Promise<SessionIndex> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this should be in the library, reading/writing files

@WillieRuemmele
Copy link
Copy Markdown
Contributor

and then claude review:

Branch Review                                                                                                   
                                             
  Verdict: REFINE                                                                                                 
                                                                                                                  
  Summary: The core feature is well-structured and tested, but the index file introduces two real race conditions,
   a type widening inconsistency, and a pattern violation in the import style.                                    
                                                                                                                  
  ---                                                                                                             
  Critical                                                                                                        
                                                                                                                  
  - Race condition on concurrent createCache calls — src/previewSessionStore.ts:55-61 — Two parallel createCache
  calls for the same agent will both read the same stale index, append their entry, and write back; the second    
  write overwrites the first, silently dropping a session from the index. While rare in a CLI, it is a real     
  data-loss scenario for programmatic callers.                                                                    
  - Race condition on concurrent removeCache calls — src/previewSessionStore.ts:97-103 — Same read-modify-write 
  pattern; concurrent removes can restore a deleted entry or silently no-op.                                      
  
  ---                                                                                                             
  Worth Fixing                                                                                                  
                                                                                                                  
  - sessionType?: string should be sessionType?: SessionType — src/commands/agent/preview/sessions.ts:29 — The  
  result type widens the union to string, breaking the discriminated-union guarantee for JSON consumers.          
  CachedSessionInfo correctly types it as SessionType; the command result type should match.
  - Inline import() type expression instead of a top-level named import — src/commands/agent/preview/start.ts:173 
  — resolveSessionType uses : import('../../../previewSessionStore.js').SessionType as its return annotation.     
  Every other consumer in this directory uses a normal top-level named import. SessionType should be added to the
  existing import { createCache } statement on line ~20.                                                          
  - displayName = index[0]?.displayName assumes sessions are homogeneous — src/previewSessionStore.ts:200 — If  
  different sessions for the same agentId were started with different --authoring-bundle or --api-name values, the
   first session's displayName is applied to all. CachedSessionInfo doesn't carry displayName, so there's no way
  to surface per-session display names. The behavior is the same as before the diff (the original code also read  
  from sessionIds[0]), but now the assumption is more exposed.                                                  
  - Unhandled writeFile errors in index updates — src/previewSessionStore.ts:60 and :102 — If the index write
  fails (disk full, permissions), the error is swallowed silently, leaving the index out of sync with session     
  directories. The existing pattern for the meta file write (line ~51) propagates the error; the index write
  should too, or at minimum log a warning.                                                                        
  - SessionIndex exported but used only internally — src/previewSessionStore.ts:28 — readSessionIndex is not    
  exported, so no external caller can ever receive a SessionIndex value. Removing the export makes the scope      
  honest.
                                                                                                                  
  ---                                                                                                           
  Simplification Opportunities
                                                                                                                  
  - Double-read of session-meta.json in fallback path — src/previewSessionStore.ts:219-225 — The fallback
  directory-scan loop already reads and parses each session's meta file, but discards displayName from the first  
  entry. It then re-reads sessions[0]'s meta file a second time solely to recover displayName. Capturing it during
   the first pass eliminates a redundant I/O call.                                                                
                                                                                                                
  ---                                                                       

- Fix race condition on concurrent createCache/removeCache calls by
  using atomic write (temp file + rename) via updateSessionIndex helper
- Remove export from SessionIndex type — used only internally
- Fix sessionType widening: use SessionType union instead of string in
  AgentPreviewSessionsResult and resolveSessionType return type
- Replace inline import() type annotation with top-level named import
- Eliminate double-read in fallback dir scan by capturing displayName
  during the first pass
- Update JSON schema to use $ref for SessionType enum

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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