Skip to content

Commit f712ccf

Browse files
committed
fix(file-search): keep killed-build stops terminal and lock order consistent
- a re-rendered artifact no longer revives a revision stopped by killed builds or resets its count - the reaper no longer touches the abandoned build, keeping cleanup's build-then-revision order - the stop update leaves the handoff column alone; correct the budget comment's figures
1 parent 6dbfbe1 commit f712ccf

5 files changed

Lines changed: 96 additions & 14 deletions

File tree

‎apps/sim/lib/file-parsers/zip-guard.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ export const DEFAULT_OOXML_SIZE_LIMITS: OoxmlSizeLimits = {
8888
* XML node before any caller's budget runs, at roughly 1 KB resident per markup token. A 184 KB
8989
* .docx holding 60 MB of one-character paragraphs (11M tokens) took a 4 GB worker past 7 GB;
9090
* 1.5M tokens peaks near 2 GB. Realistic prose carries about one token per 37 bytes, so the budget
91-
* admits ~55 MB of it, more than the per-entry ceiling already allows.
91+
* admits ~55 MB of it, close to the 64 MiB per-entry ceiling.
9292
*
9393
* Every entry is charged, media included: mammoth parses a part as XML whatever bytes precede its
9494
* root element (a PNG signature included), so no sniff of an entry's content or name can tell which
9595
* parts it will expand. Compressed media holds about one `<` per 256 bytes, so even the total
96-
* ceiling's worth of it costs well under half the budget.
96+
* ceiling's worth of it (150 MiB) costs about 40% of the budget.
9797
*/
9898
export const WORDPROCESSING_OOXML_LIMITS: OoxmlSizeLimits = {
9999
...DEFAULT_OOXML_SIZE_LIMITS,

‎apps/sim/lib/workspace-files/search/artifact-ready.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
import { and, eq, inArray, or, sql } from 'drizzle-orm'
88
import type { DbTransaction } from '@/lib/db/types'
99
import type { EditableFileOwner } from '@/lib/workspace-files/ownership'
10-
import { FILE_SEARCH_ARTIFACT_READY_LOCK_TIMEOUT_MS } from '@/lib/workspace-files/search/constants'
10+
import {
11+
FILE_SEARCH_ARTIFACT_READY_LOCK_TIMEOUT_MS,
12+
FILE_SEARCH_BUILD_ABANDONED_REASON,
13+
} from '@/lib/workspace-files/search/constants'
1114
import { lockFileSearchInputs } from '@/lib/workspace-files/search/index-state'
1215
import { lockFileSearchOwners } from '@/lib/workspace-files/search/owner-policy'
1316
import {
@@ -118,6 +121,10 @@ export async function markFileSearchArtifactReadyInTx(
118121
if (!(await lockFileSearchInputs(tx, revision, input.dependencies, input.file.key))) return false
119122
const predicate = artifactRevisionCondition(input, owner)
120123
const [state] = await tx.select().from(workspaceFileSearchRevision).where(predicate).limit(1)
124+
// A revision stopped after repeated killed builds stays stopped until its own content changes,
125+
// which replaces the row: rendering the same source again must not hand it more attempts.
126+
if (state?.status === 'failed' && state.failureReason === FILE_SEARCH_BUILD_ABANDONED_REASON)
127+
return false
121128
if (state?.buildId) {
122129
const [build] = await tx
123130
.select({
@@ -154,8 +161,6 @@ export async function markFileSearchArtifactReadyInTx(
154161
buildId: null,
155162
dispatchedAt: null,
156163
handoffExpiresAt: null,
157-
lastBuildClaim: null,
158-
abandonedBuilds: 0,
159164
failureReason: null,
160165
lineCount: 0,
161166
indexedBytes: 0,

‎apps/sim/lib/workspace-files/search/constants.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ export const FILE_SEARCH_INDEX_STALE_DISPATCH_MS = 6 * 60 * 60 * 1000
120120
* also kills a healthy build.
121121
*/
122122
export const FILE_SEARCH_INDEX_MAX_ABANDONED_BUILDS = 2
123+
/** Failure reason of a revision stopped at {@link FILE_SEARCH_INDEX_MAX_ABANDONED_BUILDS}. */
124+
export const FILE_SEARCH_BUILD_ABANDONED_REASON = 'build_abandoned'
123125
export const FILE_SEARCH_INDEX_STALE_REAP_LIMIT = 100
124126
export const FILE_SEARCH_BACKFILL_PAGE_SIZE = 1000
125127

‎apps/sim/lib/workspace-files/search/dispatcher.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import type { DbTransaction } from '@/lib/db/types'
3636
import type { EditableFileOwner } from '@/lib/workspace-files/ownership'
3737
import {
3838
FILE_SEARCH_BACKFILL_PAGE_SIZE,
39+
FILE_SEARCH_BUILD_ABANDONED_REASON,
3940
FILE_SEARCH_CLEANUP_BACKLOG_ROWS,
4041
FILE_SEARCH_DISPATCH_HANDOFF_MS,
4142
FILE_SEARCH_DISPATCH_LOCK_TIMEOUT_MS,
@@ -414,7 +415,6 @@ async function reapStaleClaims(
414415
sourceContentUpdatedAt: workspaceFileSearchRevision.sourceContentUpdatedAt,
415416
currentFileId: workspaceFiles.id,
416417
handoffExpired: sql<boolean>`coalesce(${workspaceFileSearchRevision.handoffExpiresAt} <= clock_timestamp(), false)`,
417-
buildId: workspaceFileSearchRevision.buildId,
418418
abandonedBuild: sql<boolean>`coalesce(${workspaceFileSearchRevision.lastBuildClaim} = ${workspaceFileSearchRevision.dispatchedAt}, false)`,
419419
abandonedBuilds: workspaceFileSearchRevision.abandonedBuilds,
420420
})
@@ -456,19 +456,14 @@ async function reapStaleClaims(
456456
}
457457
const exhaustedFilter = revisionFilter(exhausted)
458458
if (exhaustedFilter) {
459-
const buildIds = exhausted.flatMap((row) => (row.buildId ? [row.buildId] : []))
460-
if (buildIds.length > 0)
461-
await tx
462-
.update(workspaceFileSearchBuild)
463-
.set({ expiresAt: sql`clock_timestamp()` })
464-
.where(inArray(workspaceFileSearchBuild.id, buildIds))
459+
// The abandoned build outlived its lease long before the stale window, so cleanup reclaims it;
460+
// touching it here would take build after revision, against cleanup's build-then-revision order.
465461
await tx
466462
.update(workspaceFileSearchRevision)
467463
.set({
468464
status: 'failed',
469465
buildId: null,
470-
failureReason: 'build_abandoned',
471-
handoffExpiresAt: null,
466+
failureReason: FILE_SEARCH_BUILD_ABANDONED_REASON,
472467
abandonedBuilds: sql`${workspaceFileSearchRevision.abandonedBuilds} + 1`,
473468
updatedAt: now,
474469
})

‎apps/sim/lib/workspace-files/search/owners.integration.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ import { deleteUserAccount } from '@/lib/users/account-deletion'
6060
import { fetchAuthorizedServableWorkspaceFileBuffer } from '@/lib/workspace-files/application/fetch-servable-workspace-file-buffer'
6161
import { observeWorkspaceFileDelivery } from '@/lib/workspace-files/application/file-delivery-observer'
6262
import { searchWorkspaceFileContent } from '@/lib/workspace-files/application/search-workspace-file-content'
63+
import {
64+
FILE_SEARCH_BUILD_ABANDONED_REASON,
65+
FILE_SEARCH_INDEX_MAX_ABANDONED_BUILDS,
66+
} from '@/lib/workspace-files/search/constants'
6367
import { prepareWorkspaceFileSearchDispatch } from '@/lib/workspace-files/search/dispatcher'
6468
import {
6569
appendFileSearchChunks,
@@ -1108,6 +1112,82 @@ describe('owner-scoped indexed file search on PostgreSQL and stored bytes', () =
11081112
expect((await search(f, 'newrevisionneedle')).count).toBe(1)
11091113
}
11101114
)
1115+
check(
1116+
'keeps a revision stopped by killed builds stopped, and its count, when its artifact renders again',
1117+
async () => {
1118+
const f = await fixture()
1119+
const dependency = await create(f, 'before', { name: 'stopped-input.txt' })
1120+
const source = `getFileBase64('${dependency.file.id}')`
1121+
const generated = await create(f, source, { name: 'stopped.docx' })
1122+
const [dependencyRow] = await db
1123+
.select()
1124+
.from(workspaceFiles)
1125+
.where(eq(workspaceFiles.id, dependency.file.id))
1126+
const completion = {
1127+
owner: f.owner,
1128+
file: {
1129+
fileId: generated.file.id,
1130+
key: generated.file.key,
1131+
contentUpdatedAt: generated.file.contentUpdatedAt,
1132+
},
1133+
dependencies: [
1134+
{
1135+
fileId: dependency.file.id,
1136+
key: dependency.file.key,
1137+
sourceContentUpdatedAt: dependency.file.contentUpdatedAt,
1138+
},
1139+
],
1140+
artifactKey: compiledArtifactKey(
1141+
f.owner,
1142+
source,
1143+
'docx',
1144+
fileDocumentCacheIdentity(
1145+
source,
1146+
await requireDocxFormat(),
1147+
'text/plain',
1148+
fileDocumentInputIdentity(f.owner, [dependencyRow])
1149+
)
1150+
),
1151+
}
1152+
const { markFileSearchArtifactReadyInTx } = await import(
1153+
'@/lib/workspace-files/search/artifact-ready'
1154+
)
1155+
const revisionOf = async () =>
1156+
(
1157+
await db
1158+
.select()
1159+
.from(workspaceFileSearchRevision)
1160+
.where(eq(workspaceFileSearchRevision.fileId, generated.file.id))
1161+
)[0]
1162+
1163+
await db
1164+
.update(workspaceFileSearchRevision)
1165+
.set({ status: 'pending', buildId: null, dispatchedAt: null, abandonedBuilds: 1 })
1166+
.where(eq(workspaceFileSearchRevision.fileId, generated.file.id))
1167+
expect(await db.transaction((tx) => markFileSearchArtifactReadyInTx(tx, completion))).toBe(
1168+
true
1169+
)
1170+
expect(await revisionOf()).toMatchObject({ status: 'pending', abandonedBuilds: 1 })
1171+
1172+
await db
1173+
.update(workspaceFileSearchRevision)
1174+
.set({
1175+
status: 'failed',
1176+
buildId: null,
1177+
failureReason: FILE_SEARCH_BUILD_ABANDONED_REASON,
1178+
abandonedBuilds: FILE_SEARCH_INDEX_MAX_ABANDONED_BUILDS,
1179+
})
1180+
.where(eq(workspaceFileSearchRevision.fileId, generated.file.id))
1181+
expect(await db.transaction((tx) => markFileSearchArtifactReadyInTx(tx, completion))).toBe(
1182+
false
1183+
)
1184+
expect(await revisionOf()).toMatchObject({
1185+
status: 'failed',
1186+
failureReason: FILE_SEARCH_BUILD_ABANDONED_REASON,
1187+
abandonedBuilds: FILE_SEARCH_INDEX_MAX_ABANDONED_BUILDS,
1188+
})
1189+
}
1190+
)
11111191
check(
11121192
'fences an artifact completion queued behind a concurrent dependency revision',
11131193
async () => {

0 commit comments

Comments
 (0)