mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-21 15:05:56 +00:00
fix(db): read, merge and write a reasoning rule update in one tick
update() awaited findById() between reading the row and running the UPDATE, so two overlapping updates of the same rule could both read the same base row and the second write dropped the first caller's fields. Read the row synchronously so the read, merge, reference check and write cannot interleave.
This commit is contained in:
committed by
diegosouzapw
parent
8d1f89789a
commit
d720f0e6d8
@@ -48,7 +48,7 @@ export async function getReasoningRoutingRules(
|
||||
): Promise<ReasoningRoutingRule[]> {
|
||||
while (pendingMutations.size || !cache || cache.version !== version) {
|
||||
if (pendingMutations.size) {
|
||||
// Update already yields before writing; do not read a pre-write snapshot.
|
||||
// A backend may yield before writing; do not read a pre-write snapshot.
|
||||
await Promise.allSettled([...pendingMutations]);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -100,13 +100,17 @@ async function list(options: { enabledOnly?: boolean } = {}): Promise<ReasoningR
|
||||
return options.enabledOnly ? rules.filter((rule) => rule.enabled) : rules;
|
||||
}
|
||||
|
||||
async function findById(id: string): Promise<ReasoningRoutingRule | null> {
|
||||
function findRuleSync(id: string): ReasoningRoutingRule | null {
|
||||
const row = getDbInstance()
|
||||
.prepare("SELECT * FROM reasoning_routing_rules WHERE id = ?")
|
||||
.get(id) as RuleRow | undefined;
|
||||
return row ? rowToRule(row) : null;
|
||||
}
|
||||
|
||||
async function findById(id: string): Promise<ReasoningRoutingRule | null> {
|
||||
return findRuleSync(id);
|
||||
}
|
||||
|
||||
function ruleParams(
|
||||
rule: ReasoningRoutingRuleInput,
|
||||
id: string,
|
||||
@@ -163,7 +167,9 @@ async function update(
|
||||
id: string,
|
||||
patch: Partial<ReasoningRoutingRuleInput>
|
||||
): Promise<ReasoningRoutingRule | null> {
|
||||
const existing = await findById(id);
|
||||
// Read, merge and write in one tick: an await here would let an overlapping update read the
|
||||
// same base row and overwrite this patch with stale fields.
|
||||
const existing = findRuleSync(id);
|
||||
if (!existing) return null;
|
||||
const { id: _id, createdAt, updatedAt: _updatedAt, ...base } = existing;
|
||||
const next = { ...base, ...patch } as ReasoningRoutingRuleInput;
|
||||
@@ -181,7 +187,7 @@ async function update(
|
||||
WHERE id=@id`
|
||||
)
|
||||
.run(ruleParams(next, id, createdAt, now));
|
||||
return findById(id);
|
||||
return findRuleSync(id);
|
||||
}
|
||||
|
||||
async function deleteById(id: string): Promise<boolean> {
|
||||
|
||||
@@ -140,6 +140,17 @@ test("repository rejects missing references on create and merged update without
|
||||
}
|
||||
});
|
||||
|
||||
test("overlapping updates of one rule keep both patches", async () => {
|
||||
const created = await repository.create(ruleInput());
|
||||
await Promise.all([
|
||||
repository.update(created.id, { name: "Renamed rule" }),
|
||||
repository.update(created.id, { priority: 7 }),
|
||||
]);
|
||||
const stored = await repository.findById(created.id);
|
||||
assert.equal(stored?.name, "Renamed rule");
|
||||
assert.equal(stored?.priority, 7);
|
||||
});
|
||||
|
||||
test("retained repository survives reset/reopen and replacement at the same DATA_DIR", async () => {
|
||||
const created = await repository.create(ruleInput());
|
||||
core.resetDbInstance();
|
||||
|
||||
Reference in New Issue
Block a user