-
Notifications
You must be signed in to change notification settings - Fork 94
fix(capture): make session-event INSERTs idempotent to stop duplicate rows (C1) #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef1cccf
fix(capture): make session-event INSERT idempotent to stop duplicate …
efenocchi 36af507
fix(pi): make pi sessions INSERT idempotent too
efenocchi 87ee8db
Merge remote-tracking branch 'origin/main' into fix/session-insert-id…
efenocchi c49ccd8
fix(session-queue): make the batched cowork INSERT idempotent too
efenocchi c556c2c
fix(capture): address CodeRabbit review on the C1 idempotency PR
efenocchi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { sqlIdent, sqlStr } from "../../utils/sql.js"; | ||
|
|
||
| /** | ||
| * Fields for one session-event row written by the capture hooks. All string | ||
| * values are escaped by the builder EXCEPT `jsonForSql` and `embeddingSql`, | ||
| * which are pre-formatted SQL fragments (see notes on each field). | ||
| */ | ||
| export interface DirectSessionInsertParams { | ||
| /** Stable per-event row id. MUST be constant across retries of the same | ||
| * event so the idempotency guard below can recognise a re-send. */ | ||
| id: string; | ||
| sessionPath: string; | ||
| filename: string; | ||
| /** JSON payload with single quotes already doubled (`'' `). Embedded raw and | ||
| * cast to jsonb — do NOT pass through sqlStr(), which would corrupt the JSON. */ | ||
| jsonForSql: string; | ||
| /** SQL literal for message_embedding: either `NULL` or `ARRAY[...]::float4[]`. | ||
| * Produced by embeddingSqlLiteral(); embedded raw. */ | ||
| embeddingSql: string; | ||
| userName: string; | ||
| sizeBytes: number; | ||
| projectName: string; | ||
| description: string; | ||
| agent: string; | ||
| pluginVersion: string; | ||
| /** ISO timestamp used for both creation_date and last_update_date. */ | ||
| timestamp: string; | ||
| } | ||
|
|
||
| /** | ||
| * Build the single-row session INSERT used by every capture hook. | ||
| * | ||
| * Idempotent by construction: the row is inserted via `INSERT ... SELECT ... | ||
| * WHERE NOT EXISTS (SELECT 1 FROM <table> WHERE id = <id>)` rather than a plain | ||
| * `VALUES` insert. The Deeplake sessions table has no UNIQUE constraint on `id`, | ||
| * so a plain INSERT that the API layer retries after a transient 5xx (the | ||
| * request committed but the gateway returned 502/503) creates a duplicate row. | ||
| * The `WHERE NOT EXISTS` guard makes the re-send a no-op instead — verified | ||
| * lag-safe against the real backend even when the retry fires inside the | ||
| * documented ~5s read-your-writes window (see probe results / PR notes). | ||
| * | ||
| * The column list starts with `id, path, filename, message,` so the query is | ||
| * still recognised by isSessionInsertQuery() in deeplake-api.ts (which enables | ||
| * the transient-403 retry path for session writes). | ||
| */ | ||
| export function buildDirectSessionInsertSql(sessionsTable: string, p: DirectSessionInsertParams): string { | ||
| const table = sqlIdent(sessionsTable); | ||
| const id = sqlStr(p.id); | ||
| return ( | ||
| `INSERT INTO "${table}" (id, path, filename, message, message_embedding, author, size_bytes, project, description, agent, plugin_version, creation_date, last_update_date) ` + | ||
| `SELECT '${id}', '${sqlStr(p.sessionPath)}', '${sqlStr(p.filename)}', '${p.jsonForSql}'::jsonb, ${p.embeddingSql}, '${sqlStr(p.userName)}', ` + | ||
| `${p.sizeBytes}, '${sqlStr(p.projectName)}', '${sqlStr(p.description)}', '${sqlStr(p.agent)}', '${sqlStr(p.pluginVersion)}', '${sqlStr(p.timestamp)}', '${sqlStr(p.timestamp)}' ` + | ||
| `WHERE NOT EXISTS (SELECT 1 FROM "${table}" WHERE id = '${id}')` | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.