Skip to content

Commit ba7b46a

Browse files
committed
refactor(behaviors): reorganize workflow behavior modules
- Create index files for each behavior type (error, trigger, checkpoint, loop) - Extract common behavior file reading logic into shared reader module - Move handler logic into respective behavior modules - Update imports to use new module structure
1 parent 9762ef3 commit ba7b46a

File tree

17 files changed

+122
-91
lines changed

17 files changed

+122
-91
lines changed

src/workflows/behaviors/checkpoint/evaluator.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import * as fs from 'node:fs';
2-
import * as path from 'node:path';
31
import type { ModuleBehavior } from '../../templates/index.js';
4-
import type { BehaviorAction } from '../types.js';
2+
import { readBehaviorFile } from '../reader.js';
53

64
export interface CheckpointEvaluationOptions {
75
behavior?: ModuleBehavior;
@@ -18,22 +16,8 @@ export async function evaluateCheckpointBehavior(options: CheckpointEvaluationOp
1816
const { cwd } = options;
1917

2018
// Checkpoint is universal - any agent can write checkpoint to behavior.json
21-
// No need to check if step has checkpoint behavior configured
22-
23-
// Check for behavior file
24-
const behaviorFile = path.join(cwd, '.codemachine', 'memory', 'behavior.json');
25-
26-
// Read and parse behavior action
27-
let behaviorAction: BehaviorAction;
28-
try {
29-
const content = await fs.promises.readFile(behaviorFile, 'utf8');
30-
behaviorAction = JSON.parse(content);
31-
} catch (error) {
32-
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
33-
// No file = no special behavior, continue normally
34-
return null;
35-
}
36-
console.error(`Failed to parse behavior file: ${error instanceof Error ? error.message : String(error)}`);
19+
const behaviorAction = await readBehaviorFile(cwd);
20+
if (!behaviorAction) {
3721
return null;
3822
}
3923

File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Checkpoint Behavior Module
3+
*
4+
* Handles workflow pause/checkpoint signals for user intervention.
5+
*/
6+
7+
export * from './evaluator.js';
8+
export * from './handler.js';

src/workflows/behaviors/error/evaluator.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as fs from 'node:fs';
2-
import * as path from 'node:path';
3-
import type { BehaviorAction } from '../types.js';
1+
import { readBehaviorFile } from '../reader.js';
42

53
export interface ErrorEvaluationOptions {
64
output: string;
@@ -15,20 +13,8 @@ export interface ErrorEvaluationResult {
1513
export async function evaluateErrorBehavior(options: ErrorEvaluationOptions): Promise<ErrorEvaluationResult | null> {
1614
const { cwd } = options;
1715

18-
// Check for behavior file
19-
const behaviorFile = path.join(cwd, '.codemachine', 'memory', 'behavior.json');
20-
21-
// Read and parse behavior action
22-
let behaviorAction: BehaviorAction;
23-
try {
24-
const content = await fs.promises.readFile(behaviorFile, 'utf8');
25-
behaviorAction = JSON.parse(content);
26-
} catch (error) {
27-
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
28-
// No file = no special behavior, continue normally
29-
return null;
30-
}
31-
console.error(`Failed to parse behavior file: ${error instanceof Error ? error.message : String(error)}`);
16+
const behaviorAction = await readBehaviorFile(cwd);
17+
if (!behaviorAction) {
3218
return null;
3319
}
3420

File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Error Behavior Module
3+
*
4+
* Handles workflow termination on error signals.
5+
*/
6+
7+
export * from './evaluator.js';
8+
export * from './handler.js';

src/workflows/behaviors/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
/**
2+
* Behaviors Module
3+
*
4+
* Centralized exports for all workflow behavior handlers.
5+
*/
6+
17
export * from './types.js';
8+
export * from './reader.js';
29
export * from './skip.js';
3-
export * from './loop/controller.js';
4-
export * from './loop/evaluator.js';
5-
export * from './trigger/controller.js';
6-
export * from './trigger/evaluator.js';
7-
export * from './checkpoint/controller.js';
8-
export * from './checkpoint/evaluator.js';
10+
export * from './loop/index.js';
11+
export * from './trigger/index.js';
12+
export * from './checkpoint/index.js';
13+
export * from './error/index.js';

src/workflows/behaviors/loop/evaluator.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import * as fs from 'node:fs';
2-
import * as path from 'node:path';
31
import type { ModuleBehavior } from '../../templates/index.js';
4-
import type { BehaviorAction } from '../types.js';
2+
import { readBehaviorFile } from '../reader.js';
53

64
export interface LoopEvaluationOptions {
75
behavior?: ModuleBehavior;
@@ -23,20 +21,8 @@ export async function evaluateLoopBehavior(options: LoopEvaluationOptions): Prom
2321
return null;
2422
}
2523

26-
// Check for behavior file
27-
const behaviorFile = path.join(cwd, '.codemachine', 'memory', 'behavior.json');
28-
29-
// Read and parse behavior action
30-
let behaviorAction: BehaviorAction;
31-
try {
32-
const content = await fs.promises.readFile(behaviorFile, 'utf8');
33-
behaviorAction = JSON.parse(content);
34-
} catch (error) {
35-
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
36-
// No file = no special behavior, continue normally
37-
return null;
38-
}
39-
console.error(`Failed to parse behavior file: ${error instanceof Error ? error.message : String(error)}`);
24+
const behaviorAction = await readBehaviorFile(cwd);
25+
if (!behaviorAction) {
4026
return null;
4127
}
4228

src/workflows/behaviors/loop/controller.ts renamed to src/workflows/behaviors/loop/handler.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@ import type { WorkflowStep } from '../../templates/index.js';
22
import { isModuleStep } from '../../templates/types.js';
33
import { evaluateLoopBehavior } from './evaluator.js';
44
import { formatAgentLog } from '../../../shared/logging/index.js';
5-
import type { ActiveLoop } from '../skip.js';
65
import type { WorkflowEventEmitter } from '../../events/emitter.js';
76
import { debug } from '../../../shared/logging/logger.js';
7+
import type { ActiveLoop, LoopDecision } from './types.js';
88

9-
export interface LoopDecision {
10-
shouldRepeat: boolean;
11-
stepsBack: number;
12-
skipList: string[];
13-
reason?: string;
14-
}
9+
export type { LoopDecision };
1510

1611
export async function handleLoopLogic(
1712
step: WorkflowStep,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Loop Behavior Module
3+
*
4+
* Handles step-back looping logic when agents request iteration.
5+
*/
6+
7+
export * from './types.js';
8+
export * from './evaluator.js';
9+
export * from './handler.js';

0 commit comments

Comments
 (0)