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
+38
View File
@@ -27,6 +27,7 @@ import { DayAssignments } from '../../src/db/entities/DayAssignments.entity';
import { Places } from '../../src/db/entities/Places.entity';
import { Reservations } from '../../src/db/entities/Reservations.entity';
import { Trips } from '../../src/db/entities/Trips.entity';
import { dbNow } from '../../src/db/types';
import { TURNS_PER_MINUTE } from '../../src/nest/assistant/assistant-tools.constants';
import { AssistantToolsService } from '../../src/nest/assistant/assistant-tools.service';
import { AssistantCardContext } from '../../src/nest/assistant/cards/card-context';
@@ -896,6 +897,43 @@ describe('Trip assistant e2e', () => {
expect(provider.requests).toHaveLength(2);
});
it('ASTE2E-025: a change left waiting past its day holds nothing back, and the next one is decided alone', async () => {
const t = await travelerWithTrip();
const [day] = await readTripDays(orm, t.tripId);
const { threadId } = await searchTurn(t);
const path = `${t.base}/threads/${threadId}/found-places`;
// The newest change the answer holds: a found place joins the answer of its search.
const changeOf = (res: { body: { message: { parts: AssistantMessagePart[] } } }) =>
res.body.message.parts
.flatMap((part) => (part.type === 'data-trek-change' ? [part.data.toolCallId] : []))
.at(-1) ?? '';
const stale = changeOf(await addFound(path, t.userId, { ref: 'call_find:0', dayId: day.id }));
// A day and an hour old: the thread shows it expired, and the nightly sweep has not run yet.
await updateRows(
orm,
AssistantToolCalls,
{ id: stale },
{ created_at: dbNow(new Date(Date.now() - 25 * 3_600_000)) },
);
const shown = await get(`${t.base}/threads/${threadId}`, t.userId);
const parts = (shown.body.messages as Array<{ parts: AssistantMessagePart[] }>).flatMap((m) => m.parts);
expect(parts.find((part) => part.type === 'tool-call' && part.toolCallId === stale)).toMatchObject({
approval: { resolution: 'expired' },
});
const next = await addFound(path, t.userId, { ref: 'call_find:1', dayId: day.id });
expect(next.status).toBe(200);
const fresh = changeOf(next);
expect(fresh).not.toBe(stale);
const decided = await post(`${t.base}/threads/${threadId}/decisions`, t.userId, {
...CTX,
decisions: [{ toolCallId: fresh, approved: true }],
});
expect(decided.status).toBe(200);
expect(ofType(decided.chunks, 'data-trek-applied')[0].data).toMatchObject({ toolCallId: fresh });
expect((await findRow(orm, AssistantToolCalls, { id: stale }))?.state).toBe('approval_requested');
});
it('ASTE2E-024: "+ Day" asks for the right to edit places: 403 and nothing held without it, 404 outside the trip', async () => {
const t = await travelerWithTrip();
const { user: member } = await makeUser(orm);
@@ -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');