-
Notifications
You must be signed in to change notification settings - Fork 773
Add agent output mode (NXF_AGENT_MODE) #6782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f8ff606
Add tests for agent output mode (NXF_AGENT)
edmundmiller 7483087
Implement agent output mode (NXF_AGENT)
edmundmiller 4ec897e
Add integration tests for agent output mode
edmundmiller 1693f68
Add failing tests for '1' as truthy value in agent mode
edmundmiller 5ce41a4
Support '1' as truthy value in SysEnv.getBool()
edmundmiller 3addba3
Add LogObserver interface for common log observer contract
edmundmiller 730c6df
Implement LogObserver on AnsiLogObserver and AgentLogObserver
edmundmiller 820c8c8
Unify ansiLogObserver/agentLogObserver into single logObserver field
edmundmiller 627659b
Auto-disable ANSI log in agent mode for plain, parseable output
edmundmiller 3eed513
Fix agent output: write to stderr and install CaptureAppender in agen…
edmundmiller 28df1de
Fix printConsole: route process stdout directly in agent mode
edmundmiller 8322388
Merge branch 'master' into HEAD
pditommaso ec7fe18
Restore AnsiLogObserver, isolate agent mode in AgentLogObserver
pditommaso a0bd972
Update copyright
pditommaso a565d9d
[ci fast] Introduce LogObserver interface, unify agent/ansi log handling
pditommaso 18ff87e
Update tests
pditommaso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236 changes: 236 additions & 0 deletions
236
modules/nextflow/src/main/groovy/nextflow/trace/AgentLogObserver.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| /* | ||
| * Copyright 2013-2026, Seqera Labs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package nextflow.trace | ||
|
|
||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import groovy.util.logging.Slf4j | ||
| import nextflow.Session | ||
| import nextflow.processor.TaskRun | ||
| import nextflow.trace.event.TaskEvent | ||
| import nextflow.util.LoggerHelper | ||
|
|
||
| /** | ||
| * AI agent-friendly log observer that outputs minimal, structured information | ||
| * to standard error, optimized for AI context windows. | ||
| * | ||
| * Activated via environment variable {@code NXF_AGENT_MODE=1}. | ||
| * | ||
| * Output format: | ||
| * - {@code [PIPELINE] name version | profile=X} | ||
| * - {@code [WORKDIR] /path/to/work} | ||
| * - {@code [PROCESS hash] name (tag)} | ||
| * - {@code [WARN] warning message} (deduplicated) | ||
| * - {@code [ERROR] name} with exit/cmd/stderr/workdir | ||
| * - {@code [SUCCESS|FAILED] completed=N failed=N cached=N} | ||
| * | ||
| * @author Edmund Miller <edmund.miller@utdallas.edu> | ||
| */ | ||
| @Slf4j | ||
| @CompileStatic | ||
| class AgentLogObserver implements TraceObserverV2, LogObserver { | ||
|
|
||
| private Session session | ||
| private WorkflowStatsObserver statsObserver | ||
| private final Set<String> seenWarnings = ConcurrentHashMap.newKeySet() | ||
| private volatile boolean started = false | ||
| private volatile boolean completed = false | ||
|
|
||
| /** | ||
| * Set the workflow stats observer for retrieving task statistics | ||
| */ | ||
| void setStatsObserver(WorkflowStatsObserver observer) { | ||
| this.statsObserver = observer | ||
| } | ||
|
|
||
| /** | ||
| * Print a line to standard output (agent format) | ||
| */ | ||
| protected void println(String line) { | ||
| System.err.println(line) | ||
| } | ||
|
|
||
| // -- TraceObserverV2 lifecycle methods -- | ||
|
|
||
| @Override | ||
| void onFlowCreate(Session session) { | ||
| this.session = session | ||
| } | ||
|
|
||
| @Override | ||
| void onFlowBegin() { | ||
| if( started ) | ||
| return | ||
| started = true | ||
|
|
||
| // Print pipeline info | ||
| def manifest = session.manifest | ||
| def pipelineName = manifest?.name ?: session.scriptName ?: 'unknown' | ||
| def version = manifest?.version ?: '' | ||
| def profile = session.profile ?: 'standard' | ||
|
|
||
| def info = "[PIPELINE] ${pipelineName}" | ||
| if( version ) | ||
| info += " ${version}" | ||
| info += " | profile=${profile}" | ||
| println(info) | ||
|
|
||
| // Print work directory | ||
| def workDir = session.workDir?.toUriString() ?: session.workDir?.toString() | ||
| if( workDir ) | ||
| println("[WORKDIR] ${workDir}") | ||
| } | ||
|
|
||
| @Override | ||
| void onFlowComplete() { | ||
| if( completed ) | ||
| return | ||
| completed = true | ||
| printSummary() | ||
| } | ||
|
|
||
| @Override | ||
| void onFlowError(TaskEvent event) { | ||
| // Error is already reported by onTaskComplete for failed tasks | ||
| } | ||
|
|
||
| @Override | ||
| void onTaskSubmit(TaskEvent event) { | ||
| def task = event.handler?.task | ||
| if( task ) | ||
| println("[PROCESS ${task.hashLog}] ${task.name}") | ||
| } | ||
|
|
||
| @Override | ||
| void onTaskComplete(TaskEvent event) { | ||
| def handler = event.handler | ||
| def task = handler?.task | ||
| if( task?.isFailed() ) { | ||
| printTaskError(task) | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| void onTaskCached(TaskEvent event) { | ||
| // Not reported in agent mode | ||
| } | ||
|
|
||
| /** | ||
| * Append a warning message (deduplicated) | ||
| */ | ||
| void appendWarning(String message) { | ||
| if( message == null ) | ||
| return | ||
| // Normalize and deduplicate | ||
| def normalized = message.trim().replaceAll(/\s+/, ' ') | ||
| if( seenWarnings.add(normalized) ) { | ||
| println("[WARN] ${normalized}") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Append an error message | ||
| */ | ||
| void appendError(String message) { | ||
| if( message ) | ||
| println("[ERROR] ${message}") | ||
| } | ||
|
|
||
| /** | ||
| * Append info message to stdout. | ||
| * Hash-prefixed task log lines (e.g. {@code [ab/123456] Submitted process > ...}) | ||
| * are filtered out because {@link #onTaskSubmit} already emits a {@code [PROCESS]} line. | ||
| */ | ||
| void appendInfo(String message) { | ||
| if( message && !LoggerHelper.isHashLogPrefix(message) ) | ||
| System.out.print(message) | ||
| } | ||
|
|
||
| /** | ||
| * Print task error with full diagnostic context | ||
| */ | ||
| protected void printTaskError(TaskRun task) { | ||
| def name = task.getName() | ||
| println("[ERROR] ${name}") | ||
|
|
||
| // Exit status | ||
| def exitStatus = task.getExitStatus() | ||
| if( exitStatus != null && exitStatus != Integer.MAX_VALUE ) { | ||
| println("exit: ${exitStatus}") | ||
| } | ||
|
|
||
| // Command/script (first line or truncated) | ||
| def script = task.getScript()?.toString()?.trim() | ||
| if( script ) { | ||
| // Truncate long commands | ||
| def cmd = script.length() > 200 ? script.substring(0, 200) + '...' : script | ||
| cmd = cmd.replaceAll(/\n/, ' ').replaceAll(/\s+/, ' ') | ||
| println("cmd: ${cmd}") | ||
| } | ||
|
|
||
| // Stderr | ||
| def stderr = task.getStderr() | ||
| if( stderr ) { | ||
| def lines = stderr.readLines() | ||
| if( lines.size() > 10 ) { | ||
| lines = lines[-10..-1] | ||
| } | ||
| println("stderr: ${lines.join(' | ')}") | ||
| } | ||
|
|
||
| // Stdout (only if relevant) | ||
| def stdout = task.getStdout() | ||
| if( stdout && !stderr ) { | ||
| def lines = stdout.readLines() | ||
| if( lines.size() > 5 ) { | ||
| lines = lines[-5..-1] | ||
| } | ||
| println("stdout: ${lines.join(' | ')}") | ||
| } | ||
|
|
||
| // Work directory | ||
| def workDir = task.getWorkDir() | ||
| if( workDir ) { | ||
| println("workdir: ${workDir.toUriString()}") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Print final summary line | ||
| */ | ||
| protected void printSummary() { | ||
| def stats = statsObserver?.getStats() | ||
| def succeeded = stats?.succeededCount ?: 0 | ||
| def failed = stats?.failedCount ?: 0 | ||
| def cached = stats?.cachedCount ?: 0 | ||
| def completed = succeeded + failed | ||
|
|
||
| def status = failed > 0 ? 'FAILED' : 'SUCCESS' | ||
| println("\n[${status}] completed=${completed} failed=${failed} cached=${cached}") | ||
| } | ||
|
|
||
| /** | ||
| * Force termination - called on abort | ||
| */ | ||
| void forceTermination() { | ||
| if( !completed ) { | ||
| completed = true | ||
| printSummary() | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.