File size: 3,774 Bytes
d739b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// ์‹คํ–‰ ๊ด€์ธก ์„ค๋ช… ๋ฌธ์„œ: /docs/specs/bada-bong-agent-telemetry.md (v0.1.0)
// ๋ณ‘๋ ฌ ์ž์‹๋„ ์™ธ๋ถ€ ํšจ๊ณผ ์ „์— parent task์— ๋ฌถ์ธ ์˜์† ๊ธฐ๋ก์„ ๋งŒ๋“ค๊ณ  ๊ฒฐ๊ณผ์™€ checkpoint๋ฅผ ํ•จ๊ป˜ ํ™•์ •ํ•œ๋‹ค.
// ์„ค๋ช… ๋ฌธ์„œ: docs/specs/bong-browser-use.md (v1.11.0)
const db = require('../../knowledge-db'), { randomUUID } = require('crypto');
const uncertain = require('../utils/cli-storage-error').isUncertainCliFailure;
const error = code => Object.assign(Error('๋ณ‘๋ ฌ ์ž‘์—…์˜ ์‹คํ–‰ ๊ธฐ๋ก์„ ํ™•์ธํ•œ ๋’ค ๊ฐ™์€ ์‹คํ–‰์„ ๊ฒ€ํ† ํ•˜์„ธ์š”.'), { code, retryable: false });
const storage = () => error('WORKFLOW_PARALLEL_STORAGE_FAILED');
function prepare(run, parent, children) {
  // ์˜์† workflow๊ฐ€ ์—†๋Š” ๊ธฐ์กด ๋‹จ์œ„ ํ˜ธ์ถœ์—๋Š” ์ €์žฅ์†Œ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค.
  if (!run.runData?._badaWorkflowSnapshot) return async (child, execute) => execute();
  const parentTaskId = run.nodeTasks?.[parent.id], checkpointNodeId = run.currentNodeId;
  const assertActive = () => {
    if (run.abortController?.signal.aborted || run.status !== 'running') throw error('WORKFLOW_PARALLEL_RECONCILIATION_REQUIRED');
    const row = db.getWorkflowRun(run.runId), task = db.getWorkflowTask(parentTaskId);
    if (!row || row.status !== 'running' || row.current_node_id !== checkpointNodeId || !task || task.run_id !== run.runId || task.node_id !== parent.id || task.status !== 'running') throw storage();
  };
  try {
    db.runInTransaction(() => {
      assertActive();
      // ์ด์ „ ์ž์‹ ์‹œ๋„๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด ์ „์ฒด ๊ทธ๋ฃน ์ž๋™ retry๋กœ ์™ธ๋ถ€ ํšจ๊ณผ๋ฅผ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.
      if (db.getWorkflowTasks(run.runId).some(t => children.includes(t.node_id))) throw error('WORKFLOW_PARALLEL_RECONCILIATION_REQUIRED');
    });
  } catch (err) { throw uncertain(err) ? err : storage(); }
  return async (child, execute) => {
    const taskId = 'parallel_' + randomUUID(), payload = { ...(child.config || {}), _badaParallel: { version: 1, parentNodeId: parent.id, parentTaskId } };
    try {
      db.runInTransaction(() => {
        assertActive();
        if (db.getWorkflowTasks(run.runId).some(t => t.node_id === child.id)) throw error('WORKFLOW_PARALLEL_RECONCILIATION_REQUIRED');
        db.enqueueWorkflowTask(taskId, run.runId, child.id, child.config?.agentProfileId || child.config?.agentId || child.type, payload);
        db.completeWorkflowTask(taskId, 'running', null, null);
      });
    } catch (err) { throw uncertain(err) ? err : storage(); }
    run.nodeTasks[child.id] = taskId;
    try {
      require('../services/agent-telemetry-workflow').node(run,child,'running');
      const output = await require('./node-context').withNode(run, child.id, taskId, execute);
      const present = Object.hasOwn(run.runData, child.id), previous = run.runData[child.id];
      try {
        db.runInTransaction(() => {
          assertActive();
          run.runData[child.id] = { status: 'success', output };
          db.completeWorkflowTask(taskId, 'completed', output, null);
          db.checkpointWorkflowRun(run.runId, 'running', checkpointNodeId, run.runData);
        });
      } catch (err) {
        if (present) run.runData[child.id] = previous; else delete run.runData[child.id];
        throw uncertain(err) ? err : storage();
      }
      require('../services/agent-telemetry-workflow').node(run,child,'completed');
      return output;
    } catch (err) {
      require('../services/agent-telemetry-workflow').node(run,child,run.abortController?.signal.aborted?'cancelled':'failed');
      try { db.runInTransaction(() => db.completeWorkflowTask(taskId, 'failed', null, uncertain(err) ? err.code : err.message)); }
      catch { throw storage(); }
      throw err;
    }
  };
}
module.exports = { prepare, error };