Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions lib/application/agent-interpreter-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* - `disabled` when no agent capability is available (ci-batch profile)
*/

import { Effect, Duration } from 'effect';
import { Effect, Duration, Fiber } from 'effect';
import type { ResolutionTarget, ResolutionProposalDraft } from '../domain/types';
import type { StepAction } from '../domain/types';
import type { ScreenId, ElementId, PostureId, SnapshotTemplateId } from '../domain/identity';
Expand Down Expand Up @@ -609,13 +609,13 @@ export function withAgentTimeout(
const budgetMs = options?.budgetMs ?? DEFAULT_AGENT_TIMEOUT_MS;
const providerId = options?.provider ?? 'agent-timeout-wrapper';

return (request) => Effect.runPromise(withAgentTimeoutEffect(
return (request) => Effect.runPromise(Effect.scoped(withAgentTimeoutEffect(
Effect.tryPromise({
try: () => interpret(request),
catch: (err) => err instanceof Error ? err : new Error(String(err)),
}),
{ budgetMs, provider: providerId },
));
)));
}

export function withAgentTimeoutEffect(
Expand All @@ -624,11 +624,24 @@ export function withAgentTimeoutEffect(
): Effect.Effect<AgentInterpretationResult, never, never> {
const budgetMs = options?.budgetMs ?? DEFAULT_AGENT_TIMEOUT_MS;
const providerId = options?.provider ?? 'agent-timeout-wrapper';
return interpretEffect.pipe(
Effect.timeout(Duration.millis(budgetMs)),
Effect.map((result) => result ?? timeoutFallbackResult(providerId, budgetMs)),
Effect.catchAll(() => Effect.succeed(timeoutFallbackResult(providerId, budgetMs))),
);
return Effect.scoped(Effect.gen(function* () {
const interpretationFiber = yield* Effect.forkScoped(
interpretEffect.pipe(
Effect.map((result) => ({ kind: 'result' as const, result })),
),
);

const outcome = yield* Effect.raceFirst(
Fiber.join(interpretationFiber),
Effect.sleep(Duration.millis(budgetMs)).pipe(Effect.as({ kind: 'timeout' as const })),
).pipe(
Effect.catchAll(() => Effect.succeed({ kind: 'timeout' as const })),
);

return outcome.kind === 'result'
? outcome.result
: timeoutFallbackResult(providerId, budgetMs);
}));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/composition/local-runtime-scenario-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function buildDefaultTranslator(
function buildRunnerWithInterpreter(interpreterOverride?: AgentInterpreterProvider | undefined): RuntimeScenarioRunnerPort {
return {
runSteps(input) {
return Effect.gen(function* () {
return Effect.scoped(Effect.gen(function* () {
const paths = createProjectPaths(input.rootDir, input.suiteRoot);
const translationDisabled = Boolean(input.translationOptions?.disableTranslation);
const cacheDisabled = Boolean(input.translationOptions?.disableTranslationCache);
Expand Down Expand Up @@ -126,7 +126,7 @@ function buildRunnerWithInterpreter(interpreterOverride?: AgentInterpreterProvid
);

return [...results];
});
}));
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions lib/composition/local-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ export function createLocalServiceContext(rootDir: string, options?: LocalServic
posture,
writeJournal: () => executionContext.writeJournal(),
provide<A, E, R>(program: Effect.Effect<A, E, R>): Effect.Effect<A, E, never> {
return Effect.provide(
return Effect.scoped(Effect.provide(
program as Effect.Effect<A, E, FileSystem | AdoSource | RuntimeScenarioRunner | ExecutionContext | PipelineConfigService | VersionControl | Dashboard | StageTracer | McpServer | PlaywrightBridge>,
layer,
) as Effect.Effect<A, E, never>;
)) as Effect.Effect<A, E, never>;
},
};
}
Expand Down
29 changes: 25 additions & 4 deletions lib/infrastructure/observation/playwright-screen-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,30 @@ async function observeScreen(
* Reuses existing locator resolution, ARIA capture, and strategy matching. */
export function createPlaywrightScreenObserver(page: Page): ScreenObservationPort {
return {
observe: (input) => Effect.tryPromise({
try: () => observeScreen(page, input),
catch: (error) => new TesseractError('screen-observation-failed', `Screen observation failed: ${error}`, error),
}).pipe(Effect.withSpan('playwright-screen-observation')),
observe: (input) => Effect.scoped(Effect.gen(function* () {
const pageState = { closed: false };

const onClose = () => {
pageState.closed = true;
};

yield* Effect.acquireRelease(
Effect.sync(() => {
page.on('close', onClose);
}),
() => Effect.sync(() => {
page.off('close', onClose);
}),
);

if (pageState.closed) {
return yield* Effect.fail(new TesseractError('screen-observation-failed', 'Screen observation failed: page already closed.'));
}

return yield* Effect.tryPromise({
try: () => observeScreen(page, input),
catch: (error) => new TesseractError('screen-observation-failed', `Screen observation failed: ${error}`, error),
});
})).pipe(Effect.withSpan('playwright-screen-observation')),
};
}