Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(ai-research): read the winning ML key row when a refusal omits it (#102306)

Robbie746ef3a6dfad
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/dynamodb.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/dynamodb.ts
index 5ab7021060e9..8fe35fedce70 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/dynamodb.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/dynamodb.ts
@@ -194,7 +194,7 @@ export class MlKeyDynamoDB {
attributes: DynamoItem,
deadline?: AbortSignal
): Promise<DynamoItem | undefined> {
- return this.writeConcurrency(() =>
+ const outcome = await this.writeConcurrency(() =>
this.timed('dynamodb_put_if_absent', async () => {
try {
await this.client.send(
@@ -209,19 +209,25 @@ export class MlKeyDynamoDB {
)
// This row now exists with exactly these attributes, so the next batch needs no read for it.
this.hold(key, { ...encodeKey(key), ...attributes })
- return undefined
+ return { created: true, winner: undefined }
} catch (error) {
if (error instanceof ConditionalCheckFailedException) {
- const winner = error.Item
- if (winner) {
- this.hold(key, winner)
- }
- return winner ?? {}
+ return { created: false, winner: error.Item }
}
throw error
}
})
)
+ if (outcome.created) {
+ return undefined
+ }
+ if (outcome.winner) {
+ this.hold(key, outcome.winner)
+ return outcome.winner
+ }
+ // An endpoint that refuses the put without returning the row leaves the winner unknown. Reading it costs one
+ // request on a conflict and keeps the caller from reading an absent key as an unusable one.
+ return (await this.read([key], deadline)).get(tableKeyString(key)) ?? {}
}
public async put(key: TableKey, attributes: DynamoItem, deadline?: AbortSignal): Promise<void> {
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.test.ts
index 1de677055375..c621e7dac80b 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.test.ts
@@ -469,6 +469,28 @@ describe('ML session key batches', () => {
}
)
+ it('adopts a competing writer key when the refusal omits the stored row', async () => {
+ const send = boundary.send.bind(boundary)
+ jest.spyOn(boundary, 'send').mockImplementation((command) => {
+ if (command instanceof PutItemCommand) {
+ // An endpoint that does not implement ReturnValuesOnConditionCheckFailure refuses without the row.
+ delete (command.input as { ReturnValuesOnConditionCheckFailure?: string })
+ .ReturnValuesOnConditionCheckFailure
+ }
+ return send(command)
+ })
+ const first = await store.prepare([session])
+ const second = await store.prepare([session])
+ await first.commit()
+ jest.useFakeTimers()
+ const committing = second.commit()
+ await jest.runAllTimersAsync()
+ await committing
+ expect(second.get(session.teamId, session.sessionId)!.session.plaintext).toEqual(
+ first.get(session.teamId, session.sessionId)!.session.plaintext
+ )
+ })
+
it('adopts a competing writer key', async () => {
const first = await store.prepare([session])
const second = await store.prepare([session])