fix(assistant): a change left waiting past its day no longer holds the next one back

The open changes of a thread are now the ones still inside their decision
window. A call older than a day already reads as expired in the thread and
can no longer be decided, but it stayed approval_requested until the nightly
sweep, so "+ Tour", "+ Day" and "Plan this" answered decide_first with
nothing left to decide, and a decision had to name the expired call too.
listOpen takes the window's start, and every caller passes the same cutoff.
This commit is contained in:
Maurice
2026-10-24 01:21:18 +00:00
parent a6a9f3bbdd
commit bfcc205f05
10 changed files with 100 additions and 21 deletions
@@ -324,7 +324,9 @@ describe('proposed changes', () => {
const { threadId, messageId } = await threadWithMessage();
await calls.insertProposals([proposal('c1', threadId, messageId), proposal('c2', threadId, messageId)]);
await calls.insertProposals([]);
expect((await calls.listOpen(threadId)).map((row) => row.id)).toEqual(['c1', 'c2']);
expect((await calls.listOpen(threadId, AT)).map((row) => row.id)).toEqual(['c1', 'c2']);
// Proposed before `notBefore`, a change waits no more, though no sweep has marked it expired yet.
expect(await calls.listOpen(threadId, LATER)).toEqual([]);
expect(await calls.claimDecision('c1', threadId + 1, true, AT, LATER)).toBe(false);
expect(await calls.claimDecision('c1', threadId, true, '2026-10-21 00:00:00', LATER)).toBe(false);
@@ -3,7 +3,7 @@
* call repositories on a migrated test db: the place comes from the search
* results stored in the thread, the day must be one of the trip's, and the
* suggestion is held as a change card in one transaction, refused while a turn
* runs, while another change waits, or past the per-minute limit.
* runs, while another change waits within its day, or past the per-minute limit.
*
* Replaced: the trip tools (what the trip reads as), the rate limit, the
* stream registry and the airports.
@@ -11,6 +11,7 @@
import { AssistantMessages } from '../../../../src/db/entities/AssistantMessages.entity';
import { AssistantThreads } from '../../../../src/db/entities/AssistantThreads.entity';
import { AssistantToolCalls } from '../../../../src/db/entities/AssistantToolCalls.entity';
import { dbNow } from '../../../../src/db/types';
import { AirportsService } from '../../../../src/nest/airports/airports.service';
import { AssistantFoundPlacesService } from '../../../../src/nest/assistant/assistant-found-places.service';
import { AssistantStreamRegistry } from '../../../../src/nest/assistant/assistant-stream.registry';
@@ -19,7 +20,7 @@ import { AssistantCardContext } from '../../../../src/nest/assistant/cards/card-
import { RateLimitService } from '../../../../src/nest/common/rate-limit.service';
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createRow, findRows } from '../../../helpers/factories/rows';
import { createRow, findRows, updateRows } from '../../../helpers/factories/rows';
import { makeTrip } from '../../../helpers/factories/trips';
import { makeUser } from '../../../helpers/factories/users';
import { resetTestDb } from '../../../helpers/test-db';
@@ -182,6 +183,19 @@ describe('AssistantFoundPlacesService', () => {
expect(await calls(c.threadId)).toHaveLength(1);
});
it('ASTFND-008: a change left waiting past its day holds nothing back, before the nightly sweep marks it', async () => {
const c = await conversation();
await add(c, 's1:0');
const [stale] = await calls(c.threadId);
const dayAndHourAgo = dbNow(new Date(Date.now() - 25 * 60 * 60_000));
await updateRows(t.get(MikroORM), AssistantToolCalls, { id: stale?.id ?? '' }, { created_at: dayAndHourAgo });
await add(c, 's1:1');
const held = await calls(c.threadId);
expect(held).toHaveLength(2);
expect(held.map((call) => call.state)).toEqual(['approval_requested', 'approval_requested']);
});
it('ASTFND-007: a ref outside the search results or a day of another trip proposes nothing', async () => {
const c = await conversation();
await expect(add(c, 's9:0')).rejects.toMatchObject({ status: 400, code: 'generic' });
@@ -3,7 +3,7 @@
* and tool call repositories on a migrated test db: the proposal comes from
* the call and card stored in the thread, never from the request, and the
* change that creates it is held as a card in one transaction, refused while
* a turn runs, while another change waits, for a proposal the thread does not
* a turn runs, while another change waits within its day, for a proposal the thread does not
* hold and for one nothing can be created from. A walk is asked again, since
* the stored answer is the model's shortened copy.
*
@@ -13,6 +13,7 @@
import { AssistantMessages } from '../../../../src/db/entities/AssistantMessages.entity';
import { AssistantThreads } from '../../../../src/db/entities/AssistantThreads.entity';
import { AssistantToolCalls } from '../../../../src/db/entities/AssistantToolCalls.entity';
import { dbNow } from '../../../../src/db/types';
import { AirportsService } from '../../../../src/nest/airports/airports.service';
import {
AssistantProposalsService,
@@ -27,7 +28,7 @@ import { RateLimitService } from '../../../../src/nest/common/rate-limit.service
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
import { tourRouteCard } from '../../../helpers/assistant-tour-cards';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createRow, findRows } from '../../../helpers/factories/rows';
import { createRow, findRows, updateRows } from '../../../helpers/factories/rows';
import { makeTrip } from '../../../helpers/factories/trips';
import { makeUser } from '../../../helpers/factories/users';
import { resetTestDb } from '../../../helpers/test-db';
@@ -226,6 +227,19 @@ describe('AssistantProposalsService', () => {
expect(await calls(c.threadId)).toHaveLength(1);
});
it('ASTPROP-011: a change left waiting past its day holds nothing back, before the nightly sweep marks it', async () => {
const c = await conversation();
await take(c, 'h1');
const [stale] = await calls(c.threadId);
const dayAndHourAgo = dbNow(new Date(Date.now() - 25 * 60 * 60_000));
await updateRows(t.get(MikroORM), AssistantToolCalls, { id: stale?.id ?? '' }, { created_at: dayAndHourAgo });
await take(c, 'w1');
const held = await calls(c.threadId);
expect(held).toHaveLength(2);
expect(held.map((call) => call.state)).toEqual(['approval_requested', 'approval_requested']);
});
it('ASTPROP-008: a walk stored cut down and masked is created from the tool asked again, named and on no day', async () => {
const c = await conversation();
await take(c, 'w1');