[Session] History management — token budget, expiry cleanup, clear endpoint #40

Closed
opened 2026-03-21 00:35:23 +00:00 by replit · 1 comment
Owner

What

Three related housekeeping concerns for session conversation history:

  1. Enforce a 4 000-token budget when retrieving history (already specified in #38 retrieval helper — verify it is respected)
  2. Automatically delete session_messages rows when a session expires
  3. Expose a DELETE /api/sessions/:id/history endpoint so users can manually clear context

Depends on: #38 (session_messages table), #39 (context injection)

1 — Token budget enforcement

The getSessionHistory helper from #38 should already trim messages to the budget. Add a guard in sessions.ts to log a warning if the returned history still exceeds the budget (defensive check only — do not hard-fail the request).

2 — Expiry cleanup

When a session is marked expired (in GET /sessions/:id line ~228), also delete its history rows:

await db.delete(sessionMessages).where(eq(sessionMessages.sessionId, id));

This keeps the DB lean — expired sessions are dead anyway.

3 — Clear endpoint

Add a new route to artifacts/api-server/src/routes/sessions.ts:

DELETE /api/sessions/:id/history
  • Auth: require valid Authorization: Bearer <macaroon> header (same as /request)
  • Check session is active or paused — reject expired/missing sessions
  • Delete all session_messages rows for the session
  • Return 200 { cleared: true, sessionId }

Register the route in the Express router (already mounted at /api).

Relevant files

  • artifacts/api-server/src/routes/sessions.ts
  • packages/db/src/index.ts
  • packages/db/src/schema.ts

Acceptance

  • History stays within 4 000 tokens even for very long sessions
  • Expiring a session deletes its session_messages rows
  • DELETE /api/sessions/:id/history returns 200 and clears rows
  • DELETE with a bad macaroon returns 401
  • DELETE on an expired session returns 410
## What Three related housekeeping concerns for session conversation history: 1. Enforce a 4 000-token budget when retrieving history (already specified in #38 retrieval helper — verify it is respected) 2. Automatically delete `session_messages` rows when a session expires 3. Expose a `DELETE /api/sessions/:id/history` endpoint so users can manually clear context **Depends on**: #38 (session_messages table), #39 (context injection) ## 1 — Token budget enforcement The `getSessionHistory` helper from #38 should already trim messages to the budget. Add a guard in `sessions.ts` to log a warning if the returned history still exceeds the budget (defensive check only — do not hard-fail the request). ## 2 — Expiry cleanup When a session is marked `expired` (in `GET /sessions/:id` line ~228), also delete its history rows: ```typescript await db.delete(sessionMessages).where(eq(sessionMessages.sessionId, id)); ``` This keeps the DB lean — expired sessions are dead anyway. ## 3 — Clear endpoint Add a new route to `artifacts/api-server/src/routes/sessions.ts`: ``` DELETE /api/sessions/:id/history ``` - Auth: require valid `Authorization: Bearer <macaroon>` header (same as `/request`) - Check session is `active` or `paused` — reject expired/missing sessions - Delete all `session_messages` rows for the session - Return `200 { cleared: true, sessionId }` Register the route in the Express router (already mounted at `/api`). ## Relevant files - `artifacts/api-server/src/routes/sessions.ts` - `packages/db/src/index.ts` - `packages/db/src/schema.ts` ## Acceptance - History stays within 4 000 tokens even for very long sessions - Expiring a session deletes its `session_messages` rows - `DELETE /api/sessions/:id/history` returns 200 and clears rows - `DELETE` with a bad macaroon returns 401 - `DELETE` on an expired session returns 410
replit added the aibackend labels 2026-03-21 00:35:23 +00:00
gemini was assigned by Rockachopa 2026-03-22 23:37:29 +00:00
Collaborator

PR #100 created.

Implemented the following:

  • Added a defensive check for token budget enforcement in session history retrieval.
  • Implemented expiry cleanup for session messages when a session expires.
  • Created a DELETE /api/sessions/:id/history endpoint for manual history clearing, including authentication and session status checks.
PR #100 created. Implemented the following: - Added a defensive check for token budget enforcement in session history retrieval. - Implemented expiry cleanup for session messages when a session expires. - Created a DELETE /api/sessions/:id/history endpoint for manual history clearing, including authentication and session status checks.
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: replit/timmy-tower#40