Skip to content
Closed
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
4 changes: 2 additions & 2 deletions examples/quickstart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ async function runAgent(): Promise<void> {

// ABIs (in production, load from @noosphere/contracts)
const routerAbi = [
'event RequestStarted(bytes32 indexed requestId, uint256 indexed subscriptionId, bytes32 containerId, uint256 interval, uint8 redundancy, bool useDeliveryInbox, uint256 feeAmount, address feeToken, address verifier, address coordinator)',
'event RequestStarted(bytes32 indexed requestId, uint256 indexed subscriptionId, bytes32 containerId, uint256 interval, bool useDeliveryInbox, uint256 feeAmount, address feeToken, address verifier, address coordinator)',
];
const coordinatorAbi = [
'function redundancyCount(bytes32 requestId) view returns (uint8)',
'function requestCommitments(bytes32 requestId) view returns (bytes32)',
'function fulfill(bytes32 requestId, bytes memory result, bytes memory proof) external returns (uint8)',
];

Expand Down
98 changes: 60 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@noosphere/sdk",
"version": "0.1.0",
"version": "0.2.1-alpha.1",
"description": "Decentralized compute agent SDK for Noosphere protocol",
"private": true,
"workspaces": [
Expand Down
10 changes: 5 additions & 5 deletions packages/agent-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@noosphere/agent-core",
"version": "0.2.0-alpha.3",
"version": "0.2.1-alpha.1",
"description": "Core modules for Noosphere agent (EventMonitor, ContainerManager, NoosphereAgent)",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down Expand Up @@ -50,10 +50,10 @@
"access": "public"
},
"dependencies": {
"@noosphere/contracts": "0.2.0-alpha.1",
"@noosphere/crypto": "0.2.0-alpha.1",
"@noosphere/payload": "0.2.0-alpha.1",
"@noosphere/registry": "0.2.0-alpha.1",
"@noosphere/contracts": "0.2.1-alpha.1",
"@noosphere/crypto": "0.2.1-alpha.1",
"@noosphere/payload": "0.2.1-alpha.1",
"@noosphere/registry": "0.2.1-alpha.1",
"axios": "^1.13.2",
"dockerode": "^4.0.0",
"dotenv": "^16.3.0",
Expand Down
36 changes: 35 additions & 1 deletion packages/agent-core/src/ContainerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,32 @@ export class ContainerManager {
return new Promise((resolve) => setTimeout(() => resolve(null), ms));
}

/**
* Ensure container is connected to the specified Docker network.
* This handles the case where an existing container was created with a different network
* (e.g., after docker-compose recreates the network).
*/
private async ensureContainerNetwork(
container: Docker.Container,
containerName: string,
networkName: string
): Promise<void> {
try {
const inspect = await container.inspect();
const connectedNetworks = Object.keys(inspect.NetworkSettings.Networks || {});

if (!connectedNetworks.includes(networkName)) {
console.log(` 🔗 Connecting ${containerName} to network ${networkName}...`);
const network = this.docker.getNetwork(networkName);
await network.connect({ Container: container.id });
console.log(` ✓ Connected ${containerName} to ${networkName}`);
}
} catch (err: any) {
// Log but don't fail - container might still work on default network
console.warn(` ⚠️ Failed to ensure network connection for ${containerName}: ${err.message}`);
Comment on lines +241 to +243
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The catch (err: any) block is quite broad. While the comment indicates a conscious decision not to fail, consider if there are specific Docker-related error types that could be caught to allow for more granular logging or handling of transient vs. permanent issues. This would improve the robustness of the network connection logic.

}
}

private parseMemory(memory: string): number {
const units: { [key: string]: number } = {
b: 1,
Expand Down Expand Up @@ -339,17 +365,26 @@ export class ContainerManager {

// Check if container already exists
const existingContainer = this.docker.getContainer(containerName);
const dockerNetwork = process.env.DOCKER_NETWORK;
try {
const inspect = await existingContainer.inspect();
if (inspect.State.Running) {
console.log(` ✓ Container ${containerName} already running`);
// Ensure container is connected to the correct network
if (dockerNetwork) {
await this.ensureContainerNetwork(existingContainer, containerName, dockerNetwork);
}
this.persistentContainers.set(containerId, existingContainer);
return;
} else {
// Container exists but stopped - try to start it
try {
await existingContainer.start();
console.log(` ✓ Started existing container ${containerName}`);
// Ensure container is connected to the correct network
if (dockerNetwork) {
await this.ensureContainerNetwork(existingContainer, containerName, dockerNetwork);
}
this.persistentContainers.set(containerId, existingContainer);
return;
} catch (startErr) {
Expand All @@ -363,7 +398,6 @@ export class ContainerManager {
}

// Create new persistent container
const dockerNetwork = process.env.DOCKER_NETWORK;
const createOptions: Docker.ContainerCreateOptions = {
name: containerName,
Image: imageTag,
Expand Down
4 changes: 2 additions & 2 deletions packages/agent-core/src/EventMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,13 @@ export class EventMonitor extends EventEmitter {
subscriptionId: event.args.subscriptionId,
containerId: event.args.containerId,
interval: commitment.interval,
redundancy: commitment.redundancy,
useDeliveryInbox: commitment.useDeliveryInbox,
walletAddress: commitment.walletAddress,
feeAmount: commitment.feeAmount,
feeToken: commitment.feeToken,
verifier: commitment.verifier,
coordinator: commitment.coordinator,
walletAddress: commitment.walletAddress,
verifierFee: commitment.verifierFee,
blockNumber: event.blockNumber,
};

Expand Down
Loading
Loading