@jacebenson/jsn 5.4.0-dev.141.1 → 5.4.0-dev.142.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacebenson/jsn",
3
- "version": "5.4.0-dev.141.1",
3
+ "version": "5.4.0-dev.142.1",
4
4
  "description": "A command-line interface for ServiceNow that follows the Unix philosophy: simple, composable, and scriptable.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -9,6 +9,7 @@ import { createFlowFromManifest, updateFlowFromManifest, readCurrentUpdateSet }
9
9
  import { alignUpdateSetCapture } from '../context.js';
10
10
  import { EXCLUDED_TRIGGER_TYPES, SUPPORTED_TRIGGER_TYPES, readTriggerCatalog, readTriggerInputDefinitions } from '../flow-triggers.js';
11
11
  import { testFlow } from '../flow-test.js';
12
+ import { releaseOwnFlowSafeEdit } from '../flow-safe-edit.js';
12
13
  import { errUsage } from '../errors.js';
13
14
  import { cancelled, pickDesignerRecord, guidedFlowCreate, guidedFlowEdit, guidedFlowTestRecord, hasDefinitionData, confirmDesignerMutation } from '../flow-designer-interactive.js';
14
15
 
@@ -24,6 +25,7 @@ const FLOW_COMMANDS = [
24
25
  { name: 'create', description: 'Create a draft flow: guided in a terminal, JSON manifest in scripts' },
25
26
  { name: 'update [identifier]', description: 'Update a draft, or a published flow whose definition reports write access (alias: edit)' },
26
27
  { name: 'doctor', description: 'Check whether this instance can publish flows at all' },
28
+ { name: 'unlock [identifier]', description: 'Release this user\'s stale safe-edit session (orphaned lease) after a failed update' },
27
29
  { name: 'delete [identifier]', description: 'Delete a flow (not implemented: use the Flow Designer UI)' },
28
30
  ];
29
31
 
@@ -46,7 +48,7 @@ function triggerGuidance(trigger) {
46
48
  };
47
49
  }
48
50
 
49
- declareCapabilities('flows', { mutationSubcommands: ['create', 'update', 'edit', 'delete', 'publish', 'test'] });
51
+ declareCapabilities('flows', { mutationSubcommands: ['create', 'update', 'edit', 'delete', 'publish', 'test', 'unlock'] });
50
52
 
51
53
  function renderChecks(checks) {
52
54
  return checks.map((c) => {
@@ -383,6 +385,31 @@ export function flowsCmd(wrap) {
383
385
  });
384
386
  }),
385
387
  })
388
+ .command({
389
+ command: 'unlock [identifier]',
390
+ describe: 'Release this user\'s stale Flow Designer safe-edit session on a flow. Recovery for an orphaned lease left by a failed update: the provider only releases the calling user\'s own session, the before/after canUserEdit and currentEditor values are printed, and an unconfirmed release fails instead of reporting success. Does not edit content, publish, or touch another user\'s session.',
391
+ builder: (y) => y
392
+ .option('force', { type: 'boolean', default: false, describe: 'Skip confirmation' }),
393
+ handler: wrap(async (argv, app) => {
394
+ app.requireInstance();
395
+ const identifier = await pickFlow(argv, app);
396
+ if (identifier === undefined) return;
397
+ const sysID = unwrapSysId(await resolveRecord(app.sdk, {
398
+ table: 'sys_hub_flow', identifier, matchField: 'name', resource: 'Flow',
399
+ }));
400
+ await confirmDesignerMutation(argv, app, `Release your Flow Designer safe-edit session on flow ${identifier}? If this flow is open in Flow Designer, that editor's next save will fail.`);
401
+ const result = await releaseOwnFlowSafeEdit(app.sdk, app.getEffectiveInstance(), sysID);
402
+ const state = status => `canUserEdit=${status.canUserEdit}, currentEditor=${JSON.stringify(status.currentEditor ?? null)}`;
403
+ return app.ok({ ...result, flow: { id: sysID, name: identifier } }, {
404
+ summary: result.released
405
+ ? `Released this user's safe edit session on flow ${identifier} (${sysID}): ${state(result.before)} -> ${state(result.after)}`
406
+ : `No safe edit session to release on flow ${identifier} (${sysID}): ${state(result.before)}`,
407
+ breadcrumbs: result.released
408
+ ? [{ action: 'update', cmd: `jsn flows update ${sysID}`, description: 'Retry the edit' }]
409
+ : [{ action: 'show', cmd: `jsn flows show ${sysID}`, description: 'Inspect the flow' }],
410
+ });
411
+ }),
412
+ })
386
413
  .command({
387
414
  command: 'delete [identifier]',
388
415
  describe: 'Delete a flow (not yet implemented)',
@@ -13,15 +13,36 @@ export async function flowSafeEditRequest(sdk, instance, flowId, operation, last
13
13
  return result;
14
14
  }
15
15
 
16
+ export async function readFlowSafeEditStatus(sdk, instance, flowId) {
17
+ const { status } = await flowSafeEditRequest(sdk, instance, flowId, 'read');
18
+ if (!status || typeof status.canUserEdit !== 'boolean' || !(status.currentEditor == null || typeof status.currentEditor === 'string')) throw new Error('Unreadable safe edit status');
19
+ return status;
20
+ }
21
+
22
+ // The provider's `delete` only ever releases the calling user's own session, so
23
+ // recovery needs no ownership guess. It stays explicit anyway: releasing a lease
24
+ // that a live Flow Designer tab owns makes that editor's next save fail.
25
+ export async function releaseOwnFlowSafeEdit(sdk, instance, flowId) {
26
+ const before = await readFlowSafeEditStatus(sdk, instance, flowId);
27
+ const describe = status => `canUserEdit=${status.canUserEdit}, currentEditor=${JSON.stringify(status.currentEditor ?? null)}`;
28
+ if (!before.canUserEdit && !before.currentEditor) return { released: false, before, after: before };
29
+ const { deleteResult } = await flowSafeEditRequest(sdk, instance, flowId, 'delete');
30
+ if (deleteResult?.deleteSuccess !== true || deleteResult.id !== flowId) {
31
+ throw new Error(`Flow ${flowId} safe edit release was not confirmed by the provider (deleteSuccess=${JSON.stringify(deleteResult?.deleteSuccess ?? null)}, id=${JSON.stringify(deleteResult?.id ?? null)}); no retry attempted. Status before release: ${describe(before)}.`);
32
+ }
33
+ const after = await readFlowSafeEditStatus(sdk, instance, flowId);
34
+ if (after.canUserEdit || after.currentEditor) throw new Error(`Flow ${flowId} release was acknowledged, but readback still reports an edit session (${describe(after)}). Inspect the exact flow and Flow Designer session before retrying.`);
35
+ return { released: true, before, after };
36
+ }
37
+
16
38
  export async function withFlowSafeEdit(sdk, instance, flowId, lastUpdated, operation) {
17
39
  if (typeof lastUpdated !== 'string' || !lastUpdated) throw new Error(`Flow ${flowId} has no authoritative updated timestamp; safe edit refused`);
18
- const read = async () => {
19
- const { status } = await flowSafeEditRequest(sdk, instance, flowId, 'read');
20
- if (!status || typeof status.canUserEdit !== 'boolean' || !(status.currentEditor == null || typeof status.currentEditor === 'string')) throw new Error('Unreadable safe edit status');
21
- return status;
22
- };
40
+ const read = () => readFlowSafeEditStatus(sdk, instance, flowId);
23
41
  const before = await read();
24
- if (before.canUserEdit || before.currentEditor) throw new Error(`Flow ${flowId} already has an existing edit session/editor; close it in Flow Designer before retrying. No lock changed.`);
42
+ // Refuse any reported session, including one this same user holds: releasing
43
+ // it would close a Flow Designer tab this CLI never opened, so recovery stays
44
+ // an explicit command (`jsn flows unlock`) rather than an implicit takeover.
45
+ if (before.canUserEdit || before.currentEditor) throw new Error(`Flow ${flowId} already has an existing edit session/editor (canUserEdit=${before.canUserEdit}, currentEditor=${JSON.stringify(before.currentEditor ?? null)}); close it in Flow Designer before retrying, or release your own stale session with \`jsn flows unlock ${flowId}\`. No lock changed.`);
25
46
  let status;
26
47
  try {
27
48
  ({ status } = await flowSafeEditRequest(sdk, instance, flowId, 'upsert', lastUpdated));