@bridgebrew/harness-opencode 0.0.10 → 0.0.13

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 (2) hide show
  1. package/package.json +4 -4
  2. package/server.ts +45 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bridgebrew/harness-opencode",
3
- "version": "0.0.10",
3
+ "version": "0.0.13",
4
4
  "license": "MIT",
5
5
  "description": "Lets the harness run OpenCode 2, one server per session.",
6
6
  "keywords": [
@@ -56,10 +56,10 @@
56
56
  }
57
57
  }
58
58
  },
59
- "provenance": "official",
60
- "placement": "both"
59
+ "provenance": "official"
61
60
  },
62
61
  "peerDependencies": {
62
+ "@bridge/kernel": ">=0.0.1",
63
63
  "cordis": "4.0.0-rc.11"
64
64
  },
65
65
  "releaseStage": "dev",
@@ -68,6 +68,6 @@
68
68
  "typecheck": "bridge plugin check ."
69
69
  },
70
70
  "engines": {
71
- "bridge": ">=0.0.1-nightly.20261120.1"
71
+ "bridge": ">=0.0.1-nightly.20261123.1"
72
72
  }
73
73
  }
package/server.ts CHANGED
@@ -38,6 +38,7 @@ import { readFileSync } from 'node:fs';
38
38
  import { resolve } from 'node:path';
39
39
  import { pathToFileURL } from 'node:url';
40
40
  import { stripVTControlCharacters } from 'node:util';
41
+ import { tailText } from '@bridge/kernel/text';
41
42
  import { isRecord, listOf } from './json.ts';
42
43
  import type { Context } from 'cordis';
43
44
  import { ApiError, connect, located, type Api, type Event, type Fetch } from './api.ts';
@@ -507,7 +508,7 @@ const tailOf = (stderr: string) => {
507
508
  * `TypeError [ERR_…]: …`, `error: …`), else the last line that is neither a stack frame nor Node's `Node.js v…` line,
508
509
  * else its last line. Node prints its version after the error, which alone tells nothing. */
509
510
  const failureOf = (stderr: string) => {
510
- const lines = stderr
511
+ const lines = stripVTControlCharacters(stderr)
511
512
  .trim()
512
513
  .split('\n')
513
514
  .map((line) => line.trim());
@@ -581,7 +582,7 @@ export const opencodeDriver = ({
581
582
  });
582
583
  handle.onOutput((stream, chunk) => {
583
584
  if (stream === 'stderr') {
584
- stderr = (stderr + chunk).slice(-4096);
585
+ stderr = tailText(stderr + chunk, 4096);
585
586
  return;
586
587
  }
587
588
  if (!first) return;
@@ -821,6 +822,8 @@ export const opencodeDriver = ({
821
822
  delivered: Set<string>;
822
823
  /** The execution ended while a steered message waited: the turn ends with the execution that takes it. */
823
824
  idle?: TurnEnd;
825
+ /** An execution started during it. */
826
+ executed?: true;
824
827
  }
825
828
  | undefined;
826
829
  let stopTimer: ReturnType<typeof setTimeout> | undefined;
@@ -1390,6 +1393,7 @@ export const opencodeDriver = ({
1390
1393
  }
1391
1394
  case 'session.execution.started':
1392
1395
  turn.idle = undefined;
1396
+ turn.executed = true;
1393
1397
  return;
1394
1398
  case 'session.execution.succeeded':
1395
1399
  if (turn.stopping) return finish({ reason: 'interrupted' });
@@ -1600,12 +1604,42 @@ export const opencodeDriver = ({
1600
1604
  if (turn !== mine || closed) return;
1601
1605
  if (mine.stopping) return finish({ reason: 'interrupted' });
1602
1606
  const rest = slash?.[2] ?? '';
1603
- if (named?.kind === 'command')
1604
- return api.post(`/api/session/${id}/command`, {
1607
+ if (named?.kind === 'command') {
1608
+ await api.post(`/api/session/${id}/command`, {
1605
1609
  name: named.name,
1606
1610
  text: rest,
1607
1611
  ...files,
1608
1612
  });
1613
+ // OpenCode answers once the command's callback returned; one that called no model started no
1614
+ // execution, so no end event comes: the turn ends once OpenCode says the session is not active. The
1615
+ // read is tried again (`catalog.everyMs`) for `requestMs` at most.
1616
+ const deadline = Date.now() + requestMs;
1617
+ for (;;) {
1618
+ if (turn !== mine || closed || mine.executed) return;
1619
+ const active = await api
1620
+ .get('/api/session/active')
1621
+ .catch((error: unknown) => error);
1622
+ if (isRecord(active) && isRecord(active.data)) {
1623
+ if (id in active.data) return; // running: its end event ends the turn
1624
+ break;
1625
+ }
1626
+ if (Date.now() >= deadline)
1627
+ return finish({
1628
+ reason: 'failed',
1629
+ message: `OpenCode did not say whether /${named.name} ran: ${active instanceof Error ? active.message : 'no session list'}`,
1630
+ });
1631
+ await new Promise((resolve) => setTimeout(resolve, catalog.everyMs));
1632
+ }
1633
+ // An execution that ran and ended before that read still has its events on the way: they end the turn
1634
+ // first, with its own outcome. ponytail: a fixed wait (`catalog.everyMs`), not a sync with the stream.
1635
+ await new Promise((resolve) => setTimeout(resolve, catalog.everyMs));
1636
+ if (turn !== mine || closed || mine.executed) return;
1637
+ // As an execution that ended: a steered message still posting or waiting ends it later (`steer`), or
1638
+ // starts an execution, which clears it.
1639
+ mine.idle = mine.stopping ? { reason: 'interrupted' } : { reason: 'completed' };
1640
+ if (!mine.steering.size && !mine.posting) finish(mine.idle);
1641
+ return;
1642
+ }
1609
1643
  if (named?.kind === 'skill')
1610
1644
  return api.post(`/api/session/${id}/prompt`, {
1611
1645
  text: rest,
@@ -1614,14 +1648,13 @@ export const opencodeDriver = ({
1614
1648
  });
1615
1649
  return api.post(`/api/session/${id}/prompt`, { text, ...files });
1616
1650
  };
1617
- post()
1618
- .catch((error: unknown) => {
1619
- if (turn === mine && !closed)
1620
- finish({
1621
- reason: 'failed',
1622
- message: `OpenCode refused the turn: ${error instanceof Error ? error.message : String(error)}`,
1623
- });
1624
- });
1651
+ post().catch((error: unknown) => {
1652
+ if (turn === mine && !closed)
1653
+ finish({
1654
+ reason: 'failed',
1655
+ message: `OpenCode refused the turn: ${error instanceof Error ? error.message : String(error)}`,
1656
+ });
1657
+ });
1625
1658
  });
1626
1659
  },
1627
1660
  async steer(text, images = []) {