@cordfuse/politik 0.4.0 → 0.6.0
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/EXECUTION.md +90 -85
- package/POLITIK-ARCHITECTURE.md +111 -176
- package/PROTOCOLS.md +120 -289
- package/README.md +130 -177
- package/RESEARCH.md +142 -436
- package/RUNTIME.md +253 -261
- package/docs/adr/0001-chamber-transport.md +2 -0
- package/docs/adr/0002-charter-schema.md +2 -0
- package/docs/adr/0003-state-json-schema.md +2 -0
- package/docs/adr/0004-motion-enactment.md +2 -0
- package/docs/adr/0005-hansard-format.md +2 -0
- package/docs/adr/0006-protocol-behavioral-enforcement.md +2 -0
- package/docs/adr/0007-protocol-mechanics.md +2 -0
- package/docs/adr/0008-session-storage.md +2 -0
- package/docs/adr/0009-match-primitive.md +2 -0
- package/docs/adr/0010-operator-interface.md +2 -0
- package/docs/adr/0011-shared-sessions.md +2 -0
- package/docs/adr/0012-plain-vocabulary.md +68 -0
- package/docs/adr/0013-rule-defined-protocols.md +89 -0
- package/package.json +1 -1
- package/protocols/CONTRIBUTING.md +47 -87
- package/protocols/adversarial-review.yml +19 -0
- package/protocols/elimination-rounds.yml +18 -0
- package/protocols/majority-vote.yml +18 -0
- package/protocols/panel-review.yml +19 -0
- package/protocols/single-decider.yml +18 -0
- package/protocols/unanimous-vote.yml +17 -0
- package/src/actors.ts +124 -100
- package/src/agents.ts +64 -23
- package/src/ballot.ts +747 -0
- package/src/canon.ts +23 -10
- package/src/capability.ts +16 -99
- package/src/charter.ts +41 -34
- package/src/cli.ts +541 -480
- package/src/{prorogation.ts → closing.ts} +43 -43
- package/src/conflict.ts +14 -14
- package/src/containment.ts +11 -11
- package/src/doctor.ts +1 -1
- package/src/election.ts +4 -4
- package/src/envelope.ts +3 -3
- package/src/escalation.ts +44 -44
- package/src/fault.ts +8 -8
- package/src/federation.ts +14 -14
- package/src/git.ts +21 -18
- package/src/heartbeat.ts +32 -32
- package/src/index.ts +5 -5
- package/src/init.ts +66 -65
- package/src/integrity.ts +30 -30
- package/src/ledger.ts +3 -3
- package/src/{hansard.ts → log.ts} +20 -20
- package/src/{crisis.ts → misconduct.ts} +70 -70
- package/src/operator.ts +5 -5
- package/src/pending.ts +9 -9
- package/src/projection.ts +30 -30
- package/src/protocol-sdk.ts +12 -121
- package/src/protocol.ts +8 -78
- package/src/providers/github.ts +12 -12
- package/src/quorum.ts +7 -7
- package/src/remote.ts +1 -1
- package/src/runner.ts +134 -55
- package/src/scm.ts +14 -14
- package/src/spawn.ts +4 -4
- package/src/standings.ts +13 -13
- package/src/state.ts +6 -6
- package/src/templates/session.ts +448 -0
- package/src/tournament.ts +20 -20
- package/src/transport.ts +4 -4
- package/src/tree.ts +16 -16
- package/protocols/adversarial-collaboration.yml +0 -28
- package/protocols/corporate.yml +0 -35
- package/protocols/elimination-tournament.yml +0 -41
- package/protocols/emergency-response-ics.yml +0 -27
- package/protocols/jury-deliberation.yml +0 -32
- package/protocols/monarchy.yml +0 -31
- package/protocols/parliamentary.yml +0 -66
- package/protocols/peer-review.yml +0 -44
- package/protocols/republic.yml +0 -36
- package/protocols/socialism-collective.yml +0 -31
- package/protocols/solo.yml +0 -32
- package/src/division.ts +0 -609
- package/src/templates/parliamentary.ts +0 -580
package/src/quorum.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Quorum check.
|
|
3
3
|
*
|
|
4
|
-
* Quorum is the minimum number of actors for a valid
|
|
4
|
+
* Quorum is the minimum number of actors for a valid vote
|
|
5
5
|
* (RUNTIME.md § SESSION QUORUM, MINIMUM CAST AND SINGLE-PLAYER SESSIONS).
|
|
6
6
|
* Two distinct gates live here and are frequently confused:
|
|
7
7
|
*
|
|
8
|
-
* - **minimum_cast** — a
|
|
8
|
+
* - **minimum_cast** — a session opening gate. Are the required constituencies
|
|
9
9
|
* declared at all? Enforced by the Charter validator (ADR-0002 rule 3).
|
|
10
10
|
* - **quorum** — a runtime gate. Are enough actors *present right now* for a
|
|
11
|
-
*
|
|
11
|
+
* vote to be valid? That is this module.
|
|
12
12
|
*
|
|
13
13
|
* `quorum: 1` is a legitimate configuration, not a degenerate one: a
|
|
14
14
|
* single-player session whose governance value is the audit trail and the
|
|
@@ -24,8 +24,8 @@ import type { Role } from './canon.ts';
|
|
|
24
24
|
/* -------------------------------------------------------------------------- */
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
28
|
-
* reduced cast. Always visible in the
|
|
27
|
+
* An owner's explicit acknowledgement that the session proceeds with a
|
|
28
|
+
* reduced cast. Always visible in the record — it cannot be hidden.
|
|
29
29
|
*/
|
|
30
30
|
export interface QuorumOverride {
|
|
31
31
|
readonly enabled: boolean;
|
|
@@ -40,7 +40,7 @@ export interface QuorumOverride {
|
|
|
40
40
|
/* -------------------------------------------------------------------------- */
|
|
41
41
|
|
|
42
42
|
export interface QuorumResult {
|
|
43
|
-
/** May a
|
|
43
|
+
/** May a vote proceed? */
|
|
44
44
|
readonly met: boolean;
|
|
45
45
|
readonly required: number;
|
|
46
46
|
readonly present: number;
|
|
@@ -116,7 +116,7 @@ export const checkQuorum = (input: QuorumInput): QuorumResult => {
|
|
|
116
116
|
};
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
|
-
* Render the
|
|
119
|
+
* Render the record fields for a quorum decision.
|
|
120
120
|
*
|
|
121
121
|
* A degraded session must be legible to any later reviewer, so the override
|
|
122
122
|
* and its reason are recorded as fields rather than buried in prose.
|
package/src/remote.ts
CHANGED
|
@@ -117,7 +117,7 @@ export const configureClone = async (dir: string, handle: string | null): Promis
|
|
|
117
117
|
const branch = await currentBranch(dir);
|
|
118
118
|
if (branch === null) return null;
|
|
119
119
|
// Acts commit under this clone's git identity. A machine with none configured
|
|
120
|
-
// gets the seat handle, the same convention
|
|
120
|
+
// gets the seat handle, the same convention session opening uses for the owner.
|
|
121
121
|
if (handle !== null && (await config(dir, 'user.name')) === null) {
|
|
122
122
|
await gitx(dir, ['config', 'user.name', handle]);
|
|
123
123
|
await gitx(dir, ['config', 'user.email', `${handle}@politik.local`]);
|
package/src/runner.ts
CHANGED
|
@@ -17,16 +17,18 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { spawn } from 'node:child_process';
|
|
20
|
+
import { existsSync } from 'node:fs';
|
|
20
21
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
21
|
-
import { join } from 'node:path';
|
|
22
|
+
import { dirname, join, resolve } from 'node:path';
|
|
23
|
+
import { fileURLToPath } from 'node:url';
|
|
22
24
|
|
|
23
25
|
import { findAgent, composePrompt, type AgentSpec } from './agents.ts';
|
|
24
|
-
import type
|
|
26
|
+
import { ROLE_VERBS, type Role, type Verb } from './canon.ts';
|
|
25
27
|
import { parseCharter, type Charter } from './charter.ts';
|
|
26
28
|
import { parseEnvelope, claimSlot, type Envelope } from './envelope.ts';
|
|
27
29
|
import { standForElection, bowOut, type ElectionOptions } from './election.ts';
|
|
28
30
|
import { nodeLockFs } from './lockfs.ts';
|
|
29
|
-
import { appendEntry, type
|
|
31
|
+
import { appendEntry, parseEntries, type RecordEntry } from './log.ts';
|
|
30
32
|
import { spawnAgent, type SpawnResult } from './spawn.ts';
|
|
31
33
|
import {
|
|
32
34
|
appendLedgerRow, parseResultText, parseUsage, type Usage,
|
|
@@ -48,7 +50,7 @@ export interface RunOptions {
|
|
|
48
50
|
readonly dir: string;
|
|
49
51
|
/** Registry id of the agent to seat, e.g. `claude-code`. */
|
|
50
52
|
readonly agent_id: string;
|
|
51
|
-
/** Handle recorded in the
|
|
53
|
+
/** Handle recorded in the log for this agent. */
|
|
52
54
|
readonly actor: string;
|
|
53
55
|
/** Constituency to seat the agent in. */
|
|
54
56
|
readonly role: Role;
|
|
@@ -92,12 +94,12 @@ export type RunOutcome =
|
|
|
92
94
|
| {
|
|
93
95
|
readonly ok: true;
|
|
94
96
|
readonly result: SpawnResult;
|
|
95
|
-
readonly entry:
|
|
97
|
+
readonly entry: RecordEntry;
|
|
96
98
|
readonly files_touched: readonly string[];
|
|
97
|
-
/** The turn completed but the agent filed
|
|
99
|
+
/** The turn completed but the agent filed an escalation, so the session is now suspended. */
|
|
98
100
|
readonly suspended?: boolean;
|
|
99
101
|
}
|
|
100
|
-
| { readonly ok: false; readonly reason: string; readonly entry?:
|
|
102
|
+
| { readonly ok: false; readonly reason: string; readonly entry?: RecordEntry };
|
|
101
103
|
|
|
102
104
|
/* -------------------------------------------------------------------------- */
|
|
103
105
|
/* Helpers */
|
|
@@ -122,7 +124,11 @@ const git = (dir: string, args: readonly string[], spawnFn: typeof spawn): Promi
|
|
|
122
124
|
child.stdout?.on('data', (c: Buffer) => {
|
|
123
125
|
output += c.toString();
|
|
124
126
|
});
|
|
125
|
-
|
|
127
|
+
// trimEnd, not trim: `git status --porcelain` lines start with a status
|
|
128
|
+
// column that may be a space (" M file"). Trimming the front stripped it
|
|
129
|
+
// from the first line, so the first unstaged file lost its first letter
|
|
130
|
+
// ("ANSARD.md") and a hand edit of the record went unflagged.
|
|
131
|
+
child.on('close', () => resolve(output.trimEnd()));
|
|
126
132
|
child.on('error', () => resolve(''));
|
|
127
133
|
});
|
|
128
134
|
|
|
@@ -140,7 +146,7 @@ const head = (dir: string, spawnFn: typeof spawn): Promise<string> =>
|
|
|
140
146
|
/**
|
|
141
147
|
* Files the agent touched — committed *or* left in the tree.
|
|
142
148
|
*
|
|
143
|
-
* Both halves are necessary. The
|
|
149
|
+
* Both halves are necessary. The Charter tell agents "commit your
|
|
144
150
|
* result; uncommitted work does not exist", so a well-behaved agent leaves a
|
|
145
151
|
* clean tree and a new commit — and a working-tree diff alone would report that
|
|
146
152
|
* it did nothing. The first live session did exactly this: the agent wrote and
|
|
@@ -169,9 +175,9 @@ const filesTouched = async (
|
|
|
169
175
|
return [...new Set([...committed, ...newlyDirty])];
|
|
170
176
|
};
|
|
171
177
|
|
|
172
|
-
/** Last N entries of the
|
|
173
|
-
const recentRecord = (
|
|
174
|
-
const parts =
|
|
178
|
+
/** Last N entries of the record, as context for a stateless agent. */
|
|
179
|
+
const recentRecord = (log: string, entries = 3): string => {
|
|
180
|
+
const parts = log.split(/^## /m).slice(1);
|
|
175
181
|
return parts
|
|
176
182
|
.slice(-entries)
|
|
177
183
|
.map((p) => `## ${p.trim()}`)
|
|
@@ -240,7 +246,7 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
240
246
|
].join('\n');
|
|
241
247
|
|
|
242
248
|
// Capability gate. The architecture is explicit that the engine "validates
|
|
243
|
-
// model compliance at
|
|
249
|
+
// model compliance at session opening and refuses to spawn a mismatched model
|
|
244
250
|
// regardless of operator assignment" — so a mismatch is refused here, before
|
|
245
251
|
// an agent is spawned, not noticed afterwards.
|
|
246
252
|
const seat = charter.constituencies.find((c) => c.role === options.role);
|
|
@@ -276,7 +282,7 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
276
282
|
envelope = envelopeResult.envelope;
|
|
277
283
|
}
|
|
278
284
|
|
|
279
|
-
// Elect into the constituency. Local scope only — the
|
|
285
|
+
// Elect into the constituency. Local scope only — the record commit is the
|
|
280
286
|
// global arbiter, and this lock merely grants the right to attempt it.
|
|
281
287
|
const election: ElectionOptions = {
|
|
282
288
|
fs: nodeLockFs,
|
|
@@ -303,15 +309,23 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
303
309
|
}
|
|
304
310
|
|
|
305
311
|
try {
|
|
306
|
-
const
|
|
312
|
+
const log = (await readIf(join(options.dir, 'LOG.md'))) ?? '';
|
|
307
313
|
|
|
308
314
|
const prompt = composePrompt({
|
|
309
315
|
envelope,
|
|
310
316
|
role: options.role,
|
|
311
|
-
|
|
317
|
+
// The role's own verbs, minus ESCALATE where the protocol allows no appeals.
|
|
318
|
+
// A fixed READ/WRITE/ESCALATE list here told voting roles they could not vote.
|
|
319
|
+
// A role the Charter gives a domain veto also holds VETO.
|
|
320
|
+
verbs: options.verbs ?? [
|
|
321
|
+
...(ROLE_VERBS[options.role] ?? ['READ'])
|
|
322
|
+
.filter((v) => v !== 'ESCALATE' || charter.session.escalation !== 'disabled'),
|
|
323
|
+
...(charter.domain_veto.includes(options.role) && !(ROLE_VERBS[options.role] ?? []).includes('VETO')
|
|
324
|
+
? (['VETO'] as const) : []),
|
|
325
|
+
],
|
|
312
326
|
protocol: charter.protocol,
|
|
313
327
|
standing_orders: charter.body,
|
|
314
|
-
|
|
328
|
+
log_excerpt: recentRecord(log),
|
|
315
329
|
escalation_enabled: charter.session.escalation !== 'disabled',
|
|
316
330
|
});
|
|
317
331
|
|
|
@@ -319,6 +333,27 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
319
333
|
const beforeDirty = await dirtyFiles(options.dir, spawnFn);
|
|
320
334
|
|
|
321
335
|
const measure = options.measure !== false && agent.json_args !== undefined;
|
|
336
|
+
|
|
337
|
+
// Per-turn grants for harnesses that sandbox their own shell (agents.ts
|
|
338
|
+
// session_args): the session's git directory, which an enclosing repo may
|
|
339
|
+
// own, and where this CLI runs from, so the agent can act through it.
|
|
340
|
+
const gitDir =
|
|
341
|
+
(await git(options.dir, ['rev-parse', '--absolute-git-dir'], spawnFn)) ||
|
|
342
|
+
join(resolve(options.dir), '.git');
|
|
343
|
+
const politikRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
344
|
+
// Where the agent will find `politik`: the directory it was invoked from and
|
|
345
|
+
// the one holding it on PATH (a global npm bin often lives under $HOME,
|
|
346
|
+
// which a sandbox hides). Read-only grants, deduplicated.
|
|
347
|
+
const invokedFrom = process.argv[1] === undefined ? null : dirname(resolve(process.argv[1]));
|
|
348
|
+
const onPath = (process.env['PATH'] ?? '')
|
|
349
|
+
.split(':')
|
|
350
|
+
.find((d) => d !== '' && existsSync(join(d, 'politik')));
|
|
351
|
+
const readPaths = [...new Set([politikRoot, invokedFrom, onPath === undefined ? null : resolve(onPath)])]
|
|
352
|
+
.filter((p): p is string => p !== null)
|
|
353
|
+
// A path inside the CLI's own install is already covered by politikRoot.
|
|
354
|
+
.filter((p) => p === politikRoot || !p.startsWith(`${politikRoot}/`));
|
|
355
|
+
const sessionArgs = agent.session_args?.({ git_dir: gitDir, read_paths: readPaths }) ?? [];
|
|
356
|
+
const turnArgs = [...(measure ? (agent.json_args ?? []) : []), ...sessionArgs];
|
|
322
357
|
const sleepFn = options.sleepFn ?? ((ms: number) => new Promise((r) => setTimeout(r, ms)));
|
|
323
358
|
const policy = {
|
|
324
359
|
auto_retry_max: charter.fault_handling.auto_retry_max,
|
|
@@ -333,12 +368,12 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
333
368
|
cwd: options.dir,
|
|
334
369
|
timeout_ms: options.timeout_ms,
|
|
335
370
|
spawnFn,
|
|
336
|
-
extraArgs:
|
|
371
|
+
extraArgs: turnArgs,
|
|
337
372
|
});
|
|
338
373
|
|
|
339
374
|
let attempts = 1;
|
|
340
375
|
let classification: Classification | null = null;
|
|
341
|
-
const faultEntries:
|
|
376
|
+
const faultEntries: RecordEntry[] = [];
|
|
342
377
|
|
|
343
378
|
while (!result.ok) {
|
|
344
379
|
classification = classify(result);
|
|
@@ -363,7 +398,7 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
363
398
|
cwd: options.dir,
|
|
364
399
|
timeout_ms: options.timeout_ms,
|
|
365
400
|
spawnFn,
|
|
366
|
-
extraArgs:
|
|
401
|
+
extraArgs: turnArgs,
|
|
367
402
|
});
|
|
368
403
|
}
|
|
369
404
|
|
|
@@ -374,44 +409,81 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
374
409
|
}
|
|
375
410
|
|
|
376
411
|
// Structured output carries the answer inside a JSON envelope; unwrap it so
|
|
377
|
-
// the
|
|
412
|
+
// the record shows what the agent said, not how it was transported.
|
|
378
413
|
const usage: Usage | null = measure ? parseUsage(agent.id, result.stdout) : null;
|
|
379
414
|
const answer = (measure ? parseResultText(agent.id, result.stdout) : null) ?? result.stdout;
|
|
380
415
|
|
|
381
416
|
const touched = await filesTouched(options.dir, beforeHead, beforeDirty, spawnFn);
|
|
382
417
|
|
|
383
418
|
// Files the RECORD agent owns exclusively (POLITIK-ARCHITECTURE.md
|
|
384
|
-
// § File Sovereignty Rules). An actor writing to them is a
|
|
419
|
+
// § File Sovereignty Rules). An actor writing to them is a Charter
|
|
385
420
|
// violation, and it must be recorded rather than silently tolerated —
|
|
386
421
|
// a record an actor can write is not a record.
|
|
387
|
-
|
|
388
|
-
|
|
422
|
+
//
|
|
423
|
+
// An agent may also act *through the CLI* during its turn — cast a vote,
|
|
424
|
+
// open a vote — and those acts write and commit these same files. The
|
|
425
|
+
// record must keep them. So the files are read again after the turn, and a
|
|
426
|
+
// change to them is lawful only when it reached the record the way the CLI
|
|
427
|
+
// writes it: committed (nothing left dirty) and, for the record, purely
|
|
428
|
+
// appended. Anything else is a hand edit: a violation, and the record is
|
|
429
|
+
// restored from the pre-turn copy rather than carrying the edit forward.
|
|
430
|
+
const OWNED_BY_RECORD = ['LOG.md', 'STATE.json', 'LEDGER.md'];
|
|
431
|
+
const dirtyAfter = await dirtyFiles(options.dir, spawnFn);
|
|
432
|
+
const logAfter = (await readIf(join(options.dir, 'LOG.md'))) ?? '';
|
|
433
|
+
const logAppended =
|
|
434
|
+
logAfter.startsWith(log) && !dirtyAfter.includes('LOG.md');
|
|
435
|
+
const violations = touched.filter(
|
|
436
|
+
(f) =>
|
|
437
|
+
OWNED_BY_RECORD.includes(f) &&
|
|
438
|
+
(dirtyAfter.includes(f) || (f === 'LOG.md' && !logAppended)),
|
|
439
|
+
);
|
|
440
|
+
// Entries the CLI appended during the turn — votes, votes — kept as
|
|
441
|
+
// the base the turn's own entry is appended to.
|
|
442
|
+
const recordBase = logAppended ? logAfter : log;
|
|
443
|
+
const actsDuringTurn = logAppended
|
|
444
|
+
? parseEntries(logAfter).length - parseEntries(log).length
|
|
445
|
+
: 0;
|
|
446
|
+
// The same rule for STATE.json: a committed CLI act may have moved it (a
|
|
447
|
+
// tally that deadlocks suspends the session), and the turn must not undo it.
|
|
448
|
+
const stateAfterRaw = dirtyAfter.includes('STATE.json')
|
|
449
|
+
? null
|
|
450
|
+
: await readIf(join(options.dir, 'STATE.json'));
|
|
451
|
+
const stateBase =
|
|
452
|
+
(stateAfterRaw === null ? null : parseState(stateAfterRaw)) ?? state;
|
|
389
453
|
|
|
390
454
|
// An agent that hits a decision above its authority is told (composePrompt,
|
|
391
|
-
// Rules of the Floor) to
|
|
392
|
-
// hosted session the
|
|
455
|
+
// Rules of the Floor) to escalate instead of proceeding. On a
|
|
456
|
+
// hosted session the escalation workflow catches the pushed file and
|
|
393
457
|
// suspends; a local session has no such watcher, so the runner closes the
|
|
394
458
|
// loop: a new file under escalations/ — a ruling excepted, since that is the
|
|
395
|
-
//
|
|
459
|
+
// owner's own reply — is an agent-filed escalation, and the session
|
|
396
460
|
// suspends pending a ruling exactly as the CLI `escalate` path does.
|
|
397
461
|
const escalationFiles = touched.filter(
|
|
398
462
|
(f) => f.startsWith('escalations/') && !f.slice('escalations/'.length).startsWith('ruling'),
|
|
399
463
|
);
|
|
400
464
|
// A protocol declaring no escalation (ADR-0006) has no mechanism to resolve a
|
|
401
|
-
//
|
|
402
|
-
// suspend — suspending would deadlock the
|
|
465
|
+
// escalation, so an agent's escalation file is recorded but does not
|
|
466
|
+
// suspend — suspending would deadlock the session in a state it cannot lift.
|
|
403
467
|
const escalationDisabled = charter.session.escalation === 'disabled';
|
|
404
468
|
const agentEscalated = escalationFiles.length > 0 && !escalationDisabled;
|
|
405
469
|
const escalationIgnored = escalationFiles.length > 0 && escalationDisabled;
|
|
406
470
|
|
|
407
471
|
// Hard Containment Rule. Checked after the turn because it is detection,
|
|
408
472
|
// not prevention — a spawned process runs with the OS's permissions and
|
|
409
|
-
// nothing here can stop it. The
|
|
473
|
+
// nothing here can stop it. The record shows what happened, which is the
|
|
410
474
|
// framework's answer to most of what it cannot prevent.
|
|
411
475
|
const containment = checkContainment(options.dir, touched, `${result.stdout}\n${answer}`);
|
|
412
476
|
|
|
413
|
-
|
|
414
|
-
|
|
477
|
+
// Work is any file the turn touched other than the record and escalations.
|
|
478
|
+
const producedWork = touched.some(
|
|
479
|
+
(f) => !OWNED_BY_RECORD.includes(f) && !f.startsWith('escalations/'),
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
const entry: RecordEntry = {
|
|
483
|
+
// A turn that produced work tabled a motion. A turn that only read, or acted
|
|
484
|
+
// through the CLI (a vote, an escalation), tabled nothing: recording it as
|
|
485
|
+
// MOTION_TABLED misread every review as a proposal.
|
|
486
|
+
type: !result.ok ? 'SESSION_FAULT' : producedWork ? 'MOTION_TABLED' : 'AGENT_TURN',
|
|
415
487
|
at: options.now,
|
|
416
488
|
actor: options.actor,
|
|
417
489
|
role: options.role,
|
|
@@ -424,19 +496,22 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
424
496
|
: `exit ${String(result.exit_code)}`,
|
|
425
497
|
'Elapsed ms': String(result.elapsed_ms),
|
|
426
498
|
'Files touched': touched.length > 0 ? touched.join(', ') : 'none',
|
|
499
|
+
...(actsDuringTurn > 0
|
|
500
|
+
? { 'Acts during turn': `${String(actsDuringTurn)} recorded through the CLI` }
|
|
501
|
+
: {}),
|
|
427
502
|
...(violations.length > 0
|
|
428
503
|
? {
|
|
429
|
-
'
|
|
504
|
+
'Charter violation': `actor wrote to ${violations.join(', ')} — owned by RECORD`,
|
|
430
505
|
}
|
|
431
506
|
: {}),
|
|
432
507
|
...(agentEscalated
|
|
433
508
|
? {
|
|
434
|
-
'
|
|
509
|
+
'escalation': `actor filed ${escalationFiles.join(', ')} — the session is suspended pending an owner ruling`,
|
|
435
510
|
}
|
|
436
511
|
: {}),
|
|
437
512
|
...(escalationIgnored
|
|
438
513
|
? {
|
|
439
|
-
'
|
|
514
|
+
'escalation': `actor filed ${escalationFiles.join(', ')} — ignored: this protocol declares no escalation`,
|
|
440
515
|
}
|
|
441
516
|
: {}),
|
|
442
517
|
},
|
|
@@ -447,13 +522,13 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
447
522
|
|
|
448
523
|
// Faults are recorded before the turn's own entry: they happened first, and
|
|
449
524
|
// a reader should see the retries that led to the outcome.
|
|
450
|
-
let record =
|
|
525
|
+
let record = recordBase;
|
|
451
526
|
for (const fault of faultEntries) record = appendEntry(record, fault);
|
|
452
527
|
record = appendEntry(record, entry);
|
|
453
528
|
|
|
454
|
-
// A breach suspends the
|
|
455
|
-
//
|
|
456
|
-
// Containment Rule). The
|
|
529
|
+
// A breach suspends the session: logged immediately, escalated to the
|
|
530
|
+
// Owner, paused pending a ruling (POLITIK-ARCHITECTURE.md § Hard
|
|
531
|
+
// Containment Rule). The escalation flow already does exactly that, so
|
|
457
532
|
// the breach reuses it rather than inventing a parallel path.
|
|
458
533
|
if (!containment.contained) {
|
|
459
534
|
record = appendEntry(
|
|
@@ -462,7 +537,7 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
462
537
|
);
|
|
463
538
|
}
|
|
464
539
|
|
|
465
|
-
await writeFile(join(options.dir, '
|
|
540
|
+
await writeFile(join(options.dir, 'LOG.md'), record, 'utf8');
|
|
466
541
|
|
|
467
542
|
// The Clerk appends after every act. A turn that produced no ledger row
|
|
468
543
|
// would be work the session cannot account for.
|
|
@@ -483,34 +558,38 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
483
558
|
);
|
|
484
559
|
}
|
|
485
560
|
|
|
486
|
-
// The slot is consumed by the
|
|
561
|
+
// The slot is consumed by the record commit, not by winning the lock.
|
|
487
562
|
claimSlot(envelope);
|
|
488
563
|
|
|
489
|
-
// Quorum is carried forward untouched. A turn is not a
|
|
564
|
+
// Quorum is carried forward untouched. A turn is not a vote, and
|
|
490
565
|
// `present` counts actors eligible to vote — it was previously being
|
|
491
566
|
// overwritten with the leftover broadcast slot count, which is a different
|
|
492
567
|
// quantity entirely and made the figure meaningless.
|
|
493
|
-
// The
|
|
494
|
-
//
|
|
495
|
-
//
|
|
496
|
-
//
|
|
568
|
+
// The session suspends on a containment breach or an agent-filed
|
|
569
|
+
// escalation — both pause pending an owner ruling, and both carry the same
|
|
570
|
+
// ESCALATION cause. A breach names no escalation file; an agent's
|
|
571
|
+
// escalation does, so its path is recorded as the escalation reference.
|
|
497
572
|
const suspend = !containment.contained || agentEscalated;
|
|
573
|
+
// Built on the post-turn state, so a suspension or quorum change an act
|
|
574
|
+
// made during the turn survives; the turn only adds its own suspension.
|
|
498
575
|
const nextState = createState({
|
|
499
|
-
session_guid:
|
|
500
|
-
protocol:
|
|
501
|
-
state: suspend ? 'SUSPENDED' :
|
|
576
|
+
session_guid: stateBase.session_guid,
|
|
577
|
+
protocol: stateBase.protocol,
|
|
578
|
+
state: suspend ? 'SUSPENDED' : stateBase.state,
|
|
502
579
|
...(suspend
|
|
503
580
|
? {
|
|
504
581
|
suspension: {
|
|
505
|
-
cause: '
|
|
582
|
+
cause: 'ESCALATION' as const,
|
|
506
583
|
since: options.now,
|
|
507
|
-
record_ref: '
|
|
584
|
+
record_ref: 'LOG.md',
|
|
508
585
|
escalation_ref: agentEscalated ? (escalationFiles[0] ?? null) : null,
|
|
509
586
|
},
|
|
510
587
|
}
|
|
511
|
-
:
|
|
512
|
-
|
|
513
|
-
|
|
588
|
+
: stateBase.suspension !== null
|
|
589
|
+
? { suspension: stateBase.suspension }
|
|
590
|
+
: {}),
|
|
591
|
+
quorum: stateBase.quorum,
|
|
592
|
+
log_head: stateBase.log_head,
|
|
514
593
|
updated_at: options.now,
|
|
515
594
|
});
|
|
516
595
|
await writeFile(join(options.dir, 'STATE.json'), serializeState(nextState), 'utf8');
|
|
@@ -518,7 +597,7 @@ export const runTurn = async (options: RunOptions): Promise<RunOutcome> => {
|
|
|
518
597
|
if (!containment.contained) {
|
|
519
598
|
return {
|
|
520
599
|
ok: false,
|
|
521
|
-
reason: `containment breach — ${containment.breaches.map((b) => b.evidence).join(', ')}; the
|
|
600
|
+
reason: `containment breach — ${containment.breaches.map((b) => b.evidence).join(', ')}; the session is suspended pending an owner ruling`,
|
|
522
601
|
entry,
|
|
523
602
|
};
|
|
524
603
|
}
|
package/src/scm.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* Design rules applied here:
|
|
10
10
|
* - Every call is async. Every provider is a network boundary.
|
|
11
11
|
* - Nothing returns a bare string. Each mutation returns a typed handle so the
|
|
12
|
-
*
|
|
12
|
+
* record writer has something attributable to record.
|
|
13
13
|
* - Provider-native identifiers stay opaque (`id`), with `url` carried
|
|
14
14
|
* alongside for the human-readable record.
|
|
15
15
|
*
|
|
@@ -26,7 +26,7 @@ import type { MergeStrategy, Role } from './canon.ts';
|
|
|
26
26
|
export interface Ref {
|
|
27
27
|
/** Opaque provider identifier. Never parsed by the engine. */
|
|
28
28
|
readonly id: string;
|
|
29
|
-
/** Human-readable location, recorded in the
|
|
29
|
+
/** Human-readable location, recorded in the log. */
|
|
30
30
|
readonly url: string;
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -41,19 +41,19 @@ export interface MotionRef extends Ref {
|
|
|
41
41
|
readonly branch: string;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
/** An ESCALATION —
|
|
44
|
+
/** An ESCALATION — an escalation. */
|
|
45
45
|
export interface EscalationRef extends Ref {
|
|
46
46
|
readonly number: number;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
/** A
|
|
50
|
-
export interface
|
|
49
|
+
/** A BALLOT requested against a MOTION. */
|
|
50
|
+
export interface BallotRef extends Ref {
|
|
51
51
|
readonly motion: number;
|
|
52
52
|
readonly reviewers: readonly string[];
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/** The enactment of a carried MOTION. */
|
|
56
|
-
export interface
|
|
56
|
+
export interface EnactResult extends Ref {
|
|
57
57
|
readonly motion: number;
|
|
58
58
|
readonly strategy: MergeStrategy;
|
|
59
59
|
readonly merged: boolean;
|
|
@@ -89,7 +89,7 @@ export interface CollaboratorGrant {
|
|
|
89
89
|
* below implements all eleven as listed in the architecture document.
|
|
90
90
|
*/
|
|
91
91
|
export interface ScmProvider {
|
|
92
|
-
/** Provider identifier, e.g. `github`. Recorded in the
|
|
92
|
+
/** Provider identifier, e.g. `github`. Recorded in the record header. */
|
|
93
93
|
readonly name: string;
|
|
94
94
|
|
|
95
95
|
/** Write files to the session repo. The underlying act of every MOTION. */
|
|
@@ -98,17 +98,17 @@ export interface ScmProvider {
|
|
|
98
98
|
/** Table a MOTION. */
|
|
99
99
|
openPR(title: string, body: string, branch: string): Promise<MotionRef>;
|
|
100
100
|
|
|
101
|
-
/** Call a
|
|
102
|
-
requestReview(pr: number, actors: readonly string[]): Promise<
|
|
101
|
+
/** Call a BALLOT on a tabled MOTION. */
|
|
102
|
+
requestReview(pr: number, actors: readonly string[]): Promise<BallotRef>;
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
|
-
*
|
|
105
|
+
* ENACT — enact a carried MOTION. The vote decides; this enacts.
|
|
106
106
|
* Callers must verify the Motion carried before invoking; providers reject
|
|
107
107
|
* strategies the Charter did not permit.
|
|
108
108
|
*/
|
|
109
|
-
merge(pr: number, strategy: MergeStrategy): Promise<
|
|
109
|
+
merge(pr: number, strategy: MergeStrategy): Promise<EnactResult>;
|
|
110
110
|
|
|
111
|
-
/** File an ESCALATION —
|
|
111
|
+
/** File an ESCALATION — an escalation. */
|
|
112
112
|
openIssue(
|
|
113
113
|
title: string,
|
|
114
114
|
body: string,
|
|
@@ -122,10 +122,10 @@ export interface ScmProvider {
|
|
|
122
122
|
category: string,
|
|
123
123
|
): Promise<Ref>;
|
|
124
124
|
|
|
125
|
-
/** Close a milestone — used by the
|
|
125
|
+
/** Close a milestone — used by the closing workflow. */
|
|
126
126
|
closeMilestone(id: string): Promise<void>;
|
|
127
127
|
|
|
128
|
-
/** Notify an actor out of band.
|
|
128
|
+
/** Notify an actor out of band. Owner email notification rides this. */
|
|
129
129
|
notify(actor: string, message: string): Promise<void>;
|
|
130
130
|
|
|
131
131
|
/** Trigger provider-side automation — the Clerk. */
|
package/src/spawn.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Agent spawning.
|
|
3
3
|
*
|
|
4
4
|
* The stateless worker cycle: spawn, execute prompt, capture result, dispose.
|
|
5
|
-
* No agent outlives its turn, and no state lives in the process — the
|
|
5
|
+
* No agent outlives its turn, and no state lives in the process — the record
|
|
6
6
|
* is the session.
|
|
7
7
|
*
|
|
8
8
|
* Kept separate from `agents.ts` so the registry, invocation shapes and prompt
|
|
@@ -58,13 +58,13 @@ const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000;
|
|
|
58
58
|
/**
|
|
59
59
|
* Spawn an agent and capture its result.
|
|
60
60
|
*
|
|
61
|
-
* Never uses a shell. The prompt carries Charter text,
|
|
61
|
+
* Never uses a shell. The prompt carries Charter text, record excerpts and
|
|
62
62
|
* arbitrary session content; passing that through a shell would make every
|
|
63
63
|
* spawn an injection site. Args go to the child directly.
|
|
64
64
|
*
|
|
65
65
|
* Rejects only on a spawn failure the OS reports (command not found, permission
|
|
66
66
|
* denied). A non-zero exit is a *result*, not an exception — an agent that
|
|
67
|
-
* fails its task has still participated, and the
|
|
67
|
+
* fails its task has still participated, and the record shows the outcome
|
|
68
68
|
* either way.
|
|
69
69
|
*/
|
|
70
70
|
export const spawnAgent = async (
|
|
@@ -139,7 +139,7 @@ export const spawnAgent = async (
|
|
|
139
139
|
/**
|
|
140
140
|
* Is this agent's command present on PATH?
|
|
141
141
|
*
|
|
142
|
-
* Used at
|
|
142
|
+
* Used at session opening to fail fast: a Charter naming a constituency whose agent
|
|
143
143
|
* is not installed should be caught before the session opens, not when the
|
|
144
144
|
* first broadcast goes unanswered.
|
|
145
145
|
*/
|
package/src/standings.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Standings — scoring a competitive protocol from the record.
|
|
3
3
|
*
|
|
4
|
-
* A tournament or league protocol runs as a series of
|
|
5
|
-
* is already in the
|
|
4
|
+
* A tournament or league protocol runs as a series of votes; who fared how
|
|
5
|
+
* is already in the record, so — like `integrity.ts` and `federation.ts` — this
|
|
6
6
|
* is a pure read over the record, not new state. A "win" is voting with the side
|
|
7
|
-
* a
|
|
7
|
+
* a vote carried; elimination is the cull an `exit: elimination` protocol
|
|
8
8
|
* records (ADR-0007). Two rankings, because two questions get asked of a
|
|
9
9
|
* tournament: who won the most (win-loss) and who lasted longest (survival).
|
|
10
10
|
*
|
|
11
11
|
* On `pairing`: Swiss / round-robin / single-elimination are 1v1-match formats,
|
|
12
|
-
* and a Politik
|
|
13
|
-
* pair. Matchmaking needs a *match primitive* the
|
|
12
|
+
* and a Politik vote is an N-actor vote on a Motion — there is no match to
|
|
13
|
+
* pair. Matchmaking needs a *match primitive* the vote model does not have;
|
|
14
14
|
* that is an additive design, not a scoring read, and is deliberately not faked
|
|
15
15
|
* here.
|
|
16
16
|
*
|
|
17
17
|
* Attribution: Steve Krisjanovs, Cordfuse
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
import { parseEntries } from './
|
|
20
|
+
import { parseEntries } from './log.ts';
|
|
21
21
|
import { seats } from './actors.ts';
|
|
22
22
|
|
|
23
23
|
export interface Standing {
|
|
24
24
|
readonly actor: string;
|
|
25
25
|
/** Current role, or null if the actor is no longer seated. */
|
|
26
26
|
readonly seat: string | null;
|
|
27
|
-
/**
|
|
27
|
+
/** votes won: voted with the side that carried (or lost, for rejected). */
|
|
28
28
|
readonly wins: number;
|
|
29
29
|
readonly losses: number;
|
|
30
30
|
readonly eliminated: boolean;
|
|
@@ -42,11 +42,11 @@ export type RankBy = 'win-loss' | 'survival';
|
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
44
|
* Score every actor who took part. A win is a vote on the carrying side of a
|
|
45
|
-
* decided
|
|
45
|
+
* decided vote; a loss is a vote on the other side; an abstention is neither.
|
|
46
46
|
* Elimination and current seat come from the actor record.
|
|
47
47
|
*/
|
|
48
|
-
export const standings = (
|
|
49
|
-
const entries = parseEntries(
|
|
48
|
+
export const standings = (log: string, by: RankBy = 'win-loss'): readonly Standing[] => {
|
|
49
|
+
const entries = parseEntries(log);
|
|
50
50
|
|
|
51
51
|
// motion -> voter -> vote, and motion -> carried?
|
|
52
52
|
const votes = new Map<string, Map<string, string>>();
|
|
@@ -110,18 +110,18 @@ export const standings = (hansard: string, by: RankBy = 'win-loss'): readonly St
|
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
// Score decided
|
|
113
|
+
// Score decided votes.
|
|
114
114
|
for (const [motion, decided] of carried) {
|
|
115
115
|
const cast = votes.get(motion);
|
|
116
116
|
if (cast === undefined) continue;
|
|
117
117
|
for (const [actor, vote] of cast) {
|
|
118
118
|
if (vote === 'ABSTAIN') continue;
|
|
119
|
-
const wonSide = (vote === '
|
|
119
|
+
const wonSide = (vote === 'YES' && decided) || (vote === 'NO' && !decided);
|
|
120
120
|
bump(wonSide ? wins : losses, actor);
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
const seated = new Map(seats(
|
|
124
|
+
const seated = new Map(seats(log).map((s) => [s.actor, s.role]));
|
|
125
125
|
|
|
126
126
|
const rows: Standing[] = [...participants].map((actor) => ({
|
|
127
127
|
actor,
|