@junctionpanel/server 0.1.61 → 0.1.62

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.
@@ -29,7 +29,7 @@ import { expandTilde } from '../utils/path.js';
29
29
  import { searchHomeDirectories, searchWorkspaceEntries, searchWorkspaceEntriesAtGitRef, searchGitRepositories, checkIsGitRepo, } from '../utils/directory-suggestions.js';
30
30
  import { cloneRepository } from '../utils/git-clone.js';
31
31
  import { initRepository } from '../utils/git-init.js';
32
- import { resolveClientMessageId } from './client-message-id.js';
32
+ import { normalizeClientMessageId, resolveClientMessageId } from './client-message-id.js';
33
33
  import { deriveProjectGroupingKey, deriveProjectGroupingName } from '../shared/project-grouping.js';
34
34
  import { DEFAULT_DAEMON_PACKAGE_NAME, resolveDaemonPackageVersion, } from './daemon-package-context.js';
35
35
  import { runDaemonDoctor } from './daemon-doctor.js';
@@ -48,6 +48,8 @@ const READ_ONLY_GIT_ENV = {
48
48
  const DEFAULT_STORED_TIMELINE_FETCH_LIMIT = 200;
49
49
  const pendingAgentInitializations = new Map();
50
50
  const pendingAgentMessageExecutions = new Map();
51
+ const CREATE_AGENT_REPLAY_WINDOW_MS = 30000;
52
+ const pendingCreateAgentRequests = new Map();
51
53
  const sharedWorkspaceGitOperationStates = new Map();
52
54
  const DEFAULT_AGENT_PROVIDER = AGENT_PROVIDER_IDS[0];
53
55
  const CHECKOUT_DIFF_WATCH_DEBOUNCE_MS = 150;
@@ -59,6 +61,30 @@ const WORKSPACE_STATUS_PR_PASSIVE_REFRESH_MS = 60000;
59
61
  const TERMINAL_STREAM_WINDOW_BYTES = 256 * 1024;
60
62
  const TERMINAL_STREAM_MAX_PENDING_BYTES = 2 * 1024 * 1024;
61
63
  const TERMINAL_STREAM_MAX_PENDING_CHUNKS = 2048;
64
+ function buildCreateAgentReplayKey(clientId, clientMessageId) {
65
+ return JSON.stringify([clientId, clientMessageId]);
66
+ }
67
+ function deletePendingCreateAgentRequest(replayKey, expectedEntry) {
68
+ const existingEntry = pendingCreateAgentRequests.get(replayKey);
69
+ if (!existingEntry || (expectedEntry && existingEntry !== expectedEntry)) {
70
+ return;
71
+ }
72
+ clearTimeout(existingEntry.evictionTimer);
73
+ pendingCreateAgentRequests.delete(replayKey);
74
+ }
75
+ function storePendingCreateAgentRequest(replayKey, promise) {
76
+ const entry = {
77
+ promise,
78
+ evictionTimer: setTimeout(() => {
79
+ deletePendingCreateAgentRequest(replayKey, entry);
80
+ }, CREATE_AGENT_REPLAY_WINDOW_MS),
81
+ };
82
+ pendingCreateAgentRequests.set(replayKey, entry);
83
+ void promise.catch(() => {
84
+ deletePendingCreateAgentRequest(replayKey, entry);
85
+ });
86
+ return entry;
87
+ }
62
88
  const DIRTY_WORKTREE_CONFIRMATION_REQUIRED = 'dirty_worktree_confirmation_required';
63
89
  class SessionRequestError extends Error {
64
90
  constructor(code, message) {
@@ -1557,7 +1583,7 @@ export class Session {
1557
1583
  const archivedAt = await this.getArchivedAt(agentId);
1558
1584
  if (archivedAt) {
1559
1585
  this.handleAgentRunError(agentId, new Error(`Agent ${agentId} is archived`), 'Refusing to send prompt to archived agent');
1560
- return;
1586
+ return false;
1561
1587
  }
1562
1588
  try {
1563
1589
  await this.performAgentMessageSend({
@@ -1567,9 +1593,11 @@ export class Session {
1567
1593
  images,
1568
1594
  runOptions,
1569
1595
  });
1596
+ return true;
1570
1597
  }
1571
1598
  catch (error) {
1572
1599
  this.handleAgentRunError(agentId, error, 'Failed to send prompt to agent');
1600
+ return false;
1573
1601
  }
1574
1602
  }
1575
1603
  /**
@@ -1578,96 +1606,147 @@ export class Session {
1578
1606
  async handleCreateAgentRequest(msg) {
1579
1607
  const { config, worktreeName, requestId, initialPrompt, clientMessageId, outputSchema, initialRunOptions, git, bootstrapSetupOverride, generalPreferencesApplied, images, labels, } = msg;
1580
1608
  this.sessionLogger.info({ cwd: config.cwd, provider: config.provider, worktreeName }, `Creating agent in ${config.cwd} (${config.provider})${worktreeName ? ` with worktree ${worktreeName}` : ''}`);
1609
+ const trimmedPrompt = initialPrompt?.trim() ?? '';
1610
+ const hasInitialMessage = trimmedPrompt.length > 0 || Boolean(images?.length);
1611
+ const normalizedClientMessageId = hasInitialMessage ? normalizeClientMessageId(clientMessageId) : undefined;
1612
+ const createReplayKey = normalizedClientMessageId
1613
+ ? buildCreateAgentReplayKey(this.clientId, normalizedClientMessageId)
1614
+ : null;
1581
1615
  try {
1582
- const { sessionConfig, worktreeConfig, autoWorkspaceName } = await this.buildAgentSessionConfig(config, git, worktreeName, labels);
1583
- const mergedLabels = autoWorkspaceName
1584
- ? { ...labels, 'junction:workspace': autoWorkspaceName }
1585
- : labels;
1586
- const snapshot = await this.agentManager.createAgent(sessionConfig, undefined, {
1587
- labels: applyNotificationRelayOwnerLabel(mergedLabels, this.userId),
1588
- });
1589
- await this.forwardAgentUpdate(snapshot);
1590
- if (requestId) {
1591
- const agentPayload = await this.getAgentPayloadById(snapshot.id);
1592
- if (!agentPayload) {
1593
- throw new Error(`Agent ${snapshot.id} not found after creation`);
1594
- }
1595
- this.emit({
1596
- type: 'status',
1597
- payload: {
1598
- status: 'agent_created',
1599
- agentId: snapshot.id,
1600
- requestId,
1601
- agent: agentPayload,
1602
- },
1616
+ const createAgentLaunch = async (clearReplayEntryOnDeferredFailure) => {
1617
+ const { sessionConfig, worktreeConfig, autoWorkspaceName } = await this.buildAgentSessionConfig(config, git, worktreeName, labels);
1618
+ const mergedLabels = autoWorkspaceName
1619
+ ? { ...labels, 'junction:workspace': autoWorkspaceName }
1620
+ : labels;
1621
+ const snapshot = await this.agentManager.createAgent(sessionConfig, undefined, {
1622
+ labels: applyNotificationRelayOwnerLabel(mergedLabels, this.userId),
1603
1623
  });
1604
- }
1605
- const trimmedPrompt = initialPrompt?.trim() ?? '';
1606
- const hasInitialMessage = trimmedPrompt.length > 0 || Boolean(images?.length);
1607
- const runInitialPrompt = async () => {
1608
- if (!hasInitialMessage) {
1609
- return;
1624
+ await this.forwardAgentUpdate(snapshot);
1625
+ const runInitialPrompt = async () => {
1626
+ if (!hasInitialMessage) {
1627
+ return;
1628
+ }
1629
+ if (trimmedPrompt.length > 0) {
1630
+ scheduleAgentMetadataGeneration({
1631
+ agentManager: this.agentManager,
1632
+ agentId: snapshot.id,
1633
+ cwd: snapshot.cwd,
1634
+ initialPrompt: trimmedPrompt,
1635
+ explicitTitle: snapshot.config.title,
1636
+ junctionHome: this.junctionHome,
1637
+ logger: this.sessionLogger,
1638
+ });
1639
+ }
1640
+ const sendSucceeded = await this.handleSendAgentMessage(snapshot.id, trimmedPrompt, normalizedClientMessageId ?? resolveClientMessageId(clientMessageId), images, normalizeAgentRunOptions({
1641
+ ...(outputSchema ? { outputSchema } : {}),
1642
+ ...(initialRunOptions ?? {}),
1643
+ }));
1644
+ if (!sendSucceeded) {
1645
+ clearReplayEntryOnDeferredFailure?.();
1646
+ }
1647
+ };
1648
+ const handleInitialPromptError = (promptError) => {
1649
+ clearReplayEntryOnDeferredFailure?.();
1650
+ this.sessionLogger.error({ err: promptError, agentId: snapshot.id }, `Failed to run initial prompt for agent ${snapshot.id}`);
1651
+ this.emit({
1652
+ type: 'activity_log',
1653
+ payload: {
1654
+ id: uuidv4(),
1655
+ timestamp: new Date(),
1656
+ type: 'error',
1657
+ content: `Initial prompt failed: ${promptError?.message ?? promptError}`,
1658
+ },
1659
+ });
1660
+ };
1661
+ if (hasInitialMessage && !worktreeConfig) {
1662
+ void runInitialPrompt().catch(handleInitialPromptError);
1610
1663
  }
1611
- if (trimmedPrompt.length > 0) {
1612
- scheduleAgentMetadataGeneration({
1613
- agentManager: this.agentManager,
1664
+ if (worktreeConfig) {
1665
+ void runAsyncWorktreeBootstrap({
1614
1666
  agentId: snapshot.id,
1615
- cwd: snapshot.cwd,
1616
- initialPrompt: trimmedPrompt,
1617
- explicitTitle: snapshot.config.title,
1618
- junctionHome: this.junctionHome,
1667
+ worktree: worktreeConfig,
1668
+ setupOverride: bootstrapSetupOverride,
1669
+ generalPreferencesApplied,
1670
+ terminalManager: this.terminalManager,
1671
+ appendTimelineItem: (item) => appendTimelineItemIfAgentKnown({
1672
+ agentManager: this.agentManager,
1673
+ agentId: snapshot.id,
1674
+ item,
1675
+ }),
1676
+ emitLiveTimelineItem: (item) => emitLiveTimelineItemIfAgentKnown({
1677
+ agentManager: this.agentManager,
1678
+ agentId: snapshot.id,
1679
+ item,
1680
+ }),
1681
+ onSetupSettled: async (result) => {
1682
+ if (!hasInitialMessage || result.setupStatus === 'failed') {
1683
+ if (hasInitialMessage && result.setupStatus === 'failed') {
1684
+ clearReplayEntryOnDeferredFailure?.();
1685
+ }
1686
+ return;
1687
+ }
1688
+ await runInitialPrompt().catch(handleInitialPromptError);
1689
+ },
1619
1690
  logger: this.sessionLogger,
1691
+ }).catch((bootstrapError) => {
1692
+ clearReplayEntryOnDeferredFailure?.();
1693
+ this.sessionLogger.error({ err: bootstrapError, agentId: snapshot.id }, `Failed to bootstrap worktree for agent ${snapshot.id}`);
1620
1694
  });
1621
1695
  }
1622
- await this.handleSendAgentMessage(snapshot.id, trimmedPrompt, resolveClientMessageId(clientMessageId), images, normalizeAgentRunOptions({
1623
- ...(outputSchema ? { outputSchema } : {}),
1624
- ...(initialRunOptions ?? {}),
1625
- }));
1696
+ this.sessionLogger.info({ agentId: snapshot.id, provider: snapshot.provider }, `Created agent ${snapshot.id} (${snapshot.provider})`);
1697
+ return snapshot;
1626
1698
  };
1627
- const handleInitialPromptError = (promptError) => {
1628
- this.sessionLogger.error({ err: promptError, agentId: snapshot.id }, `Failed to run initial prompt for agent ${snapshot.id}`);
1699
+ let reusedExisting = false;
1700
+ let snapshot;
1701
+ let agentPayload;
1702
+ if (createReplayKey) {
1703
+ const existingEntry = pendingCreateAgentRequests.get(createReplayKey);
1704
+ if (existingEntry) {
1705
+ reusedExisting = true;
1706
+ snapshot = await existingEntry.promise;
1707
+ }
1708
+ else {
1709
+ reusedExisting = false;
1710
+ let clearReplayEntryOnDeferredFailure = () => { };
1711
+ const nextPromise = createAgentLaunch(() => {
1712
+ clearReplayEntryOnDeferredFailure();
1713
+ });
1714
+ const nextEntry = storePendingCreateAgentRequest(createReplayKey, nextPromise);
1715
+ clearReplayEntryOnDeferredFailure = () => {
1716
+ deletePendingCreateAgentRequest(createReplayKey, nextEntry);
1717
+ };
1718
+ try {
1719
+ snapshot = await nextPromise;
1720
+ }
1721
+ catch (error) {
1722
+ deletePendingCreateAgentRequest(createReplayKey, nextEntry);
1723
+ throw error;
1724
+ }
1725
+ }
1726
+ agentPayload = await this.getAgentPayloadById(snapshot.id);
1727
+ if (!agentPayload) {
1728
+ agentPayload = await this.buildAgentPayload(snapshot);
1729
+ }
1730
+ }
1731
+ else {
1732
+ snapshot = await createAgentLaunch();
1733
+ agentPayload = await this.getAgentPayloadById(snapshot.id);
1734
+ if (!agentPayload) {
1735
+ throw new Error(`Agent ${snapshot.id} not found after creation`);
1736
+ }
1737
+ }
1738
+ if (requestId) {
1629
1739
  this.emit({
1630
- type: 'activity_log',
1740
+ type: 'status',
1631
1741
  payload: {
1632
- id: uuidv4(),
1633
- timestamp: new Date(),
1634
- type: 'error',
1635
- content: `Initial prompt failed: ${promptError?.message ?? promptError}`,
1636
- },
1637
- });
1638
- };
1639
- if (hasInitialMessage && !worktreeConfig) {
1640
- void runInitialPrompt().catch(handleInitialPromptError);
1641
- }
1642
- if (worktreeConfig) {
1643
- void runAsyncWorktreeBootstrap({
1644
- agentId: snapshot.id,
1645
- worktree: worktreeConfig,
1646
- setupOverride: bootstrapSetupOverride,
1647
- generalPreferencesApplied,
1648
- terminalManager: this.terminalManager,
1649
- appendTimelineItem: (item) => appendTimelineItemIfAgentKnown({
1650
- agentManager: this.agentManager,
1651
- agentId: snapshot.id,
1652
- item,
1653
- }),
1654
- emitLiveTimelineItem: (item) => emitLiveTimelineItemIfAgentKnown({
1655
- agentManager: this.agentManager,
1742
+ status: 'agent_created',
1743
+ reusedExisting,
1656
1744
  agentId: snapshot.id,
1657
- item,
1658
- }),
1659
- onSetupSettled: async (result) => {
1660
- if (!hasInitialMessage || result.setupStatus === 'failed') {
1661
- return;
1662
- }
1663
- await runInitialPrompt().catch(handleInitialPromptError);
1745
+ requestId,
1746
+ agent: agentPayload,
1664
1747
  },
1665
- logger: this.sessionLogger,
1666
- }).catch((bootstrapError) => {
1667
- this.sessionLogger.error({ err: bootstrapError, agentId: snapshot.id }, `Failed to bootstrap worktree for agent ${snapshot.id}`);
1668
1748
  });
1669
1749
  }
1670
- this.sessionLogger.info({ agentId: snapshot.id, provider: snapshot.provider }, `Created agent ${snapshot.id} (${snapshot.provider})`);
1671
1750
  }
1672
1751
  catch (error) {
1673
1752
  this.sessionLogger.error({ err: error }, 'Failed to create agent');