Repository navigation
Expand file tree
/
Copy pathcmd-list-agent-groups.c
More file actions
160 lines (132 loc) · 4.17 KB
/
Copy pathcmd-list-agent-groups.c
File metadata and controls
160 lines (132 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* $OpenBSD$ */
/*
* Copyright (c) 2025 Marc Shade <marc.alan.shade@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
#include "session-agent.h"
/*
* List all agent coordination groups.
*/
static enum cmd_retval cmd_list_agent_groups_exec(struct cmd *, struct cmdq_item *);
const struct cmd_entry cmd_list_agent_groups_entry = {
.name = "list-agent-groups",
.alias = "lsag",
.args = { "", 0, 0, NULL },
.usage = "",
.target = { 0, 0, 0 },
.flags = CMD_EXTENSION,
.exec = cmd_list_agent_groups_exec
};
/* Helper structure to track unique groups */
struct group_info {
char *name;
int member_count;
int coordinator_count;
struct session **members;
int max_members;
};
static enum cmd_retval
cmd_list_agent_groups_exec(struct cmd *self, struct cmdq_item *item)
{
struct session *s;
struct session_agent *agent;
struct group_info *groups = NULL;
int num_groups = 0, max_groups = 16;
int i, j, found;
(void)self;
/* Allocate initial group tracking array */
groups = xcalloc(max_groups, sizeof(struct group_info));
/* Iterate all sessions to find groups */
RB_FOREACH(s, sessions, &sessions) {
agent = s->agent_metadata;
if (agent == NULL || agent->coordination_group == NULL)
continue;
/* Check if group already tracked */
found = -1;
for (i = 0; i < num_groups; i++) {
if (strcmp(groups[i].name, agent->coordination_group) == 0) {
found = i;
break;
}
}
if (found < 0) {
/* New group - add to list */
if (num_groups >= max_groups) {
/* Expand array */
max_groups *= 2;
groups = xreallocarray(groups, max_groups,
sizeof(struct group_info));
}
groups[num_groups].name = xstrdup(agent->coordination_group);
groups[num_groups].member_count = 0;
groups[num_groups].coordinator_count = 0;
groups[num_groups].max_members = 32;
groups[num_groups].members = xcalloc(
groups[num_groups].max_members, sizeof(struct session *));
found = num_groups;
num_groups++;
}
/* Add session to group */
if (groups[found].member_count < groups[found].max_members) {
groups[found].members[groups[found].member_count++] = s;
if (agent->is_coordinator)
groups[found].coordinator_count++;
}
}
/* Display results */
if (num_groups == 0) {
cmdq_print(item, "No coordination groups found");
free(groups);
return (CMD_RETURN_NORMAL);
}
cmdq_print(item, "Agent Coordination Groups: %d", num_groups);
cmdq_print(item, "");
for (i = 0; i < num_groups; i++) {
cmdq_print(item, "Group: %s", groups[i].name);
cmdq_print(item, " Members: %d", groups[i].member_count);
cmdq_print(item, " Coordinators: %d",
groups[i].coordinator_count);
for (j = 0; j < groups[i].member_count; j++) {
s = groups[i].members[j];
agent = s->agent_metadata;
cmdq_print(item, " - %s [%s]%s",
s->name,
agent->agent_type,
agent->is_coordinator ? " (coordinator)" : "");
}
/* Display shared context size if any member has context */
for (j = 0; j < groups[i].member_count; j++) {
agent = groups[i].members[j]->agent_metadata;
if (agent->shared_context != NULL &&
agent->shared_context_len > 0) {
cmdq_print(item, " Shared Context: %zu bytes",
agent->shared_context_len);
break;
}
}
if (i < num_groups - 1)
cmdq_print(item, "");
}
/* Cleanup */
for (i = 0; i < num_groups; i++) {
free(groups[i].name);
free(groups[i].members);
}
free(groups);
return (CMD_RETURN_NORMAL);
}