Skip to main content

Build a HarnessAgent

HarnessAgent is not a new reasoning engine. It still exposes the standard IAgent interface, composes a ReActAgent internally, and binds a Runtime to gain workspace boundaries, engineering tools, and runtime resource lifecycle.

The quick build has three steps: add the dependency → create a Workspace and LocalRuntime → assemble the model and memory with HarnessAgent.

1. Add the dependency​

<dependency>
<groupId>cloud.changlu.agentforge</groupId>
<artifactId>agentforge-harness-agent</artifactId>
<version>1.0.2-SNAPSHOT</version>
</dependency>

agentforge-harness-agent transitively brings agentforge-agent-core, agentforge-model-api, and agentforge-model-core. At runtime you also need at least one model Provider, for example:

<dependency>
<groupId>cloud.changlu.agentforge</groupId>
<artifactId>agentforge-model-openai</artifactId>
<version>1.0.2-SNAPSHOT</version>
</dependency>

With the BOM, versions can be omitted:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>cloud.changlu.agentforge</groupId>
<artifactId>agentforge-ai-bom</artifactId>
<version>1.0.2-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

2. Differences from a plain Agent​

A plain Agent targets ReActAgent directly: the caller assembles the model, memory, and tools. HarnessAgent adds an "execution environment" abstraction on top:

DimensionPlain ReActAgentHarnessAgent
PositioningGeneral ReAct reasoning loopWorkspace-oriented engineering Agent
ConstructionConfigure model, memory, and ToolService directlyMust pass a Runtime through the Builder
Tool sourceRegistered by the businessRuntime provides read / write / edit / glob / grep / rg / bash
Safety boundaryNo built-in Workspace constraintBound to one Workspace; files and commands are constrained by path boundaries
System promptProvided by the userUses the Runtime default prompt when the user value is empty
Resource lifecycleNo extra runtime resourcesAutoCloseable; releases command executors and active processes on close
Extension directionTools, middleware, custom AgentAdd a Runtime to support Docker, E2B, or remote environments
Public APIIAgentAlso IAgent; delegates to ReActAgent internally

Three things need special attention:

  1. One HarnessAgent is bound to one Workspace and does not switch between workspaces dynamically;
  2. HarnessAgent does not branch on Runtime type; Docker, E2B, and other environments are added through new Runtime implementations;
  3. The first release enforces safety boundaries in Workspace and tool code, not in prompts; bash still runs with the operating-system user that started AgentForge.

3. Quickly build a local HarnessAgent​

3.1 Create a Workspace​

The root directory must already exist and be readable, and is converted to an absolute real path:

import cloud.changlu.agentforge.harness.runtime.local.workspace.Workspace;

import java.nio.file.Paths;

Workspace workspace =
Workspace.builder()
.id("my-project")
.root(Paths.get("/path/to/project"))
.build();

id is used in logs and Trace; when omitted, the directory name is used. workingDirectory can be omitted and defaults to root.

3.2 Create a LocalRuntime​

import cloud.changlu.agentforge.harness.runtime.local.LocalRuntime;
import cloud.changlu.agentforge.harness.runtime.local.config.LocalRuntimeOptions;

LocalRuntime runtime =
LocalRuntime.builder()
.workspace(workspace)
.options(LocalRuntimeOptions.defaults())
.build();

options can be omitted. Default command parameters:

ParameterDefaultDescription
shell/bin/shRuns foreground commands through shell -lc
commandTimeoutMillis30000Forcefully destroys the process after timeout
maxCommandOutputChars20000Combined output limit; excess is truncated

3.3 Create a HarnessAgent​

import cloud.changlu.agentforge.agent.extend.memory.window.MessageWindowChatMemoryProvider;
import cloud.changlu.agentforge.harness.agent.HarnessAgent;

HarnessAgent agent =
HarnessAgent.builder()
.runtime(runtime)
.agentName("local-harness-agent")
.chatModel(chatModel)
.streamingChatModel(streamingChatModel)
.chatMemoryProvider(new MessageWindowChatMemoryProvider(50))
.build();

runtime and chatMemoryProvider are required; provide at least one of chatModel and streamingChatModel. When systemPrompt is not provided, HarnessAgent uses runtime.defaultSystemPrompt().

3.4 Run and close​

import cloud.changlu.agentforge.agent.domain.AgentRequest;
import cloud.changlu.agentforge.agent.step.ChatResult;

try {
ChatResult result =
agent.run(
AgentRequest.builder()
.memoryId("my-project:conversation:1")
.question("Inspect the project structure first, then explain how to run the tests.")
.build());

System.out.println(result.getRes());
} finally {
agent.close();
}

Streaming uses the same assembly; switch to runStream:

agent.runStream(
AgentRequest.builder()
.memoryId("my-project:conversation:1")
.question("Find the cause of the most recent failing test.")
.build())
.onPartialResponse(System.out::print)
.onToolExecuted(tool ->
System.out.println("\n[tool] " + tool.request().name()))
.onCompleteResponse(response ->
System.out.println("\n[done] " + response.aiMessage().text()))
.onError(error -> System.err.println("[error] " + error.getMessage()))
.start();

When execution is canceled, HarnessAgent propagates cancellation to the Runtime and terminates local commands:

agent.cancel(memoryId, executionId);

4. What the local runtime provides​

LocalRuntimeTools initially includes:

ToolPurposeKey constraints
readRead UTF-8 text line by lineLimits file size, line count, and line length
writeCreate or overwrite a fileExisting files must be read first
editReplace text exactlyFile already read; old text must match uniquely
globFind files by patternScans only the Workspace; limits result count
grepSearch content by regexScans only the Workspace; limits file size and results
rgInvoke local ripgrepReturns a clear error when rg is missing
bashRun foreground commands in the WorkspaceBackground tasks forbidden; timeout and output limits

All path operations go through Workspace.resolveExisting or Workspace.resolveForCreate, rejecting .. escapes and symlink escapes. After read succeeds, write to an existing file and edit are allowed.

5. Minimal complete example​

Workspace workspace =
Workspace.builder()
.id("my-project")
.root(Paths.get("/path/to/project"))
.build();

LocalRuntime runtime =
LocalRuntime.builder()
.workspace(workspace)
.build();

HarnessAgent agent =
HarnessAgent.builder()
.runtime(runtime)
.chatModel(chatModel)
.chatMemoryProvider(new MessageWindowChatMemoryProvider(50))
.build();

try {
ChatResult result =
agent.run(
AgentRequest.builder()
.memoryId("my-project:conversation:1")
.question("Summarize the project structure.")
.build());
System.out.println(result.getRes());
} finally {
agent.close();
}

bash permissions are equivalent to the operating-system user that started AgentForge. Before exposing it to untrusted users, add approval or further isolation; Workspace boundaries cannot replace an OS-level sandbox.

Next: learn about Runtime, Workspace, and RuntimeTools responsibilities and multi-runtime extension by reading Runtime Design & Implementation.