Skip to content

Commit 379c44c

Browse files
committed
fix(build-notes): attach a first baseline file once and keep a rebase's evaluation honest
1 parent fab1188 commit 379c44c

4 files changed

Lines changed: 99 additions & 37 deletions

File tree

‎apps/sim/lib/build-notes/application/build-notes.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,24 +365,25 @@ export const getBuildNotesChanges = defineAuthorizedWorkflowUseCase({
365365

366366
/**
367367
* Records the current graph as the notes' baseline, after the agent reconciled the notes with
368-
* edits made outside it. The evaluation is spent: the notes describe a different graph now.
368+
* edits made outside it. Recording it withdraws the evaluation: the notes describe a different
369+
* graph now. When a newer baseline was recorded meanwhile, that one stays, and so does any
370+
* evaluation made against it.
369371
*/
370372
export const rebaseBuildNotes = defineAuthorizedWorkflowUseCase({
371373
operation: buildNotesOperations.rebase,
372374
resolveContext,
373375
authorizeResource,
374376
async execute({ principal, context }) {
375377
const notes = await requireNotes(context)
376-
const baseline = await loadCurrentBaselineState(context.workflowId)
377-
await writeBaseline(notes, baseline, {
378-
userId: requirePrincipalSubjectUserId(principal),
379-
source: 'copilot',
380-
})
381-
await clearBuildNotesEvaluation(notes.id)
378+
const recorded = await writeBaseline(
379+
notes,
380+
await loadCurrentBaselineState(context.workflowId),
381+
{ userId: requirePrincipalSubjectUserId(principal), source: 'copilot' }
382+
)
382383
return {
383384
workflowId: context.workflowId,
384385
baseline: buildNotesPath(context.workflowId, 'baseline'),
385-
recordedAt: baseline.workflowUpdatedAt.toISOString(),
386+
recordedAt: recorded.baselineWorkflowUpdatedAt?.toISOString() ?? null,
386387
}
387388
},
388389
})

‎apps/sim/lib/build-notes/build-notes.integration.ts‎

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -655,15 +655,63 @@ check('notes created while their workflow is archived roll back with their files
655655
expect(rows).toEqual([])
656656
})
657657

658-
check('a baseline read before a newer one was recorded never replaces it', async () => {
659-
const { workflowId, startId } = await seedWorkflow()
658+
check(
659+
'a baseline read before a newer one was recorded never replaces it, nor its evaluation',
660+
async () => {
661+
const { workflowId, startId } = await seedWorkflow()
662+
await createNotes(workflowId)
663+
const stale = await loadCurrentBaselineState(workflowId)
664+
await apply(person, workflowId, functionBatch(startId, 'later'))
665+
await rebaseBuildNotes.execute({ principal: agent, input: target(workflowId) })
666+
const rebased = await notesRow(workflowId)
667+
await recordBuildNotesEvaluation(
668+
rebased.id,
669+
{ evaluatedNotesHash: 'notes', evaluatedOperationsHash: 'batch' },
670+
{ baselineWorkflowUpdatedAt: rebased.baselineWorkflowUpdatedAt }
671+
)
672+
673+
await writeBaseline(rebased, stale, { userId: ownerId, source: 'copilot' })
674+
await expect(changesSince(workflowId)).resolves.toMatchObject({ hasChanges: false })
675+
expect((await notesRow(workflowId)).evaluatedNotesHash).toBe('notes')
676+
}
677+
)
678+
679+
check('two writers of a first baseline attach one file and never orphan the other', async () => {
680+
const { workflowId } = await seedWorkflow()
660681
await createNotes(workflowId)
661-
const stale = await loadCurrentBaselineState(workflowId)
662-
await apply(person, workflowId, functionBatch(startId, 'later'))
663-
await rebaseBuildNotes.execute({ principal: agent, input: target(workflowId) })
682+
const created = await notesRow(workflowId)
683+
// As if the first baseline's commit had failed after the notes were created.
684+
await db
685+
.update(workflowBuildNotes)
686+
.set({ baselineFileId: null, baselineWorkflowUpdatedAt: null })
687+
.where(eq(workflowBuildNotes.id, created.id))
688+
const withoutBaseline = await notesRow(workflowId)
689+
const baselineFiles = async () =>
690+
(
691+
await db
692+
.select({ id: workspaceFiles.id })
693+
.from(workspaceFiles)
694+
.where(
695+
and(
696+
eq(workspaceFiles.workspaceId, workspaceId),
697+
eq(workspaceFiles.context, 'build_notes')
698+
)
699+
)
700+
).length
701+
const before = await baselineFiles()
702+
const snapshot = await loadCurrentBaselineState(workflowId)
664703

665-
await writeBaseline(await notesRow(workflowId), stale, { userId: ownerId, source: 'copilot' })
666-
await expect(changesSince(workflowId)).resolves.toMatchObject({ hasChanges: false })
704+
const first = await writeBaseline(withoutBaseline, snapshot, {
705+
userId: ownerId,
706+
source: 'copilot',
707+
})
708+
const second = await writeBaseline(withoutBaseline, snapshot, {
709+
userId: ownerId,
710+
source: 'copilot',
711+
})
712+
expect(second.baselineFileId).toBe(first.baselineFileId)
713+
expect((await notesRow(workflowId)).baselineFileId).toBe(first.baselineFileId)
714+
expect(await baselineFiles()).toBe(before + 1)
667715
})
668716

669717
check("restoring a deleted folder brings its workflows' notes and files back", async () => {

‎apps/sim/lib/build-notes/files.ts‎

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { buildNotesTemplate, MAX_BUILD_NOTES_BYTES } from '@/lib/build-notes/not
66
import { buildNotesFileName, buildNotesPath } from '@/lib/build-notes/paths'
77
import {
88
type BuildNotesRow,
9+
getLiveBuildNotesForWorkflow,
910
insertBuildNotesInTx,
1011
requireLiveWorkflowInTx,
1112
setBuildNotesBaseline,
@@ -86,26 +87,36 @@ export async function writeBaseline(
8687
)
8788
}
8889
if (!notes.baselineFileId) {
89-
const { fileId } = await createOwnedWorkspaceFile({
90-
context: 'build_notes',
91-
workspaceId: notes.workspaceId,
92-
userId: author.userId,
93-
content,
94-
contentType: 'application/json',
95-
fileName: () => buildNotesFileName(notes.workflowId, 'baseline'),
96-
insertOwner: async (tx, baselineFileId) => {
97-
await requireLiveWorkflowInTx(tx, notes.workflowId)
98-
await setBuildNotesBaseline(
99-
notes.id,
100-
{ baselineFileId, baselineWorkflowUpdatedAt: baseline.workflowUpdatedAt },
101-
tx
102-
)
103-
},
104-
})
105-
return {
106-
...notes,
107-
baselineFileId: fileId,
108-
baselineWorkflowUpdatedAt: baseline.workflowUpdatedAt,
90+
try {
91+
const { fileId } = await createOwnedWorkspaceFile({
92+
context: 'build_notes',
93+
workspaceId: notes.workspaceId,
94+
userId: author.userId,
95+
content,
96+
contentType: 'application/json',
97+
fileName: () => buildNotesFileName(notes.workflowId, 'baseline'),
98+
insertOwner: async (tx, baselineFileId) => {
99+
await requireLiveWorkflowInTx(tx, notes.workflowId)
100+
const recorded = await setBuildNotesBaseline(
101+
notes.id,
102+
{ baselineFileId, baselineWorkflowUpdatedAt: baseline.workflowUpdatedAt },
103+
tx
104+
)
105+
if (!recorded) throw new StaleBaselineError()
106+
},
107+
})
108+
return {
109+
...notes,
110+
baselineFileId: fileId,
111+
baselineWorkflowUpdatedAt: baseline.workflowUpdatedAt,
112+
}
113+
} catch (error) {
114+
if (!(error instanceof StaleBaselineError)) throw error
115+
// Another writer recorded the first baseline file; this file rolled back. Write through
116+
// that one instead, which still keeps only the newer of the two.
117+
const current = await getLiveBuildNotesForWorkflow(notes.workspaceId, notes.workflowId)
118+
if (!current?.baselineFileId) return current ?? notes
119+
return writeBaseline(current, baseline, author)
109120
}
110121
}
111122
try {
@@ -130,9 +141,9 @@ export async function writeBaseline(
130141
}
131142
)
132143
} catch (error) {
144+
if (!(error instanceof StaleBaselineError)) throw error
133145
// A newer baseline already describes a later graph; keeping it is the right outcome.
134-
if (error instanceof StaleBaselineError) return notes
135-
throw error
146+
return (await getLiveBuildNotesForWorkflow(notes.workspaceId, notes.workflowId)) ?? notes
136147
}
137148
return { ...notes, baselineWorkflowUpdatedAt: baseline.workflowUpdatedAt }
138149
}

‎apps/sim/lib/build-notes/repository.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export async function setBuildNotesBaseline(
8787
.where(
8888
and(
8989
eq(workflowBuildNotes.id, notesId),
90+
// A baseline file is attached once; later baselines write through it.
91+
baseline.baselineFileId ? isNull(workflowBuildNotes.baselineFileId) : undefined,
9092
or(
9193
isNull(workflowBuildNotes.baselineWorkflowUpdatedAt),
9294
lte(workflowBuildNotes.baselineWorkflowUpdatedAt, baseline.baselineWorkflowUpdatedAt)

0 commit comments

Comments
 (0)