Repository navigation
chore: backfill tfg.source=ui metadata on existing interactive sessions #1146
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
Open
hary73
wants to merge
8
commits into
main
Choose a base branch
from
hary/chat-history
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b712b5a
chore: backfill tfy.source=ui metadata on existing interactive sessions
hary73 847097c
chore: drop backfill migration tests
hary73 eed57fa
chore: rename backfill metadata key to tfg.source
hary73 8d8a71d
chore: add partial index for Chat History tfg.source listing
hary73 805ab35
chore: composite tfg.source list index and per-key metadata conditions
hary73 2e91752
cleanup comments
hary73 46c5bcd
chore: mirror tfg.source list index in SQLite
hary73 ee970ea
chore: put updated_at before tfg.source in list index; inline metadat…
hary73 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@truefoundry/trueforge": patch | ||
| --- | ||
|
|
||
| Backfill `tfg.source: ui` session metadata on existing non-scheduled sessions so they stay in Chat History once it filters on that key. |
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
26 changes: 26 additions & 0 deletions
26
packages/trueforge/src/db/postgres/migrations/20261022_000001_session_tfg_source_backfill.ts
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,26 @@ | ||
| import { sql, type Kysely } from 'kysely'; | ||
|
|
||
| /** | ||
| * Stamp `metadata["tfg.source"] = "ui"` on pre-existing interactive sessions so Chat History, | ||
| * which now lists only sessions carrying that key, keeps showing them. | ||
| * | ||
| * Schedule runs are the only sessions with `external_id`; checking it alongside `source` also | ||
| * skips schedule runs created before `source` existed. `updated_at` is left untouched so list | ||
| * ordering does not change. | ||
| */ | ||
| export async function up<TDatabase>(db: Kysely<TDatabase>): Promise<void> { | ||
| await sql`SET LOCAL lock_timeout = '5s'`.execute(db); | ||
| await sql` | ||
| UPDATE session | ||
| SET metadata = metadata || jsonb_build_object('tfg.source', 'ui') | ||
| WHERE source IS NULL | ||
| AND external_id IS NULL | ||
| AND metadata->'tfg.source' IS NULL | ||
| `.execute(db); | ||
| } | ||
|
|
||
| /** Data backfill — not reversed: backfilled rows are indistinguishable from UI-stamped ones. */ | ||
| export function down(db: Kysely<unknown>): Promise<void> { | ||
| void db; | ||
| return Promise.reject(new Error('20261022_000001_session_tfg_source_backfill is irreversible')); | ||
| } |
25 changes: 25 additions & 0 deletions
25
packages/trueforge/src/db/postgres/migrations/20261022_000002_session_tfg_source_list_idx.ts
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,25 @@ | ||
| import { sql, type Kysely } from 'kysely'; | ||
| import { SESSION_TFG_SOURCE_LIST_IDX } from '../../indexes'; | ||
|
|
||
| /** | ||
| * A caller's sessions in keyset order, so their newest-first listing needs no sort with or without | ||
| * a `tfg.source` filter; the filter is checked on index entries without visiting the table. | ||
| */ | ||
| export async function up(db: Kysely<unknown>): Promise<void> { | ||
| await sql`SET LOCAL lock_timeout = '5s'`.execute(db); | ||
| await sql` | ||
| CREATE INDEX ${sql.raw(SESSION_TFG_SOURCE_LIST_IDX)} | ||
| ON session ( | ||
| tenant_id, | ||
| (created_by_subject->>'subject_id'), | ||
| updated_at, | ||
| session_id, | ||
| (metadata->>'tfg.source') | ||
| ) | ||
| `.execute(db); | ||
| } | ||
|
|
||
| export async function down(db: Kysely<unknown>): Promise<void> { | ||
| await sql`SET LOCAL lock_timeout = '5s'`.execute(db); | ||
| await sql`DROP INDEX IF EXISTS ${sql.raw(SESSION_TFG_SOURCE_LIST_IDX)}`.execute(db); | ||
| } | ||
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
22 changes: 22 additions & 0 deletions
22
packages/trueforge/src/db/sqlite/migrations/20261022_000001_session_tfg_source_backfill.ts
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,22 @@ | ||
| import { sql, type Kysely } from 'kysely'; | ||
|
|
||
| /** | ||
| * SQLite mirror of postgres/migrations/20261022_000001_session_tfg_source_backfill.ts. | ||
| */ | ||
| export async function up<TDatabase>(db: Kysely<TDatabase>): Promise<void> { | ||
| await db.transaction().execute(async trx => { | ||
| await sql` | ||
| UPDATE session | ||
| SET metadata = jsonb_set(metadata, '$."tfg.source"', 'ui') | ||
| WHERE source IS NULL | ||
| AND external_id IS NULL | ||
| AND json_extract(metadata, '$."tfg.source"') IS NULL | ||
| `.execute(trx); | ||
| }); | ||
| } | ||
|
|
||
| /** Data backfill — not reversed: backfilled rows are indistinguishable from UI-stamped ones. */ | ||
| export function down(db: Kysely<unknown>): Promise<void> { | ||
| void db; | ||
| return Promise.reject(new Error('20261022_000001_session_tfg_source_backfill is irreversible')); | ||
| } |
27 changes: 27 additions & 0 deletions
27
packages/trueforge/src/db/sqlite/migrations/20261022_000002_session_tfg_source_list_idx.ts
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,27 @@ | ||
| import { sql, type Kysely } from 'kysely'; | ||
| import { SESSION_TFG_SOURCE_LIST_IDX } from '../../indexes'; | ||
|
|
||
| /** | ||
| * SQLite mirror of postgres/migrations/20261022_000002_session_tfg_source_list_idx.ts. | ||
| * The key contains a dot, so the JSON path must quote it. | ||
| */ | ||
| export async function up(db: Kysely<unknown>): Promise<void> { | ||
| await db.transaction().execute(async trx => { | ||
| await sql` | ||
| CREATE INDEX ${sql.raw(SESSION_TFG_SOURCE_LIST_IDX)} | ||
| ON session ( | ||
| tenant_id, | ||
| json_extract(created_by_subject, '$.subject_id'), | ||
| updated_at, | ||
| session_id, | ||
| json_extract(metadata, '$."tfg.source"') | ||
| ) | ||
| `.execute(trx); | ||
| }); | ||
| } | ||
|
|
||
| export async function down(db: Kysely<unknown>): Promise<void> { | ||
| await db.transaction().execute(async trx => { | ||
| await sql`DROP INDEX IF EXISTS ${sql.raw(SESSION_TFG_SOURCE_LIST_IDX)}`.execute(trx); | ||
| }); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(metadata->>'tfg.source'), I expect people to query by time range always with optionally metadata filteringThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, when listing we order by
updated_atDESCthe start / end params in the list API filter on
created_at, notupdated_at- so moving it up won't help for time range queries. We already have an index for supporting these queriessession_list_idx (tenant_id, created_at, session_id)But even if we don't supported range queries on
updated_at, it makes sense to moveupdated_atabove in the case where a user would just want to see their recent sessions. The current index performs better on the chat view for users which have a lot of scheduled sessions, but movingupdated_atabove will create a more generic index.