灵隙(synapse) v1.0.0 版本
This commit is contained in:
@@ -224,6 +224,10 @@ func (p *P2PProtocol) handleTaskRequest(ctx context.Context, msg *types.UnifiedS
|
||||
taskCtx, cancel := context.WithTimeout(ctx, p.config.TaskTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Store task status
|
||||
status := types.P2PStatusPending
|
||||
p.tasks[taskID] = &status
|
||||
|
||||
// Execute task asynchronously
|
||||
go p.executeTask(taskCtx, msg, taskID, payload.Task.Type, payload.Task.Payload)
|
||||
|
||||
@@ -233,6 +237,12 @@ func (p *P2PProtocol) handleTaskRequest(ctx context.Context, msg *types.UnifiedS
|
||||
|
||||
// executeTask executes a task and handles callbacks
|
||||
func (p *P2PProtocol) executeTask(ctx context.Context, origMsg *types.UnifiedSynapseMessage, taskID, taskType string, payload map[string]interface{}) {
|
||||
// Update status to executing
|
||||
p.mu.Lock()
|
||||
execStatus := types.P2PStatusExecuting
|
||||
p.tasks[taskID] = &execStatus
|
||||
p.mu.Unlock()
|
||||
|
||||
// Send initial status update
|
||||
p.sendStatusUpdate(ctx, taskID, types.P2PStatusExecuting, 0, "Task started")
|
||||
|
||||
@@ -241,11 +251,19 @@ func (p *P2PProtocol) executeTask(ctx context.Context, origMsg *types.UnifiedSyn
|
||||
|
||||
if err != nil {
|
||||
// Task failed
|
||||
p.mu.Lock()
|
||||
failStatus := types.P2PStatusFailed
|
||||
p.tasks[taskID] = &failStatus
|
||||
p.mu.Unlock()
|
||||
p.sendTaskFailed(ctx, taskID, "execution_failed", err.Error(), true, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Task completed
|
||||
p.mu.Lock()
|
||||
compStatus := types.P2PStatusCompleted
|
||||
p.tasks[taskID] = &compStatus
|
||||
p.mu.Unlock()
|
||||
p.sendTaskComplete(ctx, taskID, result)
|
||||
}
|
||||
|
||||
@@ -498,7 +516,18 @@ func (p *P2PProtocol) newAck(msg *types.UnifiedSynapseMessage, taskID, note stri
|
||||
|
||||
// newNACK creates a NACK response
|
||||
func (p *P2PProtocol) newNACK(msg *types.UnifiedSynapseMessage, err *types.SynapseError) (*types.UnifiedSynapseMessage, error) {
|
||||
return nil, err
|
||||
errBytes, _ := json.Marshal(err)
|
||||
return &types.UnifiedSynapseMessage{
|
||||
ID: types.GenerateMessageID(),
|
||||
Version: "1.0.0",
|
||||
Type: string(types.P2PNack),
|
||||
Sender: p.agentEndpoint,
|
||||
Receiver: &msg.Sender,
|
||||
CorrelationID: msg.CorrelationID,
|
||||
ReplyTo: msg.ID,
|
||||
Payload: errBytes,
|
||||
Timestamp: types.NowTimestamp(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// newReject creates a task rejection response
|
||||
|
||||
@@ -191,7 +191,7 @@ func TestHandleHeartbeat(t *testing.T) {
|
||||
ID: types.GenerateMessageID(),
|
||||
Version: "1.0.0",
|
||||
Type: string(types.P2PHeartbeat),
|
||||
Sender: types.AgentEndpoint{AgentID: "agent-002"},
|
||||
Sender: types.AgentEndpoint{AgentID: "agent-002", Endpoint: "http://localhost:8081"},
|
||||
Payload: json.RawMessage("{}"),
|
||||
Timestamp: types.NowTimestamp(),
|
||||
}
|
||||
@@ -217,7 +217,7 @@ func TestHandleAck(t *testing.T) {
|
||||
ID: types.GenerateMessageID(),
|
||||
Version: "1.0.0",
|
||||
Type: string(types.P2PAck),
|
||||
Sender: types.AgentEndpoint{AgentID: "agent-002"},
|
||||
Sender: types.AgentEndpoint{AgentID: "agent-002", Endpoint: "http://localhost:8081"},
|
||||
ReplyTo: "msg-001",
|
||||
Payload: json.RawMessage("{}"),
|
||||
Timestamp: types.NowTimestamp(),
|
||||
@@ -295,6 +295,10 @@ func TestGetTaskStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
proto := NewP2PProtocol(ep, nil)
|
||||
proto.SetTaskHandler(&mockTaskHandler{
|
||||
canHandle: true,
|
||||
result: "test result",
|
||||
})
|
||||
|
||||
// 查询不存在的任务
|
||||
status, ok := proto.GetTaskStatus("nonexistent")
|
||||
@@ -327,9 +331,9 @@ func TestGetTaskStatus(t *testing.T) {
|
||||
if !ok {
|
||||
t.Error("Task should be found")
|
||||
}
|
||||
// 状态应该是 pending 或 confirmed
|
||||
if status != types.P2PStatusPending && status != types.P2PStatusConfirmed {
|
||||
t.Errorf("Status should be pending or confirmed: got %s", status)
|
||||
// 状态应该是 pending 或 executing(异步执行可能已经开始)
|
||||
if status != types.P2PStatusPending && status != types.P2PStatusExecuting && status != types.P2PStatusCompleted {
|
||||
t.Errorf("Status should be pending/executing/completed: got %s", status)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -340,6 +344,10 @@ func TestListTasks(t *testing.T) {
|
||||
}
|
||||
|
||||
proto := NewP2PProtocol(ep, nil)
|
||||
proto.SetTaskHandler(&mockTaskHandler{
|
||||
canHandle: true,
|
||||
result: "test result",
|
||||
})
|
||||
|
||||
// 初始应该为空
|
||||
tasks := proto.ListTasks()
|
||||
@@ -487,8 +495,14 @@ func TestHandleUnknownMessageType(t *testing.T) {
|
||||
Timestamp: types.NowTimestamp(),
|
||||
}
|
||||
|
||||
_, err := proto.HandleMessage(context.Background(), msg)
|
||||
if err == nil {
|
||||
t.Error("Unknown message type should return error")
|
||||
resp, err := proto.HandleMessage(context.Background(), msg)
|
||||
if err != nil {
|
||||
t.Errorf("Unknown message type should return NACK response, not error: %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Error("Unknown message type should return NACK response")
|
||||
}
|
||||
if resp.Type != string(types.P2PNack) {
|
||||
t.Errorf("Expected NACK response, got: %s", resp.Type)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user