opencode-windows-scheduler 1.5.1 → 1.5.4

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.
Files changed (80) hide show
  1. package/CHANGELOG.md +56 -1
  2. package/LICENSE +21 -21
  3. package/README.md +3 -2
  4. package/ROADMAP-STRATEGY.md +5 -4
  5. package/ROADMAP.md +502 -103
  6. package/dist/core/audit.d.ts +27 -0
  7. package/dist/core/audit.d.ts.map +1 -1
  8. package/dist/core/audit.js +60 -0
  9. package/dist/core/local-ai.d.ts +52 -0
  10. package/dist/core/local-ai.d.ts.map +1 -0
  11. package/dist/core/local-ai.js +119 -0
  12. package/dist/core/ps-wrapper.d.ts +2 -30
  13. package/dist/core/ps-wrapper.d.ts.map +1 -1
  14. package/dist/core/ps-wrapper.js +258 -49
  15. package/dist/core/secrets.d.ts +10 -3
  16. package/dist/core/secrets.d.ts.map +1 -1
  17. package/dist/core/secrets.js +28 -4
  18. package/dist/core/triage-chain.d.ts +34 -0
  19. package/dist/core/triage-chain.d.ts.map +1 -0
  20. package/dist/core/triage-chain.js +70 -0
  21. package/dist/core/triage-rules.d.ts +23 -0
  22. package/dist/core/triage-rules.d.ts.map +1 -0
  23. package/dist/core/triage-rules.js +174 -0
  24. package/dist/core/triage-shadow.d.ts +47 -0
  25. package/dist/core/triage-shadow.d.ts.map +1 -0
  26. package/dist/core/triage-shadow.js +48 -0
  27. package/dist/core/triage.d.ts +76 -0
  28. package/dist/core/triage.d.ts.map +1 -0
  29. package/dist/core/triage.js +271 -0
  30. package/dist/core/wrappers/bash.d.ts +1 -0
  31. package/dist/core/wrappers/bash.d.ts.map +1 -1
  32. package/dist/core/wrappers/bash.js +91 -5
  33. package/dist/gui-experimental.cjs +1 -0
  34. package/dist/i18n.d.ts.map +1 -1
  35. package/dist/i18n.js +34 -7
  36. package/dist/index.d.ts +27 -0
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +194 -6
  39. package/dist/notifications.d.ts.map +1 -1
  40. package/dist/notifications.js +9 -1
  41. package/dist/rpc.d.ts +259 -0
  42. package/dist/rpc.d.ts.map +1 -1
  43. package/dist/rpc.js +58 -0
  44. package/dist/settings.d.ts +80 -0
  45. package/dist/settings.d.ts.map +1 -1
  46. package/dist/settings.js +199 -0
  47. package/dist/workspaces.d.ts +2 -2
  48. package/dist/workspaces.d.ts.map +1 -1
  49. package/dist/workspaces.js +6 -4
  50. package/docs/ARCHITECTURE.md +69 -10
  51. package/docs/FEATURES.md +37 -7
  52. package/docs/LOCAL-AI.md +202 -0
  53. package/docs/MIGRATION.md +17 -0
  54. package/docs/README.ru.md +5 -4
  55. package/docs/spikes/local-model-structured-output.md +81 -0
  56. package/docs/spikes/ollama-qwen3-4b-smoke.json +28 -0
  57. package/docs/spikes/ollama-qwen3-4b.json +163 -0
  58. package/gui-experimental.cjs +8 -8
  59. package/locales/en.json +32 -0
  60. package/locales/ru.json +32 -0
  61. package/package.json +9 -2
  62. package/scripts/bench-local.mjs +321 -0
  63. package/scripts/spike-local-structured.mjs +182 -0
  64. package/skills/scheduled-job-best-practices/references/recipes.md +40 -0
  65. package/src/core/audit.ts +89 -0
  66. package/src/core/local-ai.ts +173 -0
  67. package/src/core/ps-wrapper.ts +265 -48
  68. package/src/core/secrets.ts +34 -10
  69. package/src/core/triage-chain.ts +116 -0
  70. package/src/core/triage-rules.ts +215 -0
  71. package/src/core/triage-shadow.ts +96 -0
  72. package/src/core/triage.ts +366 -0
  73. package/src/core/wrappers/bash.ts +112 -5
  74. package/src/gui.tsx +4761 -4533
  75. package/src/i18n.ts +32 -9
  76. package/src/index.ts +239 -4
  77. package/src/notifications.ts +16 -0
  78. package/src/rpc.ts +58 -0
  79. package/src/settings.ts +318 -0
  80. package/src/workspaces.ts +9 -4
package/locales/en.json CHANGED
@@ -95,6 +95,7 @@
95
95
  "gui.digestLastSent": "last sent: {when}",
96
96
  "gui.digestNeverSent": "never sent",
97
97
  "gui.digestWeekly": "weekly (Mon)",
98
+ "gui.days": "Days",
98
99
  "gui.dryRunButton": "🧪 Preview",
99
100
  "gui.dryRunLocalTitle": "🧪 Preview (local, based on form fields)",
100
101
  "gui.dryRunMatrixPreview": "Matrix: {count, plural, one {# clone} other {# clones}} will be created per workspace; the parent does not run.",
@@ -492,6 +493,25 @@
492
493
  "gui.zeroKeepByAge": "0 = don't delete by age",
493
494
  "gui.zeroNoLimit": "0 = no limit",
494
495
  "gui.zeroUnlimited": "(0 = no limit)",
496
+ "localAi.baseUrl": "API base URL (Ollama or OpenAI-compatible)",
497
+ "localAi.fallback": "When the model does not answer",
498
+ "localAi.fallback.rules": "Rules (fail-open)",
499
+ "localAi.fallback.skip": "No rules, notify anyway",
500
+ "localAi.hint": "An empty model name or a missing Ollama does not block notifications: the fallback handles them. A URL ending with /v1 switches to the OpenAI protocol.",
501
+ "localAi.mode": "Triage mode",
502
+ "localAi.mode.auto": "Model, then rules",
503
+ "localAi.mode.llm": "Model only",
504
+ "localAi.mode.off": "Off",
505
+ "localAi.mode.rules": "Rules only",
506
+ "localAi.model": "Model",
507
+ "localAi.modelPlaceholder": "for example qwen3:4b",
508
+ "localAi.test": "Test connection",
509
+ "localAi.testFailed": "No connection: {reason}",
510
+ "localAi.testOk": "Connected: {model}, {latency} ms",
511
+ "localAi.testing": "Testing...",
512
+ "localAi.threshold": "Confidence threshold",
513
+ "localAi.timeout": "Timeout, ms",
514
+ "localAi.title": "Local assistant (triage)",
495
515
  "matrix.aggregate.empty": "Matrix \"{parent}\" has no clones.",
496
516
  "matrix.aggregate.partial": "Partial success: {success} of {total} succeeded, {errors} failed.",
497
517
  "matrix.aggregate.running": "Running: {done} of {total}.",
@@ -579,6 +599,15 @@
579
599
  "server.methodNotAllowed": "Method not allowed.",
580
600
  "server.notFound": "Page not found.",
581
601
  "server.taskNotFound": "Task \"{task}\" not found.",
602
+ "shadow.enabled": "Shadow mode enabled",
603
+ "shadow.disabled": "Shadow mode disabled",
604
+ "shadow.active": "Shadow mode active for {days} more days",
605
+ "shadow.expired": "Shadow mode expired",
606
+ "shadow.total": "Decisions recorded: {count}",
607
+ "shadow.skips": "Would have suppressed: {count}",
608
+ "shadow.noData": "No data",
609
+ "shadow.report": "Shadow triage report",
610
+ "shadow.reset": "Reset",
582
611
  "tool.error.allowedToolsEmpty": "Task \"{task}\": an empty allowedTools is not allowed — provide a tool list or omit the field.",
583
612
  "tool.error.cronSchedule": "triggerType=cron requires a schedule cron expression.",
584
613
  "tool.error.gitCwdRequired": "Task \"{task}\": a git trigger requires a working directory or workspace.",
@@ -624,6 +653,9 @@
624
653
  "tool.event.triggered": "Tasks started: {count}.",
625
654
  "tool.history.cleanFailed": "Failed to clean history: {error}",
626
655
  "tool.history.cleaned": "Deleted {deleted} records (by age: {byAge}, by limit: {byCount}), {kept} left.",
656
+ "tool.localAi.unchanged": "Settings unchanged.",
657
+ "tool.localAi.updateFields": "For action=update pass at least one field: mode, baseUrl, model, timeoutMs, fallback, confidenceThreshold or shadow.",
658
+ "tool.localAi.updated": "Local assistant settings saved: {fields}.",
627
659
  "tool.matrix.actionName": "action={action} requires the matrix name.",
628
660
  "tool.matrix.cancelFinished": "Already finished and left untouched: {count}.",
629
661
  "tool.matrix.cancelRunning": "Stopped running: {count}.",
package/locales/ru.json CHANGED
@@ -95,6 +95,7 @@
95
95
  "gui.digestLastSent": "последняя отправка: {when}",
96
96
  "gui.digestNeverSent": "ещё не отправлялся",
97
97
  "gui.digestWeekly": "еженедельно (Пн)",
98
+ "gui.days": "Дней",
98
99
  "gui.dryRunButton": "🧪 Превью",
99
100
  "gui.dryRunLocalTitle": "🧪 Превью (локальное, по полям формы)",
100
101
  "gui.dryRunMatrixPreview": "Matrix: будет создано {count, plural, one {# клон} few {# клона} many {# клонов} other {# клонов}} по workspace; родитель сам не выполняется.",
@@ -492,6 +493,25 @@
492
493
  "gui.zeroKeepByAge": "0 = не удалять по возрасту",
493
494
  "gui.zeroNoLimit": "0 = без ограничения",
494
495
  "gui.zeroUnlimited": "(0 = без ограничений)",
496
+ "localAi.baseUrl": "Адрес API (Ollama или OpenAI-совместимый)",
497
+ "localAi.fallback": "Если модель не ответила",
498
+ "localAi.fallback.rules": "Правила (fail-open)",
499
+ "localAi.fallback.skip": "Без правил, уведомлять",
500
+ "localAi.hint": "Пустое имя модели или недоступная Ollama не блокируют уведомления: работает fallback. Адрес с /v1 переключает на протокол OpenAI.",
501
+ "localAi.mode": "Режим триажа",
502
+ "localAi.mode.auto": "Модель, затем правила",
503
+ "localAi.mode.llm": "Только модель",
504
+ "localAi.mode.off": "Выключен",
505
+ "localAi.mode.rules": "Только правила",
506
+ "localAi.model": "Модель",
507
+ "localAi.modelPlaceholder": "например qwen3:4b",
508
+ "localAi.test": "Проверить связь",
509
+ "localAi.testFailed": "Нет связи: {reason}",
510
+ "localAi.testOk": "Связь есть: {model}, {latency} мс",
511
+ "localAi.testing": "Проверяю...",
512
+ "localAi.threshold": "Порог уверенности",
513
+ "localAi.timeout": "Таймаут, мс",
514
+ "localAi.title": "Локальный ассистент (триаж)",
495
515
  "matrix.aggregate.empty": "У матрицы \"{parent}\" нет клонов.",
496
516
  "matrix.aggregate.partial": "Частичный успех: {success} из {total} успешно, {errors} с ошибкой.",
497
517
  "matrix.aggregate.running": "Выполняется: {done} из {total}.",
@@ -579,6 +599,15 @@
579
599
  "server.methodNotAllowed": "Метод не поддерживается.",
580
600
  "server.notFound": "Страница не найдена.",
581
601
  "server.taskNotFound": "Задача \"{task}\" не найдена.",
602
+ "shadow.enabled": "Shadow-режим включён",
603
+ "shadow.disabled": "Shadow-режим выключен",
604
+ "shadow.active": "Shadow-режим активен ещё {days} дней",
605
+ "shadow.expired": "Shadow-режим истёк",
606
+ "shadow.total": "Решений зафиксировано: {count}",
607
+ "shadow.skips": "Было бы подавлено: {count}",
608
+ "shadow.noData": "Нет данных",
609
+ "shadow.report": "Отчёт shadow-триажа",
610
+ "shadow.reset": "Сбросить",
582
611
  "tool.error.allowedToolsEmpty": "Задача \"{task}\": пустой allowedTools запрещён — задайте список инструментов или уберите поле.",
583
612
  "tool.error.cronSchedule": "Для triggerType=cron нужно cron-выражение schedule.",
584
613
  "tool.error.gitCwdRequired": "Задача \"{task}\": для git-триггера нужна рабочая директория или workspace.",
@@ -624,6 +653,9 @@
624
653
  "tool.event.triggered": "Запущено задач: {count}.",
625
654
  "tool.history.cleanFailed": "Не удалось очистить историю: {error}",
626
655
  "tool.history.cleaned": "Удалено {deleted} записей (по возрасту: {byAge}, по лимиту: {byCount}), осталось {kept}.",
656
+ "tool.localAi.unchanged": "Настройки не изменились.",
657
+ "tool.localAi.updateFields": "Для action=update передайте хотя бы одно поле: mode, baseUrl, model, timeoutMs, fallback, confidenceThreshold или shadow.",
658
+ "tool.localAi.updated": "Настройки локального ассистента сохранены: {fields}.",
627
659
  "tool.matrix.actionName": "Для action={action} нужен name матрицы.",
628
660
  "tool.matrix.cancelFinished": "Уже завершены и не тронуты: {count}.",
629
661
  "tool.matrix.cancelRunning": "Остановлено выполнявшихся: {count}.",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-windows-scheduler",
3
- "version": "1.5.1",
3
+ "version": "1.5.4",
4
4
  "description": "OpenCode v2 plugin for scheduling and orchestrating AI agent jobs: cron, one-shot runs, DAG pipelines, matrix builds, quality gates, SQLite history, metrics and a local web dashboard. Windows, macOS and Linux. Free and open source (MIT).",
5
5
  "license": "MIT",
6
6
  "author": "opencode-windows-scheduler contributors",
@@ -68,9 +68,10 @@
68
68
  "build": "tsc",
69
69
  "build:gui": "vite build",
70
70
  "demo": "node scripts/demo-dashboard.mjs",
71
+ "bench:local": "node scripts/bench-local.mjs",
71
72
  "prepare": "npm run build",
72
73
  "prepublishOnly": "npm run build && npm run build:gui",
73
- "test": "node test-sqlite.mjs && node test-crash-recovery.mjs && node test-workspaces.mjs && node test-role-scoped.mjs && node test-dry-run.mjs && node test-matrix.mjs && node test-matrix-lifecycle.mjs && node test-matrix-sync.mjs && node test-retention.mjs && node test-metrics.mjs && node test-schedule-v2.mjs && node test-notifications-v2.mjs && node test-notifications-v3.mjs && node test-secrets.mjs && node test-audit.mjs && node test-profiles.mjs && node test-env.mjs && node test-agent-tools.mjs && node test-skill.mjs && node test-integrations.mjs && node test-one-shot.mjs && node test-session-continuity.mjs && node test-task-status.mjs && node test-jobs-view.mjs && node test-dag.mjs && node test-dashboard.mjs && node test-dashboard-dag.mjs && node test-dashboard-json.mjs && node test-docs.mjs && node test-glob.mjs && node test-git-trigger.mjs && node test-window-v3.mjs && node test-i18n.mjs",
74
+ "test": "node test-sqlite.mjs && node test-crash-recovery.mjs && node test-workspaces.mjs && node test-role-scoped.mjs && node test-dry-run.mjs && node test-matrix.mjs && node test-matrix-lifecycle.mjs && node test-matrix-sync.mjs && node test-retention.mjs && node test-metrics.mjs && node test-schedule-v2.mjs && node test-notifications-v2.mjs && node test-notifications-v3.mjs && node test-triage.mjs && node test-triage-rules.mjs && node test-triage-chain.mjs && node test-triage-shadow.mjs && node test-local-ai.mjs && node test-secrets.mjs && node test-audit.mjs && node test-profiles.mjs && node test-env.mjs && node test-agent-tools.mjs && node test-skill.mjs && node test-integrations.mjs && node test-one-shot.mjs && node test-session-continuity.mjs && node test-task-status.mjs && node test-jobs-view.mjs && node test-dag.mjs && node test-dashboard.mjs && node test-dashboard-dag.mjs && node test-dashboard-json.mjs && node test-docs.mjs && node test-glob.mjs && node test-git-trigger.mjs && node test-window-v3.mjs && node test-i18n.mjs && node test-gui-bundle.mjs && node test-execution-hardening.mjs",
74
75
  "test:sqlite": "node test-sqlite.mjs",
75
76
  "test:crash": "node test-crash-recovery.mjs",
76
77
  "test:workspaces": "node test-workspaces.mjs",
@@ -84,6 +85,11 @@
84
85
  "test:schedule-v2": "node test-schedule-v2.mjs",
85
86
  "test:notifications-v2": "node test-notifications-v2.mjs",
86
87
  "test:notifications-v3": "node test-notifications-v3.mjs",
88
+ "test:triage": "node test-triage.mjs",
89
+ "test:triage-rules": "node test-triage-rules.mjs",
90
+ "test:triage-chain": "node test-triage-chain.mjs",
91
+ "test:triage-shadow": "node test-triage-shadow.mjs",
92
+ "test:local-ai": "node test-local-ai.mjs",
87
93
  "test:secrets": "node test-secrets.mjs",
88
94
  "test:audit": "node test-audit.mjs",
89
95
  "test:profiles": "node test-profiles.mjs",
@@ -104,6 +110,7 @@
104
110
  "test:git-trigger": "node test-git-trigger.mjs",
105
111
  "test:window-v3": "node test-window-v3.mjs",
106
112
  "test:i18n": "node test-i18n.mjs",
113
+ "test:gui-bundle": "node test-gui-bundle.mjs",
107
114
  "test:en": "node scripts/test-locale.mjs en"
108
115
  },
109
116
  "dependencies": {
@@ -0,0 +1,321 @@
1
+ /**
2
+ * Бенчмарк локальной модели для триажа уведомлений (Спринт 30.8).
3
+ *
4
+ * Запуск:
5
+ * node scripts/bench-local.mjs --model qwen3:4b --runs 3
6
+ *
7
+ * Скрипт не входит в `npm test`: он требует запущенную Ollama и скачанную
8
+ * модель. Цель — измерить скорость (tok/s), латентность и долю схемо-валидных
9
+ * ответов, подобрать тир по объёму RAM и выдать версионированный JSON,
10
+ * пригодный для краудсорсинга (`--out`).
11
+ *
12
+ * Без Ollama скрипт не падает молча: он печатает понятную ошибку и всё равно
13
+ * формирует валидный JSON с `available: false`, чтобы замер можно было
14
+ * приложить к отчёту.
15
+ */
16
+ import fs from "node:fs";
17
+ import os from "node:os";
18
+ import path from "node:path";
19
+
20
+ const SCHEMA_VERSION = "1";
21
+
22
+ const DEFAULTS = {
23
+ baseUrl: "http://127.0.0.1:11434",
24
+ model: "qwen3:4b",
25
+ runs: 3,
26
+ timeoutMs: 120000,
27
+ out: "",
28
+ };
29
+
30
+ function parseArgs(argv) {
31
+ const args = { ...DEFAULTS };
32
+ for (let i = 0; i < argv.length; i += 1) {
33
+ const key = argv[i];
34
+ const value = argv[i + 1];
35
+ if (key === "--base-url") args.baseUrl = value;
36
+ else if (key === "--model") args.model = value;
37
+ else if (key === "--runs") args.runs = Number(value);
38
+ else if (key === "--timeout") args.timeoutMs = Number(value);
39
+ else if (key === "--out") args.out = value;
40
+ else if (key === "--help" || key === "-h") {
41
+ console.log(
42
+ "node scripts/bench-local.mjs [--model qwen3:4b] [--runs 3] [--base-url URL] [--timeout MS] [--out FILE]"
43
+ );
44
+ process.exit(0);
45
+ } else {
46
+ console.error(`Неизвестный аргумент: ${key}`);
47
+ process.exit(2);
48
+ }
49
+ i += 1;
50
+ }
51
+ if (!Number.isInteger(args.runs) || args.runs < 1 || args.runs > 50) {
52
+ console.error("--runs должен быть целым числом от 1 до 50.");
53
+ process.exit(2);
54
+ }
55
+ if (!Number.isInteger(args.timeoutMs) || args.timeoutMs < 1000 || args.timeoutMs > 600000) {
56
+ console.error("--timeout должен быть целым числом от 1000 до 600000 мс.");
57
+ process.exit(2);
58
+ }
59
+ return args;
60
+ }
61
+
62
+ const schema = {
63
+ type: "object",
64
+ properties: {
65
+ category: { type: "string", enum: ["quality", "timeout", "permission", "network", "unknown"] },
66
+ severity: { type: "string", enum: ["low", "medium", "high"] },
67
+ confidence: { type: "number", minimum: 0, maximum: 1 },
68
+ reason: { type: "string" },
69
+ },
70
+ required: ["category", "severity", "confidence", "reason"],
71
+ };
72
+
73
+ const prompt = `Ты классифицируешь уведомление планировщика задач.
74
+ Верни только JSON без markdown и пояснений по схеме:
75
+ {"category":"quality|timeout|permission|network|unknown","severity":"low|medium|high","confidence":0..1,"reason":"кратко"}
76
+
77
+ Уведомление:
78
+ Задача "nightly-tests" упала за 2.3s. Quality Gate вернул код 1: 3 теста не прошли в auth-модуле.`;
79
+
80
+ function validate(value) {
81
+ if (!value || typeof value !== "object" || Array.isArray(value)) return "not an object";
82
+ for (const key of schema.required) {
83
+ if (!(key in value)) return `missing ${key}`;
84
+ }
85
+ if (!schema.properties.category.enum.includes(value.category)) return "bad category";
86
+ if (!schema.properties.severity.enum.includes(value.severity)) return "bad severity";
87
+ if (typeof value.confidence !== "number" || value.confidence < 0 || value.confidence > 1) return "bad confidence";
88
+ if (typeof value.reason !== "string" || value.reason.trim() === "") return "bad reason";
89
+ return "";
90
+ }
91
+
92
+ /** Тир по объёму RAM: рекомендация модели и причина выбора. */
93
+ function pickTier(ramGb) {
94
+ if (ramGb < 8) {
95
+ return {
96
+ id: "rules-only",
97
+ recommendedModel: "qwen3:0.6b",
98
+ reason: `RAM ${ramGb} ГБ: локальная модель не помещается с запасом — работают только правила.`,
99
+ };
100
+ }
101
+ if (ramGb < 15) {
102
+ return {
103
+ id: "light",
104
+ recommendedModel: "qwen3:1.7b",
105
+ reason: `RAM ${ramGb} ГБ: хватает на маленькую модель, ожидайте скромную точность.`,
106
+ };
107
+ }
108
+ if (ramGb < 30) {
109
+ return {
110
+ id: "balanced",
111
+ recommendedModel: "qwen3:4b",
112
+ reason: `RAM ${ramGb} ГБ: рекомендуемый тир для триажа — qwen3:4b на CPU.`,
113
+ };
114
+ }
115
+ return {
116
+ id: "max",
117
+ recommendedModel: "qwen3:8b",
118
+ reason: `RAM ${ramGb} ГБ: можно позволить модель крупнее для лучшей точности.`,
119
+ };
120
+ }
121
+
122
+ function round(value, digits = 2) {
123
+ return Number.isFinite(value) ? Number(value.toFixed(digits)) : null;
124
+ }
125
+
126
+ function percentile(sorted, fraction) {
127
+ if (sorted.length === 0) return null;
128
+ return sorted[Math.max(0, Math.ceil(sorted.length * fraction) - 1)];
129
+ }
130
+
131
+ function machineInfo() {
132
+ const cpus = os.cpus();
133
+ return {
134
+ platform: os.platform(),
135
+ osRelease: os.release(),
136
+ arch: os.arch(),
137
+ cpu: cpus.length > 0 ? cpus[0].model.trim() : "unknown",
138
+ cpuCount: cpus.length,
139
+ ramGb: round(os.totalmem() / 1024 ** 3, 1),
140
+ nodeVersion: process.version,
141
+ };
142
+ }
143
+
144
+ async function loadModel(baseUrl, model) {
145
+ const response = await fetch(`${baseUrl}/api/tags`, { signal: AbortSignal.timeout(10000) });
146
+ if (!response.ok) throw new Error(`/api/tags: HTTP ${response.status}`);
147
+ const data = await response.json();
148
+ const names = (data.models ?? []).map((item) => item.name);
149
+ if (names.length === 0) {
150
+ throw new Error(`Ollama запущена, но моделей нет: выполните "ollama pull ${model}"`);
151
+ }
152
+ if (!names.some((name) => name === model || name === `${model}:latest`)) {
153
+ throw new Error(`Модель "${model}" не найдена. Доступны: ${names.join(", ")}`);
154
+ }
155
+ }
156
+
157
+ /** Один прогон: латентность, счётчики токенов и валидность схемы. */
158
+ async function runOnce(args, index) {
159
+ const started = Date.now();
160
+ const body = {
161
+ model: args.model,
162
+ messages: [{ role: "user", content: prompt }],
163
+ stream: false,
164
+ format: schema,
165
+ options: { temperature: 0, seed: index + 1 },
166
+ };
167
+ let response;
168
+ try {
169
+ response = await fetch(`${args.baseUrl}/api/chat`, {
170
+ method: "POST",
171
+ headers: { "content-type": "application/json" },
172
+ body: JSON.stringify(body),
173
+ signal: AbortSignal.timeout(args.timeoutMs),
174
+ });
175
+ } catch (error) {
176
+ return {
177
+ run: index + 1,
178
+ ok: false,
179
+ jsonValid: false,
180
+ schemaValid: false,
181
+ latencyMs: Date.now() - started,
182
+ evalCount: null,
183
+ evalMs: null,
184
+ tokPerSec: null,
185
+ error: error instanceof Error ? error.message : String(error),
186
+ };
187
+ }
188
+ const latencyMs = Date.now() - started;
189
+ if (!response.ok) {
190
+ return { run: index + 1, ok: false, jsonValid: false, schemaValid: false, latencyMs, evalCount: null, evalMs: null, tokPerSec: null, error: `HTTP ${response.status}` };
191
+ }
192
+ const data = await response.json();
193
+ const content = data?.message?.content ?? "";
194
+ const evalCount = typeof data?.eval_count === "number" ? data.eval_count : null;
195
+ const evalNs = typeof data?.eval_duration === "number" ? data.eval_duration : null;
196
+ const evalMs = evalNs !== null ? Math.round(evalNs / 1e6) : null;
197
+ const tokPerSec = evalCount !== null && evalNs !== null && evalNs > 0 ? (evalCount / evalNs) * 1e9 : null;
198
+
199
+ let parsed;
200
+ try {
201
+ parsed = JSON.parse(content);
202
+ } catch {
203
+ return { run: index + 1, ok: false, jsonValid: false, schemaValid: false, latencyMs, evalCount, evalMs, tokPerSec, content, error: "invalid JSON" };
204
+ }
205
+ const error = validate(parsed);
206
+ return {
207
+ run: index + 1,
208
+ ok: !error,
209
+ jsonValid: true,
210
+ schemaValid: !error,
211
+ latencyMs,
212
+ evalCount,
213
+ evalMs,
214
+ tokPerSec: round(tokPerSec),
215
+ content,
216
+ parsed,
217
+ error,
218
+ };
219
+ }
220
+
221
+ function buildReport({ args, machine, tier, available, error, results }) {
222
+ const completed = results.filter((item) => item.schemaValid);
223
+ const validRuns = results.filter((item) => item.jsonValid);
224
+ const latencies = results.map((item) => item.latencyMs).sort((a, b) => a - b);
225
+ const throughput = results
226
+ .map((item) => item.tokPerSec)
227
+ .filter((value) => typeof value === "number")
228
+ .sort((a, b) => a - b);
229
+ const avg = (values) => (values.length === 0 ? null : values.reduce((sum, value) => sum + value, 0) / values.length);
230
+
231
+ return {
232
+ schemaVersion: SCHEMA_VERSION,
233
+ generatedAt: new Date().toISOString(),
234
+ available,
235
+ error: error ?? null,
236
+ machine,
237
+ tier,
238
+ baseUrl: args.baseUrl,
239
+ model: args.model,
240
+ runs: args.runs,
241
+ metrics: {
242
+ schemaValid: completed.length,
243
+ schemaValidRate: results.length === 0 ? 0 : round(completed.length / results.length, 2),
244
+ jsonValid: validRuns.length,
245
+ jsonValidRate: results.length === 0 ? 0 : round(validRuns.length / results.length, 2),
246
+ avgLatencyMs: latencies.length === 0 ? null : Math.round(avg(latencies)),
247
+ p95LatencyMs: percentile(latencies, 0.95),
248
+ avgTokPerSec: throughput.length === 0 ? null : round(avg(throughput), 1),
249
+ p95TokPerSec: percentile(throughput, 0.95) === null ? null : round(percentile(throughput, 0.95), 1),
250
+ },
251
+ notes: [
252
+ "tok/s считается по eval_count / eval_duration из ответа Ollama /api/chat.",
253
+ "Тир подобран по объёму RAM; модель можно переопределить флагом --model.",
254
+ "Формат версионирован (schemaVersion) для краудсорсинга замеров.",
255
+ ],
256
+ results,
257
+ };
258
+ }
259
+
260
+ const args = parseArgs(process.argv.slice(2));
261
+ const machine = machineInfo();
262
+ const tier = pickTier(machine.ramGb);
263
+
264
+ let available = true;
265
+ let availabilityError = null;
266
+ try {
267
+ await loadModel(args.baseUrl, args.model);
268
+ } catch (error) {
269
+ available = false;
270
+ availabilityError = error instanceof Error ? error.message : String(error);
271
+ console.error(`Не удалось подключиться к Ollama: ${availabilityError}`);
272
+ console.error(`Бенчмарк заблокирован: установите Ollama, выполните "ollama serve" и "ollama pull ${args.model}".`);
273
+ }
274
+
275
+ const results = [];
276
+ if (available) {
277
+ for (let i = 0; i < args.runs; i += 1) {
278
+ const result = await runOnce(args, i);
279
+ results.push(result);
280
+ const mark = result.schemaValid ? "OK" : "FAIL";
281
+ console.log(
282
+ `${result.run}/${args.runs} ${mark} ${result.latencyMs}ms${result.tokPerSec ? ` ${result.tokPerSec} tok/s` : ""}${result.error ? ` — ${result.error}` : ""}`
283
+ );
284
+ }
285
+ }
286
+
287
+ const report = buildReport({ args, machine, tier, available, error: availabilityError, results });
288
+
289
+ console.log("");
290
+ console.log(`Машина: ${machine.cpu} × ${machine.cpuCount}, ${machine.ramGb} ГБ RAM, ${machine.platform}/${machine.arch}`);
291
+ console.log(`Тир: ${tier.id} → рекомендуется ${tier.recommendedModel}`);
292
+ console.log(tier.reason);
293
+ if (available) {
294
+ console.log(
295
+ `Схема: ${report.metrics.schemaValid}/${args.runs} (${Math.round(report.metrics.schemaValidRate * 100)}%); JSON: ${report.metrics.jsonValid}/${args.runs}`
296
+ );
297
+ const speed = report.metrics.avgTokPerSec === null ? "n/a" : `${report.metrics.avgTokPerSec} tok/s`;
298
+ console.log(`Латентность: avg ${report.metrics.avgLatencyMs}ms, p95 ${report.metrics.p95LatencyMs}ms; скорость: ${speed}`);
299
+ if (report.metrics.schemaValidRate >= 0.9) {
300
+ console.log("Вывод: модель подходит для триажа, валидацию схемы всё равно оставить.");
301
+ } else if (report.metrics.schemaValidRate >= 0.7) {
302
+ console.log("Вывод: нужен один retry и fallback на правила.");
303
+ } else {
304
+ console.log("Вывод: модель не подходит для критичных вердиктов — только правила или модель крупнее.");
305
+ }
306
+ } else {
307
+ console.log("Вывод: замер не выполнен, JSON всё равно сформирован для отчёта.");
308
+ }
309
+
310
+ if (args.out) {
311
+ const target = path.resolve(args.out);
312
+ fs.mkdirSync(path.dirname(target), { recursive: true });
313
+ fs.writeFileSync(target, `${JSON.stringify(report, null, 2)}\n`, "utf8");
314
+ console.log(`Результат: ${target}`);
315
+ } else {
316
+ console.log("");
317
+ console.log(JSON.stringify(report, null, 2));
318
+ }
319
+
320
+ if (!available) process.exit(2);
321
+ process.exit(report.metrics.schemaValidRate >= 0.7 ? 0 : 1);
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Спайк structured output локальной модели через Ollama.
3
+ *
4
+ * Запуск:
5
+ * node scripts/spike-local-structured.mjs --model qwen3:4b --runs 10
6
+ *
7
+ * Скрипт не входит в `npm test`: он требует запущенную Ollama и скачанную
8
+ * модель. Цель — получить воспроизводимый ответ на вопрос, можно ли доверять
9
+ * JSON 4B-модели без retry. Результат печатается в stdout и, если указан
10
+ * `--out`, сохраняется в JSON.
11
+ */
12
+ import fs from "node:fs";
13
+ import path from "node:path";
14
+
15
+ const DEFAULTS = {
16
+ baseUrl: "http://127.0.0.1:11434",
17
+ model: "qwen3:4b",
18
+ runs: 10,
19
+ timeoutMs: 60000,
20
+ out: "",
21
+ };
22
+
23
+ function parseArgs(argv) {
24
+ const args = { ...DEFAULTS };
25
+ for (let i = 0; i < argv.length; i += 1) {
26
+ const key = argv[i];
27
+ const value = argv[i + 1];
28
+ if (key === "--base-url") args.baseUrl = value;
29
+ else if (key === "--model") args.model = value;
30
+ else if (key === "--runs") args.runs = Number(value);
31
+ else if (key === "--timeout") args.timeoutMs = Number(value);
32
+ else if (key === "--out") args.out = value;
33
+ else if (key === "--help" || key === "-h") {
34
+ console.log("node scripts/spike-local-structured.mjs [--model qwen3:4b] [--runs 10] [--base-url URL] [--timeout MS] [--out FILE]");
35
+ process.exit(0);
36
+ } else {
37
+ console.error(`Неизвестный аргумент: ${key}`);
38
+ process.exit(2);
39
+ }
40
+ i += 1;
41
+ }
42
+ if (!Number.isInteger(args.runs) || args.runs < 1 || args.runs > 100) {
43
+ console.error("--runs должен быть целым числом от 1 до 100.");
44
+ process.exit(2);
45
+ }
46
+ return args;
47
+ }
48
+
49
+ const schema = {
50
+ type: "object",
51
+ properties: {
52
+ category: { type: "string", enum: ["quality", "timeout", "permission", "network", "unknown"] },
53
+ severity: { type: "string", enum: ["low", "medium", "high"] },
54
+ confidence: { type: "number", minimum: 0, maximum: 1 },
55
+ reason: { type: "string" },
56
+ },
57
+ required: ["category", "severity", "confidence", "reason"],
58
+ };
59
+
60
+ const prompt = `Ты классифицируешь уведомление планировщика задач.
61
+ Верни только JSON без markdown и пояснений по схеме:
62
+ {"category":"quality|timeout|permission|network|unknown","severity":"low|medium|high","confidence":0..1,"reason":"кратко"}
63
+
64
+ Уведомление:
65
+ Задача "nightly-tests" упала за 2.3s. Quality Gate вернул код 1: 3 теста не прошли в auth-модуле.`;
66
+
67
+ function validate(value) {
68
+ if (!value || typeof value !== "object" || Array.isArray(value)) return "not an object";
69
+ for (const key of schema.required) {
70
+ if (!(key in value)) return `missing ${key}`;
71
+ }
72
+ if (!schema.properties.category.enum.includes(value.category)) return "bad category";
73
+ if (!schema.properties.severity.enum.includes(value.severity)) return "bad severity";
74
+ if (typeof value.confidence !== "number" || value.confidence < 0 || value.confidence > 1) return "bad confidence";
75
+ if (typeof value.reason !== "string" || value.reason.trim() === "") return "bad reason";
76
+ return "";
77
+ }
78
+
79
+ async function loadModel(baseUrl, model) {
80
+ const response = await fetch(`${baseUrl}/api/tags`, { signal: AbortSignal.timeout(10000) });
81
+ if (!response.ok) throw new Error(`/api/tags: HTTP ${response.status}`);
82
+ const data = await response.json();
83
+ const names = (data.models ?? []).map((item) => item.name);
84
+ if (names.length === 0) throw new Error("Ollama запущена, но моделей нет: выполните ollama pull " + model);
85
+ if (!names.some((name) => name === model || name === `${model}:latest`)) {
86
+ throw new Error(`Модель "${model}" не найдена. Доступны: ${names.join(", ")}`);
87
+ }
88
+ }
89
+
90
+ async function runOnce(args, index) {
91
+ const started = Date.now();
92
+ const body = {
93
+ model: args.model,
94
+ messages: [{ role: "user", content: prompt }],
95
+ stream: false,
96
+ format: schema,
97
+ options: { temperature: 0, seed: index + 1 },
98
+ };
99
+ const response = await fetch(`${args.baseUrl}/api/chat`, {
100
+ method: "POST",
101
+ headers: { "content-type": "application/json" },
102
+ body: JSON.stringify(body),
103
+ signal: AbortSignal.timeout(args.timeoutMs),
104
+ });
105
+ const latencyMs = Date.now() - started;
106
+ if (!response.ok) {
107
+ return { run: index + 1, ok: false, jsonValid: false, schemaValid: false, latencyMs, error: `HTTP ${response.status}` };
108
+ }
109
+ const data = await response.json();
110
+ const content = data?.message?.content ?? "";
111
+ let parsed;
112
+ try {
113
+ parsed = JSON.parse(content);
114
+ } catch {
115
+ return { run: index + 1, ok: false, jsonValid: false, schemaValid: false, latencyMs, content, error: "invalid JSON" };
116
+ }
117
+ const error = validate(parsed);
118
+ return {
119
+ run: index + 1,
120
+ ok: !error,
121
+ jsonValid: true,
122
+ schemaValid: !error,
123
+ latencyMs,
124
+ content,
125
+ parsed,
126
+ error,
127
+ };
128
+ }
129
+
130
+ const args = parseArgs(process.argv.slice(2));
131
+
132
+ try {
133
+ await loadModel(args.baseUrl, args.model);
134
+ } catch (error) {
135
+ console.error(`Не удалось подключиться к Ollama: ${error.message}`);
136
+ console.error("Спайк заблокирован: установите Ollama, выполните `ollama serve` и `ollama pull " + args.model + "`.");
137
+ process.exit(2);
138
+ }
139
+
140
+ const results = [];
141
+ for (let i = 0; i < args.runs; i += 1) {
142
+ const result = await runOnce(args, i);
143
+ results.push(result);
144
+ const mark = result.schemaValid ? "OK" : "FAIL";
145
+ console.log(`${result.run}/${args.runs} ${mark} ${result.latencyMs}ms${result.error ? ` — ${result.error}` : ""}`);
146
+ }
147
+
148
+ const jsonValid = results.filter((item) => item.jsonValid).length;
149
+ const schemaValid = results.filter((item) => item.schemaValid).length;
150
+ const latencies = results.map((item) => item.latencyMs).sort((a, b) => a - b);
151
+ const avg = Math.round(latencies.reduce((sum, value) => sum + value, 0) / latencies.length);
152
+ const p95 = latencies[Math.max(0, Math.ceil(latencies.length * 0.95) - 1)];
153
+
154
+ const summary = {
155
+ generatedAt: new Date().toISOString(),
156
+ baseUrl: args.baseUrl,
157
+ model: args.model,
158
+ runs: args.runs,
159
+ jsonValid,
160
+ schemaValid,
161
+ schemaValidRate: Number((schemaValid / args.runs).toFixed(2)),
162
+ avgLatencyMs: avg,
163
+ p95LatencyMs: p95,
164
+ results,
165
+ };
166
+
167
+ console.log("");
168
+ console.log(`Модель: ${args.model}`);
169
+ console.log(`JSON валиден: ${jsonValid}/${args.runs}; схема: ${schemaValid}/${args.runs} (${Math.round(summary.schemaValidRate * 100)}%)`);
170
+ console.log(`Латентность: avg ${avg}ms, p95 ${p95}ms`);
171
+ if (schemaValid >= 9) console.log("Вывод: 4B можно использовать без retry, но валидацию схемы оставить.");
172
+ else if (schemaValid >= 7) console.log("Вывод: нужен ровно один retry и fallback на правила.");
173
+ else console.log("Вывод: 4B нельзя использовать для критичных вердиктов; только правила или модель крупнее.");
174
+
175
+ if (args.out) {
176
+ const target = path.resolve(args.out);
177
+ fs.mkdirSync(path.dirname(target), { recursive: true });
178
+ fs.writeFileSync(target, `${JSON.stringify(summary, null, 2)}\n`, "utf8");
179
+ console.log(`Результат: ${target}`);
180
+ }
181
+
182
+ process.exit(schemaValid >= 7 ? 0 : 1);