deuk-agent-flow 4.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.ko.md +223 -0
- package/CHANGELOG.md +227 -0
- package/LICENSE +184 -0
- package/README.ko.md +282 -0
- package/README.md +270 -0
- package/bin/deuk-agent-flow.js +50 -0
- package/bin/deuk-agent-rule.js +2 -0
- package/core-rules/AGENTS.md +153 -0
- package/core-rules/GEMINI.md +7 -0
- package/docs/architecture.ko.md +34 -0
- package/docs/architecture.md +33 -0
- package/docs/assets/architecture-v3.png +0 -0
- package/docs/how-it-works.ko.md +52 -0
- package/docs/how-it-works.md +71 -0
- package/docs/principles.ko.md +68 -0
- package/docs/principles.md +68 -0
- package/docs/usage-guide.ko.md +212 -0
- package/package.json +96 -0
- package/scripts/cli-args.mjs +200 -0
- package/scripts/cli-init-commands.mjs +1799 -0
- package/scripts/cli-init-logic.mjs +64 -0
- package/scripts/cli-prompts.mjs +104 -0
- package/scripts/cli-rule-compiler.mjs +112 -0
- package/scripts/cli-skill-commands.mjs +201 -0
- package/scripts/cli-telemetry-commands.mjs +599 -0
- package/scripts/cli-ticket-commands.mjs +2393 -0
- package/scripts/cli-ticket-index.mjs +298 -0
- package/scripts/cli-ticket-migration.mjs +320 -0
- package/scripts/cli-ticket-parser.mjs +209 -0
- package/scripts/cli-usage-commands.mjs +326 -0
- package/scripts/cli-utils.mjs +587 -0
- package/scripts/cli.mjs +246 -0
- package/scripts/lint-md.mjs +267 -0
- package/scripts/lint-rules.mjs +186 -0
- package/scripts/merge-logic.mjs +44 -0
- package/scripts/plan-parser.mjs +53 -0
- package/scripts/publish-dual-npm.mjs +141 -0
- package/scripts/smoke-npm-docker.mjs +102 -0
- package/scripts/smoke-npm-local.mjs +109 -0
- package/scripts/update-download-badge.mjs +107 -0
- package/templates/MODULE_RULE_TEMPLATE.md +11 -0
- package/templates/PROJECT_RULE.md +47 -0
- package/templates/TICKET_TEMPLATE.ko.md +44 -0
- package/templates/TICKET_TEMPLATE.md +44 -0
- package/templates/project-pilot/CONFORMANCE_GATE_TEMPLATE.md +23 -0
- package/templates/project-pilot/DRIFT_CHECKLIST.md +19 -0
- package/templates/project-pilot/FLOW_CONTRACT_TEMPLATE.md +26 -0
- package/templates/project-pilot/IMPLEMENTATION_MATRIX_TEMPLATE.md +30 -0
- package/templates/project-pilot/INTEGRATION_CONTRACT_TEMPLATE.md +26 -0
- package/templates/project-pilot/OWNER_MAP_TEMPLATE.md +15 -0
- package/templates/project-pilot/PROJECT_PILOT_RULE_TEMPLATE.md +34 -0
- package/templates/project-pilot/REFACTOR_CONTRACT_TEMPLATE.md +32 -0
- package/templates/project-pilot/REMEDIATION_PLAN_TEMPLATE.md +33 -0
- package/templates/rules.d/deukcontext-mcp.md +31 -0
- package/templates/rules.d/platform-coexistence.md +29 -0
- package/templates/skills/context-recall/SKILL.md +25 -0
- package/templates/skills/generated-file-guard/SKILL.md +25 -0
- package/templates/skills/project-pilot/SKILL.md +63 -0
- package/templates/skills/safe-refactor/SKILL.md +25 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from "fs";
|
|
2
|
+
import { basename, dirname, join } from "path";
|
|
3
|
+
import {
|
|
4
|
+
AGENT_ROOT_DIR, TICKET_SUBDIR, TICKET_LIST_FILENAME,
|
|
5
|
+
toPosixPath, toRepoRelativePath, detectProjectFromBody, deriveTopicFromBaseName, normalizeTicketGroup,
|
|
6
|
+
parseFrontMatter, stringifyFrontMatter, discoverAllWorkspaces, detectConsumerTicketDir,
|
|
7
|
+
ARCHIVE_YEAR_MONTH_RE, ARCHIVE_DAY_RE
|
|
8
|
+
} from "./cli-utils.mjs";
|
|
9
|
+
import { readTicketIndexJson, writeTicketIndexJson } from "./cli-ticket-index.mjs";
|
|
10
|
+
|
|
11
|
+
export function collectTicketMarkdownFiles(dir, out = []) {
|
|
12
|
+
if (!existsSync(dir)) return out;
|
|
13
|
+
for (const ent of readdirSync(dir, { withFileTypes: true })) {
|
|
14
|
+
const abs = join(dir, ent.name);
|
|
15
|
+
if (ent.name === "node_modules" || ent.name === ".git") continue;
|
|
16
|
+
if (ent.isDirectory()) collectTicketMarkdownFiles(abs, out);
|
|
17
|
+
else if (ent.isFile() && /\.md$/i.test(ent.name)) {
|
|
18
|
+
const base = ent.name;
|
|
19
|
+
if (base === "LATEST.md" || base === TICKET_LIST_FILENAME || base === "ACTIVE_TICKET.md") continue;
|
|
20
|
+
out.push(abs);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function discoverAllTicketDirs(baseCwd, out = []) {
|
|
27
|
+
return discoverAllWorkspaces(baseCwd, undefined, new Set(out));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function rebuildTicketIndexFromTopicFilesIfNeeded(cwd, opts = {}) {
|
|
31
|
+
const indexJson = readTicketIndexJson(cwd);
|
|
32
|
+
|
|
33
|
+
if (indexJson.entries.length > 0 && !opts.force && !opts.rebuild) {
|
|
34
|
+
return indexJson;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const ticketDir = detectConsumerTicketDir(cwd);
|
|
38
|
+
if (!ticketDir) return indexJson;
|
|
39
|
+
|
|
40
|
+
// Strictly scan only the official ticket system directory and its subdirectories
|
|
41
|
+
const ticketDirs = [ticketDir];
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if (ticketDirs.length === 0) return indexJson;
|
|
45
|
+
|
|
46
|
+
const files = [];
|
|
47
|
+
for (const dir of ticketDirs) {
|
|
48
|
+
collectTicketMarkdownFiles(dir, files);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let dirty = false;
|
|
52
|
+
const newEntries = [];
|
|
53
|
+
|
|
54
|
+
for (let i = 0; i < files.length; i++) {
|
|
55
|
+
const entry = processTicketFile(files[i], cwd, indexJson, opts);
|
|
56
|
+
if (entry) newEntries.push(entry);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (JSON.stringify(indexJson.entries) !== JSON.stringify(newEntries)) {
|
|
60
|
+
dirty = true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (dirty || opts.force || opts.rebuild) {
|
|
64
|
+
newEntries.sort((a,b) => String(b.createdAt||"").localeCompare(String(a.createdAt||"")));
|
|
65
|
+
const next = { version: 1, updatedAt: new Date().toISOString(), entries: newEntries };
|
|
66
|
+
writeTicketIndexJson(cwd, next, opts);
|
|
67
|
+
if (opts.rebuild) console.log(`[REBUILD] INDEX.json rebuilt with ${newEntries.length} entries.`);
|
|
68
|
+
return next;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return indexJson;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function processTicketFile(abs, cwd, indexJson, opts) {
|
|
75
|
+
const rel = toPosixPath(toRepoRelativePath(cwd, abs));
|
|
76
|
+
const filename = basename(abs);
|
|
77
|
+
const idFromFilename = filename.replace(/\.md$/i, "");
|
|
78
|
+
const isAlreadyInArchive = rel.includes("/archive/");
|
|
79
|
+
const storage = parseTicketStorage(rel, isAlreadyInArchive, abs);
|
|
80
|
+
const group = storage.group;
|
|
81
|
+
|
|
82
|
+
// Optimization: If entry already exists in index and not forced, reuse metadata to save I/O & tokens
|
|
83
|
+
const existing = indexJson.entries.find(e => e.id === idFromFilename);
|
|
84
|
+
if (existing && !opts.force) {
|
|
85
|
+
return {
|
|
86
|
+
...existing,
|
|
87
|
+
group,
|
|
88
|
+
archiveYearMonth: storage.archiveYearMonth || existing.archiveYearMonth,
|
|
89
|
+
status: isAlreadyInArchive ? "archived" : (existing.status === "archived" ? "open" : existing.status),
|
|
90
|
+
updatedAt: statSync(abs).mtime.toISOString()
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// New or forced entry: Parse file content
|
|
95
|
+
let meta = {}, content = "";
|
|
96
|
+
try {
|
|
97
|
+
const body = readFileSync(abs, "utf8");
|
|
98
|
+
const parsed = parseFrontMatter(body);
|
|
99
|
+
meta = parsed.meta;
|
|
100
|
+
content = parsed.content;
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.warn(`[WARNING] Failed to parse ${rel}: ${err.message}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const title = meta.title || idFromFilename;
|
|
106
|
+
const project = meta.project || detectProjectFromBody(content);
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
id: meta.id || idFromFilename,
|
|
110
|
+
title,
|
|
111
|
+
topic: deriveTopicFromBaseName(filename),
|
|
112
|
+
group,
|
|
113
|
+
fileName: filename,
|
|
114
|
+
project,
|
|
115
|
+
submodule: meta.submodule || (rel.startsWith(AGENT_ROOT_DIR) ? "" : rel.split("/")[0]),
|
|
116
|
+
createdAt: meta.createdAt || statSync(abs).mtime.toISOString(),
|
|
117
|
+
updatedAt: statSync(abs).mtime.toISOString(),
|
|
118
|
+
source: "ticket-sync",
|
|
119
|
+
status: isAlreadyInArchive ? "archived" : (meta.status || "open"),
|
|
120
|
+
archiveYearMonth: storage.archiveYearMonth,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function parseTicketStorage(rel, isArchived, abs) {
|
|
125
|
+
if (!isArchived) {
|
|
126
|
+
return { group: basename(dirname(abs)) };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const parts = rel.split("/");
|
|
130
|
+
const archiveIdx = parts.indexOf("archive");
|
|
131
|
+
const group = parts[archiveIdx + 1] || basename(dirname(abs));
|
|
132
|
+
const maybeYearMonth = parts[archiveIdx + 2];
|
|
133
|
+
const maybeDay = parts[archiveIdx + 3];
|
|
134
|
+
|
|
135
|
+
if (ARCHIVE_YEAR_MONTH_RE.test(String(maybeYearMonth || "")) && ARCHIVE_DAY_RE.test(String(maybeDay || ""))) {
|
|
136
|
+
return { group: normalizeTicketGroup(group), archiveYearMonth: maybeYearMonth, archiveDay: maybeDay };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (ARCHIVE_YEAR_MONTH_RE.test(String(maybeYearMonth || ""))) {
|
|
140
|
+
return { group: normalizeTicketGroup(group), archiveYearMonth: maybeYearMonth };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return { group: normalizeTicketGroup(group) };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function appendTicketEntry(cwd, entry, opts = {}) {
|
|
147
|
+
const indexJson = readTicketIndexJson(cwd);
|
|
148
|
+
entry.status = entry.status || "open";
|
|
149
|
+
// We no longer store 'path' snapshots in INDEX.json
|
|
150
|
+
const { path, ...cleanEntry } = entry;
|
|
151
|
+
const next = {
|
|
152
|
+
version: indexJson.version || 1,
|
|
153
|
+
updatedAt: new Date().toISOString(),
|
|
154
|
+
activeTicketId: indexJson.activeTicketId,
|
|
155
|
+
entries: [cleanEntry, ...indexJson.entries]
|
|
156
|
+
};
|
|
157
|
+
writeTicketIndexJson(cwd, next, opts);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function updateTicketEntryStatus(cwd, opts = {}) {
|
|
161
|
+
const indexJson = rebuildTicketIndexFromTopicFilesIfNeeded(cwd, opts);
|
|
162
|
+
let foundIndex = -1;
|
|
163
|
+
const targetTopic = opts.topic ? String(opts.topic).toLowerCase() : null;
|
|
164
|
+
|
|
165
|
+
if (opts.latest) {
|
|
166
|
+
foundIndex = 0;
|
|
167
|
+
} else if (targetTopic) {
|
|
168
|
+
// Match against both topic AND id for consistency with pickTicketEntry
|
|
169
|
+
foundIndex = indexJson.entries.findIndex(e =>
|
|
170
|
+
String(e.topic || "").toLowerCase().includes(targetTopic) ||
|
|
171
|
+
String(e.id || "").toLowerCase().includes(targetTopic)
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (foundIndex === -1) {
|
|
176
|
+
throw new Error("No matching ticket found to update status");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const entry = indexJson.entries[foundIndex];
|
|
180
|
+
const newStatus = opts.status || "closed";
|
|
181
|
+
entry.status = newStatus;
|
|
182
|
+
if (newStatus === "closed" || newStatus === "cancelled" || newStatus === "wontfix") {
|
|
183
|
+
entry.phase = 4;
|
|
184
|
+
}
|
|
185
|
+
entry.updatedAt = new Date().toISOString();
|
|
186
|
+
|
|
187
|
+
// Sync status back to .md frontmatter to prevent rebuild reversion
|
|
188
|
+
const absPath = join(cwd, entry.path);
|
|
189
|
+
if (existsSync(absPath)) {
|
|
190
|
+
try {
|
|
191
|
+
const body = readFileSync(absPath, "utf8");
|
|
192
|
+
const parsed = parseFrontMatter(body);
|
|
193
|
+
if (parsed.meta.status !== newStatus) {
|
|
194
|
+
parsed.meta.status = newStatus;
|
|
195
|
+
if (newStatus === "closed" || newStatus === "cancelled" || newStatus === "wontfix") {
|
|
196
|
+
parsed.meta.phase = 4;
|
|
197
|
+
}
|
|
198
|
+
const newBody = stringifyFrontMatter(parsed.meta, parsed.content);
|
|
199
|
+
writeFileSync(absPath, newBody, "utf8");
|
|
200
|
+
}
|
|
201
|
+
} catch (err) {
|
|
202
|
+
console.warn(`[WARNING] Failed to sync status to ${entry.path}: ${err.message}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const next = { version: indexJson.version, updatedAt: new Date().toISOString(), entries: indexJson.entries };
|
|
207
|
+
writeTicketIndexJson(cwd, next, opts);
|
|
208
|
+
return entry;
|
|
209
|
+
}
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { AGENT_ROOT_DIR } from "./cli-utils.mjs";
|
|
4
|
+
|
|
5
|
+
const USAGE_FILENAME = `${AGENT_ROOT_DIR}/usage.json`;
|
|
6
|
+
const USAGE_VERSION = 1;
|
|
7
|
+
const VALID_TASK_GRADES = new Set(["S", "A", "B", "C"]);
|
|
8
|
+
const DEFAULT_PLATFORM = "codex";
|
|
9
|
+
const PLATFORM_ALIASES = {
|
|
10
|
+
codex: "codex",
|
|
11
|
+
copilot: "copilot",
|
|
12
|
+
ghcopilot: "copilot",
|
|
13
|
+
"github-copilot": "copilot",
|
|
14
|
+
claude: "claude",
|
|
15
|
+
claudecode: "claude",
|
|
16
|
+
cursor: "cursor"
|
|
17
|
+
};
|
|
18
|
+
const PLATFORM_DEFAULT_CLIENT = {
|
|
19
|
+
codex: "Codex",
|
|
20
|
+
copilot: "Copilot",
|
|
21
|
+
claude: "ClaudeCode",
|
|
22
|
+
cursor: "Cursor"
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export async function runUsage(action, opts) {
|
|
26
|
+
if (action === "set") {
|
|
27
|
+
return runUsageSet(opts);
|
|
28
|
+
}
|
|
29
|
+
if (action === "status") {
|
|
30
|
+
return runUsageStatus(opts);
|
|
31
|
+
}
|
|
32
|
+
if (action === "advise") {
|
|
33
|
+
return runUsageAdvise(opts);
|
|
34
|
+
}
|
|
35
|
+
console.error("Unknown usage action: " + action);
|
|
36
|
+
console.log("Usage: npx deuk-agent-flow usage <set|status|advise> [options]");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function loadUsageState(cwd) {
|
|
40
|
+
const absPath = join(cwd, USAGE_FILENAME);
|
|
41
|
+
if (!existsSync(absPath)) return null;
|
|
42
|
+
try {
|
|
43
|
+
const data = JSON.parse(readFileSync(absPath, "utf8"));
|
|
44
|
+
if (data.version !== USAGE_VERSION) return null;
|
|
45
|
+
return normalizeUsageState(data);
|
|
46
|
+
} catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function writeUsageState(cwd, state) {
|
|
52
|
+
const absPath = join(cwd, USAGE_FILENAME);
|
|
53
|
+
const dir = join(cwd, AGENT_ROOT_DIR);
|
|
54
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
55
|
+
writeFileSync(absPath, JSON.stringify({
|
|
56
|
+
version: USAGE_VERSION,
|
|
57
|
+
platform: state.platform,
|
|
58
|
+
client: state.client,
|
|
59
|
+
agentId: state.agentId,
|
|
60
|
+
weeklyRemainingPct: state.weeklyRemainingPct,
|
|
61
|
+
fiveHourRemainingPct: state.fiveHourRemainingPct,
|
|
62
|
+
weeklyResetAt: state.weeklyResetAt,
|
|
63
|
+
fiveHourResetAt: state.fiveHourResetAt,
|
|
64
|
+
updatedAt: state.updatedAt
|
|
65
|
+
}, null, 2), "utf8");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function normalizeUsageState(raw = {}) {
|
|
69
|
+
const platform = normalizePlatform(raw.platform || DEFAULT_PLATFORM);
|
|
70
|
+
const normalizedClient = String(raw.client || "").trim();
|
|
71
|
+
return {
|
|
72
|
+
platform,
|
|
73
|
+
client: normalizedClient || resolveClientForPlatform(platform),
|
|
74
|
+
agentId: String(raw.agentId || ""),
|
|
75
|
+
weeklyRemainingPct: clampPercent(raw.weeklyRemainingPct),
|
|
76
|
+
fiveHourRemainingPct: clampPercent(raw.fiveHourRemainingPct),
|
|
77
|
+
weeklyResetAt: String(raw.weeklyResetAt || ""),
|
|
78
|
+
fiveHourResetAt: String(raw.fiveHourResetAt || ""),
|
|
79
|
+
updatedAt: String(raw.updatedAt || "")
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function clampPercent(value) {
|
|
84
|
+
if (value === null || value === undefined || Number.isNaN(Number(value))) return null;
|
|
85
|
+
return Math.min(100, Math.max(0, Number(value)));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function resolveTaskGrade(input = "") {
|
|
89
|
+
const grade = String(input || "").trim().toUpperCase();
|
|
90
|
+
if (!VALID_TASK_GRADES.has(grade)) return "";
|
|
91
|
+
return grade;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function normalizePlatform(input = "") {
|
|
95
|
+
const key = String(input || "").trim().toLowerCase();
|
|
96
|
+
if (!key) return DEFAULT_PLATFORM;
|
|
97
|
+
return PLATFORM_ALIASES[key] || key;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function resolveClientForPlatform(platform) {
|
|
101
|
+
return PLATFORM_DEFAULT_CLIENT[platform] || "Codex";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function buildBudgetAdvice(state, taskGrade) {
|
|
105
|
+
const weekly = state.weeklyRemainingPct;
|
|
106
|
+
const fiveHour = state.fiveHourRemainingPct;
|
|
107
|
+
const tight = weekly !== null && weekly <= 20 || fiveHour !== null && fiveHour <= 15;
|
|
108
|
+
const critical = weekly !== null && weekly <= 10 || fiveHour !== null && fiveHour <= 8;
|
|
109
|
+
|
|
110
|
+
if (!taskGrade) {
|
|
111
|
+
return {
|
|
112
|
+
budget: critical ? "phase1-only" : tight ? "split-large-tasks" : "normal",
|
|
113
|
+
gate: critical ? "no broad work" : tight ? "keep scope narrow" : "ok",
|
|
114
|
+
next: critical ? "ticket status" : "usage advise --task-grade A"
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (critical) {
|
|
119
|
+
return {
|
|
120
|
+
budget: `${taskGrade} blocked`,
|
|
121
|
+
gate: "phase1-only",
|
|
122
|
+
next: "summarize and stop"
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (tight) {
|
|
127
|
+
if (taskGrade === "S" || taskGrade === "A") {
|
|
128
|
+
return {
|
|
129
|
+
budget: `${taskGrade} split`,
|
|
130
|
+
gate: "no broad refactor",
|
|
131
|
+
next: "split ticket"
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
budget: `${taskGrade} ok`,
|
|
136
|
+
gate: "keep local",
|
|
137
|
+
next: "avoid broad search"
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (taskGrade === "S") {
|
|
142
|
+
return {
|
|
143
|
+
budget: "S ok with ticket",
|
|
144
|
+
gate: "verify before execute",
|
|
145
|
+
next: "keep one active task"
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
if (taskGrade === "A") {
|
|
149
|
+
return {
|
|
150
|
+
budget: "A ok",
|
|
151
|
+
gate: "normal",
|
|
152
|
+
next: "execute"
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (taskGrade === "B") {
|
|
156
|
+
return {
|
|
157
|
+
budget: "B ok",
|
|
158
|
+
gate: "normal",
|
|
159
|
+
next: "keep concise"
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
budget: "C ok",
|
|
164
|
+
gate: "normal",
|
|
165
|
+
next: "stay compact"
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function buildConversationAdvice(state, opts = {}, budgetAdvice = {}) {
|
|
170
|
+
const turnCount = Number(opts.turnCount || 0);
|
|
171
|
+
const linkedTicketCount = Number(opts.linkedTicketCount || 0);
|
|
172
|
+
const crossWorkspace = Boolean(opts.crossWorkspace);
|
|
173
|
+
const weekly = state.weeklyRemainingPct;
|
|
174
|
+
const fiveHour = state.fiveHourRemainingPct;
|
|
175
|
+
const lowFiveHour = fiveHour !== null && fiveHour <= 15;
|
|
176
|
+
const criticalFiveHour = fiveHour !== null && fiveHour <= 8;
|
|
177
|
+
const lowWeekly = weekly !== null && weekly <= 20;
|
|
178
|
+
const highTurns = turnCount >= 25;
|
|
179
|
+
const veryHighTurns = turnCount >= 40;
|
|
180
|
+
const manyLinkedTickets = linkedTicketCount >= 3;
|
|
181
|
+
|
|
182
|
+
if (criticalFiveHour || (lowFiveHour && veryHighTurns) || (lowFiveHour && crossWorkspace && highTurns)) {
|
|
183
|
+
return {
|
|
184
|
+
conversationAction: "handoff-and-new-chat",
|
|
185
|
+
conversationGate: "handoff now",
|
|
186
|
+
conversationNext: "handoff and start new chat"
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if ((crossWorkspace && highTurns) || manyLinkedTickets || (lowWeekly && highTurns)) {
|
|
191
|
+
return {
|
|
192
|
+
conversationAction: "split-chat",
|
|
193
|
+
conversationGate: "split context",
|
|
194
|
+
conversationNext: "split ticket or new chat"
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (budgetAdvice.gate === "phase1-only") {
|
|
199
|
+
return {
|
|
200
|
+
conversationAction: "stop-in-current-chat",
|
|
201
|
+
conversationGate: "phase1-only",
|
|
202
|
+
conversationNext: "summarize and stop"
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
conversationAction: "keep-current-chat",
|
|
208
|
+
conversationGate: "keep current chat",
|
|
209
|
+
conversationNext: "continue"
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async function runUsageSet(opts) {
|
|
214
|
+
const current = loadUsageState(opts.cwd) || normalizeUsageState();
|
|
215
|
+
const hasPlatformOverride = Boolean(String(opts.platform || "").trim());
|
|
216
|
+
const nextPlatform = normalizePlatform(opts.platform || current.platform || DEFAULT_PLATFORM);
|
|
217
|
+
const incomingClient = String(opts.client || "").trim();
|
|
218
|
+
const resolvedClient = incomingClient
|
|
219
|
+
|| (hasPlatformOverride
|
|
220
|
+
? resolveClientForPlatform(nextPlatform)
|
|
221
|
+
: (current.client || resolveClientForPlatform(nextPlatform)));
|
|
222
|
+
const next = normalizeUsageState({
|
|
223
|
+
...current,
|
|
224
|
+
platform: nextPlatform,
|
|
225
|
+
client: resolvedClient,
|
|
226
|
+
agentId: opts.agentId || current.agentId,
|
|
227
|
+
weeklyRemainingPct: opts.weeklyRemaining !== null ? opts.weeklyRemaining : current.weeklyRemainingPct,
|
|
228
|
+
fiveHourRemainingPct: opts.fiveHourRemaining !== null ? opts.fiveHourRemaining : current.fiveHourRemainingPct,
|
|
229
|
+
weeklyResetAt: opts.weeklyReset || current.weeklyResetAt,
|
|
230
|
+
fiveHourResetAt: opts.fiveHourReset || current.fiveHourResetAt,
|
|
231
|
+
updatedAt: new Date().toISOString()
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
writeUsageState(opts.cwd, next);
|
|
235
|
+
|
|
236
|
+
if (opts.json) {
|
|
237
|
+
console.log(JSON.stringify(next, null, 2));
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
console.log(`usage: ${next.client} weekly ${formatPct(next.weeklyRemainingPct)}, 5h ${formatPct(next.fiveHourRemainingPct)}`);
|
|
242
|
+
if (next.agentId) console.log(`agent: ${next.agentId}`);
|
|
243
|
+
if (next.weeklyResetAt) console.log(`reset: weekly ${next.weeklyResetAt}`);
|
|
244
|
+
if (next.fiveHourResetAt) console.log(`reset: 5h ${next.fiveHourResetAt}`);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function runUsageStatus(opts) {
|
|
248
|
+
const state = loadUsageState(opts.cwd);
|
|
249
|
+
if (!state) {
|
|
250
|
+
const message = "usage not set";
|
|
251
|
+
if (opts.json) {
|
|
252
|
+
console.log(JSON.stringify({ status: message }, null, 2));
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
console.log(message);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (opts.json) {
|
|
260
|
+
console.log(JSON.stringify(state, null, 2));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
console.log(`usage: ${state.client} weekly ${formatPct(state.weeklyRemainingPct)}, 5h ${formatPct(state.fiveHourRemainingPct)}`);
|
|
265
|
+
if (state.agentId) console.log(`agent: ${state.agentId}`);
|
|
266
|
+
if (state.weeklyResetAt) console.log(`reset: weekly ${state.weeklyResetAt}`);
|
|
267
|
+
if (state.fiveHourResetAt) console.log(`reset: 5h ${state.fiveHourResetAt}`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function runUsageAdvise(opts) {
|
|
271
|
+
const state = loadUsageState(opts.cwd);
|
|
272
|
+
if (!state) {
|
|
273
|
+
const message = "usage not set";
|
|
274
|
+
if (opts.json) {
|
|
275
|
+
console.log(JSON.stringify({ status: message }, null, 2));
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
console.log(message);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const taskGrade = resolveTaskGrade(opts.taskGrade);
|
|
283
|
+
const advice = buildBudgetAdvice(state, taskGrade);
|
|
284
|
+
const conversation = buildConversationAdvice(state, opts, advice);
|
|
285
|
+
const payload = {
|
|
286
|
+
...state,
|
|
287
|
+
taskGrade: taskGrade || null,
|
|
288
|
+
taskLabel: opts.taskLabel || "",
|
|
289
|
+
turnCount: Number(opts.turnCount || 0),
|
|
290
|
+
linkedTicketCount: Number(opts.linkedTicketCount || 0),
|
|
291
|
+
crossWorkspace: Boolean(opts.crossWorkspace),
|
|
292
|
+
...advice,
|
|
293
|
+
...conversation
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
if (opts.json) {
|
|
297
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
console.log(`usage: ${state.client} weekly ${formatPct(state.weeklyRemainingPct)}, 5h ${formatPct(state.fiveHourRemainingPct)}`);
|
|
302
|
+
if (state.agentId) console.log(`agent: ${state.agentId}`);
|
|
303
|
+
console.log(`budget: ${advice.budget}`);
|
|
304
|
+
console.log(`${advice.gate === "ok" ? "next" : "gate"}: ${advice.gate === "ok" ? advice.next : advice.gate}`);
|
|
305
|
+
if (advice.gate !== "ok") console.log(`next: ${advice.next}`);
|
|
306
|
+
if (conversation.conversationAction !== "keep-current-chat") {
|
|
307
|
+
console.log(`chat: ${conversation.conversationNext}`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function formatPct(value) {
|
|
312
|
+
return value === null ? "n/a" : `${value}%`;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function getUsageReminderLine(cwd, opts = {}) {
|
|
316
|
+
const state = loadUsageState(cwd);
|
|
317
|
+
if (!state) return "";
|
|
318
|
+
const taskGrade = resolveTaskGrade(opts.taskGrade || "");
|
|
319
|
+
const advice = buildBudgetAdvice(state, taskGrade);
|
|
320
|
+
const conversation = buildConversationAdvice(state, opts, advice);
|
|
321
|
+
const gateText = advice.gate === "ok" ? advice.next : advice.gate;
|
|
322
|
+
const chatText = conversation.conversationAction === "keep-current-chat"
|
|
323
|
+
? "keep current chat"
|
|
324
|
+
: conversation.conversationNext;
|
|
325
|
+
return `usage reminder: ${state.client} weekly ${formatPct(state.weeklyRemainingPct)}, 5h ${formatPct(state.fiveHourRemainingPct)} | budget ${advice.budget} | gate ${gateText} | chat ${chatText}`;
|
|
326
|
+
}
|