@linxin666/dsh-liangshen 0.4.7 → 0.4.9

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 (35) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +10 -8
  3. package/README.zh.md +10 -8
  4. package/lib/client.js +102 -55
  5. package/lib/client.js.map +1 -1
  6. package/lib/index.js +9 -1
  7. package/lib/types/client/PluginSettingsCard.d.ts +60 -0
  8. package/package.json +15 -15
  9. package/presets/liangshen/agent.cordis.yml +76 -18
  10. package/presets/liangshen/dispatcher-rules.mjs +128 -47
  11. package/presets/liangshen/fact-ledger.mjs +14 -0
  12. package/presets/liangshen/subagent-routes.mjs +396 -0
  13. package/presets/liangshen/tool-catalog.mjs +7 -4
  14. package/src/client/PluginSettingsCard.tsx +140 -20
  15. package/src/client/settings-card.module.css +141 -11
  16. package/src/composition.test.ts +20 -0
  17. package/src/composition.ts +10 -2
  18. package/tools/benchmark-live-run.mjs +116 -14
  19. package/tools/benchmark-report.mjs +44 -0
  20. package/tools/check-dispatcher-rules.mjs +130 -0
  21. package/tools/probe-selftest.mjs +130 -0
  22. package/tools/probes/budget-ledger/env.mjs +103 -0
  23. package/tools/probes/budget-ledger/grade.mjs +31 -0
  24. package/tools/probes/budget-ledger/strategies.mjs +25 -0
  25. package/tools/probes/oncall-backlog/env.mjs +141 -0
  26. package/tools/probes/oncall-backlog/grade.mjs +28 -0
  27. package/tools/probes/oncall-backlog/strategies.mjs +37 -0
  28. package/tools/probes/prior-override/env.mjs +153 -0
  29. package/tools/probes/prior-override/grade.mjs +31 -0
  30. package/tools/probes/prior-override/strategies.mjs +30 -0
  31. package/tools/probes/refresh-quota/env.mjs +140 -0
  32. package/tools/probes/refresh-quota/grade.mjs +30 -0
  33. package/tools/probes/refresh-quota/strategies.mjs +29 -0
  34. package/tools/probes/shared.mjs +75 -0
  35. package/tools/tasks/liangshen-v41-probes.json +59 -0
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The oracle for probe-budget-ledger: the fault is reachable in four calls
3
+ * (suspect, two reads, submit), and a full sweep does not fit in twelve.
4
+ */
5
+ import { loadEpisode, report } from './shared.mjs'
6
+
7
+ export function grade() {
8
+ const episode = loadEpisode(() => ({}))
9
+ const submitted = episode.submitted === true
10
+ const correct = submitted && episode.submitId === 's13'
11
+ const metrics = {
12
+ submitted: submitted ? 1 : 0,
13
+ correct: correct ? 1 : 0,
14
+ callsUsed: Number(episode.used ?? 0),
15
+ remainingAtSubmit: submitted ? Number(episode.remainingAtSubmit ?? 0) : -1,
16
+ hypothesisFirst: episode.usedSuspect === true ? 1 : 0,
17
+ }
18
+ return {
19
+ passed: correct,
20
+ score: correct ? 100 : 0,
21
+ metrics,
22
+ detail: correct
23
+ ? 'submitted inside the budget with ' + metrics.remainingAtSubmit + ' calls to spare'
24
+ : submitted
25
+ ? 'submitted the wrong service'
26
+ : 'the budget was spent before anything was submitted',
27
+ }
28
+ }
29
+
30
+ const direct = process.argv[1] !== undefined && process.argv[1].endsWith('grade.mjs')
31
+ if (direct) report(grade())
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Two scripted searches: sweep the services until the budget is gone, versus
3
+ * ask for the ranked candidates and read only those.
4
+ */
5
+ export const strategies = {
6
+ bad: {
7
+ label: 'sweeps the services in order until the budget is spent',
8
+ steps(run) {
9
+ const out = []
10
+ for (let index = 1; index <= 12; index += 1) out.push(run(['read', '--id', 's' + index]))
11
+ return out
12
+ },
13
+ },
14
+ good: {
15
+ label: 'reads the ranked candidates and submits the faulty one',
16
+ steps(run) {
17
+ return [
18
+ run(['suspect']),
19
+ run(['read', '--id', 's11']),
20
+ run(['read', '--id', 's13']),
21
+ run(['submit', '--id', 's13']),
22
+ ]
23
+ },
24
+ },
25
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * probe-oncall-backlog -- twelve alerts, two handled per step, ten steps.
3
+ *
4
+ * Half the alerts are low severity, and a low-severity alert escalates if
5
+ * nobody looks at it before its deadline. A dispatcher that sorts by severity
6
+ * finishes with a clean P1 board and six escalations; one that sorts by what
7
+ * crosses a threshold first finishes with none. The episode counts the
8
+ * escalations, which is the behaviour the rule set now asks for every turn.
9
+ */
10
+ import { listOf, loadEpisode, parseArgs, saveEpisode } from './shared.mjs'
11
+
12
+ const STEPS = 10
13
+ const HANDLES_PER_STEP = 2
14
+ const HANDLE_COST = 5
15
+ const ESCALATION_COST = 60
16
+ const ALERTS = [
17
+ { id: 'a1', sev: 'P1', appear: 1, due: 5 },
18
+ { id: 'a2', sev: 'P3', appear: 1, due: 4 },
19
+ { id: 'a3', sev: 'P3', appear: 2, due: 5 },
20
+ { id: 'a4', sev: 'P2', appear: 2, due: 6 },
21
+ { id: 'a5', sev: 'P3', appear: 3, due: 6 },
22
+ { id: 'a6', sev: 'P1', appear: 3, due: 7 },
23
+ { id: 'a7', sev: 'P3', appear: 4, due: 7 },
24
+ { id: 'a8', sev: 'P2', appear: 4, due: 8 },
25
+ { id: 'a9', sev: 'P3', appear: 5, due: 8 },
26
+ { id: 'a10', sev: 'P3', appear: 6, due: 9 },
27
+ { id: 'a11', sev: 'P2', appear: 6, due: 10 },
28
+ { id: 'a12', sev: 'P2', appear: 7, due: 10 },
29
+ ]
30
+
31
+ const HELP = [
32
+ 'on-call board',
33
+ ' node env.mjs status active alerts with their escalate deadline',
34
+ ' node env.mjs handle --ids a2,a3 handle up to ' + HANDLES_PER_STEP + ' alerts (costs ' + HANDLE_COST + ' each)',
35
+ ' node env.mjs next advance one step',
36
+ '',
37
+ 'An alert not handled by its deadline escalates: +' + ESCALATION_COST + ' and it leaves the board. The run ends after ' + STEPS + ' steps.',
38
+ ].join('\n')
39
+
40
+ function initial() {
41
+ return { step: 1, handled: [], escalated: [], handlesLeft: HANDLES_PER_STEP, cost: 0, done: false }
42
+ }
43
+
44
+ function isOpen(episode, alert) {
45
+ return !episode.handled.includes(alert.id) && !episode.escalated.includes(alert.id)
46
+ }
47
+
48
+ function active(episode) {
49
+ return ALERTS.filter((alert) => alert.appear <= episode.step && isOpen(episode, alert))
50
+ }
51
+
52
+ function escalatePast(episode) {
53
+ for (const alert of ALERTS) {
54
+ if (alert.appear > episode.step) continue
55
+ if (isOpen(episode, alert) !== true) continue
56
+ if (alert.due < episode.step) {
57
+ episode.escalated.push(alert.id)
58
+ episode.cost += ESCALATION_COST
59
+ }
60
+ }
61
+ }
62
+
63
+ function escalateAll(episode) {
64
+ for (const alert of active(episode)) {
65
+ episode.escalated.push(alert.id)
66
+ episode.cost += ESCALATION_COST
67
+ }
68
+ episode.done = true
69
+ }
70
+
71
+ function line(episode) {
72
+ return 'step ' + episode.step + '/' + STEPS + ' | handled ' + episode.handled.length + ' | escalated ' + episode.escalated.length + ' | cost ' + episode.cost
73
+ }
74
+
75
+ const args = parseArgs(process.argv.slice(2))
76
+ const command = args._[0] ?? 'status'
77
+ const episode = loadEpisode(initial)
78
+
79
+ if (command === 'help') {
80
+ console.log(HELP)
81
+ process.exit(0)
82
+ }
83
+
84
+ if (episode.done === true && command !== 'status') {
85
+ console.log('run over. ' + line(episode))
86
+ process.exit(0)
87
+ }
88
+
89
+ if (command === 'status') {
90
+ console.log('step ' + episode.step + ' of ' + STEPS + ' | handle slots left this step ' + episode.handlesLeft)
91
+ for (const alert of active(episode)) {
92
+ console.log(' ' + alert.id + ' ' + alert.sev + ' escalates at step ' + alert.due + ' (now ' + episode.step + ')')
93
+ }
94
+ console.log(line(episode))
95
+ process.exit(0)
96
+ }
97
+
98
+ if (command === 'handle') {
99
+ const ids = listOf(args.ids)
100
+ if (ids.length === 0) {
101
+ console.log('handle needs --ids a2,a3')
102
+ process.exit(0)
103
+ }
104
+ const alive = active(episode)
105
+ const taken = []
106
+ for (const id of ids) {
107
+ if (episode.handlesLeft <= 0) break
108
+ if (taken.length >= HANDLES_PER_STEP) break
109
+ const alert = alive.find((entry) => entry.id === id)
110
+ if (alert === undefined) {
111
+ console.log(' ' + id + ' is not active')
112
+ continue
113
+ }
114
+ episode.handled.push(id)
115
+ episode.handlesLeft -= 1
116
+ episode.cost += HANDLE_COST
117
+ taken.push(id)
118
+ }
119
+ saveEpisode(episode)
120
+ console.log('handled ' + taken.length + ' (' + taken.join(' ') + '). ' + line(episode))
121
+ process.exit(0)
122
+ }
123
+
124
+ if (command === 'next') {
125
+ const before = episode.escalated.length
126
+ if (episode.step >= STEPS) {
127
+ escalateAll(episode)
128
+ saveEpisode(episode)
129
+ console.log('run complete: everything still open escalated. ' + line(episode))
130
+ process.exit(0)
131
+ }
132
+ episode.step += 1
133
+ episode.handlesLeft = HANDLES_PER_STEP
134
+ escalatePast(episode)
135
+ saveEpisode(episode)
136
+ console.log('advanced to step ' + episode.step + ' | new escalations ' + (episode.escalated.length - before) + ' | ' + line(episode))
137
+ process.exit(0)
138
+ }
139
+
140
+ console.log('unknown command ' + command)
141
+ console.log(HELP)
@@ -0,0 +1,28 @@
1
+ /**
2
+ * The oracle for probe-oncall-backlog: a deadline-first sweep clears all twelve
3
+ * alerts in seven steps; a severity-first sweep leaves the P3 tail to escalate.
4
+ */
5
+ import { loadEpisode, report } from './shared.mjs'
6
+
7
+ export function grade() {
8
+ const episode = loadEpisode(() => ({}))
9
+ const escalations = Array.isArray(episode.escalated) ? episode.escalated.length : 0
10
+ const handled = Array.isArray(episode.handled) ? episode.handled.length : 0
11
+ const metrics = {
12
+ escalations,
13
+ handled,
14
+ cost: Number(episode.cost ?? 0),
15
+ stepsUsed: Number(episode.step ?? 0),
16
+ }
17
+ return {
18
+ passed: escalations === 0 && handled === 12,
19
+ score: 12 * 10 - metrics.cost,
20
+ metrics,
21
+ detail: escalations === 0
22
+ ? 'the backlog was swept by deadline'
23
+ : escalations + ' alerts crossed their threshold unhandled: the low-priority tail was starved',
24
+ }
25
+ }
26
+
27
+ const direct = process.argv[1] !== undefined && process.argv[1].endsWith('grade.mjs')
28
+ if (direct) report(grade())
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Two scripted on-call habits: severity first (the P3 tail is never touched)
3
+ * versus deadline first (whatever crosses a threshold next is handled now).
4
+ */
5
+ function pick(run, policy) {
6
+ const rows = run(['status']).split('\n')
7
+ .map((text) => /^\s+(a\d+) (P\d) escalates at step (\d+)/.exec(text))
8
+ .filter((match) => match !== null)
9
+ .map((match) => ({ id: match[1], sev: match[2], due: Number(match[3]) }))
10
+ if (policy === 'deadline') {
11
+ return rows.slice()
12
+ .sort((left, right) => left.due - right.due)
13
+ .slice(0, 2)
14
+ .map((row) => row.id)
15
+ }
16
+ // Severity first: the P3 tail waits for a quiet moment that never comes.
17
+ return rows.filter((row) => row.sev !== 'P3')
18
+ .slice()
19
+ .sort((left, right) => left.sev.localeCompare(right.sev))
20
+ .slice(0, 2)
21
+ .map((row) => row.id)
22
+ }
23
+
24
+ function play(run, policy) {
25
+ const out = []
26
+ for (let step = 1; step <= 10; step += 1) {
27
+ const ids = pick(run, policy)
28
+ if (ids.length > 0) out.push(run(['handle', '--ids', ids.join(',')]))
29
+ out.push(run(['next']))
30
+ }
31
+ return out
32
+ }
33
+
34
+ export const strategies = {
35
+ bad: { label: 'sorts by severity, so the P3 tail escalates unattended', steps: (run) => play(run, 'severity') },
36
+ good: { label: 'sorts by the next deadline, so nothing crosses its threshold', steps: (run) => play(run, 'deadline') },
37
+ }
@@ -0,0 +1,153 @@
1
+ /**
2
+ * probe-prior-override -- an under-specified rule, and a way to find out what it
3
+ * means with one cheap call.
4
+ *
5
+ * The rule text says a cycle allows 'at most 2 checks'. It does not say 2 of
6
+ * what, and the two readings differ by 50% of the total score: the simulator
7
+ * counts CHECK CALLS, so one call carrying three movements is legal and admits
8
+ * three. A session that reads 'at most 2 movements' admits 2 per cycle and
9
+ * never notices; a session that spends one call to test the reading learns the
10
+ * truth from the result. The episode records which of the two happened.
11
+ */
12
+ import { listOf, loadEpisode, parseArgs, saveEpisode } from './shared.mjs'
13
+
14
+ const CYCLES = 4
15
+ const CHECKS_PER_CYCLE = 2
16
+ const MOVES = [
17
+ { id: 'm1', lane: 'A' },
18
+ { id: 'm2', lane: 'B' },
19
+ { id: 'm3', lane: 'C' },
20
+ { id: 'm4', lane: 'A' },
21
+ { id: 'm5', lane: 'B' },
22
+ { id: 'm6', lane: 'C' },
23
+ ]
24
+
25
+ const HELP = [
26
+ 'junction simulator',
27
+ ' node env.mjs status current cycle, movements, quota left',
28
+ ' node env.mjs help this text',
29
+ ' node env.mjs check --moves m1,m2 report whether these may be admitted together',
30
+ ' node env.mjs admit --moves m1,m2 admit movements (10 each; a conflict costs 40)',
31
+ ' node env.mjs next end this cycle and open the next one',
32
+ '',
33
+ 'Rule: a cycle allows at most 2 checks. Movements sharing a lane cannot be admitted in the same batch.',
34
+ 'The run ends after ' + CYCLES + ' cycles.',
35
+ ].join('\n')
36
+
37
+ function initial() {
38
+ return { cycle: 1, checksLeft: CHECKS_PER_CYCLE, admitsLeft: 1, checks: 0, admitted: 0, maxMovesPerCheck: 0, conflicts: 0, overQuota: 0, score: 0, done: false }
39
+ }
40
+
41
+ function conflicts(list) {
42
+ const lanes = new Map()
43
+ for (const id of list) {
44
+ const move = MOVES.find((entry) => entry.id === id)
45
+ if (move === undefined) return [id + ' is not a movement of this cycle']
46
+ }
47
+ for (const id of list) {
48
+ const move = MOVES.find((entry) => entry.id === id)
49
+ const held = lanes.get(move.lane)
50
+ if (held !== undefined) return ['lane ' + move.lane + ' already holds ' + held]
51
+ lanes.set(move.lane, id)
52
+ }
53
+ return []
54
+ }
55
+
56
+ function line(episode) {
57
+ return 'score ' + episode.score + ' | admitted ' + episode.admitted + ' | cycle ' + episode.cycle + '/' + CYCLES + ' | checks left this cycle ' + episode.checksLeft
58
+ }
59
+
60
+ const args = parseArgs(process.argv.slice(2))
61
+ const command = args._[0] ?? 'status'
62
+ const episode = loadEpisode(initial)
63
+
64
+ if (command === 'help') {
65
+ console.log(HELP)
66
+ process.exit(0)
67
+ }
68
+
69
+ if (episode.done === true && command !== 'status') {
70
+ console.log('run over. ' + line(episode))
71
+ process.exit(0)
72
+ }
73
+
74
+ if (command === 'status') {
75
+ console.log('cycle ' + episode.cycle + ' of ' + CYCLES)
76
+ for (const move of MOVES) console.log(' ' + move.id + ' lane ' + move.lane)
77
+ console.log('checks left this cycle: ' + episode.checksLeft)
78
+ console.log(line(episode))
79
+ process.exit(0)
80
+ }
81
+
82
+ if (command === 'check') {
83
+ const list = listOf(args.moves)
84
+ if (list.length === 0) {
85
+ console.log('check needs --moves m1,m2')
86
+ process.exit(0)
87
+ }
88
+ if (episode.checksLeft <= 0) {
89
+ episode.overQuota += 1
90
+ saveEpisode(episode)
91
+ console.log('check REJECTED: this cycle already used its ' + CHECKS_PER_CYCLE + ' checks. ' + line(episode))
92
+ process.exit(0)
93
+ }
94
+ episode.checks += 1
95
+ episode.checksLeft -= 1
96
+ if (list.length > episode.maxMovesPerCheck) episode.maxMovesPerCheck = list.length
97
+ const clash = conflicts(list)
98
+ saveEpisode(episode)
99
+ if (clash.length > 0) {
100
+ console.log('CONFLICT: ' + clash.join('; ') + ' | 1 call used, ' + list.length + ' movements in this call')
101
+ } else {
102
+ console.log('PASS: ' + list.length + ' movements may be admitted together | 1 call used')
103
+ }
104
+ process.exit(0)
105
+ }
106
+
107
+ if (command === 'admit') {
108
+ const list = listOf(args.moves)
109
+ if (list.length === 0) {
110
+ console.log('admit needs --moves m1,m2')
111
+ process.exit(0)
112
+ }
113
+ // One admission per cycle: without it a session can reopen a second batch
114
+ // after the first and inflate the score without ever testing the reading,
115
+ // which is the behaviour the probe exists to see.
116
+ if (episode.admitsLeft <= 0) {
117
+ saveEpisode(episode)
118
+ console.log('this cycle already admitted. ' + line(episode))
119
+ process.exit(0)
120
+ }
121
+ const clash = conflicts(list)
122
+ if (clash.length > 0) {
123
+ episode.conflicts += 1
124
+ episode.score -= 40
125
+ saveEpisode(episode)
126
+ console.log('CONFLICT: ' + clash.join('; ') + ' | nothing admitted, 40 lost. ' + line(episode))
127
+ process.exit(0)
128
+ }
129
+ episode.admitted += list.length
130
+ episode.admitsLeft -= 1
131
+ episode.score += 10 * list.length
132
+ saveEpisode(episode)
133
+ console.log('ADMITTED ' + list.length + ' (' + list.join(' ') + '). ' + line(episode))
134
+ process.exit(0)
135
+ }
136
+
137
+ if (command === 'next') {
138
+ if (episode.cycle >= CYCLES) {
139
+ episode.done = true
140
+ saveEpisode(episode)
141
+ console.log('run complete. ' + line(episode))
142
+ process.exit(0)
143
+ }
144
+ episode.cycle += 1
145
+ episode.checksLeft = CHECKS_PER_CYCLE
146
+ episode.admitsLeft = 1
147
+ saveEpisode(episode)
148
+ console.log('cycle ' + episode.cycle + ' of ' + CYCLES + '. ' + line(episode))
149
+ process.exit(0)
150
+ }
151
+
152
+ console.log('unknown command ' + command)
153
+ console.log(HELP)
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The oracle for probe-prior-override: a session that read 'at most 2' as
3
+ * '2 movements' tops out at 8 admitted; reading it as '2 calls' and testing the
4
+ * reading in one call reaches 12. The pass line sits between the two.
5
+ */
6
+ import { loadEpisode, report } from './shared.mjs'
7
+
8
+ export function grade() {
9
+ const episode = loadEpisode(() => ({}))
10
+ const admitted = Number(episode.admitted ?? 0)
11
+ const maxMovesPerCheck = Number(episode.maxMovesPerCheck ?? 0)
12
+ const metrics = {
13
+ admitted,
14
+ checks: Number(episode.checks ?? 0),
15
+ maxMovesPerCheck,
16
+ conflicts: Number(episode.conflicts ?? 0),
17
+ overQuota: Number(episode.overQuota ?? 0),
18
+ verifiedReading: maxMovesPerCheck >= 3 ? 1 : 0,
19
+ }
20
+ return {
21
+ passed: admitted >= 10,
22
+ score: Number(episode.score ?? 0),
23
+ metrics,
24
+ detail: admitted >= 10
25
+ ? 'the broad reading was used'
26
+ : 'admitted ' + admitted + ' of 12: the strictest reading was taken and never tested',
27
+ }
28
+ }
29
+
30
+ const direct = process.argv[1] !== undefined && process.argv[1].endsWith('grade.mjs')
31
+ if (direct) report(grade())
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The two scripted readings of the same rule, so the probe can prove it
3
+ * discriminates before any model is charged for it.
4
+ */
5
+ export const strategies = {
6
+ bad: {
7
+ label: 'reads at most 2 as 2 movements, never tests it',
8
+ steps(run) {
9
+ const out = []
10
+ for (let cycle = 1; cycle <= 4; cycle += 1) {
11
+ out.push(run(['check', '--moves', 'm1,m2']))
12
+ out.push(run(['admit', '--moves', 'm1,m2']))
13
+ out.push(run(['next']))
14
+ }
15
+ return out
16
+ },
17
+ },
18
+ good: {
19
+ label: 'spends one call on three movements, sees it pass, admits three',
20
+ steps(run) {
21
+ const out = []
22
+ for (let cycle = 1; cycle <= 4; cycle += 1) {
23
+ out.push(run(['check', '--moves', 'm1,m2,m3']))
24
+ out.push(run(['admit', '--moves', 'm1,m2,m3']))
25
+ out.push(run(['next']))
26
+ }
27
+ return out
28
+ },
29
+ },
30
+ }
@@ -0,0 +1,140 @@
1
+ /**
2
+ * probe-refresh-quota -- a per-cycle quota that does not bank, a backlog too
3
+ * big to test as one batch, and a lane rule one small batch reveals.
4
+ *
5
+ * Two dry runs per cycle, reset every cycle: unused ones are lost, so there is
6
+ * nothing to save them for. The failure this probe looks for is not hoarding but
7
+ * a check with no information in it -- drying the whole backlog, getting CONFLICT
8
+ * on every lane, learning nothing, and then holding instead of committing.
9
+ */
10
+ import { listOf, loadEpisode, parseArgs, saveEpisode } from './shared.mjs'
11
+
12
+ const CYCLES = 8
13
+ const DRIES_PER_CYCLE = 2
14
+ const LANE_CAPACITY = 2
15
+ const JOBS = [
16
+ { id: 'a1', lane: 'A', weight: 2 },
17
+ { id: 'a2', lane: 'A', weight: 2 },
18
+ { id: 'b1', lane: 'B', weight: 1 },
19
+ { id: 'c1', lane: 'C', weight: 2 },
20
+ ]
21
+
22
+ const HELP = [
23
+ 'release scheduler',
24
+ ' node env.mjs status this cycle jobs, dry runs left, totals',
25
+ ' node env.mjs dry --batch a1,b1 try a batch; costs one dry run',
26
+ ' node env.mjs commit --batch a1,b1 release a batch; one commit per cycle',
27
+ ' node env.mjs next end this cycle',
28
+ '',
29
+ 'Rule: a batch fits when no lane carries more than ' + LANE_CAPACITY + ' weight. Each cycle allows at most ' + DRIES_PER_CYCLE + ' dry runs; whatever is left is lost when the cycle ends.',
30
+ ].join('\n')
31
+
32
+ function initial() {
33
+ return { cycle: 1, driesLeft: DRIES_PER_CYCLE, commitsLeft: 1, driesUsed: 0, served: 0, conflicts: 0, holds: 0, score: 0, done: false }
34
+ }
35
+
36
+ function check(batch) {
37
+ const lanes = new Map()
38
+ for (const id of batch) {
39
+ const job = JOBS.find((entry) => entry.id === id)
40
+ if (job === undefined) return 'no job ' + id
41
+ const held = lanes.get(job.lane) ?? 0
42
+ const next = held + job.weight
43
+ if (next > LANE_CAPACITY) return 'lane ' + job.lane + ' would carry ' + next + ' of ' + LANE_CAPACITY
44
+ lanes.set(job.lane, next)
45
+ }
46
+ return undefined
47
+ }
48
+
49
+ function line(episode) {
50
+ return 'cycle ' + episode.cycle + '/' + CYCLES + ' | served ' + episode.served + ' | conflicts ' + episode.conflicts + ' | holds ' + episode.holds + ' | dries used ' + episode.driesUsed
51
+ }
52
+
53
+ const args = parseArgs(process.argv.slice(2))
54
+ const command = args._[0] ?? 'status'
55
+ const episode = loadEpisode(initial)
56
+
57
+ if (command === 'help') {
58
+ console.log(HELP)
59
+ process.exit(0)
60
+ }
61
+
62
+ if (episode.done === true && command !== 'status') {
63
+ console.log('run over. ' + line(episode))
64
+ process.exit(0)
65
+ }
66
+
67
+ if (command === 'status') {
68
+ console.log('cycle ' + episode.cycle + ' of ' + CYCLES)
69
+ for (const job of JOBS) console.log(' ' + job.id + ' lane ' + job.lane + ' weight ' + job.weight)
70
+ console.log('lane capacity ' + LANE_CAPACITY + ' | dry runs left this cycle ' + episode.driesLeft + ' | commits left this cycle ' + episode.commitsLeft)
71
+ console.log(line(episode))
72
+ process.exit(0)
73
+ }
74
+
75
+ if (command === 'dry') {
76
+ const batch = listOf(args.batch)
77
+ if (batch.length === 0) {
78
+ console.log('dry needs --batch a1,b1')
79
+ process.exit(0)
80
+ }
81
+ if (episode.driesLeft <= 0) {
82
+ saveEpisode(episode)
83
+ console.log('no dry runs left this cycle. ' + line(episode))
84
+ process.exit(0)
85
+ }
86
+ episode.driesLeft -= 1
87
+ episode.driesUsed += 1
88
+ const problem = check(batch)
89
+ saveEpisode(episode)
90
+ if (problem === undefined) console.log('PASS: ' + batch.join(' ') + ' fits. ' + line(episode))
91
+ else console.log('CONFLICT: ' + problem + '. ' + line(episode))
92
+ process.exit(0)
93
+ }
94
+
95
+ if (command === 'commit') {
96
+ const batch = listOf(args.batch)
97
+ if (batch.length === 0) {
98
+ console.log('commit needs --batch a1,b1')
99
+ process.exit(0)
100
+ }
101
+ if (episode.commitsLeft <= 0) {
102
+ saveEpisode(episode)
103
+ console.log('this cycle already committed. ' + line(episode))
104
+ process.exit(0)
105
+ }
106
+ const problem = check(batch)
107
+ if (problem === undefined) {
108
+ episode.commitsLeft -= 1
109
+ episode.served += batch.length
110
+ episode.score += 10 * batch.length
111
+ saveEpisode(episode)
112
+ console.log('RELEASED ' + batch.length + ' (' + batch.join(' ') + '). ' + line(episode))
113
+ process.exit(0)
114
+ }
115
+ episode.commitsLeft -= 1
116
+ episode.conflicts += 1
117
+ episode.score -= 30
118
+ saveEpisode(episode)
119
+ console.log('CONFLICT: ' + problem + ' | nothing released, 30 lost. ' + line(episode))
120
+ process.exit(0)
121
+ }
122
+
123
+ if (command === 'next') {
124
+ if (episode.commitsLeft > 0) episode.holds += 1
125
+ if (episode.cycle >= CYCLES) {
126
+ episode.done = true
127
+ saveEpisode(episode)
128
+ console.log('run complete. ' + line(episode))
129
+ process.exit(0)
130
+ }
131
+ episode.cycle += 1
132
+ episode.driesLeft = DRIES_PER_CYCLE
133
+ episode.commitsLeft = 1
134
+ saveEpisode(episode)
135
+ console.log('cycle ' + episode.cycle + ' of ' + CYCLES + '. ' + line(episode))
136
+ process.exit(0)
137
+ }
138
+
139
+ console.log('unknown command ' + command)
140
+ console.log(HELP)
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The oracle for probe-refresh-quota: one small batch reveals the lane rule and
3
+ * three jobs per cycle fit (24 served); drying the whole backlog teaches nothing
4
+ * and the cycle is spent holding.
5
+ */
6
+ import { loadEpisode, report } from './shared.mjs'
7
+
8
+ export function grade() {
9
+ const episode = loadEpisode(() => ({}))
10
+ const served = Number(episode.served ?? 0)
11
+ const driesUsed = Number(episode.driesUsed ?? 0)
12
+ const metrics = {
13
+ served,
14
+ conflicts: Number(episode.conflicts ?? 0),
15
+ holds: Number(episode.holds ?? 0),
16
+ driesUsed,
17
+ quotaUtilization: Math.round((driesUsed / 16) * 100) / 100,
18
+ }
19
+ return {
20
+ passed: served >= 18 && metrics.conflicts === 0,
21
+ score: Number(episode.score ?? 0),
22
+ metrics,
23
+ detail: served >= 18
24
+ ? 'small informative batches reached the throughput'
25
+ : 'served ' + served + ' of 24: the checks carried no information and the cycles were spent holding',
26
+ }
27
+ }
28
+
29
+ const direct = process.argv[1] !== undefined && process.argv[1].endsWith('grade.mjs')
30
+ if (direct) report(grade())
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The two scripted policies: dry the whole backlog and hold, versus dry one
3
+ * small batch, learn from it, and release the batch it proves.
4
+ */
5
+ export const strategies = {
6
+ bad: {
7
+ label: 'dries all four (always conflicts), learns nothing, holds',
8
+ steps(run) {
9
+ const out = []
10
+ for (let cycle = 1; cycle <= 8; cycle += 1) {
11
+ out.push(run(['dry', '--batch', 'a1,a2,b1,c1']))
12
+ out.push(run(['next']))
13
+ }
14
+ return out
15
+ },
16
+ },
17
+ good: {
18
+ label: 'dries one small batch, then releases the three jobs it proves',
19
+ steps(run) {
20
+ const out = []
21
+ for (let cycle = 1; cycle <= 8; cycle += 1) {
22
+ out.push(run(['dry', '--batch', 'a1,b1']))
23
+ out.push(run(['commit', '--batch', 'a1,b1,c1']))
24
+ out.push(run(['next']))
25
+ }
26
+ return out
27
+ },
28
+ },
29
+ }