refactor(db): move run-deliverable detection out of raw db.queryAll
P2-37: automation-run-completion.checkRunProducedDeliverable issued 5 raw SQLs via db.queryAll. Consolidate into db.runProducedDeliverable so schema changes to knowledge_base / documents / task_comments surface in the knowledge domain module rather than silently breaking at the caller.
This commit is contained in:
1 parent
b2eed3e75a
commit
d4c0d0a6bc
2 files changed
+43
-35
No files matched your search
@@ -172,6 +172,48 @@ export function buildKnowledgeMethods(
|
||||
getDb().prepare("delete from sprints where id = ? and company_id = ?").run(id, companyId);
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns true when the given run caused a durable deliverable — a
|
||||
* knowledge entry, a document, or a substantial task comment (>100
|
||||
* chars). Causal binding via source_run_id is preferred; time-window
|
||||
* fallback handles legacy rows written before that column existed.
|
||||
*
|
||||
* Encapsulates the five raw SELECTs that automation-run-completion
|
||||
* previously issued via db.queryAll, so schema changes to knowledge_base
|
||||
* / documents / task_comments surface here rather than silently.
|
||||
*/
|
||||
runProducedDeliverable(runId: string, companyId: string, taskId: string): boolean {
|
||||
const causalKnowledge = readAll<{ id: string }>(
|
||||
"select id from knowledge_base where company_id = ? and source_run_id = ? limit 1",
|
||||
companyId, runId,
|
||||
);
|
||||
if (causalKnowledge.length > 0) return true;
|
||||
|
||||
const causalDocs = readAll<{ id: string }>(
|
||||
"select id from documents where company_id = ? and source_run_id = ? limit 1",
|
||||
companyId, runId,
|
||||
);
|
||||
if (causalDocs.length > 0) return true;
|
||||
|
||||
const legacyKnowledge = readAll<{ id: string }>(
|
||||
"select id from knowledge_base where company_id = ? and source_run_id is null and created_at >= (select started_at from runs where id = ?) limit 1",
|
||||
companyId, runId,
|
||||
);
|
||||
if (legacyKnowledge.length > 0) return true;
|
||||
|
||||
const legacyDocs = readAll<{ id: string }>(
|
||||
"select id from documents where company_id = ? and source_run_id is null and created_at >= (select started_at from runs where id = ?) limit 1",
|
||||
companyId, runId,
|
||||
);
|
||||
if (legacyDocs.length > 0) return true;
|
||||
|
||||
const recentComments = readAll<{ body: string }>(
|
||||
"select body from task_comments where task_id = ? and created_at >= (select started_at from runs where id = ?) and length(body) > 100",
|
||||
taskId, runId,
|
||||
);
|
||||
return recentComments.length > 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns all sprints for a company, ordered by start_date desc.
|
||||
* Used by GET /api/companies/:companyId/sprints.
|
||||
|
||||
Reference in new issue
Block a user